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
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
/// CST Parser - parses token indices into Green Tree
/// This is a simple recursive descent parser that produces a CST
use super::green::{GreenNodeArena, GreenNodeId, GreenTreeBuilder, Marker, SyntaxKind};
use super::preparser::PreParsedTokens;
use super::token::{Token, TokenKind};
use thiserror::Error;
/// Error detail - structured error information
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ErrorDetail {
/// Expected token not found (e.g., expected `)` but found `}`)
#[error("Expected {expected}, found {found}")]
UnexpectedToken { expected: String, found: String },
/// Unexpected end of input
#[error("Unexpected end of input, expected {expected}")]
UnexpectedEof { expected: String },
/// Invalid syntax with reason
#[error("Invalid syntax: {reason}")]
InvalidSyntax { reason: String },
}
/// Parser error with recovery information
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("{detail}")]
pub struct ParserError {
/// Token index where the error occurred
pub token_index: usize,
/// Error detail
pub detail: ErrorDetail,
}
impl ParserError {
pub fn unexpected_token(token_index: usize, expected: &str, found: &str) -> Self {
Self {
token_index,
detail: ErrorDetail::UnexpectedToken {
expected: expected.to_string(),
found: found.to_string(),
},
}
}
pub fn unexpected_eof(token_index: usize, expected: &str) -> Self {
Self {
token_index,
detail: ErrorDetail::UnexpectedEof {
expected: expected.to_string(),
},
}
}
pub fn invalid_syntax(token_index: usize, reason: &str) -> Self {
Self {
token_index,
detail: ErrorDetail::InvalidSyntax {
reason: reason.to_string(),
},
}
}
}
/// Helper trait to reduce node boilerplate
trait NodeBuilder {
fn emit_node<F>(&mut self, kind: SyntaxKind, f: F)
where
F: FnOnce(&mut Self);
}
impl NodeBuilder for Parser<'_> {
fn emit_node<F>(&mut self, kind: SyntaxKind, f: F)
where
F: FnOnce(&mut Self),
{
self.builder.start_node(kind);
f(self);
self.builder.finish_node();
}
}
/// Parser state
pub struct Parser<'a> {
tokens: Vec<Token>,
preparsed: &'a PreParsedTokens,
current: usize, // Index into preparsed.token_indices
builder: GreenTreeBuilder,
/// Collected parser errors for error recovery
errors: Vec<ParserError>,
}
/// Maximum lookahead distance for disambiguation
const MAX_LOOKAHEAD: usize = 20;
impl<'a> Parser<'a> {
pub fn new(tokens: Vec<Token>, preparsed: &'a PreParsedTokens) -> Self {
Self {
tokens,
preparsed,
current: 0,
builder: GreenTreeBuilder::new(),
errors: Vec::new(),
}
}
/// Record a parser error
fn add_error(&mut self, error: ParserError) {
self.errors.push(error);
}
/// Get the current token index in the original token array
fn current_token_index(&self) -> usize {
self.preparsed
.token_indices
.get(self.current)
.copied()
.unwrap_or(0)
}
/// Get the current token kind
fn peek(&self) -> Option<TokenKind> {
self.preparsed
.get_token(self.current, &self.tokens)
.map(|t| t.kind)
}
/// Peek ahead n tokens
fn peek_ahead(&self, n: usize) -> Option<TokenKind> {
self.preparsed
.get_token(self.current + n, &self.tokens)
.map(|t| t.kind)
}
/// Check if current position starts a path (possibly qualified) followed by macro expansion (!)
/// Returns the offset where MacroExpand is found, or None if not a macro expansion
fn find_macro_expand_after_path(&self) -> Option<usize> {
let mut offset = 0;
// First must be Ident
if self.peek_ahead(offset) != Some(TokenKind::Ident) {
return None;
}
offset += 1;
// Consume :: Ident pairs
while self.peek_ahead(offset) == Some(TokenKind::DoubleColon) {
offset += 1; // ::
if self.peek_ahead(offset) != Some(TokenKind::Ident) {
return None; // Malformed path
}
offset += 1; // Ident
}
// Check if followed by MacroExpand
if self.peek_ahead(offset) == Some(TokenKind::MacroExpand) {
Some(offset)
} else {
None
}
}
/// Get the current token
#[allow(dead_code)]
fn current_token(&self) -> Option<&Token> {
self.preparsed.get_token(self.current, &self.tokens)
}
/// Check if current token matches the expected kind
fn check(&self, kind: TokenKind) -> bool {
self.peek() == Some(kind)
}
/// Advance to the next token and add current to the tree
fn bump(&mut self) {
if let Some(&token_idx) = self.preparsed.token_indices.get(self.current)
&& let Some(token) = self.tokens.get(token_idx)
{
self.builder.add_token(token_idx, token.length);
}
self.current += 1;
}
/// Expect a specific token kind and consume it
/// Returns true if the token matched, false otherwise (and records error)
fn expect(&mut self, kind: TokenKind) -> bool {
if self.check(kind) {
self.bump();
true
} else if self.is_at_end() {
self.add_error(ParserError::unexpected_eof(
self.current_token_index(),
&format!("{kind:?}"),
));
false
} else {
let found = self.peek().map(|k| format!("{k:?}")).unwrap_or_default();
self.add_error(ParserError::unexpected_token(
self.current_token_index(),
&format!("{kind:?}"),
&found,
));
false
}
}
/// Expect any of the given token kinds and consume the first match.
/// Returns true if matched; otherwise records an error and returns false.
fn expects(&mut self, kinds: &[TokenKind]) -> bool {
let expected = kinds
.iter()
.map(|k| format!("{k:?}"))
.collect::<Vec<_>>()
.join(" or ");
match self.peek() {
Some(kind) if kinds.contains(&kind) => {
self.bump();
true
}
Some(found) => {
self.add_error(ParserError::unexpected_token(
self.current_token_index(),
&expected,
&format!("{found:?}"),
));
false
}
None => {
self.add_error(ParserError::unexpected_eof(
self.current_token_index(),
&expected,
));
false
}
}
}
/// Expect all token kinds in order, consuming each. Returns true if all matched.
fn expect_all(&mut self, kinds: &[TokenKind]) -> bool {
kinds
.iter()
.fold(true, |acc, &kind| acc & self.expect(kind))
}
/// Parse the entire program
pub fn parse(mut self) -> (GreenNodeId, GreenNodeArena, Vec<Token>, Vec<ParserError>) {
self.builder.start_node(SyntaxKind::Program);
while !self.is_at_end() {
let before = self.current;
self.parse_statement();
if self.current == before && !self.is_at_end() {
self.add_error(ParserError::invalid_syntax(
self.current_token_index(),
"parser made no progress while parsing statement; skipping token for recovery",
));
self.bump();
}
}
let root_id = self.builder.finish_node().unwrap();
let arena = self.builder.into_arena();
let errors = self.errors;
(root_id, arena, self.tokens, errors)
}
/// Check if we've reached the end
fn is_at_end(&self) -> bool {
self.peek().is_none_or(|k| k == TokenKind::Eof)
}
/// Check if the previous token has a linebreak in its trailing trivia
fn has_trailing_linebreak(&self) -> bool {
if self.current == 0 {
return false;
}
let prev_idx = self.current - 1;
if let Some(trivia_indices) = self.preparsed.trailing_trivia_map.get(&prev_idx) {
for &trivia_idx in trivia_indices {
if let Some(token) = self.tokens.get(trivia_idx)
&& token.kind == TokenKind::LineBreak
{
return true;
}
}
}
if let Some(trivia_indices) = self.preparsed.leading_trivia_map.get(&self.current) {
for &trivia_idx in trivia_indices {
if let Some(token) = self.tokens.get(trivia_idx)
&& token.kind == TokenKind::LineBreak
{
return true;
}
}
}
false
}
/// Get previous token kind if there is no trivia between previous and current tokens.
fn prev_kind_if_adjacent(&self) -> Option<TokenKind> {
if self.current == 0 {
return None;
}
let curr_raw = *self.preparsed.token_indices.get(self.current)?;
let prev_raw = *self.preparsed.token_indices.get(self.current - 1)?;
if curr_raw == prev_raw + 1 {
self.tokens.get(prev_raw).map(|t| t.kind)
} else {
None
}
}
/// Parse a statement
fn parse_statement(&mut self) {
self.emit_node(SyntaxKind::Statement, |this| {
// Check for visibility modifier first
let has_pub = this.check(TokenKind::Pub);
if has_pub {
this.emit_node(SyntaxKind::VisibilityPub, |this| {
this.bump(); // consume 'pub'
});
}
match this.peek() {
Some(TokenKind::Function) => this.parse_function_decl(),
Some(TokenKind::Macro) => this.parse_macro_decl(),
Some(TokenKind::Let) => this.parse_let_decl(),
Some(TokenKind::LetRec) => this.parse_letrec_decl(),
Some(TokenKind::Include) => this.parse_include_stmt(),
Some(TokenKind::Sharp) => this.parse_stage_decl(),
Some(TokenKind::Mod) => this.parse_module_decl(),
Some(TokenKind::Use) => this.parse_use_stmt(),
Some(TokenKind::Type) => {
// Check if this is "type alias", "type rec", or just "type"
if this.peek_ahead(1) == Some(TokenKind::Alias) {
this.parse_type_alias_decl();
} else {
this.parse_type_decl();
}
}
_ => {
// Try parsing as expression
this.parse_expr();
}
}
});
}
/// Parse module declaration: mod name { ... } or mod name;
fn parse_module_decl(&mut self) {
self.emit_node(SyntaxKind::ModuleDecl, |this| {
this.expect(TokenKind::Mod);
this.expect(TokenKind::Ident);
// Check if this is an external file module (mod foo;) or inline module (mod foo { ... })
if this.check(TokenKind::LineBreak) {
// External file module: mod foo;
// Just consume the line break, no body
this.bump();
} else if this.check(TokenKind::BlockBegin) {
// Inline module: mod foo { ... }
this.expect(TokenKind::BlockBegin);
// Parse module body (statements)
while !this.check(TokenKind::BlockEnd) && !this.is_at_end() {
let before = this.current;
this.parse_statement();
if this.current == before && !this.is_at_end() {
this.add_error(ParserError::invalid_syntax(
this.current_token_index(),
"parser made no progress in module; skipping token for recovery",
));
this.bump();
}
}
this.expect(TokenKind::BlockEnd);
}
// If neither, error recovery will handle it
});
}
/// Parse use statement:
/// - `use path::to::module` (single import)
/// - `use path::{a, b, c}` (multiple imports)
/// - `use path::*` (wildcard import)
fn parse_use_stmt(&mut self) {
self.emit_node(SyntaxKind::UseStmt, |this| {
this.expect(TokenKind::Use);
this.parse_use_path();
});
}
/// Parse use path with support for multiple/wildcard imports
fn parse_use_path(&mut self) {
self.emit_node(SyntaxKind::QualifiedPath, |this| {
this.expect(TokenKind::Ident);
while this.check(TokenKind::DoubleColon) {
this.bump(); // consume '::'
// Check for wildcard: use foo::*
if this.check(TokenKind::OpProduct) {
this.emit_node(SyntaxKind::UseTargetWildcard, |this2| {
this2.bump(); // consume '*'
});
return;
}
// Check for multiple imports: use foo::{a, b}
if this.check(TokenKind::BlockBegin) {
this.emit_node(SyntaxKind::UseTargetMultiple, |this2| {
this2.bump(); // consume '{'
// Parse comma-separated identifiers
if this2.check(TokenKind::Ident) {
this2.bump();
while this2.check(TokenKind::Comma) {
this2.bump(); // consume ','
this2.expect(TokenKind::Ident);
}
}
this2.expect(TokenKind::BlockEnd);
});
return;
}
// Regular identifier
this.expect(TokenKind::Ident);
}
});
}
/// Parse qualified path: ident (:: ident)*
fn parse_qualified_path(&mut self) {
self.emit_node(SyntaxKind::QualifiedPath, |this| {
this.expect(TokenKind::Ident);
while this.check(TokenKind::DoubleColon) {
this.bump(); // consume '::'
this.expect(TokenKind::Ident);
}
});
}
/// Parse macro declaration: macro name(params) { body }
fn parse_macro_decl(&mut self) {
self.emit_node(SyntaxKind::FunctionDecl, |this| {
this.expect(TokenKind::Macro);
// Mark macro name with IdentFunction kind
if this.check(TokenKind::Ident) {
if let Some(&token_idx) = this.preparsed.token_indices.get(this.current) {
this.tokens[token_idx].kind = TokenKind::IdentFunction;
}
this.bump();
}
// Parameters
if this.check(TokenKind::ParenBegin) {
this.parse_param_list();
}
// Optional return type annotation after '->'
if this.check(TokenKind::Arrow) {
this.bump();
this.parse_type();
}
// Body
if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
}
});
}
/// Parse include statement: include("filename.mmm")
fn parse_include_stmt(&mut self) {
self.emit_node(SyntaxKind::IncludeStmt, |this| {
this.expect_all(&[
TokenKind::Include,
TokenKind::ParenBegin,
TokenKind::Str,
TokenKind::ParenEnd,
]);
});
}
/// Parse stage declaration: #stage(main) or #stage(macro)
fn parse_stage_decl(&mut self) {
self.emit_node(SyntaxKind::StageDecl, |this| {
this.expect_all(&[TokenKind::Sharp, TokenKind::StageKwd, TokenKind::ParenBegin]);
// Expect either 'main' or 'macro'
this.expects(&[TokenKind::Main, TokenKind::Macro]);
this.expect(TokenKind::ParenEnd);
});
}
/// Parse macro expansion: MacroName!(args) or Qualified::Path!(args)
fn parse_macro_expansion(&mut self) {
self.emit_node(SyntaxKind::MacroExpansion, |this| {
// Parse the macro name which can be a simple identifier or qualified path
// Check if it's a qualified path
if this.peek_ahead(1) == Some(TokenKind::DoubleColon) {
this.parse_qualified_path();
} else {
this.expect(TokenKind::Ident);
}
// '!('
this.expect_all(&[TokenKind::MacroExpand, TokenKind::ParenBegin]);
// Parse arguments as expression list
if !this.check(TokenKind::ParenEnd) {
this.parse_expr();
while this.check(TokenKind::Comma) {
this.bump();
if !this.check(TokenKind::ParenEnd) {
this.parse_expr();
}
}
}
this.expect(TokenKind::ParenEnd);
});
}
/// Parse bracket (quote) expression: `expr or `{block}
fn parse_bracket_expr(&mut self) {
self.emit_node(SyntaxKind::BracketExpr, |this| {
this.expect(TokenKind::BackQuote);
// Check if it's a block or single expression
if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
} else {
this.parse_expr();
}
});
}
/// Parse escape (unquote) expression: $expr
fn parse_escape_expr(&mut self) {
self.emit_node(SyntaxKind::EscapeExpr, |this| {
this.expect(TokenKind::Dollar);
this.parse_prefix_expr();
});
}
/// Parse function declaration: fn name(params) { body }
fn parse_function_decl(&mut self) {
self.emit_node(SyntaxKind::FunctionDecl, |this| {
this.expect(TokenKind::Function);
// Mark function name with IdentFunction kind
if this.check(TokenKind::Ident) {
if let Some(&token_idx) = this.preparsed.token_indices.get(this.current) {
this.tokens[token_idx].kind = TokenKind::IdentFunction;
}
this.bump();
}
// Parameters
if this.check(TokenKind::ParenBegin) {
this.parse_param_list();
}
// Optional return type annotation after '->'
if this.check(TokenKind::Arrow) {
this.bump();
this.parse_type();
}
// Body
if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
}
});
}
/// Parse let declaration: let pattern = expr
fn parse_let_decl(&mut self) {
self.emit_node(SyntaxKind::LetDecl, |this| {
this.expect(TokenKind::Let);
this.parse_pattern(); // Parse pattern instead of just identifier
// Optional type annotation after ':'
if this.check(TokenKind::Colon) {
this.parse_type_annotation();
}
if this.expect(TokenKind::Assign) {
this.parse_expr();
}
});
}
/// Parse letrec declaration
fn parse_letrec_decl(&mut self) {
self.emit_node(SyntaxKind::LetRecDecl, |this| {
this.expect_all(&[TokenKind::LetRec, TokenKind::Ident]);
if this.expect(TokenKind::Assign) {
this.parse_expr();
}
});
}
/// Parse pattern: single identifier, tuple pattern, or record pattern
fn parse_pattern(&mut self) {
match self.peek() {
Some(TokenKind::Ident) | Some(TokenKind::PlaceHolder) => {
// Single pattern: x or _
self.emit_node(SyntaxKind::SinglePattern, |this| {
this.bump();
});
}
Some(TokenKind::ParenBegin) => {
// Tuple pattern: (x, y, z)
self.parse_tuple_pattern();
}
Some(TokenKind::BlockBegin) => {
// Record pattern: {a = x, b = y}
self.parse_record_pattern();
}
Some(TokenKind::BackQuote) => {
// Code type: `Type
self.emit_node(SyntaxKind::CodeType, |inner| {
inner.expect(TokenKind::BackQuote);
inner.parse_type();
});
}
_ => {
// Error: unexpected token
if let Some(kind) = self.peek() {
self.add_error(ParserError::unexpected_token(
self.current_token_index(),
"pattern (identifier, tuple, or record)",
&format!("{kind:?}"),
));
}
self.bump();
}
}
}
/// Parse tuple pattern: (x, y, z)
fn parse_tuple_pattern(&mut self) {
self.emit_node(SyntaxKind::TuplePattern, |this| {
this.expect(TokenKind::ParenBegin);
if !this.check(TokenKind::ParenEnd) {
this.parse_pattern();
while this.check(TokenKind::Comma) {
this.bump(); // ,
if !this.check(TokenKind::ParenEnd) {
this.parse_pattern();
}
}
}
this.expect(TokenKind::ParenEnd);
});
}
/// Parse record pattern: {a = x, b = y}
fn parse_record_pattern(&mut self) {
self.emit_node(SyntaxKind::RecordPattern, |this| {
this.expect(TokenKind::BlockBegin);
if !this.check(TokenKind::BlockEnd) {
// Parse field: name = pattern
this.expect_all(&[TokenKind::Ident, TokenKind::Assign]);
this.parse_pattern();
while this.check(TokenKind::Comma) {
this.bump(); // ,
if !this.check(TokenKind::BlockEnd) {
this.expect_all(&[TokenKind::Ident, TokenKind::Assign]);
this.parse_pattern();
}
}
}
this.expect(TokenKind::BlockEnd);
});
}
/// Parse parameter list: (param1, param2, ...) or (param1: Type, param2: Type)
fn parse_param_list(&mut self) {
self.emit_node(SyntaxKind::ParamList, |this| {
this.expect(TokenKind::ParenBegin);
while !this.check(TokenKind::ParenEnd) && !this.is_at_end() {
// Mark parameter name with IdentParameter kind
if this.check(TokenKind::Ident) {
if let Some(&token_idx) = this.preparsed.token_indices.get(this.current) {
// Change token kind to IdentParameter
this.tokens[token_idx].kind = TokenKind::IdentParameter;
}
this.bump();
// Optional type annotation
if this.check(TokenKind::Colon) {
this.parse_type_annotation();
}
// Optional default value
if this.check(TokenKind::Assign) {
this.emit_node(SyntaxKind::ParamDefault, |inner| {
inner.expect(TokenKind::Assign);
// Use precedence 1 to avoid statement boundary detection within params
inner.parse_expr_with_precedence(1);
});
}
}
if this.check(TokenKind::Comma) {
this.bump();
} else {
break;
}
}
this.expect(TokenKind::ParenEnd);
});
}
/// Parse expression with operator precedence and assignment
fn parse_expr(&mut self) {
self.parse_assignment_expr();
}
/// Parse assignment expression: expr = expr
/// Assignment has lower precedence than all binary operators
fn parse_assignment_expr(&mut self) {
self.parse_expr_with_precedence(0);
// Check if this is an assignment
if self.check(TokenKind::Assign) {
// Need to wrap the already-parsed LHS in AssignExpr
// This is a bit tricky since we've already added nodes to the tree
// Solution: We'll handle this at the CST level by creating AssignExpr node
// and the RHS after the operator
self.emit_node(SyntaxKind::AssignExpr, |this| {
this.expect(TokenKind::Assign);
this.parse_expr_with_precedence(0);
});
}
}
/// Parse expression with given minimum precedence (Pratt parser)
fn parse_expr_with_precedence(&mut self, min_prec: usize) {
// Save marker BEFORE parsing prefix (LHS)
let mut lhs_marker = self.builder.marker();
self.parse_prefix_expr();
// In statement context (min_prec == 0), stop at linebreak after prefix
// unless the next token is an infix operator (e.g. leading '|>').
if min_prec == 0
&& self.has_trailing_linebreak()
&& self
.peek()
.and_then(|k| self.get_infix_precedence(k))
.is_none()
{
return;
}
while let Some(token_kind) = self.peek() {
if let Some(prec) = self.get_infix_precedence(token_kind) {
if prec < min_prec {
break;
}
// Wrap LHS and infix into BinaryExpr using the marker
self.builder
.start_node_at(lhs_marker, SyntaxKind::BinaryExpr);
// Consume the binary operator
self.bump();
// Parse the right-hand side with appropriate precedence
// For left-associative operators, use prec + 1
self.parse_expr_with_precedence(prec + 1);
self.builder.finish_node();
// Update marker for next iteration (now points to the BinaryExpr we just created)
lhs_marker = Marker {
pos: self.builder.marker().pos.saturating_sub(1),
};
// After parsing infix, check for linebreak in statement context
if min_prec == 0
&& self.has_trailing_linebreak()
&& self
.peek()
.and_then(|k| self.get_infix_precedence(k))
.is_none()
{
break;
}
} else {
break;
}
}
}
/// Parse expression with given minimum precedence without linebreak stopping.
fn parse_expr_with_precedence_no_linebreak(&mut self, min_prec: usize) {
// Save marker BEFORE parsing prefix (LHS)
let mut lhs_marker = self.builder.marker();
self.parse_prefix_expr();
while let Some(token_kind) = self.peek() {
if let Some(prec) = self.get_infix_precedence(token_kind) {
if prec < min_prec {
break;
}
// Wrap LHS and infix into BinaryExpr using the marker
self.builder
.start_node_at(lhs_marker, SyntaxKind::BinaryExpr);
// Consume the binary operator
self.bump();
// Parse the right-hand side with appropriate precedence
self.parse_expr_with_precedence_no_linebreak(prec + 1);
self.builder.finish_node();
// Update marker for next iteration
lhs_marker = Marker {
pos: self.builder.marker().pos.saturating_sub(1),
};
} else {
break;
}
}
}
/// Get precedence of an infix operator (None means not an infix operator)
fn get_infix_precedence(&self, token_kind: TokenKind) -> Option<usize> {
match token_kind {
// Pipe operators share the same precedence and associate to the left.
// but must be > 0 to avoid infinite loop in statement context (min_prec == 0)
TokenKind::OpPipeMacro | TokenKind::OpPipe => Some(2),
TokenKind::OpOr => Some(3),
TokenKind::OpAnd => Some(4),
TokenKind::OpEqual | TokenKind::OpNotEqual => Some(5),
TokenKind::OpLessThan
| TokenKind::OpLessEqual
| TokenKind::OpGreaterThan
| TokenKind::OpGreaterEqual => Some(6),
TokenKind::OpSum | TokenKind::OpMinus => Some(7),
TokenKind::OpProduct | TokenKind::OpDivide | TokenKind::OpModulo => Some(8),
TokenKind::OpExponent => Some(9),
TokenKind::OpAt => Some(10),
// Arrow is NOT an infix operator in expressions, only used in type annotations
// Return None to prevent it from being treated as an operator
TokenKind::Arrow => None,
_ => None,
}
}
/// Get unary operator precedence
fn get_prefix_precedence(&self, token_kind: Option<TokenKind>) -> bool {
matches!(
token_kind,
Some(TokenKind::OpMinus)
| Some(TokenKind::OpSum)
| Some(TokenKind::Dollar)
| Some(TokenKind::BackQuote)
)
}
/// Parse prefix expression (unary operators and postfix operations)
fn parse_prefix_expr(&mut self) {
if self.get_prefix_precedence(self.peek()) {
self.parse_unary_expr();
} else {
self.parse_postfix_expr();
}
}
/// Parse unary expression
fn parse_unary_expr(&mut self) {
match self.peek() {
Some(TokenKind::BackQuote) => {
self.parse_bracket_expr();
}
Some(TokenKind::Dollar) => {
self.parse_escape_expr();
}
_ => {
// Standard unary operator (-, +)
self.emit_node(SyntaxKind::UnaryExpr, |this| {
if matches!(
this.peek(),
Some(TokenKind::OpSum) | Some(TokenKind::OpMinus)
) && let Some(prev) = this.prev_kind_if_adjacent()
&& matches!(
prev,
TokenKind::OpSum
| TokenKind::OpMinus
| TokenKind::OpProduct
| TokenKind::OpDivide
| TokenKind::OpModulo
| TokenKind::OpExponent
)
{
this.add_error(ParserError::invalid_syntax(
this.current_token_index(),
"Consecutive operators without whitespace are not allowed",
));
}
// Consume the unary operator
this.bump();
// Parse the operand with high precedence
this.parse_prefix_expr();
});
}
}
}
/// Parse postfix expression (handles function calls, field access, etc.)
fn parse_postfix_expr(&mut self) {
// Save marker before parsing primary (the potential callee/lhs)
let mut lhs_marker = self.builder.marker();
self.parse_primary();
loop {
// Do not allow postfix operators across a line break
if self.has_trailing_linebreak() {
break;
}
match self.peek() {
Some(TokenKind::ParenBegin) => {
// Function call - wrap callee and arglist into CallExpr
self.builder.start_node_at(lhs_marker, SyntaxKind::CallExpr);
self.parse_arg_list();
self.builder.finish_node();
// Update marker for chained calls (e.g., foo()())
lhs_marker = Marker {
pos: self.builder.marker().pos.saturating_sub(1),
};
}
Some(TokenKind::Dot) => {
// Field access: expr.field or projection: expr.0, expr.1
self.builder
.start_node_at(lhs_marker, SyntaxKind::FieldAccess);
self.bump(); // consume Dot
// Can be either Ident (field name) or Int (tuple projection)
self.expects(&[TokenKind::Ident, TokenKind::Int]);
self.builder.finish_node();
// Update marker for chained access
lhs_marker = Marker {
pos: self.builder.marker().pos.saturating_sub(1),
};
}
Some(TokenKind::ArrayBegin) => {
// Array indexing: expr[index]
self.builder
.start_node_at(lhs_marker, SyntaxKind::IndexExpr);
self.bump(); // consume [
self.parse_expr();
self.expect(TokenKind::ArrayEnd);
self.builder.finish_node();
// Update marker for chained indexing
lhs_marker = Marker {
pos: self.builder.marker().pos.saturating_sub(1),
};
}
_ => break,
}
}
}
/// Parse argument list: (expr, expr, ...)
fn parse_arg_list(&mut self) {
self.emit_node(SyntaxKind::ArgList, |this| {
this.expect(TokenKind::ParenBegin);
if !this.check(TokenKind::ParenEnd) {
// Do not stop at linebreaks, and allow low-precedence operators in args
this.parse_expr_with_precedence_no_linebreak(0);
while this.check(TokenKind::Comma) {
this.bump(); // ,
if !this.check(TokenKind::ParenEnd) {
this.parse_expr_with_precedence_no_linebreak(0);
}
}
}
this.expect(TokenKind::ParenEnd);
});
}
/// Parse type annotation: : Type
fn parse_type_annotation(&mut self) {
self.emit_node(SyntaxKind::TypeAnnotation, |this| {
this.expect(TokenKind::Colon);
this.parse_type();
});
}
/// Parse type expression
fn parse_type(&mut self) {
// Check for function type with parentheses: (T1, T2) -> R or (T) -> R
if self.check(TokenKind::ParenBegin) {
// Look ahead for Arrow to detect function types
// Need to handle both `(T1, T2) -> R` and `(T) -> R`
let mut ahead_arrow = None;
let mut paren_depth = 0;
for i in 1..MAX_LOOKAHEAD {
match self.peek_ahead(i) {
Some(TokenKind::ParenBegin) => paren_depth += 1,
Some(TokenKind::ParenEnd) => {
if paren_depth == 0 {
// Found the closing paren of our type expression
// Check if Arrow follows immediately after
if self.peek_ahead(i + 1) == Some(TokenKind::Arrow) {
ahead_arrow = Some(i + 1);
}
break;
} else {
paren_depth -= 1;
}
}
None => break,
_ => continue,
}
}
if ahead_arrow.is_some() {
// Function type
self.emit_node(SyntaxKind::FunctionType, |inner| {
inner.parse_type_tuple_or_paren();
inner.expect(TokenKind::Arrow);
inner.parse_type();
});
return;
}
}
self.parse_type_union();
}
/// Parse union type: A | B | C
/// Note: We need lookahead to distinguish union type separator `|` from lambda's closing `|`
fn parse_type_union(&mut self) {
let marker = self.builder.marker();
self.parse_type_primary();
// Only continue parsing union if `|` is followed by a type start token
if self.check(TokenKind::LambdaArgBeginEnd) && self.is_type_start_after_pipe() {
// We have a union type, wrap the first type and parse the rest
self.builder.start_node_at(marker, SyntaxKind::UnionType);
while self.check(TokenKind::LambdaArgBeginEnd) && self.is_type_start_after_pipe() {
self.bump(); // consume |
self.parse_type_primary();
}
self.builder.finish_node();
}
}
/// Check if the token after `|` is a type start token (for union type disambiguation)
fn is_type_start_after_pipe(&self) -> bool {
match self.peek_ahead(1) {
Some(TokenKind::FloatType)
| Some(TokenKind::IntegerType)
| Some(TokenKind::StringType)
| Some(TokenKind::ParenBegin)
| Some(TokenKind::ArrayBegin)
| Some(TokenKind::BackQuote) => true,
// For BlockBegin, we need to distinguish record type `{field: Type}` from
// lambda body block `{expression}`. Record type starts with `{ ident :`
Some(TokenKind::BlockBegin) => {
self.peek_ahead(2) == Some(TokenKind::Ident)
&& self.peek_ahead(3) == Some(TokenKind::Colon)
}
_ => self.is_type_ident_after_pipe(),
}
}
/// Check if the token after `|` is a type identifier
/// A type identifier is followed by `|`, `,`, `)`, `}`, `]`, or is a known type name
fn is_type_ident_after_pipe(&self) -> bool {
if self.peek_ahead(1) != Some(TokenKind::Ident) {
return false;
}
// Check what follows the identifier
// If it's followed by another `|` that continues union, or `,`, `)`, etc.
// it's likely a type identifier
matches!(
self.peek_ahead(2),
Some(TokenKind::LambdaArgBeginEnd) // Union continues or lambda closes
| Some(TokenKind::Comma)
| Some(TokenKind::ParenEnd)
| Some(TokenKind::BlockEnd)
| Some(TokenKind::ArrayEnd)
| Some(TokenKind::Arrow) // Function return type
| None
)
}
/// Parse primary type (primitive, tuple, etc.)
fn parse_type_primary(&mut self) {
match self.peek() {
Some(TokenKind::FloatType)
| Some(TokenKind::IntegerType)
| Some(TokenKind::StringType) => {
self.emit_node(SyntaxKind::PrimitiveType, |inner| {
inner.bump();
});
}
Some(TokenKind::ParenBegin) => {
// Tuple type: (T1, T2, ...)
self.parse_type_tuple_or_paren();
}
Some(TokenKind::BlockBegin) => {
// Record type: {field: Type, ...}
self.parse_type_record();
}
Some(TokenKind::ArrayBegin) => {
// Array type: [T]
self.emit_node(SyntaxKind::ArrayType, |inner| {
inner.expect(TokenKind::ArrayBegin);
inner.parse_type();
inner.expect(TokenKind::ArrayEnd);
});
}
Some(TokenKind::BackQuote) => {
// Code type: `Type
self.emit_node(SyntaxKind::CodeType, |inner| {
inner.expect(TokenKind::BackQuote);
inner.parse_type();
});
}
Some(TokenKind::Ident) => {
// Type variable or named type (can be qualified path like mymath::PrivateNum)
self.emit_node(SyntaxKind::TypeIdent, |inner| {
inner.bump(); // First identifier
// Check for qualified path (::)
while inner.check(TokenKind::DoubleColon) {
inner.bump(); // Consume ::
if inner.check(TokenKind::Ident) {
inner.bump(); // Next identifier
} else {
inner.add_error(ParserError::unexpected_token(
inner.current_token_index(),
"identifier after ::",
&format!("{:?}", inner.peek().unwrap_or(TokenKind::Eof)),
));
break;
}
}
});
}
_ => {
// Error: unexpected token for type
if !self.is_at_end() {
self.add_error(ParserError::unexpected_token(
self.current_token_index(),
"type",
&format!("{:?}", self.peek().unwrap_or(TokenKind::Eof)),
));
self.bump();
}
}
}
}
/// Parse tuple type or parenthesized type: (T) or (T1, T2)
fn parse_type_tuple_or_paren(&mut self) {
// Look ahead to determine if it's a tuple
let mut paren_depth = 0;
let is_tuple = (1..MAX_LOOKAHEAD)
.find_map(|i| match self.peek_ahead(i) {
Some(TokenKind::ParenBegin) => {
paren_depth += 1;
None
}
Some(TokenKind::ParenEnd) => {
if paren_depth == 0 {
Some(false)
} else {
paren_depth -= 1;
None
}
}
Some(TokenKind::Comma) if paren_depth == 0 => Some(true),
None => Some(false),
_ => None,
})
.unwrap_or(false);
if is_tuple {
self.emit_node(SyntaxKind::TupleType, |inner| {
inner.expect(TokenKind::ParenBegin);
if !inner.check(TokenKind::ParenEnd) {
inner.parse_type();
while inner.check(TokenKind::Comma) {
inner.bump();
if !inner.check(TokenKind::ParenEnd) {
inner.parse_type();
}
}
}
inner.expect(TokenKind::ParenEnd);
});
} else {
// Parenthesized type or unit type
if self.peek_ahead(1) == Some(TokenKind::ParenEnd) {
self.emit_node(SyntaxKind::UnitType, |inner| {
inner.expect(TokenKind::ParenBegin);
inner.expect(TokenKind::ParenEnd);
});
} else {
self.bump(); // (
self.parse_type();
self.expect(TokenKind::ParenEnd);
}
}
}
/// Parse record type: {field1: Type1, field2: Type2}
fn parse_type_record(&mut self) {
self.emit_node(SyntaxKind::RecordType, |inner| {
inner.expect(TokenKind::BlockBegin);
if !inner.check(TokenKind::BlockEnd) {
// Parse field: name : Type
inner.expect_all(&[TokenKind::Ident, TokenKind::Colon]);
inner.parse_type();
while inner.check(TokenKind::Comma) {
inner.bump();
if !inner.check(TokenKind::BlockEnd) {
inner.expect_all(&[TokenKind::Ident, TokenKind::Colon]);
inner.parse_type();
}
}
}
inner.expect(TokenKind::BlockEnd);
});
}
/// Parse primary expression
fn parse_primary(&mut self) {
match self.peek() {
Some(TokenKind::Int) => {
self.emit_node(SyntaxKind::IntLiteral, |this| {
this.bump();
});
}
Some(TokenKind::Float) => {
self.emit_node(SyntaxKind::FloatLiteral, |this| {
this.bump();
});
}
Some(TokenKind::Str) => {
self.emit_node(SyntaxKind::StringLiteral, |this| {
this.bump();
});
}
Some(TokenKind::SelfLit) => {
self.emit_node(SyntaxKind::SelfLiteral, |this| {
this.bump();
});
}
Some(TokenKind::Now) => {
self.emit_node(SyntaxKind::NowLiteral, |this| {
this.bump();
});
}
Some(TokenKind::SampleRate) => {
self.emit_node(SyntaxKind::SampleRateLiteral, |this| {
this.bump();
});
}
Some(TokenKind::LambdaArgBeginEnd) => {
self.parse_lambda_expr();
}
Some(TokenKind::Ident) => {
// Check if this is a macro expansion (including qualified paths)
if self.find_macro_expand_after_path().is_some() {
self.parse_macro_expansion();
} else if self.peek_ahead(1) == Some(TokenKind::DoubleColon) {
// Qualified path: mod::submod::name
self.parse_qualified_path();
} else {
self.emit_node(SyntaxKind::Identifier, |this| {
this.bump();
});
}
}
Some(TokenKind::ArrayBegin) => {
// Array literal: [1, 2, 3]
self.parse_array_expr();
}
Some(TokenKind::ParenBegin) => {
// Need lookahead to distinguish tuple from parenthesized expression
// Tuple: (a, b) or (a,)
// Paren: (a) or (expr)
if self.is_tuple_expr() {
self.parse_tuple_expr();
} else {
self.emit_node(SyntaxKind::ParenExpr, |this| {
this.bump(); // (
this.parse_expr();
this.expect(TokenKind::ParenEnd);
});
}
}
Some(TokenKind::BlockBegin) => {
// Need lookahead to distinguish record from block
// Record: {a = 1, b = 2}
// Block: {stmt; stmt}
if self.is_record_expr() {
self.parse_record_expr();
} else {
self.parse_block_expr();
}
}
Some(TokenKind::If) => {
self.parse_if_expr();
}
Some(TokenKind::Match) => {
self.parse_match_expr();
}
Some(TokenKind::PlaceHolder) => {
// Placeholder: _
self.emit_node(SyntaxKind::PlaceHolderLiteral, |this| {
this.bump();
});
}
Some(TokenKind::BlockEnd) | Some(TokenKind::ParenEnd) | Some(TokenKind::ArrayEnd) => {
// Do not consume closing tokens here; let the enclosing parser handle them.
if let Some(kind) = self.peek() {
self.add_error(ParserError::unexpected_token(
self.current_token_index(),
"expression",
&format!("{kind:?}"),
));
}
}
None | Some(TokenKind::Eof) => {
// EOF reached when expecting an expression
self.add_error(ParserError::unexpected_eof(
self.current_token_index(),
"expression",
));
}
_ => {
// Error: unexpected token
// Record the error and continue
if let Some(kind) = self.peek() {
self.add_error(ParserError::unexpected_token(
self.current_token_index(),
"expression",
&format!("{kind:?}"),
));
}
// Skip the unexpected token to recover
self.bump();
}
}
}
/// Parse lambda expression: |x, y| expr
fn parse_lambda_expr(&mut self) {
self.emit_node(SyntaxKind::LambdaExpr, |this| {
// Opening '|'
this.expect(TokenKind::LambdaArgBeginEnd);
// Parameters
while !this.check(TokenKind::LambdaArgBeginEnd) && !this.is_at_end() {
if this.check(TokenKind::Ident) {
if let Some(&token_idx) = this.preparsed.token_indices.get(this.current) {
this.tokens[token_idx].kind = TokenKind::IdentParameter;
}
this.bump();
// Optional type annotation
if this.check(TokenKind::Colon) {
this.parse_type_annotation();
}
} else {
// Skip unexpected tokens in parameter list to recover
this.bump();
}
if this.check(TokenKind::Comma) {
this.bump();
} else if !this.check(TokenKind::LambdaArgBeginEnd) {
// Invalid separator; try to recover by stopping params
break;
}
}
// Closing '|'
this.expect(TokenKind::LambdaArgBeginEnd);
// Optional return type annotation after '->'
if this.check(TokenKind::Arrow) {
this.bump();
this.parse_type();
}
// Body expression
if !this.is_at_end() {
if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
} else {
this.parse_expr();
}
}
});
}
/// Check if current position is a tuple expression
/// Tuple: (a, b) or (a,) - has comma
/// Paren: (a) - no comma
fn is_tuple_expr(&self) -> bool {
if !self.check(TokenKind::ParenBegin) {
return false;
}
// Look ahead to find comma before ParenEnd
let mut depth = 0;
for i in 1..MAX_LOOKAHEAD {
match self.peek_ahead(i) {
Some(TokenKind::ParenBegin) => depth += 1,
Some(TokenKind::ParenEnd) => {
if depth == 0 {
return false; // no comma found
}
depth -= 1;
}
Some(TokenKind::Comma) if depth == 0 => return true,
None => return false,
_ => {}
}
}
false
}
/// Check if current position is a record expression
/// Record: {a = 1, b = 2} - has assignment
/// Block: {stmt; stmt} - has other tokens
fn is_record_expr(&self) -> bool {
if !self.check(TokenKind::BlockBegin) {
return false;
}
// Get lookahead tokens once to avoid repeated peek_ahead calls
let token1 = self.peek_ahead(1);
let token2 = self.peek_ahead(2);
let is_record_key = matches!(
token1,
Some(TokenKind::Ident) | Some(TokenKind::IdentParameter)
);
(matches!(token1, Some(TokenKind::DoubleDot))
|| (is_record_key
&& matches!(token2, Some(TokenKind::Assign) | Some(TokenKind::LeftArrow))))
}
/// Parse tuple expression: (a, b, c)
fn parse_tuple_expr(&mut self) {
self.emit_node(SyntaxKind::TupleExpr, |this| {
this.expect(TokenKind::ParenBegin);
if !this.check(TokenKind::ParenEnd) {
this.parse_expr();
while this.check(TokenKind::Comma) {
this.bump(); // ,
if !this.check(TokenKind::ParenEnd) {
this.parse_expr();
}
}
}
this.expect(TokenKind::ParenEnd);
});
}
/// Parse record expression: {field1 = expr1, field2 = expr2}
/// Also handles record update: {base <- field = value}
/// And incomplete records: {field1 = expr1, ..}
fn parse_record_expr(&mut self) {
self.emit_node(SyntaxKind::RecordExpr, |this| {
this.expect(TokenKind::BlockBegin);
if !this.check(TokenKind::BlockEnd) {
// Check for record update: {base <- field = value}
if this.check(TokenKind::Ident) && this.peek_ahead(1) == Some(TokenKind::LeftArrow)
{
this.parse_expr(); // base record
this.expect(TokenKind::LeftArrow); // <-
// Parse updated fields
this.expects(&[TokenKind::Ident, TokenKind::IdentParameter]);
this.expect(TokenKind::Assign);
this.parse_expr();
while this.check(TokenKind::Comma) {
this.bump();
if !this.check(TokenKind::BlockEnd) {
this.expects(&[TokenKind::Ident, TokenKind::IdentParameter]);
this.expect(TokenKind::Assign);
this.parse_expr();
}
}
} else {
// Regular record: {field = expr, ...} or {field = expr, ..}
// Allow incomplete record: {..}
if this.check(TokenKind::DoubleDot) {
this.bump(); // ..
} else {
// Parse field: name = expr
this.expects(&[TokenKind::Ident, TokenKind::IdentParameter]);
this.expect(TokenKind::Assign);
this.parse_expr();
}
while this.check(TokenKind::Comma) {
this.bump();
if !this.check(TokenKind::BlockEnd) {
if this.check(TokenKind::DoubleDot) {
// Incomplete record: field = expr, ..
this.bump(); // ..
break;
} else {
this.expects(&[TokenKind::Ident, TokenKind::IdentParameter]);
this.expect(TokenKind::Assign);
this.parse_expr();
}
}
}
}
}
this.expect(TokenKind::BlockEnd);
});
}
/// Parse block expression: { statements }
fn parse_block_expr(&mut self) {
self.emit_node(SyntaxKind::BlockExpr, |this| {
this.expect(TokenKind::BlockBegin);
while !this.check(TokenKind::BlockEnd) && !this.is_at_end() {
let before = this.current;
this.parse_statement();
if this.current == before && !this.is_at_end() {
this.add_error(ParserError::invalid_syntax(
this.current_token_index(),
"parser made no progress in block; skipping token for recovery",
));
this.bump();
continue;
}
// After a statement, if there's trailing linebreak/semicolon,
// prepare for next statement. Otherwise, assume statement continues
// (but note that parse_statement itself stops at natural boundaries)
if this.has_trailing_linebreak() {
// Linebreak found - ready for next statement
continue;
}
// If no linebreak and not at block end, something may be off
// but let parse_statement handle it in next iteration
}
this.expect(TokenKind::BlockEnd);
});
}
/// Parse if expression: if cond then-expr [else expr]
fn parse_if_expr(&mut self) {
self.emit_node(SyntaxKind::IfExpr, |this| {
this.expect(TokenKind::If);
this.parse_expr(); // condition
if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
} else {
this.parse_expr();
}
if this.check(TokenKind::Else) {
this.bump();
if this.check(TokenKind::If) {
this.parse_if_expr();
} else if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
} else {
this.parse_expr();
}
}
});
}
/// Parse match expression: match scrutinee { pattern => expr, ... }
fn parse_match_expr(&mut self) {
self.emit_node(SyntaxKind::MatchExpr, |this| {
this.expect(TokenKind::Match);
this.parse_expr(); // scrutinee
this.expect(TokenKind::BlockBegin);
// Parse match arms
this.emit_node(SyntaxKind::MatchArmList, |this| {
while !this.check(TokenKind::BlockEnd) && !this.is_at_end() {
let before = this.current;
this.parse_match_arm();
if this.current == before && !this.is_at_end() {
this.add_error(ParserError::invalid_syntax(
this.current_token_index(),
"parser made no progress in match arm; skipping token for recovery",
));
this.bump();
continue;
}
// Arms are separated by linebreaks or commas
if this.has_trailing_linebreak() {
continue;
}
if this.check(TokenKind::Comma) {
this.bump();
}
}
});
this.expect(TokenKind::BlockEnd);
});
}
/// Check if the current position has a tuple pattern inside a constructor
/// i.e., Constructor((x, y)) - the inner (x, y) is a tuple pattern
/// Returns true if we see: ( ( or ( ident ,
fn is_tuple_pattern_in_constructor(&self) -> bool {
// We're at ( - look ahead to see if it's a tuple pattern
// A tuple pattern starts with ( and either:
// 1. Has another ( immediately (nested tuple)
// 2. Has ident, comma pattern
if self.peek() != Some(TokenKind::ParenBegin) {
return false;
}
// Check what follows the opening paren
match self.peek_ahead(1) {
// ((x, y)) - nested tuple
Some(TokenKind::ParenBegin) => true,
// (x, y) - look for comma after first element
Some(TokenKind::Ident) | Some(TokenKind::PlaceHolder) => {
// Check if there's a comma after the identifier
self.peek_ahead(2) == Some(TokenKind::Comma)
}
_ => false,
}
}
/// Parse a single match arm: pattern => expr
fn parse_match_arm(&mut self) {
self.emit_node(SyntaxKind::MatchArm, |this| {
this.parse_match_pattern();
this.expect(TokenKind::FatArrow);
// Arm body can be a block or simple expression
if this.check(TokenKind::BlockBegin) {
this.parse_block_expr();
} else {
this.parse_expr();
}
});
}
/// Parse a match pattern
/// Supports:
/// - Integer literals: 0, 1, 2
/// - Float literals: 1.0, 2.5
/// - Wildcard: _
/// - Constructor patterns: Float(x), String(s), TypeName(binding)
fn parse_match_pattern(&mut self) {
self.emit_node(SyntaxKind::MatchPattern, |this| {
match this.peek() {
Some(TokenKind::Int) => {
this.emit_node(SyntaxKind::IntLiteral, |this| {
this.bump();
});
}
Some(TokenKind::Float) => {
this.emit_node(SyntaxKind::FloatLiteral, |this| {
this.bump();
});
}
Some(TokenKind::PlaceHolder) => {
this.emit_node(SyntaxKind::PlaceHolderLiteral, |this| {
this.bump();
});
}
// Tuple pattern at top level of match arm: (pat1, pat2, ...)
Some(TokenKind::ParenBegin) => {
this.parse_match_tuple_pattern();
}
// Constructor pattern: Identifier(binding) or Identifier
// Also handle type keywords (float, string, int) as constructor names
Some(TokenKind::Ident)
| Some(TokenKind::FloatType)
| Some(TokenKind::StringType)
| Some(TokenKind::IntegerType) => {
this.emit_node(SyntaxKind::ConstructorPattern, |this| {
// Constructor name (e.g., float, string, MyType)
this.emit_node(SyntaxKind::Identifier, |this| {
this.bump();
});
// Optional binding: (x) or ((x, y)) for tuple patterns
if this.check(TokenKind::ParenBegin) {
// Look ahead to determine if this is a tuple pattern or simple binding
// If there's a comma after the first identifier, it's a tuple pattern
let is_tuple_pattern = this.is_tuple_pattern_in_constructor();
if is_tuple_pattern {
// Parse as tuple pattern
this.parse_tuple_pattern();
} else {
// Simple binding: (x) or (_)
this.bump(); // (
if this.check(TokenKind::Ident) {
this.emit_node(SyntaxKind::Identifier, |this| {
this.bump();
});
} else if this.check(TokenKind::PlaceHolder) {
// Allow _ as binding to discard the value
this.emit_node(SyntaxKind::PlaceHolderLiteral, |this| {
this.bump();
});
} else if this.check(TokenKind::ParenBegin) {
// Nested tuple pattern: ((x, y))
this.parse_tuple_pattern();
}
this.expect(TokenKind::ParenEnd);
}
}
});
}
_ => {
if let Some(kind) = this.peek() {
this.add_error(ParserError::unexpected_token(
this.current_token_index(),
"match pattern (int, float, _, or constructor)",
&format!("{kind:?}"),
));
}
this.bump();
}
}
});
}
/// Parse tuple pattern in match expression: (pat1, pat2, ...)
/// Each element is a match pattern (can be int, float, _, constructor, or nested tuple)
fn parse_match_tuple_pattern(&mut self) {
self.emit_node(SyntaxKind::TuplePattern, |this| {
this.expect(TokenKind::ParenBegin);
if !this.check(TokenKind::ParenEnd) {
this.parse_match_pattern();
while this.check(TokenKind::Comma) {
this.bump(); // ,
if !this.check(TokenKind::ParenEnd) {
this.parse_match_pattern();
}
}
}
this.expect(TokenKind::ParenEnd);
});
}
/// Parse type declaration (Union/Sum types only)
/// Type declaration: type Name = Variant1 | Variant2(Type) | ...
/// Recursive type: type rec Name = Variant1 | Variant2(Type, Name) | ...
fn parse_type_decl(&mut self) {
self.emit_node(SyntaxKind::TypeDecl, |this| {
this.expect(TokenKind::Type);
// Optionally consume 'rec' keyword for recursive types
if this.check(TokenKind::Rec) {
this.bump(); // consume 'rec'
}
this.expect(TokenKind::Ident); // type name
this.expect(TokenKind::Assign); // =
// Parse as variant declaration: type Name = Variant1 | Variant2 | ...
this.parse_variant_def();
// Parse remaining variants separated by |
while this.check(TokenKind::LambdaArgBeginEnd) {
this.bump(); // consume |
this.parse_variant_def();
}
});
}
/// Parse type alias declaration
/// Type alias: type alias Name = BaseType
fn parse_type_alias_decl(&mut self) {
self.emit_node(SyntaxKind::TypeDecl, |this| {
this.expect(TokenKind::Type);
this.expect(TokenKind::Alias);
this.expect(TokenKind::Ident); // alias name
this.expect(TokenKind::Assign); // =
// Parse the target type
this.parse_type();
});
}
/// Parse a single variant definition: Name or Name(Type)
fn parse_variant_def(&mut self) {
self.emit_node(SyntaxKind::VariantDef, |this| {
this.expect(TokenKind::Ident); // variant name
// Check for optional payload type: Name(Type) or Name(T1, T2, ...)
// If multiple comma-separated types are found, treat as tuple implicitly
if this.check(TokenKind::ParenBegin) {
this.bump(); // consume (
// Parse first type
this.parse_type();
// If there's a comma, we have multiple types - treat as tuple
if this.check(TokenKind::Comma) {
// We've already parsed the first type, now wrap it in a TupleType
// and parse the rest
while this.check(TokenKind::Comma) {
this.bump(); // consume ,
if !this.check(TokenKind::ParenEnd) {
this.parse_type();
}
}
}
this.expect(TokenKind::ParenEnd);
}
});
}
/// Parse array expression: [expr1, expr2, ...]
fn parse_array_expr(&mut self) {
self.emit_node(SyntaxKind::ArrayExpr, |this| {
this.expect(TokenKind::ArrayBegin);
if !this.check(TokenKind::ArrayEnd) {
this.parse_expr();
while this.check(TokenKind::Comma) {
this.bump(); // ,
if !this.check(TokenKind::ArrayEnd) {
this.parse_expr();
}
}
}
this.expect(TokenKind::ArrayEnd);
});
}
}
/// Parse tokens into a Green Tree (CST) with error collection
pub fn parse_cst(
tokens: Vec<Token>,
preparsed: &PreParsedTokens,
) -> (GreenNodeId, GreenNodeArena, Vec<Token>, Vec<ParserError>) {
let parser = Parser::new(tokens, preparsed);
parser.parse()
}