lang-forge 2.0.0-alpha.1

LexerSketch: forge a working language front end - lexer, parser, and lossless syntax tree - from a .lsf schematic.
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
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
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
//! The forged parser: an interpreter over the compiled grammar tables.
//!
//! # Semantics
//!
//! Alternatives are tried in order and the first that matches wins (parsing
//! expression grammar semantics). The FIRST sets prune that search before it
//! starts: an alternative that cannot begin with the current token is never
//! tried, so for the great majority of decisions exactly one alternative
//! remains and the parser commits to it at once, like a predictive LL(1)
//! parser. Only when several alternatives can begin with the same token does
//! it *speculate*: it tries each in turn in a strict mode that fails fast and
//! records nothing, rewinding between attempts. Repetitions and optionals are
//! greedy: once their body can begin, they commit to it.
//!
//! # Recovery
//!
//! Outside speculation the parser never fails. A missing token is reported and
//! treated as present; a token no rule wants is reported and wrapped in an
//! `ERROR` node, after which the enclosing repetition carries on — unless the
//! token is one an enclosing construct is waiting for (its *stop set*), in
//! which case the repetition ends and lets that construct take it. When all
//! speculative attempts fail, the alternative that got furthest is re-run in
//! recovering mode, so the reported error is the one deepest into the input.
//! At most one syntax error is reported per token position.
//!
//! On input without errors, recovering and strict parsing take the same path
//! and build the same tree.
//!
//! # Events
//!
//! The parser does not build the tree directly. It records a flat list of
//! events — start node, token, finish node — that `tree::build` replays.
//! Speculation rewinds by truncating the list, and an operator node that must
//! wrap an operand already parsed (the `a` in `a + b`) is spliced in by a
//! forward link from the operand's start event instead of an insertion into
//! the middle of the list.

use alloc::{format, string::String, vec::Vec};

use diag_lang::{Code, Diagnostic, Label, Severity};
use syntax_lang::{Node, Span, Token};

use crate::{
    codes,
    grammar::{CAT_CLASS, CAT_NODE, Expr, Grammar, Level, Program, RuleBody},
    kind::Kind,
    schematic::Fixity,
    set::{NO_SET, SetId},
    tree::{self, Event, Step},
};

/// How deeply the parser may recurse into the grammar before it reports an
/// error instead of recursing further. Every recursive step counts — a rule, a
/// sequence, an alternative, a repetition, an optional, an operator operand —
/// so the bound holds for any grammar, however its rules nest. At this limit
/// the parser needs at most about 256 KiB of stack in a release build and
/// 768 KiB in a debug build (measured on the deepest-nesting grammars known),
/// which fits every default thread, including the 1 MiB Windows main thread.
/// Typical grammars reach it only past a hundred and fifty levels of nested
/// parentheses.
pub(crate) const MAX_DEPTH: u32 = 768;

/// No contextual keyword at this token.
const NOT_KEYWORD: u16 = u16::MAX;

/// Moves an event's forward link from one base index to another.
#[inline]
fn relocate(event: Event, from: u32, to: u32) -> Event {
    match event.step() {
        Step::Start { kind, forward } if forward != 0 => Event::start(kind, forward - from + to),
        _ => event,
    }
}

/// Parses `src`, always producing a tree, and the problems found on the way.
pub(crate) fn parse(grammar: &Grammar, src: &str) -> (Node<Kind>, Vec<Diagnostic>) {
    let program = &grammar.program;
    if u32::try_from(src.len()).is_err() {
        let root = program.rules[program.start as usize]
            .node
            .unwrap_or(program.error);
        let diag = Diagnostic::new(
            Severity::Error,
            "the source is larger than 4 GiB",
            Label::unlabelled(Span::empty(0)),
        )
        .with_code(codes::SOURCE_TOO_LARGE);
        return (Node::new(root, Vec::new()), Vec::from([diag]));
    }
    let mut tokens = Vec::new();
    let mut diags = Vec::new();
    grammar.lexer.run(src, &mut tokens, &mut diags);
    parse_tokens(grammar, src, &tokens, program.start, diags)
}

/// Parses already-lexed `tokens` of `src` with rule `start` as the root.
pub(crate) fn parse_tokens(
    grammar: &Grammar,
    src: &str,
    tokens: &[Token<Kind>],
    start: u32,
    mut diags: Vec<Diagnostic>,
) -> (Node<Kind>, Vec<Diagnostic>) {
    let program = &grammar.program;
    let root = program.rules[start as usize].node.unwrap_or(program.error);
    let mut parser = Parser::new(grammar, src, tokens);
    parser.start = start;
    // Outside strict mode the parser recovers from everything; `top` only
    // reports failure to a strict caller.
    let recovered = parser.top();
    debug_assert!(recovered);
    let Parser {
        mut events,
        diags: parse_diags,
        ..
    } = parser;
    if !parse_diags.is_empty() {
        diags.extend(parse_diags);
        diags.sort_by_key(|d| d.primary().span().start().to_u32());
    }
    (tree::build(tokens, &mut events, root, program.error), diags)
}

/// Parses `src` in strict mode only: `None` if it does not match the grammar
/// exactly. The reference the recovering parser is tested against.
#[cfg(test)]
pub(crate) fn parse_strict(grammar: &Grammar, src: &str) -> Option<Node<Kind>> {
    let mut tokens = Vec::new();
    let mut diags = Vec::new();
    grammar.lexer.run(src, &mut tokens, &mut diags);
    let mut parser = Parser::new(grammar, src, &tokens);
    parser.strict = 1;
    if !parser.top() {
        return None;
    }
    let root = grammar.program.rules[grammar.program.start as usize].node?;
    let mut events = parser.events;
    Some(tree::build(
        &tokens,
        &mut events,
        root,
        grammar.program.error,
    ))
}

/// Parses `src` with memoization off: the reference memoized parsing must
/// agree with, tree and diagnostics alike.
#[cfg(test)]
pub(crate) fn parse_unmemoized(grammar: &Grammar, src: &str) -> (Node<Kind>, Vec<Diagnostic>) {
    let root = grammar.program.rules[grammar.program.start as usize]
        .node
        .unwrap_or(grammar.program.error);
    let mut tokens = Vec::new();
    let mut diags = Vec::new();
    grammar.lexer.run(src, &mut tokens, &mut diags);
    let mut parser = Parser::new(grammar, src, &tokens);
    parser.memoize = false;
    let recovered = parser.top();
    debug_assert!(recovered);
    let mut events = parser.events;
    diags.extend(parser.diags);
    diags.sort_by_key(|d| d.primary().span().start().to_u32());
    (
        tree::build(&tokens, &mut events, root, grammar.program.error),
        diags,
    )
}

/// A position to rewind to after a failed speculative attempt.
#[derive(Clone, Copy)]
struct Checkpoint {
    events: usize,
    pos: usize,
    marks: usize,
}

/// What a strict attempt at a rule produced, remembered by rule and position.
#[derive(Clone, Copy)]
struct MemoEntry {
    rule: u32,
    /// The previous entry for the same position, plus one; zero ends the chain.
    next: u32,
    /// The position after the rule, or `FAILED`.
    end: u32,
    /// The furthest position the attempt looked at.
    reach: u32,
    /// How many grammar levels below its own the attempt went: replaying it
    /// is the same as parsing it again only where that many levels are left
    /// before the depth limit (ISSUES P09).
    deep: u32,
    /// Where the attempt's events are. While they are still in the parser's
    /// event list (`saved` unset), they are `from..from + len` there. Once a
    /// rewind has rescued them, they are items `from..from + len` of
    /// `Memo::saved`, whose forward links count from `origin`.
    from: u32,
    len: u32,
    origin: u32,
    saved: bool,
}

const FAILED: u32 = u32::MAX;

/// One item of a rescued span of events.
#[derive(Clone, Copy)]
enum Saved {
    /// An event. A forward link counts from the start of the span.
    Event(Event),
    /// The events of a remembered attempt, replayed at this point.
    Entry(u32),
}

/// A remembered attempt replayed into the event list: `len` events from
/// `start` are a copy of entry `entry`'s.
#[derive(Clone, Copy)]
struct Replay {
    start: u32,
    len: u32,
    entry: u32,
}

/// Results of strict rule attempts, so speculation never parses the same rule
/// at the same position twice: without it, alternatives that share a prefix
/// through different rules re-parse that prefix once per alternative, at
/// every level of nesting — exponential time on modest input.
///
/// Strict results depend only on the rule and the position, so they stay
/// valid for as long as they are kept. They are dropped when the outermost
/// speculation ends, which bounds the memory to the work of one speculation;
/// a generation stamp makes that drop O(1).
///
/// A successful attempt's events are not copied when it is remembered: the
/// entry is a range of the parser's event list, where they already are. Only
/// when a failed alternative rewinds that list are the events about to be cut
/// off rescued into `saved`, once for every entry inside the rewound span,
/// and any part of the span that is itself a replayed attempt is kept as a
/// reference to that attempt rather than copied again. So each event the
/// parser produces is stored at most once, however deeply the rules that
/// produced it nest: copying the events of every success, at every level,
/// cost memory proportional to the input times its nesting depth.
#[derive(Default)]
struct Memo {
    /// By token position: the generation and newest entry (plus one).
    heads: Vec<(u32, u32)>,
    generation: u32,
    entries: Vec<MemoEntry>,
    /// Rescued spans: events and references to replayed entries.
    saved: Vec<Saved>,
    /// The entries whose events are still in the parser's event list, in the
    /// order they were made. That is also the order their events end in: an
    /// entry is made when its rule finishes, at the end of the list, and a
    /// rewind rescues every entry it would cut into.
    live: Vec<u32>,
    /// Replays still in the event list, in order. Replays never overlap: a
    /// replay records only the entry it expands, not the entries inside.
    replays: Vec<Replay>,
    /// Scratch for `rescue`: the entries it moves, and boundary positions.
    doomed: Vec<u32>,
    bounds: Vec<(u32, u32)>,
    /// Scratch for `expand`: the rescued spans being replayed, innermost
    /// last, as (next item, end item, origin, base).
    frames: Vec<(u32, u32, u32, u32)>,
}

