mushroomdb-query 0.5.0

Cypher query planner and executor for mushroomdb
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
//! Recursive-descent parser for the Cypher subset. Never panics on any token sequence.

use super::ast::{
    AggArg, AggFunc, ArithOp, CreateEdge, CreateNode, CreateStmt, EdgeDelete, Expr, HopRange,
    LimitSkip, MatchDeleteNodeStmt, MatchDeleteStmt, MatchSetStmt, MergeStmt, NodePat, Operand,
    OptionalClause, OrderItem, OrderTarget, Pattern, Query, RelDir, RelPat, RetItem, RetVal,
    SetClause, UnwindClause, UnwindExpr, WithStage, WriteStatement,
};
use super::Tok;
use crate::filter::CmpOp;
use core_storage::Value;

/// Max parenthesized-expression nesting. Deeper input is `Err`, not a stack overflow.
const MAX_PAREN_DEPTH: usize = 64;

/// Parse a tokenized Cypher subset query. Every failure is `Err(String)`; this
/// function never panics on a well-formed `&[Tok]` (including empty / garbage).
pub fn parse(tokens: &[Tok]) -> Result<Query, String> {
    let mut p = Parser {
        toks: tokens,
        pos: 0,
    };
    let q = p.query()?;
    if p.pos < p.toks.len() {
        // A single-query parse: a trailing `UNION` (or anything else) is an
        // error here. Use `parse_read` to accept a `UNION` chain.
        return Err(p.unsupported_or_unexpected("unexpected tokens after query"));
    }
    Ok(q)
}

/// A read query that may be a single query or a `UNION` / `UNION ALL` chain.
/// `all_flags[i]` is `true` when the boundary before `parts[i + 1]` was
/// `UNION ALL` (bag union); `false` for `UNION` (distinct). Length is
/// `parts.len() - 1`.
#[derive(Debug, Clone, PartialEq)]
pub struct UnionQuery {
    pub parts: Vec<Query>,
    pub all_flags: Vec<bool>,
}

/// Parse a read query, accepting a `UNION` / `UNION ALL` chain.
pub fn parse_read(tokens: &[Tok]) -> Result<UnionQuery, String> {
    let mut p = Parser {
        toks: tokens,
        pos: 0,
    };
    let mut parts = vec![p.query()?];
    let mut all_flags = Vec::new();
    while p.peek_is_union() {
        p.pos += 1; // consume UNION
        let all = matches!(p.peek(), Some(Tok::Ident(s)) if s.eq_ignore_ascii_case("all"));
        if all {
            p.pos += 1; // consume ALL
        }
        all_flags.push(all);
        parts.push(p.query()?);
    }
    if p.pos < p.toks.len() {
        return Err(p.err("unexpected tokens after UNION query"));
    }
    Ok(UnionQuery { parts, all_flags })
}

/// Parse a tokenized write statement (CREATE / MATCH…SET / MATCH…DELETE / MERGE).
/// Returns `Err` for read queries or malformed write statements.
pub fn parse_write(tokens: &[Tok]) -> Result<WriteStatement, String> {
    let mut p = Parser {
        toks: tokens,
        pos: 0,
    };
    p.write_statement()
}

/// Return true if the token stream starts with a write keyword, or is a MATCH
/// statement followed by SET or DELETE.  Used for fast server-side dispatch
/// without a full parse.
pub fn is_write_tokens(tokens: &[Tok]) -> bool {
    match tokens.first() {
        Some(Tok::Create) | Some(Tok::Merge) => true,
        Some(Tok::Match) => tokens.iter().any(|t| matches!(t, Tok::Set | Tok::Delete)),
        _ => false,
    }
}

struct Parser<'a> {
    toks: &'a [Tok],
    pos: usize,
}

