web2md 0.1.2

A tool that fetches web pages and returns them as Markdown for MCP token efficiency
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
//! Recursive-descent parser for the built-in JavaScript subset interpreter.
//!
//! Produces a `Vec<Stmt>` AST. Supports a pragmatic subset: variable
//! declarations, assignment, if/else, for, for-of, while, function
//! declarations, return/break/continue, throw, blocks, and a full expression
//! grammar with precedence climbing.

use super::ast::*;
use super::lexer::{tokenize, Token, TmplTokenPart};

#[derive(Debug, Clone)]
pub struct ParseError(pub String);

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "parse error: {}", self.0)
    }
}
impl std::error::Error for ParseError {}

type PResult<T> = Result<T, ParseError>;

pub struct Parser {
    toks: Vec<Token>,
    pos: usize,
}

impl Parser {
    pub fn new(src: &str) -> PResult<Self> {
        let toks = tokenize(src).map_err(|e| ParseError(e.to_string()))?;
        Ok(Self { toks, pos: 0 })
    }

    pub fn parse_program(&mut self) -> PResult<Vec<Stmt>> {
        let mut out = Vec::new();
        while !self.is_eof() {
            // Allow stray semicolons at top level.
            if self.peek_punct(";") {
                self.advance();
                continue;
            }
            out.push(self.parse_stmt()?);
        }
        Ok(out)
    }

    fn is_eof(&self) -> bool {
        matches!(self.toks.get(self.pos), Some(Token::Eof) | None)
    }

    fn peek(&self) -> &Token {
        self.toks.get(self.pos).unwrap_or(&Token::Eof)
    }

    #[allow(dead_code)]
    fn peek_n(&self, n: usize) -> &Token {
        self.toks.get(self.pos + n).unwrap_or(&Token::Eof)
    }

    fn advance(&mut self) -> Token {
        let t = self.toks.get(self.pos).cloned().unwrap_or(Token::Eof);
        if !matches!(t, Token::Eof) {
            self.pos += 1;
        }
        t
    }

    fn peek_punct(&self, p: &str) -> bool {
        matches!(self.peek(), Token::Punct(x) if *x == p)
    }

    fn eat_punct(&mut self, p: &str) -> bool {
        if self.peek_punct(p) {
            self.advance();
            true
        } else {
            false
        }
    }

    fn expect_punct(&mut self, p: &str) -> PResult<()> {
        if self.eat_punct(p) {
            Ok(())
        } else {
            Err(ParseError(format!("expected `{}`, got {:?}", p, self.peek())))
        }
    }

    fn peek_kw(&self, k: &str) -> bool {
        matches!(self.peek(), Token::Keyword(s) if s == k)
    }

    fn eat_kw(&mut self, k: &str) -> bool {
        if self.peek_kw(k) {
            self.advance();
            true
        } else {
            false
        }
    }

    fn expect_kw(&mut self, k: &str) -> PResult<()> {
        if self.eat_kw(k) {
            Ok(())
        } else {
            Err(ParseError(format!("expected `{}`, got {:?}", k, self.peek())))
        }
    }

    fn parse_stmt(&mut self) -> PResult<Stmt> {
        match self.peek() {
            Token::Keyword(k) => match k.as_str() {
                "var" | "let" | "const" => self.parse_var(),
                "if" => self.parse_if(),
                "for" => self.parse_for(),
                "while" => self.parse_while(),
                "function" => self.parse_func_decl(),
                "return" => self.parse_return(),
                "break" => {
                    self.advance();
                    self.eat_punct(";");
                    Ok(Stmt::Break)
                }
                "continue" => {
                    self.advance();
                    self.eat_punct(";");
                    Ok(Stmt::Continue)
                }
                "throw" => {
                    self.advance();
                    let e = self.parse_expr()?;
                    self.eat_punct(";");
                    Ok(Stmt::Throw(e))
                }
                _ => self.parse_expr_or_labeled(),
            },
            Token::Punct("{") => {
                self.advance();
                let body = self.parse_block_body()?;
                Ok(Stmt::Block(body))
            }
            Token::Punct(";") => {
                self.advance();
                Ok(Stmt::Empty)
            }
            _ => self.parse_expr_stmt(),
        }
    }

