jetro-core 0.5.10

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

use pest::iterators::Pair;
use pest::Parser as PestParser;
use pest_derive::Parser;
use std::{fmt, sync::Arc};

use super::ast::*;
use crate::data::value::Val;
use super::write_terminal::{
    build_patch_op, is_chain_write_terminal, steps_to_path as write_steps_to_path,
};

const INVALID_HAS_ARRAY_RHS: &str = "__jetro_invalid_has_array_rhs";

/// Pest-derived parser for the v2 grammar. The grammar file is embedded at
/// compile time via the `#[grammar]` attribute and is not loaded at runtime.
#[derive(Parser)]
#[grammar = "grammar.pest"]
pub struct V2Parser;

/// Returned by `parse` when the input does not conform to the grammar or when
/// a semantic constraint (e.g. unknown cast type) is violated.
#[derive(Debug)]
pub struct ParseError(pub String);

impl fmt::Display for ParseError {
    /// Format the error as a human-readable message including the source snippet.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "parse error: {}", self.0)
    }
}

impl std::error::Error for ParseError {}

impl From<pest::error::Error<Rule>> for ParseError {
    /// Convert a pest parse error into a `ParseError`, preserving the full
    /// location and message pest provides.
    fn from(e: pest::error::Error<Rule>) -> Self {
        ParseError(e.to_string())
    }
}

/// Parse a Jetro query string into an `Expr` AST. This is the primary public
/// entry point; all other `parse_*` functions are internal helpers.
///
/// In addition to grammar-level parsing, this entry point runs post-parse
/// semantic validation passes (currently or-pattern linearity for `match`
/// expressions) so callers see structural errors before the expression
/// reaches the compiler.
pub fn parse(input: &str) -> Result<Expr, ParseError> {
    let mut pairs = V2Parser::parse(Rule::program, input)?;
    let program = pairs.next().unwrap();
    let expr_pair = program.into_inner().next().unwrap();
    let expr = parse_expr(expr_pair);
    validate_has_array_rhs(&expr)?;
    validate_pattern_linearity(&expr)?;
    if strict_match_lint_enabled() {
        validate_match_exhaustiveness(&expr)?;
    }
    // First-class lambda macro expansion: `let f = (x => …) in …` inlines
    // `f` at every method-arg position so downstream compile sees the
    // lambda directly. Pure AST rewrite; no runtime closure value.
    let expr = crate::compile::lambda_lower::inline_let_bound_lambdas(expr);
    Ok(expr)
}

fn validate_has_array_rhs(expr: &Expr) -> Result<(), ParseError> {
    fn check(expr: &Expr) -> Result<(), ParseError> {
        match expr {
            Expr::Chain(base, steps) => {
                check(base)?;
                for step in steps {
                    match step {
                        Step::Method(name, _) if name == INVALID_HAS_ARRAY_RHS => {
                            return Err(ParseError(
                                "has [...] requires scalar literal elements".to_string(),
                            ));
                        }
                        Step::DynIndex(expr) | Step::InlineFilter(expr) => check(expr)?,
                        Step::Method(_, args) | Step::OptMethod(_, args) => {
                            for arg in args {
                                match arg {
                                    Arg::Pos(expr) | Arg::Named(_, expr) => check(expr)?,
                                }
                            }
                        }
                        Step::DeepMatch { arms, .. } => {
                            for arm in arms {
                                if let Some(guard) = &arm.guard {
                                    check(guard)?;
                                }
                                check(&arm.body)?;
                            }
                        }
                        _ => {}
                    }
                }
            }
            Expr::BinOp(lhs, _, rhs) | Expr::Coalesce(lhs, rhs) => {
                check(lhs)?;
                check(rhs)?;
            }
            Expr::UnaryNeg(inner) | Expr::Not(inner) => check(inner)?,
            Expr::Kind { expr, .. } => check(expr)?,
            Expr::Array(elems) => {
                for elem in elems {
                    match elem {
                        ArrayElem::Expr(expr) | ArrayElem::Spread(expr) => check(expr)?,
                    }
                }
            }
            Expr::Object(fields) => {
                for field in fields {
                    match field {
                        ObjField::Kv { val, cond, .. } => {
                            check(val)?;
                            if let Some(cond) = cond {
                                check(cond)?;
                            }
                        }
                        ObjField::Dynamic { key, val } => {
                            check(key)?;
                            check(val)?;
                        }
                        ObjField::Short(_) => {}
                        ObjField::Spread(expr) | ObjField::SpreadDeep(expr) => check(expr)?,
                    }
                }
            }
            Expr::Pipeline { base, steps } => {
                check(base)?;
                for step in steps {
                    match step {
                        PipeStep::Forward(expr) => check(expr)?,
                        PipeStep::Bind(_) => {}
                    }
                }
            }
            Expr::ListComp { expr, iter, cond, .. }
            | Expr::GenComp { expr, iter, cond, .. }
            | Expr::SetComp { expr, iter, cond, .. } => {
                check(expr)?;
                check(iter)?;
                if let Some(cond) = cond {
                    check(cond)?;
                }
            }
            Expr::DictComp {
                key,
                val,
                iter,
                cond,
                ..
            } => {
                check(key)?;
                check(val)?;
                check(iter)?;
                if let Some(cond) = cond {
                    check(cond)?;
                }
            }
            Expr::Lambda { body, .. } => check(body)?,
            Expr::Let { init, body, .. } => {
                check(init)?;
                check(body)?;
            }
            Expr::IfElse { cond, then_, else_ } => {
                check(cond)?;
                check(then_)?;
                check(else_)?;
            }
            Expr::Try { body, default } => {
                check(body)?;
                check(default)?;
            }
            Expr::GlobalCall { args, .. } => {
                for arg in args {
                    match arg {
                        Arg::Pos(expr) | Arg::Named(_, expr) => check(expr)?,
                    }
                }
            }
            Expr::Match { scrutinee, arms } => {
                check(scrutinee)?;
                for arm in arms {
                    if let Some(guard) = &arm.guard {
                        check(guard)?;
                    }
                    check(&arm.body)?;
                }
            }
            Expr::Cast { expr, .. } => check(expr)?,
            Expr::Patch { root, ops } => {
                check(root)?;
                for op in ops {
                    check_patch_op(op)?;
                }
            }
            Expr::UpdateBatch { root, ops, .. } => {
                check(root)?;
                for op in ops {
                    check_patch_op(op)?;
                }
            }
            Expr::DeleteMark => {}
            Expr::FString(parts) => {
                for part in parts {
                    if let FStringPart::Interp { expr, .. } = part {
                        check(expr)?;
                    }
                }
            }
            Expr::Null
            | Expr::Bool(_)
            | Expr::Int(_)
            | Expr::Float(_)
            | Expr::Str(_)
            | Expr::Root
            | Expr::Current
            | Expr::Ident(_) => {}
        }
        Ok(())
    }
    fn check_patch_op(op: &PatchOp) -> Result<(), ParseError> {
        check(&op.val)?;
        if let Some(cond) = &op.cond {
            check(cond)?;
        }
        Ok(())
    }
    check(expr)
}

/// Read the `JETRO_STRICT_MATCH` environment variable on every parse.
/// Toggling the flag at runtime is rare in practice, and reading it per
/// call lets tooling and tests flip the lint without restarting the
/// process.
fn strict_match_lint_enabled() -> bool {
    std::env::var_os("JETRO_STRICT_MATCH").is_some()
}

/// Recognise irrefutable patterns — those that match every value
/// regardless of shape and therefore qualify as a catch-all arm in the
/// exhaustiveness analysis. Beyond the trivial `Wild` and bare `Bind`
/// cases, this also accepts `Or` patterns whose alternative list
/// contains an irrefutable alt (since at least one alt fires for every
/// value) and `Kind`-bound patterns whose kind covers all runtime
/// types (currently never, but the structure is in place for future
/// extension to a `_: any` form).
fn pat_is_irrefutable(pat: &Pat) -> bool {
    match pat {
        Pat::Wild | Pat::Bind(_) => true,
        Pat::Or(alts) => alts.iter().any(pat_is_irrefutable),
        _ => false,
    }
}

/// Walk the `Expr` tree and reject any `match` whose arms cannot prove
/// exhaustiveness — that is, when no arm has an unguarded catch-all
/// (`Pat::Wild`, a bare `Pat::Bind`, or an `Or`-pattern containing one).
/// Triggered only when the `JETRO_STRICT_MATCH` environment variable
/// is set; the default behaviour is to surface non-exhaustive matches
/// as a runtime `EvalError` when no arm fires.
fn validate_match_exhaustiveness(expr: &Expr) -> Result<(), ParseError> {
    fn check(e: &Expr) -> Result<(), ParseError> {
        match e {
            Expr::Match { scrutinee, arms } => {
                check(scrutinee)?;
                let exhaustive = arms
                    .iter()
                    .any(|a| a.guard.is_none() && pat_is_irrefutable(&a.pat));
                if !exhaustive {
                    return Err(ParseError(
                        "non-exhaustive match: no unguarded catch-all arm \
                         (`_`, a bare bind, or an or-pattern containing one)"
                            .to_string(),
                    ));
                }
                for arm in arms {
                    if let Some(g) = arm.guard.as_ref() {
                        check(g)?;
                    }
                    check(&arm.body)?;
                }
                Ok(())
            }
            Expr::Chain(base, _) => check(base),
            Expr::BinOp(l, _, r) | Expr::Coalesce(l, r) => {
                check(l)?;
                check(r)
            }
            Expr::UnaryNeg(e) | Expr::Not(e) | Expr::Cast { expr: e, .. } => check(e),
            Expr::Kind { expr, .. } => check(expr),
            _ => Ok(()),
        }
    }
    check(expr)
}

