vimlrs 0.2.1

Faithful Rust port of the Vimscript (VimL) interpreter, from the Neovim C eval engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
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
//! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
//! EXTENSION — NO `vendor/` COUNTERPART. Recursive-descent parser building the
//! synthesis AST. The expression grammar transcribes the precedence ladder in
//! `eval.c` (`eval1`…`eval7`) but builds a tree instead of evaluating inline:
//!
//! ```text
//! eval1  expr2 ? expr1 : expr1   |  expr2 ?? expr1
//! eval2  expr3 || expr3
//! eval3  expr4 && expr4
//! eval4  expr5 (== != =~ !~ > >= < <= is isnot) expr5   (no assoc)
//! eval5  expr6 (+ - . ..) expr6
//! eval6  expr7 (* / %) expr7
//! eval7  (! - +)* primary subscripts
//! ```
//! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

use crate::viml_ast::{ArithOp, Expr, ForVars, LetTarget, Stmt, UnaryOp, UnletArg};
use crate::viml_lexer::{lex, CaseFlag, CmpOp, InterpPart, Tok, Token, VimlError};
use std::cell::Cell;

thread_local! {
    /// Whether the code currently being parsed is vim9 (`:vim9script` script or a
    /// `def … enddef` body). In vim9, dict literals `{key: val}` use BARE literal
    /// keys (`{a: 1}` → key `"a"`), not the legacy expression-keyed form where the
    /// key is evaluated. Set for the parse duration by [`Vim9Guard`].
    static VIM9: Cell<bool> = const { Cell::new(false) };
}

/// True when the parser is in a vim9 region (see [`VIM9`]).
fn vim9_active() -> bool {
    VIM9.with(|f| f.get())
}

/// RAII guard that switches [`VIM9`] for the duration of a parse (script body,
/// `def` body, or legacy `function` body) and restores the previous value on
/// drop, so nested regions (a legacy `:function` inside a `:vim9script`, or a
/// `:def` inside a legacy script) each parse in their own mode.
struct Vim9Guard(bool);

impl Vim9Guard {
    fn enter(on: bool) -> Self {
        Vim9Guard(VIM9.with(|f| f.replace(on)))
    }
}

impl Drop for Vim9Guard {
    fn drop(&mut self) {
        VIM9.with(|f| f.set(self.0));
    }
}

/// True when a script is a `:vim9script` — its first non-blank logical line's
/// command word is `vim9script`. Mirrors the detection in [`Lines::new`].
fn script_is_vim9(src: &str) -> bool {
    src.lines()
        .map(str::trim)
        .find(|l| !l.is_empty())
        .is_some_and(|l| l.split(char::is_whitespace).next() == Some("vim9script"))
}

/// The small set of names Phase 3 recognizes as builtin function calls. The
/// full `funcs.c` table is ported in Phase 5.
pub const PHASE3_BUILTINS: &[&str] = &[
    "len",
    "type",
    "string",
    "empty",
    "abs",
    "str2nr",
    "str2float",
    "float2nr",
];

/// Parse one statement line into a [`Stmt`].
///
/// Inline trailing comments (`echo 1  " note`) are not stripped in Phase 3: a
/// `"` is a comment only where the command grammar expects end-of-command, but
/// in expression position it opens a string. Full-line comments are skipped by
/// the source splitter before this is called.
pub fn parse_stmt(line: &str) -> Result<Stmt, VimlError> {
    // Strip leading command modifiers (`silent`, `silent!`, `verbose 9`,
    // `noautocmd`, `keepjumps`, …). They change how a command runs, not what it
    // is, and real vimrcs use them constantly (`silent! colorscheme x`). A bare
    // modifier with no command (`silent`) becomes a no-op.
    let line = strip_command_modifiers(line.trim());
    if line.is_empty() {
        return Ok(Stmt::Expr(Expr::Number(0)));
    }
    // `:vim9script [noclear]` — switches the script to vim9 mode (a no-op leaf;
    // the mode's parse effects are applied in `Lines::new`). Matched here because
    // its command word ends at the `9`, which `cmd_word` treats as a boundary.
    if line
        .split(|c: char| c.is_whitespace())
        .next()
        .is_some_and(|w| w == "vim9script")
    {
        return Ok(Stmt::Expr(Expr::Number(0)));
    }
    let cmd_end = line
        .find(|c: char| !c.is_ascii_alphabetic())
        .unwrap_or(line.len());
    let cmd = &line[..cmd_end];
    let rest = line[cmd_end..].trim_start();

    match cmd {
        "echo" | "ec" => Ok(Stmt::Echo(parse_expr_list(rest)?)),
        "echon" => Ok(Stmt::Echon(parse_expr_list(rest)?)),
        // `:echomsg`/`:echoerr` (Vim abbreviations `echom`, `echoe`/`echoer`)
        // both evaluate and print their expression list; they are modelled as
        // `:echo` here (same simplification already used for `:echomsg`). Adding
        // `:echoerr` stops an unrecognized `echoerr` in a function body from
        // falling through to `parse_expr` and aborting the `:function`.
        "echomsg" | "echom" | "echoerr" | "echoer" | "echoe" => {
            Ok(Stmt::Echo(parse_expr_list(rest)?))
        }
        // `:execute` accepts every prefix down to `:exe` (verified against Vim
        // 9.2: `exe`/`exec`/`execu`/`execut`/`execute` all run). Missing the
        // intermediate forms made `exec '…'` fall through to `parse_expr`, which
        // aborts the enclosing function definition and leaks its body to global
        // scope (E461 on `l:` vars).
        "execute" | "execut" | "execu" | "exec" | "exe" => {
            Ok(Stmt::Execute(parse_expr_list(rest)?))
        }
        "set" | "se" | "setlocal" | "setl" | "setglobal" | "setg" => {
            Ok(Stmt::Set(rest.to_string()))
        }
        "source" | "so" => Ok(Stmt::Source(rest.trim().to_string())),
        "unlet" | "unl" => {
            // `:unlet[!] x y …` — the optional `!` suppresses the missing-var
            // error. Each argument is a bare name or a List/Dict element target
            // (`l[i]` / `d.key`), matching `do_unlet_var()` (vendor/eval/vars.c).
            let args = rest.trim_start_matches('!').trim();
            split_unlet_args(args)
                .into_iter()
                .map(parse_unlet_arg)
                .collect::<Result<Vec<_>, _>>()
                .map(Stmt::Unlet)
        }
        "let" => parse_let(rest),
        // vim9 `:var {name}[: type] = {expr}` declare-and-assign like `:let`. The
        // `: type` annotation is parsed and discarded (checking/coercion deferred).
        // A type-only `:var x: number` (no initializer) default-inits to the
        // type's zero value ([`vim9_var_decl`]). RUST-PORT NOTE: real Vim rejects
        // `:var` in a legacy script (E1124); this leaf accepts it everywhere.
        "var" => parse_let(&vim9_var_decl(rest)),
        // `:final {name}[: type] = {expr}` — like `:var` but a value is REQUIRED
        // (real Vim: E1125 on a type-only `:final`), so no default-init here.
        "final" => parse_let(&strip_vim9_type(rest)),
        // A `:function` that reaches here (not caught as a block opener in
        // `parse_one` — e.g. behind a modifier, `silent function`) with no
        // parameter-list `(` is the listing/show command, not a definition:
        // no-op editor-less. (`:function {name}(…)` behind a modifier is not a
        // leaf and is left to error, matching the pre-existing limitation.)
        "function" | "fu" | "fun" | "func" | "funct" | "functi" | "functio"
            if !rest.contains('(') =>
        {
            Ok(Stmt::Expr(Expr::Number(0)))
        }
        // `:const {name} = {expr}` assigns like `:let`. RUST-PORT NOTE: Vim also
        // locks the variable (reassigning is E741); that immutability is not yet
        // enforced here — `:const` parses and assigns as `:let`.
        "const" | "cons" => parse_let(&strip_vim9_type(rest)),
        "call" => Ok(Stmt::Call(parse_expr(strip_legacy_trailing_comment(rest))?)),
        "eval" => Ok(Stmt::Expr(parse_expr(strip_legacy_trailing_comment(rest))?)),
        "break" => Ok(Stmt::Break),
        "continue" | "cont" => Ok(Stmt::Continue),
        "finish" | "finis" | "fini" => Ok(Stmt::Finish),
        "return" => Ok(if rest.trim().is_empty() {
            Stmt::Return(None)
        } else {
            Stmt::Return(Some(parse_expr(strip_legacy_trailing_comment(rest))?))
        }),
        "throw" => Ok(Stmt::Throw(parse_expr(strip_legacy_trailing_comment(
            rest,
        ))?)),
        // `:command[!] …` defines a user command; `:delcommand` removes one.
        // (`command(`/`delcommand(` are not builtins, but guard anyway.)
        "command" | "comm" | "com" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::CommandDef(line[cmd.len()..].trim_start().to_string()))
        }
        "delcommand" | "delc" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::CommandDel(rest.to_string()))
        }
        // `:delf[unction][!] {name}` removes a user function. The raw remainder
        // (the run-time handler splits off a leading `!`) carries the name; a
        // `delfunction(` form is an expression call, so guard on `(`.
        "delfunction" | "delfunctio" | "delfuncti" | "delfunct" | "delfunc" | "delfun" | "delf"
            if !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::DelFunction(
                line[cmd.len()..].trim_start().to_string(),
            ))
        }
        // `:autocmd`/`:augroup`/`:doautocmd` (with abbreviations).
        "autocmd" | "autocm" | "autoc" | "auto" | "au" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::Autocmd(line[cmd.len()..].trim_start().to_string()))
        }
        "augroup" | "aug" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::Augroup(rest.to_string()))
        }
        "doautocmd" | "doau" | "doautoall" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::Doautocmd(rest.to_string()))
        }
        // `:map`-family commands (`nmap`/`inoremap`/`vunmap`/`mapclear`/`map!`).
        // The bare `map`/`unmap`/… forms collide with the `map()`/`filter()`
        // builtins, so a name immediately followed by `(` stays an expression.
        _ if is_map_command(cmd) && !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::Map(line.to_string()))
        }
        // `:colorscheme {name}` / `:colo` — the bare form (no name) is a query.
        "colorscheme" | "colo" | "colors" | "colorsc" | "colorsch" | "colorsche" | "colorschem"
            if !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::Colorscheme(rest.trim().to_string()))
        }
        // `:highlight`/`:hi` — define or link a highlight group. `:hi` on its own
        // (or `:hi {group}` with no keys) is a listing query in real vim; we keep
        // the raw args and let the runtime decide.
        "highlight" | "hi" | "highligh" | "highlig" | "highli" | "highl" | "high" | "hig"
            if !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::Highlight(line[cmd.len()..].trim_start().to_string()))
        }
        // `:syntax`/`:syn` and `:filetype`/`:filet` — recognized so real vimrc
        // files parse. Standalone they are no-ops (zemacs highlights and detects
        // filetypes itself); an embedding editor may hook them.
        "syntax" | "syn" | "synta" | "synt" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::Syntax(rest.trim().to_string()))
        }
        // `:filet` is the shortest form (`:file` is a different command).
        "filetype" | "filetyp" | "filety" | "filet" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::Filetype(rest.trim().to_string()))
        }
        // `:normal[!] {keys}` runs normal-mode keys against the buffer, dispatched
        // by `do_excmd` at run time. Recognized even without a leading `:` so a
        // function body line like `normal! el` parses and the function defines —
        // otherwise it fell through to `parse_expr`, aborting the whole `:function`
        // and leaking its body to global scope.
        "normal" | "norm" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::ExCmd(line.to_string()))
        }
        // `:echohl {group}` sets the highlight group for later `:echo` output.
        // Its argument is a group NAME, not an expression, so it can't go through
        // the `:echo` path (that would evaluate the name as a variable). Routed to
        // `do_excmd`'s `ex_echohl` handler; recognized even bare so a function body
        // line like `echohl ErrorMsg` parses instead of aborting the `:function`.
        "echohl" | "echoh" if !line[cmd.len()..].starts_with('(') => {
            Ok(Stmt::ExCmd(line.to_string()))
        }
        // Screen/session/mark commands (`:redraw[!]`, `:redir`, `:runtime`,
        // `:mark`, `:nohlsearch`) — dispatched (or no-op'd) by `do_excmd`.
        // Recognized bare so a function body line like `redraw!` or `redir => x`
        // parses instead of being mis-read as an expression and aborting the
        // `:function`. `:noh[lsearch]` (`:h :noh`) clears search highlight — a
        // no-op editor-less; recognized so a config/syntax line `nohlsearch`
        // parses instead of falling through to `parse_expr` and erroring E121.
        "redraw" | "redr" | "redra" | "redraws" | "redrawstatus" | "redrawt" | "redrawtabline"
        | "redir" | "redi" | "runtime" | "ru" | "run" | "runt" | "runti" | "runtim" | "mark"
        | "ma" | "mar" | "noh" | "nohl" | "nohls" | "nohlse" | "nohlsea" | "nohlsear"
        | "nohlsearc" | "nohlsearch"
            if !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::ExCmd(line.to_string()))
        }
        // Fold-view commands (`ex_docmd.c` → `ex_fold`/`ex_foldopen`): `:fo[ld]`
        // creates a fold, `:foldo[pen][!]` opens folds, `:foldc[lose][!]` closes
        // them. Folds are window-view state that a standalone eval engine does not
        // have, so `do_excmd` no-ops them — but they MUST parse as Ex commands so a
        // config/syntax line like `syntax/cdl.vim`'s `%foldo!` (whole-file range +
        // recursive open) is handled instead of falling through to `parse_expr`
        // (which would raise E121 / E492 on the fold word).
        "fold" | "fo" | "fol" | "foldopen" | "foldo" | "foldop" | "foldope" | "foldclose"
        | "foldc" | "foldcl" | "foldclo" | "foldclos"
            if !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::ExCmd(line.to_string()))
        }
        // `:edit`/`:ed` (load a file / reload) and buffer-list navigation
        // (`:bnext`, `:bprevious`, `:bfirst`, `:blast`, `:buffer`, …) — dispatched
        // by `do_excmd`. Recognized bare so a body line like `edit #` or `bnext`
        // parses instead of `parse_expr` choking on it and aborting the function.
        "edit" | "ed" | "bnext" | "bn" | "bne" | "bprevious" | "bp" | "bprev" | "bNext" | "bN"
        | "bfirst" | "bf" | "blast" | "bl" | "buffer" | "bu" | "buf" | "bmodified" | "bm"
        | "bmod" | "ball" | "ba"
            if !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::ExCmd(line.to_string()))
        }
        // Script-language interface commands (`:python`/`:py3`/`:ruby`/`:perl`/
        // `:lua`/`:tcl`/`:mzscheme` and their `do`/`file` variants) run an embedded
        // interpreter. Editor-less — with the interface uncompiled (`has('ruby')`
        // etc. are 0) — they are never executed, but they MUST parse as Ex commands
        // (no-op'd by `do_excmd`) so a `:ruby …` line inside a not-taken branch
        // (ftplugin/ruby.vim's `if has('ruby') && has('win32')`) does not fail to
        // parse and drop its whole enclosing `:if` block via the tolerant parser.
        _ if is_script_lang_cmd(line) => Ok(Stmt::ExCmd(line.to_string())),
        // A `:`-prefixed line, or a `%`-prefixed line (`%s/…`), is an Ex command
        // with an optional line range. Neither can begin a valid expression
        // statement, so this is safe; unrecognized Ex commands fall back to
        // running as an ordinary statement at run time.
        // A leading `!` is the `:!{cmd}` shell command (`:h :!`); `do_excmd`
        // treats a range-less one as a handled no-op. Recognized here so a bang
        // command — especially with shell redirection (`!mkdir … > /dev/null
        // 2>&1`) — parses as a statement instead of being mis-read as the `!`
        // (logical-not) expression, which aborts the enclosing `:function`.
        // A line beginning with `'` is a mark-address Ex command (`:'<`, `:'>`,
        // `:'<,'>s/…`) — never a bare string, which isn't a valid statement. Route
        // it to `do_excmd` so it parses; otherwise `parse_expr` reads it as an
        // unterminated string literal and aborts the enclosing `:function`.
        // A line beginning with an ASCII digit is *usually* a line-range Ex
        // command: Vim's `do_one_cmd` reads a leading number as the range's first
        // address (`1print`, `1,1fold`), so a body line like `1,1print` must
        // route to `do_excmd` instead of falling through to `parse_expr`, which
        // chokes on the trailing command word and aborts the enclosing
        // `:function`. But vimlrs's eval entrypoint also evaluates a bare
        // top-level expression, and `3 + 4` / `2 * 8` parse as *complete*
        // expressions. Prefer the expression reading whenever the whole line
        // parses as one (no trailing tokens); only a line the expression grammar
        // rejects — a range comma or a trailing command word (`1,1print`,
        // `1print`) — falls through to `do_excmd`.
        _ if line.starts_with(|c: char| c.is_ascii_digit()) => match parse_expr(line) {
            Ok(e) => Ok(Stmt::Expr(e)),
            Err(_) => Ok(Stmt::ExCmd(line.to_string())),
        },
        // A `:`-prefixed line, a `%`-prefixed line (`%s/…`), a bang (`:!{cmd}`),
        // or a mark-address line (`'<,'>s/…`) is an Ex command with an optional
        // range — none can begin a valid expression statement, so routing them to
        // `do_excmd` is safe.
        _ if line.starts_with(':')
            || line.starts_with('!')
            || line.starts_with('\'')
            || (line.starts_with('%')
                && line[1..].starts_with(|c: char| c.is_ascii_alphabetic())) =>
        {
            Ok(Stmt::ExCmd(line.to_string()))
        }
        // vim9 bare assignment to an already-declared variable: `name = expr`,
        // `name += expr`, `d[key] = expr`, `g:x = expr` — vim9 assigns without a
        // `:let`/`:var` keyword. Legacy vimscript requires `:let`, so this fires
        // only in a vim9 region; [`is_vim9_assignment`] rejects user commands
        // (`MyCmd key=val`) and comparisons. Kept ahead of the uppercase
        // user-command arm so a CamelCase script var (`Total = 0`) assigns.
        _ if vim9_active() && is_vim9_assignment(line) => parse_let(line),
        // A command word starting with an uppercase letter is a user-command
        // invocation (`:Foo args`), resolved at run time. A name immediately
        // followed by `(` is a funcref call expression, not a command.
        _ if cmd.starts_with(|c: char| c.is_ascii_uppercase())
            && !line[cmd.len()..].starts_with('(') =>
        {
            Ok(Stmt::UserCmd(line.to_string()))
        }
        _ => Ok(Stmt::Expr(parse_expr(line)?)),
    }
}