    fn parse_expr_or_labeled(&mut self) -> PResult<Stmt> {
        // No labeled-loop support; fall through to expression statement.
        self.parse_expr_stmt()
    }

    fn parse_expr_stmt(&mut self) -> PResult<Stmt> {
        let e = self.parse_expr()?;
        self.eat_punct(";");
        Ok(Stmt::Expr(e))
    }

    fn parse_block_body(&mut self) -> PResult<Vec<Stmt>> {
        let mut out = Vec::new();
        while !self.peek_punct("}") && !self.is_eof() {
            out.push(self.parse_stmt()?);
        }
        self.expect_punct("}")?;
        Ok(out)
    }

    fn parse_var(&mut self) -> PResult<Stmt> {
        let kind = match self.advance() {
            Token::Keyword(k) => match k.as_str() {
                "var" => VarKind::Var,
                "let" => VarKind::Let,
                "const" => VarKind::Const,
                _ => unreachable!(),
            },
            _ => unreachable!(),
        };
        let mut decls = Vec::new();
        loop {
            let name = self.parse_ident()?;
            let init = if self.eat_punct("=") {
                Some(self.parse_assign()?)
            } else {
                None
            };
            decls.push((name, init));
            if !self.eat_punct(",") {
                break;
            }
        }
        self.eat_punct(";");
        Ok(Stmt::Var { kind, decls })
    }

    fn parse_ident(&mut self) -> PResult<String> {
        match self.advance() {
            Token::Ident(s) => Ok(s),
            other => Err(ParseError(format!("expected identifier, got {:?}", other))),
        }
    }

    fn parse_if(&mut self) -> PResult<Stmt> {
        self.expect_kw("if")?;
        self.expect_punct("(")?;
        let cond = self.parse_expr()?;
        self.expect_punct(")")?;
        let then = Box::new(self.parse_stmt()?);
        let else_ = if self.eat_kw("else") {
            Some(Box::new(self.parse_stmt()?))
        } else {
            None
        };
        Ok(Stmt::If {
            cond,
            then,
            else_,
        })
    }

    fn parse_for(&mut self) -> PResult<Stmt> {
        self.expect_kw("for")?;
        self.expect_punct("(")?;
        // Distinguish for-of: for (var x of y) / for (x of y)
        let saved = self.pos;
        let is_var_decl = self.peek_kw("var") || self.peek_kw("let") || self.peek_kw("const");
        if is_var_decl {
            let _kind_kw = match self.advance() {
                Token::Keyword(k) => k,
                _ => unreachable!(),
            };
            let name = self.parse_ident()?;
            if self.eat_kw("of") || self.eat_kw("in") {
                let iter = self.parse_expr()?;
                self.expect_punct(")")?;
                let body = Box::new(self.parse_stmt()?);
                return Ok(Stmt::ForOf { name, iter, body });
            }
            // Regular for with var init: rewind by re-parsing as declaration head.
            self.pos = saved;
        } else if matches!(self.peek(), Token::Ident(_)) {
            // possible: for (x of y)
            let name = match self.advance() {
                Token::Ident(s) => s,
                _ => unreachable!(),
            };
            if self.eat_kw("of") || self.eat_kw("in") {
                let iter = self.parse_expr()?;
                self.expect_punct(")")?;
                let body = Box::new(self.parse_stmt()?);
                return Ok(Stmt::ForOf { name, iter, body });
            }
            self.pos = saved;
        }

        // Classic for ( init? ; cond? ; update? )
        let init: Option<Box<Stmt>> = if self.peek_punct(";") {
            None
        } else if self.peek_kw("var") || self.peek_kw("let") || self.peek_kw("const") {
            Some(Box::new(self.parse_var()?))
        } else {
            let e = self.parse_expr()?;
            self.eat_punct(";");
            Some(Box::new(Stmt::Expr(e)))
        };
        if !matches!(init, Some(ref b) if matches!(**b, Stmt::Var { .. })) {
            self.eat_punct(";");
        }
        let cond = if self.peek_punct(";") {
            None
        } else {
            Some(self.parse_expr()?)
        };
        self.expect_punct(";")?;
        let update = if self.peek_punct(")") {
            None
        } else {
            Some(self.parse_expr()?)
        };
        self.expect_punct(")")?;
        let body = Box::new(self.parse_stmt()?);
        Ok(Stmt::For {
            init,
            cond,
            update,
            body,
        })
    }

