vb6parse 1.0.1

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

use std::collections::HashMap;
use std::num::NonZeroUsize;

use crate::errors::{ErrorKind, FormError};
use crate::files::common::{
    Creatable, Exposed, FileAttributes, FileFormatVersion, NameSpace, ObjectReference,
    PreDeclaredID, Properties,
};
use crate::io::{SourceFile, SourceStream};
use crate::language::{
    CheckBoxProperties, ComboBoxProperties, CommandButtonProperties, Control, ControlKind,
    DataProperties, DirListBoxProperties, DriveListBoxProperties, FileListBoxProperties, Font,
    Form, FormProperties, FormRoot, FrameProperties, LabelProperties, ListBoxProperties, MDIForm,
    MDIFormProperties, MenuControl, MenuProperties, OptionButtonProperties, PictureBoxProperties,
    PropertyGroup, TextBoxProperties, Token,
};
use crate::lexer::{tokenize, TokenStream};
use crate::parsers::SyntaxKind;
use crate::ParseResult;

use either::Either;
use rowan::{GreenNode, GreenNodeBuilder, Language};
use serde::Serialize;

// Submodules for organized CST parsing
mod assignment;
mod attribute_statements;
mod declarations;
mod deftype_statements;
mod enum_statements;
mod for_statements;
mod function_statements;
mod helpers;
mod if_statements;
mod loop_statements;
mod navigation;
mod option_statements;
mod parameters;
mod properties;
mod property_statements;
mod select_statements;
mod sub_statements;
mod type_statements;

// Re-export navigation types
pub use navigation::CstNode;

/// A serializable representation of the CST for snapshot testing.
///
/// This struct wraps the tree structure in a way that can be serialized
/// with serde, making it suitable for use with snapshot testing tools like insta.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, Hash)]
pub struct SerializableTree {
    /// The root node of the tree
    pub root: CstNode,
}

/// Helper function to serialize `ConcreteSyntaxTree` as `SerializableTree`
pub(crate) fn serialize_cst<S>(cst: &ConcreteSyntaxTree, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    cst.to_serializable().serialize(serializer)
}

/// The language type for VB6 syntax trees.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum VB6Language {}

impl Language for VB6Language {
    type Kind = SyntaxKind;

    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
        SyntaxKind::from_raw(raw)
    }

    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
        kind.to_raw()
    }
}

/// Extract typed property groups from a Vec<PropertyGroup>
fn extract_property_groups(groups: &[PropertyGroup]) -> ExtractedGroups {
    let mut font = None;

    for group in groups {
        if group.name.eq_ignore_ascii_case("Font") {
            if let Ok(f) = Font::try_from(group) {
                font = Some(f);
            }
        }
        // Future: handle other property group types (Images, etc.)
    }

    ExtractedGroups { font }
}

/// Struct to hold extracted property groups for a control
struct ExtractedGroups {
    font: Option<Font>,
    // Future: add other property group types
}

/// A Concrete Syntax Tree for VB6 code.
///
/// This structure wraps the rowan library's `GreenNode` internally but provides
/// a public API that doesn't expose rowan types.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConcreteSyntaxTree {
    /// The root green node (internal implementation detail)
    root: GreenNode,
}

impl ConcreteSyntaxTree {
    /// Create a new CST from a `GreenNode` (internal use only)
    fn new(root: GreenNode) -> Self {
        Self { root }
    }

    /// Parse a CST from a `SourceFile`.
    ///
    /// # Arguments
    ///
    /// * `source_file` - The source file to parse.
    ///
    /// # Returns
    ///
    /// A result containing the parsed CST or an error.
    #[must_use]
    pub fn from_source(source_file: &SourceFile) -> ParseResult<'_, Self> {
        Self::from_text(
            source_file.file_name().to_string(),
            source_file.source_stream().contents,
        )
    }

    /// Parse a CST from source code.
    ///
    /// # Arguments
    ///
    /// * `file_name` - The name of the source file.
    /// * `contents` - The contents of the source file.
    ///
    /// # Returns
    ///
    /// A result containing the parsed CST or an error.
    pub fn from_text<S>(file_name: S, contents: &str) -> ParseResult<'_, Self>
    where
        S: Into<String>,
    {
        let mut source_stream = SourceStream::new(file_name.into(), contents);
        let token_stream_result = tokenize(&mut source_stream);
        let (token_stream_opt, failures) = token_stream_result.unpack();

        let Some(token_stream) = token_stream_opt else {
            return ParseResult::new(None, failures);
        };

        let cst = parse(token_stream);

        ParseResult::new(Some(cst), failures)
    }

    /// Get the kind of the root node
    #[must_use]
    pub fn root_kind(&self) -> SyntaxKind {
        SyntaxKind::from_raw(self.root.kind())
    }

    /// Convert the CST to a serializable representation.
    ///
    /// This method creates a `SerializableTree` that can be used with
    /// snapshot testing tools like `insta`. The serializable tree contains
    /// the complete tree structure as a hierarchy of `CstNode` instances.
    ///
    /// # Example
    ///
    /// ```rust
    /// use vb6parse::ConcreteSyntaxTree;
    ///
    /// let source = "Sub Test()\nEnd Sub\n";
    /// let result = ConcreteSyntaxTree::from_text("test.bas", source);
    ///
    /// let (cst_opt, failures) = result.unpack();
    ///
    /// let cst = cst_opt.expect("Failed to parse source");
    ///
    /// if !failures.is_empty() {
    ///     for failure in failures.iter() {
    ///         failure.print();
    ///     }
    ///     panic!("Failed to parse source with {} errors.", failures.len());
    /// };
    ///
    /// let serializable = cst.to_serializable();
    ///
    /// // Can now be used with insta::assert_yaml_snapshot!
    /// ```
    #[must_use]
    pub fn to_serializable(&self) -> SerializableTree {
        SerializableTree {
            root: self.to_root_node(),
        }
    }

    /// Convert the internal rowan tree to a root `CstNode`.
    ///
    /// # Returns
    ///
    /// The root `CstNode` representing the entire CST.
    #[must_use]
    pub fn to_root_node(&self) -> CstNode {
        CstNode::new(SyntaxKind::Root, self.text(), false, self.children())
    }

    /// Create a new CST with specified node kinds removed from the root level.
    ///
    /// This method filters out direct children of the root node that match any of the
    /// specified kinds. This is useful for removing nodes that have already been parsed
    /// into structured data (like version statements, attributes, etc.) to avoid duplication.
    ///
    /// # Arguments
    ///
    /// * `kinds_to_remove` - A slice of `SyntaxKind` values to filter out
    ///
    /// # Returns
    ///
    /// A new `ConcreteSyntaxTree` with the specified kinds removed from the root level.
    ///
    /// # Example
    ///
    /// ```rust
    /// use vb6parse::ConcreteSyntaxTree;
    /// use vb6parse::parsers::SyntaxKind;
    ///
    /// let source = "VERSION 5.00\nSub Test()\nEnd Sub\n";
    /// let result = ConcreteSyntaxTree::from_text("test.bas", source);
    /// let (cst_opt, failures) = result.unpack();
    /// let cst = cst_opt.expect("Failed to parse source");
    ///
    /// // Remove version statement since it's already parsed
    /// let filtered = cst.without_kinds(&[SyntaxKind::VersionStatement]);
    ///
    /// assert!(!filtered.contains_kind(SyntaxKind::VersionStatement));
    /// ```
    #[must_use]
    pub fn without_kinds(&self, kinds_to_remove: &[SyntaxKind]) -> Self {
        let syntax_node = rowan::SyntaxNode::<VB6Language>::new_root(self.root.clone());
        let mut builder = GreenNodeBuilder::new();

        builder.start_node(SyntaxKind::Root.to_raw());

        // Iterate through children and only add those not in the filter list
        for child in syntax_node.children_with_tokens() {
            let child_kind = match &child {
                rowan::NodeOrToken::Node(node) => node.kind(),
                rowan::NodeOrToken::Token(token) => token.kind(),
            };

            // Skip if this kind should be removed
            if kinds_to_remove.contains(&child_kind) {
                continue;
            }

            // Add the child to the new tree
            Self::clone_node_or_token(&mut builder, child);
        }

        builder.finish_node();
        let new_root = builder.finish();

        Self::new(new_root)
    }

    /// Recursively clone a node or token into a builder
    fn clone_node_or_token(
        builder: &mut GreenNodeBuilder<'static>,
        node_or_token: rowan::NodeOrToken<
            rowan::SyntaxNode<VB6Language>,
            rowan::SyntaxToken<VB6Language>,
        >,
    ) {
        match node_or_token {
            rowan::NodeOrToken::Node(node) => {
                builder.start_node(node.kind().to_raw());
                for child in node.children_with_tokens() {
                    Self::clone_node_or_token(builder, child);
                }
                builder.finish_node();
            }
            rowan::NodeOrToken::Token(token) => {
                builder.token(token.kind().to_raw(), token.text());
            }
        }
    }
}

/// Parse a `TokenStream` into a Concrete Syntax Tree.
///
/// This function takes a `TokenStream` and constructs a CST that represents
/// the structure of the VB6 code.
///
/// # Arguments
///
/// * `tokens` - The token stream to parse
///
/// # Returns
///
/// A `ConcreteSyntaxTree` representing the parsed code.
///
/// # Example
///
/// ```rust
/// use vb6parse::lexer::TokenStream;
/// use vb6parse::parsers::cst::parse;
///
/// let tokens = TokenStream::new("example.bas".to_string(), vec![]);
/// let cst = parse(tokens);
/// ```
#[must_use]
pub fn parse(tokens: TokenStream) -> ConcreteSyntaxTree {
    let parser = Parser::new(tokens);
    parser.parse_root()
}

/// Internal parser state for building the CST
pub(crate) struct Parser<'a> {
    pub(crate) tokens: Vec<(&'a str, Token)>,
    pub(crate) pos: usize,
    pub(crate) builder: GreenNodeBuilder<'static>,
    pub(crate) parsing_header: bool,
}

impl<'a> Parser<'a> {
    fn new(token_stream: TokenStream<'a>) -> Self {
        Parser {
            tokens: token_stream.into_tokens(),
            pos: 0,
            builder: GreenNodeBuilder::new(),
            parsing_header: true,
        }
    }

