rant 4.0.0-alpha.33

The Rant procedural templating language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
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
#![allow(dead_code)]
#![allow(clippy::ptr_arg)]

use super::{reader::RantTokenReader, lexer::*, message::*, Problem, Reporter};
use crate::{InternalString, RantProgramInfo, lang::*};
use fnv::FnvBuildHasher;
use line_col::LineColLookup;
use quickscope::ScopeMap;
use std::{collections::{HashMap, HashSet}, ops::Range, rc::Rc};
use RantToken::*;

type ParseResult<T> = Result<T, ()>;

const MAIN_PROGRAM_SCOPE_NAME: &str = "main scope";

// Infix precedence categories
const PREC_PREFIX: usize = 9;
const PREC_EXPONENTIAL: usize = 8;
const PREC_MULTIPLICATIVE: usize = 7;
const PREC_ADDITIVE: usize = 6;
const PREC_RELATIONAL: usize = 5;
const PREC_EQUALITY: usize = 4;
const PREC_CONJUNCTIVE: usize = 3;
const PREC_XOR: usize = 2;
const PREC_DISJUNCTIVE: usize = 1;
const PREC_SEQUENCE: usize = 0;

#[derive(Copy, Clone, PartialEq)]
enum Op {
  Infix(InfixOp),
  Prefix(PrefixOp),
}

#[derive(Copy, Clone, PartialEq)]
enum PrefixOp {
  Negate,
  LogicNot,
}

#[derive(Copy, Clone, PartialEq)]
enum InfixOp {
  Add,
  Subtract,
  Multiply,
  Divide,
  Modulo,
  Power,
  LogicAnd,
  LogicOr,
  LogicXor,
  Equals,
  NotEquals,
  Greater,
  GreatOrEqual,
  Less,
  LessOrEqual,
}

impl InfixOp {
  #[inline]
  fn precedence(&self) -> usize {
    match self {
      Self::Power =>         PREC_EXPONENTIAL,
      Self::Multiply =>      PREC_MULTIPLICATIVE,
      Self::Divide =>        PREC_MULTIPLICATIVE,
      Self::Modulo =>        PREC_MULTIPLICATIVE,
      Self::Add =>           PREC_ADDITIVE,
      Self::Subtract =>      PREC_ADDITIVE,
      Self::Greater =>       PREC_RELATIONAL,
      Self::GreatOrEqual =>  PREC_RELATIONAL,
      Self::Less =>          PREC_RELATIONAL,
      Self::LessOrEqual =>   PREC_RELATIONAL,
      Self::Equals =>        PREC_EQUALITY,
      Self::NotEquals =>     PREC_EQUALITY,
      Self::LogicAnd =>      PREC_CONJUNCTIVE,
      Self::LogicXor =>      PREC_XOR,
      Self::LogicOr =>       PREC_DISJUNCTIVE,
    }
  }
}

#[derive(Copy, Clone, PartialEq)]
enum OpType {
  Prefix,
  Infix,
}

impl Op {

  #[inline]
  fn is_kw_supported(kw_name: &str) -> bool {
    matches!(kw_name, KW_NEG | KW_NOT | KW_EQ | KW_NEQ | KW_GT | KW_GE | KW_LT | KW_LE)
  }

  #[inline]
  fn op_type(&self) -> OpType {
    match self {
      Self::Infix(_) => OpType::Infix,
      Self::Prefix(_) => OpType::Prefix,
    }
  }

  #[inline(always)]
  fn from_token(token: &RantToken) -> Option<Op> {
    Some(match token {
      Plus =>               Self::Infix(InfixOp::Add),
      Minus =>              Self::Infix(InfixOp::Subtract),
      Star =>               Self::Infix(InfixOp::Multiply),
      Slash =>              Self::Infix(InfixOp::Divide),
      Percent =>            Self::Infix(InfixOp::Modulo),
      DoubleStar =>         Self::Infix(InfixOp::Power),
      And =>                Self::Infix(InfixOp::LogicAnd),
      VertBar =>            Self::Infix(InfixOp::LogicOr),
      Caret =>              Self::Infix(InfixOp::LogicXor),
      Keyword(kw) if kw.is_valid => match kw.name.as_str() {
        KW_NEG =>       Self::Prefix(PrefixOp::Negate),
        KW_NOT =>       Self::Prefix(PrefixOp::LogicNot),
        KW_EQ =>        Self::Infix(InfixOp::Equals),
        KW_NEQ =>       Self::Infix(InfixOp::NotEquals),
        KW_GT =>        Self::Infix(InfixOp::Greater),
        KW_GE =>        Self::Infix(InfixOp::GreatOrEqual),
        KW_LT =>        Self::Infix(InfixOp::Less),
        KW_LE =>        Self::Infix(InfixOp::LessOrEqual),
        _ => return None,
      },
      _ => return None,
    })
  }

  #[inline]
  fn precedence(&self) -> usize {
    match self {
      Self::Prefix(_) =>          PREC_PREFIX,
      Self::Infix(op) =>  op.precedence(),
    }
  }
}

/// Provides context to the sequence parser; determines valid terminating tokens among other context-sensitive features.
#[derive(Copy, Clone, PartialEq)]
enum SequenceParseMode {
  /// Parse a sequence like a top-level program.
  ///
  /// Breaks on EOF.
  TopLevel,
  /// Parse a sequence like a block element.
  ///
  /// Breaks on `Pipe`, `Colon`, and `RightBrace`.
  BlockElement,
  /// Parse a sequence like a function argument.
  ///
  /// Breaks on `Semi`, `PipeOp`, and `RightBracket`.
  FunctionArg,
  /// Parse a sequence like a function body.
  ///
  /// Breaks on `RightBrace`.
  FunctionBodyBlock,
  /// Parse a sequence like a dynamic key expression.
  ///
  /// Breaks on `RightBrace`.
  DynamicKey,
  /// Parse a sequence like a variable assignment value.
  ///
  /// Breaks on `RightAngle` and `Semi`.
  VariableAssignment,
  /// Parse a sequence like an accessor fallback value.
  ///
  /// Breaks on `RightAngle` and `Semi`.
  AccessorFallbackValue,
  /// Parses a sequence like a parameter default value.
  ///
  /// Breaks on `RightBracket` and `Semi`.
  ParamDefaultValue,
  /// Parse a sequence like a collection initializer element.
  ///
  /// Breaks on `Semi` and `RightParen`.
  CollectionInit,
  /// Parses an if-statement condition.
  /// 
  /// Breaks on `Colon`.
  Condition,
  /// Infix right-hand side
  InfixRhs,
}

/// Indicates what kind of token terminated a sequence read.
enum SequenceEndType {
  /// Top-level program sequence was terminated by end-of-file.
  ProgramEnd,
  /// Block element sequence is key and was terminated by `Colon`.
  BlockAssocDelim,
  /// Block element sequence was terminated by `Pipe`.
  BlockDelim,
  /// Block element sequence was terminated by `RightBrace`.
  BlockEnd,
  /// Function argument sequence was terminated by `Semi`.
  FunctionArgEndNext,
  /// Function argument sequence was terminated by `RightBracket`.
  FunctionArgEndBreak,
  /// Function argument sequence was terminated by `PipeOp`.
  FunctionArgEndToPipe,
  /// Function argument sequence was terminated by `RightAngle`.
  FunctionArgEndToAssignPipe,
  /// Function body sequence was terminated by `RightBrace`.
  FunctionBodyEnd,
  /// Dynamic key sequencce was terminated by `RightBrace`.
  DynamicKeyEnd,
  /// Variable accessor was terminated by `RightAngle`.
  VariableAccessEnd,
  /// Variable assignment expression was terminated by `Semi`. 
  VariableAssignDelim,
  /// Accessor fallback value was termianted by `RightAngle`.
  AccessorFallbackValueToEnd,
  /// Accessor fallback value was terminated by `Semi`.
  AccessorFallbackValueToDelim,
  /// Collection initializer was terminated by `RightParen`.
  CollectionInitEnd,
  /// Collection initializer was termianted by `Semi`.
  CollectionInitDelim,
  /// Parameter default value was terminated by `Semi`, indicating another parameter follows.
  ParamDefaultValueSeparator,
  /// Parameter default value was terminated by `RightBracket`, indicating the end of the signature was reached..
  ParamDefaultValueSignatureEnd,
  /// Sequence terminated by infix operator of lower precedence
  Operator,
  /// Condition was terminated by `Colon`.
  ConditionEnd,
}

/// Used to track variable usages during compilation.
struct VarStats {
  def_span: Range<usize>,
  writes: usize,
  reads: usize,
  /// Indicates whether the reads from this variable are fallible (meaning the variable isn't guaranteed to be defined).
  ///
  /// For optional parameters without a fallback this is `true`.
  has_fallible_read: bool,
  is_const: bool,
  is_auto_hinted: bool,
  role: VarRole,
}

impl VarStats {
  #[inline]
  fn new(role: VarRole, def_span: Range<usize>, is_const: bool) -> Self {
    Self {
      role,
      def_span,
      is_const,
      has_fallible_read: false,
      is_auto_hinted: false,
      reads: 0,
      writes: 0,
    }
  }

  #[inline]
  fn with_auto_hint(self) -> Self {
    Self {
      is_auto_hinted: true,
      .. self
    }
  }

  #[inline]
  fn with_fallible_read(self) -> Self {
    Self {
      has_fallible_read: true,
      .. self
    }
  }

  #[inline]
  fn add_write(&mut self) {
    self.writes += 1;
  }

  #[inline]
  fn add_read(&mut self, is_fallible_read: bool) {
    if matches!(self.role, VarRole::FallibleOptionalArgument) && is_fallible_read {
      self.has_fallible_read = true;
    }
    self.reads += 1;
  }
}

/// Provides context to a variable tracked by the compiler.
#[derive(Copy, Clone, PartialEq)]
enum VarRole {
  /// Just a normal variable.
  Normal,
  /// A variable known to be a function.
  Function,
  /// Function argument.
  Argument,
  /// Optional function argument without a fallback.
  /// If accessed without a fallback expression, should generate a compiler error.
  FallibleOptionalArgument,
  /// Pipeval from a piped call.
  PipeValue,
}

/// Returns a range that encompasses both input ranges.
#[inline]
fn super_range(a: &Range<usize>, b: &Range<usize>) -> Range<usize> {
  a.start.min(b.start)..a.end.max(b.end)
}

#[derive(Debug)]
enum ParsedSequenceExtras {
  WeightedBlockElement {
    weight_expr: Rc<Sequence>
  }
}

// TODO: Add a helper function for creating simple instances of `ParsedSequence` to reduce code bloat
/// Contains information about a successfully parsed sequence and its context.
struct ParsedSequence {
  sequence: Sequence,
  end_type: SequenceEndType,
  is_auto_hinted: bool,
  extras: Option<ParsedSequenceExtras>,
  next_infix_op: Option<Op>,
}

#[derive(Copy, Clone, PartialEq)]
enum BlockParseMode {
  NeedsStart,
  StartParsed(Option<BlockProtection>),
}

/// Contains information about a successfully parsed block.
struct ParsedBlock {
  is_auto_hinted: bool,
  block: Block
}

/// Contains information about a successfully parsed accessor.
struct ParsedAccessor {
  nodes: Vec<Expression>,
  is_auto_hinted: bool
}

/// Contains information about a successfully parsed function accessor.
struct ParsedFunctionAccessor {
  node: Expression,
  is_auto_hinted: bool
}

/// Contains information about a successfully parsed function parameter.
struct ParsedParameter {
  param: Parameter,
  span: Range<usize>,
  is_auto_hinted: bool
}

/// Contains information about a successfully parsed conditional.
struct ParsedConditional {
  node: Expression,
  is_auto_hinted: bool,
}

/// Contains information about a successfully parsed parenthetical expression or collection initializer.
struct ParsedGroupOrCollection {
  node: Expression,
  is_collection: bool,
  is_auto_hinted: bool,
}

/// A parser that turns Rant code into an RST (Rant Syntax Tree).
pub struct RantParser<'source, 'report, R: Reporter> {
  /// A string slice containing the source code being parsed.
  source: &'source str,
  /// Flag set if there are compiler errors.
  has_errors: bool,
  /// The token stream used by the parser.
  reader: RantTokenReader<'source>,
  /// The line/col lookup for error reporting.
  lookup: LineColLookup<'source>,
  /// The error reporter.
  reporter: &'report mut R,
  /// Enables additional debug information.
  debug_enabled: bool,
  /// A string describing the origin (containing program) of a program element.
  info: Rc<RantProgramInfo>,
  /// Keeps track of active variables in each scope while parsing.
  var_stack: ScopeMap<Identifier, VarStats>,
  /// Keeps track of active variable capture frames.
  capture_stack: Vec<(usize, HashSet<Identifier, FnvBuildHasher>)>,
}

impl<'source, 'report, R: Reporter> RantParser<'source, 'report, R> {
  pub fn new(source: &'source str, reporter: &'report mut R, debug_enabled: bool, info: &Rc<RantProgramInfo>) -> Self {
    Self {
      source,
      has_errors: false,
      reader: RantTokenReader::new(source),
      lookup: LineColLookup::new(source),
      reporter,
      debug_enabled,
      info: Rc::clone(info),
      var_stack: Default::default(),
      capture_stack: Default::default(),
    }
  }
}