impl Memo {
    /// The entry for `rule` at `pos`, and its index.
    fn get(&self, rule: u32, pos: usize) -> Option<(u32, MemoEntry)> {
        let &(generation, head) = self.heads.get(pos)?;
        if generation != self.generation {
            return None;
        }
        let mut at = head;
        while at != 0 {
            let entry = self.entries[at as usize - 1];
            if entry.rule == rule {
                return Some((at - 1, entry));
            }
            at = entry.next;
        }
        None
    }

    fn put(&mut self, mut entry: MemoEntry, pos: usize, positions: usize) {
        if self.heads.is_empty() {
            self.heads = alloc::vec![(0, 0); positions];
            self.generation = 1;
        }
        let head = &mut self.heads[pos];
        entry.next = if head.0 == self.generation { head.1 } else { 0 };
        if !entry.saved {
            debug_assert!(self.live.last().is_none_or(|&i| {
                let last = &self.entries[i as usize];
                last.from + last.len <= entry.from + entry.len
            }));
            self.live.push(self.entries.len() as u32);
        }
        self.entries.push(entry);
        *head = (self.generation, self.entries.len() as u32);
    }

    /// Appends entry `index`'s events to `events`, expanding the references
    /// in rescued spans with an explicit stack (they nest as deeply as the
    /// rules did), and relocating forward links to where the events land.
    fn expand(&mut self, index: u32, events: &mut Vec<Event>) {
        self.frames.clear();
        let mut next = Some(index);
        loop {
            if let Some(index) = next.take() {
                let entry = self.entries[index as usize];
                let base = events.len() as u32;
                if entry.saved {
                    self.frames
                        .push((entry.from, entry.from + entry.len, entry.origin, base));
                } else {
                    let range = entry.from as usize..(entry.from + entry.len) as usize;
                    events.extend_from_within(range);
                    for event in &mut events[base as usize..] {
                        *event = relocate(*event, entry.from, base);
                    }
                }
            }
            let Some(frame) = self.frames.last_mut() else {
                return;
            };
            if frame.0 == frame.1 {
                let _ = self.frames.pop();
                continue;
            }
            let (origin, base) = (frame.2, frame.3);
            let item = self.saved[frame.0 as usize];
            frame.0 += 1;
            match item {
                Saved::Event(event) => events.push(relocate(event, origin, base)),
                Saved::Entry(inner) => next = Some(inner),
            }
        }
    }

    /// Records that `len` events from `start` replay entry `entry`.
    fn replayed(&mut self, start: usize, len: usize, entry: u32) {
        if len > 0 {
            self.replays.push(Replay {
                start: start as u32,
                len: len as u32,
                entry,
            });
        }
    }

    /// Rescues the events of every entry that truncating `events` to `keep`
    /// would cut off, and forgets the replays it cuts off.
    ///
    /// Such entries lie wholly past `keep`: a rewind returns to the start of
    /// an attempt, and every entry made since began inside that attempt.
    /// Entries nest like the rules that made them, so the outermost ones are
    /// rescued as spans and the ones inside become ranges of those spans.
    fn rescue(&mut self, events: &[Event], keep: usize) {
        let entries = &self.entries;
        let split = self.live.partition_point(|&i| {
            let entry = &entries[i as usize];
            (entry.from + entry.len) as usize <= keep
        });
        let mut doomed = core::mem::take(&mut self.doomed);
        doomed.clear();
        doomed.extend(self.live.drain(split..));
        // Outermost first: by start, and the longer of two that start together.
        doomed.sort_unstable_by_key(|&i| {
            let entry = &self.entries[i as usize];
            (entry.from, u32::MAX - entry.len)
        });
        // Tags a boundary as an entry's end rather than its start.
        const END: u32 = 1 << 31;
        let mut bounds = core::mem::take(&mut self.bounds);
        let mut at = 0;
        while at < doomed.len() {
            let outer = self.entries[doomed[at] as usize];
            let (from, end) = (outer.from, outer.from + outer.len);
            debug_assert!(from as usize >= keep);
            let mut inner = at + 1;
            while inner < doomed.len() && self.entries[doomed[inner] as usize].from < end {
                inner += 1;
            }
            // The positions where the span's entries begin and end, to be
            // translated into item indexes as the span is walked.
            bounds.clear();
            for &i in &doomed[at..inner] {
                let entry = &self.entries[i as usize];
                bounds.push((entry.from, i));
                bounds.push((entry.from + entry.len, i | END));
            }
            bounds.sort_unstable_by_key(|&(position, _)| position);
            let mut bound = 0;
            let mut replay = self.replays.partition_point(|r| r.start < from);
            let mut position = from;
            loop {
                while let Some(&(at_position, tagged)) = bounds.get(bound) {
                    if at_position > position {
                        break;
                    }
                    // Entries never begin or end inside a replay.
                    debug_assert_eq!(at_position, position);
                    let item = self.saved.len() as u32;
                    let entry = &mut self.entries[(tagged & !END) as usize];
                    if tagged & END == 0 {
                        entry.origin = entry.from - from;
                        entry.from = item;
                    } else {
                        entry.len = item - entry.from;
                        entry.saved = true;
                    }
                    bound += 1;
                }
                if position >= end {
                    debug_assert_eq!(position, end, "a replay crosses an entry's end");
                    break;
                }
                match self.replays.get(replay) {
                    Some(r) if r.start == position => {
                        self.saved.push(Saved::Entry(r.entry));
                        position += r.len;
                        replay += 1;
                    }
                    _ => {
                        let event = relocate(events[position as usize], from, 0);
                        self.saved.push(Saved::Event(event));
                        position += 1;
                    }
                }
            }
            at = inner;
        }
        self.doomed = doomed;
        self.bounds = bounds;
        let kept = self.replays.partition_point(|r| (r.start as usize) < keep);
        self.replays.truncate(kept);
    }

    fn clear(&mut self) {
        self.generation = self.generation.wrapping_add(1);
        if self.generation == 0 {
            // Stamps wrapped: no old stamp may match the new generation.
            self.heads.iter_mut().for_each(|h| *h = (0, 0));
            self.generation = 1;
        }
        self.entries.clear();
        self.saved.clear();
        self.live.clear();
        self.replays.clear();
    }
}

struct Parser<'a> {
    grammar: &'a Grammar,
    program: &'a Program,
    src: &'a str,
    /// Kind indexes of the significant tokens, then the end-of-input bit.
    kinds: Vec<u16>,
    spans: Vec<Span>,
    pos: usize,
    events: Vec<Event>,
    diags: Vec<Diagnostic>,
    /// Non-zero while speculating.
    strict: u32,
    /// Speculations in progress, strict or not.
    speculating: u32,
    /// The furthest token position the current attempt reached.
    furthest: usize,
    /// The token position of the last reported error, to report one per token.
    last_error: usize,
    /// The event count right after the last `ERROR` node closed, so adjacent
    /// skipped tokens share one node.
    error_end: usize,
    /// Stop sets of the constructs being parsed, innermost last.
    stops: Vec<SetId>,
    /// By rule: the position of its innermost active invocation, or `u32::MAX`.
    active: Vec<u32>,
    depth: u32,
    too_deep: bool,
    /// Set once an attempt, strict or not, has hit the depth limit during the
    /// current outermost speculation. Cleared when an outermost speculation
    /// begins and when it ends, so a construct nested too deeply limits only
    /// the speculation it is part of, not every one after it.
    hit_limit: bool,
    /// Counts events that make a strict result depend on more than the rule
    /// and position (the depth limit, the progress guard); such results are
    /// not memoized.
    taint: u32,
    memo: Memo,
    /// Whether strict attempts are memoized. Always on; the tests turn it off
    /// to check that memoization never changes a result.
    memoize: bool,
    /// The deepest grammar level the current attempt has reached.
    max_depth: u32,
    /// The rule the parse starts with.
    start: u32,
    /// By significant token: whether a line break precedes it (only when the
    /// grammar uses `LINE_START` or `NL_BEFORE`).
    nl_before: Vec<bool>,
    /// By significant token: the contextual keyword an `IDENT` spells, or
    /// `NOT_KEYWORD` (empty when the grammar has no contextual keywords).
    contextual: Vec<u16>,
    /// Tokens labelled in the current rule invocations, for text
    /// back-references: (label, token position).
    marks: Vec<(u16, u32)>,
    /// Where the innermost rule invocation's marks begin.
    mark_base: usize,
}

impl<'a> Parser<'a> {
    fn new(grammar: &'a Grammar, src: &'a str, tokens: &[Token<Kind>]) -> Self {
        let significant = tokens.iter().filter(|t| !t.is_trivia()).count();
        let mut kinds = Vec::with_capacity(significant + 1);
        let mut spans = Vec::with_capacity(significant + 1);
        let lines = grammar.program.v2.as_ref().is_some_and(|v| v.lines);
        let mut nl_before = Vec::new();
        let mut seen_break = true;
        for t in tokens {
            if t.is_trivia() {
                if lines {
                    let text = &src[t.span().start().to_usize()..t.span().end().to_usize()];
                    seen_break |= text.contains('\n');
                }
                continue;
            }
            kinds.push(t.kind().index());
            spans.push(t.span());
            if lines {
                nl_before.push(seen_break);
                // A line-break token itself ends a line.
                seen_break = t.kind().index() == grammar.program.newline;
            }
        }
        kinds.push(grammar.program.eof);
        let end = spans
            .last()
            .map_or(0, |s: &Span| s.end().to_u32())
            .max(tokens.last().map_or(0, |t| t.span().end().to_u32()));
        spans.push(Span::empty(end));
        if lines {
            nl_before.push(seen_break);
        }
        let mut contextual = Vec::new();
        if let Some(v2) = grammar
            .program
            .v2
            .as_ref()
            .filter(|v| !v.contextual.is_empty())
        {
            let ident = grammar.program.ident;
            contextual.reserve(kinds.len());
            let mut buf = String::new();
            for (k, span) in kinds.iter().zip(&spans) {
                let mut keyword = NOT_KEYWORD;
                if *k == ident {
                    let text = &src[span.start().to_usize()..span.end().to_usize()];
                    let text = if v2.case_insensitive {
                        buf.clear();
                        buf.extend(text.chars().map(|c| c.to_ascii_lowercase()));
                        buf.as_str()
                    } else {
                        text
                    };
                    if let Ok(at) = v2.contextual.binary_search_by(|(t, _)| (**t).cmp(text)) {
                        keyword = v2.contextual[at].1;
                    }
                }
                contextual.push(keyword);
            }
        }
        Self {
            grammar,
            program: &grammar.program,
            src,
            kinds,
            spans,
            pos: 0,
            events: Vec::with_capacity(significant * 2 + 8),
            diags: Vec::new(),
            strict: 0,
            speculating: 0,
            furthest: 0,
            last_error: usize::MAX,
            error_end: usize::MAX,
            stops: Vec::new(),
            active: alloc::vec![u32::MAX; grammar.program.rules.len()],
            depth: 0,
            too_deep: false,
            hit_limit: false,
            taint: 0,
            memo: Memo::default(),
            memoize: true,
            max_depth: 0,
            start: grammar.program.start,
            nl_before,
            contextual,
            marks: Vec::new(),
            mark_base: 0,
        }
    }

