aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/parse/rust-parse-impl-expr.hxx
blob: 306a0958d829aae64ed7f84d679b173572596473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
// Copyright (C) 2025-2026 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

/* DO NOT INCLUDE ANYWHERE - this is automatically included
 *   by rust-parse-impl.h
 * This is also the reason why there are no include guards. */

#include "rust-parse.h"

namespace Rust {

// Parses a block expression, including the curly braces at start and end.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::BlockExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_block_expr (
  AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
  location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      if (!skip_token (LEFT_CURLY))
	{
	  skip_after_end_block ();
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }

  AST::AttrVec inner_attrs = parse_inner_attributes ();

  // parse statements and expression
  std::vector<std::unique_ptr<AST::Stmt>> stmts;
  std::unique_ptr<AST::Expr> expr = nullptr;

  const_TokenPtr t = lexer.peek_token ();
  while (t->get_id () != RIGHT_CURLY)
    {
      auto expr_or_stmt = parse_stmt_or_expr ();
      if (!expr_or_stmt)
	{
	  skip_after_end_block ();
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      t = lexer.peek_token ();

      if (expr_or_stmt->stmt != nullptr)
	{
	  stmts.push_back (std::move (expr_or_stmt->stmt));
	}
      else
	{
	  // assign to expression and end parsing inside
	  expr = std::move (expr_or_stmt->expr);
	}
    }

  location_t end_locus = t->get_locus ();

  if (!skip_token (RIGHT_CURLY))
    {
      // We don't need to throw an error as it already reported by skip_token
      skip_after_end_block ();
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  // grammar allows for empty block expressions

  stmts.shrink_to_fit ();

  return std::unique_ptr<AST::BlockExpr> (
    new AST::BlockExpr (std::move (stmts), std::move (expr),
			std::move (inner_attrs), std::move (outer_attrs),
			std::move (label), locus, end_locus));
}

/* Parse an anonymous const expression. This can be a regular const expression
 * or an underscore for deferred const inference */
template <typename ManagedTokenSource>
tl::expected<AST::AnonConst, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_anon_const ()
{
  auto current = lexer.peek_token ();
  auto locus = current->get_locus ();

  // Special case deferred inference constants
  if (maybe_skip_token (UNDERSCORE))
    return AST::AnonConst (locus);

  auto expr = parse_expr ();

  if (!expr)
    return tl::make_unexpected (Parse::Error::Node{});

  return AST::AnonConst (std::move (expr.value ()), locus);
}

/* Parse a "const block", a block preceded by the `const` keyword whose
 * statements can be const evaluated and used in constant contexts */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ConstBlock>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
						    location_t locus)
{
  auto block_res = parse_block_expr ();

  if (!block_res)
    {
      add_error (Error (locus, "failed to parse inner block in const block"));
      skip_after_end_block ();

      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  auto block = std::move (block_res.value ());

  auto block_locus = block->get_locus ();

  return std::make_unique<AST::ConstBlock> (AST::AnonConst (std::move (block),
							    block_locus),
					    locus, std::move (outer_attrs));
}

/* Parses a "grouped" expression (expression in parentheses), used to control
 * precedence. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::GroupedExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
{
  location_t locus = lexer.peek_token ()->get_locus ();
  skip_token (LEFT_PAREN);

  AST::AttrVec inner_attrs = parse_inner_attributes ();

  // parse required expr inside parentheses
  auto expr_in_parens = parse_expr ();
  if (!expr_in_parens)
    {
      // skip after somewhere?
      // error?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  if (!skip_token (RIGHT_PAREN))
    {
      // skip after somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  return std::unique_ptr<AST::GroupedExpr> (
    new AST::GroupedExpr (std::move (expr_in_parens.value ()),
			  std::move (inner_attrs), std::move (outer_attrs),
			  locus));
}

// Parses a closure expression (closure definition).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ClosureExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
{
  location_t locus = lexer.peek_token ()->get_locus ();
  // detect optional "move"
  bool has_move = false;
  if (lexer.peek_token ()->get_id () == MOVE)
    {
      lexer.skip_token ();
      has_move = true;
    }

  // handle parameter list
  std::vector<AST::ClosureParam> params;

  const_TokenPtr t = lexer.peek_token ();
  switch (t->get_id ())
    {
    case OR:
      // skip token, no parameters
      lexer.skip_token ();
      break;
    case PIPE:
      // actually may have parameters
      lexer.skip_token ();
      t = lexer.peek_token ();

      while (t->get_id () != PIPE)
	{
	  AST::ClosureParam param = parse_closure_param ();
	  if (param.is_error ())
	    {
	      // TODO is this really an error?
	      Error error (t->get_locus (), "could not parse closure param");
	      add_error (std::move (error));

	      break;
	    }
	  params.push_back (std::move (param));

	  if (lexer.peek_token ()->get_id () != COMMA)
	    {
	      lexer.skip_token ();
	      // not an error but means param list is done
	      break;
	    }
	  // skip comma
	  lexer.skip_token ();

	  t = lexer.peek_token ();
	}
      params.shrink_to_fit ();
      break;
    default:
      add_error (Error (t->get_locus (),
			"unexpected token %qs in closure expression - expected "
			"%<|%> or %<||%>",
			t->get_token_description ()));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  // again branch based on next token
  t = lexer.peek_token ();
  if (t->get_id () == RETURN_TYPE)
    {
      // must be return type closure with block expr

      // skip "return type" token
      lexer.skip_token ();

      // parse actual type, which is required
      std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
      if (type == nullptr)
	{
	  // error
	  Error error (t->get_locus (), "failed to parse type for closure");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      // parse block expr, which is required
      auto block = parse_block_expr ();
      if (!block)
	{
	  // error
	  Error error (lexer.peek_token ()->get_locus (),
		       "failed to parse block expr in closure");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      return std::unique_ptr<AST::ClosureExprInnerTyped> (
	new AST::ClosureExprInnerTyped (std::move (type),
					std::move (block.value ()),
					std::move (params), locus, has_move,
					std::move (outer_attrs)));
    }
  else
    {
      // must be expr-only closure

      // parse expr, which is required
      auto expr = parse_expr ();
      if (!expr)
	{
	  Error error (t->get_locus (),
		       "failed to parse expression in closure");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      return std::unique_ptr<AST::ClosureExprInner> (
	new AST::ClosureExprInner (std::move (expr.value ()),
				   std::move (params), locus, has_move,
				   std::move (outer_attrs)));
    }
}

// Parses a literal token (to literal expression).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::LiteralExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs)
{
  // TODO: change if literal representation in lexer changes

  std::string literal_value;
  AST::Literal::LitType type = AST::Literal::STRING;

  // branch based on token
  const_TokenPtr t = lexer.peek_token ();
  switch (t->get_id ())
    {
    case CHAR_LITERAL:
      type = AST::Literal::CHAR;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    case STRING_LITERAL:
      type = AST::Literal::STRING;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    case BYTE_CHAR_LITERAL:
      type = AST::Literal::BYTE;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    case BYTE_STRING_LITERAL:
      type = AST::Literal::BYTE_STRING;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    case RAW_STRING_LITERAL:
      type = AST::Literal::RAW_STRING;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    case INT_LITERAL:
      type = AST::Literal::INT;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    case FLOAT_LITERAL:
      type = AST::Literal::FLOAT;
      literal_value = t->get_str ();
      lexer.skip_token ();
      break;
    // case BOOL_LITERAL
    // use true and false keywords rather than "bool literal" Rust terminology
    case TRUE_LITERAL:
      type = AST::Literal::BOOL;
      literal_value = Values::Keywords::TRUE_LITERAL;
      lexer.skip_token ();
      break;
    case FALSE_LITERAL:
      type = AST::Literal::BOOL;
      literal_value = Values::Keywords::FALSE_LITERAL;
      lexer.skip_token ();
      break;
    default:
      // error - cannot be a literal expr
      add_error (Error (t->get_locus (),
			"unexpected token %qs when parsing literal expression",
			t->get_token_description ()));

      // skip?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  // create literal based on stuff in switch
  return std::unique_ptr<AST::LiteralExpr> (
    new AST::LiteralExpr (std::move (literal_value), std::move (type),
			  t->get_type_hint (), std::move (outer_attrs),
			  t->get_locus ()));
}

template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::BoxExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_box_expr (AST::AttrVec outer_attrs,
					    location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (BOX);
    }

  ParseRestrictions restrictions;
  restrictions.expr_can_be_null = false;

  auto expr = parse_expr (AST::AttrVec (), restrictions);
  if (!expr)
    return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);

  return std::unique_ptr<AST::BoxExpr> (
    new AST::BoxExpr (std::move (expr.value ()), std::move (outer_attrs),
		      locus));
}

// Parses a return expression (including any expression to return).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ReturnExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_return_expr (AST::AttrVec outer_attrs,
					       location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (RETURN_KW);
    }

  // parse expression to return, if it exists
  ParseRestrictions restrictions;
  restrictions.expr_can_be_null = true;
  auto returned_expr = parse_expr (AST::AttrVec (), restrictions);
  tl::optional<std::unique_ptr<AST::Expr>> expr = tl::nullopt;
  if (returned_expr)
    expr = std::move (returned_expr.value ());

  return std::make_unique<AST::ReturnExpr> (std::move (expr),
					    std::move (outer_attrs), locus);
}

// Parses a try expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::TryExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_try_expr (AST::AttrVec outer_attrs,
					    location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (TRY);
    }

  auto block_expr = parse_block_expr ();

  if (!block_expr)
    return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);

  return std::unique_ptr<AST::TryExpr> (
    new AST::TryExpr (std::move (block_expr.value ()), std::move (outer_attrs),
		      locus));
}

/* Parses a break expression (including any label to break to AND any return
 * expression). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::BreakExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_break_expr (AST::AttrVec outer_attrs,
					      location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (BREAK);
    }

  auto parsed_label = parse_lifetime (false);
  auto label = (parsed_label)
		 ? tl::optional<AST::Lifetime> (parsed_label.value ())
		 : tl::nullopt;

  // parse break return expression if it exists
  ParseRestrictions restrictions;
  restrictions.expr_can_be_null = true;
  auto return_expr = parse_expr (AST::AttrVec (), restrictions);

  if (return_expr)
    return std::unique_ptr<AST::BreakExpr> (
      new AST::BreakExpr (std::move (label), std::move (return_expr.value ()),
			  std::move (outer_attrs), locus));
  else if (return_expr.error () == Parse::Error::Expr::NULL_EXPR)
    return std::unique_ptr<AST::BreakExpr> (
      new AST::BreakExpr (std::move (label), tl::nullopt,
			  std::move (outer_attrs), locus));
  else
    return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);
}

// Parses a continue expression (including any label to continue from).
template <typename ManagedTokenSource>
std::unique_ptr<AST::ContinueExpr>
Parser<ManagedTokenSource>::parse_continue_expr (AST::AttrVec outer_attrs,
						 location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (CONTINUE);
    }

  auto parsed_label = parse_lifetime (false);
  auto label = (parsed_label)
		 ? tl::optional<AST::Lifetime> (parsed_label.value ())
		 : tl::nullopt;

  return std::make_unique<AST::ContinueExpr> (std::move (label),
					      std::move (outer_attrs), locus);
}

/* Parses an if expression of any kind, including with else, else if, else if
 * let, and neither. Note that any outer attributes will be ignored because if
 * expressions don't support them. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::IfExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_if_expr (AST::AttrVec outer_attrs,
					   location_t pratt_parsed_loc)
{
  // TODO: make having outer attributes an error?
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      if (!skip_token (IF))
	{
	  skip_after_end_block ();
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }

  // detect accidental if let
  if (lexer.peek_token ()->get_id () == LET)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "if let expression probably exists, but is being parsed "
		   "as an if expression. This may be a parser error");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  /* parse required condition expr - HACK to prevent struct expr from being
   * parsed */
  ParseRestrictions no_struct_expr;
  no_struct_expr.can_be_struct_expr = false;
  auto condition = parse_expr ({}, no_struct_expr);
  if (!condition)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse condition expression in if expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  // parse required block expr
  auto if_body = parse_block_expr ();
  if (!if_body)
    return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);

