ferrotorch-grammar 0.6.2

Constrained-decoding grammar processors (JSON-schema → token-allow masks) for ferrotorch
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
//! Character-level state machine over a JSON value matching a [`Schema`].
//!
//! [`JsonGrammar`] is the workhorse of the constrained decoder. At every
//! point during generation it knows:
//!
//! - Which characters can come next (`valid_next_chars`).
//! - Whether the value emitted so far is complete (`is_complete`).
//!
//! Tokens in a real LLM vocabulary span multiple characters; the
//! [`super::json_schema::JsonSchemaProcessor`] wrapper builds on top of this
//! by simulating each token's chars in sequence and including the token in
//! the allow-mask only if every char is accepted.
//!
//! ## Features
//!
//! - **String escapes** (REQ-5): JSON string escape sequences are
//!   accepted: `\"`, `\\`, `\/`, `\b`, `\f`, `\n`, `\r`, `\t`, and
//!   `\uXXXX` four-hex-digit Unicode escapes. The grammar tracks the
//!   escape sub-phase via `Phase::StringEscape { hex_digits }`.
//! - **Number exponents** (REQ-6): JSON number exponents are accepted:
//!   `1e5`, `-3.14E+2`, `0.5e-10`. Tracked by exponent flags on
//!   `Phase::NumberDigits`.
//! - **Whitespace-permissive mode** (REQ-7): opt-in via
//!   [`JsonGrammar::with_whitespace_permissive`]. When enabled, the
//!   structural boundaries (`{`, `:`, `,`, `}`, `[`, `]`, value start,
//!   object after-value, array after-value) accept arbitrary whitespace
//!   (`' '`, `'\t'`, `'\n'`, `'\r'`).
//!
//! ## REQ status (per `.design/ferrotorch-grammar/state.md`)
//!
//! | REQ | Status | Evidence |
//! |---|---|---|
//! | REQ-1 | SHIPPED | impl: `pub struct JsonGrammar` with private `frames: Vec<Frame>` + `done: bool`, `pub fn new(schema)`, `is_complete` in `state.rs`; non-test consumer: `JsonSchemaProcessor::new` in `json_schema.rs` calls `JsonGrammar::new(schema)` and stores it as `grammar: JsonGrammar`. |
//! | REQ-2 | SHIPPED | impl: `pub fn JsonGrammar::valid_next_chars` walking the top frame via `valid_next_chars_for` + parent terminators in `state.rs`; non-test consumer: `JsonSchemaProcessor::compute_mask` in `json_schema.rs` indirectly drives it through `step_char`'s pre-validation; `gpu_dispatch::compute_mask_gpu` calls `grammar.top_frame_parent_terminators()` in `gpu_dispatch.rs`. |
//! | REQ-3 | SHIPPED | impl: `pub fn JsonGrammar::step_char` validates against `valid_next_chars` then dispatches to private `apply_step` in `state.rs`; `pub fn step_str` is the convenience wrapper; non-test consumer: `JsonSchemaProcessor::compute_mask` calls `probe.step_char(c)` in a per-token loop (`json_schema.rs`) and `JsonSchemaProcessor::step_token` calls it per token-char to commit. |
//! | REQ-4 | SHIPPED | impl: `apply_step` covers every `(Schema, Phase)` pair in `state.rs`; `valid_next_chars_for` mirrors with the legal-chars side; non-test consumer: every production `compute_mask` / `step_token` call in `json_schema.rs` walks these arms. |
//! | REQ-5 | SHIPPED | impl: `Phase::StringEscape { hex_digits }` sub-phase in `state.rs` + `apply_step` arm handling `\\` and the 9 JSON escapes (`"`, `\\`, `/`, `b`, `f`, `n`, `r`, `t`, `uXXXX`); non-test consumer: every production `compute_mask` / `step_token` call walks the same body-char branch. |
//! | REQ-6 | SHIPPED | impl: `had_exponent_marker` + `had_exponent_sign` + `had_exponent_digit` flags on `Phase::NumberDigits` in `state.rs` + `Schema::Number` `valid_next_chars_for` emits `'e'`/`'E'` after digits; non-test consumer: every production `compute_mask` / `step_token` call covering numbers. |
//! | REQ-7 | SHIPPED | impl: `pub fn JsonGrammar::with_whitespace_permissive(bool)` + `whitespace_permissive: bool` field in `state.rs`; structural-boundary `valid_next_chars_for` arms inject `[' ', '\t', '\n', '\r']` when enabled, `apply_step` silently consumes them; non-test consumer: every production `compute_mask` / `step_token` walks the flag. |
//! | REQ-8 | SHIPPED | impl: 8 `pub enum *EmissionStage` types + 14 `pub fn JsonGrammar::*_emission_stage{,_top}` accessors + `pub fn top_frame_parent_terminators` in `state.rs`; non-test consumer: `compute_mask_gpu` in `gpu_dispatch.rs` calls `grammar.boolean_emission_stage_top()`, `null_emission_stage_top()`, `integer_emission_stage_top()`, `number_emission_stage_top()`, `string_emission_stage_top()`, `string_enum_emission_stage_top()`, `nullable_emission_stage()`, `object_key_emission_stage()`, `top_frame_parent_terminators()` in production. |

use std::collections::BTreeSet;

use super::schema::Schema;

/// Reasons the grammar may reject a character.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum StepError {
    /// `c` is not a valid next character at the current state.
    #[error("unexpected {got:?} at this point; valid next chars: {expected:?}")]
    UnexpectedChar {
        /// The character that was rejected.
        got: char,
        /// Characters that would have been valid at this state.
        expected: Vec<char>,
    },
    /// The grammar is already complete; further characters are not allowed.
    #[error("grammar is already complete")]
    AlreadyComplete,
    /// The input would advance into an unsupported branch (e.g. a string
    /// escape sequence).
    #[error("unsupported by grammar subset: {0}")]
    Unsupported(&'static str),
}

/// Per-frame phase: where we are inside the current schema value.
#[derive(Debug, Clone)]
enum Phase {
    /// Haven't emitted anything yet for this value.
    Start,
    /// Inside the literal `null`, `true`, or `false` — `remaining` holds the
    /// chars still to emit.
    Literal { remaining: &'static str },
    /// Inside a string value (between the opening `"` and closing `"`).
    StringChars {
        partial: String,
        allowed: Option<Vec<String>>,
    },
    /// Inside a JSON string escape sequence. Set after emitting `\\`
    /// in `Phase::StringChars`. `hex_digits` accumulates the four hex
    /// digits of a `\\uXXXX` escape; for short escapes (`\\"`, `\\n`,
    /// etc.) it stays empty and the phase resolves on the next char.
    StringEscape {
        partial: String,
        allowed: Option<Vec<String>>,
        hex_digits: u8,
    },
    /// Inside a number value.
    ///
    /// - `had_sign`: a leading `-` was emitted.
    /// - `had_digits`: at least one digit has been emitted.
    /// - `had_decimal`: a `.` has been emitted (no more `.` allowed).
    /// - `had_fractional_digit`: at least one digit was emitted *after* `.`.
    ///   Required by JSON: `1.` is invalid; `1.0` is valid. While
    ///   `had_decimal` is true and `had_fractional_digit` is false, only
    ///   digits are valid (no terminator).
    /// - `is_zero_only`: the first emitted digit was `0` *and* nothing else
    ///   has been emitted yet (no more leading zeros: JSON forbids `01`,
    ///   `-007`, etc., but `0`, `0.5`, `-0.25` are all fine).
    NumberDigits {
        had_sign: bool,
        had_digits: bool,
        had_decimal: bool,
        had_fractional_digit: bool,
        is_zero_only: bool,
        /// `e` / `E` has been emitted; exponent section is active.
        had_exponent_marker: bool,
        /// `+` / `-` has been emitted after the exponent marker.
        had_exponent_sign: bool,
        /// At least one digit has been emitted in the exponent section.
        had_exponent_digit: bool,
    },
    /// Inside an object: just emitted `{`. Need `"` to start a key or `}`
    /// to close (if all required keys are satisfied — for an empty
    /// `properties` set this means as soon as we open).
    ObjectFreshOpen { keys_seen: BTreeSet<String> },
    /// Inside an object: just emitted `,`. Must emit `"` to start the next
    /// key — `}` is forbidden.
    ObjectExpectKey { keys_seen: BTreeSet<String> },
    /// Inside an object: emitting key characters between `"` and `"`.
    /// `partial` is the key chars seen so far. `candidates` is the set of
    /// not-yet-seen property names that are still consistent with `partial`.
    ObjectKey {
        partial: String,
        keys_seen: BTreeSet<String>,
        candidates: Vec<String>,
    },
    /// Just emitted the closing `"` of an object key. Need `:` next.
    ObjectColon {
        current_key: String,
        keys_seen: BTreeSet<String>,
    },
    /// Just finished a property value. Need `,` (more keys) or `}` (close,
    /// only if all required keys have been seen).
    ObjectAfterValue { keys_seen: BTreeSet<String> },
    /// Inside an array: just emitted `[`. Need an element value or `]`.
    ArrayFreshOpen,
    /// Inside an array: just finished an element. Need `,` or `]`.
    ArrayAfterValue,
}

#[derive(Debug, Clone)]
struct Frame {
    schema: Schema,
    phase: Phase,
}

/// State machine over the partial JSON emission.
#[derive(Debug, Clone)]
pub struct JsonGrammar {
    frames: Vec<Frame>,
    done: bool,
    /// REQ-7: when `true`, structural boundaries accept whitespace
    /// (`' '`, `'\t'`, `'\n'`, `'\r'`).
    whitespace_permissive: bool,
}

impl JsonGrammar {
    /// Build a fresh grammar that will produce one value of the given schema.
    pub fn new(schema: Schema) -> Self {
        let frame = Frame {
            schema,
            phase: Phase::Start,
        };
        Self {
            frames: vec![frame],
            done: false,
            whitespace_permissive: false,
        }
    }

