quamina 0.6.0

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

use crate::unicode_categories::{get_block_ranges, get_category_ranges};

/// A pair of runes representing an inclusive range [lo, hi].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RunePair {
    pub lo: char,
    pub hi: char,
}

/// A collection of rune pairs representing a character class.
pub type RuneRange = Vec<RunePair>;

/// Maximum quantifier value (Go uses 100).
pub const REGEXP_QUANTIFIER_MAX: i32 = 100;

/// The escape character (~ instead of \ to avoid JSON escaping).
const ESCAPE: char = '~';

/// Maximum Unicode code point value.
pub const RUNE_MAX: char = '\u{10FFFF}';

/// Surrogate range boundaries (these are invalid Unicode code points for chars)
const SURROGATE_START_CP: u32 = 0xD800;
const SURROGATE_END_CP: u32 = 0xDFFF;

/// A quantified atom in the regexp tree.
#[derive(Debug, Clone)]
pub struct QuantifiedAtom {
    /// True if this is a `.` (matches any character)
    pub is_dot: bool,
    /// Rune ranges for character matching
    pub runes: RuneRange,
    /// Minimum number of times to match
    pub quant_min: i32,
    /// Maximum number of times to match
    pub quant_max: i32,
    /// Subtree for parenthesized groups
    pub subtree: Option<Root>,
    /// Cache key for large Unicode categories (e.g., "L", "Lu", "-L" for negated)
    /// Used to cache pre-built FA shells for performance.
    pub cache_key: Option<String>,
    /// Lookaround assertion type (if this atom is a lookaround group)
    pub lookaround: Option<LookaroundType>,
    /// For ASCII-only negated patterns like `[^x]`, stores the negated bytes (1-3).
    /// Used for memchr acceleration - since JSON input is valid UTF-8, we don't
    /// need UTF-8 validation during matching, so these are the only exit bytes.
    pub ascii_negated_bytes: Option<Vec<u8>>,
    /// Word boundary marker: Some(true) = `~b`, Some(false) = `~B`.
    pub is_word_boundary: Option<bool>,
}

impl Default for QuantifiedAtom {
    fn default() -> Self {
        Self {
            is_dot: false,
            runes: Vec::new(),
            quant_min: 1,
            quant_max: 1,
            subtree: None,
            cache_key: None,
            lookaround: None,
            ascii_negated_bytes: None,
            is_word_boundary: None,
        }
    }
}

impl QuantifiedAtom {
    /// Returns true if this atom matches exactly once (no quantifier).
    #[inline]
    #[must_use]
    pub const fn is_singleton(&self) -> bool {
        self.quant_min == 1 && self.quant_max == 1
    }

    /// Returns true if this atom is optional (?).
    #[inline]
    #[must_use]
    pub const fn is_qm(&self) -> bool {
        self.quant_min == 0 && self.quant_max == 1
    }

    /// Returns true if this atom uses + (one or more).
    #[inline]
    #[must_use]
    pub const fn is_plus(&self) -> bool {
        self.quant_min == 1 && self.quant_max == REGEXP_QUANTIFIER_MAX
    }

    /// Returns true if this atom uses * (zero or more).
    #[inline]
    #[must_use]
    pub const fn is_star(&self) -> bool {
        self.quant_min == 0 && self.quant_max == REGEXP_QUANTIFIER_MAX
    }
}

/// A branch in the regexp (sequence of atoms).
pub type Branch = Vec<QuantifiedAtom>;

/// The root of a parsed regexp (alternatives separated by |).
pub type Root = Vec<Branch>;

/// Type of lookaround assertion
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LookaroundType {
    /// `(?=...)` - positive lookahead: match if followed by pattern
    PositiveLookahead,
    /// `(?!...)` - negative lookahead: match if NOT followed by pattern
    NegativeLookahead,
    /// `(?<=...)` - positive lookbehind: match if preceded by pattern
    PositiveLookbehind,
    /// `(?<!...)` - negative lookbehind: match if NOT preceded by pattern
    NegativeLookbehind,
}

impl LookaroundType {
    /// Returns true if this is a negative lookaround ((?!...) or (?<!...))
    #[must_use]
    pub const fn is_negative(&self) -> bool {
        matches!(self, Self::NegativeLookahead | Self::NegativeLookbehind)
    }

    /// Returns true if this is a lookbehind ((?<=...) or (?<!...))
    #[must_use]
    pub const fn is_lookbehind(&self) -> bool {
        matches!(self, Self::PositiveLookbehind | Self::NegativeLookbehind)
    }
}

/// Features found during parsing (for validation).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegexpFeature {
    Dot,
    Star,
    Plus,
    QuestionMark,
    Range,
    ParenGroup,
    NonCapturingGroup,
    LazyQuantifier,
    Property,
    Class,
    NegatedClass,
    OrBar,
    /// Lookaround assertion (?=, ?!, ?<=, ?<!)
    Lookaround,
    /// Word boundary assertion (~b, ~B)
    WordBoundary,
}

/// Features that are implemented in the NFA builder.
const IMPLEMENTED_FEATURES: &[RegexpFeature] = &[
    RegexpFeature::Dot,
    RegexpFeature::Class,
    RegexpFeature::NegatedClass,
    RegexpFeature::OrBar,
    RegexpFeature::ParenGroup,
    RegexpFeature::NonCapturingGroup,
    RegexpFeature::LazyQuantifier,
    RegexpFeature::QuestionMark,
    RegexpFeature::Plus,
    RegexpFeature::Star,
    RegexpFeature::Range,
    RegexpFeature::Property,
    RegexpFeature::Lookaround,
    RegexpFeature::WordBoundary,
];

/// Parser state for regexp parsing.
struct RegexpParse {
    bytes: Vec<u8>,
    index: usize,
    last_index: usize,
    nesting: Vec<Root>,
    found_features: Vec<RegexpFeature>,
    tree: Root,
}

/// Error type for regexp parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
    pub message: String,
    pub offset: usize,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} at offset {}", self.message, self.offset)
    }
}

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

impl RegexpParse {
    fn new(re: &str) -> Self {
        Self {
            bytes: re.as_bytes().to_vec(),
            index: 0,
            last_index: 0,
            nesting: Vec::new(),
            found_features: Vec::new(),
            tree: Vec::new(),
        }
    }

    fn nest(&mut self) {
        self.nesting.push(std::mem::take(&mut self.tree));
    }

    fn unnest(&mut self) -> Root {
        let subtree = std::mem::take(&mut self.tree);
        self.tree = self.nesting.pop().unwrap_or_default();
        subtree
    }

    const fn is_nested(&self) -> bool {
        !self.nesting.is_empty()
    }

    fn next_rune(&mut self) -> Result<char, Error> {
        if self.index >= self.bytes.len() {
            return Err(Error {
                message: "end of string".into(),
                offset: self.index,
            });
        }
        self.last_index = self.index;

        // Decode UTF-8
        let remaining = &self.bytes[self.index..];
        let s = std::str::from_utf8(remaining).map_err(|_| Error {
            message: "UTF-8 encoding error".into(),
            offset: self.index,
        })?;

        let c = s.chars().next().ok_or_else(|| Error {
            message: "empty string".into(),
            offset: self.index,
        })?;

        self.index += c.len_utf8();
        Ok(c)
    }

    fn require(&mut self, wanted: char) -> Result<(), Error> {
        let got = self.next_rune()?;
        if got != wanted {
            return Err(Error {
                message: format!("expected '{wanted}', got '{got}'"),
                offset: self.last_index,
            });
        }
        Ok(())
    }

    fn bypass_optional(&mut self, c: char) -> Result<bool, Error> {
        let next = self.next_rune()?;
        if next != c {
            self.backup1(next);
        }
        Ok(next == c)
    }

    const fn backup1(&mut self, one_rune: char) {
        self.index -= one_rune.len_utf8();
    }

    const fn is_empty(&self) -> bool {
        self.index >= self.bytes.len()
    }

    fn record_feature(&mut self, feature: RegexpFeature) {
        if !self.found_features.contains(&feature) {
            self.found_features.push(feature);
        }
    }

    fn found_unimplemented(&self) -> Vec<RegexpFeature> {
        self.found_features
            .iter()
            .filter(|f| !IMPLEMENTED_FEATURES.contains(f))
            .copied()
            .collect()
    }
}

// ============================================================================
// Lookaround Validation
// ============================================================================

/// Check if a tree contains any nested lookaround (lookaround inside lookaround).
/// This is not supported and should be rejected.
fn has_nested_lookaround(tree: &Root) -> bool {
    for branch in tree {
        for atom in branch {
            if atom.lookaround.is_some() {
                // This atom IS a lookaround. Check if its subtree contains another lookaround.
                if let Some(subtree) = &atom.subtree
                    && tree_has_lookaround(subtree)
                {
                    return true;
                }
            }
        }
    }
    false
}

/// Recursively check if a tree contains any lookaround at any depth.
fn tree_has_lookaround(tree: &Root) -> bool {
    for branch in tree {
        for atom in branch {
            if atom.lookaround.is_some() {
                return true;
            }
            if let Some(subtree) = &atom.subtree
                && tree_has_lookaround(subtree)
            {
                return true;
            }
        }
    }
    false
}

/// Validate lookaround constructs in a parsed tree.
/// Returns Ok(()) if valid, Err with message if invalid.
fn validate_lookarounds(tree: &Root) -> Result<(), String> {
    // Check for nested lookarounds
    if has_nested_lookaround(tree) {
        return Err("nested lookaround not supported: `(?=...(?=...)...)`".into());
    }

    // Check for variable-length lookbehind
    for branch in tree {
        for atom in branch {
            if let Some(la_type) = &atom.lookaround
                && la_type.is_lookbehind()
                && let Some(subtree) = &atom.subtree
                && has_variable_length_pattern(subtree)
            {
                return Err("variable-length lookbehind not yet supported: `(?<=a+)`; \
                                 use fixed-length like `(?<=aaa)`"
                    .into());
            }
        }
    }

    Ok(())
}

/// Check if a pattern can match strings of different lengths.
/// Used to validate lookbehind patterns which require fixed length.
fn has_variable_length_pattern(tree: &Root) -> bool {
    // A pattern is variable-length if its first branch is variable-length, or if
    // any alternation branch has a different fixed length than the first. An empty
    // tree matches only the empty string, which is fixed-length.
    if tree.is_empty() {
        return false;
    }
    let first_len = branch_fixed_length(&tree[0]);
    if first_len.is_none() {
        return true;
    }
    tree.iter()
        .skip(1)
        .any(|branch| branch_fixed_length(branch) != first_len)
}

