vb6parse 1.2.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
//! Expression parsing for VB6 CST.
//!
//! This module implements expression parsing for Visual Basic 6 using a Pratt parsing
//! approach (also known as operator precedence parsing or precedence climbing). This
//! technique cleanly handles operator precedence and associativity while maintaining
//! a simple recursive descent structure.
//!
//! # VB6 Expression Types
//!
//! VB6 supports various expression types:
//!
//! - **Literal expressions**: Numbers, strings, dates, `True`, `False`, `Nothing`, `Null`, `Empty`
//! - **Identifier expressions**: Variable names, constants
//! - **Unary expressions**: `-x`, `Not x`, `AddressOf proc`
//! - **Binary expressions**: Arithmetic, comparison, logical operations
//! - **Member access**: `object.property`, `object.method`
//! - **Function calls**: `Function(arg1, arg2)`, `Function arg1, arg2`
//! - **Array indexing**: `array(index)`, `array(i, j)`
//! - **Parenthesized**: `(expression)`
//! - **Object creation**: `New ClassName`
//! - **Type operations**: `TypeOf object Is type`
//!
//! # Operator Precedence
//!
//! VB6 operators are parsed according to the following precedence levels (highest to lowest):
//!
//! 1. Member access (`.`), function calls `()`
//! 2. Exponentiation (`^`) - right-associative
//! 3. Unary negation (`-`)
//! 4. Multiplication (`*`), division (`/`)
//! 5. Integer division (`\`)
//! 6. Modulo (`Mod`)
//! 7. Addition (`+`), subtraction (`-`)
//! 8. String concatenation (`&`)
//! 9. Comparison (`=`, `<>`, `<`, `>`, `<=`, `>=`, `Like`, `Is`)
//! 10. Logical `Not`
//! 11. Logical `And`
//! 12. Logical `Or`
//! 13. Logical `Xor`
//! 14. Logical `Eqv`
//! 15. Logical `Imp`
//!
//! # Pratt Parsing
//!
//! The implementation uses Pratt parsing, which associates a binding power (precedence level)
//! with each operator. The parser works by:
//!
//! 1. Parsing a prefix expression (literal, identifier, unary operator, etc.)
//! 2. Looking at the next operator and comparing its binding power to the current minimum
//! 3. If the operator's binding power is higher, it binds tighter and is parsed as an infix operation
//! 4. This continues recursively until an operator with lower binding power is encountered
//!
//! This approach naturally handles precedence and associativity without complex lookahead
//! or multiple parsing passes.
//!
//! # Naming Conventions
//!
//! Expression parsing follows parser-wide naming conventions:
//!
//! - `parse_*` methods parse grammar/token constructs.
//! - `handle_*_frame` methods advance explicit frame states in the iterative parser loop.
//! - `try_*` methods probe a branch and return whether it matched.
//!
//! # Examples
//!
//! ```vb6
//! ' Arithmetic with proper precedence
//! result = 2 + 3 * 4        ' Parsed as: 2 + (3 * 4)
//! result = 10 - 5 - 2       ' Parsed as: (10 - 5) - 2
//! result = 2 ^ 3 ^ 2        ' Parsed as: 2 ^ (3 ^ 2) - right associative
//!
//! ' Logical operations
//! condition = x > 5 And y < 10       ' Parsed as: (x > 5) And (y < 10)
//! condition = Not flag1 Or flag2     ' Parsed as: (Not flag1) Or flag2
//!
//! ' Member access and calls
//! value = obj.property.method(arg1, arg2)
//!
//! ' Complex expressions
//! result = (a + b) * c - d / e Mod f
//! ```

use crate::language::Token;
use crate::parsers::SyntaxKind;
use crate::parsers::cst::Parser;
use rowan::Checkpoint;