    /// The start rule, then any leftover input. Returns `false` only in strict
    /// mode, when the input does not match.
    fn top(&mut self) -> bool {
        let program = self.program;
        let start = self.start;
        let rule = &program.rules[start as usize];
        let root = rule.node.unwrap_or(self.program.error);
        self.events.push(Event::start(root, 0));
        let ok = if self.at_set(rule.first) || rule.nullable {
            self.active[start as usize] = 0;
            match rule.body {
                RuleBody::Expr(e) => self.expr(e),
                RuleBody::Pratt(p) => self.pratt(p, start, 0),
            }
        } else {
            self.expected_rule(start)
        };
        if !ok {
            return false;
        }
        if !self.at_eof() {
            if self.strict > 0 {
                return false;
            }
            self.leftover();
        }
        self.events.push(Event::FINISH);
        true
    }

    /// Parses one grammar expression. Every recursive step of the parser
    /// passes through here or through `pratt`, and both count against
    /// `MAX_DEPTH`, which is what bounds the parser's stack.
    fn expr(&mut self, e: u32) -> bool {
        let program = self.program;
        let expr = program.exprs[e as usize];
        if let Expr::Token(kind) = expr {
            return self.token(kind);
        }
        if self.depth >= MAX_DEPTH {
            return self.too_deep();
        }
        self.depth += 1;
        self.max_depth = self.max_depth.max(self.depth);
        let ok = match expr {
            Expr::Rule(r) => self.rule(r),
            Expr::Seq { start, len } => self.seq(start, len),
            Expr::Choice { start, len } => self.choice(program.children(start, len)),
            Expr::Repeat {
                body,
                min_one,
                stop,
            } => self.repeat(body, min_one, stop),
            Expr::Optional(body) => {
                !self.at_set(program.first[body as usize]) || !self.guard(body) || self.expr(body)
            }
            Expr::Token(_) => true,
            other => self.expr_v2(e, other),
        };
        self.depth -= 1;
        ok
    }

    /// The format-2 expressions.
    #[inline(never)]
    fn expr_v2(&mut self, e: u32, expr: Expr) -> bool {
        match expr {
            Expr::Keyword(kind) => {
                if self.current() == kind {
                    self.bump();
                    return true;
                }
                if self.current() == self.program.ident && self.is_keyword_text(kind, self.pos) {
                    self.bump_as(kind);
                    return true;
                }
                self.missing_token(kind)
            }
            Expr::Word => {
                if self.at_set(self.program.first[e as usize]) && !self.at_eof() {
                    self.bump();
                    return true;
                }
                self.expected_word()
            }
            Expr::Label { label, body } => {
                self.events.push(Event::label(label));
                let before = self.pos;
                let ok = self.expr(body);
                self.events.push(Event::UNLABEL);
                if ok && self.pos > before && self.program.v2.as_ref().is_some_and(|v| v.backrefs) {
                    self.marks.push((label, self.pos as u32 - 1));
                }
                ok
            }
            Expr::And(body) => {
                if self.lookahead(body) {
                    return true;
                }
                self.failed_predicate(body, true)
            }
            Expr::Not(body) => {
                if !self.lookahead(body) {
                    return true;
                }
                self.failed_predicate(body, false)
            }
            Expr::BackRef { label, body } => self.backref(label, body),
            Expr::Eof => self.at_eof() || self.expected_eof(),
            Expr::LineStart | Expr::NlBefore => {
                let at = self.pos;
                let ok = self.nl_before.get(at).copied().unwrap_or(false)
                    || (matches!(expr, Expr::LineStart) && at == 0);
                ok || self.failed_assertion(matches!(expr, Expr::LineStart))
            }
            _ => true,
        }
    }

    /// Whether significant token `pos` is an `IDENT` spelling contextual
    /// keyword `kind`, under the keyword case policy.
    fn is_keyword_text(&self, kind: u16, pos: usize) -> bool {
        self.contextual.get(pos) == Some(&kind)
    }

    /// Whether a repetition or optional whose body begins with a predicate
    /// may commit: the predicate is asked first (format 2).
    #[inline]
    fn guard(&mut self, body: u32) -> bool {
        if !self.program.v2.as_ref().is_some_and(|v| v.predicates) {
            return true;
        }
        let mut e = body;
        loop {
            match self.program.exprs[e as usize] {
                Expr::And(inner) => return self.lookahead(inner),
                Expr::Not(inner) => return !self.lookahead(inner),
                Expr::Seq { start, len } if len > 0 => e = self.program.items[start as usize],
                Expr::Label { body, .. } => e = body,
                _ => return true,
            }
        }
    }

    /// Whether `e` would match here, without consuming input or recording
    /// anything: a strict attempt, rewound.
    fn lookahead(&mut self, e: u32) -> bool {
        let checkpoint = self.checkpoint();
        let outermost = self.speculating == 0;
        if outermost {
            self.hit_limit = false;
        }
        self.speculating += 1;
        self.strict += 1;
        let furthest = self.furthest;
        let ok = self.expr(e);
        self.furthest = furthest;
        self.strict -= 1;
        self.speculating -= 1;
        self.restore(checkpoint);
        if outermost {
            self.memo.clear();
            self.hit_limit = false;
        }
        ok
    }

    fn checkpoint(&self) -> Checkpoint {
        Checkpoint {
            events: self.events.len(),
            pos: self.pos,
            marks: self.marks.len(),
        }
    }

    fn restore(&mut self, checkpoint: Checkpoint) {
        self.rewind(checkpoint.events);
        self.pos = checkpoint.pos;
        self.marks.truncate(checkpoint.marks);
    }

    /// `body=label`: the token's text must equal the text of the token last
    /// labelled `label` in this rule invocation.
    fn backref(&mut self, label: u16, body: u32) -> bool {
        let want = self.marks[self.mark_base..]
            .iter()
            .rev()
            .find(|(l, _)| *l == label)
            .map(|(_, at)| *at as usize);
        let same = want.is_some_and(|at| {
            let (a, b) = (self.text(at), self.text(self.pos));
            a == b
                || (self.program.v2.as_ref().is_some_and(|v| v.case_insensitive)
                    && self.program.exprs[body as usize] != Expr::Token(self.program.ident)
                    && a.eq_ignore_ascii_case(b))
        });
        if same {
            return self.expr(body);
        }
        if self.strict > 0 {
            return false;
        }
        self.mismatch(want);
        self.expr(body)
    }

    fn seq(&mut self, start: u32, len: u32) -> bool {
        let program = self.program;
        let items = program.children(start, len);
        if self.strict > 0 {
            return items.iter().all(|&item| self.expr(item));
        }
        let sync = &program.sync[start as usize..(start + len) as usize];
        for (&item, &sync) in items.iter().zip(sync) {
            // While an item recovers, the tokens the rest of this sequence
            // begins with are off limits to skipping: they are why the item
            // should end. Recovering parses never fail.
            if sync == NO_SET {
                let parsed = self.expr(item);
                debug_assert!(parsed);
            } else {
                self.stops.push(sync);
                let parsed = self.expr(item);
                debug_assert!(parsed);
                let _ = self.stops.pop();
            }
        }
        true
    }

    #[inline]
    fn token(&mut self, kind: u16) -> bool {
        if self.current() == kind {
            self.bump();
            return true;
        }
        self.missing_token(kind)
    }

    fn rule(&mut self, r: u32) -> bool {
        let program = self.program;
        let rule = &program.rules[r as usize];
        if !rule.nullable && !self.at_set(rule.first) {
            return self.expected_rule(r);
        }
        let pos = self.pos as u32;
        if self.active[r as usize] == pos {
            return self.no_progress();
        }
        if self.strict == 0 || !self.memoize {
            return self.rule_body(r, pos);
        }
        if let Some((index, hit)) = self.memo.get(r, self.pos) {
            // P09: a result remembered with more levels to spare than are
            // left here could differ from parsing it again; parse it again.
            if self.depth + hit.deep <= MAX_DEPTH {
                return self.replay(index, hit);
            }
        }
        let first_event = self.events.len();
        let taint = self.taint;
        let outer = self.furthest;
        self.furthest = self.pos;
        let outer_depth = self.max_depth;
        self.max_depth = self.depth;
        let ok = self.rule_body(r, pos);
        let deep = self.max_depth - self.depth;
        self.max_depth = outer_depth.max(self.max_depth);
        let reach = self.furthest;
        self.furthest = outer.max(reach);
        if self.taint == taint {
            self.remember(r, pos, first_event, ok, reach, deep);
        }
        ok
    }

