usage-lib 6.3.0

Library for working with usage specs
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
use std::{
    num::{ParseFloatError, ParseIntError},
    sync::Arc,
};

use crate::kdl::Severity;
use crate::miette::SourceSpan;

use num_traits::CheckedMul;
use winnow::{
    ascii::{digit1, hex_digit1, oct_digit1, Caseless},
    combinator::{
        alt, cut_err, empty, eof, fail, not, opt, peek, preceded, repeat, repeat_till, separated,
        terminated, trace,
    },
    error::{AddContext, ErrMode, FromExternalError, FromRecoverableError, ParserError},
    prelude::*,
    stream::{AsChar, Location, Recover, Recoverable, Stream},
    token::{any, none_of, one_of, take_while},
    LocatingSlice,
};

use crate::kdl::{
    KdlDiagnostic, KdlDocument, KdlDocumentFormat, KdlEntry, KdlEntryFormat, KdlError,
    KdlIdentifier, KdlNode, KdlNodeFormat, KdlValue,
};

type Input<'a> = Recoverable<LocatingSlice<&'a str>, ErrMode<KdlParseError>>;
type PResult<T> = winnow::ModalResult<T, KdlParseError>;

pub(crate) fn try_parse<'a, P: ModalParser<Input<'a>, T, KdlParseError>, T>(
    mut parser: P,
    input: &'a str,
) -> Result<T, KdlError> {
    let (_, maybe_val, errs) = parser.recoverable_parse(LocatingSlice::new(input));
    if let (Some(v), true) = (maybe_val, errs.is_empty()) {
        Ok(v)
    } else {
        Err(failure_from_errs(errs, input))
    }
}

pub(crate) fn failure_from_errs(errs: Vec<ErrMode<KdlParseError>>, input: &str) -> KdlError {
    let src = Arc::new(String::from(input));
    KdlError {
        input: src.clone(),
        source_name: String::new(),
        diagnostics: errs
            .into_iter()
            // The parser is only called with &str so this should never panic.
            .map(|e| e.into_inner().unwrap())
            .map(|e| KdlDiagnostic {
                input: src.clone(),
                span: e.span.unwrap_or_else(|| (0usize..0usize).into()),
                message: e
                    .message
                    .or_else(|| e.label.clone().map(|l| format!("Expected {l}"))),
                label: e.label.map(|l| format!("not {l}")),
                help: e.help,
                severity: Severity::Error,
            })
            .collect(),
    }
}

#[derive(Debug, Clone, Default, Eq, PartialEq)]
struct KdlParseContext {
    message: Option<String>,
    label: Option<String>,
    help: Option<String>,
    severity: Option<Severity>,
}

impl KdlParseContext {
    fn msg(mut self, txt: impl AsRef<str>) -> Self {
        self.message = Some(txt.as_ref().to_string());
        self
    }

    fn lbl(mut self, txt: impl AsRef<str>) -> Self {
        self.label = Some(txt.as_ref().to_string());
        self
    }

    fn hlp(mut self, txt: impl AsRef<str>) -> Self {
        self.help = Some(txt.as_ref().to_string());
        self
    }

    // fn sev(mut self, severity: Severity) -> Self {
    //     self.severity = Some(severity);
    //     self
    // }
}

fn cx() -> KdlParseContext {
    Default::default()
}

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub(crate) struct KdlParseError {
    pub(crate) message: Option<String>,
    pub(crate) span: Option<SourceSpan>,
    pub(crate) label: Option<String>,
    pub(crate) help: Option<String>,
    pub(crate) severity: Option<Severity>,
}

impl<I: Stream> ParserError<I> for KdlParseError {
    type Inner = Self;
    fn from_input(_input: &I) -> Self {
        Self {
            message: None,
            span: None,
            label: None,
            help: None,
            severity: None,
        }
    }

    fn append(self, _input: &I, _token_start: &<I as Stream>::Checkpoint) -> Self {
        self
    }

    fn into_inner(self) -> Result<Self, Self> {
        Ok(self)
    }
}

impl<I: Stream> AddContext<I, KdlParseContext> for KdlParseError {
    fn add_context(
        mut self,
        _input: &I,
        _token_start: &<I as Stream>::Checkpoint,
        ctx: KdlParseContext,
    ) -> Self {
        self.message = ctx.message.or(self.message);
        self.label = ctx.label.or(self.label);
        self.help = ctx.help.or(self.help);
        self.severity = ctx.severity.or(self.severity);
        self
    }
}

impl<'a> FromExternalError<Input<'a>, ParseIntError> for KdlParseError {
    fn from_external_error(_: &Input<'a>, e: ParseIntError) -> Self {
        Self {
            span: None,
            message: Some(format!("{e}")),
            label: Some("invalid integer".into()),
            help: None,
            severity: Some(Severity::Error),
        }
    }
}

impl<'a> FromExternalError<Input<'a>, ParseFloatError> for KdlParseError {
    fn from_external_error(_input: &Input<'a>, e: ParseFloatError) -> Self {
        Self {
            span: None,
            label: Some("invalid float".into()),
            help: None,
            message: Some(format!("{e}")),
            severity: Some(Severity::Error),
        }
    }
}

struct NegativeUnsignedError;

impl<'a> FromExternalError<Input<'a>, NegativeUnsignedError> for KdlParseError {
    fn from_external_error(_input: &Input<'a>, _e: NegativeUnsignedError) -> Self {
        Self {
            span: None,
            message: Some("Tried to parse a negative number as an unsigned integer".into()),
            label: Some("negative unsigned int".into()),
            help: None,
            severity: Some(Severity::Error),
        }
    }
}

impl<I: Stream + Location> FromRecoverableError<I, Self> for KdlParseError {
    #[inline]
    fn from_recoverable_error(
        token_start: &<I as Stream>::Checkpoint,
        _err_start: &<I as Stream>::Checkpoint,
        input: &I,
        mut e: Self,
    ) -> Self {
        e.span = e
            .span
            .or_else(|| Some(span_from_checkpoint(input, token_start)));
        e
    }
}

fn span_from_checkpoint<I: Stream + Location>(
    input: &I,
    start: &<I as Stream>::Checkpoint,
) -> SourceSpan {
    let offset = input.offset_from(start);
    ((input.current_token_start() - offset)..input.current_token_start()).into()
}

// This is just like the standard .resume_after(), except we only resume on Cut errors.
fn resume_after_cut<Input, Output, Error, ParseNext, ParseRecover>(
    mut parser: ParseNext,
    mut recover: ParseRecover,
) -> impl Parser<Input, Option<Output>, Error>
where
    Input: Stream + Recover<Error>,
    Error: FromRecoverableError<Input, Error> + ParserError<Input>,
    ParseNext: Parser<Input, Output, Error>,
    ParseRecover: Parser<Input, (), Error>,
{
    trace("resume_after_cut", move |input: &mut Input| {
        resume_after_cut_inner(&mut parser, &mut recover, input)
    })
}