/// Calculate the fixed length of a branch, or None if variable length.
fn branch_fixed_length(branch: &Branch) -> Option<usize> {
    let mut total = 0usize;
    for atom in branch {
        // A fixed-length atom repeats a fixed number of times, so quant_min must
        // equal quant_max; `*`, `+`, and `{m,n}` with m != n are all variable.
        if atom.quant_min != atom.quant_max {
            return None;
        }

        let atom_len = if atom.is_dot {
            // Dot matches one character, but UTF-8 encoding varies in length
            // For simplicity, we allow single-character matchers
            1
        } else if !atom.runes.is_empty() {
            // Character class - could have varying UTF-8 lengths
            // But from a character count perspective, it's always 1 char
            1
        } else if let Some(subtree) = &atom.subtree {
            // Group - recurse (but it's the match count, not UTF-8 bytes)
            if subtree.len() == 1 {
                branch_fixed_length(&subtree[0])?
            } else {
                // Alternation - check if all have same length
                let first_len = branch_fixed_length(&subtree[0])?;
                for b in subtree.iter().skip(1) {
                    if branch_fixed_length(b) != Some(first_len) {
                        return None;
                    }
                }
                first_len
            }
        } else {
            0 // Empty atom
        };

        // Multiply by quantifier. The parser doesn't produce negative
        // counts here, but if that ever changed we'd rather call the
        // length unknown (None) than wrap a -1 into a giant usize.
        total += atom_len * usize::try_from(atom.quant_min).ok()?;
    }
    Some(total)
}

/// Collect all lookaround atoms from a tree with their positions.
/// Used for pattern transformation.
#[must_use]
pub fn collect_lookarounds(tree: &Root) -> Vec<(usize, usize, LookaroundType, Root)> {
    let mut result = Vec::new();
    for (branch_idx, branch) in tree.iter().enumerate() {
        for (atom_idx, atom) in branch.iter().enumerate() {
            if let Some(la_type) = atom.lookaround
                && let Some(subtree) = &atom.subtree
            {
                result.push((branch_idx, atom_idx, la_type, subtree.clone()));
            }
        }
    }
    result
}

/// Check if a tree contains any lookaround at the top level.
#[must_use]
pub fn has_top_level_lookaround(tree: &Root) -> bool {
    for branch in tree {
        for atom in branch {
            if atom.lookaround.is_some() {
                return true;
            }
        }
    }
    false
}

/// Check if a tree contains any word boundary atom (`~b` or `~B`).
#[must_use]
pub fn has_word_boundary(tree: &Root) -> bool {
    for branch in tree {
        for atom in branch {
            if atom.is_word_boundary.is_some() {
                return true;
            }
        }
    }
    false
}

/// Word char ranges: [a-zA-Z0-9_]
fn word_char_runes() -> RuneRange {
    vec![
        RunePair { lo: 'a', hi: 'z' },
        RunePair { lo: 'A', hi: 'Z' },
        RunePair { lo: '0', hi: '9' },
        RunePair { lo: '_', hi: '_' },
    ]
}

/// Non-word char ranges (inverted word chars)
fn non_word_char_runes() -> RuneRange {
    invert_rune_range(word_char_runes())
}

/// Compute the intersection of two RuneRanges.
/// Returns the set of characters that are in BOTH ranges.
fn intersect_rune_ranges(a: &RuneRange, b: &RuneRange) -> RuneRange {
    let mut result = Vec::new();
    for ap in a {
        for bp in b {
            let lo = ap.lo.max(bp.lo);
            let hi = ap.hi.min(bp.hi);
            if lo <= hi {
                result.push(RunePair { lo, hi });
            }
        }
    }
    simplify_rune_range(result)
}

/// Result of constraining an atom's character class at a word boundary.
/// For singleton atoms, a single constrained atom is returned.
/// For quantified atoms (*, +, {n,m}), we may need to split into
/// "base quantified part" + "constrained last/first char".
enum ConstrainedAtom {
    /// Single atom with class intersection applied
    Single(QuantifiedAtom),
    /// Quantified atom that needs splitting: (base_quantified, constrained_single)
    /// Used when we need to constrain only the last/first char of a quantified run.
    Split(QuantifiedAtom, QuantifiedAtom),
    /// Quantified atom with quant_min=0 (can match zero chars).
    /// First element: Split(base, constrained) for when atom matches 1+ chars.
    /// The caller must also generate a branch where this atom is absent entirely
    /// (for the zero-match case where the boundary is at the value edge).
    SplitOrAbsent(QuantifiedAtom, QuantifiedAtom),
}

/// Constrain an atom's character class at a word boundary position.
///
/// For singleton atoms, intersects the class directly.
/// For quantified atoms (*, +, {n,m} where the boundary-adjacent occurrence
/// needs constraining), splits into base + constrained single.
///
/// `is_last_char`: true if we're constraining the last char (prefix side),
///                 false if constraining the first char (suffix side).
fn constrain_atom_at_boundary(
    atom: &QuantifiedAtom,
    class: &RuneRange,
    is_last_char: bool,
) -> Option<ConstrainedAtom> {
    if atom.subtree.is_some() {
        // Group atom — can't easily intersect
        return None;
    }

    let base_runes = if atom.is_dot {
        // Dot matches any char, use full Unicode range for intersection
        vec![RunePair {
            lo: '\0',
            hi: RUNE_MAX,
        }]
    } else {
        atom.runes.clone()
    };

    let intersected = intersect_rune_ranges(&base_runes, class);
    if intersected.is_empty() {
        return None;
    }

    // Determine cache key: if the result equals ~w or ~W, cache the FA shell
    let cache_key = if intersected == simplify_rune_range(word_char_runes()) {
        Some("wb_w".to_string())
    } else if intersected == simplify_rune_range(non_word_char_runes()) {
        Some("wb_W".to_string())
    } else {
        None
    };

    let constrained_single = QuantifiedAtom {
        runes: intersected,
        cache_key,
        ..Default::default()
    };

    if atom.is_singleton() {
        return Some(ConstrainedAtom::Single(constrained_single));
    }

    // Adjust the base quantifier: reduce count by 1 since we split off one char.
    // Same logic for both is_last_char and !is_last_char.
    let _ = is_last_char; // Used for documentation; both sides reduce by 1
    let (new_min, new_max) = (
        (atom.quant_min - 1).max(0),
        if atom.quant_max == REGEXP_QUANTIFIER_MAX {
            REGEXP_QUANTIFIER_MAX
        } else {
            atom.quant_max - 1
        },
    );

    if new_max == 0 {
        // The quantified atom was {1,1} effectively → just the constrained single
        return Some(ConstrainedAtom::Single(constrained_single));
    }

    let base = QuantifiedAtom {
        is_dot: atom.is_dot,
        runes: atom.runes.clone(),
        quant_min: new_min,
        quant_max: new_max,
        cache_key: atom.cache_key.clone(),
        ascii_negated_bytes: atom.ascii_negated_bytes.clone(),
        ..Default::default()
    };

    // If the original atom can match zero times (quant_min == 0, e.g., * or ?),
    // then the atom might be absent entirely, meaning the boundary is at the
    // value edge (where `"` is non-word). Return SplitOrAbsent so the caller
    // can generate an additional branch without this atom.
    if atom.quant_min == 0 {
        Some(ConstrainedAtom::SplitOrAbsent(base, constrained_single))
    } else {
        Some(ConstrainedAtom::Split(base, constrained_single))
    }
}

/// Expand a constrained atom into atom sequences.
/// `base_first=true` gives [base, single] (prefix/last-char side),
/// `base_first=false` gives [single, base] (suffix/first-char side).
fn expand_constrained(ca: &ConstrainedAtom, base_first: bool) -> Vec<Vec<QuantifiedAtom>> {
    match ca {
        ConstrainedAtom::Single(a) => vec![vec![a.clone()]],
        ConstrainedAtom::Split(base, single) | ConstrainedAtom::SplitOrAbsent(base, single) => {
            if base_first {
                vec![vec![base.clone(), single.clone()]]
            } else {
                vec![vec![single.clone(), base.clone()]]
            }
        }
    }
}

/// Expand a `~b`/`~B` at value start (no prefix atoms before the boundary).
///
/// The `"` delimiter before the value is non-word, so:
/// - `~b`: first char must be word
/// - `~B`: first char must be non-word
fn expand_wb_at_start(suffix: &[QuantifiedAtom], is_boundary: bool, out: &mut Vec<Branch>) {
    let wc = word_char_runes();
    let nwc = non_word_char_runes();
    let required_class = if is_boundary { &wc } else { &nwc };

    let Some(constrained) = constrain_atom_at_boundary(&suffix[0], required_class, false) else {
        return;
    };

    for atoms in expand_constrained(&constrained, false) {
        let mut branch = atoms;
        branch.extend_from_slice(&suffix[1..]);
        out.push(branch);
    }

    // SplitOrAbsent: the first suffix atom matched 0 chars, so the boundary
    // falls at value start. Constrain the next real atom instead.
    if matches!(constrained, ConstrainedAtom::SplitOrAbsent(..))
        && suffix.len() > 1
        && let Some(c2) = constrain_atom_at_boundary(&suffix[1], required_class, false)
    {
        for atoms in expand_constrained(&c2, false) {
            let mut branch = atoms;
            branch.extend_from_slice(&suffix[2..]);
            out.push(branch);
        }
    }
}

/// Expand a `~b`/`~B` at value end (no suffix atoms after the boundary).
///
/// The `"` delimiter after the value is non-word, so:
/// - `~b`: last char must be word
/// - `~B`: last char must be non-word
fn expand_wb_at_end(prefix: &[QuantifiedAtom], is_boundary: bool, out: &mut Vec<Branch>) {
    let wc = word_char_runes();
    let nwc = non_word_char_runes();
    let required_class = if is_boundary { &wc } else { &nwc };
    let last_idx = prefix.len() - 1;

    let Some(constrained) = constrain_atom_at_boundary(&prefix[last_idx], required_class, true)
    else {
        return;
    };

    for atoms in expand_constrained(&constrained, true) {
        let mut branch = prefix[..last_idx].to_vec();
        branch.extend(atoms);
        out.push(branch);
    }

    // SplitOrAbsent: last prefix atom matched 0 chars, so boundary falls
    // at the end after the preceding atom. Constrain that one instead.
    if matches!(constrained, ConstrainedAtom::SplitOrAbsent(..)) && last_idx > 0 {
        let prev = last_idx - 1;
        if let Some(c2) = constrain_atom_at_boundary(&prefix[prev], required_class, true) {
            for atoms in expand_constrained(&c2, true) {
                let mut branch = prefix[..prev].to_vec();
                branch.extend(atoms);
                out.push(branch);
            }
        }
    }
}