    /// Invokes rule `r` at `pos`, marking it active there so that a cycle of
    /// rules that consumes nothing is caught instead of followed.
    fn rule_body(&mut self, r: u32, pos: u32) -> bool {
        let rule = &self.program.rules[r as usize];
        let outer = core::mem::replace(&mut self.active[r as usize], pos);
        if let Some(kind) = rule.node {
            self.events.push(Event::start(kind, 0));
        }
        let base = core::mem::replace(&mut self.mark_base, self.marks.len());
        let sync = rule.sync != NO_SET && self.strict == 0;
        if sync {
            self.stops.push(rule.sync);
        }
        let ok = match rule.body {
            RuleBody::Expr(e) => self.expr(e),
            RuleBody::Pratt(p) => self.pratt(p, r, 0),
        };
        if sync {
            let _ = self.stops.pop();
        }
        self.marks.truncate(self.mark_base);
        self.mark_base = base;
        if ok && rule.node.is_some() {
            self.events.push(Event::FINISH);
        }
        self.active[r as usize] = outer;
        ok
    }

    /// Records a strict attempt's outcome. A success's events stay where
    /// they are, at the end of the event list, and the entry points at them.
    fn remember(
        &mut self,
        rule: u32,
        pos: u32,
        first_event: usize,
        ok: bool,
        reach: usize,
        deep: u32,
    ) {
        let mut entry = MemoEntry {
            rule,
            next: 0,
            end: FAILED,
            reach: reach as u32,
            deep,
            from: 0,
            len: 0,
            origin: 0,
            saved: true,
        };
        if ok {
            entry.end = self.pos as u32;
            let len = self.events.len() - first_event;
            if len > 0 {
                entry.from = first_event as u32;
                entry.len = len as u32;
                entry.saved = false;
            }
        }
        self.memo.put(entry, pos as usize, self.kinds.len());
    }

    /// Repeats a remembered attempt without parsing it again.
    fn replay(&mut self, index: u32, hit: MemoEntry) -> bool {
        self.furthest = self.furthest.max(hit.reach as usize);
        if hit.end == FAILED {
            return false;
        }
        let base = self.events.len();
        self.memo.expand(index, &mut self.events);
        self.memo.replayed(base, self.events.len() - base, index);
        self.pos = hit.end as usize;
        true
    }

    /// Truncates the event list to `keep`, first rescuing the events of any
    /// remembered attempt that would be cut off.
    fn rewind(&mut self, keep: usize) {
        self.memo.rescue(&self.events, keep);
        self.events.truncate(keep);
    }

    fn choice(&mut self, alternatives: &[u32]) -> bool {
        let program = self.program;
        let mut viable = alternatives
            .iter()
            .filter(|&&a| program.nullable[a as usize] || self.at_set(program.first[a as usize]));
        match (viable.next(), viable.next()) {
            (Some(&only), None) => self.expr(only),
            (Some(_), Some(_)) => self.speculate(alternatives),
            (None, _) => self.expected_alternatives(alternatives),
        }
    }

    /// Tries each viable alternative strictly, in order; the first to match
    /// is kept.
    #[inline(never)]
    fn speculate(&mut self, alternatives: &[u32]) -> bool {
        if self.speculating == 0 {
            // A depth limit hit before this speculation began says nothing
            // about it: let it try every alternative again.
            self.hit_limit = false;
        }
        self.speculating += 1;
        let ok = self.try_alternatives(alternatives);
        self.speculating -= 1;
        if self.speculating == 0 {
            self.memo.clear();
            self.hit_limit = false;
        }
        ok
    }

    fn try_alternatives(&mut self, alternatives: &[u32]) -> bool {
        let program = self.program;
        let checkpoint = self.checkpoint();
        let mut best: Option<(u32, usize)> = None;
        for &alt in alternatives {
            if !program.nullable[alt as usize] && !self.at_set(program.first[alt as usize]) {
                continue;
            }
            let outer = self.furthest;
            self.furthest = self.pos;
            self.strict += 1;
            let ok = self.expr(alt);
            self.strict -= 1;
            let reach = self.furthest;
            self.furthest = outer.max(reach);
            if ok {
                return true;
            }
            if best.is_none_or(|(_, r)| reach > r) {
                best = Some((alt, reach));
            }
            self.restore(checkpoint);
            // Results that hit the depth limit cannot be memoized, so trying
            // every alternative at every level of such input would take
            // exponential time. The input is reported as too deep regardless;
            // one attempt per decision keeps the rest of this speculation
            // linear. `speculate` clears the flag when the outermost
            // speculation ends, so the code after the deep construct is
            // parsed in full.
            if self.hit_limit {
                break;
            }
        }
        if self.strict > 0 {
            return false;
        }
        match best {
            Some((alt, _)) => self.expr(alt),
            None => true,
        }
    }

    fn repeat(&mut self, body: u32, min_one: bool, stop: SetId) -> bool {
        let program = self.program;
        let first = program.first[body as usize];
        // The first item of `+` is mandatory: parse it unconditionally, so a
        // missing one is reported by the element that is missing.
        if min_one && !self.expr(body) {
            return false;
        }
        let recovering = self.strict == 0;
        if recovering {
            self.stops.push(stop);
        }
        loop {
            if self.at_set(first) && self.guard(body) {
                let before = self.pos;
                if !self.expr(body) {
                    return false;
                }
                if self.pos == before {
                    // An item that consumed nothing: the end of the input
                    // standing in for a line break, or recovery assuming the
                    // item. Stop, or skip a token, so the loop always ends.
                    if !recovering || self.at_eof() {
                        break;
                    }
                    self.skip();
                }
                continue;
            }
            if !recovering || self.at_eof() || self.in_stop(stop) || self.at_outer_stop() {
                break;
            }
            self.unexpected_item(body);
        }
        if recovering {
            let _ = self.stops.pop();
        }
        true
    }

    fn pratt(&mut self, p: u32, rule: u32, min_bp: u16) -> bool {
        if self.depth >= MAX_DEPTH {
            return self.too_deep();
        }
        self.depth += 1;
        self.max_depth = self.max_depth.max(self.depth);
        let ok = self.pratt_inner(p, rule, min_bp);
        self.depth -= 1;
        ok
    }

    /// The operator level `pratt` gives the current token, if any: from the
    /// `prefix` (or infix/postfix) table, or, for an `IDENT`, a contextual
    /// keyword operator with that text. The second value is the keyword kind
    /// to record the token as.
    fn operator(&self, p: u32, prefix: bool) -> (u8, Option<u16>) {
        let pratt = &self.program.pratts[p as usize];
        let current = self.current();
        let table = if prefix { &pratt.prefix } else { &pratt.after };
        let level = table.get(current as usize).copied().unwrap_or(0);
        if level != 0 || pratt.contextual.is_empty() || current != self.program.ident {
            return (level, None);
        }
        for &(kind, pre, after) in pratt.contextual.iter() {
            let level = if prefix { pre } else { after };
            if level != 0 && self.is_keyword_text(kind, self.pos) {
                // A keyword operator before something that cannot follow an
                // operator is an ordinary identifier (`await;`).
                let next = self
                    .kinds
                    .get(self.pos + 1)
                    .copied()
                    .unwrap_or(self.program.eof);
                let rule_first = self.program.rules.iter().find_map(|r| match r.body {
                    RuleBody::Pratt(q) if q == p => Some(r.first),
                    _ => None,
                });
                let fits = !prefix
                    || rule_first.is_some_and(|f| self.program.sets.contains(f, next as usize));
                if fits {
                    return (level, Some(kind));
                }
            }
        }
        (0, None)
    }

    fn pratt_inner(&mut self, p: u32, rule: u32, min_bp: u16) -> bool {
        let program = self.program;
        let pratt = &program.pratts[p as usize];
        // Format 2 labels the children of operator nodes.
        let labels = program.v2.as_ref().map(|v| v.op_labels);
        // A placeholder for the node that will wrap this operand, if any (and,
        // with labels, one for the `lhs` scope around the operand).
        let slot = self.events.len();
        self.events.push(Event::TOMBSTONE);
        if labels.is_some() {
            self.events.push(Event::TOMBSTONE);
        }
        let mut outer = slot;

        let (prefix, keyword) = self.operator(p, true);
        if prefix != 0 {
            let level = pratt.levels[prefix as usize - 1];
            self.events[slot] = Event::start(level.node, 0);
            self.operator_token(labels, keyword);
            if !self.operator_tail(level) {
                return false;
            }
            if let Some([.., operand]) = labels {
                self.events.push(Event::label(operand));
            }
            if !self.pratt(p, rule, level.rbp) {
                return false;
            }
            if labels.is_some() {
                self.events.push(Event::UNLABEL);
            }
            self.events.push(Event::FINISH);
        } else if self.at_set(program.first[pratt.operand as usize]) {
            if !self.expr(pratt.operand) {
                return false;
            }
        } else {
            return self.expected_rule(rule);
        }

        let mut chained: Option<u8> = None;
        loop {
            let (index, keyword) = self.operator(p, false);
            if index == 0 {
                break;
            }
            let level = pratt.levels[index as usize - 1];
            if level.lbp < min_bp {
                break;
            }
            if level.fixity == Fixity::NonAssoc && chained == Some(index) && !self.chained() {
                return false;
            }
            self.wrap(slot, &mut outer, level.node, labels);
            self.operator_token(labels, keyword);
            if !self.operator_tail(level) {
                return false;
            }
            if level.fixity != Fixity::Postfix {
                if let Some([_, _, rhs, _]) = labels {
                    self.events.push(Event::label(rhs));
                }
                if !self.pratt(p, rule, level.rbp) {
                    return false;
                }
                if labels.is_some() {
                    self.events.push(Event::UNLABEL);
                }
            }
            self.events.push(Event::FINISH);
            chained = (level.fixity == Fixity::NonAssoc).then_some(index);
        }
        true
    }

    /// Consumes an operator token, labelled `op` in format 2, recorded as
    /// `keyword` when it is a contextual keyword.
    fn operator_token(&mut self, labels: Option<[u16; 4]>, keyword: Option<u16>) {
        if let Some([_, op, ..]) = labels {
            self.events.push(Event::label(op));
        }
        match keyword {
            Some(kind) => self.bump_as(kind),
            None => self.bump(),
        }
        if labels.is_some() {
            self.events.push(Event::UNLABEL);
        }
    }

