tabnas-yaml 0.5.13

A core-subset YAML grammar plugin for the tabnas parsing engine
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
/* Copyright (c) 2021-2026 Richard Rodger, MIT License */

//! A core-subset YAML grammar plugin for the
//! [`tabnas`](https://github.com/tabnas/parser) parsing engine.
//!
//! Unlike most tabnas grammars this one layers on
//! [`tabnas_jsonic`](https://github.com/tabnas/jsonic) rather than on the
//! bare engine: YAML's flow collections are relaxed JSON, and the block
//! forms are added around jsonic's `val` / `map` / `list` / `pair` /
//! `elem` rules.
//!
//! ```
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let value = tabnas_yaml::parse("name: Alice\nitems:\n  - one\n  - two\n")?;
//!     assert_eq!(
//!         value.to_string(),
//!         r#"{"name":"Alice","items":["one","two"]}"#
//!     );
//!     Ok(())
//! }
//! ```

#![forbid(unsafe_code)]
// The engine's error type is 664 bytes and every entry point returns it,
// as the other tabnas crates' entry points do.
#![allow(clippy::result_large_err)]

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, OnceLock};

use indexmap::IndexMap;
use serde_json::json;
use tabnas::{
    Context, GrammarSpec, Plugin, PluginError, Rule, Tabnas, Token, Value, TIN_ST, TIN_TX, TIN_VL,
};

mod lex;
mod state;
mod text;

/// Every `rust` fence in the README is compiled and run as a doctest.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme_examples {}

/// VERSION is this package's version. It MUST equal `ts/package.json`
/// "version": the release orchestrator rewrites all three constants, and
/// `tests/version_test.rs` fails the build if they drift. Mirrors
/// `const VERSION` in `ts/src/yaml.ts` and `go/yaml.go`.
pub const VERSION: &str = "0.5.13";

/// The plugin name, which is also the key its options sit under.
const PLUGIN_NAME: &str = "yaml";

/// Set on the instance once the plugin has installed, so a re-application
/// during option resolution does not install the grammar twice.
const INSTALLED: &str = "yaml-installed";

/// Errors this crate returns are the engine's, re-exported so a caller
/// does not need `tabnas` in scope to name them.
pub use tabnas::TabnasError as YamlError;

// --- BEGIN EMBEDDED yaml-grammar.jsonic ---
const GRAMMAR_TEXT: &str = r##"
# YAML Grammar Definition
# Parsed by a standard Tabnas instance and passed to tabnas.grammar()
# Function references (@ prefixed) are resolved against the refs map.
# State handlers (bo/ao/bc/ac) remain wired in code, since they use
# closures over per-parse state (anchors, pendingAnchors, etc.).

