seq-compiler 3.0.6

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

use crate::ast::{
    Include, MatchArm, Pattern, Program, SourceLocation, Span, Statement, UnionDef, UnionField,
    UnionVariant, WordDef,
};
use crate::types::{Effect, SideEffect, StackType, Type};

/// A token with source position information
#[derive(Debug, Clone)]
pub struct Token {
    pub text: String,
    /// Line number (0-indexed for LSP compatibility)
    pub line: usize,
    /// Column number (0-indexed)
    pub column: usize,
}

impl Token {
    fn new(text: String, line: usize, column: usize) -> Self {
        Token { text, line, column }
    }
}

impl PartialEq<&str> for Token {
    fn eq(&self, other: &&str) -> bool {
        self.text == *other
    }
}

impl PartialEq<str> for Token {
    fn eq(&self, other: &str) -> bool {
        self.text == other
    }
}

pub struct Parser {
    tokens: Vec<Token>,
    pos: usize,
    /// Counter for assigning unique IDs to quotations
    /// Used by the typechecker to track inferred types
    next_quotation_id: usize,
    /// Pending lint annotations collected from `# seq:allow(lint-id)` comments
    pending_allowed_lints: Vec<String>,
}

impl Parser {
    pub fn new(source: &str) -> Self {
        let tokens = tokenize(source);
        Parser {
            tokens,
            pos: 0,
            next_quotation_id: 0,
            pending_allowed_lints: Vec::new(),
        }
    }

    pub fn parse(&mut self) -> Result<Program, String> {
        let mut program = Program::new();

        // Check for unclosed string error from tokenizer
        if let Some(error_token) = self.tokens.iter().find(|t| *t == "<<<UNCLOSED_STRING>>>") {
            return Err(format!(
                "Unclosed string literal at line {}, column {} - missing closing quote",
                error_token.line + 1, // 1-indexed for user display
                error_token.column + 1
            ));
        }

        while !self.is_at_end() {
            self.skip_comments();
            if self.is_at_end() {
                break;
            }

            // Check for include statement
            if self.check("include") {
                let include = self.parse_include()?;
                program.includes.push(include);
                continue;
            }

            // Check for union definition
            if self.check("union") {
                let union_def = self.parse_union_def()?;
                program.unions.push(union_def);
                continue;
            }

            let word = self.parse_word_def()?;
            program.words.push(word);
        }

        Ok(program)
    }

    /// Parse an include statement:
    ///   include std:http     -> Include::Std("http")
    ///   include ffi:readline -> Include::Ffi("readline")
    ///   include "my-utils"   -> Include::Relative("my-utils")
    fn parse_include(&mut self) -> Result<Include, String> {
        self.consume("include");

        let token = self
            .advance()
            .ok_or("Expected module name after 'include'")?
            .clone();

        // Check for std: prefix (tokenizer splits this into "std", ":", "name")
        if token == "std" {
            // Expect : token
            if !self.consume(":") {
                return Err("Expected ':' after 'std' in include statement".to_string());
            }
            // Get the module name
            let name = self
                .advance()
                .ok_or("Expected module name after 'std:'")?
                .clone();
            return Ok(Include::Std(name));
        }

        // Check for ffi: prefix
        if token == "ffi" {
            // Expect : token
            if !self.consume(":") {
                return Err("Expected ':' after 'ffi' in include statement".to_string());
            }
            // Get the library name
            let name = self
                .advance()
                .ok_or("Expected library name after 'ffi:'")?
                .clone();
            return Ok(Include::Ffi(name));
        }

        // Check for quoted string (relative path)
        if token.starts_with('"') && token.ends_with('"') {
            let path = token.trim_start_matches('"').trim_end_matches('"');
            return Ok(Include::Relative(path.to_string()));
        }

        Err(format!(
            "Invalid include syntax '{}'. Use 'include std:name', 'include ffi:lib', or 'include \"path\"'",
            token
        ))
    }

    /// Parse a union type definition:
    ///   union Message {
    ///     Get { response-chan: Int }
    ///     Increment { response-chan: Int }
    ///     Report { op: Int, delta: Int, total: Int }
    ///   }
    fn parse_union_def(&mut self) -> Result<UnionDef, String> {
        // Capture start line from 'union' token
        let start_line = self.current_token().map(|t| t.line).unwrap_or(0);

        // Consume 'union' keyword
        self.consume("union");

        // Get union name (must start with uppercase)
        let name = self
            .advance()
            .ok_or("Expected union name after 'union'")?
            .clone();

        if !name
            .chars()
            .next()
            .map(|c| c.is_uppercase())
            .unwrap_or(false)
        {
            return Err(format!(
                "Union name '{}' must start with an uppercase letter",
                name
            ));
        }

        // Skip comments and newlines
        self.skip_comments();

        // Expect '{'
        if !self.consume("{") {
            return Err(format!(
                "Expected '{{' after union name '{}', got '{}'",
                name,
                self.current()
            ));
        }

        // Parse variants until '}'
        let mut variants = Vec::new();
        loop {
            self.skip_comments();

            if self.check("}") {
                break;
            }

            if self.is_at_end() {
                return Err(format!("Unexpected end of file in union '{}'", name));
            }

            variants.push(self.parse_union_variant()?);
        }

        // Capture end line from '}' token before consuming
        let end_line = self.current_token().map(|t| t.line).unwrap_or(start_line);

        // Consume '}'
        self.consume("}");

        if variants.is_empty() {
            return Err(format!("Union '{}' must have at least one variant", name));
        }

        // Check for duplicate variant names
        let mut seen_variants = std::collections::HashSet::new();
        for variant in &variants {
            if !seen_variants.insert(&variant.name) {
                return Err(format!(
                    "Duplicate variant name '{}' in union '{}'",
                    variant.name, name
                ));
            }
        }

        Ok(UnionDef {
            name,
            variants,
            source: Some(SourceLocation::span(
                std::path::PathBuf::new(),
                start_line,
                end_line,
            )),
        })
    }

    /// Parse a single union variant:
    ///   Get { response-chan: Int }
    ///   or just: Empty (no fields)
    fn parse_union_variant(&mut self) -> Result<UnionVariant, String> {
        let start_line = self.current_token().map(|t| t.line).unwrap_or(0);

        // Get variant name (must start with uppercase)
        let name = self.advance().ok_or("Expected variant name")?.clone();

        if !name
            .chars()
            .next()
            .map(|c| c.is_uppercase())
            .unwrap_or(false)
        {
            return Err(format!(
                "Variant name '{}' must start with an uppercase letter",
                name
            ));
        }

        self.skip_comments();

        // Check for optional fields
        let fields = if self.check("{") {
            self.consume("{");
            let fields = self.parse_union_fields()?;
            if !self.consume("}") {
                return Err(format!("Expected '}}' after variant '{}' fields", name));
            }
            fields
        } else {
            Vec::new()
        };

        Ok(UnionVariant {
            name,
            fields,
            source: Some(SourceLocation::new(std::path::PathBuf::new(), start_line)),
        })
    }

    /// Parse union fields: name: Type, name: Type, ...
    fn parse_union_fields(&mut self) -> Result<Vec<UnionField>, String> {
        let mut fields = Vec::new();

        loop {
            self.skip_comments();

            if self.check("}") {
                break;
            }

            // Get field name
            let field_name = self.advance().ok_or("Expected field name")?.clone();

            // Expect ':'
            if !self.consume(":") {
                return Err(format!(
                    "Expected ':' after field name '{}', got '{}'",
                    field_name,
                    self.current()
                ));
            }

            // Get type name
            let type_name = self
                .advance()
                .ok_or("Expected type name after ':'")?
                .clone();

            fields.push(UnionField {
                name: field_name,
                type_name,
            });

            // Optional comma separator
            self.skip_comments();
            self.consume(",");
        }

        // Check for duplicate field names
        let mut seen_fields = std::collections::HashSet::new();
        for field in &fields {
            if !seen_fields.insert(&field.name) {
                return Err(format!("Duplicate field name '{}' in variant", field.name));
            }
        }

        Ok(fields)
    }

    fn parse_word_def(&mut self) -> Result<WordDef, String> {
        // Consume any pending lint annotations collected from comments before this word
        let allowed_lints = std::mem::take(&mut self.pending_allowed_lints);

        // Capture start line from ':' token
        let start_line = self.current_token().map(|t| t.line).unwrap_or(0);

        // Expect ':'
        if !self.consume(":") {
            return Err(format!(
                "Expected ':' to start word definition, got '{}'",
                self.current()
            ));
        }

        // Get word name
        let name = self
            .advance()
            .ok_or("Expected word name after ':'")?
            .clone();

        // Parse stack effect if present: ( ..a Int -- ..a Bool )
        let effect = if self.check("(") {
            Some(self.parse_stack_effect()?)
        } else {
            None
        };

        // Parse body until ';'
        let mut body = Vec::new();
        while !self.check(";") {
            if self.is_at_end() {
                return Err(format!("Unexpected end of file in word '{}'", name));
            }

            // Skip comments and newlines in body
            self.skip_comments();
            if self.check(";") {
                break;
            }

            body.push(self.parse_statement()?);
        }

        // Capture end line from ';' token before consuming
        let end_line = self.current_token().map(|t| t.line).unwrap_or(start_line);

        // Consume ';'
        self.consume(";");

        Ok(WordDef {
            name,
            effect,
            body,
            source: Some(crate::ast::SourceLocation::span(
                std::path::PathBuf::new(),
                start_line,
                end_line,
            )),
            allowed_lints,
        })
    }