/// The `:h :command-modifiers` (and their common abbreviations) that may prefix
/// any Ex command. They set execution context (silencing, verbosity, split
/// direction, …) and are stripped before the command is parsed.
const CMD_MODIFIERS: &[&str] = &[
    "silent",
    "sil",
    "unsilent",
    "uns",
    "verbose",
    "verb",
    "noautocmd",
    "noa",
    "keepmarks",
    "keepm",
    "keepjumps",
    "keepj",
    "keepalt",
    "keepa",
    "keeppatterns",
    "keepp",
    "lockmarks",
    "lockm",
    "noswapfile",
    "nos",
    "sandbox",
    "sandb",
    "browse",
    "bro",
    "confirm",
    "conf",
    "hide",
    "hid",
    "aboveleft",
    "abo",
    "belowright",
    "bel",
    "botright",
    "bo",
    "topleft",
    "to",
    "leftabove",
    "lefta",
    "rightbelow",
    "rightb",
    "vertical",
    "vert",
    "horizontal",
    "hor",
    "tab",
];

/// Strip a leading run of command modifiers from an Ex command line, returning
/// the remaining command text. Each modifier is a standalone word (optionally
/// with a `!`, e.g. `silent!`); `verbose`/`tab` may carry a numeric count
/// (`verbose 15 …`). A line that is only modifiers strips to empty.
fn strip_command_modifiers(mut line: &str) -> &str {
    loop {
        line = line.trim_start();
        let end = line
            .find(|c: char| !c.is_ascii_alphabetic())
            .unwrap_or(line.len());
        if end == 0 {
            break;
        }
        let word = &line[..end];
        if !CMD_MODIFIERS.contains(&word) {
            break;
        }
        let after = &line[end..];
        // The modifier must be a standalone token: the next char is `!`, a space,
        // or end-of-line. (`silentfoo` is an identifier, not the modifier.)
        let mut rest = match after.chars().next() {
            None => "",
            Some('!') => &after[1..],
            Some(c) if c.is_whitespace() => after,
            _ => break,
        };
        // `verbose`/`tab` take an optional numeric count.
        if matches!(word, "verbose" | "verb" | "tab") {
            let r = rest.trim_start();
            let ne = r.find(|c: char| !c.is_ascii_digit()).unwrap_or(r.len());
            if ne > 0 {
                rest = &r[ne..];
            }
        }
        line = rest;
    }
    line
}

/// Whether `cmd` is the (alphabetic) word of a `:map`-family command — any of
/// `[nivxsoctl]?(map|noremap|unmap|mapclear)`. The optional trailing `!` of
/// `map!`/`noremap!`/`unmap!` is not part of `cmd` (it is non-alphabetic), so a
/// bare `map`/`noremap`/`unmap`/`mapclear` also matches here.
fn is_map_command(cmd: &str) -> bool {
    let prefix = cmd
        .strip_suffix("mapclear")
        .or_else(|| cmd.strip_suffix("noremap"))
        .or_else(|| cmd.strip_suffix("unmap"))
        .or_else(|| cmd.strip_suffix("map"));
    matches!(
        prefix,
        Some("" | "n" | "i" | "v" | "x" | "s" | "o" | "c" | "t" | "l")
    )
}

/// Whether `line` invokes one of Vim's script-language interface Ex commands —
/// `:python`/`:py`, `:python3`/`:py3`, `:pythonx`/`:pyx`, `:perl`, `:ruby`,
/// `:lua`, `:tcl`, `:mzscheme`/`:mz`, and their `do`/`file` variants. The command
/// name may carry a trailing digit (`python3`, `py3`), which `cmd_word` splits
/// off, so it is matched on the leading alphanumeric run instead. A name directly
/// followed by `(` is a funcref call expression, not a command. Shared with
/// `do_excmd` so the runtime no-ops exactly the set the parser routes to `ExCmd`.
pub(crate) fn is_script_lang_cmd(line: &str) -> bool {
    let line = line.trim_start();
    let end = line
        .find(|c: char| !c.is_ascii_alphanumeric())
        .unwrap_or(line.len());
    if line[end..].starts_with('(') {
        return false;
    }
    // A short name (`py`/`mz`) or a full one directly followed by an assignment
    // operator (`py = 7`, `lua += 1`) is a vim9 assignment to a same-named
    // variable, not the interface command — real Vim reassigns it. Decline so the
    // vim9-assignment arm handles it. (`=<<` heredoc-assign counts; `==`/`=~`
    // comparisons do not — a bare `cmd == …` is not a valid statement anyway.)
    let after = line[end..].trim_start();
    let is_assign = matches!(
        after.as_bytes().first(),
        Some(b'+' | b'-' | b'*' | b'/' | b'%')
    ) && after[1..].starts_with('=')
        || after.starts_with("..=")
        || after.starts_with(".=")
        || (after.starts_with('=') && !after.starts_with("==") && !after.starts_with("=~"));
    if is_assign {
        return false;
    }
    matches!(
        &line[..end],
        "python"
            | "py"
            | "pydo"
            | "pyfile"
            | "python3"
            | "py3"
            | "py3do"
            | "py3file"
            | "pythonx"
            | "pyx"
            | "pyxdo"
            | "pyxfile"
            | "perl"
            | "perldo"
            | "ruby"
            | "rubydo"
            | "rubyfile"
            | "lua"
            | "luado"
            | "luafile"
            | "tcl"
            | "tcldo"
            | "tclfile"
            | "mzscheme"
            | "mz"
            | "mzfile"
    )
}

/// Split a statement line into its leading command word (ASCII letters) and the
/// remaining text. A line starting with non-alphabetic text is a bare
/// expression (empty command word).
fn cmd_word(line: &str) -> (&str, &str) {
    let line = line.trim();
    let end = line
        .find(|c: char| !c.is_ascii_alphabetic())
        .unwrap_or(line.len());
    (&line[..end], line[end..].trim_start())
}

/// Whether `cmd` closes or continues a block (so it must be handled by the
/// block parser, never as a leaf statement).
fn is_block_terminator(cmd: &str) -> bool {
    matches!(
        canon_block_kw(cmd),
        "endif"
            | "elseif"
            | "else"
            | "endwhile"
            | "endfor"
            | "endfunction"
            | "enddef"
            | "catch"
            | "finally"
            | "endtry"
    )
}

/// Normalize a command word to its canonical block-control keyword, resolving
/// Vim's command abbreviations (`fu`→`function`, `endw`→`endwhile`, `en`→`endif`,
/// `cat`→`catch`, …). The abbreviation sets match Vim 9.2's `fullcommand()`
/// exactly, including the gaps where a prefix resolves to a *different* command
/// (`fo`→`fold`, `tr`→`trewind`, `final`→`final`, `i`→`insert`) — so these are
/// explicit sets, never prefix tests. A word that is not a block keyword is
/// returned unchanged. Every block-structure decision routes through this so the
/// openers and terminators accept the same forms Vim does; missing one made an
/// abbreviated `endw`/`endf` leak out as an undefined-variable expression and
/// desync the enclosing block.
fn canon_block_kw(cmd: &str) -> &str {
    match cmd {
        "fu" | "fun" | "func" | "funct" | "functi" | "functio" | "function" => "function",
        "endf" | "endfu" | "endfun" | "endfunc" | "endfunct" | "endfuncti" | "endfunctio"
        | "endfunction" => "endfunction",
        "wh" | "whi" | "whil" | "while" => "while",
        "endw" | "endwh" | "endwhi" | "endwhil" | "endwhile" => "endwhile",
        "for" => "for",
        "endfo" | "endfor" => "endfor",
        "if" => "if",
        "elsei" | "elseif" => "elseif",
        "el" | "els" | "else" => "else",
        "en" | "end" | "endi" | "endif" => "endif",
        "try" => "try",
        "cat" | "catc" | "catch" => "catch",
        "fina" | "finall" | "finally" => "finally",
        "endt" | "endtr" | "endtry" => "endtry",
        other => other,
    }
}

/// Whether `cmd` OPENS a multi-line block: the legacy `:if`/`:while`/`:for`/
/// `:function`/`:try` (routed through `canon_block_kw` so every Vim abbreviation
/// `fu`/`wh`/… counts) plus the vim9 definition blocks `:def`/`:enum`/`:class`/
/// `:interface`. Used by the tolerant parser to consume a whole block as one
/// unit when its body fails to parse.
fn is_block_opener(cmd: &str) -> bool {
    matches!(
        canon_block_kw(cmd),
        "if" | "while" | "for" | "function" | "try" | "def" | "enum" | "class" | "interface"
    )
}

/// The command word that determines a line's block role, seen through any
/// leading command modifiers (`silent`/`verbose`/…) and a vim9 `export` marker.
/// `export def`/`export function` open a block exactly as the bare forms do, so
/// the tolerant parser must classify them as openers when it skips a body that
/// failed to parse — otherwise the raw command word is `export` (not a block
/// keyword) and the block's inner statements leak out to run at the top level.
fn block_cmd_word(line: &str) -> &str {
    let (cmd, rest) = cmd_word(strip_command_modifiers(line));
    if cmd == "export" {
        cmd_word(rest).0
    } else {
        cmd
    }
}

/// Whether `cmd` is a *final* block terminator — the keyword that closes a block
/// for good (`:endif`/`:endwhile`/`:endfor`/`:endfunction`/`:endtry`, and the
/// vim9 `:enddef`/`:endenum`/`:endclass`/`:endinterface`). The mid-block
/// continuations (`:else`/`:elseif`/`:catch`/`:finally`) are NOT finals: they
/// neither open nor close a block, so they leave nesting depth unchanged when
/// balancing openers against terminators.
fn is_final_block_terminator(cmd: &str) -> bool {
    matches!(
        canon_block_kw(cmd),
        "endif"
            | "endwhile"
            | "endfor"
            | "endfunction"
            | "enddef"
            | "endtry"
            | "endenum"
            | "endclass"
            | "endinterface"
    )
}

/// Parse a whole source block into a flat statement list with block structure
/// (the `:if`/`:while`/`:for`/`:function`/`:try` bodies nested inside).
pub fn parse_program(src: &str) -> Result<Vec<Stmt>, VimlError> {
    Ok(parse_program_lines(src)?
        .into_iter()
        .map(|(_, s)| s)
        .collect())
}

/// Like [`parse_program`] but pairs each TOP-LEVEL statement with its 1-based
/// source line (for the debugger's statement markers).
pub fn parse_program_lines(src: &str) -> Result<Vec<(u32, Stmt)>, VimlError> {
    let _vim9 = Vim9Guard::enter(script_is_vim9(src));
    let mut cur = Lines::new(src);
    let mut out = Vec::new();
    loop {
        cur.skip_blanks();
        let Some(line) = cur.peek() else { break };
        let (cmd, _) = cmd_word(&line);
        if is_block_terminator(cmd) {
            return Err(VimlError::msg(format!(
                "E580: `:{cmd}` without matching block opener"
            )));
        }
        let lineno = cur.line_no();
        for s in parse_one(&mut cur)? {
            out.push((lineno, s));
        }
    }
    Ok(out)
}

/// The result of [`parse_program_lines_tolerant`]: the top-level statements that
/// parsed (each with its 1-based source line), and `(line, message)` for each
/// statement that was skipped because it failed to parse.
pub type TolerantParse = (Vec<(u32, Stmt)>, Vec<(u32, String)>);

/// Like [`parse_program_lines`] but error-tolerant: a top-level statement (or
/// block) that fails to parse is skipped — its first logical line is dropped and
/// parsing resumes at the next — so one unsupported construct does not abort the
/// whole file. Returns the statements that parsed, paired with `(line, message)`
/// for each skipped one. Mirrors Vim reporting an error while sourcing a script
/// and continuing; used for best-effort config sourcing (e.g. a real `.vimrc`).
pub fn parse_program_lines_tolerant(src: &str) -> TolerantParse {
    let _vim9 = Vim9Guard::enter(script_is_vim9(src));
    let mut cur = Lines::new(src);
    let mut out = Vec::new();
    let mut errs = Vec::new();
    loop {
        cur.skip_blanks();
        let Some(line) = cur.peek() else { break };
        let lineno = cur.line_no();
        let snapshot = cur.i;
        let (cmd, _) = cmd_word(&line);
        if is_block_terminator(cmd) {
            // An orphaned terminator (e.g. left after skipping a broken opener):
            // report and step past it.
            errs.push((
                lineno,
                format!("E580: `:{cmd}` without matching block opener"),
            ));
            cur.i = snapshot + 1;
            continue;
        }
        match parse_one(&mut cur) {
            Ok(stmts) => {
                for s in stmts {
                    out.push((lineno, s));
                }
            }
            Err(e) => {
                errs.push((lineno, e.0));
                if is_block_opener(block_cmd_word(&line)) {
                    // A block whose body failed to parse is consumed WHOLE, up to
                    // its matching terminator — exactly as Vim reads a block to
                    // its end regardless of body contents. This keeps the inner
                    // statements from leaking out to run at the top level (a
                    // function's `while` loop must never execute at script scope).
                    cur.skip_block_from(snapshot);
                } else {
                    // Resume at the line after the one that began the failed parse.
                    cur.i = snapshot + 1;
                }
            }
        }
    }
    (out, errs)
}