{
  # Amend val rule: YAML indent/element-marker handling.
  rule: val: open: {
    alts: [
      # Doc-frame markers between docs mean an empty value here; back up so
      # the stream rule consumes the marker and starts the next document.
      { s: '#DS' b: 1 a: '@val-set-null' g: yaml }
      { s: '#DE' b: 1 a: '@val-set-null' g: yaml }
      { s: '#DR' b: 1 a: '@val-set-null' g: yaml }
      # Indent followed by content: push indent rule.
      { s: '#IN' c: '@val-indent-deeper' p: indent a: '@val-set-in-from-o0' g: yaml }
      # Same indent followed by element marker: list value at map level.
      { s: ['#IN' '#EL'] c: '@val-indent-eq-parent' p: yamlBlockList a: '@val-set-in-from-o0' g: yaml }
      # End of input means empty value.
      { s: '#ZZ' b: 1 a: '@val-set-null' g: yaml }
      # Same or lesser indent after a colon means empty value — backtrack.
      { s: '#IN' b: 1 u: { yamlEmpty: true } g: yaml }
      # This value is a list.
      { s: '#EL' p: yamlBlockList a: '@val-set-el-in' g: yaml }
    ]
    inject: { append: false }
  }
  rule: val: close: {
    alts: [
      # Doc-frame markers terminate val; back up for the stream rule.
      { s: '#DS' b: 1 g: 'yaml,end' }
      { s: '#DE' b: 1 g: 'yaml,end' }
      { s: '#DR' b: 1 g: 'yaml,end' }
      { s: '#IN' b: 1 g: 'yaml,close' }
    ]
    inject: { append: false }
  }

  # Indent rule: start for block content at a given indent.
  rule: indent: open: [
    # Key pair => map.
    { s: ['#KEY' '#CL'] p: map b: 2 g: yaml }
    # Element marker => block sequence. yamlBlockList, not jsonic's list:
    # list reads a '[' as its own opening bracket, so a flow sequence as the
    # first item (k:\n  - [1, 2]) became the block sequence itself, or
    # failed at the next item (tabnas/yaml#88).
    { s: '#EL' p: yamlBlockList g: yaml }
    # Flow collection as a block-mapping value on the FOLLOWING line:
    #     required:
    #       [a, b, c]
    # Valid YAML 1.2, and the inline spelling has always worked. The
    # indent rule opens at the deeper indent and meets a
    # flow opener, which only the val rule knows how to read, so hand it back
    # one token so val sees the bracket or brace itself.
    { s: '#OS' p: val b: 1 g: yaml }
    { s: '#OB' p: val b: 1 g: yaml }
    # Plain value after indent (for nested scalars).
    { s: '#KEY' a: '@indent-plain-value' g: yaml }
  ]

  # YAML block list: handles "- " sequences without consuming "[".
  rule: yamlBlockList: open: [
    # Element value is a key-value map: - key: val
    { s: ['#KEY' '#CL'] p: yamlElemMap b: 2 a: '@set-map-in' g: yaml }
    # Default: push to val for the element's value.
    { p: val g: yaml }
  ]
  rule: yamlBlockList: close: [
    # Doc-frame markers terminate list; back up for the stream rule.
    { s: '#DS' b: 1 g: 'yaml,end' }
    { s: '#DE' b: 1 g: 'yaml,end' }
    { s: '#DR' b: 1 g: 'yaml,end' }
    # Indent followed by element marker: next element at same level.
    { s: ['#IN' '#EL'] c: '@t0-eq-in' r: yamlBlockElem g: 'yaml,comma' }
    # Same or lesser indent: close list.
    { s: '#IN' c: '@t0-le-in' b: 1 g: 'yaml,close' }
    # Element marker at top level (no preceding newline).
    { s: '#EL' r: yamlBlockElem g: 'yaml,comma' }
    { s: '#ZZ' b: 1 g: 'yaml,end' }
  ]

  # Subsequent elements in a yamlBlockList (via rotation).
  rule: yamlBlockElem: open: [
    { s: ['#KEY' '#CL'] p: yamlElemMap b: 2 a: '@set-map-in' g: yaml }
    { p: val g: yaml }
  ]
  rule: yamlBlockElem: close: [
    # Doc-frame markers terminate elem; back up for the stream rule.
    { s: '#DS' b: 1 g: 'yaml,end' }
    { s: '#DE' b: 1 g: 'yaml,end' }
    { s: '#DR' b: 1 g: 'yaml,end' }
    { s: ['#IN' '#EL'] c: '@t0-eq-in' r: yamlBlockElem g: 'yaml,comma' }
    { s: '#IN' c: '@t0-le-in' b: 1 g: 'yaml,close' }
    { s: '#EL' r: yamlBlockElem g: 'yaml,comma' }
    { s: '#ZZ' b: 1 g: 'yaml,end' }
  ]

  # Amend list rule: close on dedent or same-indent non-element.
  rule: list: close: {
    alts: [
      # Doc-frame markers terminate list; back up for the stream rule.
      { s: '#DS' b: 1 g: 'yaml,end' }
      { s: '#DE' b: 1 g: 'yaml,end' }
      { s: '#DR' b: 1 g: 'yaml,end' }
      { s: '#IN' c: '@t0-le-in' b: 1 g: 'yaml,close' }
    ]
    inject: { append: false }
  }

  # Amend map rule: same-indent indent continues map with pair.
  rule: map: open: {
    alts: [
      { s: '#IN' c: '@o0-eq-in' r: pair g: yaml }
    ]
    inject: { append: false }
  }
  rule: map: close: {
    alts: [
      # Doc-frame markers terminate map; back up for the stream rule.
      { s: '#DS' b: 1 g: 'yaml,end' }
      { s: '#DE' b: 1 g: 'yaml,end' }
      { s: '#DR' b: 1 g: 'yaml,end' }
      { s: '#IN' c: '@t0-lt-in' b: 1 g: 'yaml,close' }
    ]
    inject: { append: false }
  }

  # Amend pair rule: end of input ends pair; dedent closes, same-indent repeats.
  # Also handle YAML flow-mapping shapes Tabnas doesn't have natively:
  # - implicit null values: {a, b: c}  — KEY followed directly by CA or CB
  # - explicit-key marker:  {? k : v}  — leading #QM is consumed
  rule: pair: open: {
    alts: [
      { s: ['#KEY' '#CA'] a: '@implicit-null-pair' b: 1 g: yaml }
      { s: ['#KEY' '#CB'] a: '@implicit-null-pair' b: 1 g: yaml }
      { s: ['#QM' '#KEY' '#CL'] p: val u: { pair: true } a: '@qm-pairkey' g: yaml }
      { s: ['#QM' '#KEY' '#CA'] a: '@qm-implicit-null-pair' b: 1 g: yaml }
      { s: ['#QM' '#KEY' '#CB'] a: '@qm-implicit-null-pair' b: 1 g: yaml }
      { s: '#ZZ' b: 1 g: 'yaml,end' }
    ]
    inject: { append: false }
  }
  rule: pair: close: {
    alts: [
      # Doc-frame markers terminate pair; back up for the stream rule.
      { s: '#DS' b: 1 g: 'yaml,end' }
      { s: '#DE' b: 1 g: 'yaml,end' }
      { s: '#DR' b: 1 g: 'yaml,end' }
      { s: '#IN' c: '@t0-eq-in' r: pair g: 'yaml,comma' }
      { s: '#IN' c: '@t0-lt-in' b: 1 g: 'yaml,close' }
    ]
    inject: { append: false }
  }

  # yamlElemMap: "- key: val" patterns.
  rule: yamlElemMap: open: [
    { s: ['#KEY' '#CL'] p: val a: '@elem-key' g: yaml }
  ]
  rule: yamlElemMap: close: [
    # Doc-frame markers terminate elem-map; back up for the stream rule.
    { s: '#DS' b: 1 g: 'yaml,end' }
    { s: '#DE' b: 1 g: 'yaml,end' }
    { s: '#DR' b: 1 g: 'yaml,end' }
    { s: '#IN' c: '@t0-eq-map-in' r: yamlElemPair g: 'yaml,comma' }
    { s: '#IN' b: 1 g: 'yaml,close' }
    { s: '#CA' b: 1 g: 'yaml,comma' }
    { s: '#CS' b: 1 g: 'yaml,close' }
    { s: '#CB' b: 1 g: 'yaml,close' }
    { s: '#ZZ' g: 'yaml,end' }
  ]

  # Additional pairs in a yamlElemMap.
  rule: yamlElemPair: open: [
    { s: ['#KEY' '#CL'] p: val a: '@elem-key' g: yaml }
  ]
  rule: yamlElemPair: close: [
    # Doc-frame markers terminate elem-pair; back up for the stream rule.
    { s: '#DS' b: 1 g: 'yaml,end' }
    { s: '#DE' b: 1 g: 'yaml,end' }
    { s: '#DR' b: 1 g: 'yaml,end' }
    { s: '#IN' c: '@t0-eq-map-in' r: yamlElemPair g: 'yaml,comma' }
    { s: '#IN' b: 1 g: 'yaml,close' }
    { s: '#CA' b: 1 g: 'yaml,comma' }
    { s: '#CS' b: 1 g: 'yaml,close' }
    { s: '#CB' b: 1 g: 'yaml,close' }
    { s: '#ZZ' g: 'yaml,end' }
  ]

  # Amend elem rule for YAML sequences ("- key: val" at top level of [ ... ]).
  # Also handle flow-sequence explicit-key entries: [? k : v] is a single-pair
  # map element. Eat the leading #QM, then back up KEY+CL so yamlElemMap
  # consumes them as a normal pair.
  rule: elem: open: {
    alts: [
      { s: ['#KEY' '#CL'] p: yamlElemMap b: 2 a: '@set-map-in' g: yaml }
      { s: ['#QM' '#KEY' '#CL'] p: yamlElemMap b: 2 a: '@set-map-in' g: yaml }
    ]
    inject: { append: false }
  }
  rule: elem: close: {
    alts: [
      # Doc-frame markers terminate elem; back up for the stream rule.
      { s: '#DS' b: 1 g: 'yaml,end' }
      { s: '#DE' b: 1 g: 'yaml,end' }
      { s: '#DR' b: 1 g: 'yaml,end' }
      { s: ['#IN' '#EL'] c: '@t0-eq-in' r: elem g: 'yaml,comma' }
      { s: '#IN' c: '@t0-eq-in' b: 1 g: 'yaml,close' }
      { s: '#IN' c: '@t0-lt-in' b: 1 g: 'yaml,close' }
      { s: '#EL' r: elem g: 'yaml,comma' }
    ]
    inject: { append: false }
  }
}
"##;
// --- END EMBEDDED yaml-grammar.jsonic ---

/// The options this plugin takes.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct YamlOptions {
    /// When true, a parse returns `{meta, content}` instead of bare
    /// content. `meta` is a per-document `{directives, explicit, ended}`
    /// object for a single document, or an array of them for a stream.
    pub meta: bool,
}

impl YamlOptions {
    /// The option bag as the engine's plugin values.
    fn to_value(self) -> Value {
        let mut map = IndexMap::new();
        map.insert("meta".to_string(), Value::Bool(self.meta));
        Value::object(map)
    }
}

// ---------------------------------------------------------------------
// Value helpers
// ---------------------------------------------------------------------

/// Assign a rule's node: the Rust spelling of TypeScript `r.node = v`.
///
/// A pushed or replaced rule SHARES its parent's node cell, so writing
/// through `rule.node.borrow_mut()` would overwrite the parent's node
/// too. Assigning installs a fresh cell instead. The cell is borrowed
/// directly only to mutate a container the rule genuinely shares.
fn set_node(rule: &mut Rule, value: Value) {
    rule.node = Rc::new(RefCell::new(value));
}

fn map_insert(node: &mut Value, key: String, value: Value) {
    match node {
        Value::Object(map) => {
            Arc::make_mut(map).insert(key, value);
        }
        Value::MapRef(map) => {
            Arc::make_mut(map).value.insert(key, value);
        }
        _ => {}
    }
}

fn map_entries(node: &Value) -> Option<&IndexMap<String, Value>> {
    match node {
        Value::Object(map) => Some(map),
        Value::MapRef(map) => Some(&map.value),
        _ => None,
    }
}

fn list_push(node: &mut Value, value: Value) {
    match node {
        Value::Array(items) => Arc::make_mut(items).push(value),
        Value::ListRef(list) => Arc::make_mut(list).value.push(value),
        _ => {}
    }
}