    fn parse_statement(&mut self) -> Result<Statement, String> {
        use crate::ast::Span;
        let tok = self.advance_token().ok_or("Unexpected end of file")?;
        let token = &tok.text;
        let tok_line = tok.line;
        let tok_column = tok.column;
        let tok_len = tok.text.len();

        // Check if it looks like a float literal (contains . or scientific notation)
        // Must check this BEFORE integer parsing
        if let Some(f) = is_float_literal(token)
            .then(|| token.parse::<f64>().ok())
            .flatten()
        {
            return Ok(Statement::FloatLiteral(f));
        }

        // Try to parse as hex literal (0x or 0X prefix)
        if let Some(hex) = token
            .strip_prefix("0x")
            .or_else(|| token.strip_prefix("0X"))
        {
            return i64::from_str_radix(hex, 16)
                .map(Statement::IntLiteral)
                .map_err(|_| format!("Invalid hex literal: {}", token));
        }

        // Try to parse as binary literal (0b or 0B prefix)
        if let Some(bin) = token
            .strip_prefix("0b")
            .or_else(|| token.strip_prefix("0B"))
        {
            return i64::from_str_radix(bin, 2)
                .map(Statement::IntLiteral)
                .map_err(|_| format!("Invalid binary literal: {}", token));
        }

        // Try to parse as decimal integer literal
        if let Ok(n) = token.parse::<i64>() {
            return Ok(Statement::IntLiteral(n));
        }

        // Try to parse as boolean literal
        if token == "true" {
            return Ok(Statement::BoolLiteral(true));
        }
        if token == "false" {
            return Ok(Statement::BoolLiteral(false));
        }

        // Try to parse as symbol literal (:foo, :some-name)
        if token == ":" {
            // Get the next token as the symbol name
            let name_tok = self
                .advance_token()
                .ok_or("Expected symbol name after ':', got end of input")?;
            let name = &name_tok.text;
            // Validate symbol name (identifier-like, kebab-case allowed)
            if name.is_empty() {
                return Err("Symbol name cannot be empty".to_string());
            }
            if name.starts_with(|c: char| c.is_ascii_digit()) {
                return Err(format!(
                    "Symbol name cannot start with a digit: ':{}'\n  Hint: Symbol names must start with a letter",
                    name
                ));
            }
            if let Some(bad_char) = name.chars().find(|c| {
                !c.is_alphanumeric()
                    && *c != '-'
                    && *c != '_'
                    && *c != '.'
                    && *c != '?'
                    && *c != '!'
            }) {
                return Err(format!(
                    "Symbol name contains invalid character '{}': ':{}'\n  Hint: Allowed: letters, digits, - _ . ? !",
                    bad_char, name
                ));
            }
            return Ok(Statement::Symbol(name.clone()));
        }

        // Try to parse as string literal
        if token.starts_with('"') {
            // Validate token has at least opening and closing quotes
            if token.len() < 2 || !token.ends_with('"') {
                return Err(format!("Malformed string literal: {}", token));
            }
            // Strip exactly one quote from each end (not all quotes, which would
            // incorrectly handle escaped quotes at string boundaries like "hello\"")
            let raw = &token[1..token.len() - 1];
            let unescaped = unescape_string(raw)?;
            return Ok(Statement::StringLiteral(unescaped));
        }

        // Check for conditional
        if token == "if" {
            return self.parse_if(tok_line, tok_column);
        }

        // Check for quotation
        if token == "[" {
            return self.parse_quotation(tok_line, tok_column);
        }

        // Check for match expression
        if token == "match" {
            return self.parse_match(tok_line, tok_column);
        }

        // Otherwise it's a word call - preserve source span for precise diagnostics
        Ok(Statement::WordCall {
            name: token.to_string(),
            span: Some(Span::new(tok_line, tok_column, tok_len)),
        })
    }

    fn parse_if(&mut self, start_line: usize, start_column: usize) -> Result<Statement, String> {
        let mut then_branch = Vec::new();

        // Parse then branch until 'else' or 'then'
        loop {
            if self.is_at_end() {
                return Err("Unexpected end of file in 'if' statement".to_string());
            }

            // Skip comments and newlines
            self.skip_comments();

            if self.check("else") {
                self.advance();
                // Parse else branch
                break;
            }

            if self.check("then") {
                self.advance();
                // End of if without else
                return Ok(Statement::If {
                    then_branch,
                    else_branch: None,
                    span: Some(Span::new(start_line, start_column, "if".len())),
                });
            }

            then_branch.push(self.parse_statement()?);
        }

        // Parse else branch until 'then'
        let mut else_branch = Vec::new();
        loop {
            if self.is_at_end() {
                return Err("Unexpected end of file in 'else' branch".to_string());
            }

            // Skip comments and newlines
            self.skip_comments();

            if self.check("then") {
                self.advance();
                return Ok(Statement::If {
                    then_branch,
                    else_branch: Some(else_branch),
                    span: Some(Span::new(start_line, start_column, "if".len())),
                });
            }

            else_branch.push(self.parse_statement()?);
        }
    }

    fn parse_quotation(
        &mut self,
        start_line: usize,
        start_column: usize,
    ) -> Result<Statement, String> {
        use crate::ast::QuotationSpan;
        let mut body = Vec::new();

        // Parse statements until ']'
        loop {
            if self.is_at_end() {
                return Err("Unexpected end of file in quotation".to_string());
            }

            // Skip comments and newlines
            self.skip_comments();

            if self.check("]") {
                let end_tok = self.advance_token().unwrap();
                let end_line = end_tok.line;
                let end_column = end_tok.column + 1; // exclusive
                let id = self.next_quotation_id;
                self.next_quotation_id += 1;
                // Span from '[' to ']' inclusive
                let span = QuotationSpan::new(start_line, start_column, end_line, end_column);
                return Ok(Statement::Quotation {
                    id,
                    body,
                    span: Some(span),
                });
            }

            body.push(self.parse_statement()?);
        }
    }

    /// Parse a match expression:
    ///   match
    ///     Get -> send-response
    ///     Increment -> do-increment send-response
    ///     Report -> aggregate-add
    ///   end
    fn parse_match(&mut self, start_line: usize, start_column: usize) -> Result<Statement, String> {
        let mut arms = Vec::new();

        loop {
            self.skip_comments();

            // Check for 'end' to terminate match
            if self.check("end") {
                self.advance();
                break;
            }

            if self.is_at_end() {
                return Err("Unexpected end of file in match expression".to_string());
            }

            arms.push(self.parse_match_arm()?);
        }

        if arms.is_empty() {
            return Err("Match expression must have at least one arm".to_string());
        }

        Ok(Statement::Match {
            arms,
            span: Some(Span::new(start_line, start_column, "match".len())),
        })
    }

    /// Parse a single match arm:
    ///   Get -> send-response
    ///   or with bindings:
    ///   Get { chan } -> chan send-response
    fn parse_match_arm(&mut self) -> Result<MatchArm, String> {
        // Get variant name with position info
        let variant_token = self
            .advance_token()
            .ok_or("Expected variant name in match arm")?;
        let variant_name = variant_token.text.clone();
        let arm_line = variant_token.line;
        let arm_column = variant_token.column;
        let arm_length = variant_name.len();

        self.skip_comments();

        // Check for optional bindings: { field1 field2 }
        let pattern = if self.check("{") {
            self.consume("{");
            let mut bindings = Vec::new();

            loop {
                self.skip_comments();

                if self.check("}") {
                    break;
                }

                if self.is_at_end() {
                    return Err(format!(
                        "Unexpected end of file in match arm bindings for '{}'",
                        variant_name
                    ));
                }

                let token = self.advance().ok_or("Expected binding name")?.clone();

                // Require > prefix to make clear these are stack extractions, not variables
                if let Some(field_name) = token.strip_prefix('>') {
                    if field_name.is_empty() {
                        return Err(format!(
                            "Expected field name after '>' in match bindings for '{}'",
                            variant_name
                        ));
                    }
                    bindings.push(field_name.to_string());
                } else {
                    return Err(format!(
                        "Match bindings must use '>' prefix to indicate stack extraction. \
                         Use '>{}' instead of '{}' in pattern for '{}'",
                        token, token, variant_name
                    ));
                }
            }

            self.consume("}");
            Pattern::VariantWithBindings {
                name: variant_name,
                bindings,
            }
        } else {
            Pattern::Variant(variant_name.clone())
        };

        self.skip_comments();

        // Expect '->' arrow
        if !self.consume("->") {
            return Err(format!(
                "Expected '->' after pattern '{}', got '{}'",
                match &pattern {
                    Pattern::Variant(n) => n.clone(),
                    Pattern::VariantWithBindings { name, .. } => name.clone(),
                },
                self.current()
            ));
        }

        // Parse body until next pattern or 'end'
        let mut body = Vec::new();
        loop {
            self.skip_comments();

            // Check for end of arm (next pattern starts with uppercase, or 'end')
            if self.check("end") {
                break;
            }

            // Check if next token looks like a match pattern (not just any uppercase word).
            // A pattern is: UppercaseName followed by '->' or '{'
            // This prevents confusing 'Make-Get' (constructor call) with a pattern.
            if let Some(token) = self.current_token()
                && let Some(first_char) = token.text.chars().next()
                && first_char.is_uppercase()
            {
                // Peek at next token to see if this is a pattern (followed by -> or {)
                if let Some(next) = self.peek_at(1)
                    && (next == "->" || next == "{")
                {
                    // This is the next pattern
                    break;
                }
                // Otherwise it's just an uppercase word call (like Make-Get), continue parsing body
            }

            if self.is_at_end() {
                return Err("Unexpected end of file in match arm body".to_string());
            }

            body.push(self.parse_statement()?);
        }

        Ok(MatchArm {
            pattern,
            body,
            span: Some(Span::new(arm_line, arm_column, arm_length)),
        })
    }