  // branch to parse end or else (and then else, else if, or else if let)
  if (lexer.peek_token ()->get_id () != ELSE)
    {
      // single selection - end of if expression
      return std::unique_ptr<AST::IfExpr> (
	new AST::IfExpr (std::move (condition.value ()),
			 std::move (if_body.value ()), std::move (outer_attrs),
			 locus));
    }
  else
    {
      // double or multiple selection - branch on end, else if, or else if let

      // skip "else"
      lexer.skip_token ();

      // branch on whether next token is '{' or 'if'
      const_TokenPtr t = lexer.peek_token ();
      switch (t->get_id ())
	{
	case LEFT_CURLY:
	  {
	    // double selection - else
	    // parse else block expr (required)
	    auto else_body = parse_block_expr ();
	    if (!else_body)
	      {
		Error error (lexer.peek_token ()->get_locus (),
			     "failed to parse else body block expression in "
			     "if expression");
		add_error (std::move (error));

		// skip somewhere?
		return tl::unexpected<Parse::Error::Node> (
		  Parse::Error::Node::CHILD_ERROR);
	      }

	    return std::unique_ptr<AST::IfExprConseqElse> (
	      new AST::IfExprConseqElse (std::move (condition.value ()),
					 std::move (if_body.value ()),
					 std::move (else_body.value ()),
					 std::move (outer_attrs), locus));
	  }
	case IF:
	  {
	    // multiple selection - else if or else if let
	    // branch on whether next token is 'let' or not
	    if (lexer.peek_token (1)->get_id () == LET)
	      {
		// parse if let expr (required)
		auto if_let_expr = parse_if_let_expr ();
		if (!if_let_expr)
		  {
		    Error error (lexer.peek_token ()->get_locus (),
				 "failed to parse (else) if let expression "
				 "after if expression");
		    add_error (std::move (error));

		    // skip somewhere?
		    return tl::unexpected<Parse::Error::Node> (
		      Parse::Error::Node::CHILD_ERROR);
		  }

		return std::unique_ptr<AST::IfExprConseqElse> (
		  new AST::IfExprConseqElse (std::move (condition.value ()),
					     std::move (if_body.value ()),
					     std::move (if_let_expr.value ()),
					     std::move (outer_attrs), locus));
	      }
	    else
	      {
		// parse if expr (required)
		auto if_expr = parse_if_expr ();
		if (!if_expr)
		  {
		    Error error (lexer.peek_token ()->get_locus (),
				 "failed to parse (else) if expression after "
				 "if expression");
		    add_error (std::move (error));

		    // skip somewhere?
		    return tl::unexpected<Parse::Error::Node> (
		      Parse::Error::Node::CHILD_ERROR);
		  }

		return std::unique_ptr<AST::IfExprConseqElse> (
		  new AST::IfExprConseqElse (std::move (condition.value ()),
					     std::move (if_body.value ()),
					     std::move (if_expr.value ()),
					     std::move (outer_attrs), locus));
	      }
	  }
	default:
	  // error - invalid token
	  add_error (Error (t->get_locus (),
			    "unexpected token %qs after else in if expression",
			    t->get_token_description ()));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }
}

/* Parses an if let expression of any kind, including with else, else if, else
 * if let, and none. Note that any outer attributes will be ignored as if let
 * expressions don't support them. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::IfLetExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_if_let_expr (AST::AttrVec outer_attrs,
					       location_t pratt_parsed_loc)
{
  // TODO: make having outer attributes an error?
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      if (!skip_token (IF))
	{
	  skip_after_end_block ();
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }

  // detect accidental if expr parsed as if let expr
  if (lexer.peek_token ()->get_id () != LET)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "if expression probably exists, but is being parsed as an "
		   "if let expression. This may be a parser error");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }
  lexer.skip_token ();

  // parse match arm patterns (which are required)
  std::unique_ptr<AST::Pattern> match_arm_pattern
    = parse_match_arm_pattern (EQUAL);
  if (match_arm_pattern == nullptr)
    {
      Error error (
	lexer.peek_token ()->get_locus (),
	"failed to parse any match arm patterns in if let expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  if (!skip_token (EQUAL))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  // parse expression (required) - HACK to prevent struct expr being parsed
  ParseRestrictions no_struct_expr;
  no_struct_expr.can_be_struct_expr = false;
  auto scrutinee_expr = parse_expr ({}, no_struct_expr);
  if (!scrutinee_expr)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse scrutinee expression in if let expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  /* TODO: check for expression not being a struct expression or lazy boolean
   * expression here? or actually probably in semantic analysis. */

  // parse block expression (required)
  auto if_let_body = parse_block_expr ();
  if (!if_let_body)
    {
      Error error (
	lexer.peek_token ()->get_locus (),
	"failed to parse if let body block expression in if let expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  // branch to parse end or else (and then else, else if, or else if let)
  if (lexer.peek_token ()->get_id () != ELSE)
    {
      // single selection - end of if let expression
      return std::unique_ptr<AST::IfLetExpr> (new AST::IfLetExpr (
	std::move (match_arm_pattern), std::move (scrutinee_expr.value ()),
	std::move (if_let_body.value ()), std::move (outer_attrs), locus));
    }
  else
    {
      // double or multiple selection - branch on end, else if, or else if let

      // skip "else"
      lexer.skip_token ();

      // branch on whether next token is '{' or 'if'
      const_TokenPtr t = lexer.peek_token ();
      switch (t->get_id ())
	{
	case LEFT_CURLY:
	  {
	    // double selection - else
	    // parse else block expr (required)
	    auto else_body = parse_block_expr ();
	    if (!else_body)
	      {
		Error error (lexer.peek_token ()->get_locus (),
			     "failed to parse else body block expression in "
			     "if let expression");
		add_error (std::move (error));

		// skip somewhere?
		return tl::unexpected<Parse::Error::Node> (
		  Parse::Error::Node::CHILD_ERROR);
	      }

	    return std::unique_ptr<AST::IfLetExprConseqElse> (
	      new AST::IfLetExprConseqElse (std::move (match_arm_pattern),
					    std::move (scrutinee_expr.value ()),
					    std::move (if_let_body.value ()),
					    std::move (else_body.value ()),
					    std::move (outer_attrs), locus));
	  }
	case IF:
	  {
	    // multiple selection - else if or else if let
	    // branch on whether next token is 'let' or not
	    if (lexer.peek_token (1)->get_id () == LET)
	      {
		// parse if let expr (required)
		auto if_let_expr = parse_if_let_expr ();
		if (!if_let_expr)
		  {
		    Error error (lexer.peek_token ()->get_locus (),
				 "failed to parse (else) if let expression "
				 "after if let expression");
		    add_error (std::move (error));

		    // skip somewhere?
		    return tl::unexpected<Parse::Error::Node> (
		      Parse::Error::Node::CHILD_ERROR);
		  }

		return std::unique_ptr<AST::IfLetExprConseqElse> (
		  new AST::IfLetExprConseqElse (
		    std::move (match_arm_pattern),
		    std::move (scrutinee_expr.value ()),
		    std::move (if_let_body.value ()),
		    std::move (if_let_expr.value ()), std::move (outer_attrs),
		    locus));
	      }
	    else
	      {
		// parse if expr (required)
		auto if_expr = parse_if_expr ();
		if (!if_expr)
		  {
		    Error error (lexer.peek_token ()->get_locus (),
				 "failed to parse (else) if expression after "
				 "if let expression");
		    add_error (std::move (error));

		    // skip somewhere?
		    return tl::unexpected<Parse::Error::Node> (
		      Parse::Error::Node::CHILD_ERROR);
		  }

		return std::unique_ptr<AST::IfLetExprConseqElse> (
		  new AST::IfLetExprConseqElse (
		    std::move (match_arm_pattern),
		    std::move (scrutinee_expr.value ()),
		    std::move (if_let_body.value ()),
		    std::move (if_expr.value ()), std::move (outer_attrs),
		    locus));
	      }
	  }
	default:
	  // error - invalid token
	  add_error (
	    Error (t->get_locus (),
		   "unexpected token %qs after else in if let expression",
		   t->get_token_description ()));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }
}

/* TODO: possibly decide on different method of handling label (i.e. not
 * parameter) */

/* Parses a "loop" infinite loop expression. Label is not parsed and should be
 * parsed via parse_labelled_loop_expr, which would call this. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::LoopExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_loop_expr (AST::AttrVec outer_attrs,
					     tl::optional<AST::LoopLabel> label,
					     location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      if (label)
	locus = label->get_locus ();
      else
	locus = lexer.peek_token ()->get_locus ();

      if (!skip_token (LOOP))
	{
	  skip_after_end_block ();
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }
  else
    {
      if (label)
	locus = label->get_locus ();
    }

  // parse loop body, which is required
  auto loop_body = parse_block_expr ();
  if (!loop_body)
    return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::CHILD_ERROR);

  return std::unique_ptr<AST::LoopExpr> (
    new AST::LoopExpr (std::move (loop_body.value ()), locus, std::move (label),
		       std::move (outer_attrs)));
}

/* Parses a "while" loop expression. Label is not parsed and should be parsed
 * via parse_labelled_loop_expr, which would call this. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::WhileLoopExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_while_loop_expr (
  AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
  location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      if (label)
	locus = label->get_locus ();
      else
	locus = lexer.peek_token ()->get_locus ();

      if (!skip_token (WHILE))
	{
	  skip_after_end_block ();
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }
  else
    {
      if (label)
	locus = label->get_locus ();
    }

  // ensure it isn't a while let loop
  if (lexer.peek_token ()->get_id () == LET)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "appears to be while let loop but is being parsed by "
		   "while loop - this may be a compiler issue");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  // parse loop predicate (required) with HACK to prevent struct expr parsing
  ParseRestrictions no_struct_expr;
  no_struct_expr.can_be_struct_expr = false;
  auto predicate = parse_expr ({}, no_struct_expr);
  if (!predicate)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse predicate expression in while loop");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  /* TODO: check that it isn't struct expression here? actually, probably in
   * semantic analysis */

  // parse loop body (required)
  auto body = parse_block_expr ();
  if (!body)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse loop body block expression in while loop");
      add_error (std::move (error));

      // skip somewhere
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  return std::unique_ptr<AST::WhileLoopExpr> (
    new AST::WhileLoopExpr (std::move (predicate.value ()),
			    std::move (body.value ()), locus, std::move (label),
			    std::move (outer_attrs)));
}

/* Parses a "while let" loop expression. Label is not parsed and should be
 * parsed via parse_labelled_loop_expr, which would call this. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::WhileLetLoopExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_while_let_loop_expr (
  AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label)
{
  location_t locus = UNKNOWN_LOCATION;
  if (label)
    locus = label->get_locus ();
  else
    locus = lexer.peek_token ()->get_locus ();
  maybe_skip_token (WHILE);

  /* check for possible accidental recognition of a while loop as a while let
   * loop */
  if (lexer.peek_token ()->get_id () != LET)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "appears to be a while loop but is being parsed by "
		   "while let loop - this may be a compiler issue");
      add_error (std::move (error));

      // skip somewhere
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }
  // as this token is definitely let now, save the computation of comparison
  lexer.skip_token ();

  // parse predicate patterns
  std::unique_ptr<AST::Pattern> predicate_pattern
    = parse_match_arm_pattern (EQUAL);
  // ensure that there is at least 1 pattern
  if (predicate_pattern == nullptr)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "should be at least 1 pattern");
      add_error (std::move (error));
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  if (!skip_token (EQUAL))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  /* parse predicate expression, which is required (and HACK to prevent struct
   * expr) */
  ParseRestrictions no_struct_expr;
  no_struct_expr.can_be_struct_expr = false;
  auto predicate_expr = parse_expr ({}, no_struct_expr);
  if (!predicate_expr)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse predicate expression in while let loop");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  /* TODO: ensure that struct expression is not parsed? Actually, probably in
   * semantic analysis. */

  // parse loop body, which is required
  auto body = parse_block_expr ();
  if (!body)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse block expr (loop body) of while let loop");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  return std::unique_ptr<AST::WhileLetLoopExpr> (
    new AST::WhileLetLoopExpr (std::move (predicate_pattern),
			       std::move (predicate_expr.value ()),
			       std::move (body.value ()), locus,
			       std::move (label), std::move (outer_attrs)));
}

/* Parses a "for" iterative loop. Label is not parsed and should be parsed via
 * parse_labelled_loop_expr, which would call this. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ForLoopExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_for_loop_expr (
  AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label)
{
  location_t locus = UNKNOWN_LOCATION;
  if (label)
    locus = label->get_locus ();
  else
    locus = lexer.peek_token ()->get_locus ();
  maybe_skip_token (FOR);

  // parse pattern, which is required
  std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
  if (!pattern)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse iterator pattern in for loop");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  if (!skip_token (IN))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  /* parse iterator expression, which is required - also HACK to prevent
   * struct expr */
  ParseRestrictions no_struct_expr;
  no_struct_expr.can_be_struct_expr = false;
  auto expr = parse_expr ({}, no_struct_expr);
  if (!expr)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse iterator expression in for loop");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  // TODO: check to ensure this isn't struct expr? Or in semantic analysis.

  // parse loop body, which is required
  auto body = parse_block_expr ();
  if (!body)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse loop body block expression in for loop");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  return std::unique_ptr<AST::ForLoopExpr> (
    new AST::ForLoopExpr (std::move (pattern), std::move (expr.value ()),
			  std::move (body.value ()), locus, std::move (label),
			  std::move (outer_attrs)));
}