/// Cursor over the LOGICAL lines of a source block. Physical lines whose first
/// non-blank char is `\` are joined onto the previous logical line (Vim's
/// line-continuation), so each entry carries its joined text plus the 1-based
/// source line where it began. `i` is the 0-based index of the next line.
struct Lines {
    lines: Vec<(u32, String)>,
    i: usize,
}

impl Lines {
    /// Build the logical-line list: first fold `\` continuation lines into the
    /// previous one (text after the `\` appended verbatim, as Vim does), then
    /// expand a one-line block (`if c | … | endif`) — a block-opener line with
    /// top-level `|` bars — into separate logical lines so the block parser
    /// handles it normally. Both keep the original 1-based source line number.
    fn new(src: &str) -> Self {
        // Pass 0: collapse heredoc assignments (`let x =<< [trim] [eval] END`)
        // into a single synthesized `let x = [...]` list-literal line. Body lines
        // are taken VERBATIM from the raw source — before continuation-folding or
        // bar-splitting — exactly as Vim's `ea_getline` feeds `heredoc_get()`
        // (vendor/eval/vars.c). Each body line becomes a single-quoted list item.
        let raw: Vec<&str> = src.lines().collect();
        let mut collapsed: Vec<(u32, String)> = Vec::new();
        let mut k = 0;
        while k < raw.len() {
            let lineno = (k + 1) as u32;
            // Script-language heredoc (`:python3 << EOF … EOF`, and the same for
            // `:python`/`:perl`/`:ruby`/`:lua`/`:tcl`/`:mzscheme`). Vim's
            // `script_get()` (ex_docmd.c) sees the command arg begin with `<<`
            // and calls `heredoc_get(…, script_get=true)`, which reads every
            // subsequent line into the embedded interpreter until the end marker
            // — a missing marker defaults to `.` (vendor/eval/vars.c:791). The
            // interface is uncompiled here, so the body is never run; skip it to
            // the marker so its lines are NOT mis-parsed as vimscript. The opener
            // line itself stays as a no-op'd Ex command.
            if let Some((trim, marker)) = script_lang_heredoc_marker(raw[k]) {
                let cmd_indent: String = raw[k].chars().take_while(|c| c.is_whitespace()).collect();
                let mut j = k + 1;
                while j < raw.len() {
                    let bl = raw[j];
                    let probe = if trim {
                        bl.strip_prefix(cmd_indent.as_str()).unwrap_or(bl)
                    } else {
                        bl
                    };
                    j += 1;
                    if probe == marker {
                        break;
                    }
                }
                collapsed.push((lineno, raw[k].to_string()));
                k = j;
                continue;
            }
            if let Some((prefix, trim, _eval, marker)) = heredoc_opener(raw[k]) {
                // With `trim`, the end marker may be indented to match the `:let`
                // command line; record that indent so it can be skipped.
                let cmd_indent: String = raw[k].chars().take_while(|c| c.is_whitespace()).collect();
                let mut body: Vec<String> = Vec::new();
                let mut j = k + 1;
                while j < raw.len() {
                    let bl = raw[j];
                    let probe = if trim {
                        bl.strip_prefix(cmd_indent.as_str()).unwrap_or(bl)
                    } else {
                        bl
                    };
                    j += 1;
                    if probe == marker {
                        break;
                    }
                    body.push(bl.to_string());
                }
                // With `trim`, strip from every line the indent of the first
                // (non-blank) body line, matching char-for-char.
                if trim {
                    if let Some(first) = body.iter().find(|l| !l.trim().is_empty()) {
                        let ti: String = first.chars().take_while(|c| c.is_whitespace()).collect();
                        for l in body.iter_mut() {
                            let n: usize = l
                                .chars()
                                .zip(ti.chars())
                                .take_while(|(a, b)| a == b)
                                .map(|(a, _)| a.len_utf8())
                                .sum();
                            *l = l[n..].to_string();
                        }
                    }
                }
                let items: Vec<String> = body
                    .iter()
                    .map(|l| format!("'{}'", l.replace('\'', "''")))
                    .collect();
                collapsed.push((lineno, format!("{prefix}= [{}]", items.join(", "))));
                k = j;
                continue;
            }
            collapsed.push((lineno, raw[k].to_string()));
            k += 1;
        }
        // Pass 1: continuation join. A leading `\` joins verbatim (legacy
        // `line-continuation`) everywhere. In a vim9 region — a script whose first
        // command is `:vim9script`, or the body of any `def … enddef` (vim9
        // functions are vim9 even inside a legacy script) — vim9 AUTOMATIC line
        // continuation also applies (`:help vim9-line-continuation`): an
        // expression joins the next physical line when it has unclosed
        // `[]`/`{}`/`()`, ends with a trailing binary operator, or the next line
        // begins with a binary operator / method `->` / member `.` / closing
        // bracket / command-`|`. vim9 `#` comments are dropped in the region.
        let script_vim9 = collapsed
            .iter()
            .map(|(_, l)| l.trim())
            .find(|t| !t.is_empty() && !t.starts_with('"'))
            .is_some_and(|t| t.split(char::is_whitespace).next() == Some("vim9script"));
        let mut joined: Vec<(u32, String)> = Vec::new();
        let mut in_def: u32 = 0; // depth of open `def … enddef`
        let mut open_depth: i32 = 0; // unclosed brackets of the current logical line
        for (lineno, raw) in collapsed {
            let trimmed = raw.trim_start();
            // Legacy `\` continuation: always joins the text after the `\`.
            if let Some(rest) = trimmed.strip_prefix('\\') {
                if let Some(last) = joined.last_mut() {
                    last.1.push_str(rest);
                    if script_vim9 || in_def > 0 {
                        open_depth = vim9_bracket_depth(&last.1);
                    }
                    continue;
                }
            }
            let fw = cmd_word(&raw).0;
            let is_enddef = fw == "enddef";
            let active_vim9 = script_vim9 || in_def > 0;
            // vim9 `#` comment: a line that is only a comment is dropped — skipped
            // silently while accumulating a bracketed expression, else emitted as
            // an empty line for `skip_blanks` to pass over.
            if active_vim9 && !trimmed.is_empty() && strip_vim9_comment(&raw).trim().is_empty() {
                if open_depth <= 0 {
                    joined.push((lineno, String::new()));
                }
                continue;
            }
            if active_vim9 && !is_enddef {
                if let Some(last) = joined.last_mut() {
                    let join = open_depth > 0
                        || vim9_trailing_continues(&last.1)
                        || vim9_leading_continues(trimmed)
                        || (trimmed.starts_with(':') && vim9_open_ternary(&last.1));
                    if join {
                        last.1.push(' ');
                        last.1.push_str(strip_vim9_comment(&raw).trim_start());
                        open_depth = vim9_bracket_depth(&last.1);
                        continue;
                    }
                }
            }
            // Start a new logical line. A vim9-region line (or a `def` opener,
            // whose body region begins here) has its trailing `#` comment stripped.
            let text = if active_vim9 || fw == "def" {
                strip_vim9_comment(&raw).to_string()
            } else {
                raw.to_string()
            };
            joined.push((lineno, text));
            if fw == "def" {
                in_def += 1;
            } else if is_enddef && in_def > 0 {
                in_def -= 1;
            }
            open_depth = if script_vim9 || in_def > 0 {
                vim9_bracket_depth(&joined.last().expect("just pushed").1)
            } else {
                0
            };
        }
        // Pass 2: split `|`-separated commands into one logical line each, so a
        // block opener anywhere on the line (`let x=1 | if x | … | endif`) is
        // parsed as its own line. Blank/comment lines are kept whole.
        let mut lines: Vec<(u32, String)> = Vec::new();
        for (lineno, text) in joined {
            let trimmed = text.trim();
            if trimmed.is_empty() || trimmed.starts_with('"') {
                lines.push((lineno, text));
                continue;
            }
            // Commands whose argument absorbs a trailing `|` (`:autocmd`,
            // `:command`, `:normal`, `:global`/`:vglobal`) must not be bar-split:
            // the `|` is part of their text. `autocmd … exe '…' | e` is one
            // autocmd, not an autocmd followed by a stray `e` statement.
            let (lead, _) = cmd_word(strip_command_modifiers(trimmed));
            if cmd_takes_bar_arg(lead) {
                lines.push((lineno, text));
                continue;
            }
            let segs = split_commands(&text);
            if segs.len() > 1 {
                for seg in segs {
                    if !seg.trim().is_empty() {
                        lines.push((lineno, seg.to_string()));
                    }
                }
            } else {
                lines.push((lineno, text));
            }
        }
        Lines { lines, i: 0 }
    }

    /// Advance past blank lines and full-line `"` comments.
    fn skip_blanks(&mut self) {
        while let Some((_, l)) = self.lines.get(self.i) {
            let t = l.trim();
            if t.is_empty() || t.starts_with('"') {
                self.i += 1;
            } else {
                break;
            }
        }
    }

    fn peek(&self) -> Option<String> {
        self.lines.get(self.i).map(|(_, s)| s.clone())
    }

    fn bump(&mut self) {
        self.i += 1;
    }

    fn line_no(&self) -> u32 {
        self.lines.get(self.i).map(|(n, _)| *n).unwrap_or(0)
    }

    /// Advance the cursor past the whole block that began at logical-line index
    /// `start` (a block-opener line), leaving `i` just after the matching final
    /// terminator. The tolerant parser calls this when a block's body fails to
    /// parse, so the block is consumed as ONE unit — mirroring Vim, which reads
    /// a `:function … :endfunction` (and every `:while`/`:if`/`:for`/`:try`) to
    /// its terminator regardless of body contents. Without it, a broken body
    /// leaks its inner statements out to run at the top level (e.g. a function's
    /// `while` loop executing unbounded at script scope). Nested blocks are
    /// balanced by depth; an unterminated block consumes to end-of-input.
    fn skip_block_from(&mut self, start: usize) {
        let mut depth = 0i32;
        let mut j = start;
        while j < self.lines.len() {
            let cmd = block_cmd_word(&self.lines[j].1);
            if is_block_opener(cmd) {
                depth += 1;
            } else if is_final_block_terminator(cmd) {
                depth -= 1;
                if depth == 0 {
                    self.i = j + 1;
                    return;
                }
            }
            j += 1;
        }
        self.i = self.lines.len();
    }
}

/// Parse the statement(s) on the line at the cursor. A block opener yields one
/// `Stmt`; a leaf line yields one statement per `|`-separated command (Vim's
/// `do_one_cmd` bar split), so `let l = [1] | echo l` is two statements.
fn parse_one(cur: &mut Lines) -> Result<Vec<Stmt>, VimlError> {
    let line = cur.peek().expect("parse_one called at EOF");
    let (cmd, rest) = cmd_word(&line);
    // vim9 `export` marks the following definition (`def`/`var`/`const`/`final`/
    // `class`/`interface`/`enum`) visible to importers. Editor-less the marker has
    // no runtime effect, so strip it and re-dispatch on the real definition. Without
    // this, `export def Foo()` would fall through to the `_` command arm and its body
    // would run as top-level statements (E117/E121 on every body line).
    if cmd == "export" {
        let stripped = rest.trim_start().to_string();
        cur.lines[cur.i].1 = stripped;
        return parse_one(cur);
    }
    // Route block openers through `canon_block_kw` so every Vim abbreviation
    // (`fu`/`fun`/`func` for `:function`, `wh` for `:while`, …) opens the block.
    match canon_block_kw(cmd) {
        "if" => {
            cur.bump();
            Ok(vec![parse_if(cur, rest)?])
        }
        "while" => {
            cur.bump();
            Ok(vec![parse_while(cur, rest)?])
        }
        "for" => {
            cur.bump();
            Ok(vec![parse_for(cur, rest)?])
        }
        "try" => {
            cur.bump();
            Ok(vec![parse_try(cur)?])
        }
        "function" => {
            cur.bump();
            // Only `[!] {name}({params})` is a definition. `:function` (list all),
            // `:function {name}` (show one), and `:function /{pat}` (list matching)
            // have no parameter-list `(` and must NOT open a block — a bare
            // `function` inside a body (e.g. `silent function`) would otherwise be
            // taken as a nested `:function` with a missing `(` (E124) and abort the
            // enclosing definition. Editor-less the listing has no output → no-op.
            let hdr = rest
                .trim_start()
                .strip_prefix('!')
                .unwrap_or(rest)
                .trim_start();
            if !hdr.starts_with('/') && hdr.contains('(') {
                Ok(vec![parse_function(cur, rest)?])
            } else {
                Ok(vec![Stmt::Expr(Expr::Number(0))])
            }
        }
        "def" => {
            cur.bump();
            // Only `[!] {name}({params})` is a vim9 definition; a bare `def`
            // (list functions) or `def {name}` (show one) has no `(` and is a
            // listing/query — a no-op editor-less, matching `:function`.
            let hdr = rest
                .trim_start()
                .strip_prefix('!')
                .unwrap_or(rest)
                .trim_start();
            if hdr.contains('(') {
                Ok(vec![parse_def(cur, rest)?])
            } else {
                Ok(vec![Stmt::Expr(Expr::Number(0))])
            }
        }
        _ => {
            cur.bump();
            // Commands that absorb a trailing `|` (`:autocmd`, `:command`,
            // `:normal`, `:global`) are parsed whole — splitting them would break
            // off part of their argument (e.g. `autocmd … exe '…' | e`).
            if cmd_takes_bar_arg(cmd_word(strip_command_modifiers(line.trim())).0) {
                return Ok(vec![parse_stmt(&line)?]);
            }
            let mut out = Vec::new();
            for seg in split_commands(&line) {
                if seg.trim().is_empty() {
                    continue;
                }
                out.push(parse_stmt(seg)?);
            }
            Ok(out)
        }
    }
}

/// Split a command line into its `|`-separated commands, the way Vim's
/// `do_one_cmd` does. A `|` ends a command except when it is inside a string,
/// part of the `||` operator, backslash-escaped, or after a `"` line comment.
/// Ex commands whose argument text includes any trailing `|` — they lack Vim's
/// `EX_TRLBAR` flag, so a `|` after them is part of the command, not a command
/// separator. Abbreviation sets match Vim 9.2 `fullcommand()`. Used by the
/// logical-line splitter (Pass 2) to keep such a line whole.
fn cmd_takes_bar_arg(cmd: &str) -> bool {
    matches!(
        cmd,
        "au" | "aut"
            | "auto"
            | "autoc"
            | "autocm"
            | "autocmd"
            | "com"
            | "comm"
            | "command"
            | "norm"
            | "norma"
            | "normal"
            | "g"
            | "gl"
            | "glo"
            | "glob"
            | "globa"
            | "global"
            | "v"
            | "vg"
            | "vgl"
            | "vglo"
            | "vglob"
            | "vgloba"
            | "vglobal"
    )
}