/// Expand a `~b`/`~B` in the middle (between prefix and suffix atoms).
///
/// Both sides are constrained: the last char of prefix and the first char of suffix
/// must be in opposite word-classes (`~b`) or the same class (`~B`).
fn expand_wb_in_middle(
    prefix: &[QuantifiedAtom],
    suffix: &[QuantifiedAtom],
    is_boundary: bool,
    out: &mut Vec<Branch>,
) {
    let wc = word_char_runes();
    let nwc = non_word_char_runes();
    let last_idx = prefix.len() - 1;

    // ~b: (last=word, first=nonword) OR (last=nonword, first=word)
    // ~B: (last=word, first=word) OR (last=nonword, first=nonword)
    let class_pairs: [(&RuneRange, &RuneRange); 2] = if is_boundary {
        [(&wc, &nwc), (&nwc, &wc)]
    } else {
        [(&wc, &wc), (&nwc, &nwc)]
    };

    for (last_class, first_class) in &class_pairs {
        let cl = constrain_atom_at_boundary(&prefix[last_idx], last_class, true);
        let cf = constrain_atom_at_boundary(&suffix[0], first_class, false);

        let (Some(cl), Some(cf)) = (&cl, &cf) else {
            continue;
        };

        // Generate all combinations from constrained prefix × suffix
        for pe in &expand_constrained(cl, true) {
            for se in &expand_constrained(cf, false) {
                let mut branch = prefix[..last_idx].to_vec();
                branch.extend(pe.clone());
                branch.extend(se.clone());
                branch.extend_from_slice(&suffix[1..]);
                out.push(branch);
            }
        }

        // SplitOrAbsent on prefix side: prefix atom absent → boundary at value start.
        // The `"` is non-word, so constrain suffix to edge class.
        if matches!(cl, ConstrainedAtom::SplitOrAbsent(..)) {
            let edge_class = if is_boundary { &wc } else { &nwc };
            if let Some(c2) = constrain_atom_at_boundary(&suffix[0], edge_class, false) {
                for se in expand_constrained(&c2, false) {
                    let mut branch = prefix[..last_idx].to_vec();
                    branch.extend(se);
                    branch.extend_from_slice(&suffix[1..]);
                    out.push(branch);
                }
            }
        }

        // SplitOrAbsent on suffix side: suffix atom absent → boundary at value end.
        if matches!(cf, ConstrainedAtom::SplitOrAbsent(..)) {
            let edge_class = if is_boundary { &wc } else { &nwc };
            if let Some(c2) = constrain_atom_at_boundary(&prefix[last_idx], edge_class, true) {
                for pe in expand_constrained(&c2, true) {
                    let mut branch = prefix[..last_idx].to_vec();
                    branch.extend(pe);
                    branch.extend_from_slice(&suffix[1..]);
                    out.push(branch);
                }
            }
        }
    }
}

/// Expand word boundaries (`~b`/`~B`) in a regexp tree using character-class intersection.
///
/// For `A~bB`: The last char of A and first char of B must be in different word-char classes.
/// This is implemented by intersecting A's last atom with `~w`/`~W` and B's first atom
/// with the opposite class, producing two alternative branches.
///
/// Returns the expanded tree (may have more branches than the input).
pub fn expand_word_boundaries(tree: &Root) -> Result<Root, String> {
    let mut result_branches: Vec<Branch> = Vec::new();

    for branch in tree {
        let wb_count = branch
            .iter()
            .filter(|a| a.is_word_boundary.is_some())
            .count();
        if wb_count == 0 {
            result_branches.push(branch.clone());
            continue;
        }
        if wb_count > 4 {
            return Err("too many word boundaries in pattern (max 4)".into());
        }

        // Expand one word boundary at a time (each pass resolves the first remaining ~b/~B)
        let mut alternatives = vec![branch.clone()];

        loop {
            let mut new_alternatives = Vec::new();
            let mut found_wb = false;

            for alt in &alternatives {
                let wb_pos = alt.iter().position(|a| a.is_word_boundary.is_some());
                let Some(pos) = wb_pos else {
                    new_alternatives.push(alt.clone());
                    continue;
                };
                found_wb = true;
                let is_boundary = alt[pos].is_word_boundary.unwrap();

                let prefix = &alt[..pos];
                let suffix = &alt[pos + 1..];

                match (prefix.is_empty(), suffix.is_empty()) {
                    (true, true) => {
                        // ~b alone: between two `"` (non-word). Boundary = never, non-boundary = always.
                        if !is_boundary {
                            new_alternatives.push(Vec::new());
                        }
                    }
                    (true, false) => expand_wb_at_start(suffix, is_boundary, &mut new_alternatives),
                    (false, true) => expand_wb_at_end(prefix, is_boundary, &mut new_alternatives),
                    (false, false) => {
                        expand_wb_in_middle(prefix, suffix, is_boundary, &mut new_alternatives);
                    }
                }
            }

            alternatives = new_alternatives;
            if !found_wb {
                break;
            }
        }

        result_branches.extend(alternatives);
    }

    // An empty result means no valid alternatives exist (e.g., hello~bworld
    // where both sides are word chars). The caller (json.rs) reports this as
    // an InvalidPattern error.
    Ok(result_branches)
}

/// Parse a regexp string into a tree structure.
pub fn parse(re: &str) -> Result<Root, Error> {
    let mut parse = RegexpParse::new(re);
    read_branches(&mut parse)?;

    // Check for unclosed parentheses
    if parse.is_nested() {
        return Err(Error {
            message: "unclosed '('".into(),
            offset: parse.index,
        });
    }

    let tree = std::mem::take(&mut parse.tree);

    // Validate lookaround constructs (nested, variable-length lookbehind)
    if parse.found_features.contains(&RegexpFeature::Lookaround)
        && let Err(msg) = validate_lookarounds(&tree)
    {
        return Err(Error {
            message: msg,
            offset: 0,
        });
    }

    let unimplemented = parse.found_unimplemented();
    if !unimplemented.is_empty() {
        return Err(Error {
            message: format!("unimplemented features: {unimplemented:?}"),
            offset: 0,
        });
    }

    Ok(tree)
}

/// Read branches separated by |
fn read_branches(parse: &mut RegexpParse) -> Result<(), Error> {
    while !parse.is_empty() {
        let branch = read_branch(parse)?;
        parse.tree.push(branch);

        if parse.is_empty() {
            return Ok(());
        }

        let b = parse.next_rune()?;
        if b == '|' {
            parse.record_feature(RegexpFeature::OrBar);
            continue;
        } else if b == ')' {
            parse.backup1(b);
            return Ok(());
        }
        // Shouldn't reach here
    }
    Ok(())
}

/// Read a single branch (sequence of pieces).
fn read_branch(parse: &mut RegexpParse) -> Result<Branch, Error> {
    let mut branch = Vec::new();

    loop {
        match read_piece(parse) {
            Ok(piece) => branch.push(piece),
            Err(e) if e.message == "stuck" => break,
            Err(e) if e.message == "end of string" => break,
            Err(e) => return Err(e),
        }
    }

    Ok(branch)
}

/// Read a piece (atom with optional quantifier).
fn read_piece(parse: &mut RegexpParse) -> Result<QuantifiedAtom, Error> {
    let mut qa = read_atom(parse)?;
    read_quantifier(parse, &mut qa)?;
    Ok(qa)
}

/// Check if a character is a "normal" character (not special).
fn is_normal_char(c: char) -> bool {
    let code = c as u32;

    if code <= 0x27 || c == ',' || c == '-' || (0x2F..=0x3E).contains(&code) {
        return true;
    }
    if (0x40..=0x5A).contains(&code) {
        return true;
    }
    // allow backslash
    if code == 0x5c {
        return true;
    }
    if (0x5E..=0x7A).contains(&code) {
        return true;
    }
    // exclude ~
    if (0x7F..=0xD7FF).contains(&code) {
        return true;
    }
    if (0xE000..=0x10FFFF).contains(&code) {
        return true;
    }
    false
}

/// Check for single-char escape sequences.
fn check_single_char_escape(c: char) -> Option<char> {
    let code = c as u32;

    // ( ) * +
    if (0x28..=0x2B).contains(&code) {
        return Some(c);
    }
    // - . ? [ \ ] ^
    if c == '-' || c == '.' || c == '?' || (0x5B..=0x5E).contains(&code) {
        return Some(c);
    }
    // Special escapes
    if c == 'n' {
        return Some('\n');
    }
    if c == 'r' {
        return Some('\r');
    }
    if c == 't' {
        return Some('\t');
    }
    // { | }
    if (0x7B..=0x7D).contains(&code) {
        return Some(c);
    }
    // Escape itself
    if c == ESCAPE {
        return Some(ESCAPE);
    }
    None
}

/// XML NameStartChar - characters that can start an XML name (XSD `\i`).
/// From W3C XML spec: <https://www.w3.org/TR/xml/#NT-NameStartChar>
fn xml_name_start_char() -> RuneRange {
    vec![
        RunePair { lo: ':', hi: ':' }, // 0x3A
        RunePair { lo: 'A', hi: 'Z' }, // 0x41-0x5A
        RunePair { lo: '_', hi: '_' }, // 0x5F
        RunePair { lo: 'a', hi: 'z' }, // 0x61-0x7A
        RunePair {
            lo: '\u{C0}',
            hi: '\u{D6}',
        },
        RunePair {
            lo: '\u{D8}',
            hi: '\u{F6}',
        },
        RunePair {
            lo: '\u{F8}',
            hi: '\u{2FF}',
        },
        RunePair {
            lo: '\u{370}',
            hi: '\u{37D}',
        },
        RunePair {
            lo: '\u{37F}',
            hi: '\u{1FFF}',
        },
        RunePair {
            lo: '\u{200C}',
            hi: '\u{200D}',
        },
        RunePair {
            lo: '\u{2070}',
            hi: '\u{218F}',
        },
        RunePair {
            lo: '\u{2C00}',
            hi: '\u{2FEF}',
        },
        RunePair {
            lo: '\u{3001}',
            hi: '\u{D7FF}',
        },
        RunePair {
            lo: '\u{F900}',
            hi: '\u{FDCF}',
        },
        RunePair {
            lo: '\u{FDF0}',
            hi: '\u{FFFD}',
        },
        RunePair {
            lo: '\u{10000}',
            hi: '\u{EFFFF}',
        },
    ]
}

/// XML NameChar - characters that can appear in an XML name (XSD `\c`).
/// Includes all NameStartChar plus additional characters.
fn xml_name_char() -> RuneRange {
    let mut rr = xml_name_start_char();
    rr.extend([
        RunePair { lo: '-', hi: '-' }, // 0x2D
        RunePair { lo: '.', hi: '.' }, // 0x2E
        RunePair { lo: '0', hi: '9' }, // 0x30-0x39
        RunePair {
            lo: '\u{B7}',
            hi: '\u{B7}',
        },
        RunePair {
            lo: '\u{300}',
            hi: '\u{36F}',
        },
        RunePair {
            lo: '\u{203F}',
            hi: '\u{2040}',
        },
    ]);
    rr
}

/// Check for multi-char escape sequences that expand to character classes.
/// Returns Some((RuneRange, Option<cache_key>)) for recognized escapes, None otherwise.
/// Large Unicode range escapes (`~i`, `~I`, `~c`, `~C`) include a cache key
/// so the FA shell can be cached and reused.
fn check_multi_char_escape(c: char) -> Option<(RuneRange, Option<String>)> {
    match c {
        // ~d = digit [0-9]
        'd' => Some((vec![RunePair { lo: '0', hi: '9' }], None)),
        // ~D = non-digit (everything except 0-9)
        'D' => Some((invert_rune_range(vec![RunePair { lo: '0', hi: '9' }]), None)),
        // ~w = word char [a-zA-Z0-9_]
        'w' => Some((word_char_runes(), None)),
        // ~W = non-word char
        'W' => Some((invert_rune_range(word_char_runes()), None)),
        // ~s = whitespace [ \t\n\r]
        's' => Some((
            vec![
                RunePair { lo: ' ', hi: ' ' },
                RunePair { lo: '\t', hi: '\t' },
                RunePair { lo: '\n', hi: '\n' },
                RunePair { lo: '\r', hi: '\r' },
            ],
            None,
        )),
        // ~S = non-whitespace
        'S' => Some((
            invert_rune_range(vec![
                RunePair { lo: ' ', hi: ' ' },
                RunePair { lo: '\t', hi: '\t' },
                RunePair { lo: '\n', hi: '\n' },
                RunePair { lo: '\r', hi: '\r' },
            ]),
            None,
        )),
        // ~i = XML NameStartChar (initial name char) — large Unicode range, cache it
        'i' => Some((xml_name_start_char(), Some("i".to_string()))),
        // ~I = NOT XML NameStartChar — large Unicode range, cache it
        'I' => Some((
            invert_rune_range(xml_name_start_char()),
            Some("-i".to_string()),
        )),
        // ~c = XML NameChar (name char) — large Unicode range, cache it
        'c' => Some((xml_name_char(), Some("c".to_string()))),
        // ~C = NOT XML NameChar — large Unicode range, cache it
        'C' => Some((invert_rune_range(xml_name_char()), Some("-c".to_string()))),
        _ => None,
    }
}