/// Whether `typeof value === 'object'` would hold in JavaScript, with
/// `null` excluded as the canonical guards exclude it.
fn is_container(value: &Value) -> bool {
    matches!(
        value,
        Value::Object(_) | Value::Array(_) | Value::MapRef(_) | Value::ListRef(_) | Value::Text(_)
    )
}

/// The `JSON.parse(JSON.stringify(v))` the canonical implementation
/// copies an anchored value with: containers are rebuilt, an `undefined`
/// entry is dropped from an object and becomes null in an array, and a
/// non-finite number becomes null.
fn deep_copy(value: &Value) -> Value {
    match value {
        Value::Object(map) => {
            let mut out = IndexMap::new();
            for (key, entry) in map.iter() {
                if matches!(entry, Value::Undefined) {
                    continue;
                }
                out.insert(key.clone(), deep_copy(entry));
            }
            Value::object(out)
        }
        Value::MapRef(map) => {
            let mut out = IndexMap::new();
            for (key, entry) in map.value.iter() {
                if matches!(entry, Value::Undefined) {
                    continue;
                }
                out.insert(key.clone(), deep_copy(entry));
            }
            Value::object(out)
        }
        Value::Array(items) => Value::array(items.iter().map(deep_copy).collect()),
        Value::ListRef(list) => Value::array(list.value.iter().map(deep_copy).collect()),
        Value::Undefined => Value::Null,
        Value::Number(number) if !number.is_finite() => Value::Null,
        Value::Text(text) => Value::String(text.string.clone()),
        other => other.clone(),
    }
}

/// `String(value)` as JavaScript spells it, which is how a non-string key
/// becomes an object property name.
pub(crate) fn string_of(value: &Value) -> String {
    match value {
        Value::Undefined => "undefined".to_string(),
        Value::Null => "null".to_string(),
        Value::Bool(flag) => flag.to_string(),
        Value::Number(number) => js_number_to_string(*number),
        Value::String(text) => text.clone(),
        Value::Text(text) => text.string.clone(),
        Value::Array(items) => items
            .iter()
            .map(|item| match item {
                Value::Undefined | Value::Null => String::new(),
                other => string_of(other),
            })
            .collect::<Vec<_>>()
            .join(","),
        Value::ListRef(list) => string_of(&Value::array(list.value.clone())),
        Value::Object(_) | Value::MapRef(_) => "[object Object]".to_string(),
    }
}

/// `String(number)` as JavaScript spells it: the shortest digit string
/// that reads back as the same double, in plain decimal while the decimal
/// point stays inside `(-6, 21]` and in exponent form outside it.
fn js_number_to_string(number: f64) -> String {
    if number.is_nan() {
        return "NaN".to_string();
    }
    // Catches -0.0 as well: JavaScript spells both zeros "0".
    if number == 0.0 {
        return "0".to_string();
    }
    if number < 0.0 {
        return format!("-{}", js_number_to_string(-number));
    }
    if number.is_infinite() {
        return "Infinity".to_string();
    }
    let shortest = format!("{number:e}");
    let shortest_k = shortest
        .split_once('e')
        .map(|(mantissa, _)| mantissa.chars().filter(char::is_ascii_digit).count())
        .expect("a finite f64 always formats with an exponent");
    let exponential = format!("{:.*e}", shortest_k - 1, number);
    let (mantissa, exponent) = exponential
        .split_once('e')
        .expect("a finite f64 always formats with an exponent");
    let digits = mantissa
        .chars()
        .filter(|digit| *digit != '.')
        .collect::<String>();
    let digits = digits.trim_end_matches('0');
    let digits = if digits.is_empty() { "0" } else { digits };
    let k = digits.len() as i32;
    let n = exponent
        .parse::<i32>()
        .expect("a formatted exponent is an integer")
        + 1;
    if (k..=21).contains(&n) {
        let mut text = digits.to_string();
        text.push_str(&"0".repeat((n - k) as usize));
        text
    } else if (1..=21).contains(&n) {
        let point = n as usize;
        format!("{}.{}", &digits[..point], &digits[point..])
    } else if (-5..=0).contains(&n) {
        format!("0.{}{}", "0".repeat(-n as usize), digits)
    } else {
        let sign = if n - 1 < 0 { '-' } else { '+' };
        let power = (n - 1).abs();
        if k == 1 {
            format!("{digits}e{sign}{power}")
        } else {
            format!("{}.{}e{sign}{power}", &digits[..1], &digits[1..])
        }
    }
}

/// `Number(text)`, the conversion the unary plus in the canonical scalar
/// handler performs. `None` stands for `NaN`, which is how a plain scalar
/// stays text.
pub(crate) fn js_to_number(text: &str) -> Option<f64> {
    let trimmed = text.trim_matches(js_space);
    if trimmed.is_empty() {
        return Some(0.0);
    }
    if let Some(radix) = match trimmed.get(..2) {
        Some("0x" | "0X") => Some(16u32),
        Some("0o" | "0O") => Some(8),
        Some("0b" | "0B") => Some(2),
        _ => None,
    } {
        let digits = &trimmed[2..];
        if digits.is_empty() || !digits.chars().all(|d| d.is_digit(radix)) {
            return None;
        }
        let mut value = 0.0f64;
        for digit in digits.chars() {
            value = value * f64::from(radix) + f64::from(digit.to_digit(radix).unwrap_or(0));
        }
        return Some(value);
    }
    let (sign, body) = match trimmed.strip_prefix('-') {
        Some(rest) => (-1.0, rest),
        None => (1.0, trimmed.strip_prefix('+').unwrap_or(trimmed)),
    };
    if body == "Infinity" {
        return Some(sign * f64::INFINITY);
    }
    if !is_decimal_literal(body) {
        return None;
    }
    body.parse::<f64>().ok().map(|value| sign * value)
}

/// JavaScript's `\s`, which is also its `StrWhiteSpace`: the ASCII
/// blanks, the Unicode space separators, the two line separators and the
/// byte-order mark. Rust's own `char::is_whitespace` is the `White_Space`
/// property, which differs in both directions: it counts U+0085 and it
/// does not count U+FEFF. Both of those decide a scalar here.
pub(crate) fn js_space(character: char) -> bool {
    matches!(
        character,
        '\t' | '\n' | '\u{b}' | '\u{c}' | '\r' | ' ' | '\u{a0}' | '\u{1680}' | '\u{2000}'
            ..='\u{200a}'
                | '\u{2028}'
                | '\u{2029}'
                | '\u{202f}'
                | '\u{205f}'
                | '\u{3000}'
                | '\u{feff}'
    )
}

/// The ECMAScript `StrUnsignedDecimalLiteral` shape, which is narrower
/// than what Rust's own parser accepts: `inf`, `nan` and `1_0` are not
/// numbers in JavaScript.
fn is_decimal_literal(body: &str) -> bool {
    let bytes = body.as_bytes();
    let mut index = 0;
    let mut integral = 0;
    while index < bytes.len() && bytes[index].is_ascii_digit() {
        index += 1;
        integral += 1;
    }
    let mut fractional = 0;
    if index < bytes.len() && bytes[index] == b'.' {
        index += 1;
        while index < bytes.len() && bytes[index].is_ascii_digit() {
            index += 1;
            fractional += 1;
        }
    }
    if integral == 0 && fractional == 0 {
        return false;
    }
    if index < bytes.len() && (bytes[index] == b'e' || bytes[index] == b'E') {
        index += 1;
        if index < bytes.len() && (bytes[index] == b'+' || bytes[index] == b'-') {
            index += 1;
        }
        let mut exponent = 0;
        while index < bytes.len() && bytes[index].is_ascii_digit() {
            index += 1;
            exponent += 1;
        }
        if exponent == 0 {
            return false;
        }
    }
    index == bytes.len()
}