// Parses a loop expression with label (any kind of loop - disambiguates).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
						      AST::AttrVec outer_attrs)
{
  // parse loop label (required)
  auto parsed_label = parse_loop_label (tok);
  if (!parsed_label)
    {
      /* TODO: decide whether it should not work if there is no label, or parse
       * it with no label at the moment, I will make it not work with no label
       * because that's the implication. */

      if (parsed_label.error ().kind
	  == Parse::Error::LoopLabel::Kind::NOT_LOOP_LABEL)
	{
	  Error error (tok->get_locus (),
		       "expected lifetime in labelled loop expr (to parse loop "
		       "label) - found %qs",
		       tok->get_token_description ());
	  add_error (std::move (error));
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      else
	{
	  Error error (lexer.peek_token ()->get_locus (),
		       "failed to parse loop label in labelled loop expr");
	  add_error (std::move (error));

	  // skip?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}
    }

  auto label = parsed_label
		 ? tl::optional<AST::LoopLabel> (parsed_label.value ())
		 : tl::nullopt;

  // branch on next token
  const_TokenPtr t = lexer.peek_token ();
  switch (t->get_id ())
    {
    case LOOP:
      return parse_loop_expr (std::move (outer_attrs), std::move (label));
    case FOR:
      return parse_for_loop_expr (std::move (outer_attrs), std::move (label));
    case WHILE:
      // further disambiguate into while vs while let
      if (lexer.peek_token (1)->get_id () == LET)
	return parse_while_let_loop_expr (std::move (outer_attrs),
					  std::move (label));
      else
	return parse_while_loop_expr (std::move (outer_attrs),
				      std::move (label));
    case LEFT_CURLY:
      return parse_block_expr (std::move (outer_attrs), std::move (label));
    default:
      // error
      add_error (Error (t->get_locus (),
			"unexpected token %qs when parsing labelled loop",
			t->get_token_description ()));

      // skip?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
}

// Parses a match expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::MatchExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_match_expr (AST::AttrVec outer_attrs,
					      location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (MATCH_KW);
    }

  /* parse scrutinee expression, which is required (and HACK to prevent struct
   * expr) */
  ParseRestrictions no_struct_expr;
  no_struct_expr.can_be_struct_expr = false;
  auto scrutinee = parse_expr ({}, no_struct_expr);
  if (!scrutinee)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse scrutinee expression in match expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  /* TODO: check for scrutinee expr not being struct expr? or do so in
   * semantic analysis */

  if (!skip_token (LEFT_CURLY))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  // parse inner attributes (if they exist)
  AST::AttrVec inner_attrs = parse_inner_attributes ();

  // parse match arms (if they exist)
  // std::vector<std::unique_ptr<AST::MatchCase> > match_arms;
  std::vector<AST::MatchCase> match_arms;

  // parse match cases
  while (lexer.peek_token ()->get_id () != RIGHT_CURLY)
    {
      // parse match arm itself, which is required
      AST::MatchArm arm = parse_match_arm ();
      if (arm.is_error ())
	{
	  // TODO is this worth throwing everything away?
	  Error error (lexer.peek_token ()->get_locus (),
		       "failed to parse match arm in match arms");
	  add_error (std::move (error));

	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      if (!skip_token (MATCH_ARROW))
	{
	  // skip after somewhere?
	  // TODO is returning here a good idea? or is break better?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}

      ParseRestrictions restrictions;
      restrictions.expr_can_be_stmt = true;

      auto expr = parse_expr ({}, restrictions);

      if (!expr)
	{
	  /* We don't need to throw an error as it already reported by
	   * parse_expr
	   */
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      bool is_expr_without_block = expr.value ()->is_expr_without_block ();

      match_arms.push_back (
	AST::MatchCase (std::move (arm), std::move (expr.value ())));

      // handle comma presence
      if (lexer.peek_token ()->get_id () != COMMA)
	{
	  if (!is_expr_without_block)
	    {
	      // allowed even if not final case
	      continue;
	    }
	  else if (is_expr_without_block
		   && lexer.peek_token ()->get_id () != RIGHT_CURLY)
	    {
	      // not allowed if not final case
	      Error error (lexer.peek_token ()->get_locus (),
			   "exprwithoutblock requires comma after match case "
			   "expression in match arm (if not final case)");
	      add_error (std::move (error));

	      return tl::unexpected<Parse::Error::Node> (
		Parse::Error::Node::MALFORMED);
	    }
	  else
	    {
	      // otherwise, must be final case, so fine
	      break;
	    }
	}
      lexer.skip_token ();
    }

  if (!skip_token (RIGHT_CURLY))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }

  match_arms.shrink_to_fit ();

  return std::unique_ptr<AST::MatchExpr> (
    new AST::MatchExpr (std::move (scrutinee.value ()), std::move (match_arms),
			std::move (inner_attrs), std::move (outer_attrs),
			locus));
}

// Parses an async block expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::AsyncBlockExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_async_block_expr (AST::AttrVec outer_attrs)
{
  location_t locus = lexer.peek_token ()->get_locus ();
  skip_token (ASYNC);

  // detect optional move token
  bool has_move = false;
  if (lexer.peek_token ()->get_id () == MOVE)
    {
      lexer.skip_token ();
      has_move = true;
    }

  // parse block expression (required)
  auto block_expr = parse_block_expr ();
  if (!block_expr)
    {
      Error error (
	lexer.peek_token ()->get_locus (),
	"failed to parse block expression of async block expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  return std::unique_ptr<AST::AsyncBlockExpr> (
    new AST::AsyncBlockExpr (std::move (block_expr.value ()), has_move,
			     std::move (outer_attrs), locus));
}

// Parses an unsafe block expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::UnsafeBlockExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_unsafe_block_expr (
  AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (UNSAFE);
    }

  // parse block expression (required)
  auto block_expr = parse_block_expr ();
  if (!block_expr)
    {
      Error error (
	lexer.peek_token ()->get_locus (),
	"failed to parse block expression of unsafe block expression");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }
  return std::unique_ptr<AST::UnsafeBlockExpr> (
    new AST::UnsafeBlockExpr (std::move (block_expr.value ()),
			      std::move (outer_attrs), locus));
}

// Parses an array definition expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArrayExpr>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_array_expr (AST::AttrVec outer_attrs,
					      location_t pratt_parsed_loc)
{
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (LEFT_SQUARE);
    }

  // parse optional inner attributes
  AST::AttrVec inner_attrs = parse_inner_attributes ();

  // parse the "array elements" section, which is optional
  if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
    {
      // no array elements
      lexer.skip_token ();

      std::vector<std::unique_ptr<AST::Expr>> exprs;
      auto array_elems
	= std::make_unique<AST::ArrayElemsValues> (std::move (exprs), locus);
      return std::make_unique<AST::ArrayExpr> (std::move (array_elems),
					       std::move (inner_attrs),
					       std::move (outer_attrs), locus);
    }
  else
    {
      // should have array elements
      // parse initial expression, which is required for either
      auto initial_expr = parse_expr ();
      if (!initial_expr)
	{
	  Error error (lexer.peek_token ()->get_locus (),
		       "could not parse expression in array expression "
		       "(even though arrayelems seems to be present)");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::CHILD_ERROR);
	}

      if (lexer.peek_token ()->get_id () == SEMICOLON)
	{
	  // copy array elems
	  lexer.skip_token ();

	  // parse copy amount expression (required)
	  auto copy_amount = parse_expr ();
	  if (!copy_amount)
	    {
	      Error error (lexer.peek_token ()->get_locus (),
			   "could not parse copy amount expression in array "
			   "expression (arrayelems)");
	      add_error (std::move (error));

	      // skip somewhere?
	      return tl::unexpected<Parse::Error::Node> (
		Parse::Error::Node::CHILD_ERROR);
	    }

	  skip_token (RIGHT_SQUARE);

	  std::unique_ptr<AST::ArrayElemsCopied> copied_array_elems (
	    new AST::ArrayElemsCopied (std::move (initial_expr.value ()),
				       std::move (copy_amount.value ()),
				       locus));
	  return std::unique_ptr<AST::ArrayExpr> (
	    new AST::ArrayExpr (std::move (copied_array_elems),
				std::move (inner_attrs),
				std::move (outer_attrs), locus));
	}
      else if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
	{
	  // single-element array expression
	  std::vector<std::unique_ptr<AST::Expr>> exprs;
	  exprs.reserve (1);
	  exprs.push_back (std::move (initial_expr.value ()));
	  exprs.shrink_to_fit ();

	  skip_token (RIGHT_SQUARE);

	  std::unique_ptr<AST::ArrayElemsValues> array_elems (
	    new AST::ArrayElemsValues (std::move (exprs), locus));
	  return std::unique_ptr<AST::ArrayExpr> (
	    new AST::ArrayExpr (std::move (array_elems),
				std::move (inner_attrs),
				std::move (outer_attrs), locus));
	}
      else if (lexer.peek_token ()->get_id () == COMMA)
	{
	  // multi-element array expression (or trailing comma)
	  std::vector<std::unique_ptr<AST::Expr>> exprs;
	  exprs.push_back (std::move (initial_expr.value ()));

	  const_TokenPtr t = lexer.peek_token ();
	  while (t->get_id () == COMMA)
	    {
	      lexer.skip_token ();

	      // quick break if right square bracket
	      if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
		break;

	      // parse expression (required)
	      auto expr = parse_expr ();
	      if (!expr)
		{
		  Error error (lexer.peek_token ()->get_locus (),
			       "failed to parse element in array expression");
		  add_error (std::move (error));

		  // skip somewhere?
		  return tl::unexpected<Parse::Error::Node> (
		    Parse::Error::Node::CHILD_ERROR);
		}
	      exprs.push_back (std::move (expr.value ()));

	      t = lexer.peek_token ();
	    }

	  skip_token (RIGHT_SQUARE);

	  exprs.shrink_to_fit ();

	  std::unique_ptr<AST::ArrayElemsValues> array_elems (
	    new AST::ArrayElemsValues (std::move (exprs), locus));
	  return std::unique_ptr<AST::ArrayExpr> (
	    new AST::ArrayExpr (std::move (array_elems),
				std::move (inner_attrs),
				std::move (outer_attrs), locus));
	}
      else
	{
	  // error
	  Error error (lexer.peek_token ()->get_locus (),
		       "unexpected token %qs in array expression (arrayelems)",
		       lexer.peek_token ()->get_token_description ());
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Node> (
	    Parse::Error::Node::MALFORMED);
	}
    }
}