    /// Enable whitespace-permissive mode. When enabled, structural
    /// boundaries (`{`, `}`, `:`, `,`, `[`, `]`, value-start, object
    /// after-value, array after-value) accept `' '`, `'\t'`, `'\n'`,
    /// `'\r'` characters in addition to the strict-JSON char set. The
    /// whitespace is silently consumed (does not advance the frame).
    pub fn with_whitespace_permissive(mut self, on: bool) -> Self {
        self.whitespace_permissive = on;
        self
    }

    /// Returns `true` if whitespace-permissive mode is enabled.
    pub fn is_whitespace_permissive(&self) -> bool {
        self.whitespace_permissive
    }

    /// Has the top-level value been fully emitted?
    pub fn is_complete(&self) -> bool {
        self.done
    }

    /// If this grammar is a single-frame `Schema::Boolean`, report which
    /// stage of literal emission we're at. Returns `None` for every other
    /// schema or for nested / multi-frame states.
    ///
    /// Used by the GPU constrained-decoding bridge in
    /// [`super::gpu_dispatch`] (`--features cuda`) to decide whether the
    /// current grammar state is DFA-compilable. Stage-2 GPU support
    /// covers exactly Boolean; everything else falls through to the
    /// existing CPU `compute_mask` loop.
    pub fn boolean_emission_stage(&self) -> Option<BooleanEmissionStage> {
        if self.done {
            return None;
        }
        if self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        if !matches!(frame.schema, Schema::Boolean) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(BooleanEmissionStage::Start),
            Phase::Literal { remaining } => {
                // Disambiguate which literal we're inside. The Phase carries
                // only the remaining suffix; we look up which of "true" /
                // "false" has it as a suffix. The boolean grammar uses
                // `&'static str` slices into the literal source strings, so
                // suffix matching is unambiguous.
                if "true".ends_with(remaining) && remaining.len() < "true".len() {
                    Some(BooleanEmissionStage::PartialTrue { remaining })
                } else if "false".ends_with(remaining) && remaining.len() < "false".len() {
                    Some(BooleanEmissionStage::PartialFalse { remaining })
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

/// Stage of `Schema::Boolean` emission. Surfaces just enough of the
/// internal `Phase` enum for the GPU dispatcher to compile a DFA without
/// exposing the rest of the grammar's state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BooleanEmissionStage {
    /// Nothing emitted yet. The DFA must accept any prefix of either
    /// `"true"` or `"false"`.
    Start,
    /// We've already emitted some prefix of `"true"`. `remaining` is the
    /// suffix still to emit (always non-empty; complete is unreachable
    /// here since the grammar reports `done` for that case).
    PartialTrue {
        /// Characters of `"true"` not yet emitted.
        remaining: &'static str,
    },
    /// Same as `PartialTrue` but for `"false"`.
    PartialFalse {
        /// Characters of `"false"` not yet emitted.
        remaining: &'static str,
    },
}

/// Stage of `Schema::Null` emission. Mirrors `BooleanEmissionStage` for
/// the "null" literal.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NullEmissionStage {
    /// Nothing emitted yet — DFA expects 'n' first.
    Start,
    /// Some prefix of `"null"` has been emitted. `remaining` is the
    /// suffix still to match.
    Partial {
        /// Characters of `"null"` not yet emitted.
        remaining: &'static str,
    },
}

/// Stage of single-frame `Schema::Integer` emission. The grammar's
/// `Phase::NumberDigits` carries five booleans; for top-level integers
/// `had_decimal` and `had_fractional_digit` are always false, so the
/// reachable space collapses to four cases.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IntegerEmissionStage {
    /// `Phase::Start` — DFA expects `'-'` or `'0'..='9'`.
    Start,
    /// After `'-'`, no digits yet — DFA expects `'0'..='9'`.
    AfterSign,
    /// First digit was `'0'`, no more chars valid (JSON forbids `01`).
    /// The DFA's only outgoing transition is to REJECT.
    AfterZero,
    /// At least one non-zero digit emitted. More digits are valid.
    AfterDigits,
}

/// Stage of single-frame `Schema::Number` emission. Like
/// `IntegerEmissionStage` but extended with the decimal / fractional
/// and exponent dimensions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NumberEmissionStage {
    /// `Phase::Start` — DFA expects `'-'` or `'0'..='9'`.
    Start,
    /// After `'-'`, no digits yet — DFA expects `'0'..='9'`.
    AfterSign,
    /// First digit was `'0'`, no decimal yet. `'.'` or `'e'`/`'E'`.
    AfterZeroNoDecimal,
    /// Non-zero integer part, no decimal yet. `'0'..='9'`, `'.'`, `'e'`/`'E'`.
    AfterDigitsNoDecimal,
    /// `'.'` emitted, no fractional digit yet. Only `'0'..='9'`.
    AfterDecimalNoFrac,
    /// At least one fractional digit. `'0'..='9'` or `'e'`/`'E'`.
    AfterFractionalDigits,
    /// `'e'` / `'E'` just emitted, no exponent digit / sign yet. Only
    /// `'+'`, `'-'`, or `'0'..='9'`.
    AfterExponentMarker,
    /// `'+'` / `'-'` emitted after exponent marker, no digit yet. Only digits.
    AfterExponentSign,
    /// At least one exponent digit emitted. More digits valid; number complete.
    AfterExponentDigits,
}

/// Map a `Phase::NumberDigits` flag tuple to the corresponding
/// `NumberEmissionStage`. Centralised so the single-frame and
/// multi-frame number-stage accessors stay in lock-step.
#[allow(clippy::too_many_arguments)]
fn number_phase_to_stage(
    had_sign: bool,
    had_digits: bool,
    had_decimal: bool,
    had_fractional_digit: bool,
    is_zero_only: bool,
    had_exponent_marker: bool,
    had_exponent_sign: bool,
    had_exponent_digit: bool,
) -> Option<NumberEmissionStage> {
    if had_exponent_marker {
        if had_exponent_digit {
            return Some(NumberEmissionStage::AfterExponentDigits);
        }
        if had_exponent_sign {
            return Some(NumberEmissionStage::AfterExponentSign);
        }
        return Some(NumberEmissionStage::AfterExponentMarker);
    }
    match (
        had_sign,
        had_digits,
        had_decimal,
        had_fractional_digit,
        is_zero_only,
    ) {
        (true, false, false, false, false) => Some(NumberEmissionStage::AfterSign),
        (_, true, false, false, true) => Some(NumberEmissionStage::AfterZeroNoDecimal),
        (_, true, false, false, false) => Some(NumberEmissionStage::AfterDigitsNoDecimal),
        (_, true, true, false, _) => Some(NumberEmissionStage::AfterDecimalNoFrac),
        (_, true, true, true, _) => Some(NumberEmissionStage::AfterFractionalDigits),
        _ => None,
    }
}

/// Stage of single-frame `Schema::String` emission (non-enum strings).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StringEmissionStage {
    /// `Phase::Start` — DFA expects opening `'"'`.
    Start,
    /// Inside the string body. Any printable ASCII except `'"'` and
    /// `'\\'` (escapes are intentionally unsupported per the existing
    /// grammar) plus the closing `'"'`.
    InBody,
}

/// Stage of single-frame `Schema::StringEnum` emission. `partial` is a
/// borrow into the grammar's current `Phase::StringChars` payload so no
/// allocation happens at dispatch time.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StringEnumEmissionStage<'a> {
    /// `Phase::Start` — DFA expects opening `'"'`.
    Start,
    /// Inside the quoted body, having emitted some prefix of one of the
    /// allowed values. `partial` is the chars between the opening `'"'`
    /// and the cursor; an empty `partial` is the just-opened state.
    InBody {
        /// Characters emitted between the opening `'"'` and the cursor.
        partial: &'a str,
    },
}

/// Stage of single-frame `Schema::Nullable(_)` emission. The grammar
/// commits to either the null branch or `inner` after the very first
/// emitted character, so we only need to surface the `Phase::Start`
/// case — every other phase is handled by the inner schema's own
/// accessor (or by `null_emission_stage` for the null branch).
///
/// `PartialEq` only — `Schema` itself derives `PartialEq` but not `Eq`
/// (consistent with the rest of the type), so neither does this.
#[derive(Debug, Clone, PartialEq)]
pub enum NullableEmissionStage<'a> {
    /// `Phase::Start` with a `Schema::Nullable(inner)`. The DFA must
    /// accept any prefix of `"null"` plus any prefix accepted by
    /// `inner`'s start state.
    Start {
        /// The non-null branch's schema.
        inner: &'a Schema,
    },
}

