nilang 0.4.1

A scripting language interpreter for Advent of Code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
mod expr;
mod lexer;
mod stmt;
mod value;
mod error;
mod token;

use crate::spanned::{Spanned, Span};
pub use expr::{Expr, LhsExpr};
pub use token::{Ctrl, KeyWord, Token};
pub use lexer::Lexer;
pub use stmt::Stmt;
pub use value::{MapKey, Value};
pub use error::{ParseError, ParseErrorItem};

use stmt::stmt;

use super::symbol_map::{SymID, SymbolMap};

pub use value::{SegmentedString, StringSegment};

use core::cell::RefCell;
use alloc::rc::Rc;

pub fn parse_program(
    input: &str,
    syms: &mut SymbolMap,
    path: Option<&String>
) -> Result<Vec<Stmt>, ParseError> {
    (stmt()
        .recover(Ctrl::SemiColon)
        .expect("Expected a statement"))
        .unless(ctrl(Ctrl::End))
        .zero_or_more()
        .parse_str(input, syms)
        // This unwrap is a little weird, but can be done b/c "zero_or_more"
        // always returns Some(vec) but vec might be empty
        .map(|result| result.unwrap())
        .map_err(|mut err| {
            err.set_path(path); 
            err
        })
}

struct ParseContext<'a> {
    lexer: Lexer<'a>,
    syms: &'a mut SymbolMap,
    errors: Vec<Spanned<ParseErrorItem>>,
    //warnings: Vec<Spanned<()>>,
    is_in_loop: bool,
}

impl<'a> ParseContext<'a> {
    fn peek(&mut self) -> Option<Spanned<Token>> {
        match self.lexer.peek(self.syms) {
            Err(lex_error) => {
                let parse_error = lex_error.map(ParseErrorItem::LexError);
                self.adv();
                self.add_err(parse_error);
                None
            }
            Ok(spanned_token) => Some(spanned_token),
        }
    }

    fn adv(&mut self) {
        let _ = self.lexer.get_token(self.syms);
    }

    fn peek_one_ahead(&mut self) -> Option<Spanned<Token>> {
        self.lexer.peek_nth(1, self.syms).ok()
    }

    fn add_err(&mut self, err: Spanned<ParseErrorItem>) {
        self.errors.push(err);
    }

    fn pos(&self) -> usize {
        // TODO: This function is only used in one place, and instead of 
        // getting the current position of the lexer, I think we actually want
        // the last parsed tokens span.end
        self.lexer.pos()
    }

    fn eof(&self) -> bool {
        self.lexer.eof()
    }
}

#[derive(Clone)]
struct Parser<'a, T> {
    func: Rc<dyn Fn(&mut ParseContext<'a>) -> Option<T> + 'a>,
}

