gracile-core 0.1.2

Core engine for the Gracile templating language — lexer, parser, AST, and renderer
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
//! Hand-written recursive descent parser that builds the AST from tokens.

use crate::ast::*;
use crate::error::{Error, Result, Span};
use crate::lexer::{Token, TokenKind};

/// Hand-written recursive descent parser state.
pub struct Parser {
    tokens: Vec<Token>,
    pos: usize,
}

impl Parser {
    pub fn new(tokens: Vec<Token>) -> Self {
        Parser { tokens, pos: 0 }
    }

    pub fn parse(mut self) -> Result<Template> {
        let nodes = self.parse_nodes()?;
        self.expect_eof()?;
        Ok(Template { nodes })
    }

    fn peek(&self) -> &Token {
        &self.tokens[self.pos]
    }

    fn peek_kind(&self) -> &TokenKind {
        &self.peek().kind
    }

    fn peek_span(&self) -> Span {
        self.peek().span.clone()
    }

    fn advance(&mut self) -> &Token {
        let tok = &self.tokens[self.pos];
        if self.pos + 1 < self.tokens.len() {
            self.pos += 1;
        }
        tok
    }

    fn expect_close(&mut self) -> Result<()> {
        match self.peek_kind() {
            TokenKind::Close => {
                self.advance();
                Ok(())
            }
            other => Err(Error::ParseError {
                message: format!("Expected '}}', got {}", other),
                span: self.peek_span(),
            }),
        }
    }

    fn expect_ident(&mut self) -> Result<String> {
        match self.peek_kind().clone() {
            TokenKind::Ident(name) => {
                self.advance();
                Ok(name)
            }
            other => Err(Error::ParseError {
                message: format!("Expected identifier, got {}", other),
                span: self.peek_span(),
            }),
        }
    }

    fn expect_string(&mut self) -> Result<String> {
        match self.peek_kind().clone() {
            TokenKind::StringLit(s) => {
                self.advance();
                Ok(s)
            }
            other => Err(Error::ParseError {
                message: format!("Expected string literal, got {}", other),
                span: self.peek_span(),
            }),
        }
    }

    fn expect_keyword(&mut self, expected: TokenKind) -> Result<()> {
        if *self.peek_kind() == expected {
            self.advance();
            Ok(())
        } else {
            Err(Error::ParseError {
                message: format!("Expected {}, got {}", expected, self.peek_kind()),
                span: self.peek_span(),
            })
        }
    }

    fn expect_eof(&mut self) -> Result<()> {
        match self.peek_kind() {
            TokenKind::Eof => Ok(()),
            other => Err(Error::ParseError {
                message: format!("Expected end of template, got {}", other),
                span: self.peek_span(),
            }),
        }
    }

    /// Parse nodes until a stop token (`ContinueOpen`, `BlockClose`, or `Eof`),
    /// then apply standalone-tag stripping.
    fn parse_nodes(&mut self) -> Result<Vec<Node>> {
        let mut nodes: Vec<Node> = Vec::new();

        loop {
            match self.peek_kind() {
                TokenKind::Eof | TokenKind::ContinueOpen | TokenKind::BlockClose => break,

                TokenKind::RawText(_) => {
                    let TokenKind::RawText(text) = self.advance().kind.clone() else {
                        unreachable!()
                    };
                    nodes.push(Node::RawText(text));
                }

                TokenKind::CommentOpen => {
                    self.advance(); // CommentOpen
                    let body = match self.peek_kind().clone() {
                        TokenKind::CommentBody(b) => {
                            self.advance();
                            b
                        }
                        _ => String::new(),
                    };
                    match self.peek_kind() {
                        TokenKind::CommentClose => {
                            self.advance();
                        }
                        _ => {
                            return Err(Error::ParseError {
                                message: "Expected comment close '!}'".to_string(),
                                span: self.peek_span(),
                            });
                        }
                    }
                    nodes.push(Node::Comment(body));
                }

                TokenKind::ExprOpen => {
                    self.advance(); // ExprOpen
                    let expr = self.parse_expr()?;
                    self.expect_close()?;
                    nodes.push(Node::ExprTag(ExprTag { expr, raw: false }));
                }

                TokenKind::ExprOpenRaw => {
                    self.advance(); // ExprOpenRaw
                    let expr = self.parse_expr()?;
                    self.expect_close()?;
                    nodes.push(Node::ExprTag(ExprTag { expr, raw: true }));
                }

                TokenKind::BlockOpen => {
                    let node = self.parse_block()?;
                    nodes.push(node);
                }

                TokenKind::SpecialOpen => {
                    let node = self.parse_special()?;
                    nodes.push(node);
                }

                other => {
                    return Err(Error::ParseError {
                        message: format!("Unexpected token {}", other),
                        span: self.peek_span(),
                    });
                }
            }
        }

        strip_standalone(&mut nodes);
        Ok(nodes)
    }