fn resume_after_cut_inner<P, R, I, O, E>(
    parser: &mut P,
    recover: &mut R,
    i: &mut I,
) -> Result<Option<O>, E>
where
    P: Parser<I, O, E>,
    R: Parser<I, (), E>,
    I: Stream,
    I: Recover<E>,
    E: FromRecoverableError<I, E> + ParserError<I>,
{
    let token_start = i.checkpoint();
    let mut err = match parser.parse_next(i) {
        Ok(o) => {
            return Ok(Some(o));
        }
        Err(e) if e.is_incomplete() => return Err(e),
        Err(e) if e.is_backtrack() => return Err(e),
        Err(err) => err,
    };
    let err_start = i.checkpoint();
    if recover.parse_next(i).is_ok() {
        if let Err(err_) = i.record_err(&token_start, &err_start, err) {
            err = err_;
        } else {
            return Ok(None);
        }
    }

    i.reset(&err_start);
    err = E::from_recoverable_error(&token_start, &err_start, i, err);
    Err(err)
}
/// `document := bom? nodes`
pub(crate) fn document(input: &mut Input<'_>) -> PResult<KdlDocument> {
    let leading_bom = opt(bom.take()).parse_next(input)?;
    let mut doc = nodes.parse_next(input)?;
    loop {
        let badend = resume_after_cut(
            cut_err(eof).context(cx().lbl("EOF").msg("Expected end of document")),
            any.void(),
        )
        .parse_next(input)?
        .is_none();
        if !badend {
            break;
        }
        opt(bom).void().parse_next(input)?;
        nodes.void().parse_next(input)?;
    }
    if let Some(bom) = leading_bom {
        if let Some(fmt) = doc.format_mut() {
            fmt.leading = format!("{bom}{}", fmt.leading);
        }
    }
    Ok(doc)
}

/// `nodes := (line-space* node)* line-space*`
fn nodes(input: &mut Input<'_>) -> PResult<KdlDocument> {
    let mut leading = repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
        .map(|()| ())
        .take()
        .parse_next(input)?;
    let _start = input.checkpoint();
    let mut ns: Vec<KdlNode> = separated(
        0..,
        node,
        alt((node_terminator.void(), (eof.void(), any.void()).void())),
    )
    .parse_next(input)?;
    let _span = span_from_checkpoint(input, &_start);
    opt(node_terminator).parse_next(input)?;
    let trailing = repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
        .map(|()| ())
        .take()
        .parse_next(input)?;

    // If there is a node, let it have the leading format
    // This gives more consistent behavior
    if let Some(first_node) = ns.get_mut(0) {
        if let Some(first_node_format) = first_node.format_mut() {
            first_node_format.leading = leading.into();
            leading = "";
        }
    }

    Ok(KdlDocument {
        nodes: ns,
        format: Some(KdlDocumentFormat {
            leading: leading.into(),
            trailing: trailing.into(),
        }),
        span: _span,
    })
}

/// base-node := slashdash? type? node-space* string
///      (node-space+ slashdash? node-prop-or-arg)*
///      (node-space+ slashdash node-children)*
///      (node-space+ node-children)?
///      (node-space+ slashdash node-children)*
///      node-space*
/// node := base-node node-space* node-terminator
/// final-node := base-node node-space* node-terminator?
fn node(input: &mut Input<'_>) -> PResult<KdlNode> {
    let leading = repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
        .map(|()| ())
        .take()
        .parse_next(input)?;
    let mut nd = base_node.parse_next(input)?;
    if let Some(fmt) = nd.format_mut() {
        fmt.leading = leading.into();
    }
    Ok(nd)
}

fn base_node(input: &mut Input<'_>) -> PResult<KdlNode> {
    trace("children closing check", not(alt(("}".void(), eof.void())))).parse_next(input)?;
    let _start = input.checkpoint();
    let open_curly = resume_after_cut(
        cut_err(not("{").context(
            cx().msg("Found child block instead of node name")
                .lbl("node name")
                .hlp("Did you forget to add the node name itself? Or perhaps terminated the node before its child block?"))),
        "{".void(),
    )
    .parse_next(input)?;
    if open_curly.is_none() {
        // If we got a weird misplaced `{`, we consume the "child block" here,
        // because otherwise the error message is going to include the entire
        // child block as its span, but we only want to point to the offending
        // curly.
        input.reset(&_start);
        node_children.parse_next(input)?;
        opt(slashdashed_children).parse_next(input)?;
        peek(opt(node_terminator)).parse_next(input)?;
        // We also return a fake node here, for good measure.
        return Ok(KdlNode::new("<<BAD_NODE>>"));
    }
    let ty = opt(ty).parse_next(input)?;
    let after_ty = node_space0.take().parse_next(input)?;
    let _before_ident = input.checkpoint();
    let name = resume_after_cut(cut_err(identifier).context(
        cx().msg("Found invalid node name")
                                  .lbl("node name")
                                  .hlp("This can be any string type, including a quoted, raw, or multiline string, as well as a plain identifier string.")

    ), badval)
        .parse_next(input)?
        .unwrap_or_else(|| KdlIdentifier::from("/BAD_IDENT\\"));
    let name_is_valid = name.repr.as_ref().map(|s| s.is_empty()) != Some(true);
    // resume_after_cut() only picks up context from parsers passed into it. In
    // order to add an error that's more specific about us wanting a _node name_
    // here, we have to do some shenanigans with a "fake" parse here.
    // While this does result in double errors, I think it's still useful to get
    // _both_ the error message for a string/ident parser error _and_ the error
    // message for a node name being expected.
    if !name_is_valid {
        resume_after_cut((|input: &mut Input<'_>| -> PResult<()> {
                Err(ErrMode::Cut(KdlParseError {
                   span: Some(span_from_checkpoint(input, &_before_ident)),
                   ..Default::default()
                }))
            }).context(cx().msg("Found invalid node name")
                          .lbl("node name")
                          .hlp("This can be any string type, including a quoted, raw, or multiline string, as well as a plain identifier string.")),
        empty).parse_next(input)?;
    }
    let entries = repeat(
        0..,
        (peek(node_space1), node_entry).map(|(_, e): ((), _)| e),
    )
    .map(|e: Vec<Option<KdlEntry>>| e.into_iter().flatten().collect::<Vec<KdlEntry>>())
    .parse_next(input)?;
    let children = opt((
        before_node_children.take(),
        trace("node children", node_children),
    ))
    .parse_next(input)?;
    let (before_terminator, terminator) = if children.is_some() {
        (
            opt(slashdashed_children).take(),
            peek(opt(node_terminator).take()),
        )
            .parse_next(input)?
    } else {
        (
            before_node_children.take(),
            peek(opt(node_terminator).take()),
        )
            .parse_next(input)?
    };
    node_space0.parse_next(input)?;
    let (before_inner_ty, ty, after_inner_ty) = ty.unwrap_or_default();
    let (before_children, children) = children
        .map(|(before_children, children)| (before_children.into(), Some(children)))
        .unwrap_or(("".into(), None));
    Ok(KdlNode {
        ty,
        name,
        entries,
        children,
        format: Some(KdlNodeFormat {
            before_ty_name: before_inner_ty.into(),
            after_ty_name: after_inner_ty.into(),
            after_ty: after_ty.into(),
            before_children,
            before_terminator: before_terminator.into(),
            terminator: terminator.into(),
            ..Default::default()
        }),
        span: span_from_checkpoint(input, &_start),
    })
}
pub(crate) fn padded_node(input: &mut Input<'_>) -> PResult<KdlNode> {
    let ((mut node, _terminator, trailing), _span) = (
        node,
        opt(node_terminator),
        repeat(0.., alt((line_space, node_space)))
            .map(|_: ()| ())
            .take(),
    )
        .with_span()
        .parse_next(input)?;
    if let Some(fmt) = node.format_mut() {
        fmt.trailing = trailing.into();
    }
    {
        node.span = _span.into();
    }
    Ok(node)
}