/// Walk the `Expr` tree and validate that every `match` arm's pattern
/// satisfies the linearity rule for or-patterns: each alternative inside
/// an `Or` must bind exactly the same set of variable names. This rules
/// out arms whose body cannot consistently reference a captured value
/// (e.g. `{a: x} | {b: y} -> x` would be ambiguous).
fn validate_pattern_linearity(expr: &Expr) -> Result<(), ParseError> {
    use std::collections::BTreeSet;

    fn collect_binds(pat: &Pat, out: &mut BTreeSet<String>) {
        match pat {
            Pat::Wild | Pat::Lit(_) | Pat::Range { .. } => {}
            Pat::Bind(name) => {
                out.insert(name.clone());
            }
            Pat::Kind { name, .. } => {
                if let Some(n) = name.as_deref() {
                    out.insert(n.to_string());
                }
            }
            Pat::Or(alts) => {
                for alt in alts {
                    collect_binds(alt, out);
                }
            }
            Pat::Obj { fields, rest } => {
                for (_, sub) in fields {
                    collect_binds(sub, out);
                }
                if let Some(Some(name)) = rest {
                    out.insert(name.clone());
                }
            }
            Pat::Arr { elems, rest } => {
                for sub in elems {
                    collect_binds(sub, out);
                }
                if let Some(Some(name)) = rest {
                    out.insert(name.clone());
                }
            }
        }
    }

    fn walk_pat(pat: &Pat) -> Result<(), ParseError> {
        if let Pat::Or(alts) = pat {
            let mut first: Option<BTreeSet<String>> = None;
            for alt in alts {
                let mut names = BTreeSet::new();
                collect_binds(alt, &mut names);
                match &first {
                    None => first = Some(names),
                    Some(existing) if existing == &names => {}
                    Some(existing) => {
                        return Err(ParseError(format!(
                            "or-pattern arms must bind the same variables; \
                             got {{{}}} vs {{{}}}",
                            existing.iter().cloned().collect::<Vec<_>>().join(", "),
                            names.iter().cloned().collect::<Vec<_>>().join(", "),
                        )));
                    }
                }
            }
        }
        match pat {
            Pat::Wild | Pat::Lit(_) | Pat::Bind(_) | Pat::Kind { .. } | Pat::Range { .. } => {}
            Pat::Or(alts) => {
                for alt in alts {
                    walk_pat(alt)?;
                }
            }
            Pat::Obj { fields, .. } => {
                for (_, sub) in fields {
                    walk_pat(sub)?;
                }
            }
            Pat::Arr { elems, .. } => {
                for sub in elems {
                    walk_pat(sub)?;
                }
            }
        }
        Ok(())
    }

    fn walk(e: &Expr) -> Result<(), ParseError> {
        match e {
            Expr::Match { scrutinee, arms } => {
                walk(scrutinee)?;
                for arm in arms {
                    walk_pat(&arm.pat)?;
                    if let Some(g) = arm.guard.as_ref() {
                        walk(g)?;
                    }
                    walk(&arm.body)?;
                }
            }
            Expr::Chain(base, steps) => {
                walk(base)?;
                for step in steps {
                    if let Step::DeepMatch { arms, .. } = step {
                        for arm in arms {
                            walk_pat(&arm.pat)?;
                            if let Some(g) = arm.guard.as_ref() {
                                walk(g)?;
                            }
                            walk(&arm.body)?;
                        }
                    }
                }
            }
            Expr::BinOp(l, _, r) | Expr::Coalesce(l, r) => {
                walk(l)?;
                walk(r)?;
            }
            Expr::UnaryNeg(e) | Expr::Not(e) | Expr::Cast { expr: e, .. } => walk(e)?,
            Expr::Kind { expr, .. } => walk(expr)?,
            Expr::Object(_)
            | Expr::Array(_)
            | Expr::Pipeline { .. }
            | Expr::ListComp { .. }
            | Expr::DictComp { .. }
            | Expr::SetComp { .. }
            | Expr::GenComp { .. }
            | Expr::Lambda { .. }
            | Expr::Let { .. }
            | Expr::IfElse { .. }
            | Expr::Try { .. }
            | Expr::GlobalCall { .. }
            | Expr::Patch { .. }
            | Expr::UpdateBatch { .. }
            | Expr::FString(_)
            | Expr::Null
            | Expr::Bool(_)
            | Expr::Int(_)
            | Expr::Float(_)
            | Expr::Str(_)
            | Expr::Root
            | Expr::Current
            | Expr::Ident(_)
            | Expr::DeleteMark => {}
        }
        Ok(())
    }

    walk(expr)
}

/// Return `true` when `rule` is a keyword terminal (`and`, `or`, `not`, …).
/// Used to skip keyword tokens that appear as decoration in binary/unary rules.
fn is_kw(rule: Rule) -> bool {
    matches!(
        rule,
        Rule::kw_and
            | Rule::kw_or
            | Rule::kw_not
            | Rule::kw_for
            | Rule::kw_in
            | Rule::kw_if
            | Rule::kw_else
            | Rule::kw_let
            | Rule::kw_lambda
            | Rule::kw_kind
            | Rule::kw_is
            | Rule::kw_as
            | Rule::kw_try
            | Rule::kw_when
            | Rule::kw_match
            | Rule::kw_with
    )
}

/// Dispatch on `pair.as_rule()` and delegate to the appropriate specialised
/// `parse_*` function, covering the full expression precedence hierarchy.
fn parse_expr(pair: Pair<Rule>) -> Expr {
    match pair.as_rule() {
        Rule::expr => parse_expr(pair.into_inner().next().unwrap()),
        Rule::cond_expr => parse_cond(pair),
        Rule::pipe_expr => parse_pipeline(pair),
        Rule::coalesce_expr => parse_coalesce(pair),
        Rule::or_expr => parse_or(pair),
        Rule::and_expr => parse_and(pair),
        Rule::not_expr => parse_not(pair),
        Rule::kind_expr => parse_kind(pair),
        Rule::contains_expr => parse_contains(pair),
        Rule::cmp_expr => parse_cmp(pair),
        Rule::add_expr => parse_add(pair),
        Rule::mul_expr => parse_mul(pair),
        Rule::cast_expr => parse_cast(pair),
        Rule::unary_expr => parse_unary(pair),
        Rule::postfix_expr => parse_postfix_expr(pair),
        Rule::primary => parse_primary(pair),
        r => panic!("unexpected rule in parse_expr: {:?}", r),
    }
}

/// Parse a conditional expression (`if … then … else …`) or a `try … else …`
/// expression. When the pair contains only one sub-expression, it is returned
/// directly without wrapping.
fn parse_cond(pair: Pair<Rule>) -> Expr {
    // Grammar: cond_expr = { try_expr | (pipe_expr ("if" pipe_expr "else" pipe_expr)?) }
    // After filtering keywords the order is: then, cond, else.
    let mut inner = pair.into_inner().filter(|p| !is_kw(p.as_rule()));
    let head = inner.next().unwrap();
    if head.as_rule() == Rule::try_expr {
        return parse_try(head);
    }
    let then_ = parse_expr(head);
    let cond = match inner.next() {
        Some(p) => parse_expr(p),
        None => return then_,
    };
    let else_ = parse_expr(inner.next().unwrap());
    Expr::IfElse {
        cond: Box::new(cond),
        then_: Box::new(then_),
        else_: Box::new(else_),
    }
}

/// Parse a `try <expr> else <default>` expression into `Expr::Try`,
/// evaluating `body` and falling back to `default` on any evaluation error.
fn parse_try(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner().filter(|p| !is_kw(p.as_rule()));
    // The body is wrapped in a try_body rule; unwrap it.
    let body_pair = inner.next().unwrap();
    let body = {
        // try_body contains a single expr child
        let mut bi = body_pair.into_inner();
        parse_expr(bi.next().unwrap())
    };
    let default = parse_expr(inner.next().unwrap());
    Expr::Try {
        body: Box::new(body),
        default: Box::new(default),
    }
}

/// Parse a pipeline expression `base | step1 | step2 …` into `Expr::Pipeline`.
/// Each step is either a forward expression or a `-> pattern` bind target.
/// Returns `base` directly when there are no pipeline steps.
fn parse_pipeline(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let base = parse_expr(inner.next().unwrap()); // first child is the base expr
    let mut steps: Vec<PipeStep> = Vec::new();
    for step_pair in inner {
        // Each pipe_step has exactly one inner rule: pipe_forward or pipe_bind.
        let inner_step = step_pair.into_inner().next().unwrap();
        match inner_step.as_rule() {
            Rule::pipe_forward => {
                let inner_pair = inner_step.into_inner().next().unwrap();
                let expr = if inner_pair.as_rule() == Rule::pipe_method_call {
                    let mut mi = inner_pair.into_inner();
                    let name = mi.next().unwrap().as_str().to_string();
                    let args = mi.next().map(parse_arg_list).unwrap_or_default();
                    Expr::Chain(Box::new(Expr::Current), vec![Step::Method(name, args)])
                } else {
                    parse_expr(inner_pair)
                };
                steps.push(PipeStep::Forward(expr));
            }
            Rule::pipe_bind => {
                let target = parse_bind_target(inner_step.into_inner().next().unwrap());
                steps.push(PipeStep::Bind(target));
            }
            r => panic!("unexpected pipe_step inner: {:?}", r),
        }
    }
    if steps.is_empty() {
        base
    } else {
        Expr::Pipeline {
            base: Box::new(base),
            steps,
        }
    }
}

/// Parse a bind target for a pipe bind step (`-> name`, `-> {a, b, ..rest}`,
/// or `-> [a, b]`), returning the corresponding `BindTarget` variant.
fn parse_bind_target(pair: Pair<Rule>) -> BindTarget {
    // pair is bind_target; its single inner child determines the variant.
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::ident => BindTarget::Name(inner.as_str().to_string()),
        Rule::bind_obj => {
            let mut fields = Vec::new();
            let mut rest = None;
            for p in inner.into_inner() {
                match p.as_rule() {
                    Rule::ident => fields.push(p.as_str().to_string()),
                    Rule::bind_rest => {
                        rest = Some(
                            p.into_inner()
                                .find(|x| x.as_rule() == Rule::ident)
                                .unwrap()
                                .as_str()
                                .to_string(),
                        );
                    }
                    _ => {}
                }
            }
            BindTarget::Obj { fields, rest }
        }
        Rule::bind_arr => {
            let fields: Vec<String> = inner
                .into_inner()
                .filter(|p| p.as_rule() == Rule::ident)
                .map(|p| p.as_str().to_string())
                .collect();
            BindTarget::Arr(fields)
        }
        r => panic!("unexpected bind_target inner: {:?}", r),
    }
}

/// Parse a coalesce expression `a ?? b ?? c` left-associatively into nested
/// `Expr::Coalesce` nodes, returning the first non-null result at runtime.
fn parse_coalesce(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let first = parse_expr(inner.next().unwrap());
    inner.fold(first, |acc, rhs| {
        Expr::Coalesce(Box::new(acc), Box::new(parse_expr(rhs)))
    })
}

/// Parse an `or` expression `a or b or c` left-associatively, filtering keyword
/// tokens, into nested `Expr::BinOp(_, BinOp::Or, _)` nodes.
fn parse_or(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner().filter(|p| !is_kw(p.as_rule()));
    let first = parse_expr(inner.next().unwrap());
    inner.fold(first, |acc, rhs| {
        Expr::BinOp(Box::new(acc), BinOp::Or, Box::new(parse_expr(rhs)))
    })
}

/// Parse an `and` expression left-associatively, filtering keyword tokens,
/// into nested `Expr::BinOp(_, BinOp::And, _)` nodes.
fn parse_and(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner().filter(|p| !is_kw(p.as_rule()));
    let first = parse_expr(inner.next().unwrap());
    inner.fold(first, |acc, rhs| {
        Expr::BinOp(Box::new(acc), BinOp::And, Box::new(parse_expr(rhs)))
    })
}

/// Parse a `not` expression; wraps the operand in `Expr::Not` when the first
/// token is the `not` keyword, otherwise delegates to the operand directly.
fn parse_not(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let first = inner.next().unwrap();
    if first.as_rule() == Rule::kw_not {
        let operand = inner.next().unwrap();
        Expr::Not(Box::new(parse_expr(operand)))
    } else {
        parse_expr(first)
    }
}

/// Parse a `kind is <type>` or `kind is not <type>` type-check expression,
/// returning the bare operand when no kind clause is present.
fn parse_kind(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let cmp = parse_expr(inner.next().unwrap());
    match inner.next() {
        None => cmp,
        Some(p) if matches!(p.as_rule(), Rule::kw_kind | Rule::kw_is) => {
            let next = inner.next().unwrap();
            let (negate, kind_type_str) = if next.as_rule() == Rule::kw_not {
                (true, inner.next().unwrap().as_str())
            } else {
                (false, next.as_str())
            };
            let ty = match kind_type_str {
                "null" => KindType::Null,
                "bool" => KindType::Bool,
                "number" => KindType::Number,
                "string" => KindType::Str,
                "array" => KindType::Array,
                "object" => KindType::Object,
                other => panic!("unknown kind type: {}", other),
            };
            Expr::Kind {
                expr: Box::new(cmp),
                ty,
                negate,
            }
        }
        _ => cmp,
    }
}

