oxc-yaml-parser 0.0.1

A YAML 1.2 parser that produces a comment-preserving typed AST.
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
//! The YAML scanner (tokenizer).
//!
//! A port of the libyaml scanning algorithm (by way of
//! [saphyr](https://github.com/saphyr-rs/saphyr)), adapted to:
//!
//! - operate on byte offsets over `&str` (no buffering, no `char` ring buffer),
//! - produce span-only tokens (no cooked scalar values),
//! - retain comments as trivia (libyaml discards them),
//! - be tolerant of uninterpreted directives (Prettier-compat: unknown
//!   directives and `%YAML x.y` versions are accepted).
//!
//! The core machinery — the simple key stack, the indent stack with
//! synthesized `Block*` tokens, and the flow level with implicit flow mapping
//! states — deliberately mirrors libyaml/saphyr; do not "improve" it without
//! cross-checking against the yaml-test-suite.

// Indentation comparisons follow libyaml's `isize` convention (-1 = no indent);
// columns are small, so the casts are safe.
#![expect(clippy::cast_possible_wrap, clippy::cast_sign_loss, clippy::cast_possible_truncation)]

use crate::{
    ast::{Chomping, Comment},
    error::{Error, ErrorKind},
    pos::Span,
};
use std::{collections::VecDeque, num::NonZeroU32};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScalarStyle {
    Plain,
    SingleQuoted,
    DoubleQuoted,
    Literal,
    Folded,
}

/// 1-based index into [`Scanner::block_headers`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BlockHeaderIndex(NonZeroU32);

impl BlockHeaderIndex {
    fn new(zero_based: usize) -> Self {
        Self(NonZeroU32::new(zero_based as u32 + 1).unwrap())
    }

    pub fn get(self) -> usize {
        self.0.get() as usize - 1
    }
}

/// Extra information scanned from a block scalar header. Stored out-of-band
/// in [`Scanner::block_headers`] (indexed by the `Scalar` token) to keep
/// [`Token`] small — every buffered token would otherwise pay for it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BlockScalarHeader {
    pub chomping: Chomping,
    /// Explicit indentation indicator digit, if any.
    pub indent: Option<u32>,
    /// Offset right after the header line's line break.
    pub content_start: u32,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TokenKind {
    StreamStart,
    StreamEnd,
    /// `%NAME ...`, uninterpreted. The span excludes any trailing comment.
    Directive,
    /// `---`
    DocumentStart,
    /// `...`
    DocumentEnd,
    BlockSequenceStart,
    BlockMappingStart,
    BlockEnd,
    FlowSequenceStart,
    FlowSequenceEnd,
    FlowMappingStart,
    FlowMappingEnd,
    BlockEntry,
    FlowEntry,
    Key,
    Value,
    Alias,
    Anchor,
    Tag,
    /// A scalar. Block scalars (`Literal`/`Folded`) carry their 1-based index
    /// into [`Scanner::block_headers`] (`NonZeroU32` so the `Option` is free).
    Scalar(ScalarStyle, Option<BlockHeaderIndex>),
}

impl TokenKind {
    /// Whether a token of this kind can start a node (in any position).
    pub(crate) fn starts_node(self) -> bool {
        matches!(
            self,
            TokenKind::Scalar(..)
                | TokenKind::Alias
                | TokenKind::Anchor
                | TokenKind::Tag
                | TokenKind::FlowSequenceStart
                | TokenKind::FlowMappingStart
                | TokenKind::BlockSequenceStart
                | TokenKind::BlockMappingStart
        )
    }

    /// Whether a token of this kind can start a node in mapping key/value
    /// position, where a bare `BlockEntry` (an indentless sequence) is also
    /// allowed.
    pub(crate) fn starts_mapping_entry_node(self) -> bool {
        self.starts_node() || self == TokenKind::BlockEntry
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Token {
    pub kind: TokenKind,
    pub span: Span,
    /// `true` for tokens the scanner invents (block/flow structure markers,
    /// retroactively inserted `Key`s, ...) rather than reads from the source.
    pub synthesized: bool,
}

impl Token {
    fn new(kind: TokenKind, span: Span) -> Self {
        Self { kind, span, synthesized: false }
    }

    /// A zero-width token invented by the scanner at the given offset.
    fn synthesized(kind: TokenKind, at: usize) -> Self {
        Self { kind, span: span(at, at), synthesized: true }
    }
}

/// A scalar that was scanned and may retroactively become a mapping key.
///
/// Upon scanning `a` in `a: b` we do not yet know it is a key; the token is
/// buffered and this bookkeeping records where a `Key` token would need to be
/// inserted if a `:` follows.
#[derive(Clone, Copy, Debug)]
struct SimpleKey {
    possible: bool,
    required: bool,
    /// Index of the buffered token, counting tokens already handed out.
    token_number: usize,
    /// Byte offset of the candidate key.
    pos: usize,
    line: usize,
    col: usize,
}

impl SimpleKey {
    fn new() -> Self {
        Self { possible: false, required: false, token_number: 0, pos: 0, line: 0, col: 0 }
    }
}

/// An indentation level on the stack of indentations.
#[derive(Clone, Copy, Debug)]
struct Indent {
    /// The former indentation level.
    indent: isize,
    /// Whether, upon closing, this indent generates a `BlockEnd` token.
    needs_block_end: bool,
}

/// Whether a flow sequence may hold / holds an implicit `key: value` mapping
/// (`[ a: b ]`), which requires synthesizing `FlowMapping*` tokens.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ImplicitMappingState {
    Possible,
    Inside,
}

type ScanResult<T = ()> = Result<T, Error>;

fn is_blank(b: u8) -> bool {
    b == b' ' || b == b'\t'
}

fn is_break(b: u8) -> bool {
    b == b'\n' || b == b'\r'
}

/// Break or end-of-input (we use NUL as the EOF sentinel like libyaml).
fn is_breakz(b: u8) -> bool {
    is_break(b) || b == 0
}

fn is_blank_or_breakz(b: u8) -> bool {
    is_blank(b) || is_breakz(b)
}

fn is_flow(b: u8) -> bool {
    matches!(b, b',' | b'[' | b']' | b'{' | b'}')
}

fn is_alpha(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}

/// Whether a plain scalar ends at byte `b` (with lookahead `next`).
/// See 7.3.3. Plain Style. Context-sensitive: flow indicators only terminate
/// a plain scalar inside a flow collection.
#[inline]
fn ends_plain_scalar(b: u8, next: u8, in_flow: bool) -> bool {
    match b {
        b':' if is_blank_or_breakz(next) || (in_flow && is_flow(next)) => true,
        _ if in_flow && is_flow(b) => true,
        _ => false,
    }
}

fn is_anchor_char(b: u8) -> bool {
    // Any non-space, non-break, non-flow character. Multi-byte UTF-8 lead and
    // continuation bytes are all >= 0x80 and thus allowed.
    !is_blank_or_breakz(b) && !is_flow(b)
}

fn is_uri_char(b: u8) -> bool {
    is_alpha(b) || b"#;/?:@&=+$,_.!~*'()[]%".contains(&b)
}

fn is_tag_char(b: u8) -> bool {
    is_uri_char(b) && !is_flow(b) && b != b'!'
}

const BOM: &[u8] = "\u{FEFF}".as_bytes();

pub struct Scanner<'a> {
    src: &'a [u8],
    /// Current byte offset.
    pos: usize,
    /// Current line (0-based; only compared relatively).
    line: usize,
    /// Current column (0-based, counted in characters).
    col: usize,