impl<'a, T: 'a> Parser<'a, T> {
    pub fn new(func: impl Fn(&mut ParseContext<'a>) -> Option<T> + 'a) -> Self {
        Self {
            func: Rc::new(func),
        }
    }

    pub fn parse_str(self, input: &'a str, syms: &'a mut SymbolMap) -> Result<Option<T>, ParseError> {
        let lexer = Lexer::new(input);

        let mut ctx = ParseContext {
            lexer,
            syms,
            errors: vec![],
            //warnings: vec![],
            is_in_loop: false,
        };

        let value = self.parse(&mut ctx);

        if ctx.errors.is_empty() {
            Ok(value)
        } else {
            Err(ParseError::new(ctx.errors, None))
        }
    }

    fn parse(&self, ctx: &mut ParseContext<'a>) -> Option<T> {
        (self.func)(ctx)
    }

    pub fn then<A: 'a>(self, then: Parser<'a, A>) -> Parser<'a, A> {
        Parser::new(move |ctx: &mut ParseContext<'a>| {
            if self.parse(ctx).is_some() {
                then.parse(ctx)
            } else {
                None
            }
        })
    }

    pub fn append<A: 'a>(self, then: Parser<'a, A>) -> Parser<'a, (T, A)> {
        Parser::new(move |ctx: &mut ParseContext<'a>| {
            let first = self.parse(ctx)?;
            let second = then.parse(ctx)?;

            Some((first, second))
        })
    }

    pub fn closed_by<A: 'a>(self, then: Parser<'a, A>) -> Parser<'a, T> {
        Parser::new(move |ctx: &mut ParseContext<'a>| {
            let first = self.parse(ctx)?;
            let _ = then.parse(ctx)?;

            Some(first)
        })
    }

    pub fn expect(self, error_msg: &'static str) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            if let Some(result) = self.parse(ctx) {
                Some(result)
            } else {
                let span = ctx.peek()?.span;
                let error = ParseErrorItem::Expected {
                    msg: error_msg.to_string(),
                    found: ctx.lexer.get_input()[span.start..span.end].to_string(),
                };

                ctx.add_err(Spanned::new(error, span));
                None
            }
        })
    }

    pub fn or(self, alternative: Parser<'a, T>) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            if let Some(value) = self.parse(ctx) {
                Some(value)
            } else {
                alternative.parse(ctx)
            }
        })
    }

    pub fn unless<A: 'a>(self, alternative: Parser<'a, A>) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            if alternative.parse(ctx).is_some() {
                None
            } else {
                self.parse(ctx)
            }
        })
    }

    pub fn looping(self, is_looping: bool) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            let prev_loop_state = ctx.is_in_loop;
            ctx.is_in_loop = is_looping;

            let result = self.parse(ctx)?;

            ctx.is_in_loop = prev_loop_state;

            Some(result)
        })
    }

    pub fn recover(self, ctrl_recover: Ctrl) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            if let Some(value) = self.parse(ctx) {
                Some(value)
            } else {

                loop {
                    if ctx.eof() {
                        break;
                    }

                    match ctrl(ctrl_recover).parse(ctx) {
                        Some(_) => {
                            if let Some(value) = self.parse(ctx) {
                                return Some(value)
                            }
                        }
                        None => ctx.adv(),
                    }
                }

                None
            }
        })
    }

    pub fn zero_or_more(self) -> Parser<'a, Vec<T>> {
        Parser::new(move |ctx| {
            let mut values = vec![];

            while let Some(value) = self.parse(ctx) {
                values.push(value);
            }

            Some(values)
        })
    }

    pub fn expect_looped(self) -> Parser<'a, T> {
        let p = self.spanned();

        Parser::new(move |ctx| {
            let value = p.parse(ctx)?;
            let span = value.span;

            if !ctx.is_in_loop {
                let error = ParseErrorItem::Expected {
                    msg: "Item not contained inside a loop".to_string(),
                    found: ctx.lexer.get_input()[span.start..span.end].to_string(),
                };

                ctx.add_err(Spanned::new(error, span));
            }

            Some(value.item)
        })
    }

    pub fn map<C: 'a, N: 'a>(self, callback: C) -> Parser<'a, N>
    where
        C: Fn(T) -> N,
    {
        Parser::new(move |ctx| self.parse(ctx).map(&callback))
    }

    pub fn mix<B: 'a, C: 'a, D: 'a>(self, other: Parser<'a, B>, callback: C) -> Parser<'a, D>
    where
        C: Fn(T, Option<B>) -> Option<D>,
    {
        Parser::new(move |ctx| {
            if let Some(result) = self.parse(ctx) {
                callback(result, other.parse(ctx))
            } else {
                None
            }
        })
    }

    pub fn spanned(self) -> Parser<'a, Spanned<T>> {
        span(self)
    }

    pub fn delimited<A: 'a, B: 'a>(
        self,
        left: Parser<'a, A>,
        right: Parser<'a, B>,
    ) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            let _ = left.parse(ctx)?;
            let result = self.parse(ctx)?;
            let _ = right.parse(ctx)?;

            Some(result)
        })
    }

    pub fn delimited_list<A: 'a>(self, delimiter: Parser<'a, A>) -> Parser<'a, Vec<T>> {
        Parser::new(move |ctx| {
            let mut items = vec![];

            let first = self.parse(ctx)?;
            items.push(first);

            loop {
                if delimiter.parse(ctx).is_some() {
                    if let Some(next) = self.parse(ctx) {
                        items.push(next);
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }

            Some(items)
        })
    }

    /*
    pub fn debug(self, msg: &'static str) -> Parser<'a, T> {
        Parser::new(move |ctx| {
            println!("{}", msg);

            self.parse(ctx)
        })
    }
    */
}

fn nothing<'a>() -> Parser<'a, ()> {
    Parser::new(move |_| Some(()))
}