impl<'source, 'report, R: Reporter> RantParser<'source, 'report, R> {
  /// Top-level parsing function invoked by the compiler.
  pub fn parse(&mut self) -> Result<Rc<Sequence>, ()> {
    let result = self.parse_sequence(SequenceParseMode::TopLevel);
    match result {
      // Err if parsing "succeeded" but there are soft syntax errors
      Ok(..) if self.has_errors => Err(()),
      // Ok if parsing succeeded and there are no syntax errors
      Ok(ParsedSequence { sequence, .. }) => Ok(Rc::new(sequence)),
      // Err on hard syntax error
      Err(()) => Err(())
    }
  }
  
  /// Reports a syntax error, allowing parsing to continue but causing the final compilation to fail. 
  fn report_error(&mut self, problem: Problem, span: &Range<usize>) {
    let (line, col) = self.lookup.get(span.start);
    self.has_errors = true;
    self.reporter.report(CompilerMessage::new(problem, Severity::Error, Some(Position::new(line, col, span.clone()))));
  }

  /// Reports a warning, but allows compiling to succeed.
  fn report_warning(&mut self, problem: Problem, span: &Range<usize>) {
    let (line, col) = self.lookup.get(span.start);
    self.reporter.report(CompilerMessage::new(problem, Severity::Warning, Some(Position::new(line, col, span.clone()))));
  }
  
  /// Emits an "unexpected token" error for the most recently read token.
  #[inline]
  fn report_unexpected_last_token_error(&mut self) {
    self.report_error(Problem::UnexpectedToken(self.reader.last_token_string().to_string()), &self.reader.last_token_span())
  }

  #[inline]
  fn report_unexpected_token_error(&mut self, span: &Range<usize>) {
    self.report_error(Problem::UnexpectedToken(self.source[span.start .. span.end].to_string()), span)
  }

  #[inline]
  fn report_expected_token_error(&mut self, expected: &str, span: &Range<usize>) {
    self.report_error(Problem::ExpectedToken(expected.to_owned()), span);
  }

  /// Parses a sequence of items with a new variable scope. Items are individual elements of a Rant program (fragments, blocks, function calls, etc.)
  #[inline]
  fn parse_sequence(&mut self, mode: SequenceParseMode) -> ParseResult<ParsedSequence> {
    self.var_stack.push_layer();
    let parsed_seq = self.parse_sequence_inner(mode, PREC_SEQUENCE)?;
    self.analyze_top_vars();
    self.var_stack.pop_layer();
    Ok(parsed_seq)
  }
  