    /// Buffered tokens not yet handed to the parser.
    tokens: VecDeque<Token>,
    /// Comments encountered so far, in source order.
    pub(crate) comments: oxc_allocator::Vec<'a, Comment>,
    /// Block scalar headers, indexed by `TokenKind::Scalar`'s payload.
    pub(crate) block_headers: Vec<BlockScalarHeader>,

    stream_start_produced: bool,
    stream_end_produced: bool,
    /// Offset at which a `:` may be adjacent to a JSON-like key in flow context.
    adjacent_value_allowed_at: usize,
    simple_key_allowed: bool,
    simple_keys: Vec<SimpleKey>,
    indent: isize,
    indents: Vec<Indent>,
    flow_level: usize,
    /// Number of tokens already handed to the parser.
    tokens_parsed: usize,
    /// Whether all characters since the last newline were whitespace.
    leading_whitespace: bool,
    flow_mapping_started: bool,
    implicit_flow_mapping_states: Vec<ImplicitMappingState>,
}

impl<'a> Scanner<'a> {
    pub fn new(allocator: &'a oxc_allocator::Allocator, source: &'a str) -> Self {
        Self {
            src: source.as_bytes(),
            pos: 0,
            line: 0,
            col: 0,
            tokens: VecDeque::new(),
            comments: oxc_allocator::Vec::new_in(&allocator),
            block_headers: Vec::new(),
            stream_start_produced: false,
            stream_end_produced: false,
            adjacent_value_allowed_at: usize::MAX,
            simple_key_allowed: true,
            simple_keys: Vec::new(),
            indent: -1,
            indents: Vec::new(),
            flow_level: 0,
            tokens_parsed: 0,
            leading_whitespace: true,
            flow_mapping_started: false,
            implicit_flow_mapping_states: Vec::new(),
        }
    }

    // ---------------------------------------------------------------- cursor

    fn peek(&self) -> u8 {
        self.src.get(self.pos).copied().unwrap_or(0)
    }

    fn peek_nth(&self, n: usize) -> u8 {
        self.src.get(self.pos + n).copied().unwrap_or(0)
    }

    fn is_eof(&self) -> bool {
        self.pos >= self.src.len()
    }

    /// End of input, or a NUL byte (libyaml's EOF sentinel).
    fn next_is_z(&self) -> bool {
        self.peek() == 0
    }

    /// Consume one character (possibly multi-byte). Must not be a line break.
    fn bump(&mut self) {
        debug_assert!(!self.is_eof());
        let b = self.src[self.pos];
        // 0 leading ones = ASCII (width 1); otherwise the count is the width.
        let width = (b.leading_ones() as usize).max(1);
        self.pos = (self.pos + width).min(self.src.len());
        self.col += 1;
        self.leading_whitespace = false;
    }

    /// Consume one blank character (space or tab) without clearing
    /// `leading_whitespace`.
    fn bump_blank(&mut self) {
        debug_assert!(is_blank(self.peek()));
        self.pos += 1;
        self.col += 1;
    }

    /// Consume a line break (`\n`, `\r` or `\r\n`).
    fn bump_break(&mut self) {
        debug_assert!(is_break(self.peek()));
        if self.peek() == b'\r' && self.peek_nth(1) == b'\n' {
            self.pos += 1;
        }
        self.pos += 1;
        self.col = 0;
        self.line += 1;
        self.leading_whitespace = true;
    }

    /// Consume a line break; crossing a line break in block context re-allows
    /// a simple key.
    fn bump_break_in_stream(&mut self) {
        self.bump_break();
        if self.flow_level == 0 {
            self.simple_key_allowed = true;
        }
    }

    /// Consume a BOM without affecting the column.
    fn bump_bom(&mut self) {
        debug_assert!(self.next_is_bom());
        self.pos += BOM.len();
    }

    /// Bulk-consume bytes while `keep` holds. To never split a multi-byte
    /// character, the predicate must treat all bytes >= 0x80 uniformly (keep
    /// all of them, or reject all of them — a rejected lead byte stops the run
    /// at a character boundary).
    #[inline]
    fn bump_while(&mut self, keep: impl Fn(u8) -> bool) {
        let start = self.pos;
        let mut i = start;
        while i < self.src.len() && keep(self.src[i]) {
            i += 1;
        }
        if i > start {
            self.col += char_count(&self.src[start..i]);
            self.pos = i;
            self.leading_whitespace = false;
        }
    }

    /// Bulk-consume a run of spaces without clearing `leading_whitespace`.
    /// Returns the number of spaces consumed.
    #[inline]
    fn bump_space_run(&mut self) -> usize {
        let start = self.pos;
        while self.pos < self.src.len() && self.src[self.pos] == b' ' {
            self.pos += 1;
        }
        let n = self.pos - start;
        self.col += n;
        n
    }

    fn next_is_bom(&self) -> bool {
        self.src[self.pos..].starts_with(BOM)
    }

    fn next_is_document_start(&self) -> bool {
        self.src[self.pos..].starts_with(b"---") && is_blank_or_breakz(self.peek_nth(3))
    }