    fn parse_block(&mut self) -> Result<Node> {
        self.advance(); // BlockOpen

        match self.peek_kind().clone() {
            TokenKind::KwIf => {
                self.advance(); // `if`
                let cond = self.parse_expr()?;
                self.expect_close()?;
                self.parse_if_block(cond)
            }
            TokenKind::KwEach => {
                self.advance(); // `each`
                let iterable = self.parse_expr()?;
                self.expect_keyword(TokenKind::KwAs)?;
                let pattern = self.parse_pattern()?;
                let index_binding = if self.peek_kind() == &TokenKind::Comma {
                    self.advance(); // `,`
                    Some(self.expect_ident()?)
                } else {
                    None
                };
                let loop_binding = if self.peek_kind() == &TokenKind::Comma {
                    self.advance(); // `,`
                    Some(self.expect_ident()?)
                } else {
                    None
                };
                self.expect_close()?;
                self.parse_each_block(iterable, pattern, index_binding, loop_binding)
            }
            TokenKind::KwSnippet => {
                self.advance(); // `snippet`
                let name = self.expect_ident()?;
                self.expect(&TokenKind::LParen)?;
                let params = self.parse_param_list()?;
                self.expect(&TokenKind::RParen)?;
                self.expect_close()?;
                self.parse_snippet_block(name, params)
            }
            TokenKind::KwRaw => {
                self.advance(); // `raw`
                self.expect_close()?;
                let body = match self.peek_kind().clone() {
                    TokenKind::RawBody(b) => {
                        self.advance();
                        b
                    }
                    _ => String::new(),
                };
                self.expect(&TokenKind::BlockClose)?;
                self.expect_keyword(TokenKind::KwRaw)?;
                self.expect_close()?;
                Ok(Node::RawBlock(body))
            }
            other => Err(Error::ParseError {
                message: format!("Unknown block keyword {}", other),
                span: self.peek_span(),
            }),
        }
    }

    fn parse_if_block(&mut self, first_cond: Expr) -> Result<Node> {
        let first_body = self.parse_nodes()?;
        let mut branches = vec![IfBranch {
            condition: first_cond,
            body: first_body,
        }];
        let mut else_body: Option<Vec<Node>> = None;

        loop {
            match self.peek_kind() {
                TokenKind::ContinueOpen => {
                    self.advance(); // ContinueOpen
                    self.expect_keyword(TokenKind::KwElse)?;
                    if self.peek_kind() == &TokenKind::KwIf {
                        self.advance(); // `if`
                        let cond = self.parse_expr()?;
                        self.expect_close()?;
                        let body = self.parse_nodes()?;
                        branches.push(IfBranch {
                            condition: cond,
                            body,
                        });
                    } else {
                        self.expect_close()?;
                        else_body = Some(self.parse_nodes()?);
                    }
                }
                TokenKind::BlockClose => {
                    self.advance(); // BlockClose
                    self.expect_keyword(TokenKind::KwIf)?;
                    self.expect_close()?;
                    break;
                }
                other => {
                    return Err(Error::ParseError {
                        message: format!(
                            "Expected {{:else}}, {{:else if}}, or {{/if}}, got {}",
                            other
                        ),
                        span: self.peek_span(),
                    });
                }
            }
        }

        Ok(Node::IfBlock(IfBlock {
            branches,
            else_body,
        }))
    }