/// Parse a `contains` / `in` membership test, desugaring it into a call to the
/// `.includes(rhs)` method on the left-hand side. Returns `lhs` when no
/// operator is present.
fn parse_contains(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let lhs = parse_expr(inner.next().unwrap());
    match inner.next() {
        None => lhs,
        Some(_op_pair) => {
            let rhs = parse_expr(inner.next().unwrap());
            let method = match has_array_literal_arg(&rhs) {
                Some(arg) => Step::Method("has_all".to_string(), vec![Arg::Pos(arg)]),
                None if matches!(rhs, Expr::Array(_)) => {
                    Step::Method(INVALID_HAS_ARRAY_RHS.to_string(), Vec::new())
                }
                None => Step::Method("includes".to_string(), vec![Arg::Pos(rhs)]),
            };
            Expr::Chain(
                Box::new(lhs),
                vec![method],
            )
        }
    }
}

fn has_array_literal_arg(expr: &Expr) -> Option<Expr> {
    let Expr::Array(elems) = expr else {
        return None;
    };
    let mut out = Vec::with_capacity(elems.len());
    for elem in elems {
        let ArrayElem::Expr(expr) = elem else {
            return None;
        };
        out.push(scalar_literal_to_val(expr)?);
    }
    Some(val_to_expr(Val::Arr(Arc::new(out))))
}

fn scalar_literal_to_val(expr: &Expr) -> Option<Val> {
    match expr {
        Expr::Null => Some(Val::Null),
        Expr::Bool(b) => Some(Val::Bool(*b)),
        Expr::Int(n) => Some(Val::Int(*n)),
        Expr::Float(f) => Some(Val::Float(*f)),
        Expr::Str(s) => Some(Val::Str(Arc::from(s.as_str()))),
        _ => None,
    }
}

fn val_to_expr(value: Val) -> Expr {
    match value {
        Val::Null => Expr::Null,
        Val::Bool(b) => Expr::Bool(b),
        Val::Int(n) => Expr::Int(n),
        Val::Float(f) => Expr::Float(f),
        Val::Str(s) => Expr::Str(s.to_string()),
        Val::Arr(items) => Expr::Array(
            items
                .iter()
                .cloned()
                .map(|item| ArrayElem::Expr(val_to_expr(item)))
                .collect(),
        ),
        other => panic!("unexpected has literal value: {other:?}"),
    }
}

/// Parse a comparison expression `lhs op rhs` (`==`, `!=`, `<`, `<=`, `>`,
/// `>=`, `~=`) into `Expr::BinOp`. Returns the bare `lhs` when no operator
/// is present.
fn parse_cmp(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let lhs = parse_expr(inner.next().unwrap());
    if let Some(op_pair) = inner.next() {
        let op = match op_pair.as_str() {
            "==" => BinOp::Eq,
            "!=" => BinOp::Neq,
            "<" => BinOp::Lt,
            "<=" => BinOp::Lte,
            ">" => BinOp::Gt,
            ">=" => BinOp::Gte,
            "~=" => BinOp::Fuzzy,
            o => panic!("unknown cmp op: {}", o),
        };
        let rhs = parse_expr(inner.next().unwrap());
        Expr::BinOp(Box::new(lhs), op, Box::new(rhs))
    } else {
        lhs
    }
}

/// Parse additive expressions (`+`, `-`) left-associatively using
/// `parse_left_assoc`.
fn parse_add(pair: Pair<Rule>) -> Expr {
    parse_left_assoc(pair, |s| match s {
        "+" => Some(BinOp::Add),
        "-" => Some(BinOp::Sub),
        _ => None,
    })
}

/// Parse multiplicative expressions (`*`, `/`, `%`) left-associatively using
/// `parse_left_assoc`.
fn parse_mul(pair: Pair<Rule>) -> Expr {
    parse_left_assoc(pair, |s| match s {
        "*" => Some(BinOp::Mul),
        "/" => Some(BinOp::Div),
        "%" => Some(BinOp::Mod),
        _ => None,
    })
}

/// Generic left-associative binary expression builder. Iterates alternating
/// `expr op expr` children; `op_fn` maps operator text to `BinOp`.
fn parse_left_assoc<F>(pair: Pair<Rule>, op_fn: F) -> Expr
where
    F: Fn(&str) -> Option<BinOp>,
{
    let mut inner = pair.into_inner().peekable();
    let first = parse_expr(inner.next().unwrap());
    let mut acc = first;
    while inner.peek().is_some() {
        let op_pair = inner.next().unwrap();
        let op = op_fn(op_pair.as_str()).unwrap();
        let rhs = parse_expr(inner.next().unwrap());
        acc = Expr::BinOp(Box::new(acc), op, Box::new(rhs));
    }
    acc
}

/// Parse a chain of `as <type>` cast suffixes, building nested `Expr::Cast`
/// nodes left-to-right. Returns the bare operand when no cast is present.
fn parse_cast(pair: Pair<Rule>) -> Expr {
    // cast_expr = { unary_expr ~ (kw_as ~ cast_type)* }
    let mut inner = pair.into_inner().peekable();
    let mut acc = parse_expr(inner.next().unwrap());
    while inner.peek().is_some() {
        // consume the `as` keyword token
        let kw = inner.next().unwrap();
        debug_assert_eq!(kw.as_rule(), Rule::kw_as);
        let ty_pair = inner.next().unwrap();
        let ty = match ty_pair.as_str() {
            "int" => CastType::Int,
            "float" => CastType::Float,
            "number" => CastType::Number,
            "string" => CastType::Str,
            "bool" => CastType::Bool,
            "array" => CastType::Array,
            "object" => CastType::Object,
            "null" => CastType::Null,
            o => panic!("unknown cast type: {}", o),
        };
        acc = Expr::Cast {
            expr: Box::new(acc),
            ty,
        };
    }
    acc
}

/// Parse a unary negation expression (`-expr`); delegates to `parse_expr` for
/// any rule that is not a `unary_neg` marker.
fn parse_unary(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let first = inner.next().unwrap();
    match first.as_rule() {
        Rule::unary_neg => {
            let operand = parse_expr(inner.next().unwrap());
            Expr::UnaryNeg(Box::new(operand))
        }
        _ => parse_expr(first),
    }
}

/// Parse a postfix expression: a primary value followed by zero or more
/// postfix steps (field access, method calls, index, slice, `?` optional).
/// Coalesces adjacent `?` quantifiers into `OptField`/`OptMethod` steps and
/// delegates to `classify_chain_write` for write rewrites.
fn parse_postfix_expr(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let base = parse_primary(inner.next().unwrap());
    let raw_steps: Vec<Step> = inner.flat_map(parse_postfix_step).collect();

    // Merge `?` quantifiers into the preceding step by converting Field → OptField
    // and Method → OptMethod; bare quantifiers with no suitable predecessor are kept.
    let mut steps: Vec<Step> = Vec::with_capacity(raw_steps.len());
    for s in raw_steps {
        match s {
            Step::Quantifier(QuantifierKind::First) => {
                match steps.last() {
                    Some(Step::Field(_)) => {
                        if let Some(Step::Field(k)) = steps.pop() {
                            steps.push(Step::OptField(k));
                        }
                    }
                    Some(Step::Method(_, _)) => {
                        if let Some(Step::Method(n, a)) = steps.pop() {
                            steps.push(Step::OptMethod(n, a));
                        }
                    }
                    _ => {
                        // No suitable predecessor; discard the quantifier to
                        // avoid silently changing semantics.
                    }
                }
            }
            other => steps.push(other),
        }
    }
    if let Some(rewritten) = classify_chain_write(&base, &steps) {
        return rewritten;
    }

    // Wildcard expansion. Mid-chain `[*]` (e.g. `$.items[*].x`) means
    // "iterate the receiver and apply the rest of the chain per element".
    // Rewrite the chain so subsequent steps are pushed inside a `.map(@ +
    // rest)` lambda. End-of-chain `[*]` is dropped (identity over array).
    //
    // Done before write classification so chain-writes like
    // `$.xs[*].field.set(v)` are seen as `$.xs.map(@.field.set(v))` (which
    // the planner can broadcast through `patch_fusion`).
    let steps = expand_wildcards(steps);
    base.maybe_chain(steps)
}

/// Rewrite mid-chain `Step::Wildcard` into an equivalent `.map(@ + rest)`
/// method call. Trailing wildcards (with no further steps) collapse to
/// nothing: `$.xs[*]` is equivalent to `$.xs`.
fn expand_wildcards(steps: Vec<Step>) -> Vec<Step> {
    // Fast path: no wildcards present.
    if !steps.iter().any(|s| matches!(s, Step::Wildcard)) {
        return steps;
    }
    let mut out: Vec<Step> = Vec::with_capacity(steps.len());
    let mut iter = steps.into_iter().peekable();
    while let Some(step) = iter.next() {
        if matches!(step, Step::Wildcard) {
            // Collect remaining steps. Recursively expand any further
            // wildcards in the rest so `xs[*].ys[*].x` is supported.
            let rest: Vec<Step> = iter.by_ref().collect();
            if rest.is_empty() {
                // Trailing `[*]` is identity over the array.
                break;
            }
            let rest = expand_wildcards(rest);
            // Build `Lambda(@ -> Chain(@, rest))` as the map argument.
            let body = Expr::Chain(Box::new(Expr::Current), rest);
            let lam = Expr::Lambda {
                params: vec!["@".to_string()],
                body: Box::new(body),
            };
            out.push(Step::Method("map".to_string(), vec![Arg::Pos(lam)]));
            return out;
        }
        out.push(step);
    }
    out
}

/// Detect rooted write terminals (`$.path.set(v)` etc.) and rewrite them into
/// `Expr::Patch` nodes. Only fires when `base` is `Expr::Root` and the last
/// step is a recognised terminal write method. Returns `None` for all other
/// expressions, leaving them unchanged.
fn classify_chain_write(base: &Expr, steps: &[Step]) -> Option<Expr> {
    if !matches!(base, Expr::Root) {
        return None;
    }
    let last = steps.last()?;
    let (name, args) = match last {
        Step::Method(n, a) => (n.as_str(), a),
        _ => return None,
    };
    if !is_chain_write_terminal(name) {
        return None;
    }

    let prefix = &steps[..steps.len() - 1];
    let path = write_steps_to_path(prefix, true)?;

    if name == "update" {
        let ops = build_update_ops(args)?;
        return Some(Expr::UpdateBatch {
            root: Box::new(Expr::Root),
            selector: path,
            ops,
        });
    }

    let op = build_patch_op(name, args, Vec::new())?;
    Some(Expr::UpdateBatch {
        root: Box::new(Expr::Root),
        selector: path,
        ops: vec![op],
    })
}

fn build_update_ops(args: &[Arg]) -> Option<Vec<PatchOp>> {
    if args.len() != 1 {
        return None;
    }
    let Expr::Object(fields) = arg_expr(args.first()?) else {
        return None;
    };
    let focus = crate::plan::update::UPDATE_FOCUS_BINDING.to_string();
    let mut ops = Vec::new();
    for field in fields {
        let ObjField::Kv {
            key,
            val,
            optional: false,
            cond,
        } = field
        else {
            return None;
        };
        ops.push(PatchOp {
            path: parse_update_path_key(key)?,
            val: qualify_update_expr(val.clone(), &focus, &[]),
            cond: cond
                .as_ref()
                .map(|expr| qualify_update_expr(expr.clone(), &focus, &[])),
        });
    }
    Some(ops)
}