/// `parseInt(text, 10)`. The leading run it skips is JavaScript's
/// whitespace, which is not Rust's: `trim_start` would leave a
/// byte-order mark in place and strip a next-line character.
pub(crate) fn parse_int(text: &str) -> f64 {
    let trimmed = text.trim_start_matches(js_space);
    let bytes = trimmed.as_bytes();
    let mut index = 0;
    if index < bytes.len() && (bytes[index] == b'+' || bytes[index] == b'-') {
        index += 1;
    }
    let start = index;
    while index < bytes.len() && bytes[index].is_ascii_digit() {
        index += 1;
    }
    if index == start {
        return f64::NAN;
    }
    trimmed[..index].parse::<f64>().unwrap_or(f64::NAN)
}

/// `parseFloat(text)`, skipping the same JavaScript whitespace
/// `parse_int` skips.
pub(crate) fn parse_float(text: &str) -> f64 {
    let trimmed = text.trim_start_matches(js_space);
    let bytes = trimmed.as_bytes();
    let mut index = 0;
    if index < bytes.len() && (bytes[index] == b'+' || bytes[index] == b'-') {
        index += 1;
    }
    if trimmed[index..].starts_with("Infinity") {
        return if bytes.first() == Some(&b'-') {
            f64::NEG_INFINITY
        } else {
            f64::INFINITY
        };
    }
    let mut digits = 0;
    while index < bytes.len() && bytes[index].is_ascii_digit() {
        index += 1;
        digits += 1;
    }
    if index < bytes.len() && bytes[index] == b'.' {
        index += 1;
        while index < bytes.len() && bytes[index].is_ascii_digit() {
            index += 1;
            digits += 1;
        }
    }
    if digits == 0 {
        return f64::NAN;
    }
    let mantissa_end = index;
    if index < bytes.len() && (bytes[index] == b'e' || bytes[index] == b'E') {
        let mut probe = index + 1;
        if probe < bytes.len() && (bytes[probe] == b'+' || bytes[probe] == b'-') {
            probe += 1;
        }
        let exponent_start = probe;
        while probe < bytes.len() && bytes[probe].is_ascii_digit() {
            probe += 1;
        }
        if probe > exponent_start {
            index = probe;
        }
    }
    trimmed[..index.max(mantissa_end)]
        .parse::<f64>()
        .unwrap_or(f64::NAN)
}

/// `parseInt(text, 16)`. Like `parse_int` it skips JavaScript's leading
/// whitespace, takes an optional sign and then the LONGEST PREFIX that
/// reads as hexadecimal, rather than demanding the whole string: the
/// digits of a `\x`, `\u` or `\U` escape are a fixed-width window that
/// routinely holds the closing quote or the rest of the line, and the
/// canonical handler reads a number out of the window all the same.
/// `NaN` when the prefix has no digit.
pub(crate) fn parse_int_16(text: &str) -> f64 {
    let trimmed = text.trim_start_matches(js_space);
    let bytes = trimmed.as_bytes();
    let mut index = 0;
    let negative = bytes.first() == Some(&b'-');
    if index < bytes.len() && (bytes[index] == b'+' || bytes[index] == b'-') {
        index += 1;
    }
    // `parseInt` allows the `0x` prefix when the radix is 16.
    if bytes.len() > index + 1
        && bytes[index] == b'0'
        && (bytes[index + 1] == b'x' || bytes[index + 1] == b'X')
    {
        index += 2;
    }
    let start = index;
    let mut value = 0f64;
    while index < bytes.len() && bytes[index].is_ascii_hexdigit() {
        value = value * 16.0 + f64::from((bytes[index] as char).to_digit(16).expect("hex digit"));
        index += 1;
    }
    if index == start {
        return f64::NAN;
    }
    if negative {
        -value
    } else {
        value
    }
}

/// `String.fromCharCode(n)`, whose argument goes through `ToUint16`: a
/// `NaN` becomes NUL rather than an error, which is why an unreadable
/// `\x` or `\u` escape is a NUL in the canonical handler.
pub(crate) fn to_uint16(number: f64) -> u32 {
    if !number.is_finite() {
        return 0;
    }
    let truncated = number.trunc();
    let wrapped = truncated.rem_euclid(65536.0);
    wrapped as u32
}

/// `String.fromCodePoint(n)`, which THROWS a `RangeError` for anything
/// that is not a code point. `None` stands for the throw: the canonical
/// matcher does not catch it, so the token is never produced and the
/// document is refused.
pub(crate) fn from_code_point(number: f64) -> Option<u32> {
    if !number.is_finite() || number.trunc() != number {
        return None;
    }
    if !(0.0..=1_114_111.0).contains(&number) {
        return None;
    }
    Some(number as u32)
}

/// The YAML value keywords, and the three non-finite numbers that have
/// no JSON spelling.
pub(crate) fn yaml_keyword(text: &str) -> Option<Value> {
    Some(match text {
        "true" | "True" | "TRUE" | "yes" | "Yes" | "YES" | "on" | "On" | "ON" => Value::Bool(true),
        "false" | "False" | "FALSE" | "no" | "No" | "NO" | "off" | "Off" | "OFF" => {
            Value::Bool(false)
        }
        "null" | "Null" | "NULL" | "~" => Value::Null,
        ".inf" | ".Inf" | ".INF" | "+.inf" | "+.Inf" | "+.INF" => Value::Number(f64::INFINITY),
        "-.inf" | "-.Inf" | "-.INF" => Value::Number(f64::NEG_INFINITY),
        ".nan" | ".NaN" | ".NAN" => Value::Number(f64::NAN),
        _ => return None,
    })
}

// ---------------------------------------------------------------------
// Grammar references
// ---------------------------------------------------------------------

/// The value a token carries as a mapping key, resolving an alias marker
/// against the anchors recorded so far.
fn extract_key(token: &Token, context: &Context) -> Value {
    if token.tin == TIN_VL {
        if let Value::Object(map) = &token.val {
            if let Some(Value::String(name)) = map.get("__yamlAlias") {
                return match state::map_get(context, state::ANCHORS, name) {
                    Some(value) if !matches!(value, Value::Undefined) => value,
                    _ => Value::String(format!("*{name}")),
                };
            }
        }
    }
    if token.tin == TIN_ST || token.tin == TIN_TX {
        token.val.clone()
    } else {
        Value::String(token.src.as_str().to_string())
    }
}

fn token_key(rule: &Rule, index: usize, context: &Context) -> Value {
    let token = if index == 0 { rule.o0() } else { rule.o1() };
    token.map_or(Value::Undefined, |token| extract_key(token, context))
}

fn counter(rule: &Rule, name: &str) -> Option<i32> {
    rule.n.get(name).copied()
}

fn token_number(value: &Value) -> Option<f64> {
    match value {
        Value::Number(number) => Some(*number),
        _ => None,
    }
}

fn t0_number(context: &Context) -> Option<f64> {
    context.t0().and_then(|token| token_number(&token.val))
}