fn split_commands(line: &str) -> Vec<&str> {
    let bytes = line.as_bytes();
    let mut segs = Vec::new();
    let mut start = 0usize;
    let mut i = 0usize;
    let mut sq = false; // inside a single-quoted string ('' is an escaped quote)
    let mut dq = false; // inside a double-quoted string (\ escapes)
                        // A `:sy[ntax]` command carries `/…/`-delimited regex patterns whose bars are
                        // literal alternation, not command separators: `syn match Foo /\v(N|G|E)/`.
                        // Vim's syntax parser skips each pattern via `skip_regexp` before it looks for
                        // a trailing `|`, so a `|` inside `/…/` never ends the command. Track a slash
                        // pattern the same way sq/dq strings are tracked (`\` escapes, `/` closes) so
                        // inner bars stay literal. (`'…'`/`"…"` delimiters are already covered by the
                        // sq/dq handling.) Scoped to `:syntax` so real division — `let x = 4/2 | …` —
                        // still splits on the bar.
    let is_syntax_cmd = matches!(
        cmd_word(strip_command_modifiers(line.trim())).0,
        "sy" | "syn" | "synt" | "synta" | "syntax"
    );
    let mut slash = false; // inside a `/…/` :syntax pattern (\ escapes, / closes)
    while i < bytes.len() {
        let c = bytes[i];
        if slash {
            if c == b'\\' {
                i += 2;
                continue;
            }
            if c == b'/' {
                slash = false;
            }
            i += 1;
            continue;
        }
        if sq {
            if c == b'\'' {
                if bytes.get(i + 1) == Some(&b'\'') {
                    i += 2;
                    continue;
                }
                sq = false;
            }
            i += 1;
            continue;
        }
        if dq {
            if c == b'\\' {
                i += 2;
                continue;
            }
            if c == b'"' {
                dq = false;
            }
            i += 1;
            continue;
        }
        match c {
            b'/' if is_syntax_cmd => slash = true,
            b'\'' => sq = true,
            b'"' => {
                // A `"` with only whitespace before it in this command is a
                // line comment → ignore the rest of the line.
                if line[start..i].trim().is_empty() {
                    let seg = &line[start..i];
                    if !seg.trim().is_empty() {
                        segs.push(seg);
                    }
                    return segs;
                }
                // Otherwise a `"` opens a string literal.
                dq = true;
            }
            b'|' => {
                if bytes.get(i + 1) == Some(&b'|') {
                    i += 2; // `||` logical-or, not a separator
                    continue;
                }
                if i > 0 && bytes[i - 1] == b'\\' {
                    i += 1; // escaped bar
                    continue;
                }
                segs.push(&line[start..i]);
                start = i + 1;
            }
            _ => {}
        }
        i += 1;
    }
    let tail = &line[start..];
    if !tail.trim().is_empty() {
        segs.push(tail);
    }
    segs
}

/// A parsed block body plus the terminator `(cmd, rest)` it stopped on
/// (`None` at EOF).
type ParseBlockResult = Result<(Vec<Stmt>, Option<(String, String)>), VimlError>;

/// Parse statements until a terminator in `terms`. Returns the body and the
/// terminator `(cmd, rest)` it stopped on (`None` at EOF).
fn parse_block(cur: &mut Lines, terms: &[&str]) -> ParseBlockResult {
    let mut stmts = Vec::new();
    loop {
        cur.skip_blanks();
        let Some(line) = cur.peek() else {
            return Ok((stmts, None));
        };
        let (cmd, rest) = cmd_word(&line);
        // Compare (and hand back) the canonical keyword so an abbreviated
        // terminator (`endw`, `endf`, `en`, `cat`, …) closes its block.
        let ck = canon_block_kw(cmd);
        if terms.contains(&ck) {
            cur.bump();
            return Ok((stmts, Some((ck.to_string(), rest.to_string()))));
        }
        if is_block_terminator(cmd) {
            return Err(VimlError::msg(format!("E580: unexpected `:{cmd}`")));
        }
        stmts.extend(parse_one(cur)?);
    }
}

const IF_TERMS: &[&str] = &["elseif", "else", "endif"];

/// Strip a legacy trailing `"` line comment from a single-expression command
/// argument (`:if`/`:elseif`/`:while`/`:for`/`:return`/`:eval`/`:call`/`:throw`
/// and a `:let` RHS). In legacy Vim script a double-quoted string never spans a
/// line, so a top-level `"` whose body does not close before end-of-line cannot
/// be a string operand — it opens a comment (`:help :comment`), which these
/// commands accept after their expression. Binary-verified against Vim 9.2:
/// `if 1 " c`, `elseif c ==? 'f' " c`, `let x = 1 " c` all succeed, while
/// `echo 1 " c` errors — so this is applied only to the single-expression
/// commands, never to `:echo` (which parses a whole expression list). A `"`
/// inside a complete `'…'`/`"…"` string, or one that *does* close, is a real
/// operand and left intact — so for a well-formed argument this only trims
/// trailing whitespace, never altering an expression that already parses. This
/// makes a trailing-comment line parse in the strict fast path (so the enclosing
/// `:function` body is captured and the function is defined) instead of failing
/// with E114 and dropping to the tolerant fallback.
fn strip_legacy_trailing_comment(s: &str) -> &str {
    let b = s.as_bytes();
    let mut i = 0;
    while i < b.len() {
        match b[i] {
            b'\'' => {
                // Single-quoted string: `''` is an escaped quote.
                i += 1;
                while i < b.len() {
                    if b[i] == b'\'' {
                        if b.get(i + 1) == Some(&b'\'') {
                            i += 2;
                            continue;
                        }
                        i += 1;
                        break;
                    }
                    i += 1;
                }
            }
            b'"' => {
                // Does this `"` open a string that closes before EOL (`\` escapes)?
                let mut j = i + 1;
                let mut closed = false;
                while j < b.len() {
                    match b[j] {
                        b'\\' => j += 2,
                        b'"' => {
                            closed = true;
                            j += 1;
                            break;
                        }
                        _ => j += 1,
                    }
                }
                if closed {
                    i = j; // a real string operand — skip past it
                } else {
                    return s[..i].trim_end(); // unterminated → start of comment
                }
            }
            _ => i += 1,
        }
    }
    s.trim_end()
}

fn parse_if(cur: &mut Lines, cond_str: &str) -> Result<Stmt, VimlError> {
    let mut arms = Vec::new();
    let mut else_body = None;
    let (body, mut term) = parse_block(cur, IF_TERMS)?;
    arms.push((parse_expr(strip_legacy_trailing_comment(cond_str))?, body));
    loop {
        match term {
            Some((ref c, ref rest)) if c == "elseif" => {
                let cond = parse_expr(strip_legacy_trailing_comment(rest))?;
                let (b, t) = parse_block(cur, IF_TERMS)?;
                arms.push((cond, b));
                term = t;
            }
            Some((ref c, _)) if c == "else" => {
                let (b, t) = parse_block(cur, &["endif"])?;
                else_body = Some(b);
                if t.is_none() {
                    return Err(VimlError::msg("E171: Missing :endif"));
                }
                break;
            }
            Some((ref c, _)) if c == "endif" => break,
            None => return Err(VimlError::msg("E171: Missing :endif")),
            Some((c, _)) => return Err(VimlError::msg(format!("E580: unexpected `:{c}` in :if"))),
        }
    }
    Ok(Stmt::If { arms, else_body })
}

fn parse_while(cur: &mut Lines, cond_str: &str) -> Result<Stmt, VimlError> {
    let cond = parse_expr(strip_legacy_trailing_comment(cond_str))?;
    let (body, term) = parse_block(cur, &["endwhile"])?;
    if term.is_none() {
        return Err(VimlError::msg("E170: Missing :endwhile"));
    }
    Ok(Stmt::While { cond, body })
}

fn parse_for(cur: &mut Lines, header: &str) -> Result<Stmt, VimlError> {
    // `{var} in {expr}` — split on the first whitespace-delimited `in`.
    let idx = header
        .find(" in ")
        .ok_or_else(|| VimlError::msg("E690: Missing \"in\" after :for"))?;
    let var = header[..idx].trim();
    let vars = if let Some(inner) = var.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
        ForVars::List(
            inner
                .split(',')
                .map(|n| n.trim().to_string())
                .filter(|n| !n.is_empty())
                .collect(),
        )
    } else {
        ForVars::One(var.to_string())
    };
    let iter = parse_expr(strip_legacy_trailing_comment(header[idx + 4..].trim()))?;
    let (body, term) = parse_block(cur, &["endfor"])?;
    if term.is_none() {
        return Err(VimlError::msg("E170: Missing :endfor"));
    }
    Ok(Stmt::For { vars, iter, body })
}

/// Return the code portion of a vim9 line, dropping a trailing `#` comment. In
/// vim9script `#` (at the start of a token — line start or preceded by
/// whitespace) is the comment leader and `"` delimits a string (unlike legacy,
/// where `"` starts the comment). `#` mid-token (`autoload#name`) is not a
/// comment. Skips `'…'` (`''` escapes) and `"…"` (`\` escapes) string bodies.
fn strip_vim9_comment(s: &str) -> &str {
    let b = s.as_bytes();
    let mut i = 0;
    let mut sq = false;
    let mut dq = false;
    let mut prev_ws = true; // start of line counts as preceded-by-whitespace
    while i < b.len() {
        let c = b[i];
        if sq {
            if c == b'\'' {
                if b.get(i + 1) == Some(&b'\'') {
                    i += 2;
                    continue;
                }
                sq = false;
            }
            prev_ws = false;
            i += 1;
            continue;
        }
        if dq {
            if c == b'\\' {
                i += 2;
                prev_ws = false;
                continue;
            }
            if c == b'"' {
                dq = false;
            }
            prev_ws = false;
            i += 1;
            continue;
        }
        match c {
            b'\'' => {
                sq = true;
                prev_ws = false;
            }
            b'"' => {
                dq = true;
                prev_ws = false;
            }
            b'#' if prev_ws => return &s[..i],
            _ => prev_ws = c == b' ' || c == b'\t',
        }
        i += 1;
    }
    s
}

/// Net unclosed-bracket depth of a vim9 code fragment (`([{` add, `)]}`
/// subtract), skipping string and `#`-comment bytes. A positive result means the
/// expression is unterminated and continues on the next physical line — vim9's
/// automatic line continuation inside `[]`/`{}`/`()` (`:help
/// vim9-line-continuation`), the fix for the earlier `[`-continuation recursion.
fn vim9_bracket_depth(s: &str) -> i32 {
    let code = strip_vim9_comment(s);
    let b = code.as_bytes();
    let mut i = 0;
    let mut depth = 0i32;
    let mut sq = false;
    let mut dq = false;
    while i < b.len() {
        let c = b[i];
        if sq {
            if c == b'\'' {
                if b.get(i + 1) == Some(&b'\'') {
                    i += 2;
                    continue;
                }
                sq = false;
            }
            i += 1;
            continue;
        }
        if dq {
            if c == b'\\' {
                i += 2;
                continue;
            }
            if c == b'"' {
                dq = false;
            }
            i += 1;
            continue;
        }
        match c {
            b'\'' => sq = true,
            b'"' => dq = true,
            b'(' | b'[' | b'{' => depth += 1,
            b')' | b']' | b'}' => depth -= 1,
            _ => {}
        }
        i += 1;
    }
    depth
}

/// Whether a vim9 logical line ends mid-expression with a trailing binary
/// operator (or an assignment `=` whose RHS is on the next line), so the next
/// physical line continues it. Trailing single `.` is excluded (`.member`
/// continuation is leading, and `edit .` legitimately ends in `.`).
fn vim9_trailing_continues(s: &str) -> bool {
    let code = strip_vim9_comment(s).trim_end();
    for op in ["..", "&&", "||", "==", "!=", ">=", "<=", "=~", "!~", "->"] {
        if code.ends_with(op) {
            return true;
        }
    }
    // `<`/`>` are deliberately excluded: they clash with vim9 generic type
    // syntax (`list<number>`, `dict<string>`) that ends a `def`/`var` line.
    matches!(
        code.as_bytes().last(),
        Some(b'+' | b'-' | b'*' | b'%' | b'?' | b'&' | b'|' | b'=')
    )
}

/// Whether a vim9 physical line begins with a continuation leader — a binary
/// operator, method `->`, member `.`, closing bracket, or command-list `|` — so
/// it continues the previous line (`:help vim9-line-continuation`). A leading `:`
/// is NOT handled here (it introduces a range); the ternary `:` case is gated on
/// [`vim9_open_ternary`] by the caller.
fn vim9_leading_continues(trimmed: &str) -> bool {
    for op in ["->", "..", "&&", "||", "==", "!=", ">=", "<=", "=~", "!~"] {
        if trimmed.starts_with(op) {
            return true;
        }
    }
    // `<`/`>` are excluded (they clash with vim9 `<type>` syntax); `>=`/`<=` are
    // still matched by the multi-char loop above.
    matches!(
        trimmed.as_bytes().first(),
        Some(b'+' | b'-' | b'*' | b'/' | b'%' | b'.' | b'?' | b')' | b']' | b'}' | b'|' | b'&')
    )
}

/// Whether `s` has an unmatched top-level `?` (an open ternary), so a following
/// line that begins with `:` is the ternary's else-branch continuation rather
/// than a range. Scope colons (`g:`/`a:`/…) can undercount this; that rare miss
/// is accepted for the bounded slice.
fn vim9_open_ternary(s: &str) -> bool {
    let code = strip_vim9_comment(s);
    let b = code.as_bytes();
    let mut i = 0;
    let mut sq = false;
    let mut dq = false;
    let mut depth = 0i32;
    let mut q = 0i32;
    while i < b.len() {
        let c = b[i];
        if sq {
            if c == b'\'' {
                if b.get(i + 1) == Some(&b'\'') {
                    i += 2;
                    continue;
                }
                sq = false;
            }
            i += 1;
            continue;
        }
        if dq {
            if c == b'\\' {
                i += 2;
                continue;
            }
            if c == b'"' {
                dq = false;
            }
            i += 1;
            continue;
        }
        match c {
            b'\'' => sq = true,
            b'"' => dq = true,
            b'(' | b'[' | b'{' => depth += 1,
            b')' | b']' | b'}' => depth -= 1,
            b'?' if depth == 0 => q += 1,
            b':' if depth == 0 => q -= 1,
            _ => {}
        }
        i += 1;
    }
    q > 0
}

/// Byte offset of the top-level assignment `=` in `s` (not part of `==`/`!=`/
/// `<=`/`>=`/`=~`), skipping strings and bracketed groups. Used to split a vim9
/// `def` parameter or `var` declaration into its LHS and initializer.
fn find_top_eq(s: &str) -> Option<usize> {
    let b = s.as_bytes();
    let mut depth = 0i32;
    let mut quote: Option<u8> = None;
    for (i, &c) in b.iter().enumerate() {
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => match c {
                b'\'' | b'"' => quote = Some(c),
                b'[' | b'(' | b'{' => depth += 1,
                b']' | b')' | b'}' => depth -= 1,
                b'=' if depth == 0
                    && !matches!(b.get(i.wrapping_sub(1)), Some(b'!' | b'<' | b'>' | b'='))
                    && !matches!(b.get(i + 1), Some(b'=' | b'~')) =>
                {
                    return Some(i);
                }
                _ => {}
            },
        }
    }
    None
}

/// Byte offset of the first top-level `:` in `s` (a vim9 `name: type`
/// annotation separator), skipping strings and bracketed groups.
fn find_top_colon(s: &str) -> Option<usize> {
    let b = s.as_bytes();
    let mut depth = 0i32;
    let mut quote: Option<u8> = None;
    for (i, &c) in b.iter().enumerate() {
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => match c {
                b'\'' | b'"' => quote = Some(c),
                b'[' | b'(' | b'{' => depth += 1,
                b']' | b')' | b'}' => depth -= 1,
                b':' if depth == 0 => return Some(i),
                _ => {}
            },
        }
    }
    None
}

/// Whether `s` is a plain identifier (`[A-Za-z_][A-Za-z0-9_]*`) — a vim9 `def`
/// parameter that gets a bare-name `let` prologue (excludes `...` varargs).
fn is_plain_ident(s: &str) -> bool {
    !s.is_empty()
        && s.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'_')
        && !s.as_bytes()[0].is_ascii_digit()
}

/// Strip a vim9 `: type` annotation from a `var`/`final`/`const` declaration's
/// LHS, leaving `NAME = expr` for [`parse_let`]. `var x: number = 5` → `x = 5`.
/// The vim9 type system (checking/coercion, default init for a type-only
/// `var x: number`) is deferred; the annotation is parsed and discarded.
fn strip_vim9_type(rest: &str) -> String {
    let Some(eq) = find_top_eq(rest) else {
        return rest.to_string();
    };
    let (decl, tail) = (rest[..eq].trim_end(), &rest[eq..]);
    // A list-unpack LHS (`[a, b]`) has no scalar `: type`; leave it untouched.
    if decl.starts_with('[') {
        return rest.to_string();
    }
    match find_top_colon(decl) {
        Some(p) => format!("{} {}", decl[..p].trim_end(), tail),
        None => rest.to_string(),
    }
}