impl JsonGrammar {
    /// If this grammar is a single-frame `Schema::Null`, report which
    /// stage of literal emission we're at.
    pub fn null_emission_stage(&self) -> Option<NullEmissionStage> {
        if self.done || self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        if !matches!(frame.schema, Schema::Null) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(NullEmissionStage::Start),
            Phase::Literal { remaining }
                if "null".ends_with(remaining) && !remaining.is_empty() =>
            {
                Some(NullEmissionStage::Partial { remaining })
            }
            _ => None,
        }
    }

    /// If this grammar is a single-frame `Schema::Integer`, report the
    /// emission stage. Stage-3 GPU dispatch handles the four reachable
    /// cases for top-level integers; nested integers fall through to
    /// CPU because their value-end terminator depends on the parent.
    pub fn integer_emission_stage(&self) -> Option<IntegerEmissionStage> {
        if self.done || self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        if !matches!(frame.schema, Schema::Integer) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(IntegerEmissionStage::Start),
            Phase::NumberDigits {
                had_sign,
                had_digits,
                is_zero_only,
                had_decimal,
                had_fractional_digit,
                ..
            } => {
                // Integer mode never sets decimal or fractional flags;
                // assert that to catch grammar drift early.
                debug_assert!(!*had_decimal && !*had_fractional_digit);
                if !*had_digits {
                    if *had_sign {
                        Some(IntegerEmissionStage::AfterSign)
                    } else {
                        // had_sign=F && had_digits=F is Phase::Start; should be unreachable.
                        None
                    }
                } else if *is_zero_only {
                    Some(IntegerEmissionStage::AfterZero)
                } else {
                    Some(IntegerEmissionStage::AfterDigits)
                }
            }
            _ => None,
        }
    }

    /// If this grammar is a single-frame `Schema::Number`, report the
    /// emission stage. Same nested-vs-top-level caveat as
    /// `integer_emission_stage`.
    pub fn number_emission_stage(&self) -> Option<NumberEmissionStage> {
        if self.done || self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        if !matches!(frame.schema, Schema::Number) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(NumberEmissionStage::Start),
            Phase::NumberDigits {
                had_sign,
                had_digits,
                had_decimal,
                had_fractional_digit,
                is_zero_only,
                had_exponent_marker,
                had_exponent_sign,
                had_exponent_digit,
            } => number_phase_to_stage(
                *had_sign,
                *had_digits,
                *had_decimal,
                *had_fractional_digit,
                *is_zero_only,
                *had_exponent_marker,
                *had_exponent_sign,
                *had_exponent_digit,
            ),
            _ => None,
        }
    }

    /// If this grammar is a single-frame `Schema::String` (non-enum),
    /// report the emission stage.
    pub fn string_emission_stage(&self) -> Option<StringEmissionStage> {
        if self.done || self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        if !matches!(frame.schema, Schema::String) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(StringEmissionStage::Start),
            Phase::StringChars { allowed: None, .. } => Some(StringEmissionStage::InBody),
            _ => None,
        }
    }

    /// If this grammar is a single-frame `Schema::StringEnum`, report
    /// the emission stage *and* the allowed value list. The list is
    /// borrowed from the schema, so callers don't pay an allocation.
    pub fn string_enum_emission_stage(&self) -> Option<(StringEnumEmissionStage<'_>, &[String])> {
        if self.done || self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        let values: &[String] = match &frame.schema {
            Schema::StringEnum(v) => v.as_slice(),
            _ => return None,
        };
        let stage = match &frame.phase {
            Phase::Start => StringEnumEmissionStage::Start,
            Phase::StringChars {
                partial,
                allowed: Some(_),
            } => StringEnumEmissionStage::InBody {
                partial: partial.as_str(),
            },
            _ => return None,
        };
        Some((stage, values))
    }

    /// If this grammar is a single-frame `Schema::Nullable(_)` at
    /// `Phase::Start`, surface the inner schema so the GPU dispatcher
    /// can build a merged DFA. After the first character is emitted
    /// the grammar commits to either the null branch or the inner
    /// schema, and subsequent compute_mask calls hit those accessors
    /// directly — so only `Phase::Start` is in scope here.
    pub fn nullable_emission_stage(&self) -> Option<NullableEmissionStage<'_>> {
        if self.done || self.frames.len() != 1 {
            return None;
        }
        let frame = &self.frames[0];
        match (&frame.schema, &frame.phase) {
            (Schema::Nullable(inner), Phase::Start) => Some(NullableEmissionStage::Start { inner }),
            _ => None,
        }
    }

    /// Compute the chars that legally terminate the top-of-stack value
    /// frame, given the current parent (one level up). Returns an empty
    /// vector when the top frame is the only frame (single-frame
    /// dispatch path). Wraps the private `parent_terminators` so the
    /// GPU dispatch module can use it without inheriting the rest of
    /// `Phase`'s API surface.
    pub fn top_frame_parent_terminators(&self) -> Vec<char> {
        if self.done || self.frames.is_empty() {
            return Vec::new();
        }
        let parent = if self.frames.len() > 1 {
            Some(&self.frames[self.frames.len() - 2])
        } else {
            None
        };
        parent_terminators(parent)
    }

    /// If the *top* frame of this grammar is `Schema::Integer`, report
    /// the emission stage. Unlike [`Self::integer_emission_stage`] this
    /// version permits multi-frame grammars (Integer nested inside an
    /// Object property value or Array element). Pair the result with
    /// [`Self::top_frame_parent_terminators`] to feed the GPU compiler.
    pub fn integer_emission_stage_top(&self) -> Option<IntegerEmissionStage> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        if !matches!(frame.schema, Schema::Integer) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(IntegerEmissionStage::Start),
            Phase::NumberDigits {
                had_sign,
                had_digits,
                is_zero_only,
                had_decimal,
                had_fractional_digit,
                ..
            } => {
                debug_assert!(!*had_decimal && !*had_fractional_digit);
                if !*had_digits {
                    if *had_sign {
                        Some(IntegerEmissionStage::AfterSign)
                    } else {
                        None
                    }
                } else if *is_zero_only {
                    Some(IntegerEmissionStage::AfterZero)
                } else {
                    Some(IntegerEmissionStage::AfterDigits)
                }
            }
            _ => None,
        }
    }

    /// Multi-frame variant of [`Self::number_emission_stage`].
    pub fn number_emission_stage_top(&self) -> Option<NumberEmissionStage> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        if !matches!(frame.schema, Schema::Number) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(NumberEmissionStage::Start),
            Phase::NumberDigits {
                had_sign,
                had_digits,
                had_decimal,
                had_fractional_digit,
                is_zero_only,
                had_exponent_marker,
                had_exponent_sign,
                had_exponent_digit,
            } => number_phase_to_stage(
                *had_sign,
                *had_digits,
                *had_decimal,
                *had_fractional_digit,
                *is_zero_only,
                *had_exponent_marker,
                *had_exponent_sign,
                *had_exponent_digit,
            ),
            _ => None,
        }
    }

    /// Multi-frame variant of [`Self::string_emission_stage`].
    pub fn string_emission_stage_top(&self) -> Option<StringEmissionStage> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        if !matches!(frame.schema, Schema::String) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(StringEmissionStage::Start),
            Phase::StringChars { allowed: None, .. } => Some(StringEmissionStage::InBody),
            _ => None,
        }
    }

    /// Multi-frame variant of [`Self::boolean_emission_stage`].
    pub fn boolean_emission_stage_top(&self) -> Option<BooleanEmissionStage> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        if !matches!(frame.schema, Schema::Boolean) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(BooleanEmissionStage::Start),
            Phase::Literal { remaining }
                if "true".ends_with(remaining) && remaining.len() < "true".len() =>
            {
                Some(BooleanEmissionStage::PartialTrue { remaining })
            }
            Phase::Literal { remaining }
                if "false".ends_with(remaining) && remaining.len() < "false".len() =>
            {
                Some(BooleanEmissionStage::PartialFalse { remaining })
            }
            _ => None,
        }
    }

    /// Multi-frame variant of [`Self::null_emission_stage`].
    pub fn null_emission_stage_top(&self) -> Option<NullEmissionStage> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        if !matches!(frame.schema, Schema::Null) {
            return None;
        }
        match &frame.phase {
            Phase::Start => Some(NullEmissionStage::Start),
            Phase::Literal { remaining }
                if "null".ends_with(remaining) && !remaining.is_empty() =>
            {
                Some(NullEmissionStage::Partial { remaining })
            }
            _ => None,
        }
    }

    /// Multi-frame variant of [`Self::string_enum_emission_stage`].
    pub fn string_enum_emission_stage_top(
        &self,
    ) -> Option<(StringEnumEmissionStage<'_>, &[String])> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        let values: &[String] = match &frame.schema {
            Schema::StringEnum(v) => v.as_slice(),
            _ => return None,
        };
        let stage = match &frame.phase {
            Phase::Start => StringEnumEmissionStage::Start,
            Phase::StringChars {
                partial,
                allowed: Some(_),
            } => StringEnumEmissionStage::InBody {
                partial: partial.as_str(),
            },
            _ => return None,
        };
        Some((stage, values))
    }

    /// REQ-7 partial: returns `true` iff the top frame is
    /// `Schema::Object { .. }` at `Phase::Start` (i.e. the next valid
    /// char is `'{'`). Used by the GPU dispatcher's REQ-7 partial DFA.
    pub fn is_object_at_start_top(&self) -> bool {
        if self.done {
            return false;
        }
        let Some(frame) = self.frames.last() else {
            return false;
        };
        matches!(frame.schema, Schema::Object { .. }) && matches!(frame.phase, Phase::Start)
    }

    /// REQ-7 partial: returns `true` iff the top frame is
    /// `Schema::Array { .. }` at `Phase::Start` (i.e. the next valid
    /// char is `'['`).
    pub fn is_array_at_start_top(&self) -> bool {
        if self.done {
            return false;
        }
        let Some(frame) = self.frames.last() else {
            return false;
        };
        matches!(frame.schema, Schema::Array { .. }) && matches!(frame.phase, Phase::Start)
    }

    /// If the top frame is in `Phase::ObjectKey`, surface the partial
    /// chars and the still-unseen-property candidates list. The
    /// candidates borrow into the grammar's own state — no clone.
    pub fn object_key_emission_stage(&self) -> Option<ObjectKeyEmissionStage<'_>> {
        if self.done {
            return None;
        }
        let frame = self.frames.last()?;
        if !matches!(frame.schema, Schema::Object { .. }) {
            return None;
        }
        match &frame.phase {
            Phase::ObjectKey {
                partial,
                candidates,
                ..
            } => Some(ObjectKeyEmissionStage {
                partial: partial.as_str(),
                candidates: candidates.as_slice(),
            }),
            _ => None,
        }
    }
}

/// Stage of `Phase::ObjectKey` emission. The DFA is structurally the
/// same as a [`StringEnumEmissionStage`] over `candidates` — a prefix
/// trie with closing `'"'` enabled at trie nodes that match a complete
/// candidate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObjectKeyEmissionStage<'a> {
    /// Characters of the in-progress key emitted between the opening `'"'` and the cursor.
    pub partial: &'a str,
    /// Set of property names still consistent with `partial` (not yet seen and prefix-matched).
    pub candidates: &'a [String],
}