fn register_refs(parser: &mut Tabnas) {
    // The two lexer matchers.
    parser.imperative_lex_match_ref(lex::MATCHER, lex::yaml_matcher);
    parser.imperative_lex_match_ref(text::MATCHER, text::yaml_text);

    // Conditions.
    parser.alt_condition("@val-indent-deeper", |rule, context| {
        let Some(t0) = t0_number(context) else {
            return false;
        };
        if let Some(Value::Number(list_in)) = rule.k.get("yamlListIn") {
            if t0 <= *list_in {
                return false;
            }
        }
        match rule.k.get("yamlIn") {
            Some(Value::Number(parent)) => t0 > *parent,
            _ => true,
        }
    });
    parser.alt_condition("@val-indent-eq-parent", |rule, context| {
        let Some(t0) = t0_number(context) else {
            return false;
        };
        matches!(rule.k.get("yamlIn"), Some(Value::Number(parent)) if t0 == *parent)
    });
    parser.alt_condition("@t0-eq-in", |rule, context| {
        matches!((t0_number(context), counter(rule, "in")),
            (Some(t0), Some(indent)) if t0 == f64::from(indent))
    });
    parser.alt_condition("@t0-le-in", |rule, context| {
        matches!((t0_number(context), counter(rule, "in")),
            (Some(t0), Some(indent)) if t0 <= f64::from(indent))
    });
    parser.alt_condition("@t0-lt-in", |rule, context| {
        matches!((t0_number(context), counter(rule, "in")),
            (Some(t0), Some(indent)) if t0 < f64::from(indent))
    });
    parser.alt_condition("@o0-eq-in", |rule, _context| {
        let open = rule.o0().and_then(|token| token_number(&token.val));
        matches!((open, counter(rule, "in")),
            (Some(value), Some(indent)) if value == f64::from(indent))
    });
    parser.alt_condition("@t0-eq-map-in", |rule, context| {
        matches!((t0_number(context), rule.k.get("yamlMapIn")),
            (Some(t0), Some(Value::Number(indent))) if t0 == *indent)
    });

    // Actions.
    parser.action_with_context("@val-set-in-from-o0", |rule, _context| {
        if let Some(value) = rule.o0().and_then(|token| token_number(&token.val)) {
            rule.n_mut().insert("in".to_string(), value as i32);
        }
        Ok(())
    });
    parser.action_with_context("@val-set-null", |rule, _context| {
        set_node(rule, Value::Null);
        Ok(())
    });
    parser.action_with_context("@val-set-el-in", |rule, _context| {
        let column = rule.o0().map_or(0, |token| token.site.ci);
        rule.n_mut().insert("in".to_string(), column as i32 - 1);
        Ok(())
    });
    parser.action_with_context("@indent-plain-value", |rule, _context| {
        let node = rule.o0().map_or(Value::Undefined, |token| {
            if token.tin == TIN_ST || token.tin == TIN_TX {
                token.val.clone()
            } else {
                Value::String(token.src.as_str().to_string())
            }
        });
        set_node(rule, node);
        Ok(())
    });
    parser.action_with_context("@set-map-in", |rule, _context| {
        // `r.k.yamlMapIn = r.n.in + 2`. With no `in` counter that is
        // `undefined + 2`, which is NaN, and NaN never equals the indent
        // `@t0-eq-map-in` compares it to. A zero here would instead make
        // the comparison true at the outermost indent.
        let indent = counter(rule, "in").map_or(f64::NAN, |value| f64::from(value + 2));
        rule.k_mut()
            .insert("yamlMapIn".to_string(), Value::Number(indent));
        Ok(())
    });
    parser.action_with_context("@elem-key", |rule, context| {
        let key = token_key(rule, 0, context);
        rule.u_mut().insert("key".to_string(), key);
        Ok(())
    });
    parser.action_with_context("@implicit-null-pair", |rule, context| {
        let key = token_key(rule, 0, context);
        rule.u_mut().insert("key".to_string(), key.clone());
        map_insert(&mut rule.node.borrow_mut(), string_of(&key), Value::Null);
        Ok(())
    });
    parser.action_with_context("@qm-pairkey", |rule, context| {
        let key = token_key(rule, 1, context);
        rule.u_mut().insert("key".to_string(), key);
        Ok(())
    });
    parser.action_with_context("@qm-implicit-null-pair", |rule, context| {
        let key = token_key(rule, 1, context);
        rule.u_mut().insert("key".to_string(), key.clone());
        map_insert(&mut rule.node.borrow_mut(), string_of(&key), Value::Null);
        Ok(())
    });

    // The stream rule's actions.
    parser.action_with_context("@yaml-apply-directive", |rule, context| {
        apply_directive(rule, context);
        Ok(())
    });
    parser.action_with_context("@yaml-mark-explicit", |_rule, context| {
        ensure_meta(context);
        set_meta_flag(context, "explicit", true);
        Ok(())
    });
    parser.action_with_context("@yaml-accum-doc", |rule, context| {
        accumulate_document(rule, context);
        Ok(())
    });
    parser.action_with_context("@yaml-accum-and-directive", |rule, context| {
        accumulate_document(rule, context);
        apply_directive(rule, context);
        Ok(())
    });
    parser.action_with_context("@yaml-finalize-stream", |rule, context| {
        finalize_stream(rule, context);
        Ok(())
    });
}

// ---------------------------------------------------------------------
// The stream rule's bookkeeping
// ---------------------------------------------------------------------

fn ensure_meta(context: &mut Context) {
    if context.u.get(state::CUR_META).is_none() {
        let mut meta = IndexMap::new();
        meta.insert("directives".to_string(), Value::array(Vec::new()));
        meta.insert("explicit".to_string(), Value::Bool(false));
        meta.insert("ended".to_string(), Value::Bool(false));
        context
            .u
            .insert(state::CUR_META.to_string(), Value::object(meta));
    }
}

fn set_meta_flag(context: &mut Context, field: &str, value: bool) {
    ensure_meta(context);
    state::map_set(
        context,
        state::CUR_META,
        field.to_string(),
        Value::Bool(value),
    );
}

fn flush_meta(context: &mut Context, ended: bool) {
    ensure_meta(context);
    if ended {
        set_meta_flag(context, "ended", true);
    }
    let meta = context
        .u
        .shift_remove(state::CUR_META)
        .unwrap_or(Value::Undefined);
    state::list_push(context, state::METAS, meta);
}

fn apply_directive(rule: &mut Rule, context: &mut Context) {
    let source = rule.o0().map_or(String::new(), |token| {
        let src = token.src.as_str();
        if src.is_empty() {
            match &token.val {
                Value::String(text) => text.clone(),
                _ => String::new(),
            }
        } else {
            src.to_string()
        }
    });
    if let Some((handle, prefix)) = tag_directive(&source) {
        state::map_set(context, state::TAG_HANDLES, handle, Value::String(prefix));
    }
    ensure_meta(context);
    let mut directives = match state::map_get(context, state::CUR_META, "directives") {
        Some(Value::Array(items)) => (*items).clone(),
        _ => Vec::new(),
    };
    directives.push(Value::String(source));
    state::map_set(
        context,
        state::CUR_META,
        "directives".to_string(),
        Value::array(directives),
    );
}

/// `%TAG <handle> <prefix>`, the only directive that changes a parse.
/// The canonical pattern is `^%TAG\s+(\S+)\s+(\S+)`, so the separator
/// is JavaScript's whitespace: `is_whitespace` and `split_whitespace`
/// would read a next-line character as a separator and a byte-order mark
/// as part of a field.
fn tag_directive(source: &str) -> Option<(String, String)> {
    let rest = source.strip_prefix("%TAG")?;
    if !rest.starts_with(js_space) {
        return None;
    }
    let mut fields = rest.split(js_space).filter(|field| !field.is_empty());
    let handle = fields.next()?;
    let prefix = fields.next()?;
    Some((handle.to_string(), prefix.to_string()))
}

fn child_document(rule: &Rule) -> Option<Value> {
    (!rule.child_node.is_undefined()).then(|| rule.child_node.clone())
}