/// vim9 `:var` declaration LHS → a `:let`-compatible string for [`parse_let`].
/// With an initializer (`var x: T = e`) the `: T` annotation is stripped
/// ([`strip_vim9_type`]). A type-only declaration (`var x: T`, no `=`)
/// default-inits to `T`'s zero value (`:help vim9-declaration`), producing
/// `x = <default>`. Defaults are binary-verified against Vim 9.2:
/// `string ''`, `number 0`, `float 0.0`, `bool v:false`, `list []`, `dict {}`,
/// `blob 0z`, `any 0`. Opaque types real Vim rejects or has no literal for
/// (`func` → E1017, `job`/`channel`/class names) pass through unchanged so the
/// existing error path is preserved.
fn vim9_var_decl(rest: &str) -> String {
    if find_top_eq(rest).is_some() {
        return strip_vim9_type(rest);
    }
    let trimmed = rest.trim();
    // A list-unpack LHS (`[a, b]`) is never a type-only scalar declaration.
    if trimmed.starts_with('[') {
        return rest.to_string();
    }
    let Some(p) = find_top_colon(trimmed) else {
        return rest.to_string();
    };
    let name = trimmed[..p].trim_end();
    // Outermost type constructor: the leading identifier of the annotation,
    // up to `<` (list<…>/dict<…>), `(` (func(…)), whitespace, or a `#` comment.
    let outer: String = trimmed[p + 1..]
        .trim_start()
        .chars()
        .take_while(|c| c.is_ascii_alphanumeric() || *c == '_')
        .collect();
    let default = match outer.as_str() {
        "string" => "''",
        "number" => "0",
        "float" => "0.0",
        "bool" => "v:false",
        "list" => "[]",
        "dict" => "{}",
        "blob" => "0z",
        "any" => "0",
        _ => return rest.to_string(),
    };
    format!("{name} = {default}")
}

/// True when `line` (in a vim9 region) is a bare assignment to an already-declared
/// variable — `name = expr`, `name += expr`, `d[key] = expr`, `g:x = expr`,
/// `obj.field = expr`, etc. vim9 assigns without a `:let`/`:var` keyword
/// (`:help vim9-declaration`); legacy vimscript requires `:let`, so the caller
/// gates this on [`vim9_active`]. Detection scans a valid lvalue (identifier with
/// optional scope `:`, chained `[...]` subscripts and `.field` members) and
/// requires an assignment operator (`= += -= *= /= %= ..=`) as the next token —
/// a space before the operator (as in a user command `MyCmd key=val`) or a `==`/
/// `=~` comparison is rejected. Routed through [`parse_let`], which handles every
/// assignment operator and lvalue form.
fn is_vim9_assignment(line: &str) -> bool {
    let b = line.as_bytes();
    // An lvalue starts with an identifier char, or `[` for a `[a, b] = …` /
    // `[a, b; rest] = …` list-unpack assignment; anything else is not an
    // assignment. The scan loop below skips the balanced bracket group, and the
    // trailing operator check confirms a top-level `=` follows.
    match b.first() {
        Some(&c) if c.is_ascii_alphabetic() || c == b'_' || c == b'[' => {}
        _ => return false,
    }
    let mut i = 0;
    while i < b.len() {
        match b[i] {
            c if c.is_ascii_alphanumeric() || c == b'_' => i += 1,
            b':' => i += 1, // scope separator: g:, b:, s:, …
            b'[' => {
                // Skip a bracketed subscript, tracking nesting and strings.
                let mut depth = 0i32;
                let mut quote: Option<u8> = None;
                while i < b.len() {
                    let c = b[i];
                    i += 1;
                    match quote {
                        Some(q) => {
                            if c == q {
                                quote = None;
                            }
                        }
                        None => match c {
                            b'\'' | b'"' => quote = Some(c),
                            b'[' => depth += 1,
                            b']' => {
                                depth -= 1;
                                if depth == 0 {
                                    break;
                                }
                            }
                            _ => {}
                        },
                    }
                }
            }
            // `.field` member access (not the `..` concat / `..=` operator).
            b'.' if b
                .get(i + 1)
                .is_some_and(|&c| c.is_ascii_alphabetic() || c == b'_') =>
            {
                i += 1
            }
            _ => break,
        }
    }
    let op = line[i..].trim_start().as_bytes();
    match op.first() {
        Some(b'=') => !matches!(op.get(1), Some(b'=') | Some(b'~')),
        Some(b'+') | Some(b'-') | Some(b'*') | Some(b'%') | Some(b'/') => op.get(1) == Some(&b'='),
        Some(b'.') => op.get(1) == Some(&b'.') && op.get(2) == Some(&b'='),
        _ => false,
    }
}

fn parse_function(cur: &mut Lines, header: &str) -> Result<Stmt, VimlError> {
    // A legacy `:function … endfunction` body is legacy even inside a
    // `:vim9script`: its dict literals use expression keys, not vim9 bare keys.
    let _vim9 = Vim9Guard::enter(false);
    // `[!] {name}({a}, {b}, …) [flags]`.
    let header = header.trim();
    let (bang, header) = match header.strip_prefix('!') {
        Some(rest) => (true, rest.trim()),
        None => (false, header),
    };
    let lparen = header
        .find('(')
        .ok_or_else(|| VimlError::msg("E124: Missing '(' in :function"))?;
    let name = header[..lparen].trim().to_string();
    // Find the `)` that matches the parameter-list `(` — not merely the first
    // one, since a default value may itself contain parens (`a = abs(-7)`) or
    // brackets. Track nesting and skip quoted strings.
    let rparen = {
        let mut depth = 0i32;
        let mut quote: Option<u8> = None;
        let mut found = None;
        for (i, &b) in header.as_bytes().iter().enumerate().skip(lparen) {
            match quote {
                Some(q) => {
                    if b == q {
                        quote = None;
                    }
                }
                None => match b {
                    b'\'' | b'"' => quote = Some(b),
                    b'(' | b'[' | b'{' => depth += 1,
                    b')' | b']' | b'}' => {
                        depth -= 1;
                        if depth == 0 {
                            found = Some(i);
                            break;
                        }
                    }
                    _ => {}
                },
            }
        }
        found.ok_or_else(|| VimlError::msg("E125: Missing ')' in :function"))?
    };
    // Split the parameter list, separating optional `name = default` params
    // (`:help optional-function-argument`) into the name list plus a parallel
    // list of `(index, default expr)`.
    let mut args: Vec<String> = Vec::new();
    let mut defaults: Vec<(usize, Expr)> = Vec::new();
    for raw in split_top_commas(&header[lparen + 1..rparen]) {
        let raw = raw.trim();
        if raw.is_empty() {
            continue;
        }
        // `=` introduces a default, but `==`/`=~` etc. inside the default expr
        // must not be mistaken for it: only an unescaped top-level `=` that is
        // not part of a comparison operator separates name from default.
        match raw.find('=').filter(|&p| {
            raw[..p]
                .trim_end()
                .chars()
                .all(|c| c.is_alphanumeric() || c == '_' || c == ':')
                && !matches!(raw.as_bytes().get(p + 1), Some(b'=') | Some(b'~'))
        }) {
            Some(p) => {
                defaults.push((args.len(), parse_expr(raw[p + 1..].trim())?));
                args.push(raw[..p].trim().to_string());
            }
            None => args.push(raw.to_string()),
        }
    }
    let (body, term) = parse_block(cur, &["endfunction"])?;
    if term.is_none() {
        return Err(VimlError::msg("E126: Missing :endfunction"));
    }
    Ok(Stmt::Function {
        name,
        args,
        defaults,
        body,
        bang,
        // Legacy `:function`: bare names in the body do NOT see script-scope
        // vars (that requires an explicit `s:`/`g:` prefix).
        vim9: false,
    })
}

/// Parse a vim9 `def[!] {name}({params}): {rettype} … enddef` definition.
///
/// Unlike legacy `:function`, vim9 parameters are BARE names: inside the body `x`
/// refers to the parameter `x` directly (no `a:` prefix). This is realized by
/// synthesizing a `let {p} = a:{p}` prologue for each plain-identifier
/// parameter — the identical mechanism the `{args -> body}` lambda compiler uses
/// (compile_viml.rs) — so params still bind through the existing `a:` call
/// machinery yet resolve as bare locals when the body runs.
///
/// Parameter `: type` annotations and the `: rettype` return type are PARSED and
/// discarded (the vim9 type system is deferred). Optional `param = default`
/// values are honored via the shared [`Stmt::Function`] `defaults` path. The body
/// uses vim9 automatic line continuation, joined in [`Lines::new`]. `...` varargs
/// collection and full type checking are deferred.
fn parse_def(cur: &mut Lines, header: &str) -> Result<Stmt, VimlError> {
    // A `def … enddef` body is vim9 even inside a legacy script: its parameter
    // defaults and body statements parse with vim9 bare-key dict semantics.
    let _vim9 = Vim9Guard::enter(true);
    let header = header.trim();
    // A leading `!` (`def!`) is accepted and discarded — vim9 `def` always
    // (re)defines, so there is no bang distinction as for legacy `:function`.
    let header = header.strip_prefix('!').map_or(header, str::trim_start);
    let lparen = header
        .find('(')
        .ok_or_else(|| VimlError::msg("E1055: Missing '(' in :def"))?;
    let name = header[..lparen].trim().to_string();
    // Match the parameter-list `)` (tracking nesting, skipping quoted strings) —
    // a default value may contain its own parens/brackets.
    let rparen = {
        let mut depth = 0i32;
        let mut quote: Option<u8> = None;
        let mut found = None;
        for (i, &b) in header.as_bytes().iter().enumerate().skip(lparen) {
            match quote {
                Some(q) => {
                    if b == q {
                        quote = None;
                    }
                }
                None => match b {
                    b'\'' | b'"' => quote = Some(b),
                    b'(' | b'[' | b'{' => depth += 1,
                    b')' | b']' | b'}' => {
                        depth -= 1;
                        if depth == 0 {
                            found = Some(i);
                            break;
                        }
                    }
                    _ => {}
                },
            }
        }
        found.ok_or_else(|| VimlError::msg("E1055: Missing ')' in :def"))?
    };
    // Text after `)` is `: rettype` (or nothing) — parsed and ignored.
    let mut args: Vec<String> = Vec::new();
    let mut defaults: Vec<(usize, Expr)> = Vec::new();
    for raw in split_top_commas(&header[lparen + 1..rparen]) {
        let raw = raw.trim();
        if raw.is_empty() {
            continue;
        }
        if raw.starts_with("...") {
            // Varargs boundary: record the marker so fixed params before it bind
            // correctly; collecting the rest into a typed list is deferred.
            args.push("...".to_string());
            continue;
        }
        // `name[: type][ = default]` — split off the default, then the type.
        let (decl, default) = match find_top_eq(raw) {
            Some(p) => (raw[..p].trim(), Some(raw[p + 1..].trim())),
            None => (raw, None),
        };
        let pname = match find_top_colon(decl) {
            Some(p) => decl[..p].trim(),
            None => decl,
        };
        if let Some(d) = default {
            defaults.push((args.len(), parse_expr(d)?));
        }
        args.push(pname.to_string());
    }
    let (body_stmts, term) = parse_block(cur, &["enddef"])?;
    if term.is_none() {
        return Err(VimlError::msg("E1057: Missing :enddef"));
    }
    // Prepend `let {p} = a:{p}` so each bare parameter name resolves in the body.
    let mut body: Vec<Stmt> = Vec::with_capacity(body_stmts.len() + args.len());
    for p in &args {
        if is_plain_ident(p) {
            body.push(Stmt::Let {
                target: LetTarget::Var(p.clone()),
                expr: Expr::Var(format!("a:{p}")),
            });
        }
    }
    body.extend(body_stmts);
    Ok(Stmt::Function {
        name,
        args,
        defaults,
        body,
        // vim9 `def` always (re)defines; there is no "already defined" error as
        // for legacy `:function` without `!`.
        bang: true,
        // vim9 `def`: bare names in the body resolve to script-scope
        // vars/functions when they are not locals or parameters.
        vim9: true,
    })
}

fn parse_try(cur: &mut Lines) -> Result<Stmt, VimlError> {
    const TRY_TERMS: &[&str] = &["catch", "finally", "endtry"];
    let (body, mut term) = parse_block(cur, TRY_TERMS)?;
    let mut catches = Vec::new();
    let mut finally = None;
    loop {
        match term {
            Some((ref c, ref rest)) if c == "catch" => {
                let pat = {
                    let r = rest.trim();
                    if r.is_empty() {
                        None
                    } else {
                        Some(r.trim_matches('/').to_string())
                    }
                };
                let (b, t) = parse_block(cur, TRY_TERMS)?;
                catches.push((pat, b));
                term = t;
            }
            Some((ref c, _)) if c == "finally" => {
                let (b, t) = parse_block(cur, &["endtry"])?;
                finally = Some(b);
                if t.is_none() {
                    return Err(VimlError::msg("E170: Missing :endtry"));
                }
                break;
            }
            Some((ref c, _)) if c == "endtry" => break,
            None => return Err(VimlError::msg("E170: Missing :endtry")),
            Some((c, _)) => return Err(VimlError::msg(format!("E580: unexpected `:{c}` in :try"))),
        }
    }
    Ok(Stmt::Try {
        body,
        catches,
        finally,
    })
}

fn parse_let(rest: &str) -> Result<Stmt, VimlError> {
    let Some(eq) = rest.find('=') else {
        // No `=`: `:let` (list all variables) or `:let {var}` (show one) — a
        // listing/show command, not an assignment. Editor-less it has no
        // observable output, so it is a no-op; erroring here would abort an
        // enclosing `:function` whose body lists variables (`silent let`).
        return Ok(Stmt::Expr(Expr::Number(0)));
    };
    // Compound assignment (`+= -= *= /= %= .=`, ex_let's `tv_op`): the char just
    // before `=` is the operator. A `:let` target never ends in one of these, so
    // its presence unambiguously marks a compound assign.
    let op = match rest.as_bytes()[..eq].last() {
        Some(b'+') => Some(ArithOp::Add),
        Some(b'-') => Some(ArithOp::Sub),
        Some(b'*') => Some(ArithOp::Mul),
        Some(b'/') => Some(ArithOp::Div),
        Some(b'%') => Some(ArithOp::Mod),
        Some(b'.') => Some(ArithOp::Concat),
        _ => None,
    };
    // vim9 string concat-assign is `..=` (two dots); legacy is `.=` (one). The
    // op char sits at `eq - 1`; for `..=` a second dot precedes it, so strip both.
    let lhs_end = match op {
        Some(ArithOp::Concat) if eq >= 2 && rest.as_bytes()[eq - 2] == b'.' => eq - 2,
        Some(_) => eq - 1,
        None => eq,
    };
    let lhs = rest[..lhs_end].trim();
    let rhs = rest[eq + 1..].trim();
    let target = if let Some(inner) = lhs.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
        // `[a, b]` or `[a, b; rest]` list-unpack.
        let (head, rest_name) = match inner.split_once(';') {
            Some((h, r)) => (h, Some(r.trim().to_string())),
            None => (inner, None),
        };
        let names = head
            .split(',')
            .map(|n| n.trim().to_string())
            .filter(|n| !n.is_empty())
            .collect();
        LetTarget::List {
            names,
            rest: rest_name,
        }
    } else if let Some(name) = lhs.strip_prefix('&') {
        LetTarget::Option(name.to_string())
    } else if let Some(name) = lhs.strip_prefix('$') {
        LetTarget::Env(name.to_string())
    } else if let Some(reg) = lhs.strip_prefix('@') {
        LetTarget::Register(reg.chars().next().unwrap_or('"'))
    } else if lhs.ends_with(']') && lhs.contains('[') {
        // `base[index] = …` — split at the LAST top-level subscript (so nested
        // `d['a']['b']` parses `d['a']` as the base expression).
        let bytes = lhs.as_bytes();
        let mut depth = 0i32;
        let mut open = 0;
        for i in (0..lhs.len()).rev() {
            match bytes[i] {
                b']' => depth += 1,
                b'[' => {
                    depth -= 1;
                    if depth == 0 {
                        open = i;
                        break;
                    }
                }
                _ => {}
            }
        }
        let base_src = lhs[..open].trim();
        let index_src = &lhs[open + 1..lhs.len() - 1];
        // A top-level `:` inside the subscript marks a range assign `l[i:j]=…`
        // (split at the colon that is not nested in `[]`/`()` or a string).
        match split_top_colon(index_src) {
            Some((a, b)) => {
                let parse_opt = |s: &str| -> Result<Option<Box<Expr>>, VimlError> {
                    let s = s.trim();
                    Ok(if s.is_empty() {
                        None
                    } else {
                        Some(Box::new(parse_expr(s)?))
                    })
                };
                LetTarget::Range {
                    base: Box::new(parse_expr(base_src)?),
                    idx1: parse_opt(a)?,
                    idx2: parse_opt(b)?,
                }
            }
            None => LetTarget::Index {
                base: Box::new(parse_expr(base_src)?),
                index: Box::new(parse_expr(index_src)?),
            },
        }
    } else if !lhs.contains('[')
        && lhs.contains('.')
        && lhs.rsplit_once('.').is_some_and(|(_, k)| {
            !k.is_empty() && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')
        })
    {
        // `base.key = …` — split at the last `.` (nested `d.a.b` → base `d.a`).
        let (base, key) = lhs.rsplit_once('.').unwrap();
        LetTarget::Index {
            base: Box::new(parse_expr(base)?),
            index: Box::new(Expr::Str(key.to_string())),
        }
    } else {
        LetTarget::Var(lhs.to_string())
    };
    // Plain `=` stores the RHS directly; `op=` desugars to `target = target op rhs`
    // (`tv_op` semantics), reusing the same store path so it stays JIT-eligible.
    let rhs = strip_legacy_trailing_comment(rhs);
    let expr = match op {
        None => parse_expr(rhs)?,
        Some(op) => {
            let cur = let_target_expr(&target)?;
            Expr::Arith {
                op,
                lhs: Box::new(cur),
                rhs: Box::new(parse_expr(rhs)?),
            }
        }
    };
    Ok(Stmt::Let { target, expr })
}