// Parses a grouped or tuple expression (disambiguates).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ExprWithoutBlock>, Parse::Error::Node>
Parser<ManagedTokenSource>::parse_grouped_or_tuple_expr (
  AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
{
  // adjustment to allow Pratt parsing to reuse function without copy-paste
  location_t locus = pratt_parsed_loc;
  if (locus == UNKNOWN_LOCATION)
    {
      locus = lexer.peek_token ()->get_locus ();
      skip_token (LEFT_PAREN);
    }

  // parse optional inner attributes
  AST::AttrVec inner_attrs = parse_inner_attributes ();

  if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    {
      // must be empty tuple
      lexer.skip_token ();

      // create tuple with empty tuple elems
      return std::unique_ptr<AST::TupleExpr> (
	new AST::TupleExpr (std::vector<std::unique_ptr<AST::Expr>> (),
			    std::move (inner_attrs), std::move (outer_attrs),
			    locus));
    }

  // parse first expression (required)
  auto first_expr = parse_expr ();
  if (!first_expr)
    {
      Error error (lexer.peek_token ()->get_locus (),
		   "failed to parse expression in grouped or tuple expression");
      add_error (std::move (error));

      // skip after somewhere?
      return tl::unexpected<Parse::Error::Node> (
	Parse::Error::Node::CHILD_ERROR);
    }

  // detect whether grouped expression with right parentheses as next token
  if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    {
      // must be grouped expr
      lexer.skip_token ();

      // create grouped expr
      return std::unique_ptr<AST::GroupedExpr> (
	new AST::GroupedExpr (std::move (first_expr.value ()),
			      std::move (inner_attrs), std::move (outer_attrs),
			      locus));
    }
  else if (lexer.peek_token ()->get_id () == COMMA)
    {
      // tuple expr
      std::vector<std::unique_ptr<AST::Expr>> exprs;
      exprs.push_back (std::move (first_expr.value ()));

      // parse potential other tuple exprs
      const_TokenPtr t = lexer.peek_token ();
      while (t->get_id () == COMMA)
	{
	  lexer.skip_token ();

	  // break out if right paren
	  if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
	    break;

	  // parse expr, which is now required
	  auto expr = parse_expr ();
	  if (!expr)
	    {
	      Error error (lexer.peek_token ()->get_locus (),
			   "failed to parse expr in tuple expr");
	      add_error (std::move (error));

	      // skip somewhere?
	      return tl::unexpected<Parse::Error::Node> (
		Parse::Error::Node::CHILD_ERROR);
	    }
	  exprs.push_back (std::move (expr.value ()));

	  t = lexer.peek_token ();
	}

      // skip right paren
      skip_token (RIGHT_PAREN);

      return std::unique_ptr<AST::TupleExpr> (
	new AST::TupleExpr (std::move (exprs), std::move (inner_attrs),
			    std::move (outer_attrs), locus));
    }
  else
    {
      // error
      const_TokenPtr t = lexer.peek_token ();
      Error error (t->get_locus (),
		   "unexpected token %qs in grouped or tuple expression "
		   "(parenthesised expression) - expected %<)%> for grouped "
		   "expr and %<,%> for tuple expr",
		   t->get_token_description ());
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Node> (Parse::Error::Node::MALFORMED);
    }
}

// Parses a struct expression field.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::StructExprField>,
	     Parse::Error::StructExprField>
Parser<ManagedTokenSource>::parse_struct_expr_field ()
{
  AST::AttrVec outer_attrs = parse_outer_attributes ();
  const_TokenPtr t = lexer.peek_token ();
  switch (t->get_id ())
    {
    case IDENTIFIER:
      if (lexer.peek_token (1)->get_id () == COLON)
	{
	  // struct expr field with identifier and expr
	  Identifier ident = {t};
	  lexer.skip_token (1);

	  // parse expression (required)
	  auto expr = parse_expr ();
	  if (!expr)
	    {
	      Error error (t->get_locus (),
			   "failed to parse struct expression field with "
			   "identifier and expression");
	      add_error (std::move (error));

	      return tl::unexpected<Parse::Error::StructExprField> (
		Parse::Error::StructExprField::CHILD_ERROR);
	    }

	  return std::unique_ptr<AST::StructExprFieldIdentifierValue> (
	    new AST::StructExprFieldIdentifierValue (std::move (ident),
						     std::move (expr.value ()),
						     std::move (outer_attrs),
						     t->get_locus ()));
	}
      else
	{
	  // struct expr field with identifier only
	  Identifier ident{t};
	  lexer.skip_token ();

	  return std::unique_ptr<AST::StructExprFieldIdentifier> (
	    new AST::StructExprFieldIdentifier (std::move (ident),
						std::move (outer_attrs),
						t->get_locus ()));
	}
    case INT_LITERAL:
      {
	// parse tuple index field
	int index = atoi (t->get_str ().c_str ());
	lexer.skip_token ();

	if (!skip_token (COLON))
	  {
	    // skip somewhere?
	    return tl::unexpected<Parse::Error::StructExprField> (
	      Parse::Error::StructExprField::MALFORMED);
	  }

	// parse field expression (required)
	auto expr = parse_expr ();
	if (!expr)
	  {
	    Error error (t->get_locus (),
			 "failed to parse expr in struct (or enum) expr "
			 "field with tuple index");
	    add_error (std::move (error));

	    return tl::unexpected<Parse::Error::StructExprField> (
	      Parse::Error::StructExprField::CHILD_ERROR);
	  }

	return std::unique_ptr<AST::StructExprFieldIndexValue> (
	  new AST::StructExprFieldIndexValue (index, std::move (expr.value ()),
					      std::move (outer_attrs),
					      t->get_locus ()));
      }
    case DOT_DOT:
      /* this is a struct base and can't be parsed here, so just return
       * nothing without erroring */

      return tl::unexpected<Parse::Error::StructExprField> (
	Parse::Error::StructExprField::STRUCT_BASE);
    default:
      add_error (
	Error (t->get_locus (),
	       "unrecognised token %qs as first token of struct expr field - "
	       "expected identifier or integer literal",
	       t->get_token_description ()));

      return tl::unexpected<Parse::Error::StructExprField> (
	Parse::Error::StructExprField::MALFORMED);
    }
}

/* Pratt parser impl of parse_expr. FIXME: this is only provisional and
 * probably will be changed. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
					AST::AttrVec outer_attrs,
					ParseRestrictions restrictions)
{
  const_TokenPtr current_token = lexer.peek_token ();
  // Special hack because we are allowed to return nullptr, in that case we
  // don't want to skip the token, since we don't actually parse it. But if
  // null isn't allowed it indicates an error, and we want to skip past that.
  // So return early if it is one of the tokens that ends an expression
  // (or at least cannot start a new expression).
  if (restrictions.expr_can_be_null)
    {
      TokenId id = current_token->get_id ();
      if (id == SEMICOLON || id == RIGHT_PAREN || id == RIGHT_CURLY
	  || id == RIGHT_SQUARE || id == COMMA || id == LEFT_CURLY)
	return tl::unexpected<Parse::Error::Expr> (
	  Parse::Error::Expr::NULL_EXPR);
    }

  ParseRestrictions null_denotation_restrictions = restrictions;
  null_denotation_restrictions.expr_can_be_stmt = false;

  // parse null denotation (unary part of expression)
  tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr
    = null_denotation ({}, null_denotation_restrictions);
  if (!expr)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  if (expr.value () == nullptr)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  
  return left_denotations (std::move (expr), right_binding_power,
			   std::move (outer_attrs), restrictions);
}

// Parse expression with lowest left binding power.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_expr (AST::AttrVec outer_attrs,
					ParseRestrictions restrictions)
{
  return parse_expr (LBP_LOWEST, std::move (outer_attrs), restrictions);
}

template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::left_denotations (
  tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr,
  int right_binding_power, AST::AttrVec outer_attrs,
  ParseRestrictions restrictions)
{
  if (!expr)
    {
      // DEBUG
      rust_debug ("null denotation is null; returning null for parse_expr");
      return tl::unexpected<Parse::Error::Expr> (
	Parse::Error::Expr::NULL_DENOTATION);
    }

  const_TokenPtr current_token = lexer.peek_token ();

  if (restrictions.expr_can_be_stmt && !expr.value ()->is_expr_without_block ()
      && current_token->get_id () != DOT
      && current_token->get_id () != QUESTION_MARK)
    {
      rust_debug ("statement expression with block");
      expr.value ()->set_outer_attrs (std::move (outer_attrs));
      return expr;
    }

  restrictions.expr_can_be_stmt = false;

  // stop parsing if find lower priority token - parse higher priority first
  while (right_binding_power < left_binding_power (current_token))
    {
      lexer.skip_token ();

      // FIXME attributes should generally be applied to the null denotation.
      expr = left_denotation (current_token, std::move (expr.value ()),
			      std::move (outer_attrs), restrictions);

      if (!expr)
	{
	  // DEBUG
	  rust_debug ("left denotation is null; returning null for parse_expr");

	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::LEFT_DENOTATION);
	}

      current_token = lexer.peek_token ();
    }

  return expr;
}

/* Determines action to take when finding token at beginning of expression. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::null_denotation (AST::AttrVec outer_attrs,
					     ParseRestrictions restrictions)
{
  /* note: tok is previous character in input stream, not current one, as
   * parse_expr skips it before passing it in */

  /* as a Pratt parser (which works by decomposing expressions into a null
   * denotation and then a left denotation), null denotations handle primaries
   * and unary operands (but only prefix unary operands) */

  auto tok = lexer.peek_token ();

  switch (tok->get_id ())
    {
    case IDENTIFIER:
    case SELF:
    case SELF_ALIAS:
    case DOLLAR_SIGN:
    case CRATE:
    case SUPER:
    case SCOPE_RESOLUTION:
      {
	// DEBUG
	rust_debug ("beginning null denotation identifier handling");

	/* best option: parse as path, then extract identifier, macro,
	 * struct/enum, or just path info from it */
	AST::PathInExpression path = parse_path_in_expression ();

	return null_denotation_path (std::move (path), std::move (outer_attrs),
				     restrictions);
      }
    case HASH:
      {
	// Parse outer attributes and then the expression that follows
	AST::AttrVec attrs = parse_outer_attributes ();

	// Merge with any existing outer attributes
	if (!outer_attrs.empty ())
	  attrs.insert (attrs.begin (), outer_attrs.begin (),
			outer_attrs.end ());

	// Try to parse the expression that should follow the attributes
	auto expr = parse_expr (std::move (attrs), restrictions);
	if (!expr)
	  {
	    /* If parsing failed and we're at a semicolon, provide a better
	     * error
	     */
	    const_TokenPtr next_tok = lexer.peek_token ();
	    if (next_tok->get_id () == SEMICOLON)
	      add_error (Error (next_tok->get_locus (),
				"expected expression, found %<;%>"));
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	  }
	return expr;
      }
    default:
      if (tok->get_id () == LEFT_SHIFT)
	{
	  lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
	  tok = lexer.peek_token ();
	}

      lexer.skip_token ();
      return null_denotation_not_path (std::move (tok), std::move (outer_attrs),
				       restrictions);
    }
}

// Handling of expresions that start with a path for `null_denotation`.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::null_denotation_path (
  AST::PathInExpression path, AST::AttrVec outer_attrs,
  ParseRestrictions restrictions)
{
  rust_debug ("parsing null denotation after path");

  // HACK: always make "self" by itself a path (regardless of next
  // tokens)
  if (path.is_single_segment () && path.get_segments ()[0].is_lower_self_seg ())
    {
      // HACK: add outer attrs to path
      path.set_outer_attrs (std::move (outer_attrs));
      return std::make_unique<AST::PathInExpression> (std::move (path));
    }

  // branch on next token
  const_TokenPtr t = lexer.peek_token ();
  switch (t->get_id ())
    {
    case EXCLAM:
      {
	// macro
	auto macro = parse_macro_invocation_partial (std::move (path),
						     std::move (outer_attrs));
	if (macro == nullptr)
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	return std::unique_ptr<AST::Expr> (std::move (macro));
      }
    case LEFT_CURLY:
      {
	bool not_a_block = lexer.peek_token (1)->get_id () == IDENTIFIER
			   && (lexer.peek_token (2)->get_id () == COMMA
			       || (lexer.peek_token (2)->get_id () == COLON
				   && (lexer.peek_token (4)->get_id () == COMMA
				       || !Parse::Utils::can_tok_start_type (
					 lexer.peek_token (3)->get_id ()))));

	/* definitely not a block:
	 *  path '{' ident ','
	 *  path '{' ident ':' [anything] ','
	 *  path '{' ident ':' [not a type]
	 * otherwise, assume block expr and thus path */
	// DEBUG
	rust_debug ("values of lookahead: '%s' '%s' '%s' '%s' ",
		    lexer.peek_token (1)->get_token_description (),
		    lexer.peek_token (2)->get_token_description (),
		    lexer.peek_token (3)->get_token_description (),
		    lexer.peek_token (4)->get_token_description ());

	rust_debug ("can be struct expr: '%s', not a block: '%s'",
		    restrictions.can_be_struct_expr ? "true" : "false",
		    not_a_block ? "true" : "false");

	// struct/enum expr struct
	if (!restrictions.can_be_struct_expr && !not_a_block)
	  {
	    // HACK: add outer attrs to path
	    path.set_outer_attrs (std::move (outer_attrs));
	    return std::unique_ptr<AST::PathInExpression> (
	      new AST::PathInExpression (std::move (path)));
	  }
	auto struct_expr
	  = parse_struct_expr_struct_partial (std::move (path),
					      std::move (outer_attrs));
	if (struct_expr == nullptr)
	  {
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	  }
	return struct_expr;
      }
    case LEFT_PAREN:
      {
	// struct/enum expr tuple
	if (!restrictions.can_be_struct_expr)
	  {
	    // assume path is returned
	    // HACK: add outer attributes to path
	    path.set_outer_attrs (std::move (outer_attrs));
	    return std::make_unique<AST::PathInExpression> (std::move (path));
	  }
	auto tuple_expr
	  = parse_struct_expr_tuple_partial (std::move (path),
					     std::move (outer_attrs));
	if (tuple_expr == nullptr)
	  {
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	  }
	return tuple_expr;
      }
    default:
      // assume path is returned if not single segment
      if (path.is_single_segment ())
	{
	  // FIXME: This should probably be returned as a path.
	  /* HACK: may have to become permanent, but this is my current
	   * identifier expression */
	  return std::unique_ptr<AST::IdentifierExpr> (new AST::IdentifierExpr (
	    path.get_segments ()[0].get_ident_segment ().as_string (), {},
	    path.get_locus ()));
	}
      // HACK: add outer attrs to path
      path.set_outer_attrs (std::move (outer_attrs));
      return std::unique_ptr<AST::PathInExpression> (
	new AST::PathInExpression (std::move (path)));
    }
  rust_unreachable ();
}