    /// Parse a stack effect declaration: ( ..a Int -- ..a Bool )
    /// With optional computational effects: ( ..a Int -- ..a Bool | Yield Int )
    fn parse_stack_effect(&mut self) -> Result<Effect, String> {
        // Consume '('
        if !self.consume("(") {
            return Err("Expected '(' to start stack effect".to_string());
        }

        // Parse input stack types (until '--' or ')')
        let (input_row_var, input_types) =
            self.parse_type_list_until(&["--", ")"], "stack effect inputs", 0)?;

        // Consume '--'
        if !self.consume("--") {
            return Err("Expected '--' separator in stack effect".to_string());
        }

        // Parse output stack types (until ')' or '|')
        let (output_row_var, output_types) =
            self.parse_type_list_until(&[")", "|"], "stack effect outputs", 0)?;

        // Parse optional computational effects after '|'
        let effects = if self.consume("|") {
            self.parse_effect_annotations()?
        } else {
            Vec::new()
        };

        // Consume ')'
        if !self.consume(")") {
            return Err("Expected ')' to end stack effect".to_string());
        }

        // Build input and output StackTypes
        let inputs = self.build_stack_type(input_row_var, input_types);
        let outputs = self.build_stack_type(output_row_var, output_types);

        Ok(Effect::with_effects(inputs, outputs, effects))
    }

    /// Parse computational effect annotations after '|'
    /// Example: | Yield Int
    fn parse_effect_annotations(&mut self) -> Result<Vec<SideEffect>, String> {
        let mut effects = Vec::new();

        // Parse effects until we hit ')'
        while let Some(token) = self.peek_at(0) {
            if token == ")" {
                break;
            }

            match token {
                "Yield" => {
                    self.advance(); // consume "Yield"
                    // Parse the yield type
                    if let Some(type_token) = self.current_token() {
                        if type_token.text == ")" {
                            return Err("Expected type after 'Yield'".to_string());
                        }
                        let type_token = type_token.clone();
                        self.advance();
                        let yield_type = self.parse_type(&type_token)?;
                        effects.push(SideEffect::Yield(Box::new(yield_type)));
                    } else {
                        return Err("Expected type after 'Yield'".to_string());
                    }
                }
                _ => {
                    return Err(format!("Unknown effect '{}'. Expected 'Yield'", token));
                }
            }
        }

        if effects.is_empty() {
            return Err("Expected at least one effect after '|'".to_string());
        }

        Ok(effects)
    }

    /// Parse a single type token into a Type
    fn parse_type(&self, token: &Token) -> Result<Type, String> {
        match token.text.as_str() {
            "Int" => Ok(Type::Int),
            "Float" => Ok(Type::Float),
            "Bool" => Ok(Type::Bool),
            "String" => Ok(Type::String),
            // Reject 'Quotation' - it looks like a type but would be silently treated as a type variable.
            // Users must use explicit effect syntax like [Int -- Int] instead.
            "Quotation" => Err(format!(
                "'Quotation' is not a valid type at line {}, column {}. Use explicit quotation syntax like [Int -- Int] or [ -- ] instead.",
                token.line + 1,
                token.column + 1
            )),
            _ => {
                // Check if it's a type variable (starts with uppercase)
                if let Some(first_char) = token.text.chars().next() {
                    if first_char.is_uppercase() {
                        Ok(Type::Var(token.text.to_string()))
                    } else {
                        Err(format!(
                            "Unknown type: '{}' at line {}, column {}. Expected Int, Bool, String, Closure, or a type variable (uppercase)",
                            token.text.escape_default(),
                            token.line + 1, // 1-indexed for user display
                            token.column + 1
                        ))
                    }
                } else {
                    Err(format!(
                        "Invalid type: '{}' at line {}, column {}",
                        token.text.escape_default(),
                        token.line + 1,
                        token.column + 1
                    ))
                }
            }
        }
    }

    /// Validate row variable name
    /// Row variables must start with a lowercase letter and contain only alphanumeric characters
    fn validate_row_var_name(&self, name: &str) -> Result<(), String> {
        if name.is_empty() {
            return Err("Row variable must have a name after '..'".to_string());
        }

        // Must start with lowercase letter
        let first_char = name.chars().next().unwrap();
        if !first_char.is_ascii_lowercase() {
            return Err(format!(
                "Row variable '..{}' must start with a lowercase letter (a-z)",
                name
            ));
        }

        // Rest must be alphanumeric or underscore
        for ch in name.chars() {
            if !ch.is_alphanumeric() && ch != '_' {
                return Err(format!(
                    "Row variable '..{}' can only contain letters, numbers, and underscores",
                    name
                ));
            }
        }

        // Check for reserved keywords (type names that might confuse users)
        match name {
            "Int" | "Bool" | "String" => {
                return Err(format!(
                    "Row variable '..{}' cannot use type name as identifier",
                    name
                ));
            }
            _ => {}
        }

        Ok(())
    }

    /// Parse a list of types until one of the given terminators is reached
    /// Returns (optional row variable, list of types)
    /// Used by both parse_stack_effect and parse_quotation_type
    ///
    /// depth: Current nesting depth for quotation types (0 at top level)
    fn parse_type_list_until(
        &mut self,
        terminators: &[&str],
        context: &str,
        depth: usize,
    ) -> Result<(Option<String>, Vec<Type>), String> {
        const MAX_QUOTATION_DEPTH: usize = 32;

        if depth > MAX_QUOTATION_DEPTH {
            return Err(format!(
                "Quotation type nesting exceeds maximum depth of {} (possible deeply nested types or DOS attack)",
                MAX_QUOTATION_DEPTH
            ));
        }

        let mut types = Vec::new();
        let mut row_var = None;

        while !terminators.iter().any(|t| self.check(t)) {
            // Skip comments and blank lines within type lists
            self.skip_comments();

            // Re-check terminators after skipping comments
            if terminators.iter().any(|t| self.check(t)) {
                break;
            }

            if self.is_at_end() {
                return Err(format!(
                    "Unexpected end while parsing {} - expected one of: {}",
                    context,
                    terminators.join(", ")
                ));
            }

            let token = self
                .advance_token()
                .ok_or_else(|| format!("Unexpected end in {}", context))?
                .clone();

            // Check for row variable: ..name
            if token.text.starts_with("..") {
                let var_name = token.text.trim_start_matches("..").to_string();
                self.validate_row_var_name(&var_name)?;
                row_var = Some(var_name);
            } else if token.text == "Closure" {
                // Closure type: Closure[effect]
                if !self.consume("[") {
                    return Err("Expected '[' after 'Closure' in type signature".to_string());
                }
                let effect_type = self.parse_quotation_type(depth)?;
                match effect_type {
                    Type::Quotation(effect) => {
                        types.push(Type::Closure {
                            effect,
                            captures: Vec::new(), // Filled in by type checker
                        });
                    }
                    _ => unreachable!("parse_quotation_type should return Quotation"),
                }
            } else if token.text == "[" {
                // Nested quotation type
                types.push(self.parse_quotation_type(depth)?);
            } else {
                // Parse as concrete type
                types.push(self.parse_type(&token)?);
            }
        }

        Ok((row_var, types))
    }

    /// Parse a quotation type: [inputs -- outputs]
    /// Note: The opening '[' has already been consumed
    ///
    /// depth: Current nesting depth (incremented for each nested quotation)
    fn parse_quotation_type(&mut self, depth: usize) -> Result<Type, String> {
        // Parse input stack types (until '--' or ']')
        let (input_row_var, input_types) =
            self.parse_type_list_until(&["--", "]"], "quotation type inputs", depth + 1)?;

        // Require '--' separator for clarity
        if !self.consume("--") {
            // Check if user closed with ] without separator
            if self.check("]") {
                return Err(
                    "Quotation types require '--' separator. Did you mean '[Int -- ]' or '[ -- Int]'?"
                        .to_string(),
                );
            }
            return Err("Expected '--' separator in quotation type".to_string());
        }

        // Parse output stack types (until ']')
        let (output_row_var, output_types) =
            self.parse_type_list_until(&["]"], "quotation type outputs", depth + 1)?;

        // Consume ']'
        if !self.consume("]") {
            return Err("Expected ']' to end quotation type".to_string());
        }

        // Build input and output StackTypes
        let inputs = self.build_stack_type(input_row_var, input_types);
        let outputs = self.build_stack_type(output_row_var, output_types);

        Ok(Type::Quotation(Box::new(Effect::new(inputs, outputs))))
    }

    /// Build a StackType from an optional row variable and a list of types
    /// Example: row_var="a", types=[Int, Bool] => RowVar("a") with Int on top of Bool
    ///
    /// IMPORTANT: ALL stack effects are implicitly row-polymorphic in concatenative languages.
    /// This means:
    ///   ( -- )        becomes  ( ..rest -- ..rest )       - no-op, preserves stack
    ///   ( -- Int )    becomes  ( ..rest -- ..rest Int )   - pushes Int
    ///   ( Int -- )    becomes  ( ..rest Int -- ..rest )   - consumes Int
    ///   ( Int -- Int) becomes  ( ..rest Int -- ..rest Int ) - transforms top
    fn build_stack_type(&self, row_var: Option<String>, types: Vec<Type>) -> StackType {
        // Always use row polymorphism - this is fundamental to concatenative semantics
        let base = match row_var {
            Some(name) => StackType::RowVar(name),
            None => StackType::RowVar("rest".to_string()),
        };

        // Push types onto the stack (bottom to top order)
        types.into_iter().fold(base, |stack, ty| stack.push(ty))
    }