  /// Inner logic of `parse_sequence()`. Intended to be wrapped in other specialized sequence-parsing functions.
  fn parse_sequence_inner(&mut self, mode: SequenceParseMode, precedence: usize) -> ParseResult<ParsedSequence> {
    let mut sequence = Sequence::empty(&self.info);
    let mut next_print_flag = PrintFlag::None;
    let mut last_print_flag_span: Option<Range<usize>> = None;
    let mut is_seq_text = false;
    let mut pending_whitespace = None;
    let debug = self.debug_enabled;

    macro_rules! check_dangling_printflags {
      () => {
        // Make sure there are no dangling printflags
        match next_print_flag {
          PrintFlag::None => {},
          PrintFlag::Hint => {
            if let Some(flag_span) = last_print_flag_span.take() {
              self.report_error(Problem::InvalidHint, &flag_span);
            }
          },
          PrintFlag::Sink => {
            if let Some(flag_span) = last_print_flag_span.take() {
              self.report_error(Problem::InvalidSink, &flag_span);
            }
          }
        }
      }
    }
    
    #[allow(unused_assignments)] // whitespace macro needs this, but https://github.com/rust-lang/rust/issues/15701
    while let Some((token, span)) = self.reader.next() {
      let _debug_inject_toggle = true;

      macro_rules! whitespace {
        (allow) => {{
          if let Some(ws) = pending_whitespace.take() {
            if is_seq_text {
              emit!(Expression::Whitespace(ws));
            }
          }
        }};
        (queue next) => {{
          if let Some((Whitespace, ..)) = self.reader.take_where(|tt| matches!(tt, Some((Whitespace, ..)))) {
            pending_whitespace = Some(self.reader.last_token_string());
          }
        }};
        (queue $ws:expr) => {
          pending_whitespace = Some($ws);
        };
        (ignore prev) => {{
          pending_whitespace = None;
        }};
        (ignore next) => {
          self.reader.skip_ws()
        };
        (ignore both) => {{
          whitespace!(ignore prev);
          whitespace!(ignore next);
        }};
      }

      macro_rules! no_debug {
        ($e:expr) => {{
          let _debug_inject_toggle = false;
          $e
        }}
      }

      macro_rules! inject_debug_info {
        () => {
          if debug && _debug_inject_toggle {
            let (line, col) = self.lookup.get(span.start);
            sequence.push(Rc::new(Expression::DebugCursor(DebugInfo::Location { line, col })));
          }
        }
      }
      
      // Macro for prohibiting hints/sinks before certain tokens
      macro_rules! no_flags {
        () => {
          if (!matches!(next_print_flag, PrintFlag::None)) {
            if let Some(flag_span) = last_print_flag_span.take() {
              self.report_error(match next_print_flag {
                PrintFlag::Hint => Problem::InvalidHint,
                PrintFlag::Sink => Problem::InvalidSink,
                PrintFlag::None => unreachable!()
              }, &flag_span)
            }
          }
        };
        ($b:block) => {
          if matches!(next_print_flag, PrintFlag::None) {
            $b
          } else if let Some(flag_span) = last_print_flag_span.take() {
            self.report_error(match next_print_flag {
              PrintFlag::Hint => Problem::InvalidHint,
              PrintFlag::Sink => Problem::InvalidSink,
              PrintFlag::None => unreachable!()
            }, &flag_span)
          }
        };
        (on $b:block) => {{
          let elem = $b;
          if !matches!(next_print_flag, PrintFlag::None) {
            if let Some(flag_span) = last_print_flag_span.take() {
              self.report_error(match next_print_flag {
                PrintFlag::Hint => Problem::InvalidHintOn(elem.display_name()),
                PrintFlag::Sink => Problem::InvalidSinkOn(elem.display_name()),
                PrintFlag::None => unreachable!()
              }, &flag_span)
            }
          }
          inject_debug_info!();
          sequence.push(Rc::new(elem));
        }};        
      }

      macro_rules! emit {
        ($elem:expr) => {{
          inject_debug_info!();
          sequence.push(Rc::new($elem));
        }}
      }

      macro_rules! emit_last_string {
        () => {{
          inject_debug_info!();
          sequence.push(Rc::new(Expression::Fragment(InternalString::from(self.reader.last_token_string()))));
        }}
      }
      
      // Shortcut macro for "unexpected token" error
      macro_rules! unexpected_token_error {
        () => {
          self.report_error(Problem::UnexpectedToken(self.reader.last_token_string().to_string()), &span)
        };
      }

      /// Eats as many fragments / escape sequences as possible and combines their string representations into the input `String`.
      macro_rules! consume_fragments {
        ($s:ident) => {
          while let Some((token, span)) = self.reader.take_where(|t| matches!(t, Some((Escape(..) | Fragment, ..)))) {
            match token {
              Escape(ch) => match ch {
                ParsedEscape::Char(ch) => $s.push(ch),
                ParsedEscape::InvalidChar(ch) => {
                  self.report_error(Problem::InvalidEscapeChar(ch), &span);
                },
                ParsedEscape::InvalidUnicode(cp) => {
                  self.report_error(Problem::InvalidEscapeCodePoint(cp), &span);
                },
              },
              _ => $s.push_str(&self.reader.last_token_string()),
            }
          }
        }
      }
      
      // Parse next sequence item
      match token {
        
        // Hint
        Hint => no_flags!({
          whitespace!(allow);
          is_seq_text = true;
          next_print_flag = PrintFlag::Hint;
          last_print_flag_span = Some(span.clone());
          continue
        }),
        
        // Sink
        Sink => no_flags!({
          // Ignore pending whitespace
          whitespace!(ignore prev);
          next_print_flag = PrintFlag::Sink;
          last_print_flag_span = Some(span.clone());
          continue
        }),

        // Prefix keyword
        Keyword(KeywordInfo { name: kw, is_valid: is_kw_valid }) if is_kw_valid && !Op::is_kw_supported(kw.as_str()) => {
          let kwstr = kw.as_str();
          match kwstr {            
            // Boolean constants
            KW_TRUE => no_debug!(no_flags!(on {
              whitespace!(ignore both);
              Expression::Boolean(true)
            })),
            KW_FALSE => no_debug!(no_flags!(on {
              whitespace!(ignore both);
              Expression::Boolean(false)
            })),
            KW_IF => {
              whitespace!(allow);              
              let ParsedConditional {
                node: cond_node,
                is_auto_hinted: cond_auto_hinted,
              } = self.parse_conditional(false)?;

              is_seq_text |= cond_auto_hinted;
              emit!(cond_node);
            },
            // @require statement
            KW_REQUIRE => no_flags!({
              whitespace!(ignore both);
              whitespace!(ignore next);
              if let Some((require_first_arg_token, require_first_arg_token_span)) = self.reader.next() {                
                match require_first_arg_token {
                  RantToken::StringLiteral(require_path) => {
                    let node = Expression::Require {
                      alias: None,
                      path: require_path,
                    };
                    whitespace!(ignore next);
                    emit!(node);
                  },
                  RantToken::Fragment => {
                    let require_alias = self.reader.last_token_string();
                    if !is_valid_ident(require_alias.as_str()) {
                      self.report_error(Problem::InvalidIdentifier(require_alias.to_string()), &require_first_arg_token_span);
                    }
                    whitespace!(ignore next);
                    if !self.reader.eat_where(|t| matches!(t, Some((RantToken::Colon, _)))) {
                      self.report_error(Problem::ExpectedToken(":".to_owned()), &self.reader.last_token_span());
                      continue
                    }
                    if let Some((require_path_token, require_path_token_span)) = self.reader.next_solid() {
                      whitespace!(ignore next);
                      match require_path_token {
                        RantToken::StringLiteral(require_path) => {
                          let node = Expression::Require {
                            alias: Some(require_alias),
                            path: require_path,
                          };
                          emit!(node);
                        },
                        _ => self.report_error(Problem::InvalidRequireArgumentToken, &require_path_token_span)
                      }
                    } else {
                      self.report_error(Problem::MissingRequireArgument, &self.reader.last_token_span());
                    }
                  },
                  _ => unexpected_token_error!()
                }
              } else {
                self.report_error(Problem::MissingRequireArgument, &self.reader.last_token_span());
                return Err(())
              }
            }),

            // Control flow
            KW_RETURN | KW_CONTINUE | KW_BREAK | KW_WEIGHT => {
              whitespace!(ignore both);
              let ParsedSequence {
                sequence: charm_sequence,
                end_type: charm_end_type,
                is_auto_hinted: is_charm_printing,
                extras: mut charm_extras,
                ..
              } = self.parse_sequence(mode)?;
              let charm_sequence_name = charm_sequence.name.clone();
              let charm_sequence = (!charm_sequence.is_empty()).then(|| Rc::new(charm_sequence));
              match kw.as_str() {
                KW_RETURN => emit!(Expression::Return(charm_sequence)),
                KW_CONTINUE => emit!(Expression::Continue(charm_sequence)),
                KW_BREAK => emit!(Expression::Break(charm_sequence)),
                KW_WEIGHT => {
                  if mode == SequenceParseMode::BlockElement {
                    charm_extras = Some(ParsedSequenceExtras::WeightedBlockElement {
                      weight_expr: charm_sequence.unwrap_or_else(|| Rc::new(Sequence::empty(&self.info)))
                    });
                  } else {
                    self.report_error(Problem::WeightNotAllowed, &span);
                  }
                },
                _ => unreachable!()
              };
              check_dangling_printflags!();
              return Ok(ParsedSequence {
                sequence: if let Some(charm_sequence_name) = charm_sequence_name {
                  sequence.with_name(charm_sequence_name)
                } else {
                  sequence
                },
                end_type: charm_end_type,
                is_auto_hinted: is_charm_printing || is_seq_text,
                extras: charm_extras,
                next_infix_op: None,
              })
            },
            _ => self.report_error(Problem::UnexpectedToken(self.reader.last_token_string().to_string()), &span),
          }          
        },
        
        // Block start
        LeftBrace => {
          // Read in the entire block
          let parsed_block = self.parse_block(BlockParseMode::StartParsed(None))?;

          // Decide what to do with previous whitespace
          match next_print_flag {                        
            // If hinted, allow pending whitespace
            PrintFlag::Hint => {
              whitespace!(allow);
              is_seq_text = true;
            },
            
            // If sinked, delete pending whitespace
            PrintFlag::Sink => whitespace!(ignore both),
            
            // If no flag, infer from block contents
            PrintFlag::None => {
              if parsed_block.is_auto_hinted {
                whitespace!(allow);
                is_seq_text = true;
              } else {
                whitespace!(ignore both)
              }
            }
          }
          
          emit!(Expression::Block(Rc::new(parsed_block.block)));
        },

        // Pipe operator
        PipeOp => no_flags!({
          // Ignore pending whitespace
          whitespace!(ignore prev);
          match mode {
            SequenceParseMode::FunctionArg => {
              return Ok(ParsedSequence {
                sequence: sequence.with_name_str("argument"),
                end_type: SequenceEndType::FunctionArgEndToPipe,
                is_auto_hinted: is_seq_text,
                extras: None,
                next_infix_op: None,
              })
            },
            _ => unexpected_token_error!()
          }
        }),

        // Pipe value
        PipeValue => {
          emit!(Expression::PipeValue);
          self.track_pipeval_read(&span);
        },
        
        // Block element delimiter (when in block parsing mode)
        VertBar if mode == SequenceParseMode::BlockElement => no_flags!({
          // Ignore pending whitespace
          whitespace!(ignore prev);
          return Ok(ParsedSequence {
            sequence: sequence.with_name_str("block element"),
            end_type: SequenceEndType::BlockDelim,
            is_auto_hinted: is_seq_text,
            extras: None,
            next_infix_op: None,
          })
        }),
        
        // Block/func body/dynamic key end
        RightBrace => no_flags!({
          // Ignore pending whitespace
          whitespace!(ignore prev);
          match mode {
            SequenceParseMode::BlockElement => {
              return Ok(ParsedSequence {
                sequence: sequence.with_name_str("block element"),
                end_type: SequenceEndType::BlockEnd,
                is_auto_hinted: is_seq_text,
                extras: None,
                next_infix_op: None,
              })
            },
            SequenceParseMode::FunctionBodyBlock => {
              return Ok(ParsedSequence {
                sequence: sequence.with_name_str("function body"),
                end_type: SequenceEndType::FunctionBodyEnd,
                is_auto_hinted: is_seq_text,
                extras: None,
                next_infix_op: None,
              })
            },
            _ => unexpected_token_error!()
          }
        }),
        
        // Map initializer or protected block
        At => {
          match self.reader.next_solid() {
            Some((LeftBrace, _)) => {
              // Read in the entire block
              let parsed_block = self.parse_block(BlockParseMode::StartParsed(Some(BlockProtection::Outer)))?;

              // Decide what to do with previous whitespace
              match next_print_flag {                        
                // If hinted, allow pending whitespace
                PrintFlag::Hint => {
                  whitespace!(allow);
                  is_seq_text = true;
                },
                
                // If sinked, delete pending whitespace
                PrintFlag::Sink => whitespace!(ignore both),
                
                // If no flag, infer from block contents
                PrintFlag::None => {
                  if parsed_block.is_auto_hinted {
                    whitespace!(allow);
                    is_seq_text = true;
                  } else {
                    whitespace!(ignore both)
                  }
                }
              }
              
              emit!(Expression::Block(Rc::new(parsed_block.block)));
            }
            _ => {
              // TODO: Use a more descriptive error here
              self.report_unexpected_last_token_error();
              emit!(Expression::NothingVal);
            },
          }
        },
        
        // Group or collection initializer
        LeftParen => {
          let ParsedGroupOrCollection {
            node,
            is_collection,
            is_auto_hinted
          } = self.parse_group_or_collection_init(&span)?;

          if is_collection {
            // Flags are redundant on collection initializers since they're always non-text
            no_flags!();
          } else if is_seq_text && is_auto_hinted {
            whitespace!(allow);
          }

          emit!(node);
        },
        
        // Collection init termination
        RightParen => no_flags!({
          match mode {
            SequenceParseMode::DynamicKey => {
              return Ok(ParsedSequence {
                sequence: sequence.with_name_str("dynamic key"),
                end_type: SequenceEndType::DynamicKeyEnd,
                is_auto_hinted: is_seq_text,
                extras: None,
                next_infix_op: None,
              })
            },
            SequenceParseMode::CollectionInit => {
              return Ok(ParsedSequence {
                sequence,
                end_type: SequenceEndType::CollectionInitEnd,
                is_auto_hinted: true,
                extras: None,
                next_infix_op: None,
              })
            },
            _ => unexpected_token_error!()
          }
        }),
        
        // Function creation or call
        LeftBracket => {
          let ParsedFunctionAccessor {
            node: func_access,
            is_auto_hinted
          } = self.parse_func_access()?;

          match next_print_flag {
            PrintFlag::Sink => whitespace!(ignore next),
            PrintFlag::None => {
              if is_auto_hinted {
                is_seq_text = true;
                whitespace!(allow);
              }
            },
            _ => whitespace!(allow)
          }
          
          // Definitions are implicitly sinked and ignore surrounding whitespace
          if let Expression::FuncDef(_) = func_access {
            no_flags!();
            whitespace!(ignore both);
          }
          
          emit!(func_access);
        },
        
        // Can be terminator for function args and anonymous function expressions
        RightBracket => no_flags!({
          match mode {
            SequenceParseMode::FunctionArg => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("argument"),
              end_type: SequenceEndType::FunctionArgEndBreak,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::ParamDefaultValue => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("default value"),
              end_type: SequenceEndType::ParamDefaultValueSignatureEnd,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            _ => unexpected_token_error!()
          }
        }),
        
        // Variable access start
        LeftAngle => {
          let ParsedAccessor {
            nodes,
            is_auto_hinted
          } = self.parse_accessor()?;

          if is_auto_hinted && matches!(next_print_flag, PrintFlag::None) {
            is_seq_text = true;
            whitespace!(allow);
          }

          for node in nodes {
            match node {
              Expression::Get(..) => {
                whitespace!(allow);
              },
              Expression::Set(..) | Expression::Define(..) => {
                // whitespace!(ignore both);
              },
              _ => unreachable!()
            }
            emit!(node);
          }
        },
        
        // Variable access end
        RightAngle => no_flags!({
          match mode {
            SequenceParseMode::FunctionArg => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("argument"),
              end_type: SequenceEndType::FunctionArgEndToAssignPipe,
              is_auto_hinted: is_seq_text,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::VariableAssignment => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("setter value"),
              end_type: SequenceEndType::VariableAccessEnd,
              is_auto_hinted: is_seq_text,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::AccessorFallbackValue => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("fallback value"),
              end_type: SequenceEndType::AccessorFallbackValueToEnd,
              is_auto_hinted: is_seq_text,
              extras: None,
              next_infix_op: None,
            }),
            _ => unexpected_token_error!()
          }
        }),

        // These symbols are only used in special contexts and can be safely printed
        Question | Dollar | Equals | DoubleDot
        => no_flags!(on {
          whitespace!(allow);
          is_seq_text = true;
          let frag = self.reader.last_token_string();
          Expression::Fragment(frag)
        }),
        
        // Fragment
        Fragment => no_flags!(on {
          whitespace!(allow);
          is_seq_text = true;
          let mut frag = self.reader.last_token_string();
          consume_fragments!(frag);
          Expression::Fragment(frag)
        }),
        
        // Whitespace (only if sequence isn't empty)
        Whitespace => no_flags!({
          // Don't set is_printing here; whitespace tokens always appear with other printing tokens
          if is_seq_text {
            let ws = self.reader.last_token_string();
            whitespace!(queue ws);
          }
        }),
        
        // Escape sequences
        Escape(esc) => {
          whitespace!(allow);
          is_seq_text = true;
          let mut frag = InternalString::new();
          match esc {
            ParsedEscape::Char(c) => {
              frag.push(c);
            },
            ParsedEscape::InvalidChar(c) => {
              self.report_error(Problem::InvalidEscapeChar(c), &span);    
            },
            ParsedEscape::InvalidUnicode(cp) => {
              self.report_error(Problem::InvalidEscapeCodePoint(cp), &span);
            },
          }
          consume_fragments!(frag);
          emit!(Expression::Fragment(frag))
        },

        // Minus (considered a const negation only if it's in text or at the start of a sequence)
        Minus if is_seq_text || sequence.is_empty() => {
          whitespace!(allow);
          whitespace!(queue next);
          // Check if it's supposed to be a negative number
          if let Some((number_token, number_token_span)) = self.reader.take_where(|t| matches!(t, Some((RantToken::IntegerPositive(_) | RantToken::FloatPositive(_), _)))) {
            whitespace!(ignore prev);
            emit!(match number_token {
              RantToken::IntegerPositive(nt) => match nt {
                PositiveIntegerToken::Value(n) => match self.try_sign_unsigned_int(n, true, &number_token_span) {
                  Ok(n) => Expression::Integer(n),
                  Err(_) => Expression::NothingVal
                },
                PositiveIntegerToken::OutOfRange => {
                  self.report_error(Problem::IntegerLiteralOutOfRange, &span);
                  Expression::NothingVal
                }
              },
              RantToken::FloatPositive(nt) => match nt {
                PositiveFloatToken::Value(n) => Expression::Float(-n),
                PositiveFloatToken::OutOfRange => {
                  self.report_error(Problem::FloatLiteralOutOfRange, &span);
                  Expression::NothingVal
                },
              },
              _ => unreachable!()
            });
          } else {
            whitespace!(allow);
          }
        },
        
        // Integers
        IntegerPositive(nt) => no_flags!(on {
          whitespace!(allow);
          match nt {
            PositiveIntegerToken::Value(n) => match cast::i64(n) {
              Ok(n) => Expression::Integer(n),
              Err(_) => {
                self.report_error(Problem::IntegerLiteralOutOfRange, &span);
                Expression::NothingVal
              }
            },
            PositiveIntegerToken::OutOfRange => {
              self.report_error(Problem::IntegerLiteralOutOfRange, &span);
              Expression::NothingVal
            }
          }
        }),
        
        // Floats
        FloatPositive(nt) => no_flags!(on {
          whitespace!(allow);
          match nt {
            PositiveFloatToken::Value(n) => Expression::Float(n),
            PositiveFloatToken::OutOfRange => {
              self.report_error(Problem::FloatLiteralOutOfRange, &span);
              Expression::NothingVal
            }
          }
        }),
        
        // Empty
        NothingLiteral => no_flags!(on {
          Expression::NothingVal
        }),
        
        // Verbatim string literals
        StringLiteral(s) => {
          whitespace!(allow);
          if next_print_flag == PrintFlag::Sink {
            whitespace!(ignore next);
          } else {
            is_seq_text = true;
          }
          emit!(Expression::Fragment(s))
        },
        
        // Colon can be either fragment or argument separator.
        Colon => no_flags!({
          match mode {
            SequenceParseMode::Condition => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("condition"),
              end_type: SequenceEndType::ConditionEnd,
              is_auto_hinted: is_seq_text,
              extras: None,
              next_infix_op: None,
            }),
            _ => emit_last_string!(),
          }
        }),
        
        // Semicolon can be a fragment, collection element separator, or argument separator.
        Semicolon => no_flags!({
          match mode {
            SequenceParseMode::FunctionArg => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("argument"),
              end_type: SequenceEndType::FunctionArgEndNext,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::CollectionInit => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("collection item"),
              end_type: SequenceEndType::CollectionInitDelim,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::VariableAssignment => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("variable assignment"),
              end_type: SequenceEndType::VariableAssignDelim,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::AccessorFallbackValue => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("fallback"),
              end_type: SequenceEndType::AccessorFallbackValueToDelim,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            SequenceParseMode::ParamDefaultValue => return Ok(ParsedSequence {
              sequence: sequence.with_name_str("default value"),
              end_type: SequenceEndType::ParamDefaultValueSeparator,
              is_auto_hinted: true,
              extras: None,
              next_infix_op: None,
            }),
            // If we're anywhere else, just print the semicolon like normal text
            _ => emit_last_string!(),
          }
        }),
        
        // Handle unclosed string literals as hard errors
        UnterminatedStringLiteral => {
          self.report_error(Problem::UnclosedStringLiteral, &span); 
          return Err(())
        },
        token => {          
          // Check for ordered operator
          // TODO: Split out this horror into its own function PLEASE
          if let Some(mut next_op) = Op::from_token(&token) {
            let mut op_end_type = SequenceEndType::Operator;
            loop {
              let op_precedence = next_op.precedence();
              match next_op {
                // Check for prefix operator
                Op::Prefix(prefix_op) => {
                  // Read operand for prefix op
                  let ParsedSequence {
                    sequence: operand_seq,
                    next_infix_op: operand_next_lower_infix_op,
                    is_auto_hinted: operand_is_text,
                    end_type: operand_end_type,
                    ..
                  } = self.parse_sequence_inner(mode, op_precedence)?;

                  op_end_type = operand_end_type;
                  is_seq_text |= operand_is_text;

                  // Make sure operand is not empty
                  if operand_seq.is_empty() {
                    self.report_error(Problem::MissingOperand, &span);
                  }

                  let operand = Rc::new(operand_seq);

                  let prefix_op_node = match prefix_op {
                    PrefixOp::Negate => {
                      whitespace!(allow);
                      Expression::Negate(operand)
                    },
                    PrefixOp::LogicNot => Expression::LogicNot(operand),
                  };
                  whitespace!(ignore prev);
                  emit!(prefix_op_node);

                  // Check if operand sequence signaled a lower precedence operator was read
                  // It might not be lower than the current context precedence,
                  // so we'll loop back and check that.
                  if let Some(operand_next_lower_infix_op) = operand_next_lower_infix_op {
                    next_op = operand_next_lower_infix_op;
                    continue
                  } else {
                    // Since there's no more operators it means we're done reading, so return the sequence.
                    return Ok(ParsedSequence {
                      sequence,
                      end_type: op_end_type,
                      is_auto_hinted: is_seq_text,
                      extras: None,
                      next_infix_op: None,
                    })
                  }
                },
                // Check for infix operator
                Op::Infix(infix_op) => {
                  whitespace!(ignore both);
                  // Make sure LHS is not empty
                  if sequence.is_empty() {
                    self.report_error(Problem::MissingLeftOperand, &span);
                  }
                  
                  // If this operator has lower or equal precedence, stop reading the sequence.
                  if op_precedence <= precedence {
                    break
                  }

                  // If the next operator has equal or higher precedence, replace the current sequence with the operation.
                  // The current sequence will then become the LHS, and the RHS is parsed according to the operator's precedence.

                  // Read RHS with higher precedence
                  let ParsedSequence {
                    sequence: rhs_seq,
                    next_infix_op: rhs_next_lower_infix_op,
                    is_auto_hinted: rhs_is_text,
                    end_type: rhs_end_type,
                    ..
                  } = self.parse_sequence_inner(mode, op_precedence)?;

                  op_end_type = rhs_end_type;

                  // Make sure RHS is not empty
                  if rhs_seq.is_empty() {
                    self.report_error(Problem::MissingRightOperand, &span);
                  }

                  // Take the current sequence as the LHS
                  let lhs = Rc::new(sequence);
                  let rhs = Rc::new(rhs_seq);              

                  // Produce infix operation node
                  let op_node = match infix_op {
                    InfixOp::Add => Expression::Add(lhs, rhs),
                    InfixOp::Subtract => Expression::Subtract(lhs, rhs),
                    InfixOp::Multiply => Expression::Multiply(lhs, rhs),
                    InfixOp::Divide => Expression::Divide(lhs, rhs),
                    InfixOp::Modulo => Expression::Modulo(lhs, rhs),
                    InfixOp::Power => Expression::Power(lhs, rhs),
                    InfixOp::LogicAnd => Expression::LogicAnd(lhs, rhs),
                    InfixOp::LogicOr => Expression::LogicOr(lhs, rhs),
                    InfixOp::LogicXor => Expression::LogicXor(lhs, rhs),
                    InfixOp::Equals => Expression::Equals(lhs, rhs),
                    InfixOp::NotEquals => Expression::NotEquals(lhs, rhs),
                    InfixOp::Greater => Expression::Greater(lhs, rhs),
                    InfixOp::GreatOrEqual => Expression::GreaterOrEqual(lhs, rhs),
                    InfixOp::Less => Expression::Less(lhs, rhs),
                    InfixOp::LessOrEqual => Expression::LessOrEqual(lhs, rhs),
                  };

                  // Replace current sequence with operation (containing old sequence as LHS)
                  sequence = Sequence::one(op_node, &self.info);              
                  is_seq_text |= rhs_is_text;
                  
                  // Check if RHS sequence signaled a lower precedence operator was read
                  // It might not be lower than the current context precedence,
                  // so we'll loop back and check that.
                  if let Some(rhs_next_lower_infix_op) = rhs_next_lower_infix_op {
                    next_op = rhs_next_lower_infix_op;
                    continue
                  } else {
                    // Since there's no more operators it means we're done reading, so return the sequence.
                    return Ok(ParsedSequence {
                      sequence,
                      end_type: op_end_type,
                      is_auto_hinted: is_seq_text,
                      extras: None,
                      next_infix_op: None,
                    })
                  }
                }
              }
            }

            // If the next operator has *lower* precedence, stop reading and return current sequence to the upper parser.
            // We return the operator as next_infix_op along with the sequence to tell upper parser which operator is next.
            // The upper parsers will continue to short-circuit as long as the next_infix_op's precedence is lower than theirs.
            return Ok(ParsedSequence {
              sequence,
              end_type: op_end_type,
              is_auto_hinted: is_seq_text,
              extras: None,
              next_infix_op: Some(next_op),
            })
          } else {
            match token {
              // Check if it's an unsupported keyword
              Keyword(kw) if !kw.is_valid => self.report_error(Problem::InvalidKeyword(kw.name.to_string()), &span),
              // If it's none of the above, we don't want it there
              _ => unexpected_token_error!()
            }
          }
        },
      }
      
      // Clear flag
      next_print_flag = PrintFlag::None;
    }
    
    // Reached when the whole program has been read
    // This should only get hit for top-level sequences
    
    check_dangling_printflags!();
    
    // Return the top-level sequence
    Ok(ParsedSequence {
      sequence: sequence.with_name_str(MAIN_PROGRAM_SCOPE_NAME),
      end_type: SequenceEndType::ProgramEnd,
      is_auto_hinted: is_seq_text,
      extras: None,
      next_infix_op: None,
    })
  }

  fn track_pipeval_read(&mut self, span: &Range<usize>) {
    if let Some(pipeval) = self.var_stack.get_mut(PIPE_VALUE_NAME) {
      pipeval.add_read(false);
      // Handle capturing
      if let Some((capture_frame_height, captures)) = self.capture_stack.last_mut() {
        // Variable must not exist in the current scope of the active function
        if self.var_stack.height_of(PIPE_VALUE_NAME).unwrap_or_default() < *capture_frame_height {
          captures.insert(PIPE_VALUE_NAME.into());
        }
      }
    } else {
      self.report_error(Problem::NothingToPipe, span);
    }
  }

  #[inline]
  fn parse_conditional(&mut self, expect_opening_if: bool) -> ParseResult<ParsedConditional> {
    let mut conditions = vec![];
    let mut fallback = None;
    let mut is_auto_hinted = false;

    if expect_opening_if && !self.reader.eat_kw(KW_IF) {
      self.report_expected_token_error("@if", &self.reader.last_token_span());
    }

    self.reader.skip_ws();

    // Parse main condition
    let ParsedSequence {
      sequence: cond_seq,
      end_type: cond_end_type,
      ..
    } = self.parse_sequence_inner(SequenceParseMode::Condition, PREC_SEQUENCE)?;

    match cond_end_type {
      SequenceEndType::ConditionEnd => {},
      SequenceEndType::ProgramEnd => {
        self.report_error(Problem::UnclosedCondition, &self.reader.last_token_span());
        return Err(())
      },
      _ => unreachable!()
    }

    self.reader.skip_ws();

    let ParsedBlock {
      is_auto_hinted: body_auto_hint,
      block: body_block,
    } = self.parse_block(BlockParseMode::NeedsStart)?;

    is_auto_hinted |= body_auto_hint;

    conditions.push((Rc::new(cond_seq), Rc::new(body_block)));
    
    // Parse else-ifs
    self.reader.skip_ws();            
    while self.reader.eat_kw(KW_ELSEIF) {
      self.reader.skip_ws();

      let ParsedSequence {
        sequence: cond_seq,
        end_type: cond_end_type,
        ..
      } = self.parse_sequence_inner(SequenceParseMode::Condition, PREC_SEQUENCE)?;

      match cond_end_type {
        SequenceEndType::ConditionEnd => {},
        SequenceEndType::ProgramEnd => {
          self.report_error(Problem::UnclosedCondition, &self.reader.last_token_span());
          return Err(())
        },
        _ => unreachable!()
      }

      self.reader.skip_ws();

      let ParsedBlock {
        is_auto_hinted: body_auto_hint,
        block: body_block,
      } = self.parse_block(BlockParseMode::NeedsStart)?;

      is_auto_hinted |= body_auto_hint;

      conditions.push((Rc::new(cond_seq), Rc::new(body_block)));
      self.reader.skip_ws();
    }

    // Parse else
    self.reader.skip_ws();
    if self.reader.eat_kw(KW_ELSE) {
      self.reader.skip_ws();
      if !self.reader.eat_where(|t| matches!(t, Some((RantToken::Colon, _)))) {
        self.report_error(Problem::ExpectedToken(":".to_owned()), &self.reader.last_token_span());
      }
      
      self.reader.skip_ws();

      let ParsedBlock {
        is_auto_hinted: body_auto_hint,
        block: body_block,
      } = self.parse_block(BlockParseMode::NeedsStart)?;

      is_auto_hinted |= body_auto_hint;

      fallback = Some(Rc::new(body_block));
    }

    // Emit final conditional node
    let node = Expression::Conditional {
      conditions: Rc::new(conditions),
      fallback,
    };

    Ok(ParsedConditional {
      node,
      is_auto_hinted,
    })
  }
  
  /// Parses a group or list/map/tuple initializer.
  fn parse_group_or_collection_init(&mut self, start_span: &Range<usize>) -> ParseResult<ParsedGroupOrCollection> {
    self.reader.skip_ws();
    let collection_type_token_info = self.reader.take_where(|t| matches!(t, Some((RantToken::Colon | RantToken::DoubleColon, ..))));
    self.reader.skip_ws();

    if let Some((collection_type_token, _)) = collection_type_token_info {
      match collection_type_token {
        // Is it a list?
        RantToken::Colon => {
          // Exit early on empty list
          if self.reader.eat_where(|token| matches!(token, Some((RightParen, ..)))) {
            return Ok(ParsedGroupOrCollection {
              node: Expression::ListInit(Rc::new(vec![])),
              is_collection: true,
              is_auto_hinted: false,
            })
          }
          
          let mut sequences = vec![];
          
          loop {
            self.reader.skip_ws();
            
            let ParsedSequence { sequence, end_type: seq_end, .. } = self.parse_sequence(SequenceParseMode::CollectionInit)?;
            
            match seq_end {
              SequenceEndType::CollectionInitDelim => {
                sequences.push(Rc::new(sequence));
              },
              SequenceEndType::CollectionInitEnd => {
                sequences.push(Rc::new(sequence));
                break
              },
              SequenceEndType::ProgramEnd => {
                self.report_error(Problem::UnclosedList, &super_range(start_span, &self.reader.last_token_span()));
                return Err(())
              },
              _ => unreachable!()
            }
          }

          // To allow trailing semicolons, remove the last element if its sequence is empty
          if let Some(seq) = sequences.last() {
            if seq.is_empty() {
              sequences.pop();
            }
          }

          Ok(ParsedGroupOrCollection {
            node: Expression::ListInit(Rc::new(sequences)),
            is_collection: true, 
            is_auto_hinted: false,
          })
        },
        // Is it a map?
        RantToken::DoubleColon => {
          let mut pairs = vec![];
        
          loop {
            let key_expr = match self.reader.next_solid() {
              // Allow blocks as dynamic keys
              Some((LeftParen, _)) => {
                Some(MapKeyExpr::Dynamic(Rc::new(self.parse_dynamic_key(false)?)))
              },
              // Allow getters as shorthands for both a key AND value, ONLY IF they target a variable
              Some((LeftAngle, _)) => {
                let start_span = self.reader.last_token_span();
                let ParsedAccessor {
                  nodes: mut accessor_nodes,
                  ..
                } = self.parse_accessor()?;
                let end_span = self.reader.last_token_span();
                let accessor_span = super_range(&start_span, &end_span);

                self.reader.skip_ws();
                
                // Ignore the separator since that's usually handled by the value sequence parser
                self.reader.eat(RantToken::Semicolon);

                if accessor_nodes.len() != 1 {
                  self.report_error(Problem::InvalidShorthandVariable, &accessor_span);
                  continue
                }

                let accessor = accessor_nodes.drain(..).next();
                match accessor {
                  Some(Expression::Get(getter)) => {
                    let mut is_valid_shorthand = false;
                    if getter.path.is_variable_target() {
                      if let Some(getter_var_name) = getter.path.var_name().as_ref().map(|v| v.as_str()) {
                        is_valid_shorthand = true;
                        let key = InternalString::from(getter_var_name);
                        let value_seq = Sequence::one(Expression::Get(getter), &self.info);
                        pairs.push((MapKeyExpr::Static(key), Rc::new(value_seq)));
                      }
                    }
                    
                    if !is_valid_shorthand {
                      self.report_error(Problem::InvalidShorthandVariable, &accessor_span);
                    }

                    continue
                  },
                  _ => {
                    self.report_error(Problem::InvalidShorthandVariable, &accessor_span);
                    None
                  }
                }
              },
              // Allow fragments as keys if they are valid identifiers
              Some((Fragment, span)) => {
                let key = self.reader.last_token_string();
                if !is_valid_ident(key.as_str()) {
                  self.report_error(Problem::InvalidIdentifier(key.to_string()), &span);
                }
                Some(MapKeyExpr::Static(key))
              },
              // Allow string literals as static keys
              Some((StringLiteral(s), _)) => {
                Some(MapKeyExpr::Static(s))
              },
              // End of map
              Some((RightParen, _)) => break,
              // Soft error on anything weird
              Some(_) => {
                self.report_unexpected_last_token_error();
                None
              },
              // Hard error on EOF
              None => {
                self.report_error(Problem::UnclosedMap, &super_range(start_span, &self.reader.last_token_span()));
                return Err(())
              }
            };
            
            self.reader.skip_ws();
            if !self.reader.eat_where(|tok| matches!(tok, Some((Equals, ..)))) {
              self.report_error(Problem::ExpectedToken("=".to_owned()), &self.reader.last_token_span());
              return Err(())
            }
            self.reader.skip_ws();
            let ParsedSequence { 
              sequence: value_expr, 
              end_type: value_expr_end, 
              .. 
            } = self.parse_sequence(SequenceParseMode::CollectionInit)?;
            
            if let Some(key_expr) = key_expr {
              match value_expr_end {
                SequenceEndType::CollectionInitDelim => {
                  pairs.push((key_expr, Rc::new(value_expr)));
                },
                SequenceEndType::CollectionInitEnd => {
                  pairs.push((key_expr, Rc::new(value_expr)));
                  break
                },
                SequenceEndType::ProgramEnd => {
                  self.report_error(Problem::UnclosedMap, &super_range(start_span, &self.reader.last_token_span()));
                  return Err(())
                },
                _ => unreachable!()
              }
            }
          }
          
          Ok(ParsedGroupOrCollection {
            node: Expression::MapInit(Rc::new(pairs)),
            is_collection: true,
            is_auto_hinted: false,
          })
        },
        _ => unreachable!()
      }
    } else {
      // If we end up here, it's either a tuple or expression group
      let mut sequences = vec![];

      loop {
        self.reader.skip_ws();
        
        let ParsedSequence { sequence, end_type: seq_end, is_auto_hinted, .. } = self.parse_sequence(SequenceParseMode::CollectionInit)?;
        
        match seq_end {
          SequenceEndType::CollectionInitDelim => {
            sequences.push(Rc::new(sequence));
          },
          SequenceEndType::CollectionInitEnd => {
            // If this was the only element, it's either an empty tuple or a group
            if sequences.is_empty() {
              // If the sequence we just read is empty, exit loop and generate an (empty) tuple
              if sequence.is_empty() {
                break
              }
              return Ok(ParsedGroupOrCollection {
                node: Expression::Sequence(Rc::new(sequence)),
                is_collection: false,
                is_auto_hinted,
              })
            }
            sequences.push(Rc::new(sequence));
            break
          },
          SequenceEndType::ProgramEnd => {
            // If this was the first element, we won't know whether it's a tuple yet, so give a more general error
            if sequences.is_empty() {
              self.report_error(Problem::UnclosedParens, &super_range(start_span, &self.reader.last_token_span()));
            } else {
              self.report_error(Problem::UnclosedTuple, &super_range(start_span, &self.reader.last_token_span()));
            }
            return Err(())
          },
          _ => unreachable!()
        }
      }

      // To allow trailing semicolons, remove the last element if its sequence is empty
      if let Some(seq) = sequences.last() {
        if seq.is_empty() {
          sequences.pop();
        }
      }

      Ok(ParsedGroupOrCollection {
        node: Expression::TupleInit(Rc::new(sequences)),
        is_collection: true,
        is_auto_hinted: false,
      })
    }
  }
  
  fn parse_func_params(&mut self, start_span: &Range<usize>) -> ParseResult<Vec<ParsedParameter>> {
    // List of parameter names for function
    let mut params = vec![];
    // Separate set of all parameter names to check for duplicates
    let mut params_set = HashSet::new();
    // Most recently used parameter varity in this signature
    let mut last_varity = Varity::Required;
    // Keep track of whether we've encountered any variadic params
    let mut is_sig_variadic = false;
    
    // At this point there should either be ':' or ']'
    match self.reader.next_solid() {
      // ':' means there are params to be read
      Some((Colon, _)) => {
        // Read the params
        'read_params: loop {
          self.reader.skip_ws();
          let is_auto_hinted = self.reader.eat_kw(KW_TEXT);

          match self.reader.next_solid() {
            // Regular parameter
            Some((Fragment, span)) => {              
              // We only care about verifying/recording the param if it's in a valid position
              let param_name = Identifier::new(self.reader.last_token_string());
              // Make sure it's a valid identifier
              if !is_valid_ident(param_name.as_str()) {
                self.report_error(Problem::InvalidIdentifier(param_name.to_string()), &span)
              }
              // Check for duplicates
              // I'd much prefer to store references in params_set, but that's way more annoying to deal with
              if !params_set.insert(param_name.clone()) {
                self.report_error(Problem::DuplicateParameter(param_name.to_string()), &span);
              }                
              
              // Get varity of parameter
              self.reader.skip_ws();
              let (varity, full_param_span) = 
              if let Some((varity_token, varity_span)) = self.reader.take_where(|t| matches!(t, Some((Question | Star | Plus, _)))) 
              {
                (match varity_token {
                  // Optional parameter
                  Question => Varity::Optional,
                  // Optional variadic parameter
                  Star => Varity::VariadicStar,
                  // Required variadic parameter
                  Plus => Varity::VariadicPlus,
                  _ => unreachable!()
                }, super_range(&span, &varity_span))
              } else {
                (Varity::Required, span)
              };
              
              let is_param_variadic = varity.is_variadic();
                
              // Check for varity issues
              if is_sig_variadic && is_param_variadic {
                // Soft error on multiple variadics
                self.report_error(Problem::MultipleVariadicParams, &full_param_span);
              } else if !Varity::is_valid_order(last_varity, varity) {
                // Soft error on bad varity order
                self.report_error(Problem::InvalidParamOrder(last_varity.to_string(), varity.to_string()), &full_param_span);
              }

              last_varity = varity;
              is_sig_variadic |= is_param_variadic;

              // Read default value expr on optional params
              if matches!(varity, Varity::Optional) {
                let ParsedSequence {
                  sequence: default_value_seq,
                  end_type: default_value_end_type,
                  ..
                } = self.parse_sequence(SequenceParseMode::ParamDefaultValue)?;

                let should_continue = match default_value_end_type {
                  SequenceEndType::ParamDefaultValueSeparator => true,
                  SequenceEndType::ParamDefaultValueSignatureEnd => false,
                  SequenceEndType::ProgramEnd => {
                    self.report_error(Problem::UnclosedFunctionSignature, start_span);
                    return Err(())
                  }
                  _ => unreachable!(),
                };

                let opt_param = Parameter {
                  name: param_name,
                  varity: Varity::Optional,
                  default_value_expr: (!default_value_seq.is_empty()).then(|| Rc::new(default_value_seq))
                };

                // Add parameter to list
                params.push(ParsedParameter {
                  param: opt_param,
                  span: full_param_span.end .. self.reader.last_token_span().start,
                  is_auto_hinted,
                });

                // Keep reading other params if needed
                if should_continue {
                  continue 'read_params
                } else {
                  break 'read_params
                }
              }

              // Handle other varities here...

              let param = Parameter {
                name: param_name,
                varity,
                default_value_expr: None,
              };
              
              // Add parameter to list
              params.push(ParsedParameter {
                param,
                span: full_param_span,
                is_auto_hinted,
              });
                
              // Check if there are more params or if the signature is done
              match self.reader.next_solid() {
                // ';' means there are more params
                Some((Semicolon, ..)) => {
                  continue 'read_params
                },
                // ']' means end of signature
                Some((RightBracket, ..)) => {
                  break 'read_params
                },
                // Emit a hard error on anything else
                Some((_, span)) => {
                  self.report_error(Problem::UnexpectedToken(self.reader.last_token_string().to_string()), &span);
                  return Err(())
                },
                None => {
                  self.report_error(Problem::UnclosedFunctionSignature, start_span);
                  return Err(())
                },
              }
            },
            // Error on early close
            Some((RightBracket, span)) => {
              self.report_error(Problem::MissingIdentifier, &span);
              break 'read_params
            },
            // Error on anything else
            Some((.., span)) => {
              self.report_error(Problem::InvalidIdentifier(self.reader.last_token_string().to_string()), &span)
            },
            None => {
              self.report_error(Problem::UnclosedFunctionSignature, start_span);
              return Err(())
            }
          }
        }
      },
      // ']' means there are no params-- fall through to the next step
      Some((RightBracket, _)) => {},
      // Something weird is here, emit a hard error
      Some((.., span)) => {
        self.report_error(Problem::UnexpectedToken(self.reader.last_token_string().to_string()), &span);
        return Err(())
      },
      // Nothing is here, emit a hard error
      None => {
        self.report_error(Problem::UnclosedFunctionSignature, start_span);
        return Err(())
      }
    }
      
    Ok(params)
  }
    
  /// Parses a function definition, anonymous function, or function call.
  fn parse_func_access(&mut self) -> ParseResult<ParsedFunctionAccessor> {
    let start_span = self.reader.last_token_span();
    let mut is_auto_hinted = false;
    self.reader.skip_ws();
    
    // Check if we're defining a function (with [$|% ...]) or creating a lambda (with [? ...])
    if let Some((func_access_type_token, func_access_type_span)) 
    = self.reader.take_where(|t| matches!(t, Some((Dollar | Percent | Question, ..)))) {
      match func_access_type_token {
        // Function definition
        tt @ Dollar | tt @ Percent => {
          let is_const = matches!(tt, Percent);
          // Name of variable function will be stored in
          let (func_path, _func_path_span) = self.parse_access_path(false)?;

          // Warn user if non-variable function definition is marked as a constant
          if is_const && !func_path.is_variable_target() {
            self.report_warning(Problem::NestedFunctionDefMarkedConstant, &func_access_type_span);
          }
          
          self.reader.skip_ws();

          let ((body, params, end_func_sig_span, is_auto_hinted_def), captures) = self.capture_pass(|self_| {
            // Function params
            let params = self_.parse_func_params(&start_span)?;
            let end_func_sig_span = self_.reader.last_token_span();

            self_.reader.skip_ws();
            let is_auto_hinted_def = self_.reader.eat_kw(KW_TEXT);
            self_.reader.skip_ws();
            
            // Read function body
            let body = self_.parse_func_body(&params)?;
            
            Ok((body, params, end_func_sig_span, is_auto_hinted_def))
          })?;

          // Track variable
          if func_path.is_variable_target() {
            if let Some(id) = &func_path.var_name() {
              let func_def_span = super_range(&start_span, &end_func_sig_span);
              self.track_variable(id, &func_path.mode(), is_const, is_auto_hinted_def, VarRole::Function, &func_def_span);
            }
          }
          
          Ok(ParsedFunctionAccessor {
            is_auto_hinted: false,
            node: Expression::FuncDef(FunctionDef {
              body: Rc::new(body.with_name_str(format!("[{}]", func_path).as_str())),
              path: Rc::new(func_path),
              params: Rc::new(params.into_iter().map(|pp| pp.param).collect()),
              capture_vars: Rc::new(captures),
              is_const,
            })
          })
        },
        // Lambda
        Question => {
          // Lambda params
          let params = self.parse_func_params(&start_span)?;
          self.reader.skip_ws();
          // Read function body
          let (body, captures) = self.capture_pass(|self_| self_.parse_func_body(&params))?;
          
          Ok(ParsedFunctionAccessor {
            is_auto_hinted,
            node: Expression::Lambda(LambdaExpr {
              capture_vars: Rc::new(captures),
              body: Rc::new(body.with_name_str("lambda")),
              params: Rc::new(params.into_iter().map(|pp| pp.param).collect()),
            })
          })
        },
        _ => unreachable!()
      }
    } else {
      // Function calls, both piped and otherwise
      
      // List of calls in chain. This will only contain one call if it's non-piped (chain of one).
      let mut calls: Vec<FunctionCall> = vec![];
      // Target of assignment pipe
      let mut assignment_pipe = None;
      // Flag indicating whether call is piped (has multiple chained function calls)
      let mut is_piped = false;
      // Indicates whether the last call in the chain has been parsed
      let mut is_finished = false;
      // Indicates whether the chain has any temporal calls
      let mut is_chain_temporal = false;

      // Read all calls in chain
      while !is_finished {
        self.reader.skip_ws();
        // Argument list for current call
        let mut func_args = vec![];
        // Currently tracked temporal labels
        let mut temporal_index_labels: HashMap<InternalString, usize> = Default::default();
        // Next temporal index to be consumed
        let mut cur_temporal_index: usize = 0;
        // Temporal call flag
        let mut is_temporal = false;
        // Do the user-supplied args use the pipe value?
        let mut is_pipeval_used = false;
        
        /// Reads arguments until a terminating / delimiting token is reached.
        macro_rules! parse_args {
          () => {{
            #[allow(unused_assignments)] // added because rustc whines about `spread_mode` being unused; that is a LIE
            loop {
              self.reader.skip_ws();
              let mut spread_mode = ArgumentSpreadMode::NoSpread;

              // Check for spread operators
              match self.reader.take_where(|t| matches!(t, Some((Star | DoubleStar | TemporalLabeled(_), ..)))) {
                // Parametric spread
                Some((Star, ..)) => {
                  self.reader.skip_ws();
                  spread_mode = ArgumentSpreadMode::Parametric;
                },
                // Unlabeled temporal spread
                Some((DoubleStar, ..)) => {
                  is_temporal = true;
                  self.reader.skip_ws();
                  let is_complex = self.reader.eat(RantToken::Star);
                  self.reader.skip_ws();
                  spread_mode = ArgumentSpreadMode::Temporal { label: cur_temporal_index, is_complex };
                  cur_temporal_index += 1;
                },
                // Labeled temporal spread
                Some((TemporalLabeled(label_str), ..)) => {
                  is_temporal = true;
                  self.reader.skip_ws();
                  let is_complex = self.reader.eat(RantToken::Star);
                  self.reader.skip_ws();
                  let label_index = if let Some(label_index) = temporal_index_labels.get(&label_str) {
                    *label_index
                  } else {
                    let label_index = cur_temporal_index;
                    temporal_index_labels.insert(label_str.clone(), label_index);
                    cur_temporal_index += 1;
                    label_index
                  };
                  spread_mode = ArgumentSpreadMode::Temporal { label: label_index, is_complex };
                },
                Some(_) => unreachable!(),
                None => {},
              }

              // Parse argument
              let ParsedSequence {
                sequence: arg_seq,
                end_type: arg_end,
                ..
              } = if is_piped {
                self.var_stack.push_layer();
                // Track pipe value inside arguement scope
                let pipeval_stats = VarStats::new(VarRole::PipeValue, Default::default(), true);
                self.var_stack.define(Identifier::from(PIPE_VALUE_NAME), pipeval_stats);
                let parsed_arg_expr = self.parse_sequence_inner(SequenceParseMode::FunctionArg, PREC_SEQUENCE)?;
                is_pipeval_used |= self.var_stack.get(PIPE_VALUE_NAME).unwrap().reads > 0;
                self.analyze_top_vars();
                self.var_stack.pop_layer();
                parsed_arg_expr
              } else {
                self.parse_sequence(SequenceParseMode::FunctionArg)?
              };

              let arg = ArgumentExpr {
                expr: Rc::new(arg_seq),
                spread_mode,
              };
              func_args.push(arg);
              match arg_end {
                SequenceEndType::FunctionArgEndNext => continue,
                SequenceEndType::FunctionArgEndBreak => {
                  is_finished = true;
                  break
                },
                SequenceEndType::FunctionArgEndToPipe => {
                  is_piped = true;
                  break
                },
                SequenceEndType::FunctionArgEndToAssignPipe => {
                  let assign_pipe_start_span = self.reader.last_token_span();
                  self.reader.skip_ws();
                  let is_auto_hinted_def = self.reader.eat_kw(KW_TEXT);
                  let auto_hint_span = self.reader.last_token_span();
                  self.reader.skip_ws();

                  let (is_def, is_const_def) = if let Some((def_token, ..)) 
                  = self.reader.take_where(|t| matches!(t, Some((Dollar | Percent, ..)))) {
                    match def_token {
                      // Variable declaration
                      Dollar => (true, false),
                      // Constant declaration
                      Percent => (true, true),
                      _ => unreachable!()
                    }
                  } else {
                    (false, false)
                  };
                  self.reader.skip_ws();

                  // Error on @text when accessor isn't a definition
                  if !is_def && is_auto_hinted_def {
                    self.report_unexpected_token_error(&auto_hint_span);
                  }
                  
                  assignment_pipe = Some(Rc::new(if is_def {
                    let access_mode = self.parse_access_mode();
                    self.reader.skip_ws();
                    let ident = self.parse_ident()?;
                    let ident_span = self.reader.last_token_span();

                    if is_temporal {
                      // Don't allow constant definitions in temporal context
                      if is_const_def {
                        self.report_error(Problem::TemporalAssignPipeRedefinesConstant(ident.to_string()), &super_range(&assign_pipe_start_span, &ident_span));
                      } else {
                        self.report_warning(Problem::TemporalAssignPipeRedefinesVariable(ident.to_string()), &super_range(&assign_pipe_start_span, &ident_span));
                      }
                    }
                    
                    AssignmentPipeTarget::Def { ident, is_const: is_const_def, access_mode }
                  } else {
                    let (path, path_span) = self.parse_access_path(true)?;
                    
                    // Don't allow assignment to known anonymous values
                    if path.is_anonymous_target() {
                      self.report_error(Problem::AnonValueAssignment, &path_span);
                    }

                    self.track_variable_access(&path, true, false, &path_span, None);

                    AssignmentPipeTarget::Set(Rc::new(path))
                  }));

                  self.reader.skip_ws();

                  // We expect the function accessor to end here
                  if !self.reader.eat(RantToken::RightBracket) {
                    self.report_expected_token_error("]", &self.reader.last_token_span());
                  }

                  is_piped = true;
                  is_finished = true;
                  is_auto_hinted = false;
                  break
                },
                SequenceEndType::ProgramEnd => {
                  self.report_error(Problem::UnclosedFunctionCall, &self.reader.last_token_span());
                  return Err(())
                },
                _ => unreachable!()
              }
            }
          }}
        }
        
        /// If the pipe value wasn't used, inserts it as the first argument.
        macro_rules! fallback_pipe {
          () => {
            if calls.len() > 0 && !is_pipeval_used {
              let arg = ArgumentExpr {
                expr: Rc::new(Sequence::one(Expression::PipeValue, &self.info)),
                spread_mode: ArgumentSpreadMode::NoSpread,
              };
              func_args.insert(0, arg);
            }
          }
        }
        
        self.reader.skip_ws();

        let old_pipeval = if is_piped {
          let old = self.var_stack.remove(PIPE_VALUE_NAME);
          self.var_stack.define(PIPE_VALUE_NAME.into(), VarStats::new(VarRole::PipeValue, Default::default(), true));
          old
        } else {
          None
        };

        // Read function access path
        let (func_path, func_path_span) = self.parse_access_path(true)?;
        
        if let Some(pipeval_stats) = self.var_stack.get(PIPE_VALUE_NAME) {
          is_pipeval_used |= pipeval_stats.reads > 0;
        }
        
        if let Some((token, _)) = self.reader.next_solid() {
          match token {
            // No args, fall through
            RightBracket => {
              is_finished = true;
            },
            // Parse arguments
            Colon => parse_args!(),
            // Pipe without args
            PipeOp => {
              is_piped = true;
            }
            _ => {
              self.report_unexpected_last_token_error();
              return Err(())
            }
          }

          fallback_pipe!();
          
          // Record access to function
          self.track_variable_access(&func_path, false, false, &func_path_span, Some(&mut is_auto_hinted));
          
          // Create final node for function call
          let fcall = FunctionCall {
            target: FunctionCallTarget::Path(Rc::new(func_path)),
            arguments: Rc::new(func_args),
            is_temporal,
          };

          calls.push(fcall);
        } else {
          // Found EOF instead of end of function call, emit hard error
          self.report_error(Problem::UnclosedFunctionCall, &self.reader.last_token_span());
          return Err(())
        }

        if is_piped {
          self.var_stack.remove(PIPE_VALUE_NAME);
          if let Some(old_pipeval) = old_pipeval {
            self.var_stack.define(PIPE_VALUE_NAME.into(), old_pipeval);
          }
        }

        is_chain_temporal |= is_temporal;
      }

      // Return the finished node
      Ok(ParsedFunctionAccessor {
        is_auto_hinted,
        node: if is_piped {
          Expression::PipedCall(PipedCall {
            is_temporal: is_chain_temporal,
            steps: Rc::new(calls),
            assignment_pipe,
          })
        } else {
          Expression::FuncCall(calls.drain(..).next().unwrap())
        },
      })
    }
  }
    
  #[inline]
  fn parse_access_mode(&mut self) -> VarAccessMode {    
    if let Some((token, _span)) = self.reader.take_where(
      |t| matches!(t, Some((Slash | Caret, _)))

    ) {
      match token {
        // Accessor is explicit global
        Slash => {
          VarAccessMode::ExplicitGlobal
        },
        // Accessor is for parent scope (descope operator)
        Caret => {
          let mut descope_count = 1;
          loop {
            if !self.reader.eat_where(|t| matches!(t, Some((Caret, _)))) {
              break VarAccessMode::Descope(descope_count)
            }
            descope_count += 1;
          }
        },
        _ => unreachable!()
      }
    } else {
      VarAccessMode::Local
    }
  }
  
  /// Parses an access path.
  #[inline]
  fn parse_access_path(&mut self, allow_anonymous: bool) -> ParseResult<(AccessPath, Range<usize>)> {
    self.reader.skip_ws();
    let mut idparts = vec![];
    let start_span = self.reader.last_token_span();

    // Check for global/descope specifiers
    let access_mode = self.parse_access_mode();

    self.reader.skip_ws();

    // Parse the first part of the path
    // Check for certain index/slice forms and disallow them
    if let Ok(Some(_)) = self.try_read_signed_int() {
      let span = self.reader.last_token_span();
      self.reader.skip_ws();
      if self.reader.eat_where(|t| matches!(t, Some((DoubleDot, ..)))) {
        self.report_error(Problem::AccessPathStartsWithSlice, &super_range(&span, &self.reader.last_token_span()));
      } else {
        self.report_error(Problem::AccessPathStartsWithIndex, &span);
      }
    } else {        
      match self.reader.next() {
        // Variable name
        Some((Fragment, span)) => {
          let varname = Identifier::new(self.reader.last_token_string());
          if is_valid_ident(varname.as_str()) {
            idparts.push(AccessPathComponent::Name(varname));
          } else {
            self.report_error(Problem::InvalidIdentifier(varname.to_string()), &span);
          }
        },
        // Pipeval can be directly accessed here too
        Some((PipeValue, span)) => {
          idparts.push(AccessPathComponent::PipeValue);
          self.track_pipeval_read(&span);
        }
        // An expression can also be used to provide the variable
        Some((LeftParen, _)) if allow_anonymous => {
          let anon_value_expr = self.parse_dynamic_key(false)?;
          idparts.push(AccessPathComponent::Expression(Rc::new(anon_value_expr)));
        },
        // First path part can't be a slice
        Some((DoubleDot, span)) => {
          let _ = self.try_read_signed_int();
          self.report_error(Problem::AccessPathStartsWithSlice, &super_range(&span, &self.reader.last_token_span()));
        },
        Some((.., span)) => {
          self.report_error(Problem::InvalidIdentifier(self.reader.last_token_string().to_string()), &span);
        },
        None => {
          self.report_error(Problem::MissingIdentifier, &start_span);
          return Err(())
        }
      }
    }
    
    // Parse the rest of the path
    loop {
      // We expect a '/' between each component, so check for that first.
      // If it's anything else, terminate the path and return it.
      self.reader.skip_ws();
      if self.reader.eat_where(|t| matches!(t, Some((Slash, ..)))) {
        // From here we expect to see either another key (fragment) or index (integer).
        // If it's anything else, return a syntax error.
        self.reader.skip_ws();
        match self.try_read_signed_int() {
          // Index or slice with static from-bound
          Ok(Some(i)) => {
            self.reader.skip_ws();
            // Look for a slice separator to see if it's a slice
            if self.reader.eat_where(|t| matches!(t, Some((DoubleDot, ..)))) {
              self.reader.skip_ws();
              match self.try_read_signed_int() {
                // Between-slice with static from- and to-bounds
                Ok(Some(j)) => {
                  idparts.push(AccessPathComponent::Slice(SliceExpr::Between(SliceIndex::Static(i), SliceIndex::Static(j))));
                },
                Ok(None) => {
                  match self.reader.peek() {
                    // Between-slice with static from-bound + dynamic to-bound
                    Some((LeftParen, ..)) => {
                      let to_expr = Rc::new(self.parse_dynamic_key(true)?);
                      idparts.push(AccessPathComponent::Slice(SliceExpr::Between(SliceIndex::Static(i), SliceIndex::Dynamic(to_expr))));
                    },
                    // From-slice with static from-bound
                    Some((Slash | RightAngle | Equals | Question | Semicolon, ..)) => {
                      idparts.push(AccessPathComponent::Slice(SliceExpr::From(SliceIndex::Static(i))));
                    },
                    // Found something weird as the to-bound, emit an error
                    Some(_) => {
                      self.reader.next();
                      let token = self.reader.last_token_string().to_string();
                      self.report_error(Problem::InvalidSliceBound(token), &self.reader.last_token_span());
                    },
                    None => {
                      self.report_error(Problem::UnclosedAccessor, &super_range(&start_span, &self.reader.last_token_span()));
                      return Err(())
                    }
                  }
                },
                Err(()) => {}
              }
            } else {
              // No slice separator, so it's an index
              idparts.push(AccessPathComponent::Index(i));
            }
          },
          Ok(None) => {
            match self.reader.next() {
              // Key
              Some((Fragment, span)) => {
                let varname = Identifier::new(self.reader.last_token_string());
                if is_valid_ident(varname.as_str()) {
                  idparts.push(AccessPathComponent::Name(varname));
                } else {
                  self.report_error(Problem::InvalidIdentifier(varname.to_string()), &span);
                }
              },
              // Pipeval
              Some((PipeValue, _span)) => {
                idparts.push(AccessPathComponent::PipeValue);
                let pipeval_span = &self.reader.last_token_span();
                self.track_pipeval_read(pipeval_span);
              }
              // Full- or to-slice
              Some((DoubleDot, _)) => {
                self.reader.skip_ws();
                match self.try_read_signed_int() {
                  // To-slice with static bound
                  Ok(Some(to)) => {
                    idparts.push(AccessPathComponent::Slice(SliceExpr::To(SliceIndex::Static(to))));
                  },
                  Ok(None) => {
                    match self.reader.peek() {
                      // To-slice with dynamic bound
                      Some((LeftParen, ..)) => {
                        let to_expr = Rc::new(self.parse_dynamic_key(true)?);
                        idparts.push(AccessPathComponent::Slice(SliceExpr::To(SliceIndex::Dynamic(to_expr))));
                      },
                      // Full-slice
                      Some((Slash | RightAngle | Equals | Question | Semicolon, ..)) => {
                        idparts.push(AccessPathComponent::Slice(SliceExpr::Full));
                      },
                      // Found something weird as the to-bound, emit an error
                      Some(_) => {
                        self.reader.next();
                        let token = self.reader.last_token_string().to_string();
                        self.report_error(Problem::InvalidSliceBound(token), &self.reader.last_token_span());
                      },
                      None => {
                        self.report_error(Problem::UnclosedAccessor, &super_range(&start_span, &self.reader.last_token_span()));
                        return Err(())
                      }
                    }
                  },
                  Err(()) => {}
                }
              },
              // Dynamic key or slice with dynamic from-bound
              Some((LeftParen, _)) => {
                let expr = Rc::new(self.parse_dynamic_key(false)?);
                self.reader.skip_ws();
                // Look for a slice separator to see if it's a slice
                if self.reader.eat_where(|t| matches!(t, Some((DoubleDot, ..)))) {
                  self.reader.skip_ws();
                  match self.try_read_signed_int() {
                    // Between-slice with a dynamic from-bound + static to-bound
                    Ok(Some(to)) => {
                      idparts.push(AccessPathComponent::Slice(SliceExpr::Between(SliceIndex::Dynamic(expr), SliceIndex::Static(to))));
                    },
                    Ok(None) => {
                      match self.reader.peek() {
                        // Between-slice with dynamic from- + to-bounds
                        Some((LeftParen, ..)) => {
                          let to_expr = Rc::new(self.parse_dynamic_key(true)?);
                          idparts.push(AccessPathComponent::Slice(SliceExpr::Between(SliceIndex::Dynamic(expr), SliceIndex::Dynamic(to_expr))));
                        },
                        // From-slice with dynamic bound
                        Some((Slash | RightAngle | Equals | Question | Semicolon, ..)) => {
                          idparts.push(AccessPathComponent::Slice(SliceExpr::From(SliceIndex::Dynamic(expr))));
                        },
                        // Found something weird as the to-bound, emit an error
                        Some(_) => {
                          self.reader.next();
                          let token = self.reader.last_token_string().to_string();
                          self.report_error(Problem::InvalidSliceBound(token), &self.reader.last_token_span());
                        },
                        None => {
                          self.report_error(Problem::UnclosedAccessor, &super_range(&start_span, &self.reader.last_token_span()));
                          return Err(())
                        }
                      }
                    },
                    Err(()) => {}
                  }
                } else {
                  // No slice separator, so it's an dynamic key
                  idparts.push(AccessPathComponent::Expression(expr));
                }
              },
              Some((.., span)) => {
                // error
                self.report_error(Problem::InvalidIdentifier(self.reader.last_token_string().to_string()), &span);
              },
              None => {
                self.report_error(Problem::MissingIdentifier, &self.reader.last_token_span());
                return Err(())
              }
            }
          },
          Err(()) => {}
        }
        
      } else {
        return Ok((AccessPath::new(idparts, access_mode), start_span.start .. self.reader.last_token_span().start))
      }
    }
  }
    
  /// Parses a dynamic key.
  fn parse_dynamic_key(&mut self, expect_opening_paren: bool) -> ParseResult<Sequence> {
    if expect_opening_paren && !self.reader.eat_where(|t| matches!(t, Some((LeftParen, _)))) {
      self.report_expected_token_error("(", &self.reader.last_token_span());
      return Err(())
    }
    
    let start_span = self.reader.last_token_span();
    let ParsedSequence { sequence, end_type, .. } = self.parse_sequence(SequenceParseMode::DynamicKey)?;
    
    match end_type {
      SequenceEndType::DynamicKeyEnd => {},
      SequenceEndType::ProgramEnd => {
        // Hard error if block isn't closed
        let err_span = start_span.start .. self.source.len();
        self.report_error(Problem::UnclosedParens, &err_span);
        return Err(())
      },
      _ => unreachable!()
    }
    
    Ok(sequence)
  }

  /// Parses a function body and DOES NOT capture variables.
  fn parse_func_body(&mut self, params: &Vec<ParsedParameter>) -> ParseResult<Sequence> {
    self.reader.skip_ws();

    if !self.reader.eat_where(|t| matches!(t, Some((LeftBrace, _)))) {
      self.report_error(Problem::ExpectedToken("{".to_owned()), &self.reader.last_token_span());
      return Err(())
    }

    let start_span = self.reader.last_token_span();

    // Define each parameter as a variable in the current var_stack frame so they are not accidentally captured
    for ParsedParameter {
      param,
      span,
      is_auto_hinted
    } in params {
      self.var_stack.define(param.name.clone(), VarStats {
        reads: 0,
        writes: 1,
        def_span: span.clone(),
        is_const: true,
        is_auto_hinted: *is_auto_hinted,
        has_fallible_read: false,
        role: if param.is_optional() && param.default_value_expr.is_none() {
          VarRole::FallibleOptionalArgument
        } else { 
          VarRole::Argument 
        }
      });
    }

    // parse_sequence_inner() is used here so that the new stack frame can be customized before use
    let ParsedSequence { 
      sequence, 
      end_type, 
      .. 
    } = self.parse_sequence_inner(SequenceParseMode::FunctionBodyBlock, PREC_SEQUENCE)?;

    match end_type {
      SequenceEndType::FunctionBodyEnd => {},
      SequenceEndType::ProgramEnd => {
        let err_span = start_span.start .. self.source.len();
        self.report_error(Problem::UnclosedFunctionBody, &err_span);
        return Err(())
      },
      _ => unreachable!()
    }

    Ok(sequence)
  }
  
  fn capture_pass<T>(&mut self, parse_func: impl FnOnce(&mut Self) -> ParseResult<T>) -> ParseResult<(T, Vec<Identifier>)> {
    // Since we're about to push another var_stack frame, we can use the current depth of var_stack as the index
    let capture_height = self.var_stack.depth();

    // Push a new capture frame
    self.capture_stack.push((capture_height, Default::default()));

    // Push a new variable frame
    self.var_stack.push_layer();

    // Call parse_func
    let parse_out = parse_func(self)?;

    // Run static analysis on variable/param usage
    self.analyze_top_vars();

    self.var_stack.pop_layer();

    // Pop the topmost capture frame and grab the set of captures
    let (_, mut capture_set) = self.capture_stack.pop().unwrap();

    Ok((parse_out, capture_set.drain().collect()))
  }
    
  /// Parses a standard (attribute-consuming) block.
  fn parse_block(&mut self, parse_mode: BlockParseMode) -> ParseResult<ParsedBlock> {
    let protection = match parse_mode {
      BlockParseMode::NeedsStart => {
        self.reader.skip_ws();
        let protection = if self.reader.eat(RantToken::At) {
          Some(BlockProtection::Outer)
        } else {
          None
        };

        if !self.reader.eat_where(|t| matches!(t, Some((LeftBrace, _)))) {
          self.report_error(Problem::ExpectedToken("{".to_owned()), &self.reader.last_token_span());
          return Err(())
        }

        protection
      },
      BlockParseMode::StartParsed(protection) => protection,
    };
    
    // Get position of starting brace for error reporting
    let start_pos = self.reader.last_token_pos();
    // Keeps track of inherited hinting
    let mut auto_hint = false;
    // Is the block weighted?
    let mut is_weighted = false;
    // Block content
    let mut elements = vec![];
    
    loop {
      self.reader.skip_ws();

      self.var_stack.push_layer();

      // Check for @edit
      let modifier = if self.reader.eat_kw(KW_EDIT) {
        self.reader.skip_ws();
        let input_id = if self.reader.eat(RantToken::Colon) {
          None // No input variable
        } else {
          // Get the name of the input variable
          let input_id = self.parse_ident()?;
          let input_id_span = self.reader.last_token_span();
          self.reader.skip_ws();
          if !self.reader.eat(RantToken::Colon) {
            self.report_expected_token_error(":", &self.reader.last_token_span());
          }
          self.var_stack.define(input_id.clone(), VarStats::new(VarRole::Normal, input_id_span, true));
          Some(input_id)
        };        
        self.reader.skip_ws();

        Some(OutputModifierSig {
          input_var: input_id
        })
      } else {
        None
      };

      let ParsedSequence { 
        sequence, 
        end_type, 
        is_auto_hinted, 
        extras ,
        ..
      } = self.parse_sequence_inner(SequenceParseMode::BlockElement, PREC_SEQUENCE)?;

      self.analyze_top_vars();
      self.var_stack.pop_layer();
      
      auto_hint |= is_auto_hinted;

      let element = Rc::new(BlockElement {
        main: Rc::new(sequence),
        weight: if let Some(ParsedSequenceExtras::WeightedBlockElement { weight_expr }) = extras {
          is_weighted = true;
          // Optimize constant weights
          Some(match (weight_expr.len(), weight_expr.first().map(Rc::as_ref)) {
            (1, Some(Expression::Integer(n))) => BlockWeight::Constant(*n as f64),
            (1, Some(Expression::Float(n))) => BlockWeight::Constant(*n),
            _ => BlockWeight::Dynamic(weight_expr)
          })
        } else {
          None
        },
        output_modifier: modifier,
      });
      
      match end_type {
        SequenceEndType::BlockDelim => {
          elements.push(element);
        },
        SequenceEndType::BlockEnd => {
          elements.push(element);
          break
        },
        SequenceEndType::ProgramEnd => {
          // Hard error if block isn't closed
          let err_span = start_pos .. self.source.len();
          self.report_error(Problem::UnclosedBlock, &err_span);
          return Err(())
        },
        _ => unreachable!()
      }
    }
    
    Ok(ParsedBlock {
      block: Block::new(is_weighted, protection, elements),
      is_auto_hinted: auto_hint
    })
  }
  
  /// Parses an identifier.
  fn parse_ident(&mut self) -> ParseResult<Identifier> {
    if let Some((token, span)) = self.reader.next_solid() {
      match token {
        Fragment => {
          let idstr = self.reader.last_token_string();
          if !is_valid_ident(idstr.as_str()) {
            self.report_error(Problem::InvalidIdentifier(idstr.to_string()), &span);
          }
          Ok(Identifier::new(idstr))
        },
        _ => {
          self.report_unexpected_last_token_error();
          Err(())
        }
      }
    } else {
      self.report_error(Problem::MissingIdentifier, &self.reader.last_token_span());
      Err(())
    }
  }

  #[inline]
  fn track_variable(&mut self, id: &Identifier, access_kind: &VarAccessMode, is_const: bool, is_auto_hinted: bool, role: VarRole, def_span: &Range<usize>) {
    // Check if there's already a variable with this name
    let (prev_tracker, requested_depth, found_depth) = match access_kind {        
      VarAccessMode::Local => {
        (self.var_stack.get(id), 0, self.var_stack.depth_of(id))
      },
      VarAccessMode::Descope(n) => {
        let (v, d) = self.var_stack
          .get_parent_depth(id, *n)
          .map(|(v, d)| (Some(v), Some(d)))
          .unwrap_or_default();
        (v, *n, d)
      },
      VarAccessMode::ExplicitGlobal => {
        let rd = self.var_stack.depth();
        let (v, d) = self.var_stack
          .get_parent_depth(id, rd)
          .map(|(v, d)| (Some(v), Some(d)))
          .unwrap_or_default();
        (v, rd, d)
      },
    };

    // Check for constant redef
    if let Some(prev_tracker) = prev_tracker {
      if prev_tracker.is_const && found_depth == Some(requested_depth) {
        self.report_error(Problem::ConstantRedefinition(id.to_string()), def_span);
      }
    }

    // Create variable tracking info
    let v = VarStats {
      writes: 0,
      reads: 0,
      def_span: def_span.clone(),
      has_fallible_read: false,
      is_const,
      is_auto_hinted,
      role,
    };

    // Add to stack
    match access_kind {
      VarAccessMode::Local => {
        self.var_stack.define(id.clone(), v);
      },
      VarAccessMode::Descope(n) => {
        self.var_stack.define_parent(id.clone(), v, *n);
      },
      VarAccessMode::ExplicitGlobal => {
        self.var_stack.define_parent(id.clone(), v, self.var_stack.depth());
      },
    }
  }

  #[inline]
  fn track_variable_access(&mut self, path: &AccessPath, is_write: bool, has_fallback: bool, span: &Range<usize>, out_auto_hinted_read: Option<&mut bool>) {
    let mut out_auto_hinted_read_value = false;

    // Handle access stats
    if let Some(id) = &path.var_name() {
      let tracker = match path.mode() {
        VarAccessMode::Local => {
          self.var_stack.get_mut(id)
        },
        VarAccessMode::Descope(n) => {
          self.var_stack.get_parent_mut(id, n)
        },
        VarAccessMode::ExplicitGlobal => {
          self.var_stack.get_parent_mut(id, self.var_stack.depth())
        }
      };

      // Update tracker
      if let Some(tracker) = tracker {
        if is_write {
          tracker.writes += 1;

          if tracker.is_const {
            self.report_error(Problem::ConstantReassignment(id.to_string()), span);
          }
        } else {
          tracker.add_read(!has_fallback);
          out_auto_hinted_read_value = tracker.is_auto_hinted;

          // Error if user is accessing a fallible optional argument without a fallback
          if tracker.has_fallible_read && tracker.role == VarRole::FallibleOptionalArgument {
            self.report_error(Problem::FallibleOptionalArgAccess(id.to_string()), span);
          }
        }
      }
    }

    if let Some(auto_hinted_read) = out_auto_hinted_read {
      *auto_hinted_read = out_auto_hinted_read_value;
    }
    
    // Handle captures
    if path.mode().is_local() {
      // At least one capture frame must exist
      if let Some((capture_frame_height, captures)) = self.capture_stack.last_mut() {
        // Must be accessing a variable
        if let Some(id) = path.var_name() {
          // Variable must not exist in the current scope of the active function
          if self.var_stack.height_of(&id).unwrap_or_default() < *capture_frame_height {
            captures.insert(id);
          }
        }
      }
    }
  }

  #[inline]
  fn analyze_top_vars(&mut self) {
    let mut unused_vars: Vec<(String, VarRole, Range<usize>)> = vec![];

    // Can't warn inside the loop due to bOrRoWiNg RuLeS!
    // Have to store the warning contents in a vec first...
    for (id, tracker) in self.var_stack.iter_top() {
      if tracker.reads == 0 {
        unused_vars.push((id.to_string(), tracker.role, tracker.def_span.clone()));
      }
    }

    // Generate warnings
    unused_vars.sort_by(|(.., a_span), (.., b_span)| a_span.start.cmp(&b_span.start));
    for (name, role, span) in unused_vars {
      match role {
        VarRole::Normal => self.report_warning(Problem::UnusedVariable(name), &span),
        VarRole::Argument | VarRole::FallibleOptionalArgument => self.report_warning(Problem::UnusedParameter(name), &span),
        VarRole::Function => self.report_warning(Problem::UnusedFunction(name), &span),
        // Ignore any other roles
        _ => {},
      }
    }
  }
    
  /// Parses one or more accessors (getter/setter/definition).
  #[inline(always)]
  fn parse_accessor(&mut self) -> ParseResult<ParsedAccessor> {
    let mut accessors = vec![];
    let mut is_auto_hinted_accessor = false;

    macro_rules! add_accessor {
      ($rst:expr) => {{
        let rst = $rst;
        accessors.push(rst);
      }}
    }
    
    'read: loop {      
      self.reader.skip_ws();
      let is_auto_hinted_def = self.reader.eat_kw(KW_TEXT);
      let auto_hint_span = self.reader.last_token_span();
      self.reader.skip_ws();

      // Check if the accessor ends here as long as there's at least one component
      if !accessors.is_empty() && self.reader.eat_where(|t| matches!(t, Some((RightAngle, ..)))) {
        break
      }
      
      let (is_def, is_const_def) = if let Some((def_token, ..)) 
      = self.reader.take_where(|t| matches!(t, Some((Dollar | Percent, ..)))) {
        match def_token {
          // Variable declaration
          Dollar => (true, false),
          // Constant declaration
          Percent => (true, true),
          _ => unreachable!()
        }
      } else {
        (false, false)
      };

      // Error on @text when accessor isn't a definition
      if !is_def && is_auto_hinted_def {
        self.report_unexpected_token_error(&auto_hint_span);
      }

      let access_start_span = self.reader.last_token_span();

      self.reader.skip_ws();
      
      // Check if it's a definition. If not, it's a getter or setter
      if is_def {
        // Check for accessor modifiers
        let access_mode = self.parse_access_mode();
        self.reader.skip_ws();
        // Read name of variable we're defining
        let var_name = self.parse_ident()?;
        let def_span = access_start_span.start .. self.reader.last_token_span().end;
        
        if let Some((token, _token_span)) = self.reader.next_solid() {
          match token {
            // End of accessor
            RightAngle => {              
              self.track_variable(&var_name, &access_mode, is_const_def, is_auto_hinted_def, VarRole::Normal, &def_span);
              add_accessor!(Expression::Define(Definition {
                is_const: is_const_def,
                access_mode,
                name: var_name,
                value: None
              }));
              break 'read
            },
            // Accessor delimiter
            Semicolon => {
              self.track_variable(&var_name, &access_mode, is_const_def, is_auto_hinted_def, VarRole::Normal, &def_span);
              add_accessor!(Expression::Define(Definition {
                is_const: is_const_def,
                access_mode,
                name: var_name,
                value: None
              }));
              continue 'read;
            },
            // Definition and assignment
            Equals => {
              self.reader.skip_ws();
              let ParsedSequence { 
                sequence: setter_expr, 
                end_type: setter_end_type, 
                .. 
              } = self.parse_sequence(SequenceParseMode::VariableAssignment)?;

              let def_span = access_start_span.start .. self.reader.last_token_span().start;
              self.track_variable(&var_name, &access_mode, is_const_def, is_auto_hinted_def, VarRole::Normal, &def_span);
              add_accessor!(Expression::Define(Definition {
                name: var_name,
                access_mode,
                is_const: is_const_def,
                value: Some(Rc::new(setter_expr)),
              }));
              
              match setter_end_type {
                SequenceEndType::VariableAssignDelim => {
                  continue 'read
                },
                SequenceEndType::VariableAccessEnd => {
                  break 'read
                },
                SequenceEndType::ProgramEnd => {
                  self.report_error(Problem::UnclosedAccessor, &self.reader.last_token_span());
                  return Err(())
                },
                _ => unreachable!()
              }
            },
            // Ran into something we don't support
            _ => {
              self.report_unexpected_last_token_error();
              return Err(())
            }
          }
        } else {
          self.report_error(Problem::UnclosedAccessor, &self.reader.last_token_span());
          return Err(())
        }
      } else {
        // Read the path to what we're accessing
        let mut is_auto_hinted_get = false;
        let (var_path, var_path_span) = self.parse_access_path(true)?;
        
        self.reader.skip_ws();
        
        if let Some((token, cur_token_span)) = self.reader.next_solid() {
          match token {
            // If we hit a '>', it's a getter
            RightAngle => {
              self.track_variable_access(&var_path, false, false, &var_path_span, Some(&mut is_auto_hinted_get));
              is_auto_hinted_accessor |= is_auto_hinted_get;
              add_accessor!(Expression::Get(Getter { path: Rc::new(var_path), fallback: None }));
              break 'read;
            },
            // If we hit a ';', it's a getter with another accessor chained after it
            Semicolon => {
              self.track_variable_access(&var_path, false, false, &var_path_span, Some(&mut is_auto_hinted_get));
              is_auto_hinted_accessor |= is_auto_hinted_get;
              add_accessor!(Expression::Get(Getter { path: Rc::new(var_path), fallback: None }));
              continue 'read;
            },
            // If we hit a `?`, it's a getter with a fallback
            Question => {
              self.reader.skip_ws();
              let ParsedSequence {
                sequence: fallback_expr,
                end_type: fallback_end_type,
                ..
              } = self.parse_sequence(SequenceParseMode::AccessorFallbackValue)?;

              self.track_variable_access(&var_path, false, true, &var_path_span, Some(&mut is_auto_hinted_get));
              is_auto_hinted_accessor |= is_auto_hinted_get;
              add_accessor!(Expression::Get(Getter { path: Rc::new(var_path), fallback: Some(Rc::new(fallback_expr)) }));

              match fallback_end_type {
                SequenceEndType::AccessorFallbackValueToDelim => continue 'read,
                SequenceEndType::AccessorFallbackValueToEnd => break 'read,
                // Error
                SequenceEndType::ProgramEnd => {
                  self.report_error(Problem::UnclosedAccessor, &cur_token_span);
                  return Err(())
                },
                _ => unreachable!()
              }
            },
            // If we hit a `=`, it's a DIRECT ASSIGNMENT setter
            Equals => {
              self.reader.skip_ws();
              let ParsedSequence {
                sequence: setter_rhs_expr,
                end_type: setter_rhs_end,
                ..
              } = self.parse_sequence(SequenceParseMode::VariableAssignment)?;
              
              let rhs_end_span = self.reader.last_token_span();
              let setter_span = super_range(&access_start_span, &rhs_end_span);

              // Don't allow setters directly on anonymous values
              if var_path.is_anonymous_target() {
                self.report_error(Problem::AnonValueAssignment, &setter_span);
              }

              self.track_variable_access(&var_path, var_path.is_variable_target(), false, &setter_span, None);
              add_accessor!(Expression::Set(Setter { path: Rc::new(var_path), value: Rc::new(setter_rhs_expr) }));

              match setter_rhs_end {
                // Accessor was terminated
                SequenceEndType::VariableAccessEnd => {                  
                  break 'read;
                },
                // Expression ended with delimiter
                SequenceEndType::VariableAssignDelim => {
                  continue 'read;
                },
                // Error
                SequenceEndType::ProgramEnd => {
                  self.report_error(Problem::UnclosedAccessor, &self.reader.last_token_span());
                  return Err(())
                },
                _ => unreachable!()
              }
            },
            // If we hit a compound assignment operator... well, it's a COMPOUND ASSIGNMENT setter!
            comp_op @ (PlusEquals | MinusEquals | StarEquals | SlashEquals | DoubleStarEquals | PercentEquals | AndEquals | VertBarEquals)  => {
              self.reader.skip_ws();
              
              let ParsedSequence {
                sequence: setter_rhs_expr,
                end_type: setter_rhs_end,
                ..
              } = self.parse_sequence(SequenceParseMode::VariableAssignment)?;
              
              let rhs_end_span = self.reader.last_token_span();
              let setter_span = super_range(&access_start_span, &rhs_end_span);

              // Don't allow setters directly on anonymous values
              if var_path.is_anonymous_target() {
                self.report_error(Problem::AnonValueAssignment, &setter_span);
              }

              self.track_variable_access(&var_path, var_path.is_variable_target(), false, &setter_span, None);

              let op_lhs = Rc::new(Sequence::one(Expression::Get(Getter { path: Rc::new(var_path.clone().add_descope(1)), fallback: None }), &self.info));
              let op_rhs = Rc::new(setter_rhs_expr);
              let setter_path = Rc::new(var_path);

              add_accessor!(Expression::Set(Setter {
                path: setter_path,
                value: Rc::new(Sequence::one(match comp_op {
                  PlusEquals => Expression::Add(op_lhs, op_rhs),
                  MinusEquals => Expression::Subtract(op_lhs, op_rhs),
                  StarEquals => Expression::Multiply(op_lhs, op_rhs),
                  SlashEquals => Expression::Divide(op_lhs, op_rhs),
                  PercentEquals => Expression::Modulo(op_lhs, op_rhs),
                  DoubleStarEquals => Expression::Power(op_lhs, op_rhs),
                  AndEquals => Expression::LogicAnd(op_lhs, op_rhs),
                  VertBarEquals => Expression::LogicOr(op_lhs, op_rhs),
                  CaretEquals => Expression::LogicXor(op_lhs, op_rhs),
                  _ => unreachable!(),
                }, &self.info))
              }));

              match setter_rhs_end {
                // Accessor was terminated
                SequenceEndType::VariableAccessEnd => {                  
                  break 'read;
                },
                // Expression ended with delimiter
                SequenceEndType::VariableAssignDelim => {
                  continue 'read;
                },
                // Error
                SequenceEndType::ProgramEnd => {
                  self.report_error(Problem::UnclosedAccessor, &self.reader.last_token_span());
                  return Err(())
                },
                _ => unreachable!()
              }
            },
            _ => {
              // Anything else is an error
              self.report_unexpected_last_token_error();
              return Err(())
            }
          }
        } else {
          self.report_error(Problem::UnclosedAccessor, &self.reader.last_token_span());
          return Err(())
        }
      }
    }
    
    Ok(ParsedAccessor {
      nodes: accessors,
      is_auto_hinted: is_auto_hinted_accessor
    })
  }

  /// Attempt to read a signed integer.
  ///
  /// Will generate a compiler error if there's a dangling sign.
  fn try_read_signed_int(&mut self) -> ParseResult<Option<i64>> {
    if let Some((token, span)) = self.reader.take_where(|t| matches!(t, Some((RantToken::Minus | RantToken::IntegerPositive(_), _)))) {
      match token {
        RantToken::IntegerPositive(nt) => match nt {
          PositiveIntegerToken::Value(n) => match self.try_sign_unsigned_int(n, false, &span) {
            Ok(i) => return Ok(Some(i)),
            Err(()) => return Err(())
          },
          PositiveIntegerToken::OutOfRange => {
            self.report_error(Problem::IntegerLiteralOutOfRange, &span);
            return Err(())
          }
        },
        RantToken::Minus => {
          self.reader.skip_ws();
          if let Some((RantToken::IntegerPositive(nt), span)) = self.reader.take_where(|t| matches!(t, Some((RantToken::IntegerPositive(_), _)))) {
            return match nt {
              PositiveIntegerToken::Value(n) => match self.try_sign_unsigned_int(n, true, &span) {
                Ok(i) => Ok(Some(i)),
                Err(()) => Err(())
              },
              PositiveIntegerToken::OutOfRange => {
                self.report_error(Problem::IntegerLiteralOutOfRange, &span);
                Err(())
              }
            }
          } else {
            self.report_expected_token_error("<integer>", &self.reader.last_token_span());
            return Err(())
          }
        }
        _ => unreachable!()
      }
    }
    Ok(None)
  }

  /// Tries to convert a unsigned 64-bit integer to a signed 64-bit integer with the specified sign.
  /// 
  /// Accounts for the minimum 64-bit integer value, whose absolute value is out of range for `i64`.
  #[inline]
  fn try_sign_unsigned_int(&mut self, value_unsigned: u64, negate: bool, span: &Range<usize>) -> ParseResult<i64> {
    match cast::i64(if negate { -1 } else { 1 } * cast::i128(value_unsigned)) {
      Ok(value_signed) => Ok(value_signed),
      Err(_) => {
        self.report_error(Problem::IntegerLiteralOutOfRange, span);
        Err(())
      }
    }
  }
}