/// Split a `:unlet` argument list on top-level whitespace, keeping `[…]`
/// subscripts and quoted strings (e.g. `d['a b']`) intact. A plain
/// `split_whitespace()` would wrongly break `unlet d['a b']` in two.
fn split_unlet_args(s: &str) -> Vec<&str> {
    let bytes = s.as_bytes();
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut quote: Option<u8> = None;
    let mut start: Option<usize> = None;
    for (i, &c) in bytes.iter().enumerate() {
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => match c {
                b'\'' | b'"' => quote = Some(c),
                b'[' | b'(' => depth += 1,
                b']' | b')' => depth -= 1,
                _ if c.is_ascii_whitespace() && depth == 0 => {
                    if let Some(st) = start.take() {
                        out.push(&s[st..i]);
                    }
                    continue;
                }
                _ => {}
            },
        }
        if start.is_none() {
            start = Some(i);
        }
    }
    if let Some(st) = start {
        out.push(&s[st..]);
    }
    out
}

/// Split a function parameter list on its top-level commas, keeping commas
/// inside a default value's `[]`/`()`/`{}` or quoted string intact (so
/// `func F(l = [1, 2], d = {'a': 1})` splits into two params, not five).
fn split_top_commas(s: &str) -> Vec<&str> {
    let bytes = s.as_bytes();
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut quote: Option<u8> = None;
    let mut start = 0usize;
    for (i, &c) in bytes.iter().enumerate() {
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => match c {
                b'\'' | b'"' => quote = Some(c),
                b'[' | b'(' | b'{' => depth += 1,
                b']' | b')' | b'}' => depth -= 1,
                b',' if depth == 0 => {
                    out.push(&s[start..i]);
                    start = i + 1;
                }
                _ => {}
            },
        }
    }
    out.push(&s[start..]);
    out
}

/// If `line` is a heredoc assignment opener (`let X =<< [trim] [eval] MARKER`),
/// return `(prefix, trim, eval, marker)` where `prefix` is the `:let` target
/// text before `=<<` (the caller appends `= [...]`). Mirrors the keyword scan
/// at the top of `heredoc_get()` (vendor/eval/vars.c). vim9 declaration keywords
/// (`var`/`final`/`const`) also open a heredoc (`:help vim9` uses the same
/// `=<<` list-assignment form as legacy `:let`).
fn heredoc_opener(line: &str) -> Option<(String, bool, bool, String)> {
    let (cmd, _) = cmd_word(line.trim_start());
    if !matches!(cmd, "let" | "const" | "cons" | "var" | "final") {
        return None;
    }
    let op = line.find("=<<")?;
    let prefix = line[..op].to_string();
    let mut rest = line[op + 3..].trim_start();
    let (mut trim, mut eval) = (false, false);
    loop {
        let kw = |r: &str, w: &str| -> bool {
            r.strip_prefix(w)
                .is_some_and(|t| t.is_empty() || t.starts_with(char::is_whitespace))
        };
        if kw(rest, "trim") {
            trim = true;
            rest = rest[4..].trim_start();
        } else if kw(rest, "eval") {
            eval = true;
            rest = rest[4..].trim_start();
        } else {
            break;
        }
    }
    let marker = rest.split_whitespace().next()?;
    if marker.is_empty() {
        return None;
    }
    Some((prefix, trim, eval, marker.to_string()))
}

/// If `line` invokes a plain script-language interface command whose argument
/// begins with `<<`, return `(trim, marker)` for the heredoc that follows.
/// Mirrors Vim's `script_get()` (ex_docmd.c): only the bare interpreter
/// commands (`:python`/`:py`, `:python3`/`:py3`, `:pythonx`/`:pyx`, `:perl`,
/// `:ruby`, `:lua`, `:tcl`, `:mzscheme`/`:mz`) read an embedded script — the
/// `do`/`file` variants take an expression / filename, not a heredoc. The arg
/// (leading whitespace skipped) must start with `<<`; after it, `heredoc_get`
/// (vendor/eval/vars.c) reads the optional `trim`/`eval` words then the marker
/// word, defaulting to `.` when the marker is missing (script_get case).
fn script_lang_heredoc_marker(line: &str) -> Option<(bool, String)> {
    // The interpreter name may carry a trailing digit (`python3`, `py3`), which
    // `cmd_word` (ASCII-letter split) would truncate — split on the alphanumeric
    // run instead, matching `is_script_lang_cmd`.
    let line = line.trim_start();
    let end = line
        .find(|c: char| !c.is_ascii_alphanumeric())
        .unwrap_or(line.len());
    let (cmd, rest) = (&line[..end], line[end..].trim_start());
    if !matches!(
        cmd,
        "python"
            | "py"
            | "python3"
            | "py3"
            | "pythonx"
            | "pyx"
            | "perl"
            | "ruby"
            | "lua"
            | "tcl"
            | "mzscheme"
            | "mz"
    ) {
        return None;
    }
    let mut r = rest.strip_prefix("<<")?.trim_start();
    let mut trim = false;
    loop {
        let kw = |s: &str, w: &str| -> bool {
            s.strip_prefix(w)
                .is_some_and(|t| t.is_empty() || t.starts_with(char::is_whitespace))
        };
        if kw(r, "trim") {
            trim = true;
            r = r[4..].trim_start();
        } else if kw(r, "eval") {
            r = r[4..].trim_start();
        } else {
            break;
        }
    }
    // The marker is the next word; a missing or comment-only remainder means the
    // `.` default (only reached for script_get, which this always is).
    let marker = match r.split_whitespace().next() {
        Some(m) if !m.starts_with('"') => m.to_string(),
        _ => ".".to_string(),
    };
    Some((trim, marker))
}

/// Parse one `:unlet` argument into a bare name or a List/Dict element target.
/// Reuses the same `l[i]` / `d.key` shapes as `:let`'s `LetTarget::Index`; the
/// removal itself happens at runtime (mirroring `do_unlet_var()`).
fn parse_unlet_arg(arg: &str) -> Result<UnletArg, VimlError> {
    let arg = arg.trim();
    // `base[index]` — find the matching `[` for the trailing `]`.
    if arg.ends_with(']') && arg.contains('[') {
        let bytes = arg.as_bytes();
        let mut depth = 0i32;
        let mut open = 0;
        for i in (0..arg.len()).rev() {
            match bytes[i] {
                b']' => depth += 1,
                b'[' => {
                    depth -= 1;
                    if depth == 0 {
                        open = i;
                        break;
                    }
                }
                _ => {}
            }
        }
        let base_src = arg[..open].trim();
        let index_src = &arg[open + 1..arg.len() - 1];
        // A range subscript (`unlet l[i:j]`) is not yet supported; fall through
        // to treat the whole thing as a name so the runtime reports E108/E116.
        if split_top_colon(index_src).is_none() {
            return Ok(UnletArg::Item {
                base: Box::new(parse_expr(base_src)?),
                index: Box::new(parse_expr(index_src)?),
            });
        }
    } else if !arg.contains('[')
        && arg.contains('.')
        && arg.rsplit_once('.').is_some_and(|(_, k)| {
            !k.is_empty() && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')
        })
    {
        // `base.key` — split at the last `.` (nested `d.a.b` → base `d.a`).
        let (base, key) = arg.rsplit_once('.').unwrap();
        return Ok(UnletArg::Item {
            base: Box::new(parse_expr(base)?),
            index: Box::new(Expr::Str(key.to_string())),
        });
    }
    Ok(UnletArg::Name(arg.to_string()))
}

/// Split a subscript on its top-level `:` (the list-range separator). Returns
/// `(before, after)`, or `None` when there is no unnested `:` (a plain index).
/// `:` inside `[]`/`()` or a quoted string is ignored.
fn split_top_colon(s: &str) -> Option<(&str, &str)> {
    let bytes = s.as_bytes();
    let mut depth = 0i32;
    let mut quote: Option<u8> = None;
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i];
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => match c {
                b'\'' | b'"' => quote = Some(c),
                b'[' | b'(' => depth += 1,
                b']' | b')' => depth -= 1,
                b':' if depth == 0 => {
                    // Skip a scope-prefix colon (`s:`/`g:`/`a:`/…): a single scope
                    // letter at a token boundary, followed by an identifier char —
                    // that's a scoped variable index, not a range separator.
                    let is_ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
                    let scope_prefix = i >= 1
                        && matches!(
                            bytes[i - 1],
                            b's' | b'g' | b'b' | b'w' | b't' | b'l' | b'a' | b'v'
                        )
                        && (i == 1 || !is_ident(bytes[i - 2]))
                        && i + 1 < bytes.len()
                        && is_ident(bytes[i + 1]);
                    if !scope_prefix {
                        return Some((&s[..i], &s[i + 1..]));
                    }
                }
                _ => {}
            },
        }
        i += 1;
    }
    None
}

/// The current value of a compound-assignment target, as an expression. List
/// unpack targets cannot take a compound operator (`E734`).
fn let_target_expr(target: &LetTarget) -> Result<Expr, VimlError> {
    Ok(match target {
        LetTarget::Var(n) => Expr::Var(n.clone()),
        LetTarget::Option(n) => Expr::Option(n.clone()),
        LetTarget::Env(n) => Expr::Env(n.clone()),
        LetTarget::Register(c) => Expr::Register(*c),
        LetTarget::Index { base, index } => Expr::Index {
            base: base.clone(),
            index: index.clone(),
        },
        LetTarget::List { .. } | LetTarget::Range { .. } => {
            return Err(VimlError::msg("E734: Wrong variable type for +="))
        }
    })
}

fn parse_expr_list(src: &str) -> Result<Vec<Expr>, VimlError> {
    if src.trim().is_empty() {
        return Ok(Vec::new());
    }
    let toks = lex(src)?;
    let mut p = Parser::new(toks, src);
    let mut out = Vec::new();
    loop {
        out.push(p.eval1()?);
        if matches!(p.peek(), Tok::Eof) {
            break;
        }
    }
    Ok(out)
}

/// Parse a single expression string into an [`Expr`].
pub fn parse_expr(src: &str) -> Result<Expr, VimlError> {
    let toks = lex(src)?;
    let mut p = Parser::new(toks, src);
    let e = p.eval1()?;
    if !matches!(p.peek(), Tok::Eof) {
        return Err(VimlError::msg(
            "E15: Invalid expression: trailing tokens".to_string(),
        ));
    }
    Ok(e)
}

struct Parser {
    toks: Vec<Token>,
    i: usize,
    /// The source string the tokens were lexed from. Token spans are byte
    /// offsets into this; used to extract the exact text of a vim9 bare dict key
    /// (`{a-b: 1}` → `"a-b"`), which does not survive tokenization intact.
    src: String,
    /// Count of bracket-nested sub-expressions currently open (`(expr)`, list
    /// elements, call arguments, dict values). Vim caps this at
    /// [`Self::EXPR_MAX_DEPTH`] and raises `E1169` once it is reached — verified
    /// against `/opt/homebrew/bin/vim`: 999 nested `(` succeed, 1000 → E1169.
    /// Ternary branches, unary leaders and left-associative chains (`.`, `+`,
    /// `[idx]`) do NOT bump this — vim accepts those to great depth (500000
    /// nested `-` succeed), matching a loop/tail-recursive parse here.
    depth: u32,
}

impl Parser {
    /// Vim's expression-nesting limit (`E1169: Expression too recursive`). The
    /// error fires when the 1000th bracket opens, so 999 levels are accepted.
    const EXPR_MAX_DEPTH: u32 = 1000;

    fn new(toks: Vec<Token>, src: &str) -> Self {
        Parser {
            toks,
            i: 0,
            src: src.to_string(),
            depth: 0,
        }
    }

    /// Parse a bracket-nested sub-expression, bumping the recursion counter so a
    /// pathologically deep `((((…))))` / `[[[…]]]` / `f(f(f(…)))` / `{a:{a:…}}`
    /// raises Vim's `E1169` instead of overflowing the native stack. Guards only
    /// the bracket-open recursion points (paren, list/call element, dict value)
    /// so it matches vim, which does not count ternary/unary/concat depth.
    fn nested_eval1(&mut self) -> Result<Expr, VimlError> {
        self.depth += 1;
        if self.depth >= Self::EXPR_MAX_DEPTH {
            self.depth -= 1;
            let tail = self.src.get(self.toks[self.i].span..).unwrap_or("");
            return Err(VimlError::msg(format!(
                "E1169: Expression too recursive: {tail}"
            )));
        }
        let r = self.eval1();
        self.depth -= 1;
        r
    }

    fn peek(&self) -> &Tok {
        &self.toks[self.i].kind
    }

    fn advance(&mut self) -> Tok {
        let t = self.toks[self.i].kind.clone();
        if self.i + 1 < self.toks.len() {
            self.i += 1;
        }
        t
    }

    fn eat(&mut self, want: &Tok) -> Result<(), VimlError> {
        if self.peek() == want {
            self.advance();
            Ok(())
        } else {
            Err(VimlError::msg(format!(
                "E15: expected {want:?}, found {:?}",
                self.peek()
            )))
        }
    }

    fn eval1(&mut self) -> Result<Expr, VimlError> {
        let cond = self.eval2()?;
        match self.peek() {
            Tok::Question => {
                self.advance();
                let then = self.eval1()?;
                self.eat(&Tok::Colon)?;
                let otherwise = self.eval1()?;
                Ok(Expr::Ternary {
                    cond: Box::new(cond),
                    then: Box::new(then),
                    otherwise: Box::new(otherwise),
                })
            }
            Tok::QuestionQuestion => {
                self.advance();
                let rhs = self.eval1()?;
                Ok(Expr::Coalesce(Box::new(cond), Box::new(rhs)))
            }
            _ => Ok(cond),
        }
    }

    fn eval2(&mut self) -> Result<Expr, VimlError> {
        let mut lhs = self.eval3()?;
        while matches!(self.peek(), Tok::OrOr) {
            self.advance();
            let rhs = self.eval3()?;
            lhs = Expr::Or(Box::new(lhs), Box::new(rhs));
        }
        Ok(lhs)
    }