/// Parse object keys inside `.update({ ... })`.
///
/// Plain keys (`tags`) update fields below the selected focus. Quoted path
/// keys (`"books[*].tags"`) allow root-level batched updates without widening
/// the object-key grammar for every object literal.
fn parse_update_path_key(key: &str) -> Option<Vec<PathStep>> {
    let mut idx = 0usize;
    let bytes = key.as_bytes();
    let first = parse_update_ident(key, &mut idx)?;
    let mut out = vec![PathStep::Field(first)];
    while idx < bytes.len() {
        match bytes[idx] {
            b'.' => {
                idx += 1;
                out.push(PathStep::Field(parse_update_ident(key, &mut idx)?));
            }
            b'[' => {
                let close = find_matching_bracket(key, idx)?;
                let inner = key[idx + 1..close].trim();
                if inner == "*" {
                    out.push(PathStep::Wildcard);
                } else if let Some(rest) = inner.strip_prefix("* if") {
                    let expr = parse_embedded_expr(rest.trim())?;
                    out.push(PathStep::WildcardFilter(Box::new(expr)));
                } else if let Ok(n) = inner.parse::<i64>() {
                    out.push(PathStep::Index(n));
                } else {
                    let expr = parse_embedded_expr(inner)?;
                    out.push(PathStep::DynIndex(expr));
                }
                idx = close + 1;
            }
            _ => return None,
        }
    }
    Some(out)
}

fn parse_update_ident(src: &str, idx: &mut usize) -> Option<String> {
    let start = *idx;
    for (off, ch) in src[start..].char_indices() {
        if !(ch.is_ascii_alphanumeric() || ch == '_' || ch == '-') {
            if off == 0 {
                return None;
            }
            *idx = start + off;
            return Some(src[start..start + off].to_string());
        }
    }
    if start == src.len() {
        return None;
    }
    *idx = src.len();
    Some(src[start..].to_string())
}

fn find_matching_bracket(src: &str, open: usize) -> Option<usize> {
    let mut depth = 0usize;
    for (idx, ch) in src[open..].char_indices() {
        match ch {
            '[' => depth += 1,
            ']' => {
                depth = depth.checked_sub(1)?;
                if depth == 0 {
                    return Some(open + idx);
                }
            }
            _ => {}
        }
    }
    None
}

fn parse_embedded_expr(src: &str) -> Option<Expr> {
    let mut pairs = V2Parser::parse(Rule::expr, src).ok()?;
    Some(parse_expr(pairs.next()?))
}

fn qualify_update_expr(expr: Expr, focus: &str, bound: &[String]) -> Expr {
    match expr {
        Expr::Ident(name) if !bound.iter().any(|b| b == &name) => Expr::Chain(
            Box::new(Expr::Ident(focus.to_string())),
            vec![Step::Field(name)],
        ),
        Expr::Chain(base, steps) => {
            Expr::Chain(Box::new(qualify_update_expr(*base, focus, bound)), steps)
        }
        Expr::BinOp(lhs, op, rhs) => Expr::BinOp(
            Box::new(qualify_update_expr(*lhs, focus, bound)),
            op,
            Box::new(qualify_update_expr(*rhs, focus, bound)),
        ),
        Expr::UnaryNeg(inner) => {
            Expr::UnaryNeg(Box::new(qualify_update_expr(*inner, focus, bound)))
        }
        Expr::Not(inner) => Expr::Not(Box::new(qualify_update_expr(*inner, focus, bound))),
        Expr::Kind { expr, ty, negate } => Expr::Kind {
            expr: Box::new(qualify_update_expr(*expr, focus, bound)),
            ty,
            negate,
        },
        Expr::Coalesce(lhs, rhs) => Expr::Coalesce(
            Box::new(qualify_update_expr(*lhs, focus, bound)),
            Box::new(qualify_update_expr(*rhs, focus, bound)),
        ),
        Expr::Object(fields) => Expr::Object(
            fields
                .into_iter()
                .map(|field| qualify_update_obj_field(field, focus, bound))
                .collect(),
        ),
        Expr::Array(items) => Expr::Array(
            items
                .into_iter()
                .map(|item| match item {
                    ArrayElem::Expr(expr) => {
                        ArrayElem::Expr(qualify_update_expr(expr, focus, bound))
                    }
                    ArrayElem::Spread(expr) => {
                        ArrayElem::Spread(qualify_update_expr(expr, focus, bound))
                    }
                })
                .collect(),
        ),
        Expr::IfElse { cond, then_, else_ } => Expr::IfElse {
            cond: Box::new(qualify_update_expr(*cond, focus, bound)),
            then_: Box::new(qualify_update_expr(*then_, focus, bound)),
            else_: Box::new(qualify_update_expr(*else_, focus, bound)),
        },
        Expr::Try { body, default } => Expr::Try {
            body: Box::new(qualify_update_expr(*body, focus, bound)),
            default: Box::new(qualify_update_expr(*default, focus, bound)),
        },
        Expr::Cast { expr, ty } => Expr::Cast {
            expr: Box::new(qualify_update_expr(*expr, focus, bound)),
            ty,
        },
        Expr::FString(parts) => Expr::FString(
            parts
                .into_iter()
                .map(|part| match part {
                    FStringPart::Lit(s) => FStringPart::Lit(s),
                    FStringPart::Interp { expr, fmt } => FStringPart::Interp {
                        expr: qualify_update_expr(expr, focus, bound),
                        fmt,
                    },
                })
                .collect(),
        ),
        Expr::GlobalCall { name, args } => Expr::GlobalCall {
            name,
            args: args
                .into_iter()
                .map(|arg| qualify_update_arg(arg, focus, bound))
                .collect(),
        },
        Expr::Pipeline { base, steps } => Expr::Pipeline {
            base: Box::new(qualify_update_expr(*base, focus, bound)),
            steps: qualify_update_pipe_steps(steps, focus, bound),
        },
        Expr::ListComp {
            expr,
            vars,
            iter,
            cond,
        } => {
            let iter = qualify_update_expr(*iter, focus, bound);
            let nested = extend_bound(bound, &vars);
            Expr::ListComp {
                expr: Box::new(qualify_update_expr(*expr, focus, &nested)),
                vars,
                iter: Box::new(iter),
                cond: cond.map(|expr| Box::new(qualify_update_expr(*expr, focus, &nested))),
            }
        }
        Expr::DictComp {
            key,
            val,
            vars,
            iter,
            cond,
        } => {
            let iter = qualify_update_expr(*iter, focus, bound);
            let nested = extend_bound(bound, &vars);
            Expr::DictComp {
                key: Box::new(qualify_update_expr(*key, focus, &nested)),
                val: Box::new(qualify_update_expr(*val, focus, &nested)),
                vars,
                iter: Box::new(iter),
                cond: cond.map(|expr| Box::new(qualify_update_expr(*expr, focus, &nested))),
            }
        }
        Expr::SetComp {
            expr,
            vars,
            iter,
            cond,
        } => {
            let iter = qualify_update_expr(*iter, focus, bound);
            let nested = extend_bound(bound, &vars);
            Expr::SetComp {
                expr: Box::new(qualify_update_expr(*expr, focus, &nested)),
                vars,
                iter: Box::new(iter),
                cond: cond.map(|expr| Box::new(qualify_update_expr(*expr, focus, &nested))),
            }
        }
        Expr::GenComp {
            expr,
            vars,
            iter,
            cond,
        } => {
            let iter = qualify_update_expr(*iter, focus, bound);
            let nested = extend_bound(bound, &vars);
            Expr::GenComp {
                expr: Box::new(qualify_update_expr(*expr, focus, &nested)),
                vars,
                iter: Box::new(iter),
                cond: cond.map(|expr| Box::new(qualify_update_expr(*expr, focus, &nested))),
            }
        }
        Expr::Lambda { params, body } => {
            let nested = extend_bound(bound, &params);
            Expr::Lambda {
                params,
                body: Box::new(qualify_update_expr(*body, focus, &nested)),
            }
        }
        Expr::Let { name, init, body } => {
            let init = qualify_update_expr(*init, focus, bound);
            let mut nested = bound.to_vec();
            nested.push(name.clone());
            Expr::Let {
                name,
                init: Box::new(init),
                body: Box::new(qualify_update_expr(*body, focus, &nested)),
            }
        }
        Expr::Patch { root, ops } => Expr::Patch {
            root: Box::new(qualify_update_expr(*root, focus, bound)),
            ops: ops
                .into_iter()
                .map(|op| qualify_update_patch_op(op, focus, bound))
                .collect(),
        },
        Expr::Match { scrutinee, arms } => Expr::Match {
            scrutinee: Box::new(qualify_update_expr(*scrutinee, focus, bound)),
            arms: arms
                .into_iter()
                .map(|arm| qualify_update_match_arm(arm, focus, bound))
                .collect(),
        },
        other => other,
    }
}

fn qualify_update_obj_field(field: ObjField, focus: &str, bound: &[String]) -> ObjField {
    match field {
        ObjField::Kv {
            key,
            val,
            optional,
            cond,
        } => ObjField::Kv {
            key,
            val: qualify_update_expr(val, focus, bound),
            optional,
            cond: cond.map(|expr| qualify_update_expr(expr, focus, bound)),
        },
        ObjField::Dynamic { key, val } => ObjField::Dynamic {
            key: qualify_update_expr(key, focus, bound),
            val: qualify_update_expr(val, focus, bound),
        },
        ObjField::Spread(expr) => ObjField::Spread(qualify_update_expr(expr, focus, bound)),
        ObjField::SpreadDeep(expr) => ObjField::SpreadDeep(qualify_update_expr(expr, focus, bound)),
        other => other,
    }
}

fn qualify_update_arg(arg: Arg, focus: &str, bound: &[String]) -> Arg {
    match arg {
        Arg::Pos(expr) => Arg::Pos(qualify_update_expr(expr, focus, bound)),
        Arg::Named(name, expr) => Arg::Named(name, qualify_update_expr(expr, focus, bound)),
    }
}

fn qualify_update_pipe_steps(steps: Vec<PipeStep>, focus: &str, bound: &[String]) -> Vec<PipeStep> {
    let mut scoped = bound.to_vec();
    steps
        .into_iter()
        .map(|step| match step {
            PipeStep::Forward(expr) => PipeStep::Forward(qualify_update_expr(expr, focus, &scoped)),
            PipeStep::Bind(target) => {
                collect_bind_target_names(&target, &mut scoped);
                PipeStep::Bind(target)
            }
        })
        .collect()
}

fn qualify_update_patch_op(op: PatchOp, focus: &str, bound: &[String]) -> PatchOp {
    PatchOp {
        path: op
            .path
            .into_iter()
            .map(|step| qualify_update_path_step(step, focus, bound))
            .collect(),
        val: qualify_update_expr(op.val, focus, bound),
        cond: op.cond.map(|expr| qualify_update_expr(expr, focus, bound)),
    }
}

fn qualify_update_path_step(step: PathStep, focus: &str, bound: &[String]) -> PathStep {
    match step {
        PathStep::DynIndex(expr) => PathStep::DynIndex(qualify_update_expr(expr, focus, bound)),
        PathStep::WildcardFilter(expr) => {
            PathStep::WildcardFilter(Box::new(qualify_update_expr(*expr, focus, bound)))
        }
        other => other,
    }
}