    /// Create parser for direct extraction mode (control-only parsing)
    pub(crate) fn new_direct_extraction(tokens: Vec<(&'a str, Token)>, pos: usize) -> Self {
        Parser {
            tokens,
            pos,
            builder: GreenNodeBuilder::new(),
            parsing_header: true,
        }
    }

    // Create parser for hybrid mode (`FormFile` optimization)
    // ==================== Direct Extraction Helpers ====================
    // These methods support direct extraction without CST building

    /// Consume the parser and return the remaining tokens
    /// Used to get tokens after direct extraction for CST building
    pub(crate) fn into_tokens(self) -> Vec<(&'a str, Token)> {
        // Return tokens from current position onwards
        self.tokens[self.pos..].to_vec()
    }

    /// Skip whitespace tokens without consuming them into the CST
    pub(crate) fn skip_whitespace(&mut self) {
        while self.at_token(Token::Whitespace) {
            self.pos += 1;
        }
    }

    /// Skip whitespace and newline tokens without consuming them into the CST
    pub(crate) fn skip_whitespace_and_newlines(&mut self) {
        while self.at_token(Token::Whitespace) || self.at_token(Token::Newline) {
            self.pos += 1;
        }
    }

    /// Consume and advance past the current token without adding to CST
    /// Returns the consumed token for inspection
    pub(crate) fn consume_advance(&mut self) -> Option<(&'a str, Token)> {
        if self.pos < self.tokens.len() {
            let token = self.tokens[self.pos];
            self.pos += 1;
            Some(token)
        } else {
            None
        }
    }

    // ==================== Direct Extraction Methods ====================

    /// Parse VERSION statement directly without building CST
    ///
    /// Extracts the file format version (e.g., \"VERSION 5.00\") by directly
    /// parsing tokens without CST construction overhead.
    ///
    /// # Returns
    ///
    /// A `ParseResult` containing:
    /// - `result`: `Some(FileFormatVersion)` if found and valid, `None` if not present or invalid
    /// - `failures`: Empty vec (no errors generated for missing VERSION)
    pub(crate) fn parse_version_direct(&mut self) -> ParseResult<'a, FileFormatVersion> {
        self.skip_whitespace();

        // Check if VERSION keyword is present
        if !self.at_token(Token::VersionKeyword) {
            return ParseResult::new(None, Vec::new());
        }

        self.consume_advance(); // VERSION keyword
        self.skip_whitespace();

        // Parse version number (e.g., \"5.00\" or \"1.0\")
        let version_result = if let Some((text, token)) = self.tokens.get(self.pos) {
            match token {
                Token::SingleLiteral | Token::DoubleLiteral | Token::IntegerLiteral => {
                    let version_str = text.trim();
                    self.consume_advance();

                    // Parse \"major.minor\" format
                    let parts: Vec<&str> = version_str.split('.').collect();
                    if parts.len() == 2 {
                        if let (Ok(major), Ok(minor)) =
                            (parts[0].parse::<u8>(), parts[1].parse::<u8>())
                        {
                            Some(FileFormatVersion { major, minor })
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                }
                _ => None,
            }
        } else {
            None
        };

        // Skip optional CLASS keyword and trailing whitespace
        self.skip_whitespace();
        if self.at_token(Token::ClassKeyword) {
            self.consume_advance();
        }
        self.skip_whitespace_and_newlines();

        ParseResult::new(version_result, Vec::new())
    }

    // ==================== Core Control Extraction Methods ====================

    /// Check if current token is `BeginProperty` identifier
    fn is_begin_property(&self) -> bool {
        if let Some((text, token)) = self.tokens.get(self.pos) {
            *token == Token::Identifier && text.eq_ignore_ascii_case("BeginProperty")
        } else {
            false
        }
    }

    /// Check if current token is an identifier matching the target text (case-insensitive)
    fn is_identifier_text(&self, target: &str) -> bool {
        if let Some((text, token)) = self.tokens.get(self.pos) {
            *token == Token::Identifier && text.eq_ignore_ascii_case(target)
        } else {
            false
        }
    }

    /// Convert a Menu-typed Control into `MenuControl`
    fn control_to_menu(control: Control) -> MenuControl {
        let (name, tag, index, kind) = control.into_parts();

        if let ControlKind::Menu {
            properties,
            sub_menus,
        } = kind
        {
            MenuControl::new(name, tag, index, properties, sub_menus)
        } else {
            // Fallback: create empty menu control
            MenuControl::new(name, tag, index, MenuProperties::default(), Vec::new())
        }
    }

    /// Parse control type directly from tokens (e.g., "VB.Form", "VB.CommandButton")
    fn parse_control_type_direct(&mut self) -> String {
        let mut parts = Vec::new();

        // Parse identifier or keyword
        if self.is_identifier() || self.at_keyword() {
            if let Some((text, _)) = self.tokens.get(self.pos) {
                parts.push(text.to_string());
                self.consume_advance();
            }
        }

        // Parse dot-separated parts (e.g., "VB.Form")
        while self.at_token(Token::PeriodOperator) {
            self.consume_advance(); // dot
            if self.is_identifier() || self.at_keyword() {
                if let Some((text, _)) = self.tokens.get(self.pos) {
                    parts.push(".".to_string());
                    parts.push(text.to_string());
                    self.consume_advance();
                }
            }
        }

        parts.join("")
    }

    /// Parse control name directly from tokens
    fn parse_control_name_direct(&mut self) -> String {
        if self.is_identifier() || self.at_keyword() {
            if let Some((text, _)) = self.tokens.get(self.pos) {
                let name = text.to_string();
                self.consume_advance();
                return name;
            }
        }
        String::new()
    }

    /// Parse a property assignment (Key = Value) directly from tokens
    /// Returns (key, value) tuple
    fn parse_property_direct(&mut self) -> Option<(String, String)> {
        // Parse property key
        let key = if self.is_identifier() || self.at_keyword() {
            if let Some((text, _)) = self.tokens.get(self.pos) {
                let k = text.to_string();
                self.consume_advance();
                k
            } else {
                return None;
            }
        } else {
            return None;
        };

        self.skip_whitespace();

        // Parse = sign
        if !self.at_token(Token::EqualityOperator) {
            return None;
        }
        self.consume_advance();
        self.skip_whitespace();

        // Parse value (everything until newline/colon)
        // Special case: Resource references like "file.frx":0000 or $"file.frx":0000
        // should include the colon and offset. The quotes should be preserved.
        let mut value_parts = Vec::new();
        let mut in_resource_reference = false;

        while !self.is_at_end() && !self.at_token(Token::Newline) {
            if let Some((text, token)) = self.tokens.get(self.pos) {
                let text_copy = *text;
                let token_copy = *token;

                // Check if we see a dollar sign
                if token_copy == Token::DollarSign {
                    value_parts.push(text_copy);
                    self.consume_advance();
                }
                // If we see a string literal (with or without $), check if resource reference follows
                else if token_copy == Token::StringLiteral {
                    value_parts.push(text_copy);
                    self.consume_advance();

                    // Peek ahead - if next token is colon, this is a resource reference
                    if let Some((_, next_token)) = self.tokens.get(self.pos) {
                        if *next_token == Token::ColonOperator {
                            in_resource_reference = true;
                        }
                    }
                }
                // If in resource reference, capture colon
                else if in_resource_reference && token_copy == Token::ColonOperator {
                    value_parts.push(text_copy);
                    self.consume_advance();
                }
                // If in resource reference and we see the offset number, capture it and stop
                else if in_resource_reference
                    && (token_copy == Token::IntegerLiteral || token_copy == Token::LongLiteral)
                {
                    value_parts.push(text_copy);
                    self.consume_advance();
                    break; // Done with resource reference
                }
                // If we hit a colon and not in resource reference, stop
                else if token_copy == Token::ColonOperator {
                    break;
                }
                // Otherwise, capture the token
                else {
                    value_parts.push(text_copy);
                    self.consume_advance();
                }
            } else {
                break;
            }
        }

        // Skip newline
        self.skip_whitespace_and_newlines();

        // Join tokens directly without intermediate conversion
        let value = value_parts.concat().trim().to_string();
        Some((key, value))
    }

    /// Parse property group directly (BeginProperty...EndProperty)
    fn parse_property_group_direct(&mut self) -> Option<PropertyGroup> {
        // Expect BeginProperty identifier
        if !self.is_identifier_text("BeginProperty") {
            return None;
        }

        self.consume_advance(); // BeginProperty
        self.skip_whitespace();

        // Parse property group name and optional GUID
        let (name, guid) = self.parse_property_group_name_direct();
        self.skip_whitespace_and_newlines();

        // Parse nested properties and property groups
        let mut properties = HashMap::new();

        while !self.is_at_end() && !self.is_identifier_text("EndProperty") {
            self.skip_whitespace();

            if self.is_identifier_text("EndProperty") {
                break;
            }

            if self.is_identifier_text("BeginProperty") {
                // Nested property group
                if let Some(nested_group) = self.parse_property_group_direct() {
                    properties.insert(nested_group.name.clone(), Either::Right(nested_group));
                }
            } else if self.is_identifier() || self.at_keyword() {
                // Regular property
                if let Some((key, value)) = self.parse_property_direct() {
                    properties.insert(key, Either::Left(value));
                }
            } else {
                self.consume_advance();
            }
        }

        // Parse EndProperty
        if self.is_identifier_text("EndProperty") {
            self.consume_advance();
            self.skip_whitespace_and_newlines();
        }

        Some(PropertyGroup {
            name,
            guid,
            properties,
        })
    }

    /// Parse property group name and extract optional GUID
    /// Format: "Name {GUID}" or just "Name"
    fn parse_property_group_name_direct(&mut self) -> (String, Option<uuid::Uuid>) {
        let mut name_parts: Vec<&str> = Vec::new();
        let mut guid_parts: Vec<&str> = Vec::new();
        let mut in_guid = false;

        // Collect tokens until newline
        while !self.is_at_end() && !self.at_token(Token::Newline) {
            if let Some((text, token)) = self.tokens.get(self.pos) {
                if *token == Token::LeftCurlyBrace {
                    // Start of GUID
                    in_guid = true;
                } else if *token == Token::RightCurlyBrace {
                    // End of GUID
                    in_guid = false;
                } else if *token != Token::Whitespace && *token != Token::EndOfLineComment {
                    // Collect non-whitespace tokens
                    if in_guid {
                        guid_parts.push(*text);
                    } else {
                        name_parts.push(*text);
                    }
                }
            }
            self.consume_advance();
        }

        let name = name_parts.concat();
        let guid = if guid_parts.is_empty() {
            None
        } else {
            let guid_str = guid_parts.concat();
            uuid::Uuid::parse_str(&guid_str).ok()
        };

        (name, guid)
    }

    /// Parse Object statements directly (without CST)
    /// Phase 5: Direct extraction of Object references
    pub(crate) fn parse_objects_direct(&mut self) -> Vec<ObjectReference> {
        let mut objects = Vec::new();

        self.skip_whitespace_and_newlines();

        // Continue parsing Object statements until we hit something else
        while self.at_token(Token::ObjectKeyword) {
            if let Some(obj_ref) = self.parse_single_object_direct() {
                objects.push(obj_ref);
            }
            self.skip_whitespace_and_newlines();
        }

        objects
    }

    /// Parse a single Object statement line
    /// Format: Object = "{UUID}#version#flags"; "filename"
    /// Or:     Object = *\G{UUID}#version#flags; "filename"
    fn parse_single_object_direct(&mut self) -> Option<ObjectReference> {
        // Expect "Object" keyword
        if !self.at_token(Token::ObjectKeyword) {
            return None;
        }
        self.consume_advance(); // Object
        self.skip_whitespace();

        // Expect "="
        if !self.at_token(Token::EqualityOperator) {
            return None;
        }
        self.consume_advance(); // =
        self.skip_whitespace();

        // Check for optional "*\G" prefix (embedded object)
        let mut _is_embedded = false;
        if self.at_token(Token::MultiplicationOperator) {
            self.consume_advance(); // *
                                    // Expect \G (backslash followed by identifier "G")
            if let Some((_text, token)) = self.tokens.get(self.pos) {
                if *token == Token::BackwardSlashOperator {
                    self.consume_advance(); // \
                    if let Some((text2, token2)) = self.tokens.get(self.pos) {
                        if *token2 == Token::Identifier && text2.eq_ignore_ascii_case("G") {
                            self.consume_advance(); // G
                            _is_embedded = true;
                        }
                    }
                }
            }
        }
        self.skip_whitespace();

        // Parse first string literal or GUID tokens: "{UUID}#version#flags"
        // The GUID may be tokenized as:
        //   - A StringLiteral: "{UUID}#version#flags"
        //   - Individual tokens: { UUID-parts } # version # flags
        let uuid_part = if let Some((text, token)) = self.tokens.get(self.pos) {
            if *token == Token::StringLiteral {
                // String literal format
                let s = text.trim_matches('"').to_string();
                self.consume_advance();
                s
            } else if *token == Token::LeftCurlyBrace {
                // Token format: { guid-parts } #version# flags
                // Collect all tokens until semicolon
                let mut parts: Vec<&str> = Vec::new();
                while !self.is_at_end() && !self.at_token(Token::Semicolon) {
                    if let Some((text, token)) = self.tokens.get(self.pos) {
                        // Skip whitespace but collect everything else
                        if *token != Token::Whitespace {
                            parts.push(text);
                        }
                        self.consume_advance();
                    } else {
                        break;
                    }
                }
                // Reconstruct the UUID part string
                // Need to convert: { ... } #version# flags -> {UUID}#version#flags
                parts.concat()
            } else {
                return None;
            }
        } else {
            return None;
        };

        self.skip_whitespace();

        // Expect semicolon
        if !self.at_token(Token::Semicolon) {
            return None;
        }
        self.consume_advance(); // ;
        self.skip_whitespace();

        // Parse second string literal: filename
        let file_name = if let Some((text, token)) = self.tokens.get(self.pos) {
            if *token == Token::StringLiteral {
                let s = text.trim_matches('"').to_string();
                self.consume_advance();
                s
            } else {
                return None;
            }
        } else {
            return None;
        };

        // Parse UUID part: {UUID}#version#flags or UUID#version#flags
        let parts: Vec<&str> = uuid_part.split('#').collect();
        if parts.len() >= 3 {
            // Extract UUID (remove braces if present)
            let uuid_str = parts[0].trim_matches(|c| c == '{' || c == '}');

            if let Ok(uuid) = uuid::Uuid::parse_str(uuid_str) {
                let version = parts[1].to_string();
                let unknown1 = parts[2].to_string();

                return Some(ObjectReference::Compiled {
                    uuid,
                    version,
                    unknown1,
                    file_name,
                });
            }
        }

        None
    }

    /// Parse Attribute statements directly (without CST)
    /// Phase 6: Direct extraction of file attributes
    pub(crate) fn parse_attributes_direct(&mut self) -> FileAttributes {
        let mut name = String::new();
        let mut global_name_space = NameSpace::Local;
        let mut creatable = Creatable::True;
        let mut predeclared_id = PreDeclaredID::False;
        let mut exposed = Exposed::False;
        let mut description: Option<String> = None;
        let mut ext_key: HashMap<String, String> = HashMap::new();

        self.skip_whitespace_and_newlines();

        // Continue parsing Attribute statements until we hit something else
        while self.at_token(Token::AttributeKeyword) {
            if let Some((key, value)) = self.parse_single_attribute_direct() {
                // Process the extracted key-value pair
                match key.as_str() {
                    "VB_Name" => {
                        name = value;
                    }
                    "VB_GlobalNameSpace" => {
                        global_name_space = if value == "True" || value == "-1" {
                            NameSpace::Global
                        } else {
                            NameSpace::Local
                        };
                    }
                    "VB_Creatable" => {
                        creatable = if value == "True" || value == "-1" {
                            Creatable::True
                        } else {
                            Creatable::False
                        };
                    }
                    "VB_PredeclaredId" => {
                        predeclared_id = if value == "True" || value == "-1" {
                            PreDeclaredID::True
                        } else {
                            PreDeclaredID::False
                        };
                    }
                    "VB_Exposed" => {
                        exposed = if value == "True" || value == "-1" {
                            Exposed::True
                        } else {
                            Exposed::False
                        };
                    }
                    "VB_Description" => {
                        description = Some(value);
                    }
                    _ => {
                        // Store any other attributes in ext_key
                        ext_key.insert(key, value);
                    }
                }
            }
            self.skip_whitespace_and_newlines();
        }

        FileAttributes {
            name,
            global_name_space,
            creatable,
            predeclared_id,
            exposed,
            description,
            ext_key,
        }
    }

    /// Parse a single Attribute statement line
    /// ```Attribute VB_Name = "Value"```
    /// Or
    /// ```Attribute VB_GlobalNameSpace = True```
    fn parse_single_attribute_direct(&mut self) -> Option<(String, String)> {
        // Expect "Attribute" keyword
        if !self.at_token(Token::AttributeKeyword) {
            return None;
        }
        self.consume_advance(); // Attribute
        self.skip_whitespace();

        // Parse attribute key (e.g., "VB_Name")
        let key = if let Some((text, token)) = self.tokens.get(self.pos) {
            if *token == Token::Identifier {
                let k = text.to_string();
                self.consume_advance();
                k
            } else {
                return None;
            }
        } else {
            return None;
        };

        self.skip_whitespace();

        // Expect "="
        if !self.at_token(Token::EqualityOperator) {
            return None;
        }
        self.consume_advance(); // =
        self.skip_whitespace();

        // Parse value (can be string, True/False, or number)
        let mut value = String::new();
        let mut found_value = false;

        // Check for negative sign first (for values like "-1")
        if self.at_token(Token::SubtractionOperator) {
            value.push('-');
            self.consume_advance();
            self.skip_whitespace();
        }

        if let Some((text, token)) = self.tokens.get(self.pos) {
            match token {
                Token::StringLiteral => {
                    // Remove surrounding quotes
                    value.push_str(text.trim().trim_matches('"'));
                    self.consume_advance();
                    found_value = true;
                }
                Token::TrueKeyword => {
                    value.push_str("True");
                    self.consume_advance();
                    found_value = true;
                }
                Token::FalseKeyword => {
                    value.push_str("False");
                    self.consume_advance();
                    found_value = true;
                }
                Token::IntegerLiteral | Token::LongLiteral => {
                    value.push_str(text.trim());
                    self.consume_advance();
                    found_value = true;
                }
                _ => {}
            }
        }

        // Consume the rest of the line (for complex attributes like VB_Ext_KEY)
        // Skip until we hit a newline or end of tokens
        while self.pos < self.tokens.len() {
            if let Some((_, token)) = self.tokens.get(self.pos) {
                if *token == Token::Newline {
                    break;
                }
                self.consume_advance();
            } else {
                break;
            }
        }

        if found_value {
            Some((key, value))
        } else {
            None
        }
    }

    /// Build `FormRoot` from control type string and properties
    ///
    /// This function is used for parsing top-level form elements.
    /// Only `VB.Form` and `VB.MDIForm` are valid top-level types.
    fn build_form_root(
        control_type: &str,
        control_name: String,
        tag: String,
        index: i32,
        properties: Properties,
        groups: &[PropertyGroup],
        child_controls: Vec<Control>,
        menus: Vec<MenuControl>,
    ) -> Result<FormRoot, ErrorKind> {
        match control_type {
            "VB.Form" => {
                let mut form_properties: FormProperties = properties.into();
                // Override with property group if present
                let extracted_groups = extract_property_groups(groups);
                if let Some(font) = extracted_groups.font {
                    form_properties.font = Some(font);
                }

                Ok(FormRoot::Form(Form {
                    name: control_name,
                    tag,
                    index,
                    properties: form_properties,
                    controls: child_controls,
                    menus,
                }))
            }
            "VB.MDIForm" => {
                let mut mdi_form_properties: MDIFormProperties = properties.into();
                // Override with property group if present
                let extracted_groups = extract_property_groups(groups);
                if let Some(font) = extracted_groups.font {
                    mdi_form_properties.font = Some(font);
                }

                Ok(FormRoot::MDIForm(MDIForm {
                    name: control_name,
                    tag,
                    index,
                    properties: mdi_form_properties,
                    controls: child_controls,
                    menus,
                }))
            }
            _ => Err(ErrorKind::Form(FormError::InvalidTopLevelControl {
                control_type: control_type.to_string(),
            })),
        }
    }

    /// Build `ControlKind` from control type string and properties
    ///
    /// Note: This function rejects `VB.Form` and `VB.MDIForm` as they are now
    /// top-level types only and cannot be child controls.
    fn build_control_kind(
        control_type: &str,
        properties: Properties,
        child_controls: Vec<Control>,
        menus: Vec<MenuControl>,
        property_groups: Vec<PropertyGroup>,
    ) -> ControlKind {
        use ControlKind;
        // Extract typed property groups
        let groups = extract_property_groups(&property_groups);

        match control_type {
            "VB.CommandButton" => {
                let mut props: CommandButtonProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::CommandButton { properties: props }
            }
            "VB.Data" => {
                let mut props: DataProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::Data { properties: props }
            }
            "VB.TextBox" => {
                let mut props: TextBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::TextBox { properties: props }
            }
            "VB.Label" => {
                let mut props: LabelProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::Label { properties: props }
            }
            "VB.CheckBox" => {
                let mut props: CheckBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::CheckBox { properties: props }
            }
            "VB.Line" => ControlKind::Line {
                properties: properties.into(),
            },
            "VB.Shape" => ControlKind::Shape {
                properties: properties.into(),
            },
            "VB.ListBox" => {
                let mut props: ListBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::ListBox { properties: props }
            }
            "VB.ComboBox" => {
                let mut props: ComboBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::ComboBox { properties: props }
            }
            "VB.Timer" => ControlKind::Timer {
                properties: properties.into(),
            },
            "VB.HScrollBar" => ControlKind::HScrollBar {
                properties: properties.into(),
            },
            "VB.VScrollBar" => ControlKind::VScrollBar {
                properties: properties.into(),
            },
            "VB.Frame" => {
                let mut props: FrameProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::Frame {
                    properties: props,
                    controls: child_controls,
                }
            }
            "VB.PictureBox" => {
                let mut props: PictureBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::PictureBox {
                    properties: props,
                    controls: child_controls,
                }
            }
            "VB.FileListBox" => {
                let mut props: FileListBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::FileListBox { properties: props }
            }
            "VB.DirListBox" => {
                let mut props: DirListBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::DirListBox { properties: props }
            }
            "VB.DriveListBox" => {
                let mut props: DriveListBoxProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::DriveListBox { properties: props }
            }
            "VB.Image" => ControlKind::Image {
                properties: properties.into(),
            },
            "VB.OptionButton" => {
                let mut props: OptionButtonProperties = properties.into();
                // Override with property group if present
                if let Some(font) = groups.font {
                    props.font = Some(font);
                }
                ControlKind::OptionButton { properties: props }
            }
            "VB.OLE" => ControlKind::Ole {
                properties: properties.into(),
            },
            "VB.Menu" => ControlKind::Menu {
                properties: properties.into(),
                sub_menus: menus,
            },
            _ => ControlKind::Custom {
                properties: properties.into(),
                property_groups,
            },
        }
    }

    /// Parse properties block directly to Control without building CST
    /// Phase 4: Full implementation with nested controls and property groups
    pub(crate) fn parse_properties_block_to_control(&mut self) -> ParseResult<'a, Control> {
        self.skip_whitespace();

        // Expect BEGIN keyword
        if !self.at_token(Token::BeginKeyword) {
            return ParseResult::new(None, Vec::new());
        }

        self.consume_advance(); // BEGIN
        self.skip_whitespace();

        // Parse control type (e.g., "VB.Form")
        let control_type = self.parse_control_type_direct();
        self.skip_whitespace();

        // Parse control name
        let control_name = self.parse_control_name_direct();
        self.skip_whitespace_and_newlines();

        // Parse properties, child controls, and property groups
        let mut properties = Properties::new();
        let mut child_controls = Vec::new();
        let mut menus = Vec::new();
        let mut property_groups = Vec::new();
        let mut failures = Vec::new();

        while !self.is_at_end() && !self.at_token(Token::EndKeyword) {
            self.skip_whitespace();

            if self.at_token(Token::EndKeyword) {
                break;
            }

            if self.at_token(Token::BeginKeyword) {
                // Nested control (Begin VB.xxx)
                let child_result = self.parse_properties_block_to_control();
                let (child_opt, child_failures) = child_result.unpack();
                failures.extend(child_failures);

                if let Some(child) = child_opt {
                    // Check if it's a menu control
                    if matches!(child.kind(), ControlKind::Menu { .. }) {
                        menus.push(Self::control_to_menu(child));
                    } else {
                        child_controls.push(child);
                    }
                }
            } else if self.is_begin_property() {
                // Parse property group (BeginProperty)
                if let Some(group) = self.parse_property_group_direct() {
                    property_groups.push(group);
                }
            } else if self.is_identifier() || self.at_keyword() {
                // Parse property (Key = Value)
                if let Some((key, value)) = self.parse_property_direct() {
                    // Remove surrounding quotes if this is a simple string literal
                    // BUT NOT if it's a resource reference (contains ":digit" pattern)
                    let is_resource_reference = value.contains(':')
                        && value
                            .split(':')
                            .next_back()
                            .is_some_and(|part| part.chars().all(|c| c.is_ascii_digit()));

                    let cleaned_value = if !is_resource_reference
                        && value.starts_with('"')
                        && value.ends_with('"')
                        && value.len() >= 2
                    {
                        &value[1..value.len() - 1]
                    } else {
                        &value
                    };
                    properties.insert(&key, cleaned_value);
                }
            } else {
                // Skip unknown token
                self.consume_advance();
            }
        }

        // Parse END keyword
        if self.at_token(Token::EndKeyword) {
            self.consume_advance();
            self.skip_whitespace_and_newlines();
        }

        // Extract tag and index from properties
        let tag = properties.get("Tag").cloned().unwrap_or_default();
        let index = properties
            .get("Index")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);

        // Build control kind with all components
        let kind = Self::build_control_kind(
            &control_type,
            properties,
            child_controls,
            menus,
            property_groups,
        );

        let control = Control::new(control_name, tag, index, kind);

        ParseResult::new(Some(control), failures)
    }