fn accumulate_document(rule: &mut Rule, context: &mut Context) {
    let value = child_document(rule).unwrap_or(Value::Null);
    state::list_push(context, state::DOCS, value);
    // The close token says whether this document ended with `...`.
    let ended = rule.c0().is_some_and(|token| token.name.as_ref() == "#DE");
    flush_meta(context, ended);
}

fn finalize_stream(rule: &mut Rule, context: &mut Context) {
    if let Some(value) = child_document(rule) {
        state::list_push(context, state::DOCS, value);
        flush_meta(context, false);
    } else if context.u.get(state::CUR_META).is_some() {
        // The final document was opened explicitly but its value
        // coalesced away: a bare `---` at end of stream, or the trailing
        // empty document of `---\n---\n---`. Restore the null the other
        // empty documents are given.
        state::list_push(context, state::DOCS, Value::Null);
        flush_meta(context, false);
    }

    let documents = state::list_all(context, state::DOCS);
    let content = match documents.len() {
        0 => Value::Undefined,
        1 => documents[0].clone(),
        _ => Value::array(documents),
    };

    let want_meta = matches!(
        context
            .options
            .plugin
            .get(PLUGIN_NAME)
            .and_then(|options| match options {
                Value::Object(map) => map.get("meta").cloned(),
                _ => None,
            }),
        Some(Value::Bool(true))
    );

    let result = if want_meta {
        let metas = state::list_all(context, state::METAS);
        let meta = match metas.len() {
            0 => Value::Undefined,
            1 => metas[0].clone(),
            _ => Value::array(metas),
        };
        let mut wrapper = IndexMap::new();
        wrapper.insert("meta".to_string(), meta);
        wrapper.insert("content".to_string(), content);
        Value::object(wrapper)
    } else {
        content
    };

    // A rotation through `r: stream` shares the start rule's node cell,
    // which is the parse root, so writing THROUGH the cell is the Rust
    // spelling of the canonical `ctx.root().node = result`.
    *rule.node.borrow_mut() = result;
}

// ---------------------------------------------------------------------
// Rule lifecycle
// ---------------------------------------------------------------------

fn wire_rules(parser: &mut Tabnas) {
    parser.define_rule("val", |spec| {
        spec.add_ao(|rule, context| {
            let pending = state::list_take(context, state::PENDING_ANCHORS);
            if !pending.is_empty() {
                // Only whether the open node was a scalar is ever read back,
                // so a container is recorded as `Undefined`: a clone of an
                // inherited container is a second handle on the parent's
                // accumulator (see below).
                let open_node = match &*rule.node.borrow() {
                    node if is_container(node) => Value::Undefined,
                    node => node.clone(),
                };
                rule.u_mut()
                    .insert("yamlAnchors".to_string(), Value::array(pending));
                rule.u_mut()
                    .insert("yamlAnchorOpenNode".to_string(), open_node);
            }
            // A YAML alt that opens on `#IN` or `#EL` (pushing `indent` or
            // `yamlBlockList`, or backing up for an empty value) leaves this val's node as the cell
            // it inherited from its parent: the parent MAP when the value is
            // a pair's and starts on the next line. jsonic's val before-close
            // stashes a clone of that node (`u.openval`), a second `Arc` on
            // the map the pair is about to insert into, so every insert
            // copied the whole map and a mapping of N such values parsed in
            // O(N^2). jsonic's own alts reset the node when they open; these
            // do the same. The value is the child's either way.
            // The empty-value alt (`#IN`, backed up) is the same shape: the
            // val ends with no value, and the stash held the map while the
            // pair inserted that absence.
            let yaml_alt = matches!(
                rule.o0().map(|token| token.name.as_ref()),
                Some("#EL" | "#IN")
            );
            if yaml_alt && is_container(&rule.node.borrow()) {
                set_node(rule, Value::Undefined);
            }
        });
        spec.add_bc(|rule, _context| {
            if matches!(rule.u.get("yamlEmpty"), Some(Value::Bool(true))) {
                set_node(rule, Value::Undefined);
            }
        });
        spec.add_ac(|rule, context| {
            // An alias whose anchor was not yet recorded when it lexed.
            let alias = match &*rule.node.borrow() {
                Value::Object(map) => match map.get("__yamlAlias") {
                    Some(Value::String(name)) => Some(name.clone()),
                    _ => None,
                },
                _ => None,
            };
            if let Some(name) = alias {
                let resolved = state::map_get(context, state::ANCHORS, &name).map_or(
                    Value::Undefined,
                    |value| {
                        if is_container(&value) {
                            deep_copy(&value)
                        } else {
                            value
                        }
                    },
                );
                set_node(rule, resolved);
            }

            let Some(Value::Array(anchors)) = rule.u.get("yamlAnchors").cloned() else {
                return;
            };
            let open_node = rule
                .u
                .get("yamlAnchorOpenNode")
                .cloned()
                .unwrap_or(Value::Undefined);
            let node = rule.node.borrow().clone();
            for anchor in anchors.iter() {
                let Value::Object(entry) = anchor else {
                    continue;
                };
                let inline = matches!(entry.get("i"), Some(Value::Bool(true)));
                let Some(Value::String(name)) = entry.get("n") else {
                    continue;
                };
                // An inline anchor that named a scalar keeps that scalar:
                // the container this val ended up holding belongs to a
                // later, deeper value.
                if inline
                    && !matches!(open_node, Value::Undefined | Value::Null)
                    && !is_container(&open_node)
                    && is_container(&node)
                {
                    continue;
                }
                let value = if is_container(&node) {
                    deep_copy(&node)
                } else {
                    node.clone()
                };
                state::map_set(context, state::ANCHORS, name.clone(), value);
            }
        });
    });

    parser.define_rule("indent", |spec| {
        spec.add_bc(|rule, _context| {
            if !rule.child_node.is_undefined() {
                let child = rule.child_node.clone();
                set_node(rule, child);
            }
        });
    });

    parser.define_rule("yamlBlockList", |spec| {
        spec.add_bo(|rule, _context| {
            // A fresh cell, as `r.node = []` installs a fresh array.
            // Every rotation of this rule shares the cell it installs, so
            // the elements all land in one list, which is what the
            // canonical port passes along in `k.yamlBlockArr`.
            set_node(rule, Value::array(Vec::new()));
            set_indent_key(rule, "yamlListIn");
        });
        spec.add_bc(|rule, _context| {
            let value = if rule.child_node.is_undefined() {
                Value::Null
            } else {
                rule.child_node.clone()
            };
            list_push(&mut rule.node.borrow_mut(), value);
        });
    });

    parser.define_rule("yamlBlockElem", |spec| {
        spec.add_bc(|rule, _context| {
            let value = if rule.child_node.is_undefined() {
                Value::Null
            } else {
                rule.child_node.clone()
            };
            list_push(&mut rule.node.borrow_mut(), value);
        });
    });

    parser.define_rule("list", |spec| {
        spec.add_bo(|rule, _context| {
            set_indent_key(rule, "yamlListIn");
            // A YAML block sequence reaches `list` through the indent
            // rule's `#EL` alternate, with no `[` and no implicit-list
            // promotion, so neither of jsonic's array builders runs and
            // the node is still the inherited parent container. Allocate
            // the array here so the element pushes land in a list.
            if rule
                .parent_rule
                .as_ref()
                .is_some_and(|parent| parent.name.as_ref() == "indent")
            {
                set_node(rule, Value::array(Vec::new()));
            }
        });
    });

    parser.define_rule("map", |spec| {
        spec.add_bo(|rule, _context| {
            if counter(rule, "in").is_none() {
                rule.n_mut().insert("in".to_string(), 0);
            }
            set_indent_key(rule, "yamlIn");
        });
        spec.add_ac(|rule, _context| {
            apply_merge_keys(rule);
        });
    });

    parser.define_rule("yamlElemMap", |spec| {
        spec.add_bo(|rule, _context| {
            set_node(rule, Value::object(IndexMap::new()));
        });
        spec.add_bc(|rule, _context| {
            store_elem_pair(rule);
        });
    });

    parser.define_rule("yamlElemPair", |spec| {
        spec.add_bc(|rule, _context| {
            store_elem_pair(rule);
        });
    });
}

