ruitl_compiler 0.2.1

Parser and code generator for the RUITL (Rust UI Template Language)
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
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
//! RUITL Template Parser
//!
//! Parses .ruitl files and converts them to an AST that can be compiled to Rust code

use crate::error::{CompileError, Result};
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub struct RuitlFile {
    pub components: Vec<ComponentDef>,
    pub templates: Vec<TemplateDef>,
    pub imports: Vec<ImportDef>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ComponentDef {
    pub name: String,
    pub props: Vec<PropDef>,
    pub generics: Vec<GenericParam>,
    /// Line / block comments that immediately precede this declaration.
    /// Stored verbatim (without the `//` or `/* */` markers) so the
    /// formatter can re-emit them in canonical position.
    pub leading_comments: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PropDef {
    pub name: String,
    pub prop_type: String,
    pub optional: bool,
    pub default_value: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TemplateDef {
    pub name: String,
    pub params: Vec<ParamDef>,
    pub body: TemplateAst,
    pub generics: Vec<GenericParam>,
    /// See `ComponentDef::leading_comments`.
    pub leading_comments: Vec<String>,
}

/// A single generic type parameter: `T` or `T: Bound1 + Bound2`.
#[derive(Debug, Clone, PartialEq)]
pub struct GenericParam {
    pub name: String,
    pub bounds: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ParamDef {
    pub name: String,
    pub param_type: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ImportDef {
    pub path: String,
    pub items: Vec<String>,
    /// See `ComponentDef::leading_comments`.
    pub leading_comments: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TemplateAst {
    /// HTML element: <div class="foo">content</div>
    Element {
        tag: String,
        attributes: Vec<Attribute>,
        children: Vec<TemplateAst>,
        self_closing: bool,
    },
    /// Plain text content
    Text(String),
    /// Rust expression: {expr}
    Expression(String),
    /// Raw-HTML Rust expression: `{!expr}`. Content is emitted via
    /// `Html::raw(...)` instead of `Html::text(...)`, so the rendered
    /// result is injected verbatim without HTML-entity escaping. Use
    /// sparingly — caller is responsible for ensuring the expression
    /// produces safe HTML.
    RawExpression(String),
    /// Conditional rendering: if condition { ... } else { ... }
    If {
        condition: String,
        then_branch: Box<TemplateAst>,
        else_branch: Option<Box<TemplateAst>>,
    },
    /// Loop rendering: for item in items { ... }
    For {
        variable: String,
        iterable: String,
        body: Box<TemplateAst>,
    },
    /// Match expression: match expr { ... }
    Match {
        expression: String,
        arms: Vec<MatchArm>,
    },
    /// Component invocation: `@Button(props)` or `@Card(title: "x") { <p/>body }`.
    /// `children` carries the optional `{ ... }` body block passed to the
    /// callee as its `children: Html` prop.
    Component {
        name: String,
        props: Vec<PropValue>,
        children: Option<Box<TemplateAst>>,
    },
    /// `{children}` inside a template body — placeholder that is replaced at
    /// codegen with `props.children.clone()`. The props struct for the owning
    /// component auto-gains a `pub children: Html` field when this variant
    /// appears anywhere in the body.
    Children,
    /// Multiple nodes
    Fragment(Vec<TemplateAst>),
    /// Raw HTML (unescaped)
    Raw(String),
}

#[derive(Debug, Clone, PartialEq)]
pub struct Attribute {
    pub name: String,
    pub value: AttributeValue,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
    /// Static string value: class="foo"
    Static(String),
    /// Expression value: class={expr}
    Expression(String),
    /// Conditional attribute: disabled?={condition}
    Conditional(String),
}

#[derive(Debug, Clone, PartialEq)]
pub struct MatchArm {
    pub pattern: String,
    pub body: TemplateAst,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PropValue {
    pub name: String,
    pub value: String,
}

#[derive(Debug)]
pub struct RuitlParser {
    input: Vec<char>,
    position: usize,
    line: usize,
    column: usize,
    /// Comments collected by `skip_whitespace_and_comments` that haven't
    /// yet been attached to a declaration. The next top-level `parse_*`
    /// drains this buffer into its `leading_comments` field.
    pending_comments: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct ParseError {
    pub message: String,
    pub line: usize,
    pub column: usize,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Parse error at line {}, column {}: {}",
            self.line, self.column, self.message
        )
    }
}

impl std::error::Error for ParseError {}

impl RuitlParser {
    pub fn new(input: String) -> Self {
        Self {
            input: input.chars().collect(),
            position: 0,
            line: 1,
            column: 1,
            pending_comments: Vec::new(),
        }
    }

    pub fn parse(&mut self) -> Result<RuitlFile> {
        let mut components = Vec::new();
        let mut templates = Vec::new();
        let mut imports = Vec::new();

        self.skip_whitespace_and_comments();

        while !self.is_at_end() {
            if self.match_keyword("import") {
                imports.push(self.parse_import()?);
            } else if self.match_keyword("component") {
                components.push(self.parse_component()?);
            } else if self.match_keyword("ruitl") {
                templates.push(self.parse_template()?);
            } else {
                return Err(self.error("Expected 'import', 'component', or 'ruitl'"));
            }
            self.skip_whitespace_and_comments();
        }

        Ok(RuitlFile {
            components,
            templates,
            imports,
        })
    }

    fn parse_import(&mut self) -> Result<ImportDef> {
        let leading_comments = self.take_pending_comments();
        self.skip_whitespace();
        let path = self.parse_string_literal()?;

        self.skip_whitespace();
        if !self.match_char('{') {
            return Err(self.error("Expected '{' after import path"));
        }

        let mut items = Vec::new();
        self.skip_whitespace();

        while !self.check_char('}') && !self.is_at_end() {
            let item = self.parse_identifier()?;
            items.push(item);

            self.skip_whitespace();
            if self.match_char(',') {
                self.skip_whitespace();
            } else if !self.check_char('}') {
                return Err(self.error("Expected ',' or '}' in import list"));
            }
        }

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close import list"));
        }

        Ok(ImportDef {
            path,
            items,
            leading_comments,
        })
    }

    fn parse_component(&mut self) -> Result<ComponentDef> {
        let leading_comments = self.take_pending_comments();
        self.skip_whitespace();
        let name = self.parse_identifier()?;

        self.skip_whitespace();
        let generics = if self.check_char('<') {
            self.parse_generics()?
        } else {
            Vec::new()
        };

        self.skip_whitespace();
        if !self.match_char('{') {
            return Err(self.error("Expected '{' after component name"));
        }

        self.skip_whitespace_and_comments();

        let mut props = Vec::new();

        if self.match_keyword("props") {
            self.skip_whitespace();
            if !self.match_char('{') {
                return Err(self.error("Expected '{' after 'props'"));
            }

            self.skip_whitespace_and_comments();
            while !self.check_char('}') && !self.is_at_end() {
                props.push(self.parse_prop_def()?);
                self.skip_whitespace_and_comments();
            }

            if !self.match_char('}') {
                return Err(self.error("Expected '}' to close props block"));
            }
            self.skip_whitespace_and_comments();
        }

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close component definition"));
        }

        Ok(ComponentDef {
            name,
            props,
            generics,
            leading_comments,
        })
    }

    fn parse_prop_def(&mut self) -> Result<PropDef> {
        let name = self.parse_identifier()?;

        self.skip_whitespace();
        if !self.match_char(':') {
            return Err(self.error("Expected ':' after prop name"));
        }

        self.skip_whitespace();
        let prop_type = self.parse_type()?;

        self.skip_whitespace();
        let mut optional = false;
        let mut default_value = None;

        if self.match_char('=') {
            self.skip_whitespace();
            default_value = Some(self.parse_expression_until(&[',', '\n', '}'])?);
        } else if self.match_char('?') {
            optional = true;
        }

        self.skip_whitespace();
        if self.match_char(',') {
            self.skip_whitespace();
        }

        Ok(PropDef {
            name,
            prop_type,
            optional,
            default_value,
        })
    }

    fn parse_template(&mut self) -> Result<TemplateDef> {
        let leading_comments = self.take_pending_comments();
        self.skip_whitespace();
        let name = self.parse_identifier()?;

        self.skip_whitespace();
        let generics = if self.check_char('<') {
            self.parse_generics()?
        } else {
            Vec::new()
        };

        self.skip_whitespace();
        if !self.match_char('(') {
            return Err(self.error("Expected '(' after template name"));
        }

        let mut params = Vec::new();
        self.skip_whitespace();

        while !self.check_char(')') && !self.is_at_end() {
            let param_name = self.parse_identifier()?;
            self.skip_whitespace();

            if !self.match_char(':') {
                return Err(self.error("Expected ':' after parameter name"));
            }

            self.skip_whitespace();
            let param_type = self.parse_type()?;

            params.push(ParamDef {
                name: param_name,
                param_type,
            });

            self.skip_whitespace();
            if self.match_char(',') {
                self.skip_whitespace();
            } else if !self.check_char(')') {
                return Err(self.error("Expected ',' or ')' in parameter list"));
            }
        }

        if !self.match_char(')') {
            return Err(self.error("Expected ')' to close parameter list"));
        }

        self.skip_whitespace();
        if !self.match_char('{') {
            return Err(self.error("Expected '{' to start template body"));
        }

        let body = self.parse_template_body()?;

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close template body"));
        }

        Ok(TemplateDef {
            name,
            params,
            body,
            generics,
            leading_comments,
        })
    }

    /// Parse `<T, U: Bound1 + Bound2>` — a comma-separated list of generic
    /// parameters, each optionally followed by `: Bound1 + Bound2 + ...`.
    fn parse_generics(&mut self) -> Result<Vec<GenericParam>> {
        if !self.match_char('<') {
            return Err(self.error("Expected '<' to start generic parameter list"));
        }

        let mut params = Vec::new();
        self.skip_whitespace();

        while !self.check_char('>') && !self.is_at_end() {
            // Reject lifetime parameters (`<'a>`) explicitly. RUITL components
            // use owned types only; lifetime-generic components would need
            // lifetime inference on the render method, which is out of scope
            // for v0.2.
            if self.check_char('\'') {
                return Err(self.error(
                    "Lifetime parameters in component declarations are not supported; use owned types",
                ));
            }
            let name = self.parse_identifier()?;
            self.skip_whitespace();

            let mut bounds = Vec::new();
            if self.match_char(':') {
                self.skip_whitespace();
                loop {
                    let bound = self.parse_generic_bound()?;
                    if !bound.is_empty() {
                        bounds.push(bound);
                    }
                    self.skip_whitespace();
                    if !self.match_char('+') {
                        break;
                    }
                    self.skip_whitespace();
                }
            }

            params.push(GenericParam { name, bounds });

            self.skip_whitespace();
            if self.match_char(',') {
                self.skip_whitespace();
            } else if !self.check_char('>') {
                return Err(self.error("Expected ',' or '>' in generic parameter list"));
            }
        }

        if !self.match_char('>') {
            return Err(self.error("Expected '>' to close generic parameter list"));
        }

        Ok(params)
    }

    /// Parse a single trait-bound inside a generic parameter list. Stops at
    /// `+` (next bound), `,` (next param), or `>` (end of list). Nested
    /// `<...>` is tracked so bounds like `Iterator<Item=u32>` work.
    fn parse_generic_bound(&mut self) -> Result<String> {
        let mut out = String::new();
        let mut angle_depth = 0i32;

        while !self.is_at_end() {
            let ch = self.current_char();
            match ch {
                '<' => angle_depth += 1,
                '>' if angle_depth > 0 => angle_depth -= 1,
                '>' | ',' | '+' if angle_depth == 0 => break,
                _ => {}
            }
            out.push(ch);
            self.advance();
        }

        Ok(out.trim().to_string())
    }

    fn parse_template_body(&mut self) -> Result<TemplateAst> {
        let mut nodes = Vec::new();
        self.skip_whitespace();

        while !self.check_char('}') && !self.is_at_end() {
            let node = self.parse_template_node()?;
            nodes.push(node);
            self.skip_whitespace();
        }

        if nodes.len() == 1 {
            Ok(nodes.into_iter().next().unwrap())
        } else {
            Ok(TemplateAst::Fragment(nodes))
        }
    }

    fn parse_template_node(&mut self) -> Result<TemplateAst> {
        // Whitespace between an expression (`{x}`) and adjacent text is
        // significant for HTML rendering. Only eat leading whitespace when the
        // next non-whitespace token is a structured node (element /
        // expression / component / control-flow keyword). When it's text,
        // keep the whitespace as part of the text node.
        let after_ws = self.cursor_after_whitespace();
        let next_is_structured = if after_ws >= self.input.len() {
            false
        } else {
            let c = self.input[after_ws];
            c == '<'
                || c == '{'
                || c == '@'
                || c == '}'
                || self.at_keyword_at(after_ws, &["if", "for", "match", "else"])
        };

        if next_is_structured {
            self.skip_whitespace();
        }

        if self.check_char('<') {
            // Check if this is a DOCTYPE declaration
            if self.peek_string(9) == "<!DOCTYPE" {
                self.parse_doctype()
            } else {
                self.parse_element()
            }
        } else if self.check_char('{') {
            self.parse_expression_node()
        } else if self.check_char('@') {
            self.parse_component_invocation()
        } else if self.match_keyword("if") {
            self.parse_if_statement()
        } else if self.match_keyword("for") {
            self.parse_for_statement()
        } else if self.match_keyword("match") {
            self.parse_match_statement()
        } else {
            self.parse_text()
        }
    }

    fn parse_element(&mut self) -> Result<TemplateAst> {
        if !self.match_char('<') {
            return Err(self.error("Expected '<' to start element"));
        }

        let tag = self.parse_identifier()?;
        let mut attributes = Vec::new();
        let mut self_closing = false;

        self.skip_whitespace();

        // Parse attributes
        while !self.check_char('>') && !self.check_char('/') && !self.is_at_end() {
            let attr = self.parse_attribute()?;
            attributes.push(attr);
            self.skip_whitespace();
        }

        // Check for self-closing
        if self.match_char('/') {
            self_closing = true;
            if !self.match_char('>') {
                return Err(self.error("Expected '>' after '/' in self-closing tag"));
            }
            return Ok(TemplateAst::Element {
                tag,
                attributes,
                children: Vec::new(),
                self_closing,
            });
        }

        if !self.match_char('>') {
            return Err(self.error("Expected '>' to close opening tag"));
        }

        // Parse children
        let mut children = Vec::new();
        while !self.check_closing_tag(&tag) && !self.is_at_end() {
            // If we hit a template-body close `}` before the closing tag, the
            // element is unclosed. Bail out so the caller raises the "Expected
            // closing tag" error below instead of spinning forever on an empty
            // text node.
            if self.check_char('}') {
                break;
            }
            let child = self.parse_template_node()?;
            children.push(child);
        }

        // Parse closing tag
        self.skip_whitespace();
        if !self.match_str(&format!("</{}>", tag)) {
            return Err(self.error(&format!("Expected closing tag '</{}>", tag)));
        }

        Ok(TemplateAst::Element {
            tag,
            attributes,
            children,
            self_closing,
        })
    }

    fn parse_attribute(&mut self) -> Result<Attribute> {
        let name = self.parse_attribute_name()?;

        // Check for conditional attribute (disabled?)
        let conditional = self.match_char('?');

        self.skip_whitespace();
        if !self.match_char('=') {
            // Boolean attribute
            return Ok(Attribute {
                name,
                value: AttributeValue::Static("true".to_string()),
            });
        }

        self.skip_whitespace();

        let value = if self.check_char('{') {
            self.advance(); // consume '{'
            let expr = self.parse_expression_until(&['}'])?;
            if !self.match_char('}') {
                return Err(self.error("Expected '}' to close attribute expression"));
            }

            if conditional {
                AttributeValue::Conditional(expr)
            } else {
                AttributeValue::Expression(expr)
            }
        } else {
            let value = self.parse_string_literal()?;
            AttributeValue::Static(value)
        };

        Ok(Attribute { name, value })
    }

    fn parse_expression_node(&mut self) -> Result<TemplateAst> {
        if !self.match_char('{') {
            return Err(self.error("Expected '{' to start expression"));
        }

        // `{!expr}` denotes a raw-HTML expression: its runtime value is
        // injected verbatim via `Html::raw(...)` instead of going through
        // `Html::text(...)` which would HTML-escape the output.
        let raw = self.match_char('!');

        let expr = self.parse_expression_until(&['}'])?;

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close expression"));
        }

        // `{children}` (not `{children.foo}` or `{my.children}`) is the
        // slot-placeholder form. Only recognise it when the bare identifier
        // `children` appears with no further path/call syntax.
        if !raw && expr.trim() == "children" {
            return Ok(TemplateAst::Children);
        }

        if raw {
            Ok(TemplateAst::RawExpression(expr))
        } else {
            Ok(TemplateAst::Expression(expr))
        }
    }

    fn parse_component_invocation(&mut self) -> Result<TemplateAst> {
        if !self.match_char('@') {
            return Err(self.error("Expected '@' to start component invocation"));
        }

        let name = self.parse_identifier()?;
        self.skip_whitespace();

        if !self.match_char('(') {
            return Err(self.error("Expected '(' after component name"));
        }

        let mut props = Vec::new();
        self.skip_whitespace();

        while !self.check_char(')') && !self.is_at_end() {
            let prop_name = self.parse_identifier()?;
            self.skip_whitespace();

            if !self.match_char(':') {
                return Err(self.error("Expected ':' after prop name"));
            }

            self.skip_whitespace();
            let value = self.parse_expression_until(&[',', ')'])?;

            props.push(PropValue {
                name: prop_name,
                value,
            });

            self.skip_whitespace();
            if self.match_char(',') {
                self.skip_whitespace();
            } else if !self.check_char(')') {
                return Err(self.error("Expected ',' or ')' in component props"));
            }
        }

        if !self.match_char(')') {
            return Err(self.error("Expected ')' to close component invocation"));
        }

        // Optional body block: `@Card(title: "x") { <p/>More }`. The body
        // becomes the callee's `children` prop.
        self.skip_whitespace();
        let children = if self.check_char('{') {
            self.advance(); // consume '{'
            let body = self.parse_template_body()?;
            if !self.match_char('}') {
                return Err(self.error("Expected '}' to close component body"));
            }
            Some(Box::new(body))
        } else {
            None
        };

        Ok(TemplateAst::Component {
            name,
            props,
            children,
        })
    }

    fn parse_if_statement(&mut self) -> Result<TemplateAst> {
        self.skip_whitespace();
        let condition = self.parse_expression_until(&['{'])?;

        self.skip_whitespace();
        if !self.match_char('{') {
            return Err(self.error("Expected '{' after if condition"));
        }

        let then_branch = Box::new(self.parse_template_body()?);

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close if block"));
        }

        self.skip_whitespace();
        let else_branch = if self.match_keyword("else") {
            self.skip_whitespace();
            if !self.match_char('{') {
                return Err(self.error("Expected '{' after else"));
            }
            let else_body = Box::new(self.parse_template_body()?);
            if !self.match_char('}') {
                return Err(self.error("Expected '}' to close else block"));
            }
            Some(else_body)
        } else {
            None
        };

        Ok(TemplateAst::If {
            condition,
            then_branch,
            else_branch,
        })
    }

    fn parse_for_statement(&mut self) -> Result<TemplateAst> {
        self.skip_whitespace();
        let variable = self.parse_for_binding()?;

        self.skip_whitespace();
        if !self.match_keyword("in") {
            return Err(self.error("Expected 'in' after for variable"));
        }

        self.skip_whitespace();
        let iterable = self.parse_expression_until(&['{'])?;

        self.skip_whitespace();
        if !self.match_char('{') {
            return Err(self.error("Expected '{' after for expression"));
        }

        let body = Box::new(self.parse_template_body()?);

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close for block"));
        }

        Ok(TemplateAst::For {
            variable,
            iterable,
            body,
        })
    }

    fn parse_match_statement(&mut self) -> Result<TemplateAst> {
        self.skip_whitespace();
        let expression = self.parse_expression_until(&['{'])?;

        self.skip_whitespace();
        if !self.match_char('{') {
            return Err(self.error("Expected '{' after match expression"));
        }

        let mut arms = Vec::new();
        self.skip_whitespace();

        while !self.check_char('}') && !self.is_at_end() {
            let pattern = self.parse_expression_until(&['='])?;

            if !self.match_str("=>") {
                return Err(self.error("Expected '=>' after match pattern"));
            }

            self.skip_whitespace();
            if !self.match_char('{') {
                return Err(self.error("Expected '{' after '=>'"));
            }

            let body = self.parse_template_body()?;

            if !self.match_char('}') {
                return Err(self.error("Expected '}' to close match arm"));
            }

            arms.push(MatchArm { pattern, body });
            self.skip_whitespace();
        }

        if !self.match_char('}') {
            return Err(self.error("Expected '}' to close match block"));
        }

        Ok(TemplateAst::Match { expression, arms })
    }

    fn parse_text(&mut self) -> Result<TemplateAst> {
        let mut text = String::new();

        while !self.is_at_end() {
            let ch = self.current_char();

            if ch == '<' || ch == '{' || ch == '@' || ch == '}' {
                break;
            }

            if self.at_keyword(&["if", "for", "match", "else"]) {
                break;
            }

            text.push(ch);
            self.advance();
        }

        if text.trim().is_empty() {
            text = text.trim().to_string();
        }

        Ok(TemplateAst::Text(text))
    }

    // Utility methods
    fn parse_identifier(&mut self) -> Result<String> {
        let mut identifier = String::new();

        if !self.current_char().is_ascii_alphabetic() && self.current_char() != '_' {
            return Err(self.error("Expected identifier"));
        }

        while !self.is_at_end() {
            let ch = self.current_char();
            if ch.is_ascii_alphanumeric() || ch == '_' {
                identifier.push(ch);
                self.advance();
            } else {
                break;
            }
        }

        Ok(identifier)
    }

    /// Parse a `for` loop binding. Accepts either a bare identifier (`item`)
    /// or a tuple destructure pattern (`(key, value)`). Returned verbatim so
    /// codegen can parse it as a `syn::Pat`.
    fn parse_for_binding(&mut self) -> Result<String> {
        if self.check_char('(') {
            let mut out = String::new();
            let mut depth = 0i32;
            while !self.is_at_end() {
                let ch = self.current_char();
                out.push(ch);
                if ch == '(' {
                    depth += 1;
                } else if ch == ')' {
                    depth -= 1;
                    self.advance();
                    if depth == 0 {
                        return Ok(out);
                    }
                    continue;
                }
                self.advance();
            }
            return Err(self.error("Unterminated tuple pattern in for binding"));
        }
        self.parse_identifier()
    }

    /// Parse an HTML/XML attribute name. Like `parse_identifier` but also
    /// allows `-` (e.g. `aria-hidden`) and `:` (e.g. `xmlns:xlink`).
    fn parse_attribute_name(&mut self) -> Result<String> {
        let mut name = String::new();

        if !self.current_char().is_ascii_alphabetic() && self.current_char() != '_' {
            return Err(self.error("Expected attribute name"));
        }

        while !self.is_at_end() {
            let ch = self.current_char();
            if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' || ch == ':' {
                name.push(ch);
                self.advance();
            } else {
                break;
            }
        }

        Ok(name)
    }

    fn parse_string_literal(&mut self) -> Result<String> {
        if !self.match_char('"') {
            return Err(self.error("Expected '\"' to start string literal"));
        }

        let mut string = String::new();

        while !self.is_at_end() && !self.check_char('"') {
            let ch = self.current_char();
            if ch == '\\' {
                self.advance();
                if self.is_at_end() {
                    return Err(self.error("Unexpected end of input in string literal"));
                }
                let escaped = match self.current_char() {
                    'n' => '\n',
                    't' => '\t',
                    'r' => '\r',
                    '\\' => '\\',
                    '"' => '"',
                    c => c,
                };
                string.push(escaped);
            } else {
                string.push(ch);
            }
            self.advance();
        }

        if !self.match_char('"') {
            return Err(self.error("Expected '\"' to end string literal"));
        }

        Ok(string)
    }

    fn parse_type(&mut self) -> Result<String> {
        let mut type_str = String::new();
        let mut bracket_depth = 0;
        let mut angle_depth = 0;

        while !self.is_at_end() {
            let ch = self.current_char();

            match ch {
                '[' => bracket_depth += 1,
                ']' => bracket_depth -= 1,
                '<' => angle_depth += 1,
                '>' => angle_depth -= 1,
                ',' | '=' | '?' | ')' | '\n' if bracket_depth == 0 && angle_depth == 0 => break,
                '}' if bracket_depth == 0 && angle_depth == 0 => break,
                _ => {}
            }

            type_str.push(ch);
            self.advance();
        }

        Ok(type_str.trim().to_string())
    }

    fn parse_expression_until(&mut self, terminators: &[char]) -> Result<String> {
        let mut expr = String::new();
        let mut brace_depth = 0;
        let mut paren_depth = 0;
        let mut bracket_depth = 0;

        while !self.is_at_end() {
            let ch = self.current_char();

            match ch {
                '{' => {
                    if brace_depth == 0 && terminators.contains(&ch) {
                        break;
                    }
                    brace_depth += 1;
                }
                '}' => {
                    if brace_depth == 0 && terminators.contains(&ch) {
                        break;
                    }
                    brace_depth -= 1;
                }
                '(' => {
                    if paren_depth == 0 && terminators.contains(&ch) {
                        break;
                    }
                    paren_depth += 1;
                }
                ')' => {
                    if paren_depth == 0 && terminators.contains(&ch) {
                        break;
                    }
                    paren_depth -= 1;
                }
                '[' => {
                    if bracket_depth == 0 && terminators.contains(&ch) {
                        break;
                    }
                    bracket_depth += 1;
                }
                ']' => {
                    if bracket_depth == 0 && terminators.contains(&ch) {
                        break;
                    }
                    bracket_depth -= 1;
                }
                c if terminators.contains(&c)
                    && brace_depth == 0
                    && paren_depth == 0
                    && bracket_depth == 0 =>
                {
                    break;
                }
                _ => {}
            }

            expr.push(ch);
            self.advance();
        }

        Ok(expr.trim().to_string())
    }

    fn skip_whitespace(&mut self) {
        while !self.is_at_end() && self.current_char().is_whitespace() {
            if self.current_char() == '\n' {
                self.line += 1;
                self.column = 1;
            } else {
                self.column += 1;
            }
            self.position += 1;
        }
    }

    fn skip_whitespace_and_comments(&mut self) {
        loop {
            self.skip_whitespace();
            if self.match_str("//") {
                let mut text = String::new();
                while !self.is_at_end() && self.current_char() != '\n' {
                    text.push(self.current_char());
                    self.advance();
                }
                self.pending_comments.push(text.trim().to_string());
            } else if self.match_str("/*") {
                let mut text = String::new();
                while !self.is_at_end() && !self.match_str("*/") {
                    text.push(self.current_char());
                    self.advance();
                }
                self.pending_comments.push(text.trim().to_string());
            } else {
                break;
            }
        }
    }

    /// Drain any buffered comments. Called by each top-level parse_*
    /// so whatever the lexer has collected attaches to the next decl.
    fn take_pending_comments(&mut self) -> Vec<String> {
        std::mem::take(&mut self.pending_comments)
    }

    fn current_char(&self) -> char {
        if self.is_at_end() {
            '\0'
        } else {
            self.input[self.position]
        }
    }

    fn peek_string(&self, len: usize) -> String {
        if self.position + len > self.input.len() {
            return String::new();
        }
        self.input.iter().skip(self.position).take(len).collect()
    }

    fn parse_doctype(&mut self) -> Result<TemplateAst> {
        // Consume the entire DOCTYPE declaration
        let start_pos = self.position;

        // Move past '<'
        self.advance();

        // Read until we find the closing '>'
        while !self.is_at_end() && self.current_char() != '>' {
            self.advance();
        }

        if self.is_at_end() {
            return Err(self.error("Unterminated DOCTYPE declaration"));
        }

        // Consume the closing '>'
        self.advance();

        // Extract the full DOCTYPE text
        let end_pos = self.position;
        let doctype_text: String = self
            .input
            .iter()
            .skip(start_pos)
            .take(end_pos - start_pos)
            .collect();

        Ok(TemplateAst::Text(doctype_text))
    }

    fn advance(&mut self) {
        if !self.is_at_end() {
            if self.current_char() == '\n' {
                self.line += 1;
                self.column = 1;
            } else {
                self.column += 1;
            }
            self.position += 1;
        }
    }

    fn is_at_end(&self) -> bool {
        self.position >= self.input.len()
    }

    fn check_char(&self, expected: char) -> bool {
        !self.is_at_end() && self.current_char() == expected
    }

    fn match_char(&mut self, expected: char) -> bool {
        if self.check_char(expected) {
            self.advance();
            true
        } else {
            false
        }
    }

    fn match_str(&mut self, expected: &str) -> bool {
        let expected_chars: Vec<char> = expected.chars().collect();

        if self.position + expected_chars.len() > self.input.len() {
            return false;
        }

        for (i, &expected_char) in expected_chars.iter().enumerate() {
            if self.input[self.position + i] != expected_char {
                return false;
            }
        }

        for _ in 0..expected_chars.len() {
            self.advance();
        }
        true
    }

    fn match_keyword(&mut self, keyword: &str) -> bool {
        let start_pos = self.position;
        let start_line = self.line;
        let start_column = self.column;

        if self.match_str(keyword) {
            // Check that it's not part of a larger identifier
            if self.is_at_end()
                || !self.current_char().is_ascii_alphanumeric() && self.current_char() != '_'
            {
                return true;
            }
        }

        // Restore position if not a complete keyword
        self.position = start_pos;
        self.line = start_line;
        self.column = start_column;
        false
    }

    /// Return the position after skipping any run of whitespace starting at
    /// `self.position`, without mutating the parser state.
    fn cursor_after_whitespace(&self) -> usize {
        let mut i = self.position;
        while i < self.input.len() && self.input[i].is_whitespace() {
            i += 1;
        }
        i
    }

    /// Like `at_keyword`, but checks at an arbitrary position `pos` instead of
    /// `self.position`. Used for lookahead.
    fn at_keyword_at(&self, pos: usize, keywords: &[&str]) -> bool {
        for &keyword in keywords {
            let kw: Vec<char> = keyword.chars().collect();
            if pos + kw.len() <= self.input.len() {
                let mut matches = true;
                for (i, &ch) in kw.iter().enumerate() {
                    if self.input[pos + i] != ch {
                        matches = false;
                        break;
                    }
                }
                if matches {
                    let after = pos + kw.len();
                    if after >= self.input.len()
                        || !(self.input[after].is_ascii_alphanumeric() || self.input[after] == '_')
                    {
                        return true;
                    }
                }
            }
        }
        false
    }

    fn at_keyword(&self, keywords: &[&str]) -> bool {
        for &keyword in keywords {
            let keyword_chars: Vec<char> = keyword.chars().collect();

            if self.position + keyword_chars.len() <= self.input.len() {
                let mut matches = true;
                for (i, &expected_char) in keyword_chars.iter().enumerate() {
                    if self.input[self.position + i] != expected_char {
                        matches = false;
                        break;
                    }
                }

                if matches {
                    // Check that it's not part of a larger identifier
                    let next_pos = self.position + keyword_chars.len();
                    if next_pos >= self.input.len()
                        || (!self.input[next_pos].is_ascii_alphanumeric()
                            && self.input[next_pos] != '_')
                    {
                        return true;
                    }
                }
            }
        }
        false
    }

    fn check_closing_tag(&self, tag: &str) -> bool {
        let closing_tag = format!("</{}>", tag);
        let closing_chars: Vec<char> = closing_tag.chars().collect();

        // Skip whitespace to find the closing tag
        let mut pos = self.position;
        while pos < self.input.len() && self.input[pos].is_whitespace() {
            pos += 1;
        }

        if pos + closing_chars.len() > self.input.len() {
            return false;
        }

        for (i, &expected_char) in closing_chars.iter().enumerate() {
            if self.input[pos + i] != expected_char {
                return false;
            }
        }

        true
    }

    fn error(&self, message: &str) -> CompileError {
        CompileError::parse(self.format_error(message))
    }

    /// Build a rustc-style framed error message. Shows the offending line
    /// (plus up to two lines of leading context) with a caret under the
    /// offending column.
    fn format_error(&self, message: &str) -> String {
        let source: String = self.input.iter().collect();
        let lines: Vec<&str> = source.lines().collect();
        // `self.line` is 1-indexed; clamp so out-of-range errors don't panic.
        let err_line_idx = (self.line.saturating_sub(1)).min(lines.len().saturating_sub(1));
        let start_line_idx = err_line_idx.saturating_sub(2);

        let mut out = String::new();
        out.push_str(&format!(
            "{} at line {}, column {}\n",
            message, self.line, self.column
        ));

        // Width of the line-number gutter (at least 2 chars for aesthetics).
        let gutter = std::cmp::max(2, (err_line_idx + 1).to_string().len());

        out.push_str(&format!("{:>width$} |\n", "", width = gutter));
        for (offset, line) in lines[start_line_idx..=err_line_idx].iter().enumerate() {
            let lineno = start_line_idx + offset + 1;
            out.push_str(&format!("{:>width$} | {}\n", lineno, line, width = gutter));
        }
        // Caret under the offending column. column is 1-indexed.
        let caret_pad = self.column.saturating_sub(1);
        out.push_str(&format!(
            "{:>width$} | {}^ {}\n",
            "",
            " ".repeat(caret_pad),
            message,
            width = gutter
        ));

        out
    }
}

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

    #[test]
    fn test_parse_component_generics() {
        let input = r#"
component Box<T> {
    props {
        value: T,
    }
}
"#;
        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();
        let component = &result.components[0];
        assert_eq!(component.generics.len(), 1);
        assert_eq!(component.generics[0].name, "T");
        assert!(component.generics[0].bounds.is_empty());
    }

    #[test]
    fn test_parse_component_generics_with_bounds() {
        let input = r#"
component List<T: Clone + Display, U> {
    props {
        items: Vec<T>,
    }
}
"#;
        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();
        let component = &result.components[0];
        assert_eq!(component.generics.len(), 2);
        assert_eq!(component.generics[0].name, "T");
        assert_eq!(component.generics[0].bounds, vec!["Clone", "Display"]);
        assert_eq!(component.generics[1].name, "U");
        assert!(component.generics[1].bounds.is_empty());
    }

    #[test]
    fn test_parse_template_generics() {
        let input = r#"
component Box<T> {
    props {
        value: T,
    }
}

ruitl Box<T>(value: T) {
    <div>{value}</div>
}
"#;
        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();
        assert_eq!(result.templates.len(), 1);
        assert_eq!(result.templates[0].generics.len(), 1);
        assert_eq!(result.templates[0].generics[0].name, "T");
    }

    #[test]
    fn test_parse_identifier() {
        let mut parser = RuitlParser::new("hello_world".to_string());
        let result = parser.parse_identifier().unwrap();
        assert_eq!(result, "hello_world");
    }

    #[test]
    fn test_parse_string_literal() {
        let mut parser = RuitlParser::new("\"hello world\"".to_string());
        let result = parser.parse_string_literal().unwrap();
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_parse_simple_component() {
        let input = r#"
component Button {
    props {
        text: String,
        disabled: bool = false,
    }
}
        "#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();

        assert_eq!(result.components.len(), 1);
        let component = &result.components[0];
        assert_eq!(component.name, "Button");
        assert_eq!(component.props.len(), 2);

        assert_eq!(component.props[0].name, "text");
        assert_eq!(component.props[0].prop_type, "String");
        assert!(!component.props[0].optional);

        assert_eq!(component.props[1].name, "disabled");
        assert_eq!(component.props[1].prop_type, "bool");
        // A prop with a default value is not Option-wrapped; `optional`
        // tracks explicit `?` markers only.
        assert!(!component.props[1].optional);
        assert_eq!(component.props[1].default_value, Some("false".to_string()));
    }

    #[test]
    fn test_parse_simple_template() {
        let input = r#"
ruitl Greeting(name: String) {
    <div class="greeting">
        <h1>Hello, {name}!</h1>
    </div>
}
        "#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();

        assert_eq!(result.templates.len(), 1);
        let template = &result.templates[0];
        assert_eq!(template.name, "Greeting");
        assert_eq!(template.params.len(), 1);
        assert_eq!(template.params[0].name, "name");
        assert_eq!(template.params[0].param_type, "String");
    }

    #[test]
    fn test_parse_import() {
        let input = r#"import "std::collections" { HashMap, Vec }"#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();

        assert_eq!(result.imports.len(), 1);
        let import = &result.imports[0];
        assert_eq!(import.path, "std::collections");
        assert_eq!(import.items, vec!["HashMap", "Vec"]);
    }

    #[test]
    fn test_parse_element_with_attributes() {
        let input = r#"<button class="btn" disabled?={is_disabled}>Click me</button>"#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_element().unwrap();

        if let TemplateAst::Element {
            tag,
            attributes,
            children,
            ..
        } = result
        {
            assert_eq!(tag, "button");
            assert_eq!(attributes.len(), 2);

            assert_eq!(attributes[0].name, "class");
            if let AttributeValue::Static(value) = &attributes[0].value {
                assert_eq!(value, "btn");
            } else {
                panic!("Expected static attribute value");
            }

            assert_eq!(attributes[1].name, "disabled");
            if let AttributeValue::Conditional(expr) = &attributes[1].value {
                assert_eq!(expr, "is_disabled");
            } else {
                panic!("Expected conditional attribute value");
            }

            assert_eq!(children.len(), 1);
            if let TemplateAst::Text(text) = &children[0] {
                assert_eq!(text, "Click me");
            } else {
                panic!("Expected text child");
            }
        } else {
            panic!("Expected element AST node");
        }
    }

    #[test]
    fn test_parse_expression() {
        let input = r#"{user.name.to_uppercase()}"#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_expression_node().unwrap();

        if let TemplateAst::Expression(expr) = result {
            assert_eq!(expr, "user.name.to_uppercase()");
        } else {
            panic!("Expected expression AST node");
        }
    }

    #[test]
    fn test_parse_component_invocation() {
        let input = r#"@Button(text: "Click me", disabled: false)"#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_component_invocation().unwrap();

        if let TemplateAst::Component {
            name,
            props,
            children,
        } = result
        {
            assert_eq!(name, "Button");
            assert_eq!(props.len(), 2);
            assert!(children.is_none(), "no body block → children is None");

            assert_eq!(props[0].name, "text");
            assert_eq!(props[0].value, "\"Click me\"");

            assert_eq!(props[1].name, "disabled");
            assert_eq!(props[1].value, "false");
        } else {
            panic!("Expected component AST node");
        }
    }

    #[test]
    fn test_parse_component_with_body() {
        let input = r#"@Card(title: "Hi") { <p>Body</p> }"#;
        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_component_invocation().unwrap();

        let TemplateAst::Component {
            name,
            props,
            children,
        } = result
        else {
            panic!("expected Component")
        };
        assert_eq!(name, "Card");
        assert_eq!(props.len(), 1);
        let body = children.expect("body block must be captured as children");
        // The body should contain an element `<p>Body</p>`.
        let children_vec = match *body {
            TemplateAst::Fragment(v) => v,
            other => vec![other],
        };
        let has_p = children_vec.iter().any(|n| matches!(n, TemplateAst::Element { tag, .. } if tag == "p"));
        assert!(has_p, "body must contain <p> element");
    }

    #[test]
    fn test_children_keyword_node() {
        let input = "{children}";
        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_expression_node().unwrap();
        assert!(
            matches!(result, TemplateAst::Children),
            "bare `{{children}}` must emit TemplateAst::Children, got {:?}",
            result
        );
    }

    #[test]
    fn test_dotted_children_is_expression_not_slot() {
        let input = "{my.children}";
        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_expression_node().unwrap();
        // Dotted `children` is a regular field access — NOT the slot form.
        assert!(
            matches!(result, TemplateAst::Expression(ref s) if s == "my.children"),
            "`{{my.children}}` must parse as Expression, got {:?}",
            result
        );
    }

    #[test]
    fn test_parse_if_statement() {
        let input = r#"if show_message { <p>Hello!</p> } else { <p>Goodbye!</p> }"#;

        let mut parser = RuitlParser::new(input.to_string());
        parser.match_keyword("if"); // Consume the "if" keyword first
        let result = parser.parse_if_statement().unwrap();

        if let TemplateAst::If {
            condition,
            then_branch,
            else_branch,
        } = result
        {
            assert_eq!(condition, "show_message");
            assert!(then_branch.as_ref().is_element_with_tag("p"));
            assert!(else_branch.is_some());
            assert!(else_branch.unwrap().as_ref().is_element_with_tag("p"));
        } else {
            panic!("Expected if AST node");
        }
    }

    #[test]
    fn test_parse_for_statement() {
        let input = r#"for item in items { <li>{item}</li> }"#;

        let mut parser = RuitlParser::new(input.to_string());
        parser.match_keyword("for"); // Consume the "for" keyword first
        let result = parser.parse_for_statement().unwrap();

        if let TemplateAst::For {
            variable,
            iterable,
            body,
        } = result
        {
            assert_eq!(variable, "item");
            assert_eq!(iterable, "items");
            assert!(body.as_ref().is_element_with_tag("li"));
        } else {
            panic!("Expected for AST node");
        }
    }

    #[test]
    fn test_parse_self_closing_element() {
        let input = r#"<img src="photo.jpg" alt="Photo" />"#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse_element().unwrap();

        if let TemplateAst::Element {
            tag,
            attributes,
            children,
            self_closing,
        } = result
        {
            assert_eq!(tag, "img");
            assert!(self_closing);
            assert!(children.is_empty());
            assert_eq!(attributes.len(), 2);
        } else {
            panic!("Expected element AST node");
        }
    }

    #[test]
    fn test_parse_complex_template() {
        let input = r#"
import "std::collections" { HashMap }

component UserCard {
    props {
        user: User,
        show_email: bool = true,
    }
}

ruitl UserCard(props: UserCardProps) {
    <div class="user-card">
        <h2>{props.user.name}</h2>
        if props.show_email {
            <p class="email">{props.user.email}</p>
        }
        <ul>
            for skill in props.user.skills {
                <li>{skill}</li>
            }
        </ul>
    </div>
}
        "#;

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse().unwrap();

        assert_eq!(result.imports.len(), 1);
        assert_eq!(result.components.len(), 1);
        assert_eq!(result.templates.len(), 1);

        let component = &result.components[0];
        assert_eq!(component.name, "UserCard");
        assert_eq!(component.props.len(), 2);

        let template = &result.templates[0];
        assert_eq!(template.name, "UserCard");
        assert_eq!(template.params.len(), 1);
    }

    #[test]
    fn test_parse_error_handling() {
        let input = r#"component Button { props { text String } }"#; // Missing colon

        let mut parser = RuitlParser::new(input.to_string());
        let result = parser.parse();

        assert!(result.is_err());
    }

    #[test]
    fn test_parse_nested_elements() {
        let input = r#"
<div class="container">
    <header>
        <h1>Title</h1>
        <nav>
            <a href="/">Home</a>
            <a href="/about">About</a>
        </nav>
    </header>
    <main>
        <p>Content goes here</p>
    </main>
</div>
        "#;

        let mut parser = RuitlParser::new(input.to_string());
        parser.skip_whitespace(); // Skip leading whitespace
        let result = parser.parse_element().unwrap();

        if let TemplateAst::Element { tag, children, .. } = result {
            assert_eq!(tag, "div");
            assert_eq!(children.len(), 2); // header and main (text nodes are trimmed)
        } else {
            panic!("Expected element AST node");
        }
    }

    // Helper trait for tests
    trait TestAstHelper {
        fn is_element_with_tag(&self, expected_tag: &str) -> bool;
    }

    impl TestAstHelper for TemplateAst {
        fn is_element_with_tag(&self, tag: &str) -> bool {
            match self {
                TemplateAst::Element {
                    tag: element_tag, ..
                } => element_tag == tag,
                _ => false,
            }
        }
    }
}