/// Read an atom.
fn read_atom(parse: &mut RegexpParse) -> Result<QuantifiedAtom, Error> {
    let b = parse.next_rune()?;

    match b {
        c if is_normal_char(c) => Ok(QuantifiedAtom {
            runes: vec![RunePair { lo: c, hi: c }],
            ..Default::default()
        }),
        '.' => {
            parse.record_feature(RegexpFeature::Dot);
            Ok(QuantifiedAtom {
                is_dot: true,
                ..Default::default()
            })
        }
        '(' => read_group(parse),
        ')' => {
            if parse.is_nested() {
                parse.backup1(b);
                Err(Error {
                    message: "stuck".into(),
                    offset: parse.last_index,
                })
            } else {
                Err(Error {
                    message: "unbalanced ')'".into(),
                    offset: parse.last_index,
                })
            }
        }
        '[' => {
            parse.record_feature(RegexpFeature::Class);
            let (rr, ascii_negated_bytes) = read_char_class_expr(parse)?;
            Ok(QuantifiedAtom {
                runes: rr,
                ascii_negated_bytes,
                ..Default::default()
            })
        }
        ']' => Err(Error {
            message: "invalid ']'".into(),
            offset: parse.last_index,
        }),
        c if c == ESCAPE => read_escape(parse),
        '?' | '+' | '*' | '{' => Err(Error {
            message: format!("invalid character '{b}' (quantifier without atom)"),
            offset: parse.last_index,
        }),
        // '|' is handled here too: top-level alternation is the caller's
        // problem, so back up and signal "stuck" to let it resume parsing.
        _ => {
            parse.backup1(b);
            Err(Error {
                message: "stuck".into(),
                offset: parse.last_index,
            })
        }
    }
}

/// Read a `(...)` group, including the `(?:`, `(?=`, `(?!`, `(?<=`, `(?<!` extensions.
fn read_group(parse: &mut RegexpParse) -> Result<QuantifiedAtom, Error> {
    parse.nest();
    let lookaround_type = read_group_extension(parse)?;
    read_branches(parse)?;
    parse.require(')')?;
    let subtree = parse.unnest();
    Ok(QuantifiedAtom {
        subtree: Some(subtree),
        lookaround: lookaround_type,
        ..Default::default()
    })
}

/// Classify what follows the opening `(`. Returns the lookaround kind if any.
fn read_group_extension(parse: &mut RegexpParse) -> Result<Option<LookaroundType>, Error> {
    match parse.next_rune() {
        Ok('?') => read_question_extension(parse),
        Ok(c) => {
            // Regular capturing group - back up the character we peeked.
            parse.record_feature(RegexpFeature::ParenGroup);
            parse.backup1(c);
            Ok(None)
        }
        Err(_) => {
            // Empty group or EOF - will fail on require(')') later.
            parse.record_feature(RegexpFeature::ParenGroup);
            Ok(None)
        }
    }
}

/// Handle the part after `(?`: `:`, `=`, `!`, `<=`, `<!`.
fn read_question_extension(parse: &mut RegexpParse) -> Result<Option<LookaroundType>, Error> {
    match parse.next_rune() {
        Ok(':') => {
            parse.record_feature(RegexpFeature::NonCapturingGroup);
            Ok(None)
        }
        Ok('=') => {
            parse.record_feature(RegexpFeature::Lookaround);
            Ok(Some(LookaroundType::PositiveLookahead))
        }
        Ok('!') => {
            parse.record_feature(RegexpFeature::Lookaround);
            Ok(Some(LookaroundType::NegativeLookahead))
        }
        Ok('<') => read_lookbehind_extension(parse),
        Ok(c) => Err(Error {
            message: format!("unsupported group extension (?{c}...)"),
            offset: parse.last_index,
        }),
        Err(_) => Err(Error {
            message: "unexpected end after (?".into(),
            offset: parse.last_index,
        }),
    }
}

/// Handle the part after `(?<`: either `=` (positive) or `!` (negative).
fn read_lookbehind_extension(parse: &mut RegexpParse) -> Result<Option<LookaroundType>, Error> {
    match parse.next_rune() {
        Ok('=') => {
            parse.record_feature(RegexpFeature::Lookaround);
            Ok(Some(LookaroundType::PositiveLookbehind))
        }
        Ok('!') => {
            parse.record_feature(RegexpFeature::Lookaround);
            Ok(Some(LookaroundType::NegativeLookbehind))
        }
        Ok(c) => Err(Error {
            message: format!("named capturing groups (?<{c}...) not supported"),
            offset: parse.last_index,
        }),
        Err(_) => Err(Error {
            message: "unexpected end after (?<".into(),
            offset: parse.last_index,
        }),
    }
}

/// Read the atom that follows an `ESCAPE` (`~`) byte.
fn read_escape(parse: &mut RegexpParse) -> Result<QuantifiedAtom, Error> {
    let next = parse.next_rune().map_err(|_| Error {
        message: format!("'{ESCAPE}' at end of regular expression"),
        offset: parse.last_index,
    })?;

    if let Some(escaped) = check_single_char_escape(next) {
        return Ok(QuantifiedAtom {
            runes: vec![RunePair {
                lo: escaped,
                hi: escaped,
            }],
            ..Default::default()
        });
    }

    // Multi-char escapes (~d, ~w, ~s, ~D, ~W, ~S, ~i, ~I, ~c, ~C)
    if let Some((runes, cache_key)) = check_multi_char_escape(next) {
        return Ok(QuantifiedAtom {
            runes,
            cache_key,
            ..Default::default()
        });
    }

    if next == 'p' || next == 'P' {
        return read_property_escape(parse, next);
    }

    if next == 'b' || next == 'B' {
        parse.record_feature(RegexpFeature::WordBoundary);
        return Ok(QuantifiedAtom {
            is_word_boundary: Some(next == 'b'),
            ..Default::default()
        });
    }

    // Backreferences (~1 through ~9) are not supported.
    if let Some(digit) = next.to_digit(10)
        && (1..=9).contains(&digit)
    {
        return Err(Error {
            message: format!("backreferences (~{digit}) are not supported"),
            offset: parse.last_index,
        });
    }

    Err(Error {
        message: format!("invalid character '{next}' after '{ESCAPE}'"),
        offset: parse.last_index,
    })
}

/// `~p{Name}` and the inverted `~P{Name}`.
fn read_property_escape(parse: &mut RegexpParse, marker: char) -> Result<QuantifiedAtom, Error> {
    parse.record_feature(RegexpFeature::Property);
    let (mut runes, cache_key) = read_category(parse)?;
    // `~P{...}` means NOT in the category; prefix the cache key with "-".
    let cache_key = if marker == 'P' {
        runes = invert_rune_range(runes);
        cache_key.map(|k| format!("-{k}"))
    } else {
        cache_key
    };
    Ok(QuantifiedAtom {
        runes,
        cache_key,
        ..Default::default()
    })
}

/// Read a character class expression [...]
/// Returns `(RuneRange, Option<Vec<u8>>)` where the second element contains
/// ASCII negated bytes for patterns like [^x] that can use memchr acceleration.
fn read_char_class_expr(parse: &mut RegexpParse) -> Result<(RuneRange, Option<Vec<u8>>), Error> {
    read_char_class_expr_depth(parse, 0)
}

/// Maximum nesting depth for character class subtraction (e.g., [a-[b-[c-[...]]]]).
const MAX_CLASS_SUBTRACTION_DEPTH: usize = 8;

fn read_char_class_expr_depth(
    parse: &mut RegexpParse,
    depth: usize,
) -> Result<(RuneRange, Option<Vec<u8>>), Error> {
    if depth > MAX_CLASS_SUBTRACTION_DEPTH {
        return Err(Error {
            message: "character class subtraction nested too deeply".into(),
            offset: parse.index,
        });
    }

    // Check for unclosed bracket (EOF immediately after '[')
    if parse.is_empty() {
        return Err(Error {
            message: "unclosed character class".into(),
            offset: parse.index,
        });
    }

    // Check for negation
    let is_negated = parse.bypass_optional('^')?;
    if is_negated {
        parse.record_feature(RegexpFeature::NegatedClass);
    }

    let mut rr = read_cce1s(parse)?;

    // Check for character class subtraction -[...] or trailing -
    if parse.bypass_optional('-') == Ok(true) {
        // Peek ahead to see if this is subtraction syntax -[
        let next = parse.next_rune().map_err(|_| Error {
            message: "unclosed character class".into(),
            offset: parse.index,
        })?;
        if next == '[' {
            // Character class subtraction: base-[subtract]
            // Recursively parse the subtracted class (which may itself contain subtraction)
            let (subtract_rr, _) = read_char_class_expr_depth(parse, depth + 1)?;
            rr = subtract_rune_range(rr, subtract_rr);
        } else {
            // Not subtraction — it's a trailing literal '-'
            parse.backup1(next);
            rr.push(RunePair { lo: '-', hi: '-' });
        }
    }

    parse.require(']')?;

    // Detect ASCII-only negated patterns for memchr acceleration
    // For patterns like [^x], [^/], [^"], we can accelerate by searching for exit bytes
    let ascii_negated_bytes = if is_negated {
        detect_ascii_negated_bytes(&rr)
    } else {
        None
    };

    // Apply negation if needed
    if is_negated {
        rr = invert_rune_range(rr);
    }

    Ok((rr, ascii_negated_bytes))
}

/// Detect if a rune range (BEFORE inversion) represents an ASCII-only character set
/// with 1-3 characters. Used for memchr acceleration of negated patterns.
///
/// For patterns like `[^x]`, the pre-inversion runes are just `[{x, x}]`.
/// We return Some(vec![b'x']) which can be used for memchr acceleration.
fn detect_ascii_negated_bytes(rr: &RuneRange) -> Option<Vec<u8>> {
    let mut bytes = Vec::new();

    for pair in rr {
        // Check if both lo and hi are ASCII
        if pair.lo as u32 >= 128 || pair.hi as u32 >= 128 {
            return None; // Non-ASCII character in negated set
        }

        // Expand the range and collect bytes
        for c in (pair.lo as u8)..=(pair.hi as u8) {
            bytes.push(c);
            if bytes.len() > 3 {
                return None; // Too many exit bytes for memchr acceleration
            }
        }
    }

    if bytes.is_empty() || bytes.len() > 3 {
        return None;
    }

    Some(bytes)
}