/// `r.k.<name> = r.n.in`, including when there is no `in` counter: the
/// canonical assignment writes `undefined` there, and every reader takes
/// that as "no enclosing indent". Writing a zero instead would make a
/// block sequence at the outermost indent look nested inside itself.
fn set_indent_key(rule: &mut Rule, name: &str) {
    let indent =
        counter(rule, "in").map_or(Value::Undefined, |value| Value::Number(f64::from(value)));
    rule.k_mut().insert(name.to_string(), indent);
}

fn store_elem_pair(rule: &mut Rule) {
    let Some(key) = rule.u.get("key").cloned() else {
        return;
    };
    if matches!(key, Value::Null | Value::Undefined) {
        return;
    }
    // A pair whose value never arrived (`- i: ` at end of source) is
    // assigned `undefined` in the canonical implementation, which makes
    // the key present but drops it from every serialization. This
    // engine's values turn `Undefined` into null on the way out, so the
    // entry is left out instead and the serialized map agrees.
    if rule.child_node.is_undefined() {
        return;
    }
    let child = rule.child_node.clone();
    map_insert(&mut rule.node.borrow_mut(), string_of(&key), child);
}

/// Resolve a `<<` merge key in place: the merged entries are appended
/// after the explicit ones, and an explicit key always wins.
fn apply_merge_keys(rule: &mut Rule) {
    let mut node = rule.node.borrow_mut();
    let Some(entries) = map_entries(&node) else {
        return;
    };
    let Some(merge) = entries.get("<<").cloned() else {
        return;
    };
    let sources = match &merge {
        Value::Array(items) => (**items).clone(),
        Value::ListRef(list) => list.value.clone(),
        other => vec![other.clone()],
    };
    match &mut *node {
        Value::Object(map) => {
            Arc::make_mut(map).shift_remove("<<");
        }
        Value::MapRef(map) => {
            Arc::make_mut(map).value.shift_remove("<<");
        }
        _ => {}
    }
    for source in sources {
        let Some(source_entries) = map_entries(&source) else {
            continue;
        };
        let additions: Vec<(String, Value)> = source_entries
            .iter()
            .filter(|(key, _)| {
                map_entries(&node).is_some_and(|entries| !entries.contains_key(*key))
            })
            .map(|(key, value)| (key.clone(), value.clone()))
            .collect();
        for (key, value) in additions {
            map_insert(&mut node, key, value);
        }
    }
}

// ---------------------------------------------------------------------
// Installation
// ---------------------------------------------------------------------

/// The lexer and option overlay this plugin needs, plus the `stream`
/// rule, which the canonical implementation also declares in code rather
/// than in the shared grammar file.
fn options_document() -> serde_json::Value {
    json!({
        "rule": {
            "stream": {
                "open": [
                    // A directive line: apply it, then look again.
                    { "s": "#DR", "a": "@yaml-apply-directive", "r": "stream", "g": "yaml" },
                    // An explicit document start: push val for its content.
                    { "s": "#DS", "a": "@yaml-mark-explicit", "p": "val", "g": "yaml" },
                    // A `...` with no document open terminates nothing, so
                    // it produces no document. Consume it and look again.
                    { "s": "#DE", "r": "stream", "g": "yaml" },
                    // End of source: close immediately.
                    { "s": "#ZZ", "b": 1, "g": "yaml" },
                    // The implicit first document.
                    { "p": "val", "g": "yaml" }
                ],
                "close": [
                    { "s": "#ZZ", "a": "@yaml-finalize-stream", "g": "yaml" },
                    { "s": "#DR", "a": "@yaml-accum-and-directive", "r": "stream", "g": "yaml" },
                    { "s": "#DE", "a": "@yaml-accum-doc", "r": "stream", "g": "yaml" },
                    { "s": "#DS", "b": 1, "a": "@yaml-accum-doc", "r": "stream", "g": "yaml" }
                ]
            }
        },
        "options": {
            // A bare colon is not a YAML token; `: ` is, and the matcher
            // emits it.
            "fixed": { "token": { "#CL": null } },
            // A colon still ends unquoted text.
            "ender": ":",
            // YAML's quoting is not the engine's: both forms are lexed by
            // the YAML matcher, and a backtick is not a quote at all.
            "string": { "chars": "" },
            "lex": {
                "match": {
                    // Below the first built-in band, so YAML syntax is
                    // seen before the fixed-token matcher.
                    "yaml": { "order": 5e5, "make": "@yaml-matcher" },
                    // Between the number matcher and the text matcher,
                    // which is where `options.text.check` runs.
                    "yamlText": { "order": 7.5e6, "make": "@yaml-text" }
                }
            },
            // A document stream is the entry point, and it opens into the
            // shared `val` rule.
            "rule": { "start": "stream" }
        }
    })
}

/// jsonic's numbers are doubles, so `b: 2` arrives as `2.0`; the
/// alternate decoder reads an integer field as an integer. Put every
/// whole number back into integer form.
fn integral_numbers(value: serde_json::Value) -> serde_json::Value {
    match value {
        serde_json::Value::Number(number) => match number.as_f64() {
            Some(float) if float.fract() == 0.0 && float.abs() < 9.0e15 => json!(float as i64),
            _ => serde_json::Value::Number(number),
        },
        serde_json::Value::Array(items) => {
            serde_json::Value::Array(items.into_iter().map(integral_numbers).collect())
        }
        serde_json::Value::Object(fields) => serde_json::Value::Object(
            fields
                .into_iter()
                .map(|(key, value)| (key, integral_numbers(value)))
                .collect(),
        ),
        other => other,
    }
}

/// The grammar document, parsed from the embedded jsonic text exactly as
/// the TypeScript and Go ports parse their copies of it.
fn grammar_document() -> Result<serde_json::Value, PluginError> {
    let parsed = tabnas_jsonic::parse(GRAMMAR_TEXT)
        .map_err(|error| PluginError(format!("yaml: cannot parse the grammar text: {error}")))?;
    let document = integral_numbers(parsed.to_json());
    if document
        .get("rule")
        .and_then(serde_json::Value::as_object)
        .is_none()
    {
        return Err(PluginError(
            "yaml: the grammar text has no `rule` table".to_string(),
        ));
    }
    Ok(document)
}

