ayaml 0.1.3

A safe, pure-Rust YAML parser, event stream, value model, Serde layer, and emitter
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
use super::{
    Anchor, Call, CollectionStyle, Cont, Cow, DUPLICATE_PROPERTIES, Error, ErrorKind, Event,
    FlowLack, FlowState, LineIndex, Parser, Properties, Run, ScalarStyle, Span, Step, Tag, Token,
    TokenKind, implicit_key_fits,
};

impl<'a> Parser<'a> {
    pub(super) fn flow_sequence(
        &mut self,
        tokens: Run,
        start: usize,
        depth: usize,
        tag: Option<Tag>,
        anchor: Option<Anchor<'a>>,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let start_span = self.token(tokens, start).span;
        self.limits
            .check_depth(depth + 1)
            .map_err(|error| self.limit_at(start_span, error))?;
        self.emit(Event::SequenceStart {
            tag,
            anchor,
            style: CollectionStyle::Flow,
            span: start_span,
        })?;
        let state = FlowState {
            tokens,
            start_span,
            index: start + 1,
            entries: 0,
            depth,
        };
        self.flow_sequence_loop(state, stack)
    }

    pub(super) fn flow_sequence_loop(
        &mut self,
        mut state: FlowState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let tokens = state.tokens;
        let index = state.index;
        if index >= tokens.len() {
            return Err(self.flow_error(tokens, index, FlowLack::Separator));
        }
        let token = self.token(tokens, index);
        if matches!(token.kind, TokenKind::FlowSequenceEnd) {
            let end_span = token.span;
            self.emit(Event::SequenceEnd { span: end_span })?;
            return Ok(Step::Return(index + 1));
        }
        if matches!(token.kind, TokenKind::FlowEntry) {
            return Err(self.flow_error(
                tokens,
                index,
                FlowLack::Node("unexpected empty flow sequence entry".to_owned()),
            ));
        }
        state.entries += 1;
        self.limits
            .check_collection_entries(state.entries)
            .map_err(|error| self.limit_at(token.span, error))?;
        let depth = state.depth + 1;
        if matches!(self.token(tokens, index).kind, TokenKind::Key) {
            stack.push(Cont::FlowSequenceItem {
                state,
                inline: false,
            });
            return Ok(Step::Call(Call::FlowExplicitMapping {
                tokens,
                start: index,
                depth,
            }));
        }
        if let Some(colon) = self.implicit_flow_colon(self.tokens(tokens), index) {
            stack.push(Cont::FlowSequenceItem {
                state,
                inline: false,
            });
            return Ok(Step::Call(Call::ImplicitFlowMapping {
                tokens,
                start: index,
                colon,
                depth,
            }));
        }
        stack.push(Cont::FlowSequenceItem {
            state,
            inline: true,
        });
        Ok(Step::Call(Call::Inline {
            tokens: tokens.from(index),
            depth,
            tag: None,
            anchor: None,
        }))
    }

    pub(super) fn flow_explicit_mapping(
        &mut self,
        tokens: Run,
        start: usize,
        depth: usize,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        // The pair is a mapping inside the sequence, so its key and value
        // nest one level deeper than the sequence's items.
        let depth = depth + 1;
        let start_span = self.token(tokens, start).span;
        self.limits
            .check_depth(depth)
            .map_err(|error| self.limit_at(start_span, error))?;
        self.emit(Event::MappingStart {
            tag: None,
            anchor: None,
            style: CollectionStyle::Flow,
            span: start_span,
        })?;
        let key_start = start + 1;
        let key_end = self.find_flow_value(self.tokens(tokens), key_start);
        let key = tokens.slice(key_start, key_end);
        if key.is_empty() {
            self.emit(self.null_scalar(Span::empty(start_span.end())))?;
            return self.flow_explicit_value(tokens, start_span, key_end, depth, stack);
        }
        stack.push(Cont::FlowExplicitKey {
            tokens,
            start_span,
            key,
            key_end,
            depth,
        });
        Ok(Step::Call(Call::Inline {
            tokens: key,
            depth,
            tag: None,
            anchor: None,
        }))
    }