    fn parse_while(&mut self) -> PResult<Stmt> {
        self.expect_kw("while")?;
        self.expect_punct("(")?;
        let cond = self.parse_expr()?;
        self.expect_punct(")")?;
        let body = Box::new(self.parse_stmt()?);
        Ok(Stmt::While { cond, body })
    }

    fn parse_func_decl(&mut self) -> PResult<Stmt> {
        self.expect_kw("function")?;
        let name = self.parse_ident()?;
        self.expect_punct("(")?;
        let params = self.parse_params()?;
        self.expect_punct("{")?;
        let body = self.parse_block_body()?;
        Ok(Stmt::Func { name, params, body })
    }

    fn parse_params(&mut self) -> PResult<Vec<String>> {
        let mut params = Vec::new();
        if !self.peek_punct(")") {
            loop {
                if self.peek_punct(")") {
                    break;
                }
                // allow default values and rest; we ignore them gracefully
                if self.eat_punct("...") {
                    let _ = self.parse_ident()?;
                    params.push("rest".to_string());
                } else {
                    let name = self.parse_ident()?;
                    if self.eat_punct("=") {
                        let _ = self.parse_assign()?;
                    }
                    params.push(name);
                }
                if !self.eat_punct(",") {
                    break;
                }
            }
        }
        self.expect_punct(")")?;
        Ok(params)
    }

    fn parse_return(&mut self) -> PResult<Stmt> {
        self.expect_kw("return")?;
        let val = if self.peek_punct(";") || self.peek_punct("}") || self.is_eof() {
            None
        } else {
            Some(self.parse_expr()?)
        };
        self.eat_punct(";");
        Ok(Stmt::Return(val))
    }

    // ===== Expressions =====

    fn parse_expr(&mut self) -> PResult<Expr> {
        // comma operator -> Seq
        let first = self.parse_assign()?;
        if self.peek_punct(",") {
            let mut items = vec![first];
            while self.eat_punct(",") {
                items.push(self.parse_assign()?);
            }
            Ok(Expr::Seq(items))
        } else {
            Ok(first)
        }
    }

    fn parse_assign(&mut self) -> PResult<Expr> {
        let left = self.parse_ternary()?;
        let op = match self.peek() {
            Token::Punct("=") => Some(AssignOp::Assign),
            Token::Punct("+=") => Some(AssignOp::AddAssign),
            Token::Punct("-=") => Some(AssignOp::SubAssign),
            Token::Punct("*=") => Some(AssignOp::MulAssign),
            Token::Punct("/=") => Some(AssignOp::DivAssign),
            Token::Punct("%=") => Some(AssignOp::ModAssign),
            _ => None,
        };
        if let Some(op) = op {
            self.advance();
            let right = self.parse_assign()?;
            Ok(Expr::Assign(Box::new(left), op, Box::new(right)))
        } else {
            Ok(left)
        }
    }

    fn parse_ternary(&mut self) -> PResult<Expr> {
        let cond = self.parse_logical_or()?;
        if self.eat_punct("?") {
            let then = self.parse_assign()?;
            self.expect_punct(":")?;
            let else_ = self.parse_assign()?;
            Ok(Expr::Ternary(
                Box::new(cond),
                Box::new(then),
                Box::new(else_),
            ))
        } else {
            Ok(cond)
        }
    }