/// Frame for iterative expression parsing.
/// Tracks the state of expression parsing to eliminate recursion.
#[derive(Debug, Clone, Copy)]
enum ExprParseFrame {
    /// Parse a prefix expression and start the infix loop
    ParsePrefix {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Finish processing infix operators after parsing RHS
    InfixLoop {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Finish a binary expression node
    FinishBinary {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Finish a unary expression node and continue with outer infix loop
    FinishUnary {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Finish a `TypeOf` expression node and continue with outer infix loop
    FinishTypeOf {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Finish a parenthesized expression node and continue with outer infix loop
    FinishParenthesized {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Check for and handle postfix operators (., (, !)
    ParsePostfix {
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    },
    /// Start parsing an argument list after consuming '('
    StartArgumentList {
        min_bp: BindingPower,
        call_checkpoint: Checkpoint,
    },
    /// After finishing an argument, check for comma or close paren
    NextArgument {
        min_bp: BindingPower,
        call_checkpoint: Checkpoint,
    },
}

/// Operator binding power (precedence) levels.
///
/// Higher values indicate tighter binding (higher precedence).
/// These values are based on the VB6 language specification and determine
/// the order in which operators are applied when parsing expressions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct BindingPower(u8);

impl BindingPower {
    /// No binding power - used as a minimum baseline
    pub(super) const NONE: BindingPower = BindingPower(0);

    /// Logical implication operator (`Imp`) - lowest precedence
    pub(super) const IMP: BindingPower = BindingPower(10);

    /// Logical equivalence operator (`Eqv`)
    pub(super) const EQV: BindingPower = BindingPower(20);

    /// Logical exclusive or operator (`Xor`)
    pub(super) const XOR: BindingPower = BindingPower(30);

    /// Logical or operator (`Or`)
    pub(super) const OR: BindingPower = BindingPower(40);

    /// Logical and operator (`And`)
    pub(super) const AND: BindingPower = BindingPower(50);

    /// Logical not operator (`Not`) - prefix operator
    pub(super) const NOT: BindingPower = BindingPower(60);

    /// Comparison operators (`=`, `<>`, `<`, `>`, `<=`, `>=`, `Like`, `Is`)
    pub(super) const COMPARISON: BindingPower = BindingPower(70);

    /// String concatenation operator (`&`)
    pub(super) const CONCATENATION: BindingPower = BindingPower(80);

    /// Addition and subtraction operators (`+`, `-`)
    pub(super) const ADDITION: BindingPower = BindingPower(90);

    /// Modulo operator (`Mod`)
    pub(super) const MODULO: BindingPower = BindingPower(100);

    /// Integer division operator (`\`)
    pub(super) const INT_DIVISION: BindingPower = BindingPower(110);

    /// Multiplication and division operators (`*`, `/`)
    pub(super) const MULTIPLICATION: BindingPower = BindingPower(120);

    /// Unary operators (unary `-`, `AddressOf`) - prefix operators
    pub(super) const UNARY: BindingPower = BindingPower(130);

    /// Exponentiation operator (`^`) - right-associative
    pub(super) const EXPONENTIATION: BindingPower = BindingPower(140);

    // Function/method calls and array indexing
    //pub(super) const CALL: BindingPower = BindingPower(150);

    // Member access operator (`.`) - highest precedence
    //pub(super) const MEMBER: BindingPower = BindingPower(160);
}

impl Parser<'_> {
    /// Parse an expression starting with no minimum binding power.
    ///
    /// This is the main entry point for expression parsing. It delegates to
    /// [`parse_expression_with_binding_power`](Self::parse_expression_with_binding_power)
    /// with a minimum binding power of zero.
    ///
    /// # Examples
    ///
    /// ```vb6
    /// x = 5 + 3 * 2          ' Simple arithmetic
    /// y = obj.method(arg)    ' Member access and call
    /// z = (a + b) * c        ' Parenthesized expression
    /// ```
    pub(crate) fn parse_expression(&mut self) {
        self.parse_expression_with_binding_power(BindingPower::NONE);
    }

    /// Parse an lvalue (left-hand side of assignment).
    ///
    /// This parses expressions but stops before the `=` operator,
    /// since `=` in VB6 can be both assignment and comparison.
    ///
    /// # Examples
    ///
    /// ```vb6
    /// x = 5                  ' x is the lvalue
    /// obj.property = value   ' obj.property is the lvalue
    /// arr(i) = 10           ' arr(i) is the lvalue
    /// ```
    pub(crate) fn parse_lvalue(&mut self) {
        // Parse with a minimum binding power HIGHER than COMPARISON
        // This ensures = is not treated as a binary operator
        // COMPARISON is 70, so we use 75 to exclude it
        self.parse_expression_with_binding_power(BindingPower(75));
    }

    /// Parse an expression with a minimum binding power.
    ///
    /// This is the core of the Pratt parser, implemented iteratively to prevent
    /// stack overflow on deeply nested expressions.
    ///
    /// # Parameters
    ///
    /// - `min_bp`: The minimum binding power required for an operator to be parsed.
    ///   Operators with lower binding power will end the current expression.
    ///
    /// # Implementation Note
    ///
    /// This uses an explicit frame stack instead of recursion to handle arbitrary
    /// nesting depth without stack overflow.
    pub(crate) fn parse_expression_with_binding_power(&mut self, min_bp: BindingPower) {
        // Use iterative approach with explicit stack
        // This prevents stack overflow on deeply nested expressions

        let mut frame_stack: Vec<ExprParseFrame> = Vec::new();

        // Start with the initial frame
        self.consume_whitespace();
        let initial_checkpoint = self.builder.checkpoint();
        frame_stack.push(ExprParseFrame::ParsePrefix {
            min_bp,
            lhs_checkpoint: initial_checkpoint,
        });

        while let Some(frame) = frame_stack.pop() {
            self.dispatch_expression_frame(frame, &mut frame_stack);
        }
    }

    fn dispatch_expression_frame(
        &mut self,
        frame: ExprParseFrame,
        frame_stack: &mut Vec<ExprParseFrame>,
    ) {
        match frame {
            ExprParseFrame::ParsePrefix {
                min_bp,
                lhs_checkpoint,
            } => self.handle_prefix_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::InfixLoop {
                min_bp,
                lhs_checkpoint,
            } => self.handle_infix_loop_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::FinishBinary {
                min_bp,
                lhs_checkpoint,
            } => self.handle_finish_binary_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::FinishTypeOf {
                min_bp,
                lhs_checkpoint,
            } => self.handle_finish_typeof_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::FinishUnary {
                min_bp,
                lhs_checkpoint,
            } => self.handle_finish_unary_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::FinishParenthesized {
                min_bp,
                lhs_checkpoint,
            } => self.handle_finish_parenthesized_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::ParsePostfix {
                min_bp,
                lhs_checkpoint,
            } => self.handle_postfix_frame(frame_stack, min_bp, lhs_checkpoint),
            ExprParseFrame::StartArgumentList {
                min_bp,
                call_checkpoint,
            } => self.handle_argument_list_start_frame(frame_stack, min_bp, call_checkpoint),
            ExprParseFrame::NextArgument {
                min_bp,
                call_checkpoint,
            } => self.handle_argument_next_frame(frame_stack, min_bp, call_checkpoint),
        }
    }

    fn handle_prefix_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        // Parse a prefix expression using explicit parser frames.
        let (pushes_frames, prefix_checkpoint) =
            self.parse_prefix_expression_frame(frame_stack, min_bp, lhs_checkpoint);

        // Only push postfix/infix if no frames were pushed
        // If frames were pushed, those frames will handle the continuation
        if !pushes_frames {
            // First handle postfix operators (., (, !) then infix
            frame_stack.push(ExprParseFrame::ParsePostfix {
                min_bp,
                lhs_checkpoint: prefix_checkpoint,
            });
        }
    }

    fn handle_infix_loop_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        let Some((left_bp, right_bp)) = self.peek_infix_binding_power_after_trivia() else {
            return;
        };

        // If the operator doesn't bind tightly enough, stop
        if left_bp < min_bp {
            return;
        }

        // Now actually consume the whitespace
        self.consume_whitespace();

        // Wrap the left-hand side in a BinaryExpression
        self.builder
            .start_node_at(lhs_checkpoint, SyntaxKind::BinaryExpression.to_raw());

        // Consume the operator
        self.consume_token();

        // Skip whitespace after operator
        self.consume_whitespace();

        // Parse the right-hand side - push frames instead of recursing
        let rhs_checkpoint = self.builder.checkpoint();

        // After parsing RHS, we need to finish binary, then continue infix loop
        frame_stack.push(ExprParseFrame::FinishBinary {
            min_bp,
            lhs_checkpoint,
        });
        frame_stack.push(ExprParseFrame::ParsePrefix {
            min_bp: right_bp,
            lhs_checkpoint: rhs_checkpoint,
        });
    }

    fn handle_finish_binary_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        self.builder.finish_node();

        // Continue with infix loop to check for more operators
        frame_stack.push(ExprParseFrame::InfixLoop {
            min_bp,
            lhs_checkpoint,
        });
    }

    fn handle_finish_unary_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        self.builder.finish_node();

        // After finishing unary expression, continue with outer infix loop
        frame_stack.push(ExprParseFrame::InfixLoop {
            min_bp,
            lhs_checkpoint,
        });
    }

    fn handle_finish_typeof_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        // Operand expression has been parsed. Now consume "Is TypeName".
        self.consume_whitespace();

        if self.at_token(Token::IsKeyword) {
            self.consume_token(); // Is
        }

        self.consume_whitespace();

        // Parse the type name (identifier)
        if self.is_identifier() || self.at_keyword() {
            self.consume_token();
        }

        self.builder.finish_node(); // TypeOfExpression

        // Continue with outer infix loop
        frame_stack.push(ExprParseFrame::InfixLoop {
            min_bp,
            lhs_checkpoint,
        });
    }

    fn handle_finish_parenthesized_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        // Check for comma - in VB6, parentheses can contain comma-separated expressions
        // This is used in graphics methods like Line: Picture1.Line (x1, y1)-(x2, y2)
        self.consume_whitespace();

        if self.at_token(Token::Comma) {
            // Consume comma and any whitespace
            self.consume_token();
            self.consume_whitespace();

            // Check if there's another expression or if we hit the closing paren
            if !self.at_token(Token::RightParenthesis) && !self.is_at_end() {
                // Parse the next expression in the comma-separated list
                // Push this frame again to handle more commas after the next expression
                frame_stack.push(ExprParseFrame::FinishParenthesized {
                    min_bp,
                    lhs_checkpoint,
                });

                // Parse the next expression
                let inner_checkpoint = self.builder.checkpoint();
                frame_stack.push(ExprParseFrame::ParsePrefix {
                    min_bp: BindingPower::NONE,
                    lhs_checkpoint: inner_checkpoint,
                });
                return;
            }
        }

        // Trailing comma before closing paren or no comma - just finish
        if self.at_token(Token::RightParenthesis) {
            self.consume_token();
        }
        self.builder.finish_node();

        // After finishing parenthesized expression, continue with postfix then infix
        frame_stack.push(ExprParseFrame::ParsePostfix {
            min_bp,
            lhs_checkpoint,
        });
    }