// Handling of expresions that do not start with a path for `null_denotation`.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::null_denotation_not_path (
  const_TokenPtr tok, AST::AttrVec outer_attrs, ParseRestrictions restrictions)
{
  switch (tok->get_id ())
    {
    // FIXME: Handle in null_denotation_path?
    case LEFT_SHIFT:
    case LEFT_ANGLE:
      {
	// qualified path
	// HACK: add outer attrs to path
	AST::QualifiedPathInExpression path
	  = parse_qualified_path_in_expression (tok->get_locus ());
	path.set_outer_attrs (std::move (outer_attrs));
	return std::make_unique<AST::QualifiedPathInExpression> (
	  std::move (path));
      }
    // FIXME: delegate to parse_literal_expr instead? would have to rejig
    // tokens and whatever.
    // FIXME: for literal exprs, outer attrs should be passed in, and later
    // error if it does not make up the entire statement.
    case INT_LITERAL:
      // we should check the range, but ignore for now
      // encode as int?
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::INT,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case FLOAT_LITERAL:
      // encode as float?
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::FLOAT,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case STRING_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case BYTE_STRING_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case RAW_STRING_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::RAW_STRING,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case CHAR_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case BYTE_CHAR_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE,
			      tok->get_type_hint (), {}, tok->get_locus ()));
    case TRUE_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (Values::Keywords::TRUE_LITERAL,
			      AST::Literal::BOOL, tok->get_type_hint (), {},
			      tok->get_locus ()));
    case FALSE_LITERAL:
      return std::unique_ptr<AST::LiteralExpr> (
	new AST::LiteralExpr (Values::Keywords::FALSE_LITERAL,
			      AST::Literal::BOOL, tok->get_type_hint (), {},
			      tok->get_locus ()));
    case LEFT_PAREN:
      {
	auto grouped_or_tuple_expr
	  = parse_grouped_or_tuple_expr (std::move (outer_attrs),
					 tok->get_locus ());
	if (grouped_or_tuple_expr)
	  return std::move (grouped_or_tuple_expr.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }

    /*case PLUS: { // unary plus operator
	// invoke parse_expr recursively with appropriate priority, etc. for
    below AST::Expr* expr = parse_expr(LBP_UNARY_PLUS);

	if (expr == nullptr)
	    return nullptr;
	// can only apply to integer and float expressions
	if (expr->get_type() != integer_type_node || expr->get_type() !=
    float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
    plus must be int or float but it is %s", print_type(expr->get_type()));
    return nullptr;
	}

	return Tree(expr, tok->get_locus());
    }*/
    // Rust has no unary plus operator
    case MINUS:
      { // unary minus
	ParseRestrictions entered_from_unary;
	entered_from_unary.entered_from_unary = true;
	if (!restrictions.can_be_struct_expr)
	  entered_from_unary.can_be_struct_expr = false;
	auto expr = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary);

	if (!expr)
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	// can only apply to integer and float expressions
	/*if (expr.get_type() != integer_type_node || expr.get_type() !=
	float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
	minus must be int or float but it is %s",
	print_type(expr.get_type())); return Tree::error();
	}*/
	/* FIXME: when implemented the "get type" method on expr, ensure it is
	 * int or float type (except unsigned int). Actually, this would
	 * probably have to be done in semantic analysis (as type checking).
	 */

	/* FIXME: allow outer attributes on these expressions by having an
	 * outer attrs parameter in function*/
	return std::make_unique<AST::NegationExpr> (std::move (expr.value ()),
						    NegationOperator::NEGATE,
						    std::move (outer_attrs),
						    tok->get_locus ());
      }
    case EXCLAM:
      { // logical or bitwise not
	ParseRestrictions entered_from_unary;
	entered_from_unary.entered_from_unary = true;
	if (!restrictions.can_be_struct_expr)
	  entered_from_unary.can_be_struct_expr = false;
	auto expr = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary);

	if (!expr)
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	// can only apply to boolean expressions
	/*if (expr.get_type() != boolean_type_node) {
	    rust_error_at(tok->get_locus(),
	      "operand of logical not must be a boolean but it is %s",
	      print_type(expr.get_type()));
	    return Tree::error();
	}*/
	/* FIXME: type checking for boolean or integer expressions in semantic
	 * analysis */

	// FIXME: allow outer attributes on these expressions
	return std::make_unique<AST::NegationExpr> (std::move (expr.value ()),
						    NegationOperator::NOT,
						    std::move (outer_attrs),
						    tok->get_locus ());
      }
    case ASTERISK:
      {
	/* pointer dereference only - HACK: as struct expressions should
	 * always be value expressions, cannot be dereferenced */
	ParseRestrictions entered_from_unary;
	entered_from_unary.entered_from_unary = true;
	entered_from_unary.can_be_struct_expr = false;
	auto expr = parse_expr (LBP_UNARY_ASTERISK, {}, entered_from_unary);
	if (!expr)
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	// FIXME: allow outer attributes on expression
	return std::make_unique<AST::DereferenceExpr> (std::move (
							 expr.value ()),
						       std::move (outer_attrs),
						       tok->get_locus ());
      }
    case AMP:
      {
	// (single) "borrow" expression - shared (mutable) or immutable
	tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr> expr
	  = tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
	Mutability mutability = Mutability::Imm;
	bool raw_borrow = false;

	ParseRestrictions entered_from_unary;
	entered_from_unary.entered_from_unary = true;
	if (!restrictions.can_be_struct_expr)
	  entered_from_unary.can_be_struct_expr = false;

	auto is_mutability = [] (const_TokenPtr token) {
	  return token->get_id () == CONST || token->get_id () == MUT;
	};

	auto t = lexer.peek_token ();
	// Weak raw keyword, we look (1) ahead and treat it as an identifier if
	// there is no mut nor const.
	if (t->get_id () == IDENTIFIER
	    && t->get_str () == Values::WeakKeywords::RAW
	    && is_mutability (lexer.peek_token (1)))
	  {
	    lexer.skip_token ();
	    switch (lexer.peek_token ()->get_id ())
	      {
	      case MUT:
		mutability = Mutability::Mut;
		break;
	      case CONST:
		mutability = Mutability::Imm;
		break;
	      default:
		rust_error_at (lexer.peek_token ()->get_locus (),
			       "raw borrow should be either const or mut");
	      }
	    lexer.skip_token ();
	    auto expr_result
	      = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
	    if (expr_result)
	      expr = std::move (expr_result.value ());
	    else
	      return tl::unexpected<Parse::Error::Expr> (
		Parse::Error::Expr::CHILD_ERROR);
	    raw_borrow = true;
	  }
	else if (t->get_id () == MUT)
	  {
	    lexer.skip_token ();
	    auto expr_result
	      = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
	    if (expr_result)
	      expr = std::move (expr_result.value ());
	    else
	      return tl::unexpected<Parse::Error::Expr> (
		Parse::Error::Expr::CHILD_ERROR);
	    mutability = Mutability::Mut;
	    raw_borrow = false;
	  }
	else
	  {
	    auto expr_result
	      = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
	    if (expr_result)
	      expr = std::move (expr_result.value ());
	    else
	      return tl::unexpected<Parse::Error::Expr> (
		Parse::Error::Expr::CHILD_ERROR);
	    raw_borrow = false;
	  }

	// FIXME: allow outer attributes on expression
	return std::make_unique<AST::BorrowExpr> (std::move (expr.value ()),
						  mutability, raw_borrow, false,
						  std::move (outer_attrs),
						  tok->get_locus ());
      }
    case LOGICAL_AND:
      {
	// (double) "borrow" expression - shared (mutable) or immutable
	std::unique_ptr<AST::Expr> expr = nullptr;
	Mutability mutability = Mutability::Imm;

	ParseRestrictions entered_from_unary;
	entered_from_unary.entered_from_unary = true;

	if (lexer.peek_token ()->get_id () == MUT)
	  {
	    lexer.skip_token ();
	    auto expr_res
	      = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
	    if (!expr_res)
	      return tl::unexpected<Parse::Error::Expr> (
		Parse::Error::Expr::CHILD_ERROR);
	    expr = std::move (expr_res.value ());
	    mutability = Mutability::Mut;
	  }
	else
	  {
	    auto expr_result
	      = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
	    if (expr_result)
	      expr = std::move (expr_result.value ());
	    else
	      return tl::unexpected<Parse::Error::Expr> (
		Parse::Error::Expr::CHILD_ERROR);
	    mutability = Mutability::Imm;
	  }

	// FIXME: allow outer attributes on expression
	return std::make_unique<AST::BorrowExpr> (std::move (expr), mutability,
						  false, true,
						  std::move (outer_attrs),
						  tok->get_locus ());
      }
    case OR:
    case PIPE:
    case MOVE:
      // closure expression
      {
	auto ret = parse_closure_expr_pratt (tok, std::move (outer_attrs));
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case DOT_DOT:
      // either "range to" or "range full" expressions
      {
	auto ret
	  = parse_nud_range_exclusive_expr (tok, std::move (outer_attrs));
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case DOT_DOT_EQ:
      // range to inclusive expr
      {
	auto ret = parse_range_to_inclusive_expr (tok, std::move (outer_attrs));
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case RETURN_KW:
      // FIXME: is this really a null denotation expression?
      {
	auto ret
	  = parse_return_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case TRY:
      // FIXME: is this really a null denotation expression?
      {
	auto ret = parse_try_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case BREAK:
      // FIXME: is this really a null denotation expression?
      {
	auto ret
	  = parse_break_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case CONTINUE:
      return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
    case LEFT_CURLY:
      // ok - this is an expression with block for once.
      {
	auto ret = parse_block_expr (std::move (outer_attrs), tl::nullopt,
				     tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case IF:
      // if or if let, so more lookahead to find out
      if (lexer.peek_token ()->get_id () == LET)
	{
	  // if let expr
	  auto ret
	    = parse_if_let_expr (std::move (outer_attrs), tok->get_locus ());
	  if (ret)
	    return std::move (ret.value ());
	  else
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	}
      else
	{
	  // if expr
	  auto ret = parse_if_expr (std::move (outer_attrs), tok->get_locus ());
	  if (ret)
	    return std::move (ret.value ());
	  else
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	}
    case LIFETIME:
      {
	auto ret = parse_labelled_loop_expr (tok, std::move (outer_attrs));
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case LOOP:
      {
	auto ret = parse_loop_expr (std::move (outer_attrs), tl::nullopt,
				    tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case WHILE:
      if (lexer.peek_token ()->get_id () == LET)
	{
	  auto ret = parse_while_let_loop_expr (std::move (outer_attrs));
	  if (ret)
	    return std::move (ret.value ());
	  else
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	}
      else
	{
	  auto ret = parse_while_loop_expr (std::move (outer_attrs),
					    tl::nullopt, tok->get_locus ());
	  if (ret)
	    return std::move (ret.value ());
	  else
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::CHILD_ERROR);
	}
    case FOR:
      {
	auto ret = parse_for_loop_expr (std::move (outer_attrs), tl::nullopt);
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case MATCH_KW:
      // also an expression with block
      {
	auto ret
	  = parse_match_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }

    case LEFT_SQUARE:
      // array definition expr (not indexing)
      {
	auto ret
	  = parse_array_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case UNSAFE:
      {
	auto ret = parse_unsafe_block_expr (std::move (outer_attrs),
					    tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case BOX:
      {
	auto ret = parse_box_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    case UNDERSCORE:
      add_error (
	Error (tok->get_locus (),
	       "use of %qs is not allowed on the right-side of an assignment",
	       tok->get_token_description ()));
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    case CONST:
      {
	auto ret
	  = parse_const_block_expr (std::move (outer_attrs), tok->get_locus ());
	if (ret)
	  return std::move (ret.value ());
	else
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
      }
    default:
      if (!restrictions.expr_can_be_null)
	add_error (Error (tok->get_locus (),
			  "found unexpected token %qs in null denotation",
			  tok->get_token_description ()));
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }
}

/* Called for each token that can appear in infix (between) position. Can be
 * operators or other punctuation. Returns a function pointer to member
 * function that implements the left denotation for the token given. */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::Expr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::left_denotation (const_TokenPtr tok,
					     std::unique_ptr<AST::Expr> left,
					     AST::AttrVec outer_attrs,
					     ParseRestrictions restrictions)
{
  // Token passed in has already been skipped, so peek gives "next" token
  switch (tok->get_id ())
    {
    // FIXME: allow for outer attributes to be applied
    case QUESTION_MARK:
      {
	location_t left_locus = left->get_locus ();
	// error propagation expression - unary postfix
	return std::make_unique<AST::ErrorPropagationExpr> (
	  std::move (left), std::move (outer_attrs), left_locus);
      }
    case PLUS:
      // sum expression - binary infix
      /*return parse_binary_plus_expr (tok, std::move (left),
				     std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (tok, std::move (left),
					       std::move (outer_attrs),
					       ArithmeticOrLogicalOperator::ADD,
					       restrictions);
    case MINUS:
      // difference expression - binary infix
      /*return parse_binary_minus_expr (tok, std::move (left),
				      std::move (outer_attrs),
	 restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::SUBTRACT, restrictions);
    case ASTERISK:
      // product expression - binary infix
      /*return parse_binary_mult_expr (tok, std::move (left),
				     std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::MULTIPLY, restrictions);
    case DIV:
      // quotient expression - binary infix
      /*return parse_binary_div_expr (tok, std::move (left),
				    std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::DIVIDE, restrictions);
    case PERCENT:
      // modulo expression - binary infix
      /*return parse_binary_mod_expr (tok, std::move (left),
				    std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::MODULUS, restrictions);
    case AMP:
      // logical or bitwise and expression - binary infix
      /*return parse_bitwise_and_expr (tok, std::move (left),
				     std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::BITWISE_AND, restrictions);
    case PIPE:
      // logical or bitwise or expression - binary infix
      /*return parse_bitwise_or_expr (tok, std::move (left),
				    std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::BITWISE_OR, restrictions);
    case CARET:
      // logical or bitwise xor expression - binary infix
      /*return parse_bitwise_xor_expr (tok, std::move (left),
				     std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::BITWISE_XOR, restrictions);
    case LEFT_SHIFT:
      // left shift expression - binary infix
      /*return parse_left_shift_expr (tok, std::move (left),
				    std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::LEFT_SHIFT, restrictions);
    case RIGHT_SHIFT:
      // right shift expression - binary infix
      /*return parse_right_shift_expr (tok, std::move (left),
				     std::move (outer_attrs), restrictions);*/
      return parse_arithmetic_or_logical_expr (
	tok, std::move (left), std::move (outer_attrs),
	ArithmeticOrLogicalOperator::RIGHT_SHIFT, restrictions);
    case EQUAL_EQUAL:
      // equal to expression - binary infix (no associativity)
      /*return parse_binary_equal_expr (tok, std::move (left),
				      std::move (outer_attrs),
	 restrictions);*/
      return parse_comparison_expr (tok, std::move (left),
				    std::move (outer_attrs),
				    ComparisonOperator::EQUAL, restrictions);
    case NOT_EQUAL:
      // not equal to expression - binary infix (no associativity)
      /*return parse_binary_not_equal_expr (tok, std::move (left),
					  std::move (outer_attrs),
					  restrictions);*/
      return parse_comparison_expr (tok, std::move (left),
				    std::move (outer_attrs),
				    ComparisonOperator::NOT_EQUAL,
				    restrictions);
    case RIGHT_ANGLE:
      // greater than expression - binary infix (no associativity)
      /*return parse_binary_greater_than_expr (tok, std::move (left),
					     std::move (outer_attrs),
					     restrictions);*/
      return parse_comparison_expr (tok, std::move (left),
				    std::move (outer_attrs),
				    ComparisonOperator::GREATER_THAN,
				    restrictions);
    case LEFT_ANGLE:
      // less than expression - binary infix (no associativity)
      /*return parse_binary_less_than_expr (tok, std::move (left),
					  std::move (outer_attrs),
					  restrictions);*/
      return parse_comparison_expr (tok, std::move (left),
				    std::move (outer_attrs),
				    ComparisonOperator::LESS_THAN,
				    restrictions);
    case GREATER_OR_EQUAL:
      // greater than or equal to expression - binary infix (no associativity)
      /*return parse_binary_greater_equal_expr (tok, std::move (left),
					      std::move (outer_attrs),
					      restrictions);*/
      return parse_comparison_expr (tok, std::move (left),
				    std::move (outer_attrs),
				    ComparisonOperator::GREATER_OR_EQUAL,
				    restrictions);
    case LESS_OR_EQUAL:
      // less than or equal to expression - binary infix (no associativity)
      /*return parse_binary_less_equal_expr (tok, std::move (left),
					   std::move (outer_attrs),
					   restrictions);*/
      return parse_comparison_expr (tok, std::move (left),
				    std::move (outer_attrs),
				    ComparisonOperator::LESS_OR_EQUAL,
				    restrictions);
    case OR:
      // lazy logical or expression - binary infix
      return parse_lazy_or_expr (tok, std::move (left), std::move (outer_attrs),
				 restrictions);
    case LOGICAL_AND:
      // lazy logical and expression - binary infix
      return parse_lazy_and_expr (tok, std::move (left),
				  std::move (outer_attrs), restrictions);
    case AS:
      /* type cast expression - kind of binary infix (RHS is actually a
       * TypeNoBounds) */
      return parse_type_cast_expr (tok, std::move (left),
				   std::move (outer_attrs), restrictions);
    case EQUAL:
      // assignment expression - binary infix (note right-to-left
      // associativity)
      return parse_assig_expr (tok, std::move (left), std::move (outer_attrs),
			       restrictions);
    case PLUS_EQ:
      /* plus-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_plus_assig_expr (tok, std::move (left),
				    std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (tok, std::move (left),
					     std::move (outer_attrs),
					     CompoundAssignmentOperator::ADD,
					     restrictions);
    case MINUS_EQ:
      /* minus-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_minus_assig_expr (tok, std::move (left),
				     std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::SUBTRACT, restrictions);
    case ASTERISK_EQ:
      /* multiply-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_mult_assig_expr (tok, std::move (left),
				    std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::MULTIPLY, restrictions);
    case DIV_EQ:
      /* division-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_div_assig_expr (tok, std::move (left),
				   std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (tok, std::move (left),
					     std::move (outer_attrs),
					     CompoundAssignmentOperator::DIVIDE,
					     restrictions);
    case PERCENT_EQ:
      /* modulo-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_mod_assig_expr (tok, std::move (left),
				   std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::MODULUS, restrictions);
    case AMP_EQ:
      /* bitwise and-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_and_assig_expr (tok, std::move (left),
				   std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::BITWISE_AND, restrictions);
    case PIPE_EQ:
      /* bitwise or-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_or_assig_expr (tok, std::move (left),
				  std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::BITWISE_OR, restrictions);
    case CARET_EQ:
      /* bitwise xor-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_xor_assig_expr (tok, std::move (left),
				   std::move (outer_attrs), restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::BITWISE_XOR, restrictions);
    case LEFT_SHIFT_EQ:
      /* left shift-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_left_shift_assig_expr (tok, std::move (left),
					  std::move (outer_attrs),
					  restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::LEFT_SHIFT, restrictions);
    case RIGHT_SHIFT_EQ:
      /* right shift-assignment expression - binary infix (note right-to-left
       * associativity) */
      /*return parse_right_shift_assig_expr (tok, std::move (left),
					   std::move (outer_attrs),
					   restrictions);*/
      return parse_compound_assignment_expr (
	tok, std::move (left), std::move (outer_attrs),
	CompoundAssignmentOperator::RIGHT_SHIFT, restrictions);
    case DOT_DOT:
      /* range exclusive expression - binary infix (no associativity)
       * either "range" or "range from" */
      return parse_led_range_exclusive_expr (tok, std::move (left),
					     std::move (outer_attrs),
					     restrictions);
    case DOT_DOT_EQ:
      /* range inclusive expression - binary infix (no associativity)
       * unambiguously RangeInclusiveExpr */
      return parse_range_inclusive_expr (tok, std::move (left),
					 std::move (outer_attrs), restrictions);
    case SCOPE_RESOLUTION:
      // path expression - binary infix? FIXME should this even be parsed
      // here?
      add_error (
	Error (tok->get_locus (),
	       "found scope resolution operator in left denotation "
	       "function - this should probably be handled elsewhere"));

      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    case DOT:
      {
	/* field expression or method call - relies on parentheses after next
	 * identifier or await if token after is "await" (unary postfix) or
	 * tuple index if token after is a decimal int literal */

	const_TokenPtr next_tok = lexer.peek_token ();
	if (next_tok->get_id () == IDENTIFIER
	    && next_tok->get_str () == Values::Keywords::AWAIT)
	  {
	    // await expression
	    return parse_await_expr (tok, std::move (left),
				     std::move (outer_attrs));
	  }
	else if (next_tok->get_id () == INT_LITERAL)
	  {
	    // tuple index expression - TODO check for decimal int literal
	    return parse_tuple_index_expr (tok, std::move (left),
					   std::move (outer_attrs),
					   restrictions);
	  }
	else if (next_tok->get_id () == FLOAT_LITERAL)
	  {
	    // Lexer has misidentified a tuple index as a float literal
	    // eg: `(x, (y, z)).1.0` -> 1.0 has been identified as a float
	    // literal. This means we should split it into three new separate
	    // tokens, the first tuple index, the dot and the second tuple
	    // index.
	    auto current_loc = next_tok->get_locus ();
	    auto str = next_tok->get_str ();
	    auto dot_pos = str.find (".");
	    auto prefix = str.substr (0, dot_pos);
	    auto suffix = str.substr (dot_pos + 1);
	    if (dot_pos == str.size () - 1)
	      lexer.split_current_token (
		{Token::make_int (current_loc, std::move (prefix),
				  CORETYPE_PURE_DECIMAL),
		 Token::make (DOT, current_loc + 1)});
	    else
	      lexer.split_current_token (
		{Token::make_int (current_loc, std::move (prefix),
				  CORETYPE_PURE_DECIMAL),
		 Token::make (DOT, current_loc + 1),
		 Token::make_int (current_loc + 2, std::move (suffix),
				  CORETYPE_PURE_DECIMAL)});
	    return parse_tuple_index_expr (tok, std::move (left),
					   std::move (outer_attrs),
					   restrictions);
	  }
	else if (next_tok->get_id () == IDENTIFIER
		 && lexer.peek_token (1)->get_id () != LEFT_PAREN
		 && lexer.peek_token (1)->get_id () != SCOPE_RESOLUTION)
	  {
	    /* field expression (or should be) - FIXME: scope resolution right
	     * after identifier should always be method, I'm pretty sure */
	    return parse_field_access_expr (tok, std::move (left),
					    std::move (outer_attrs),
					    restrictions);
	  }
	else
	  {
	    // method call (probably)
	    return parse_method_call_expr (tok, std::move (left),
					   std::move (outer_attrs),
					   restrictions);
	  }
      }
    case LEFT_PAREN:
      // function call - method call is based on dot notation first
      return parse_function_call_expr (tok, std::move (left),
				       std::move (outer_attrs), restrictions);
    case LEFT_SQUARE:
      // array or slice index expression (pseudo binary infix)
      return parse_index_expr (tok, std::move (left), std::move (outer_attrs),
			       restrictions);
    default:
      add_error (Error (tok->get_locus (),
			"found unexpected token %qs in left denotation",
			tok->get_token_description ()));

      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }
}

/* Returns the left binding power for the given ArithmeticOrLogicalExpr type.
 * TODO make constexpr? Would that even do anything useful? */
inline binding_powers
get_lbp_for_arithmetic_or_logical_expr (
  AST::ArithmeticOrLogicalExpr::ExprType expr_type)
{
  switch (expr_type)
    {
    case ArithmeticOrLogicalOperator::ADD:
      return LBP_PLUS;
    case ArithmeticOrLogicalOperator::SUBTRACT:
      return LBP_MINUS;
    case ArithmeticOrLogicalOperator::MULTIPLY:
      return LBP_MUL;
    case ArithmeticOrLogicalOperator::DIVIDE:
      return LBP_DIV;
    case ArithmeticOrLogicalOperator::MODULUS:
      return LBP_MOD;
    case ArithmeticOrLogicalOperator::BITWISE_AND:
      return LBP_AMP;
    case ArithmeticOrLogicalOperator::BITWISE_OR:
      return LBP_PIPE;
    case ArithmeticOrLogicalOperator::BITWISE_XOR:
      return LBP_CARET;
    case ArithmeticOrLogicalOperator::LEFT_SHIFT:
      return LBP_L_SHIFT;
    case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
      return LBP_R_SHIFT;
    default:
      // WTF? should not happen, this is an error
      rust_unreachable ();

      return LBP_PLUS;
    }
}

// Parses an arithmetic or logical expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_arithmetic_or_logical_expr (
  const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
  AST::ArithmeticOrLogicalExpr::ExprType expr_type,
  ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (get_lbp_for_arithmetic_or_logical_expr (expr_type),
			   AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()), expr_type, locus);
}

// Parses a binary addition expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_plus_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_PLUS, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::ADD, locus);
}

// Parses a binary subtraction expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_minus_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_MINUS, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::SUBTRACT, locus);
}

// Parses a binary multiplication expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_mult_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_MUL, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::MULTIPLY, locus);
}

// Parses a binary division expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_div_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_DIV, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::DIVIDE, locus);
}

// Parses a binary modulo expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_mod_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_MOD, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::MODULUS, locus);
}

/* Parses a binary bitwise (or eager logical) and expression (with Pratt
 * parsing). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_bitwise_and_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_AMP, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::BITWISE_AND, locus);
}

/* Parses a binary bitwise (or eager logical) or expression (with Pratt
 * parsing). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_bitwise_or_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_PIPE, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::BITWISE_OR, locus);
}

/* Parses a binary bitwise (or eager logical) xor expression (with Pratt
 * parsing). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_bitwise_xor_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_CARET, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::BITWISE_XOR, locus);
}

// Parses a binary left shift expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_left_shift_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_L_SHIFT, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::LEFT_SHIFT, locus);
}

// Parses a binary right shift expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArithmeticOrLogicalExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_right_shift_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_R_SHIFT, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ArithmeticOrLogicalExpr> (
    std::move (left), std::move (right.value ()),
    ArithmeticOrLogicalOperator::RIGHT_SHIFT, locus);
}

/* Returns the left binding power for the given ComparisonExpr type.
 * TODO make constexpr? Would that even do anything useful? */
inline binding_powers
get_lbp_for_comparison_expr (AST::ComparisonExpr::ExprType expr_type)
{
  switch (expr_type)
    {
    case ComparisonOperator::EQUAL:
      return LBP_EQUAL;
    case ComparisonOperator::NOT_EQUAL:
      return LBP_NOT_EQUAL;
    case ComparisonOperator::GREATER_THAN:
      return LBP_GREATER_THAN;
    case ComparisonOperator::LESS_THAN:
      return LBP_SMALLER_THAN;
    case ComparisonOperator::GREATER_OR_EQUAL:
      return LBP_GREATER_EQUAL;
    case ComparisonOperator::LESS_OR_EQUAL:
      return LBP_SMALLER_EQUAL;
    default:
      // WTF? should not happen, this is an error
      rust_unreachable ();

      return LBP_EQUAL;
    }
}

/* Parses a ComparisonExpr of given type and LBP. TODO find a way to only
 * specify one and have the other looked up - e.g. specify ExprType and
 * binding power is looked up? */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_comparison_expr (
  const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
  AST::ComparisonExpr::ExprType expr_type, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (get_lbp_for_comparison_expr (expr_type),
			   AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (std::move (left),
						std::move (right.value ()),
						expr_type, locus);
}

// Parses a binary equal to expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_equal_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_EQUAL, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (std::move (left),
						std::move (right.value ()),
						ComparisonOperator::EQUAL,
						locus);
}

// Parses a binary not equal to expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_not_equal_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_NOT_EQUAL, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (std::move (left),
						std::move (right.value ()),
						ComparisonOperator::NOT_EQUAL,
						locus);
}

// Parses a binary greater than expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_greater_than_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_GREATER_THAN, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (
    std::move (left), std::move (right.value ()),
    ComparisonOperator::GREATER_THAN, locus);
}

// Parses a binary less than expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_less_than_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_SMALLER_THAN, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (std::move (left),
						std::move (right.value ()),
						ComparisonOperator::LESS_THAN,
						locus);
}