    /// The `then` part of an operator level, if it has one.
    fn operator_tail(&mut self, level: Level) -> bool {
        level.then.is_none_or(|then| self.expr(then))
    }

    /// Opens a node of `kind` around everything since `slot`. With labels,
    /// what it wraps becomes its `lhs` (or `operand`, for postfix).
    fn wrap(&mut self, slot: usize, outer: &mut usize, kind: Kind, labels: Option<[u16; 4]>) {
        if self.events[slot] == Event::TOMBSTONE {
            self.events[slot] = Event::start(kind, 0);
            if let Some([lhs, ..]) = labels {
                self.events[slot + 1] = Event::label(lhs);
                self.events.push(Event::UNLABEL);
            }
            return;
        }
        let new = self.events.len();
        self.events.push(Event::start(kind, 0));
        if let Step::Start { kind: wrapped, .. } = self.events[*outer].step() {
            let wrapped = match labels {
                Some([lhs, ..]) => wrapped.with_label(Some(lhs)),
                None => wrapped,
            };
            self.events[*outer] = Event::start(wrapped, new as u32);
        }
        *outer = new;
    }

    // ----- tokens -----

    #[inline]
    fn current(&self) -> u16 {
        self.kinds[self.pos]
    }

    #[inline]
    fn at_eof(&self) -> bool {
        self.pos + 1 >= self.kinds.len()
    }

    /// Whether the current token can begin something whose FIRST set is
    /// `set`. The end of the input also stands for a line break, so a
    /// `NEWLINE` anywhere a construct may begin accepts it.
    #[inline]
    fn at_set(&self, set: SetId) -> bool {
        let sets = &self.program.sets;
        let current = self.current();
        sets.contains(set, current as usize)
            || (current == self.program.eof && sets.contains(set, self.program.newline as usize))
            || self.keyword_in(set)
    }

    /// Whether the current token is an `IDENT` spelling a contextual keyword
    /// that is in `set` (format 2).
    #[inline]
    fn keyword_in(&self, set: SetId) -> bool {
        !self.contextual.is_empty()
            && self
                .contextual
                .get(self.pos)
                .is_some_and(|&k| k != NOT_KEYWORD && self.program.sets.contains(set, k as usize))
    }

    /// Whether the current token is in stop set `set`.
    #[inline]
    fn in_stop(&self, set: SetId) -> bool {
        self.program.sets.contains(set, self.current() as usize) || self.keyword_in(set)
    }

    fn at_outer_stop(&self) -> bool {
        self.stops.iter().any(|&s| self.in_stop(s))
    }

    #[inline]
    fn bump(&mut self) {
        self.events.push(Event::TOKEN);
        self.pos += 1;
        self.furthest = self.furthest.max(self.pos);
    }

    /// Consumes the current token, recording it as keyword `kind`.
    fn bump_as(&mut self, kind: u16) {
        let kind = self.grammar.kinds.at(kind as usize);
        self.events.push(Event::token_as(kind));
        self.pos += 1;
        self.furthest = self.furthest.max(self.pos);
    }

    /// Wraps the current token in an `ERROR` node, extending the previous one
    /// when it ends right here.
    fn skip(&mut self) {
        if self.error_end == self.events.len() {
            let _ = self.events.pop();
            self.bump();
        } else {
            self.events.push(Event::start(self.program.error, 0));
            self.bump();
        }
        self.events.push(Event::FINISH);
        self.error_end = self.events.len();
    }

    // ----- errors -----
    //
    // Everything below runs only on malformed input. Each method is kept out of
    // line and marked cold, so the recursive functions above stay small: their
    // stack frames bound how deeply the parser can nest, and their code stays
    // hot in the instruction cache.

    /// A required token is missing. In recovering mode it is reported and
    /// assumed — or, when the token after the current one is the expected one,
    /// the current token is skipped as a stray.
    #[cold]
    #[inline(never)]
    fn missing_token(&mut self, kind: u16) -> bool {
        // The end of the input ends the last line too, so a file need not
        // finish with a line break.
        if kind == self.program.newline && self.at_eof() {
            return true;
        }
        if self.strict > 0 {
            return false;
        }
        let message = format!("expected {}, found {}", self.describe(kind), self.found());
        let stray = self.kinds.get(self.pos + 1) == Some(&kind) && !self.at_outer_stop();
        self.report(message);
        if stray {
            self.skip();
            self.bump();
        }
        true
    }

    /// `WORD` found something else.
    #[cold]
    #[inline(never)]
    fn expected_word(&mut self) -> bool {
        if self.strict > 0 {
            return false;
        }
        let message = format!("expected an identifier or keyword, found {}", self.found());
        self.report(message);
        true
    }

    /// A predicate that failed outside speculation: reported, and the parse
    /// carries on as if it had held.
    #[cold]
    #[inline(never)]
    fn failed_predicate(&mut self, body: u32, positive: bool) -> bool {
        if self.strict > 0 {
            return false;
        }
        let message = if positive {
            format!(
                "expected {}, found {}",
                self.describe_expr(body),
                self.found()
            )
        } else {
            format!("{} is not allowed here", capitalize(&self.found()))
        };
        self.report(message);
        true
    }

    /// `EOF` found more input.
    #[cold]
    #[inline(never)]
    fn expected_eof(&mut self) -> bool {
        if self.strict > 0 {
            return false;
        }
        let message = format!("expected the end of the input, found {}", self.found());
        self.report(message);
        true
    }

    /// `LINE_START` or `NL_BEFORE` did not hold.
    #[cold]
    #[inline(never)]
    fn failed_assertion(&mut self, line_start: bool) -> bool {
        if self.strict > 0 {
            return false;
        }
        let message = if line_start {
            format!("expected {} to begin a line", self.found())
        } else {
            format!("expected a line break before {}", self.found())
        };
        self.report(message);
        true
    }

    /// A text back-reference whose text differs from the labelled token's.
    #[cold]
    #[inline(never)]
    fn mismatch(&mut self, want: Option<usize>) {
        let found = self.text(self.pos);
        let message = match want {
            Some(at) => format!("`{found}` does not match `{}`", self.text(at)),
            None => format!("`{found}` has nothing earlier to match"),
        };
        self.report_code(codes::PARSE_MISMATCH, message);
    }

    /// The current token cannot begin rule `r`.
    #[cold]
    #[inline(never)]
    fn expected_rule(&mut self, r: u32) -> bool {
        if self.strict > 0 {
            return false;
        }
        let message = format!(
            "expected {}, found {}",
            self.program.rules[r as usize].name,
            self.found()
        );
        self.report(message);
        true
    }

    /// The current token can begin none of `alternatives`.
    #[cold]
    #[inline(never)]
    fn expected_alternatives(&mut self, alternatives: &[u32]) -> bool {
        if self.strict > 0 {
            return false;
        }
        let sets: Vec<SetId> = alternatives
            .iter()
            .map(|&a| self.program.first[a as usize])
            .collect();
        let message = format!(
            "expected {}, found {}",
            self.describe_sets(&sets),
            self.found()
        );
        self.report(message);
        true
    }

    /// A token no item of the repetition can begin, and that nothing around
    /// it is waiting for: reported and skipped.
    #[cold]
    #[inline(never)]
    fn unexpected_item(&mut self, body: u32) {
        // A run of such tokens is one problem: report its first token only.
        if self.error_end != self.events.len() {
            let message = format!(
                "expected {}, found {}",
                self.describe_expr(body),
                self.found()
            );
            self.report(message);
        }
        self.skip();
    }

    /// A second non-associative operator at the same level, as in `a < b < c`.
    #[cold]
    #[inline(never)]
    fn chained(&mut self) -> bool {
        if self.strict > 0 {
            return false;
        }
        let message = format!(
            "`{}` cannot be chained; add parentheses",
            self.text(self.pos)
        );
        self.report_code(codes::PARSE_CHAINED, message);
        true
    }

    /// A rule entered again where it is already active, with nothing consumed
    /// in between. A grammar that forges cannot do this on valid input — that
    /// would be left recursion — but recovery that assumes a missing token, or
    /// the end of the input standing in for a line break, can. The inner
    /// invocation matches nothing instead of recursing.
    #[cold]
    #[inline(never)]
    fn no_progress(&mut self) -> bool {
        self.taint += 1;
        true
    }

    /// Input after the start rule is complete: one error, one `ERROR` node.
    #[cold]
    #[inline(never)]
    fn leftover(&mut self) {
        let message = format!("expected the end of the input, found {}", self.found());
        self.report_code(codes::PARSE_LEFTOVER, message);
        self.events.push(Event::start(self.program.error, 0));
        while !self.at_eof() {
            self.bump();
        }
        self.events.push(Event::FINISH);
    }

    fn report(&mut self, message: String) {
        self.report_code(codes::PARSE_EXPECTED, message);
    }

    fn report_code(&mut self, code: Code, message: String) {
        if self.last_error == self.pos {
            return;
        }
        self.last_error = self.pos;
        let span = self.spans[self.pos];
        self.diags.push(
            Diagnostic::new(Severity::Error, message, Label::unlabelled(span)).with_code(code),
        );
    }

    #[cold]
    #[inline(never)]
    fn too_deep(&mut self) -> bool {
        self.hit_limit = true;
        if self.strict > 0 {
            self.taint += 1;
            return false;
        }
        if !self.too_deep {
            self.too_deep = true;
            let span = self.spans[self.pos];
            self.diags.push(Diagnostic::new(
                Severity::Error,
                format!(
                    "the input is nested too deeply to parse (more than {MAX_DEPTH} grammar levels)"
                ),
                Label::unlabelled(span),
            ).with_code(codes::PARSE_TOO_DEEP));
        }
        true
    }