    fn handle_argument_list_start_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        call_checkpoint: Checkpoint,
    ) {
        // Start the ArgumentList node
        self.builder.start_node(SyntaxKind::ArgumentList.to_raw());

        // Skip whitespace after opening paren
        self.consume_whitespace();

        // Check if argument list is empty
        if self.at_token(Token::RightParenthesis) {
            // Empty argument list - finish nodes and continue
            self.builder.finish_node(); // ArgumentList
            self.consume_token(); // )
            self.builder.finish_node(); // CallExpression

            // Check for more postfix operators
            frame_stack.push(ExprParseFrame::ParsePostfix {
                min_bp,
                lhs_checkpoint: call_checkpoint,
            });
            return;
        }

        // Support an empty first argument: e.g., GetObject(, "Excel.Application")
        if self.at_token(Token::Comma) {
            self.builder.start_node(SyntaxKind::Argument.to_raw());
            self.builder.finish_node(); // Argument (empty)
            self.consume_token(); // ,
            self.consume_whitespace();
            self.parse_argument_after_separator(frame_stack, min_bp, call_checkpoint);
            return;
        }

        // Parse first argument
        self.parse_argument_expression(frame_stack, min_bp, call_checkpoint);
    }

    /// Parse the argument that follows a comma separator.
    /// Handles empty arguments between separators and before the closing parenthesis.
    fn parse_argument_after_separator(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        call_checkpoint: Checkpoint,
    ) {
        loop {
            if self.at_token(Token::RightParenthesis) {
                // Trailing separator means final empty argument.
                self.builder.start_node(SyntaxKind::Argument.to_raw());
                self.builder.finish_node(); // Argument (empty)

                self.builder.finish_node(); // ArgumentList
                self.consume_token(); // )
                self.builder.finish_node(); // CallExpression

                frame_stack.push(ExprParseFrame::ParsePostfix {
                    min_bp,
                    lhs_checkpoint: call_checkpoint,
                });
                return;
            }

            if self.at_token(Token::Comma) {
                // Consecutive separators imply an empty argument.
                self.builder.start_node(SyntaxKind::Argument.to_raw());
                self.builder.finish_node(); // Argument (empty)
                self.consume_token(); // ,
                self.consume_whitespace();
                continue;
            }

            // Non-empty argument expression.
            self.parse_argument_expression(frame_stack, min_bp, call_checkpoint);
            return;
        }
    }

    /// Start parsing a non-empty argument expression.
    ///
    /// VB6 supports named arguments in calls using `name := value`.
    /// The lexer emits `:` and `=` as separate tokens, so we consume that
    /// prefix here before parsing the argument value expression.
    fn parse_argument_expression(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        call_checkpoint: Checkpoint,
    ) {
        self.builder.start_node(SyntaxKind::Argument.to_raw());

        // Consume optional named-argument prefix: `Identifier :=`
        let _ = self.try_consume_named_argument_prefix();

        frame_stack.push(ExprParseFrame::NextArgument {
            min_bp,
            call_checkpoint,
        });

        let arg_checkpoint = self.builder.checkpoint();
        frame_stack.push(ExprParseFrame::ParsePrefix {
            min_bp: BindingPower::NONE,
            lhs_checkpoint: arg_checkpoint,
        });
    }

    /// Try to consume a VB6 named-argument prefix (`name :=`) at the start
    /// of an argument. Returns true if consumed.
    fn try_consume_named_argument_prefix(&mut self) -> bool {
        let mut idx = self.pos;

        let Some((_, first_token)) = self.tokens.get(idx) else {
            return false;
        };

        if !(*first_token == Token::Identifier || first_token.is_keyword()) {
            return false;
        }

        idx += 1;
        while let Some((_, Token::Whitespace)) = self.tokens.get(idx) {
            idx += 1;
        }

        if self.tokens.get(idx).map(|(_, token)| *token) != Some(Token::ColonOperator) {
            return false;
        }

        idx += 1;
        while let Some((_, Token::Whitespace)) = self.tokens.get(idx) {
            idx += 1;
        }

        if self.tokens.get(idx).map(|(_, token)| *token) != Some(Token::EqualityOperator) {
            return false;
        }

        while self.pos <= idx {
            self.consume_token();
        }

        self.consume_whitespace();
        true
    }

    fn handle_argument_next_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        call_checkpoint: Checkpoint,
    ) {
        // Finish the Argument node
        self.builder.finish_node();

        // Skip whitespace
        self.consume_whitespace();

        // Check if there's a comma for next argument
        if self.at_token(Token::Comma) {
            self.consume_token();
            self.consume_whitespace();

            self.parse_argument_after_separator(frame_stack, min_bp, call_checkpoint);
            return;
        }

        // No more arguments - finish nodes
        self.builder.finish_node(); // ArgumentList

        // Consume closing paren if present
        if self.at_token(Token::RightParenthesis) {
            self.consume_token();
        }

        self.builder.finish_node(); // CallExpression

        // Check for more postfix operators
        frame_stack.push(ExprParseFrame::ParsePostfix {
            min_bp,
            lhs_checkpoint: call_checkpoint,
        });
    }

    fn skip_expression_trivia(&mut self) {
        loop {
            match self.current_token() {
                Some(Token::Whitespace) => {
                    self.pos += 1;
                }
                Some(Token::Underscore) => {
                    // Check for line continuation
                    let mut lookahead = 1;
                    let mut is_continuation = false;
                    while let Some((_, token)) = self.tokens.get(self.pos + lookahead) {
                        if *token == Token::Whitespace {
                            lookahead += 1;
                        } else if *token == Token::Newline {
                            is_continuation = true;
                            break;
                        } else {
                            break;
                        }
                    }

                    if is_continuation {
                        self.pos += lookahead + 1;
                    } else {
                        break;
                    }
                }
                _ => break,
            }
        }
    }

    fn has_postfix_operator_ahead(&mut self) -> bool {
        let saved_pos = self.pos;
        self.skip_expression_trivia();

        let found_postfix = matches!(
            self.current_token(),
            Some(Token::PeriodOperator | Token::LeftParenthesis | Token::ExclamationMark)
        );

        self.pos = saved_pos;
        found_postfix
    }

    fn peek_infix_binding_power_after_trivia(&mut self) -> Option<(BindingPower, BindingPower)> {
        let saved_pos = self.pos;
        self.skip_expression_trivia();

        if self.is_at_end() || self.is_at_expression_delimiter() {
            self.pos = saved_pos;
            return None;
        }

        let binding_power = self.get_infix_binding_power();
        self.pos = saved_pos;
        binding_power
    }

    fn handle_postfix_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        // Check for postfix operators: ., (, !
        let found_postfix = self.has_postfix_operator_ahead();

        if found_postfix {
            // Found postfix operator - consume whitespace and handle it
            self.consume_whitespace();

            match self.current_token() {
                Some(Token::PeriodOperator) => {
                    // Member access: .property or .method
                    self.builder
                        .start_node_at(lhs_checkpoint, SyntaxKind::MemberAccessExpression.to_raw());

                    self.parse_member_access_content();
                    self.builder.finish_node();

                    // Check for more postfix operators
                    frame_stack.push(ExprParseFrame::ParsePostfix {
                        min_bp,
                        lhs_checkpoint,
                    });
                }
                Some(Token::LeftParenthesis) => {
                    // Function call or array indexing
                    // Wrap in CallExpression and start parsing arguments
                    self.builder
                        .start_node_at(lhs_checkpoint, SyntaxKind::CallExpression.to_raw());

                    // Consume '('
                    self.consume_token();

                    // Push frames to handle argument list
                    frame_stack.push(ExprParseFrame::StartArgumentList {
                        min_bp,
                        call_checkpoint: lhs_checkpoint,
                    });
                }
                Some(Token::ExclamationMark) => {
                    // Dictionary access: collection!key
                    self.builder
                        .start_node_at(lhs_checkpoint, SyntaxKind::MemberAccessExpression.to_raw());

                    self.parse_dictionary_access_content();
                    self.builder.finish_node();

                    // Check for more postfix operators
                    frame_stack.push(ExprParseFrame::ParsePostfix {
                        min_bp,
                        lhs_checkpoint,
                    });
                }
                _ => {
                    // Shouldn't happen since we checked above
                    frame_stack.push(ExprParseFrame::InfixLoop {
                        min_bp,
                        lhs_checkpoint,
                    });
                }
            }
        } else {
            // No postfix operator - continue to infix loop
            frame_stack.push(ExprParseFrame::InfixLoop {
                min_bp,
                lhs_checkpoint,
            });
        }
    }

    fn push_unary_prefix_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
        operand_min_bp: BindingPower,
    ) {
        self.builder
            .start_node(SyntaxKind::UnaryExpression.to_raw());
        self.consume_token();
        self.consume_whitespace();

        frame_stack.push(ExprParseFrame::FinishUnary {
            min_bp,
            lhs_checkpoint,
        });

        let operand_checkpoint = self.builder.checkpoint();
        frame_stack.push(ExprParseFrame::ParsePrefix {
            min_bp: operand_min_bp,
            lhs_checkpoint: operand_checkpoint,
        });
    }

    fn push_typeof_prefix_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        self.builder
            .start_node(SyntaxKind::TypeOfExpression.to_raw());
        self.consume_token(); // TypeOf
        self.consume_whitespace();

        // Push finish frame: will consume "Is TypeName" after operand is parsed
        frame_stack.push(ExprParseFrame::FinishTypeOf {
            min_bp,
            lhs_checkpoint,
        });

        // Parse the operand expression with CONCATENATION binding power (80),
        // which is higher than COMPARISON (70), so "Is" is not consumed as an operator
        let operand_checkpoint = self.builder.checkpoint();
        frame_stack.push(ExprParseFrame::ParsePrefix {
            min_bp: BindingPower::CONCATENATION,
            lhs_checkpoint: operand_checkpoint,
        });
    }

    fn push_parenthesized_prefix_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) {
        self.builder
            .start_node(SyntaxKind::ParenthesizedExpression.to_raw());
        self.consume_token();
        self.consume_whitespace();

        frame_stack.push(ExprParseFrame::FinishParenthesized {
            min_bp,
            lhs_checkpoint,
        });

        let inner_checkpoint = self.builder.checkpoint();
        frame_stack.push(ExprParseFrame::ParsePrefix {
            min_bp: BindingPower::NONE,
            lhs_checkpoint: inner_checkpoint,
        });
    }

    fn try_push_prefix_frames(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) -> bool {
        match self.current_token() {
            // Logical NOT
            Some(Token::NotKeyword) => {
                self.push_unary_prefix_frame(
                    frame_stack,
                    min_bp,
                    lhs_checkpoint,
                    BindingPower::NOT,
                );
                true
            }
            // Argument passing modifiers used in Declare/API calls, e.g. WriteFile(..., ByVal ptr, ...)
            // or, the value is being negated which is another unary operation.
            Some(Token::ByValKeyword | Token::ByRefKeyword | Token::SubtractionOperator) => {
                self.push_unary_prefix_frame(
                    frame_stack,
                    min_bp,
                    lhs_checkpoint,
                    BindingPower::UNARY,
                );
                true
            }
            // Parenthesized expression
            Some(Token::LeftParenthesis) => {
                self.push_parenthesized_prefix_frame(frame_stack, min_bp, lhs_checkpoint);
                true
            }
            _ => false,
        }
    }

    /// Parse a prefix expression using explicit parser frames.
    ///
    /// This pushes follow-up work onto `frame_stack` instead of relying on call-stack recursion.
    /// Returns (`pushed_frames`, `checkpoint`) where:
    /// - `pushed_frames`: true if frames were pushed (meaning caller shouldn't push `ParsePostfix` yet)
    /// - `checkpoint`: the checkpoint to use for wrapping postfix operations
    fn parse_prefix_expression_frame(
        &mut self,
        frame_stack: &mut Vec<ExprParseFrame>,
        min_bp: BindingPower,
        lhs_checkpoint: Checkpoint,
    ) -> (bool, Checkpoint) {
        // Skip any leading whitespace
        self.consume_whitespace();

        // Create checkpoint at the start - this will be used for wrapping
        let checkpoint = self.builder.checkpoint();

        let mut is_identifier = false;
        if self.try_push_prefix_frames(frame_stack, min_bp, lhs_checkpoint) {
            return (true, checkpoint);
        }

        match self.current_token() {
            // AddressOf operator
            Some(Token::AddressOfKeyword) => {
                self.parse_addressof_expression();
            }
            // New operator
            Some(Token::NewKeyword) => {
                self.parse_new_expression();
            }
            // Numeric literals
            Some(
                Token::IntegerLiteral
                | Token::LongLiteral
                | Token::SingleLiteral
                | Token::DoubleLiteral
                | Token::DecimalLiteral,
            ) => {
                self.parse_numeric_literal();
            }
            Some(Token::StringLiteral) => {
                self.parse_string_literal();
            }
            Some(Token::TrueKeyword | Token::FalseKeyword) => {
                self.parse_boolean_literal();
            }
            Some(Token::NullKeyword | Token::EmptyKeyword) => {
                self.parse_special_literal();
            }
            Some(Token::DateTimeLiteral) => {
                self.parse_date_literal();
            }
            // File number reference in expressions (for example: #1, #fileNum, #.FileNum)
            Some(Token::Octothorpe) => {
                self.parse_file_number_reference();
                is_identifier = true;
            }
            Some(Token::TypeOfKeyword) => {
                self.push_typeof_prefix_frame(frame_stack, min_bp, lhs_checkpoint);
                return (true, checkpoint);
            }
            // Period operator at start of expression (With block member access)
            // In a With block, ".Property" is shorthand for accessing the With object's property
            Some(Token::PeriodOperator) => {
                self.parse_with_member_expression();
                is_identifier = true;
            }
            // Identifiers (including keywords that can be identifiers in expression context)
            _ => {
                self.parse_identifier_or_call_expression();
                is_identifier = true;
            }
        }

        // If we didn't push frames and this was a bare identifier, wrap it
        if is_identifier {
            self.parse_bare_identifier(checkpoint);
        }

        (false, checkpoint)
    }

    /// If we parsed an identifier but it doesn't have any postfix operators, wrap it in an `IdentifierExpression`.
    ///
    /// This ensures that simple identifiers are represented as `IdentifierExpression` nodes, while identifiers
    /// that are part of member access or calls are wrapped in the appropriate nodes (`MemberAccessExpression`, `CallExpression`)
    /// without an extra `IdentifierExpression` layer.
    fn parse_bare_identifier(&mut self, checkpoint: Checkpoint) {
        // Check if we'll have postfix operators (peek ahead)
        let has_postfix = self.has_postfix_operator_ahead();

        if !has_postfix {
            // Wrap bare identifier in IdentifierExpression using the prefix checkpoint
            self.builder
                .start_node_at(checkpoint, SyntaxKind::IdentifierExpression.to_raw());
            self.builder.finish_node();
        }
    }

    /// Parse an identifier or a function/method call expression.
    ///
    /// This handles:
    /// - Simple identifiers: `myVar`
    /// - Identifiers with type characters: `myVar$`, `count%`
    /// - Keywords used as identifiers in expression context
    fn parse_identifier_or_call_expression(&mut self) {
        // Escaped identifiers in VB6 use square brackets and can contain spaces/keywords.
        if self.at_token(Token::LeftSquareBracket) {
            self.parse_bracketed_identifier();
            return;
        }

        // In expression context, many keywords can be used as identifiers
        if self.is_identifier() || self.at_keyword() {
            // Check if this is a dollar-sign library function (Chr$, UCase$, etc.)
            if self.at_keyword_dollar() {
                // Consume both the identifier/keyword and the dollar sign as a single identifier
                self.consume_keyword_dollar_as_identifier();
            } else {
                // Consume just the identifier/keyword
                self.consume_token();

                // Check for type character suffix ($, %, &, #, @) - but NOT for library functions
                // Only consume dollar sign if it's NOT part of a library function name
                if matches!(
                    self.current_token(),
                    Some(
                        Token::DollarSign
                            | Token::Percent
                            | Token::Ampersand
                            | Token::Octothorpe
                            | Token::AtSign
                    )
                ) {
                    self.consume_token();
                }
            }
        } else {
            // Unexpected token - consume it anyway to avoid infinite loop
            self.consume_token();
        }

        // Don't wrap in a node here - let parse_postfix_operators handle it
    }

    /// Parse a bracketed VB6 escaped identifier.
    ///
    /// Examples: `[eRender]`, `[Get Default Audio Endpoint]`, `[Property]`
    fn parse_bracketed_identifier(&mut self) {
        // Emit opening bracket token.
        if let Some((text, _)) = self.tokens.get(self.pos) {
            self.builder
                .token(SyntaxKind::LeftSquareBracket.to_raw(), text);
            self.pos += 1;
        }

        // Merge all tokens inside brackets into one Identifier token.
        let content_start = self.pos;
        while !self.is_at_end() && !self.at_token(Token::RightSquareBracket) {
            self.pos += 1;
        }
        let content_end = self.pos;

        if let Some((start_offset, end_offset)) =
            self.tokens_span_offsets(content_start, content_end)
        {
            let content = &self.source_content[start_offset..end_offset];
            self.builder.token(SyntaxKind::Identifier.to_raw(), content);
        } else {
            // Fallback for parser modes that do not have a usable source backing slice.
            let mut merged_identifier = String::new();
            for idx in content_start..content_end {
                if let Some((text, _)) = self.tokens.get(idx) {
                    merged_identifier.push_str(text);
                }
            }
            self.builder
                .token(SyntaxKind::Identifier.to_raw(), &merged_identifier);
        }

        // Emit closing bracket token if present.
        if let Some((text, _)) = self.tokens.get(self.pos)
            && self.at_token(Token::RightSquareBracket)
        {
            self.builder
                .token(SyntaxKind::RightSquareBracket.to_raw(), text);
            self.pos += 1;
        }

        // Support optional VB6 type characters after escaped identifiers.
        if matches!(
            self.current_token(),
            Some(
                Token::DollarSign
                    | Token::Percent
                    | Token::Ampersand
                    | Token::Octothorpe
                    | Token::AtSign
            )
        ) {
            self.consume_token();
        }
    }

    /// Parse a VB6 file-number reference used in expressions.
    ///
    /// Examples: `#1`, `#fileNum`, `#.FileNum`
    fn parse_file_number_reference(&mut self) {
        // Consume the octothorpe prefix.
        self.consume_token();
        self.consume_whitespace();

        // Allow with-block member style references, such as #.FileNum
        if self.at_token(Token::PeriodOperator) {
            self.parse_with_member_expression();
            return;
        }

        // Normal file number references: #1 or #fileNum
        if self.is_number() {
            self.consume_token();
            return;
        }

        if self.is_identifier() || self.at_keyword() {
            self.consume_token_as_identifier();
        }
    }

    /// Parse an `AddressOf` expression.
    ///
    /// Syntax: `AddressOf procedureName`
    ///
    /// Used to pass procedure addresses to API functions.
    fn parse_addressof_expression(&mut self) {
        self.builder
            .start_node(SyntaxKind::AddressOfExpression.to_raw());

        // Consume "AddressOf"
        self.consume_token();

        // Skip whitespace
        self.consume_whitespace();

        // Parse the procedure name (identifier)
        if self.is_identifier() || self.at_keyword() {
            self.consume_token();
        }

        self.builder.finish_node();
    }

    /// Parse a `New` expression.
    ///
    /// Syntax: `New ClassName`
    ///
    /// Creates a new instance of a class.
    fn parse_new_expression(&mut self) {
        self.builder.start_node(SyntaxKind::NewExpression.to_raw());

        // Consume "New"
        self.consume_token();

        // Skip whitespace
        self.consume_whitespace();

        // Parse the class name (identifier)
        if self.is_identifier() || self.at_keyword() {
            self.consume_token();
        }

        self.builder.finish_node();
    }

    /// Parse a numeric literal.
    ///
    /// Examples: `42`, `3.14`, `&HFF` (hex), `&O77` (octal), `123.45E-6` (scientific)
    fn parse_numeric_literal(&mut self) {
        self.builder
            .start_node(SyntaxKind::NumericLiteralExpression.to_raw());

        // Consume the number token (already includes type suffix in tokenizer)
        self.consume_token();

        self.builder.finish_node();
    }

    /// Parse a string literal.
    ///
    /// Example: `"Hello, World!"`
    fn parse_string_literal(&mut self) {
        self.builder
            .start_node(SyntaxKind::StringLiteralExpression.to_raw());

        // Consume the string literal token
        self.consume_token();

        self.builder.finish_node();
    }

    /// Parse a boolean literal.
    ///
    /// Examples: `True`, `False`
    fn parse_boolean_literal(&mut self) {
        self.builder
            .start_node(SyntaxKind::BooleanLiteralExpression.to_raw());

        // Consume True or False keyword
        self.consume_token();

        self.builder.finish_node();
    }

    /// Parse a special literal.
    ///
    /// Examples: `Nothing`, `Null`, `Empty`
    fn parse_special_literal(&mut self) {
        self.builder
            .start_node(SyntaxKind::LiteralExpression.to_raw());

        // Consume the keyword
        self.consume_token();

        self.builder.finish_node();
    }

    /// Parse a date literal.
    ///
    /// Syntax: `#1/1/2024#`, `#12:30:45 PM#`, `#1/1/2024 3:45 PM#`
    ///
    /// Note: VB6 has `DateLiteral` as a token, so it's already parsed as a single token
    fn parse_date_literal(&mut self) {
        self.builder
            .start_node(SyntaxKind::LiteralExpression.to_raw());

        // Consume the date literal token
        self.consume_token();

        self.builder.finish_node();
    }

    /// Parse a With block member expression.
    ///
    /// Syntax: `.PropertyName` or `.MethodName`
    ///
    /// This is used inside With blocks where the period operator at the start
    /// indicates a member access on the implicit With object.
    ///
    /// Example:
    /// ```vb
    /// With myObject
    ///     .Property = 123  ' .Property accesses myObject.Property
    /// End With
    /// ```
    fn parse_with_member_expression(&mut self) {
        // Consume the period operator
        self.consume_token();

        // Skip whitespace after period
        self.consume_whitespace();

        // Parse the member name (can be a keyword in VB6)
        if self.is_identifier() || self.at_keyword() {
            self.consume_token_as_identifier();

            // Check for type character suffix
            if matches!(
                self.current_token(),
                Some(
                    Token::DollarSign
                        | Token::Percent
                        | Token::Ampersand
                        | Token::ExclamationMark
                        | Token::Octothorpe
                        | Token::AtSign
                )
            ) {
                self.consume_token();
            }
        }
    }

    /// Parse the content of a member access (everything after the dot).
    fn parse_member_access_content(&mut self) {
        // Consume the period
        self.consume_token();

        // Skip whitespace after period
        self.consume_whitespace();

        // Parse the member name (can be a keyword in VB6)
        if self.is_identifier() || self.at_keyword() {
            self.consume_token();

            // Check for type character suffix
            if matches!(
                self.current_token(),
                Some(
                    Token::DollarSign
                        | Token::Percent
                        | Token::Ampersand
                        | Token::ExclamationMark
                        | Token::Octothorpe
                        | Token::AtSign
                )
            ) {
                self.consume_token();
            }
        }
    }

    /// Parse the content of dictionary access (everything after the !).
    fn parse_dictionary_access_content(&mut self) {
        // Consume the exclamation mark
        self.consume_token();

        // Skip whitespace
        self.consume_whitespace();

        // Parse the key (identifier or string)
        if self.is_identifier() || self.at_keyword() || self.at_token(Token::StringLiteral) {
            self.consume_token();
        }
    }

    /// Get the binding power for an infix operator.
    ///
    /// Returns `Some((left_bp, right_bp))` if the current token is an infix operator,
    /// where `left_bp` is the left binding power and `right_bp` is the right binding power.
    /// Returns `None` if the current token is not an infix operator.
    ///
    /// The difference between left and right binding power determines associativity:
    /// - Left-associative: `right_bp = left_bp + 1` (most operators)
    /// - Right-associative: `right_bp = left_bp` (exponentiation)
    fn get_infix_binding_power(&self) -> Option<(BindingPower, BindingPower)> {
        let token = self.current_token()?;

        let (left_bp, right_bp) = match token {
            // Exponentiation (right-associative)
            Token::ExponentiationOperator => {
                (BindingPower::EXPONENTIATION, BindingPower::EXPONENTIATION)
            }

            // Multiplication and division
            Token::MultiplicationOperator | Token::DivisionOperator => {
                let bp = BindingPower::MULTIPLICATION;
                (bp, BindingPower(bp.0 + 1))
            }

            // Integer division
            Token::BackwardSlashOperator => {
                let bp = BindingPower::INT_DIVISION;
                (bp, BindingPower(bp.0 + 1))
            }

            // Modulo
            Token::ModKeyword => {
                let bp = BindingPower::MODULO;
                (bp, BindingPower(bp.0 + 1))
            }

            // Addition and subtraction
            Token::AdditionOperator | Token::SubtractionOperator => {
                let bp = BindingPower::ADDITION;
                (bp, BindingPower(bp.0 + 1))
            }

            // String concatenation
            Token::Ampersand => {
                let bp = BindingPower::CONCATENATION;
                (bp, BindingPower(bp.0 + 1))
            }

            // Comparison operators
            Token::EqualityOperator
            | Token::InequalityOperator
            | Token::LessThanOrEqualOperator
            | Token::GreaterThanOrEqualOperator
            | Token::LessThanOperator
            | Token::GreaterThanOperator
            | Token::LikeKeyword
            | Token::IsKeyword => {
                let bp = BindingPower::COMPARISON;
                (bp, BindingPower(bp.0 + 1))
            }

            // Logical AND
            Token::AndKeyword => {
                let bp = BindingPower::AND;
                (bp, BindingPower(bp.0 + 1))
            }

            // Logical OR
            Token::OrKeyword => {
                let bp = BindingPower::OR;
                (bp, BindingPower(bp.0 + 1))
            }

            // Logical XOR
            Token::XorKeyword => {
                let bp = BindingPower::XOR;
                (bp, BindingPower(bp.0 + 1))
            }

            // Logical EQV
            Token::EqvKeyword => {
                let bp = BindingPower::EQV;
                (bp, BindingPower(bp.0 + 1))
            }

            // Logical IMP
            Token::ImpKeyword => {
                let bp = BindingPower::IMP;
                (bp, BindingPower(bp.0 + 1))
            }

            _ => return None,
        };

        Some((left_bp, right_bp))
    }

    /// Check if we're at a delimiter that ends an expression.
    ///
    /// Expression delimiters include:
    /// - Newline
    /// - `Then` (in If statements)
    /// - `To` (in For loops)
    /// - `Step` (in For loops)
    /// - Colon (statement separator)
    /// - Comma (argument separator)
    /// - Closing parenthesis/bracket (in some contexts)
    fn is_at_expression_delimiter(&self) -> bool {
        matches!(
            self.current_token(),
            Some(
                Token::Newline
                    | Token::ThenKeyword
                    | Token::ToKeyword
                    | Token::StepKeyword
                    | Token::ColonOperator
                    | Token::EndOfLineComment
                    | Token::RemComment
                    | Token::Comma
                    | Token::RightParenthesis
            )
        )
    }
}

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

    #[test]
    fn numeric_literal() {
        let source = "x = 42\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn numeric_literal_with_type_suffix() {
        let source = "x = 42%\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_literal() {
        let source = "x = \"Hello, World!\"\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn boolean_literal_true() {
        let source = "x = True\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn boolean_literal_false() {
        let source = "x = False\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn identifier_expression() {
        let source = "x = myVariable\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn octothorpe_file_number_reference() {
        let source = "x = #1\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn simple_addition() {
        let source = "x = 2 + 3\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn simple_subtraction() {
        let source = "x = 10 - 5\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn simple_multiplication() {
        let source = "x = 4 * 5\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn simple_division() {
        let source = "x = 20 / 4\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn operator_precedence_multiplication_before_addition() {
        let source = "x = 2 + 3 * 4\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn operator_precedence_with_line_continuation() {
        let source = "x = 2 + _\n    3 * 4\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn operator_precedence_left_associativity() {
        let source = "x = 10 - 5 - 2\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn unary_negation() {
        let source = "x = -5\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn logical_not() {
        let source = "x = Not True\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn logical_and() {
        let source = "x = True And False\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn logical_or() {
        let source = "x = True Or False\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn comparison_equal() {
        let source = "x = a = b\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn comparison_less_than() {
        let source = "x = a < b\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn comparison_greater_than() {
        let source = "x = a > b\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn parenthesized_expression() {
        let source = "x = (5 + 3)\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn parenthesized_changes_precedence() {
        let source = "x = (2 + 3) * 4\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn member_access() {
        let source = "x = obj.property\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn chained_member_access() {
        let source = "x = obj.prop1.prop2\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn function_call_no_args() {
        let source = "x = MyFunction()\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn function_call_one_arg() {
        let source = "x = MyFunction(42)\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn function_call_multiple_args() {
        let source = "x = MyFunction(1, 2, 3)\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn method_call() {
        let source = "x = obj.Method(arg)\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn new_expression() {
        let source = "Set x = New MyClass\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn addressof_expression() {
        let source = "x = AddressOf MyProc\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn string_concatenation() {
        let source = "x = \"Hello\" & \" \" & \"World\"\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn modulo_operator() {
        let source = "x = 10 Mod 3\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn integer_division() {
        let source = "x = 10 \\ 3\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn exponentiation() {
        let source = "x = 2 ^ 8\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn complex_arithmetic() {
        let source = "x = (a + b) * c - d / e\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn complex_logical() {
        let source = "x = Not a And b Or c\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn nothing_literal() {
        let source = "Set x = Nothing\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn null_literal() {
        let source = "x = Null\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn empty_literal() {
        let source = "x = Empty\n";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn dollar_sign_functions_merged() {
        let source = r#"
x = Chr$(65)
y = UCase$("hello")
z = Left$("test", 2)
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        assert_eq!(_failures.len(), 0, "Expected no parse failures.");
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../snapshots/syntax/expressions");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}