/// Read CCE1 elements
fn read_cce1s(parse: &mut RegexpParse) -> Result<RuneRange, Error> {
    let mut rr = Vec::new();
    let mut first = true;

    loop {
        let cce1 = read_cce1(parse, first)?;
        rr.extend(cce1);
        first = false;

        match parse.next_rune() {
            Ok(r) => {
                parse.backup1(r);
                if r == '-' || r == ']' {
                    return Ok(simplify_rune_range(rr));
                }
            }
            Err(_) => {
                // EOF inside character class - unclosed bracket
                return Err(Error {
                    message: "unclosed character class".into(),
                    offset: parse.index,
                });
            }
        }
    }
}

/// Check if a character is valid in a character class
fn is_cc_char(r: char) -> bool {
    let code = r as u32;
    if code <= 0x2c || (0x2e..=0x5A).contains(&code) {
        return true;
    }
    if (0x5e..=0xd7ff).contains(&code) {
        return true;
    }
    if (0xe000..=0x10ffff).contains(&code) {
        return true;
    }
    if r == '\\' {
        return true;
    }
    false
}

/// Read a single CCE1 element
fn read_cce1(parse: &mut RegexpParse, first: bool) -> Result<RuneRange, Error> {
    let r = parse.next_rune().map_err(|_| Error {
        message: "unclosed character class".into(),
        offset: parse.index,
    })?;

    let lo = if first && r == '-' {
        return Ok(vec![RunePair { lo: '-', hi: '-' }]);
    } else if r == ESCAPE {
        let next = parse.next_rune().map_err(|_| Error {
            message: "unclosed character class".into(),
            offset: parse.index,
        })?;
        if next == 'p' || next == 'P' {
            parse.record_feature(RegexpFeature::Property);
            // Inside character class, we don't use the cache key
            let (mut runes, _cache_key) = read_category(parse)?;
            // ~P{...} means NOT in the category (inverted)
            if next == 'P' {
                runes = invert_rune_range(runes);
            }
            return Ok(runes);
        }
        // Check for multi-char escapes (can't participate in ranges)
        // Inside character classes, cache_key is not used since ranges are combined.
        if let Some((runes, _cache_key)) = check_multi_char_escape(next) {
            return Ok(runes);
        }
        check_single_char_escape(next).ok_or_else(|| Error {
            message: format!("invalid character '{next}' after {ESCAPE} in character class"),
            offset: parse.last_index,
        })?
    } else {
        if !is_cc_char(r) {
            return Err(Error {
                message: format!("invalid character '{r}' in character class"),
                offset: parse.last_index,
            });
        }
        r
    };

    // Check for range
    let next = parse.next_rune().map_err(|_| Error {
        message: "unclosed character class".into(),
        offset: parse.index,
    })?;
    if next != '-' {
        parse.backup1(next);
        return Ok(vec![RunePair { lo, hi: lo }]);
    }

    // Looking at a range?
    let range_end = parse.next_rune().map_err(|_| Error {
        message: "unclosed character class".into(),
        offset: parse.index,
    })?;

    // Might be end of class -]
    if range_end == ']' {
        parse.backup1(range_end);
        return Ok(vec![RunePair { lo, hi: lo }, RunePair { lo: '-', hi: '-' }]);
    }

    // Character class subtraction: d-[ should NOT be a range attempt.
    // Back up both '[' and '-' so the caller can handle subtraction syntax.
    if range_end == '[' {
        parse.backup1(range_end);
        parse.backup1('-');
        return Ok(vec![RunePair { lo, hi: lo }]);
    }

    let hi = if range_end == ESCAPE {
        let escaped = parse.next_rune().map_err(|_| Error {
            message: "unclosed character class".into(),
            offset: parse.index,
        })?;
        check_single_char_escape(escaped).ok_or_else(|| Error {
            message: format!("invalid char '{escaped}' after - in character class"),
            offset: parse.last_index,
        })?
    } else {
        if !is_cc_char(range_end) {
            return Err(Error {
                message: format!("invalid char '{range_end}' in range"),
                offset: parse.last_index,
            });
        }
        range_end
    };

    if lo > hi {
        return Err(Error {
            message: format!("invalid range {lo}-{hi}"),
            offset: parse.last_index,
        });
    }

    Ok(vec![RunePair { lo, hi }])
}

/// Subtract one rune range from another: `base - subtract`.
/// Returns characters that are in `base` but NOT in `subtract`.
/// Both inputs are simplified before processing.
pub fn subtract_rune_range(base: RuneRange, subtract: RuneRange) -> RuneRange {
    let base = simplify_rune_range(base);
    let subtract = simplify_rune_range(subtract);

    let mut result = Vec::new();
    let mut sub_idx = 0;

    for pair in &base {
        let mut lo = pair.lo as u32;
        let hi = pair.hi as u32;

        // Walk through subtract ranges that might overlap this base range
        while sub_idx < subtract.len() && (subtract[sub_idx].hi as u32) < lo {
            let _prev = sub_idx;
            sub_idx += 1;
            // This loop terminates only because sub_idx strictly increases each
            // pass; assert it so a broken increment fails fast instead of hanging.
            debug_assert!(sub_idx > _prev, "subtract_rune_range: sub_idx must advance");
        }

        let mut si = sub_idx;
        while si < subtract.len() && (subtract[si].lo as u32) <= hi {
            let _prev_si = si;
            let sub_lo = subtract[si].lo as u32;
            let sub_hi = subtract[si].hi as u32;

            // Add the gap before this subtract range (if any)
            if lo < sub_lo
                && let (Some(r_lo), Some(r_hi)) = (char::from_u32(lo), char::from_u32(sub_lo - 1))
            {
                result.push(RunePair { lo: r_lo, hi: r_hi });
            }

            // Advance past the subtracted portion
            lo = sub_hi + 1;
            si += 1;
            // This inner loop also terminates only because si strictly increases
            // each pass; assert it so a broken increment fails fast, not hangs.
            debug_assert!(si > _prev_si, "subtract_rune_range: si must advance");
        }

        // Add remaining portion of base range after all subtract ranges
        if lo <= hi
            && let (Some(r_lo), Some(r_hi)) = (char::from_u32(lo), char::from_u32(hi))
        {
            result.push(RunePair { lo: r_lo, hi: r_hi });
        }
    }

    result
}

/// Simplify and merge overlapping rune ranges
pub fn simplify_rune_range(mut rranges: RuneRange) -> RuneRange {
    if rranges.is_empty() {
        return rranges;
    }

    rranges.sort_by_key(|rp| rp.lo);

    let mut out = Vec::new();
    let mut current = rranges[0];

    for next in rranges.iter().skip(1).copied() {
        if next.lo as u32 > current.hi as u32 + 1 {
            out.push(current);
            current = next;
            continue;
        }
        if next.hi <= current.hi {
            continue;
        }
        current.hi = next.hi;
    }
    out.push(current);
    out
}

/// Add a gap range to inverted, handling surrogate boundary
fn add_gap_range(inverted: &mut Vec<RunePair>, start: u32, end: u32) {
    // Skip empty or invalid ranges
    if start > end {
        return;
    }

    // If the range spans the surrogate area, split it.
    if spans_into_surrogate(start, end) {
        // Part before surrogates
        if let (Some(lo), Some(hi)) = (
            char::from_u32(start),
            char::from_u32(SURROGATE_START_CP - 1),
        ) {
            inverted.push(RunePair { lo, hi });
        }
        // Part after surrogates (if any)
        if past_surrogate_end(end)
            && let (Some(lo), Some(hi)) =
                (char::from_u32(SURROGATE_END_CP + 1), char::from_u32(end))
        {
            inverted.push(RunePair { lo, hi });
        }
    } else if (SURROGATE_START_CP..=SURROGATE_END_CP).contains(&start) {
        // Starts in surrogate range, only add part after
        if past_surrogate_end(end)
            && let (Some(lo), Some(hi)) =
                (char::from_u32(SURROGATE_END_CP + 1), char::from_u32(end))
        {
            inverted.push(RunePair { lo, hi });
        }
    } else {
        // Normal range (not touching surrogates)
        if let (Some(lo), Some(hi)) = (char::from_u32(start), char::from_u32(end)) {
            inverted.push(RunePair { lo, hi });
        }
    }
}

/// Returns true when `[start, end]` (inclusive) crosses into the surrogate
/// codepoint window — `start` is strictly below `SURROGATE_START_CP` and
/// `end` reaches or passes it.
const fn spans_into_surrogate(start: u32, end: u32) -> bool {
    start < SURROGATE_START_CP && end >= SURROGATE_START_CP
}

/// Returns true when a codepoint lies strictly past the surrogate window —
/// the post-surrogate tail of a split range.
const fn past_surrogate_end(end: u32) -> bool {
    end > SURROGATE_END_CP
}

/// Returns true when a sorted RunePair starting at `next_lo` overlaps or is
/// adjacent to a previously merged RunePair ending at `prev_hi`.
const fn range_pair_overlaps_or_adjacent(next_lo: u32, prev_hi: u32) -> bool {
    next_lo <= prev_hi + 1
}

/// Invert a rune range (for negated character classes).
/// Returns a range that matches everything NOT in the input range.
pub fn invert_rune_range(mut rr: RuneRange) -> RuneRange {
    rr.sort_by_key(|rp| rp.lo);

    // Merge overlapping/adjacent ranges after sorting.
    let mut merged: Vec<RunePair> = Vec::new();
    for pair in rr {
        if let Some(last) = merged.last_mut()
            && range_pair_overlaps_or_adjacent(pair.lo as u32, last.hi as u32)
        {
            last.hi = last.hi.max(pair.hi);
            continue;
        }
        merged.push(pair);
    }

    let mut inverted = Vec::new();
    let mut point: u32 = 0;

    for pair in &merged {
        let lo = pair.lo as u32;
        if lo > point {
            add_gap_range(&mut inverted, point, lo - 1);
        }
        point = pair.hi as u32 + 1;
    }

    if point <= RUNE_MAX as u32 {
        add_gap_range(&mut inverted, point, RUNE_MAX as u32);
    }

    inverted
}