    /// Parse properties block directly to `FormRoot` for top-level form elements.
    ///
    /// This function is specifically for parsing the top-level form element in
    /// `.frm`, `.ctl`, and `.dob` files. It enforces that only `VB.Form` or
    /// `VB.MDIForm` can be used as the root element.
    ///
    /// # Returns
    ///
    /// A `ParseResult` containing either a `FormRoot` (`Form` or `MDIForm`) or `None`,
    /// along with any parsing failures encountered.
    pub(crate) fn parse_properties_block_to_form_root(&mut self) -> ParseResult<'a, FormRoot> {
        use Properties;

        let mut groups = Vec::new();

        self.skip_whitespace();

        // Expect BEGIN keyword
        if !self.at_token(Token::BeginKeyword) {
            return ParseResult::new(None, Vec::new());
        }

        self.consume_advance(); // BEGIN
        self.skip_whitespace();

        // Parse control type (e.g., "VB.Form" or "VB.MDIForm")
        let control_type = self.parse_control_type_direct();
        self.skip_whitespace();

        // Parse control name
        let control_name = self.parse_control_name_direct();
        self.skip_whitespace_and_newlines();

        // Parse properties, child controls, and property groups
        let mut properties = Properties::new();
        let mut child_controls = Vec::new();
        let mut menus = Vec::new();
        let mut failures = Vec::new();