pub(crate) fn padded_node_entry(input: &mut Input<'_>) -> PResult<KdlEntry> {
    let ((leading, entry, trailing), _span) = (
        repeat(0.., line_space).map(|_: ()| ()).take(),
        trace("node entry", node_entry),
        repeat(0.., alt((line_space, node_space)))
            .map(|_: ()| ())
            .take(),
    )
        .with_span()
        .parse_next(input)?;
    if let Some(entry) = entry.map(|mut val| {
        if let Some(fmt) = val.format_mut() {
            fmt.leading = format!("{leading}{}", fmt.leading);
            fmt.trailing = format!("{}{trailing}", fmt.trailing);
        }
        {
            val.span = _span.into();
        }
        val
    }) {
        Ok(entry)
    } else {
        fail.parse_next(input)?
    }
}

/// `node-prop-or-arg := prop | value`
/// `prop := string optional-node-space equals-sign optional-node-space value`
fn node_entry(input: &mut Input<'_>) -> PResult<Option<KdlEntry>> {
    let leading = (node_space0, opt((slashdashed_entries, node_space1)))
        .take()
        .parse_next(input)?;
    let _start = input.checkpoint();
    let maybe_ident = trace("prop name or string val", opt(identifier)).parse_next(input)?;
    let ident_was_parsed = maybe_ident.is_some();
    let after_key = if ident_was_parsed {
        opt((node_space0.take(), equals_sign))
            .parse_next(input)?
            .map(|(after_key, _)| after_key)
    } else {
        None
    };
    let entry = if let Some(after_key) = after_key {
        let (after_eq, value) = (
            node_space0.take(),
            cut_err(value.context(cx().lbl("property value"))),
        )
            .parse_next(input)?;
        value.map(|mut value| {
            value.name = maybe_ident;
            if let Some(fmt) = value.format_mut() {
                fmt.after_key = after_key.into();
                fmt.after_eq = after_eq.into();
            }
            value
        })
    } else if let Some(ident) = maybe_ident {
        // It was ambiguous, but this ident is actually a value.
        Some(KdlEntry {
            format: Some(KdlEntryFormat {
                value_repr: ident.repr.unwrap_or_else(|| ident.value.clone()),
                ..Default::default()
            }),
            value: KdlValue::String(ident.value),
            name: None,
            ty: None,
            span: (0..0).into(),
        })
    } else {
        trace("non-string value", resume_after_cut(value, badval))
            .parse_next(input)?
            .flatten()
    };
    Ok(entry.map(|mut value| {
        if let Some(fmt) = value.format_mut() {
            fmt.leading = leading.into();
        }
        {
            value.span = span_from_checkpoint(input, &_start);
        }
        value
    }))
}

fn slashdashed_entries(input: &mut Input<'_>) -> PResult<()> {
    separated(1.., (slashdash, node_entry), node_space1)
        .map(|()| ())
        .take()
        .map(|x| x.to_string())
        .parse_next(input)?;
    Ok(())
}
fn before_node_children(input: &mut Input<'_>) -> PResult<()> {
    alt((
        (
            node_space1,
            slashdashed_entries,
            // This second one will fail if `node_entry_leading` is empty.
            node_space1,
            slashdashed_children,
        )
            .take(),
        (node_space1, slashdashed_entries).take(),
        (node_space1, slashdashed_children).take(),
        node_space0.take(),
    ))
    .void()
    .parse_next(input)?;
    node_space0.parse_next(input)?;
    Ok(())
}
fn slashdashed_children(input: &mut Input<'_>) -> PResult<()> {
    node_space0.parse_next(input)?;
    trace(
        "slashdashed children",
        separated(
            1..,
            (slashdash.void(), node_children.void()).void(),
            node_space1,
        ),
    )
    .map(|()| ())
    .parse_next(input)
}
/// `node-children := '{' nodes final-node? '}'`
fn node_children(input: &mut Input<'_>) -> PResult<KdlDocument> {
    let _before_open = input.checkpoint();
    let _before_open_loc = input.current_token_start();
    "{".parse_next(input)?;
    let _after_open_loc = input.previous_token_end();
    let ns = trace("child nodes", nodes).parse_next(input)?;
    let _after_nodes = input.checkpoint();
    let _after_nodes_loc = input.previous_token_end();
    let close_res: PResult<_> = cut_err("}")
        .context(cx().msg("No closing '}' for child block").lbl("closed"))
        .parse_next(input);
    if close_res.is_err() {
        return close_res
            .map(|_| KdlDocument::new())
            .or_else(|mut e: ErrMode<KdlParseError>| {
                e = match e {
                    ErrMode::Cut(mut pe) => {
                        pe.span = Some((_before_open_loc.._after_open_loc).into());
                        ErrMode::Cut(pe)
                    }
                    e => return Err(e),
                };
                input.record_err(&_before_open, &_before_open, e)?;
                if !ns.is_empty() {
                    input.record_err(
                        &_after_nodes,
                        &_after_nodes,
                        ErrMode::Cut(KdlParseError {
                            message: Some("Closing '}' was not found after nodes".into()),
                            span: Some((_after_open_loc.._after_nodes_loc).into()),
                            label: Some("closed".into()),
                            help: None,
                            severity: Some(Severity::Error),
                        }),
                    )?;
                }
                Ok(KdlDocument::new())
            });
    }
    Ok(ns)
}

/// `node-terminator := single-line-comment | newline | ';' | eof`
fn node_terminator(input: &mut Input<'_>) -> PResult<()> {
    trace(
        "node_terminator",
        alt((";".void(), newline, single_line_comment)),
    )
    .void()
    .parse_next(input)
}