/// Read a Unicode category ~p{...} or ~P{...} and return the character ranges
/// along with a cache key for large categories.
/// Handles both general categories (Lu, Ll, Nd, etc.) and Unicode blocks (IsBasicLatin, etc.).
/// Returns (ranges, cache_key) where cache_key is Some for general categories.
fn read_category(parse: &mut RegexpParse) -> Result<(RuneRange, Option<String>), Error> {
    parse.require('{')?;

    // Collect all characters until '}'
    let mut name = String::new();
    loop {
        let c = parse.next_rune()?;
        if c == '}' {
            break;
        }
        name.push(c);
    }

    if name.is_empty() {
        return Err(Error {
            message: "empty category name".into(),
            offset: parse.last_index,
        });
    }

    // Check for Unicode block (starts with "Is")
    // Unicode blocks are not cached (smaller than categories)
    if name.starts_with("Is") {
        if let Some(ranges) = get_block_ranges(&name) {
            return Ok((ranges, None));
        }
        return Err(Error {
            message: format!("unknown Unicode block ~p{{{name}}}"),
            offset: parse.last_index,
        });
    }

    // Parse as general category
    let mut chars = name.chars();
    let initial = match chars.next() {
        Some(c) => c,
        None => {
            return Err(Error {
                message: "empty category name".into(),
                offset: parse.last_index,
            });
        }
    };

    let valid_initials = ['L', 'M', 'N', 'P', 'Z', 'S', 'C'];
    if !valid_initials.contains(&initial) {
        return Err(Error {
            message: format!("unknown category {initial}"),
            offset: parse.last_index,
        });
    }

    let detail = chars.next();

    // Validate detail letter based on initial
    if let Some(d) = detail {
        let valid_details = match initial {
            'L' => "ultmo",
            'M' => "nce",
            'N' => "dlo",
            'P' => "cdseifo",
            'Z' => "slp",
            'S' => "mcko",
            'C' => "cfon",
            _ => "",
        };

        if !valid_details.contains(d) {
            return Err(Error {
                message: format!("unknown category {ESCAPE}p{{{initial}{d}"),
                offset: parse.last_index,
            });
        }

        // Check for extra characters
        if chars.next().is_some() {
            return Err(Error {
                message: format!("invalid category name ~p{{{name}}}"),
                offset: parse.last_index,
            });
        }
    }

    // Build the cache key (e.g., "L", "Lu", "Nd")
    let cache_key = detail.map_or_else(|| initial.to_string(), |d| format!("{initial}{d}"));

    // Look up the category ranges
    if let Some(ranges) = get_category_ranges(initial, detail) {
        Ok((ranges, Some(cache_key)))
    } else {
        Err(Error {
            message: format!("unknown category ~p{{{name}}}"),
            offset: parse.last_index,
        })
    }
}

/// Read a quantifier (?, *, +, {m,n}) with optional lazy modifier (?)
fn read_quantifier(parse: &mut RegexpParse, qa: &mut QuantifiedAtom) -> Result<(), Error> {
    let b = if let Ok(c) = parse.next_rune() {
        c
    } else {
        qa.quant_min = 1;
        qa.quant_max = 1;
        return Ok(());
    };

    let mut is_quantifier = true;
    match b {
        '*' => {
            parse.record_feature(RegexpFeature::Star);
            qa.quant_min = 0;
            qa.quant_max = REGEXP_QUANTIFIER_MAX;
        }
        '+' => {
            parse.record_feature(RegexpFeature::Plus);
            qa.quant_min = 1;
            qa.quant_max = REGEXP_QUANTIFIER_MAX;
        }
        '?' => {
            parse.record_feature(RegexpFeature::QuestionMark);
            qa.quant_min = 0;
            qa.quant_max = 1;
        }
        '{' => {
            parse.record_feature(RegexpFeature::Range);
            read_range_quantifier(parse, qa)?;
        }
        _ => {
            qa.quant_min = 1;
            qa.quant_max = 1;
            parse.backup1(b);
            is_quantifier = false;
        }
    }

    // Check for lazy quantifier modifier (e.g., *?, +?, {n,m}?)
    // Since we're doing pattern matching (not extraction), lazy vs greedy
    // makes no semantic difference - we just consume the optional '?'
    if is_quantifier {
        match parse.next_rune() {
            Ok('?') => {
                parse.record_feature(RegexpFeature::LazyQuantifier);
                // Lazy quantifier - consumed but has no effect on matching
            }
            Ok(c) => {
                // We consumed a char that wasn't '?', backup
                parse.backup1(c);
            }
            Err(_) => {
                // End of string - nothing to do
            }
        }
    }

    Ok(())
}

fn quantifier_too_large_error(parse: &RegexpParse) -> Error {
    Error {
        message: format!(
            "invalid range quantifier, repetition count exceeds maximum of {REGEXP_QUANTIFIER_MAX}"
        ),
        offset: parse.last_index,
    }
}