impl JsonGrammar {
    /// Set of single-byte characters that may legally come next.
    ///
    /// Emits an empty vector when the grammar is complete (no more input is
    /// valid). Used by [`super::json_schema::JsonSchemaProcessor`] to compute
    /// per-token allow masks.
    pub fn valid_next_chars(&self) -> Vec<char> {
        if self.done {
            return Vec::new();
        }
        let top = self.frames.len() - 1;
        let frame = &self.frames[top];
        let parent = if top > 0 {
            Some(&self.frames[top - 1])
        } else {
            None
        };
        let mut chars = valid_next_chars_for(frame, parent);
        // REQ-7: whitespace-permissive mode injects ASCII whitespace at
        // structural boundaries — anywhere that is NOT inside a string
        // body / string escape / number digit / literal walk / object
        // key walk. The phases where whitespace IS permitted are: any
        // `Phase::Start` (before the value-start char), `ObjectFreshOpen`,
        // `ObjectExpectKey`, `ObjectColon`, `ObjectAfterValue`,
        // `ArrayFreshOpen`, `ArrayAfterValue`.
        if self.whitespace_permissive && is_structural_phase(&frame.phase) {
            for ws in [' ', '\t', '\n', '\r'] {
                if !chars.contains(&ws) {
                    chars.push(ws);
                }
            }
        }
        // REQ-7 / #1490: whitespace also legally TERMINATES a number
        // when the digit-state is at a valid completion point. Without
        // this branch, `{ "a" : 1 }` rejects at the space after the
        // digit because `Phase::NumberDigits` is not a structural
        // phase and `valid_next_chars_for` doesn't emit whitespace
        // there. We add the whitespace chars to the mask iff at least
        // one digit has been emitted AND we're not mid-decimal /
        // mid-exponent-without-digit — the same completion-state
        // predicate used by the DFA dispatch's `complete_states`.
        if self.whitespace_permissive
            && let Phase::NumberDigits {
                had_digits,
                had_decimal,
                had_fractional_digit,
                had_exponent_marker,
                had_exponent_digit,
                ..
            } = &frame.phase
            && *had_digits
            && (!*had_decimal || *had_fractional_digit)
            && (!*had_exponent_marker || *had_exponent_digit)
        {
            for ws in [' ', '\t', '\n', '\r'] {
                if !chars.contains(&ws) {
                    chars.push(ws);
                }
            }
        }
        chars
    }

    /// Advance the state by one character. Returns an error if the character
    /// is not a valid next emission. The state is left unchanged on error.
    ///
    /// # Errors
    ///
    /// Returns [`StepError::AlreadyComplete`] if the grammar has
    /// already accepted a complete value, [`StepError::UnexpectedChar`]
    /// if `c` is not in the current valid-next-chars set, or
    /// [`StepError::Unsupported`] if the character would advance into
    /// an unsupported branch (e.g. a JSON string escape).
    pub fn step_char(&mut self, c: char) -> Result<(), StepError> {
        if self.done {
            return Err(StepError::AlreadyComplete);
        }
        let allowed = self.valid_next_chars();
        if !allowed.contains(&c) {
            return Err(StepError::UnexpectedChar {
                got: c,
                expected: allowed,
            });
        }
        // REQ-7: whitespace-permissive mode silently consumes
        // whitespace at structural phases (does not advance the frame).
        if self.whitespace_permissive
            && matches!(c, ' ' | '\t' | '\n' | '\r')
            && !self.frames.is_empty()
            && is_structural_phase(&self.frames[self.frames.len() - 1].phase)
        {
            return Ok(());
        }
        // REQ-7 / #1490: whitespace-permissive mode also TERMINATES a
        // number frame when the digits-state is at a completion point.
        // `valid_next_chars` above already gates the mask so this
        // branch is unreachable unless the number frame is at a legal
        // completion state. We pop the frame (treating the number as
        // complete) and silently consume the whitespace — the parent's
        // post-pop phase is structural (`ObjectAfterValue` /
        // `ArrayAfterValue`) and the next `step_char` call will admit
        // further whitespace via the structural-phase bypass above.
        if self.whitespace_permissive
            && matches!(c, ' ' | '\t' | '\n' | '\r')
            && !self.frames.is_empty()
            && matches!(
                self.frames[self.frames.len() - 1].phase,
                Phase::NumberDigits {
                    had_digits: true,
                    ..
                }
            )
        {
            self.frames.pop();
            self.bubble_value_done();
            return Ok(());
        }
        // We've accepted that `c` is a legal next char; now mutate the
        // top-of-stack frame accordingly. The `apply_step` helper handles all
        // transitions (push, pop, internal phase change).
        self.apply_step(c)
    }

    /// Convenience: feed a string of characters one at a time. Stops at the
    /// first error.
    ///
    /// # Errors
    ///
    /// Forwards the first [`StepError`] returned by [`Self::step_char`].
    pub fn step_str(&mut self, s: &str) -> Result<(), StepError> {
        for c in s.chars() {
            self.step_char(c)?;
        }
        Ok(())
    }