    fn parse_each_block(
        &mut self,
        iterable: Expr,
        pattern: Pattern,
        index_binding: Option<String>,
        loop_binding: Option<String>,
    ) -> Result<Node> {
        let body = self.parse_nodes()?;
        let mut else_body: Option<Vec<Node>> = None;

        loop {
            match self.peek_kind() {
                TokenKind::ContinueOpen => {
                    self.advance(); // ContinueOpen
                    self.expect_keyword(TokenKind::KwElse)?;
                    self.expect_close()?;
                    else_body = Some(self.parse_nodes()?);
                }
                TokenKind::BlockClose => {
                    self.advance(); // BlockClose
                    self.expect_keyword(TokenKind::KwEach)?;
                    self.expect_close()?;
                    break;
                }
                other => {
                    return Err(Error::ParseError {
                        message: format!("Expected {{:else}} or {{/each}}, got {}", other),
                        span: self.peek_span(),
                    });
                }
            }
        }

        Ok(Node::EachBlock(EachBlock {
            iterable,
            pattern,
            index_binding,
            loop_binding,
            body,
            else_body,
        }))
    }

    fn parse_snippet_block(&mut self, name: String, params: Vec<String>) -> Result<Node> {
        let body = self.parse_nodes()?;
        match self.peek_kind() {
            TokenKind::BlockClose => {
                self.advance(); // BlockClose
                self.expect_keyword(TokenKind::KwSnippet)?;
                self.expect_close()?;
            }
            other => {
                return Err(Error::ParseError {
                    message: format!("Expected {{/snippet}}, got {}", other),
                    span: self.peek_span(),
                });
            }
        }
        Ok(Node::SnippetBlock(SnippetBlock { name, params, body }))
    }

    fn parse_special(&mut self) -> Result<Node> {
        self.advance(); // SpecialOpen

        match self.peek_kind().clone() {
            TokenKind::KwRender => {
                self.advance(); // `render`
                let name = self.expect_ident()?;
                self.expect(&TokenKind::LParen)?;
                let args = self.parse_arg_list()?;
                self.expect(&TokenKind::RParen)?;
                self.expect_close()?;
                Ok(Node::RenderTag(RenderTag { name, args }))
            }
            TokenKind::KwConst => {
                self.advance(); // `const`
                let name = self.expect_ident()?;
                self.expect(&TokenKind::Assign)?;
                let expr = self.parse_expr()?;
                self.expect_close()?;
                Ok(Node::ConstTag(ConstTag { name, expr }))
            }
            TokenKind::KwInclude => {
                self.advance(); // `include`
                let path = self.expect_string()?;
                self.expect_close()?;
                Ok(Node::IncludeTag(IncludeTag { path }))
            }
            TokenKind::KwDebug => {
                self.advance(); // `debug`
                let expr = if self.peek_kind() != &TokenKind::Close {
                    Some(self.parse_expr()?)
                } else {
                    None
                };
                self.expect_close()?;
                Ok(Node::DebugTag(DebugTag { expr }))
            }
            other => Err(Error::ParseError {
                message: format!("Unknown special tag keyword {}", other),
                span: self.peek_span(),
            }),
        }
    }

    fn parse_pattern(&mut self) -> Result<Pattern> {
        if self.peek_kind() == &TokenKind::LBraceD {
            self.advance(); // `{`
            let mut names = Vec::new();
            loop {
                if self.peek_kind() == &TokenKind::RBraceD {
                    self.advance(); // `}`
                    break;
                }
                names.push(self.expect_ident()?);
                if self.peek_kind() == &TokenKind::Comma {
                    self.advance();
                    if self.peek_kind() == &TokenKind::RBraceD {
                        self.advance();
                        break;
                    }
                }
            }
            Ok(Pattern::Destructure(names))
        } else {
            Ok(Pattern::Ident(self.expect_ident()?))
        }
    }

    fn parse_param_list(&mut self) -> Result<Vec<String>> {
        let mut params = Vec::new();
        if self.peek_kind() == &TokenKind::RParen {
            return Ok(params);
        }
        params.push(self.expect_ident()?);
        while self.peek_kind() == &TokenKind::Comma {
            self.advance();
            if self.peek_kind() == &TokenKind::RParen {
                break;
            }
            params.push(self.expect_ident()?);
        }
        Ok(params)
    }