// Parses a binary greater than or equal to expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_greater_equal_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_GREATER_EQUAL, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (
    std::move (left), std::move (right.value ()),
    ComparisonOperator::GREATER_OR_EQUAL, locus);
}

// Parses a binary less than or equal to expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ComparisonExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_binary_less_equal_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_SMALLER_EQUAL, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::ComparisonExpr> (
    std::move (left), std::move (right.value ()),
    ComparisonOperator::LESS_OR_EQUAL, locus);
}

// Parses a binary lazy boolean or expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::LazyBooleanExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_lazy_or_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_LOGICAL_OR, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::LazyBooleanExpr> (
    std::move (left), std::move (right.value ()),
    LazyBooleanOperator::LOGICAL_OR, locus);
}

// Parses a binary lazy boolean and expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::LazyBooleanExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_lazy_and_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_LOGICAL_AND, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::LazyBooleanExpr> (
    std::move (left), std::move (right.value ()),
    LazyBooleanOperator::LOGICAL_AND, locus);
}

// Parses a pseudo-binary infix type cast expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::TypeCastExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_type_cast_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> expr_to_cast,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED,
  ParseRestrictions restrictions ATTRIBUTE_UNUSED)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto type = parse_type_no_bounds ();
  if (!type)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: how do I get precedence put in here?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = expr_to_cast->get_locus ();

  return std::make_unique<AST::TypeCastExpr> (std::move (expr_to_cast),
					      std::move (type), locus);
}