fn qualify_update_match_arm(arm: MatchArm, focus: &str, bound: &[String]) -> MatchArm {
    let mut nested = bound.to_vec();
    collect_pat_names(&arm.pat, &mut nested);
    MatchArm {
        pat: arm.pat,
        guard: arm
            .guard
            .map(|expr| qualify_update_expr(expr, focus, &nested)),
        body: qualify_update_expr(arm.body, focus, &nested),
    }
}

fn extend_bound(bound: &[String], names: &[String]) -> Vec<String> {
    let mut nested = bound.to_vec();
    nested.extend(names.iter().cloned());
    nested
}

fn collect_bind_target_names(target: &BindTarget, out: &mut Vec<String>) {
    match target {
        BindTarget::Name(name) => out.push(name.clone()),
        BindTarget::Obj { fields, rest } => {
            out.extend(fields.iter().cloned());
            if let Some(name) = rest {
                out.push(name.clone());
            }
        }
        BindTarget::Arr(names) => out.extend(names.iter().cloned()),
    }
}

fn collect_pat_names(pat: &Pat, out: &mut Vec<String>) {
    match pat {
        Pat::Bind(name) => out.push(name.clone()),
        Pat::Or(alts) => {
            for alt in alts {
                collect_pat_names(alt, out);
            }
        }
        Pat::Obj { fields, rest } => {
            for (_, field_pat) in fields {
                collect_pat_names(field_pat, out);
            }
            if let Some(Some(name)) = rest {
                out.push(name.clone());
            }
        }
        Pat::Arr { elems, rest } => {
            for elem in elems {
                collect_pat_names(elem, out);
            }
            if let Some(Some(name)) = rest {
                out.push(name.clone());
            }
        }
        Pat::Kind {
            name: Some(name), ..
        } => out.push(name.clone()),
        Pat::Wild | Pat::Lit(_) | Pat::Kind { name: None, .. } | Pat::Range { .. } => {}
    }
}

/// Extract the expression from a positional or named `Arg`, unwrapping the
/// outer `Arg` wrapper.
fn arg_expr(a: &Arg) -> &Expr {
    match a {
        Arg::Pos(e) | Arg::Named(_, e) => e,
    }
}

/// Parse a single postfix step from a `postfix_step` rule pair, returning a
/// `Vec<Step>` because `map_into_shape` can expand to two steps.
fn parse_postfix_step(pair: Pair<Rule>) -> Vec<Step> {
    let inner_pair = pair.into_inner().next().unwrap();
    match inner_pair.as_rule() {
        Rule::field_access => {
            let name = inner_pair.into_inner().next().unwrap().as_str().to_string();
            vec![Step::Field(name)]
        }
        Rule::descendant => {
            let mut di = inner_pair.into_inner();
            match di.next() {
                Some(p) => vec![Step::Descendant(p.as_str().to_string())],
                None => vec![Step::DescendAll],
            }
        }
        Rule::deep_method => {
            // `$..find(pred)` etc. are parsed here and mapped to `deep_*` method names.
            let mut mi = inner_pair.into_inner();
            let name = mi.next().unwrap().as_str().to_string();
            let args = mi.next().map(parse_arg_list).unwrap_or_default();
            let mapped = match name.as_str() {
                "find" | "find_all" | "findAll" => "deep_find".to_string(),
                "shape" => "deep_shape".to_string(),
                "like" => "deep_like".to_string(),
                other => format!("deep_{}", other),
            };
            vec![Step::Method(mapped, args)]
        }
        Rule::deep_match => {
            // `$..match { arms }` (collect all) and `$..match! { arms }`
            // (early-stop on first truthy match) share the parsing path.
            // The `deep_match_op` token spelling distinguishes the two.
            let mut arms: Vec<MatchArm> = Vec::new();
            let mut early_stop = false;
            for child in inner_pair.into_inner().filter(|p| !is_kw(p.as_rule())) {
                match child.as_rule() {
                    Rule::deep_match_op => {
                        early_stop = child.as_str().ends_with('!');
                    }
                    Rule::match_arm => {
                        let mut ai = child.into_inner().filter(|p| !is_kw(p.as_rule()));
                        let pat = parse_pat(ai.next().expect("match arm pattern"));
                        let rest: Vec<_> = ai.collect();
                        let (guard, body) = match rest.len() {
                            1 => (None, parse_expr(rest.into_iter().next().unwrap())),
                            2 => {
                                let mut it = rest.into_iter();
                                let g = parse_expr(it.next().unwrap());
                                let b = parse_expr(it.next().unwrap());
                                (Some(g), b)
                            }
                            _ => panic!(
                                "deep_match arm: expected 1 or 2 trailing exprs (guard?, body)"
                            ),
                        };
                        arms.push(MatchArm { pat, guard, body });
                    }
                    _ => {}
                }
            }
            vec![Step::DeepMatch { arms, early_stop }]
        }
        Rule::inline_filter => {
            let expr = parse_expr(inner_pair.into_inner().next().unwrap());
            vec![Step::InlineFilter(Box::new(expr))]
        }
        Rule::quantifier => {
            let s = inner_pair.as_str();
            if s.starts_with('!') {
                vec![Step::Quantifier(QuantifierKind::One)]
            } else {
                vec![Step::Quantifier(QuantifierKind::First)]
            }
        }
        Rule::method_call => {
            let mut mi = inner_pair.into_inner();
            let name = mi.next().unwrap().as_str().to_string();
            let args = mi.next().map(parse_arg_list).unwrap_or_default();
            vec![Step::Method(name, args)]
        }
        Rule::index_access => {
            let bi = inner_pair.into_inner().next().unwrap();
            vec![parse_bracket(bi)]
        }
        Rule::dyn_field => {
            let expr = parse_expr(inner_pair.into_inner().next().unwrap());
            vec![Step::DynIndex(Box::new(expr))]
        }
        Rule::map_into_shape => {
            // `[if pred] { body }` desugars to an optional `.filter(pred)` step
            // followed by a `.map(body)` step.
            let mut guard: Option<Expr> = None;
            let mut body: Option<Expr> = None;
            let mut saw_if = false;
            for p in inner_pair.into_inner() {
                match p.as_rule() {
                    Rule::kw_if => saw_if = true,
                    Rule::expr => {
                        if saw_if && guard.is_none() {
                            guard = Some(parse_expr(p));
                        } else {
                            body = Some(parse_expr(p));
                        }
                    }
                    _ => {}
                }
            }
            let body = body.expect("map_into_shape requires body");
            let mut steps = Vec::new();
            if let Some(g) = guard {
                steps.push(Step::Method("filter".into(), vec![Arg::Pos(g)]));
            }
            steps.push(Step::Method("map".into(), vec![Arg::Pos(body)]));
            steps
        }
        r => panic!("unexpected postfix rule: {:?}", r),
    }
}

/// Parse a bracket access expression into the appropriate `Step`. Forms:
///
/// - `[n]` → `Step::Index(n)`
/// - `[a:b]`, `[a:]`, `[:b]` → `Step::Slice(_, _, None)` (step defaults to 1)
/// - `[a:b:s]`, `[::s]`, `[a::s]`, `[:b:s]` → `Step::Slice(_, _, Some(s))`
/// - `[*]` → `Step::Wildcard`
/// - `[* if expr]` → `Step::InlineFilter(expr)`
/// - `[expr]` → `Step::DynIndex(expr)`
fn parse_bracket(pair: Pair<Rule>) -> Step {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::idx_only => Step::Index(inner.as_str().parse().unwrap()),
        Rule::wildcard => Step::Wildcard,
        Rule::wildcard_filter => {
            let expr = inner
                .into_inner()
                .find(|p| p.as_rule() == Rule::expr)
                .map(parse_expr)
                .expect("wildcard filter requires predicate");
            Step::InlineFilter(Box::new(expr))
        }
        Rule::slice_full => {
            let mut i = inner.into_inner();
            let a = i.next().unwrap().as_str().parse().ok();
            let b = i.next().unwrap().as_str().parse().ok();
            Step::Slice(a, b, None)
        }
        Rule::slice_from => {
            let a = inner.into_inner().next().unwrap().as_str().parse().ok();
            Step::Slice(a, None, None)
        }
        Rule::slice_to => {
            let b = inner.into_inner().next().unwrap().as_str().parse().ok();
            Step::Slice(None, b, None)
        }
        Rule::slice_step => {
            // The grammar produces 0-3 `idx_val` children; missing positions
            // are absent in the inner iterator. Walk text-position aware via
            // a 3-slot accumulator parsed from the literal source.
            let s = inner.as_str();
            // Split on ':' in the raw literal — preserves positional missing-ness.
            let mut parts = s.split(':');
            let a = parts.next().and_then(|p| p.parse().ok());
            let b = parts.next().and_then(|p| p.parse().ok());
            let st = parts.next().and_then(|p| p.parse().ok());
            Step::Slice(a, b, st)
        }
        Rule::expr => Step::DynIndex(Box::new(parse_expr(inner))),
        r => panic!("unexpected bracket rule: {:?}", r),
    }
}

/// Parse a primary expression: a literal, `$`, `@`, identifier, `let`, lambda,
/// comprehension, object/array constructor, global call, or patch block.
fn parse_primary(pair: Pair<Rule>) -> Expr {
    let inner = if pair.as_rule() == Rule::primary {
        pair.into_inner().next().unwrap()
    } else {
        pair
    };
    match inner.as_rule() {
        Rule::literal => parse_literal(inner),
        Rule::root => Expr::Root,
        Rule::current => Expr::Current,
        Rule::bare_leading_field => {
            // `.field` is sugar for `@.field` — desugar at parse time so
            // method-arg shapes like `filter(.active)` become indistinguishable
            // from the explicit `filter(@.active)` form downstream.
            let name = inner
                .into_inner()
                .next()
                .expect("bare_leading_field has a field_name child")
                .as_str()
                .to_string();
            Expr::Chain(
                Box::new(Expr::Current),
                vec![Step::Field(name)],
            )
        }
        Rule::ident => Expr::Ident(inner.as_str().to_string()),
        Rule::let_expr => parse_let(inner),
        Rule::lambda_expr => parse_lambda(inner),
        Rule::arrow_lambda => parse_arrow_lambda(inner),
        Rule::list_comp => parse_list_comp(inner),
        Rule::dict_comp => parse_dict_comp(inner),
        Rule::set_comp => parse_set_comp(inner),
        Rule::gen_comp => parse_gen_comp(inner),
        Rule::obj_construct => parse_obj(inner),
        Rule::arr_construct => parse_arr(inner),
        Rule::global_call => parse_global_call(inner),
        Rule::expr => parse_expr(inner),
        Rule::patch_block => parse_patch(inner),
        Rule::match_expr => parse_match_expr(inner),
        Rule::kw_delete => Expr::DeleteMark,
        r => panic!("unexpected primary rule: {:?}", r),
    }
}