    fn parse_arg_list(&mut self) -> Result<Vec<Expr>> {
        let mut args = Vec::new();
        if self.peek_kind() == &TokenKind::RParen {
            return Ok(args);
        }
        args.push(self.parse_expr()?);
        while self.peek_kind() == &TokenKind::Comma {
            self.advance();
            if self.peek_kind() == &TokenKind::RParen {
                break;
            }
            args.push(self.parse_expr()?);
        }
        Ok(args)
    }

    fn parse_expr(&mut self) -> Result<Expr> {
        self.parse_filter_expr()
    }

    fn parse_filter_expr(&mut self) -> Result<Expr> {
        let expr = self.parse_ternary()?;
        let mut filters = Vec::new();
        while self.peek_kind() == &TokenKind::Pipe {
            self.advance();
            let name = self.expect_ident()?;
            let args = if self.peek_kind() == &TokenKind::LParen {
                self.advance();
                let a = self.parse_arg_list()?;
                self.expect(&TokenKind::RParen)?;
                a
            } else {
                Vec::new()
            };
            filters.push(FilterApplication { name, args });
        }
        if filters.is_empty() {
            Ok(expr)
        } else {
            Ok(Expr::Filter {
                expr: Box::new(expr),
                filters,
            })
        }
    }

    fn parse_ternary(&mut self) -> Result<Expr> {
        let cond = self.parse_nullish()?;
        if self.peek_kind() == &TokenKind::Question {
            self.advance();
            let consequent = self.parse_expr()?;
            self.expect(&TokenKind::Colon)?;
            let alternate = self.parse_expr()?;
            Ok(Expr::Ternary {
                condition: Box::new(cond),
                consequent: Box::new(consequent),
                alternate: Box::new(alternate),
            })
        } else {
            Ok(cond)
        }
    }