        while !self.is_at_end() && !self.at_token(Token::EndKeyword) {
            self.skip_whitespace();

            if self.at_token(Token::EndKeyword) {
                break;
            }

            if self.at_token(Token::BeginKeyword) {
                // Nested control (Begin VB.xxx) - use parse_properties_block_to_control for children
                let child_result = self.parse_properties_block_to_control();
                let (child_opt, child_failures) = child_result.unpack();
                failures.extend(child_failures);

                if let Some(child) = child_opt {
                    // Check if it's a menu control
                    if matches!(child.kind(), ControlKind::Menu { .. }) {
                        menus.push(Self::control_to_menu(child));
                    } else {
                        child_controls.push(child);
                    }
                }
            } else if self.is_begin_property() {
                // Parse property group (BeginProperty)
                if let Some(group) = self.parse_property_group_direct() {
                    // Property groups are not used in Form/MDIForm, but we parse them anyway
                    // to avoid errors if they appear
                    groups.push(group);
                }
            } else if self.is_identifier() || self.at_keyword() {
                // Parse property (Key = Value)
                if let Some((key, value)) = self.parse_property_direct() {
                    // Remove surrounding quotes if this is a simple string literal
                    // BUT NOT if it's a resource reference (contains ":digit" pattern)
                    let is_resource_reference = value.contains(':')
                        && value
                            .split(':')
                            .next_back()
                            .is_some_and(|part| part.chars().all(|c| c.is_ascii_digit()));

                    let cleaned_value = if !is_resource_reference
                        && value.starts_with('"')
                        && value.ends_with('"')
                        && value.len() >= 2
                    {
                        &value[1..value.len() - 1]
                    } else {
                        &value
                    };
                    properties.insert(&key, cleaned_value);
                }
            } else {
                // Skip unknown token
                self.consume_advance();
            }
        }

        // Parse END keyword
        if self.at_token(Token::EndKeyword) {
            self.consume_advance();
            self.skip_whitespace_and_newlines();
        }