/// Parse a `match scrutinee { pat when guard -> body, ... }` expression.
fn parse_match_expr(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner().filter(|p| !is_kw(p.as_rule()));
    let scrutinee = parse_expr(inner.next().expect("match scrutinee"));
    let mut arms: Vec<MatchArm> = Vec::new();
    for arm_pair in inner {
        if arm_pair.as_rule() != Rule::match_arm {
            continue;
        }
        let mut ai = arm_pair.into_inner().filter(|p| !is_kw(p.as_rule()));
        let pat_pair = ai.next().expect("match arm pattern");
        let pat = parse_pat(pat_pair);
        // Remaining: optional guard expr, then body expr.
        let rest: Vec<_> = ai.collect();
        let (guard, body) = match rest.len() {
            1 => (None, parse_expr(rest.into_iter().next().unwrap())),
            2 => {
                let mut it = rest.into_iter();
                let g = parse_expr(it.next().unwrap());
                let b = parse_expr(it.next().unwrap());
                (Some(g), b)
            }
            _ => panic!("match arm: expected 1 or 2 trailing exprs (guard?, body)"),
        };
        arms.push(MatchArm { pat, guard, body });
    }
    Expr::Match {
        scrutinee: Box::new(scrutinee),
        arms,
    }
}

/// Parse a `pat_or` / `pat_atom` rule into a `Pat` AST node.
fn parse_pat(pair: Pair<Rule>) -> Pat {
    match pair.as_rule() {
        Rule::pat_or => {
            let mut alts: Vec<Pat> = pair.into_inner().map(parse_pat).collect();
            if alts.len() == 1 {
                alts.pop().unwrap()
            } else {
                Pat::Or(alts)
            }
        }
        Rule::pat_atom => {
            let inner = pair.into_inner().next().expect("pat_atom inner");
            parse_pat(inner)
        }
        Rule::pat_range => {
            let mut it = pair.into_inner();
            let lo_pair = it.next().expect("pat_range: lo");
            let op_pair = it.next().expect("pat_range: op");
            let hi_pair = it.next().expect("pat_range: hi");
            let lo: f64 = lo_pair.as_str().parse().expect("range lo as f64");
            let hi: f64 = hi_pair.as_str().parse().expect("range hi as f64");
            let inclusive = op_pair.as_str() == "..=";
            Pat::Range { lo, hi, inclusive }
        }
        Rule::pat_wild => Pat::Wild,
        Rule::pat_literal => Pat::Lit(parse_pat_literal(pair)),
        Rule::pat_kind_bind => {
            let mut it = pair.into_inner();
            let name = it.next().unwrap().as_str().to_string();
            let kind = parse_kind_type(it.next().unwrap().as_str());
            Pat::Kind {
                name: Some(name),
                kind,
            }
        }
        Rule::pat_kind_only => {
            let kt = pair.into_inner().next().unwrap().as_str();
            Pat::Kind {
                name: None,
                kind: parse_kind_type(kt),
            }
        }
        Rule::pat_bind => Pat::Bind(pair.as_str().to_string()),
        Rule::pat_obj => {
            let mut fields: Vec<(String, Pat)> = Vec::new();
            let mut rest: Option<Option<String>> = None;
            for p in pair.into_inner() {
                match p.as_rule() {
                    Rule::pat_obj_field => {
                        // Either `key: pat` (Kv) or `key` (Short) — the
                        // grammar splits these into two named subrules so
                        // we can branch without re-checking presence of
                        // the trailing colon.
                        let inner = p.into_inner().next().unwrap();
                        match inner.as_rule() {
                            Rule::pat_obj_field_kv => {
                                let mut fi = inner.into_inner();
                                let k = fi.next().unwrap().as_str().to_string();
                                let v = parse_pat(fi.next().unwrap());
                                fields.push((k, v));
                            }
                            Rule::pat_obj_field_short => {
                                let k = inner.as_str().to_string();
                                fields.push((k.clone(), Pat::Bind(k)));
                            }
                            _ => {}
                        }
                    }
                    Rule::pat_obj_rest => {
                        let inner = p.into_inner().next().unwrap();
                        match inner.as_rule() {
                            Rule::pat_obj_rest_named => {
                                let nm = inner.into_inner().next().unwrap().as_str().to_string();
                                rest = Some(Some(nm));
                            }
                            Rule::pat_obj_rest_anon => rest = Some(None),
                            _ => {}
                        }
                    }
                    _ => {}
                }
            }
            Pat::Obj { fields, rest }
        }
        Rule::pat_arr => {
            let mut elems: Vec<Pat> = Vec::new();
            let mut rest: Option<Option<String>> = None;
            for p in pair.into_inner() {
                match p.as_rule() {
                    Rule::pat_or | Rule::pat_atom => elems.push(parse_pat(p)),
                    Rule::pat_arr_rest => {
                        let inner = p.into_inner().next().unwrap();
                        match inner.as_rule() {
                            Rule::pat_arr_rest_named => {
                                let nm = inner.into_inner().next().unwrap().as_str().to_string();
                                rest = Some(Some(nm));
                            }
                            Rule::pat_arr_rest_anon => rest = Some(None),
                            _ => {}
                        }
                    }
                    _ => {}
                }
            }
            Pat::Arr { elems, rest }
        }
        r => panic!("unexpected pattern rule: {:?}", r),
    }
}

/// Decode a `pat_literal` rule into a `PatLit`.
fn parse_pat_literal(pair: Pair<Rule>) -> PatLit {
    let inner = pair.into_inner().next().expect("pat_literal inner");
    match inner.as_rule() {
        Rule::pat_lit_null => PatLit::Null,
        Rule::pat_lit_true => PatLit::Bool(true),
        Rule::pat_lit_false => PatLit::Bool(false),
        Rule::pat_lit_int => PatLit::Int(inner.as_str().parse().expect("pat int")),
        Rule::pat_lit_float => PatLit::Float(inner.as_str().parse().expect("pat float")),
        Rule::pat_lit_str => {
            let raw = inner.as_str();
            // Strip surrounding quotes.
            let stripped = &raw[1..raw.len() - 1];
            PatLit::Str(stripped.to_string())
        }
        r => panic!("unexpected pat literal rule: {:?}", r),
    }
}

/// Map a kind type name (`number`, `string`, ...) to `KindType`.
fn parse_kind_type(name: &str) -> KindType {
    match name {
        "number" => KindType::Number,
        "string" => KindType::Str,
        "array" => KindType::Array,
        "object" => KindType::Object,
        "bool" => KindType::Bool,
        "null" => KindType::Null,
        other => panic!("unknown kind type: {other}"),
    }
}

/// Parse a literal token (null, true, false, integer, float, f-string, or
/// quoted string) into the corresponding `Expr` variant.
fn parse_literal(pair: Pair<Rule>) -> Expr {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::lit_null => Expr::Null,
        Rule::lit_true => Expr::Bool(true),
        Rule::lit_false => Expr::Bool(false),
        Rule::lit_int => Expr::Int(inner.as_str().parse().unwrap()),
        Rule::lit_float => Expr::Float(inner.as_str().parse().unwrap()),
        Rule::lit_fstring => {
            let raw = inner.as_str();
            let content = &raw[2..raw.len() - 1]; // strip `f"` prefix and `"` suffix
            let parts = parse_fstring_content(content);
            Expr::FString(parts)
        }
        Rule::lit_str => {
            let s = inner.into_inner().next().unwrap();
            let raw = s.as_str();
            // Strip surrounding quote characters and process backslash
            // escapes (`\n`, `\t`, `\r`, `\\`, `\"`, `\'`, `\0`, `\xNN`,
            // `\uXXXX`). Pre-fix this was raw passthrough — `"a\nb"`
            // became the literal 4-char string `a\nb`, breaking
            // every method that operates on real newlines (lines,
            // dedent, indent, words, regex with `\n`, etc.).
            Expr::Str(unescape_str_lit(&raw[1..raw.len() - 1]))
        }
        r => panic!("unexpected literal rule: {:?}", r),
    }
}

/// Parse the interior of an f-string (`f"…{expr}…"`) into a list of `FStringPart`
/// values. `{{` and `}}` are escape sequences for literal braces.
fn parse_fstring_content(raw: &str) -> Vec<FStringPart> {
    let mut parts = Vec::new();
    let mut lit = String::new();
    let mut chars = raw.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '{' => {
                if chars.peek() == Some(&'{') {
                    chars.next();
                    lit.push('{');
                } else {
                    if !lit.is_empty() {
                        parts.push(FStringPart::Lit(std::mem::take(&mut lit)));
                    }
                    let mut inner = String::new();
                    let mut depth = 1usize;
                    for c2 in chars.by_ref() {
                        match c2 {
                            '{' => {
                                depth += 1;
                                inner.push(c2);
                            }
                            '}' => {
                                depth -= 1;
                                if depth == 0 {
                                    break;
                                }
                                inner.push(c2);
                            }
                            _ => inner.push(c2),
                        }
                    }
                    let (expr_str, fmt) = split_fstring_interp(&inner);
                    let expr = parse(expr_str.trim())
                        .unwrap_or_else(|e| panic!("f-string parse error in {{{}}}: {}", inner, e));
                    parts.push(FStringPart::Interp { expr, fmt });
                }
            }
            '}' if chars.peek() == Some(&'}') => {
                chars.next();
                lit.push('}');
            }
            _ => lit.push(c),
        }
    }
    if !lit.is_empty() {
        parts.push(FStringPart::Lit(lit));
    }
    parts
}

/// Split an f-string interpolation `{…}` interior at the first top-level `|`
/// (pipe format) or `:` (spec format), returning the expression substring and
/// an optional `FmtSpec`. Depth tracking avoids splitting inside nested braces.
fn split_fstring_interp(inner: &str) -> (&str, Option<FmtSpec>) {
    // Scan at depth 0 only; `(`, `[`, `{` increase depth.
    let mut depth = 0usize;
    let mut pipe_pos: Option<usize> = None;
    let mut colon_pos: Option<usize> = None;
    for (i, c) in inner.char_indices() {
        match c {
            '(' | '[' | '{' => depth += 1,
            ')' | ']' | '}' => {
                if depth > 0 {
                    depth -= 1;
                }
            }
            '|' if depth == 0 && pipe_pos.is_none() => pipe_pos = Some(i),
            ':' if depth == 0 && colon_pos.is_none() => colon_pos = Some(i),
            _ => {}
        }
    }
    if let Some(p) = pipe_pos {
        return (
            &inner[..p],
            Some(FmtSpec::Pipe(inner[p + 1..].trim().to_string())),
        );
    }
    if let Some(c) = colon_pos {
        return (&inner[..c], Some(FmtSpec::Spec(inner[c + 1..].to_string())));
    }
    (inner, None)
}

/// Parse a `let name = init in body` expression, supporting multiple bindings
/// (`let a = 1, b = 2 in …`) by folding them right-to-left into nested
/// `Expr::Let` nodes.
fn parse_let(pair: Pair<Rule>) -> Expr {
    // Filter out `let` and `in` keywords, then split: all but the last
    // pair are bindings; the last is the body expression.
    let inner: Vec<Pair<Rule>> = pair
        .into_inner()
        .filter(|p| !matches!(p.as_rule(), Rule::kw_let | Rule::kw_in))
        .collect();
    let (bindings, body_pair) = inner.split_at(inner.len() - 1);
    let body = parse_expr(body_pair[0].clone());
    let mut tuple_counter = 0u32;
    bindings.iter().rev().fold(body, |acc, b| {
        let mut bi = b.clone().into_inner();
        let target = bi.next().unwrap();
        let init = parse_expr(bi.next().unwrap());
        match target.as_rule() {
            Rule::ident => Expr::Let {
                name: target.as_str().to_string(),
                init: Box::new(init),
                body: Box::new(acc),
            },
            Rule::let_target => {
                let inner = target.into_inner().next().unwrap();
                match inner.as_rule() {
                    Rule::ident => Expr::Let {
                        name: inner.as_str().to_string(),
                        init: Box::new(init),
                        body: Box::new(acc),
                    },
                    Rule::let_tuple_target => lower_tuple_let_binding(
                        init,
                        parse_let_tuple_names(inner),
                        acc,
                        &mut tuple_counter,
                    ),
                    _ => unreachable!("unexpected let target inner: {:?}", inner.as_rule()),
                }
            }
            Rule::let_tuple_target => lower_tuple_let_binding(
                init,
                parse_let_tuple_names(target),
                acc,
                &mut tuple_counter,
            ),
            _ => unreachable!("unexpected let binding target: {:?}", target.as_rule()),
        }
    })
}