/// `value := type? optional-node-space (string | number | keyword)`
fn value(input: &mut Input<'_>) -> PResult<Option<KdlEntry>> {
    let ((ty, (value, raw)), _span) = trace(
        "value",
        (
            opt((ty, node_space0.take())),
            alt((keyword.map(Some), number.map(Some), string)).with_taken(),
        ),
    )
    .with_span()
    .parse_next(input)?;
    let ((before_ty_name, ty, after_ty_name), after_ty) = ty.unwrap_or_default();
    Ok(value.map(|value| KdlEntry {
        ty,
        value,
        name: None,
        format: Some(KdlEntryFormat {
            value_repr: raw.into(),
            after_ty: after_ty.into(),
            before_ty_name: before_ty_name.into(),
            after_ty_name: after_ty_name.into(),
            ..Default::default()
        }),
        span: _span.into(),
    }))
}

fn badval(input: &mut Input<'_>) -> PResult<()> {
    trace("badval", repeat_till(1.., any, peek(value_terminator)))
        .map(|((), _)| ())
        .parse_next(input)
}

fn value_terminator(input: &mut Input<'_>) -> PResult<()> {
    alt((
        eof.void(),
        "=".void(),
        ")".void(),
        "{".void(),
        "}".void(),
        node_space,
        node_terminator,
    ))
    .parse_next(input)
}

fn value_terminator_check(input: &mut Input<'_>) -> PResult<()> {
    trace("value terminator check", cut_err(peek(value_terminator).context(cx().hlp("A valid value was partially parsed, but was not followed by a value terminator. Did you want a space here?")))).parse_next(input)
}

/// `type := '(' optional-node-space string optional-node-space ')'`
fn ty<'s>(input: &mut Input<'s>) -> PResult<(&'s str, Option<KdlIdentifier>, &'s str)> {
    "(".parse_next(input)?;
    let (before_ty, ty, after_ty) = (
        node_space0.take(),
        resume_after_cut(
            cut_err(
                (identifier, peek(alt((node_space, ")".void())))).context(
                    cx().lbl("type name")
                        .msg("invalid contents inside type annotation"),
                ),
            ),
            repeat_till(1.., (not(badval_ty_char), any), peek(badval_ty_char)).map(|((), _)| ()),
        )
        .map(|opt| opt.map(|(i, _)| i)),
        node_space0.take(),
    )
        .parse_next(input)?;
    ")".parse_next(input)?;
    Ok((before_ty, ty, after_ty))
}

fn badval_ty_char(input: &mut Input<'_>) -> PResult<()> {
    alt((")".void(), "{".void(), node_space, node_terminator)).parse_next(input)
}

/// `line-space := newline | ws | single-line-comment`
fn line_space(input: &mut Input<'_>) -> PResult<()> {
    alt((node_space, newline, single_line_comment)).parse_next(input)
}

/// `node-space := ws* escline ws* | ws+`
fn node_space(input: &mut Input<'_>) -> PResult<()> {
    alt(((wss, escline, wss).void(), wsp)).parse_next(input)
}

fn node_space0(input: &mut Input<'_>) -> PResult<()> {
    repeat(0.., node_space).parse_next(input)
}

fn node_space1(input: &mut Input<'_>) -> PResult<()> {
    repeat(1.., node_space).parse_next(input)
}

/// string := identifier-string | quoted-string | raw-string ¶
pub(crate) fn string(input: &mut Input<'_>) -> PResult<Option<KdlValue>> {
    trace(
        "string",
        alt((
            resume_after_cut(
                (identifier_string, value_terminator_check).context(cx().lbl("identifier string")),
                badval,
            ),
            resume_after_cut(
                (raw_string, value_terminator_check).context(cx().lbl("raw string")),
                alt((raw_string_badval, badval)).void(),
            ),
            resume_after_cut(
                (quoted_string, value_terminator_check).context(cx().lbl("quoted string")),
                alt((quoted_string_badval, badval)).void(),
            ),
        )),
    )
    .map(|res| res.map(|(s, _)| s))
    .parse_next(input)
}

pub(crate) fn identifier(input: &mut Input<'_>) -> PResult<KdlIdentifier> {
    let mut bad_ident = false;
    let ((mut ident, raw), _span) = string
        .verify_map(|ident| {
            ident
                .or_else(|| {
                    // This is a sentinel we use later for better error messages
                    bad_ident = true;
                    Some(KdlValue::String("/BAD_IDENT\\".into()))
                })
                .and_then(|v| match v {
                    KdlValue::String(s) => Some(KdlIdentifier::from(s)),
                    _ => None,
                })
        })
        .with_taken()
        .with_span()
        .parse_next(input)?;
    ident.set_repr(if bad_ident { "" } else { raw });
    {
        ident.set_span(_span);
    }
    Ok(ident)
}

/// `identifier-string := unambiguous-ident | signed-ident | dotted-ident`
fn identifier_string(input: &mut Input<'_>) -> PResult<KdlValue> {
    alt((unambiguous_ident, signed_ident, dotted_ident))
        .take()
        .map(|s| KdlValue::String(s.into()))
        .parse_next(input)
}

/// `unambiguous-ident := ((identifier-char - digit - sign - '.') identifier-char*) - 'true' - 'false' - 'null' - 'inf' - '-inf' - 'nan'`
fn unambiguous_ident(input: &mut Input<'_>) -> PResult<()> {
    not(alt((digit1.void(), alt(("-", "+")).void(), ".".void()))).parse_next(input)?;
    peek(identifier_char).parse_next(input)?;
    trace(
        "identifier chars",
        cut_err(
            repeat(1.., identifier_char)
                .verify_map(|s: String| {
                    if matches!(
                        s.as_str(),
                        "true" | "false" | "null" | "inf" | "-inf" | "nan"
                    ) {
                        None
                    } else {
                        Some(s)
                    }
                })
                .void(),
        ),
    )
    .parse_next(input)
}

/// `signed-ident := sign ((identifier-char - digit - '.') identifier-char*)?`
fn signed_ident(input: &mut Input<'_>) -> PResult<()> {
    alt(("+", "-")).parse_next(input)?;
    not(alt((digit1.void(), ".".void()))).parse_next(input)?;
    repeat(0.., identifier_char).parse_next(input)
}

/// `dotted-ident := sign? '.' ((identifier-char - digit) identifier-char*)?`
fn dotted_ident(input: &mut Input<'_>) -> PResult<()> {
    (
        opt(signum),
        ".",
        not(digit1),
        repeat(0.., identifier_char).map(|_: ()| ()),
    )
        .void()
        .parse_next(input)
}

static DISALLOWED_IDENT_CHARS: [char; 11] =
    ['\\', '/', '(', ')', '{', '}', '[', ']', ';', '"', '#'];

pub(crate) fn is_disallowed_ident_char(c: char) -> bool {
    DISALLOWED_IDENT_CHARS.iter().any(|ic| ic == &c)
        || NEWLINES.iter().copied().collect::<String>().contains(c)
        || UNICODE_SPACES.iter().any(|us| us == &c)
        || is_disallowed_unicode(c)
        || c == '='
}