        // Extract tag and index from properties
        let tag = properties.get("Tag").cloned().unwrap_or_default();
        let index = properties
            .get("Index")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);

        // Build FormRoot with all components
        if let Ok(form_root) = Self::build_form_root(
            &control_type,
            control_name,
            tag,
            index,
            properties,
            &groups,
            child_controls,
            menus,
        ) {
            ParseResult::new(Some(form_root), failures)
        } else {
            // If invalid top-level control type, return a default Form as fallback
            let default_form = FormRoot::Form(Form {
                name: String::new(),
                tag: String::new(),
                index: 0,
                properties: FormProperties::default(),
                controls: Vec::new(),
                menus: Vec::new(),
            });
            ParseResult::new(Some(default_form), failures)
        }
    }

    /// Parse a complete module/class/form (the top-level structure)
    ///
    /// This function loops through all tokens and identifies what kind of
    /// VB6 construct to parse based on the current token. As more VB6 syntax
    /// is supported, additional branches can be added to this loop.
    fn parse_root(mut self) -> ConcreteSyntaxTree {
        self.builder.start_node(SyntaxKind::Root.to_raw());

        // Parse VERSION statement (if present)
        if self.at_token(Token::VersionKeyword) {
            self.parse_version_statement();
        }

        // Parse BEGIN ... END block (if present)
        if self.at_token(Token::BeginKeyword) {
            self.parse_properties_block();
        }

        // Parse Attribute statements (if present)
        // These come after the PropertiesBlock in forms/classes
        while self.at_token(Token::AttributeKeyword) {
            self.parse_attribute_statement();
        }

        self.parse_module_body();
        self.builder.finish_node(); // Root

        let root = self.builder.finish();
        ConcreteSyntaxTree::new(root)
    }

    fn parse_module_body(&mut self) {
        while !self.is_at_end() {
            // For a CST, we need to consume ALL tokens, including whitespace and comments
            // We look ahead to determine structure, but still consume everything

            // Check what kind of statement or declaration we're looking at
            match self.current_token() {
                // BEGIN ... END block (for forms/classes with properties)
                // This can appear after Object statements in form files
                Some(Token::BeginKeyword) => {
                    self.parse_properties_block();
                }
                // Object statement: Object = "{UUID}#version#flags"; "filename"
                // Only parse as ObjectStatement if it matches the proper format
                Some(Token::ObjectKeyword) if self.is_object_statement() => {
                    self.parse_object_statement();
                }
                // Attribute statement: Attribute VB_Name = "..."
                Some(Token::AttributeKeyword) => {
                    self.parse_attribute_statement();
                }
                Some(Token::OptionKeyword) => {
                    // Peek ahead to check if this is Option Base, Option Compare, or Option Private
                    if let Some(Token::BaseKeyword) = self.peek_next_keyword() {
                        self.parse_option_base_statement();
                    } else if let Some(Token::CompareKeyword) = self.peek_next_keyword() {
                        self.parse_option_compare_statement();
                    } else if let Some(Token::PrivateKeyword) = self.peek_next_keyword() {
                        self.parse_option_private_statement();
                    } else {
                        self.parse_option_statement();
                    }
                }
                // DefType statements: DefInt, DefLng, DefStr, etc.
                Some(
                    Token::DefBoolKeyword
                    | Token::DefByteKeyword
                    | Token::DefIntKeyword
                    | Token::DefLngKeyword
                    | Token::DefCurKeyword
                    | Token::DefSngKeyword
                    | Token::DefDblKeyword
                    | Token::DefDecKeyword
                    | Token::DefDateKeyword
                    | Token::DefStrKeyword
                    | Token::DefObjKeyword
                    | Token::DefVarKeyword,
                ) => {
                    self.parse_deftype_statement();
                }
                // Declare statement: Declare Sub/Function Name Lib "..."
                Some(Token::DeclareKeyword) => {
                    self.parse_declare_statement();
                }
                // Event statement: Event Name(...)
                Some(Token::EventKeyword) => {
                    self.parse_event_statement();
                }
                // Implements statement: Implements InterfaceName
                Some(Token::ImplementsKeyword) => {
                    self.parse_implements_statement();
                }
                // Enum statement: Enum Name ... End Enum
                Some(Token::EnumKeyword) => {
                    self.parse_enum_statement();
                }
                // Type statement: Type Name ... End Type
                Some(Token::TypeKeyword) => {
                    self.parse_type_statement();
                }
                // Sub procedure: Sub Name(...)
                Some(Token::SubKeyword) => {
                    self.parse_sub_statement();
                }
                // Function Procedure Syntax:
                //
                // [Public | Private | Friend] [ Static ] Function name [ ( arglist ) ] [ As type ]
                //
                Some(Token::FunctionKeyword) => {
                    self.parse_function_statement();
                }
                // Property Procedure Syntax:
                //
                // [Public | Private | Friend] [ Static ] Property Get|Let|Set name [ ( arglist ) ] [ As type ]
                //
                Some(Token::PropertyKeyword) => {
                    self.parse_property_statement();
                }
                // Variable declarations: Dim/Const
                // For Public/Private/Friend/Static, we need to look ahead to see if it's a
                // function/sub declaration or a variable declaration
                Some(Token::DimKeyword | Token::ConstKeyword) => {
                    self.parse_dim();
                }
                // Public/Private/Friend/Static - could be function/sub/property or declaration
                Some(
                    Token::PrivateKeyword
                    | Token::PublicKeyword
                    | Token::FriendKeyword
                    | Token::StaticKeyword,
                ) => {
                    // Look ahead to see if this is a function/sub/property/enum declaration
                    // Peek at the next 2 keywords to handle cases like "Public Static Function"
                    let next_keywords: Vec<_> = self
                        .peek_next_count_keywords(NonZeroUsize::new(2).unwrap())
                        .collect();

                    match next_keywords.as_slice() {
                        // Direct: Public/Private/Friend Function, Sub, Property, Enum, Type, Declare, or Event
                        [Token::FunctionKeyword, ..] => self.parse_function_statement(), // Function
                        [Token::SubKeyword, ..] => self.parse_sub_statement(),           // Sub
                        [Token::PropertyKeyword, ..] => self.parse_property_statement(), // Property
                        [Token::DeclareKeyword, ..] => self.parse_declare_statement(),   // Declare
                        [Token::EnumKeyword, ..] => self.parse_enum_statement(),         // Enum
                        [Token::TypeKeyword, ..] => self.parse_type_statement(),         // Type
                        [Token::EventKeyword, ..] => self.parse_event_statement(),       // Event
                        [Token::ImplementsKeyword, ..] => self.parse_implements_statement(), // Implements
                        // With Static: Public/Private/Friend Static Function, Sub, or Property
                        [Token::StaticKeyword, Token::FunctionKeyword] => {
                            self.parse_function_statement();
                        }
                        [Token::StaticKeyword, Token::SubKeyword] => {
                            self.parse_sub_statement();
                        }
                        [Token::StaticKeyword, Token::PropertyKeyword] => {
                            self.parse_property_statement();
                        }
                        // Anything else is a declaration
                        _ => self.parse_dim(),
                    }
                }
                // Anything else - check if it's a statement, label, assignment, or unknown
                _ => {
                    // Whitespace, newlines, and comments - consume directly FIRST
                    // This must be checked before is_at_procedure_call to avoid
                    // treating REM comments as procedure calls
                    if matches!(
                        self.current_token(),
                        Some(
                            Token::Whitespace
                                | Token::Newline
                                | Token::EndOfLineComment
                                | Token::RemComment
                        )
                    ) {
                        self.consume_token();
                    // Try control flow statements
                    } else if self.is_control_flow_keyword() {
                        self.parse_control_flow_statement();
                    // Try built-in statements
                    } else if self.is_library_statement_keyword() {
                        self.parse_library_statement();
                    // Try array statements
                    } else if self.is_variable_declaration_keyword() {
                        self.parse_array_statement();
                    // Try to parse common statements using centralized dispatcher
                    } else if self.is_statement_keyword() {
                        self.parse_statement();
                    // Check if this is a label (identifier followed by colon)
                    } else if self.is_at_label() {
                        self.parse_label_statement();
                    // Check for Let statement (optional assignment keyword)
                    } else if self.at_token(Token::LetKeyword) {
                        self.parse_let_statement();
                    // Check if this looks like an assignment statement (identifier = expression)
                    // This must come BEFORE at_keyword() check to handle keywords used as variables
                    } else if self.is_at_assignment() {
                        self.parse_assignment_statement();
                    // Check if this looks like a procedure call (identifier without assignment)
                    } else if self.is_at_procedure_call() {
                        self.parse_procedure_call();
                    } else if self.is_identifier() || self.at_keyword() {
                        self.consume_token();
                    } else {
                        // This is purely being done this way to make it easier during development.
                        // In a full implementation, we would have specific parsing functions
                        // for all VB6 constructs with anything unrecognized being treated as an error node.
                        self.consume_token_as_unknown();
                    }
                }
            }
        }
    }

    /// Check if the current token is a control flow keyword.
    /// Checks both current position and next non-whitespace token.
    fn is_control_flow_keyword(&self) -> bool {
        let token = if self.at_token(Token::Whitespace) {
            self.peek_next_keyword()
        } else {
            self.current_token().copied()
        };

        matches!(
            token,
            Some(
                Token::IfKeyword
                    | Token::SelectKeyword
                    | Token::ForKeyword
                    | Token::DoKeyword
                    | Token::WhileKeyword
                    | Token::GotoKeyword
                    | Token::GoSubKeyword
                    | Token::ReturnKeyword
                    | Token::ResumeKeyword
                    | Token::ExitKeyword
                    | Token::OnKeyword
            )
        )
    }

    /// Dispatch control flow statement parsing to the appropriate parser.
    fn parse_control_flow_statement(&mut self) {
        let token = if self.at_token(Token::Whitespace) {
            self.peek_next_keyword()
        } else {
            self.current_token().copied()
        };

        match token {
            Some(Token::IfKeyword) => {
                self.parse_if_statement();
            }
            Some(Token::SelectKeyword) => {
                self.parse_select_case_statement();
            }
            Some(Token::ForKeyword) => {
                // Peek ahead to see if next keyword is "Each"
                // Need to peek TWO keywords ahead if we're currently at whitespace
                let next_kw = if self.at_token(Token::Whitespace) {
                    // We peeked to find "For", now peek one more for "Each"
                    self.peek_next_count_keywords(NonZeroUsize::new(2).unwrap())
                        .nth(1)
                } else {
                    // We're directly at "For", peek once for "Each"
                    self.peek_next_keyword()
                };

                if let Some(Token::EachKeyword) = next_kw {
                    self.parse_for_each_statement();
                } else {
                    self.parse_for_statement();
                }
            }
            Some(Token::DoKeyword) => {
                self.parse_do_statement();
            }
            Some(Token::WhileKeyword) => {
                self.parse_while_statement();
            }
            Some(Token::GotoKeyword) => {
                self.parse_goto_statement();
            }
            Some(Token::GoSubKeyword) => {
                self.parse_gosub_statement();
            }
            Some(Token::ReturnKeyword) => {
                self.parse_return_statement();
            }
            Some(Token::ResumeKeyword) => {
                self.parse_resume_statement();
            }
            Some(Token::ExitKeyword) => {
                self.parse_exit_statement();
            }
            Some(Token::OnKeyword) => {
                // Look ahead to distinguish between On Error, On GoTo, and On GoSub
                // Need to peek different amounts depending on whether we're at whitespace
                let next_kw = if self.at_token(Token::Whitespace) {
                    // We peeked to find "On", now peek one more for "Error/GoTo/GoSub"
                    self.peek_next_count_keywords(NonZeroUsize::new(2).unwrap())
                        .nth(1)
                } else {
                    // We're directly at "On", peek once for next keyword
                    self.peek_next_keyword()
                };

                if let Some(Token::ErrorKeyword) = next_kw {
                    self.parse_on_error_statement();
                } else {
                    // Need to scan ahead to find GoTo or GoSub keyword
                    // to distinguish between On GoTo and On GoSub
                    let peek_start = if self.at_token(Token::Whitespace) {
                        2
                    } else {
                        1
                    };
                    let keywords: Vec<Token> = self
                        .peek_next_count_keywords(NonZeroUsize::new(20).unwrap())
                        .skip(peek_start)
                        .collect();

                    let has_goto = keywords.contains(&Token::GotoKeyword);
                    let has_gosub = keywords.contains(&Token::GoSubKeyword);

                    if has_goto {
                        self.parse_on_goto_statement();
                    } else if has_gosub {
                        self.parse_on_gosub_statement();
                    } else {
                        // Fallback - treat as On Error if we can't determine
                        self.parse_on_error_statement();
                    }
                }
            }
            _ => {}
        }
    }

    /// Check if the current token is an array statement keyword.
    /// Checks both current position and next non-whitespace token.
    fn is_variable_declaration_keyword(&self) -> bool {
        let token = if self.at_token(Token::Whitespace) {
            self.peek_next_keyword()
        } else {
            self.current_token().copied()
        };

        matches!(token, Some(Token::ReDimKeyword | Token::EraseKeyword))
    }

    /// Check if we're at an Object statement with proper format.
    ///
    /// Object statements in VB6 forms have the format:
    /// `Object = "{GUID}#version#flags"; "filename"`
    /// or
    /// `Object = *\G{GUID}#version#flags; "filename"`
    ///
    /// This checks if the pattern matches before committing to parse as `ObjectStatement`.
    #[allow(clippy::needless_continue)] // continue on whitespace is needed but clippy is incorrectly catching here.
    fn is_object_statement(&self) -> bool {
        // Must start with Object keyword
        if !self.at_token(Token::ObjectKeyword) {
            return false;
        }

        // Look ahead to verify it matches Object statement pattern
        // Skip whitespace, should find =, then whitespace, then string or *\G pattern
        let mut found_equals = false;
        for (_text, token) in self.tokens.iter().skip(self.pos + 1) {
            match token {
                // TODO: Change this parsing to better handle leading whitespace on object statements.
                Token::Whitespace => continue,
                Token::EqualityOperator if !found_equals => {
                    found_equals = true;
                }
                // After =, we expect either a quoted string starting with { or * for type library refs
                Token::StringLiteral | Token::MultiplicationOperator if found_equals => {
                    // Valid Object statement - string literal after =
                    // or
                    // Could be *\G{ pattern for type libraries
                    return true;
                }
                // If we hit anything else after =, not an Object statement
                _ if found_equals => return false,
                // If we hit a newline before =, not an Object statement
                Token::Newline | Token::EndOfLineComment | Token::RemComment => {
                    return false;
                }
                _ => return false,
            }
        }
        false
    }

    /// Dispatch array statement parsing to the appropriate parser.
    fn parse_array_statement(&mut self) {
        let token = if self.at_token(Token::Whitespace) {
            self.peek_next_keyword()
        } else {
            self.current_token().copied()
        };

        match token {
            Some(Token::ReDimKeyword) => {
                self.parse_redim_statement();
            }
            Some(Token::EraseKeyword) => {
                self.parse_erase_statement();
            }
            _ => {}
        }
    }

    /// Parse a statement list, consuming tokens until a termination condition is met.
    ///
    /// This is a generic statement list parser that can handle different termination conditions:
    /// - End Sub, End Function, End If, etc.
    /// - `ElseIf` or Else (for If statements)
    ///
    /// # Arguments
    /// * `stop_conditions` - A closure that returns true when the block should stop parsing
    pub(crate) fn parse_statement_list<F>(&mut self, stop_conditions: F)
    where
        F: Fn(&Parser) -> bool,
    {
        // Statement lists can appear in both header and body, so we do not modify parsing_header here.

        // Start a StatementList node
        self.builder.start_node(SyntaxKind::StatementList.to_raw());

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

            // Try control flow statements first
            if self.is_control_flow_keyword() {
                self.parse_control_flow_statement();
                continue;
            }

            // Try built-in library statements
            if self.is_library_statement_keyword() {
                self.parse_library_statement();
                continue;
            }

            // Try array statements
            if self.is_variable_declaration_keyword() {
                self.parse_array_statement();
                continue;
            }

            // Try to parse a statement using the centralized dispatcher
            if self.is_statement_keyword() {
                self.parse_statement();
                continue;
            }

            // Handle other constructs that aren't in parse_statement
            match self.current_token() {
                // Whitespace, newlines, and comments - consume directly FIRST
                // This must be checked before is_at_procedure_call to avoid
                // treating REM comments as procedure calls
                Some(
                    Token::Whitespace
                    | Token::Newline
                    | Token::EndOfLineComment
                    | Token::RemComment,
                ) => {
                    self.consume_token();
                }
                // Variable declarations: Dim/Private/Public/Const/Static
                Some(
                    Token::DimKeyword
                    | Token::PrivateKeyword
                    | Token::PublicKeyword
                    | Token::ConstKeyword
                    | Token::StaticKeyword,
                ) => {
                    self.parse_dim();
                }
                // Anything else - check if it's a label, assignment, procedure call, or unknown
                _ => {
                    // Check if this is a label (identifier followed by colon)
                    if self.is_at_label() {
                        self.parse_label_statement();
                    // Check for Let statement (optional assignment keyword)
                    } else if self.at_token(Token::LetKeyword)
                        || (self.at_token(Token::Whitespace)
                            && self.peek_next_keyword() == Some(Token::LetKeyword))
                    {
                        self.parse_let_statement();
                    // Check if this looks like an assignment statement (identifier = expression)
                    } else if self.is_at_assignment() {
                        self.parse_assignment_statement();
                    // Check if this looks like a procedure call (identifier without assignment)
                    } else if self.is_at_procedure_call() {
                        self.parse_procedure_call();
                    } else {
                        self.consume_token_as_unknown();
                    }
                }
            }
        }
        self.builder.finish_node(); // StatementList
    }
}