fn parse_let_tuple_names(pair: Pair<Rule>) -> Vec<String> {
    pair.into_inner()
        .filter(|p| matches!(p.as_rule(), Rule::ident))
        .map(|p| p.as_str().to_string())
        .collect()
}

fn lower_tuple_let_binding(init: Expr, names: Vec<String>, body: Expr, counter: &mut u32) -> Expr {
    let tmp = loop {
        let candidate = format!("__lettuple_{}", *counter);
        *counter += 1;
        if !names.iter().any(|name| name == &candidate) {
            break candidate;
        }
    };
    let destructured = names
        .into_iter()
        .enumerate()
        .rev()
        .fold(body, |acc, (i, name)| {
            let indexed = Expr::Chain(
                Box::new(Expr::Ident(tmp.clone())),
                vec![Step::Index(i as i64)],
            );
            Expr::Let {
                name,
                init: Box::new(indexed),
                body: Box::new(acc),
            }
        });
    Expr::Let {
        name: tmp,
        init: Box::new(init),
        body: Box::new(destructured),
    }
}

/// Parsed lambda parameter: either a single identifier or an `[a, b, ...]`
/// array-pattern destructure. Used internally by `parse_lambda` /
/// `parse_arrow_lambda` to desugar destructure params into
/// `synthetic_name + chained let-bindings` before constructing
/// `Expr::Lambda`.
enum LambdaPatParam {
    Ident(String),
    /// `[a, b, ...rest]` — bind each fixed-prefix name to the indexed
    /// element of the receiver, with an optional trailing identifier that
    /// captures the rest of the array (sliced from `len(prefix)` onward).
    Array {
        names: Vec<String>,
        rest: Option<String>,
    },
}

/// Walk a `lambda_params` / `arrow_params` pair and collect each child as
/// either an identifier or an array-pattern.
fn collect_lambda_params(params_pair: Pair<Rule>) -> Vec<LambdaPatParam> {
    let mut out = Vec::new();
    for p in params_pair.into_inner() {
        match p.as_rule() {
            Rule::ident => out.push(LambdaPatParam::Ident(p.as_str().to_string())),
            Rule::lambda_param => {
                // The `lambda_param` rule wraps either an `ident` or a
                // `lambda_array_pat`. Unwrap one level.
                let inner = p.into_inner().next().unwrap();
                match inner.as_rule() {
                    Rule::ident => out.push(LambdaPatParam::Ident(inner.as_str().to_string())),
                    Rule::lambda_array_pat => {
                        out.push(parse_lambda_array_pat(inner));
                    }
                    _ => {}
                }
            }
            Rule::lambda_array_pat => {
                out.push(parse_lambda_array_pat(p));
            }
            _ => {}
        }
    }
    out
}

/// Decode a `lambda_array_pat` Pest pair into the destructure shape:
/// the ordered list of fixed-prefix names, plus an optional trailing
/// `...rest` capture identifier.
fn parse_lambda_array_pat(pair: Pair<Rule>) -> LambdaPatParam {
    let mut names: Vec<String> = Vec::new();
    let mut rest: Option<String> = None;
    for child in pair.into_inner() {
        match child.as_rule() {
            Rule::ident => names.push(child.as_str().to_string()),
            Rule::lambda_array_rest => {
                let inner = child.into_inner().next().unwrap();
                rest = Some(inner.as_str().to_string());
            }
            _ => {}
        }
    }
    LambdaPatParam::Array { names, rest }
}

/// Lower a `[a, b, ...rest]` array-pattern into a synthetic ident plus a chain
/// of `let` bindings indexing the synthetic. Returns the synthetic name and
/// a function that wraps a body expression in the destructuring lets.
///
/// `__lampat_N` reuses the same naming scheme everywhere; the synthetic
/// shadows nothing user-visible since identifiers starting with `__` are
/// reserved.
fn synth_destructure(
    counter: &mut u32,
    pat: Vec<String>,
    rest: Option<String>,
) -> (String, Box<dyn FnOnce(Expr) -> Expr>) {
    let id = format!("__lampat_{}", *counter);
    *counter += 1;
    let synth = id.clone();
    let wrap: Box<dyn FnOnce(Expr) -> Expr> = Box::new(move |body| {
        // Optional rest: `let rest = synth[len(pat):] in body`. Innermost
        // because the index lookups for fixed names then bind around it.
        let mut acc = body;
        if let Some(rest_name) = rest {
            let prefix_len = pat.len() as i64;
            let slice = Expr::Chain(
                Box::new(Expr::Ident(synth.clone())),
                vec![Step::Slice(Some(prefix_len), None, None)],
            );
            acc = Expr::Let {
                name: rest_name,
                init: Box::new(slice),
                body: Box::new(acc),
            };
        }
        // Build nested Let from innermost outward: rightmost name is the
        // innermost binding. Walk pat in reverse so the leftmost binding
        // ends up outermost (so name-shadowing matches source order).
        pat.into_iter()
            .enumerate()
            .rev()
            .fold(acc, |acc, (i, name)| {
                // `let name = synth[i] in acc`
                let index = Expr::Chain(
                    Box::new(Expr::Ident(synth.clone())),
                    vec![Step::Index(i as i64)],
                );
                Expr::Let {
                    name,
                    init: Box::new(index),
                    body: Box::new(acc),
                }
            })
    });
    (id, wrap)
}

/// Lower a list of `LambdaPatParam` to (final param-name list, body wrapper).
/// Identifier params pass through; array-pattern params get synthetic names
/// and contribute let-binding wrappers.
fn lower_lambda_params(
    params: Vec<LambdaPatParam>,
) -> (Vec<String>, Box<dyn FnOnce(Expr) -> Expr>) {
    let mut names = Vec::with_capacity(params.len());
    let mut wrappers: Vec<Box<dyn FnOnce(Expr) -> Expr>> = Vec::new();
    let mut counter: u32 = 0;
    for p in params {
        match p {
            LambdaPatParam::Ident(n) => names.push(n),
            LambdaPatParam::Array { names: pat, rest } => {
                let (synth, wrap) = synth_destructure(&mut counter, pat, rest);
                names.push(synth);
                wrappers.push(wrap);
            }
        }
    }
    let body_wrap: Box<dyn FnOnce(Expr) -> Expr> = Box::new(move |body| {
        // Apply outermost-first so leftmost destructure is outer let.
        wrappers.into_iter().rev().fold(body, |acc, w| w(acc))
    });
    (names, body_wrap)
}

/// Parse a `lambda params body` expression (keyword-form lambda) into
/// `Expr::Lambda`. Array-pattern parameters are desugared via
/// `lower_lambda_params`.
fn parse_lambda(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner().filter(|p| p.as_rule() != Rule::kw_lambda);
    let params_pair = inner.next().unwrap();
    let raw = collect_lambda_params(params_pair);
    let (params, wrap) = lower_lambda_params(raw);
    let body = parse_expr(inner.next().unwrap());
    Expr::Lambda {
        params,
        body: Box::new(wrap(body)),
    }
}

/// Parse an arrow-lambda expression (`(params) => body`) into `Expr::Lambda`,
/// using the same representation as keyword-form lambdas.
fn parse_arrow_lambda(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let params_pair = inner.next().unwrap();
    let raw = collect_lambda_params(params_pair);
    let (params, wrap) = lower_lambda_params(raw);
    let body = parse_expr(inner.next().unwrap());
    Expr::Lambda {
        params,
        body: Box::new(wrap(body)),
    }
}

/// Filter keyword tokens (`for`, `in`, `if`) out of a comprehension pair's
/// children, returning only the meaningful sub-expressions and variable lists.
fn comp_inner_filter(pair: Pair<Rule>) -> impl Iterator<Item = Pair<Rule>> {
    pair.into_inner()
        .filter(|p| !matches!(p.as_rule(), Rule::kw_for | Rule::kw_in | Rule::kw_if))
}

/// Collect all `ident` children of a `comp_vars` pair as `Vec<String>`.
fn parse_comp_vars(pair: Pair<Rule>) -> Vec<String> {
    pair.into_inner()
        .filter(|p| p.as_rule() == Rule::ident)
        .map(|p| p.as_str().to_string())
        .collect()
}

/// Process backslash escapes inside a string literal body (the contents
/// between matching quotes, *not* including the quotes themselves).
/// Recognised escapes: `\n`, `\r`, `\t`, `\0`, `\\`, `\"`, `\'`, `\xNN`
/// (two hex digits → byte), `\uXXXX` (four hex digits → Unicode code
/// point). An unrecognised escape sequence is preserved literally
/// (e.g. `\d` stays `\d`) so regex patterns that pass through string
/// literals continue to work.
fn unescape_str_lit(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = String::with_capacity(s.len());
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if b != b'\\' || i + 1 >= bytes.len() {
            out.push(b as char);
            i += 1;
            continue;
        }
        let next = bytes[i + 1];
        match next {
            b'n' => {
                out.push('\n');
                i += 2;
            }
            b'r' => {
                out.push('\r');
                i += 2;
            }
            b't' => {
                out.push('\t');
                i += 2;
            }
            b'0' => {
                out.push('\0');
                i += 2;
            }
            b'\\' => {
                out.push('\\');
                i += 2;
            }
            b'"' => {
                out.push('"');
                i += 2;
            }
            b'\'' => {
                out.push('\'');
                i += 2;
            }
            b'x' if i + 3 < bytes.len() => {
                let hex = &s[i + 2..i + 4];
                if let Ok(n) = u8::from_str_radix(hex, 16) {
                    out.push(n as char);
                    i += 4;
                } else {
                    out.push('\\');
                    i += 1;
                }
            }
            b'u' if i + 5 < bytes.len() => {
                let hex = &s[i + 2..i + 6];
                if let Ok(n) = u32::from_str_radix(hex, 16) {
                    if let Some(c) = char::from_u32(n) {
                        out.push(c);
                        i += 6;
                        continue;
                    }
                }
                out.push('\\');
                i += 1;
            }
            // Unknown escape: keep the backslash + next char literal so
            // regex patterns (`\d`, `\w`, `\s`) survive intact.
            _ => {
                out.push('\\');
                i += 1;
            }
        }
    }
    out
}

/// Conjoin a sequence of `if` clauses with logical `and`. Returns `None`
/// when the iterator is empty so callers can keep `cond: None` for the
/// no-guard case (which lets the runtime skip the predicate check entirely).
fn collect_comp_conds<'a, I: Iterator<Item = Pair<'a, Rule>>>(rest: I) -> Option<Box<Expr>> {
    let exprs: Vec<Expr> = rest.map(parse_expr).collect();
    let mut it = exprs.into_iter();
    let first = it.next()?;
    let combined = it.fold(first, |acc, e| {
        Expr::BinOp(Box::new(acc), BinOp::And, Box::new(e))
    });
    Some(Box::new(combined))
}