impl<'a> Parser<'a> {
    fn peek(&self) -> Option<&'a Tok> {
        self.toks.get(self.pos)
    }

    fn eat(&mut self, want: &Tok) -> bool {
        if self.peek() == Some(want) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn err(&self, msg: &str) -> String {
        match self.peek() {
            Some(tok) => format!("parse error at token {}: {msg} (found {tok:?})", self.pos),
            None => format!(
                "parse error at token {}: {msg} (found end of input)",
                self.pos
            ),
        }
    }

    fn expect(&mut self, want: &Tok, what: &str) -> Result<(), String> {
        if self.eat(want) {
            Ok(())
        } else {
            Err(self.err(what))
        }
    }

    fn ident(&mut self, what: &str) -> Result<String, String> {
        match self.peek() {
            Some(Tok::Ident(s)) => {
                let s = s.clone();
                self.pos += 1;
                Ok(s)
            }
            _ => Err(self.err(what)),
        }
    }

    fn query(&mut self) -> Result<Query, String> {
        let mut matches = Vec::new();
        while self.peek() == Some(&Tok::Match) {
            matches.push(self.match_clause()?);
        }
        if matches.is_empty() {
            return Err(self.err("expected MATCH"));
        }
        // Optional WHERE clause that follows the required MATCH(es) — may come
        // before or after OPTIONAL MATCH.  In standard Cypher, WHERE applies to
        // the preceding MATCH, so `MATCH (a) WHERE … OPTIONAL MATCH … RETURN`
        // is legal.  Parse it here, then parse OPTIONAL MATCHes, then check
        // again in case WHERE follows the OPTIONAL MATCHes instead.
        let where_expr_pre = if self.eat(&Tok::Where) {
            Some(self.expr(0)?)
        } else {
            None
        };
        // Optional MATCH clauses (zero or more, after required MATCHes + WHERE).
        let mut optional_clauses = Vec::new();
        while self.peek() == Some(&Tok::Optional) {
            optional_clauses.push(self.optional_match_clause()?);
        }
        // WHERE may also appear *after* OPTIONAL MATCHes (if not already seen).
        let where_expr = if where_expr_pre.is_some() {
            where_expr_pre
        } else if self.eat(&Tok::Where) {
            Some(self.expr(0)?)
        } else {
            None
        };
        // Optional top-level UNWIND clauses (after WHERE, before WITH).
        let mut unwinds = Vec::new();
        while self.peek() == Some(&Tok::Unwind) {
            unwinds.push(self.unwind_clause()?);
        }
        // Optional WHERE that follows UNWIND (references UNWIND aliases).
        let post_unwind_where = if !unwinds.is_empty() && self.eat(&Tok::Where) {
            Some(self.expr(0)?)
        } else {
            None
        };
        // WITH pipeline stages (zero or more).
        let mut stages = Vec::new();
        while self.peek() == Some(&Tok::With) {
            stages.push(self.with_stage()?);
        }
        let (distinct, returns) = self.return_clause()?;
        let aliases: Vec<&str> = returns.iter().filter_map(|r| r.alias.as_deref()).collect();
        let order_by = if self.peek() == Some(&Tok::Order) {
            self.order_clause(&aliases)?
        } else {
            Vec::new()
        };
        let skip = if self.eat(&Tok::Skip) {
            Some(self.uint("SKIP")?)
        } else {
            None
        };
        let limit = if self.eat(&Tok::Limit) {
            Some(self.uint("LIMIT")?)
        } else {
            None
        };
        // NOTE: the leftover-token check lives in `parse` / `parse_read`, not
        // here, so `query()` can stop cleanly at a `UNION` boundary.
        Ok(Query {
            matches,
            optional_clauses,
            where_expr,
            unwinds,
            post_unwind_where,
            stages,
            returns,
            distinct,
            order_by,
            skip,
            limit,
        })
    }

    /// True if the next token is the `UNION` keyword (an ordinary identifier).
    fn peek_is_union(&self) -> bool {
        matches!(self.peek(), Some(Tok::Ident(s)) if s.eq_ignore_ascii_case("union"))
    }

    /// Named errors for still-unsupported Cypher forms (`UNION`, `CASE`).
    fn unsupported_or_unexpected(&self, msg: &str) -> String {
        match self.peek() {
            Some(Tok::Ident(s)) if s.eq_ignore_ascii_case("union") => {
                "UNION is not supported".to_string()
            }
            Some(Tok::Ident(s)) if s.eq_ignore_ascii_case("case") => {
                "CASE is not supported".to_string()
            }
            _ => self.err(msg),
        }
    }

    /// Consume an identifier keyword (case-insensitive). Keywords that are
    /// not lexer tokens (`IN`, `DISTINCT`, `ON`) stay `Ident` so they can
    /// still be used as variable names in other positions.
    fn eat_ident_kw(&mut self, kw: &str) -> bool {
        if let Some(Tok::Ident(s)) = self.peek() {
            if s.eq_ignore_ascii_case(kw) {
                self.pos += 1;
                return true;
            }
        }
        false
    }

    /// Parse one `OPTIONAL MATCH pattern [WHERE expr]` clause.
    ///
    /// Standard openCypher allows exactly one MATCH per OPTIONAL MATCH.
    fn optional_match_clause(&mut self) -> Result<OptionalClause, String> {
        self.expect(&Tok::Optional, "expected OPTIONAL")?;
        self.expect(&Tok::Match, "expected MATCH after OPTIONAL")?;
        let patterns = vec![self.pattern()?];
        // Optional WHERE inside the optional scope.
        let where_expr = if self.eat(&Tok::Where) {
            Some(self.expr(0)?)
        } else {
            None
        };
        Ok(OptionalClause {
            patterns,
            where_expr,
        })
    }

    /// Parse one `WITH <items> [WHERE] [ORDER BY] [SKIP] [LIMIT] [MATCH]* [UNWIND]* [WHERE]`
    /// stage and return a `WithStage`.
    fn with_stage(&mut self) -> Result<WithStage, String> {
        self.expect(&Tok::With, "expected WITH")?;
        let mut items = vec![self.ret_item()?];
        while self.eat(&Tok::Comma) {
            items.push(self.ret_item()?);
        }
        // Optional WHERE / HAVING immediately after WITH items.
        let where_expr = if self.eat(&Tok::Where) {
            Some(self.expr(0)?)
        } else {
            None
        };
        // Optional ORDER BY inside WITH.
        let aliases: Vec<&str> = items.iter().filter_map(|r| r.alias.as_deref()).collect();
        let order_by = if self.peek() == Some(&Tok::Order) {
            self.order_clause(&aliases)?
        } else {
            Vec::new()
        };
        let skip = if self.eat(&Tok::Skip) {
            Some(self.uint("SKIP")?)
        } else {
            None
        };
        let limit = if self.eat(&Tok::Limit) {
            Some(self.uint("LIMIT")?)
        } else {
            None
        };
        // Optional MATCH clauses that follow this WITH.
        let mut matches = Vec::new();
        while self.peek() == Some(&Tok::Match) {
            matches.push(self.match_clause()?);
        }
        // Optional OPTIONAL MATCH clauses that follow those MATCHes.
        let mut optional_clauses = Vec::new();
        while self.peek() == Some(&Tok::Optional) {
            optional_clauses.push(self.optional_match_clause()?);
        }
        // Optional UNWIND clauses that follow those MATCHes.
        let mut stage_unwinds = Vec::new();
        while self.peek() == Some(&Tok::Unwind) {
            stage_unwinds.push(self.unwind_clause()?);
        }
        // Optional WHERE that follows those MATCHes / UNWINDs.
        let post_where = if self.peek() == Some(&Tok::Where)
            && (!matches.is_empty() || !optional_clauses.is_empty() || !stage_unwinds.is_empty())
        {
            self.pos += 1; // consume WHERE
            Some(self.expr(0)?)
        } else {
            None
        };
        Ok(WithStage {
            items,
            where_expr,
            order_by,
            skip,
            limit,
            matches,
            optional_clauses,
            unwinds: stage_unwinds,
            post_where,
        })
    }

    /// Parse `UNWIND <expr> AS <alias>`.
    fn unwind_clause(&mut self) -> Result<UnwindClause, String> {
        self.expect(&Tok::Unwind, "expected UNWIND")?;
        let list = self.unwind_expr()?;
        self.expect(&Tok::As, "expected AS after UNWIND expression")?;
        let alias = self.ident("expected alias identifier after AS")?;
        Ok(UnwindClause { list, alias })
    }

    /// Parse the list expression in an UNWIND clause:
    /// - `[v1, v2, …]` — literal list.
    /// - `var.field`   — property reference.
    /// - `var`         — bare variable (alias from a prior WITH).
    fn unwind_expr(&mut self) -> Result<UnwindExpr, String> {
        match self.peek() {
            Some(Tok::LBracket) => {
                self.pos += 1; // consume '['
                let mut vals = Vec::new();
                if !self.eat(&Tok::RBracket) {
                    loop {
                        vals.push(self.literal_value("UNWIND list element")?);
                        if self.eat(&Tok::Comma) {
                            continue;
                        }
                        self.expect(&Tok::RBracket, "expected ']' to close UNWIND list")?;
                        break;
                    }
                }
                Ok(UnwindExpr::Lit(vals))
            }
            Some(Tok::Ident(_)) => {
                let name = self.ident("expected variable or property in UNWIND")?;
                if self.eat(&Tok::Dot) {
                    let field = self.ident("expected field name after '.' in UNWIND")?;
                    Ok(UnwindExpr::Prop { var: name, field })
                } else {
                    Ok(UnwindExpr::Var(name))
                }
            }
            _ => Err(self.err("expected list literal, property, or variable in UNWIND")),
        }
    }

    fn match_clause(&mut self) -> Result<Pattern, String> {
        self.expect(&Tok::Match, "expected MATCH")?;
        // Detect `MATCH shortestPath(...)`.
        if let Some(Tok::Ident(s)) = self.peek() {
            if s.eq_ignore_ascii_case("shortestpath") {
                return self.shortest_path_clause();
            }
        }
        self.pattern()
    }

    fn shortest_path_clause(&mut self) -> Result<Pattern, String> {
        self.pos += 1; // consume "shortestPath" identifier
        self.expect(&Tok::LParen, "expected '(' after shortestPath")?;
        let start = self.node()?;
        let rel = self.rel()?;
        if rel.hops.is_none() {
            return Err(
                self.err("shortestPath requires a variable-length relationship (e.g. [*..5])")
            );
        }
        let dest = self.node()?;
        self.expect(&Tok::RParen, "expected ')' to close shortestPath")?;
        Ok(Pattern {
            start,
            chain: vec![(rel, dest)],
            shortest: true,
        })
    }

    fn pattern(&mut self) -> Result<Pattern, String> {
        let start = self.node()?;
        let mut chain = Vec::new();
        while matches!(self.peek(), Some(Tok::Dash) | Some(Tok::Lt)) {
            let rel = self.rel()?;
            let dest = self.node()?;
            chain.push((rel, dest));
        }
        Ok(Pattern {
            start,
            chain,
            shortest: false,
        })
    }

    fn node(&mut self) -> Result<NodePat, String> {
        self.expect(&Tok::LParen, "expected '(' to start a node pattern")?;
        let var = match self.peek() {
            Some(Tok::Ident(s)) => {
                let s = s.clone();
                self.pos += 1;
                Some(s)
            }
            _ => None,
        };
        let label = if self.eat(&Tok::Colon) {
            Some(self.ident("expected label identifier after ':'")?)
        } else {
            None
        };
        let props = if self.peek() == Some(&Tok::LBrace) {
            self.props()?
        } else {
            Vec::new()
        };
        self.expect(&Tok::RParen, "expected ')' to close a node pattern")?;
        Ok(NodePat { var, label, props })
    }

    fn props(&mut self) -> Result<Vec<(String, Operand)>, String> {
        self.expect(&Tok::LBrace, "expected '{'")?;
        let mut out = Vec::new();
        loop {
            let key = self.ident("expected property key")?;
            self.expect(&Tok::Colon, "expected ':' after property key")?;
            let val = self.operand()?;
            out.push((key, val));
            if self.eat(&Tok::Comma) {
                continue;
            }
            break;
        }
        self.expect(&Tok::RBrace, "expected '}' to close property map")?;
        Ok(out)
    }

    fn rel(&mut self) -> Result<RelPat, String> {
        if self.eat(&Tok::Lt) {
            self.expect(
                &Tok::Dash,
                "expected '-' after '<' in a left-directed relationship",
            )?;
            let (var, etypes, hops) = self.rel_body()?;
            self.expect(
                &Tok::Dash,
                "expected '-' to close a left-directed relationship",
            )?;
            return Ok(RelPat {
                var,
                etypes,
                dir: RelDir::Left,
                hops,
            });
        }
        self.expect(&Tok::Dash, "expected '-' to start a relationship")?;
        let (var, etypes, hops) = self.rel_body()?;
        self.expect(&Tok::Dash, "expected '-' after ']'")?;
        let dir = if self.eat(&Tok::Gt) {
            RelDir::Right
        } else {
            RelDir::Undirected
        };
        Ok(RelPat {
            var,
            etypes,
            dir,
            hops,
        })
    }

    #[allow(clippy::type_complexity)]
    fn rel_body(&mut self) -> Result<(Option<String>, Vec<String>, Option<HopRange>), String> {
        self.expect(&Tok::LBracket, "expected '[' in a relationship pattern")?;
        let var = match self.peek() {
            Some(Tok::Ident(s)) => {
                let s = s.clone();
                self.pos += 1;
                Some(s)
            }
            _ => None,
        };
        // `:A`, or `:A|:B|:C` alternation. Empty = any type.
        let mut etypes = Vec::new();
        if self.eat(&Tok::Colon) {
            etypes.push(self.ident("expected relationship type identifier after ':'")?);
            while self.eat(&Tok::Pipe) {
                // Both `|:B` (openCypher) and the lenient `|B` are accepted.
                self.eat(&Tok::Colon);
                etypes.push(self.ident("expected relationship type identifier after '|'")?);
            }
        }
        let hops = if self.eat(&Tok::Star) {
            Some(self.parse_hop_range()?)
        } else {
            None
        };
        self.expect(
            &Tok::RBracket,
            "expected ']' to close a relationship pattern",
        )?;
        Ok((var, etypes, hops))
    }

    /// Parse the hop-count range that follows `*` inside a relationship bracket.
    ///
    /// Recognised forms (after `*` is already consumed):
    /// - `]`        → bare `*`, treated as `1..10`
    /// - `n`        → exactly n hops (`n..n`)
    /// - `n..m`     → n..m hops
    /// - `..m`      → 1..m hops
    /// - `n..`      → unbounded → hard-cap error
    ///
    /// Hard cap: max > 10 or unbounded → Err("variable-length paths are capped at 10 hops").
    fn parse_hop_range(&mut self) -> Result<HopRange, String> {
        const CAP_ERR: &str = "variable-length paths are capped at 10 hops";

        match self.peek() {
            // bare `*`  →  min=1, max=10
            Some(Tok::RBracket) => Ok(HopRange { min: 1, max: 10 }),

            // `*n`  or  `*n..`  or  `*n..m`
            Some(Tok::Int(n)) => {
                let n = *n;
                self.pos += 1;
                if self.eat(&Tok::Dot) {
                    self.expect(&Tok::Dot, "expected '..' separator in hop range")?;
                    match self.peek() {
                        Some(Tok::Int(m)) => {
                            let m = *m;
                            self.pos += 1;
                            // `*n..m`
                            self.validate_hop_range(n, m, CAP_ERR)
                        }
                        _ => {
                            // `*n..` — unbounded
                            Err(CAP_ERR.to_string())
                        }
                    }
                } else {
                    // `*n` — exact hops
                    self.validate_hop_range(n, n, CAP_ERR)
                }
            }

            // `*..m`
            Some(Tok::Dot) => {
                self.pos += 1; // consume first '.'
                self.expect(&Tok::Dot, "expected '..' range separator after '*'")?;
                match self.peek() {
                    Some(Tok::Int(m)) => {
                        let m = *m;
                        self.pos += 1;
                        self.validate_hop_range(1, m, CAP_ERR)
                    }
                    _ => Err(self.err("expected max-hop integer after '*..'")),
                }
            }

            // Anything else after `*` — treat as bare `*`
            _ => Ok(HopRange { min: 1, max: 10 }),
        }
    }

    fn validate_hop_range(
        &self,
        min_n: i64,
        max_n: i64,
        cap_err: &str,
    ) -> Result<HopRange, String> {
        if min_n < 0 || max_n < 0 {
            return Err(self.err("hop counts must be non-negative"));
        }
        if min_n == 0 {
            return Err(self.err(
                "zero-length variable-length paths are not supported; minimum hop count is 1",
            ));
        }
        if max_n > 10 {
            return Err(cap_err.to_string());
        }
        let min = min_n as u8;
        let max = max_n as u8;
        if min > max {
            return Err(self.err(&format!(
                "variable-length path min ({min}) must not exceed max ({max})"
            )));
        }
        Ok(HopRange { min, max })
    }

    fn expr(&mut self, paren_depth: usize) -> Result<Expr, String> {
        let mut left = self.term(paren_depth)?;
        while self.eat(&Tok::Or) {
            let right = self.term(paren_depth)?;
            left = Expr::Or(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn term(&mut self, paren_depth: usize) -> Result<Expr, String> {
        let mut left = self.factor(paren_depth)?;
        while self.eat(&Tok::And) {
            let right = self.factor(paren_depth)?;
            left = Expr::And(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn factor(&mut self, paren_depth: usize) -> Result<Expr, String> {
        let negated = self.eat(&Tok::Not);
        let inner = if self.eat(&Tok::LParen) {
            if paren_depth >= MAX_PAREN_DEPTH {
                return Err(self.err("expression nesting too deep"));
            }
            let e = self.expr(paren_depth + 1)?;
            self.expect(
                &Tok::RParen,
                "expected ')' to close parenthesized expression",
            )?;
            e
        } else {
            self.cmp()?
        };
        if negated {
            Ok(Expr::Not(Box::new(inner)))
        } else {
            Ok(inner)
        }
    }

    fn cmp(&mut self) -> Result<Expr, String> {
        let lhs = self.arith_expr()?;
        // Check for IS NULL / IS NOT NULL postfix.
        if let Some(Tok::Ident(s)) = self.peek() {
            if s.eq_ignore_ascii_case("is") {
                self.pos += 1; // consume "IS"
                               // Optional NOT.
                let negated = self.eat(&Tok::Not);
                // Expect NULL identifier.
                match self.peek() {
                    Some(Tok::Ident(n)) if n.eq_ignore_ascii_case("null") => {
                        self.pos += 1; // consume "NULL"
                        return Ok(if negated {
                            Expr::IsNotNull(lhs)
                        } else {
                            Expr::IsNull(lhs)
                        });
                    }
                    _ => {
                        return Err(self.err(if negated {
                            "expected NULL after IS NOT"
                        } else {
                            "expected NULL after IS"
                        }));
                    }
                }
            }
        }
        // `IN [a, b, $p]` or `IN $list`.
        if self.eat_ident_kw("in") {
            let list = self.in_list()?;
            return Ok(Expr::In { expr: lhs, list });
        }
        // If no comparison operator follows, treat the operand as a standalone
        // boolean predicate (Expr::Truthy).  This enables:
        //   WHERE textMatches(n.bio, 'query')
        // without requiring an explicit `= true` or similar.
        match self.cmp_op() {
            Ok(op) => {
                let rhs = self.arith_expr()?;
                Ok(Expr::Cmp { lhs, op, rhs })
            }
            Err(_) => Ok(Expr::Truthy(lhs)),
        }
    }

    /// Parse the list operand of `IN`: `[a, b, $p]` or a single operand (`$cities`).
    fn in_list(&mut self) -> Result<Vec<Operand>, String> {
        if self.eat(&Tok::LBracket) {
            let mut items = Vec::new();
            if !self.eat(&Tok::RBracket) {
                loop {
                    items.push(self.arith_expr()?);
                    if self.eat(&Tok::Comma) {
                        continue;
                    }
                    self.expect(&Tok::RBracket, "expected ']' to close IN list")?;
                    break;
                }
            }
            Ok(items)
        } else {
            Ok(vec![self.arith_expr()?])
        }
    }

    // ── Arithmetic expression parsing (precedence: * / > + -) ──────────────
    //
    // Grammar:
    //   arith_expr  = arith_add
    //   arith_add   = arith_mul ((+ | -) arith_mul)*
    //   arith_mul   = arith_unary ((* | /) arith_unary)*
    //   arith_unary = - arith_atom | arith_atom
    //   arith_atom  = literal | param | ident | ident.field | ident(args…) | (arith_add)

    /// Parse a full arithmetic expression (additive level).
    fn arith_expr(&mut self) -> Result<Operand, String> {
        let mut left = self.arith_mul()?;
        loop {
            let op = if self.eat(&Tok::Plus) {
                ArithOp::Add
            } else if self.eat(&Tok::Dash) {
                // Dash is `-`; but we must not consume a Dash that starts a
                // relationship pattern (those appear at the top-level pattern
                // parser, not inside an expression). Inside expressions `-`
                // is always subtraction.
                ArithOp::Sub
            } else {
                break;
            };
            let right = self.arith_mul()?;
            left = Operand::BinArith {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    /// Parse a multiplicative-level arithmetic expression.
    fn arith_mul(&mut self) -> Result<Operand, String> {
        let mut left = self.arith_unary()?;
        loop {
            let op = if self.eat(&Tok::Star) {
                ArithOp::Mul
            } else if self.eat(&Tok::Slash) {
                ArithOp::Div
            } else {
                break;
            };
            let right = self.arith_unary()?;
            left = Operand::BinArith {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    /// Parse a unary-level arithmetic expression (handles unary `-`).
    fn arith_unary(&mut self) -> Result<Operand, String> {
        if self.eat(&Tok::Dash) {
            // Unary minus: fold into the next atom.
            return match self.peek() {
                Some(Tok::Int(n)) => {
                    let n = *n;
                    self.pos += 1;
                    let neg = n
                        .checked_neg()
                        .ok_or_else(|| self.err("integer negation overflow"))?;
                    Ok(Operand::Lit(Value::Int(neg)))
                }
                Some(Tok::Float(x)) => {
                    let x = *x;
                    self.pos += 1;
                    Ok(Operand::Lit(Value::Float(-x)))
                }
                _ => Err(self.err("unary minus only applies to numeric literals")),
            };
        }
        self.arith_atom()
    }

    /// Parse an atomic operand (leaf of the arithmetic expression tree).
    fn arith_atom(&mut self) -> Result<Operand, String> {
        // Parenthesized arithmetic expression.
        if self.eat(&Tok::LParen) {
            let inner = self.arith_expr()?;
            self.expect(&Tok::RParen, "expected ')' to close arithmetic expression")?;
            return Ok(inner);
        }
        // Delegate to the existing atom parser (no unary minus here — handled above).
        self.operand_atom()
    }

    fn cmp_op(&mut self) -> Result<CmpOp, String> {
        let op = match self.peek() {
            Some(Tok::Eq) => CmpOp::Eq,
            Some(Tok::Ne) => CmpOp::Ne,
            Some(Tok::Lt) => CmpOp::Lt,
            Some(Tok::Le) => CmpOp::Le,
            Some(Tok::Gt) => CmpOp::Gt,
            Some(Tok::Ge) => CmpOp::Ge,
            _ => return Err(self.err("expected comparison operator")),
        };
        self.pos += 1;
        Ok(op)
    }

    /// Parse a single atomic operand (no arithmetic wrapping — use `arith_expr`
    /// for full expression support).  This is the leaf parser for literals,
    /// parameters, property references, variable references, and function calls.
    fn operand_atom(&mut self) -> Result<Operand, String> {
        match self.peek() {
            Some(Tok::Int(n)) => {
                let n = *n;
                self.pos += 1;
                Ok(Operand::Lit(Value::Int(n)))
            }
            Some(Tok::Float(x)) => {
                let x = *x;
                self.pos += 1;
                Ok(Operand::Lit(Value::Float(x)))
            }
            Some(Tok::Str(s)) => {
                let s = s.clone();
                self.pos += 1;
                Ok(Operand::Lit(Value::Str(s)))
            }
            Some(Tok::Param(s)) => {
                let s = s.clone();
                self.pos += 1;
                Ok(Operand::Param(s))
            }
            Some(Tok::Ident(_)) => {
                let name = self.ident("expected identifier")?;
                if name.eq_ignore_ascii_case("case") {
                    return self.parse_case();
                }
                if name.eq_ignore_ascii_case("collect") && self.peek() == Some(&Tok::LParen) {
                    // collect() is a top-level RETURN/WITH aggregate (handled in
                    // ret_item); it is not valid inside a larger expression.
                    return Err(
                        "collect() is only supported as a top-level RETURN/WITH aggregate"
                            .to_string(),
                    );
                }
                if self.peek() == Some(&Tok::LParen) {
                    // Scalar function call: name(arg, ...)
                    // Function arguments may be arbitrary arithmetic expressions.
                    self.pos += 1; // consume '('
                    let mut args = Vec::new();
                    if self.peek() != Some(&Tok::RParen) {
                        args.push(self.arith_expr()?);
                        while self.eat(&Tok::Comma) {
                            args.push(self.arith_expr()?);
                        }
                    }
                    self.expect(&Tok::RParen, "expected ')' to close function call")?;
                    Ok(Operand::FuncCall { name, args })
                } else if self.eat(&Tok::Dot) {
                    let field = self.ident("expected field name after '.'")?;
                    Ok(Operand::Prop { var: name, field })
                } else {
                    // Bare variable reference (e.g. alias name in WITH … WHERE c > 2).
                    Ok(Operand::Var(name))
                }
            }
            _ => Err(self.err("expected operand (property, literal, or parameter)")),
        }
    }

    /// Parse a generic `CASE WHEN <cond> THEN <value> … [ELSE <value>] END`.
    /// The `CASE` keyword has already been consumed. `WHEN`/`THEN`/`ELSE`/`END`
    /// are ordinary identifiers to the lexer, matched case-insensitively.
    fn parse_case(&mut self) -> Result<Operand, String> {
        let is_kw = |tok: Option<&Tok>, kw: &str| matches!(tok, Some(Tok::Ident(s)) if s.eq_ignore_ascii_case(kw));
        let mut branches = Vec::new();
        while is_kw(self.peek(), "when") {
            self.pos += 1; // consume WHEN
            let cond = self.expr(0)?;
            if !is_kw(self.peek(), "then") {
                return Err(self.err("expected THEN in a CASE branch"));
            }
            self.pos += 1; // consume THEN
            let value = self.arith_expr()?;
            branches.push((cond, value));
        }
        if branches.is_empty() {
            return Err(self.err("CASE requires at least one WHEN ... THEN branch"));
        }
        let default = if is_kw(self.peek(), "else") {
            self.pos += 1; // consume ELSE
            Some(Box::new(self.arith_expr()?))
        } else {
            None
        };
        if !is_kw(self.peek(), "end") {
            return Err(self.err("expected END to close CASE"));
        }
        self.pos += 1; // consume END
        Ok(Operand::Case { branches, default })
    }

    /// Parse a full arithmetic expression (additive + multiplicative + unary +
    /// atom).  This is the primary operand entry point for WHERE, RETURN, SET,
    /// and function arguments.  Use `operand_atom` for contexts that truly
    /// require a single atom (e.g., MATCH property map values where arithmetic
    /// would be syntactically ambiguous with the `}` delimiter).
    fn operand(&mut self) -> Result<Operand, String> {
        // In contexts where `operand` is called for MATCH props, the parser
        // never sees arithmetic operators (`+`/`/`) because they can't appear
        // inside `{key: val}` maps.  Delegating to `arith_expr` is safe here.
        self.arith_expr()
    }

    fn return_clause(&mut self) -> Result<(bool, Vec<RetItem>), String> {
        if !self.eat(&Tok::Return) {
            return Err(self.unsupported_or_unexpected("expected RETURN"));
        }
        let distinct = self.eat_ident_kw("distinct");
        let mut items = vec![self.ret_item()?];
        while self.eat(&Tok::Comma) {
            items.push(self.ret_item()?);
        }
        Ok((distinct, items))
    }

    fn return_items(&mut self) -> Result<Vec<RetItem>, String> {
        let mut items = vec![self.ret_item()?];
        while self.eat(&Tok::Comma) {
            items.push(self.ret_item()?);
        }
        Ok(items)
    }

    fn ret_item(&mut self) -> Result<RetItem, String> {
        // Check for an aggregate function name (COUNT/SUM/AVG/MIN/MAX).
        // These are ordinary identifiers in the lexer, so we peek and check
        // the lowercased string before deciding the parse branch.
        if let Some(Tok::Ident(s)) = self.peek() {
            let func = match s.to_ascii_lowercase().as_str() {
                "count" => Some(AggFunc::Count),
                "sum" => Some(AggFunc::Sum),
                "avg" => Some(AggFunc::Avg),
                "min" => Some(AggFunc::Min),
                "max" => Some(AggFunc::Max),
                "collect" => Some(AggFunc::Collect),
                _ => None,
            };
            if let Some(func) = func {
                self.pos += 1; // consume the function name
                self.expect(&Tok::LParen, "expected '(' after aggregate function name")?;
                let arg = if self.eat(&Tok::Star) {
                    AggArg::Star
                } else {
                    let var = self.ident("expected variable or '*' in aggregate argument")?;
                    if self.eat(&Tok::Dot) {
                        let field =
                            self.ident("expected field name after '.' in aggregate argument")?;
                        AggArg::Prop { var, field }
                    } else {
                        AggArg::Var(var)
                    }
                };
                self.expect(&Tok::RParen, "expected ')' to close aggregate function")?;
                let alias = if self.eat(&Tok::As) {
                    Some(self.ident("expected alias identifier after AS")?)
                } else {
                    None
                };
                return Ok(RetItem {
                    value: RetVal::Agg { func, arg },
                    alias,
                });
            }
        }

        // Non-aggregate RETURN item: parse as a full arithmetic expression,
        // then convert the resulting Operand to the appropriate RetVal variant.
        // This unified path handles:
        //   n          → RetVal::Var
        //   n.prop     → RetVal::Prop
        //   f(...)     → RetVal::FuncCall
        //   n.age + 1  → RetVal::ScalarExpr(BinArith)
        //   42         → RetVal::ScalarExpr(Lit)
        let op = self.arith_expr()?;
        let value = match op {
            Operand::Var(name) => RetVal::Var(name),
            Operand::Prop { var, field } => RetVal::Prop { var, field },
            Operand::FuncCall { name, args } => RetVal::FuncCall { name, args },
            other => RetVal::ScalarExpr(other),
        };
        let alias = if self.eat(&Tok::As) {
            Some(self.ident("expected alias identifier after AS")?)
        } else {
            None
        };
        Ok(RetItem { value, alias })
    }

    fn order_clause(&mut self, aliases: &[&str]) -> Result<Vec<OrderItem>, String> {
        self.expect(&Tok::Order, "expected ORDER")?;
        self.expect(&Tok::By, "expected BY after ORDER")?;
        let mut items = vec![self.order_item(aliases)?];
        while self.eat(&Tok::Comma) {
            items.push(self.order_item(aliases)?);
        }
        Ok(items)
    }

    fn order_item(&mut self, aliases: &[&str]) -> Result<OrderItem, String> {
        let name = self.ident("expected ORDER BY target")?;
        let target = if self.eat(&Tok::Dot) {
            let field = self.ident("expected field name after '.'")?;
            OrderTarget::Prop { var: name, field }
        } else if aliases.contains(&name.as_str()) {
            OrderTarget::Alias(name)
        } else {
            OrderTarget::Var(name)
        };
        let descending = if self.eat(&Tok::Desc) {
            true
        } else {
            let _ = self.eat(&Tok::Asc);
            false
        };
        Ok(OrderItem { target, descending })
    }

    fn uint(&mut self, what: &str) -> Result<LimitSkip, String> {
        match self.peek() {
            Some(Tok::Int(n)) if *n >= 0 => {
                let n = *n as u64;
                self.pos += 1;
                Ok(LimitSkip::Exact(n))
            }
            Some(Tok::Int(_)) => Err(self.err(&format!("{what} must be a non-negative integer"))),
            Some(Tok::Param(_)) => {
                let name = match self.toks.get(self.pos) {
                    Some(Tok::Param(s)) => s.clone(),
                    _ => unreachable!(),
                };
                self.pos += 1;
                Ok(LimitSkip::Param(name))
            }
            _ => Err(self.err(&format!("expected integer or $parameter after {what}"))),
        }
    }

    // ── Write statement parsing ───────────────────────────────────────────────

    fn write_statement(&mut self) -> Result<WriteStatement, String> {
        match self.peek() {
            Some(Tok::Create) => self.create_stmt(),
            Some(Tok::Merge) => self.merge_stmt(),
            Some(Tok::Match) => self.match_write_stmt(),
            _ => Err(self
                .err("expected CREATE, MERGE, or MATCH … SET/DELETE (write statement required)")),
        }
    }

    // ── CREATE ────────────────────────────────────────────────────────────────

    fn create_stmt(&mut self) -> Result<WriteStatement, String> {
        self.expect(&Tok::Create, "expected CREATE")?;
        let mut stmt = self.create_pattern()?;
        // Optional RETURN clause: `CREATE (n:L {…}) RETURN n` or `RETURN n.id AS id`.
        if self.eat(&Tok::Return) {
            stmt.returns = Some(self.return_items()?);
        }
        if self.pos < self.toks.len() {
            return Err(self.err("unexpected tokens after CREATE"));
        }
        Ok(WriteStatement::Create(stmt))
    }

    fn create_pattern(&mut self) -> Result<CreateStmt, String> {
        // Parse the first (possibly only) node.
        let first = self.create_node(0)?;
        let first_var = first.var.clone().unwrap_or_else(|| format!("_cn{}", 0));
        let mut nodes: Vec<CreateNode> = vec![first];
        let mut edges: Vec<CreateEdge> = Vec::new();

        // Chain: (-[:T]-> | <-[:T]-) followed by another node.
        while matches!(self.peek(), Some(Tok::Dash) | Some(Tok::Lt)) {
            let (etype, src_is_left) = self.create_rel()?;
            let idx = nodes.len();
            let next = self.create_node(idx)?;
            let next_var = next.var.clone().unwrap_or_else(|| format!("_cn{idx}"));
            let prev_var = nodes.last().unwrap().var.clone().unwrap_or_else(|| {
                if nodes.len() == 1 {
                    first_var.clone()
                } else {
                    format!("_cn{}", nodes.len() - 1)
                }
            });
            let (src_var, dst_var) = if src_is_left {
                // <-[:T]- means next→prev i.e. next is src
                (next_var.clone(), prev_var)
            } else {
                // -[:T]-> means prev→next
                (prev_var, next_var.clone())
            };
            edges.push(CreateEdge {
                src_var,
                etype,
                dst_var,
            });
            nodes.push(next);
        }
        Ok(CreateStmt {
            nodes,
            edges,
            returns: None,
        })
    }

    fn create_node(&mut self, idx: usize) -> Result<CreateNode, String> {
        self.expect(&Tok::LParen, "expected '(' in CREATE node pattern")?;
        let var = match self.peek() {
            Some(Tok::Ident(s)) => {
                let s = s.clone();
                self.pos += 1;
                Some(s)
            }
            _ => None,
        };
        if !self.eat(&Tok::Colon) {
            return Err(self.err("CREATE node requires a label (e.g., (n:Label {…}))"));
        }
        let label = self.ident("expected label identifier after ':'")?;
        let props = if self.peek() == Some(&Tok::LBrace) {
            self.literal_props()?
        } else {
            Vec::new()
        };
        self.expect(&Tok::RParen, "expected ')' to close CREATE node pattern")?;
        let var = Some(var.unwrap_or_else(|| format!("_cn{idx}")));
        Ok(CreateNode { var, label, props })
    }

    /// Parse `{key: literal, …}` where all values must be literals (no params, no props).
    fn literal_props(&mut self) -> Result<Vec<(String, Value)>, String> {
        self.expect(&Tok::LBrace, "expected '{'")?;
        let mut out = Vec::new();
        if self.eat(&Tok::RBrace) {
            return Ok(out);
        }
        loop {
            let key = self.ident("expected property key")?;
            self.expect(&Tok::Colon, "expected ':' after property key")?;
            let val = self.literal_value("property value")?;
            out.push((key, val));
            if self.eat(&Tok::Comma) {
                continue;
            }
            break;
        }
        self.expect(&Tok::RBrace, "expected '}' to close property map")?;
        Ok(out)
    }

    /// Parse a literal value (int, float, or string). Parameters and property
    /// references are not accepted in write statements (v1 limitation).
    fn literal_value(&mut self, what: &str) -> Result<Value, String> {
        if self.eat(&Tok::Dash) {
            return match self.peek() {
                Some(Tok::Int(n)) => {
                    let n = *n;
                    self.pos += 1;
                    Ok(Value::Int(-n))
                }
                Some(Tok::Float(x)) => {
                    let x = *x;
                    self.pos += 1;
                    Ok(Value::Float(-x))
                }
                _ => Err(self.err("unary minus only applies to numeric literals")),
            };
        }
        match self.peek() {
            Some(Tok::Int(n)) => {
                let n = *n;
                self.pos += 1;
                Ok(Value::Int(n))
            }
            Some(Tok::Float(x)) => {
                let x = *x;
                self.pos += 1;
                Ok(Value::Float(x))
            }
            Some(Tok::Str(s)) => {
                let s = s.clone();
                self.pos += 1;
                Ok(Value::Str(s))
            }
            // List literal: `[]`, `[v1, v2, …]`, nesting allowed. Elements are
            // themselves literal values (recursion), matching the UNWIND list
            // form and downstream `Value::List` support in the store.
            Some(Tok::LBracket) => {
                self.pos += 1; // consume '['
                let mut items = Vec::new();
                if !self.eat(&Tok::RBracket) {
                    loop {
                        items.push(self.literal_value(what)?);
                        if self.eat(&Tok::Comma) {
                            continue;
                        }
                        self.expect(&Tok::RBracket, "expected ']' to close list literal")?;
                        break;
                    }
                }
                Ok(Value::List(items))
            }
            Some(Tok::Param(_)) => Err(self.err(&format!(
                "parameter references are not supported in {what} (v1 limitation: use literals only)"
            ))),
            Some(Tok::Ident(_)) => Err(self.err(&format!(
                "expression RHS not supported in {what} (v1 limitation: use literals only)"
            ))),
            _ => Err(self.err(&format!("expected literal value for {what}"))),
        }
    }

    /// Parse `-[:TYPE]->` or `<-[:TYPE]-`.  Returns `(etype, src_is_left)` where
    /// `src_is_left = true` means left node is dst (i.e., next node is src).
    fn create_rel(&mut self) -> Result<(String, bool), String> {
        if self.eat(&Tok::Lt) {
            // <-[:TYPE]-
            self.expect(&Tok::Dash, "expected '-' after '<' in relationship")?;
            self.expect(&Tok::LBracket, "expected '[' in relationship pattern")?;
            self.expect(&Tok::Colon, "expected ':TYPE' in CREATE relationship")?;
            let etype = self.ident("expected relationship type")?;
            self.expect(&Tok::RBracket, "expected ']'")?;
            self.expect(&Tok::Dash, "expected '-'")?;
            return Ok((etype, true));
        }
        // -[:TYPE]->
        self.expect(&Tok::Dash, "expected '-' to start relationship")?;
        self.expect(&Tok::LBracket, "expected '[' in relationship pattern")?;
        self.expect(&Tok::Colon, "expected ':TYPE' in CREATE relationship")?;
        let etype = self.ident("expected relationship type")?;
        self.expect(&Tok::RBracket, "expected ']'")?;
        self.expect(&Tok::Dash, "expected '-'")?;
        self.expect(
            &Tok::Gt,
            "expected '>' — CREATE requires directed relationships",
        )?;
        Ok((etype, false))
    }

    // ── MERGE ─────────────────────────────────────────────────────────────────

    fn merge_stmt(&mut self) -> Result<WriteStatement, String> {
        self.expect(&Tok::Merge, "expected MERGE")?;
        self.expect(&Tok::LParen, "expected '(' after MERGE")?;
        // Optional var: `MERGE (n:Label {…})` — capture n for RETURN projection.
        let var = match self.peek() {
            Some(Tok::Ident(_)) => {
                let s = match self.toks.get(self.pos) {
                    Some(Tok::Ident(s)) => s.clone(),
                    _ => unreachable!(),
                };
                self.pos += 1; // consume var name
                Some(s)
            }
            _ => None,
        };
        if !self.eat(&Tok::Colon) {
            return Err(self.err("MERGE requires a label (e.g., MERGE (n:Label {key: 'x'}))"));
        }
        let label = self.ident("expected label identifier after ':'")?;
        if self.peek() != Some(&Tok::LBrace) {
            return Err(self.err(
                "MERGE requires a property map with exactly one key (e.g., MERGE (n:Label {id: 'x'}))",
            ));
        }
        let mut props = self.literal_props()?;
        if props.len() != 1 {
            return Err(format!(
                "MERGE supports exactly one key property (got {}); use CREATE for multi-prop nodes",
                props.len()
            ));
        }
        self.expect(&Tok::RParen, "expected ')' to close MERGE pattern")?;
        let mut on_create = Vec::new();
        let mut on_match = Vec::new();
        while self.eat_ident_kw("on") {
            if self.eat(&Tok::Create) {
                self.expect(&Tok::Set, "expected SET after ON CREATE")?;
                on_create.extend(self.set_clauses()?);
            } else if self.eat(&Tok::Match) {
                self.expect(&Tok::Set, "expected SET after ON MATCH")?;
                on_match.extend(self.set_clauses()?);
            } else {
                return Err(self.err("expected CREATE or MATCH after ON"));
            }
        }
        // Optional RETURN clause.
        let returns = if self.eat(&Tok::Return) {
            Some(self.return_items()?)
        } else {
            None
        };
        if self.pos < self.toks.len() {
            return Err(self.unsupported_or_unexpected("unexpected tokens after MERGE"));
        }
        let (key_field, key_value) = props.remove(0);
        Ok(WriteStatement::Merge(MergeStmt {
            label,
            key_field,
            key_value,
            var,
            on_create,
            on_match,
            returns,
        }))
    }

    // ── MATCH … SET / MATCH … DELETE ─────────────────────────────────────────

    fn match_write_stmt(&mut self) -> Result<WriteStatement, String> {
        // Parse MATCH clauses (same as read query).
        let mut matches = Vec::new();
        while self.peek() == Some(&Tok::Match) {
            matches.push(self.match_clause()?);
        }
        if matches.is_empty() {
            return Err(self.err("expected MATCH"));
        }
        // Optional WHERE.
        let where_expr = if self.eat(&Tok::Where) {
            Some(self.expr(0)?)
        } else {
            None
        };
        // Dispatch on SET, DETACH DELETE, or DELETE.
        match self.peek() {
            Some(Tok::Set) => {
                self.pos += 1; // consume SET
                let sets = self.set_clauses()?;
                let returns = if self.eat(&Tok::Return) {
                    Some(self.return_items()?)
                } else {
                    None
                };
                if self.pos < self.toks.len() {
                    return Err(self.unsupported_or_unexpected("unexpected tokens after SET"));
                }
                Ok(WriteStatement::MatchSet(MatchSetStmt {
                    matches,
                    where_expr,
                    sets,
                    returns,
                }))
            }
            Some(Tok::Detach) => {
                // DETACH DELETE <node_var> [, …]
                self.pos += 1; // consume DETACH
                self.expect(&Tok::Delete, "expected DELETE after DETACH")?;
                let node_vars = self.node_delete_targets(&matches)?;
                if self.pos < self.toks.len() {
                    return Err(self.err("unexpected tokens after DETACH DELETE"));
                }
                Ok(WriteStatement::MatchDeleteNode(MatchDeleteNodeStmt {
                    matches,
                    where_expr,
                    node_vars,
                    detach: true,
                }))
            }
            Some(Tok::Delete) => {
                self.pos += 1; // consume DELETE
                               // Try to resolve all targets as edge vars first. If the first
                               // target is a node var (not an edge var), fall through to node delete.
                match self.delete_targets_or_node(&matches)? {
                    DeleteTargetResult::Edges(deletes) => {
                        if self.pos < self.toks.len() {
                            return Err(self.err("unexpected tokens after DELETE"));
                        }
                        Ok(WriteStatement::MatchDelete(MatchDeleteStmt {
                            matches,
                            where_expr,
                            deletes,
                        }))
                    }
                    DeleteTargetResult::Nodes(node_vars) => {
                        if self.pos < self.toks.len() {
                            return Err(self.err("unexpected tokens after DELETE"));
                        }
                        Ok(WriteStatement::MatchDeleteNode(MatchDeleteNodeStmt {
                            matches,
                            where_expr,
                            node_vars,
                            detach: false,
                        }))
                    }
                }
            }
            _ => Err(self.err(
                "expected SET or DELETE after MATCH [WHERE]; \
                 combined MATCH…RETURN is a read query, not a write statement",
            )),
        }
    }

    fn set_clauses(&mut self) -> Result<Vec<SetClause>, String> {
        let mut sets = vec![self.set_clause()?];
        while self.eat(&Tok::Comma) {
            sets.push(self.set_clause()?);
        }
        Ok(sets)
    }

    fn set_clause(&mut self) -> Result<SetClause, String> {
        let var = self.ident("expected variable in SET clause")?;
        self.expect(&Tok::Dot, "expected '.' after variable in SET")?;
        let field = self.ident("expected field name after '.'")?;
        self.expect(&Tok::Eq, "expected '=' in SET clause")?;
        // Accept Lit, Param, BinArith (arithmetic expression), and FuncCall on
        // the RHS. Bare Prop/Var (e.g. `SET n.x = m.y`) remain a named error
        // because that form requires join semantics not supported in v1; use
        // an arithmetic expression like `m.y + 0` if needed.
        let value = match self.peek() {
            Some(
                Tok::Int(_)
                | Tok::Float(_)
                | Tok::Str(_)
                | Tok::Param(_)
                | Tok::Dash
                | Tok::Ident(_),
            ) => {
                let op = self.arith_expr()?;
                match &op {
                    Operand::Lit(_)
                    | Operand::Param(_)
                    | Operand::BinArith { .. }
                    | Operand::FuncCall { .. }
                    | Operand::Case { .. } => op,
                    Operand::Prop { .. } | Operand::Var(_) => {
                        return Err(self.err(
                            "SET RHS: bare property/variable reference is not supported; \
                             use a literal, $parameter, or arithmetic expression (e.g. n.x + 1)",
                        ));
                    }
                }
            }
            // List-literal RHS: `SET n.tags = ['a', 'b']`. Lists are pure
            // literals (no arithmetic), so parse directly into `Operand::Lit`.
            Some(Tok::LBracket) => Operand::Lit(self.literal_value("SET value")?),
            _ => {
                return Err(
                    self.err("expected literal, $parameter, or arithmetic expression as SET value")
                )
            }
        };
        Ok(SetClause { var, field, value })
    }

    /// Find the rel-var `var` in `patterns` and return its etype, src node var,
    /// and dst node var.  Returns Err if the var is not a rel var or has no type.
    fn resolve_edge_var(&self, var: &str, patterns: &[Pattern]) -> Result<EdgeDelete, String> {
        for pat in patterns {
            let start_var = pat.start.var.as_deref().unwrap_or("_unknown");
            let mut from_var = start_var;
            for (rel, dest) in &pat.chain {
                let to_var = dest.var.as_deref().unwrap_or("_unknown");
                if rel.var.as_deref() == Some(var) {
                    let etype = match rel.etypes.as_slice() {
                        [t] => t.clone(),
                        [] => {
                            return Err(format!(
                                "DELETE `{var}`: relationship has no type; \
                                 DELETE requires an explicit edge type (e.g., [r:TYPE])"
                            ))
                        }
                        _ => {
                            return Err(format!(
                                "DELETE `{var}`: relationship has multiple types; \
                                 DELETE requires a single explicit edge type (e.g., [r:TYPE])"
                            ))
                        }
                    };
                    let (src_var, dst_var) = match rel.dir {
                        RelDir::Right => (from_var.to_string(), to_var.to_string()),
                        RelDir::Left => (to_var.to_string(), from_var.to_string()),
                        RelDir::Undirected => {
                            return Err(format!(
                                "DELETE `{var}`: undirected relationship DELETE is not supported; \
                                 use a directed pattern (e.g., -[r:TYPE]->)"
                            ))
                        }
                    };
                    return Ok(EdgeDelete {
                        rel_var: var.to_string(),
                        etype,
                        src_var,
                        dst_var,
                    });
                }
                from_var = to_var;
            }
        }
        Err(format!(
            "DELETE `{var}`: variable is not bound as a relationship in any MATCH pattern; \
             only relationship variables can be deleted (DELETE edge vars, not node vars)"
        ))
    }

    /// Return `true` if `var` is bound as a node variable in `patterns`.
    fn is_node_var(&self, var: &str, patterns: &[Pattern]) -> bool {
        for pat in patterns {
            if pat.start.var.as_deref() == Some(var) {
                return true;
            }
            for (_, dest) in &pat.chain {
                if dest.var.as_deref() == Some(var) {
                    return true;
                }
            }
        }
        false
    }

    /// Parse a comma-separated list of node-variable targets for
    /// `[DETACH] DELETE`.  All targets must be node variables bound in `patterns`.
    fn node_delete_targets(&mut self, patterns: &[Pattern]) -> Result<Vec<String>, String> {
        let mut vars = Vec::new();
        loop {
            let var = self.ident("expected node variable to DELETE")?;
            if !self.is_node_var(&var, patterns) {
                return Err(format!(
                    "DELETE `{var}`: variable is not bound as a node in any MATCH pattern"
                ));
            }
            vars.push(var);
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        Ok(vars)
    }

    /// Try to parse DELETE targets as edge vars; if the first target is a node
    /// var, fall back to parsing all targets as node vars.
    fn delete_targets_or_node(
        &mut self,
        patterns: &[Pattern],
    ) -> Result<DeleteTargetResult, String> {
        // Peek at the identifier to decide which path to take.
        let var = self.ident("expected variable to DELETE")?;
        // Try edge var first.
        match self.resolve_edge_var(&var, patterns) {
            Ok(edge_del) => {
                // At least the first target is an edge var; parse the rest as
                // edge vars too.
                let mut targets = vec![edge_del];
                while self.eat(&Tok::Comma) {
                    let v = self.ident("expected variable to DELETE")?;
                    targets.push(self.resolve_edge_var(&v, patterns)?);
                }
                Ok(DeleteTargetResult::Edges(targets))
            }
            Err(_) => {
                // Not an edge var; try as node var.
                if self.is_node_var(&var, patterns) {
                    let mut node_vars = vec![var];
                    while self.eat(&Tok::Comma) {
                        let v = self.ident("expected variable to DELETE")?;
                        if !self.is_node_var(&v, patterns) {
                            return Err(format!(
                                "DELETE `{v}`: variable is not bound as a node in any MATCH pattern"
                            ));
                        }
                        node_vars.push(v);
                    }
                    Ok(DeleteTargetResult::Nodes(node_vars))
                } else {
                    Err(format!(
                        "DELETE `{var}`: variable is not bound as a relationship or node \
                         in any MATCH pattern"
                    ))
                }
            }
        }
    }
}

/// Used internally by `delete_targets_or_node` to signal whether the targets
/// resolved to edge variables or node variables.
enum DeleteTargetResult {
    Edges(Vec<EdgeDelete>),
    Nodes(Vec<String>),
}

#[cfg(test)]
mod tests {
    use super::parse;
    use crate::cypher::ast::{
        AggFunc, Expr, HopRange, LimitSkip, NodePat, Operand, OrderItem, OrderTarget, Pattern,
        Query, RelDir, RelPat, RetItem, RetVal,
    };
    use crate::cypher::{lex, Tok};
    use crate::filter::CmpOp;
    use core_storage::Value;

    fn parse_src(src: &str) -> Result<Query, String> {
        parse(&lex(src)?)
    }

    fn prop(var: &str, field: &str) -> Operand {
        Operand::Prop {
            var: var.into(),
            field: field.into(),
        }
    }

    fn cmp(lhs: Operand, op: CmpOp, rhs: Operand) -> Expr {
        Expr::Cmp { lhs, op, rhs }
    }

    fn node(var: Option<&str>, label: Option<&str>, props: Vec<(String, Operand)>) -> NodePat {
        NodePat {
            var: var.map(str::to_string),
            label: label.map(str::to_string),
            props,
        }
    }

    #[test]
    fn full_feature_query_exact_ast() {
        let src = "\
MATCH (a:Person {name: $n, age: 30})-[r:KNOWS]->(b)-[u:TEAM]-(c)<-[s:LIKES]-(d) \
WHERE NOT a.age < 18 AND b.name = 'x' OR c.score >= 2.5 \
RETURN a, r.since AS since, b.name \
ORDER BY since DESC, b.name ASC \
SKIP 1 LIMIT 5";
        let got = parse_src(src).expect("full-feature query must parse");
        let expected = Query {
            matches: vec![Pattern {
                start: node(
                    Some("a"),
                    Some("Person"),
                    vec![
                        ("name".into(), Operand::Param("n".into())),
                        ("age".into(), Operand::Lit(Value::Int(30))),
                    ],
                ),
                chain: vec![
                    (
                        RelPat {
                            var: Some("r".into()),
                            etypes: vec!["KNOWS".into()],
                            dir: RelDir::Right,
                            hops: None,
                        },
                        node(Some("b"), None, vec![]),
                    ),
                    (
                        RelPat {
                            var: Some("u".into()),
                            etypes: vec!["TEAM".into()],
                            dir: RelDir::Undirected,
                            hops: None,
                        },
                        node(Some("c"), None, vec![]),
                    ),
                    (
                        RelPat {
                            var: Some("s".into()),
                            etypes: vec!["LIKES".into()],
                            dir: RelDir::Left,
                            hops: None,
                        },
                        node(Some("d"), None, vec![]),
                    ),
                ],
                shortest: false,
            }],
            optional_clauses: vec![],
            unwinds: vec![],
            post_unwind_where: None,
            where_expr: Some(Expr::Or(
                Box::new(Expr::And(
                    Box::new(Expr::Not(Box::new(cmp(
                        prop("a", "age"),
                        CmpOp::Lt,
                        Operand::Lit(Value::Int(18)),
                    )))),
                    Box::new(cmp(
                        prop("b", "name"),
                        CmpOp::Eq,
                        Operand::Lit(Value::Str("x".into())),
                    )),
                )),
                Box::new(cmp(
                    prop("c", "score"),
                    CmpOp::Ge,
                    Operand::Lit(Value::Float(2.5)),
                )),
            )),
            stages: vec![],
            returns: vec![
                RetItem {
                    value: RetVal::Var("a".into()),
                    alias: None,
                },
                RetItem {
                    value: RetVal::Prop {
                        var: "r".into(),
                        field: "since".into(),
                    },
                    alias: Some("since".into()),
                },
                RetItem {
                    value: RetVal::Prop {
                        var: "b".into(),
                        field: "name".into(),
                    },
                    alias: None,
                },
            ],
            order_by: vec![
                OrderItem {
                    target: OrderTarget::Alias("since".into()),
                    descending: true,
                },
                OrderItem {
                    target: OrderTarget::Prop {
                        var: "b".into(),
                        field: "name".into(),
                    },
                    descending: false,
                },
            ],
            distinct: false,
            skip: Some(LimitSkip::Exact(1)),
            limit: Some(LimitSkip::Exact(5)),
        };
        assert_eq!(got, expected);
    }

    #[test]
    fn rel_direction_right() {
        let q = parse_src("MATCH (a)-[r:T]->(b) RETURN a").unwrap();
        assert_eq!(q.matches[0].chain[0].0.dir, RelDir::Right);
        assert_eq!(q.matches[0].chain[0].0.var.as_deref(), Some("r"));
        assert_eq!(q.matches[0].chain[0].0.etypes, vec!["T".to_string()]);
    }

    #[test]
    fn rel_direction_left() {
        let q = parse_src("MATCH (a)<-[r:T]-(b) RETURN a").unwrap();
        assert_eq!(q.matches[0].chain[0].0.dir, RelDir::Left);
    }

    #[test]
    fn rel_direction_undirected() {
        let q = parse_src("MATCH (a)-[r:T]-(b) RETURN a").unwrap();
        assert_eq!(q.matches[0].chain[0].0.dir, RelDir::Undirected);
    }

    #[test]
    fn node_props_map_with_param() {
        let q = parse_src("MATCH (t:Talent {id: $tid, n: 1, s: 'x'}) RETURN t").unwrap();
        assert_eq!(
            q.matches[0].start.props,
            vec![
                ("id".into(), Operand::Param("tid".into())),
                ("n".into(), Operand::Lit(Value::Int(1))),
                ("s".into(), Operand::Lit(Value::Str("x".into()))),
            ]
        );
        assert_eq!(q.matches[0].start.var.as_deref(), Some("t"));
        assert_eq!(q.matches[0].start.label.as_deref(), Some("Talent"));
    }

    #[test]
    fn operator_precedence_or_and_not() {
        let q = parse_src("MATCH (a) WHERE a.x = 1 OR b.y = 2 AND NOT c.z = 3 RETURN a").unwrap();
        let expected = Expr::Or(
            Box::new(cmp(prop("a", "x"), CmpOp::Eq, Operand::Lit(Value::Int(1)))),
            Box::new(Expr::And(
                Box::new(cmp(prop("b", "y"), CmpOp::Eq, Operand::Lit(Value::Int(2)))),
                Box::new(Expr::Not(Box::new(cmp(
                    prop("c", "z"),
                    CmpOp::Eq,
                    Operand::Lit(Value::Int(3)),
                )))),
            )),
        );
        assert_eq!(q.where_expr, Some(expected));
    }

    #[test]
    fn unary_minus_folds_numeric_literals() {
        let q = parse_src("MATCH (a) WHERE a.x > -5 AND a.y < -1.5 RETURN a").unwrap();
        let expected = Expr::And(
            Box::new(cmp(prop("a", "x"), CmpOp::Gt, Operand::Lit(Value::Int(-5)))),
            Box::new(cmp(
                prop("a", "y"),
                CmpOp::Lt,
                Operand::Lit(Value::Float(-1.5)),
            )),
        );
        assert_eq!(q.where_expr, Some(expected));
    }

    fn assert_parse_err(src: &str) {
        let result = std::panic::catch_unwind(|| parse_src(src));
        assert!(result.is_ok(), "parse({src:?}) panicked");
        let err = result
            .unwrap()
            .expect_err(&format!("parse({src:?}) must be Err"));
        // Parse errors say "token"; lex errors say "position". Either is a valid reject.
        assert!(
            err.contains("token") || err.contains("position"),
            "error must include token or lex position, got: {err}"
        );
    }

    #[test]
    fn malformed_match_alone_is_err() {
        assert_parse_err("MATCH");
    }

    #[test]
    fn malformed_missing_return_is_err() {
        assert_parse_err("MATCH (n)");
    }

    #[test]
    fn malformed_rel_colon_without_type_is_err() {
        assert_parse_err("MATCH (a)-[x:]->(b) RETURN a");
    }

    #[test]
    fn malformed_dangling_comma_in_return_is_err() {
        assert_parse_err("MATCH (n) RETURN n,");
    }

    #[test]
    fn malformed_unclosed_paren_is_err() {
        assert_parse_err("MATCH (n RETURN n");
        assert_parse_err("MATCH (n) WHERE (a.x = 1 RETURN n");
    }

    #[test]
    fn malformed_order_by_before_return_is_err() {
        assert_parse_err("MATCH (n) ORDER BY n RETURN n");
    }

    #[test]
    fn malformed_garbage_after_limit_is_err() {
        assert_parse_err("MATCH (n) RETURN n LIMIT 1 extra");
    }

    #[test]
    fn dogfood_query_exact_ast() {
        let src = "\
MATCH (t:Talent {id: $tid}) \
MATCH (c:Company)-[i:INDUSTRY_ALIGNMENT]->(t) \
MATCH (c)-[s:SPECIALTY_MATCH]->(t) \
WHERE i.score >= 0.5 AND s.score >= 0.5 \
RETURN c, i.score AS industry, s.score AS specialty \
ORDER BY industry DESC, specialty DESC \
LIMIT 10";
        let got = parse_src(src).expect("dogfood query must parse");
        let expected = Query {
            matches: vec![
                Pattern {
                    start: node(
                        Some("t"),
                        Some("Talent"),
                        vec![("id".into(), Operand::Param("tid".into()))],
                    ),
                    chain: vec![],
                    shortest: false,
                },
                Pattern {
                    start: node(Some("c"), Some("Company"), vec![]),
                    chain: vec![(
                        RelPat {
                            var: Some("i".into()),
                            etypes: vec!["INDUSTRY_ALIGNMENT".into()],
                            dir: RelDir::Right,
                            hops: None,
                        },
                        node(Some("t"), None, vec![]),
                    )],
                    shortest: false,
                },
                Pattern {
                    start: node(Some("c"), None, vec![]),
                    chain: vec![(
                        RelPat {
                            var: Some("s".into()),
                            etypes: vec!["SPECIALTY_MATCH".into()],
                            dir: RelDir::Right,
                            hops: None,
                        },
                        node(Some("t"), None, vec![]),
                    )],
                    shortest: false,
                },
            ],
            optional_clauses: vec![],
            unwinds: vec![],
            post_unwind_where: None,
            where_expr: Some(Expr::And(
                Box::new(cmp(
                    prop("i", "score"),
                    CmpOp::Ge,
                    Operand::Lit(Value::Float(0.5)),
                )),
                Box::new(cmp(
                    prop("s", "score"),
                    CmpOp::Ge,
                    Operand::Lit(Value::Float(0.5)),
                )),
            )),
            stages: vec![],
            returns: vec![
                RetItem {
                    value: RetVal::Var("c".into()),
                    alias: None,
                },
                RetItem {
                    value: RetVal::Prop {
                        var: "i".into(),
                        field: "score".into(),
                    },
                    alias: Some("industry".into()),
                },
                RetItem {
                    value: RetVal::Prop {
                        var: "s".into(),
                        field: "score".into(),
                    },
                    alias: Some("specialty".into()),
                },
            ],
            order_by: vec![
                OrderItem {
                    target: OrderTarget::Alias("industry".into()),
                    descending: true,
                },
                OrderItem {
                    target: OrderTarget::Alias("specialty".into()),
                    descending: true,
                },
            ],
            distinct: false,
            skip: None,
            limit: Some(LimitSkip::Exact(10)),
        };
        assert_eq!(got, expected);
    }

    #[test]
    fn unary_minus_in_props_and_dash_elsewhere_is_err() {
        let q = parse_src("MATCH (a {x: -5, y: -1.5}) RETURN a").unwrap();
        assert_eq!(
            q.matches[0].start.props,
            vec![
                ("x".into(), Operand::Lit(Value::Int(-5))),
                ("y".into(), Operand::Lit(Value::Float(-1.5))),
            ]
        );
        // `1 - 2` is now valid arithmetic — no longer a parse error.
        let q2 = parse_src("MATCH (a) WHERE a.x = 1 - 2 RETURN a").unwrap();
        assert!(q2.where_expr.is_some());
        // Unary minus on non-literal remains an error.
        assert_parse_err("MATCH (a) WHERE a.x > -b.y RETURN a");
        assert_parse_err("MATCH (a) RETURN a SKIP -1");
    }

    // ── IS NULL / IS NOT NULL ──────────────────────────────────────────────────

    #[test]
    fn is_null_parses_on_prop() {
        let q = parse_src("MATCH (a) WHERE a.x IS NULL RETURN a").unwrap();
        assert_eq!(q.where_expr, Some(Expr::IsNull(prop("a", "x"))),);
    }

    #[test]
    fn is_not_null_parses_on_prop() {
        let q = parse_src("MATCH (a) WHERE a.x IS NOT NULL RETURN a").unwrap();
        assert_eq!(q.where_expr, Some(Expr::IsNotNull(prop("a", "x"))),);
    }

    #[test]
    fn is_null_on_var() {
        let q =
            parse_src("MATCH (a) OPTIONAL MATCH (a)-[:T]->(b) WITH a, b WHERE b IS NULL RETURN a")
                .unwrap();
        // The IS NULL filter lives on the first WITH stage's where_expr.
        let stage = &q.stages[0];
        assert_eq!(
            stage.where_expr,
            Some(Expr::IsNull(Operand::Var("b".into()))),
        );
    }

    #[test]
    fn is_null_case_insensitive() {
        let q = parse_src("MATCH (a) WHERE a.x is null RETURN a").unwrap();
        assert_eq!(q.where_expr, Some(Expr::IsNull(prop("a", "x"))));
        let q2 = parse_src("MATCH (a) WHERE a.x IS NOT NULL RETURN a").unwrap();
        assert_eq!(q2.where_expr, Some(Expr::IsNotNull(prop("a", "x"))));
    }

    #[test]
    fn is_null_combined_with_and() {
        let q = parse_src("MATCH (a) WHERE a.x IS NULL AND a.y > 5 RETURN a").unwrap();
        assert!(matches!(q.where_expr, Some(Expr::And(_, _))));
    }

    // ── Arithmetic expression parsing ──────────────────────────────────────────

    #[test]
    fn arith_add_in_where() {
        use crate::cypher::ast::ArithOp;
        let q = parse_src("MATCH (n) WHERE n.age + 1 > 5 RETURN n").unwrap();
        let expected_lhs = Operand::BinArith {
            op: ArithOp::Add,
            left: Box::new(prop("n", "age")),
            right: Box::new(Operand::Lit(Value::Int(1))),
        };
        assert_eq!(
            q.where_expr,
            Some(Expr::Cmp {
                lhs: expected_lhs,
                op: CmpOp::Gt,
                rhs: Operand::Lit(Value::Int(5)),
            })
        );
    }

    #[test]
    fn arith_precedence_mul_over_add() {
        use crate::cypher::ast::ArithOp;
        // 1 + 2 * 3  should parse as  1 + (2 * 3)
        let q = parse_src("MATCH (n) WHERE n.x = 1 + 2 * 3 RETURN n").unwrap();
        let expected_rhs = Operand::BinArith {
            op: ArithOp::Add,
            left: Box::new(Operand::Lit(Value::Int(1))),
            right: Box::new(Operand::BinArith {
                op: ArithOp::Mul,
                left: Box::new(Operand::Lit(Value::Int(2))),
                right: Box::new(Operand::Lit(Value::Int(3))),
            }),
        };
        assert_eq!(
            q.where_expr,
            Some(Expr::Cmp {
                lhs: prop("n", "x"),
                op: CmpOp::Eq,
                rhs: expected_rhs,
            })
        );
    }

    #[test]
    fn arith_parens_override_precedence() {
        use crate::cypher::ast::ArithOp;
        // In RETURN position: (1+2)*3 should parse as (1+2)*3
        let q = parse_src("MATCH (n) RETURN (1 + 2) * 3 AS r").unwrap();
        let expected = RetVal::ScalarExpr(Operand::BinArith {
            op: ArithOp::Mul,
            left: Box::new(Operand::BinArith {
                op: ArithOp::Add,
                left: Box::new(Operand::Lit(Value::Int(1))),
                right: Box::new(Operand::Lit(Value::Int(2))),
            }),
            right: Box::new(Operand::Lit(Value::Int(3))),
        });
        assert_eq!(q.returns[0].value, expected);
        assert_eq!(q.returns[0].alias, Some("r".into()));
    }

    #[test]
    fn arith_scalar_expr_in_return() {
        use crate::cypher::ast::ArithOp;
        let q = parse_src("MATCH (n) RETURN n.age + 1 AS adjusted").unwrap();
        let expected = RetVal::ScalarExpr(Operand::BinArith {
            op: ArithOp::Add,
            left: Box::new(prop("n", "age")),
            right: Box::new(Operand::Lit(Value::Int(1))),
        });
        assert_eq!(q.returns[0].value, expected);
        assert_eq!(q.returns[0].alias, Some("adjusted".into()));
    }

    #[test]
    fn arith_div_in_where() {
        use crate::cypher::ast::ArithOp;
        let q = parse_src("MATCH (n) WHERE n.x / 2 > 3 RETURN n").unwrap();
        assert!(matches!(
            q.where_expr,
            Some(Expr::Cmp {
                lhs: Operand::BinArith {
                    op: ArithOp::Div,
                    ..
                },
                ..
            })
        ));
    }

    // ── CREATE...RETURN and MERGE...RETURN parser tests ────────────────────────

    #[test]
    fn create_return_parses_node_var() {
        use super::parse_write;
        use crate::cypher::ast::{RetVal, WriteStatement};

        let toks = crate::cypher::lex("CREATE (n:Thing {id: 'x'}) RETURN n").unwrap();
        let stmt = parse_write(&toks).unwrap();
        match stmt {
            WriteStatement::Create(s) => {
                assert_eq!(s.nodes.len(), 1);
                let returns = s.returns.expect("expected RETURN clause");
                assert_eq!(returns.len(), 1);
                assert_eq!(returns[0].value, RetVal::Var("n".into()));
            }
            _ => panic!("expected Create"),
        }
    }

    #[test]
    fn create_return_prop_with_alias() {
        use super::parse_write;
        use crate::cypher::ast::{RetVal, WriteStatement};

        let toks = crate::cypher::lex("CREATE (n:Thing {id: 'x'}) RETURN n.id AS node_id").unwrap();
        let stmt = parse_write(&toks).unwrap();
        match stmt {
            WriteStatement::Create(s) => {
                let returns = s.returns.expect("RETURN required");
                assert_eq!(
                    returns[0].value,
                    RetVal::Prop {
                        var: "n".into(),
                        field: "id".into()
                    }
                );
                assert_eq!(returns[0].alias, Some("node_id".into()));
            }
            _ => panic!("expected Create"),
        }
    }

    #[test]
    fn merge_return_parses_node_var() {
        use super::parse_write;
        use crate::cypher::ast::{RetVal, WriteStatement};

        let toks = crate::cypher::lex("MERGE (n:Thing {id: 'x'}) RETURN n").unwrap();
        let stmt = parse_write(&toks).unwrap();
        match stmt {
            WriteStatement::Merge(s) => {
                assert_eq!(s.var, Some("n".into()));
                let returns = s.returns.expect("RETURN required");
                assert_eq!(returns[0].value, RetVal::Var("n".into()));
            }
            _ => panic!("expected Merge"),
        }
    }

    #[test]
    fn is_write_tokens_still_true_for_create_return() {
        use super::is_write_tokens;
        use crate::cypher::lex;

        let toks = lex("CREATE (n:T {id: 'x'}) RETURN n").unwrap();
        assert!(
            is_write_tokens(&toks),
            "CREATE...RETURN must still be classified as write"
        );
    }

    #[test]
    fn where_in_list_and_param_parses() {
        let q = parse_src("MATCH (n) WHERE n.city IN ['Austin', $c] RETURN n").unwrap();
        match q.where_expr {
            Some(Expr::In { expr, list }) => {
                assert_eq!(
                    expr,
                    Operand::Prop {
                        var: "n".into(),
                        field: "city".into()
                    }
                );
                assert_eq!(list.len(), 2);
                assert_eq!(list[0], Operand::Lit(Value::Str("Austin".into())));
                assert_eq!(list[1], Operand::Param("c".into()));
            }
            other => panic!("expected Expr::In, got {other:?}"),
        }
        let q2 = parse_src("MATCH (n) WHERE n.city IN $cities RETURN n").unwrap();
        match q2.where_expr {
            Some(Expr::In { list, .. }) => {
                assert_eq!(list, vec![Operand::Param("cities".into())]);
            }
            other => panic!("expected Expr::In, got {other:?}"),
        }
    }

    #[test]
    fn return_distinct_parses() {
        let q = parse_src("MATCH (n) RETURN DISTINCT n.city").unwrap();
        assert!(q.distinct);
        assert_eq!(q.returns.len(), 1);
    }

    #[test]
    fn union_query_parses_into_parts() {
        use super::parse_read;
        let u = parse_read(&lex("MATCH (n) RETURN n UNION ALL MATCH (m) RETURN m").unwrap())
            .expect("UNION parses");
        assert_eq!(u.parts.len(), 2);
        assert_eq!(u.all_flags, vec![true]);
        // A single query yields one part, no boundaries.
        let single = parse_read(&lex("MATCH (n) RETURN n").unwrap()).unwrap();
        assert_eq!(single.parts.len(), 1);
        assert!(single.all_flags.is_empty());
    }

    #[test]
    fn case_when_expression_parses() {
        let q = parse_src("MATCH (n) RETURN CASE WHEN n.x = 1 THEN 2 ELSE 3 END AS c")
            .expect("CASE parses");
        assert!(matches!(
            q.returns[0].value,
            RetVal::ScalarExpr(Operand::Case { .. })
        ));
    }

    #[test]
    fn collect_is_a_supported_aggregate() {
        let q = parse_src("MATCH (n) RETURN collect(n.name) AS names").expect("collect parses");
        assert!(matches!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Collect,
                ..
            }
        ));
    }

    #[test]
    fn match_set_return_parses() {
        use super::parse_write;
        use crate::cypher::ast::{RetVal, WriteStatement};

        let toks = crate::cypher::lex("MATCH (n {id:'a'}) SET n.x = 2 RETURN n.x").unwrap();
        let stmt = parse_write(&toks).unwrap();
        match stmt {
            WriteStatement::MatchSet(s) => {
                let returns = s.returns.expect("RETURN required");
                assert_eq!(
                    returns[0].value,
                    RetVal::Prop {
                        var: "n".into(),
                        field: "x".into()
                    }
                );
            }
            other => panic!("expected MatchSet, got {other:?}"),
        }
    }

    #[test]
    fn merge_on_create_and_on_match_parse() {
        use super::parse_write;
        use crate::cypher::ast::WriteStatement;

        let toks = crate::cypher::lex(
            "MERGE (n:L {id:'new'}) ON CREATE SET n.born = 1 ON MATCH SET n.hit = 1 RETURN n",
        )
        .unwrap();
        let stmt = parse_write(&toks).unwrap();
        match stmt {
            WriteStatement::Merge(s) => {
                assert_eq!(s.on_create.len(), 1);
                assert_eq!(s.on_create[0].field, "born");
                assert_eq!(s.on_match.len(), 1);
                assert_eq!(s.on_match[0].field, "hit");
                assert!(s.returns.is_some());
            }
            other => panic!("expected Merge, got {other:?}"),
        }
    }

    #[test]
    fn paren_grouping_and_and_left_assoc() {
        let q = parse_src("MATCH (a) WHERE (a.x = 1 OR a.y = 2) AND a.z = 3 RETURN a").unwrap();
        let expected = Expr::And(
            Box::new(Expr::Or(
                Box::new(cmp(prop("a", "x"), CmpOp::Eq, Operand::Lit(Value::Int(1)))),
                Box::new(cmp(prop("a", "y"), CmpOp::Eq, Operand::Lit(Value::Int(2)))),
            )),
            Box::new(cmp(prop("a", "z"), CmpOp::Eq, Operand::Lit(Value::Int(3)))),
        );
        assert_eq!(q.where_expr, Some(expected));

        let q = parse_src("MATCH (a) WHERE a.x = 1 AND a.y = 2 AND a.z = 3 RETURN a").unwrap();
        let expected = Expr::And(
            Box::new(Expr::And(
                Box::new(cmp(prop("a", "x"), CmpOp::Eq, Operand::Lit(Value::Int(1)))),
                Box::new(cmp(prop("a", "y"), CmpOp::Eq, Operand::Lit(Value::Int(2)))),
            )),
            Box::new(cmp(prop("a", "z"), CmpOp::Eq, Operand::Lit(Value::Int(3)))),
        );
        assert_eq!(q.where_expr, Some(expected));
    }

    #[test]
    fn order_by_bare_ident_is_var_when_not_an_alias() {
        let q = parse_src("MATCH (a) RETURN a, b.name ORDER BY a, b.name").unwrap();
        assert_eq!(
            q.order_by,
            vec![
                OrderItem {
                    target: OrderTarget::Var("a".into()),
                    descending: false,
                },
                OrderItem {
                    target: OrderTarget::Prop {
                        var: "b".into(),
                        field: "name".into(),
                    },
                    descending: false,
                },
            ]
        );
    }

    #[test]
    fn parse_never_panics_on_token_sequences() {
        let sequences: Vec<Vec<Tok>> = vec![
            vec![],
            vec![Tok::Match],
            vec![Tok::Return],
            vec![Tok::Dash, Tok::Dash, Tok::Dash],
            vec![Tok::Lt, Tok::Gt, Tok::Eq],
            vec![Tok::LParen, Tok::RParen, Tok::RParen],
            vec![Tok::Int(1), Tok::Float(2.0), Tok::Str("x".into())],
            vec![Tok::Where, Tok::Not, Tok::And, Tok::Or],
            vec![Tok::Order, Tok::By, Tok::Asc, Tok::Desc],
            vec![Tok::Skip, Tok::Limit, Tok::As],
            vec![Tok::Ident("n".into()), Tok::Dot, Tok::Ident("x".into())],
            vec![Tok::Param("p".into()), Tok::Colon, Tok::Comma],
            vec![Tok::LBracket, Tok::RBracket, Tok::LBrace, Tok::RBrace],
            lex("MATCH (a)-[x:]->(b) RETURN a ORDER BY a LIMIT 1 extra").unwrap(),
            // Aggregate tokens: COUNT(*), SUM/AVG/MIN/MAX in various positions.
            vec![
                Tok::Ident("COUNT".into()),
                Tok::LParen,
                Tok::Star,
                Tok::RParen,
            ],
            vec![
                Tok::Ident("sum".into()),
                Tok::LParen,
                Tok::Ident("n".into()),
                Tok::Dot,
                Tok::Ident("x".into()),
                Tok::RParen,
            ],
            vec![Tok::Star],
            vec![Tok::Star, Tok::LParen, Tok::RParen, Tok::Star],
            vec![
                Tok::Ident("avg".into()),
                Tok::LParen,
                Tok::Star,
                Tok::RParen,
            ],
            vec![Tok::Ident("min".into()), Tok::LParen, Tok::RParen],
            vec![
                Tok::Ident("max".into()),
                Tok::LParen,
                Tok::Star,
                Tok::RParen,
            ],
        ];
        for toks in sequences {
            let result = std::panic::catch_unwind(|| parse(&toks));
            assert!(result.is_ok(), "parse panicked on token sequence {toks:?}");
            let parsed = result.unwrap();
            if let Err(err) = parsed {
                assert!(
                    err.contains("token"),
                    "error must include token position, got: {err}"
                );
            }
        }
    }

    #[test]
    fn aggregate_functions_parse_to_agg_retval() {
        use crate::cypher::ast::{AggArg, AggFunc, RetVal};

        // COUNT(*) → AggFunc::Count, AggArg::Star
        let q = parse_src("MATCH (n) RETURN COUNT(*)").unwrap();
        assert_eq!(q.returns.len(), 1);
        assert_eq!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Count,
                arg: AggArg::Star,
            }
        );
        assert_eq!(q.returns[0].alias, None);

        // COUNT(n) → AggFunc::Count, AggArg::Var
        let q = parse_src("MATCH (n) RETURN COUNT(n)").unwrap();
        assert_eq!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Count,
                arg: AggArg::Var("n".into()),
            }
        );

        // SUM(n.age) → AggFunc::Sum, AggArg::Prop
        let q = parse_src("MATCH (n) RETURN SUM(n.age) AS total").unwrap();
        assert_eq!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Sum,
                arg: AggArg::Prop {
                    var: "n".into(),
                    field: "age".into()
                },
            }
        );
        assert_eq!(q.returns[0].alias, Some("total".into()));

        // AVG, MIN, MAX case-insensitive
        let q = parse_src("MATCH (n) RETURN avg(n.score)").unwrap();
        assert!(matches!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Avg,
                ..
            }
        ));
        let q = parse_src("MATCH (n) RETURN Min(n.x)").unwrap();
        assert!(matches!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Min,
                ..
            }
        ));
        let q = parse_src("MATCH (n) RETURN MAX(n.x)").unwrap();
        assert!(matches!(
            q.returns[0].value,
            RetVal::Agg {
                func: AggFunc::Max,
                ..
            }
        ));
    }

    #[test]
    fn nested_parens_beyond_limit_is_err_not_panic() {
        let mut src = String::from("MATCH (a) WHERE ");
        for _ in 0..80 {
            src.push('(');
        }
        src.push_str("a.x = 1");
        for _ in 0..80 {
            src.push(')');
        }
        src.push_str(" RETURN a");
        assert_parse_err(&src);
    }

    // ── Variable-length path parser tests ─────────────────────────────────────

    fn hop_range_of(src: &str) -> HopRange {
        let q = parse_src(src).expect(src);
        let (rel, _) = &q.matches[0].chain[0];
        rel.hops.expect("expected hop range")
    }

    fn assert_hop_err(src: &str, needle: &str) {
        let result = std::panic::catch_unwind(|| parse_src(src));
        assert!(result.is_ok(), "parse panicked on {src:?}");
        let err = result
            .unwrap()
            .expect_err(&format!("parse({src:?}) must Err"));
        assert!(
            err.contains(needle),
            "error must contain {needle:?}, got: {err}"
        );
    }

    #[test]
    fn var_length_bare_star_is_one_to_ten() {
        let r = hop_range_of("MATCH (a)-[r:T*]->(b) RETURN a");
        assert_eq!(r, HopRange { min: 1, max: 10 });
    }

    #[test]
    fn var_length_exact_n_hops() {
        let r = hop_range_of("MATCH (a)-[r:T*3]->(b) RETURN a");
        assert_eq!(r, HopRange { min: 3, max: 3 });
    }

    #[test]
    fn var_length_min_max_range() {
        let r = hop_range_of("MATCH (a)-[r:T*2..5]->(b) RETURN a");
        assert_eq!(r, HopRange { min: 2, max: 5 });
    }

    #[test]
    fn var_length_dotdot_max() {
        let r = hop_range_of("MATCH (a)-[r:T*..4]->(b) RETURN a");
        assert_eq!(r, HopRange { min: 1, max: 4 });
    }

    #[test]
    fn var_length_cap_at_ten_is_ok() {
        let r = hop_range_of("MATCH (a)-[r:T*10]->(b) RETURN a");
        assert_eq!(r, HopRange { min: 10, max: 10 });
        let r2 = hop_range_of("MATCH (a)-[r:T*1..10]->(b) RETURN a");
        assert_eq!(r2, HopRange { min: 1, max: 10 });
    }

    #[test]
    fn var_length_cap_exceeded_is_err() {
        assert_hop_err(
            "MATCH (a)-[r:T*11]->(b) RETURN a",
            "variable-length paths are capped at 10 hops",
        );
        assert_hop_err(
            "MATCH (a)-[r:T*1..11]->(b) RETURN a",
            "variable-length paths are capped at 10 hops",
        );
    }

    #[test]
    fn var_length_unbounded_min_dot_dot_is_err() {
        assert_hop_err(
            "MATCH (a)-[r:T*2..]->(b) RETURN a",
            "variable-length paths are capped at 10 hops",
        );
    }

    #[test]
    fn var_length_shortest_path_parses() {
        let q =
            parse_src("MATCH (a:N) MATCH (b:N) MATCH shortestPath((a)-[r:T*..5]->(b)) RETURN a")
                .expect("shortestPath must parse");
        assert!(q.matches[2].shortest, "third match must be shortest=true");
        let (rel, _) = &q.matches[2].chain[0];
        assert_eq!(rel.hops, Some(HopRange { min: 1, max: 5 }));
        assert_eq!(rel.etypes, vec!["T".to_string()]);
    }

    #[test]
    fn var_length_no_type_is_ok() {
        // Bare `*` with no type filter
        let r = hop_range_of("MATCH (a)-[r*1..3]->(b) RETURN a");
        assert_eq!(r, HopRange { min: 1, max: 3 });
    }

    #[test]
    fn var_length_rel_appears_in_chain() {
        let q = parse_src("MATCH (a)-[r:T*2..4]->(b) RETURN a").unwrap();
        let (rel, dest) = &q.matches[0].chain[0];
        assert_eq!(rel.var.as_deref(), Some("r"));
        assert_eq!(rel.etypes, vec!["T".to_string()]);
        assert_eq!(rel.dir, RelDir::Right);
        assert_eq!(rel.hops, Some(HopRange { min: 2, max: 4 }));
        assert_eq!(dest.var.as_deref(), Some("b"));
    }

    #[test]
    fn var_length_zero_hop_minimum_is_err() {
        // `*0` — exact form with min=0
        assert_hop_err(
            "MATCH (a)-[r:T*0]->(b) RETURN a",
            "zero-length variable-length paths are not supported",
        );
        // `*0..3` — range form with min=0
        assert_hop_err(
            "MATCH (a)-[r:T*0..3]->(b) RETURN a",
            "zero-length variable-length paths are not supported",
        );
    }

    #[test]
    fn create_accepts_list_literal_property() {
        use super::parse_write;
        use crate::cypher::ast::WriteStatement;
        let src = "CREATE (n:Person {id: 'p1', tags: ['a', 'b']})";
        let stmt = parse_write(&lex(src).unwrap())
            .expect("CREATE with a list-literal property must parse");
        let WriteStatement::Create(c) = stmt else {
            panic!("expected a Create statement");
        };
        let tags = &c.nodes[0]
            .props
            .iter()
            .find(|(k, _)| k == "tags")
            .expect("tags property present")
            .1;
        assert_eq!(
            *tags,
            Value::List(vec![Value::Str("a".into()), Value::Str("b".into())])
        );
    }

    #[test]
    fn create_accepts_empty_and_nested_list_literals() {
        use super::parse_write;
        use crate::cypher::ast::WriteStatement;
        let src = "CREATE (n:L {id: 'p1', empty: [], nested: [[1, 2], [3]]})";
        let stmt = parse_write(&lex(src).unwrap()).expect("empty and nested lists must parse");
        let WriteStatement::Create(c) = stmt else {
            panic!("expected a Create statement");
        };
        let get = |k: &str| {
            c.nodes[0]
                .props
                .iter()
                .find(|(name, _)| name == k)
                .expect("property present")
                .1
                .clone()
        };
        assert_eq!(get("empty"), Value::List(vec![]));
        assert_eq!(
            get("nested"),
            Value::List(vec![
                Value::List(vec![Value::Int(1), Value::Int(2)]),
                Value::List(vec![Value::Int(3)]),
            ])
        );
    }

    #[test]
    fn set_accepts_list_literal_rhs() {
        use super::parse_write;
        use crate::cypher::ast::{Operand, WriteStatement};
        let src = "MATCH (n:Person {id: 'p1'}) SET n.tags = ['x', 'y']";
        let stmt = parse_write(&lex(src).unwrap()).expect("SET with a list-literal RHS must parse");
        let WriteStatement::MatchSet(m) = stmt else {
            panic!("expected a MatchSet statement");
        };
        let set = &m.sets[0];
        assert_eq!(set.field, "tags");
        assert_eq!(
            set.value,
            Operand::Lit(Value::List(vec![
                Value::Str("x".into()),
                Value::Str("y".into())
            ]))
        );
    }
}