#[cfg(test)]
mod tests {
    use super::Parser;
    use crate::parsers::cst::{
        ControlKind, Creatable, Exposed, FormRoot, NameSpace, ObjectReference, PreDeclaredID,
    };
    use crate::*;

    use assert_matches::assert_matches;

    #[test]
    fn parse_single_quote_comment() {
        let code = "' This is a comment\nSub Main()\n";

        let mut source_stream = SourceStream::new("test.bas", code);
        let result = tokenize(&mut source_stream);
        let (token_stream_opt, _failures) = result.unpack();

        let token_stream = token_stream_opt.expect("Tokenization failed");
        let cst = parse(token_stream);

        assert_eq!(cst.root_kind(), SyntaxKind::Root);
        // Should have 2 children: the comment and the SubStatement
        assert_eq!(cst.child_count(), 3); // 2 statements + EOF
        assert!(cst.text().contains("' This is a comment"));
        assert!(cst.text().contains("Sub Main()"));

        // Use navigation methods
        assert!(cst.contains_kind(SyntaxKind::EndOfLineComment));
        assert!(cst.contains_kind(SyntaxKind::SubStatement));

        let first = cst.first_child().expect("Expected first child");
        assert_eq!(first.kind(), SyntaxKind::EndOfLineComment);
        assert!(first.is_token());
    }

    #[test]
    fn syntax_kind_conversions() {
        // Test keyword conversions
        assert_eq!(
            SyntaxKind::from(Token::FunctionKeyword),
            SyntaxKind::FunctionKeyword
        );
        assert_eq!(SyntaxKind::from(Token::IfKeyword), SyntaxKind::IfKeyword);
        assert_eq!(SyntaxKind::from(Token::ForKeyword), SyntaxKind::ForKeyword);

        // Test operators
        assert_eq!(
            SyntaxKind::from(Token::AdditionOperator),
            SyntaxKind::AdditionOperator
        );
        assert_eq!(
            SyntaxKind::from(Token::EqualityOperator),
            SyntaxKind::EqualityOperator
        );

        // Test literals
        assert_eq!(
            SyntaxKind::from(Token::StringLiteral),
            SyntaxKind::StringLiteral
        );
        assert_eq!(
            SyntaxKind::from(Token::IntegerLiteral),
            SyntaxKind::IntegerLiteral
        );
        assert_eq!(
            SyntaxKind::from(Token::LongLiteral),
            SyntaxKind::LongLiteral
        );
        assert_eq!(
            SyntaxKind::from(Token::SingleLiteral),
            SyntaxKind::SingleLiteral
        );
        assert_eq!(
            SyntaxKind::from(Token::DoubleLiteral),
            SyntaxKind::DoubleLiteral
        );
        assert_eq!(
            SyntaxKind::from(Token::DateTimeLiteral),
            SyntaxKind::DateLiteral
        );
    }

    #[test]
    fn parse_empty_stream() {
        let source = "";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("Failed to parse source");

        assert_eq!(cst.root_kind(), SyntaxKind::Root);
        assert_eq!(cst.child_count(), 0);
    }

    #[test]
    fn parse_rem_comment() {
        let source = "REM This is a REM comment\nSub Test()\nEnd Sub\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("Failed to parse source");

        assert_eq!(cst.root_kind(), SyntaxKind::Root);
        // Should have 2 children: the REM comment and the SubStatement
        assert_eq!(cst.child_count(), 3); // 2 statements + EOF
        assert!(cst.text().contains("REM This is a REM comment"));
        assert!(cst.text().contains("Sub Test()"));

        // Verify REM comment is preserved
        let debug = cst.debug_tree();
        assert!(debug.contains("RemComment"));
    }