    fn eval3(&mut self) -> Result<Expr, VimlError> {
        let mut lhs = self.eval4()?;
        while matches!(self.peek(), Tok::AndAnd) {
            self.advance();
            let rhs = self.eval4()?;
            lhs = Expr::And(Box::new(lhs), Box::new(rhs));
        }
        Ok(lhs)
    }

    fn eval4(&mut self) -> Result<Expr, VimlError> {
        let lhs = self.eval5()?;
        let (op, case) = match self.peek() {
            Tok::Cmp(op, case) => (*op, *case),
            Tok::Ident(id) if id == "is" => (CmpOp::Is, CaseFlag::Default),
            Tok::Ident(id) if id == "isnot" => (CmpOp::IsNot, CaseFlag::Default),
            _ => return Ok(lhs),
        };
        self.advance();
        let rhs = self.eval5()?;
        Ok(Expr::Compare {
            op,
            case,
            lhs: Box::new(lhs),
            rhs: Box::new(rhs),
        })
    }

    fn eval5(&mut self) -> Result<Expr, VimlError> {
        let mut lhs = self.eval6()?;
        loop {
            let op = match self.peek() {
                Tok::Plus => ArithOp::Add,
                Tok::Minus => ArithOp::Sub,
                Tok::Dot | Tok::DotDot => ArithOp::Concat,
                _ => break,
            };
            self.advance();
            let rhs = self.eval6()?;
            lhs = Expr::Arith {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
            };
        }
        Ok(lhs)
    }

    fn eval6(&mut self) -> Result<Expr, VimlError> {
        let mut lhs = self.eval7()?;
        loop {
            let op = match self.peek() {
                Tok::Star => ArithOp::Mul,
                Tok::Slash => ArithOp::Div,
                Tok::Percent => ArithOp::Mod,
                _ => break,
            };
            self.advance();
            let rhs = self.eval7()?;
            lhs = Expr::Arith {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
            };
        }
        Ok(lhs)
    }

    fn eval7(&mut self) -> Result<Expr, VimlError> {
        let mut leaders = Vec::new();
        loop {
            match self.peek() {
                Tok::Bang => {
                    self.advance();
                    leaders.push(UnaryOp::Not);
                }
                Tok::Minus => {
                    self.advance();
                    leaders.push(UnaryOp::Neg);
                }
                Tok::Plus => {
                    self.advance();
                    leaders.push(UnaryOp::Plus);
                }
                _ => break,
            }
        }
        let mut e = self.primary()?;
        e = self.postfix(e)?;
        for op in leaders.into_iter().rev() {
            e = Expr::Unary {
                op,
                expr: Box::new(e),
            };
        }
        Ok(e)
    }

    fn primary(&mut self) -> Result<Expr, VimlError> {
        match self.advance() {
            Tok::Number(n) => Ok(Expr::Number(n)),
            Tok::Float(f) => Ok(Expr::Float(f)),
            // Blob literal `0z…` desugars to `list2blob([byte, …])`, reusing the
            // ported list2blob builtin to build the Blob value.
            Tok::Blob(bytes) => Ok(Expr::Call {
                name: "list2blob".to_string(),
                args: vec![Expr::List(
                    bytes.into_iter().map(|b| Expr::Number(b as i64)).collect(),
                )],
            }),
            Tok::Str(s) => Ok(Expr::Str(s)),
            Tok::InterpStr(parts) => self.lower_interp(parts),
            Tok::Option(o) => Ok(Expr::Option(o)),
            Tok::Env(e) => Ok(Expr::Env(e)),
            Tok::Register(r) => Ok(Expr::Register(r)),
            Tok::LParen => {
                // vim9 lambda `(params) => body` (opening `(` already consumed);
                // otherwise an ordinary parenthesised expression.
                if self.at_vim9_lambda() {
                    return self.vim9_lambda();
                }
                let e = self.nested_eval1()?;
                self.eat(&Tok::RParen)?;
                Ok(e)
            }
            Tok::LBracket => self.list_literal(),
            Tok::LBrace => {
                if self.at_lambda() {
                    self.lambda()
                } else {
                    self.dict_literal()
                }
            }
            Tok::HashBrace => self.literal_dict(),
            Tok::Ident(name) => {
                if matches!(self.peek(), Tok::LParen) {
                    self.advance();
                    let args = self.arg_list(&Tok::RParen)?;
                    Ok(Expr::Call { name, args })
                } else if vim9_active() {
                    // vim9 keyword literals (`vim9.txt`): in a `:vim9script` script
                    // or a `def…enddef` body, bare `true`/`false`/`null` are the
                    // boolean/special constants — equal to `v:true`/`v:false`/`v:null`
                    // (binary-verified: `true == v:true`, `type(true) == 6`,
                    // `type(null) == 7`). In legacy mode they stay ordinary names
                    // (bare `true` → E121 undefined variable), so this only fires
                    // under `vim9_active()`.
                    // The `null_*` names are vim9 predefined constants
                    // (`vim9.txt`, "Predefined variables"). Each is the null
                    // value of its type; oracle-verified against Vim 9.2 they
                    // are observably the empty literal of that type
                    // (`type(null_string) == 1 && null_string == ''`,
                    // `type(null_list) == 3 && string(null_list) == '[]'`,
                    // `null_blob` → `0z`, `null_function`/`null_partial` →
                    // `function('')`). `null_channel`/`null_job` need channel
                    // and job value types vimlrs does not have, so they stay
                    // ordinary names rather than being faked.
                    match name.as_str() {
                        "true" => Ok(Expr::Var("v:true".to_string())),
                        "false" => Ok(Expr::Var("v:false".to_string())),
                        "null" => Ok(Expr::Var("v:null".to_string())),
                        "null_string" => Ok(Expr::Str(String::new())),
                        "null_list" => Ok(Expr::List(Vec::new())),
                        "null_dict" => Ok(Expr::Dict(Vec::new())),
                        "null_blob" => Ok(Expr::Call {
                            name: "list2blob".to_string(),
                            args: vec![Expr::List(Vec::new())],
                        }),
                        "null_function" | "null_partial" => Ok(Expr::Call {
                            name: "function".to_string(),
                            args: vec![Expr::Str(String::new())],
                        }),
                        _ => Ok(Expr::Var(name)),
                    }
                } else {
                    Ok(Expr::Var(name))
                }
            }
            other => Err(VimlError::msg(format!(
                "E15: Invalid expression: unexpected {other:?}"
            ))),
        }
    }

    /// Lower an interpolated string's raw parts into an [`Expr::Interp`]: each
    /// literal chunk becomes an `Expr::Str`, each `{expr}` region is sub-parsed.
    /// A blank/empty region (`{ }`, `{}`) sub-parses to an E15 error, matching
    /// Vim (an empty interpolation expression is invalid). The compiler
    /// echo-stringifies and concatenates the segments.
    fn lower_interp(&mut self, parts: Vec<InterpPart>) -> Result<Expr, VimlError> {
        let mut segs = Vec::with_capacity(parts.len());
        for part in parts {
            match part {
                InterpPart::Lit(s) => segs.push(Expr::Str(s)),
                InterpPart::Expr(src) => segs.push(parse_expr(&src)?),
            }
        }
        Ok(Expr::Interp(segs))
    }

    /// True when the `(` at the current position directly abuts the previous
    /// token (no whitespace), i.e. it is a call applied to the preceding value
    /// rather than a separate parenthesised argument.
    fn lparen_abuts_prev(&self) -> bool {
        let i = self.i;
        i > 0
            && self.toks.get(i).is_some_and(|t| t.kind == Tok::LParen)
            && self.toks[i].span == self.toks[i - 1].end
    }

    /// True when the `Tok::Dot` at the current position is a dict member access
    /// `d.key` rather than the `..`-style concat operator: it must directly abut
    /// the base (no space before) and be immediately followed by a bare name (no
    /// space after). `a . b` (spaced) stays concatenation.
    fn at_member_dot(&self) -> bool {
        let i = self.i;
        if i == 0 || i + 1 >= self.toks.len() {
            return false;
        }
        let dot = &self.toks[i];
        if dot.kind != Tok::Dot {
            return false;
        }
        let prev = &self.toks[i - 1];
        let next = &self.toks[i + 1];
        // `.name(` is concatenation with a function call (`a().b(x)` / `s.f(x)`),
        // not a member call — legacy Vimscript has no direct `dict.key(args)`
        // call syntax (that is vim9). So only treat `.name` as a member read when
        // the name is NOT immediately followed by '('.
        let followed_by_call =
            matches!(self.toks.get(i + 2), Some(t) if t.kind == Tok::LParen && t.span == next.end);
        matches!(next.kind, Tok::Ident(_))
            && dot.span == prev.end // no space before the dot
            && next.span == dot.end // no space after the dot
            && !followed_by_call
    }

    /// Postfix subscripts: `[index]`, `[from:to]`, `.name` dict member access,
    /// and `->method()`. A no-space `d.key` is a member read (a string subscript);
    /// a spaced `a . b` is left to `eval6` as concatenation.
    fn postfix(&mut self, mut base: Expr) -> Result<Expr, VimlError> {
        loop {
            // `d.key` member read. Skipped for statically-known non-Dict bases —
            // number/float (`1.foo`), string (`'('.name`) and list literals can
            // never be a Dict at runtime, so their `.name` is unambiguously
            // concat and is left to `eval6`. Leaving it there is also what makes
            // the concat RHS bind trailing subscripts correctly (`'('.p[0]` →
            // `'(' . p[0]`, not `('(' . p)[0]`): eval6 re-parses the RHS as a
            // fresh postfix chain, matching vim's `handle_subscript`, which only
            // consumes `.name` when `rettv->v_type == VAR_DICT`.
            if self.at_member_dot()
                && !matches!(
                    base,
                    Expr::Number(_) | Expr::Float(_) | Expr::Str(_) | Expr::List(_)
                )
            {
                self.advance(); // consume the dot
                if let Tok::Ident(key) = self.advance() {
                    // Syntactically identical to string concat (`a.b`): whether
                    // this is a Dict subscript or `.`-concat is decided by the
                    // runtime type of `base` (see the `Expr::Member` lowering in
                    // `compile_viml.rs`). Carry the literal key; it doubles as the
                    // bare variable name for the concat RHS (`a.b` → `a . b`).
                    base = Expr::Member {
                        base: Box::new(base),
                        key,
                    };
                    continue;
                }
            }
            // `expr(args)` — call a funcref-valued expression directly. Only when
            // `(` abuts the base (no space): a bare `name(...)` is already a Call
            // from `primary`, so an abutting `(` here always follows another
            // postfix result (`function('x')(a)`, `funcs[0](a)`); a spaced
            // `echo F (1)` stays two arguments.
            if matches!(self.peek(), Tok::LParen) && self.lparen_abuts_prev() {
                self.advance(); // consume '('
                let args = self.arg_list(&Tok::RParen)?;
                base = Expr::CallExpr {
                    callee: Box::new(base),
                    args,
                };
                continue;
            }
            match self.peek() {
                Tok::LBracket => {
                    self.advance();
                    base = self.subscript(base)?;
                }
                Tok::Arrow => {
                    self.advance();
                    let name = match self.advance() {
                        Tok::Ident(n) => n,
                        other => {
                            return Err(VimlError::msg(format!(
                                "E15: expected method name after '->', found {other:?}"
                            )))
                        }
                    };
                    self.eat(&Tok::LParen)?;
                    let args = self.arg_list(&Tok::RParen)?;
                    base = Expr::Method {
                        base: Box::new(base),
                        name,
                        args,
                    };
                }
                _ => break,
            }
        }
        Ok(base)
    }

    fn subscript(&mut self, base: Expr) -> Result<Expr, VimlError> {
        if matches!(self.peek(), Tok::Colon) {
            self.advance();
            let to = if matches!(self.peek(), Tok::RBracket) {
                None
            } else {
                Some(Box::new(self.eval1()?))
            };
            self.eat(&Tok::RBracket)?;
            return Ok(Expr::Slice {
                base: Box::new(base),
                from: None,
                to,
            });
        }
        let first = self.eval1()?;
        if matches!(self.peek(), Tok::Colon) {
            self.advance();
            let to = if matches!(self.peek(), Tok::RBracket) {
                None
            } else {
                Some(Box::new(self.eval1()?))
            };
            self.eat(&Tok::RBracket)?;
            Ok(Expr::Slice {
                base: Box::new(base),
                from: Some(Box::new(first)),
                to,
            })
        } else {
            self.eat(&Tok::RBracket)?;
            Ok(Expr::Index {
                base: Box::new(base),
                index: Box::new(first),
            })
        }
    }

    fn list_literal(&mut self) -> Result<Expr, VimlError> {
        Ok(Expr::List(self.arg_list(&Tok::RBracket)?))
    }

    /// Lookahead (just past the opening `{`) deciding lambda vs dict: a lambda
    /// is `{ -> …}` or `{ ident (, ident)* -> …}` — a top-level `->` reached
    /// through only bare names and commas. Anything else (a `:` key, a string
    /// key, `}`) is a dict.
    fn at_lambda(&self) -> bool {
        let mut j = self.i;
        if matches!(self.toks.get(j).map(|t| &t.kind), Some(Tok::Arrow)) {
            return true; // {-> body}
        }
        loop {
            if !matches!(self.toks.get(j).map(|t| &t.kind), Some(Tok::Ident(_))) {
                return false;
            }
            j += 1;
            match self.toks.get(j).map(|t| &t.kind) {
                Some(Tok::Arrow) => return true,
                Some(Tok::Comma) => j += 1,
                _ => return false,
            }
        }
    }

    /// Lookahead (opening `(` already consumed) distinguishing a vim9 lambda
    /// `(params) => body` — optionally `(params): rettype => body` — from an
    /// ordinary parenthesised expression. Scans to the matching `)`, then requires
    /// a `=>` next, allowing an optional `: rettype` annotation in between. Only
    /// fires in a vim9 region; legacy scripts have no `=>` lambda.
    fn at_vim9_lambda(&self) -> bool {
        if !vim9_active() {
            return false;
        }
        let mut j = self.i;
        let mut depth = 1i32;
        while depth > 0 {
            match self.toks.get(j).map(|t| &t.kind) {
                None | Some(Tok::Eof) => return false,
                Some(Tok::LParen) | Some(Tok::LBracket) | Some(Tok::LBrace) => depth += 1,
                Some(Tok::RParen) | Some(Tok::RBracket) | Some(Tok::RBrace) => depth -= 1,
                _ => {}
            }
            j += 1;
        }
        // `j` is now just past the matching `)`.
        match self.toks.get(j).map(|t| &t.kind) {
            Some(Tok::FatArrow) => true,
            // `(params): rettype => body` — a top-level `=>` must follow the `:`.
            Some(Tok::Colon) => {
                let mut k = j + 1;
                let mut d = 0i32;
                while let Some(t) = self.toks.get(k) {
                    match &t.kind {
                        Tok::FatArrow if d == 0 => return true,
                        Tok::LParen | Tok::LBracket | Tok::LBrace => d += 1,
                        Tok::RParen | Tok::RBracket | Tok::RBrace => {
                            if d == 0 {
                                return false;
                            }
                            d -= 1;
                        }
                        Tok::Comma if d == 0 => return false,
                        Tok::Eof => return false,
                        _ => {}
                    }
                    k += 1;
                }
                false
            }
            _ => false,
        }
    }