    pub(super) fn flow_explicit_value(
        &mut self,
        tokens: Run,
        start_span: Span,
        key_end: usize,
        depth: usize,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let mut index = key_end;
        if index < tokens.len() && matches!(self.token(tokens, index).kind, TokenKind::Value) {
            index += 1;
            let value_end = self.find_flow_item_end(self.tokens(tokens), index);
            if index == value_end {
                let span = Span::empty(self.token(tokens, index - 1).span.end());
                self.emit(self.null_scalar(span))?;
                return self.flow_mapping_end(tokens, start_span, value_end);
            }
            let value = tokens.slice(index, value_end);
            stack.push(Cont::FlowExplicitValue {
                tokens,
                start_span,
                value,
                value_end,
            });
            return Ok(Step::Call(Call::Inline {
                tokens: value,
                depth,
                tag: None,
                anchor: None,
            }));
        }
        let span = self.empty_before(tokens, index, start_span);
        self.emit(self.null_scalar(span))?;
        self.flow_mapping_end(tokens, start_span, index)
    }

    /// End a single-pair flow mapping before the token at `index`.
    pub(super) fn flow_mapping_end(
        &mut self,
        tokens: Run,
        start_span: Span,
        index: usize,
    ) -> Result<Step<'a>, Error> {
        let span = self.empty_before(tokens, index, start_span);
        self.emit(Event::MappingEnd { span })?;
        Ok(Step::Return(index))
    }

    pub(super) fn implicit_flow_mapping(
        &mut self,
        tokens: Run,
        start: usize,
        colon: usize,
        depth: usize,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        // As in `flow_explicit_mapping`, the pair is one level deeper.
        let depth = depth + 1;
        let start_span = self.token(tokens, start).span;
        self.limits
            .check_depth(depth)
            .map_err(|error| self.limit_at(start_span, error))?;
        self.emit(Event::MappingStart {
            tag: None,
            anchor: None,
            style: CollectionStyle::Flow,
            span: start_span,
        })?;
        let key_start = start
            + usize::from(matches!(
                self.token_at(tokens, start).map(|token| &token.kind),
                Some(TokenKind::Key)
            ));
        let key = tokens.slice(key_start, colon);
        if key.is_empty() {
            let span = Span::empty(self.token(tokens, colon).span.start());
            self.emit(self.null_scalar(span))?;
            return self.implicit_flow_value(tokens, start_span, colon, depth, stack);
        }
        stack.push(Cont::ImplicitFlowKey {
            tokens,
            start_span,
            key,
            colon,
            depth,
        });
        Ok(Step::Call(Call::Inline {
            tokens: key,
            depth,
            tag: None,
            anchor: None,
        }))
    }

    pub(super) fn implicit_flow_value(
        &mut self,
        tokens: Run,
        start_span: Span,
        colon: usize,
        depth: usize,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let value_start = colon + 1;
        let value_end = self.find_flow_item_end(self.tokens(tokens), value_start);
        if value_start >= tokens.len() || value_start == value_end {
            let span = Span::empty(self.token(tokens, colon).span.end());
            self.emit(self.null_scalar(span))?;
            return self.flow_mapping_end(tokens, start_span, value_end);
        }
        let value = tokens.slice(value_start, value_end);
        stack.push(Cont::ImplicitFlowValue {
            tokens,
            start_span,
            value,
            value_end,
        });
        Ok(Step::Call(Call::Inline {
            tokens: value,
            depth,
            tag: None,
            anchor: None,
        }))
    }

    pub(super) fn flow_mapping(
        &mut self,
        tokens: Run,
        start: usize,
        depth: usize,
        tag: Option<Tag>,
        anchor: Option<Anchor<'a>>,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let start_span = self.token(tokens, start).span;
        self.limits
            .check_depth(depth + 1)
            .map_err(|error| self.limit_at(start_span, error))?;
        self.emit(Event::MappingStart {
            tag,
            anchor,
            style: CollectionStyle::Flow,
            span: start_span,
        })?;
        let state = FlowState {
            tokens,
            start_span,
            index: start + 1,
            entries: 0,
            depth,
        };
        self.flow_mapping_loop(state, stack)
    }

    pub(super) fn flow_mapping_loop(
        &mut self,
        mut state: FlowState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let tokens = state.tokens;
        let mut index = state.index;
        if index >= tokens.len() {
            return Err(self.flow_error(tokens, index, FlowLack::Separator));
        }
        let token = self.token(tokens, index);
        if matches!(token.kind, TokenKind::FlowMappingEnd) {
            let end_span = token.span;
            self.emit(Event::MappingEnd { span: end_span })?;
            return Ok(Step::Return(index + 1));
        }
        if matches!(token.kind, TokenKind::FlowEntry) {
            return Err(self.flow_error(
                tokens,
                index,
                FlowLack::Node("unexpected empty flow mapping entry".to_owned()),
            ));
        }
        if matches!(token.kind, TokenKind::Key) {
            index += 1;
        }
        let key_end = self.find_flow_value(self.tokens(tokens), index);
        let key = tokens.slice(index, key_end);
        state.index = key_end;
        if key.is_empty() {
            // Where a key starts, after `{`, `,` or `?`, nothing stands for it, so a `]` is where a
            // node belongs, as in a sequence. After an entry with no `,`, a separator is missing.
            if index > 0
                && matches!(
                    self.token(tokens, index - 1).kind,
                    TokenKind::FlowMappingStart | TokenKind::FlowEntry | TokenKind::Key
                )
                && self
                    .token_at(tokens, key_end)
                    .is_some_and(|token| matches!(token.kind, TokenKind::FlowSequenceEnd))
            {
                return Err(self.flow_error(
                    tokens,
                    key_end,
                    FlowLack::Node("unexpected ] in node position".to_owned()),
                ));
            }
            let span = Span::empty(self.token(tokens, index.saturating_sub(1)).span.end());
            self.emit(self.null_scalar(span))?;
            return self.flow_mapping_after_key(state, stack);
        }
        let depth = state.depth + 1;
        stack.push(Cont::FlowMappingKey { state, key });
        Ok(Step::Call(Call::Inline {
            tokens: key,
            depth,
            tag: None,
            anchor: None,
        }))
    }

    pub(super) fn flow_mapping_after_key(
        &mut self,
        mut state: FlowState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let tokens = state.tokens;
        let mut index = state.index;
        // A key ends at the end of its tokens, or at a `]` that closes nothing here.
        let Some(token) = self
            .token_at(tokens, index)
            .filter(|token| !matches!(token.kind, TokenKind::FlowSequenceEnd))
        else {
            return Err(self.flow_error(tokens, index, FlowLack::Separator));
        };
        state.entries += 1;
        let span = token.span;
        self.limits
            .check_collection_entries(state.entries)
            .map_err(|error| self.limit_at(span, error))?;
        if matches!(self.token(tokens, index).kind, TokenKind::Value) {
            index += 1;
        } else {
            // Flow mappings permit omitted values: `{key, other: value}`.
            let span = self.empty_before(tokens, index, state.start_span);
            self.emit(self.null_scalar(span))?;
        }
        if index >= tokens.len()
            || matches!(
                self.token(tokens, index).kind,
                TokenKind::FlowEntry | TokenKind::FlowMappingEnd
            )
        {
            if matches!(
                self.token(tokens, index.saturating_sub(1)).kind,
                TokenKind::Value
            ) {
                let span = self.empty_before(tokens, index, state.start_span);
                self.emit(self.null_scalar(span))?;
            }
            state.index = index;
            // Through the driver, as for block `?` entries: `{:, :, …}`
            // must not nest a native frame per entry.
            stack.push(Cont::FlowMappingNext(state));
            return Ok(Step::Return(0));
        }
        let value_end = self.find_flow_item_end(self.tokens(tokens), index);
        // Only a `]` ends a value before it starts, since the other closers returned above.
        if value_end == index {
            return Err(self.flow_error(
                tokens,
                index,
                FlowLack::Node("unexpected ] in node position".to_owned()),
            ));
        }
        let value = tokens.slice(index, value_end);
        state.index = value_end;
        let depth = state.depth + 1;
        stack.push(Cont::FlowMappingValue { state, value });
        Ok(Step::Call(Call::Inline {
            tokens: value,
            depth,
            tag: None,
            anchor: None,
        }))
    }

    pub(super) fn flow_mapping_next(
        &mut self,
        mut state: FlowState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        if state.index < state.tokens.len()
            && matches!(
                self.token(state.tokens, state.index).kind,
                TokenKind::FlowEntry
            )
        {
            state.index += 1;
        }
        self.flow_mapping_loop(state, stack)
    }

    #[allow(
        clippy::unused_self,
        reason = "the method is part of a uniform grammar or diagnostic surface"
    )]
    pub(super) fn implicit_flow_colon(&self, tokens: &[Token<'a>], start: usize) -> Option<usize> {
        let mut depth = 0usize;
        for (index, token) in tokens.iter().enumerate().skip(start) {
            match token.kind {
                TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart => depth += 1,
                TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => {
                    if depth == 0 {
                        return None;
                    }
                    depth -= 1;
                }
                TokenKind::Value if depth == 0 => {
                    let key_start = tokens[start].span.start();
                    let implicit_key = key_start.line() == token.span.start().line()
                        && implicit_key_fits(key_start, token.span.start());
                    if implicit_key || matches!(tokens[start].kind, TokenKind::Key) {
                        return Some(index);
                    }
                    return None;
                }
                TokenKind::FlowEntry if depth == 0 => return None,
                _ => {}
            }
        }
        None
    }

    /// Where the flow key from `start` ends. With no end in sight the parse goes on to the end of
    /// `tokens`, and finds what is wrong in order.
    #[allow(
        clippy::match_same_arms,
        reason = "the arms stay separate to document each representation or state"
    )]
    #[allow(
        clippy::unused_self,
        reason = "the method is part of a uniform grammar or diagnostic surface"
    )]
    pub(super) fn find_flow_value(&self, tokens: &[Token<'a>], start: usize) -> usize {
        let mut depth = 0usize;
        for (index, token) in tokens.iter().enumerate().skip(start) {
            match token.kind {
                TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart => depth += 1,
                TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => {
                    if depth == 0 {
                        return index;
                    }
                    depth -= 1;
                }
                TokenKind::Value if depth == 0 => return index,
                TokenKind::FlowEntry if depth == 0 => return index,
                _ => {}
            }
        }
        tokens.len()
    }

    /// Where the flow item from `start` ends, on the same terms as [`Self::find_flow_value`].
    #[allow(
        clippy::unused_self,
        reason = "the method is part of a uniform grammar or diagnostic surface"
    )]
    pub(super) fn find_flow_item_end(&self, tokens: &[Token<'a>], start: usize) -> usize {
        let mut depth = 0usize;
        for (index, token) in tokens.iter().enumerate().skip(start) {
            match token.kind {
                TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart => depth += 1,
                TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => {
                    if depth == 0 {
                        return index;
                    }
                    depth -= 1;
                }
                TokenKind::FlowEntry if depth == 0 => return index,
                _ => {}
            }
        }
        tokens.len()
    }

    /// The error for a flow collection that fails at `index` of `tokens`, or at the end of the
    /// input past the last of them. Inside a collection it names the failure and the opening.
    #[cold]
    pub(super) fn flow_error(&self, tokens: Run, index: usize, lack: FlowLack) -> Error {
        let failure = self.token_at(tokens, index).map(|token| token.span);
        let span = failure.unwrap_or_else(|| self.end_of_input());
        let Some((opening, sequence)) = self.open_flow(tokens, index) else {
            return self.error(
                span,
                match lack {
                    FlowLack::Separator => "expected ',' or a closing bracket".to_owned(),
                    FlowLack::Node(problem) => problem,
                },
            );
        };
        let (name, closing) = if sequence {
            ("sequence", ']')
        } else {
            ("mapping", '}')
        };
        let from = opening.start();
        let there = format!("at line {} column {}", from.line(), from.column());
        let lack = if self.compat {
            self.libyaml_flow_lack(tokens, index, sequence, lack)
        } else {
            lack
        };
        // The failure's location goes between the two when the error is displayed.
        let (problem, context) = match lack {
            FlowLack::Separator if self.compat => (
                format!("did not find expected ',' or '{closing}'"),
                format!(", while parsing a flow {name} {there}"),
            ),
            FlowLack::Node(_) if self.compat => (
                "did not find expected node content".to_owned(),
                format!(", while parsing a flow {name} {there}"),
            ),
            FlowLack::Separator if failure.is_none() => (
                format!("unterminated flow {name}"),
                format!(", opened {there}"),
            ),
            FlowLack::Separator => (
                format!("expected ',' or '{closing}'"),
                format!(", in the flow {name} opened {there}"),
            ),
            FlowLack::Node(problem) => (problem, format!(", in the flow {name} opened {there}")),
        };
        self.error(span, problem).with_context(&context)
    }

    /// What libyaml's parser lacks where compat's flow collection fails at `index` of `tokens`, or
    /// at the end of the input past them, for its wording. `sequence` is whether the innermost
    /// open collection is a sequence.
    pub(super) fn libyaml_flow_lack(
        &self,
        tokens: Run,
        index: usize,
        sequence: bool,
        lack: FlowLack,
    ) -> FlowLack {
        // The tokens before the failure on its logical line, which a run that starts at an item
        // leaves out.
        let line = &self.lines[tokens.line];
        let at = line.start + tokens.start + index.min(tokens.len());
        let before = |back: usize| {
            at.checked_sub(back)
                .filter(|&token| token >= line.start)
                .map(|token| &self.token_arena[token].kind)
        };
        let failing = self.token_at(tokens, index).map(|token| &token.kind);
        // In a flow sequence, libyaml takes the `:`, `,` or `]` after an empty `?` key as part of
        // the pair, then ends the pair and asks for a `,` or `]` (`[? :`, `[? ,}`).
        if sequence
            && matches!(before(2), Some(TokenKind::Key))
            && matches!(before(1), Some(TokenKind::Value | TokenKind::FlowEntry))
            && !matches!(failing, Some(TokenKind::Value | TokenKind::FlowEntry))
        {
            return FlowLack::Separator;
        }
        // Where an entry, a key or a value starts, it asks for a node: at the end of the input
        // (`[a,`, `{a:`), and at a `}` in a sequence (`[a: }`, `[? }`).
        match lack {
            FlowLack::Separator
                if (failing.is_none()
                    || sequence && matches!(failing, Some(TokenKind::FlowMappingEnd)))
                    && matches!(
                        before(1),
                        Some(
                            TokenKind::FlowSequenceStart
                                | TokenKind::FlowMappingStart
                                | TokenKind::FlowEntry
                                | TokenKind::Key
                                | TokenKind::Value
                        )
                    ) =>
            {
                FlowLack::Node(String::new())
            }
            lack => lack,
        }
    }

    /// The innermost flow collection still open before `index` of `tokens`: its opening token's
    /// span, and whether it is a sequence. A closing bracket here closes what it follows, since
    /// the parse stops at the first one that doesn't match.
    pub(super) fn open_flow(&self, tokens: Run, index: usize) -> Option<(Span, bool)> {
        let line = &self.lines[tokens.line];
        let before = (line.start + tokens.start + index).min(line.end);
        let mut closed = 0_usize;
        for token in self.token_arena[line.start..before].iter().rev() {
            match token.kind {
                TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => closed += 1,
                TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart if closed > 0 => {
                    closed -= 1;
                }
                TokenKind::FlowSequenceStart => return Some((token.span, true)),
                TokenKind::FlowMappingStart => return Some((token.span, false)),
                _ => {}
            }
        }
        None
    }

    /// An empty span at the end of the input, counted as the scanner counts it.
    pub(super) fn end_of_input(&self) -> Span {
        Span::empty(LineIndex::end_marker(self.input, self.compat))
    }

    /// A node's properties at the start of `run`, and how many tokens they are. A node
    /// has at most one tag and one anchor, in either order.
    #[inline]
    pub(super) fn properties(&self, run: Run) -> Result<Properties<'a>, Error> {
        // Most nodes have none.
        if !self.starts_with_property(run) {
            return Ok((None, None, 0));
        }
        self.read_properties(run)
    }

    #[inline]
    pub(super) fn starts_with_property(&self, run: Run) -> bool {
        matches!(
            self.tokens(run).first().map(|token| &token.kind),
            Some(TokenKind::Tag(_) | TokenKind::Anchor(_))
        )
    }

    pub(super) fn read_properties(&self, run: Run) -> Result<Properties<'a>, Error> {
        let mut tag = None;
        let mut anchor = None;
        let mut index = 0;
        for token in self.tokens(run) {
            let duplicate = match &token.kind {
                TokenKind::Tag(value) => tag.replace(value.clone()).is_some(),
                TokenKind::Anchor(value) => anchor.replace(Anchor::new(value.clone())).is_some(),
                _ => break,
            };
            if duplicate {
                return Err(self.error(token.span, DUPLICATE_PROPERTIES));
            }
            index += 1;
        }
        Ok((tag, anchor, index))
    }

    /// All of line `index`'s tokens.
    pub(super) fn line_first(&self, index: usize) -> Option<&Token<'a>> {
        let line = self.lines.get(index)?;
        if line.is_empty() {
            return None;
        }
        self.token_arena.get(line.start)
    }

    pub(super) fn line_last(&self, index: usize) -> Option<&Token<'a>> {
        let line = self.lines.get(index)?;
        if line.is_empty() {
            return None;
        }
        line.end
            .checked_sub(1)
            .and_then(|end| self.token_arena.get(end))
    }

    pub(super) fn line_tokens(&self, index: usize) -> &[Token<'a>] {
        let line = &self.lines[index];
        &self.token_arena[line.start..line.end]
    }

    pub(super) fn take_line_first_span(&mut self, index: usize) -> Option<Span> {
        let line = self.lines.get_mut(index)?;
        if line.is_empty() {
            return None;
        }
        let span = self.token_arena[line.start].span;
        line.advance(1);
        Some(span)
    }

    pub(super) fn line_run(&self, index: usize) -> Run {
        Run {
            line: index,
            start: 0,
            end: self.lines[index].len(),
        }
    }

    pub(super) fn tokens(&self, run: Run) -> &[Token<'a>] {
        let line = &self.lines[run.line];
        &self.token_arena[line.start + run.start..line.start + run.end]
    }

    /// The token at `index` within `run`, which must exist.
    pub(super) fn token(&self, run: Run, index: usize) -> &Token<'a> {
        &self.tokens(run)[index]
    }

    pub(super) fn token_at(&self, run: Run, index: usize) -> Option<&Token<'a>> {
        self.tokens(run).get(index)
    }

    /// An empty span at the start of `run`'s token at `index`, or, past the
    /// end of the run, at the end of `start` (a flow collection's opening
    /// token).
    pub(super) fn empty_before(&self, run: Run, index: usize, start: Span) -> Span {
        self.token_at(run, index).map_or_else(
            || Span::empty(start.end()),
            |token| Span::empty(token.span.start()),
        )
    }

    pub(super) fn null_scalar(&self, span: Span) -> Event<'a> {
        self.null_scalar_with(span, None, None)
    }

    #[allow(
        clippy::unused_self,
        reason = "the method is part of a uniform grammar or diagnostic surface"
    )]
    pub(super) fn null_scalar_with(
        &self,
        span: Span,
        tag: Option<Tag>,
        anchor: Option<Anchor<'a>>,
    ) -> Event<'a> {
        Event::Scalar {
            value: Cow::Borrowed(""),
            raw: Cow::Borrowed(""),
            style: ScalarStyle::Plain,
            tag,
            anchor,
            span,
        }
    }

    /// Count one event against `max_events`, and pass it on.
    pub(super) fn emit(&mut self, mut event: Event<'a>) -> Result<(), Error> {
        match &mut event {
            Event::Scalar { tag, .. } => {
                if tag.as_ref().is_some_and(Tag::is_core_collection) {
                    *tag = None;
                }
            }
            Event::SequenceStart { tag, .. } => {
                if tag.as_ref().is_some_and(|tag| {
                    tag.is_core_scalar()
                        || (tag.is_core_collection()
                            && !matches!(tag.suffix(), "seq" | "omap" | "pairs"))
                }) {
                    *tag = None;
                }
            }
            Event::MappingStart { tag, .. } => {
                let drop = tag.as_ref().is_some_and(|tag| {
                    tag.is_core_scalar()
                        || (tag.is_core_collection() && !matches!(tag.suffix(), "map" | "set"))
                });
                if drop {
                    *tag = None;
                }
            }
            _ => {}
        }
        self.emitted = self.emitted.saturating_add(1);
        let span = event.span();
        self.limits
            .check_events(self.emitted)
            .map_err(|error| self.limit_at(span, error))?;
        self.sink.event(event)
    }

    pub(super) fn line_starts_with(
        &self,
        index: usize,
        predicate: impl Fn(&TokenKind<'a>) -> bool,
    ) -> bool {
        self.line_first(index)
            .is_some_and(|token| predicate(&token.kind))
    }

    pub(super) fn empty_span(&self, index: usize) -> Span {
        if self.lines.get(index).is_some()
            && let Some(token) = self.line_first(index)
        {
            let marker = token.span.start();
            return Span::empty(marker);
        }
        Span::empty(self.end)
    }

    pub(super) fn last_span(&self, index: usize) -> Span {
        if index > 0
            && self.lines.get(index - 1).is_some()
            && let Some(token) = self.line_last(index - 1)
        {
            return Span::empty(token.span.end());
        }
        self.empty_span(index)
    }

    #[allow(
        clippy::unused_self,
        reason = "the method is part of a uniform grammar or diagnostic surface"
    )]
    pub(super) fn error(&self, span: Span, message: impl Into<String>) -> Error {
        Error::new(ErrorKind::Parser, message).with_span(span)
    }

    #[allow(
        clippy::unused_self,
        reason = "the method is part of a uniform grammar or diagnostic surface"
    )]
    pub(super) fn limit_at(&self, span: Span, limit: crate::limits::LimitExceeded) -> Error {
        Error::from(limit).with_span(span)
    }
}