    fn next_is_document_end(&self) -> bool {
        self.src[self.pos..].starts_with(b"...") && is_blank_or_breakz(self.peek_nth(3))
    }

    fn next_is_document_indicator(&self) -> bool {
        self.next_is_document_start() || self.next_is_document_end()
    }

    /// Whether the next characters may be part of a plain scalar.
    /// See 7.3.3. Plain Style. This is context-sensitive: flow indicators only
    /// terminate a plain scalar inside a flow collection.
    fn next_can_be_plain_scalar(&self, in_flow: bool) -> bool {
        !ends_plain_scalar(self.peek(), self.peek_nth(1), in_flow)
    }

    fn error(&self, kind: ErrorKind, at: usize) -> Error {
        let end = (at + 1).min(self.src.len()).max(at);
        Error::new(kind, span(at, end))
    }

    // -------------------------------------------------------------- whitespace

    /// Record a `#` comment (cursor must be at the `#`) as trivia and consume
    /// it up to (excluding) the line break.
    fn eat_comment(&mut self) {
        debug_assert!(self.peek() == b'#');
        let start = self.pos;
        self.bump_while(|b| !is_breakz(b));
        self.comments.push(Comment { span: span(start, self.pos) });
    }

    /// Skip over whitespace, comments and line breaks until the next token.
    fn skip_to_next_token(&mut self) -> ScanResult {
        loop {
            match self.peek() {
                // Tabs may not be used as indentation. "Indentation" only
                // exists as long as a block is started, but does not exist
                // inside flow-style constructs.
                b'\t'
                    if self.is_within_block()
                        && self.leading_whitespace
                        && (self.col as isize) < self.indent =>
                {
                    self.skip_ws_to_eol(true)?;
                    // If we have content on that line with a tab, error out.
                    if !is_breakz(self.peek()) {
                        return Err(self.error(ErrorKind::TabAsIndent, self.pos));
                    }
                }
                b' ' => {
                    self.bump_space_run();
                }
                b'\t' => self.bump_blank(),
                b'\n' | b'\r' => self.bump_break_in_stream(),
                b'#' => self.eat_comment(),
                0xEF if self.next_is_bom() => self.bump_bom(),
                _ => break,
            }
        }
        Ok(())
    }

    /// Skip spaces (and optionally tabs) and comments up to the end of line.
    /// Returns `(found_tabs, has_yaml_ws)`.
    fn skip_ws_to_eol(&mut self, skip_tabs: bool) -> ScanResult<(bool, bool)> {
        let mut found_tabs = false;
        let mut has_yaml_ws = false;
        loop {
            match self.peek() {
                b' ' => {
                    has_yaml_ws = true;
                    self.bump_space_run();
                }
                b'\t' if skip_tabs => {
                    found_tabs = true;
                    self.bump_blank();
                }
                // YAML comments must be preceded by whitespace.
                b'#' if !found_tabs && !has_yaml_ws && self.col != 0 => {
                    return Err(self.error(ErrorKind::UnexpectedToken("comment"), self.pos));
                }
                b'#' => self.eat_comment(),
                _ => break,
            }
        }
        Ok((found_tabs, has_yaml_ws))
    }

    /// Skip YAML whitespace (` `, line breaks) and comments. Errors if none found.
    fn skip_yaml_whitespace(&mut self) -> ScanResult {
        let mut need_whitespace = true;
        loop {
            match self.peek() {
                b' ' => {
                    self.bump_blank();
                    need_whitespace = false;
                }
                b'\n' | b'\r' => {
                    self.bump_break_in_stream();
                    need_whitespace = false;
                }
                b'#' => self.eat_comment(),
                _ => break,
            }
        }
        if need_whitespace {
            Err(self.error(ErrorKind::UnexpectedToken("non-whitespace"), self.pos))
        } else {
            Ok(())
        }
    }

    // ------------------------------------------------------------- public API

    /// Return the next token in the stream, or `None` after `StreamEnd`.
    pub fn next_token(&mut self) -> ScanResult<Option<Token>> {
        if self.stream_end_produced {
            return Ok(None);
        }

        self.fetch_more_tokens()?;
        let Some(t) = self.tokens.pop_front() else {
            return Err(self.error(ErrorKind::UnexpectedEof, self.pos));
        };
        self.tokens_parsed += 1;

        if t.kind == TokenKind::StreamEnd {
            self.stream_end_produced = true;
        }
        Ok(Some(t))
    }

    fn fetch_more_tokens(&mut self) -> ScanResult {
        loop {
            let mut need_more = self.tokens.is_empty();
            if !need_more {
                self.stale_simple_keys()?;
                // If our next token to be emitted may be a key, fetch more context.
                need_more = self
                    .simple_keys
                    .iter()
                    .any(|sk| sk.possible && sk.token_number == self.tokens_parsed);
            }
            if !need_more {
                break;
            }
            self.fetch_next_token()?;
        }
        Ok(())
    }

    fn fetch_next_token(&mut self) -> ScanResult {
        if !self.stream_start_produced {
            self.fetch_stream_start();
            return Ok(());
        }
        self.skip_to_next_token()?;
        self.stale_simple_keys()?;

        self.unroll_indent(self.col as isize);

        if self.next_is_z() {
            self.fetch_stream_end()?;
            return Ok(());
        }

        if self.col == 0 {
            if self.peek() == b'%' {
                return self.fetch_directive();
            } else if self.next_is_document_start() {
                return self.fetch_document_indicator(TokenKind::DocumentStart);
            } else if self.next_is_document_end() {
                self.fetch_document_indicator(TokenKind::DocumentEnd)?;
                self.skip_ws_to_eol(true)?;
                if !is_breakz(self.peek()) {
                    return Err(self.error(ErrorKind::ExpectedDocumentEnd, self.pos));
                }
                return Ok(());
            }
        }

        if (self.col as isize) < self.indent {
            return Err(self.error(ErrorKind::UnexpectedIndicator, self.pos));
        }

        let c = self.peek();
        let nc = self.peek_nth(1);
        match c {
            b'[' => self.fetch_flow_collection_start(TokenKind::FlowSequenceStart),
            b'{' => self.fetch_flow_collection_start(TokenKind::FlowMappingStart),
            b']' => self.fetch_flow_collection_end(TokenKind::FlowSequenceEnd),
            b'}' => self.fetch_flow_collection_end(TokenKind::FlowMappingEnd),
            b',' => self.fetch_flow_entry(),
            b'-' if is_blank_or_breakz(nc) => self.fetch_block_entry(),
            b'?' if is_blank_or_breakz(nc) => self.fetch_key(),
            b':' if is_blank_or_breakz(nc) => self.fetch_value(),
            b':' if self.flow_level > 0
                && (is_flow(nc) || self.pos == self.adjacent_value_allowed_at) =>
            {
                self.fetch_flow_value()
            }
            b'*' => self.fetch_anchor(true),
            b'&' => self.fetch_anchor(false),
            b'!' => self.fetch_tag(),
            b'|' if self.flow_level == 0 => self.fetch_block_scalar(true),
            b'>' if self.flow_level == 0 => self.fetch_block_scalar(false),
            b'\'' => self.fetch_flow_scalar(true),
            b'"' => self.fetch_flow_scalar(false),
            b'-' if !is_blank_or_breakz(nc) => self.fetch_plain_scalar(),
            b':' | b'?' if !is_blank_or_breakz(nc) && self.flow_level == 0 => {
                self.fetch_plain_scalar()
            }
            b'%' | b'@' | b'`' => Err(self.error(ErrorKind::InvalidChar, self.pos)),
            _ => self.fetch_plain_scalar(),
        }
    }