    /// Parse a vim9 lambda `(params) => body` (opening `(` already consumed).
    /// Parameter type annotations (`x: type`) and an optional return type
    /// (`): type =>`) are accepted and discarded — only the names bind, reusing the
    /// same `Expr::Lambda` representation as the legacy `{params -> body}` form.
    fn vim9_lambda(&mut self) -> Result<Expr, VimlError> {
        let mut params = Vec::new();
        if !matches!(self.peek(), Tok::RParen) {
            loop {
                match self.advance() {
                    // A scope-letter parameter (`a`, `b`, `s`, `g`, `l`, `t`, `v`,
                    // `w`) followed by a `: type` annotation is lexed with the colon
                    // absorbed into the identifier (`a: number` → `Ident("a:")`,
                    // `a:list` → `Ident("a:list")`). Split the name off at that colon
                    // and skip the remaining type tokens; the standalone-colon case
                    // (`n: number`) is handled just below.
                    Tok::Ident(n) => match n.find(':') {
                        Some(pos) => {
                            params.push(n[..pos].to_string());
                            self.skip_type();
                        }
                        None => params.push(n),
                    },
                    other => {
                        return Err(VimlError::msg(format!(
                            "E15: expected lambda parameter, found {other:?}"
                        )))
                    }
                }
                if matches!(self.peek(), Tok::Colon) {
                    self.advance();
                    self.skip_type();
                }
                match self.peek() {
                    Tok::Comma => {
                        self.advance();
                    }
                    Tok::RParen => break,
                    other => {
                        return Err(VimlError::msg(format!(
                            "E15: expected ',' or ')' in lambda, found {other:?}"
                        )))
                    }
                }
            }
        }
        self.eat(&Tok::RParen)?;
        if matches!(self.peek(), Tok::Colon) {
            self.advance();
            self.skip_type();
        }
        self.eat(&Tok::FatArrow)?;
        let body = self.eval1()?;
        Ok(Expr::Lambda {
            params,
            body: Box::new(body),
        })
    }

    /// Skip a vim9 type expression in a lambda signature, consuming tokens up to a
    /// top-level `,`, `)`, or `=>` (its terminators in param / return position).
    /// `<…>` (`list<number>`), `(…)` (`func(...)`) and `[…]` nest.
    fn skip_type(&mut self) {
        let mut angle = 0i32;
        let mut paren = 0i32;
        loop {
            match self.peek() {
                Tok::Cmp(CmpOp::Less, _) => {
                    angle += 1;
                    self.advance();
                }
                Tok::Cmp(CmpOp::Greater, _) if angle > 0 => {
                    angle -= 1;
                    self.advance();
                }
                Tok::LParen | Tok::LBracket => {
                    paren += 1;
                    self.advance();
                }
                Tok::RParen | Tok::RBracket if paren > 0 => {
                    paren -= 1;
                    self.advance();
                }
                Tok::Comma | Tok::RParen | Tok::FatArrow if angle == 0 && paren == 0 => break,
                Tok::Eof => break,
                _ => {
                    self.advance();
                }
            }
        }
    }

    /// Parse a lambda `{params -> body}` (the opening `{` already consumed).
    fn lambda(&mut self) -> Result<Expr, VimlError> {
        let mut params = Vec::new();
        if !matches!(self.peek(), Tok::Arrow) {
            loop {
                match self.advance() {
                    Tok::Ident(n) => params.push(n),
                    other => {
                        return Err(VimlError::msg(format!(
                            "E15: expected lambda parameter, found {other:?}"
                        )))
                    }
                }
                match self.peek() {
                    Tok::Comma => {
                        self.advance();
                    }
                    Tok::Arrow => break,
                    other => {
                        return Err(VimlError::msg(format!(
                            "E15: expected ',' or '->' in lambda, found {other:?}"
                        )))
                    }
                }
            }
        }
        self.eat(&Tok::Arrow)?;
        let body = self.eval1()?;
        self.eat(&Tok::RBrace)?;
        Ok(Expr::Lambda {
            params,
            body: Box::new(body),
        })
    }

    /// Parse a literal-key Dict `#{key: val, …}` (the opening `#{` consumed):
    /// each key is a bare word (or number) used as a String. Because a single
    /// scope-like char absorbs its `:` in the lexer (`a:`), a key Ident ending
    /// in `:` already includes the separator; otherwise a `:` token follows.
    fn literal_dict(&mut self) -> Result<Expr, VimlError> {
        let mut pairs = Vec::new();
        if matches!(self.peek(), Tok::RBrace) {
            self.advance();
            return Ok(Expr::Dict(pairs));
        }
        loop {
            let raw = match self.advance() {
                Tok::Ident(s) => s,
                Tok::Number(n) => n.to_string(),
                Tok::Str(s) => s,
                other => {
                    return Err(VimlError::msg(format!(
                        "E15: expected literal Dict key, found {other:?}"
                    )))
                }
            };
            let (key, val) = if let Some(stripped) = raw.strip_suffix(':') {
                // `a:` — a scope-letter key absorbed its `:`; the value follows.
                (stripped.to_string(), self.nested_eval1()?)
            } else if let Some(c) = raw.find(':') {
                // `a:1` — a scope-letter key merged with a glued simple value
                // (the lexer can only glue a bareword/number after the `:`); split
                // and parse that fragment as the value.
                (raw[..c].to_string(), parse_expr(&raw[c + 1..])?)
            } else {
                self.eat(&Tok::Colon)?;
                (raw, self.nested_eval1()?)
            };
            pairs.push((Expr::Str(key), val));
            match self.advance() {
                Tok::Comma => {
                    if matches!(self.peek(), Tok::RBrace) {
                        self.advance();
                        break;
                    }
                }
                Tok::RBrace => break,
                other => {
                    return Err(VimlError::msg(format!(
                        "E15: expected ',' or '}}' in #{{}}, found {other:?}"
                    )))
                }
            }
        }
        Ok(Expr::Dict(pairs))
    }

    fn dict_literal(&mut self) -> Result<Expr, VimlError> {
        let mut pairs = Vec::new();
        if matches!(self.peek(), Tok::RBrace) {
            self.advance();
            return Ok(Expr::Dict(pairs));
        }
        // In a vim9 region `{key: val}` uses BARE literal keys — the key text is
        // taken verbatim, not evaluated as a variable (`:help vim9-scriptlocal`,
        // vim9.txt "the {} form uses literal keys"). Legacy scripts keep the
        // expression-keyed form.
        let vim9 = vim9_active();
        loop {
            let key = if vim9 {
                self.vim9_dict_key()?
            } else {
                let k = self.eval1()?;
                self.eat(&Tok::Colon)?;
                k
            };
            let val = self.nested_eval1()?;
            pairs.push((key, val));
            match self.advance() {
                Tok::Comma => {
                    if matches!(self.peek(), Tok::RBrace) {
                        self.advance();
                        break;
                    }
                }
                Tok::RBrace => break,
                other => {
                    return Err(VimlError::msg(format!(
                        "E15: expected ',' or '}}' in dict, found {other:?}"
                    )))
                }
            }
        }
        Ok(Expr::Dict(pairs))
    }

    /// Parse a vim9 dict key and its trailing `:`, leaving the cursor at the
    /// value. Three key forms, matching Vim 9.2:
    ///  * `{'k': v}` / `{"k": v}` — a quoted string key.
    ///  * `{[expr]: v}` — a bracketed computed key; `expr` is evaluated and its
    ///    string form is the key.
    ///  * `{a: 1}`, `{a-b: 1}`, `{007: 1}` — a BARE key: a run of
    ///    `[A-Za-z0-9_-]` used verbatim as a string (leading zeros kept). The
    ///    tokens (`Ident`/`Number`/`Minus`, or a scope-letter `Ident` that
    ///    absorbed the `:`) do not preserve the key intact, so it is read from
    ///    the source between the key start and the `:`.
    fn vim9_dict_key(&mut self) -> Result<Expr, VimlError> {
        if let Tok::Str(s) = self.peek().clone() {
            self.advance();
            self.eat(&Tok::Colon)?;
            return Ok(Expr::Str(s));
        }
        if matches!(self.peek(), Tok::LBracket) {
            self.advance();
            let key = self.eval1()?;
            self.eat(&Tok::RBracket)?;
            self.eat(&Tok::Colon)?;
            return Ok(key);
        }
        let start = self.toks[self.i].span;
        let bytes = self.src.as_bytes();
        let mut p = start;
        while p < bytes.len()
            && (bytes[p].is_ascii_alphanumeric() || bytes[p] == b'_' || bytes[p] == b'-')
        {
            p += 1;
        }
        if p == start {
            return Err(VimlError::msg(format!(
                "E15: expected literal Dict key, found {:?}",
                self.peek()
            )));
        }
        let key = self.src[start..p].to_string();
        // The `:` separator follows the key (optional intervening whitespace is
        // tolerated as a benign superset; strict Vim rejects it with E1068).
        let mut colon = p;
        while colon < bytes.len() && (bytes[colon] == b' ' || bytes[colon] == b'\t') {
            colon += 1;
        }
        if colon >= bytes.len() || bytes[colon] != b':' {
            return Err(VimlError::msg(
                "E720: Missing colon in Dictionary".to_string(),
            ));
        }
        // Advance past every token up to and including the `:` at offset `colon`
        // (a standalone `Colon`, or one absorbed into a scope-letter `Ident`),
        // leaving the cursor at the value.
        while self.toks[self.i].span <= colon && !matches!(self.peek(), Tok::Eof) {
            self.advance();
        }
        Ok(Expr::Str(key))
    }

    fn arg_list(&mut self, close: &Tok) -> Result<Vec<Expr>, VimlError> {
        let mut args = Vec::new();
        if self.peek() == close {
            self.advance();
            return Ok(args);
        }
        loop {
            args.push(self.nested_eval1()?);
            match self.advance() {
                Tok::Comma => {
                    if self.peek() == close {
                        self.advance();
                        break;
                    }
                }
                ref t if t == close => break,
                other => {
                    return Err(VimlError::msg(format!(
                        "E15: expected ',' or {close:?}, found {other:?}"
                    )))
                }
            }
        }
        Ok(args)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn command_modifiers_are_stripped() {
        // `silent!` + a real command → the command survives.
        assert!(matches!(
            parse_stmt("silent! colorscheme molokai").unwrap(),
            Stmt::Colorscheme(n) if n == "molokai"
        ));
        // Stacked modifiers, a `verbose` count, and abbreviations.
        assert!(matches!(
            parse_stmt("silent noautocmd verbose 9 set number").unwrap(),
            Stmt::Set(a) if a == "number"
        ));
        // A bare modifier is a no-op, not an error.
        assert!(matches!(parse_stmt("silent").unwrap(), Stmt::Expr(_)));
        // A longer identifier that merely starts with a modifier word is NOT a
        // modifier (`silentfoo` is a user command, not `silent` + `foo`).
        assert!(matches!(
            parse_stmt("Silentcmd arg").unwrap(),
            Stmt::UserCmd(_)
        ));
    }

    #[test]
    fn tolerant_parse_skips_bad_statements() {
        // Line 2 is an unterminated string (a parse error). Tolerant parsing must
        // still yield the good statements on lines 1 and 3.
        let src = "set number\nlet x = \"oops\ncolorscheme molokai\n";
        let (stmts, errs) = parse_program_lines_tolerant(src);
        assert_eq!(errs.len(), 1, "one statement skipped");
        assert!(stmts
            .iter()
            .any(|(_, s)| matches!(s, Stmt::Set(a) if a == "number")));
        assert!(stmts
            .iter()
            .any(|(_, s)| matches!(s, Stmt::Colorscheme(n) if n == "molokai")));
    }

    #[test]
    fn precedence_add_mul() {
        match parse_expr("1 + 2 * 3").unwrap() {
            Expr::Arith {
                op: ArithOp::Add,
                rhs,
                ..
            } => assert!(matches!(
                *rhs,
                Expr::Arith {
                    op: ArithOp::Mul,
                    ..
                }
            )),
            e => panic!("bad tree: {e:?}"),
        }
    }

    #[test]
    fn dict_member_dot_vs_concat() {
        // `d.key` (no spaces) → a runtime-dispatched Member (Dict subscript vs
        // string concat, decided by the runtime type of the base).
        match parse_expr("d.key").unwrap() {
            Expr::Member { key, .. } => assert_eq!(key, "key"),
            e => panic!("expected Member, got {e:?}"),
        }
        // Nested `d.a.b` → chained Members.
        assert!(matches!(parse_expr("d.a.b").unwrap(), Expr::Member { .. }));
        // `a . b` (spaced) stays concatenation.
        assert!(matches!(
            parse_expr("a . b").unwrap(),
            Expr::Arith {
                op: ArithOp::Concat,
                ..
            }
        ));
        // `'x' .. 'y'` stays concatenation.
        assert!(matches!(
            parse_expr("'x' .. 'y'").unwrap(),
            Expr::Arith {
                op: ArithOp::Concat,
                ..
            }
        ));
    }

    #[test]
    fn literal_key_dict() {
        // `#{a: 1}` is a Dict with the bare word as a String key.
        match parse_expr("#{a: 1, name: 'x'}").unwrap() {
            Expr::Dict(pairs) => {
                assert_eq!(pairs.len(), 2);
                assert!(matches!(&pairs[0].0, Expr::Str(s) if s == "a"));
                assert!(matches!(&pairs[1].0, Expr::Str(s) if s == "name"));
            }
            e => panic!("expected Dict, got {e:?}"),
        }
        assert!(matches!(parse_expr("#{}").unwrap(), Expr::Dict(_)));
    }

    #[test]
    fn one_line_blocks() {
        // `if … | … | endif` on one line parses as a full If block.
        assert!(matches!(
            parse_program("if 1 | echo 'y' | endif").unwrap().as_slice(),
            [Stmt::If { .. }]
        ));
        // A leaf command then a one-line block: two statements.
        match parse_program("let x = 5 | if x > 3 | echo 'big' | endif")
            .unwrap()
            .as_slice()
        {
            [Stmt::Let { .. }, Stmt::If { .. }] => {}
            s => panic!("expected [Let, If], got {s:?}"),
        }
        // A `for` one-liner.
        assert!(matches!(
            parse_program("for i in [1] | echo i | endfor")
                .unwrap()
                .as_slice(),
            [Stmt::For { .. }]
        ));
        // A plain bar line (no block) is still two leaf statements.
        assert_eq!(parse_program("let a = 1 | echo a").unwrap().len(), 2);
    }

    #[test]
    fn line_continuation() {
        // A `\` continuation line joins onto the previous logical line.
        let prog = parse_program("let x = [1,\n      \\ 2,\n      \\ 3]").unwrap();
        match &prog[0] {
            Stmt::Let {
                expr: Expr::List(items),
                ..
            } => assert_eq!(items.len(), 3),
            s => panic!("expected 3-item list, got {s:?}"),
        }
        // Line numbers: the statement after a 2-physical-line logical line keeps
        // its real physical number.
        let lines = parse_program_lines("let a = 1\nlet b = [10,\n  \\ 20]\nlet c = 3").unwrap();
        assert_eq!(lines[0].0, 1);
        assert_eq!(lines[1].0, 2); // the [10, 20] let starts on physical line 2
        assert_eq!(lines[2].0, 4); // `let c` is on physical line 4
    }

    #[test]
    fn lambda_vs_dict() {
        // `{x -> …}` and `{-> …}` are lambdas.
        assert!(matches!(
            parse_expr("{x -> x + 1}").unwrap(),
            Expr::Lambda { .. }
        ));
        assert!(matches!(
            parse_expr("{-> 42}").unwrap(),
            Expr::Lambda { .. }
        ));
        match parse_expr("{a, b -> a - b}").unwrap() {
            Expr::Lambda { params, .. } => assert_eq!(params, vec!["a", "b"]),
            e => panic!("expected lambda, got {e:?}"),
        }
        // `{...}` with string keys and `{}` are dicts (not lambdas).
        assert!(matches!(parse_expr("{'a': 1}").unwrap(), Expr::Dict(_)));
        assert!(matches!(
            parse_expr("{'k': v, 'j': w}").unwrap(),
            Expr::Dict(_)
        ));
        assert!(matches!(parse_expr("{}").unwrap(), Expr::Dict(_)));
    }

    #[test]
    fn collections_and_stmts() {
        assert!(matches!(parse_expr("[1, 2, 3]").unwrap(), Expr::List(_)));
        assert!(matches!(parse_expr("x[1:2]").unwrap(), Expr::Slice { .. }));
        assert!(matches!(parse_stmt("echo 1 + 1").unwrap(), Stmt::Echo(_)));
        assert!(matches!(parse_stmt("let x = 5").unwrap(), Stmt::Let { .. }));
    }
}