    fn apply_step(&mut self, c: char) -> Result<(), StepError> {
        let top_idx = self.frames.len() - 1;
        let frame = &mut self.frames[top_idx];

        match (&frame.schema.clone(), &frame.phase.clone()) {
            // ----- STRING -----
            (Schema::String, Phase::Start) => {
                // Must be `"`.
                debug_assert_eq!(c, '"');
                frame.phase = Phase::StringChars {
                    partial: String::new(),
                    allowed: None,
                };
            }
            (Schema::StringEnum(values), Phase::Start) => {
                debug_assert_eq!(c, '"');
                frame.phase = Phase::StringChars {
                    partial: String::new(),
                    allowed: Some(values.clone()),
                };
            }
            (Schema::StringConstrained { .. }, Phase::Start) => {
                debug_assert_eq!(c, '"');
                frame.phase = Phase::StringChars {
                    partial: String::new(),
                    allowed: None,
                };
            }
            (Schema::StringConstrained { .. }, Phase::StringChars { .. }) => {
                if c == '"' {
                    self.frames.pop();
                    self.bubble_value_done();
                    return Ok(());
                }
                if c == '\\' {
                    let phase_owned = std::mem::replace(&mut frame.phase, Phase::Start);
                    if let Phase::StringChars { partial, allowed } = phase_owned {
                        frame.phase = Phase::StringEscape {
                            partial,
                            allowed,
                            hex_digits: 0,
                        };
                    }
                    return Ok(());
                }
                let phase_owned = std::mem::replace(&mut frame.phase, Phase::Start);
                if let Phase::StringChars {
                    mut partial,
                    allowed,
                } = phase_owned
                {
                    partial.push(c);
                    frame.phase = Phase::StringChars { partial, allowed };
                }
            }
            (Schema::StringConstrained { .. }, Phase::StringEscape { .. }) => {
                // Same escape handling as Schema::String (single-arm
                // re-dispatch via a synthetic Frame is overkill; replicate
                // the StringEscape arm body here verbatim).
                let phase_owned = std::mem::replace(&mut frame.phase, Phase::Start);
                if let Phase::StringEscape {
                    mut partial,
                    allowed,
                    hex_digits,
                } = phase_owned
                {
                    if hex_digits == 0 {
                        if c == 'u' {
                            frame.phase = Phase::StringEscape {
                                partial,
                                allowed,
                                hex_digits: 1,
                            };
                        } else {
                            let mapped = match c {
                                '"' => '"',
                                '\\' => '\\',
                                '/' => '/',
                                'b' => '\u{0008}',
                                'f' => '\u{000C}',
                                'n' => '\n',
                                'r' => '\r',
                                't' => '\t',
                                _ => unreachable!(
                                    "valid_next_chars for StringEscape hex_digits=0 \
                                     restricts to short-escapes + 'u'"
                                ),
                            };
                            partial.push(mapped);
                            frame.phase = Phase::StringChars { partial, allowed };
                        }
                    } else {
                        debug_assert!(c.is_ascii_hexdigit());
                        let new_n = hex_digits + 1;
                        if new_n < 5 {
                            frame.phase = Phase::StringEscape {
                                partial,
                                allowed,
                                hex_digits: new_n,
                            };
                        } else {
                            partial.push('\u{FFFD}');
                            frame.phase = Phase::StringChars { partial, allowed };
                        }
                    }
                }
            }
            (
                Schema::NumberConstrained { .. } | Schema::IntegerConstrained { .. },
                Phase::Start,
            ) => {
                // Re-dispatch through the unconstrained Number/Integer
                // path by morphing the frame's schema in-place. The
                // constraint is honoured at parse time / by downstream
                // validators; the grammar-level emission set is
                // identical.
                let is_integer = matches!(frame.schema, Schema::IntegerConstrained { .. });
                frame.schema = if is_integer {
                    Schema::Integer
                } else {
                    Schema::Number
                };
                return self.apply_step(c);
            }
            (Schema::String | Schema::StringEnum(_), Phase::StringChars { .. }) => {
                if c == '"' {
                    // For enums: only allow closing if partial matches a complete value.
                    if let Phase::StringChars {
                        partial,
                        allowed: Some(values),
                    } = &frame.phase
                    {
                        if !values.iter().any(|v| v == partial) {
                            return Err(StepError::UnexpectedChar {
                                got: c,
                                expected: vec![],
                            });
                        }
                    }
                    // Pop the string frame; the parent (if any) was already
                    // transitioned to its post-value phase before push.
                    self.frames.pop();
                    self.bubble_value_done();
                    return Ok(());
                }
                if c == '\\' {
                    // REQ-5: enter escape sub-phase for `Schema::String`
                    // only (StringEnum bodies are closed-value sets and
                    // never need escape sequences). `valid_next_chars_for`
                    // gates StringEnum so '\\' is never offered for them.
                    let phase_owned = std::mem::replace(&mut frame.phase, Phase::Start);
                    if let Phase::StringChars { partial, allowed } = phase_owned {
                        frame.phase = Phase::StringEscape {
                            partial,
                            allowed,
                            hex_digits: 0,
                        };
                    }
                    return Ok(());
                }
                // Else accumulate the char into `partial`.
                let phase_owned = std::mem::replace(&mut frame.phase, Phase::Start);
                if let Phase::StringChars {
                    mut partial,
                    allowed,
                } = phase_owned
                {
                    partial.push(c);
                    frame.phase = Phase::StringChars { partial, allowed };
                }
            }
            (Schema::String, Phase::StringEscape { .. }) => {
                // REQ-5 escape sequence handling.
                let phase_owned = std::mem::replace(&mut frame.phase, Phase::Start);
                if let Phase::StringEscape {
                    mut partial,
                    allowed,
                    hex_digits,
                } = phase_owned
                {
                    if hex_digits == 0 {
                        // First char after `\\`. Either one of the short
                        // escapes ("/\\\"bfnrt) or 'u' to start a 4-hex
                        // walk.
                        if c == 'u' {
                            frame.phase = Phase::StringEscape {
                                partial,
                                allowed,
                                hex_digits: 1,
                            };
                        } else {
                            let mapped = match c {
                                '"' => '"',
                                '\\' => '\\',
                                '/' => '/',
                                'b' => '\u{0008}',
                                'f' => '\u{000C}',
                                'n' => '\n',
                                'r' => '\r',
                                't' => '\t',
                                _ => unreachable!(
                                    "valid_next_chars for StringEscape hex_digits=0 \
                                     restricts to short-escapes + 'u'"
                                ),
                            };
                            partial.push(mapped);
                            frame.phase = Phase::StringChars { partial, allowed };
                        }
                    } else {
                        // hex_digits in 1..=4: walking `\\uXXXX`.
                        debug_assert!(c.is_ascii_hexdigit());
                        let new_n = hex_digits + 1;
                        debug_assert!(new_n <= 5);
                        if new_n < 5 {
                            // Still walking the hex sequence.
                            frame.phase = Phase::StringEscape {
                                partial,
                                allowed,
                                hex_digits: new_n,
                            };
                        } else {
                            // Resolved — `\\uXXXX` complete. Push a
                            // placeholder char (specific codepoint
                            // doesn't matter for grammar tracking; the
                            // on-the-wire form is the `\\uXXXX`
                            // sequence) and return to body.
                            partial.push('\u{FFFD}');
                            frame.phase = Phase::StringChars { partial, allowed };
                        }
                    }
                }
            }

            // ----- NUMBER / INTEGER -----
            (Schema::Number | Schema::Integer, Phase::Start) => {
                let mut had_sign = false;
                let mut had_digits = false;
                let mut is_zero_only = false;
                if c == '-' {
                    had_sign = true;
                } else {
                    debug_assert!(c.is_ascii_digit());
                    had_digits = true;
                    is_zero_only = c == '0';
                }
                frame.phase = Phase::NumberDigits {
                    had_sign,
                    had_digits,
                    had_decimal: false,
                    had_fractional_digit: false,
                    is_zero_only,
                    had_exponent_marker: false,
                    had_exponent_sign: false,
                    had_exponent_digit: false,
                };
            }
            (
                Schema::Number,
                Phase::NumberDigits {
                    had_sign,
                    had_digits,
                    had_decimal,
                    had_fractional_digit,
                    is_zero_only,
                    had_exponent_marker,
                    had_exponent_sign,
                    had_exponent_digit,
                },
            ) => {
                let (
                    had_sign,
                    had_digits,
                    had_decimal,
                    had_fractional_digit,
                    had_exponent_marker,
                    had_exponent_sign,
                    had_exponent_digit,
                ) = (
                    *had_sign,
                    *had_digits,
                    *had_decimal,
                    *had_fractional_digit,
                    *had_exponent_marker,
                    *had_exponent_sign,
                    *had_exponent_digit,
                );
                let _ = is_zero_only;
                if c == '.' {
                    // valid_next_chars only emits `.` when had_decimal is false,
                    // had_digits is true, and no exponent started yet.
                    frame.phase = Phase::NumberDigits {
                        had_sign,
                        had_digits,
                        had_decimal: true,
                        had_fractional_digit: false,
                        is_zero_only: false,
                        had_exponent_marker: false,
                        had_exponent_sign: false,
                        had_exponent_digit: false,
                    };
                } else if (c == 'e' || c == 'E') && had_digits && !had_exponent_marker {
                    // Begin exponent section.
                    frame.phase = Phase::NumberDigits {
                        had_sign,
                        had_digits,
                        had_decimal,
                        had_fractional_digit,
                        is_zero_only: false,
                        had_exponent_marker: true,
                        had_exponent_sign: false,
                        had_exponent_digit: false,
                    };
                } else if (c == '+' || c == '-')
                    && had_exponent_marker
                    && !had_exponent_sign
                    && !had_exponent_digit
                {
                    // Sign immediately after exponent marker.
                    frame.phase = Phase::NumberDigits {
                        had_sign,
                        had_digits,
                        had_decimal,
                        had_fractional_digit,
                        is_zero_only: false,
                        had_exponent_marker: true,
                        had_exponent_sign: true,
                        had_exponent_digit: false,
                    };
                } else if c.is_ascii_digit() {
                    if had_exponent_marker {
                        // Exponent digit.
                        frame.phase = Phase::NumberDigits {
                            had_sign,
                            had_digits,
                            had_decimal,
                            had_fractional_digit,
                            is_zero_only: false,
                            had_exponent_marker: true,
                            had_exponent_sign,
                            had_exponent_digit: true,
                        };
                    } else {
                        let new_is_zero_only = !had_digits && c == '0';
                        let new_fractional = had_decimal || had_fractional_digit;
                        frame.phase = Phase::NumberDigits {
                            had_sign,
                            had_digits: true,
                            had_decimal,
                            had_fractional_digit: new_fractional,
                            is_zero_only: new_is_zero_only,
                            had_exponent_marker: false,
                            had_exponent_sign: false,
                            had_exponent_digit: false,
                        };
                    }
                } else {
                    // Number ended by some non-digit; the parent decides what's
                    // valid based on context (`,`, `}`, `]`, end). The current
                    // frame is done — pop and re-dispatch the char to the parent.
                    self.frames.pop();
                    self.bubble_value_done();
                    return self.apply_step(c);
                }
            }
            (
                Schema::Integer,
                Phase::NumberDigits {
                    had_sign,
                    had_digits,
                    ..
                },
            ) => {
                let (had_sign, had_digits) = (*had_sign, *had_digits);
                if c.is_ascii_digit() {
                    let new_is_zero_only = !had_digits && c == '0';
                    frame.phase = Phase::NumberDigits {
                        had_sign,
                        had_digits: true,
                        had_decimal: false,
                        had_fractional_digit: false,
                        is_zero_only: new_is_zero_only,
                        had_exponent_marker: false,
                        had_exponent_sign: false,
                        had_exponent_digit: false,
                    };
                } else {
                    self.frames.pop();
                    self.bubble_value_done();
                    return self.apply_step(c);
                }
            }

            // ----- LITERALS (true/false/null) -----
            (Schema::Boolean, Phase::Start) => {
                let remaining = match c {
                    't' => "rue",
                    'f' => "alse",
                    _ => unreachable!("valid_next_chars only accepts t/f for Boolean"),
                };
                frame.phase = Phase::Literal { remaining };
            }
            (Schema::Null, Phase::Start) => {
                debug_assert_eq!(c, 'n');
                frame.phase = Phase::Literal { remaining: "ull" };
            }
            (Schema::Boolean | Schema::Null, Phase::Literal { remaining }) => {
                let r = *remaining;
                debug_assert!(r.starts_with(c));
                let new_remaining = &r[c.len_utf8()..];
                if new_remaining.is_empty() {
                    self.frames.pop();
                    self.bubble_value_done();
                } else {
                    frame.phase = Phase::Literal {
                        remaining: new_remaining,
                    };
                }
            }

            // ----- NULLABLE: dispatch into inner or null based on first char -----
            (Schema::Nullable(inner), Phase::Start) => {
                let inner = (**inner).clone();
                if c == 'n' {
                    // Treat as Null literal.
                    frame.schema = Schema::Null;
                    frame.phase = Phase::Literal { remaining: "ull" };
                } else {
                    // Switch to the inner schema and re-dispatch this char.
                    frame.schema = inner;
                    frame.phase = Phase::Start;
                    return self.apply_step(c);
                }
            }

            // ----- ONE_OF / ANY_OF: commit to the first branch whose
            // start state accepts the emitted char, then re-dispatch.
            // REQ-8 PARTIAL (#1486). Same shape as `Nullable`'s commit-
            // on-first-char semantics, generalised to N branches. The
            // walk is in document order — disjoint first-char sets
            // (the common case) make ordering irrelevant; overlapping
            // first-chars commit to the earlier branch, which the
            // post-emit validator can re-check against any other branch
            // if `oneOf`'s "exactly one" semantics matter to the
            // caller.
            (Schema::OneOf(branches), Phase::Start) | (Schema::AnyOf(branches), Phase::Start) => {
                let branches = branches.clone();
                let mut committed_idx: Option<usize> = None;
                for (i, branch) in branches.iter().enumerate() {
                    let probe = Frame {
                        schema: branch.clone(),
                        phase: Phase::Start,
                    };
                    if valid_next_chars_for(&probe, None).contains(&c) {
                        committed_idx = Some(i);
                        break;
                    }
                }
                let Some(idx) = committed_idx else {
                    return Err(StepError::UnexpectedChar {
                        got: c,
                        expected: Vec::new(),
                    });
                };
                frame.schema = branches[idx].clone();
                frame.phase = Phase::Start;
                return self.apply_step(c);
            }

            // ----- OBJECT -----
            (Schema::Object { .. }, Phase::Start) => {
                debug_assert_eq!(c, '{');
                frame.phase = Phase::ObjectFreshOpen {
                    keys_seen: BTreeSet::new(),
                };
            }
            (Schema::Object { .. }, Phase::ObjectFreshOpen { keys_seen })
            | (Schema::Object { .. }, Phase::ObjectExpectKey { keys_seen }) => {
                let keys_seen = keys_seen.clone();
                if c == '"' {
                    let candidates = match &frame.schema {
                        Schema::Object { properties, .. } => properties
                            .keys()
                            .filter(|k| !keys_seen.contains(*k))
                            .cloned()
                            .collect::<Vec<_>>(),
                        _ => unreachable!(),
                    };
                    frame.phase = Phase::ObjectKey {
                        partial: String::new(),
                        keys_seen,
                        candidates,
                    };
                } else {
                    debug_assert_eq!(c, '}');
                    self.frames.pop();
                    self.bubble_value_done();
                }
            }
            (
                Schema::Object { .. },
                Phase::ObjectKey {
                    partial,
                    keys_seen,
                    candidates,
                },
            ) => {
                let mut partial = partial.clone();
                let keys_seen = keys_seen.clone();
                let candidates = candidates.clone();
                if c == '"' {
                    // Key complete; partial must match exactly one candidate.
                    if !candidates.iter().any(|k| k == &partial) {
                        return Err(StepError::UnexpectedChar {
                            got: c,
                            expected: vec![],
                        });
                    }
                    frame.phase = Phase::ObjectColon {
                        current_key: partial,
                        keys_seen,
                    };
                } else {
                    partial.push(c);
                    frame.phase = Phase::ObjectKey {
                        partial,
                        keys_seen,
                        candidates,
                    };
                }
            }
            (
                Schema::Object { properties, .. },
                Phase::ObjectColon {
                    current_key,
                    keys_seen,
                },
            ) => {
                debug_assert_eq!(c, ':');
                // Push a child frame for the property's value.
                //
                // Invariant: `current_key` was set on the
                // `Phase::ObjectKey -> Phase::ObjectColon` transition
                // (line ~973) only after the guard at line ~967
                // (`candidates.iter().any(|k| k == &partial)`) succeeded.
                // `candidates` is constructed at line ~936 as the
                // not-yet-seen subset of *this same frame's*
                // `Schema::Object { properties, .. }` keys, so
                // `current_key in properties` holds by construction.
                let prop_schema = properties
                    .get(current_key)
                    .expect(
                        "invariant: ObjectColon.current_key was previously matched against \
                         this frame's Schema::Object.properties keys at the line ~967 guard \
                         (candidates.iter().any) — `properties.get(current_key)` is therefore Some",
                    )
                    .clone();
                let mut keys_seen = keys_seen.clone();
                keys_seen.insert(current_key.clone());
                frame.phase = Phase::ObjectAfterValue { keys_seen };
                self.frames.push(Frame {
                    schema: prop_schema,
                    phase: Phase::Start,
                });
            }
            (Schema::Object { .. }, Phase::ObjectAfterValue { keys_seen }) => {
                let keys_seen = keys_seen.clone();
                if c == ',' {
                    frame.phase = Phase::ObjectExpectKey { keys_seen };
                } else {
                    debug_assert_eq!(c, '}');
                    self.frames.pop();
                    self.bubble_value_done();
                }
            }

            // ----- ARRAY -----
            (Schema::Array { .. }, Phase::Start) => {
                debug_assert_eq!(c, '[');
                frame.phase = Phase::ArrayFreshOpen;
            }
            (Schema::Array { item }, Phase::ArrayFreshOpen) => {
                if c == ']' {
                    self.frames.pop();
                    self.bubble_value_done();
                } else {
                    let item_schema = (**item).clone();
                    frame.phase = Phase::ArrayAfterValue;
                    self.frames.push(Frame {
                        schema: item_schema,
                        phase: Phase::Start,
                    });
                    // Re-dispatch the char to the new top frame.
                    return self.apply_step(c);
                }
            }
            (Schema::Array { item }, Phase::ArrayAfterValue) => {
                if c == ',' {
                    let item_schema = (**item).clone();
                    frame.phase = Phase::ArrayAfterValue;
                    self.frames.push(Frame {
                        schema: item_schema,
                        phase: Phase::Start,
                    });
                } else {
                    debug_assert_eq!(c, ']');
                    self.frames.pop();
                    self.bubble_value_done();
                }
            }

            (schema, phase) => {
                return Err(StepError::Unsupported(Box::leak(
                    format!("schema={schema:?} phase={phase:?}").into_boxed_str(),
                )));
            }
        }
        Ok(())
    }