    /// Mark simple keys that can no longer be keys as such.
    fn stale_simple_keys(&mut self) -> ScanResult {
        // Only block-context simple keys can go stale.
        if self.flow_level > 0 {
            return Ok(());
        }
        for sk in &mut self.simple_keys {
            if sk.possible
                // If not in a flow construct, simple keys cannot span multiple lines.
                && (sk.line < self.line || sk.pos + 1024 < self.pos)
            {
                if sk.required {
                    return Err(Error::point(ErrorKind::InvalidSimpleKey, sk.pos));
                }
                sk.possible = false;
            }
        }
        Ok(())
    }

    // ----------------------------------------------------------------- fetches

    fn fetch_stream_start(&mut self) {
        self.indent = -1;
        self.stream_start_produced = true;
        self.simple_key_allowed = true;
        self.tokens.push_back(Token::synthesized(TokenKind::StreamStart, 0));
        self.simple_keys.push(SimpleKey::new());
    }

    fn fetch_stream_end(&mut self) -> ScanResult {
        // If the stream ended, we won't have more context. Stale all simple
        // keys; a required one is an error.
        for sk in &mut self.simple_keys {
            if sk.required && sk.possible {
                return Err(Error::point(ErrorKind::InvalidSimpleKey, sk.pos));
            }
            sk.possible = false;
        }

        self.unroll_indent(-1);
        self.remove_simple_key()?;
        self.simple_key_allowed = false;

        self.tokens.push_back(Token::synthesized(TokenKind::StreamEnd, self.pos));
        Ok(())
    }

    /// Scan a directive line. Directives are uninterpreted: any `%NAME ...` up
    /// to the end of the line is accepted (Prettier treats unknown directives
    /// and unsupported `%YAML` versions as warnings, i.e. accepts them).
    fn fetch_directive(&mut self) -> ScanResult {
        self.unroll_indent(-1);
        self.remove_simple_key()?;
        self.simple_key_allowed = false;

        let start = self.pos;
        // Consume up to the line break, stopping before a trailing comment
        // (a comment must be preceded by whitespace to count as one).
        let mut end = self.pos;
        loop {
            let b = self.peek();
            if is_breakz(b) {
                break;
            }
            if b == b'#' && self.pos > start && is_blank(self.src[self.pos - 1]) {
                self.eat_comment();
                break;
            }
            if is_blank(b) {
                self.bump_blank();
            } else {
                // A `#` NOT preceded by whitespace is part of the directive
                // content, so the run only stops at whitespace; the comment
                // check at the loop top handles blank-preceded `#`.
                self.bump_while(|b| !is_blank_or_breakz(b));
                end = self.pos;
            }
        }
        self.tokens.push_back(Token::new(TokenKind::Directive, span(start, end)));
        if is_break(self.peek()) {
            self.bump_break();
        }
        Ok(())
    }

    fn fetch_document_indicator(&mut self, kind: TokenKind) -> ScanResult {
        self.unroll_indent(-1);
        self.remove_simple_key()?;
        self.simple_key_allowed = false;

        let start = self.pos;
        self.pos += 3;
        self.col += 3;
        self.leading_whitespace = false;

        self.tokens.push_back(Token::new(kind, span(start, self.pos)));
        Ok(())
    }

    fn fetch_flow_collection_start(&mut self, kind: TokenKind) -> ScanResult {
        // The indicators '[' and '{' may start a simple key.
        self.save_simple_key();

        self.roll_one_col_indent();
        self.increase_flow_level()?;

        self.simple_key_allowed = true;

        let start = self.pos;
        self.bump();

        if kind == TokenKind::FlowMappingStart {
            self.flow_mapping_started = true;
        } else {
            self.implicit_flow_mapping_states.push(ImplicitMappingState::Possible);
        }

        let token_end = self.pos;
        self.skip_ws_to_eol(true)?;

        self.tokens.push_back(Token::new(kind, span(start, token_end)));
        Ok(())
    }

    fn fetch_flow_collection_end(&mut self, kind: TokenKind) -> ScanResult {
        self.remove_simple_key()?;
        self.decrease_flow_level();

        self.simple_key_allowed = false;

        if kind == TokenKind::FlowSequenceEnd {
            self.end_implicit_mapping(self.pos);
            self.implicit_flow_mapping_states.pop();
        }

        let start = self.pos;
        self.bump();
        let token_end = self.pos;
        self.skip_ws_to_eol(true)?;

        // A flow collection within a flow mapping can be a key. In that case,
        // the value may be adjacent to the `:`.
        if self.flow_level > 0 {
            self.adjacent_value_allowed_at = self.pos;
        }

        self.tokens.push_back(Token::new(kind, span(start, token_end)));
        Ok(())
    }