fn ctrl<'a>(expected: Ctrl) -> Parser<'a, ()> {
    Parser::new(move |ctx| match ctx.peek() {
        Some(spanned_token) => match spanned_token.item {
            Token::Ctrl(ctrl) if ctrl == expected => {
                ctx.adv();
                Some(())
            }
            _ => None,
        },
        None => None,
    })
}

fn keyword<'a>(expected: KeyWord) -> Parser<'a, ()> {
    Parser::new(move |ctx| match ctx.peek() {
        Some(spanned_token) => match spanned_token.item {
            Token::KeyWord(keyword) if keyword == expected => {
                ctx.adv();
                Some(())
            }
            _ => None,
        },
        None => None,
    })
}

fn symbol<'a>() -> Parser<'a, SymID> {
    Parser::new(|ctx| match ctx.peek() {
        Some(spanned_token) => match spanned_token.item {
            Token::Ident(sym_id) => {
                ctx.adv();
                Some(sym_id)
            }
            _ => None,
        },
        None => None,
    })
}

fn inputs<'a>() -> Parser<'a, Spanned<Vec<SymID>>> {
    let left_paren = ctrl(Ctrl::LeftParen);
    let right_paren = ctrl(Ctrl::RightParen).expect("Expected ')', found something else");
    let parsed_args = inner_inputs().or(nothing().map(|_| vec![])).spanned();

    parsed_args.delimited(left_paren, right_paren)
}

fn inner_inputs<'a>() -> Parser<'a, Vec<SymID>> {
    Parser::new(move |ctx| {
        let symbol = symbol().spanned();
        let comma = ctrl(Ctrl::Comma);
        let mut items = vec![];

        let first = symbol.parse(ctx)?;
        items.push(first.item);

        loop {
            if comma.parse(ctx).is_some() {
                if let Some(next) = symbol.parse(ctx) {
                    if items.contains(&next.item) {
                        let error = ParseErrorItem::DuplicateArgs;

                        ctx.add_err(Spanned::new(error, next.span));
                    }

                    items.push(next.item);
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        Some(items)
    })
}

fn block(sp: Parser<'_, Stmt>) -> Parser<'_, Vec<Stmt>> {
    let left_curly = ctrl(Ctrl::LeftCurly);
    let right_curly = ctrl(Ctrl::RightCurly);
    let items = sp.zero_or_more();

    items.delimited(left_curly, right_curly)
}

fn recursive<'a, T>(func: impl Fn(Parser<'a, T>) -> Parser<'a, T> + 'a) -> Parser<'a, T>
where
    T: 'a,
{
    let recursive_parser: Rc<RefCell<Option<Parser<'a, T>>>> = Rc::new(RefCell::new(None));
    let recursive_parser_clone = recursive_parser.clone();

    let parser = Parser::new(move |ctx| {
        if recursive_parser_clone.borrow().is_none() {
            let rec_parser = func(Parser::new({
                let recursive_parser_inner = recursive_parser_clone.clone();
                move |ctx| recursive_parser_inner.borrow().as_ref().unwrap().parse(ctx)
            }));
            *recursive_parser_clone.borrow_mut() = Some(rec_parser);
        }
        recursive_parser_clone.borrow().as_ref().unwrap().parse(ctx)
    });

    parser
}

fn span<'a, T: 'a>(func: Parser<'a, T>) -> Parser<'a, Spanned<T>> {
    Parser::new(move |ctx| {
        let start = ctx.peek()?.span.start;
        let result: Option<T> = func.parse(ctx);
        let end = ctx.pos();

        result.map(|value| Spanned::new(value, Span::new(start, end)))
    })
}

#[cfg(test)]
mod tests {
    use crate::parser::ParseError;
    use super::*;
    use crate::symbol_map::SymbolMap;

    fn parse_program_with_syms(input: &str, syms: &mut SymbolMap) -> Result<Vec<Stmt>, ParseError> {
        let result = parse_program(input, syms, None);
        result
    }

    #[test]
    fn bad_method_chaining() {
        let mut syms = SymbolMap::new();
        let input = ".foo;";
        let result = parse_program_with_syms(input, &mut syms);

        assert!(result.is_err());
    }

    #[test]
    fn unclosed_curly() {
        let mut syms = SymbolMap::new();
        let input = ";}";
        let result = parse_program_with_syms(input, &mut syms);

        assert!(result.is_err());
    }
}