/// `identifier-char := unicode - unicode-space - newline - [\\/(){};\[\]"#] - disallowed-literal-code-points - equals-sign`
fn identifier_char(input: &mut Input<'_>) -> PResult<char> {
    (
        not(alt((
            unicode_space,
            newline,
            disallowed_unicode,
            equals_sign,
        ))),
        none_of(DISALLOWED_IDENT_CHARS),
    )
        .map(|(_, c)| c)
        .parse_next(input)
}

/// `equals-sign := See Table ([Equals Sign](#equals-sign))`
fn equals_sign(input: &mut Input<'_>) -> PResult<()> {
    "=".void().parse_next(input)
}

/// ```text
/// quoted-string := '"' single-line-string-body '"' | '"""' newline multi-line-string-body newline (unicode-space | ('\' (unicode-space | newline)+)*) '"""'
/// single-line-string-body := (string-character - newline)*
/// multi-line-string-body := (('"' | '""')? string-character)*
/// ```
fn quoted_string(input: &mut Input<'_>) -> PResult<KdlValue> {
    let quotes =
        alt((
            (
                "\"\"\"",
                cut_err(newline).context(cx().lbl("multi-line string newline").msg(
                    "Multi-line string opening quotes must be immediately followed by a newline",
                )),
            )
                .take(),
            "\"",
        ))
        .parse_next(input)?;
    let is_multiline = quotes.len() > 1;
    let ml_prefix: Option<String> = if is_multiline {
        Some(
            cut_err(peek(preceded(
                repeat_till(
                    0..,
                    (
                        repeat(
                            0..,
                            (
                                not(newline),
                                alt((
                                    ws_escape.void(),
                                    trace(
                                        "valid string body char(s)",
                                        alt((
                                            ('\"', not("\"\"")).void(),
                                            ('\"', not("\"")).void(),
                                            string_char.void(),
                                        )),
                                    )
                                    .void(),
                                )),
                            ),
                        )
                        .map(|()| ()),
                        newline,
                    ),
                    peek(terminated(
                        repeat(0.., alt((ws_escape, unicode_space))).map(|()| ()),
                        "\"\"\"",
                    )),
                )
                .map(|((), ())| ()),
                terminated(
                    repeat(0.., alt((ws_escape.map(|_| ""), unicode_space.take())))
                        .map(|s: String| s),
                    "\"\"\"",
                ),
            )))
            .context(cx().lbl("multi-line string"))
            .parse_next(input)?,
        )
    } else {
        None
    };
    let body = if let Some(prefix) = ml_prefix {
        let parser = repeat_till(
            0..,
            (
                cut_err(alt(((&prefix[..]).void(), peek(empty_line).void())))
                    .context(cx().msg("matching multiline string prefix").lbl("bad prefix").hlp("Multi-line string bodies must be prefixed by the exact same whitespace as the leading whitespace before the closing '\"\"\"'")),
                alt((
                    empty_line.map(|s| s.to_string()),
                    repeat_till(
                        0..,
                        (
                            not(newline),
                            alt((
                                ws_escape.map(|_| None),
                                alt((
                                    ('\"', not("\"\"")).map(|(c, ())| Some(c)),
                                    ('\"', not("\"")).map(|(c, ())| Some(c)),
                                    string_char.map(Some),
                                ))
                            ))
                        ).map(|(_, c)| c),
                        newline,
                    )
                    // multiline string literal newlines are normalized to `\n`
                    .map(|(cs, _): (Vec<Option<char>>, _)| cs.into_iter().flatten().chain(vec!['\n']).collect::<String>()),
                )),
            )
                .map(|(_, s)| s),
            (
                &prefix[..],
                repeat(0.., ws_escape.void()).map(|()| ()),
                peek("\"\"\""),
            ),
        )
        .map(|(s, _): (Vec<String>, (_, _, _))| {
            let mut s = s.join("");
            // Slice off the `\n` at the end of the last line.
            s.truncate(s.len().saturating_sub(1));
            s
        })
        .context(cx().lbl("multi-line quoted string"));
        cut_err(parser).parse_next(input)?
    } else {
        let parser = repeat_till(
            0..,
            (
                cut_err(
                    not(newline).context(
                        cx().msg("Unexpected newline in single-line quoted string")
                            .hlp("You can make a string multi-line by wrapping it in '\"\"\"', with a newline immediately after the opening quotes."),
                    ),
                ),
                alt((
                    ws_escape.map(|_| None),
                    string_char.map(Some),
                ))
                ).map(|(_, c)| c),
            peek("\"")
        )
        .map(|(cs, _): (Vec<Option<char>>, _)| cs.into_iter().flatten().collect::<String>())
        .context(cx().lbl("quoted string"));
        cut_err(parser).parse_next(input)?
    };
    let closing_quotes = if is_multiline {
        "\"\"\"".context(cx().msg("missing multiline string closing quotes").hlp("Multiline strings must be closed by '\"\"\"' on a standalone line, only prefixed by whitespace."))
    } else {
        "\"".context(
            cx().msg("missing string closing quote")
                .hlp("Did you forget to escape something?"),
        )
    };
    cut_err(closing_quotes).parse_next(input)?;
    Ok(KdlValue::String(body))
}