    fn fetch_flow_entry(&mut self) -> ScanResult {
        if self.flow_level == 0 {
            return Err(self.error(ErrorKind::UnexpectedFlowIndicator, self.pos));
        }
        self.remove_simple_key()?;
        self.simple_key_allowed = true;

        self.end_implicit_mapping(self.pos);

        let start = self.pos;
        self.bump();
        let token_end = self.pos;
        self.skip_ws_to_eol(true)?;

        self.tokens.push_back(Token::new(TokenKind::FlowEntry, span(start, token_end)));
        Ok(())
    }

    fn fetch_block_entry(&mut self) -> ScanResult {
        if self.flow_level > 0 {
            return Err(self.error(ErrorKind::UnexpectedIndicator, self.pos));
        }
        if !self.simple_key_allowed {
            return Err(self.error(ErrorKind::UnexpectedIndicator, self.pos));
        }

        // An anchor or tag at column 0 cannot apply to a sequence starting at
        // column 0 of the next line (yaml-test-suite G9HC).
        if let Some(Token { span, kind: TokenKind::Anchor | TokenKind::Tag, .. }) =
            self.tokens.back()
            && self.col == 0
            && self.indent > -1
            && span_starts_at_col0(self.src, *span)
        {
            return Err(Error::new(ErrorKind::UnexpectedIndicator, *span));
        }

        let start = self.pos;
        let col = self.col;
        self.bump();

        // Generate BLOCK-SEQUENCE-START if indented.
        self.roll_indent(col, None, TokenKind::BlockSequenceStart, start);
        let (found_tabs, _) = self.skip_ws_to_eol(true)?;
        if found_tabs && self.peek() == b'-' && is_blank_or_breakz(self.peek_nth(1)) {
            return Err(self.error(ErrorKind::TabAsIndent, self.pos));
        }

        self.skip_ws_to_eol(false)?;
        if is_break(self.peek()) || is_flow(self.peek()) {
            self.roll_one_col_indent();
        }

        self.remove_simple_key()?;
        self.simple_key_allowed = true;

        self.tokens.push_back(Token::new(TokenKind::BlockEntry, span(start, start + 1)));
        Ok(())
    }

    fn fetch_key(&mut self) -> ScanResult {
        let start = self.pos;
        if self.flow_level == 0 {
            // Check if we are allowed to start a new key (not necessarily simple).
            if !self.simple_key_allowed {
                return Err(self.error(ErrorKind::UnexpectedIndicator, self.pos));
            }
            self.roll_indent(self.col, None, TokenKind::BlockMappingStart, start);
        } else {
            // The scanner, upon emitting a `Key`, will prepend a `MappingStart` event.
            self.flow_mapping_started = true;
        }

        self.remove_simple_key()?;

        self.simple_key_allowed = self.flow_level == 0;

        self.bump();
        let token_end = self.pos;
        self.skip_yaml_whitespace()?;
        if self.peek() == b'\t' {
            return Err(self.error(ErrorKind::TabAsIndent, self.pos));
        }
        self.tokens.push_back(Token::new(TokenKind::Key, span(start, token_end)));
        Ok(())
    }

    /// Fetch a value in a mapping inside a flow collection, reached through a
    /// `:` NOT followed by a blank.
    fn fetch_flow_value(&mut self) -> ScanResult {
        let nc = self.peek_nth(1);
        // `["a":[]]` is valid (adjacent value after JSON-like key) while
        // `[a:[]]` is not (`[a: []]` is; `[a:b]` is the scalar `a:b`).
        if self.pos != self.adjacent_value_allowed_at && (nc == b'[' || nc == b'{') {
            return Err(self.error(ErrorKind::UnexpectedValue, self.pos));
        }
        self.fetch_value()
    }

    /// Fetch the `Value` token (after a `:`).
    fn fetch_value(&mut self) -> ScanResult {
        let sk = *self.simple_keys.last().unwrap();
        let start = self.pos;
        let start_col = self.col;
        let is_implicit_flow_mapping =
            !self.implicit_flow_mapping_states.is_empty() && !self.flow_mapping_started;
        if is_implicit_flow_mapping {
            *self.implicit_flow_mapping_states.last_mut().unwrap() = ImplicitMappingState::Inside;
        }

        // Skip over ':'.
        self.bump();
        if self.peek() == b'\t' {
            let (_, has_ws) = self.skip_ws_to_eol(true)?;
            if !has_ws && (self.peek() == b'-' || is_alpha(self.peek())) {
                return Err(self.error(ErrorKind::TabAsIndent, self.pos));
            }
        }

        if sk.possible {
            // Insert the simple key token.
            let tok = Token::synthesized(TokenKind::Key, sk.pos);
            self.insert_token(sk.token_number - self.tokens_parsed, tok);
            if is_implicit_flow_mapping {
                if sk.line < self.line {
                    return Err(self.error(ErrorKind::UnexpectedValue, start));
                }
                self.insert_token(
                    sk.token_number - self.tokens_parsed,
                    Token::synthesized(TokenKind::FlowMappingStart, sk.pos),
                );
            }

            // Add the BLOCK-MAPPING-START token if needed.
            self.roll_indent(sk.col, Some(sk.token_number), TokenKind::BlockMappingStart, sk.pos);
            self.roll_one_col_indent();

            self.simple_keys.last_mut().unwrap().possible = false;
            self.simple_key_allowed = false;
        } else {
            if is_implicit_flow_mapping {
                self.tokens.push_back(Token::synthesized(TokenKind::FlowMappingStart, start));
            }
            // The ':' indicator follows a complex key.
            if self.flow_level == 0 {
                if !self.simple_key_allowed {
                    return Err(self.error(ErrorKind::UnexpectedValue, start));
                }
                self.roll_indent(start_col, None, TokenKind::BlockMappingStart, start);
            }
            self.roll_one_col_indent();

            self.simple_key_allowed = self.flow_level == 0;
        }
        self.tokens.push_back(Token::new(TokenKind::Value, span(start, start + 1)));
        Ok(())
    }

    fn fetch_anchor(&mut self, alias: bool) -> ScanResult {
        self.save_simple_key();
        self.simple_key_allowed = false;

        let start = self.pos;
        self.bump(); // `&` or `*`
        self.bump_while(is_anchor_char);
        if self.pos == start + 1 {
            return Err(self.error(ErrorKind::EmptyAnchorName, start));
        }
        let kind = if alias { TokenKind::Alias } else { TokenKind::Anchor };
        self.tokens.push_back(Token::new(kind, span(start, self.pos)));
        Ok(())
    }