    fn parse_logical_or(&mut self) -> PResult<Expr> {
        let mut left = self.parse_logical_and()?;
        while self.peek_punct("||") {
            self.advance();
            let right = self.parse_logical_and()?;
            left = Expr::Logical(LogOp::Or, Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn parse_logical_and(&mut self) -> PResult<Expr> {
        let mut left = self.parse_equality()?;
        while self.peek_punct("&&") {
            self.advance();
            let right = self.parse_equality()?;
            left = Expr::Logical(LogOp::And, Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn parse_equality(&mut self) -> PResult<Expr> {
        let mut left = self.parse_relational()?;
        loop {
            let op = match self.peek() {
                Token::Punct("==") => Some(BinOp::Eq),
                Token::Punct("!=") => Some(BinOp::Ne),
                Token::Punct("===") => Some(BinOp::StrictEq),
                Token::Punct("!==") => Some(BinOp::StrictNe),
                _ => None,
            };
            if let Some(op) = op {
                self.advance();
                let right = self.parse_relational()?;
                left = Expr::Binary(op, Box::new(left), Box::new(right));
            } else {
                break;
            }
        }
        Ok(left)
    }

    fn parse_relational(&mut self) -> PResult<Expr> {
        let mut left = self.parse_additive()?;
        loop {
            let op = match self.peek() {
                Token::Punct("<") => Some(BinOp::Lt),
                Token::Punct(">") => Some(BinOp::Gt),
                Token::Punct("<=") => Some(BinOp::Le),
                Token::Punct(">=") => Some(BinOp::Ge),
                _ => None,
            };
            if let Some(op) = op {
                self.advance();
                let right = self.parse_additive()?;
                left = Expr::Binary(op, Box::new(left), Box::new(right));
            } else {
                break;
            }
        }
        Ok(left)
    }

    fn parse_additive(&mut self) -> PResult<Expr> {
        let mut left = self.parse_multiplicative()?;
        loop {
            let op = match self.peek() {
                Token::Punct("+") => Some(BinOp::Add),
                Token::Punct("-") => Some(BinOp::Sub),
                _ => None,
            };
            if let Some(op) = op {
                self.advance();
                let right = self.parse_multiplicative()?;
                left = Expr::Binary(op, Box::new(left), Box::new(right));
            } else {
                break;
            }
        }
        Ok(left)
    }

    fn parse_multiplicative(&mut self) -> PResult<Expr> {
        let mut left = self.parse_exponent()?;
        loop {
            let op = match self.peek() {
                Token::Punct("*") => Some(BinOp::Mul),
                Token::Punct("/") => Some(BinOp::Div),
                Token::Punct("%") => Some(BinOp::Mod),
                _ => None,
            };
            if let Some(op) = op {
                self.advance();
                let right = self.parse_exponent()?;
                left = Expr::Binary(op, Box::new(left), Box::new(right));
            } else {
                break;
            }
        }
        Ok(left)
    }

    fn parse_exponent(&mut self) -> PResult<Expr> {
        let base = self.parse_unary()?;
        if self.peek_punct("**") {
            self.advance();
            let exp = self.parse_exponent()?; // right-assoc
            Ok(Expr::Binary(BinOp::Mul, Box::new(base), Box::new(exp)))
        } else {
            Ok(base)
        }
    }

    fn parse_unary(&mut self) -> PResult<Expr> {
        match self.peek() {
            Token::Punct("!") => {
                self.advance();
                Ok(Expr::Unary(UnOp::Not, Box::new(self.parse_unary()?)))
            }
            Token::Punct("-") => {
                self.advance();
                Ok(Expr::Unary(UnOp::Neg, Box::new(self.parse_unary()?)))
            }
            Token::Punct("+") => {
                self.advance();
                let operand = self.parse_unary()?;
                // unary + coerces to number; emulate via double negation.
                Ok(Expr::Unary(
                    UnOp::Neg,
                    Box::new(Expr::Unary(UnOp::Neg, Box::new(operand))),
                ))
            }
            Token::Punct("++") | Token::Punct("--") => {
                let op = match self.advance() {
                    Token::Punct("++") => AssignOp::AddAssign,
                    _ => AssignOp::SubAssign,
                };
                let target = self.parse_unary()?;
                Ok(Expr::Assign(
                    Box::new(target),
                    op,
                    Box::new(Expr::Number(1.0)),
                ))
            }
            Token::Keyword(k) if k == "typeof" => {
                self.advance();
                Ok(Expr::Unary(UnOp::Typeof, Box::new(self.parse_unary()?)))
            }
            Token::Keyword(k) if k == "void" => {
                self.advance();
                let _ = self.parse_unary()?;
                Ok(Expr::Undefined)
            }
            Token::Keyword(k) if k == "new" => self.parse_new(),
            _ => self.parse_postfix(),
        }
    }

    fn parse_new(&mut self) -> PResult<Expr> {
        self.expect_kw("new")?;
        // Parse a member/call chain; if there is a call, mark as New.
        let callee = self.parse_call_member(true)?;
        Ok(callee)
    }

    fn parse_postfix(&mut self) -> PResult<Expr> {
        let e = self.parse_call_member(false)?;
        // postfix ++/--: emit an assignment so loops increment correctly.
        // (Returns the new value rather than the old; acceptable for the subset.)
        if self.peek_punct("++") || self.peek_punct("--") {
            let op = match self.advance() {
                Token::Punct("++") => AssignOp::AddAssign,
                _ => AssignOp::SubAssign,
            };
            return Ok(Expr::Assign(Box::new(e), op, Box::new(Expr::Number(1.0))));
        }
        Ok(e)
    }

    fn parse_call_member(&mut self, new_expr: bool) -> PResult<Expr> {
        let mut e = self.parse_primary()?;
        let mut saw_new = new_expr;
        loop {
            match self.peek() {
                Token::Punct(".") => {
                    self.advance();
                    let prop = match self.advance() {
                        Token::Ident(s) => s,
                        Token::Keyword(s) => s,
                        other => return Err(ParseError(format!("expected property after '.', got {:?}", other))),
                    };
                    e = Expr::Member(Box::new(e), prop);
                }
                Token::Punct("[") => {
                    self.advance();
                    let idx = self.parse_expr()?;
                    self.expect_punct("]")?;
                    e = Expr::Index(Box::new(e), Box::new(idx));
                }
                Token::Punct("(") => {
                    let args = self.parse_args()?;
                    e = if saw_new {
                        saw_new = false;
                        Expr::New(Box::new(e), args)
                    } else {
                        Expr::Call(Box::new(e), args)
                    };
                }
                _ => break,
            }
        }
        Ok(e)
    }

    fn parse_args(&mut self) -> PResult<Vec<Expr>> {
        self.expect_punct("(")?;
        let mut args = Vec::new();
        if !self.peek_punct(")") {
            loop {
                args.push(self.parse_assign()?);
                if !self.eat_punct(",") {
                    break;
                }
            }
        }
        self.expect_punct(")")?;
        Ok(args)
    }

    fn parse_primary(&mut self) -> PResult<Expr> {
        match self.peek().clone() {
            Token::Num(n) => {
                self.advance();
                Ok(Expr::Number(n))
            }
            Token::Str(s) => {
                self.advance();
                Ok(Expr::Str(s))
            }
            Token::Template(parts) => {
                self.advance();
                let mut out = Vec::with_capacity(parts.len());
                for p in parts {
                    match p {
                        TmplTokenPart::Text(t) => out.push(TmplPart::Text(t)),
                        TmplTokenPart::Expr(src) => {
                            let mut sub = Parser::new(&src)?;
                            let e = sub.parse_expr()?;
                            // trailing semicolons OK
                            out.push(TmplPart::Expr(Box::new(e)));
                        }
                    }
                }
                Ok(Expr::Template(out))
            }
            Token::Keyword(k) => {
                self.advance();
                match k.as_str() {
                    "true" => Ok(Expr::Bool(true)),
                    "false" => Ok(Expr::Bool(false)),
                    "null" => Ok(Expr::Null),
                    "undefined" => Ok(Expr::Undefined),
                    "this" => Ok(Expr::This),
                    "function" => self.parse_func_expr(),
                    "new" => self.parse_new(),
                    _ => Err(ParseError(format!("unexpected keyword `{}`", k))),
                }
            }
            Token::Ident(_) => {
                self.advance();
                // re-read name
                if let Some(Token::Ident(name)) = self.toks.get(self.pos - 1) {
                    Ok(Expr::Ident(name.clone()))
                } else {
                    unreachable!()
                }
            }
            Token::Punct("(") => {
                self.advance();
                let e = self.parse_expr()?;
                self.expect_punct(")")?;
                Ok(e)
            }
            Token::Punct("[") => {
                self.advance();
                let mut items = Vec::new();
                if !self.peek_punct("]") {
                    loop {
                        if self.peek_punct("]") {
                            break;
                        }
                        // allow elision
                        if self.peek_punct(",") {
                            items.push(Expr::Undefined);
                            self.advance();
                            continue;
                        }
                        items.push(self.parse_assign()?);
                        if !self.eat_punct(",") {
                            break;
                        }
                    }
                }
                self.expect_punct("]")?;
                Ok(Expr::Array(items))
            }
            Token::Punct("{") => {
                self.advance();
                let mut props = Vec::new();
                if !self.peek_punct("}") {
                    loop {
                        if self.peek_punct("}") {
                            break;
                        }
                        let key = match self.advance() {
                            Token::Ident(s) => s,
                            Token::Keyword(s) => s,
                            Token::Str(s) => s,
                            Token::Num(n) => n.to_string(),
                            other => {
                                return Err(ParseError(format!(
                                    "bad object key {:?}",
                                    other
                                )))
                            }
                        };
                        let val = if self.eat_punct(":") {
                            self.parse_assign()?
                        } else {
                            Expr::Ident(key.clone())
                        };
                        props.push((key, val));
                        if !self.eat_punct(",") {
                            break;
                        }
                    }
                }
                self.expect_punct("}")?;
                Ok(Expr::Object(props))
            }
            other => Err(ParseError(format!("unexpected token {:?}", other))),
        }
    }

    fn parse_func_expr(&mut self) -> PResult<Expr> {
        self.expect_kw("function")?;
        let name = if matches!(self.peek(), Token::Ident(_)) {
            Some(self.parse_ident()?)
        } else {
            None
        };
        self.expect_punct("(")?;
        let params = self.parse_params()?;
        self.expect_punct("{")?;
        let body = self.parse_block_body()?;
        Ok(Expr::Func(name, params, body))
    }
}

/// Parse a source string into a list of statements.
pub fn parse(src: &str) -> Result<Vec<Stmt>, ParseError> {
    let mut p = Parser::new(src)?;
    p.parse_program()
}

/// Parse a source string as a single expression (used for template `${...}`).
#[allow(dead_code)]
pub fn parse_expression(src: &str) -> Result<Expr, ParseError> {
    let mut p = Parser::new(src)?;
    let e = p.parse_expr()?;
    Ok(e)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_var_and_call() {
        let stmts = parse("var x = 'hi'; document.write(x);").unwrap();
        assert_eq!(stmts.len(), 2);
        assert!(matches!(&stmts[0], Stmt::Var { kind: VarKind::Var, .. }));
        assert!(matches!(&stmts[1], Stmt::Expr(Expr::Call(..))));
    }

    #[test]
    fn parse_if_else() {
        let s = parse("if (a > 1) { f(); } else { g(); }").unwrap();
        assert!(matches!(&s[0], Stmt::If { .. }));
    }

    #[test]
    fn parse_for_of() {
        let s = parse("for (var item of items) { document.write(item); }").unwrap();
        assert!(matches!(&s[0], Stmt::ForOf { .. }));
    }

    #[test]
    fn parse_template_member_call() {
        let s = parse("document.write(`<p>${name.toUpperCase()}</p>`);").unwrap();
        match &s[0] {
            Stmt::Expr(Expr::Call(_, args)) => match &args[0] {
                Expr::Template(_) => {}
                other => panic!("expected template arg, got {:?}", other),
            },
            other => panic!("expected call stmt, got {:?}", other),
        }
    }

    #[test]
    fn parse_function_decl() {
        let s = parse("function add(a, b) { return a + b; }").unwrap();
        assert!(matches!(&s[0], Stmt::Func { .. }));
    }

    #[test]
    fn parse_object_literal() {
        let s = parse("var o = { a: 1, b: 'x' };").unwrap();
        match &s[0] {
            Stmt::Var { decls, .. } => match &decls[0].1 {
                Some(Expr::Object(_)) => {}
                other => panic!("expected object, got {:?}", other),
            },
            other => panic!("{:?}", other),
        }
    }

    #[test]
    fn parse_operator_precedence() {
        let s = parse("var z = 1 + 2 * 3;").unwrap();
        match &s[0] {
            Stmt::Var { decls, .. } => match &decls[0].1 {
                Some(Expr::Binary(BinOp::Add, _, right)) => {
                    assert!(matches!(**right, Expr::Binary(BinOp::Mul, _, _)));
                }
                other => panic!("expected add, got {:?}", other),
            },
            other => panic!("{:?}", other),
        }
    }
}