/// Read a range quantifier {m,n}
fn read_range_quantifier(parse: &mut RegexpParse, qa: &mut QuantifiedAtom) -> Result<(), Error> {
    // Helper to convert EOF to a more specific error
    let next_or_eof = |p: &mut RegexpParse| -> Result<char, Error> {
        p.next_rune().map_err(|e| {
            if e.message == "end of string" {
                Error {
                    message: "unexpected end of string in quantifier".into(),
                    offset: e.offset,
                }
            } else {
                e
            }
        })
    };

    let mut lo_digits = String::new();

    loop {
        let b = next_or_eof(parse)?;
        if b.is_ascii_digit() {
            lo_digits.push(b);
        } else {
            if lo_digits.is_empty() {
                return Err(Error {
                    message: "invalid range quantifier, expecting digits".into(),
                    offset: parse.last_index,
                });
            }

            let lo: i32 = lo_digits.parse().map_err(|_| Error {
                message: "invalid number in quantifier".into(),
                offset: parse.last_index,
            })?;
            // The NFA builder materializes one state per repetition, so
            // repetition counts must be bounded before any states are built.
            if lo > REGEXP_QUANTIFIER_MAX {
                return Err(quantifier_too_large_error(parse));
            }
            qa.quant_min = lo;
            // Default to exact match; will be updated if comma is present
            qa.quant_max = lo;

            match b {
                // {n} means exactly n times
                '}' => return Ok(()),
                // {n,} or {n,m} - will parse upper bound below
                ',' => {
                    // Set to unbounded initially for {n,} case
                    qa.quant_max = REGEXP_QUANTIFIER_MAX;
                    break;
                }
                _ => {
                    return Err(Error {
                        message: format!("unexpected character '{b}' in quantifier"),
                        offset: parse.last_index,
                    });
                }
            }
        }
    }

    // After comma
    let b = next_or_eof(parse)?;
    if b == '}' {
        return Ok(());
    }

    if !b.is_ascii_digit() {
        return Err(Error {
            message: format!("invalid character '{b}' in quantifier range, wanted a digit"),
            offset: parse.last_index,
        });
    }

    let mut hi_digits = String::from(b);
    loop {
        let b = next_or_eof(parse)?;
        if b.is_ascii_digit() {
            hi_digits.push(b);
        } else if b == '}' {
            let hi: i32 = hi_digits.parse().map_err(|_| Error {
                message: "invalid number in quantifier".into(),
                offset: parse.last_index,
            })?;
            if hi > REGEXP_QUANTIFIER_MAX {
                return Err(quantifier_too_large_error(parse));
            }
            if hi < qa.quant_min {
                return Err(Error {
                    message: "invalid range quantifier, top must be greater than bottom".into(),
                    offset: parse.last_index,
                });
            }
            qa.quant_max = hi;
            return Ok(());
        } else {
            return Err(Error {
                message: format!("invalid character '{b}', expected '}}'"),
                offset: parse.last_index,
            });
        }
    }
}

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

    #[test]
    fn test_spans_into_surrogate() {
        // True when [start, end] crosses into the surrogate window: start lies
        // below the window AND end reaches it. Cases walk both edges.
        assert!(!spans_into_surrogate(0, 0xD7FF)); // end just below the window
        assert!(spans_into_surrogate(0, SURROGATE_START_CP)); // end at lower edge
        assert!(spans_into_surrogate(0, SURROGATE_END_CP));
        assert!(spans_into_surrogate(0xD7FF, SURROGATE_START_CP));
        // A range that starts at or past the window does not "span into" it.
        assert!(!spans_into_surrogate(SURROGATE_START_CP, 0xFFFF));
        assert!(!spans_into_surrogate(SURROGATE_END_CP, 0xFFFF));
        assert!(!spans_into_surrogate(SURROGATE_END_CP + 1, 0xFFFF));
    }

    #[test]
    fn test_range_pair_overlaps_or_adjacent() {
        // True when the next range overlaps or sits immediately adjacent to the
        // previous one (`next_lo <= prev_hi + 1`); the `+ 1` is what lets a
        // zero-gap adjacency count as mergeable. A gap of one or more is false.
        assert!(range_pair_overlaps_or_adjacent(5, 10)); // overlapping inside
        assert!(range_pair_overlaps_or_adjacent(10, 10)); // overlap at edge
        assert!(range_pair_overlaps_or_adjacent(11, 10)); // adjacent (lo == hi + 1)
        assert!(!range_pair_overlaps_or_adjacent(12, 10)); // gap of one
        assert!(!range_pair_overlaps_or_adjacent(100, 10)); // far gap
        assert!(range_pair_overlaps_or_adjacent(0, 0));
        assert!(range_pair_overlaps_or_adjacent(1, 0));
        assert!(!range_pair_overlaps_or_adjacent(2, 0));
    }

    #[test]
    fn test_past_surrogate_end() {
        // True only strictly beyond the surrogate window; the window end itself
        // is not "past".
        assert!(!past_surrogate_end(SURROGATE_END_CP)); // window end is not past
        assert!(!past_surrogate_end(SURROGATE_END_CP - 1));
        assert!(past_surrogate_end(SURROGATE_END_CP + 1));
        assert!(past_surrogate_end(0x10FFFF));
    }

    #[test]
    fn test_detect_ascii_negated_bytes_single_char() {
        // [^x] -> should detect 'x' as exit byte
        let rr = vec![RunePair { lo: 'x', hi: 'x' }];
        let result = detect_ascii_negated_bytes(&rr);
        assert_eq!(result, Some(vec![b'x']));
    }

    #[test]
    fn test_detect_ascii_negated_bytes_multiple_chars() {
        // [^abc] -> should detect a, b, c as exit bytes
        let rr = vec![
            RunePair { lo: 'a', hi: 'a' },
            RunePair { lo: 'b', hi: 'b' },
            RunePair { lo: 'c', hi: 'c' },
        ];
        let result = detect_ascii_negated_bytes(&rr);
        assert_eq!(result, Some(vec![b'a', b'b', b'c']));
    }

    #[test]
    fn test_detect_ascii_negated_bytes_range() {
        // [^a-c] -> should detect a, b, c as exit bytes
        let rr = vec![RunePair { lo: 'a', hi: 'c' }];
        let result = detect_ascii_negated_bytes(&rr);
        assert_eq!(result, Some(vec![b'a', b'b', b'c']));
    }

    #[test]
    fn test_detect_ascii_negated_bytes_too_many() {
        // [^a-z] -> too many exit bytes (26), should return None
        let rr = vec![RunePair { lo: 'a', hi: 'z' }];
        let result = detect_ascii_negated_bytes(&rr);
        assert_eq!(result, None);
    }

    #[test]
    fn test_detect_ascii_negated_bytes_non_ascii() {
        // [^ü] -> non-ASCII, should return None
        let rr = vec![RunePair { lo: 'ü', hi: 'ü' }];
        let result = detect_ascii_negated_bytes(&rr);
        assert_eq!(result, None);
    }

    #[test]
    fn test_detect_ascii_negated_bytes_mixed() {
        // [^aü] -> contains non-ASCII, should return None
        let rr = vec![RunePair { lo: 'a', hi: 'a' }, RunePair { lo: 'ü', hi: 'ü' }];
        let result = detect_ascii_negated_bytes(&rr);
        assert_eq!(result, None);
    }

    #[test]
    fn test_parse_negated_char_class_stores_ascii_bytes() {
        // Parse [^x]+ and verify ascii_negated_bytes is set
        let tree = parse("[^x]+").unwrap();
        assert_eq!(tree.len(), 1);
        assert_eq!(tree[0].len(), 1);

        let qa = &tree[0][0];
        assert_eq!(qa.ascii_negated_bytes, Some(vec![b'x']));
    }

    #[test]
    fn test_parse_non_negated_class_no_ascii_bytes() {
        // Parse [abc]+ - not negated, so no ascii_negated_bytes
        let tree = parse("[abc]+").unwrap();
        assert_eq!(tree.len(), 1);
        assert_eq!(tree[0].len(), 1);

        let qa = &tree[0][0];
        assert_eq!(qa.ascii_negated_bytes, None);
    }

    #[test]
    fn test_parse_negated_unicode_class_no_ascii_bytes() {
        // Parse [^ü]+ - negated but non-ASCII, so no ascii_negated_bytes
        let tree = parse("[^ü]+").unwrap();
        assert_eq!(tree.len(), 1);
        assert_eq!(tree[0].len(), 1);

        let qa = &tree[0][0];
        assert_eq!(qa.ascii_negated_bytes, None);
    }

    #[test]
    fn test_parse_negated_slash_class() {
        // Parse [^/]+ - common pattern for path parsing
        let tree = parse("[^/]+").unwrap();
        assert_eq!(tree.len(), 1);
        assert_eq!(tree[0].len(), 1);

        let qa = &tree[0][0];
        assert_eq!(qa.ascii_negated_bytes, Some(vec![b'/']));
    }

    #[test]
    fn test_parse_negated_quote_class() {
        // Parse [^"]+ - common pattern for quoted string parsing
        // Note: in I-Regexp, " is a normal character, no escaping needed
        let tree = parse("[^\"]+").unwrap();
        assert_eq!(tree.len(), 1);
        assert_eq!(tree[0].len(), 1);

        let qa = &tree[0][0];
        assert_eq!(qa.ascii_negated_bytes, Some(vec![b'"']));
    }

    // ========================================================================
    // Quantifier type detection
    // ========================================================================

    #[test]
    fn test_quantifier_detection() {
        // Parse "a" (singleton)
        let tree = parse("a").unwrap();
        assert_eq!(tree.len(), 1);
        assert_eq!(tree[0].len(), 1);
        let qa = &tree[0][0];
        assert!(
            qa.is_singleton(),
            "single char without quantifier should be singleton"
        );
        assert!(!qa.is_qm(), "singleton should not be optional");
        assert!(!qa.is_plus(), "singleton should not be plus");
        assert!(!qa.is_star(), "singleton should not be star");

        // Parse "a?" (optional, catches is_qm)
        let tree = parse("a?").unwrap();
        let qa = &tree[0][0];
        assert!(!qa.is_singleton(), "optional should not be singleton");
        assert!(qa.is_qm(), "a? should be optional");
        assert!(!qa.is_plus(), "a? should not be plus");
        assert!(!qa.is_star(), "a? should not be star");

        // Parse "a+" (one or more, catches is_plus)
        let tree = parse("a+").unwrap();
        let qa = &tree[0][0];
        assert!(!qa.is_singleton(), "a+ should not be singleton");
        assert!(!qa.is_qm(), "a+ should not be optional");
        assert!(qa.is_plus(), "a+ should be plus");
        assert!(!qa.is_star(), "a+ should not be star");

        // Parse "a*" (zero or more, catches is_star)
        let tree = parse("a*").unwrap();
        let qa = &tree[0][0];
        assert!(!qa.is_singleton(), "a* should not be singleton");
        assert!(!qa.is_qm(), "a* should not be optional");
        assert!(!qa.is_plus(), "a* should not be plus");
        assert!(qa.is_star(), "a* should be star");
    }

    // ========================================================================
    // Lookaround type detection
    // ========================================================================

    #[test]
    fn test_lookaround_type_classification() {
        // Each variant should correctly report negative/lookbehind classification

        let pos_lookahead = LookaroundType::PositiveLookahead;
        assert!(
            !pos_lookahead.is_negative(),
            "positive lookahead should not be negative"
        );
        assert!(
            !pos_lookahead.is_lookbehind(),
            "lookahead should not be lookbehind"
        );

        let neg_lookahead = LookaroundType::NegativeLookahead;
        assert!(
            neg_lookahead.is_negative(),
            "negative lookahead should be negative"
        );
        assert!(
            !neg_lookahead.is_lookbehind(),
            "lookahead should not be lookbehind"
        );

        let pos_lookbehind = LookaroundType::PositiveLookbehind;
        assert!(
            !pos_lookbehind.is_negative(),
            "positive lookbehind should not be negative"
        );
        assert!(
            pos_lookbehind.is_lookbehind(),
            "positive lookbehind should be lookbehind"
        );

        let neg_lookbehind = LookaroundType::NegativeLookbehind;
        assert!(
            neg_lookbehind.is_negative(),
            "negative lookbehind should be negative"
        );
        assert!(
            neg_lookbehind.is_lookbehind(),
            "negative lookbehind should be lookbehind"
        );
    }

    // ========================================================================
    // Atom dispatch + character-class membership
    // ========================================================================

    #[test]
    fn test_atom_unbalanced_close_paren() {
        // An unmatched ')' is a hard error, not the end of an empty branch.
        assert!(parse(")").unwrap_err().message.contains("unbalanced"));
        assert!(parse("a)").is_err());
    }

    #[test]
    fn test_atom_bare_quantifier_rejected() {
        // A quantifier at atom start has nothing to quantify and is rejected.
        for q in ["?", "+", "*", "{"] {
            assert!(
                parse(q)
                    .unwrap_err()
                    .message
                    .contains("quantifier without atom")
            );
        }
    }

    #[test]
    fn test_char_class_backslash_is_valid_member() {
        // '\' falls in the gap between is_cc_char's member ranges, so it is a
        // valid class member only via the explicit backslash case.
        let tree = parse("[\\]").unwrap();
        assert_eq!(tree[0][0].runes, vec![RunePair { lo: '\\', hi: '\\' }]);
    }

    #[test]
    fn test_inverted_category_in_class() {
        // ~P{...} is the inverted category, ~p{...} the plain one: a control
        // char belongs to ~p{Cc} only, an ordinary letter to ~P{Cc} only.
        let contains = |runes: &RuneRange, c: char| runes.iter().any(|p| p.lo <= c && c <= p.hi);

        let plain = parse("[~p{Cc}]").unwrap()[0][0].runes.clone();
        assert!(contains(&plain, '\u{01}'));
        assert!(!contains(&plain, 'a'));

        let inverted = parse("[~P{Cc}]").unwrap()[0][0].runes.clone();
        assert!(!contains(&inverted, '\u{01}'));
        assert!(contains(&inverted, 'a'));
    }

    // ========================================================================
    // Word boundary (~b/~B) expansion
    // ========================================================================

    #[test]
    fn test_wb_parse_boundary_marker() {
        let tree = parse("~bhello").unwrap();
        assert!(has_word_boundary(&tree));
        let expanded = expand_word_boundaries(&tree).unwrap();
        // ~b at start: first char ('h' = word char) must be word → trivially satisfied
        assert_eq!(expanded.len(), 1);
        assert_eq!(expanded[0].len(), 5);
    }

    #[test]
    fn test_wb_impossible_boundary_empty_tree() {
        // ab~bcd: 'b' and 'c' are both word chars → ~b impossible → empty tree
        let tree = parse("ab~bcd").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert!(
            expanded.is_empty(),
            "impossible boundary should produce empty tree"
        );
    }

    #[test]
    fn test_wb_middle_boundary_expansion() {
        // ab~b cd: 'b' is word, ' ' is non-word → boundary valid
        let tree = parse("ab~b cd").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert_eq!(expanded.len(), 1); // Only word→nonword branch survives
    }

    #[test]
    fn test_wb_dot_star_expansion() {
        // .*~bcat: dot-star before boundary, 'c' (word) after
        let tree = parse(".*~bcat").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // Two branches:
        // 1. .*{0,MAX-1} ~W c a t (last of .* is non-word, c is word)
        // 2. c a t (when .* matches 0 chars, boundary at value start, c must be word)
        assert_eq!(expanded.len(), 2);
    }

    // ========================================================================
    // Lookbehind fixed-length validation
    // ========================================================================

    #[test]
    fn test_lookbehind_same_length_alternation_accepted() {
        // (?<=ab|cd)x — two branches, both length 2 → fixed length, should succeed
        let result = parse("(?<=ab|cd)x");
        assert!(
            result.is_ok(),
            "lookbehind with same-length alternation should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_lookbehind_different_length_alternation_rejected() {
        // (?<=a|cd)x — branches of length 1 and 2 → variable length, should fail
        let result = parse("(?<=a|cd)x");
        assert!(
            result.is_err(),
            "lookbehind with different-length alternation must be rejected"
        );
        assert!(
            result.unwrap_err().message.contains("variable-length"),
            "error should mention variable-length"
        );
    }

    #[test]
    fn test_lookbehind_star_rejected() {
        // (?<=a*)x — star is variable length
        let result = parse("(?<=a*)x");
        assert!(result.is_err(), "lookbehind with star must be rejected");
    }

    #[test]
    fn test_lookbehind_plus_rejected() {
        // (?<=a+)x — plus is variable length
        let result = parse("(?<=a+)x");
        assert!(result.is_err(), "lookbehind with plus must be rejected");
    }

    #[test]
    fn test_lookbehind_char_class_accepted() {
        // (?<=[abc])x — character class, fixed length 1
        let result = parse("(?<=[abc])x");
        assert!(
            result.is_ok(),
            "lookbehind with char class should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_lookbehind_single_branch_group_accepted() {
        // (?<=(?:ab))x — single-branch group of length 2
        let result = parse("(?<=(?:ab))x");
        assert!(
            result.is_ok(),
            "lookbehind with single-branch group should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_lookbehind_multi_branch_group_same_length() {
        // (?<=(?:ab|cd))x — alternation inside group, both length 2
        let result = parse("(?<=(?:ab|cd))x");
        assert!(
            result.is_ok(),
            "lookbehind with same-length alternation group should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_lookbehind_multi_branch_group_different_length() {
        // (?<=(?:a|cd))x — alternation inside group, different lengths
        let result = parse("(?<=(?:a|cd))x");
        assert!(
            result.is_err(),
            "lookbehind with different-length alternation group must be rejected"
        );
    }

    #[test]
    fn test_lookbehind_quantified_group_accepted() {
        // (?<=a{3})x — fixed quantifier {3,3}, total length 3
        let result = parse("(?<=a{3})x");
        assert!(
            result.is_ok(),
            "lookbehind with fixed quantifier should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_lookbehind_range_quantifier_rejected() {
        // (?<=a{2,4})x — variable range quantifier
        let result = parse("(?<=a{2,4})x");
        assert!(
            result.is_err(),
            "lookbehind with range quantifier must be rejected"
        );
    }

    // ========================================================================
    // Word boundary with quantified atoms (constrain_atom_at_boundary)
    // ========================================================================

    #[test]
    fn test_wb_quantified_plus_at_end() {
        // a+~b — plus-quantified word char at boundary end
        // The last char of a+ must be word (trivially true for 'a'),
        // so this splits into [a{0,MAX}, a_constrained]
        let tree = parse("a+~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // Should produce at least one branch (a+ ends with word char,
        // boundary before implicit " which is non-word)
        assert!(
            !expanded.is_empty(),
            "a+~b should have valid expansion since 'a' is a word char"
        );
        // The expansion should have more atoms than the original (split quantifier)
        assert!(
            expanded[0].len() >= 2,
            "quantified atom should split into base + constrained"
        );
    }

    #[test]
    fn test_wb_quantified_star_at_end() {
        // x.*~b — star before boundary at end, triggers SplitOrAbsent path
        // When .* matches 0 chars, boundary falls after 'x' (word → non-word " = valid)
        let tree = parse("x.*~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // Should produce multiple branches:
        // 1. x + .*{adjusted} constrained (dot split)
        // 2. x alone (when .* is absent, 'x' is word, " is non-word → boundary)
        assert!(
            expanded.len() >= 2,
            "x.*~b should produce SplitOrAbsent branches, got {}",
            expanded.len()
        );
    }

    #[test]
    fn test_wb_star_at_start() {
        // ~ba*x — star-quantified 'a' at boundary start, triggers SplitOrAbsent
        // in expand_wb_at_start. When a* matches 0 chars, boundary falls before 'x'.
        let tree = parse("~ba*x").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // Should produce branches for both: a present (constrained) and a absent (x constrained)
        assert!(
            expanded.len() >= 2,
            "~ba*x should produce SplitOrAbsent branches at start, got {}",
            expanded.len()
        );
    }

    #[test]
    fn test_wb_dot_at_end_constrain() {
        // .~b — dot before boundary at end, tests is_dot field in constrained atom
        let tree = parse(".~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // Dot intersected with word chars → should produce a branch with rune-constrained atom
        assert!(
            !expanded.is_empty(),
            ".~b should expand (dot can match word chars)"
        );
        // The expanded atom should NOT be a dot anymore (it's constrained to word chars)
        assert!(
            !expanded[0][0].is_dot,
            "constrained dot should no longer be marked as dot"
        );
    }

    #[test]
    fn test_wb_negated_class_at_boundary() {
        // [^x]+~b — negated class with ascii_negated_bytes at boundary
        let tree = parse("[^x]+~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // [^x] intersected with word chars still has valid members
        assert!(!expanded.is_empty(), "[^x]+~b should have valid expansion");
    }

    #[test]
    fn test_wb_count_limit_exactly_four() {
        // Pattern with exactly 4 word boundaries (the maximum allowed)
        let tree = parse("~ba~b ~Bc~b").unwrap();
        let result = expand_word_boundaries(&tree);
        assert!(
            result.is_ok(),
            "4 word boundaries should be accepted (max is 4)"
        );
    }

    #[test]
    fn test_wb_count_limit_five_rejected() {
        // Pattern with 5 word boundaries (exceeds max of 4)
        let tree = parse("~ba~b ~Bc~bd~b").unwrap();
        let result = expand_word_boundaries(&tree);
        assert!(result.is_err(), "5 word boundaries should be rejected");
        assert!(
            result.unwrap_err().contains("too many word boundaries"),
            "error should mention word boundary limit"
        );
    }

    // ========================================================================
    // branch_fixed_length with quantifiers
    // ========================================================================

    #[test]
    fn test_lookbehind_quantifier_vs_individual_same_length() {
        // (?<=a{2}|bc)x — branch 1 uses quantifier {2}, branch 2 spells out two chars
        // Both have character-count length 2. Must be accepted as equal-length.
        let result = parse("(?<=a{2}|bc)x");
        assert!(
            result.is_ok(),
            "a{{2}} and bc are both length 2: {:?}",
            result.err()
        );
    }

    // ========================================================================
    // Word boundary with SplitOrAbsent fallback (expand_wb_at_end)
    // ========================================================================

    #[test]
    fn test_wb_at_end_with_star_after_literal() {
        // xa*~b — 'x' is a word char, 'a*' can match zero chars (SplitOrAbsent).
        // When a* is absent, boundary falls after 'x' (word → non-word " = valid).
        // When a* is present, its last char must be word (trivially true for 'a').
        // The SplitOrAbsent path constrains the preceding atom ('x') as fallback.
        let tree = parse("xa*~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // Must produce branches for both the present case (a* constrained) and
        // the absent case (x constrained as fallback)
        assert!(
            expanded.len() >= 2,
            "xa*~b needs SplitOrAbsent fallback branches, got {}",
            expanded.len()
        );
    }

    // ========================================================================
    // ~B alone (non-boundary between two implicit " delimiters)
    // ========================================================================

    #[test]
    fn test_non_boundary_alone_always_matches() {
        // ~B alone: both sides are `"` (non-word), so they're in the same class → always a match
        let tree = parse("~B").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert!(
            !expanded.is_empty(),
            "~B alone should produce an empty-branch alternative (always matches)"
        );
    }

    #[test]
    fn test_boundary_alone_never_matches() {
        // ~b alone: both sides are `"` (non-word), same class → boundary never holds
        let tree = parse("~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert!(
            expanded.is_empty(),
            "~b alone should produce no alternatives (impossible boundary)"
        );
    }

    #[test]
    fn test_wb_single_star_at_end() {
        // .*~b — single zero-or-more atom before boundary at end.
        // Prefix is [.*] (last_idx = 0). SplitOrAbsent path must NOT
        // try to access prefix[-1]; the fallback is skipped when there is
        // no preceding atom.
        let tree = parse(".*~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        // .* can match word chars, so at least one branch constrains the dot
        assert!(
            !expanded.is_empty(),
            ".*~b should produce at least one branch"
        );
    }

    #[test]
    fn test_read_category_two_letter_initials() {
        // Two-letter Unicode categories are validated by their initial letter.
        // Every supported initial (M, P, Z, S, C) must accept a valid detail.
        for cat in ["~p{Mn}", "~p{Pc}", "~p{Zs}", "~p{Sm}", "~p{Cc}"] {
            assert!(
                parse(cat).is_ok(),
                "category {cat} must parse: {:?}",
                parse(cat).err()
            );
        }
    }

    #[test]
    fn test_class_subtraction_at_max_depth() {
        // Eight nested `-[` levels sits exactly at MAX_CLASS_SUBTRACTION_DEPTH,
        // which is still allowed; only deeper nesting is rejected.
        let re = "[0-[1-[2-[3-[4-[5-[6-[7-[8]]]]]]]]]";
        assert!(
            parse(re).is_ok(),
            "depth-8 subtraction: {:?}",
            parse(re).err()
        );
    }

    #[test]
    fn test_property_escape_inside_char_class() {
        // A `~p{...}` / `~P{...}` property escape is valid inside a character
        // class, not just at top level.
        assert!(parse("[~p{L}]").is_ok(), "{:?}", parse("[~p{L}]").err());
        assert!(parse("[~P{N}]").is_ok(), "{:?}", parse("[~P{N}]").err());
    }

    #[test]
    fn test_equal_endpoint_char_range() {
        // A range whose endpoints are equal (`[a-a]`) is a valid single-char
        // class; only lo strictly greater than hi is an error.
        assert!(parse("[a-a]").is_ok(), "{:?}", parse("[a-a]").err());
    }

    #[test]
    fn test_standalone_close_bracket_error_message() {
        // A standalone `]` reports its own specific diagnostic rather than the
        // generic "stuck" fallthrough.
        let err = parse("]").expect_err("standalone ] must error");
        assert!(
            err.message.contains("invalid ']'"),
            "expected \"invalid ']'\", got {:?}",
            err.message
        );
    }

    #[test]
    fn test_invert_rune_range_skips_surrogate_window() {
        // Inverting [U+0000-U+D7FF] yields the high plane [U+E000..=MAX]: the
        // complement jumps over the surrogate window rather than including it.
        let inv = invert_rune_range(vec![RunePair {
            lo: '\u{0}',
            hi: '\u{D7FF}',
        }]);
        assert!(!inv.is_empty(), "inverted range must not be empty");
        assert!(
            inv.iter()
                .any(|p| (p.lo as u32) >= 0xE000 && p.hi == RUNE_MAX),
            "inverted range must cover the post-surrogate high plane, got {inv:?}"
        );
    }

    #[test]
    fn test_detect_ascii_negated_bytes_boundary_and_empty() {
        // A pair straddling the ASCII boundary (lo below 128, hi at/above 128)
        // is not pure-ASCII, so no exit bytes are produced.
        assert_eq!(
            detect_ascii_negated_bytes(&vec![RunePair {
                lo: '\u{7F}',
                hi: '\u{80}',
            }]),
            None
        );
        // No ranges means nothing to negate.
        assert_eq!(detect_ascii_negated_bytes(&Vec::new()), None);
    }

    #[test]
    fn test_wb_at_start_single_optional_suffix() {
        // `~b` at value start followed by a single optional atom (`a*`) is the
        // minimal SplitOrAbsent case: the suffix has exactly one element.
        let tree = parse("~ba*").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert!(!expanded.is_empty(), "~ba* should expand");
    }

    #[test]
    fn test_wb_at_end_bounded_quantifier_remainder() {
        // `a{2,3}~b` splits one boundary char off the end; the remainder atom
        // keeps a quant_max of one less than the original (3 - 1 = 2).
        let tree = parse("a{2,3}~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert_eq!(expanded.len(), 1, "got {expanded:?}");
        assert_eq!(expanded[0].len(), 2, "got {expanded:?}");
        assert_eq!(
            expanded[0][0].quant_max, 2,
            "remainder atom quant_max must be original_max-1=2, got {}",
            expanded[0][0].quant_max
        );
    }

    #[test]
    fn test_wb_at_end_split_fallback_uses_previous_atom() {
        // In ` a*~b` the prefix is [' ', a*] and a* is SplitOrAbsent. When a*
        // is absent the boundary lands after the space, a non-word char, so
        // constraining the atom before a* adds no fallback branch — the
        // fallback considers the previous atom, not a* itself.
        let tree = parse(" a*~b").unwrap();
        let expanded = expand_word_boundaries(&tree).unwrap();
        assert_eq!(
            expanded.len(),
            1,
            "space is non-word so the SplitOrAbsent fallback adds nothing; got {expanded:?}"
        );
    }

    #[test]
    fn test_constrain_atom_base_keeps_cache_key_and_ascii_bytes() {
        // Constraining a quantified atom at a boundary splits off one occurrence
        // and keeps the remaining count as a `base` atom. That base must carry
        // over the original atom's cache_key and ascii_negated_bytes rather than
        // reset them to their defaults.
        let atom = QuantifiedAtom {
            runes: vec![RunePair { lo: 'a', hi: 'z' }],
            quant_min: 2,
            quant_max: 3,
            cache_key: Some("wb_test".to_string()),
            ascii_negated_bytes: Some(vec![b'q']),
            ..Default::default()
        };
        let constrained = constrain_atom_at_boundary(&atom, &word_char_runes(), true)
            .expect("a-z intersects the word class, so the atom constrains");
        let ConstrainedAtom::Split(base, _single) = constrained else {
            panic!("a non-zero-min {{2,3}} quantifier must produce a Split");
        };
        assert_eq!(base.cache_key.as_deref(), Some("wb_test"));
        assert_eq!(base.ascii_negated_bytes, Some(vec![b'q']));
    }
}