    fn bubble_value_done(&mut self) {
        // Called whenever a value-level frame finishes. If the stack is now
        // empty, the whole grammar is done. Otherwise, the parent frame's
        // phase update (e.g. ObjectAfterValue, ArrayAfterValue) was handled
        // before push; this is just a stack-empty check.
        if self.frames.is_empty() {
            self.done = true;
        }
    }
}

/// True for phases where whitespace can be silently inserted under
/// REQ-7 whitespace-permissive mode. Whitespace is NOT permitted in
/// the middle of a literal walk, number digit sequence, string body /
/// escape, or object-key emission — those represent the middle of a
/// terminal token; permitting whitespace there would mean accepting
/// invalid JSON (`tr ue`, `12 3`, `"he llo"`, `{ "ke y" : 1}`).
fn is_structural_phase(phase: &Phase) -> bool {
    matches!(
        phase,
        Phase::Start
            | Phase::ObjectFreshOpen { .. }
            | Phase::ObjectExpectKey { .. }
            | Phase::ObjectColon { .. }
            | Phase::ObjectAfterValue { .. }
            | Phase::ArrayFreshOpen
            | Phase::ArrayAfterValue
    )
}

/// Compute the set of valid next characters for the given top-of-stack frame,
/// using `parent` (if any) to compute correct value-end terminators for
/// number/integer children.
fn valid_next_chars_for(frame: &Frame, parent: Option<&Frame>) -> Vec<char> {
    match (&frame.schema, &frame.phase) {
        (Schema::String, Phase::Start) | (Schema::StringEnum(_), Phase::Start) => vec!['"'],
        (Schema::String, Phase::StringChars { partial: _, .. }) => {
            // REQ-5: `\\` is now allowed and transitions into
            // `Phase::StringEscape` (apply_step's `(Schema::String,
            // Phase::StringChars)` branch handles the transition).
            let mut chars: Vec<char> = (0x20u8..=0x7Eu8)
                .filter(|b| *b != b'"')
                .map(|b| b as char)
                .collect();
            // chars already includes '"' since it's in 0x20..=0x7E and
            // we don't filter it; correct that — '"' is filtered above.
            chars.push('"'); // closing quote (re-add since filtered)
            chars.sort_unstable();
            chars.dedup();
            chars
        }
        (
            Schema::StringConstrained {
                min_length,
                max_length,
            },
            Phase::StringChars { partial, .. },
        ) => {
            // REQ-6: length-constrained string body. Same content set
            // as Schema::String, but the closing `"` is only valid
            // when `partial.chars().count() >= min_length`, and body
            // chars are only valid while `< max_length`.
            let len = partial.chars().count() as u32;
            let mut chars: Vec<char> = Vec::new();
            let at_max = max_length.map(|m| len >= m).unwrap_or(false);
            if !at_max {
                chars.extend((0x20u8..=0x7Eu8).filter(|b| *b != b'"').map(|b| b as char));
            }
            if len >= *min_length {
                chars.push('"');
            }
            chars.sort_unstable();
            chars.dedup();
            chars
        }
        (Schema::StringConstrained { .. }, Phase::Start) => vec!['"'],
        (Schema::NumberConstrained { .. }, Phase::Start)
        | (Schema::IntegerConstrained { .. }, Phase::Start) => {
            let mut v: Vec<char> = ('0'..='9').collect();
            v.push('-');
            v
        }
        (
            Schema::NumberConstrained { .. },
            Phase::NumberDigits {
                had_digits,
                had_decimal,
                had_fractional_digit,
                is_zero_only,
                had_exponent_marker,
                had_exponent_sign,
                had_exponent_digit,
                ..
            },
        ) => {
            // Same emission set as `Schema::Number`; the numeric
            // bound (min/max) is enforced by the post-emit validator
            // — not the grammar — because mid-stream we can't know
            // the final value (e.g. `0.5e3` only resolves at emit
            // end).
            if *had_exponent_marker {
                let mut chars: Vec<char> = Vec::new();
                if !*had_exponent_sign && !*had_exponent_digit {
                    chars.push('+');
                    chars.push('-');
                }
                chars.extend('0'..='9');
                if *had_exponent_digit {
                    chars.extend(parent_terminators(parent));
                }
                return chars;
            }
            let mid_decimal = *had_decimal && !*had_fractional_digit;
            let mut chars: Vec<char> = if *is_zero_only {
                Vec::new()
            } else {
                ('0'..='9').collect()
            };
            if *had_digits && !*had_decimal {
                chars.push('.');
            }
            if *had_digits && !mid_decimal {
                chars.push('e');
                chars.push('E');
                chars.extend(parent_terminators(parent));
            }
            chars
        }
        (
            Schema::IntegerConstrained { .. },
            Phase::NumberDigits {
                had_digits,
                is_zero_only,
                ..
            },
        ) => {
            let mut chars: Vec<char> = if *is_zero_only {
                Vec::new()
            } else {
                ('0'..='9').collect()
            };
            if *had_digits {
                chars.extend(parent_terminators(parent));
            }
            chars
        }
        (Schema::StringEnum(_), Phase::StringChars { partial, allowed }) => {
            // Only chars that could extend `partial` toward a known enum
            // value (or close the string if partial equals a value).
            //
            // Invariant: this match arm is gated on
            // `Schema::StringEnum`. The only path that constructs a
            // `Phase::StringChars` from a `Schema::StringEnum` frame
            // (apply_step `(Schema::StringEnum(values), Phase::Start)`,
            // line ~755) initialises `allowed: Some(values.clone())`.
            // The `allowed: None` variant is only produced by the
            // `Schema::String` arm (~750), which never reaches here.
            let allowed = allowed.as_ref().expect(
                "invariant: Phase::StringChars constructed from Schema::StringEnum always sets \
                 allowed = Some(values.clone()) — see apply_step (Schema::StringEnum, Phase::Start) \
                 around line 755; the None variant is unique to Schema::String",
            );
            let mut next: BTreeSet<char> = BTreeSet::new();
            for v in allowed {
                if v.starts_with(partial.as_str()) && v.len() > partial.len() {
                    // Invariant: `v.len() > partial.len()` (just checked
                    // on the line above) means the slice
                    // `&v[partial.len()..]` has at least one byte;
                    // since `v` is a `String` and the slice starts at a
                    // char boundary by construction (we only push whole
                    // chars into `partial` — see apply_step's
                    // (Schema::String | Schema::StringEnum, StringChars)
                    // branch around line 790), `chars().next()` returns
                    // `Some`.
                    let next_c = v[partial.len()..].chars().next().expect(
                        "invariant: v.len() > partial.len() guarantees the suffix slice is \
                         non-empty, and partial only ever contains whole chars (see apply_step \
                         StringChars accumulation around line 790), so the slice begins at a char \
                         boundary",
                    );
                    next.insert(next_c);
                }
            }
            // Closing quote allowed only if `partial` is itself a complete value.
            if allowed.iter().any(|v| v == partial) {
                next.insert('"');
            }
            next.into_iter().collect()
        }

        (Schema::Number, Phase::Start) | (Schema::Integer, Phase::Start) => {
            let mut v: Vec<char> = ('0'..='9').collect();
            v.push('-');
            v
        }
        (
            Schema::Number,
            Phase::NumberDigits {
                had_digits,
                had_decimal,
                had_fractional_digit,
                is_zero_only,
                had_exponent_marker,
                had_exponent_sign,
                had_exponent_digit,
                ..
            },
        ) => {
            // After an initial `-`, must emit digits next. Once any digits
            // are present, the number can end; the *parent* frame decides
            // what character ends it via re-dispatch in apply_step.
            //
            // JSON forbids leading zeros: after a single `0` as the first
            // digit, no more digits are allowed (only `.` or `e`/`E` or a
            // terminator). JSON also requires at least one fractional
            // digit after `.`: `1.` is invalid, `1.0` is valid. While
            // we're mid-decimal, only digits are allowed (no terminator).
            //
            // REQ-6: exponent section after digits (or after fractional
            // digit). `e`/`E` opens it; an optional `+`/`-` follows; one
            // or more digits complete it.
            if *had_exponent_marker {
                let mut chars: Vec<char> = Vec::new();
                if !*had_exponent_sign && !*had_exponent_digit {
                    chars.push('+');
                    chars.push('-');
                }
                // The exponent-digit upper bound is enforced by
                // apply_step transitioning to a pop on overflow — see
                // the parent-terminator-on-bounded-exponent comment
                // there. valid_next_chars here always emits digits
                // because the cap is on emit count, not legality.
                chars.extend('0'..='9');
                if *had_exponent_digit {
                    chars.extend(parent_terminators(parent));
                }
                return chars;
            }
            let mid_decimal = *had_decimal && !*had_fractional_digit;
            let mut chars: Vec<char> = if *is_zero_only {
                Vec::new()
            } else {
                ('0'..='9').collect()
            };
            if *had_digits && !*had_decimal {
                chars.push('.');
            }
            // Exponent marker valid once we have digits (with or without
            // fractional part), and not mid-decimal.
            if *had_digits && !mid_decimal {
                chars.push('e');
                chars.push('E');
                chars.extend(parent_terminators(parent));
            }
            chars
        }
        (Schema::String, Phase::StringEscape { hex_digits, .. }) => {
            if *hex_digits == 0 {
                // Short escapes: " \ / b f n r t, plus 'u' to begin \uXXXX.
                vec!['"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u']
            } else if *hex_digits < 4 {
                // Hex digits remaining.
                let mut v: Vec<char> = ('0'..='9').collect();
                v.extend('a'..='f');
                v.extend('A'..='F');
                v
            } else {
                // hex_digits == 4: the next hex digit completes the
                // escape and returns us to body. Same valid set.
                let mut v: Vec<char> = ('0'..='9').collect();
                v.extend('a'..='f');
                v.extend('A'..='F');
                v
            }
        }
        (
            Schema::Integer,
            Phase::NumberDigits {
                had_digits,
                is_zero_only,
                ..
            },
        ) => {
            let mut chars: Vec<char> = if *is_zero_only {
                Vec::new()
            } else {
                ('0'..='9').collect()
            };
            if *had_digits {
                chars.extend(parent_terminators(parent));
            }
            chars
        }

        (Schema::Boolean, Phase::Start) => vec!['t', 'f'],
        (Schema::Null, Phase::Start) => vec!['n'],
        (Schema::Boolean, Phase::Literal { remaining })
        | (Schema::Null, Phase::Literal { remaining }) => {
            // Invariant: `Phase::Literal` is *never* observable with
            // `remaining = ""`. The Boolean/Null branch in apply_step
            // (around line 895) checks `new_remaining.is_empty()` and
            // pops the frame in that case rather than leaving an empty
            // `Phase::Literal` on the stack. Initial construction
            // (lines ~889 and ~893) seeds with "rue"/"alse"/"ull",
            // none of which are empty.
            vec![remaining.chars().next().expect(
                "invariant: Phase::Literal with empty `remaining` is never observable — \
                 apply_step (Schema::Boolean | Schema::Null, Phase::Literal) at line ~895 pops \
                 the frame instead of leaving an empty literal on the stack",
            )]
        }

        (Schema::Nullable(inner), Phase::Start) => {
            let mut v = valid_next_chars_for(
                &Frame {
                    schema: (**inner).clone(),
                    phase: Phase::Start,
                },
                parent,
            );
            v.push('n'); // null branch
            v.sort_unstable();
            v.dedup();
            v
        }

        // REQ-8 PARTIAL (#1486): oneOf / anyOf union of branch start
        // chars. The frame commits to a branch on the first emitted
        // char (`apply_step` matches the same shape).
        (Schema::OneOf(branches), Phase::Start) | (Schema::AnyOf(branches), Phase::Start) => {
            let mut set: BTreeSet<char> = BTreeSet::new();
            for branch in branches {
                let probe = Frame {
                    schema: branch.clone(),
                    phase: Phase::Start,
                };
                for c in valid_next_chars_for(&probe, parent) {
                    set.insert(c);
                }
            }
            set.into_iter().collect()
        }

        (Schema::Object { properties, .. }, Phase::Start) => {
            let _ = properties;
            vec!['{']
        }
        (
            Schema::Object {
                properties,
                required,
            },
            Phase::ObjectFreshOpen { keys_seen },
        ) => {
            let mut v = vec![];
            // Need at least one more key if any required key is unseen.
            let unseen_required: Vec<&String> = required
                .iter()
                .filter(|k| !keys_seen.contains(*k))
                .collect();
            if !properties.keys().all(|k| keys_seen.contains(k)) {
                v.push('"');
            }
            if unseen_required.is_empty() {
                v.push('}');
            }
            v
        }
        (Schema::Object { .. }, Phase::ObjectExpectKey { keys_seen: _ }) => vec!['"'],
        (
            Schema::Object { .. },
            Phase::ObjectKey {
                partial,
                candidates,
                ..
            },
        ) => {
            let mut next: BTreeSet<char> = BTreeSet::new();
            for k in candidates {
                if k.starts_with(partial.as_str()) && k.len() > partial.len() {
                    // Invariant: `k.len() > partial.len()` (just
                    // checked) means `&k[partial.len()..]` is
                    // non-empty. `partial` is built up character-by-
                    // character via `partial.push(c)` in apply_step's
                    // `(Schema::Object { .. }, Phase::ObjectKey { .. })`
                    // branch (line ~978), so `partial.len()` is on a
                    // char boundary of `k` (when `k.starts_with(partial)`).
                    next.insert(k[partial.len()..].chars().next().expect(
                        "invariant: k.len() > partial.len() makes the suffix slice non-empty, \
                         and partial is accumulated via push(c) on whole chars in apply_step \
                         ObjectKey (line ~978), so the slice begins at a char boundary",
                    ));
                }
            }
            // Allow closing the key string only if partial matches one of the
            // candidate keys exactly.
            if candidates.iter().any(|k| k == partial) {
                next.insert('"');
            }
            next.into_iter().collect()
        }
        (Schema::Object { .. }, Phase::ObjectColon { .. }) => vec![':'],
        (
            Schema::Object {
                properties,
                required,
            },
            Phase::ObjectAfterValue { keys_seen },
        ) => {
            let mut v = vec![];
            let unseen_required: Vec<&String> = required
                .iter()
                .filter(|k| !keys_seen.contains(*k))
                .collect();
            if !properties.keys().all(|k| keys_seen.contains(k)) {
                v.push(',');
            }
            if unseen_required.is_empty() {
                v.push('}');
            }
            v
        }

        (Schema::Array { .. }, Phase::Start) => vec!['['],
        (Schema::Array { item }, Phase::ArrayFreshOpen) => {
            // Either close the array, or emit a value-start character.
            let mut v = valid_next_chars_for(
                &Frame {
                    schema: (**item).clone(),
                    phase: Phase::Start,
                },
                Some(frame),
            );
            v.push(']');
            v.sort_unstable();
            v.dedup();
            v
        }
        (Schema::Array { .. }, Phase::ArrayAfterValue) => vec![',', ']'],

        // We've already handled all defined transitions; the bubble-up branches
        // are reached via `apply_step` re-dispatch only and shouldn't surface
        // here.
        _ => Vec::new(),
    }
}