    #[test]
    fn parse_mixed_comments() {
        let source = "' Single quote comment\nREM REM comment\nSub Test()\nEnd Sub\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("Failed to parse source");

        assert_eq!(cst.root_kind(), SyntaxKind::Root);
        // Should have 5 children: EndOfLineComment, Newline, RemComment, Newline, SubStatement
        assert_eq!(cst.child_count(), 5);
        assert!(cst.text().contains("' Single quote comment"));
        assert!(cst.text().contains("REM REM comment"));

        // Use navigation methods
        let children = cst.children();
        assert_eq!(children[0].kind(), SyntaxKind::EndOfLineComment);
        assert_eq!(children[1].kind(), SyntaxKind::Newline);
        assert_eq!(children[2].kind(), SyntaxKind::RemComment);
        assert_eq!(children[3].kind(), SyntaxKind::Newline);
        assert_eq!(children[4].kind(), SyntaxKind::SubStatement);

        assert!(cst.contains_kind(SyntaxKind::EndOfLineComment));
        assert!(cst.contains_kind(SyntaxKind::RemComment));
    }

    #[test]
    fn cst_with_comments() {
        let source = "' This is a comment\nSub Main()\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("Failed to parse source");

        // Now has 3 children: comment token, newline token, SubStatement
        assert_eq!(cst.child_count(), 3);
        assert!(cst.text().contains("' This is a comment"));
        assert!(cst.text().contains("Sub Main()"));
    }

    #[test]
    fn cst_serializable_tree() {
        let source = "Sub Test()\nEnd Sub\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("Failed to parse source");

        // Convert to serializable format
        let serializable = cst.to_serializable();

        // Verify structure
        assert_eq!(serializable.root.kind(), SyntaxKind::Root);
        assert!(!serializable.root.is_token());
        assert_eq!(serializable.root.children().len(), 1);
        assert_eq!(
            serializable.root.children()[0].kind(),
            SyntaxKind::SubStatement
        );

        // Can be used with insta for snapshot testing:
        // insta::assert_yaml_snapshot!(serializable);
    }

    #[test]
    fn cst_serializable_with_insta() {
        let source = "Dim x As Integer\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("Failed to parse source");
        let serializable = cst.to_serializable();

        // Example of using with insta (commented out to not create snapshot files in normal test runs)
        // insta::assert_yaml_snapshot!(serializable);

        // Verify it's serializable by checking structure
        assert!(!serializable.root.children().is_empty());
    }

    #[test]
    fn parser_mode_full_cst_default() {
        let source = "Sub Test()\nEnd Sub\n";
        let mut stream = SourceStream::new("test.bas".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");

        let parser = Parser::new(token_stream);
        // Verify parser was created successfully
        assert_eq!(parser.pos, 0);
    }

    #[test]
    fn parser_mode_direct_extraction() {
        let source = "Sub Test()\nEnd Sub\n";
        let mut stream = SourceStream::new("test.bas".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let parser = Parser::new_direct_extraction(tokens, 0);
        assert_eq!(parser.pos, 0);
    }

    #[test]
    fn parser_constructors_preserve_tokens() {
        let source = "VERSION 5.00\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");

        let tokens_vec = token_stream.into_tokens();
        let token_count = tokens_vec.len();

        let parser = Parser::new_direct_extraction(tokens_vec, 0);
        assert_eq!(parser.tokens.len(), token_count);
        assert!(parser.tokens[0].1 == Token::VersionKeyword);
    }

    #[test]
    fn parser_new_with_position() {
        let source = "VERSION 5.00\nSub Test()\nEnd Sub\n";
        let mut stream = SourceStream::new("test.bas".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        // Create parser starting at position 3 (after VERSION keyword, whitespace, and version number)
        let parser = Parser::new_direct_extraction(tokens, 3);
        assert_eq!(parser.pos, 3);
    }

    #[test]
    fn parse_version_direct_with_version() {
        let source = "VERSION 5.00\nSub Test()\nEnd Sub\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (version_opt, failures) = parser.parse_version_direct().unpack();

        assert!(version_opt.is_some());
        let version = version_opt.expect("Expected version to be parsed");
        assert_eq!(version.major, 5);
        assert_eq!(version.minor, 0);
        assert!(failures.is_empty());
    }

    #[test]
    fn parse_version_direct_without_version() {
        let source = "Sub Test()\nEnd Sub\n";
        let mut stream = SourceStream::new("test.bas".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (version_opt, failures) = parser.parse_version_direct().unpack();

        assert!(version_opt.is_none());
        assert!(failures.is_empty());
    }

    #[test]
    fn parse_version_direct_with_class_keyword() {
        let source = "VERSION 1.0 CLASS\nSub Test()\nEnd Sub\n";
        let mut stream = SourceStream::new("test.cls".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (version_opt, failures) = parser.parse_version_direct().unpack();

        assert!(version_opt.is_some());
        let version = version_opt.expect("Expected version to be parsed");
        assert_eq!(version.major, 1);
        assert_eq!(version.minor, 0);
        assert!(failures.is_empty());
    }

    #[test]
    fn parse_version_direct_version_100() {
        let source = "VERSION 1.00\n";
        let mut stream = SourceStream::new("test.cls".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (version_opt, _failures) = parser.parse_version_direct().unpack();

        assert!(version_opt.is_some());
        let version = version_opt.expect("Expected version to be parsed");
        assert_eq!(version.major, 1);
        assert_eq!(version.minor, 0);
    }

    #[test]
    fn parse_version_direct_with_whitespace() {
        let source = "  VERSION   5.00  \nSub Test()\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (version_opt, _failures) = parser.parse_version_direct().unpack();

        assert!(version_opt.is_some());
        let version = version_opt.expect("Expected version to be parsed");
        assert_eq!(version.major, 5);
        assert_eq!(version.minor, 0);
    }

    #[test]
    fn parse_version_direct_position_advances() {
        let source = "VERSION 5.00\nBegin VB.Form Form1\nEnd\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let initial_pos = parser.pos;
        let _result = parser.parse_version_direct();

        // Position should have advanced past VERSION statement
        assert!(parser.pos > initial_pos);

        // Should now be positioned at Begin keyword
        assert_eq!(parser.current_token(), Some(&Token::BeginKeyword));
    }

    #[test]
    fn parse_version_direct_accuracy() {
        let test_cases = vec![
            ("VERSION 5.00\n", Some((5, 0))),
            ("VERSION 1.0\n", Some((1, 0))),
            ("VERSION 6.00 CLASS\n", Some((6, 0))),
            ("VERSION 4.00\n", Some((4, 0))),
            ("Sub Test()\n", None), // No VERSION
        ];

        for (source, expected) in test_cases {
            let mut stream = SourceStream::new("test.vb".to_string(), source);
            let (token_stream_opt, _) = tokenize(&mut stream).unpack();
            let token_stream = token_stream_opt.expect("Tokenization failed");
            let tokens = token_stream.into_tokens();

            let mut parser = Parser::new_direct_extraction(tokens, 0);
            let (version_opt, _failures) = parser.parse_version_direct().unpack();

            match expected {
                Some((major, minor)) => {
                    assert!(version_opt.is_some(), "Expected version for: {source}");
                    let version = version_opt.expect("Expected version to be parsed");
                    assert_eq!(version.major, major, "Major mismatch for: {source}");
                    assert_eq!(version.minor, minor, "Minor mismatch for: {source}");
                }
                None => {
                    assert!(version_opt.is_none(), "Expected no version for: {source}");
                }
            }
        }
    }

    #[test]
    fn parse_control_type_direct_simple() {
        let source = "VB.Form Form1\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let control_type = parser.parse_control_type_direct();

        assert_eq!(control_type, "VB.Form");
    }

    #[test]
    fn parse_control_name_direct_simple() {
        let source = "Form1 \n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let control_name = parser.parse_control_name_direct();

        assert_eq!(control_name, "Form1");
    }

    #[test]
    fn parse_property_direct_simple() {
        let source = "Caption = \"Hello World\"\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let property = parser.parse_property_direct();

        assert!(property.is_some());
        let (key, value) = property.expect("Expected property to be parsed");
        assert_eq!(key, "Caption");
        assert_eq!(value, "\"Hello World\"");
    }

    #[test]
    fn parse_properties_block_to_control_simple_form() {
        let source = r#"Begin VB.Form Form1
   Caption = "Test Form"
   ClientHeight = 3000
   ClientWidth = 4000
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, failures) = parser.parse_properties_block_to_form_root().unpack();

        assert!(failures.is_empty(), "Expected no failures");
        assert!(control_opt.is_some(), "Expected form root to be parsed");

        let form_root = control_opt.expect("Expected form root to be parsed");
        assert_eq!(form_root.name(), "Form1");

        // Verify it's a Form
        assert!(form_root.is_form());
    }

    #[test]
    fn parse_properties_block_to_control_command_button() {
        let source = r#"Begin VB.CommandButton Command1
   Caption = "Click Me"
   Height = 495
   Width = 1215
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, failures) = parser.parse_properties_block_to_control().unpack();

        assert!(failures.is_empty());
        assert!(control_opt.is_some());

        let control = control_opt.expect("Expected control to be parsed");
        assert_eq!(control.name(), "Command1");
        assert_matches!(control.kind(), ControlKind::CommandButton { .. });
    }

    #[test]
    fn parse_properties_block_to_control_textbox() {
        let source = r#"Begin VB.TextBox Text1
   Text = "Initial Text"
   Height = 300
   Width = 2000
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, _failures) = parser.parse_properties_block_to_control().unpack();

        assert!(control_opt.is_some());
        let control = control_opt.expect("Expected control to be parsed");
        assert_eq!(control.name(), "Text1");
        assert_matches!(control.kind(), ControlKind::TextBox { .. });
    }

    #[test]
    fn parse_properties_block_without_begin() {
        let source = "Caption = \"Test\"\nEnd\n";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, _failures) = parser.parse_properties_block_to_control().unpack();

        // Should return None when BEGIN is missing
        assert!(control_opt.is_none());
    }

    #[test]
    fn parse_form_with_nested_control() {
        let source = r#"Begin VB.Form Form1
   Caption = "Main Form"
   Begin VB.CommandButton Command1
      Caption = "Click Me"
      Height = 400
   End
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (form_root_opt, failures) = parser.parse_properties_block_to_form_root().unpack();

        assert!(failures.is_empty(), "Should have no failures");
        assert!(form_root_opt.is_some());
        let form_root = form_root_opt.expect("Expected form root to be parsed");
        assert_eq!(form_root.name(), "Form1");

        // Check form has child controls
        if let FormRoot::Form(form) = &form_root {
            assert_eq!(form.controls.len(), 1);
            assert_eq!(form.controls[0].name(), "Command1");
            assert_matches!(form.controls[0].kind(), ControlKind::CommandButton { .. });
        } else {
            panic!("Expected Form");
        }
    }

    #[test]
    fn parse_frame_with_multiple_nested_controls() {
        let source = r#"Begin VB.Frame Frame1
   Caption = "Options"
   Begin VB.CheckBox Check1
      Caption = "Option 1"
   End
   Begin VB.CheckBox Check2
      Caption = "Option 2"
   End
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, failures) = parser.parse_properties_block_to_control().unpack();

        assert!(failures.is_empty());
        assert!(control_opt.is_some());
        let control = control_opt.expect("Expected control to be parsed");
        assert_eq!(control.name(), "Frame1");

        // Check frame has 2 child checkboxes
        if let ControlKind::Frame { controls, .. } = control.kind() {
            assert_eq!(controls.len(), 2);
            assert_eq!(controls[0].name(), "Check1");
            assert_eq!(controls[1].name(), "Check2");
        } else {
            panic!("Expected Frame control kind");
        }
    }