fn empty_line(input: &mut Input<'_>) -> PResult<&'static str> {
    repeat(0.., alt((ws_escape.void(), unicode_space.void())))
        .map(|()| ())
        .parse_next(input)?;
    newline.parse_next(input)?;
    Ok("\n")
}

/// Like badval, but is able to slurp up invalid raw strings, which contain whitespace.
fn quoted_string_badval(input: &mut Input<'_>) -> PResult<()> {
    // TODO(@zkat): this should have different behavior based on whether we're
    // resuming a single or multi-line string. Right now, multi-liners end up
    // with silly errors.
    (
        repeat_till(
            0..,
            (not(quoted_string_terminator), any),
            quoted_string_terminator,
        ),
        quoted_string_terminator,
    )
        .map(|(((), _), _)| ())
        .parse_next(input)
}

fn quoted_string_terminator(input: &mut Input<'_>) -> PResult<()> {
    alt(("\"\"\"".void(), "\"".void(), peek(value_terminator))).parse_next(input)
}

/// ```text
/// string-character := '\' escape | [^\\"] - disallowed-literal-code-points
/// ```
fn string_char(input: &mut Input<'_>) -> PResult<char> {
    alt((
        trace("escaped char", escaped_char),
        trace(
            "regular string char",
            (not(disallowed_unicode), none_of(['\\', '"'])).map(|(_, c)| c),
        ),
    ))
    .parse_next(input)
}

fn ws_escape(input: &mut Input<'_>) -> PResult<()> {
    trace(
        "ws_escape",
        (
            "\\",
            repeat(1.., alt((unicode_space, newline))).map(|()| ()),
        ),
    )
    .void()
    .parse_next(input)
}

/// ```text
/// escape := ["\\bfnrts] | 'u{' hex-digit{1, 6} '}' | (unicode-space | newline)+
/// hex-digit := [0-9a-fA-F]
/// ```
fn escaped_char(input: &mut Input<'_>) -> PResult<char> {
    "\\".parse_next(input)?;
    alt((
        alt((
            "\\".value('\\'),
            "\"".value('\"'),
            "b".value('\u{0008}'),
            "f".value('\u{000C}'),
            "n".value('\n'),
            "r".value('\r'),
            "t".value('\t'),
            "s".value(' '),
        )),
        (
            "u{",
            cut_err(take_while(1..=6, AsChar::is_hex_digit)),
            cut_err("}"),
        )
            .context(cx().lbl("unicode escape char"))
            .verify_map(|(_, hx, _)| {
                let val = u32::from_str_radix(hx, 16)
                    .expect("Should have already been validated to be a hex string.");
                char::from_u32(val)
            }),
    ))
    .parse_next(input)
}

/// ```text
/// raw-string := '#' raw-string-quotes '#' | '#' raw-string '#'
/// raw-string-quotes := '"' single-line-raw-string-body '"' | '"""' newline multi-line-raw-string-body '"""'
/// single-line-raw-string-body := '' | (single-line-raw-string-char - '"') single-line-raw-string-char*? | '"' (single-line-raw-string-char - '"') single-line-raw-string-char*?
/// single-line-raw-string-char := unicode - newline - disallowed-literal-code-points
/// multi-line-raw-string-body := (unicode - disallowed-literal-code-points)*?
/// ```
fn raw_string(input: &mut Input<'_>) -> PResult<KdlValue> {
    let _start_loc = input.current_token_start();
    let hashes: String = repeat(1.., "#").parse_next(input)?;
    let quotes = alt((("\"\"\"", newline).take(), "\"")).parse_next(input)?;
    let is_multiline = quotes.len() > 1;
    let ml_prefix: Option<String> = if is_multiline {
        Some(
            peek(preceded(
                repeat_till(
                    0..,
                    (
                        repeat(
                            0..,
                            (
                                not(newline),
                                not(disallowed_unicode),
                                not(("\"\"\"", &hashes[..])),
                                any,
                            ),
                        )
                        .map(|()| ()),
                        newline,
                    ),
                    peek(terminated(
                        repeat(0.., unicode_space).map(|()| ()),
                        ("\"\"\"", &hashes[..]),
                    )),
                )
                .map(|((), ())| ()),
                terminated(
                    repeat(0.., unicode_space).map(|()| ()).take(),
                    ("\"\"\"", &hashes[..]),
                ),
            ))
            .parse_next(input)?
            .to_string(),
        )
    } else {
        None
    };
    let body = if let Some(prefix) = ml_prefix {
        repeat_till(
            0..,
            (
                cut_err(alt(((&prefix[..]).void(), peek(empty_line).void())))
                    .context(cx().lbl("matching multiline raw string prefix")),
                alt((
                    empty_line.map(|s| s.to_string()),
                    repeat_till(
                        0..,
                        (not(newline), not(("\"\"\"", &hashes[..])), any)
                            .map(|((), (), _)| ())
                            .take(),
                        newline,
                    )
                    // multiline string literal newlines are normalized to `\n`
                    .map(|(s, _): (Vec<&str>, _)| format!("{}\n", s.join(""))),
                )),
            )
                .map(|(_, s)| s),
            (
                &prefix[..],
                repeat(0.., unicode_space).map(|()| ()).take(),
                peek(("\"\"\"", &hashes[..])),
            ),
        )
        .map(|(s, _): (Vec<String>, (_, _, _))| {
            let mut s = s.join("");
            // Slice off the `\n` at the end of the last line.
            s.truncate(s.len().saturating_sub(1));
            s
        })
        .parse_next(input)?
    } else {
        repeat_till(
            0..,
            (
                not(disallowed_unicode),
                not(newline),
                not(("\"", &hashes[..])),
                any,
            )
                .map(|(_, _, _, s)| s),
            peek(("\"", &hashes[..])),
        )
        .map(|(s, _): (String, _)| s)
        .context(cx().lbl("raw string"))
        .parse_next(input)?
    };
    let closing_quotes = if is_multiline {
        "\"\"\"".context(cx().lbl("multiline raw string closing quotes"))
    } else {
        "\"".context(cx().lbl("raw string closing quotes"))
    };
    cut_err((closing_quotes, &hashes[..])).parse_next(input)?;
    if body == "\"" {
        Err(ErrMode::Cut(KdlParseError {
            message: Some("Single-line raw strings cannot look like multi-line ones".into()),
            span: Some((_start_loc..input.previous_token_end()).into()),
            label: Some("triple quotes".into()),
            help: Some("Consider using a regular escaped string if all you want is a single quote: \"\\\"\"".into()),
            severity: Some(Severity::Error),
        }))
    } else {
        Ok(KdlValue::String(body))
    }
}

/// Like badval, but is able to slurp up invalid raw strings, which contain whitespace.
fn raw_string_badval(input: &mut Input<'_>) -> PResult<()> {
    repeat_till(
        0..,
        (not(alt(("#", "\""))), any),
        (alt(("#", "\"")), peek(alt((ws, newline, eof.void())))),
    )
    .map(|(v, _)| v)
    .parse_next(input)
}
/// ```text
/// keyword := '#true' | '#false' | '#null'
/// keyword-number := '#inf' | '#-inf' | '#nan'
/// ````
fn keyword(input: &mut Input<'_>) -> PResult<KdlValue> {
    let _ = "#".parse_next(input)?;
    not(one_of(['#', '"'])).parse_next(input)?;
    cut_err(alt((
        "true".value(KdlValue::Bool(true)),
        "false".value(KdlValue::Bool(false)),
        "null".value(KdlValue::Null),
        "nan".value(KdlValue::Float(f64::NAN)),
        "inf".value(KdlValue::Float(f64::INFINITY)),
        "-inf".value(KdlValue::Float(f64::NEG_INFINITY)),
    )))
    .context(cx().lbl("keyword").hlp(
        "Available keywords in KDL are '#true', '#false', '#null', '#nan', '#inf', and '#-inf'; they are case-sensitive.",
    ))
    .parse_next(input)
}

/// `bom := '\u{FEFF}'`
fn bom(input: &mut Input<'_>) -> PResult<()> {
    "\u{FEFF}".void().parse_next(input)
}

pub(crate) fn is_disallowed_unicode(c: char) -> bool {
    matches!(c,
        '\u{0000}'..='\u{0008}'
        | '\u{000E}'..='\u{001F}'
        | '\u{200E}'..='\u{200F}'
        | '\u{202A}'..='\u{202E}'
        | '\u{2066}'..='\u{2069}'
        | '\u{FEFF}'
    )
}

/// `disallowed-literal-code-points := See Table (Disallowed Literal Code
/// Points)`
/// ```markdown
/// * The codepoints `U+0000-0008` or the codepoints `U+000E-001F`  (various
///   control characters).
/// * `U+007F` (the Delete control character).
/// * Any codepoint that is not a [Unicode Scalar
///   Value](https://unicode.org/glossary/#unicode_scalar_value) (`U+D800-DFFF`).
/// * `U+200E-200F`, `U+202A-202E`, and `U+2066-2069`, the [unicode
///   "direction control"
///   characters](https://www.w3.org/International/questions/qa-bidi-unicode-controls)
/// * `U+FEFF`, aka Zero-width Non-breaking Space (ZWNBSP)/Byte Order Mark (BOM),
///   except as the first code point in a document.
/// ```
fn disallowed_unicode(input: &mut Input<'_>) -> PResult<()> {
    take_while(1.., is_disallowed_unicode)
        .void()
        .parse_next(input)
}

/// `escline := '\\' ws* (single-line-comment | newline | eof)`
fn escline(input: &mut Input<'_>) -> PResult<()> {
    "\\".parse_next(input)?;
    wss.parse_next(input)?;
    alt((single_line_comment, newline, eof.void())).parse_next(input)?;
    wss.parse_next(input)
}
pub(crate) static NEWLINES: [&str; 8] = [
    "\u{000D}\u{000A}",
    "\u{000D}",
    "\u{000A}",
    "\u{0085}",
    "\u{000B}",
    "\u{000C}",
    "\u{2028}",
    "\u{2029}",
];

/// `newline := <See Table>`
fn newline(input: &mut Input<'_>) -> PResult<()> {
    alt(NEWLINES)
        .void()
        .context(cx().lbl("newline"))
        .parse_next(input)
}

fn wss(input: &mut Input<'_>) -> PResult<()> {
    repeat(0.., ws).parse_next(input)
}

fn wsp(input: &mut Input<'_>) -> PResult<()> {
    repeat(1.., ws).parse_next(input)
}

/// `ws := unicode-space | multi-line-comment``
fn ws(input: &mut Input<'_>) -> PResult<()> {
    alt((unicode_space, multi_line_comment)).parse_next(input)
}

static UNICODE_SPACES: [char; 18] = [
    '\u{0009}', '\u{0020}', '\u{00A0}', '\u{1680}', '\u{2000}', '\u{2001}', '\u{2002}', '\u{2003}',
    '\u{2004}', '\u{2005}', '\u{2006}', '\u{2007}', '\u{2008}', '\u{2009}', '\u{200A}', '\u{202F}',
    '\u{205F}', '\u{3000}',
];

/// `unicode-space := <See Table>`
fn unicode_space(input: &mut Input<'_>) -> PResult<()> {
    one_of(UNICODE_SPACES).void().parse_next(input)
}

/// `single-line-comment := '//' ^newline* (newline | eof)`
fn single_line_comment(input: &mut Input<'_>) -> PResult<()> {
    "//".parse_next(input)?;
    repeat_till(
        0..,
        (not(alt((newline, eof.void()))), any),
        alt((newline, eof.void())),
    )
    .map(|(_, _): ((), _)| ())
    .parse_next(input)
}

/// `multi-line-comment := '/*' commented-block`
fn multi_line_comment(input: &mut Input<'_>) -> PResult<()> {
    "/*".parse_next(input)?;
    cut_err(commented_block)
        .context(cx().lbl("closing of multi-line comment"))
        .parse_next(input)
}

/// `commented-block := '*/' | (multi-line-comment | '*' | '/' | [^*/]+) commented-block`
fn commented_block(input: &mut Input<'_>) -> PResult<()> {
    loop {
        let closing: PResult<()> = "*/".void().parse_next(input);
        if closing.is_ok() {
            return Ok(());
        }
        alt((
            multi_line_comment,
            "*".void(),
            "/".void(),
            repeat(1.., none_of(['*', '/'])).map(|()| ()),
        ))
        .parse_next(input)?;
    }
}
/// slashdash := '/-' (node-space | line-space)*
fn slashdash(input: &mut Input<'_>) -> PResult<()> {
    (
        "/-",
        repeat(0.., alt((node_space, line_space))).map(|()| ()),
    )
        .void()
        .parse_next(input)
}
/// `number := keyword-number | hex | octal | binary | decimal`
fn number(input: &mut Input<'_>) -> PResult<KdlValue> {
    alt((float_value, integer_value)).parse_next(input)
}

/// ```text
/// decimal := sign? integer ('.' integer)? exponent?
/// exponent := ('e' | 'E') sign? integer
/// ```
fn float_value(input: &mut Input<'_>) -> PResult<KdlValue> {
    float.map(KdlValue::Float).parse_next(input)
}

fn float<T: ParseFloat>(input: &mut Input<'_>) -> PResult<T> {
    (
        alt((
            (
                decimal::<i128>,
                opt(preceded(
                    '.',
                    cut_err(
                        udecimal::<i128>.context(
                            cx().msg("Non-digit character found after the '.' of a float"),
                        ),
                    ),
                )),
                Caseless("e"),
                opt(one_of(['-', '+'])),
                cut_err(udecimal::<i128>.context(
                    cx().msg("Non-digit character found in the exponent part of a float").hlp("Floats with exponent parts should look like '2.0e123', or '43.3E-4'."),
                )),
            )
                .take(),
            (
                decimal::<i128>,
                '.',
                cut_err(
                    udecimal::<i128>
                        .context(cx().msg("Non-digit character found after the '.' of a float")),
                ),
            )
                .take(),
        )),
        value_terminator_check,
    )
        .try_map(|(float_str, _)| T::parse_float(&str::replace(float_str, "_", "")))
        .context(cx().lbl("float"))
        .parse_next(input)
}
fn integer_value(input: &mut Input<'_>) -> PResult<KdlValue> {
    alt((
        (hex, value_terminator_check).context(cx().lbl("hexadecimal number")),
        (octal, value_terminator_check).context(cx().lbl("octal number")),
        (binary, value_terminator_check).context(cx().lbl("binary number")),
        (decimal, value_terminator_check).context(cx().lbl("integer")),
    ))
    .map(|(val, _)| KdlValue::Integer(val))
    .parse_next(input)
}

/// Non-float decimal
fn decimal<T: FromStrRadix + MaybeNegatable>(input: &mut Input<'_>) -> PResult<T> {
    let positive = signum.parse_next(input)?;
    udecimal::<T>
        .try_map(|x| {
            if positive {
                Ok(x)
            } else {
                x.negated().ok_or(NegativeUnsignedError)
            }
        })
        .parse_next(input)
}
/// `integer := digit (digit | '_')*`
fn udecimal<T: FromStrRadix>(input: &mut Input<'_>) -> PResult<T> {
    (
        digit1,
        repeat(
            0..,
            alt(("_", take_while(1.., AsChar::is_dec_digit).take())),
        ),
    )
        .try_map(|(l, r): (&str, Vec<&str>)| {
            T::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 10)
        })
        .parse_next(input)
}