// Parses a binary assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::AssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  location_t locus = left->get_locus ();

  return std::make_unique<AST::AssignmentExpr> (std::move (left),
						std::move (right.value ()),
						std::move (outer_attrs), locus);
}

/* Returns the left binding power for the given CompoundAssignmentExpr type.
 * TODO make constexpr? Would that even do anything useful? */
inline binding_powers
get_lbp_for_compound_assignment_expr (
  AST::CompoundAssignmentExpr::ExprType expr_type)
{
  switch (expr_type)
    {
    case CompoundAssignmentOperator::ADD:
      return LBP_PLUS;
    case CompoundAssignmentOperator::SUBTRACT:
      return LBP_MINUS;
    case CompoundAssignmentOperator::MULTIPLY:
      return LBP_MUL;
    case CompoundAssignmentOperator::DIVIDE:
      return LBP_DIV;
    case CompoundAssignmentOperator::MODULUS:
      return LBP_MOD;
    case CompoundAssignmentOperator::BITWISE_AND:
      return LBP_AMP;
    case CompoundAssignmentOperator::BITWISE_OR:
      return LBP_PIPE;
    case CompoundAssignmentOperator::BITWISE_XOR:
      return LBP_CARET;
    case CompoundAssignmentOperator::LEFT_SHIFT:
      return LBP_L_SHIFT;
    case CompoundAssignmentOperator::RIGHT_SHIFT:
      return LBP_R_SHIFT;
    default:
      // WTF? should not happen, this is an error
      rust_unreachable ();

      return LBP_PLUS;
    }
}

// Parses a compound assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_compound_assignment_expr (
  const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
  AST::CompoundAssignmentExpr::ExprType expr_type,
  ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (get_lbp_for_compound_assignment_expr (expr_type) - 1,
			   AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()), expr_type, locus);
}

// Parses a binary add-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_plus_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_PLUS_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::ADD, locus);
}

// Parses a binary minus-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_minus_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_MINUS_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::SUBTRACT, locus);
}

// Parses a binary multiplication-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_mult_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_MULT_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::MULTIPLY, locus);
}

// Parses a binary division-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_div_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_DIV_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::DIVIDE, locus);
}

// Parses a binary modulo-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_mod_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_MOD_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::MODULUS, locus);
}

// Parses a binary and-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_and_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_AMP_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::BITWISE_AND, locus);
}

// Parses a binary or-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_or_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_PIPE_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::BITWISE_OR, locus);
}

// Parses a binary xor-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_xor_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_CARET_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::BITWISE_XOR, locus);
}

// Parses a binary left shift-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_left_shift_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right
    = parse_expr (LBP_L_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::LEFT_SHIFT, locus);
}

// Parses a binary right shift-assignment expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CompoundAssignmentExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_right_shift_assig_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right
    = parse_expr (LBP_R_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::CompoundAssignmentExpr> (
    std::move (left), std::move (right.value ()),
    CompoundAssignmentOperator::RIGHT_SHIFT, locus);
}

// Parses a postfix unary await expression (with Pratt parsing).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::AwaitExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_await_expr (
  const_TokenPtr tok, std::unique_ptr<AST::Expr> expr_to_await,
  AST::AttrVec outer_attrs)
{
  /* skip "await" identifier (as "." has already been consumed in
   * parse_expression) this assumes that the identifier was already identified
   * as await */
  if (!skip_token (IDENTIFIER))
    {
      Error error (tok->get_locus (), "failed to skip %<await%> in await expr "
				      "- this is probably a deep issue");
      add_error (std::move (error));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // TODO: check inside async block in semantic analysis
  location_t locus = expr_to_await->get_locus ();

  return std::unique_ptr<AST::AwaitExpr> (
    new AST::AwaitExpr (std::move (expr_to_await), std::move (outer_attrs),
			locus));
}

/* Parses an exclusive range ('..') in left denotation position (i.e.
 * RangeFromExpr or RangeFromToExpr). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::RangeExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_led_range_exclusive_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // FIXME: this probably parses expressions accidently or whatever
  // try parsing RHS (as tok has already been consumed in parse_expression)
  // Can be nullptr, in which case it is a RangeFromExpr, otherwise a
  // RangeFromToExpr.
  restrictions.expr_can_be_null = true;
  auto right = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);

  location_t locus = left->get_locus ();

  if (!right)
    {
      // range from expr
      return std::make_unique<AST::RangeFromExpr> (std::move (left), locus);
    }
  else
    {
      return std::make_unique<AST::RangeFromToExpr> (std::move (left),
						     std::move (right.value ()),
						     locus);
    }
  // FIXME: make non-associative
}

/* Parses an exclusive range ('..') in null denotation position (i.e.
 * RangeToExpr or RangeFullExpr). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::RangeExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_nud_range_exclusive_expr (
  const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
{
  auto restrictions = ParseRestrictions ();
  restrictions.expr_can_be_null = true;

  // FIXME: this probably parses expressions accidently or whatever
  // try parsing RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);

  location_t locus = tok->get_locus ();

  if (!right)
    {
      // range from expr
      return std::make_unique<AST::RangeFullExpr> (locus);
    }
  else
    {
      return std::make_unique<AST::RangeToExpr> (std::move (right.value ()),
						 locus);
    }
  // FIXME: make non-associative
}

// Parses a full binary range inclusive expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::RangeFromToInclExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_range_inclusive_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
  AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_DOT_DOT_EQ, AST::AttrVec (), restrictions);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: make non-associative

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = left->get_locus ();

  return std::make_unique<AST::RangeFromToInclExpr> (std::move (left),
						     std::move (right.value ()),
						     locus);
}

// Parses an inclusive range-to prefix unary expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::RangeToInclExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_range_to_inclusive_expr (
  const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  auto right = parse_expr (LBP_DOT_DOT_EQ);
  if (!right)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
  // FIXME: make non-associative

  // TODO: check types. actually, do so during semantic analysis

  return std::make_unique<AST::RangeToInclExpr> (std::move (right.value ()),
						 tok->get_locus ());
}

// Parses a pseudo-binary infix tuple index expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::TupleIndexExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_tuple_index_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> tuple_expr,
  AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
{
  // parse int literal (as token already skipped)
  const_TokenPtr index_tok = expect_token (INT_LITERAL);
  if (index_tok == nullptr)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);

  std::string index = index_tok->get_str ();

  // convert to integer
  if (!index_tok->is_pure_decimal ())
    {
      Error error (index_tok->get_locus (),
		   "tuple index should be a pure decimal literal");
      add_error (std::move (error));
    }
  int index_int = atoi (index.c_str ());

  location_t locus = tuple_expr->get_locus ();

  return std::make_unique<AST::TupleIndexExpr> (std::move (tuple_expr),
						index_int,
						std::move (outer_attrs), locus);
}

// Parses a pseudo-binary infix array (or slice) index expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ArrayIndexExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_index_expr (
  const_TokenPtr, std::unique_ptr<AST::Expr> array_expr,
  AST::AttrVec outer_attrs, ParseRestrictions)
{
  // parse RHS (as tok has already been consumed in parse_expression)
  /*std::unique_ptr<AST::Expr> index_expr
    = parse_expr (LBP_ARRAY_REF, AST::AttrVec (),
    restrictions);*/
  // TODO: conceptually, should treat [] as brackets, so just parse all expr
  auto index_expr = parse_expr ();
  if (!index_expr)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);

  // skip ']' at end of array
  if (!skip_token (RIGHT_SQUARE))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = array_expr->get_locus ();

  return std::make_unique<AST::ArrayIndexExpr> (std::move (array_expr),
						std::move (index_expr.value ()),
						std::move (outer_attrs), locus);
}

