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
use super::{
    Anchor, BlockState, Call, CollectionStyle, Cont, Cow, DUPLICATE_PROPERTIES, Error, Event,
    FlowLack, NodeBounds, Parser, Run, ScalarStyle, Span, Step, Tag, TokenKind,
    flow_brackets_match, has_value_indicator, implicit_key_fits, is_mapping_line,
    value_indicator_index,
};

impl<'a> Parser<'a> {
    #[allow(
        clippy::too_many_arguments,
        reason = "the grammar routine takes its full parsed state explicitly so recursion stays heap-based"
    )]
    pub(super) fn node(
        &mut self,
        mut index: usize,
        mut indent: usize,
        depth: usize,
        mut tag: Option<Tag>,
        mut anchor: Option<Anchor<'a>>,
        bounds: NodeBounds,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let span = self.empty_span(index);
        self.limits
            .check_depth(depth)
            .map_err(|error| self.limit_at(span, error))?;
        loop {
            let line = self.line_run(index);
            let (line_tag, line_anchor, rest_start) = self.properties(line)?;
            let inherited_tag = tag.clone();
            let inherited_anchor = anchor.clone();
            tag = line_tag.clone().or(tag);
            anchor = line_anchor.clone().or(anchor);
            let rest = line.from(rest_start);
            let duplicate = (inherited_tag.is_some() && line_tag.is_some())
                || (inherited_anchor.is_some() && line_anchor.is_some());
            if rest.is_empty() {
                if duplicate {
                    return Err(self.error(self.token(line, 0).span, DUPLICATE_PROPERTIES));
                }
                let next_index = index + 1;
                let has_properties = tag.is_some() || anchor.is_some();
                let next_indent = self.lines.get(next_index).map_or(0, |line| line.indent);
                // After a property-only line, a `-` line may start the node's
                // block sequence down to the parent's bound: a mapping value's
                // sequence may sit at the key's column, a sequence entry's
                // must be indented past its `-`.
                let next_is_indentless_sequence = next_indent >= bounds.sequence_indent
                    && self
                        .line_starts_with(next_index, |kind| matches!(kind, TokenKind::BlockEntry));
                // A block scalar header may sit at any column here, as in
                // libyaml; YAML 1.2.2 wants `min_indent`.
                let next_is_block_scalar = self.line_starts_with(next_index, |kind| {
                    matches!(
                        kind,
                        TokenKind::Scalar {
                            style: ScalarStyle::Literal | ScalarStyle::Folded,
                            ..
                        }
                    )
                });
                // Other content may sit left of the property line, down to
                // the parent's bound (`-\n    !t\n  x`).
                let next_is_node = next_index < self.lines.len()
                    && (next_indent >= indent.min(bounds.min_indent)
                        || next_is_indentless_sequence
                        || next_is_block_scalar)
                    && !self.line_starts_with(next_index, |kind| {
                        matches!(
                            kind,
                            TokenKind::DocumentStart { .. } | TokenKind::DocumentEnd
                        )
                    });
                if has_properties && next_is_node {
                    index = next_index;
                    indent = self.lines[index].indent;
                    continue;
                }
                let span = self.empty_span(index);
                let scalar = self.null_scalar_with(span, tag, anchor);
                self.emit(scalar)?;
                return Ok(Step::Return(index + 1));
            }

            if matches!(self.token(rest, 0).kind, TokenKind::BlockEntry) {
                if line_tag.is_some() || line_anchor.is_some() {
                    return Err(self.error(
                        self.token(rest, 0).span,
                        "properties cannot precede a compact block sequence",
                    ));
                }
                return Ok(Step::Call(Call::SequenceFirst {
                    index,
                    first: rest,
                    indent,
                    depth,
                    tag,
                    anchor,
                }));
            }
            if is_mapping_line(self.tokens(rest)) {
                // Properties on the same line as a block mapping key belong to
                // the key. A mapping's own properties must be on a preceding
                // property-only line or an enclosing flow collection.
                if line_tag.is_some() || line_anchor.is_some() {
                    return Ok(Step::Call(Call::MappingFirst {
                        first: line,
                        next_index: index + 1,
                        indent,
                        depth,
                        tag: inherited_tag,
                        anchor: inherited_anchor,
                    }));
                }
                return Ok(Step::Call(Call::MappingFirst {
                    first: rest,
                    next_index: index + 1,
                    indent,
                    depth,
                    tag,
                    anchor,
                }));
            }
            if duplicate {
                return Err(self.error(self.token(rest, 0).span, DUPLICATE_PROPERTIES));
            }
            stack.push(Cont::NodeInline { rest, index });
            return Ok(Step::Call(Call::Inline {
                tokens: rest,
                depth,
                tag,
                anchor,
            }));
        }
    }

    #[allow(
        clippy::too_many_arguments,
        reason = "the grammar routine takes its full parsed state explicitly so recursion stays heap-based"
    )]
    pub(super) fn sequence_first(
        &mut self,
        index: usize,
        first: Run,
        indent: usize,
        depth: usize,
        tag: Option<Tag>,
        anchor: Option<Anchor<'a>>,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let start_span = self.token(first, 0).span;
        self.limits
            .check_depth(depth + 1)
            .map_err(|error| self.limit_at(start_span, error))?;
        self.emit(Event::SequenceStart {
            tag,
            anchor,
            style: CollectionStyle::Block,
            span: start_span,
        })?;
        let state = BlockState {
            indent,
            depth,
            entries: 0,
            current_index: index + 1,
            first: Some(first.from(1)),
        };
        self.sequence_loop(state, stack)
    }

    pub(super) fn sequence_loop(
        &mut self,
        mut state: BlockState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        loop {
            let (rest, next_index) = if let Some(rest) = state.first.take() {
                (rest, state.current_index)
            } else {
                let current_index = state.current_index;
                if current_index >= self.lines.len()
                    || self.lines[current_index].indent != state.indent
                    || !self.line_starts_with(current_index, |kind| {
                        matches!(kind, TokenKind::BlockEntry)
                    })
                {
                    self.emit(Event::SequenceEnd {
                        span: self.last_span(current_index),
                    })?;
                    return Ok(Step::Return(current_index));
                }
                (self.line_run(current_index).from(1), current_index + 1)
            };
            state.entries += 1;
            let span = self.last_span(state.current_index);
            self.limits
                .check_collection_entries(state.entries)
                .map_err(|error| self.limit_at(span, error))?;
            // The overwhelmingly common block-sequence entry is one scalar.
            // Emit it inline so a wide sequence does not pay a continuation
            // stack push/pop and nested parser call for every item.
            if let Some(token) = self.single_scalar(rest) {
                let event = Event::Scalar {
                    value: token.0.clone(),
                    raw: token.1.clone(),
                    style: token.2,
                    tag: None,
                    anchor: None,
                    span: token.3,
                };
                self.emit(event)?;
                state.current_index = next_index;
                continue;
            }
            let (indent, depth) = (state.indent, state.depth);
            stack.push(Cont::Sequence(state));
            return Ok(Step::Call(Call::SequenceItem {
                rest,
                next_index,
                indent,
                depth: depth + 1,
            }));
        }
    }

    /// A one-token scalar entry that needs no property or nested-node work.
    pub(super) fn single_scalar(
        &self,
        run: Run,
    ) -> Option<(Cow<'a, str>, Cow<'a, str>, ScalarStyle, Span)> {
        if run.len() != 1 {
            return None;
        }
        let token = self.token(run, 0);
        match &token.kind {
            TokenKind::Scalar { value, raw, style } => {
                Some((value.clone(), raw.clone(), *style, token.span))
            }
            _ => None,
        }
    }

    pub(super) fn sequence_item(
        &mut self,
        rest: Run,
        next_index: usize,
        indent: usize,
        depth: usize,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        if rest.is_empty() {
            if next_index < self.lines.len() && self.lines[next_index].indent > indent {
                return Ok(Step::Call(Call::Node {
                    index: next_index,
                    indent: self.lines[next_index].indent,
                    depth,
                    tag: None,
                    anchor: None,
                    bounds: NodeBounds::block_in(indent),
                }));
            }
            let span = self.empty_span(next_index);
            self.emit(self.null_scalar(span))?;
            return Ok(Step::Return(next_index));
        }
        let (tag, anchor, rest_start) = self.properties(rest)?;
        let content = rest.from(rest_start);
        if content.is_empty() {
            if tag.is_some() || anchor.is_some() {
                if next_index < self.lines.len() && self.lines[next_index].indent > indent {
                    return Ok(Step::Call(Call::Node {
                        index: next_index,
                        indent: self.lines[next_index].indent,
                        depth,
                        tag,
                        anchor,
                        bounds: NodeBounds::block_in(indent),
                    }));
                }
                let span = self.empty_span(next_index);
                self.emit(self.null_scalar_with(span, tag, anchor))?;
                return Ok(Step::Return(next_index));
            }
            let span = self.empty_span(next_index);
            self.emit(self.null_scalar(span))?;
            return Ok(Step::Return(next_index));
        }
        let content_first = self.token(content, 0);
        if matches!(content_first.kind, TokenKind::BlockEntry) {
            if tag.is_some() || anchor.is_some() {
                return Err(self.error(
                    content_first.span,
                    "properties cannot precede a compact block sequence",
                ));
            }
            let nested_indent = content_first.span.start().column().saturating_sub(1);
            return Ok(Step::Call(Call::SequenceFirst {
                index: next_index.saturating_sub(1),
                first: content,
                indent: nested_indent,
                depth,
                tag,
                anchor,
            }));
        }
        let explicit_key = matches!(content_first.kind, TokenKind::Key);
        if explicit_key || has_value_indicator(self.tokens(content)) {
            if explicit_key && (tag.is_some() || anchor.is_some()) {
                return Err(self.error(
                    self.token(content, 0).span,
                    "properties cannot precede an explicit mapping key",
                ));
            }
            // On a sequence item, properties before the first block key belong
            // to that key, not to the mapping node, so the mapping starts at
            // the first property (`- &x a: b` has its keys at column 2).
            let mapping_indent = self.token(rest, 0).span.start().column().saturating_sub(1);
            return Ok(Step::Call(Call::MappingFirst {
                first: rest,
                next_index,
                indent: mapping_indent,
                depth,
                tag: None,
                anchor: None,
            }));
        }
        stack.push(Cont::ItemInline {
            content,
            next_index,
        });
        Ok(Step::Call(Call::Inline {
            tokens: content,
            depth,
            tag,
            anchor,
        }))
    }

    #[allow(
        clippy::too_many_arguments,
        reason = "the grammar routine takes its full parsed state explicitly so recursion stays heap-based"
    )]
    pub(super) fn mapping_first(
        &mut self,
        first: Run,
        next_index: usize,
        indent: usize,
        depth: usize,
        tag: Option<Tag>,
        anchor: Option<Anchor<'a>>,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let start_span = self.token(first, 0).span;
        self.limits
            .check_depth(depth + 1)
            .map_err(|error| self.limit_at(start_span, error))?;
        self.emit(Event::MappingStart {
            tag,
            anchor,
            style: CollectionStyle::Block,
            span: start_span,
        })?;
        let state = BlockState {
            indent,
            depth,
            entries: 0,
            current_index: next_index,
            first: Some(first),
        };
        self.mapping_loop(state, stack)
    }

    pub(super) fn mapping_loop(
        &mut self,
        mut state: BlockState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        loop {
            let tokens = if let Some(first) = state.first.take() {
                first
            } else {
                let current_index = state.current_index;
                if current_index >= self.lines.len()
                    || self.lines[current_index].indent != state.indent
                    || !is_mapping_line(self.line_tokens(current_index))
                {
                    self.emit(Event::MappingEnd {
                        span: self.last_span(current_index),
                    })?;
                    return Ok(Step::Return(current_index));
                }
                state.current_index += 1;
                self.line_run(current_index)
            };
            let (indent, depth) = (state.indent, state.depth);

            // A very common explicit entry is `? key` followed by `: value`,
            // both single plain scalars. Emit the pair inline instead of paying
            // continuation frames for the key and value nodes.
            if matches!(self.token(tokens, 0).kind, TokenKind::Key) {
                let key_rest = tokens.from(1);
                let current_index = state.current_index;
                if let Some(key) = self.single_scalar(key_rest)
                    && current_index < self.lines.len()
                    && self.lines[current_index].indent == indent
                    && self.line_starts_with(current_index, |kind| matches!(kind, TokenKind::Value))
                {
                    let value = self.line_run(current_index).from(1);
                    if let Some(value) = self.single_scalar(value) {
                        state.entries += 1;
                        self.limits
                            .check_collection_entries(state.entries)
                            .map_err(|error| self.limit_at(key.3, error))?;
                        self.emit(Event::Scalar {
                            value: key.0,
                            raw: key.1,
                            style: key.2,
                            tag: None,
                            anchor: None,
                            span: key.3,
                        })?;
                        self.emit(Event::Scalar {
                            value: value.0,
                            raw: value.1,
                            style: value.2,
                            tag: None,
                            anchor: None,
                            span: value.3,
                        })?;
                        state.current_index = current_index + 1;
                        continue;
                    }
                }
            }

            if matches!(self.token(tokens, 0).kind, TokenKind::Key) {
                // `? <node>`: the rest of the line is a block-indented node, so a
                // `:` on this line belongs to a compact mapping inside the key.
                // The entry's value is a separate `:` line at this indentation.
                let key_rest = tokens.from(1);
                let current_index = state.current_index;
                if key_rest.is_empty() {
                    if current_index < self.lines.len() {
                        let next_indent = self.lines[current_index].indent;
                        let indentless_sequence = next_indent == indent
                            && self.line_starts_with(current_index, |kind| {
                                matches!(kind, TokenKind::BlockEntry)
                            });
                        if next_indent > indent || indentless_sequence {
                            stack.push(Cont::MappingExplicitKey(state));
                            return Ok(Step::Call(Call::Node {
                                index: current_index,
                                indent: next_indent,
                                depth: depth + 1,
                                tag: None,
                                anchor: None,
                                bounds: NodeBounds::block_out(indent),
                            }));
                        }
                    }
                    let span = Span::empty(self.token(tokens, 0).span.end());
                    self.emit(self.null_scalar(span))?;
                    return self.mapping_after_explicit_key(state, stack);
                }
                // Properties alone on the `?` line may be followed by an indentless
                // sequence, as on a mapping value's line.
                let (tag, anchor, rest_start) = self.properties(key_rest)?;
                if rest_start == key_rest.len()
                    && current_index < self.lines.len()
                    && self.lines[current_index].indent == indent
                    && self.line_starts_with(current_index, |kind| {
                        matches!(kind, TokenKind::BlockEntry)
                    })
                {
                    stack.push(Cont::MappingExplicitKey(state));
                    return Ok(Step::Call(Call::Node {
                        index: current_index,
                        indent,
                        depth: depth + 1,
                        tag,
                        anchor,
                        bounds: NodeBounds::block_out(indent),
                    }));
                }
                stack.push(Cont::MappingExplicitKey(state));
                return Ok(Step::Call(Call::SequenceItem {
                    rest: key_rest,
                    next_index: current_index,
                    indent,
                    depth: depth + 1,
                }));
            }

            let value_index = value_indicator_index(self.tokens(tokens));
            let key = value_index.map_or(tokens, |index| tokens.until(index));
            if !key.is_empty() {
                let key_tokens = self.tokens(key);
                let key_line = key_tokens[0].span.start().line();
                if key_tokens.iter().any(|token| {
                    token.span.start().line() != key_line || token.span.end().line() != key_line
                }) && flow_brackets_match(key_tokens)
                {
                    return Err(self.error(
                        key_tokens[0].span,
                        "implicit mapping keys must be on one line",
                    ));
                }
                if let Some(value_index) = value_index
                    && !implicit_key_fits(
                        key_tokens[0].span.start(),
                        self.token(tokens, value_index).span.start(),
                    )
                {
                    return Err(self.error(
                        key_tokens[0].span,
                        "an implicit mapping key may be at most 1024 characters long",
                    ));
                }
            }
            if key.is_empty() {
                let span = Span::empty(self.token(tokens, 0).span.start());
                self.emit(self.null_scalar(span))?;
                return self.mapping_after_implicit_key(state, tokens, value_index, stack);
            }
            stack.push(Cont::MappingImplicitKey {
                state,
                tokens,
                value_index,
                key,
            });
            // The key is inside the mapping, as an explicit key and a value are.
            return Ok(Step::Call(Call::Inline {
                tokens: key,
                depth: depth + 1,
                tag: None,
                anchor: None,
            }));
        }
    }

    pub(super) fn mapping_after_explicit_key(
        &mut self,
        mut state: BlockState,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        state.entries += 1;
        let span = self.empty_span(state.current_index);
        self.limits
            .check_collection_entries(state.entries)
            .map_err(|error| self.limit_at(span, error))?;
        let current_index = state.current_index;
        let has_value_line = current_index < self.lines.len()
            && self.lines[current_index].indent == state.indent
            && self.line_starts_with(current_index, |kind| matches!(kind, TokenKind::Value));
        if has_value_line {
            let value = self.line_run(current_index).from(1);
            state.current_index += 1;
            let (current_index, indent, depth) = (state.current_index, state.indent, state.depth);
            stack.push(Cont::MappingEntry(state));
            return Ok(Step::Call(Call::MappingValue {
                value,
                current_index,
                indent,
                depth,
                allow_compact: true,
            }));
        }
        let span = self.empty_span(current_index);
        self.emit(self.null_scalar(span))?;
        // Continue through the driver rather than calling `mapping_loop`:
        // a run of `?` entries must not nest a native frame per entry.
        stack.push(Cont::MappingEntry(state));
        Ok(Step::Return(current_index))
    }

    #[allow(
        clippy::needless_pass_by_ref_mut,
        reason = "the signature mirrors the surrounding grammar dispatch even where this path does not mutate"
    )]
    pub(super) fn mapping_after_implicit_key(
        &mut self,
        mut state: BlockState,
        tokens: Run,
        value_index: Option<usize>,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        state.entries += 1;
        let span = self.token(tokens, 0).span;
        self.limits
            .check_collection_entries(state.entries)
            .map_err(|error| self.limit_at(span, error))?;
        let Some(value_index) = value_index else {
            return Err(self.error(
                self.token(tokens, 0).span,
                "explicit mapping key must be followed by a ':' line",
            ));
        };
        let (current_index, indent, depth) = (state.current_index, state.indent, state.depth);
        stack.push(Cont::MappingEntry(state));
        Ok(Step::Call(Call::MappingValue {
            value: tokens.from(value_index + 1),
            current_index,
            indent,
            depth,
            allow_compact: false,
        }))
    }

    pub(super) fn mapping_value(
        &mut self,
        value: Run,
        current_index: usize,
        indent: usize,
        depth: usize,
        allow_compact: bool,
        stack: &mut Vec<Cont>,
    ) -> Result<Step<'a>, Error> {
        let (tag, anchor, rest_start) = self.properties(value)?;
        let content = value.from(rest_start);
        if content.is_empty() {
            let next_indent = self.lines.get(current_index).map_or(0, |line| line.indent);
            let indentless_sequence = next_indent == indent
                && self
                    .line_starts_with(current_index, |kind| matches!(kind, TokenKind::BlockEntry));
            // libyaml allows a block scalar at the parent's column as a mapping value, as in
            // `&a::\n|`. The scanner has already established that this line is the scalar.
            let block_scalar = self.compat
                && self.line_starts_with(current_index, |kind| {
                    matches!(
                        kind,
                        TokenKind::Scalar {
                            style: ScalarStyle::Literal | ScalarStyle::Folded,
                            ..
                        }
                    )
                })
                && self
                    .line_first(current_index)
                    .is_some_and(|token| token.span.start().column() == 1)
                && current_index > 0
                && self
                    .line_tokens(current_index - 1)
                    .iter()
                    .any(|token| matches!(token.kind, TokenKind::Anchor(_)));
            if current_index < self.lines.len()
                && (next_indent > indent || indentless_sequence || block_scalar)
            {
                return Ok(Step::Call(Call::Node {
                    index: current_index,
                    indent: self.lines[current_index].indent,
                    depth: depth + 1,
                    tag,
                    anchor,
                    bounds: NodeBounds::block_out(indent),
                }));
            }
            let span = self.empty_span(current_index);
            self.emit(self.null_scalar_with(span, tag, anchor))?;
            return Ok(Step::Return(current_index));
        }
        let content_first = self.token(content, 0);
        if allow_compact && matches!(content_first.kind, TokenKind::BlockEntry) {
            if tag.is_some() || anchor.is_some() {
                return Err(self.error(
                    content_first.span,
                    "properties cannot precede a compact block sequence",
                ));
            }
            let sequence_indent = content_first.span.start().column().saturating_sub(1);
            return Ok(Step::Call(Call::SequenceFirst {
                index: current_index.saturating_sub(1),
                first: content,
                indent: sequence_indent,
                depth: depth + 1,
                tag,
                anchor,
            }));
        }
        let explicit_key = matches!(content_first.kind, TokenKind::Key);
        if allow_compact && (explicit_key || has_value_indicator(self.tokens(content))) {
            if explicit_key && (tag.is_some() || anchor.is_some()) {
                return Err(self.error(
                    self.token(content, 0).span,
                    "properties cannot precede an explicit mapping key",
                ));
            }
            // As on a sequence item: properties belong to the first key, and
            // the compact mapping starts at the first property.
            let mapping_indent = self.token(value, 0).span.start().column().saturating_sub(1);
            return Ok(Step::Call(Call::MappingFirst {
                first: value,
                next_index: current_index,
                indent: mapping_indent,
                depth: depth + 1,
                tag: None,
                anchor: None,
            }));
        }
        stack.push(Cont::ValueInline {
            value,
            current_index,
        });
        Ok(Step::Call(Call::Inline {
            tokens: value,
            depth: depth + 1,
            tag: None,
            anchor: None,
        }))
    }

    /// Parse one node from a run of a line's tokens. Returns how many
    /// tokens it consumed.
    pub(super) fn inline(
        &mut self,
        tokens: Run,
        depth: usize,
        mut tag: Option<Tag>,
        mut anchor: Option<Anchor<'a>>,
    ) -> Result<Step<'a>, Error> {
        let mut index = 0;
        if self.starts_with_property(tokens) {
            let (own_tag, own_anchor, count) = self.read_properties(tokens)?;
            tag = own_tag.or(tag);
            anchor = own_anchor.or(anchor);
            index = count;
        }
        if index >= tokens.len() {
            if index > 0 {
                let span = Span::empty(self.token(tokens, index - 1).span.end());
                self.emit(self.null_scalar_with(span, tag, anchor))?;
                return Ok(Step::Return(index));
            }
            return Err(self.flow_error(tokens, 0, FlowLack::Node("expected a node".to_owned())));
        }
        let token = self.token(tokens, index);
        let span = token.span;
        match &token.kind {
            TokenKind::Scalar { value, raw, style } => {
                let event = Event::Scalar {
                    value: value.clone(),
                    raw: raw.clone(),
                    style: *style,
                    tag,
                    anchor,
                    span,
                };
                self.emit(event)?;
                Ok(Step::Return(index + 1))
            }
            TokenKind::Alias(name) => {
                if tag.is_some() || anchor.is_some() {
                    return Err(self.error(span, "aliases cannot carry tags or anchors"));
                }
                let event = Event::Alias {
                    anchor: Anchor::new(name.clone()),
                    span,
                };
                self.emit(event)?;
                Ok(Step::Return(index + 1))
            }
            TokenKind::FlowSequenceStart => Ok(Step::Call(Call::FlowSequence {
                tokens,
                start: index,
                depth,
                tag,
                anchor,
            })),
            TokenKind::FlowMappingStart => Ok(Step::Call(Call::FlowMapping {
                tokens,
                start: index,
                depth,
                tag,
                anchor,
            })),
            // Properties before `,`, `]`, or `}` belong to an empty node
            // (`[!t ]`, `[&a , b]`; YAML 1.2.2 production 161).
            TokenKind::FlowEntry | TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd
                if index > 0 =>
            {
                self.emit(self.null_scalar_with(Span::empty(span.start()), tag, anchor))?;
                Ok(Step::Return(index))
            }
            kind => {
                let problem = format!("unexpected {kind} in node position");
                Err(self.flow_error(tokens, index, FlowLack::Node(problem)))
            }
        }
    }
}