/// Install YAML on a jsonic-enabled `parser`.
///
/// ```
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut parser = tabnas_jsonic::make();
///     tabnas_yaml::yaml(&mut parser, &tabnas_yaml::YamlOptions::default())?;
///     assert_eq!(parser.parse("a: 1")?.to_string(), r#"{"a":1}"#);
///     Ok(())
/// }
/// ```
pub fn yaml(parser: &mut Tabnas, options: &YamlOptions) -> Result<(), PluginError> {
    // Guard against re-entry while options are re-applied, which would
    // install the grammar twice.
    if parser.decoration::<bool>(INSTALLED).is_some() {
        return Ok(());
    }
    // This plugin reshapes jsonic's rules rather than declaring a whole
    // grammar, so an instance without them earns a clear refusal now
    // instead of a grammar that can parse nothing later.
    if !parser.rule_names().iter().any(|name| name == "val") {
        return Err(PluginError(
            "yaml: the jsonic grammar is not installed on this instance; \
             install tabnas_jsonic first"
                .to_string(),
        ));
    }
    parser.decorate(INSTALLED, true);
    parser.set_plugin_options(PLUGIN_NAME, options.to_value());

    // The tokens the matcher emits by name, allocated before any grammar
    // names them.
    for name in ["#IN", "#EL", "#QM", "#DS", "#DE", "#DR"] {
        parser.token(name);
    }

    register_refs(parser);

    let spec = GrammarSpec::from_value(options_document())
        .map_err(|error| PluginError(format!("yaml: invalid option document: {error}")))?;
    parser
        .grammar(&spec)
        .map_err(|error| PluginError(format!("yaml: cannot apply the options: {error}")))?;

    let spec = GrammarSpec::from_value(grammar_document()?)
        .map_err(|error| PluginError(format!("yaml: invalid grammar document: {error}")))?;
    parser
        .grammar(&spec)
        .map_err(|error| PluginError(format!("yaml: cannot apply the grammar: {error}")))?;

    wire_rules(parser);

    // Replaces jsonic's guard, which counts only jsonic's own containers
    // (see `DEPTH_GUARD`). A guard rather than the parse budget, which is
    // one slot a caller's `parse_budget` replaces: the limit holds
    // whatever budget the caller sets.
    parser.parse_guard(DEPTH_GUARD, within_depth_limit);
    Ok(())
}

/// How many containers a parse may hold before it is refused, with the
/// engine's `cancel` code: jsonic's number, so a flow collection, which
/// is jsonic's, is bounded where it always was.
///
/// The engine parses iteratively, but displaying, converting or dropping
/// a `Value` walks the tree with the call stack, so an unbounded document
/// ends the caller's process rather than returning an error: a compact
/// block sequence 64,000 levels deep parsed, and then overflowed a 2 MiB
/// stack when it was dropped. TypeScript and Go have no limit, which
/// `../DIVERGENCE.md` records.
const DEPTH_LIMIT: usize = 127;

/// The name the depth check is installed under, as a parse guard.
///
/// It is the name jsonic installs its own check under, so this one
/// replaces it. jsonic's counts its `map` and `list` rules, which is all a
/// flow collection or an indented block collection uses, but a compact
/// block sequence (`- - - x`) nests through YAML's own rules, and under
/// jsonic's check it went unbounded.
const DEPTH_GUARD: &str = "depth";

/// Whether a rule of this name holds a container: jsonic's `map` and
/// `list`, and YAML's own block collections. A block sequence opens as
/// `yamlBlockList` and, after its first entry, is replaced by
/// `yamlBlockElem`; a mapping that starts in a sequence entry opens as
/// `yamlElemMap` and is replaced by `yamlElemPair` after its first pair.
/// Each replacement shares the container's cell and takes its place on
/// the stack, so a container is counted once under whichever name it has
/// reached. jsonic's `pair` and `elem`, and `indent`, hold none.
fn is_container_rule(name: &str) -> bool {
    matches!(
        name,
        "map" | "list" | "yamlBlockList" | "yamlBlockElem" | "yamlElemMap" | "yamlElemPair"
    )
}

/// How many containers are open: those on the rule stack, and the rule
/// the loop is working on, which the engine hands over apart from them.
///
/// Counted afresh at each step. Every level holds a container and a few
/// other rules, so the stack a step walks is bounded by the limit itself.
fn depth(context: &Context) -> usize {
    let ancestors = context
        .rule_stack
        .iter()
        .filter(|rule| is_container_rule(&rule.name))
        .count();
    let current = usize::from(
        context
            .rule
            .as_ref()
            .is_some_and(|rule| is_container_rule(&rule.name)),
    );
    ancestors + current
}

/// The depth guard: [`DEPTH_LIMIT`] containers parse, the next one is
/// refused.
fn within_depth_limit(context: &Context) -> bool {
    depth(context) <= DEPTH_LIMIT
}

/// The plugin form of [`yaml`], for [`Tabnas::use_plugin`]. Installed
/// this way the grammar is re-applied to derived instances, as every
/// native plugin is.
///
/// ```
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut parser = tabnas_jsonic::make();
///     parser.use_plugin(tabnas_yaml::plugin(), None)?;
///     assert_eq!(parser.parse("- a\n- b")?.to_string(), r#"["a","b"]"#);
///     Ok(())
/// }
/// ```
pub fn plugin() -> Plugin {
    Plugin::new(PLUGIN_NAME, |parser, options| {
        let meta = matches!(
            match options {
                Value::Object(map) => map.get("meta").cloned(),
                _ => None,
            },
            Some(Value::Bool(true))
        );
        yaml(parser, &YamlOptions { meta })
    })
    .with_defaults(YamlOptions::default().to_value())
}

/// Build a YAML parser with caller options.
///
/// ```
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let parser = tabnas_yaml::make_with(tabnas_yaml::YamlOptions { meta: true });
///     let value = parser.parse("---\na: 1\n...")?;
///     assert_eq!(
///         value.to_string(),
///         r#"{"meta":{"directives":[],"explicit":true,"ended":true},"content":{"a":1}}"#
///     );
///     Ok(())
/// }
/// ```
#[must_use]
pub fn make_with(options: YamlOptions) -> Tabnas {
    let mut parser = tabnas_jsonic::make();
    parser
        .use_plugin(plugin(), Some(options.to_value()))
        .expect("the yaml grammar documents are fixed and valid");
    parser
}

/// Build a YAML parser with the default options.
///
/// ```
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let parser = tabnas_yaml::make();
///     assert_eq!(parser.parse("a:\n  b: 1")?.to_string(), r#"{"a":{"b":1}}"#);
///     Ok(())
/// }
/// ```
#[must_use]
pub fn make() -> Tabnas {
    make_with(YamlOptions::default())
}

/// Parse a YAML source string with the shared default parser.
///
/// The parser is built once, on first use, and reused after that, as the
/// other two runtimes do. Reuse is safe: [`Tabnas::parse`] takes `&self`
/// and builds a fresh parse context per call, and every piece of
/// per-parse state this grammar keeps lives in that context.
///
/// ```
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let value = tabnas_yaml::parse("- 1\n- 2\n")?;
///     assert_eq!(value.to_string(), "[1,2]");
///     Ok(())
/// }
/// ```
pub fn parse(src: &str) -> Result<Value, YamlError> {
    static DEFAULT: OnceLock<Tabnas> = OnceLock::new();
    DEFAULT.get_or_init(make).parse(src)
}

/// The plugin's manifest, `tabnas.plugin.json`, as the repository carries
/// it. Its `translate` object is what a host that translates reads: the
/// shape YAML is read as and written from (`tree`), the file that holds
/// the render, and the sentences that say what the render does not keep.
/// The crate embeds its own copy, `translate/manifest.json`, since a
/// packaged crate holds nothing outside `rs/`; `tests/translate_test.rs`
/// holds the copy to the file.
///
/// ```
/// assert!(tabnas_yaml::manifest_text().contains("\"translate\""));
/// ```
pub fn manifest_text() -> &'static str {
    include_str!("../translate/manifest.json")
}

/// YAML's render, `alchemy/render.alc`, the file the manifest's
/// `translate.render` names: a library of alchemy definitions, with no
/// `export`, whose entry point `yaml-render` writes a tree's events as one
/// YAML document. A host links it with its own program. The crate embeds
/// its own copy, `translate/render.alc`, held to the file as the
/// manifest's is.
///
/// ```
/// assert!(tabnas_yaml::render_text().contains("def yaml-render [input]"));
/// ```
pub fn render_text() -> &'static str {
    include_str!("../translate/render.alc")
}