    fn skip_comments(&mut self) {
        loop {
            // Check for comment: either standalone "#" or token starting with "#"
            // The latter handles shebangs like "#!/usr/bin/env seqc"
            let is_comment = if self.is_at_end() {
                false
            } else {
                let tok = self.current();
                tok == "#" || tok.starts_with("#!")
            };

            if is_comment {
                self.advance(); // consume # or shebang token

                // Collect all tokens until newline to reconstruct the comment text
                let mut comment_parts: Vec<String> = Vec::new();
                while !self.is_at_end() && self.current() != "\n" {
                    comment_parts.push(self.current().to_string());
                    self.advance();
                }
                if !self.is_at_end() {
                    self.advance(); // skip newline
                }

                // Join parts and check for seq:allow annotation
                // Format: # seq:allow(lint-id) -> parts = ["seq", ":", "allow", "(", "lint-id", ")"]
                let comment = comment_parts.join("");
                if let Some(lint_id) = comment
                    .strip_prefix("seq:allow(")
                    .and_then(|s| s.strip_suffix(")"))
                {
                    self.pending_allowed_lints.push(lint_id.to_string());
                }
            } else if self.check("\n") {
                // Skip blank lines
                self.advance();
            } else {
                break;
            }
        }
    }

    fn check(&self, expected: &str) -> bool {
        if self.is_at_end() {
            return false;
        }
        self.current() == expected
    }

    fn consume(&mut self, expected: &str) -> bool {
        if self.check(expected) {
            self.advance();
            true
        } else {
            false
        }
    }

    /// Get the text of the current token
    fn current(&self) -> &str {
        if self.is_at_end() {
            ""
        } else {
            &self.tokens[self.pos].text
        }
    }

    /// Get the full current token with position info
    fn current_token(&self) -> Option<&Token> {
        if self.is_at_end() {
            None
        } else {
            Some(&self.tokens[self.pos])
        }
    }

    /// Peek at a token N positions ahead without consuming
    fn peek_at(&self, n: usize) -> Option<&str> {
        let idx = self.pos + n;
        if idx < self.tokens.len() {
            Some(&self.tokens[idx].text)
        } else {
            None
        }
    }

    /// Advance and return the token text (for compatibility with existing code)
    fn advance(&mut self) -> Option<&String> {
        if self.is_at_end() {
            None
        } else {
            let token = &self.tokens[self.pos];
            self.pos += 1;
            Some(&token.text)
        }
    }

    /// Advance and return the full token with position info
    fn advance_token(&mut self) -> Option<&Token> {
        if self.is_at_end() {
            None
        } else {
            let token = &self.tokens[self.pos];
            self.pos += 1;
            Some(token)
        }
    }

    fn is_at_end(&self) -> bool {
        self.pos >= self.tokens.len()
    }
}

/// Check if a token looks like a float literal
///
/// Float literals contain either:
/// - A decimal point: `3.14`, `.5`, `5.`
/// - Scientific notation: `1e10`, `1E-5`, `1.5e3`
///
/// This check must happen BEFORE integer parsing to avoid
/// parsing "5" in "5.0" as an integer.
fn is_float_literal(token: &str) -> bool {
    // Skip leading minus sign for negative numbers
    let s = token.strip_prefix('-').unwrap_or(token);

    // Must have at least one digit
    if s.is_empty() {
        return false;
    }

    // Check for decimal point or scientific notation
    s.contains('.') || s.contains('e') || s.contains('E')
}

/// Process escape sequences in a string literal
///
/// Supported escape sequences:
/// - `\"` -> `"`  (quote)
/// - `\\` -> `\`  (backslash)
/// - `\n` -> newline
/// - `\r` -> carriage return
/// - `\t` -> tab
/// - `\xNN` -> Unicode code point U+00NN (hex value 00-FF)
///
/// # Note on `\xNN` encoding
///
/// The `\xNN` escape creates a Unicode code point U+00NN, not a raw byte.
/// For values 0x00-0x7F (ASCII), this maps directly to the byte value.
/// For values 0x80-0xFF (Latin-1 Supplement), the character is stored as
/// a multi-byte UTF-8 sequence. For example:
/// - `\x41` -> 'A' (1 byte in UTF-8)
/// - `\x1b` -> ESC (1 byte in UTF-8, used for ANSI terminal codes)
/// - `\xFF` -> 'ÿ' (U+00FF, 2 bytes in UTF-8: 0xC3 0xBF)
///
/// This matches Python 3 and Rust string behavior. For terminal ANSI codes,
/// which are the primary use case, all values are in the ASCII range.
///
/// # Errors
/// Returns error if an unknown escape sequence is encountered
fn unescape_string(s: &str) -> Result<String, String> {
    let mut result = String::new();
    let mut chars = s.chars();

    while let Some(ch) = chars.next() {
        if ch == '\\' {
            match chars.next() {
                Some('"') => result.push('"'),
                Some('\\') => result.push('\\'),
                Some('n') => result.push('\n'),
                Some('r') => result.push('\r'),
                Some('t') => result.push('\t'),
                Some('x') => {
                    // Hex escape: \xNN
                    let hex1 = chars.next().ok_or_else(|| {
                        "Incomplete hex escape sequence '\\x' - expected 2 hex digits".to_string()
                    })?;
                    let hex2 = chars.next().ok_or_else(|| {
                        format!(
                            "Incomplete hex escape sequence '\\x{}' - expected 2 hex digits",
                            hex1
                        )
                    })?;

                    let hex_str: String = [hex1, hex2].iter().collect();
                    let byte_val = u8::from_str_radix(&hex_str, 16).map_err(|_| {
                        format!(
                            "Invalid hex escape sequence '\\x{}' - expected 2 hex digits (00-FF)",
                            hex_str
                        )
                    })?;

                    result.push(byte_val as char);
                }
                Some(c) => {
                    return Err(format!(
                        "Unknown escape sequence '\\{}' in string literal. \
                         Supported: \\\" \\\\ \\n \\r \\t \\xNN",
                        c
                    ));
                }
                None => {
                    return Err("String ends with incomplete escape sequence '\\'".to_string());
                }
            }
        } else {
            result.push(ch);
        }
    }

    Ok(result)
}

