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
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
//! Embedded-action grammar model and `$`-attribute translator.
//!
//! In embedded mode the generator receives a grammar whose actions and
//! predicates are already **real Rust code** — rendered by the conformance
//! harness through `Rust.test.stg`, exactly like every official ANTLR target
//! renders its `.test.stg` — and splices those bodies verbatim into the
//! generated recognizer. The only rewriting applied is ANTLR's own
//! `$attribute` reference translation (the Rust analog of ANTLR's
//! `ActionTranslator`): `$text`, `$ctx`, `$_p`, rule/token/label references,
//! and rule attribute (`args`/`returns`/`locals`) reads and writes.
//!
//! This module consumes the structural grammar model needed for that
//! translation: per-rule attribute declarations, per-alternative element
//! references with labels (for `$label.attr` occurrence resolution), and
//! `@members` bodies split into struct fields, impl items, and module items.
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;
use std::io;
use crate::templates::{matching_action_brace, skip_ascii_whitespace};
/// One `name: type` attribute declared in a rule's `[...]` args clause or
/// `returns [...]` / `locals [...]` clauses.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AttrDecl {
pub(crate) name: String,
/// Rust type after mapping (Java `int` -> `i32`, `boolean` -> `bool`, …).
pub(crate) ty: String,
}
/// Number of children with one grammar target that an alternative can emit.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ChildCardinality {
pub(crate) min: usize,
/// `None` denotes an unbounded maximum.
pub(crate) max: Option<usize>,
}
impl ChildCardinality {
pub(crate) const ZERO: Self = Self {
min: 0,
max: Some(0),
};
pub(crate) const ONE: Self = Self {
min: 1,
max: Some(1),
};
pub(crate) const fn is_required_single(self) -> bool {
self.min == 1 && matches!(self.max, Some(1))
}
pub(crate) const fn is_repeated(self) -> bool {
match self.max {
Some(max) => max > 1,
None => true,
}
}
}
/// Suffix marking a label that was temporarily renamed while resolving a *sibling*
/// declaration of the same label in isolation. Grammar labels are identifiers, so
/// this cannot collide with a real one.
const SIBLING_DECLARATION_SUFFIX: &str = " (sibling declaration)";
/// One enclosing block: its byte extent, and whether its own quantifier relaxes
/// the lower bound of the elements inside it.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GroupSpan {
pub(crate) start: usize,
pub(crate) end: usize,
/// `true` for `(…)?` / `(…)*` — the group may contribute nothing.
pub(crate) optional: bool,
/// `true` for `(…)*` / `(…)+` — the group may run more than once, so the number
/// of children it contributes is not fixed even when it is known to have run.
pub(crate) repeated: bool,
}
/// One element reference inside an alternative: a rule ref, token ref, or a
/// labeled sub-block, in source order.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ElementRef {
pub(crate) label: Option<String>,
/// Referenced rule or token spelling; empty for token sets and wildcards.
pub(crate) target: String,
/// Token types matched by this element. Empty for rule references.
pub(crate) token_types: Vec<i32>,
pub(crate) is_block: bool,
/// `label+=ref` list label.
pub(crate) is_list: bool,
/// Cardinality of this element after its direct EBNF suffix.
pub(crate) cardinality: ChildCardinality,
/// Whether source-order occurrence lookup is unambiguous for a generated
/// label accessor. Single-alternative EBNF groups preserve it; choices opt
/// out because their flattened CST children do not retain the chosen path.
pub(crate) stable_accessor: bool,
/// `(choice id, alternative index)` for every enclosing *multi*-alternative
/// block, outermost first. Two refs that share a choice id but sit in
/// different alternatives of it are mutually exclusive: no parse contains
/// both. Empty means the ref is on the rule's own sequential path.
///
/// The whole ancestry is kept, not just the innermost choice: for
/// `((x=e | f) | e)` the labeled `x` and the trailing `e` are separated by
/// the *outer* choice, which an innermost-only tag would lose.
pub(crate) choice_branch: Vec<(usize, usize)>,
/// Alternative count of each choice named in `choice_branch`, in the same
/// order. Recorded at collection time because an *empty* alternative emits no
/// ref at all, so the branch count cannot be recovered from the refs alone —
/// `(a=A | )` would otherwise look like a one-branch choice that always
/// yields an `A`.
pub(crate) choice_arity: Vec<usize>,
/// Byte span of each enclosing choice *block*, in the same order as
/// `choice_branch`. An action lies inside a branch only when its offset falls
/// within the block's span — refs alone cannot tell `(A | xs+=A) {…}` (action
/// after the group) from `(A x=A {…} | B)` (action inside it), since both put
/// branch refs on either side of the action.
pub(crate) choice_spans: Vec<(usize, usize)>,
/// Cardinality with every *enclosing* quantifier and choice split treated as
/// satisfied — only this element's own EBNF suffix applies. An action inside
/// `(A x=A {…})?` runs only when the group matched, so on that path the
/// preceding `A` is exactly-once even though both `cardinality` and
/// `branch_local_cardinality` report `0..1` from the group's `?`.
pub(crate) group_local_cardinality: ChildCardinality,
/// Byte span and lower-bound-relaxing flag of *every* enclosing block, including
/// single-alternative groups that `choice_spans` omits. The flag says whether
/// that group's own quantifier is what made this element optional (`(…)?` or
/// `(…)*`), so a group known taken can have *its* contribution removed without
/// disturbing the others: in `((q) x=q {…})?` the outer `?` is satisfied when the
/// action runs while the inner `(q)` is mandatory and already closed.
pub(crate) group_spans: Vec<GroupSpan>,
/// Byte span of each enclosing choice *alternative* (the branch itself), in the
/// same order as `choice_branch`. Lets an action be attributed to the branch
/// whose text contains it, including a branch holding only actions or
/// predicates — such a branch emits no `ElementRef` at all, so ref spans alone
/// would attribute the action to a neighbouring branch.
pub(crate) branch_spans: Vec<(usize, usize)>,
/// Whether no terminal can precede this element on its own parse path. Only
/// then do a block read (which indexes every terminal child) and a token read
/// (which indexes only same-type children) provably agree, so this is what
/// makes a mixed-mode merge sound — occurrence zero alone is not enough, since
/// a token-mode zero can still sit at a non-zero terminal position.
pub(crate) leading_terminal: bool,
/// Byte span of the element in the grammar source, when known. A mid-rule
/// action executes at *its* source position, so only refs that start before
/// the action's offset have been matched when its body runs.
pub(crate) span: Option<(usize, usize)>,
/// Cardinality this element would have if every enclosing choice took the
/// branch containing it — i.e. with only the *quantifiers* applied, not the
/// `min: 0` that `choice_branch` membership imposes.
///
/// `(a=A | b=A) x=A` and `(a=A | b=A)? x=A` give their branch refs the same
/// `cardinality` (`0..1`), yet the first choice always yields one `A` and the
/// second may yield none. Only this field separates them.
pub(crate) branch_local_cardinality: ChildCardinality,
}
impl ElementRef {
/// Whether `self` and `other` can both appear in one parse. They cannot when
/// any choice encloses both in *different* alternatives.
pub(crate) fn can_coexist_with(&self, other: &Self) -> bool {
!self.choice_branch.iter().any(|(choice, branch)| {
other
.choice_branch
.iter()
.any(|(other_choice, other_branch)| {
choice == other_choice && branch != other_branch
})
})
}
/// Drops the choices `discard` selects, keeping every parallel choice array
/// aligned with `choice_branch`.
///
/// `choice_arity`, `choice_spans`, and `branch_spans` are indexed *by position*
/// in `choice_branch`, so removing an entry must remove the same position from
/// each. Truncating to the surviving length instead keeps the outermost
/// entries — which is wrong whenever an *outer* choice is the one dropped:
/// `((q | q | b) x=q | c)` would then read the outer choice's arity of 2
/// against the surviving inner choice, mistaking a three-way choice for an
/// exhaustive two-way one.
pub(crate) fn retain_choices(&mut self, mut keep: impl FnMut(usize) -> bool) {
let mask = self
.choice_branch
.iter()
.map(|&(choice, _)| keep(choice))
.collect::<Vec<_>>();
fn retain_by_mask<T>(list: &mut Vec<T>, mask: &[bool]) {
let mut index = 0;
list.retain(|_| {
let kept = mask.get(index).copied().unwrap_or(true);
index += 1;
kept
});
}
retain_by_mask(&mut self.choice_branch, &mask);
retain_by_mask(&mut self.choice_arity, &mask);
retain_by_mask(&mut self.choice_spans, &mask);
retain_by_mask(&mut self.branch_spans, &mask);
}
}
/// One top-level alternative of a parser rule.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AltModel {
/// `# altLabel`, if present.
pub(crate) label: Option<String>,
/// Byte span of the alternative inside the grammar source.
pub(crate) span: (usize, usize),
pub(crate) refs: Vec<ElementRef>,
/// Aggregate child cardinality by referenced rule or symbolic token.
pub(crate) children: BTreeMap<String, ChildCardinality>,
/// Target of the first syntactic element when it is a bare (possibly
/// labeled) rule/token reference; `None` for a leading literal, set,
/// block, or action. ANTLR's left-recursion transformer only treats an
/// alternative as an operator alternative when the recursion is the
/// first element, so `'(' e ')'` stays primary even though its first
/// *reference* is the rule itself.
pub(crate) leading_target: Option<String>,
}
impl AltModel {
/// Whether this is a left-recursive operator alternative of `rule_name`.
pub(crate) fn is_lr_operator(&self, rule_name: &str) -> bool {
self.leading_target.as_deref() == Some(rule_name)
}
}
/// Structural model of one compiled parser rule.
#[derive(Clone, Debug, Default)]
pub(crate) struct RuleModel {
pub(crate) name: String,
/// Args, returns and locals, flattened (names are unique per rule in the
/// runtime testsuite corpus).
pub(crate) attrs: Vec<AttrDecl>,
/// Names declared specifically by the rule's `locals [...]` clause.
pub(crate) local_names: Vec<String>,
/// Names of the attrs that come from the `[...]` args clause, in order —
/// call sites initialize these positionally (`a[2]`).
pub(crate) arg_names: Vec<String>,
pub(crate) init_body: Option<String>,
pub(crate) after_body: Option<String>,
pub(crate) alts: Vec<AltModel>,
}
impl RuleModel {
pub(crate) const fn has_attrs(&self) -> bool {
!self.attrs.is_empty()
}
fn attr(&self, name: &str) -> Option<&AttrDecl> {
self.attrs.iter().find(|attr| attr.name == name)
}
/// The alternative whose span contains `offset`, if any.
fn alt_at(&self, offset: usize) -> Option<&AltModel> {
self.alts
.iter()
.find(|alt| alt.span.0 <= offset && offset < alt.span.1)
}
}
/// One member field declared through the target's field-with-initializer
/// members convention (`i: i32 = 0;`).
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct MemberField {
pub(crate) name: String,
pub(crate) ty: String,
pub(crate) init: String,
}
/// `@members` content split by item kind.
#[derive(Clone, Debug, Default)]
pub(crate) struct MembersModel {
/// Field declarations lowered onto the recognizer struct.
pub(crate) fields: Vec<MemberField>,
/// `fn` items spliced into the recognizer's inherent `impl` block.
pub(crate) impl_items: Vec<String>,
/// `struct` / `impl` / attribute-prefixed items emitted at module level
/// (test listeners, custom nodes, …).
pub(crate) module_items: Vec<String>,
}
/// Full grammar model for embedded translation.
#[derive(Clone, Debug, Default)]
pub(crate) struct EmbeddedModel {
/// Parser rules keyed by parser rule index (grammar order).
pub(crate) rules: Vec<RuleModel>,
pub(crate) parser_members: MembersModel,
}
/// Where an action body executes, which changes how `$text` translates.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ActionSite {
/// Mid-rule action: an `action` local minted by
/// `parser_action_at_current` is in scope.
Body,
/// Rule `@after`: runs after the body, before `finish_rule`.
After,
/// Rule `@init`: runs at rule entry.
Init,
}
/// Maps a grammar attribute type (possibly Java-flavored, possibly already
/// Rust from the rendered templates) onto the Rust type the generated attrs
/// struct uses.
pub(crate) fn map_attr_type(raw: &str) -> String {
let raw = raw.trim();
if let Some(inner) = raw
.strip_prefix("List")
.map(str::trim_start)
.and_then(|rest| rest.strip_prefix('<'))
.and_then(|inner| inner.strip_suffix('>'))
.map(str::trim)
.filter(|inner| !inner.is_empty())
{
return format!("Vec<{}>", map_attr_type(inner));
}
match raw {
"int" | "Integer" => "i32".to_owned(),
"boolean" => "bool".to_owned(),
"float" | "double" => "f64".to_owned(),
other => other.to_owned(),
}
}
/// Splits `name: type`, tolerating generic types containing `:` (`Vec<T>` has
/// none today, but `::` paths do appear, e.g. `std::string::String`).
fn split_name_colon_type(part: &str) -> Option<(&str, &str)> {
let colon = part.find(':')?;
if part[colon..].starts_with("::") {
return None;
}
let name = part[..colon].trim();
let ty = part[colon + 1..].trim();
(is_identifier(name) && !ty.is_empty()).then_some((name, ty))
}
fn is_identifier(value: &str) -> bool {
let mut chars = value.chars();
chars
.next()
.is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic())
&& chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
}
/// Splits a members body into field declarations, impl items, and module
/// items.
pub(crate) fn classify_members(body: &str, members: &mut MembersModel) -> io::Result<()> {
let mut offset = 0;
let mut pending_attrs = String::new();
while offset < body.len() {
offset = skip_ascii_whitespace(body, offset);
if offset >= body.len() {
break;
}
let rest = &body[offset..];
if rest.starts_with("//") {
offset += rest.find('\n').map_or(rest.len(), |nl| nl + 1);
} else if rest.starts_with('#') {
// `#[derive(..)]` / `#[allow(..)]` — attaches to the next item.
let Some(close) = rest.find(']') else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"unterminated attribute in @members block",
));
};
pending_attrs.push_str(&rest[..=close]);
pending_attrs.push('\n');
offset += close + 1;
} else if rest.starts_with("fn ") {
let item_end = item_end_from(body, offset)?;
let mut item = std::mem::take(&mut pending_attrs);
item.push_str(body[offset..item_end].trim());
members.impl_items.push(item);
offset = item_end;
} else if rest.starts_with("struct ")
|| rest.starts_with("impl ")
|| rest.starts_with("use ")
{
let item_end = item_end_from(body, offset)?;
let mut item = std::mem::take(&mut pending_attrs);
item.push_str(body[offset..item_end].trim());
members.module_items.push(item);
offset = item_end;
} else if let Some(field) = parse_member_field(&body[offset..]) {
let (field, consumed) = field;
members.fields.push(field);
offset += consumed;
} else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"unsupported @members item starting at: {}",
&rest[..rest.len().min(60)]
),
));
}
}
Ok(())
}
/// Finds the end of an item: the matching `}` of its first top-level brace
/// block, or the terminating `;` for braceless items (`use x;`).
fn item_end_from(body: &str, offset: usize) -> io::Result<usize> {
let mut quoted = false;
let mut escaped = false;
let mut index = offset;
while let Some(ch) = body[index..].chars().next() {
if escaped {
escaped = false;
index += ch.len_utf8();
continue;
}
match ch {
'\\' if quoted => escaped = true,
'"' => quoted = !quoted,
'{' if !quoted => {
let close = matching_action_brace(body, index + 1).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"unterminated brace in @members item",
)
})?;
return Ok(close + 1);
}
';' if !quoted => return Ok(index + 1),
_ => {}
}
index += ch.len_utf8();
}
Err(io::Error::new(
io::ErrorKind::InvalidData,
"unterminated @members item",
))
}
/// Parses one `name: type = init;` member-field declaration; returns the
/// field and the number of bytes consumed.
fn parse_member_field(rest: &str) -> Option<(MemberField, usize)> {
let semicolon = rest.find(';')?;
let decl = &rest[..semicolon];
if decl.contains('{') || decl.contains('(') {
return None;
}
let (name_ty, init) = decl.split_once('=')?;
let (name, ty) = split_name_colon_type(name_ty.trim())?;
Some((
MemberField {
name: name.to_owned(),
ty: ty.to_owned(),
init: init.trim().to_owned(),
},
semicolon + 1,
))
}
/// Context for translating one action/predicate body.
pub(crate) struct TranslationCtx<'a> {
pub(crate) model: &'a EmbeddedModel,
/// Rule containing the body.
pub(crate) rule_index: usize,
/// Byte offset of the body inside the grammar source, used to pick the
/// enclosing alternative for label resolution. `None` for `@init` /
/// `@after` bodies (labels resolve across all alternatives there).
pub(crate) body_offset: Option<usize>,
pub(crate) site: ActionSite,
/// Token name -> token type, from the compiled recognizer metadata.
pub(crate) token_types: &'a BTreeMap<String, i32>,
}
impl TranslationCtx<'_> {
fn rule(&self) -> &RuleModel {
&self.model.rules[self.rule_index]
}
fn rule_index_by_name(&self, name: &str) -> Option<usize> {
self.model.rules.iter().position(|rule| rule.name == name)
}
/// Resolves a label to `(ref, occurrence-among-same-target-in-alt)`.
///
/// Every read `translate_element_read` can emit is a *positional* query over
/// the flattened CST children — `nth(i)` for a single label, "all children of
/// this target" for a list label, "the last terminal child" for a block
/// label. None of those retain which grammar branch built a child, so a
/// label only resolves when its read provably selects the label's own
/// element and nothing else. When it cannot, this returns `None` and the
/// caller fails loudly rather than translating to a silently wrong read.
///
/// The conditions that make a read unfaithful, by label kind:
///
/// * **single** — a preceding ref with inexact cardinality (sibling branches
/// of a choice are mutually exclusive and report `min: 0`, so counting
/// them indexes past what the parse built), or an *optional* label with a
/// following same-target child that slides into its position when absent;
/// * **list** — any same-target child outside the label, since the read
/// cannot exclude it;
/// * **block** — a following terminal, which would become the `last()` the
/// read takes;
/// * **any kind** — a second declaration of the same label that this one
/// read cannot also serve.
fn resolve_label(&self, label: &str) -> Option<(ElementRef, usize)> {
let rule = self.rule();
if self.site == ActionSite::Init {
// An `@init` body runs at rule entry, before any child exists, so every
// read over children is empty and nothing can pollute it — no hazard
// applies. Most reads degrade gracefully (an iterator yields nothing, a
// `.text` read yields `""`), but a *scalar rule* label lowers to
// `.nth(i).expect("labeled rule child")`, which panics on every parse.
// Decline that one rather than emit code that cannot run.
let element = rule
.alts
.iter()
.flat_map(|alt| alt.refs.iter())
.find(|element| element.label.as_deref() == Some(label))?;
let panics_when_absent =
!element.is_list && element.token_types.is_empty() && !element.target.is_empty();
return (!panics_when_absent).then(|| (element.clone(), 0));
}
if let Some((offset, alt)) = self
.body_offset
.and_then(|offset| rule.alt_at(offset).map(|alt| (offset, alt)))
{
// A mid-rule action executes at its own source position, so only refs
// starting before it have been matched, and only branches enclosing
// that position can have run.
return Self::resolve_label_in_alt(alt, label, Some(offset));
}
// `@after` / `@init` bodies are not scoped to an alternative, so the
// label may be declared in several. One read has to serve whichever
// alternative the parse took: taking the first match would emit that
// alternative's lookup and silently yield a default on the others.
let mut resolved: Option<(ElementRef, usize)> = None;
let mut non_declaring = Vec::new();
for alt in &rule.alts {
let declares = alt
.refs
.iter()
.any(|element| element.label.as_deref() == Some(label));
if !declares {
non_declaring.push(alt);
continue;
}
// An `@after` / `@init` body runs whichever branch the parse took, so
// a sibling branch's match *can* be the child present when the read
// executes — sibling exclusion does not apply here.
let candidate = Self::resolve_label_in_alt(alt, label, None)?;
if resolved
.as_ref()
.is_some_and(|existing| !Self::same_label_read(existing, &candidate))
{
return None;
}
resolved = Some(candidate);
}
let (element, occurrence) = resolved?;
// An alternative that never declares the label leaves it unset, so the
// read must come up empty there. It will not if that alternative happens
// to build a child the read would select anyway (`r : x=A | A`), which
// would report a value for a label the parse never bound.
for alt in non_declaring {
if Self::alt_can_satisfy_read(alt, &element, occurrence) {
return None;
}
}
Some((element, occurrence))
}
/// Whether `alt` builds a child that the read for `element` would select,
/// even though `alt` does not declare the label.
fn alt_can_satisfy_read(alt: &AltModel, element: &ElementRef, occurrence: usize) -> bool {
// Route on `is_block`, matching `translate_element_read`: a *literal* label
// (`x='a'`) is block-mode yet keeps a non-empty source target, so keying on
// an empty target here would fall through to token-type matching while the
// read actually ignores token type entirely.
if element.is_block {
// A positional block read selects a terminal by index, so the
// alternative can satisfy it whenever it builds a terminal there.
return alt
.refs
.iter()
.filter(|candidate| {
!candidate.token_types.is_empty() && candidate.cardinality.max != Some(0)
})
.any(|candidate| Self::can_occupy_terminal_index(alt, candidate, occurrence));
}
// The read queries by token *type*, so a differently-spelled terminal with
// the same type is the same child (`A : 'a';` makes `A` and `'a'` one).
let same_read_target = |candidate: &ElementRef| {
if element.token_types.is_empty() || candidate.token_types.is_empty() {
return candidate.target == element.target;
}
candidate
.token_types
.iter()
.any(|token_type| element.token_types.contains(token_type))
};
// The most matching children *one parse* can build. Sequential refs add;
// branches of a choice are alternatives, so the widest branch wins. Nested
// choices must fold innermost-first — reducing each choice independently
// and then summing would double-count, rejecting valid reads such as
// `q x=q | ((q|b)|(q|c))` where the second alternative builds one `q`.
let available = Self::widest_child_count(
alt.refs
.iter()
.filter(|candidate| same_read_target(candidate)),
);
// A list read selects any same-target child; a positional read needs one
// at `occurrence`. An unbounded count can always reach either.
available.is_none_or(|available| available > occurrence)
}
/// Whether `candidate` can occupy terminal index `occurrence` on its own parse
/// path. A *repeated* candidate spans a range of positions rather than one, so
/// comparing a single index would miss it: in `C x=(A | B) | (D | E)+` the
/// repeated group starts at 0 yet also covers 1, where `x` reads.
fn can_occupy_terminal_index(
alt: &AltModel,
candidate: &ElementRef,
occurrence: usize,
) -> bool {
// `usize::MAX` is the sentinel for "no fixed index, the read falls back to
// `last()`" — not a position. Any terminal the alternative builds can be that
// last child, so every candidate can occupy it.
if occurrence == usize::MAX {
return true;
}
let Some(start) = Self::exact_terminal_index(alt, candidate) else {
// No fixed start: the candidate could be anywhere.
return true;
};
if start > occurrence {
return false;
}
// Unbounded repetition reaches every later index.
candidate
.cardinality
.max
.is_none_or(|max| start + max > occurrence)
}
/// Index among terminal children at which `element` sits on its own parse
/// path, or `None` when that index is not fixed.
fn exact_terminal_index(alt: &AltModel, element: &ElementRef) -> Option<usize> {
let position = alt
.refs
.iter()
.position(|candidate| std::ptr::eq(candidate, element))?;
// `can_coexist_with` keeps everything a ref *after* the choice coexists
// with — both branches — so it does not by itself select one path. Strip the
// tags of choices the element is inside (those branches are taken on its
// path) and leave the rest tagged, so `exact_child_count` still demands
// cross-branch agreement for choices the element is not part of.
let on_path = alt.refs[..position]
.iter()
.filter(|candidate| {
!candidate.token_types.is_empty() && candidate.can_coexist_with(element)
})
.cloned()
.map(|mut candidate| {
candidate.retain_choices(|choice| {
!element
.choice_branch
.iter()
.any(|&(taken, _)| taken == choice)
});
if candidate.choice_branch.is_empty() {
candidate.cardinality = candidate.group_local_cardinality;
}
candidate
})
.collect::<Vec<_>>();
Self::exact_child_count(on_path.iter(), false)
}
/// Total children contributed by `refs`, or `None` when that total is not the
/// same on every parse.
///
/// Refs are grouped by their enclosing choices and folded **innermost-first**:
/// once a choice's branches agree, its count is attributed to the enclosing
/// branch that contains it, so nested exhaustive choices
/// (`((a=A | b=A) | c=A)`) stay exact while nested *differing* ones
/// (`((a=A | b=B) C | D)`) correctly do not.
///
/// `restricted_to_one_path` says the caller already filtered `refs` down to a
/// single parse path. Cross-branch agreement is then meaningless — the other
/// branches were removed on purpose — so each surviving branch simply counts.
fn exact_child_count<'a>(
refs: impl Iterator<Item = &'a ElementRef>,
restricted_to_one_path: bool,
) -> Option<usize> {
let refs = refs.collect::<Vec<_>>();
let mut total = 0_usize;
let mut per_branch: BTreeMap<(usize, usize), Option<usize>> = BTreeMap::new();
// Arity is recorded, not observed: an *empty* alternative emits no ref, so
// `(a=A | )` would otherwise look like a one-branch choice.
let mut arity_of_choice: BTreeMap<usize, usize> = BTreeMap::new();
let mut depth_of_choice: BTreeMap<usize, usize> = BTreeMap::new();
let mut ancestry: BTreeMap<(usize, usize), Vec<(usize, usize)>> = BTreeMap::new();
for candidate in &refs {
for (depth, (&(choice, branch), &arity)) in candidate
.choice_branch
.iter()
.zip(&candidate.choice_arity)
.enumerate()
{
arity_of_choice.insert(choice, arity);
// A choice's depth is where it sits in the ancestry; take the
// *shallowest* sighting, since that is its real nesting level
// (a deeper ref lists it at the same index, never a lower one).
depth_of_choice
.entry(choice)
.and_modify(|existing| *existing = (*existing).min(depth))
.or_insert(depth);
ancestry.insert((choice, branch), candidate.choice_branch[..=depth].to_vec());
}
}
for candidate in &refs {
let max = candidate.cardinality.max?;
// Within its own branch a ref contributes its branch-local count; the
// `min: 0` that branch membership imposes is not optionality.
let local = candidate.branch_local_cardinality;
let exact = (local.min == max && local.max == Some(max)).then_some(max);
match candidate.choice_branch.last() {
None => total = total.saturating_add(exact?),
Some(&key) => {
let slot = per_branch.entry(key).or_insert(Some(0));
*slot = match (*slot, exact) {
(Some(sum), Some(next)) => Some(sum.saturating_add(next)),
_ => None,
};
}
}
}
// Deepest choices first, so an inner result rolls up into its parent branch.
// The list is rebuilt from `per_branch` each pass, because folding an inner
// choice *creates* an entry for its parent that must then fold in turn.
let mut processed: BTreeSet<usize> = BTreeSet::new();
// Deepest unprocessed choice still holding entries. Folding one creates an
// entry for its parent, so the candidate set is re-examined every pass.
while let Some(choice) = per_branch
.keys()
.map(|(choice, _)| *choice)
.filter(|choice| !processed.contains(choice))
.max_by_key(|choice| depth_of_choice.get(choice).copied().unwrap_or(0))
{
processed.insert(choice);
let counts = per_branch
.iter()
.filter(|((candidate, _), _)| *candidate == choice)
.map(|((_, branch), count)| (*branch, *count))
.collect::<Vec<_>>();
if counts.is_empty() {
continue;
}
let agreed = if restricted_to_one_path {
// One path survives, so there is nothing to agree with.
counts.iter().try_fold(0_usize, |sum, (_, count)| {
Some(sum.saturating_add((*count)?))
})?
} else {
let expected = arity_of_choice.get(&choice).copied()?;
let first = counts.first().and_then(|(_, count)| *count)?;
if counts.len() != expected || counts.iter().any(|(_, count)| *count != Some(first))
{
return None;
}
first
};
let parent = counts.first().and_then(|(branch, _)| {
ancestry.get(&(choice, *branch)).and_then(|chain| {
chain
.split_last()
.and_then(|(_, rest)| rest.last().copied())
})
});
for (branch, _) in &counts {
per_branch.remove(&(choice, *branch));
}
match parent {
Some(parent_key) => {
let slot = per_branch.entry(parent_key).or_insert(Some(0));
*slot = slot.map(|sum| sum.saturating_add(agreed));
}
None => total = total.saturating_add(agreed),
}
}
Some(total)
}
/// Greatest number of children `refs` can contribute on any single parse, or
/// `None` when unbounded. Sequential refs add; branches of a choice are
/// alternatives, so the widest one wins. Choices fold innermost-first so a
/// nested choice's maximum lands in its enclosing branch rather than being
/// summed alongside it.
fn widest_child_count<'a>(refs: impl Iterator<Item = &'a ElementRef>) -> Option<usize> {
let refs = refs.collect::<Vec<_>>();
let mut total = Some(0_usize);
let mut per_branch: BTreeMap<(usize, usize), Option<usize>> = BTreeMap::new();
let mut depth_of_choice: BTreeMap<usize, usize> = BTreeMap::new();
let mut ancestry: BTreeMap<(usize, usize), Vec<(usize, usize)>> = BTreeMap::new();
for candidate in &refs {
for (depth, &(choice, branch)) in candidate.choice_branch.iter().enumerate() {
depth_of_choice
.entry(choice)
.and_modify(|existing| *existing = (*existing).min(depth))
.or_insert(depth);
ancestry.insert((choice, branch), candidate.choice_branch[..=depth].to_vec());
}
}
let add = |slot: &mut Option<usize>, value: Option<usize>| {
*slot = match (*slot, value) {
(Some(total), Some(next)) => Some(total.saturating_add(next)),
_ => None,
};
};
for candidate in &refs {
match candidate.choice_branch.last() {
None => add(&mut total, candidate.cardinality.max),
Some(&key) => {
let slot = per_branch.entry(key).or_insert(Some(0));
add(slot, candidate.cardinality.max);
}
}
}
let mut processed: BTreeSet<usize> = BTreeSet::new();
while let Some(choice) = per_branch
.keys()
.map(|(choice, _)| *choice)
.filter(|choice| !processed.contains(choice))
.max_by_key(|choice| depth_of_choice.get(choice).copied().unwrap_or(0))
{
processed.insert(choice);
let counts = per_branch
.iter()
.filter(|((candidate, _), _)| *candidate == choice)
.map(|((_, branch), count)| (*branch, *count))
.collect::<Vec<_>>();
if counts.is_empty() {
continue;
}
let widest = counts
.iter()
.try_fold(0_usize, |widest, (_, count)| Some(widest.max((*count)?)));
let parent = counts.first().and_then(|(branch, _)| {
ancestry.get(&(choice, *branch)).and_then(|chain| {
chain
.split_last()
.and_then(|(_, rest)| rest.last().copied())
})
});
for (branch, _) in &counts {
per_branch.remove(&(choice, *branch));
}
match parent {
Some(parent_key) => {
let slot = per_branch.entry(parent_key).or_insert(Some(0));
add(slot, widest);
}
None => add(&mut total, widest),
}
}
total
}
/// Whether the action sits inside the branch that separates `element` from
/// `candidate` — i.e. inside the label's own branch of the choice that makes the
/// two mutually exclusive. Only then can the candidate be dismissed: the action
/// cannot run on the branch that would supply it.
///
/// Judged per choice rather than rule-wide, because an action confined to some
/// *unrelated* later choice says nothing about an earlier one.
fn action_inside_separating_branch(
element: &ElementRef,
candidate: &ElementRef,
action_branches: Option<&[(usize, usize)]>,
) -> bool {
let Some(branches) = action_branches else {
// An unscoped body runs whatever branch matched.
return false;
};
element.choice_branch.iter().any(|&(choice, branch)| {
// A choice that separates them...
candidate
.choice_branch
.iter()
.any(|&(other, other_branch)| other == choice && other_branch != branch)
// ...and whose label-side branch encloses the action.
&& branches.contains(&(choice, branch))
})
}
/// Whether two per-alternative resolutions lower to the same read, so one
/// translation can stand for both. The fields compared are exactly those
/// `translate_element_read` consumes to pick a read: list mode, block mode,
/// and the target it queries. Two block labels are equivalent regardless of
/// their token sets, because the block read ignores them.
fn same_label_read(left: &(ElementRef, usize), right: &(ElementRef, usize)) -> bool {
if left.0.is_list != right.0.is_list {
return false;
}
// Token-backed resolutions can be equivalent across source forms (`x=A` is
// token-mode, `x='a'` is block-mode) because both lower to the same
// `child_tokens(A)` query — but their occurrences are counted in different
// units: a block read indexes *every* terminal child, a token read only
// same-type children. `x='a' | A x=A` has both reporting 1 while meaning
// different children, and `A x='a' B | A x=A C` has them meaning the same
// child while reporting 1 and 1 only by coincidence.
//
// Rather than guess, mixed-mode pairs merge only when *neither* has anything
// ahead of it — occurrence zero in both systems *and* no preceding terminal
// of any type. Occurrence zero alone is not enough: in `B x=A | x='a'` the
// symbolic side reports same-token occurrence 0 while sitting at terminal
// position 1, and merging it with the literal's terminal 0 exposed `B`.
// Same-mode pairs compare directly.
if !left.0.token_types.is_empty() && left.0.token_types == right.0.token_types {
if left.0.is_block == right.0.is_block {
return left.1 == right.1;
}
// The mixed-mode merge works because both sides lower to the same
// *scalar* `nth(0)`. A list read has no such common form: token mode
// yields an iterator (`child_tokens(A)`), block mode a `String` — it
// resolves `target` against `ctx.token_types`, and a literal target
// (`xs+='a'`) is not a key there, so it falls through to the positional
// block read. Merging the two emitted `.collect()` on a `String`.
if left.0.is_list {
return false;
}
return left.1 == 0
&& right.1 == 0
&& left.0.leading_terminal
&& right.0.leading_terminal;
}
if left.0.is_block != right.0.is_block {
return false;
}
// Block reads are positional now, so two block labels agree only when
// their terminal indices do: `x=(A | B) | C x=(A | B)` puts `x` at 0 and 1.
if left.0.is_block && right.0.is_block {
return left.1 == right.1;
}
left.1 == right.1 && left.0.target == right.0.target
}
/// `action_offset` is the byte offset of a *mid-rule* action body, or `None`
/// for an `@after` / `@init` body that runs after the whole rule.
fn resolve_label_in_alt(
alt: &AltModel,
label: &str,
action_offset: Option<usize>,
) -> Option<(ElementRef, usize)> {
let declarations = alt
.refs
.iter()
.filter(|element| element.label.as_deref() == Some(label))
.collect::<Vec<_>>();
let element = *declarations.first()?;
// A renamed sibling declaration (see the isolation below) still counts as
// declaring the label: it binds it too, so it can never impersonate it.
let declares_label = |candidate: &ElementRef| {
candidate.label.as_deref().is_some_and(|name| {
name == label || name.strip_suffix(SIBLING_DECLARATION_SUFFIX) == Some(label)
})
};
// The generated read queries by rule index or *token type*, so a
// differently-spelled terminal with the same type is the same child as
// far as the read is concerned (`A : 'a';` makes `A` and `'a'` aliases).
let same_target = |candidate: &ElementRef| {
if candidate.cardinality.max == Some(0) {
return false;
}
// A token *group* has no target yet still contributes a child of the
// label's type when their sets overlap (`(xs+=A)? (A | B)`), so match
// on token types whenever both sides have them — empty target or not.
if element.token_types.is_empty() || candidate.token_types.is_empty() {
return !candidate.target.is_empty() && candidate.target == element.target;
}
candidate
.token_types
.iter()
.any(|token_type| element.token_types.contains(token_type))
};
// Whether `candidate` has been matched by the time the action body runs.
// A mid-rule action executes at its source position, so a ref that starts
// after it is still in the future and cannot affect the read; a ref in a
// branch the action does not sit inside cannot have run either. An
// `@after` body runs after everything, so every ref counts.
// The choice ancestry the action itself sits in, derived from spans: the
// action belongs to the innermost branch whose refs bracket its offset.
// Refs from any *other* branch of those choices cannot have run.
// The choice branches that syntactically *enclose* the action. A branch
// encloses it only when the branch has a ref before the action AND no
// sibling branch of the same choice has a ref after it — a sibling ref
// afterwards means the choice is still open, i.e. the action follows the
// whole group rather than sitting inside one branch. Nearest-preceding-ref
// alone gets `(A | xs+=A) {…}` wrong, marking the action as confined to the
// final branch when it actually runs for either.
// The choice branches that syntactically enclose the action: those whose
// *block* span contains the action's offset. Ref spans alone cannot decide
// this — `(A | xs+=A) {…}` and `(A x=A {…} | B)` both put branch refs on
// either side of the action — but the block's own extent can: in the first
// the action sits after the closing paren, in the second inside it.
let action_branches = action_offset.map(|offset| {
// For each enclosing choice, the *one* branch the action sits in: the
// branch whose own refs span the offset. Refs of an earlier sibling
// also precede the action and share the choice's block span, so
// collecting every preceding ref's tags would record mutually
// conflicting branches (`(B | C x=A? A {…})` would claim both).
//
// A branch contains the action when some ref of it starts before the
// offset and no ref of a *later* sibling does — source order means a
// later branch having started implies the action is past this one.
let mut chosen: Vec<(usize, usize)> = Vec::new();
// Every (choice, branch) whose *branch text* contains the action. This
// reads the branch's own span rather than inferring from ref positions,
// so a branch holding only an action or predicate — which emits no
// `ElementRef` — is still identified (`x=A? (A | {$x.text})`).
for candidate in &alt.refs {
for ((&key, &(branch_start, branch_end)), &(choice_start, choice_end)) in candidate
.choice_branch
.iter()
.zip(&candidate.branch_spans)
.zip(&candidate.choice_spans)
{
let inside_choice = choice_start <= offset && offset < choice_end;
let inside_branch = branch_start <= offset && offset < branch_end;
if inside_choice && inside_branch && !chosen.contains(&key) {
chosen.push(key);
}
}
}
// A choice enclosing the action but with no branch claiming it means the
// action sits in a ref-free branch: nothing of that branch has matched,
// so record it as its own branch so siblings are excluded.
for candidate in &alt.refs {
for (&(choice, _), &(choice_start, choice_end)) in
candidate.choice_branch.iter().zip(&candidate.choice_spans)
{
if choice_start <= offset
&& offset < choice_end
&& !chosen
.iter()
.any(|&(chosen_choice, _)| chosen_choice == choice)
{
// usize::MAX marks "a branch with no refs of its own".
chosen.push((choice, usize::MAX));
}
}
}
chosen
});
let branch_confined = action_branches
.as_ref()
.is_some_and(|branches| !branches.is_empty());
// A ref inside a group that also encloses the action has run: the action
// only executes when that group was taken. Its cardinality still reports
// `min: 0` from the group's `?`, so use the quantifier-free figure —
// `(A x=A {…})?` has exactly one `A` before the label whenever the action
// runs at all.
//
// *Every* group the ref sits in must enclose the action, not merely one: an
// inner group that closed before the action proves nothing, so
// `((q)? x=q {…})?` must not treat the inner `(q)?` as matched.
// A ref is exactly-once on the action's path when every group that *relaxed*
// its lower bound is one the action also sits inside — the action running
// proves those groups were taken. Groups that impose nothing (a mandatory
// `(…)`) are irrelevant whether or not they enclose the action, so requiring
// all of them to would reject `((q) x=q {…})?`, where the inner group is
// mandatory and already closed.
let on_taken_group = |candidate: &ElementRef| {
action_offset.is_some_and(|offset| {
let encloses = |group: &GroupSpan| group.start <= offset && offset < group.end;
// A *repeated* group that has closed still contributes an unknown
// number of children, so knowing it ran does not fix the count:
// `((A B)+ x=A {…})?` has a variable run of `A` before the label.
if candidate
.group_spans
.iter()
.any(|group| group.repeated && !encloses(group))
{
return false;
}
let relaxing = candidate
.group_spans
.iter()
.filter(|group| group.optional)
.collect::<Vec<_>>();
!relaxing.is_empty() && relaxing.iter().all(|group| encloses(group))
})
};
// Whether a ref can have run before the action, given that ancestry. An
// action after the whole choice (`(x=A | A) {$x}`) has no branch tag, so
// every branch counts; one written inside a branch excludes its siblings —
// including when the label itself is in another branch (`(e | xs+=e {…})`).
let on_action_path = |candidate: &ElementRef| {
action_branches.as_ref().is_none_or(|branches| {
!candidate.choice_branch.iter().any(|(choice, branch)| {
branches
.iter()
.any(|(a_choice, a_branch)| choice == a_choice && branch != a_branch)
})
})
};
let matched_at_action = |candidate: &ElementRef| {
action_offset.is_none_or(|offset| {
let started = candidate.span.is_none_or(|(start, _)| start < offset);
started && on_action_path(candidate)
})
};
// Two declarations can share one read when the generated query is the
// same. For token-backed refs that is the token type, not the source form:
// `x=A` and `x='a'` differ in spelling and block-ness yet query alike.
let same_read_as_element = |candidate: &ElementRef| {
candidate.is_list == element.is_list
&& if candidate.token_types.is_empty() || element.token_types.is_empty() {
candidate.target == element.target && candidate.is_block == element.is_block
} else {
candidate.token_types == element.token_types
}
};
if element.is_list {
// `translate_element_read` lowers a list label to a per-target child
// iterator, which needs a rule or token *target*. A list over a token
// group (`xs+=(A | B)`) has none, so the read would fall through to
// the scalar block path and emit `.last()…collect()` — code that does
// not compile. Leave it unresolved instead.
if element.target.is_empty() {
return None;
}
// A list read yields every child of *one* query, so repeated declarations
// are the normal idiom (`xs+=e (op xs+=e)+`) only while they all name that
// same query. `xs+=A xs+=B` would iterate `A` alone and drop every `B`.
// The query is the token type for token-backed refs, not the spelling:
// `xs+=A B | xs+='a' C` binds one type through two source forms.
let same_query = |candidate: &ElementRef| {
if element.token_types.is_empty() || candidate.token_types.is_empty() {
candidate.target == element.target
} else {
candidate.token_types == element.token_types
}
};
if declarations
.iter()
.any(|candidate| !same_query(candidate) || !candidate.is_list)
{
return None;
}
// What the read cannot express is exclusion, so the label resolves
// only when no *already-matched* same-target element sits outside it.
// A trailing `A` in `r : xs+=A {$xs} A;` has not been matched when the
// action runs, so it cannot pollute the iterator.
let exclusive = alt.refs.iter().all(|candidate| {
declares_label(candidate)
|| !same_target(candidate)
|| !matched_at_action(candidate)
});
return exclusive.then(|| (element.clone(), 0));
}
// Only declarations the action can actually observe constrain its read. Two
// rule it out: one in a sibling branch, which never runs alongside a
// branch-confined action (`(x=A {$x} | x=A+ B)`), and one *after* the action,
// which has not assigned the label yet (`x=A {$x} x=A` reads the first
// assignment unambiguously).
let relevant = declarations
.iter()
.copied()
.filter(|candidate| {
(!branch_confined || on_action_path(candidate)) && matched_at_action(candidate)
})
.collect::<Vec<_>>();
let declarations = if relevant.is_empty() {
declarations
} else {
relevant
};
let element = *declarations.first()?;
// A single label read is one positional lookup. Several declarations can
// still share it when each lowers to the same query — mutually exclusive
// branches holding `x=A` at the same occurrence do. What cannot be served
// is declarations that query differently (`(x=A | x=B)`), or that could
// both be present and so want different positions.
if declarations.iter().any(|candidate| {
!same_read_as_element(candidate)
|| (!std::ptr::eq(*candidate, element) && candidate.can_coexist_with(element))
}) {
return None;
}
// Deriving the read from the *first* declaration and probing the others
// property-by-property kept missing a dimension — occurrence and repetition
// among them. Instead resolve each declaration on its own and require the
// results to agree, so one read demonstrably serves every branch:
// `(A x=A B | x=A C)` wants occurrence 1 then 0, and `(x=A B | x=A+ C)`
// wants a first-match read then a last-match one.
if declarations.len() > 1 {
let mut resolutions = Vec::with_capacity(declarations.len());
for candidate in &declarations {
let position = alt
.refs
.iter()
.position(|other| std::ptr::eq(other, *candidate))?;
let mut alone = alt.clone();
// Isolate this declaration by *renaming* the others rather than
// clearing their labels. Clearing would reclassify a fellow
// declaration as an unlabeled impostor and trip the shadow check —
// `(x=A | x=A)` would reject itself. Renaming keeps them labeled, so
// `declares_label` still exempts them, while only one answers to
// `label`.
let shadow_name = format!("{label}{SIBLING_DECLARATION_SUFFIX}");
for (index, ref_at) in alone.refs.iter_mut().enumerate() {
if index != position && ref_at.label.as_deref() == Some(label) {
ref_at.label = Some(shadow_name.clone());
}
}
resolutions.push(Self::resolve_label_in_alt(&alone, label, action_offset)?);
}
let first = resolutions.first()?.clone();
if resolutions
.iter()
.any(|resolution| !Self::same_label_read(resolution, &first))
{
return None;
}
return Some(first);
}
// `element` is borrowed from `alt.refs`, so identity holds — but compare by
// value as a fallback, because the sibling-isolation rename above clones the
// alternative and a filtered `declarations` list can outlive that borrow.
let position = alt
.refs
.iter()
.position(|candidate| std::ptr::eq(candidate, element))
.or_else(|| alt.refs.iter().position(|candidate| candidate == element))?;
let (before, after) = (&alt.refs[..position], &alt.refs[position + 1..]);
// `translate_element_read` routes on `is_block`, which covers labeled
// groups and *literal* terminals alike (`x='b'`), so the occurrence has to
// be computed the same way for both — keying on an empty target here would
// leave a literal label counting same-target children while its read walks
// every terminal.
if element.is_block {
// A *repeated* block label (`x=(A | B)+`) is overwritten each iteration,
// so ANTLR exposes the last match while a positional read pins the first.
// The non-block path already declines this; do the same rather than read
// the wrong iteration.
if element.cardinality.is_repeated() {
return None;
}
// A block label has no single target to query, so its read walks the
// context's terminal children by position. The index is the number of
// terminals matched ahead of the block on this parse path — every
// terminal counts, not just ones sharing the block's token set, since
// each is a distinct child of the same context.
// `on_action_path` already narrowed these to one parse path (when the
// action is inside a branch), so branches that survive simply count.
// Confinement to one *outer* branch does not restrict a nested choice
// inside it: `((a=A | b=B) x=(C | D) {…} | E)` still has the `A`/`B`
// branches to reconcile, and calling the count path-restricted summed
// them. Strip the tags of choices the action is genuinely inside, then
// let any surviving tag force cross-branch agreement.
let counted = before
.iter()
.filter(|candidate| {
!candidate.token_types.is_empty()
&& on_action_path(candidate)
// Only children already matched when the action runs affect
// its read. For a *forward* label (`A {$x.text} B? x=(C|D)`)
// the prefix is entirely in the future, so counting `B?`
// made the index inexact and fell back to `last()` — which
// returns the already-matched `A`.
&& matched_at_action(candidate)
// A ref in a branch this label cannot reach never precedes it:
// in `(x=A | x='a')` the sibling declaration is not a prefix
// terminal of the literal's path.
&& candidate.can_coexist_with(element)
})
.cloned()
.map(|mut candidate| {
if let Some(branches) = action_branches.as_ref() {
candidate.retain_choices(|choice| {
!branches.iter().any(|&(taken, _)| taken == choice)
});
}
candidate
})
.collect::<Vec<_>>();
let restricted = counted
.iter()
.all(|candidate| candidate.choice_branch.is_empty());
let terminals_before = Self::exact_child_count(counted.iter(), restricted);
// Without a fixed index the read falls back to the most recent
// terminal, which is only right when nothing has been matched since.
// A sibling branch that puts a terminal at the same index supplies the
// child this read selects on a parse where the label is unset:
// `((x=(A | B)) | C) {$x}` reads `C` on the `C` branch.
if let Some(index) = terminals_before {
// A sibling branch's terminal can only be mistaken for the label
// when the read actually runs on that branch. An action confined to
// the label's own branch never executes there, so the sibling is
// irrelevant — `(x=(A | B) {…} | C)` is safe even though `C` sits at
// the same index.
let sibling_at_index = !branch_confined
&& alt.refs.iter().any(|candidate| {
// Another declaration of the same label binds it too, so it
// can never impersonate it — `(x=A | x='a')` is one label
// over two branches, not a label and an impostor.
!declares_label(candidate)
&& !candidate.can_coexist_with(element)
&& !candidate.token_types.is_empty()
&& candidate.cardinality.max != Some(0)
&& Self::can_occupy_terminal_index(alt, candidate, index)
});
if sibling_at_index {
return None;
}
// ROOT J: a fixed index is not enough when the label is *optional* —
// `x=(A | B)? C {…}` puts `C` at index 0 whenever the block is
// absent, so the read would report it as the label's token.
let optional_here = if on_taken_group(element) {
element.group_local_cardinality.min == 0
} else {
element.cardinality.min == 0
};
if optional_here
&& after.iter().any(|candidate| {
!candidate.token_types.is_empty()
&& candidate.cardinality.max != Some(0)
&& matched_at_action(candidate)
})
{
return None;
}
}
return terminals_before.map_or_else(
|| {
let displaced = after.iter().any(|candidate| {
!candidate.token_types.is_empty()
&& candidate.cardinality.max != Some(0)
&& matched_at_action(candidate)
});
(!displaced).then(|| (element.clone(), usize::MAX))
},
|index| Some((element.clone(), index)),
);
}
// A repeated single label (`(x=A)+`) is overwritten on every iteration,
// so ANTLR exposes the *latest* match. The read here is a fixed
// `nth(i)`, which would pin the first one; only the accessor path can
// express `.last()`. Leave it unresolved rather than read the wrong
// iteration.
if element.cardinality.is_repeated() {
return None;
}
// Single label: count the children ahead of it that the read would also
// select, bailing as soon as one contributes an unfixed number. A token
// *group* has no target yet still produces a child of the label's token
// type when their sets overlap (`(A | B) x=A`), so it must be counted —
// and since only some of its members match, its contribution is not
// exact and the label declines.
let counts_toward_occurrence = |candidate: &ElementRef| {
if element.token_types.is_empty() || candidate.token_types.is_empty() {
return candidate.target == element.target;
}
candidate
.token_types
.iter()
.any(|token_type| element.token_types.contains(token_type))
};
// A token *group* only sometimes yields a matching child, so its count is
// exact only when every member is one the read selects.
if before.iter().any(|candidate| {
counts_toward_occurrence(candidate)
&& !candidate.token_types.is_empty()
&& !candidate
.token_types
.iter()
.all(|token_type| element.token_types.contains(token_type))
}) {
return None;
}
// Count only children on the label's own parse path, and — when the action
// is confined to a branch — count the surviving branch as that path rather
// than demanding agreement from branches already filtered out.
let counted = before
.iter()
.filter(|candidate| {
// Only children already matched when the action runs can affect its
// read. An inline action *before* the label sees none of them, so a
// later unbounded run must not poison the count:
// `r : {$x.text} A* x=A EOF;` reads an empty list, whatever follows.
counts_toward_occurrence(candidate)
&& matched_at_action(candidate)
&& candidate.can_coexist_with(element)
})
.cloned()
.map(|mut candidate| {
if on_taken_group(&candidate) {
// The enclosing group is taken on the action's path, so the
// group's own quantifier no longer relaxes this ref.
candidate.cardinality = candidate.group_local_cardinality;
candidate.branch_local_cardinality = candidate.group_local_cardinality;
}
// Choices the *label* is inside are settled on its path, so those
// tags carry no remaining alternation. Tags that survive belong to
// choices the label sits outside of, whose branches are genuinely
// alternative.
candidate.retain_choices(|choice| {
!element
.choice_branch
.iter()
.any(|&(taken, _)| taken == choice)
});
candidate
})
.collect::<Vec<_>>();
// `can_coexist_with` keeps every branch of a choice the label is outside of,
// so it does not by itself select one path: `(A B | A C) x=A` would sum both
// prefixes. Only claim path-restriction once no alternation remains.
let restricted = counted
.iter()
.all(|candidate| candidate.choice_branch.is_empty());
let occurrence = Self::exact_child_count(counted.iter(), restricted)?;
// An optional label is displaced by a following same-target child that can
// slide into its position, whether that child is mandatory (`x=A? A`) or
// optional (`(pred x=A)? A?` — the follower may consume the only token).
// Two kinds cannot: a ref from a sibling branch of the same choice, since
// no parse contains both, and — for a mid-rule action — a ref that starts
// after the action and so has not been matched when the read runs.
// `matched_at_action` already folds in coexistence when the action is
// confined to the label's branch; applying it again here would also
// exclude siblings for an action that runs after the whole choice.
// Another *declaration* of the same label is not a shadow — it binds the
// label too, and the guard above already proved they share one read.
// The label's own `min: 0` may come from a group the action shares, in which
// case it is *not* optional relative to the action: `(A x=A {…})?` only runs
// the action when the group matched, so `x` is bound.
let element_optional_here = if on_taken_group(element) {
element.group_local_cardinality.min == 0
} else {
element.cardinality.min == 0
};
// A sibling branch's child cannot slide into the label's slot *within a
// parse that bound the label* — the two never coexist. It is still a hazard
// when the read may run with the label unset, which is when the action is
// not inside the branch that separates them. That has to be judged per
// *choice*: a rule-wide flag would let an action confined to some unrelated
// later choice exempt an earlier sibling
// (`({false}? x=A | A) (B {$x} | C)`).
let excluded_by_confinement = |candidate: &ElementRef| {
!candidate.can_coexist_with(element)
&& Self::action_inside_separating_branch(
element,
candidate,
action_branches.as_deref(),
)
};
let shadowed_when_absent = element_optional_here
&& (after.iter().any(|candidate| {
!declares_label(candidate)
&& same_target(candidate)
&& matched_at_action(candidate)
&& !excluded_by_confinement(candidate)
})
// A same-target sibling *before* the label impersonates it just as well:
// in `(A | x=A) {$x}` the unlabeled `A` is the only child of its type on
// its own branch, so `child_tokens(A).nth(0)` reports it. Only
// non-coexisting refs matter here — a ref the label coexists with is a
// genuine prefix and is already folded into `occurrence`.
|| before.iter().any(|candidate| {
!declares_label(candidate)
&& same_target(candidate)
&& matched_at_action(candidate)
&& !candidate.can_coexist_with(element)
&& !excluded_by_confinement(candidate)
}));
(!shadowed_when_absent).then_some((element.clone(), occurrence))
}
}
/// Generated attrs struct name for a rule.
pub(crate) fn attrs_struct_name(rule_index: usize) -> String {
format!("__RuleAttrs{rule_index}")
}
/// Translates every `$…` reference in an embedded body to Rust.
pub(crate) fn translate_body(body: &str, ctx: &TranslationCtx<'_>) -> io::Result<String> {
let mut out = String::with_capacity(body.len());
let mut rest = body;
while let Some(dollar) = find_dollar(rest) {
out.push_str(&rest[..dollar]);
let after = &rest[dollar + 1..];
let name_len = after
.find(|ch: char| ch != '_' && !ch.is_ascii_alphanumeric())
.unwrap_or(after.len());
if name_len == 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("stray $ in embedded action: {body}"),
));
}
let name = &after[..name_len];
// Optional `.suffix`.
let mut consumed = name_len;
let mut suffix: Option<&str> = None;
if after[name_len..].starts_with('.') {
let suffix_text = &after[name_len + 1..];
let suffix_len = suffix_text
.find(|ch: char| ch != '_' && !ch.is_ascii_alphanumeric())
.unwrap_or(suffix_text.len());
if suffix_len > 0 {
// Only treat it as an attribute suffix when it is not a
// method call — `$ctx.to_string_tree(...)` keeps its call.
let after_suffix = suffix_text[suffix_len..].trim_start();
let is_call = after_suffix.starts_with('(');
if !is_call {
suffix = Some(&suffix_text[..suffix_len]);
consumed = name_len + 1 + suffix_len;
} else if name == "ctx"
&& (suffix_text[..suffix_len].ends_with("_children")
|| suffix_text[..suffix_len].ends_with("_all"))
&& after_suffix.starts_with("()")
{
// `$ctx.<rule>_children()` (or the legacy `_all()` form) is
// an active-context collection read. Consume the empty
// parens along with the suffix.
suffix = Some(&suffix_text[..suffix_len]);
let call_end = suffix_text[suffix_len..]
.find(')')
.map_or(suffix_len, |close| suffix_len + close + 1);
consumed = name_len + 1 + call_end;
}
}
}
let translated = translate_reference(name, suffix, ctx, body)?;
out.push_str(&translated);
rest = &rest[dollar + 1 + consumed..];
}
out.push_str(rest);
Ok(out)
}
/// Finds the next `$` that is outside a string literal.
fn find_dollar(text: &str) -> Option<usize> {
let mut quoted = false;
let mut escaped = false;
for (index, ch) in text.char_indices() {
if escaped {
escaped = false;
continue;
}
match ch {
'\\' if quoted => escaped = true,
'"' => quoted = !quoted,
'$' if !quoted => return Some(index),
_ => {}
}
}
None
}
fn translate_reference(
name: &str,
suffix: Option<&str>,
ctx: &TranslationCtx<'_>,
body: &str,
) -> io::Result<String> {
// Special names first.
match (name, suffix) {
("ctx", None) => return Ok("(&__ctx)".to_owned()),
("ctx", Some(member)) => return translate_ctx_member(member, ctx, body),
("text", None) => return Ok(text_expression(ctx)),
("_p", None) => return Ok("__precedence".to_owned()),
("parser", None) => return Ok("self".to_owned()),
("start", None) => {
return Ok("__ctx.start(self.base.token_store())".to_owned());
}
_ => {}
}
let rule = ctx.rule();
// Labels shadow attrs; attrs shadow rule/token names.
if let Some((element, occurrence)) = ctx.resolve_label(name) {
return translate_element_read(&element, occurrence, suffix, ctx, body);
}
if rule.attr(name).is_some() {
let mut expr = format!("__attrs.{}", escape_keyword(name));
if let Some(suffix) = suffix {
let _ = write!(expr, ".{suffix}");
}
return Ok(expr);
}
if let Some(target_rule) = ctx.rule_index_by_name(name) {
let element = ElementRef {
label: None,
target: name.to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality::ONE,
stable_accessor: false,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let _ = target_rule;
return translate_element_read(&element, usize::MAX, suffix, ctx, body);
}
if ctx.token_types.contains_key(name) {
let element = ElementRef {
label: None,
target: name.to_owned(),
token_types: vec![ctx.token_types[name]],
is_block: false,
is_list: false,
cardinality: ChildCardinality::ONE,
stable_accessor: false,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
return translate_element_read(&element, usize::MAX, suffix, ctx, body);
}
Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("cannot translate ${name} in embedded action: {body}"),
))
}
/// `$text` — text matched so far for the current rule.
fn text_expression(ctx: &TranslationCtx<'_>) -> String {
match ctx.site {
ActionSite::Body => {
"self.base.text_interval(action.start_index(), action.stop_index())".to_owned()
}
ActionSite::After | ActionSite::Init => {
"{ let __stop = self.base.rule_stop_token_index(antlr4_runtime::IntStream::index(self.base.input()), __consumed_eof); self.base.text_interval(__rule_start, __stop) }"
.to_owned()
}
}
}
/// `$ctx.member` — a labeled element read (`$ctx.r`) or a generated child
/// iterator (`$ctx.elseIfStatement_children()`).
fn translate_ctx_member(member: &str, ctx: &TranslationCtx<'_>, body: &str) -> io::Result<String> {
if let Some((element, occurrence)) = ctx.resolve_label(member) {
// `$ctx.r` denotes the labeled child's subtree (Java field of the
// context); translate like `$r.ctx`.
return translate_element_read(&element, occurrence, Some("ctx"), ctx, body);
}
if let Some(rule_name) = member.strip_suffix("_children") {
if let Some(rule_index) = ctx.rule_index_by_name(rule_name) {
return Ok(format!(
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), {rule_index})"
));
}
}
if let Some(rule_name) = member.strip_suffix("_all") {
if let Some(rule_index) = ctx.rule_index_by_name(rule_name) {
return Ok(format!(
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), {rule_index}).collect::<Vec<_>>()"
));
}
}
if ctx.rule().attr(member).is_some() {
return Ok(format!("__attrs.{}", escape_keyword(member)));
}
Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("cannot translate $ctx.{member} in embedded action: {body}"),
))
}
/// Reads a rule/token element reference with an optional attribute suffix.
///
/// `occurrence == usize::MAX` means "implicit reference": ANTLR resolves
/// `$e` to the most recent `e` match, i.e. the LAST matching child so far.
fn translate_element_read(
element: &ElementRef,
occurrence: usize,
suffix: Option<&str>,
ctx: &TranslationCtx<'_>,
body: &str,
) -> io::Result<String> {
if element.is_list {
// `label+=x`: expose the matching children as a lazy Rust iterator.
if let Some(rule_index) = ctx.rule_index_by_name(&element.target) {
return match suffix {
None | Some("ctx") => Ok(format!(
"__ctx.child_rule_trees(self.base.parse_tree_storage(), self.base.token_store(), {rule_index})"
)),
Some(other) => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("unsupported list-label read .{other} in embedded action: {body}"),
)),
};
}
if let Some(token_type) = ctx.token_types.get(&element.target) {
return Ok(format!(
"__ctx.child_tokens(self.base.parse_tree_storage(), self.base.token_store(), {token_type})"
));
}
// A list label whose target names neither a rule nor a token type has no
// iterator form: the block read below picks *one* terminal and renders it as
// a `String`, so falling through emitted `.collect()` on a `String` for
// `xs+='a'` (a literal is not a `token_types` key). Decline instead of
// generating code that does not compile.
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"cannot translate list label ${} in embedded action: {body}",
element.label.as_deref().unwrap_or_default()
),
));
}
if element.is_block {
// A labeled `(...)` block over tokens: `$myset.stop` / `$myset.text` read
// the token the block matched. A bare `$myset` read denotes the Token
// object itself (Java prints `Token.toString()`), the same rendering as
// start/stop.
//
// The block has no target to query, so the read walks the context's
// terminal children and picks by *position*. It uses the *labeled* iterator,
// which skips deleted-token errors while keeping inserted missing ones —
// a grammar-derived index knows nothing about recovery, and a deleted token
// would otherwise shift every later position (see #235 for the same rule on
// token accessors): `occurrence` is the number of
// terminals matched ahead of the block on this parse path. `usize::MAX`
// means the position is not fixed, in which case the most recent terminal
// is the best available answer — the historical behaviour.
let pick = if occurrence == usize::MAX {
"last()".to_owned()
} else {
format!("nth({occurrence})")
};
return match suffix {
None | Some("stop" | "start") => Ok(format!(
"__ctx.labeled_terminal_children(self.base.parse_tree_storage(), self.base.token_store()).{pick}.map(|__t| __t.symbol().to_string()).unwrap_or_default()"
)),
Some("text") => Ok(format!(
"__ctx.labeled_terminal_children(self.base.parse_tree_storage(), self.base.token_store()).{pick}.map(|__t| __t.text().to_owned()).unwrap_or_default()"
)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("unsupported block-label read in embedded action: {body}"),
)),
};
}
if let Some(rule_index) = ctx.rule_index_by_name(&element.target) {
let pick = if occurrence == usize::MAX {
"last()".to_owned()
} else {
format!("nth({occurrence})")
};
return match suffix {
Some("ctx") | None => Ok(format!(
"__ctx.child_rule_trees(self.base.parse_tree_storage(), self.base.token_store(), {rule_index}).{pick}.expect(\"labeled rule child\")"
)),
Some("text") => Ok(format!(
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), {rule_index}).{pick}.map(|__c| __c.text()).unwrap_or_default()"
)),
Some("start") => Ok(format!(
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), {rule_index}).{pick}.and_then(|__c| __c.start()).map(|__t| __t.to_string()).unwrap_or_default()"
)),
Some("stop") => Ok(format!(
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), {rule_index}).{pick}.and_then(|__c| __c.stop()).map(|__t| __t.to_string()).unwrap_or_default()"
)),
Some(attr) => {
let target_rule = &ctx.model.rules[rule_index];
let Some(decl) = target_rule.attr(attr) else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"rule {} has no attribute {attr} (embedded action: {body})",
element.target
),
));
};
let attrs_struct = attrs_struct_name(rule_index);
let field = escape_keyword(&decl.name);
Ok(format!(
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), {rule_index}).{pick}.and_then(|__c| __c.generated_attrs::<{attrs_struct}>()).map(|__a| __a.{field}.clone()).unwrap_or_default()"
))
}
};
}
if let Some(token_type) = ctx.token_types.get(&element.target) {
let pick = if occurrence == usize::MAX {
"last()".to_owned()
} else {
format!("nth({occurrence})")
};
return match suffix {
Some("text") => Ok(format!(
"__ctx.child_tokens(self.base.parse_tree_storage(), self.base.token_store(), {token_type}).{pick}.map(|__t| __t.text().to_owned()).unwrap_or_default()"
)),
Some("int") => Ok(format!(
"__ctx.child_tokens(self.base.parse_tree_storage(), self.base.token_store(), {token_type}).{pick}.map(|__t| __t.text().parse::<i32>().unwrap_or_default()).unwrap_or_default()"
)),
Some("line") => Ok(format!(
"__ctx.child_tokens(self.base.parse_tree_storage(), self.base.token_store(), {token_type}).{pick}.map(|__t| __t.symbol().line()).unwrap_or_default()"
)),
None | Some("stop" | "start") => Ok(format!(
"__ctx.child_tokens(self.base.parse_tree_storage(), self.base.token_store(), {token_type}).{pick}.map(|__t| __t.symbol().to_string()).unwrap_or_default()"
)),
Some(other) => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"unsupported token attribute .{other} on ${} (embedded action: {body})",
element.target
),
)),
};
}
Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"cannot resolve element ${} in embedded action: {body}",
element.target
),
))
}
/// Escapes attribute names that collide with Rust keywords (`$return`).
pub(crate) fn escape_keyword(name: &str) -> String {
match name {
"return" | "type" | "match" | "loop" | "move" | "ref" | "self" | "super" | "box"
| "const" | "continue" | "crate" | "else" | "enum" | "extern" | "fn" | "for" | "if"
| "impl" | "in" | "let" | "mod" | "mut" | "pub" | "static" | "struct" | "trait"
| "unsafe" | "use" | "where" | "while" => format!("r#{name}"),
_ => name.to_owned(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::grammar::{ScopeDecl, parse_scope_decls};
fn model(rules: Vec<RuleModel>) -> EmbeddedModel {
EmbeddedModel {
rules,
parser_members: MembersModel::default(),
}
}
fn rule(name: &str) -> RuleModel {
RuleModel {
name: name.to_owned(),
..RuleModel::default()
}
}
fn tokens(pairs: &[(&str, i32)]) -> BTreeMap<String, i32> {
pairs
.iter()
.map(|(name, ty)| ((*name).to_owned(), *ty))
.collect()
}
#[test]
fn maps_attribute_types_for_generated_rust() {
assert_eq!(map_attr_type("int"), "i32");
assert_eq!(map_attr_type("Integer"), "i32");
assert_eq!(map_attr_type("boolean"), "bool");
assert_eq!(map_attr_type("List<Integer>"), "Vec<i32>");
assert_eq!(map_attr_type("List < List<Integer> >"), "Vec<Vec<i32>>");
assert_eq!(map_attr_type("std::string::String"), "std::string::String");
}
mod upstream_scope_parsing {
use super::*;
const CASES: &[(&str, &str)] = &[
("", ""),
(" ", ""),
("int i", "i:int"),
("int[] i, int j[]", "i:int[], j:int []"),
("Map<A,B>[] i, int j[]", "i:Map<A,B>[], j:int []"),
("Map<A,List<B>>[] i", "i:Map<A,List<B>>[]"),
(
"int i = 34+a[3], int j[] = new int[34]",
"i:int=34+a[3], j:int []=new int[34]",
),
("char *[3] foo = {1,2,3}", "foo:char *[3]={1,2,3}"),
("String[] headers", "headers:String[]"),
("std::vector<std::string> x", "x:std::vector<std::string>"),
("i", "i"),
("i,j", "i, j"),
("i\t,j, k", "i, j, k"),
("x: int", "x:int"),
("x :int", "x:int"),
("x:int", "x:int"),
("x:int=3", "x:int=3"),
(
"r:Rectangle=Rectangle(fromLength: 6, fromBreadth: 12)",
"r:Rectangle=Rectangle(fromLength: 6, fromBreadth: 12)",
),
("p:pointer to int", "p:pointer to int"),
("a: array[3] of int", "a:array[3] of int"),
("a \t:\tfunc(array[3] of int)", "a:func(array[3] of int)"),
("x:int, y:float", "x:int, y:float"),
(
"x:T?, f:func(array[3] of int), y:int",
"x:T?, f:func(array[3] of int), y:int",
),
("float64 x = 3", "x:float64=3"),
("map[string]int x", "x:map[string]int"),
];
#[test]
fn argument_declarations_match_java() {
for &(input, expected) in CASES {
let actual = parse_scope_decls(input)
.into_iter()
.map(render)
.collect::<Vec<_>>()
.join(", ");
assert_eq!(actual, expected, "input {input:?}");
}
}
fn render(declaration: ScopeDecl) -> String {
let ty = declaration
.ty
.map_or_else(String::new, |ty| format!(":{ty}"));
let initializer = declaration
.initializer
.map_or_else(String::new, |initializer| format!("={initializer}"));
format!("{}{ty}{initializer}", declaration.name)
}
}
#[test]
fn translates_attr_and_rule_reads() {
let mut expression = rule("e");
expression.attrs.push(AttrDecl {
name: "v".to_owned(),
ty: "i32".to_owned(),
});
let m = model(vec![rule("s"), expression]);
let toks = tokens(&[("INT", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 1,
body_offset: None,
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$v = $INT.int;", &ctx).expect("translates");
assert!(translated.starts_with("__attrs.v = "), "{translated}");
assert!(
translated.contains(
"child_tokens(self.base.parse_tree_storage(), self.base.token_store(), 1)"
),
"{translated}"
);
let parent_ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::Body,
token_types: &toks,
};
let read = translate_body("writeln!(self.output(), \"{}\", $e.v);", &parent_ctx)
.expect("translates");
assert!(read.contains("generated_attrs::<__RuleAttrs1>"), "{read}");
}
#[test]
fn resolves_structural_labels_within_the_owning_alternative() {
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![
ElementRef {
label: Some("left".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality::ONE,
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
ElementRef {
label: Some("right".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality::ONE,
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
],
children: BTreeMap::from([(
"e".to_owned(),
ChildCardinality {
min: 2,
max: Some(2),
},
)]),
leading_target: Some("e".to_owned()),
});
let mut expression = rule("e");
expression.attrs.push(AttrDecl {
name: "v".to_owned(),
ty: "i32".to_owned(),
});
let m = model(vec![statement, expression]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$right.v", &ctx).expect("translates");
assert!(translated.contains(".nth(1)"), "{translated}");
assert!(
translated.contains("generated_attrs::<__RuleAttrs1>"),
"{translated}"
);
}
/// A label preceded by a same-target ref from a *sibling* choice branch has
/// no fixed CST position: `r : (e | x=e) {$x...}` builds one `e` child, so
/// counting the flattened refs would emit `nth(1)` and silently read an
/// element the parse never produced. Such a label must stay unresolved and
/// surface as a translation error.
#[test]
fn inexact_preceding_refs_leave_labels_unresolved_instead_of_misindexing() {
let mut statement = rule("s");
// `r : (e | x=e) {…}`: the two refs are *sequential* here, not branches of
// one choice — an unlabeled `e` genuinely precedes the label on the same
// path, which is what leaves its position unfixed. (The mutually exclusive
// spelling is covered by `sibling_branch_children_do_not_shadow_an_optional_label`.)
let branch_ref = |label: Option<&str>| ElementRef {
label: label.map(ToOwned::to_owned),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
// Optional on its own path, so the count ahead of the label floats.
branch_local_cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
group_local_cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
};
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![branch_ref(None), branch_ref(Some("x"))],
children: BTreeMap::from([(
"e".to_owned(),
ChildCardinality {
min: 1,
max: Some(1),
},
)]),
leading_target: Some("e".to_owned()),
});
let m = model(vec![statement, rule("e")]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$x.text", &ctx).expect_err("must not translate");
assert_eq!(error.kind(), io::ErrorKind::InvalidData);
assert!(error.to_string().contains("cannot translate $x"), "{error}");
}
/// An *optional* label with a following same-target child has no fixed
/// position either: in `r : ({false}? x=A)? A {$x...}` the mandatory `A`
/// slides into `nth(0)` whenever the optional group is skipped, so the
/// action would receive a value for an unset label.
#[test]
fn optional_labels_shadowed_by_a_following_child_stay_unresolved() {
let mut statement = rule("s");
let token_ref = |label: Option<&str>, min| ElementRef {
label: label.map(ToOwned::to_owned),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
cardinality: ChildCardinality { min, max: Some(1) },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
statement.alts.push(AltModel {
label: None,
span: (10, 20),
// `x=A?` then a mandatory `A`.
refs: vec![token_ref(Some("x"), 0), token_ref(None, 1)],
children: BTreeMap::new(),
leading_target: Some("A".to_owned()),
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$x.text", &ctx).expect_err("must not translate");
assert!(error.to_string().contains("cannot translate $x"), "{error}");
}
/// Token groups carry no target, so they must not share one occurrence
/// bucket: an optional disjoint group ahead of a labeled group
/// (`r : (A | B)? x=(C | D) {$x...}`) must not poison it. Block-label reads
/// take the last terminal child and never consult the index at all.
#[test]
fn disjoint_token_groups_do_not_poison_a_later_block_label() {
let mut statement = rule("s");
// `branch_local_cardinality` mirrors `cardinality` here: the optionality
// comes from the group's own `?`, not from choice membership.
let group_ref = |label: Option<&str>, token_types: Vec<i32>, min| ElementRef {
label: label.map(ToOwned::to_owned),
target: String::new(),
token_types,
is_block: true,
is_list: false,
cardinality: ChildCardinality { min, max: Some(1) },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality { min, max: Some(1) },
group_local_cardinality: ChildCardinality { min, max: Some(1) },
};
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![
group_ref(None, vec![1, 2], 0),
group_ref(Some("x"), vec![3, 4], 1),
],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2), ("C", 3), ("D", 4)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$x.text", &ctx).expect("translates");
assert!(translated.contains("terminal_children"), "{translated}");
assert!(translated.contains(".last()"), "{translated}");
}
/// A list label ahead of a same-target single label still contributes
/// children, so it must go through the occurrence accounting rather than be
/// skipped: `r : xs+=e name=e` puts one `e` before `name` (exact, countable
/// → `nth(1)`), while `r : xs+=e+ name=e` puts an unbounded run there and
/// leaves no fixed position at all.
#[test]
fn list_refs_ahead_of_a_single_label_are_counted_then_poison_when_unbounded() {
let list_ref = |max| ElementRef {
label: Some("xs".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: true,
cardinality: ChildCardinality { min: 1, max },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let single_ref = ElementRef {
label: Some("name".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let translate = |max| {
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![list_ref(max), single_ref.clone()],
children: BTreeMap::new(),
leading_target: Some("e".to_owned()),
});
let m = model(vec![statement, rule("e")]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
translate_body("$name.text", &ctx).map_err(|error| error.to_string())
};
// Exactly one preceding `e`: the position is known.
let exact = translate(Some(1)).expect("exact list count still resolves");
assert!(exact.contains(".nth(1)"), "{exact}");
// Unbounded run of `e` ahead of the label: no fixed index exists.
let error = translate(None).expect_err("unbounded list must not resolve");
assert!(error.contains("cannot translate $name"), "{error}");
}
/// A list read yields *every* same-target child, so it can only stand for
/// the label when no same-target element sits outside it. In the `mixed`
/// shape (`name=e ... errors+=e`) a `$errors` read would fold in `name`'s
/// child, so the label must not resolve.
#[test]
fn list_labels_sharing_a_target_with_another_label_stay_unresolved() {
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![
ElementRef {
label: Some("name".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
ElementRef {
label: Some("errors".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: true,
cardinality: ChildCardinality { min: 0, max: None },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
],
children: BTreeMap::new(),
leading_target: Some("e".to_owned()),
});
let m = model(vec![statement, rule("e")]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$errors", &ctx).expect_err("must not translate");
assert!(
error.to_string().contains("cannot translate $errors"),
"{error}"
);
// `name` is still resolvable: it precedes the list, so its own index is
// fixed at 0 and the list contributes nothing ahead of it.
let name = translate_body("$name.text", &ctx).expect("translates");
assert!(name.contains(".nth(0)"), "{name}");
}
/// A block label has no target to query, so its read walks the context's
/// terminal children by *position*: the count of terminals matched ahead of
/// the block. That is what makes `t=~'x' 'z' {$t.text}` read the token the
/// label bound rather than the trailing `'z'` the old `last()` picked
/// (issue #233). Where the count is not fixed, `last()` remains the fallback.
#[test]
fn block_labels_read_the_terminal_the_label_bound() {
let translate = |action_offset| {
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (0, 100),
refs: vec![
ElementRef {
label: Some("x".to_owned()),
target: String::new(),
token_types: vec![1, 2],
is_block: true,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some((10, 20)),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
ElementRef {
label: None,
target: "C".to_owned(),
token_types: vec![3],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some((30, 31)),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2), ("C", 3)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(action_offset),
site: ActionSite::Body,
token_types: &toks,
};
translate_body("$x.text", &ctx).map_err(|error| error.to_string())
};
// The block is the first terminal either way, so the read is `nth(0)` and
// the trailing `C` cannot be mistaken for it — regardless of whether the
// action precedes or follows `C`.
for offset in [25, 40] {
let translated = translate(offset).expect("a fixed terminal position resolves");
assert!(translated.contains("terminal_children"), "{translated}");
assert!(
translated.contains(".nth(0)"),
"offset {offset}: {translated}"
);
}
}
/// A mid-rule action executes at its own source position, so refs written
/// after it have not been matched and cannot affect its read. Spans decide
/// this: `r : xs+=A {$xs} A;` iterates the sole child available at the action,
/// while the same refs read from `@after` see both and must decline.
#[test]
fn future_children_do_not_constrain_a_mid_rule_read() {
let token_ref = |label: Option<&str>, is_list, span| ElementRef {
label: label.map(ToOwned::to_owned),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (0, 100),
// `xs+=A {action at 20} A`
refs: vec![
token_ref(Some("xs"), true, (10, 11)),
token_ref(None, false, (30, 31)),
],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1)]);
let mid_rule = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(20),
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$xs", &mid_rule).expect("trailing A has not matched yet");
assert!(translated.contains("child_tokens"), "{translated}");
// Read from `@after`, the trailing `A` has matched and would be iterated.
let after = TranslationCtx {
body_offset: None,
site: ActionSite::After,
..mid_rule
};
let error = translate_body("$xs", &after).expect_err("both children are present by then");
assert!(
error.to_string().contains("cannot translate $xs"),
"{error}"
);
}
/// Several declarations of one label can share a single read when each lowers
/// to the same query and they are mutually exclusive: `(x=A e {$x} | x=A f
/// {$x})` resolves, while `x=A | x='a'` resolves too because token-backed
/// equivalence is by *type*, not by source form or block-ness.
#[test]
fn compatible_declarations_share_one_read() {
// Each declaration is mandatory *within its branch* — `min: 0` on
// `cardinality` would say the label is genuinely optional, which is a
// different (and displaceable) shape.
let decl = |branch, is_block, span| ElementRef {
label: Some("x".to_owned()),
target: if is_block { "'a'" } else { "A" }.to_owned(),
token_types: vec![1],
is_block,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: vec![(5, branch)],
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
// `separate_alts` models `x=… | x=…` written as *top-level* alternatives,
// which the collector emits as two `AltModel`s — the shape a real grammar
// produces. Both in one `AltModel` instead models a nested `(… | …)` choice.
let translate = |second: ElementRef, offset, separate_alts: bool| {
let mut statement = rule("s");
if separate_alts {
for (index, declaration) in
[decl(0, false, (10, 11)), second].into_iter().enumerate()
{
statement.alts.push(AltModel {
label: None,
span: (index * 50, index * 50 + 50),
refs: vec![declaration],
children: BTreeMap::new(),
leading_target: None,
});
}
} else {
statement.alts.push(AltModel {
label: None,
span: (0, 100),
refs: vec![decl(0, false, (10, 11)), second],
children: BTreeMap::new(),
leading_target: None,
});
}
let m = model(vec![statement]);
let toks = tokens(&[("A", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: offset,
site: if offset.is_some() {
ActionSite::Body
} else {
ActionSite::After
},
token_types: &toks,
};
translate_body("$x.text", &ctx).map_err(|error| error.to_string())
};
// `(x=A e {action} | x=A f {action})`: same query, exclusive branches of one
// nested choice.
let translated = translate(decl(1, false, (30, 31)), Some(20), false)
.expect("identical reads in exclusive branches share one lookup");
assert!(translated.contains(".nth(0)"), "{translated}");
// `r @after {…} : x=A | x='a';` — literal and symbolic forms of one token
// type, as *top-level* alternatives. Both resolve at occurrence zero, the
// index where the block and token coordinate systems coincide.
let aliased = translate(decl(1, true, (60, 63)), None, true)
.expect("token-type equivalence ignores source form");
assert!(aliased.contains(".nth(0)"), "{aliased}");
}
/// A *literal* terminal label (`x='b'`) is `is_block` too, so it must take the
/// same positional terminal count as a labeled group — keying the count on an
/// empty target instead would leave it counting same-target children while its
/// read walks every terminal. This is ANTLR's
/// `ParserErrors/ConjuringUpToken` shape, where `'a'` precedes the label.
#[test]
fn literal_terminal_labels_count_every_preceding_terminal() {
let terminal = |label: Option<&str>, target: &str, token_type, span| ElementRef {
label: label.map(ToOwned::to_owned),
target: target.to_owned(),
token_types: vec![token_type],
// Literals and groups alike route through the block read.
is_block: true,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (0, 100),
// `'a' x='b' {action} 'c'`
refs: vec![
terminal(None, "'a'", 1, (10, 13)),
terminal(Some("x"), "'b'", 2, (14, 17)),
terminal(None, "'c'", 3, (40, 43)),
],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(20),
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$x", &ctx).expect("translates");
assert!(translated.contains("terminal_children"), "{translated}");
// `'a'` is terminal 0, so the label is terminal 1 — not `last()`, which
// would become `'c'` once that matched.
assert!(translated.contains(".nth(1)"), "{translated}");
}
/// An alternative that does not declare the label leaves it unset, so the
/// read has to come up empty there. `r : x=A | A` breaks that: the second
/// alternative builds an `A` the read would select, reporting a value for a
/// label the parse never bound. Conversely `r : x=A | B` is fine.
#[test]
fn unscoped_reads_reject_alternatives_that_would_satisfy_them_unbound() {
let token_ref = |label: Option<&str>, target: &str, token_type| ElementRef {
label: label.map(ToOwned::to_owned),
target: target.to_owned(),
token_types: vec![token_type],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let translate = |second: ElementRef| {
let mut statement = rule("s");
for (index, refs) in [vec![token_ref(Some("x"), "A", 1)], vec![second]]
.into_iter()
.enumerate()
{
statement.alts.push(AltModel {
label: None,
span: (index * 10, index * 10 + 10),
refs,
children: BTreeMap::new(),
leading_target: None,
});
}
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::After,
token_types: &toks,
};
translate_body("$x.text", &ctx).map_err(|error| error.to_string())
};
// `x=A | A`: the unlabeled `A` satisfies the read with `x` unset.
let error = translate(token_ref(None, "A", 1))
.expect_err("an unbound label must not read another alternative's child");
assert!(error.contains("cannot translate $x"), "{error}");
// `x=A | B`: nothing in the second alternative can be mistaken for `x`.
let translated =
translate(token_ref(None, "B", 2)).expect("disjoint alternative keeps the read");
assert!(translated.contains(".nth(0)"), "{translated}");
}
/// A list read iterates one target, so every declaration of the label must
/// name that target: `xs+=A xs+=B` would iterate `A` and drop every `B`.
/// Equivalent *block* labels across alternatives (`x=(A | B) | x=(C | D)`)
/// conversely stay resolvable, because the block read ignores token sets.
#[test]
fn list_declarations_must_share_a_target_while_block_reads_ignore_token_sets() {
let list_ref = |target: &str, token_type| ElementRef {
label: Some("xs".to_owned()),
target: target.to_owned(),
token_types: vec![token_type],
is_block: false,
is_list: true,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![list_ref("A", 1), list_ref("B", 2)],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$xs", &ctx).expect_err("mixed list targets must not resolve");
assert!(
error.to_string().contains("cannot translate $xs"),
"{error}"
);
// Two block labels over different token sets lower to the same read.
let block_ref = |token_types: Vec<i32>| ElementRef {
label: Some("x".to_owned()),
target: String::new(),
token_types,
is_block: true,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut choice = rule("s");
for (index, refs) in [vec![block_ref(vec![1, 2])], vec![block_ref(vec![3, 4])]]
.into_iter()
.enumerate()
{
choice.alts.push(AltModel {
label: None,
span: (index * 10, index * 10 + 10),
refs,
children: BTreeMap::new(),
leading_target: None,
});
}
let m = model(vec![choice]);
let toks = tokens(&[("A", 1), ("B", 2), ("C", 3), ("D", 4)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::After,
token_types: &toks,
};
let translated = translate_body("$x.text", &ctx).expect("equivalent block reads resolve");
assert!(translated.contains("terminal_children"), "{translated}");
}
/// Reads that `translate_element_read` cannot express must stay unresolved
/// rather than fall through to a different read: a list over a *token group*
/// (`xs+=(A | B)`) has no target to iterate and would emit
/// `.last()…collect()` — Rust that does not compile — and a *repeated* single
/// label (`(x=A)+`) is overwritten each iteration, so a fixed `nth(0)` pins
/// the first match where ANTLR exposes the latest.
#[test]
fn reads_the_translator_cannot_express_stay_unresolved() {
let translate = |element: ElementRef, read: &str| {
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![element],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
translate_body(read, &ctx).map_err(|error| error.to_string())
};
let group_list = ElementRef {
label: Some("xs".to_owned()),
target: String::new(),
token_types: vec![1, 2],
is_block: true,
is_list: true,
cardinality: ChildCardinality { min: 1, max: None },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let error = translate(group_list, "$xs").expect_err("no target to iterate");
assert!(error.contains("cannot translate $xs"), "{error}");
let repeated_single = ElementRef {
label: Some("x".to_owned()),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
cardinality: ChildCardinality { min: 1, max: None },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let error = translate(repeated_single, "$x.text").expect_err("no last-occurrence read");
assert!(error.contains("cannot translate $x"), "{error}");
}
/// Mutual exclusion needs the *whole* choice ancestry, not just the innermost
/// choice. In `((x=e | f) | e)` the label and the trailing `e` are separated
/// by the outer choice; keeping only the inner tag would make them look
/// independent and reject a valid action.
#[test]
fn nested_choices_keep_their_outer_branch_ancestry() {
let rule_ref = |label: Option<&str>, branches: Vec<(usize, usize)>, span| ElementRef {
label: label.map(ToOwned::to_owned),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: branches,
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (0, 100),
refs: vec![
// Outer choice 1 branch 0, then inner choice 2 branch 0.
rule_ref(Some("x"), vec![(1, 0), (2, 0)], (10, 11)),
// Outer choice 1 branch 1 — excluded by the *outer* choice alone.
rule_ref(None, vec![(1, 1)], (30, 31)),
],
children: BTreeMap::new(),
leading_target: Some("e".to_owned()),
});
let m = model(vec![statement, rule("e")]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$x.text", &ctx).expect("outer exclusion still applies");
assert!(translated.contains(".nth(0)"), "{translated}");
}
/// Sibling exclusion is valid only for a *mid-rule* action, which is confined
/// to the branch it is written in. An `@after` body runs whichever branch the
/// parse took, so `r @after {$x.text} : (x=A | A) EOF;` must decline — the
/// unlabeled branch's `A` is present when the read executes.
#[test]
fn unscoped_bodies_treat_sibling_matches_as_hazards() {
let branch_ref = |label: Option<&str>, branch, span| ElementRef {
label: label.map(ToOwned::to_owned),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: vec![(3, branch)],
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (0, 100),
refs: vec![
branch_ref(Some("x"), 0, (10, 11)),
branch_ref(None, 1, (20, 21)),
],
children: BTreeMap::new(),
leading_target: Some("A".to_owned()),
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1)]);
let after = TranslationCtx {
model: &m,
rule_index: 0,
// `@after`: unscoped, so any branch may have run.
body_offset: None,
site: ActionSite::After,
token_types: &toks,
};
let error = translate_body("$x.text", &after).expect_err("sibling match is a hazard here");
assert!(error.to_string().contains("cannot translate $x"), "{error}");
// The same refs read from a mid-rule action inside the labeled branch do
// resolve, since that action cannot run on the sibling branch.
let body = TranslationCtx {
body_offset: Some(15),
site: ActionSite::Body,
..after
};
let translated =
translate_body("$x.text", &body).expect("mid-rule action excludes sibling");
assert!(translated.contains(".nth(0)"), "{translated}");
}
/// A token label's read queries by token *type*, so a differently-spelled
/// terminal with the same type is the same child: with `A : 'a';`,
/// `r : (xs+=A)? 'a' {$xs}` must decline because the mandatory `'a'` would be
/// iterated as an `xs` element.
#[test]
fn token_labels_compare_types_not_source_spelling() {
let mut statement = rule("s");
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![
ElementRef {
label: Some("xs".to_owned()),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list: true,
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
ElementRef {
label: None,
// Literal spelling differs; the token type is identical.
target: "'a'".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$xs", &ctx).expect_err("aliased terminal is the same child");
assert!(
error.to_string().contains("cannot translate $xs"),
"{error}"
);
}
/// A same-target ref in a *sibling* choice branch never coexists with the
/// label, so it cannot displace an optional one: `r : (x=A {$x.text} B | A C)`
/// must still translate. Only a certainly-matched following child shadows.
#[test]
fn sibling_branch_children_do_not_shadow_an_optional_label() {
let mut statement = rule("s");
// Two branches of one choice: same choice id, different branch index.
let branch_ref = |label: Option<&str>, branch, span| ElementRef {
label: label.map(ToOwned::to_owned),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
// Mutually exclusive branches both report `min: 0`.
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: vec![(7, branch)],
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
statement.alts.push(AltModel {
label: None,
span: (0, 100),
// `(x=A {action} B | A C)`: the action sits inside branch 0.
refs: vec![
branch_ref(Some("x"), 0, (10, 11)),
branch_ref(None, 1, (30, 31)),
],
children: BTreeMap::new(),
leading_target: Some("A".to_owned()),
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let translated = translate_body("$x.text", &ctx).expect("translates");
assert!(translated.contains(".nth(0)"), "{translated}");
// The sequential counterpart — `(pred x=A)? A?`, both on the rule's own
// path — *is* a hazard: the follower may consume the only token while the
// label is unset. Cardinality alone cannot tell these two apart, which is
// what `choice_branch` exists for.
let mut sequential = rule("s");
let sequential_ref = |label: Option<&str>, span| ElementRef {
label: label.map(ToOwned::to_owned),
target: "A".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: Some(span),
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
sequential.alts.push(AltModel {
label: None,
span: (0, 100),
// `(pred x=A)? A?` with the action last: both have matched by then.
refs: vec![
sequential_ref(Some("x"), (10, 11)),
sequential_ref(None, (12, 13)),
],
children: BTreeMap::new(),
leading_target: Some("A".to_owned()),
});
let m = model(vec![sequential]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$x.text", &ctx).expect_err("sequential follower shadows");
assert!(error.to_string().contains("cannot translate $x"), "{error}");
}
/// A list label repeated within one alternative is the ordinary
/// comma-separated idiom (`xs+=e (op xs+=e)+`) — every declaration feeds the
/// same iteration, so repeats must not be mistaken for a conflict. This is
/// the shape of ANTLR's `ParserExec/ListLabelsOnRuleRefStartOfAlt`
/// descriptor, read from `@after` across alternatives that declare it plus
/// one that does not.
#[test]
fn repeated_list_declarations_across_alternatives_still_resolve() {
let list_ref = || ElementRef {
label: Some("args".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: true,
cardinality: ChildCardinality { min: 1, max: None },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let token_ref = ElementRef {
label: None,
target: "ID".to_owned(),
token_types: vec![1],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let mut statement = rule("s");
for (index, refs) in [
// `args+=e (AND args+=e)+` — two declarations, one iteration.
vec![list_ref(), list_ref()],
// An alternative that never mentions the label at all.
vec![token_ref],
]
.into_iter()
.enumerate()
{
statement.alts.push(AltModel {
label: None,
span: (index * 10, index * 10 + 10),
refs,
children: BTreeMap::new(),
leading_target: None,
});
}
let m = model(vec![statement, rule("e")]);
let toks = tokens(&[("ID", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::After,
token_types: &toks,
};
let translated = translate_body("$args", &ctx).expect("list label resolves");
assert!(translated.contains("child_rule_trees"), "{translated}");
}
/// `@after` / `@init` bodies are not scoped to an alternative, so one read
/// has to serve whichever alternative the parse took. `r : x=A | x=B` with an
/// `@after` read of `$x` would emit the `A` lookup and yield a default on the
/// `B` branch, while `r : x=A B | x=A C` resolves identically in both.
#[test]
fn unscoped_bodies_reject_labels_that_resolve_differently_per_alternative() {
let token_ref = |label: Option<&str>, target: &str, token_type| ElementRef {
label: label.map(ToOwned::to_owned),
target: target.to_owned(),
token_types: vec![token_type],
is_block: false,
is_list: false,
cardinality: ChildCardinality {
min: 1,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
let translate = |second: Vec<ElementRef>| {
let mut statement = rule("s");
for (index, refs) in [vec![token_ref(Some("x"), "A", 1)], second]
.into_iter()
.enumerate()
{
statement.alts.push(AltModel {
label: None,
span: (index * 10, index * 10 + 10),
refs,
children: BTreeMap::new(),
leading_target: None,
});
}
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2), ("C", 3)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
// `@after`: no offset, so no single owning alternative.
body_offset: None,
site: ActionSite::After,
token_types: &toks,
};
translate_body("$x.text", &ctx).map_err(|error| error.to_string())
};
// `x=A | x=B`: the two alternatives need different token lookups.
let error = translate(vec![token_ref(Some("x"), "B", 2)])
.expect_err("conflicting per-alternative reads must not translate");
assert!(error.contains("cannot translate $x"), "{error}");
// `x=A B | x=A C`: both resolve to the same `A` lookup at occurrence 0.
let agreed = translate(vec![token_ref(Some("x"), "A", 1), token_ref(None, "C", 3)])
.expect("agreeing per-alternative reads still translate");
assert!(agreed.contains(".nth(0)"), "{agreed}");
}
/// One label declared over disjoint targets (`r : (x=A | x=B)`) cannot be
/// served by a single positional read: picking the first declaration yields
/// an empty value whenever the parse took the other branch.
#[test]
fn labels_repeated_over_disjoint_targets_stay_unresolved() {
let mut statement = rule("s");
let token_ref = |target: &str, token_type| ElementRef {
label: Some("x".to_owned()),
target: target.to_owned(),
token_types: vec![token_type],
is_block: false,
is_list: false,
// Mutually exclusive branches.
cardinality: ChildCardinality {
min: 0,
max: Some(1),
},
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
};
statement.alts.push(AltModel {
label: None,
span: (10, 20),
refs: vec![token_ref("A", 1), token_ref("B", 2)],
children: BTreeMap::new(),
leading_target: None,
});
let m = model(vec![statement]);
let toks = tokens(&[("A", 1), ("B", 2)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: Some(15),
site: ActionSite::Body,
token_types: &toks,
};
let error = translate_body("$x.text", &ctx).expect_err("must not translate");
assert!(error.to_string().contains("cannot translate $x"), "{error}");
}
#[test]
fn translates_ctx_and_text() {
let m = model(vec![rule("s")]);
let toks = tokens(&[("ID", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::Body,
token_types: &toks,
};
let text = translate_body("$text", &ctx).expect("translates");
assert!(
text.contains("text_interval(action.start_index()"),
"{text}"
);
let tree = translate_body("$ctx.to_string_tree(Some(self))", &ctx).expect("translates");
assert_eq!(tree, "(&__ctx).to_string_tree(Some(self))");
}
#[test]
fn translates_active_context_child_iterators() {
let m = model(vec![rule("s"), rule("elseIfStatement")]);
let toks = tokens(&[]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::Body,
token_types: &toks,
};
let translated =
translate_body("$ctx.elseIfStatement_children()", &ctx).expect("translates");
assert_eq!(
translated,
"__ctx.child_rules(self.base.parse_tree_storage(), self.base.token_store(), 1)"
);
}
#[test]
fn translates_list_labels_as_lazy_iterators() {
let mut start = rule("s");
start.alts.push(AltModel {
label: None,
span: (0, 10),
refs: vec![
ElementRef {
label: Some("args".to_owned()),
target: "e".to_owned(),
token_types: Vec::new(),
is_block: false,
is_list: true,
cardinality: ChildCardinality { min: 1, max: None },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
ElementRef {
label: Some("ids".to_owned()),
target: "ID".to_owned(),
token_types: vec![1],
is_block: false,
is_list: true,
cardinality: ChildCardinality { min: 1, max: None },
stable_accessor: true,
choice_branch: Vec::new(),
choice_arity: Vec::new(),
choice_spans: Vec::new(),
group_spans: Vec::new(),
branch_spans: Vec::new(),
leading_terminal: true,
span: None,
branch_local_cardinality: ChildCardinality::ONE,
group_local_cardinality: ChildCardinality::ONE,
},
],
children: BTreeMap::new(),
leading_target: Some("e".to_owned()),
});
let m = model(vec![start, rule("e")]);
let toks = tokens(&[("ID", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::After,
token_types: &toks,
};
let rules = translate_body("let _: Vec<_> = $args.collect();", &ctx).expect("rule list");
assert_eq!(rules.matches(".collect()").count(), 1, "{rules}");
assert!(rules.contains("__ctx.child_rule_trees("), "{rules}");
let tokens = translate_body("let _: Vec<_> = $ids.collect();", &ctx).expect("token list");
assert_eq!(tokens.matches(".collect()").count(), 1, "{tokens}");
assert!(tokens.contains("__ctx.child_tokens("), "{tokens}");
}
#[test]
fn classifies_member_blocks() {
let body = "i: i32 = 0;\n\
#[allow(non_snake_case)]\n\
fn Property(&self) -> bool {\n true\n}\n\
struct LeafListener;\n";
let mut members = MembersModel::default();
classify_members(body, &mut members).expect("members classify");
assert_eq!(members.fields.len(), 1);
assert_eq!(members.fields[0].name, "i");
assert_eq!(members.fields[0].init, "0");
assert_eq!(members.impl_items.len(), 1);
assert!(members.impl_items[0].contains("fn Property"));
assert_eq!(members.module_items.len(), 1);
}
#[test]
fn dollar_inside_strings_is_left_alone() {
let m = model(vec![rule("s")]);
let toks = tokens(&[("ID", 1)]);
let ctx = TranslationCtx {
model: &m,
rule_index: 0,
body_offset: None,
site: ActionSite::Body,
token_types: &toks,
};
let body = "writeln!(self.output(), \"{}\", \"$notaref\");";
assert_eq!(translate_body(body, &ctx).expect("translates"), body);
}
}