    fn fetch_tag(&mut self) -> ScanResult {
        self.save_simple_key();
        self.simple_key_allowed = false;

        let tok = self.scan_tag()?;
        self.tokens.push_back(tok);
        Ok(())
    }

    fn scan_tag(&mut self) -> ScanResult<Token> {
        let start = self.pos;

        if self.peek_nth(1) == b'<' {
            // Verbatim tag `!<...>`.
            self.bump();
            self.bump();
            self.bump_while(is_uri_char);
            if self.peek() != b'>' {
                return Err(self.error(ErrorKind::InvalidTag, start));
            }
            self.bump();
        } else {
            // `!`, `!suffix`, `!!suffix` or `!handle!suffix`.
            self.bump(); // leading `!`
            self.bump_while(is_alpha);
            if self.peek() == b'!' {
                // It was a handle; scan the suffix.
                self.bump();
            }
            self.bump_while(is_tag_char);
        }

        if is_blank_or_breakz(self.peek()) || (self.flow_level > 0 && is_flow(self.peek())) {
            Ok(Token::new(TokenKind::Tag, span(start, self.pos)))
        } else {
            Err(self.error(ErrorKind::InvalidTag, start))
        }
    }

    fn fetch_block_scalar(&mut self, literal: bool) -> ScanResult {
        self.save_simple_key();
        self.simple_key_allowed = true;
        let tok = self.scan_block_scalar(literal)?;
        self.tokens.push_back(tok);
        Ok(())
    }

    fn scan_block_scalar(&mut self, literal: bool) -> ScanResult<Token> {
        let start = self.pos;
        let mut chomping = Chomping::Clip;
        let mut increment: usize = 0;
        let style = if literal { ScalarStyle::Literal } else { ScalarStyle::Folded };

        // Skip `|` or `>`.
        self.bump();
        self.unroll_non_block_indents();

        // Parse the header: chomping and indentation indicators, either order.
        if self.peek() == b'+' || self.peek() == b'-' {
            chomping = if self.peek() == b'+' { Chomping::Keep } else { Chomping::Strip };
            self.bump();
            if self.peek().is_ascii_digit() {
                if self.peek() == b'0' {
                    return Err(self.error(ErrorKind::InvalidBlockScalarHeader, start));
                }
                increment = (self.peek() - b'0') as usize;
                self.bump();
            }
        } else if self.peek().is_ascii_digit() {
            if self.peek() == b'0' {
                return Err(self.error(ErrorKind::InvalidBlockScalarHeader, start));
            }
            increment = (self.peek() - b'0') as usize;
            self.bump();
            if self.peek() == b'+' || self.peek() == b'-' {
                chomping = if self.peek() == b'+' { Chomping::Keep } else { Chomping::Strip };
                self.bump();
            }
        }

        self.skip_ws_to_eol(true)?;

        if !is_breakz(self.peek()) {
            return Err(self.error(ErrorKind::InvalidBlockScalarHeader, start));
        }

        if is_break(self.peek()) {
            self.bump_break();
        }
        let content_start = self.pos;

        if self.peek() == b'\t' {
            return Err(self.error(ErrorKind::TabAsIndent, self.pos));
        }

        let mut indent: usize = 0;
        if increment > 0 {
            indent = if self.indent >= 0 { (self.indent as usize) + increment } else { increment };
        }

        // Scan the leading line breaks and determine the indentation level if needed.
        if indent == 0 {
            self.skip_block_scalar_first_line_indent(&mut indent);
        } else {
            self.skip_block_scalar_indent(indent);
        }

        let header_index = BlockHeaderIndex::new(self.block_headers.len());
        self.block_headers.push(BlockScalarHeader {
            chomping,
            indent: if increment > 0 { Some(increment as u32) } else { None },
            content_start: content_start as u32,
        });

        // End-of-stream with no content, e.g. `- |+`.
        if self.next_is_z() {
            return Ok(Token::new(
                TokenKind::Scalar(style, Some(header_index)),
                span(start, self.pos),
            ));
        }

        if self.col < indent && (self.col as isize) > self.indent {
            return Err(self.error(ErrorKind::InvalidBlockScalarIndent, self.pos));
        }

        while self.col == indent && !(self.next_is_z()) {
            if indent == 0 && self.next_is_document_indicator() {
                break;
            }

            // Consume the content line, in bulk.
            self.bump_while(|b| !is_breakz(b));

            if self.next_is_z() {
                break;
            }
            self.bump_break();

            // Eat the following indentation spaces and line breaks.
            self.skip_block_scalar_indent(indent);
        }

        Ok(Token::new(TokenKind::Scalar(style, Some(header_index)), span(start, self.pos)))
    }

    /// Skip the block scalar indentation and empty lines.
    fn skip_block_scalar_indent(&mut self, indent: usize) {
        loop {
            // Bounded bulk run of indentation spaces.
            while self.col < indent && self.peek() == b' ' {
                let want = indent - self.col;
                let mut i = self.pos;
                let limit = (self.pos + want).min(self.src.len());
                while i < limit && self.src[i] == b' ' {
                    i += 1;
                }
                self.col += i - self.pos;
                self.pos = i;
            }
            if is_break(self.peek()) {
                self.bump_break();
            } else {
                break;
            }
        }
    }

    /// Determine the indentation level for a block scalar from the first line
    /// of its contents.
    fn skip_block_scalar_first_line_indent(&mut self, indent: &mut usize) {
        let mut max_indent = 0;
        loop {
            self.bump_space_run();
            if self.col > max_indent {
                max_indent = self.col;
            }
            if is_break(self.peek()) {
                self.bump_break();
            } else {
                break;
            }
        }

        *indent = max_indent.max((self.indent + 1) as usize);
        if self.indent > 0 {
            *indent = (*indent).max(1);
        }
    }

    fn fetch_flow_scalar(&mut self, single: bool) -> ScanResult {
        self.save_simple_key();
        self.simple_key_allowed = false;

        let tok = self.scan_flow_scalar(single)?;

        // To ensure JSON compatibility, if a key inside a flow mapping is
        // JSON-like, YAML allows the following value to be adjacent to the `:`.
        self.skip_to_next_token()?;
        self.adjacent_value_allowed_at = self.pos;

        self.tokens.push_back(tok);
        Ok(())
    }