/// Parse a list comprehension `[expr for vars in iter if cond ...]` into
/// `Expr::ListComp`. Multiple `if` clauses are folded together with `and`.
fn parse_list_comp(pair: Pair<Rule>) -> Expr {
    let mut inner = comp_inner_filter(pair);
    let expr = parse_expr(inner.next().unwrap());
    let vars = parse_comp_vars(inner.next().unwrap());
    let iter = parse_expr(inner.next().unwrap());
    let cond = collect_comp_conds(inner);
    Expr::ListComp {
        expr: Box::new(expr),
        vars,
        iter: Box::new(iter),
        cond,
    }
}

/// Parse a dict comprehension `{key: val for vars in iter if cond ...}` into
/// `Expr::DictComp`. Multiple `if` clauses are folded together with `and`.
fn parse_dict_comp(pair: Pair<Rule>) -> Expr {
    let mut inner = comp_inner_filter(pair);
    let key = parse_expr(inner.next().unwrap());
    let val = parse_expr(inner.next().unwrap());
    let vars = parse_comp_vars(inner.next().unwrap());
    let iter = parse_expr(inner.next().unwrap());
    let cond = collect_comp_conds(inner);
    Expr::DictComp {
        key: Box::new(key),
        val: Box::new(val),
        vars,
        iter: Box::new(iter),
        cond,
    }
}

/// Parse a set comprehension `{expr for vars in iter if cond ...}` into
/// `Expr::SetComp`. Multiple `if` clauses are folded together with `and`.
fn parse_set_comp(pair: Pair<Rule>) -> Expr {
    let mut inner = comp_inner_filter(pair);
    let expr = parse_expr(inner.next().unwrap());
    let vars = parse_comp_vars(inner.next().unwrap());
    let iter = parse_expr(inner.next().unwrap());
    let cond = collect_comp_conds(inner);
    Expr::SetComp {
        expr: Box::new(expr),
        vars,
        iter: Box::new(iter),
        cond,
    }
}

/// Parse a generator comprehension `(expr for vars in iter if cond ...)` into
/// `Expr::GenComp`. Semantically identical to `ListComp` but distinct in AST.
/// Multiple `if` clauses are folded together with `and`.
fn parse_gen_comp(pair: Pair<Rule>) -> Expr {
    let mut inner = comp_inner_filter(pair);
    let expr = parse_expr(inner.next().unwrap());
    let vars = parse_comp_vars(inner.next().unwrap());
    let iter = parse_expr(inner.next().unwrap());
    let cond = collect_comp_conds(inner);
    Expr::GenComp {
        expr: Box::new(expr),
        vars,
        iter: Box::new(iter),
        cond,
    }
}

/// Parse an object constructor `{ field, … }` into `Expr::Object`, collecting
/// all `obj_field` children via `parse_obj_field`.
fn parse_obj(pair: Pair<Rule>) -> Expr {
    let fields = pair
        .into_inner()
        .filter(|p| p.as_rule() == Rule::obj_field)
        .map(parse_obj_field)
        .collect();
    Expr::Object(fields)
}

/// Parse a single object field entry, dispatching on the field variant:
/// dynamic key-value, optional value, optional shorthand, spread, deep-spread,
/// conditional kv, or plain shorthand name.
fn parse_obj_field(pair: Pair<Rule>) -> ObjField {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::obj_field_dyn => {
            let mut i = inner.into_inner();
            let key = parse_expr(i.next().unwrap());
            let val = parse_expr(i.next().unwrap());
            ObjField::Dynamic { key, val }
        }
        Rule::obj_field_opt_v => {
            let mut i = inner.into_inner();
            let key = obj_key_str(i.next().unwrap());
            let val = parse_expr(i.next().unwrap());
            ObjField::Kv {
                key,
                val,
                optional: true,
                cond: None,
            }
        }
        Rule::obj_field_opt => {
            let key = obj_key_str(inner.into_inner().next().unwrap());
            ObjField::Kv {
                key: key.clone(),
                val: Expr::Ident(key),
                optional: true,
                cond: None,
            }
        }
        Rule::obj_field_spread | Rule::obj_field_spread_star => {
            let expr = parse_expr(inner.into_inner().next().unwrap());
            ObjField::Spread(expr)
        }
        Rule::obj_field_spread_deep => {
            let expr = parse_expr(inner.into_inner().next().unwrap());
            ObjField::SpreadDeep(expr)
        }
        Rule::obj_field_kv => {
            let mut cond: Option<Expr> = None;
            let mut key: Option<String> = None;
            let mut val: Option<Expr> = None;
            let mut saw_when = false;
            for p in inner.into_inner() {
                match p.as_rule() {
                    Rule::kw_when => saw_when = true,
                    Rule::obj_key_expr => key = Some(obj_key_str(p)),
                    Rule::expr => {
                        if saw_when {
                            cond = Some(parse_expr(p));
                        } else {
                            val = Some(parse_expr(p));
                        }
                    }
                    _ => {}
                }
            }
            ObjField::Kv {
                key: key.expect("obj_field_kv missing key"),
                val: val.expect("obj_field_kv missing val"),
                optional: false,
                cond,
            }
        }
        Rule::obj_field_short => {
            let name = inner.into_inner().next().unwrap().as_str().to_string();
            ObjField::Short(name)
        }
        r => panic!("unexpected obj_field rule: {:?}", r),
    }
}

/// Extract the string key from an `obj_key_expr` pair, unwrapping either a
/// loose identifier (which permits reserved keywords like `kind` in key
/// position) or a quoted string literal.
fn obj_key_str(pair: Pair<Rule>) -> String {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::ident | Rule::loose_ident => inner.as_str().to_string(),
        Rule::lit_str => {
            let s = inner.into_inner().next().unwrap();
            let raw = s.as_str();
            raw[1..raw.len() - 1].to_string()
        }
        r => panic!("unexpected obj_key_expr rule: {:?}", r),
    }
}

/// Parse an array constructor `[elem, …]` into `Expr::Array`, handling both
/// plain expressions and spread elements (`...expr`).
fn parse_arr(pair: Pair<Rule>) -> Expr {
    let elems = pair
        .into_inner()
        .filter(|p| p.as_rule() == Rule::arr_elem)
        .map(|elem| {
            let inner = elem.into_inner().next().unwrap();
            match inner.as_rule() {
                Rule::arr_spread => {
                    let expr = parse_expr(inner.into_inner().next().unwrap());
                    ArrayElem::Spread(expr)
                }
                _ => ArrayElem::Expr(parse_expr(inner)),
            }
        })
        .collect();
    Expr::Array(elems)
}

/// Parse a top-level global function call `name(args)` into
/// `Expr::GlobalCall`, used for functions that are not dot-method syntax
/// (e.g. `coalesce(…)`, `range(…)`).
fn parse_global_call(pair: Pair<Rule>) -> Expr {
    let mut inner = pair.into_inner();
    let name = inner.next().unwrap().as_str().to_string();
    let args = inner.next().map(parse_arg_list).unwrap_or_default();
    Expr::GlobalCall { name, args }
}

/// Parse a `patch root { field: val … }` block into `Expr::Patch`, collecting
/// all `patch_field` operations and the mandatory root expression.
fn parse_patch(pair: Pair<Rule>) -> Expr {
    let mut root: Option<Expr> = None;
    let mut ops: Vec<PatchOp> = Vec::new();
    for p in pair.into_inner() {
        match p.as_rule() {
            Rule::kw_patch => {}
            Rule::patch_field => ops.push(parse_patch_field(p)),
            _ => {
                // The root expression comes first (before any patch_field rules);
                // ignore the kw_patch token by matching it above.
                if root.is_none() {
                    root = Some(parse_expr(p));
                }
            }
        }
    }
    Expr::Patch {
        root: Box::new(root.expect("patch requires root expression")),
        ops,
    }
}

/// Parse a single `field: value [when cond]` entry inside a patch block into
/// a `PatchOp`, extracting the path, value, and optional condition.
fn parse_patch_field(pair: Pair<Rule>) -> PatchOp {
    let mut path: Vec<PathStep> = Vec::new();
    let mut val: Option<Expr> = None;
    let mut cond: Option<Expr> = None;
    let mut saw_when = false;
    for p in pair.into_inner() {
        match p.as_rule() {
            Rule::patch_key => path = parse_patch_key(p),
            Rule::kw_when => saw_when = true,
            Rule::expr => {
                if saw_when {
                    cond = Some(parse_expr(p));
                } else {
                    val = Some(parse_expr(p));
                }
            }
            _ => {}
        }
    }
    PatchOp {
        path,
        val: val.expect("patch_field missing val"),
        cond,
    }
}

/// Parse a patch key (`field.sub[0].*` etc.) into a `Vec<PathStep>`, starting
/// with the mandatory leading identifier and followed by zero or more
/// `patch_step` refinements.
fn parse_patch_key(pair: Pair<Rule>) -> Vec<PathStep> {
    let mut steps: Vec<PathStep> = Vec::new();
    let mut first = true;
    for p in pair.into_inner() {
        match p.as_rule() {
            Rule::ident if first => {
                steps.push(PathStep::Field(p.as_str().to_string()));
                first = false;
            }
            Rule::patch_step => steps.push(parse_patch_step(p)),
            _ => {}
        }
    }
    steps
}

/// Parse a single patch path step (`pp_dot_field`, `pp_index`, `pp_wild`,
/// `pp_wild_filter`, or `pp_descendant`) into a `PathStep`.
fn parse_patch_step(pair: Pair<Rule>) -> PathStep {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::pp_dot_field => {
            let name = inner.into_inner().next().unwrap().as_str().to_string();
            PathStep::Field(name)
        }
        Rule::pp_index => {
            let idx: i64 = inner.into_inner().next().unwrap().as_str().parse().unwrap();
            PathStep::Index(idx)
        }
        Rule::pp_wild => PathStep::Wildcard,
        Rule::pp_wild_filter => {
            // Extract the inner filter expression from `[* if expr]`.
            let mut e: Option<Expr> = None;
            for p in inner.into_inner() {
                if p.as_rule() == Rule::expr {
                    e = Some(parse_expr(p));
                }
            }
            PathStep::WildcardFilter(Box::new(e.expect("pp_wild_filter missing expr")))
        }
        Rule::pp_descendant => {
            let name = inner.into_inner().next().unwrap().as_str().to_string();
            PathStep::Descendant(name)
        }
        r => panic!("unexpected patch_step rule: {:?}", r),
    }
}

/// Parse an argument list into a `Vec<Arg>`, filtering out separator tokens
/// and mapping each `arg` rule to a positional or named `Arg`.
fn parse_arg_list(pair: Pair<Rule>) -> Vec<Arg> {
    pair.into_inner()
        .filter(|p| p.as_rule() == Rule::arg)
        .map(parse_arg)
        .collect()
}

/// Parse a single argument, returning `Arg::Named(name, expr)` for named args
/// (`key: expr`) and `Arg::Pos(expr)` for positional args.
fn parse_arg(pair: Pair<Rule>) -> Arg {
    let inner = pair.into_inner().next().unwrap();
    match inner.as_rule() {
        Rule::named_arg => {
            let mut i = inner.into_inner();
            let name = i.next().unwrap().as_str().to_string();
            let val = parse_expr(i.next().unwrap());
            Arg::Named(name, val)
        }
        Rule::pos_arg => Arg::Pos(parse_expr(inner.into_inner().next().unwrap())),
        r => panic!("unexpected arg rule: {:?}", r),
    }
}