/// What characters does the parent frame use as a terminator for the
/// currently-active number/integer child?
///
/// When the active value is at top level (no parent), there is no JSON
/// terminator — the value ends implicitly via [`JsonGrammar::is_complete`]
/// reporting `true` once at least one digit has been emitted. We return an
/// empty set in that case so the LLM can keep emitting digits up to its
/// own EOS decision.
fn parent_terminators(parent: Option<&Frame>) -> Vec<char> {
    let Some(parent) = parent else {
        return Vec::new();
    };
    match (&parent.schema, &parent.phase) {
        (
            Schema::Object {
                properties,
                required,
            },
            Phase::ObjectAfterValue { keys_seen },
        ) => {
            let mut v = vec![];
            let unseen_required: Vec<&String> = required
                .iter()
                .filter(|k| !keys_seen.contains(*k))
                .collect();
            if !properties.keys().all(|k| keys_seen.contains(k)) {
                v.push(',');
            }
            if unseen_required.is_empty() {
                v.push('}');
            }
            v
        }
        (Schema::Array { .. }, Phase::ArrayAfterValue) => vec![',', ']'],
        // Other parent shapes don't host value children directly; over-approx
        // empty rather than guess wrong.
        _ => Vec::new(),
    }
}

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

    fn obj(props: &[(&str, Schema)], required: &[&str]) -> Schema {
        let mut p = BTreeMap::new();
        for (k, s) in props {
            p.insert((*k).to_string(), s.clone());
        }
        let mut r = BTreeSet::new();
        for k in required {
            r.insert((*k).to_string());
        }
        Schema::Object {
            properties: p,
            required: r,
        }
    }

    #[test]
    fn empty_object_round_trip() {
        let s = obj(&[], &[]);
        let mut g = JsonGrammar::new(s);
        assert_eq!(g.valid_next_chars(), vec!['{']);
        g.step_char('{').unwrap();
        assert!(g.valid_next_chars().contains(&'}'));
        g.step_char('}').unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn rejects_emitting_after_complete() {
        let mut g = JsonGrammar::new(Schema::Boolean);
        g.step_str("true").unwrap();
        assert!(g.is_complete());
        let err = g.step_char(',').unwrap_err();
        assert!(matches!(err, StepError::AlreadyComplete));
    }

    #[test]
    fn boolean_true_and_false() {
        let mut g = JsonGrammar::new(Schema::Boolean);
        assert_eq!(g.valid_next_chars(), vec!['t', 'f']);
        g.step_str("true").unwrap();
        assert!(g.is_complete());

        let mut g = JsonGrammar::new(Schema::Boolean);
        g.step_str("false").unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn null_literal() {
        let mut g = JsonGrammar::new(Schema::Null);
        g.step_str("null").unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn integer_round_trip() {
        let mut g = JsonGrammar::new(Schema::Integer);
        g.step_str("123").unwrap();
        // Integer ends implicitly; force end via a top-level boundary: at this
        // point we're at top-level, so re-dispatching, say, EOF means done.
        // To detect: number frame should pop on first non-digit; we simulate
        // by checking the frame stack via valid_next_chars: after digits at
        // top level there is no further valid char, so digit-only stream is
        // legal but doesn't auto-complete. We model "complete" via emitting
        // a synthetic terminator — for top-level use the public API
        // `finish()` (added in the public wrapper) or rely on the LLM
        // producing the EOS token. For the unit test, just assert
        // valid_next_chars contains digits and terminators are the empty set
        // at top level.
        let chars = g.valid_next_chars();
        assert!(chars.contains(&'4'));
        // The parent_terminators heuristic adds `,`, `}`, `]` which are
        // **not** valid at the top level; the resolver here is honest about
        // its over-approximation. The test asserts the digit branch works.
    }

    #[test]
    fn negative_number() {
        let mut g = JsonGrammar::new(Schema::Number);
        assert!(g.valid_next_chars().contains(&'-'));
        g.step_char('-').unwrap();
        // Sign emitted; need a digit next.
        assert!(g.valid_next_chars().contains(&'1'));
        g.step_str("3.14").unwrap();
    }

    #[test]
    fn string_round_trip() {
        let mut g = JsonGrammar::new(Schema::String);
        assert_eq!(g.valid_next_chars(), vec!['"']);
        g.step_char('"').unwrap();
        assert!(g.valid_next_chars().contains(&'a'));
        // REQ-5 SHIPPED: `\\` is now allowed (transitions into escape phase).
        assert!(g.valid_next_chars().contains(&'\\'));
        // Quote not allowed mid-string... wait, quote ENDS the string.
        // The test was wrong. Quote IS allowed (closes string).
        g.step_str("hi").unwrap();
        g.step_char('"').unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn string_enum_round_trip() {
        let s = Schema::StringEnum(vec!["high".into(), "medium".into(), "low".into()]);
        let mut g = JsonGrammar::new(s);
        g.step_char('"').unwrap();
        // Only h, m, l should be valid (first chars of enum values).
        let nx = g.valid_next_chars();
        assert!(nx.contains(&'h'));
        assert!(nx.contains(&'m'));
        assert!(nx.contains(&'l'));
        assert!(!nx.contains(&'z'));
        g.step_str("medium\"").unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn string_enum_rejects_invalid_prefix() {
        let s = Schema::StringEnum(vec!["high".into(), "low".into()]);
        let mut g = JsonGrammar::new(s);
        g.step_char('"').unwrap();
        // `m` is not a valid first char of any enum value.
        assert!(g.step_char('m').is_err());
    }

    #[test]
    fn object_with_required_field() {
        let s = obj(
            &[("name", Schema::String), ("n", Schema::Integer)],
            &["name"],
        );
        let mut g = JsonGrammar::new(s);
        g.step_char('{').unwrap();
        // Can't close yet — `name` is required and unseen.
        assert!(!g.valid_next_chars().contains(&'}'));
        g.step_str("\"name\":\"foo\"").unwrap();
        // Now `,` is allowed (more keys) AND `}` is allowed (required satisfied).
        let nx = g.valid_next_chars();
        assert!(nx.contains(&','));
        assert!(nx.contains(&'}'));
        g.step_char('}').unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn object_rejects_unknown_key() {
        let s = obj(&[("name", Schema::String)], &["name"]);
        let mut g = JsonGrammar::new(s);
        g.step_char('{').unwrap();
        g.step_char('"').unwrap();
        // Only 'n' is a valid first char (only key is "name").
        assert!(g.step_char('z').is_err());
    }

    #[test]
    fn object_rejects_duplicate_key() {
        let s = obj(&[("a", Schema::Integer), ("b", Schema::Integer)], &[]);
        let mut g = JsonGrammar::new(s);
        g.step_str("{\"a\":1,").unwrap();
        // Now we need another key — `a` is gone, must be `b`.
        g.step_char('"').unwrap();
        assert!(g.step_char('a').is_err());
        assert!(g.step_char('b').is_ok());
    }

    #[test]
    fn array_of_numbers() {
        let s = Schema::Array {
            item: Box::new(Schema::Number),
        };
        let mut g = JsonGrammar::new(s);
        g.step_str("[1,2.5,3]").unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn empty_array() {
        let s = Schema::Array {
            item: Box::new(Schema::Number),
        };
        let mut g = JsonGrammar::new(s);
        g.step_str("[]").unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn nested_object() {
        let s = obj(
            &[("inner", obj(&[("v", Schema::Boolean)], &["v"]))],
            &["inner"],
        );
        let mut g = JsonGrammar::new(s);
        g.step_str("{\"inner\":{\"v\":true}}").unwrap();
        assert!(g.is_complete());
    }

    #[test]
    fn nullable_string() {
        let s = Schema::Nullable(Box::new(Schema::String));
        let mut g = JsonGrammar::new(s.clone());
        g.step_str("null").unwrap();
        assert!(g.is_complete());

        let mut g = JsonGrammar::new(s);
        g.step_str("\"hi\"").unwrap();
        assert!(g.is_complete());
    }

    /// REQ-5 SHIPPED: short escapes (\", \\, \/, \b, \f, \n, \r, \t)
    /// transition into the escape sub-phase and resolve in one extra
    /// step.
    #[test]
    fn accepts_short_string_escape() {
        let mut g = JsonGrammar::new(Schema::String);
        g.step_str("\"a\\n").unwrap();
        // We're now back in the string body after the `\n` short escape.
        // Closing quote completes the string.
        g.step_char('"').unwrap();
        assert!(g.is_complete());
    }

    /// REQ-5 SHIPPED: backslash inside a string transitions to escape
    /// phase; the next char must be a JSON escape letter or 'u'.
    #[test]
    fn rejects_invalid_escape_letter() {
        let mut g = JsonGrammar::new(Schema::String);
        g.step_str("\"\\").unwrap();
        // Now in StringEscape; only `"\\/bfnrtu` are valid.
        let nx = g.valid_next_chars();
        assert!(nx.contains(&'n'));
        assert!(nx.contains(&'u'));
        assert!(!nx.contains(&'z'));
        assert!(g.step_char('z').is_err());
    }

    /// REQ-5 SHIPPED: \uXXXX hex escapes accept 4 hex digits.
    #[test]
    fn accepts_unicode_hex_escape() {
        let mut g = JsonGrammar::new(Schema::String);
        g.step_str("\"x\\u00FF").unwrap();
        // Back to body after 4 hex digits; closing quote completes.
        g.step_char('"').unwrap();
        assert!(g.is_complete());
    }

    /// REQ-6 SHIPPED: number exponents work for both signs and bare.
    #[test]
    fn number_with_exponent() {
        let mut g = JsonGrammar::new(Schema::Number);
        g.step_str("1.5e10").unwrap();
        // No more emission required to be valid.
        let mut g = JsonGrammar::new(Schema::Number);
        g.step_str("-3E+02").unwrap();
        let mut g = JsonGrammar::new(Schema::Number);
        g.step_str("0e-1").unwrap();
        let _ = g.is_complete();
    }

    /// REQ-6 SHIPPED: exponent marker requires at least one digit;
    /// after marker, a sign is optional; then digits required.
    #[test]
    fn number_exponent_requires_digit() {
        let mut g = JsonGrammar::new(Schema::Number);
        g.step_str("1e").unwrap();
        // After bare `e`, only +/- or digits valid.
        let nx = g.valid_next_chars();
        assert!(nx.contains(&'+'));
        assert!(nx.contains(&'-'));
        assert!(nx.contains(&'5'));
        assert!(!nx.contains(&'.'));
    }

    /// REQ-7 SHIPPED: whitespace-permissive mode accepts ASCII
    /// whitespace at structural boundaries.
    #[test]
    fn whitespace_permissive_accepts_whitespace() {
        let s = obj(&[("v", Schema::Boolean)], &["v"]);
        let mut g = JsonGrammar::new(s).with_whitespace_permissive(true);
        // Walk a whitespace-decorated input.
        g.step_str("{ \"v\" : true }").unwrap();
        assert!(g.is_complete());
    }

    /// REQ-7 SHIPPED: default mode still rejects whitespace.
    #[test]
    fn default_mode_rejects_whitespace() {
        let s = obj(&[("v", Schema::Boolean)], &["v"]);
        let mut g = JsonGrammar::new(s);
        assert!(g.step_char(' ').is_err());
    }
}