    fn scan_flow_scalar(&mut self, single: bool) -> ScanResult<Token> {
        let start = self.pos;
        let start_line = self.line;

        // Eat the left quote.
        self.bump();

        loop {
            if self.col == 0 && self.next_is_document_indicator() {
                return Err(self.error(ErrorKind::UnterminatedFlowScalar, start));
            }
            if self.next_is_z() {
                return Err(self.error(ErrorKind::UnterminatedFlowScalar, start));
            }
            if (self.col as isize) < self.indent {
                return Err(self.error(ErrorKind::UnterminatedFlowScalar, start));
            }

            // Consume non-whitespace characters, in bulk up to the next
            // special character (quote / escape / whitespace).
            let mut done = false;
            while !is_blank_or_breakz(self.peek()) {
                if single {
                    self.bump_while(|b| !is_blank_or_breakz(b) && b != b'\'');
                } else {
                    self.bump_while(|b| !is_blank_or_breakz(b) && b != b'"' && b != b'\\');
                }
                match self.peek() {
                    b'\'' if single && self.peek_nth(1) == b'\'' => {
                        self.bump();
                        self.bump();
                    }
                    b'\'' if single => {
                        done = true;
                        break;
                    }
                    b'"' if !single => {
                        done = true;
                        break;
                    }
                    b'\\' if !single && is_break(self.peek_nth(1)) => {
                        self.bump();
                        self.bump_break();
                        break;
                    }
                    b'\\' if !single => {
                        self.scan_flow_scalar_escape(start)?;
                    }
                    _ => break,
                }
            }
            if done {
                break;
            }

            // Consume blank characters.
            loop {
                match self.peek() {
                    b' ' => {
                        self.bump_space_run();
                    }
                    b'\t' => {
                        if self.leading_whitespace && (self.col as isize) < self.indent {
                            return Err(self.error(ErrorKind::TabAsIndent, self.pos));
                        }
                        self.bump_blank();
                    }
                    b'\n' | b'\r' => self.bump_break(),
                    _ => break,
                }
            }
        }

        // Eat the right quote.
        self.bump();
        let token_end = self.pos;
        // Ensure there is no invalid trailing content.
        self.skip_ws_to_eol(true)?;
        match self.peek() {
            // These can be encountered in flow sequences or mappings.
            b',' | b'}' | b']' if self.flow_level > 0 => {}
            // An end-of-line / end-of-stream is fine. No trailing content.
            c if is_breakz(c) => {}
            // `:` can be encountered if our scalar is a key. Outside of flow
            // contexts, keys cannot span multiple lines.
            b':' if self.flow_level > 0 || self.line == start_line => {}
            _ => {
                return Err(
                    self.error(ErrorKind::UnexpectedToken("content after quoted scalar"), self.pos)
                );
            }
        }

        let style = if single { ScalarStyle::SingleQuoted } else { ScalarStyle::DoubleQuoted };
        Ok(Token::new(TokenKind::Scalar(style, None), span(start, token_end)))
    }

    /// Validate an escape sequence in a double quoted scalar. The cursor must
    /// be at the `\`.
    fn scan_flow_scalar_escape(&mut self, start: usize) -> ScanResult {
        let code_length = match self.peek_nth(1) {
            b'0' | b'a' | b'b' | b't' | b'\t' | b'n' | b'v' | b'f' | b'r' | b'e' | b' ' | b'"'
            | b'/' | b'\\' | b'N' | b'_' | b'L' | b'P' => 0,
            b'x' => 2,
            b'u' => 4,
            b'U' => 8,
            _ => {
                return Err(self.error(ErrorKind::UnexpectedToken("escape character"), start));
            }
        };
        self.bump();
        self.bump();

        if code_length > 0 {
            let mut value: u32 = 0;
            for i in 0..code_length {
                let c = self.peek_nth(i);
                if !c.is_ascii_hexdigit() {
                    return Err(self.error(ErrorKind::UnexpectedToken("hexadecimal digit"), start));
                }
                let digit = match c {
                    b'0'..=b'9' => c - b'0',
                    b'a'..=b'f' => c - b'a' + 10,
                    _ => c - b'A' + 10,
                };
                value = (value << 4) + u32::from(digit);
            }
            if char::from_u32(value).is_none() {
                return Err(self.error(ErrorKind::InvalidChar, start));
            }
            // Hex digits are ASCII; advance in one step.
            self.pos += code_length;
            self.col += code_length;
        }
        Ok(())
    }

    fn fetch_plain_scalar(&mut self) -> ScanResult {
        self.save_simple_key();
        self.simple_key_allowed = false;

        let tok = self.scan_plain_scalar()?;
        self.tokens.push_back(tok);
        Ok(())
    }

    fn scan_plain_scalar(&mut self) -> ScanResult<Token> {
        self.unroll_non_block_indents();
        let indent = self.indent + 1;
        let start = self.pos;

        if self.flow_level > 0 && (self.col as isize) < indent {
            return Err(self.error(ErrorKind::UnexpectedIndicator, start));
        }

        let mut end = self.pos;
        let mut consumed_content = false;

        loop {
            if (self.leading_whitespace && self.next_is_document_indicator()) || self.peek() == b'#'
            {
                break;
            }

            if self.flow_level > 0 && self.peek() == b'-' && is_flow(self.peek_nth(1)) {
                return Err(self.error(ErrorKind::UnexpectedIndicator, self.pos));
            }

            if !is_blank_or_breakz(self.peek())
                && self.next_can_be_plain_scalar(self.flow_level > 0)
            {
                self.leading_whitespace = false;
                // Add content non-blank characters to the scalar, in bulk.
                let in_flow = self.flow_level > 0;
                let run_start = self.pos;
                let mut i = self.pos;
                while let Some(&b) = self.src.get(i) {
                    let next = self.src.get(i + 1).copied().unwrap_or(0);
                    if is_blank_or_breakz(b) || ends_plain_scalar(b, next, in_flow) {
                        break;
                    }
                    i += 1;
                }
                if i > run_start {
                    self.col += char_count(&self.src[run_start..i]);
                    self.pos = i;
                }
                end = self.pos;
                consumed_content = true;
            }

            // We may reach the end of a plain scalar if we reach EOF, `: ` or
            // a flow character in a flow context.
            if !(is_blank(self.peek()) || is_break(self.peek())) {
                break;
            }

            // Process blank characters.
            loop {
                match self.peek() {
                    b' ' => {
                        self.bump_space_run();
                    }
                    b'\t' if self.leading_whitespace && (self.col as isize) < indent => {
                        // Tabs in an indentation column are allowed if and
                        // only if the line is empty.
                        self.skip_ws_to_eol(true)?;
                        if !is_breakz(self.peek()) {
                            return Err(self.error(ErrorKind::TabAsIndent, self.pos));
                        }
                    }
                    b'\t' => self.bump_blank(),
                    b'\n' | b'\r' => self.bump_break(),
                    _ => break,
                }
            }

            // Check indentation level.
            if self.flow_level == 0 && (self.col as isize) < indent {
                break;
            }
        }

        if self.leading_whitespace {
            self.simple_key_allowed = true;
        }

        if consumed_content {
            Ok(Token::new(TokenKind::Scalar(ScalarStyle::Plain, None), span(start, end)))
        } else {
            // `fetch_plain_scalar` must absolutely consume at least one byte;
            // an empty plain scalar happens with erroneous inputs like `{...`.
            Err(self.error(ErrorKind::ExpectedNode, start))
        }
    }