fn tokenize(source: &str) -> Vec<Token> {
    let mut tokens = Vec::new();
    let mut current = String::new();
    let mut current_start_line = 0;
    let mut current_start_col = 0;
    let mut in_string = false;
    let mut prev_was_backslash = false;

    // Track current position (0-indexed)
    let mut line = 0;
    let mut col = 0;

    for ch in source.chars() {
        if in_string {
            current.push(ch);
            if ch == '"' && !prev_was_backslash {
                // Unescaped quote ends the string
                in_string = false;
                tokens.push(Token::new(
                    current.clone(),
                    current_start_line,
                    current_start_col,
                ));
                current.clear();
                prev_was_backslash = false;
            } else if ch == '\\' && !prev_was_backslash {
                // Start of escape sequence
                prev_was_backslash = true;
            } else {
                // Regular character or escaped character
                prev_was_backslash = false;
            }
            // Track newlines inside strings
            if ch == '\n' {
                line += 1;
                col = 0;
            } else {
                col += 1;
            }
        } else if ch == '"' {
            if !current.is_empty() {
                tokens.push(Token::new(
                    current.clone(),
                    current_start_line,
                    current_start_col,
                ));
                current.clear();
            }
            in_string = true;
            current_start_line = line;
            current_start_col = col;
            current.push(ch);
            prev_was_backslash = false;
            col += 1;
        } else if ch.is_whitespace() {
            if !current.is_empty() {
                tokens.push(Token::new(
                    current.clone(),
                    current_start_line,
                    current_start_col,
                ));
                current.clear();
            }
            // Preserve newlines for comment handling
            if ch == '\n' {
                tokens.push(Token::new("\n".to_string(), line, col));
                line += 1;
                col = 0;
            } else {
                col += 1;
            }
        } else if "():;[]{},".contains(ch) {
            if !current.is_empty() {
                tokens.push(Token::new(
                    current.clone(),
                    current_start_line,
                    current_start_col,
                ));
                current.clear();
            }
            tokens.push(Token::new(ch.to_string(), line, col));
            col += 1;
        } else {
            if current.is_empty() {
                current_start_line = line;
                current_start_col = col;
            }
            current.push(ch);
            col += 1;
        }
    }

    // Check for unclosed string literal
    if in_string {
        // Return error by adding a special error token
        // The parser will handle this as a parse error
        tokens.push(Token::new(
            "<<<UNCLOSED_STRING>>>".to_string(),
            current_start_line,
            current_start_col,
        ));
    } else if !current.is_empty() {
        tokens.push(Token::new(current, current_start_line, current_start_col));
    }

    tokens
}

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

    #[test]
    fn test_parse_hello_world() {
        let source = r#"
: main ( -- )
  "Hello, World!" write_line ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert_eq!(program.words[0].name, "main");
        assert_eq!(program.words[0].body.len(), 2);

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => assert_eq!(s, "Hello, World!"),
            _ => panic!("Expected StringLiteral"),
        }

        match &program.words[0].body[1] {
            Statement::WordCall { name, .. } => assert_eq!(name, "write_line"),
            _ => panic!("Expected WordCall"),
        }
    }

    #[test]
    fn test_parse_with_numbers() {
        let source = ": add-example ( -- ) 2 3 add ;";

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words[0].body.len(), 3);
        assert_eq!(program.words[0].body[0], Statement::IntLiteral(2));
        assert_eq!(program.words[0].body[1], Statement::IntLiteral(3));
        assert!(matches!(
            &program.words[0].body[2],
            Statement::WordCall { name, .. } if name == "add"
        ));
    }

    #[test]
    fn test_parse_hex_literals() {
        let source = ": test ( -- ) 0xFF 0x10 0X1A ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words[0].body[0], Statement::IntLiteral(255));
        assert_eq!(program.words[0].body[1], Statement::IntLiteral(16));
        assert_eq!(program.words[0].body[2], Statement::IntLiteral(26));
    }

    #[test]
    fn test_parse_binary_literals() {
        let source = ": test ( -- ) 0b1010 0B1111 0b0 ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words[0].body[0], Statement::IntLiteral(10));
        assert_eq!(program.words[0].body[1], Statement::IntLiteral(15));
        assert_eq!(program.words[0].body[2], Statement::IntLiteral(0));
    }

    #[test]
    fn test_parse_invalid_hex_literal() {
        let source = ": test ( -- ) 0xGG ;";
        let mut parser = Parser::new(source);
        let err = parser.parse().unwrap_err();
        assert!(err.contains("Invalid hex literal"));
    }

    #[test]
    fn test_parse_invalid_binary_literal() {
        let source = ": test ( -- ) 0b123 ;";
        let mut parser = Parser::new(source);
        let err = parser.parse().unwrap_err();
        assert!(err.contains("Invalid binary literal"));
    }

    #[test]
    fn test_parse_escaped_quotes() {
        let source = r#": main ( -- ) "Say \"hello\" there" write_line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert_eq!(program.words[0].body.len(), 2);

        match &program.words[0].body[0] {
            // Escape sequences should be processed: \" becomes actual quote
            Statement::StringLiteral(s) => assert_eq!(s, "Say \"hello\" there"),
            _ => panic!("Expected StringLiteral with escaped quotes"),
        }
    }

    /// Regression test for issue #117: escaped quote at end of string
    /// Previously failed with "String ends with incomplete escape sequence"
    #[test]
    fn test_escaped_quote_at_end_of_string() {
        let source = r#": main ( -- ) "hello\"" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => assert_eq!(s, "hello\""),
            _ => panic!("Expected StringLiteral ending with escaped quote"),
        }
    }

    /// Test escaped quote at start of string (boundary case)
    #[test]
    fn test_escaped_quote_at_start_of_string() {
        let source = r#": main ( -- ) "\"hello" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => assert_eq!(s, "\"hello"),
            _ => panic!("Expected StringLiteral starting with escaped quote"),
        }
    }

    #[test]
    fn test_escape_sequences() {
        let source = r#": main ( -- ) "Line 1\nLine 2\tTabbed" write_line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => assert_eq!(s, "Line 1\nLine 2\tTabbed"),
            _ => panic!("Expected StringLiteral"),
        }
    }

    #[test]
    fn test_unknown_escape_sequence() {
        let source = r#": main ( -- ) "Bad \q sequence" write_line ;"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Unknown escape sequence"));
    }

    #[test]
    fn test_hex_escape_sequence() {
        // \x1b is ESC (27), \x41 is 'A' (65)
        let source = r#": main ( -- ) "\x1b[2K\x41" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => {
                assert_eq!(s.len(), 5); // ESC [ 2 K A
                assert_eq!(s.as_bytes()[0], 0x1b); // ESC
                assert_eq!(s.as_bytes()[4], 0x41); // 'A'
            }
            _ => panic!("Expected StringLiteral"),
        }
    }

    #[test]
    fn test_hex_escape_null_byte() {
        let source = r#": main ( -- ) "before\x00after" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => {
                assert_eq!(s.len(), 12); // "before" + NUL + "after"
                assert_eq!(s.as_bytes()[6], 0x00);
            }
            _ => panic!("Expected StringLiteral"),
        }
    }

    #[test]
    fn test_hex_escape_uppercase() {
        // Both uppercase and lowercase hex digits should work
        // Note: Values > 0x7F become Unicode code points (U+00NN), multi-byte in UTF-8
        let source = r#": main ( -- ) "\x41\x42\x4F" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => {
                assert_eq!(s, "ABO"); // 0x41='A', 0x42='B', 0x4F='O'
            }
            _ => panic!("Expected StringLiteral"),
        }
    }

    #[test]
    fn test_hex_escape_high_bytes() {
        // Values > 0x7F become Unicode code points (Latin-1), which are multi-byte in UTF-8
        let source = r#": main ( -- ) "\xFF" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::StringLiteral(s) => {
                // \xFF becomes U+00FF (ÿ), which is 2 bytes in UTF-8: C3 BF
                assert_eq!(s, "\u{00FF}");
                assert_eq!(s.chars().next().unwrap(), 'ÿ');
            }
            _ => panic!("Expected StringLiteral"),
        }
    }

    #[test]
    fn test_hex_escape_incomplete() {
        // \x with only one hex digit
        let source = r#": main ( -- ) "\x1" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Incomplete hex escape"));
    }

    #[test]
    fn test_hex_escape_invalid_digits() {
        // \xGG is not valid hex
        let source = r#": main ( -- ) "\xGG" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid hex escape"));
    }

    #[test]
    fn test_hex_escape_at_end_of_string() {
        // \x at end of string with no digits
        let source = r#": main ( -- ) "test\x" io.write-line ;"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Incomplete hex escape"));
    }

    #[test]
    fn test_unclosed_string_literal() {
        let source = r#": main ( -- ) "unclosed string ;"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(err_msg.contains("Unclosed string literal"));
        // Should include position information (line 1, column 15 for the opening quote)
        assert!(
            err_msg.contains("line 1"),
            "Expected line number in error: {}",
            err_msg
        );
        assert!(
            err_msg.contains("column 15"),
            "Expected column number in error: {}",
            err_msg
        );
    }

    #[test]
    fn test_multiple_word_definitions() {
        let source = r#"
: double ( Int -- Int )
  2 multiply ;

: quadruple ( Int -- Int )
  double double ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 2);
        assert_eq!(program.words[0].name, "double");
        assert_eq!(program.words[1].name, "quadruple");

        // Verify stack effects were parsed
        assert!(program.words[0].effect.is_some());
        assert!(program.words[1].effect.is_some());
    }

    #[test]
    fn test_user_word_calling_user_word() {
        let source = r#"
: helper ( -- )
  "helper called" write_line ;

: main ( -- )
  helper ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 2);

        // Check main calls helper
        match &program.words[1].body[0] {
            Statement::WordCall { name, .. } => assert_eq!(name, "helper"),
            _ => panic!("Expected WordCall to helper"),
        }
    }

    #[test]
    fn test_parse_simple_stack_effect() {
        // Test: ( Int -- Bool )
        // With implicit row polymorphism, this becomes: ( ..rest Int -- ..rest Bool )
        let source = ": test ( Int -- Bool ) 1 ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        let word = &program.words[0];
        assert!(word.effect.is_some());

        let effect = word.effect.as_ref().unwrap();

        // Input: Int on RowVar("rest") (implicit row polymorphism)
        assert_eq!(
            effect.inputs,
            StackType::Cons {
                rest: Box::new(StackType::RowVar("rest".to_string())),
                top: Type::Int
            }
        );

        // Output: Bool on RowVar("rest") (implicit row polymorphism)
        assert_eq!(
            effect.outputs,
            StackType::Cons {
                rest: Box::new(StackType::RowVar("rest".to_string())),
                top: Type::Bool
            }
        );
    }

    #[test]
    fn test_parse_row_polymorphic_stack_effect() {
        // Test: ( ..a Int -- ..a Bool )
        let source = ": test ( ..a Int -- ..a Bool ) 1 ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        let word = &program.words[0];
        assert!(word.effect.is_some());

        let effect = word.effect.as_ref().unwrap();

        // Input: Int on RowVar("a")
        assert_eq!(
            effect.inputs,
            StackType::Cons {
                rest: Box::new(StackType::RowVar("a".to_string())),
                top: Type::Int
            }
        );

        // Output: Bool on RowVar("a")
        assert_eq!(
            effect.outputs,
            StackType::Cons {
                rest: Box::new(StackType::RowVar("a".to_string())),
                top: Type::Bool
            }
        );
    }

    #[test]
    fn test_parse_invalid_row_var_starts_with_digit() {
        // Test: Row variable cannot start with digit
        let source = ": test ( ..123 Int -- ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("lowercase letter"),
            "Expected error about lowercase letter, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_invalid_row_var_starts_with_uppercase() {
        // Test: Row variable cannot start with uppercase (that's a type variable)
        let source = ": test ( ..Int Int -- ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("lowercase letter") || err_msg.contains("type name"),
            "Expected error about lowercase letter or type name, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_invalid_row_var_with_special_chars() {
        // Test: Row variable cannot contain special characters
        let source = ": test ( ..a-b Int -- ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("letters, numbers, and underscores")
                || err_msg.contains("Unknown type"),
            "Expected error about valid characters, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_valid_row_var_with_underscore() {
        // Test: Row variable CAN contain underscore
        let source = ": test ( ..my_row Int -- ..my_row Bool ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_ok(), "Should accept row variable with underscore");
    }

    #[test]
    fn test_parse_multiple_types_stack_effect() {
        // Test: ( Int String -- Bool )
        // With implicit row polymorphism: ( ..rest Int String -- ..rest Bool )
        let source = ": test ( Int String -- Bool ) 1 ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Input: String on Int on RowVar("rest")
        let (rest, top) = effect.inputs.clone().pop().unwrap();
        assert_eq!(top, Type::String);
        let (rest2, top2) = rest.pop().unwrap();
        assert_eq!(top2, Type::Int);
        assert_eq!(rest2, StackType::RowVar("rest".to_string()));

        // Output: Bool on RowVar("rest") (implicit row polymorphism)
        assert_eq!(
            effect.outputs,
            StackType::Cons {
                rest: Box::new(StackType::RowVar("rest".to_string())),
                top: Type::Bool
            }
        );
    }

    #[test]
    fn test_parse_type_variable() {
        // Test: ( ..a T -- ..a T T ) for dup
        let source = ": dup ( ..a T -- ..a T T ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Input: T on RowVar("a")
        assert_eq!(
            effect.inputs,
            StackType::Cons {
                rest: Box::new(StackType::RowVar("a".to_string())),
                top: Type::Var("T".to_string())
            }
        );

        // Output: T on T on RowVar("a")
        let (rest, top) = effect.outputs.clone().pop().unwrap();
        assert_eq!(top, Type::Var("T".to_string()));
        let (rest2, top2) = rest.pop().unwrap();
        assert_eq!(top2, Type::Var("T".to_string()));
        assert_eq!(rest2, StackType::RowVar("a".to_string()));
    }

    #[test]
    fn test_parse_empty_stack_effect() {
        // Test: ( -- )
        // In concatenative languages, even empty effects are row-polymorphic
        // ( -- ) means ( ..rest -- ..rest ) - preserves stack
        let source = ": test ( -- ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Both inputs and outputs should use the same implicit row variable
        assert_eq!(effect.inputs, StackType::RowVar("rest".to_string()));
        assert_eq!(effect.outputs, StackType::RowVar("rest".to_string()));
    }

    #[test]
    fn test_parse_invalid_type() {
        // Test invalid type (lowercase, not a row var)
        let source = ": test ( invalid -- Bool ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Unknown type"));
    }

    #[test]
    fn test_parse_unclosed_stack_effect() {
        // Test unclosed stack effect - parser tries to parse all tokens until ')' or EOF
        // In this case, it encounters "body" which is an invalid type
        let source = ": test ( Int -- Bool body ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        // Parser will try to parse "body" as a type and fail
        assert!(err_msg.contains("Unknown type"));
    }

    #[test]
    fn test_parse_simple_quotation_type() {
        // Test: ( [Int -- Int] -- )
        let source = ": apply ( [Int -- Int] -- ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Input should be: Quotation(Int -- Int) on RowVar("rest")
        let (rest, top) = effect.inputs.clone().pop().unwrap();
        match top {
            Type::Quotation(quot_effect) => {
                // Check quotation's input: Int on RowVar("rest")
                assert_eq!(
                    quot_effect.inputs,
                    StackType::Cons {
                        rest: Box::new(StackType::RowVar("rest".to_string())),
                        top: Type::Int
                    }
                );
                // Check quotation's output: Int on RowVar("rest")
                assert_eq!(
                    quot_effect.outputs,
                    StackType::Cons {
                        rest: Box::new(StackType::RowVar("rest".to_string())),
                        top: Type::Int
                    }
                );
            }
            _ => panic!("Expected Quotation type, got {:?}", top),
        }
        assert_eq!(rest, StackType::RowVar("rest".to_string()));
    }

    #[test]
    fn test_parse_quotation_type_with_row_vars() {
        // Test: ( ..a [..a T -- ..a Bool] -- ..a )
        let source = ": test ( ..a [..a T -- ..a Bool] -- ..a ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Input: Quotation on RowVar("a")
        let (rest, top) = effect.inputs.clone().pop().unwrap();
        match top {
            Type::Quotation(quot_effect) => {
                // Check quotation's input: T on RowVar("a")
                let (q_in_rest, q_in_top) = quot_effect.inputs.clone().pop().unwrap();
                assert_eq!(q_in_top, Type::Var("T".to_string()));
                assert_eq!(q_in_rest, StackType::RowVar("a".to_string()));

                // Check quotation's output: Bool on RowVar("a")
                let (q_out_rest, q_out_top) = quot_effect.outputs.clone().pop().unwrap();
                assert_eq!(q_out_top, Type::Bool);
                assert_eq!(q_out_rest, StackType::RowVar("a".to_string()));
            }
            _ => panic!("Expected Quotation type, got {:?}", top),
        }
        assert_eq!(rest, StackType::RowVar("a".to_string()));
    }

    #[test]
    fn test_parse_nested_quotation_type() {
        // Test: ( [[Int -- Int] -- Bool] -- )
        let source = ": nested ( [[Int -- Int] -- Bool] -- ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Input: Quotation([Int -- Int] -- Bool) on RowVar("rest")
        let (_, top) = effect.inputs.clone().pop().unwrap();
        match top {
            Type::Quotation(outer_effect) => {
                // Outer quotation input: [Int -- Int] on RowVar("rest")
                let (_, outer_in_top) = outer_effect.inputs.clone().pop().unwrap();
                match outer_in_top {
                    Type::Quotation(inner_effect) => {
                        // Inner quotation: Int -- Int
                        assert!(matches!(
                            inner_effect.inputs.clone().pop().unwrap().1,
                            Type::Int
                        ));
                        assert!(matches!(
                            inner_effect.outputs.clone().pop().unwrap().1,
                            Type::Int
                        ));
                    }
                    _ => panic!("Expected nested Quotation type"),
                }

                // Outer quotation output: Bool
                let (_, outer_out_top) = outer_effect.outputs.clone().pop().unwrap();
                assert_eq!(outer_out_top, Type::Bool);
            }
            _ => panic!("Expected Quotation type"),
        }
    }

    #[test]
    fn test_parse_deeply_nested_quotation_type_exceeds_limit() {
        // Test: Deeply nested quotation types should fail with max depth error
        // Build a quotation type nested 35 levels deep (exceeds MAX_QUOTATION_DEPTH = 32)
        let mut source = String::from(": deep ( ");

        // Build opening brackets: [[[[[[...
        for _ in 0..35 {
            source.push_str("[ -- ");
        }

        source.push_str("Int");

        // Build closing brackets: ...]]]]]]
        for _ in 0..35 {
            source.push_str(" ]");
        }

        source.push_str(" -- ) ;");

        let mut parser = Parser::new(&source);
        let result = parser.parse();

        // Should fail with depth limit error
        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("depth") || err_msg.contains("32"),
            "Expected depth limit error, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_empty_quotation_type() {
        // Test: ( [ -- ] -- )
        // An empty quotation type is also row-polymorphic: [ ..rest -- ..rest ]
        let source = ": empty-quot ( [ -- ] -- ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        let (_, top) = effect.inputs.clone().pop().unwrap();
        match top {
            Type::Quotation(quot_effect) => {
                // Empty quotation preserves the stack (row-polymorphic)
                assert_eq!(quot_effect.inputs, StackType::RowVar("rest".to_string()));
                assert_eq!(quot_effect.outputs, StackType::RowVar("rest".to_string()));
            }
            _ => panic!("Expected Quotation type"),
        }
    }

    #[test]
    fn test_parse_quotation_type_in_output() {
        // Test: ( -- [Int -- Int] )
        let source = ": maker ( -- [Int -- Int] ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Output should be: Quotation(Int -- Int) on RowVar("rest")
        let (_, top) = effect.outputs.clone().pop().unwrap();
        match top {
            Type::Quotation(quot_effect) => {
                assert!(matches!(
                    quot_effect.inputs.clone().pop().unwrap().1,
                    Type::Int
                ));
                assert!(matches!(
                    quot_effect.outputs.clone().pop().unwrap().1,
                    Type::Int
                ));
            }
            _ => panic!("Expected Quotation type"),
        }
    }

    #[test]
    fn test_parse_unclosed_quotation_type() {
        // Test: ( [Int -- Int -- )  (missing ])
        let source = ": broken ( [Int -- Int -- ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        // Parser might error with various messages depending on where it fails
        // It should at least indicate a parsing problem
        assert!(
            err_msg.contains("Unclosed")
                || err_msg.contains("Expected")
                || err_msg.contains("Unexpected"),
            "Got error: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_multiple_quotation_types() {
        // Test: ( [Int -- Int] [String -- Bool] -- )
        let source = ": multi ( [Int -- Int] [String -- Bool] -- ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Pop second quotation (String -- Bool)
        let (rest, top) = effect.inputs.clone().pop().unwrap();
        match top {
            Type::Quotation(quot_effect) => {
                assert!(matches!(
                    quot_effect.inputs.clone().pop().unwrap().1,
                    Type::String
                ));
                assert!(matches!(
                    quot_effect.outputs.clone().pop().unwrap().1,
                    Type::Bool
                ));
            }
            _ => panic!("Expected Quotation type"),
        }

        // Pop first quotation (Int -- Int)
        let (_, top2) = rest.pop().unwrap();
        match top2 {
            Type::Quotation(quot_effect) => {
                assert!(matches!(
                    quot_effect.inputs.clone().pop().unwrap().1,
                    Type::Int
                ));
                assert!(matches!(
                    quot_effect.outputs.clone().pop().unwrap().1,
                    Type::Int
                ));
            }
            _ => panic!("Expected Quotation type"),
        }
    }

    #[test]
    fn test_parse_quotation_type_without_separator() {
        // Test: ( [Int] -- ) should be REJECTED
        //
        // Design decision: The '--' separator is REQUIRED for clarity.
        // [Int] looks like a list type in most languages, not a consumer function.
        // This would confuse users.
        //
        // Require explicit syntax:
        // - `[Int -- ]` for quotation that consumes Int and produces nothing
        // - `[ -- Int]` for quotation that produces Int
        // - `[Int -- Int]` for transformation
        let source = ": consumer ( [Int] -- ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        // Should fail with helpful error message
        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("require") && err_msg.contains("--"),
            "Expected error about missing '--' separator, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_bare_quotation_type_rejected() {
        // Test: ( Int Quotation -- Int ) should be REJECTED
        //
        // 'Quotation' looks like a type name but would be silently treated as a
        // type variable without this check. Users must use explicit effect syntax.
        let source = ": apply-twice ( Int Quotation -- Int ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("Quotation") && err_msg.contains("not a valid type"),
            "Expected error about 'Quotation' not being valid, got: {}",
            err_msg
        );
        assert!(
            err_msg.contains("[Int -- Int]") || err_msg.contains("[ -- ]"),
            "Expected error to suggest explicit syntax, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_no_stack_effect() {
        // Test word without stack effect (should still work)
        let source = ": test 1 2 add ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert!(program.words[0].effect.is_none());
    }

    #[test]
    fn test_parse_simple_quotation() {
        let source = r#"
: test ( -- Quot )
  [ 1 add ] ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert_eq!(program.words[0].name, "test");
        assert_eq!(program.words[0].body.len(), 1);

        match &program.words[0].body[0] {
            Statement::Quotation { body, .. } => {
                assert_eq!(body.len(), 2);
                assert_eq!(body[0], Statement::IntLiteral(1));
                assert!(matches!(&body[1], Statement::WordCall { name, .. } if name == "add"));
            }
            _ => panic!("Expected Quotation statement"),
        }
    }

    #[test]
    fn test_parse_empty_quotation() {
        let source = ": test [ ] ;";

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);

        match &program.words[0].body[0] {
            Statement::Quotation { body, .. } => {
                assert_eq!(body.len(), 0);
            }
            _ => panic!("Expected Quotation statement"),
        }
    }

    #[test]
    fn test_parse_quotation_with_call() {
        let source = r#"
: test ( -- )
  5 [ 1 add ] call ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert_eq!(program.words[0].body.len(), 3);

        assert_eq!(program.words[0].body[0], Statement::IntLiteral(5));

        match &program.words[0].body[1] {
            Statement::Quotation { body, .. } => {
                assert_eq!(body.len(), 2);
            }
            _ => panic!("Expected Quotation"),
        }

        assert!(matches!(
            &program.words[0].body[2],
            Statement::WordCall { name, .. } if name == "call"
        ));
    }

    #[test]
    fn test_parse_nested_quotation() {
        let source = ": test [ [ 1 add ] call ] ;";

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);

        match &program.words[0].body[0] {
            Statement::Quotation {
                body: outer_body, ..
            } => {
                assert_eq!(outer_body.len(), 2);

                match &outer_body[0] {
                    Statement::Quotation {
                        body: inner_body, ..
                    } => {
                        assert_eq!(inner_body.len(), 2);
                        assert_eq!(inner_body[0], Statement::IntLiteral(1));
                        assert!(
                            matches!(&inner_body[1], Statement::WordCall { name, .. } if name == "add")
                        );
                    }
                    _ => panic!("Expected nested Quotation"),
                }

                assert!(
                    matches!(&outer_body[1], Statement::WordCall { name, .. } if name == "call")
                );
            }
            _ => panic!("Expected Quotation"),
        }
    }

    #[test]
    fn test_parse_while_with_quotations() {
        let source = r#"
: countdown ( Int -- )
  [ dup 0 > ] [ 1 subtract ] while drop ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert_eq!(program.words[0].body.len(), 4);

        // First quotation: [ dup 0 > ]
        match &program.words[0].body[0] {
            Statement::Quotation { body: pred, .. } => {
                assert_eq!(pred.len(), 3);
                assert!(matches!(&pred[0], Statement::WordCall { name, .. } if name == "dup"));
                assert_eq!(pred[1], Statement::IntLiteral(0));
                assert!(matches!(&pred[2], Statement::WordCall { name, .. } if name == ">"));
            }
            _ => panic!("Expected predicate quotation"),
        }

        // Second quotation: [ 1 subtract ]
        match &program.words[0].body[1] {
            Statement::Quotation { body, .. } => {
                assert_eq!(body.len(), 2);
                assert_eq!(body[0], Statement::IntLiteral(1));
                assert!(matches!(&body[1], Statement::WordCall { name, .. } if name == "subtract"));
            }
            _ => panic!("Expected body quotation"),
        }

        // while call
        assert!(matches!(
            &program.words[0].body[2],
            Statement::WordCall { name, .. } if name == "while"
        ));

        // drop
        assert!(matches!(
            &program.words[0].body[3],
            Statement::WordCall { name, .. } if name == "drop"
        ));
    }

    #[test]
    fn test_parse_simple_closure_type() {
        // Test: ( Int -- Closure[Int -- Int] )
        let source = ": make-adder ( Int -- Closure[Int -- Int] ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        let word = &program.words[0];
        assert!(word.effect.is_some());

        let effect = word.effect.as_ref().unwrap();

        // Input: Int on RowVar("rest")
        let (input_rest, input_top) = effect.inputs.clone().pop().unwrap();
        assert_eq!(input_top, Type::Int);
        assert_eq!(input_rest, StackType::RowVar("rest".to_string()));

        // Output: Closure[Int -- Int] on RowVar("rest")
        let (output_rest, output_top) = effect.outputs.clone().pop().unwrap();
        match output_top {
            Type::Closure { effect, captures } => {
                // Closure effect: Int -> Int
                assert_eq!(
                    effect.inputs,
                    StackType::Cons {
                        rest: Box::new(StackType::RowVar("rest".to_string())),
                        top: Type::Int
                    }
                );
                assert_eq!(
                    effect.outputs,
                    StackType::Cons {
                        rest: Box::new(StackType::RowVar("rest".to_string())),
                        top: Type::Int
                    }
                );
                // Captures should be empty (filled in by type checker)
                assert_eq!(captures.len(), 0);
            }
            _ => panic!("Expected Closure type, got {:?}", output_top),
        }
        assert_eq!(output_rest, StackType::RowVar("rest".to_string()));
    }

    #[test]
    fn test_parse_closure_type_with_row_vars() {
        // Test: ( ..a Config -- ..a Closure[Request -- Response] )
        let source = ": make-handler ( ..a Config -- ..a Closure[Request -- Response] ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Output: Closure on RowVar("a")
        let (rest, top) = effect.outputs.clone().pop().unwrap();
        match top {
            Type::Closure { effect, .. } => {
                // Closure effect: Request -> Response
                let (_, in_top) = effect.inputs.clone().pop().unwrap();
                assert_eq!(in_top, Type::Var("Request".to_string()));
                let (_, out_top) = effect.outputs.clone().pop().unwrap();
                assert_eq!(out_top, Type::Var("Response".to_string()));
            }
            _ => panic!("Expected Closure type"),
        }
        assert_eq!(rest, StackType::RowVar("a".to_string()));
    }

    #[test]
    fn test_parse_closure_type_missing_bracket() {
        // Test: ( Int -- Closure ) should fail
        let source = ": broken ( Int -- Closure ) ;";
        let mut parser = Parser::new(source);
        let result = parser.parse();

        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(
            err_msg.contains("[") && err_msg.contains("Closure"),
            "Expected error about missing '[' after Closure, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_parse_closure_type_in_input() {
        // Test: ( Closure[Int -- Int] -- )
        let source = ": apply-closure ( Closure[Int -- Int] -- ) ;";
        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        let effect = program.words[0].effect.as_ref().unwrap();

        // Input: Closure[Int -- Int] on RowVar("rest")
        let (_, top) = effect.inputs.clone().pop().unwrap();
        match top {
            Type::Closure { effect, .. } => {
                // Verify closure effect
                assert!(matches!(effect.inputs.clone().pop().unwrap().1, Type::Int));
                assert!(matches!(effect.outputs.clone().pop().unwrap().1, Type::Int));
            }
            _ => panic!("Expected Closure type in input"),
        }
    }

    // Tests for token position tracking

    #[test]
    fn test_token_position_single_line() {
        // Test token positions on a single line
        let source = ": main ( -- ) ;";
        let tokens = tokenize(source);

        // : is at line 0, column 0
        assert_eq!(tokens[0].text, ":");
        assert_eq!(tokens[0].line, 0);
        assert_eq!(tokens[0].column, 0);

        // main is at line 0, column 2
        assert_eq!(tokens[1].text, "main");
        assert_eq!(tokens[1].line, 0);
        assert_eq!(tokens[1].column, 2);

        // ( is at line 0, column 7
        assert_eq!(tokens[2].text, "(");
        assert_eq!(tokens[2].line, 0);
        assert_eq!(tokens[2].column, 7);
    }

    #[test]
    fn test_token_position_multiline() {
        // Test token positions across multiple lines
        let source = ": main ( -- )\n  42\n;";
        let tokens = tokenize(source);

        // Find the 42 token (after the newline)
        let token_42 = tokens.iter().find(|t| t.text == "42").unwrap();
        assert_eq!(token_42.line, 1);
        assert_eq!(token_42.column, 2); // After 2 spaces of indentation

        // Find the ; token (on line 2)
        let token_semi = tokens.iter().find(|t| t.text == ";").unwrap();
        assert_eq!(token_semi.line, 2);
        assert_eq!(token_semi.column, 0);
    }

    #[test]
    fn test_word_def_source_location_span() {
        // Test that word definitions capture correct start and end lines
        let source = r#": helper ( -- )
  "hello"
  write_line
;

: main ( -- )
  helper
;"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 2);

        // First word: helper spans lines 0-3
        let helper = &program.words[0];
        assert_eq!(helper.name, "helper");
        let helper_source = helper.source.as_ref().unwrap();
        assert_eq!(helper_source.start_line, 0);
        assert_eq!(helper_source.end_line, 3);

        // Second word: main spans lines 5-7
        let main_word = &program.words[1];
        assert_eq!(main_word.name, "main");
        let main_source = main_word.source.as_ref().unwrap();
        assert_eq!(main_source.start_line, 5);
        assert_eq!(main_source.end_line, 7);
    }

    #[test]
    fn test_token_position_string_with_newline() {
        // Test that newlines inside strings are tracked correctly
        let source = "\"line1\\nline2\"";
        let tokens = tokenize(source);

        // The string token should start at line 0, column 0
        assert_eq!(tokens.len(), 1);
        assert_eq!(tokens[0].line, 0);
        assert_eq!(tokens[0].column, 0);
    }

    // ============================================================================
    //                         ADT PARSING TESTS
    // ============================================================================

    #[test]
    fn test_parse_simple_union() {
        let source = r#"
union Message {
  Get { response-chan: Int }
  Set { value: Int }
}

: main ( -- ) ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.unions.len(), 1);
        let union_def = &program.unions[0];
        assert_eq!(union_def.name, "Message");
        assert_eq!(union_def.variants.len(), 2);

        // Check first variant
        assert_eq!(union_def.variants[0].name, "Get");
        assert_eq!(union_def.variants[0].fields.len(), 1);
        assert_eq!(union_def.variants[0].fields[0].name, "response-chan");
        assert_eq!(union_def.variants[0].fields[0].type_name, "Int");

        // Check second variant
        assert_eq!(union_def.variants[1].name, "Set");
        assert_eq!(union_def.variants[1].fields.len(), 1);
        assert_eq!(union_def.variants[1].fields[0].name, "value");
        assert_eq!(union_def.variants[1].fields[0].type_name, "Int");
    }

    #[test]
    fn test_parse_union_with_multiple_fields() {
        let source = r#"
union Report {
  Data { op: Int, delta: Int, total: Int }
  Empty
}

: main ( -- ) ;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.unions.len(), 1);
        let union_def = &program.unions[0];
        assert_eq!(union_def.name, "Report");
        assert_eq!(union_def.variants.len(), 2);

        // Check Data variant with 3 fields
        let data_variant = &union_def.variants[0];
        assert_eq!(data_variant.name, "Data");
        assert_eq!(data_variant.fields.len(), 3);
        assert_eq!(data_variant.fields[0].name, "op");
        assert_eq!(data_variant.fields[1].name, "delta");
        assert_eq!(data_variant.fields[2].name, "total");

        // Check Empty variant with no fields
        let empty_variant = &union_def.variants[1];
        assert_eq!(empty_variant.name, "Empty");
        assert_eq!(empty_variant.fields.len(), 0);
    }

    #[test]
    fn test_parse_union_lowercase_name_error() {
        let source = r#"
union message {
  Get { }
}
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("uppercase"));
    }

    #[test]
    fn test_parse_union_empty_error() {
        let source = r#"
union Message {
}
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("at least one variant"));
    }

    #[test]
    fn test_parse_union_duplicate_variant_error() {
        let source = r#"
union Message {
  Get { x: Int }
  Get { y: String }
}
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Duplicate variant name"));
        assert!(err.contains("Get"));
    }

    #[test]
    fn test_parse_union_duplicate_field_error() {
        let source = r#"
union Data {
  Record { x: Int, x: String }
}
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Duplicate field name"));
        assert!(err.contains("x"));
    }

    #[test]
    fn test_parse_simple_match() {
        let source = r#"
: handle ( -- )
  match
    Get -> send-response
    Set -> process-set
  end
;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);
        assert_eq!(program.words[0].body.len(), 1);

        match &program.words[0].body[0] {
            Statement::Match { arms, span: _ } => {
                assert_eq!(arms.len(), 2);

                // First arm: Get ->
                match &arms[0].pattern {
                    Pattern::Variant(name) => assert_eq!(name, "Get"),
                    _ => panic!("Expected Variant pattern"),
                }
                assert_eq!(arms[0].body.len(), 1);

                // Second arm: Set ->
                match &arms[1].pattern {
                    Pattern::Variant(name) => assert_eq!(name, "Set"),
                    _ => panic!("Expected Variant pattern"),
                }
                assert_eq!(arms[1].body.len(), 1);
            }
            _ => panic!("Expected Match statement"),
        }
    }

    #[test]
    fn test_parse_match_with_bindings() {
        let source = r#"
: handle ( -- )
  match
    Get { >chan } -> chan send-response
    Report { >delta >total } -> delta total process
  end
;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        assert_eq!(program.words.len(), 1);

        match &program.words[0].body[0] {
            Statement::Match { arms, span: _ } => {
                assert_eq!(arms.len(), 2);

                // First arm: Get { chan } ->
                match &arms[0].pattern {
                    Pattern::VariantWithBindings { name, bindings } => {
                        assert_eq!(name, "Get");
                        assert_eq!(bindings.len(), 1);
                        assert_eq!(bindings[0], "chan");
                    }
                    _ => panic!("Expected VariantWithBindings pattern"),
                }

                // Second arm: Report { delta total } ->
                match &arms[1].pattern {
                    Pattern::VariantWithBindings { name, bindings } => {
                        assert_eq!(name, "Report");
                        assert_eq!(bindings.len(), 2);
                        assert_eq!(bindings[0], "delta");
                        assert_eq!(bindings[1], "total");
                    }
                    _ => panic!("Expected VariantWithBindings pattern"),
                }
            }
            _ => panic!("Expected Match statement"),
        }
    }

    #[test]
    fn test_parse_match_bindings_require_prefix() {
        // Old syntax without > prefix should error
        let source = r#"
: handle ( -- )
  match
    Get { chan } -> chan send-response
  end
;
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains(">chan"));
        assert!(err.contains("stack extraction"));
    }

    #[test]
    fn test_parse_match_with_body_statements() {
        let source = r#"
: handle ( -- )
  match
    Get -> 1 2 add send-response
    Set -> process-value store
  end
;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::Match { arms, span: _ } => {
                // Get arm has 4 statements: 1, 2, add, send-response
                assert_eq!(arms[0].body.len(), 4);
                assert_eq!(arms[0].body[0], Statement::IntLiteral(1));
                assert_eq!(arms[0].body[1], Statement::IntLiteral(2));
                assert!(
                    matches!(&arms[0].body[2], Statement::WordCall { name, .. } if name == "add")
                );

                // Set arm has 2 statements: process-value, store
                assert_eq!(arms[1].body.len(), 2);
            }
            _ => panic!("Expected Match statement"),
        }
    }

    #[test]
    fn test_parse_match_empty_error() {
        let source = r#"
: handle ( -- )
  match
  end
;
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("at least one arm"));
    }

    #[test]
    fn test_parse_symbol_literal() {
        let source = r#"
: main ( -- )
    :hello drop
;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();
        assert_eq!(program.words.len(), 1);

        let main = &program.words[0];
        assert_eq!(main.body.len(), 2);

        match &main.body[0] {
            Statement::Symbol(name) => assert_eq!(name, "hello"),
            _ => panic!("Expected Symbol statement, got {:?}", main.body[0]),
        }
    }

    #[test]
    fn test_parse_symbol_with_hyphen() {
        let source = r#"
: main ( -- )
    :hello-world drop
;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::Symbol(name) => assert_eq!(name, "hello-world"),
            _ => panic!("Expected Symbol statement"),
        }
    }

    #[test]
    fn test_parse_symbol_starting_with_digit_fails() {
        let source = r#"
: main ( -- )
    :123abc drop
;
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("cannot start with a digit"));
    }

    #[test]
    fn test_parse_symbol_with_invalid_char_fails() {
        let source = r#"
: main ( -- )
    :hello@world drop
;
"#;

        let mut parser = Parser::new(source);
        let result = parser.parse();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("invalid character"));
    }

    #[test]
    fn test_parse_symbol_special_chars_allowed() {
        // Test that ? and ! are allowed in symbol names
        let source = r#"
: main ( -- )
    :empty? drop
    :save! drop
;
"#;

        let mut parser = Parser::new(source);
        let program = parser.parse().unwrap();

        match &program.words[0].body[0] {
            Statement::Symbol(name) => assert_eq!(name, "empty?"),
            _ => panic!("Expected Symbol statement"),
        }
        match &program.words[0].body[2] {
            Statement::Symbol(name) => assert_eq!(name, "save!"),
            _ => panic!("Expected Symbol statement"),
        }
    }
}