/// `hex := sign? '0x' hex-digit (hex-digit | '_')*`
fn hex<T: FromStrRadix + MaybeNegatable>(input: &mut Input<'_>) -> PResult<T> {
    let positive = signum.parse_next(input)?;
    uhex::<T>
        .try_map(|x| {
            if positive {
                Ok(x)
            } else {
                x.negated().ok_or(NegativeUnsignedError)
            }
        })
        .parse_next(input)
}

fn uhex<T: FromStrRadix>(input: &mut Input<'_>) -> PResult<T> {
    alt(("0x", "0X")).parse_next(input)?;
    cut_err((
        hex_digit1,
        repeat(
            0..,
            alt(("_", take_while(1.., AsChar::is_hex_digit).take())),
        ),
    ))
    .try_map(|(l, r): (&str, Vec<&str>)| {
        T::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 16)
    })
    .context(cx().lbl("hexadecimal"))
    .parse_next(input)
}
/// `octal := sign? '0o' [0-7] [0-7_]*`
fn octal<T: FromStrRadix + MaybeNegatable>(input: &mut Input<'_>) -> PResult<T> {
    let positive = signum.parse_next(input)?;
    uoctal::<T>
        .try_map(|x| {
            if positive {
                Ok(x)
            } else {
                x.negated().ok_or(NegativeUnsignedError)
            }
        })
        .parse_next(input)
}