    // ------------------------------------------------------- indent bookkeeping

    /// Add an indentation level to the stack with the given block token, if needed.
    fn roll_indent(&mut self, col: usize, number: Option<usize>, kind: TokenKind, at: usize) {
        if self.flow_level > 0 {
            return;
        }

        // If the last indent was a non-block indent, remove it. We prepared an
        // indent that we thought we wouldn't use, but realized just now that
        // it is a block indent.
        if self.indent <= col as isize
            && let Some(indent) = self.indents.last()
            && !indent.needs_block_end
        {
            self.indent = indent.indent;
            self.indents.pop();
        }

        if self.indent < col as isize {
            self.indents.push(Indent { indent: self.indent, needs_block_end: true });
            self.indent = col as isize;
            let tokens_parsed = self.tokens_parsed;
            match number {
                Some(n) => self.insert_token(n - tokens_parsed, Token::synthesized(kind, at)),
                None => self.tokens.push_back(Token::synthesized(kind, at)),
            }
        }
    }

    /// Pop indentation levels from the stack as much as needed.
    fn unroll_indent(&mut self, col: isize) {
        if self.flow_level > 0 {
            return;
        }
        while self.indent > col {
            let indent = self.indents.pop().unwrap();
            self.indent = indent.indent;
            if indent.needs_block_end {
                self.tokens.push_back(Token::synthesized(TokenKind::BlockEnd, self.pos));
            }
        }
    }

    /// Add an indentation level of 1 column that does not start a block.
    fn roll_one_col_indent(&mut self) {
        if self.flow_level == 0 && self.indents.last().is_some_and(|x| x.needs_block_end) {
            self.indents.push(Indent { indent: self.indent, needs_block_end: false });
            self.indent += 1;
        }
    }

    /// Unroll all indents created with [`Self::roll_one_col_indent`].
    fn unroll_non_block_indents(&mut self) {
        while let Some(indent) = self.indents.last() {
            if indent.needs_block_end {
                break;
            }
            self.indent = indent.indent;
            self.indents.pop();
        }
    }

    // --------------------------------------------------- simple key bookkeeping

    /// Mark the next token to be inserted as a potential simple key.
    fn save_simple_key(&mut self) {
        if self.simple_key_allowed {
            let required = self.flow_level == 0
                && self.indent == (self.col as isize)
                && self.indents.last().is_some_and(|i| i.needs_block_end);
            let sk = SimpleKey {
                possible: true,
                required,
                token_number: self.tokens_parsed + self.tokens.len(),
                pos: self.pos,
                line: self.line,
                col: self.col,
            };
            *self.simple_keys.last_mut().unwrap() = sk;
        }
    }

    fn remove_simple_key(&mut self) -> ScanResult {
        let last = self.simple_keys.last_mut().unwrap();
        if last.possible && last.required {
            return Err(Error::point(ErrorKind::InvalidSimpleKey, last.pos));
        }
        last.possible = false;
        Ok(())
    }

    // ----------------------------------------------------------- flow plumbing

    fn increase_flow_level(&mut self) -> ScanResult {
        self.simple_keys.push(SimpleKey::new());
        self.flow_level = self
            .flow_level
            .checked_add(1)
            .ok_or_else(|| self.error(ErrorKind::UnexpectedIndicator, self.pos))?;
        Ok(())
    }

    fn decrease_flow_level(&mut self) {
        if self.flow_level > 0 {
            self.flow_level -= 1;
            self.simple_keys.pop().unwrap();
        }
    }

    /// If an implicit mapping had started, end it (does not pop the state).
    fn end_implicit_mapping(&mut self, at: usize) {
        if let Some(implicit_mapping) = self.implicit_flow_mapping_states.last_mut()
            && *implicit_mapping == ImplicitMappingState::Inside
        {
            self.flow_mapping_started = false;
            *implicit_mapping = ImplicitMappingState::Possible;
            self.tokens.push_back(Token::synthesized(TokenKind::FlowMappingEnd, at));
        }
    }

    /// Return whether the scanner is inside a block but outside a flow sequence.
    fn is_within_block(&self) -> bool {
        !self.indents.is_empty()
    }

    /// Insert a token at the given position.
    fn insert_token(&mut self, pos: usize, tok: Token) {
        assert!(pos <= self.tokens.len());
        self.tokens.insert(pos, tok);
    }
}

/// Construct a [`Span`] from byte-offset cursor positions.
#[inline]
fn span(start: usize, end: usize) -> Span {
    Span::new(start as u32, end as u32)
}

/// The number of characters in a byte slice (counting non-continuation bytes).
#[inline]
fn char_count(bytes: &[u8]) -> usize {
    // `is_ascii` is vectorized in std; content runs are overwhelmingly ASCII.
    if bytes.is_ascii() {
        bytes.len()
    } else {
        bytes.iter().filter(|&&b| (b & 0xC0) != 0x80).count()
    }
}

/// Whether the given span starts at column 0 (its start is at the beginning of
/// the source or right after a line break).
fn span_starts_at_col0(src: &[u8], span: Span) -> bool {
    span.start == 0 || matches!(src.get(span.start as usize - 1), Some(b'\n' | b'\r'))
}