    /// The source text of significant token `pos`.
    fn text(&self, pos: usize) -> &'a str {
        let span = self.spans[pos];
        &self.src[span.start().to_usize()..span.end().to_usize()]
    }

    /// The current token, for "found ...".
    fn found(&self) -> String {
        let kind = self.current();
        if self.at_eof() {
            return String::from("the end of the input");
        }
        let text = self.text(self.pos);
        let short: String = text.chars().take(32).collect();
        let ellipsis = if short.len() < text.len() { "…" } else { "" };
        let program = self.program;
        match kind {
            k if k == program.ident => format!("identifier `{short}{ellipsis}`"),
            k if k == program.number => format!("number `{short}{ellipsis}`"),
            k if program.strings.contains(&k) => String::from("a string"),
            k if k == program.newline => String::from("a line break"),
            k if self.grammar.kinds.cats.get(k as usize) == Some(&CAT_CLASS) && text.is_empty() => {
                String::from(self.grammar.kinds.name_at(k as usize))
            }
            _ => format!("`{short}{ellipsis}`"),
        }
    }

    /// A token kind, for "expected ...".
    fn describe(&self, kind: u16) -> String {
        let program = self.program;
        match kind {
            k if k == program.ident => String::from("an identifier"),
            k if k == program.number => String::from("a number"),
            k if program.strings.contains(&k) => String::from("a string"),
            k if k == program.newline => String::from("a line break"),
            k if k == program.eof => String::from("the end of the input"),
            k if matches!(
                self.grammar.kinds.cats.get(k as usize),
                Some(&CAT_CLASS | &CAT_NODE)
            ) =>
            {
                String::from(self.grammar.kinds.name_at(k as usize))
            }
            k => format!("`{}`", self.grammar.kinds.name_at(k as usize)),
        }
    }

    fn describe_expr(&self, e: u32) -> String {
        match self.program.exprs[e as usize] {
            Expr::Rule(r) => String::from(&*self.program.rules[r as usize].name),
            Expr::Token(kind) | Expr::Keyword(kind) => self.describe(kind),
            Expr::Repeat { body, .. }
            | Expr::Optional(body)
            | Expr::Label { body, .. }
            | Expr::And(body)
            | Expr::BackRef { body, .. } => self.describe_expr(body),
            _ => self.describe_sets(&[self.program.first[e as usize]]),
        }
    }

    /// The members of the union of `sets`: "a", "a or b", "a, b, or c".
    fn describe_sets(&self, sets: &[SetId]) -> String {
        let mut members: Vec<usize> = sets
            .iter()
            .flat_map(|&s| self.program.sets.members(s))
            .collect();
        members.sort_unstable();
        members.dedup();
        let mut names: Vec<String> = members
            .iter()
            .take(6)
            .map(|&k| self.describe(k as u16))
            .collect();
        if members.len() > 6 {
            names.truncate(5);
            names.push(String::from("…"));
        }
        match names.len() {
            0 => String::from("nothing"),
            1 => names.remove(0),
            2 => format!("{} or {}", names[0], names[1]),
            n => format!("{}, or {}", names[..n - 1].join(", "), names[n - 1]),
        }
    }
}