// Parses a pseudo-binary infix struct field access expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::FieldAccessExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_field_access_expr (
  const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> struct_expr,
  AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
{
  /* get field name identifier (assume that this is a field access expr and
   * not await, for instance) */
  const_TokenPtr ident_tok = expect_token (IDENTIFIER);
  if (ident_tok == nullptr)
    return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);

  Identifier ident{ident_tok};

  location_t locus = struct_expr->get_locus ();

  // TODO: check types. actually, do so during semantic analysis
  return std::make_unique<AST::FieldAccessExpr> (std::move (struct_expr),
						 std::move (ident),
						 std::move (outer_attrs),
						 locus);
}

// Parses a pseudo-binary infix method call expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::MethodCallExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_method_call_expr (
  const_TokenPtr tok, std::unique_ptr<AST::Expr> receiver_expr,
  AST::AttrVec outer_attrs, ParseRestrictions)
{
  // parse path expr segment
  AST::PathExprSegment segment = parse_path_expr_segment ();
  if (segment.is_error ())
    {
      Error error (tok->get_locus (),
		   "failed to parse path expr segment of method call expr");
      add_error (std::move (error));

      return tl::unexpected<Parse::Error::Expr> (
	Parse::Error::Expr::CHILD_ERROR);
    }

  // skip left parentheses
  if (!skip_token (LEFT_PAREN))
    {
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // parse method params (if they exist)
  std::vector<std::unique_ptr<AST::Expr>> params;

  const_TokenPtr t = lexer.peek_token ();
  while (t->get_id () != RIGHT_PAREN)
    {
      auto param = parse_expr ();
      if (!param)
	{
	  Error error (t->get_locus (),
		       "failed to parse method param in method call");
	  add_error (std::move (error));

	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	}
      params.push_back (std::move (param.value ()));

      if (lexer.peek_token ()->get_id () != COMMA)
	break;

      lexer.skip_token ();
      t = lexer.peek_token ();
    }

  // skip right paren
  if (!skip_token (RIGHT_PAREN))
    {
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // TODO: check types. actually do so in semantic analysis pass.
  location_t locus = receiver_expr->get_locus ();

  return std::make_unique<AST::MethodCallExpr> (std::move (receiver_expr),
						std::move (segment),
						std::move (params),
						std::move (outer_attrs), locus);
}

// Parses a pseudo-binary infix function call expression.
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CallExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_function_call_expr (
  const_TokenPtr, std::unique_ptr<AST::Expr> function_expr,
  AST::AttrVec outer_attrs, ParseRestrictions)
{
  // parse function params (if they exist)
  std::vector<std::unique_ptr<AST::Expr>> params;

  const_TokenPtr t = lexer.peek_token ();
  while (t->get_id () != RIGHT_PAREN)
    {
      auto param = parse_expr ();
      if (!param)
	{
	  Error error (t->get_locus (),
		       "failed to parse function param in function call");
	  add_error (std::move (error));

	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	}
      params.push_back (std::move (param.value ()));

      if (lexer.peek_token ()->get_id () != COMMA)
	break;

      lexer.skip_token ();
      t = lexer.peek_token ();
    }

  // skip ')' at end of param list
  if (!skip_token (RIGHT_PAREN))
    {
      // skip somewhere?
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // TODO: check types. actually, do so during semantic analysis
  location_t locus = function_expr->get_locus ();

  return std::make_unique<AST::CallExpr> (std::move (function_expr),
					  std::move (params),
					  std::move (outer_attrs), locus);
}

/* Parses a struct expr struct with a path in expression already parsed (but
 * not
 * '{' token). */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::StructExprStruct>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_struct_expr_struct_partial (
  AST::PathInExpression path, AST::AttrVec outer_attrs)
{
  // assume struct expr struct (as struct-enum disambiguation requires name
  // lookup) again, make statement if final ';'
  if (!skip_token (LEFT_CURLY))
    {
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // parse inner attributes
  AST::AttrVec inner_attrs = parse_inner_attributes ();

  // branch based on next token
  const_TokenPtr t = lexer.peek_token ();
  location_t path_locus = path.get_locus ();
  switch (t->get_id ())
    {
    case RIGHT_CURLY:
      // struct with no body
      lexer.skip_token ();

      return std::make_unique<AST::StructExprStruct> (std::move (path),
						      std::move (inner_attrs),
						      std::move (outer_attrs),
						      path_locus);
    case DOT_DOT:
      /* technically this would give a struct base-only struct, but this
       * algorithm should work too. As such, AST type not happening. */
    case IDENTIFIER:
    case HASH:
    case INT_LITERAL:
      {
	// struct with struct expr fields

	// parse struct expr fields
	std::vector<std::unique_ptr<AST::StructExprField>> fields;

	while (t->get_id () != RIGHT_CURLY && t->get_id () != DOT_DOT)
	  {
	    auto field = parse_struct_expr_field ();
	    if (!field
		&& field.error () != Parse::Error::StructExprField::STRUCT_BASE)
	      {
		Error error (t->get_locus (),
			     "failed to parse struct (or enum) expr field");
		add_error (std::move (error));

		return tl::unexpected<Parse::Error::Expr> (
		  Parse::Error::Expr::CHILD_ERROR);
	      }

	    // DEBUG:
	    rust_debug ("struct/enum expr field validated to not be null");

	    fields.push_back (std::move (field.value ()));

	    // DEBUG:
	    rust_debug ("struct/enum expr field pushed back");

	    if (lexer.peek_token ()->get_id () != COMMA)
	      {
		// DEBUG:
		rust_debug ("lack of comma detected in struct/enum expr "
			    "fields - break");
		break;
	      }
	    lexer.skip_token ();

	    // DEBUG:
	    rust_debug ("struct/enum expr fields comma skipped ");

	    t = lexer.peek_token ();
	  }

	// DEBUG:
	rust_debug ("struct/enum expr about to parse struct base ");

	// parse struct base if it exists
	AST::StructBase struct_base = AST::StructBase::error ();
	if (lexer.peek_token ()->get_id () == DOT_DOT)
	  {
	    location_t dot_dot_location = lexer.peek_token ()->get_locus ();
	    lexer.skip_token ();

	    // parse required struct base expr
	    auto base_expr = parse_expr ();
	    if (!base_expr)
	      {
		Error error (lexer.peek_token ()->get_locus (),
			     "failed to parse struct base expression in struct "
			     "expression");
		add_error (std::move (error));

		return tl::unexpected<Parse::Error::Expr> (
		  Parse::Error::Expr::CHILD_ERROR);
	      }

	    // DEBUG:
	    rust_debug ("struct/enum expr - parsed and validated base expr");

	    struct_base = AST::StructBase (std::move (base_expr.value ()),
					   dot_dot_location);

	    // DEBUG:
	    rust_debug ("assigned struct base to new struct base ");
	  }

	if (!skip_token (RIGHT_CURLY))
	  {
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::MALFORMED);
	  }

	// DEBUG:
	rust_debug (
	  "struct/enum expr skipped right curly - done and ready to return");

	return std::make_unique<AST::StructExprStructFields> (
	  std::move (path), std::move (fields), path_locus,
	  std::move (struct_base), std::move (inner_attrs),
	  std::move (outer_attrs));
      }
    default:
      add_error (
	Error (t->get_locus (),
	       "unrecognised token %qs in struct (or enum) expression - "
	       "expected %<}%>, identifier, integer literal, or %<..%>",
	       t->get_token_description ()));

      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }
}

/* Parses a struct expr tuple with a path in expression already parsed (but
 * not
 * '(' token).
 * FIXME: this currently outputs a call expr, as they cannot be disambiguated.
 * A better solution would be to just get this to call that function directly.
 * */
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::CallExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_struct_expr_tuple_partial (
  AST::PathInExpression path, AST::AttrVec outer_attrs)
{
  if (!skip_token (LEFT_PAREN))
    {
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  AST::AttrVec inner_attrs = parse_inner_attributes ();

  std::vector<std::unique_ptr<AST::Expr>> exprs;

  const_TokenPtr t = lexer.peek_token ();
  while (t->get_id () != RIGHT_PAREN)
    {
      // parse expression (required)
      auto expr = parse_expr ();
      if (!expr)
	{
	  Error error (t->get_locus (), "failed to parse expression in "
					"struct (or enum) expression tuple");
	  add_error (std::move (error));

	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	}
      exprs.push_back (std::move (expr.value ()));

      if (lexer.peek_token ()->get_id () != COMMA)
	break;

      lexer.skip_token ();

      t = lexer.peek_token ();
    }

  if (!skip_token (RIGHT_PAREN))
    {
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  location_t path_locus = path.get_locus ();

  auto pathExpr = std::make_unique<AST::PathInExpression> (std::move (path));

  return std::make_unique<AST::CallExpr> (std::move (pathExpr),
					  std::move (exprs),
					  std::move (outer_attrs), path_locus);
}

// Parses a closure expression with pratt parsing (from null denotation).
template <typename ManagedTokenSource>
tl::expected<std::unique_ptr<AST::ClosureExpr>, Parse::Error::Expr>
Parser<ManagedTokenSource>::parse_closure_expr_pratt (const_TokenPtr tok,
						      AST::AttrVec outer_attrs)
{
  // TODO: does this need pratt parsing (for precedence)? probably not, but
  // idk
  location_t locus = tok->get_locus ();
  bool has_move = false;
  if (tok->get_id () == MOVE)
    {
      has_move = true;
      tok = lexer.peek_token ();
      lexer.skip_token ();
      // skip token and reassign
    }

  // handle parameter list
  std::vector<AST::ClosureParam> params;

  switch (tok->get_id ())
    {
    case OR:
      // no parameters, don't skip token
      break;
    case PIPE:
      {
	// actually may have parameters
	// don't skip token
	const_TokenPtr t = lexer.peek_token ();
	while (t->get_id () != PIPE)
	  {
	    AST::ClosureParam param = parse_closure_param ();
	    if (param.is_error ())
	      {
		// TODO is this really an error?
		Error error (t->get_locus (), "could not parse closure param");
		add_error (std::move (error));

		return tl::unexpected<Parse::Error::Expr> (
		  Parse::Error::Expr::CHILD_ERROR);
	      }
	    params.push_back (std::move (param));

	    if (lexer.peek_token ()->get_id () != COMMA)
	      {
		if (lexer.peek_token ()->get_id () == OR)
		  lexer.split_current_token (PIPE, PIPE);
		// not an error but means param list is done
		break;
	      }
	    // skip comma
	    lexer.skip_token ();

	    if (lexer.peek_token ()->get_id () == OR)
	      lexer.split_current_token (PIPE, PIPE);

	    t = lexer.peek_token ();
	  }

	if (!skip_token (PIPE))
	  {
	    return tl::unexpected<Parse::Error::Expr> (
	      Parse::Error::Expr::MALFORMED);
	  }
	break;
      }
    default:
      add_error (Error (tok->get_locus (),
			"unexpected token %qs in closure expression - expected "
			"%<|%> or %<||%>",
			tok->get_token_description ()));

      // skip somewhere?
      return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::MALFORMED);
    }

  // again branch based on next token
  tok = lexer.peek_token ();
  if (tok->get_id () == RETURN_TYPE)
    {
      // must be return type closure with block expr

      // skip "return type" token
      lexer.skip_token ();

      // parse actual type, which is required
      auto type = parse_type_no_bounds ();
      if (!type)
	{
	  // error
	  Error error (tok->get_locus (), "failed to parse type for closure");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	}

      // parse block expr, which is required
      auto block = parse_block_expr ();
      if (!block)
	{
	  // error
	  Error error (lexer.peek_token ()->get_locus (),
		       "failed to parse block expr in closure");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	}

      return std::make_unique<AST::ClosureExprInnerTyped> (
	std::move (type), std::move (block.value ()), std::move (params), locus,
	has_move, std::move (outer_attrs));
    }
  else
    {
      // must be expr-only closure

      // parse expr, which is required
      auto expr = parse_expr ();
      if (!expr)
	{
	  Error error (tok->get_locus (),
		       "failed to parse expression in closure");
	  add_error (std::move (error));

	  // skip somewhere?
	  return tl::unexpected<Parse::Error::Expr> (
	    Parse::Error::Expr::CHILD_ERROR);
	}

      return std::make_unique<AST::ClosureExprInner> (std::move (expr.value ()),
						      std::move (params), locus,
						      has_move,
						      std::move (outer_attrs));
    }
}

} // namespace Rust