    #[test]
    fn parse_control_with_property_group() {
        let source = r#"Begin VB.CommandButton Command1
   Caption = "Button"
   BeginProperty Font
      Name = "Arial"
      Size = 12
   EndProperty
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, failures) = parser.parse_properties_block_to_control().unpack();

        assert!(failures.is_empty());
        assert!(control_opt.is_some());
        let control = control_opt.expect("Expected control to be parsed");
        assert_eq!(control.name(), "Command1");

        // Check for property - CommandButton should have parsed successfully
        // Property groups are stored in Custom control kind, not specific control types
        assert_matches!(control.kind(), ControlKind::CommandButton { .. });
    }

    #[test]
    fn parse_custom_control_with_property_group() {
        let source = r#"Begin MSComctlLib.TreeView TreeView1
   BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
      Name = "MS Sans Serif"
      Size = 8.25
      Charset = 0
   EndProperty
   Caption = "Tree"
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, failures) = parser.parse_properties_block_to_control().unpack();

        assert!(failures.is_empty());
        assert!(control_opt.is_some());
        let control = control_opt.expect("Expected control to be parsed");
        assert_eq!(control.name(), "TreeView1");

        // Check for Custom control with property groups
        if let ControlKind::Custom {
            property_groups, ..
        } = control.kind()
        {
            assert_eq!(property_groups.len(), 1);
            assert_eq!(property_groups[0].name, "Font");
            assert!(property_groups[0].guid.is_some());
        } else {
            panic!("Expected Custom control kind");
        }
    }

    #[test]
    fn parse_simple_object_statement() {
        let source = r#"Object = "{12345678-1234-1234-1234-123456789ABC}#1.0#0"; "MyLib.dll""#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let objects = parser.parse_objects_direct();

        assert_eq!(objects.len(), 1);
        match &objects[0] {
            ObjectReference::Compiled {
                uuid,
                version,
                unknown1,
                file_name,
            } => {
                assert_eq!(
                    uuid.to_string().to_uppercase(),
                    "12345678-1234-1234-1234-123456789ABC"
                );
                assert_eq!(version, "1.0");
                assert_eq!(unknown1, "0");
                assert_eq!(file_name, "MyLib.dll");
            }
            ObjectReference::Project { .. } => {
                panic!("Expected Compiled object reference")
            }
        }
    }

    #[test]
    fn parse_multiple_object_statements() {
        let source = r#"Object = "{AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA}#1.0#0"; "Lib1.dll"
Object = "{BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB}#2.0#1"; "Lib2.ocx"
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let objects = parser.parse_objects_direct();

        assert_eq!(objects.len(), 2);

        match &objects[0] {
            ObjectReference::Compiled { file_name, .. } => {
                assert_eq!(file_name, "Lib1.dll");
            }
            ObjectReference::Project { .. } => {
                panic!("Expected Compiled object reference")
            }
        }

        match &objects[1] {
            ObjectReference::Compiled { file_name, .. } => {
                assert_eq!(file_name, "Lib2.ocx");
            }
            ObjectReference::Project { .. } => {
                panic!("Expected Compiled object reference")
            }
        }
    }

    #[test]
    fn parse_embedded_object_statement() {
        let source = r#"Object = *\G{87654321-4321-4321-4321-CBA987654321}#3.0#5; "Embedded.ocx""#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let objects = parser.parse_objects_direct();

        assert_eq!(objects.len(), 1);
        match &objects[0] {
            ObjectReference::Compiled {
                uuid,
                version,
                file_name,
                ..
            } => {
                assert_eq!(
                    uuid.to_string().to_uppercase(),
                    "87654321-4321-4321-4321-CBA987654321"
                );
                assert_eq!(version, "3.0");
                assert_eq!(file_name, "Embedded.ocx");
            }
            ObjectReference::Project { .. } => {
                panic!("Expected Compiled object reference")
            }
        }
    }

    #[test]
    fn parse_nested_property_groups() {
        use either::Either;

        let source = r#"Begin Custom.Control Ctrl1
   BeginProperty Outer
      Value1 = "Test"
      BeginProperty Inner
         Value2 = "Nested"
      EndProperty
   EndProperty
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (control_opt, failures) = parser.parse_properties_block_to_control().unpack();

        assert!(failures.is_empty());
        assert!(control_opt.is_some());
        let control = control_opt.expect("Expected control to be parsed");

        // Check for nested property groups
        if let ControlKind::Custom {
            property_groups, ..
        } = control.kind()
        {
            assert_eq!(property_groups.len(), 1);
            assert_eq!(property_groups[0].name, "Outer");

            // Check for nested group

            if let Some(Either::Right(inner)) = property_groups[0].properties.get("Inner") {
                assert_eq!(inner.name, "Inner");
            } else {
                panic!("Expected nested Inner property group");
            }
        } else {
            panic!("Expected Custom control kind");
        }
    }

    #[test]
    fn parse_deeply_nested_controls() {
        let source = r#"Begin VB.Form Form1
   Caption = "Outer"
   Begin VB.PictureBox Picture1
      Begin VB.Frame Frame1
         Begin VB.Label Label1
            Caption = "Deep"
         End
      End
   End
End
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let (form_root_opt, failures) = parser.parse_properties_block_to_form_root().unpack();

        assert!(failures.is_empty());
        assert!(form_root_opt.is_some());
        let form_root = form_root_opt.expect("Expected form root to be parsed");

        // Verify deep nesting: Form > PictureBox > Frame > Label
        if let FormRoot::Form(form) = &form_root {
            assert_eq!(form.controls.len(), 1);
            if let ControlKind::PictureBox { controls, .. } = form.controls[0].kind() {
                assert_eq!(controls.len(), 1);
                if let ControlKind::Frame { controls, .. } = controls[0].kind() {
                    assert_eq!(controls.len(), 1);
                    assert_eq!(controls[0].name(), "Label1");
                } else {
                    panic!("Expected Frame");
                }
            } else {
                panic!("Expected PictureBox");
            }
        } else {
            panic!("Expected Form");
        }
    }

    #[test]
    fn parse_simple_string_attribute() {
        let source = r#"Attribute VB_Name = "Form1"
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let attrs = parser.parse_attributes_direct();

        assert_eq!(attrs.name, "Form1");
        assert_eq!(attrs.global_name_space, NameSpace::Local);
        assert_eq!(attrs.creatable, Creatable::True);
        assert_eq!(attrs.predeclared_id, PreDeclaredID::False);
        assert_eq!(attrs.exposed, Exposed::False);
        assert_eq!(attrs.description, None);
    }

    #[test]
    fn parse_boolean_attributes() {
        let source = r"Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let attrs = parser.parse_attributes_direct();

        assert_eq!(attrs.global_name_space, NameSpace::Local);
        assert_eq!(attrs.creatable, Creatable::True);
        assert_eq!(attrs.predeclared_id, PreDeclaredID::True);
        assert_eq!(attrs.exposed, Exposed::False);
    }

    #[test]
    fn parse_numeric_attribute() {
        let source = r"Attribute VB_PredeclaredId = -1
";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let attrs = parser.parse_attributes_direct();

        // -1 is truthy in VB6, so should be parsed as true
        assert_eq!(attrs.predeclared_id, PreDeclaredID::True);
    }

    #[test]
    fn parse_multiple_attributes() {
        let source = r#"Attribute VB_Name = "MyForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Attribute VB_Description = "This is a test form"
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let attrs = parser.parse_attributes_direct();

        assert_eq!(attrs.name, "MyForm");
        assert_eq!(attrs.global_name_space, NameSpace::Local);
        assert_eq!(attrs.creatable, Creatable::False);
        assert_eq!(attrs.predeclared_id, PreDeclaredID::True);
        assert_eq!(attrs.exposed, Exposed::False);
        assert_eq!(attrs.description, Some("This is a test form".to_string()));
    }

    #[test]
    fn parse_ext_key_attributes() {
        let source = r#"Attribute VB_Name = "Form1"
Attribute VB_Ext_KEY = "CustomKey" ,"CustomValue"
Attribute VB_Description = "Test"
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let attrs = parser.parse_attributes_direct();

        assert_eq!(attrs.name, "Form1");
        assert_eq!(attrs.description, Some("Test".to_string()));
        assert_eq!(attrs.ext_key.len(), 1);
        assert!(attrs.ext_key.contains_key("VB_Ext_KEY"));
    }

    #[test]
    fn parse_empty_attributes() {
        let source = r"";
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let attrs = parser.parse_attributes_direct();

        assert_eq!(attrs.name, "");
        assert_eq!(attrs.global_name_space, NameSpace::Local);
        assert_eq!(attrs.creatable, Creatable::True);
        assert_eq!(attrs.predeclared_id, PreDeclaredID::False);
        assert_eq!(attrs.exposed, Exposed::False);
        assert_eq!(attrs.description, None);
        assert!(attrs.ext_key.is_empty());
    }

    #[test]
    fn parse_resource_reference_property() {
        let source = r#"Caption = $"Gradient.frx":0000
"#;
        let mut stream = SourceStream::new("test.frm".to_string(), source);
        let (token_stream_opt, _) = tokenize(&mut stream).unpack();
        let token_stream = token_stream_opt.expect("Tokenization failed");
        let tokens = token_stream.into_tokens();

        let mut parser = Parser::new_direct_extraction(tokens, 0);
        let property = parser.parse_property_direct();

        assert!(property.is_some());
        let (key, value) = property.expect("Expected property to be parsed");
        assert_eq!(key, "Caption");
        assert_eq!(value, r#"$"Gradient.frx":0000"#);
    }
}