    fn parse_nullish(&mut self) -> Result<Expr> {
        let mut left = self.parse_or()?;
        while self.peek_kind() == &TokenKind::NullCoalesce {
            self.advance();
            let right = self.parse_or()?;
            left = Expr::Binary {
                op: BinaryOp::NullCoalesce,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_or(&mut self) -> Result<Expr> {
        let mut left = self.parse_and()?;
        while self.peek_kind() == &TokenKind::Or {
            self.advance();
            let right = self.parse_and()?;
            left = Expr::Binary {
                op: BinaryOp::Or,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_and(&mut self) -> Result<Expr> {
        let mut left = self.parse_equality()?;
        while self.peek_kind() == &TokenKind::And {
            self.advance();
            let right = self.parse_equality()?;
            left = Expr::Binary {
                op: BinaryOp::And,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_equality(&mut self) -> Result<Expr> {
        let mut left = self.parse_comparison()?;
        loop {
            let op = match self.peek_kind() {
                TokenKind::Eq => BinaryOp::Eq,
                TokenKind::Neq => BinaryOp::Neq,
                _ => break,
            };
            self.advance();
            let right = self.parse_comparison()?;
            left = Expr::Binary {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_comparison(&mut self) -> Result<Expr> {
        let mut left = self.parse_test()?;
        loop {
            let op = match self.peek_kind() {
                TokenKind::Lt => BinaryOp::Lt,
                TokenKind::Gt => BinaryOp::Gt,
                TokenKind::Lte => BinaryOp::Lte,
                TokenKind::Gte => BinaryOp::Gte,
                _ => break,
            };
            self.advance();
            let right = self.parse_test()?;
            left = Expr::Binary {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_test(&mut self) -> Result<Expr> {
        let expr = self.parse_membership()?;
        if self.peek_kind() == &TokenKind::KwIs {
            self.advance();
            let negated = if self.peek_kind() == &TokenKind::KwNot {
                self.advance();
                true
            } else {
                false
            };
            let test_name = self.expect_ident()?;
            Ok(Expr::Test {
                expr: Box::new(expr),
                negated,
                test_name,
            })
        } else {
            Ok(expr)
        }
    }

    fn parse_membership(&mut self) -> Result<Expr> {
        let expr = self.parse_additive()?;
        match self.peek_kind() {
            TokenKind::KwIn => {
                self.advance();
                let collection = self.parse_additive()?;
                Ok(Expr::Membership {
                    expr: Box::new(expr),
                    negated: false,
                    collection: Box::new(collection),
                })
            }
            TokenKind::KwNot => {
                self.advance();
                self.expect_keyword(TokenKind::KwIn)?;
                let collection = self.parse_additive()?;
                Ok(Expr::Membership {
                    expr: Box::new(expr),
                    negated: true,
                    collection: Box::new(collection),
                })
            }
            _ => Ok(expr),
        }
    }

    fn parse_additive(&mut self) -> Result<Expr> {
        let mut left = self.parse_multiplicative()?;
        loop {
            let op = match self.peek_kind() {
                TokenKind::Add => BinaryOp::Add,
                TokenKind::Sub => BinaryOp::Sub,
                _ => break,
            };
            self.advance();
            let right = self.parse_multiplicative()?;
            left = Expr::Binary {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_multiplicative(&mut self) -> Result<Expr> {
        let mut left = self.parse_unary()?;
        loop {
            let op = match self.peek_kind() {
                TokenKind::Mul => BinaryOp::Mul,
                TokenKind::Div => BinaryOp::Div,
                TokenKind::Mod => BinaryOp::Mod,
                _ => break,
            };
            self.advance();
            let right = self.parse_unary()?;
            left = Expr::Binary {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_unary(&mut self) -> Result<Expr> {
        match self.peek_kind() {
            TokenKind::Bang => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnaryOp::Not,
                    operand: Box::new(self.parse_unary()?),
                })
            }
            TokenKind::Sub => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnaryOp::Neg,
                    operand: Box::new(self.parse_unary()?),
                })
            }
            _ => self.parse_postfix(),
        }
    }

    fn parse_postfix(&mut self) -> Result<Expr> {
        let mut expr = self.parse_primary()?;
        loop {
            match self.peek_kind() {
                TokenKind::Dot => {
                    self.advance();
                    let prop = self.expect_ident()?;
                    expr = Expr::MemberAccess {
                        object: Box::new(expr),
                        property: prop,
                    };
                }
                TokenKind::LBracket => {
                    self.advance();
                    let index = self.parse_expr()?;
                    self.expect(&TokenKind::RBracket)?;
                    expr = Expr::IndexAccess {
                        object: Box::new(expr),
                        index: Box::new(index),
                    };
                }
                _ => break,
            }
        }
        Ok(expr)
    }

    fn parse_primary(&mut self) -> Result<Expr> {
        match self.peek_kind().clone() {
            TokenKind::Null => {
                self.advance();
                Ok(Expr::Null)
            }
            TokenKind::True => {
                self.advance();
                Ok(Expr::Bool(true))
            }
            TokenKind::False => {
                self.advance();
                Ok(Expr::Bool(false))
            }
            TokenKind::IntLit(i) => {
                self.advance();
                Ok(Expr::Int(i))
            }
            TokenKind::FloatLit(f) => {
                self.advance();
                Ok(Expr::Float(f))
            }
            TokenKind::StringLit(s) => {
                self.advance();
                Ok(Expr::String(s))
            }
            TokenKind::Ident(name) => {
                self.advance();
                Ok(Expr::Ident(name))
            }
            TokenKind::LParen => {
                self.advance();
                let expr = self.parse_expr()?;
                self.expect(&TokenKind::RParen)?;
                Ok(expr)
            }
            TokenKind::LBracket => {
                self.advance();
                let mut elements = Vec::new();
                if self.peek_kind() != &TokenKind::RBracket {
                    elements.push(self.parse_expr()?);
                    while self.peek_kind() == &TokenKind::Comma {
                        self.advance();
                        if self.peek_kind() == &TokenKind::RBracket {
                            break;
                        }
                        elements.push(self.parse_expr()?);
                    }
                }
                self.expect(&TokenKind::RBracket)?;
                Ok(Expr::Array(elements))
            }
            other => Err(Error::ParseError {
                message: format!("Expected expression, got {}", other),
                span: self.peek_span(),
            }),
        }
    }

    fn expect(&mut self, kind: &TokenKind) -> Result<&Token> {
        if std::mem::discriminant(self.peek_kind()) == std::mem::discriminant(kind) {
            Ok(self.advance())
        } else {
            Err(Error::ParseError {
                message: format!("Expected {}, got {}", kind, self.peek_kind()),
                span: self.peek_span(),
            })
        }
    }
}

/// Strip standalone block tags from their surrounding lines.
///
/// A block-level tag is "standalone" when:
/// - the text before it on its line is entirely whitespace (spaces/tabs), and
/// - the text after it on its line is entirely whitespace (spaces/tabs) + newline.
///
/// When standalone, the tag's entire line is removed from the output:
/// - the whitespace prefix is stripped from the preceding raw-text node,
/// - the trailing newline is stripped from the following raw-text node,
/// - the opening newline is stripped from the block's first body raw-text node,
/// - the closing whitespace prefix is stripped from the block's last body raw-text node.
fn strip_standalone(nodes: &mut Vec<Node>) {
    let len = nodes.len();
    for i in 0..len {
        if !is_standalone_eligible(&nodes[i]) {
            continue;
        }

        // For block nodes that have Vec<Node> bodies (if/each/snippet): require that
        // the first body node is RawText starting with '\n'.  This confirms the opener
        // tag was immediately followed by a newline and therefore IS on its own line.
        // Without this check, inline blocks like `{#if cond}text{/if}` would wrongly
        // be treated as standalone and their bodies would be stripped.
        if has_vec_body(&nodes[i]) {
            match block_first_body_text(&nodes[i]) {
                Some(s) if s.starts_with('\n') => {} // opener was on its own line — OK
                _ => continue,                       // inline or empty body — skip
            }
        }

        // Check: is text after the last newline in the preceding node blank?
        let prefix_blank = match i.checked_sub(1) {
            None => true, // start of body — treat as after a newline
            Some(prev) => match &nodes[prev] {
                Node::RawText(s) => after_last_nl(s).chars().all(|c| c == ' ' || c == '\t'),
                _ => false,
            },
        };
        if !prefix_blank {
            continue;
        }

        // Check: is text before the first newline in the following node blank?
        let suffix_blank = match nodes.get(i + 1) {
            None => true, // end of body — treat as before a newline
            Some(Node::RawText(s)) => before_first_nl(s).chars().all(|c| c == ' ' || c == '\t'),
            Some(_) => false,
        };
        if !suffix_blank {
            continue;
        }

        // Strip the blank prefix from the preceding raw-text node.
        if i > 0
            && let Node::RawText(s) = &mut nodes[i - 1]
        {
            trim_line_tail(s);
        }

        // Strip the rest of the tag's line (blank spaces + newline) from the
        // start of the following raw-text node.
        if i + 1 < len
            && let Node::RawText(s) = &mut nodes[i + 1]
        {
            strip_through_first_nl(s);
        }

        // Strip the opening/closing newlines inside block bodies.
        strip_block_body_edges(&mut nodes[i]);
    }

    // Remove raw-text nodes that became empty after stripping.
    nodes.retain(|n| !matches!(n, Node::RawText(s) if s.is_empty()));
}

/// Whether a node triggers standalone-line stripping.
fn is_standalone_eligible(node: &Node) -> bool {
    matches!(
        node,
        Node::IfBlock(_)
            | Node::EachBlock(_)
            | Node::SnippetBlock(_)
            | Node::RawBlock(_)
            | Node::RenderTag(_)
            | Node::ConstTag(_)
            | Node::IncludeTag(_)
            | Node::DebugTag(_)
            | Node::Comment(_)
    )
}

/// Whether a node has a `Vec<Node>` body (as opposed to no body or a String body).
fn has_vec_body(node: &Node) -> bool {
    matches!(
        node,
        Node::IfBlock(_) | Node::EachBlock(_) | Node::SnippetBlock(_)
    )
}

/// Return the text of the first body node if it is `RawText`, otherwise `None`.
fn block_first_body_text(node: &Node) -> Option<&str> {
    let body = match node {
        Node::IfBlock(b) => &b.branches[0].body,
        Node::EachBlock(b) => &b.body,
        Node::SnippetBlock(b) => &b.body,
        _ => return None,
    };
    match body.first() {
        Some(Node::RawText(s)) => Some(s.as_str()),
        _ => None,
    }
}

/// Strip the opening newline (from the opener tag's line) from the first body
/// node, and the closing whitespace prefix (from the closer tag's line) from
/// the last body node.
fn strip_block_body_edges(node: &mut Node) {
    match node {
        Node::IfBlock(b) => {
            // Every branch body is bounded by standalone tags on both sides
            // ({#if}, {:else if}, {:else}, {/if}).  Strip the head and tail of
            // each branch so all those lines disappear from the output.
            for branch in &mut b.branches {
                strip_body_head(&mut branch.body);
                strip_body_tail(&mut branch.body);
            }
            if let Some(eb) = &mut b.else_body {
                strip_body_head(eb);
                strip_body_tail(eb);
            }
        }
        Node::EachBlock(b) => {
            strip_body_head(&mut b.body);
            if let Some(eb) = &mut b.else_body {
                strip_body_tail(&mut b.body); // blank line before {:else}
                strip_body_head(eb); // newline after {:else}
                strip_body_tail(eb); // blank line before {/each}
            } else {
                strip_body_tail(&mut b.body);
            }
        }
        Node::SnippetBlock(b) => {
            strip_body_head(&mut b.body);
            strip_body_tail(&mut b.body);
        }
        _ => {}
    }
}

fn strip_body_head(body: &mut Vec<Node>) {
    if let Some(Node::RawText(s)) = body.first_mut() {
        // Only strip when the first "line" of the body (before the first \n) is
        // entirely blank — meaning the opener tag occupied its own line.
        let should_strip = match s.find('\n') {
            Some(pos) => s[..pos].chars().all(|c| c == ' ' || c == '\t'),
            None => false, // body is on the same line as the opener — don't touch
        };
        if should_strip {
            strip_through_first_nl(s);
            if s.is_empty() {
                body.remove(0);
            }
        }
    }
}

fn strip_body_tail(body: &mut Vec<Node>) {
    if let Some(Node::RawText(s)) = body.last_mut() {
        // Only strip when the last "line" of the body (after the last \n) is
        // entirely blank — meaning the closer tag occupied its own line.
        let blank_tail = match s.rfind('\n') {
            Some(pos) => s[pos + 1..].chars().all(|c| c == ' ' || c == '\t'),
            None => false, // body is on the same line as the closer — don't touch
        };
        if blank_tail {
            trim_line_tail(s);
            if s.is_empty() {
                body.pop();
            }
        }
    }
}

/// Returns the substring after the last `\n` (or the whole string if none).
fn after_last_nl(s: &str) -> &str {
    match s.rfind('\n') {
        Some(pos) => &s[pos + 1..],
        None => s,
    }
}

/// Returns the substring before the first `\n` (or the whole string if none).
fn before_first_nl(s: &str) -> &str {
    match s.find('\n') {
        Some(pos) => &s[..pos],
        None => s,
    }
}

/// Strip everything after the last `\n`, keeping the `\n` itself.
/// If there is no `\n`, clear the whole string (it was a blank opening prefix).
fn trim_line_tail(s: &mut String) {
    match s.rfind('\n') {
        Some(pos) => s.truncate(pos + 1),
        None => s.clear(),
    }
}

/// Strip everything up to and including the first `\n` (handling `\r\n` too).
/// If there is no `\n`, clear the whole string (everything is on the tag's line).
fn strip_through_first_nl(s: &mut String) {
    if let Some(pos) = s.find('\n') {
        *s = s[pos + 1..].to_string();
    } else {
        s.clear();
    }
}

/// Parse a token stream into a [`Template`] AST.
pub fn parse(tokens: Vec<Token>) -> Result<Template> {
    Parser::new(tokens).parse()
}