/// `text` with its first letter in upper case.
fn capitalize(text: &str) -> String {
    let mut chars = text.chars();
    match chars.next() {
        Some(c) => c.to_uppercase().chain(chars).collect(),
        None => String::new(),
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

    use alloc::string::ToString;

    use proptest::prelude::*;

    use super::*;
    use crate::Language;

    fn forge(rules: &str) -> Language {
        let schematic = format!(
            "[language]\nname = \"t\"\n[lexer]\nline_comments = [\"#\"]\nstrings = ['\"']\n{rules}"
        );
        Language::from_lsf(&schematic).unwrap_or_else(|e| panic!("{e}\n{:?}", e.diagnostics()))
    }

    /// The tree as an S-expression of node names, tokens shown by text.
    fn sexp(lang: &Language, src: &str) -> String {
        fn walk(lang: &Language, src: &str, node: &Node<Kind>, out: &mut String) {
            out.push('(');
            out.push_str(lang.kind_name(*node.kind()));
            for child in node.children() {
                match child {
                    syntax_lang::Element::Node(n) => {
                        out.push(' ');
                        walk(lang, src, n, out);
                    }
                    syntax_lang::Element::Token(t) => {
                        if !syntax_lang::TokenKind::is_trivia(t.kind()) {
                            out.push(' ');
                            out.push_str(
                                &src[t.span().start().to_usize()..t.span().end().to_usize()],
                            );
                        }
                    }
                }
            }
            out.push(')');
        }
        let parse = lang.parse(src);
        let mut out = String::new();
        walk(lang, src, parse.tree(), &mut out);
        out
    }

    fn messages(lang: &Language, src: &str) -> Vec<String> {
        lang.parse(src)
            .diagnostics()
            .iter()
            .map(|d| d.message().to_string())
            .collect()
    }

    fn assert_strict_agrees(lang: &Language, src: &str) {
        let parse = lang.parse(src);
        assert!(!parse.has_errors(), "{src:?}: {:?}", messages(lang, src));
        let strict = parse_strict(lang.grammar(), src).expect("strict parse succeeds");
        assert_eq!(&strict, parse.tree());
    }

    #[test]
    fn test_parse_ordered_choice_speculates_across_rules() {
        let lang = forge(
            "[rules]\nfile = \"stmt*\"\nstmt = \"assign | call\"\n\
             assign = \"IDENT '=' NUMBER ';'\"\ncall = \"IDENT '(' ')' ';'\"\n",
        );
        assert_eq!(
            sexp(&lang, "f(); x = 1;"),
            "(file (stmt (call f ( ) ;)) (stmt (assign x = 1 ;)))"
        );
        assert_strict_agrees(&lang, "f(); x = 1;");
    }

    #[test]
    fn test_parse_left_factored_alternatives() {
        let lang = forge("[rules]\nitem = \"IDENT '=' NUMBER | IDENT '(' ')' | IDENT\"\n");
        assert_eq!(sexp(&lang, "a = 1"), "(item a = 1)");
        assert_eq!(sexp(&lang, "a ( )"), "(item a ( ))");
        assert_eq!(sexp(&lang, "a"), "(item a)");
    }

    #[test]
    fn test_parse_hidden_rules_splice_children() {
        let lang =
            forge("[rules]\nlist = \"_item*\"\n_item = \"NUMBER | word\"\nword = \"IDENT\"\n");
        assert_eq!(sexp(&lang, "1 a 2"), "(list 1 (word a) 2)");
    }

    #[test]
    fn test_parse_pratt_precedence_and_associativity() {
        let lang = forge(
            "[rules.expr]\noperand = \"NUMBER | IDENT | '(' expr ')'\"\nlevels = [\n\
             { left = [\"+\", \"-\"] },\n{ left = [\"*\"] },\n{ prefix = [\"-\"] },\n\
             { right = [\"^\"] },\n{ postfix = [\"!\"] },\n]\n",
        );
        assert_eq!(
            sexp(&lang, "1 + 2 * 3"),
            "(expr (binary 1 + (binary 2 * 3)))"
        );
        assert_eq!(
            sexp(&lang, "1 - 2 - 3"),
            "(expr (binary (binary 1 - 2) - 3))"
        );
        assert_eq!(
            sexp(&lang, "2 ^ 3 ^ 4"),
            "(expr (binary 2 ^ (binary 3 ^ 4)))"
        );
        assert_eq!(sexp(&lang, "-a * b"), "(expr (binary (prefix - a) * b))");
        assert_eq!(sexp(&lang, "-a ^ b"), "(expr (prefix - (binary a ^ b)))");
        assert_eq!(sexp(&lang, "-a!"), "(expr (prefix - (postfix a !)))");
        assert_eq!(
            sexp(&lang, "(1 + 2) * 3"),
            "(expr (binary ( (expr (binary 1 + 2)) ) * 3))"
        );
        for src in ["1 + 2 * 3", "-a ^ b ^ c!", "((1))", "1 - -2"] {
            assert_strict_agrees(&lang, src);
        }
    }

    #[test]
    fn test_parse_pratt_then_tails_and_node_names() {
        let lang = forge(
            "[rules]\nfile = \"expr\"\nargs = \"expr (',' expr)*\"\n[rules.expr]\noperand = \"IDENT | NUMBER\"\nlevels = [\n\
             { right = [\"?\"], then = \"expr ':'\", node = \"ternary\" },\n\
             { left = [\"+\"] },\n\
             { postfix = [\"(\"], then = \"args? ')'\", node = \"call\" },\n\
             { postfix = [\"[\"], then = \"expr ']'\", node = \"index\" },\n]\n",
        );
        assert_eq!(
            sexp(&lang, "f(a, 1)[0] + b"),
            "(file (expr (binary (index (call f ( (args (expr a) , (expr 1)) )) [ (expr 0) ]) + b)))"
        );
        assert_eq!(
            sexp(&lang, "a ? b : c ? d : e"),
            "(file (expr (ternary a ? (expr b) : (ternary c ? (expr d) : e))))"
        );
        assert_eq!(sexp(&lang, "g()"), "(file (expr (call g ( ))))");
        assert_strict_agrees(&lang, "f(a, 1)[0] + b ? x : y");
    }

    #[test]
    fn test_parse_nonassoc_chain_is_reported() {
        let lang = forge(
            "[rules.expr]\noperand = \"NUMBER\"\nlevels = [{ none = [\"<\", \"==\"] }, { left = [\"+\"] }]\n",
        );
        assert!(messages(&lang, "1 < 2 + 3").is_empty());
        assert_eq!(
            messages(&lang, "1 < 2 == 3"),
            ["`==` cannot be chained; add parentheses"]
        );
        assert!(parse_strict(lang.grammar(), "1 < 2 < 3").is_none());
    }

    #[test]
    fn test_parse_missing_token_is_assumed() {
        let lang = forge("[rules]\nfile = \"stmt*\"\nstmt = \"'let' IDENT '=' NUMBER ';'\"\n");
        assert_eq!(
            messages(&lang, "let x = 1 let y = 2;"),
            ["expected `;`, found `let`"]
        );
        assert_eq!(
            sexp(&lang, "let x = 1 let y = 2;"),
            "(file (stmt let x = 1) (stmt let y = 2 ;))"
        );
    }

    #[test]
    fn test_parse_one_stray_token_is_skipped() {
        let lang = forge("[rules]\nfile = \"stmt*\"\nstmt = \"'let' IDENT '=' NUMBER ';'\"\n");
        assert_eq!(
            messages(&lang, "let x = 1 = ;"),
            ["expected `;`, found `=`"]
        );
        assert_eq!(
            sexp(&lang, "let x = 1 = ;"),
            "(file (stmt let x = 1 (ERROR =) ;))"
        );
    }

    #[test]
    fn test_parse_garbage_in_repetition_is_wrapped() {
        let lang = forge("[rules]\nblock = \"'{' stmt* '}'\"\nstmt = \"IDENT ';'\"\n");
        assert_eq!(
            messages(&lang, "{ a; ; ; b; }"),
            ["expected stmt, found `;`"]
        );
        assert_eq!(
            sexp(&lang, "{ a; ; ; b; }"),
            "(block { (stmt a ;) (ERROR ; ;) (stmt b ;) })"
        );
    }

    #[test]
    fn test_parse_outer_stop_set_ends_inner_repetition() {
        let lang = forge(
            "[rules]\nblock = \"'{' stmt* '}'\"\nstmt = \"IDENT '(' (IDENT (',' IDENT)*)? ')' ';'\"\n",
        );
        // The `}` closes the block even though the call is unfinished.
        assert_eq!(sexp(&lang, "{ f(a, b }"), "(block { (stmt f ( a , b) })");
        assert_eq!(messages(&lang, "{ f(a, b }"), ["expected `)`, found `}`"]);
    }

    #[test]
    fn test_parse_leftover_input_is_one_error() {
        let lang = forge("[rules]\npair = \"NUMBER NUMBER\"\n");
        assert_eq!(
            messages(&lang, "1 2 3 4"),
            ["expected the end of the input, found number `3`"]
        );
        assert_eq!(sexp(&lang, "1 2 3 4"), "(pair 1 2 (ERROR 3 4))");
    }

    #[test]
    fn test_parse_empty_input() {
        let lang = forge("[rules]\nfile = \"NUMBER*\"\n");
        let parse = lang.parse("");
        assert!(!parse.has_errors());
        assert_eq!(parse.tree().span(), Span::empty(0));
        let lang = forge("[rules]\nfile = \"NUMBER+\"\n");
        assert_eq!(
            messages(&lang, "  "),
            ["expected file, found the end of the input"]
        );
    }

    #[test]
    fn test_parse_reports_lexer_errors_in_order() {
        let lang = forge("[rules]\nfile = \"(NUMBER | STRING)*\"\n");
        assert_eq!(
            messages(&lang, "1 @ \"open"),
            ["unexpected character `@`", "unterminated string"]
        );
    }

    #[test]
    fn test_parse_deep_nesting_is_bounded() {
        let lang = forge(
            "[rules.expr]\noperand = \"NUMBER | '(' expr ')'\"\nlevels = [{ left = [\"+\"] }]\n",
        );
        let depth = 100_000;
        let src = format!("{}1{}", "(".repeat(depth), ")".repeat(depth));
        // A small stack proves the bound: the parser stops descending long
        // before it could exhaust it.
        let handle = std::thread::Builder::new()
            .stack_size(1024 * 1024)
            .spawn(move || {
                let parse = lang.parse(&src);
                let count = parse
                    .diagnostics()
                    .iter()
                    .filter(|d| d.message().contains("nested too deeply"))
                    .count();
                (count, parse.tree().text(&src).map(str::len))
            })
            .unwrap();
        let (count, covered) = handle.join().unwrap();
        assert_eq!(count, 1);
        assert_eq!(covered, Some(2 * depth + 1));
    }

    #[test]
    fn test_parse_significant_newlines() {
        let schematic = "[language]\nname = \"lines\"\n[lexer]\nnewlines = true\n[rules]\n\
                         file = \"(entry | NEWLINE)*\"\nentry = \"IDENT '=' NUMBER NEWLINE\"\n";
        let lang = Language::from_lsf(schematic).unwrap();
        assert_eq!(
            sexp(&lang, "a = 1\n\nb = 2\n"),
            "(file (entry a = 1 \n) \n (entry b = 2 \n))"
        );
        // The end of the input ends the last line.
        assert_eq!(
            sexp(&lang, "a = 1\nb = 2"),
            "(file (entry a = 1 \n) (entry b = 2))"
        );
        assert_strict_agrees(&lang, "a = 1\nb = 2");
        assert_eq!(
            messages(&lang, "a = 1 b = 2\n"),
            ["expected a line break, found identifier `b`"]
        );

        // A mandatory `NEWLINE+` is satisfied by the end of the input too.
        let schematic = "[language]\nname = \"lines\"\n[lexer]\nnewlines = true\n[rules]\n\
                         file = \"entry*\"\nentry = \"IDENT NEWLINE+\"\n";
        let lang = Language::from_lsf(schematic).unwrap();
        assert!(messages(&lang, "a\n\nb").is_empty());
        assert_strict_agrees(&lang, "a\n\nb");
        assert_eq!(
            messages(&lang, "a b"),
            ["expected a line break, found identifier `b`"]
        );
    }

    #[test]
    fn test_parse_plus_reports_the_missing_element() {
        let lang = forge("[rules]\nlist = \"'[' NUMBER+ ']'\"\n");
        assert_eq!(messages(&lang, "[ ]"), ["expected a number, found `]`"]);
        let lang = forge("[rules]\nlist = \"'[' item+ ']'\"\nitem = \"NUMBER ';'\"\n");
        assert_eq!(messages(&lang, "[ ]"), ["expected item, found `]`"]);
    }

    /// A small statement language for token-soup properties.
    fn soup_language() -> Language {
        forge(
            "[rules]\nfile = \"stmt*\"\n\
             stmt = \"'let' IDENT '=' expr ';' | 'if' expr block ('else' block)? | block | expr ';'\"\n\
             block = \"'{' stmt* '}'\"\nargs = \"expr (',' expr)*\"\n\
             [rules.expr]\noperand = \"NUMBER | IDENT | STRING | '(' expr ')'\"\nlevels = [\n\
             { none = [\"==\", \"<\"] },\n{ left = [\"+\", \"-\"] },\n{ left = [\"*\"] },\n\
             { prefix = [\"-\", \"!\"] },\n{ postfix = [\"(\"], then = \"args? ')'\", node = \"call\" },\n]\n",
        )
    }

    /// Statements that are assignments or expressions, both beginning with an
    /// expression that can hold blocks of statements: every statement
    /// speculates, and shares its prefix across alternatives.
    fn speculative_language() -> Language {
        forge(
            "[rules]\nfile = \"stmt*\"\nstmt = \"assign | expr ';' | 'let' IDENT ';'\"\n\
             assign = \"expr '=' expr ';'\"\nblock = \"'{' stmt* '}'\"\ncall = \"IDENT '(' (expr (',' expr)*)? ')'\"\n\
             [rules.expr]\noperand = \"call | IDENT | NUMBER | block\"\nlevels = [{ left = [\"+\"] }, { prefix = [\"-\"] }]\n",
        )
    }

    const SPECULATIVE_WORDS: [&str; 14] = [
        "x", "f", "1", "=", ";", "{", "}", "(", ")", ",", "+", "-", "let", "y",
    ];

    const VOCABULARY: [&str; 22] = [
        "let", "if", "else", "x", "y", "1", "2", "\"s\"", "=", ";", "{", "}", "(", ")", ",", "+",
        "-", "*", "!", "==", "<", "# c\n",
    ];

    /// Cases per property: 2000, or `PROPTEST_CASES` for a longer soak.
    fn cases() -> u32 {
        std::env::var("PROPTEST_CASES")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(2000)
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(cases()))]

        /// Whatever the input, the tree is lossless, and when no error is
        /// reported the strict parser builds exactly the same tree.
        #[test]
        fn prop_parse_is_lossless_and_agrees_with_strict(words in proptest::collection::vec(0..VOCABULARY.len(), 0..40)) {
            let lang = soup_language();
            let src: String = words.iter().map(|&w| VOCABULARY[w]).collect::<Vec<_>>().join(" ");
            let parse = lang.parse(&src);
            prop_assert_eq!(parse.tree().text(&src), Some(src.as_str()));
            prop_assert_eq!(parse.tree().span(), Span::new(0, src.len() as u32));
            let strict = parse_strict(lang.grammar(), &src);
            if parse.has_errors() {
                prop_assert!(strict.is_none(), "strict accepted input with errors: {:?}", src);
            } else {
                prop_assert_eq!(strict.as_ref(), Some(parse.tree()), "trees differ for {:?}", src);
            }
            for d in parse.diagnostics() {
                prop_assert!(d.primary().span().end().to_usize() <= src.len());
            }
        }

        /// Memoization is invisible: with it on or off, the same tree and the
        /// same diagnostics, on a grammar that speculates at every level.
        #[test]
        fn prop_memoization_never_changes_a_result(words in proptest::collection::vec(0..SPECULATIVE_WORDS.len(), 0..40)) {
            let lang = speculative_language();
            let src: String = words.iter().map(|&w| SPECULATIVE_WORDS[w]).collect::<Vec<_>>().join(" ");
            let memoized = parse(lang.grammar(), &src);
            let plain = parse_unmemoized(lang.grammar(), &src);
            prop_assert_eq!(&memoized.0, &plain.0, "trees differ for {:?}", src);
            prop_assert_eq!(memoized.1, plain.1, "diagnostics differ for {:?}", src);
        }

        #[test]
        fn prop_parse_arbitrary_text_never_panics(src in "\\PC{0,80}") {
            let lang = soup_language();
            let parse = lang.parse(&src);
            prop_assert_eq!(parse.tree().text(&src), Some(src.as_str()));
        }
    }

    /// Nested speculation that rewinds at every level, so the memo rescues
    /// spans that hold replays of spans it rescued before: memoized parsing
    /// must still agree with unmemoized parsing exactly, with and without
    /// errors. (Past the depth limit the two may differ: a rule remembered
    /// at a shallow depth is replayed where parsing it would hit the limit.
    /// Such input is reported as nested too deeply either way.)
    #[test]
    fn test_memo_rescues_agree_with_unmemoized_parsing() {
        let lang = speculative_language();
        let nested = |depth: usize, width: usize, inner: &str, tail: &str| {
            let mut src = String::new();
            for _ in 0..depth {
                src.push_str("{ ");
                for i in 0..width {
                    src.push_str(&format!("f(x{i}, {{ y; }}) + -1; z = {{ {i}; }}; "));
                }
            }
            src.push_str(inner);
            for _ in 0..depth {
                src.push_str(tail);
            }
            src
        };
        for (depth, width, inner, tail) in [
            (1, 1, "x;", " };"),
            (4, 2, "x = 1;", " };"),
            (6, 3, "x", " }"),
            (6, 3, "x = ;", " } = 1;"),
            (8, 1, "f(", " };"),
            (10, 1, "x;", " };"),
        ] {
            let src = nested(depth, width, inner, tail);
            let handle = std::thread::Builder::new()
                .stack_size(8 << 20)
                .spawn({
                    let lang = lang.clone();
                    move || {
                        let memoized = parse(lang.grammar(), &src);
                        let plain = parse_unmemoized(lang.grammar(), &src);
                        assert_eq!(memoized.0, plain.0, "trees differ for {src:?}");
                        assert_eq!(memoized.1, plain.1, "diagnostics differ for {src:?}");
                        assert_eq!(memoized.0.text(&src), Some(src.as_str()));
                    }
                })
                .unwrap();
            handle.join().unwrap();
        }
    }

    // ----- format 2 -----

    /// A format-2 grammar using every parser feature format 2 adds: labels
    /// (on tokens, nodes, groups), predicates, a text back-reference,
    /// contextual keywords (in rules and as Pratt operators), case-folded
    /// keywords, interpolated strings with a rule inside, and ordered
    /// choice that speculates across rules.
    fn v2_language() -> Language {
        let sketch = "[sketch]\nformat = 2\n[language]\nname = \"v\"\nversion = \"1.0.0\"\n\
             [lexer]\nline_comments = [\"#\"]\n\
             [lexer.keywords]\ncontextual = [\"async\", \"await\", \"is\"]\ncase = \"ascii-insensitive\"\n\
             [lexer.strings.DQ]\nopen = '\"'\nescape = \"\\\\\"\ninterpolate = [{ open = \"{\", close = \"}\", rule = \"expr\" }]\n\
             [rules]\nfile = \"items:stmt*\"\n\
             stmt = \"assign | fn_def | block_stmt | value:expr ';' | 'let' name:IDENT ';'\"\n\
             assign = \"target:expr '=' value:expr ';'\"\n\
             fn_def = \"'async'? 'fn' name:IDENT '(' (params:IDENT (',' params:IDENT)*)? ')' body:block\"\n\
             block_stmt = \"'begin' tag:IDENT body:stmt* 'end' IDENT=tag ';'\"\n\
             block = \"'{' stmts:stmt* '}'\"\n\
             call = \"callee:IDENT !'=' '(' (args:expr (',' args:expr)*)? ')'\"\n\
             [rules.expr]\noperand = \"call | IDENT | NUMBER | DQ | block\"\n\
             levels = [{ left = [\"is\"] }, { left = [\"+\"] }, { prefix = [\"-\", \"await\"] }]\n";
        Language::from_lsf(sketch).unwrap_or_else(|e| panic!("{e}\n{:?}", e.diagnostics()))
    }

    const V2_WORDS: [&str; 26] = [
        "x",
        "f",
        "1",
        "=",
        ";",
        "{",
        "}",
        "(",
        ")",
        ",",
        "+",
        "-",
        "let",
        "async",
        "await",
        "is",
        "fn",
        "FN",
        "begin",
        "end",
        "\"a {x} b\"",
        "\"",
        "{x",
        "# c\n",
        "\\",
        "IS",
    ];

    /// The tree as text, with every kind and label: trees that differ only
    /// in a label differ here (kinds compare equal regardless of labels).
    fn labelled_dump(lang: &Language, node: &Node<Kind>) -> String {
        fn walk(lang: &Language, node: &Node<Kind>, depth: usize, out: &mut String) {
            out.push_str(&format!(
                "{depth}:{:?}:{}@{:?}\n",
                node.kind().label(),
                lang.kind_name(*node.kind()),
                node.span()
            ));
            for child in node.children() {
                match child {
                    syntax_lang::Element::Node(n) => walk(lang, n, depth + 1, out),
                    syntax_lang::Element::Token(t) => {
                        out.push_str(&format!(
                            "{}:{:?}:{}@{:?}\n",
                            depth + 1,
                            t.kind().label(),
                            lang.kind_name(*t.kind()),
                            t.span()
                        ));
                    }
                }
            }
        }
        // The test inputs nest a few dozen levels at most.
        let mut out = String::new();
        walk(lang, node, 0, &mut out);
        out
    }

    #[test]
    fn test_v2_samples_agree_with_strict() {
        let lang = v2_language();
        for src in [
            "let x; x = f(1, -2) + 3; async fn g(a, b) { await g(1); }",
            "begin a x; begin b end b; end a; \"s {x + 1} t\";",
            "async = 1; await; x is f(1) is 2; FN h() { }",
        ] {
            let parse = lang.parse(src);
            assert!(!parse.has_errors(), "{src:?}: {:?}", messages(&lang, src));
            let strict = parse_strict(lang.grammar(), src).expect("strict parse succeeds");
            assert_eq!(
                labelled_dump(&lang, &strict),
                labelled_dump(&lang, parse.tree())
            );
        }
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(cases()))]

        /// Format 2: lossless, labels included in the strict agreement.
        #[test]
        fn prop_v2_parse_is_lossless_and_agrees_with_strict(words in proptest::collection::vec(0..V2_WORDS.len(), 0..40)) {
            let lang = v2_language();
            let src: String = words.iter().map(|&w| V2_WORDS[w]).collect::<Vec<_>>().join(" ");
            let parse = lang.parse(&src);
            prop_assert_eq!(parse.tree().text(&src), Some(src.as_str()));
            prop_assert_eq!(parse.tree().span(), Span::new(0, src.len() as u32));
            let strict = parse_strict(lang.grammar(), &src);
            // Lexical errors (an unknown character, an unterminated string)
            // are the lexer's; strict mode judges the grammar only.
            let parse_errors = parse
                .diagnostics()
                .iter()
                .any(|d| d.code().is_some_and(|c| c.to_string().starts_with("LF1")));
            if parse_errors {
                prop_assert!(strict.is_none(), "strict accepted input with errors: {:?}", src);
            } else {
                let strict = strict.expect("strict agrees");
                prop_assert_eq!(labelled_dump(&lang, &strict), labelled_dump(&lang, parse.tree()), "trees differ for {:?}", src);
            }
        }

        /// Format 2: memoization is invisible, labels included.
        #[test]
        fn prop_v2_memoization_never_changes_a_result(words in proptest::collection::vec(0..V2_WORDS.len(), 0..40)) {
            let lang = v2_language();
            let src: String = words.iter().map(|&w| V2_WORDS[w]).collect::<Vec<_>>().join(" ");
            let memoized = parse(lang.grammar(), &src);
            let plain = parse_unmemoized(lang.grammar(), &src);
            prop_assert_eq!(labelled_dump(&lang, &memoized.0), labelled_dump(&lang, &plain.0), "trees differ for {:?}", src);
            prop_assert_eq!(memoized.1, plain.1, "diagnostics differ for {:?}", src);
        }

        #[test]
        fn prop_v2_arbitrary_text_never_panics(src in "\\PC{0,80}") {
            let lang = v2_language();
            let parse = lang.parse(&src);
            prop_assert_eq!(parse.tree().text(&src), Some(src.as_str()));
        }
    }

    /// ISSUES P09: a rule remembered at a shallow depth must not be replayed
    /// where parsing it would pass the depth limit. Here `x` is parsed
    /// under `a` (depth d) and memoized, then needed again under `b`, two
    /// levels deeper; near the limit, replaying it would accept input that
    /// the unmemoized parser reports as nested too deeply. Memoized and
    /// unmemoized parsing agree at every nesting around the limit.
    #[test]
    fn test_memo_replay_respects_the_depth_limit() {
        let lang = forge(
            "[rules]\ns = \"a | b\"\na = \"x 'q'\"\nb = \"w\"\nw = \"v\"\nv = \"x 'r'\"\nx = \"'(' x ')' | 'z'\"\n",
        );
        // One parenthesis costs several grammar levels, so the nestings tried
        // start well below the limit; without the P09 check they differ
        // from nesting 254 on.
        let limit = MAX_DEPTH as usize;
        let handle = std::thread::Builder::new()
            .stack_size(64 << 20)
            .spawn(move || {
                let mut deep = 0;
                for n in (limit / 4)..(limit + 4) {
                    let src = format!("{}z{} r", "(".repeat(n), ")".repeat(n));
                    let memoized = parse(lang.grammar(), &src);
                    let plain = parse_unmemoized(lang.grammar(), &src);
                    assert_eq!(memoized.0, plain.0, "trees differ at nesting {n}");
                    assert_eq!(memoized.1, plain.1, "diagnostics differ at nesting {n}");
                    assert_eq!(memoized.0.text(&src), Some(src.as_str()));
                    if memoized
                        .1
                        .iter()
                        .any(|d| d.message().contains("nested too deeply"))
                    {
                        deep += 1;
                    }
                }
                // The range crosses the limit: some inputs are too deep.
                assert!(deep > 0);
            })
            .unwrap();
        handle.join().unwrap();
    }

    #[test]
    fn test_parse_soup_samples_agree() {
        let lang = soup_language();
        for src in [
            "let x = 1 + 2 * -y; if x == 1 { x(1, 2); } else { }",
            "{ { } }",
            "x((1)) ;",
        ] {
            assert_strict_agrees(&lang, src);
        }
    }
}