fn uoctal<T: FromStrRadix>(input: &mut Input<'_>) -> PResult<T> {
    alt(("0o", "0O")).parse_next(input)?;
    cut_err((
        oct_digit1,
        repeat(
            0..,
            alt(("_", take_while(1.., AsChar::is_oct_digit).take())),
        ),
    ))
    .try_map(|(l, r): (&str, Vec<&str>)| {
        T::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 8)
    })
    .context(cx().lbl("octal"))
    .parse_next(input)
}
/// `binary := sign? '0b' ('0' | '1') ('0' | '1' | '_')*`
fn binary<T: FromStrRadix + MaybeNegatable>(input: &mut Input<'_>) -> PResult<T> {
    let positive = signum.parse_next(input)?;
    ubinary::<T>
        .try_map(|x| {
            if positive {
                Ok(x)
            } else {
                x.negated().ok_or(NegativeUnsignedError)
            }
        })
        .parse_next(input)
}

fn ubinary<T: FromStrRadix>(input: &mut Input<'_>) -> PResult<T> {
    alt(("0b", "0B")).parse_next(input)?;
    cut_err(
        (alt(("0", "1")), repeat(0.., alt(("0", "1", "_")))).try_map(
            move |(x, xs): (&str, Vec<&str>)| {
                T::from_str_radix(&format!("{x}{}", str::replace(&xs.join(""), "_", "")), 2)
            },
        ),
    )
    .context(cx().lbl("binary"))
    .parse_next(input)
}
fn signum(input: &mut Input<'_>) -> PResult<bool> {
    let sign = opt(alt(('+', '-'))).parse_next(input)?;
    let mult = if let Some(sign) = sign {
        sign == '+'
    } else {
        true
    };
    Ok(mult)
}

trait FromStrRadix {
    fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>
    where
        Self: Sized;
}

macro_rules! impl_from_str_radix {
    ($($t:ty),*) => {
        $(
            impl FromStrRadix for $t {
                fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
                    <$t>::from_str_radix(s, radix)
                }
            }
        )*
    };
}

impl_from_str_radix!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);

trait MaybeNegatable: CheckedMul {
    fn negated(&self) -> Option<Self>;
}

macro_rules! impl_negatable_signed {
    ($($t:ty),*) => {
        $(
            impl MaybeNegatable for $t {
                fn negated(&self) -> Option<Self> {
                    Some(self * -1)
                }
            }
        )*
    };
}

macro_rules! impl_negatable_unsigned {
    ($($t:ty),*) => {
        $(
            impl MaybeNegatable for $t {
                fn negated(&self) -> Option<Self> {
                    None
                }
            }
        )*
    };
}

trait ParseFloat {
    fn parse_float(input: &str) -> Result<Self, ParseFloatError>
    where
        Self: Sized;
}

impl ParseFloat for f32 {
    fn parse_float(input: &str) -> Result<Self, ParseFloatError> {
        input.parse()
    }
}
impl ParseFloat for f64 {
    fn parse_float(input: &str) -> Result<Self, ParseFloatError> {
        input.parse()
    }
}

impl_negatable_signed!(i8, i16, i32, i64, i128, isize);
impl_negatable_unsigned!(u8, u16, u32, u64, u128, usize);

#[cfg(test)]
mod regression_tests {
    use crate::kdl::{KdlDocument, KdlValue};

    #[test]
    fn long_invalid_document_recovery_does_not_recurse() {
        let source = "}".repeat(20_000);
        assert!(source.parse::<KdlDocument>().is_err());
    }

    #[test]
    fn long_multiline_comment_does_not_recurse_per_character() {
        let source = format!("/*{}x*/", "*".repeat(20_000));
        assert!(source.parse::<KdlDocument>().is_ok());
    }

    // Representative cases retained from kdl-rs 6.7.1's Apache-2.0 parser tests. These cover
    // the constructs usage specs depend on without restoring the unused upstream API surface.
    #[test]
    fn multiline_strings_are_dedented_and_newlines_are_normalized() {
        let source = "node \"\"\"\r\n  foo\r\n    bar\n  baz\r  \"\"\"";
        let document = source.parse::<KdlDocument>().unwrap();
        assert_eq!(
            document.get("node").unwrap().get(0),
            Some(&KdlValue::String("foo\n  bar\nbaz".into()))
        );
    }

    #[test]
    fn raw_strings_and_escapes_keep_their_values() {
        let document = r##"raw #"quotes \ stay literal"#
escaped "line\n\u{1f642}"
"##
        .parse::<KdlDocument>()
        .unwrap();
        assert_eq!(
            document.get("raw").unwrap().get(0),
            Some(&KdlValue::String("quotes \\ stay literal".into()))
        );
        assert_eq!(
            document.get("escaped").unwrap().get(0),
            Some(&KdlValue::String("line\n🙂".into()))
        );
    }

    #[test]
    fn hexadecimal_octal_and_binary_numbers_parse() {
        let document = "numbers 0x10 0o10 0b10\n".parse::<KdlDocument>().unwrap();
        let numbers = document.get("numbers").unwrap();
        assert_eq!(numbers.get(0), Some(&KdlValue::Integer(16)));
        assert_eq!(numbers.get(1), Some(&KdlValue::Integer(8)));
        assert_eq!(numbers.get(2), Some(&KdlValue::Integer(2)));
    }

    #[test]
    fn invalid_number_diagnostic_keeps_its_exact_span() {
        let error = "node 0x1asdf 2".parse::<KdlDocument>().unwrap_err();
        let diagnostic = error
            .diagnostics
            .iter()
            .find(|diagnostic| diagnostic.message.as_deref() == Some("Expected hexadecimal number"))
            .unwrap();
        assert_eq!(diagnostic.span, (5..12).into());
        assert_eq!(diagnostic.label.as_deref(), Some("not hexadecimal number"));
    }
}