oneyaml-syntax 0.1.0

Lossless YAML 1.2 decoder, parser, event stream, and syntax tree underneath oneyaml.
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
//! Node heads: properties, then an alias or flow scalar, then possibly the
//! `:` that makes it an implicit key (`ns-l-block-map-implicit-entry`,
//! production 192; `ns-flow-yaml-node`, production 159).
//!
//! # Inline colons in plain values (Q2.1)
//!
//! A block mapping value that starts on its entry's line cannot be a block
//! mapping (`s-l+block-node`, production 196, allows a compact collection
//! only after `-`, `?`, or an explicit `:`). So in `title: The farm: a
//! banana story`, the second `: ` is an error. Exactly this case is
//! diagnosed as `INLINE_COLON_IN_PLAIN_VALUE` (`Y0027`), with a fix that
//! single-quotes the value:
//!
//! * the head is the value of a **block mapping** entry (after an implicit
//!   key's `:`, or after `:` with an empty key), including compact mappings
//!   in sequence entries (`- a: b: c`);
//! * it is on the **same line** as that `:` (not after a line break);
//! * its content is a **plain** scalar (optionally after an anchor or tag on
//!   the same line) whose first line is followed by `: `.
//!
//! Not covered, and unchanged: flow context; quoted, alias, and flow
//! collection values (`a: 'b': c`); values after `---`, `-`, `?`, or an
//! explicit-value `:`; values on a later line; and every valid input.
//!
//! With `lenient_inline_colons` on, the same case instead yields one plain
//! scalar: its first line runs to the end of the line (stopping only before
//! ` #`, a line break, or a non-printable character), then continues over
//! more-indented lines under the normal plain-scalar rules, so a `: ` on a
//! continuation line is still an error (`Y0007`). A `LENIENT_INLINE_COLON`
//! warning (`Y0028`) with the same quoting fix always follows the scalar.

use oneyaml_core::code::{
    ALIAS_WITH_PROPERTIES, DUPLICATE_PROPERTY, IMPLICIT_KEY_TOO_LONG, INLINE_COLON_IN_PLAIN_VALUE,
    INVALID_ANCHOR, INVALID_NODE_START, LENIENT_INLINE_COLON, MISSING_MAPPING_COLON,
    MULTILINE_IMPLICIT_KEY, SCALAR_TOO_LONG, UNEXPECTED_CONTENT,
};
use oneyaml_core::{Applicability, Code, Diagnostic, Label, Replacement, Span, Suggestion};

use super::block::HeadMode;
use super::polish::{self, Place};
use super::scalar::Count;
use super::scan::{
    anchor_end, break_or_end, byte, indicator_at, is_nb_json, is_plain_first, key_too_long,
    mark_at, plain_line_end, plain_line_end_lenient, skip_white,
};
use super::{Frame, MapPhase, NodeCtx, Parser, State, scalar};
use crate::event::{
    CollectionStyle, Event, EventKind, Indicator, Properties, ScalarStyle, TriviaKind,
};

/// The content token of a head.
enum Content<'a> {
    /// An alias covering `start..end`.
    Alias(&'a str),
    /// A flow scalar; `first_end` is the end of its first line (plain only).
    Scalar {
        style: ScalarStyle,
        /// The value's length in bytes (Q4.5), as far as scanned.
        len: usize,
        multiline: bool,
        /// End of the first line (the whole scalar when quoted).
        first_end: usize,
    },
    /// No content: properties directly followed by `:` (an empty key).
    Empty,
}

/// A scanned head, ready to be emitted as a key.
struct KeyHead<'a> {
    events: Vec<Event<'a>>,
    props: Properties,
    content: Content<'a>,
    content_start: usize,
    content_end: usize,
    props_end: usize,
    colon: usize,
}

fn event<'a>(kind: EventKind, start: usize, end: usize) -> Event<'a> {
    Event::new(kind, Span::new(start, end))
}

/// The fix for an inline colon: single-quote `src[start..end]` (Q2.3's
/// preferred quote), doubling any `'`. A single-quoted scalar folds lines
/// exactly like the plain one it replaces, so the value is unchanged.
///
/// Unit 46: a byte order mark in plain text is misplaced (`Y0032`, reported
/// with its own fix: remove it), never content, so it is left out: inside
/// quotes it would become part of the value (`nb-json` allows it there),
/// and the invisible mark would hide in the suggested text.
pub(super) fn quote_fix(
    src: &str,
    start: usize,
    end: usize,
    applicability: Applicability,
) -> Suggestion {
    let text = src.get(start..end).unwrap_or("");
    let mut quoted = String::with_capacity(text.len().saturating_add(2));
    quoted.push('\'');
    for c in text.chars().filter(|c| *c != '\u{feff}') {
        if c == '\'' {
            quoted.push('\'');
        }
        quoted.push(c);
    }
    quoted.push('\'');
    Suggestion::new(
        "quote the value",
        vec![Replacement::new(Span::new(start, end), quoted)],
        applicability,
    )
}

/// Where the text glued to a plain scalar through a byte order mark at
/// `end` ends on its line: before a comment (`#` after white space) and
/// trailing white space. `None` when no mark is glued at `end`.
fn glued_after_mark(src: &str, end: usize) -> Option<usize> {
    let rest = src.get(end..)?;
    if !rest.starts_with('\u{feff}') {
        return None;
    }
    let line = rest.find(['\n', '\r']).unwrap_or(rest.len());
    let line = rest.get(..line)?;
    let mut cut = line.len();
    let mut prev_white = false;
    for (k, c) in line.char_indices() {
        if c == '#' && prev_white {
            cut = k;
            break;
        }
        prev_white = c == ' ' || c == '\t';
    }
    let text = line.get(..cut)?.trim_end_matches([' ', '\t']);
    end.checked_add(text.len())
}

/// Drops a head's trailing white space event, which stays pending trivia.
fn pop_white(events: &mut Vec<Event<'_>>) {
    if matches!(
        events.last().map(Event::kind),
        Some(EventKind::Trivia(TriviaKind::Whitespace))
    ) {
        events.pop();
    }
}

impl<'a> Parser<'a> {
    /// Parses a node head at the scan position (see [`HeadMode`]).
    pub(super) fn head(&mut self, mode: HeadMode) {
        self.mark_before_error = None;
        let slot_open = matches!(mode, HeadMode::Node { .. });
        let mut events = self.take_scratch();
        let (props, props_end, i) = match self.scan_props(&mut events) {
            Ok(scanned) => scanned,
            Err((code, message)) => return self.fail(code, message, slot_open),
        };
        let src = self.src;
        let head_col = if self.fresh {
            self.line_indent
        } else {
            self.col()
        };
        // Unit 41b: byte order marks skipped among the properties are no
        // columns; the rest of the line is counted without them (as
        // `misplaced_bom` does).
        let marks = events.iter().filter(|e| e.is_error()).count();
        self.line_start = self
            .line_start
            .saturating_add(marks.saturating_mul('\u{feff}'.len_utf8()))
            .min(i);

        // Properties alone on their line belong to the node that follows
        // (production 200's `s-l-comments` after properties).
        if !props.is_empty() && (break_or_end(src, i) || self.comment_at(i)) {
            let HeadMode::Node { ctx, .. } = mode else {
                return self.fail(MISSING_MAPPING_COLON, "properties without a key", false);
            };
            if !self.merge_props(props) {
                return self.fail(DUPLICATE_PROPERTY, "duplicate anchor or tag", true);
            }
            if i > props_end {
                pop_white(&mut events); // trailing white space stays trivia
            }
            self.begin_content();
            self.commit_scratch(events);
            self.state = State::Node(NodeCtx {
                compact: false,
                ..ctx
            });
            return;
        }

        // The content token (productions 104, 109-135).
        let parent = match mode {
            HeadMode::Node { ctx, .. } => ctx.parent,
            HeadMode::Key { indent } => Some(indent),
        };
        let content_start = i;
        let content = match byte(src, i) {
            _ if !props.is_empty() && indicator_at(src, i, b':') => Ok(Content::Empty),
            _ if !props.is_empty()
                && (indicator_at(src, i, b'-') || indicator_at(src, i, b'?')) =>
            {
                let (code, message) = self.collection_error();
                return self.fail(code, message, slot_open);
            }
            Some(b'*') if !props.is_empty() => {
                Err((ALIAS_WITH_PROPERTIES, "an alias cannot have properties"))
            }
            Some(b'*') => {
                let from = i.saturating_add(1);
                let end = anchor_end(src, from);
                if end == from {
                    Err((INVALID_ANCHOR, "empty alias name"))
                } else {
                    Ok(Content::Alias(src.get(from..end).unwrap_or("")))
                }
            }
            Some(q @ (b'"' | b'\'')) => {
                scalar::quoted::<Count>(src, i, parent, q == b'"').map(|f| Content::Scalar {
                    style: if q == b'"' {
                        ScalarStyle::DoubleQuoted
                    } else {
                        ScalarStyle::SingleQuoted
                    },
                    len: f.len,
                    multiline: f.multiline,
                    first_end: f.end,
                })
            }
            Some(b'|' | b'>') => match mode {
                HeadMode::Node { ctx, .. } => return self.block_scalar(ctx, events, props),
                HeadMode::Key { .. } => Err((
                    MULTILINE_IMPLICIT_KEY,
                    "a block scalar cannot be an implicit key",
                )),
            },
            Some(b'[' | b'{') => return self.flow_head(mode, events, props, i),
            _ if is_plain_first(src, i, false) => {
                let end = plain_line_end(src, i, false);
                Ok(Content::Scalar {
                    style: ScalarStyle::Plain,
                    len: end.saturating_sub(i),
                    multiline: false,
                    first_end: end,
                })
            }
            _ => Err((INVALID_NODE_START, "this character cannot start a node")),
        };
        let content = match content {
            Ok(c) => c,
            Err((code, message)) => {
                // A head that may still become a key ends at its `: `.
                let clash = self.props.anchor.is_some() && props.anchor.is_some()
                    || self.props.tag.is_some() && props.tag.is_some();
                let place = match mode {
                    HeadMode::Key { indent } => Place {
                        key: true,
                        key_only: true,
                        parent: Some(indent),
                        flow: false,
                        sound: !clash,
                    },
                    HeadMode::Node { ctx, can_open } => Place {
                        key: can_open,
                        key_only: false,
                        parent: ctx.parent,
                        flow: false,
                        sound: !clash,
                    },
                };
                return self.fail_content(code, message, slot_open, content_start, place);
            }
        };
        if let Content::Scalar { len, .. } = &content {
            if !self.scalar_len_ok(*len) {
                return self.fail(SCALAR_TOO_LONG, "the scalar is too long", slot_open);
            }
        }
        if self.doc_start_deferred {
            // Content has started: the implicit document certainly exists
            // (timing convention T1).
            self.begin_content();
        }
        let content_end = match &content {
            Content::Alias(name) => content_start.saturating_add(1).saturating_add(name.len()),
            Content::Scalar { first_end, .. } => *first_end,
            Content::Empty => props_end,
        };
        let multiline = matches!(
            content,
            Content::Scalar {
                multiline: true,
                ..
            }
        );
        // Unit 41d: byte order marks between an implicit key and its `:`
        // are skipped as if absent (each is reported, `commit_key`); marks
        // not followed by a `:` are reported with the error they cause.
        let mut colon = skip_white(src, content_end);
        let first_mark = mark_at(src, colon).then_some(colon);
        while mark_at(src, colon) {
            colon = skip_white(src, colon.saturating_add('\u{feff}'.len_utf8()));
        }
        let is_key = indicator_at(src, colon, b':');
        if !is_key {
            colon = skip_white(src, content_end);
        }
        self.mark_before_error = first_mark;
        let key = KeyHead {
            events,
            props,
            content,
            content_start,
            content_end,
            props_end,
            colon,
        };
        if is_key {
            if let HeadMode::Node { ctx, .. } = mode {
                if self.inline_colon_value(ctx, &key.content) {
                    return self.inline_colon(key, ctx);
                }
            }
        }
        if !is_key && matches!(mode, HeadMode::Node { .. }) {
            // A node, not a key: a mark after it is reported by `next`.
            self.mark_before_error = None;
        }
        if is_key && !multiline && key_too_long(src, self.pos, colon) {
            let message = "an implicit key must be at most 1024 characters";
            return self.fail(IMPLICIT_KEY_TOO_LONG, message, slot_open);
        }

        let (ctx, can_open) = match mode {
            HeadMode::Key { indent } => {
                if !is_key {
                    // A plain key continued on the next lines and then
                    // followed by `:` is a multi-line key (production 154),
                    // not a missing `:`.
                    let continued = match &key.content {
                        Content::Scalar {
                            style: ScalarStyle::Plain,
                            first_end,
                            ..
                        } => {
                            let f = scalar::plain(
                                src,
                                key.content_start,
                                *first_end,
                                Some(indent),
                                false,
                            );
                            f.multiline && indicator_at(src, skip_white(src, f.end), b':')
                        }
                        _ => false,
                    };
                    if continued {
                        let message = "an implicit key must be on one line";
                        return self.fail(MULTILINE_IMPLICIT_KEY, message, false);
                    }
                    return self.missing_colon(&key);
                }
                if multiline {
                    return self.fail(
                        MULTILINE_IMPLICIT_KEY,
                        "an implicit key must be on one line",
                        false,
                    );
                }
                self.begin_content();
                self.commit_key(key);
                self.set_key_value(indent);
                return;
            }
            HeadMode::Node { ctx, can_open } => (ctx, can_open),
        };

        let quoted = matches!(
            key.content,
            Content::Scalar {
                style: ScalarStyle::SingleQuoted | ScalarStyle::DoubleQuoted,
                ..
            }
        );
        if is_key && !multiline && !can_open && quoted {
            // A complete quoted node followed by a `:` that cannot start a
            // mapping here: emit the node, then report the `:` (timing
            // convention T3).
            // A failed node was already reported; never report twice.
            if !self.emit_node(key, ctx) {
                return;
            }
            self.scan_trivia(false);
            let (code, message) = self.collection_error();
            return self.fail(code, message, false);
        }
        if is_key {
            if multiline {
                return self.fail(
                    MULTILINE_IMPLICIT_KEY,
                    "an implicit key must be on one line",
                    true,
                );
            }
            if !can_open {
                let (code, message) = self.collection_error();
                return self.fail(code, message, true);
            }
            // A new block mapping whose first key is this head; properties
            // from earlier lines belong to the mapping.
            if self.fail_if_too_deep(self.block_depth().saturating_add(1)) {
                return;
            }
            self.begin_content();
            let map_props = core::mem::take(&mut self.props);
            self.zero_node(EventKind::mapping_start(CollectionStyle::Block), map_props);
            self.stack.push(Frame::Map {
                indent: head_col,
                phase: MapPhase::Value,
            });
            self.commit_key(key);
            self.set_key_value(head_col);
            return;
        }

        let _emitted = self.emit_node(key, ctx);
    }

    /// Emits a head that is a complete node (not a key), extending a plain
    /// scalar over its continuation lines.
    #[must_use]
    fn emit_node(&mut self, key: KeyHead<'a>, ctx: NodeCtx) -> bool {
        // A mark after a node is reported by the trivia scan (unit 41d).
        self.mark_before_error = None;
        let src = self.src;
        let KeyHead {
            mut events,
            props,
            content,
            content_start,
            content_end,
            ..
        } = key;
        // `scalar`: a scalar (else an alias or nothing), its style and end.
        let (scalar, style, end) = match content {
            Content::Scalar {
                style: ScalarStyle::Plain,
                first_end,
                ..
            } => {
                let f = scalar::plain(src, content_start, first_end, ctx.parent, false);
                if f.multiline && indicator_at(src, skip_white(src, f.end), b':') {
                    // A multi-line plain scalar cannot be a key (production
                    // 154); report before emitting it (timing convention T3).
                    let message = "an implicit key must be on one line";
                    self.fail(MULTILINE_IMPLICIT_KEY, message, true);
                    return false;
                }
                if !self.scalar_len_ok(f.len) {
                    self.fail(SCALAR_TOO_LONG, "the scalar is too long", true);
                    return false;
                }
                (true, ScalarStyle::Plain, f.end)
            }
            Content::Scalar {
                style, first_end, ..
            } => {
                self.last_quoted = (content_start, first_end);
                (true, style, first_end)
            }
            Content::Alias(_) | Content::Empty => (false, ScalarStyle::Plain, content_end),
        };
        if !scalar && !self.props.is_empty() {
            // Properties on an earlier line cannot apply to an alias
            // (production 104); as a key the alias would leave them to the
            // mapping instead.
            self.fail(
                ALIAS_WITH_PROPERTIES,
                "an alias cannot have properties",
                true,
            );
            return false;
        }
        if !self.merge_props(props) {
            self.fail(DUPLICATE_PROPERTY, "duplicate anchor or tag", true);
            return false;
        }
        let props = core::mem::take(&mut self.props);
        let span = Span::new(content_start, end);
        events.push(if scalar {
            Event::scalar(span, style, props, None)
        } else {
            Event::new(EventKind::Alias, span)
        });
        self.begin_content();
        self.commit_scratch(events);
        self.child_done();
        true
    }

    /// The head is a plain block mapping value on its entry's line, so a
    /// `: ` after its first line is an inline colon (module docs).
    fn inline_colon_value(&self, ctx: NodeCtx, content: &Content<'a>) -> bool {
        let plain = matches!(
            content,
            Content::Scalar {
                style: ScalarStyle::Plain,
                ..
            }
        );
        let in_value = matches!(
            self.stack.last(),
            Some(Frame::Map {
                phase: MapPhase::Value,
                ..
            })
        );
        plain && in_value && !self.fresh && !ctx.compact
    }

    /// An inline colon in a plain value (module docs): the Q2.1 error with
    /// its quoting fix, or, under `lenient_inline_colons`, one plain scalar
    /// and a warning.
    fn inline_colon(&mut self, mut key: KeyHead<'a>, ctx: NodeCtx) {
        self.mark_before_error = None;
        let src = self.src;
        let start = key.content_start;
        let colon = key.colon;
        let first_end = plain_line_end_lenient(src, start);
        let full = scalar::plain(src, start, first_end, ctx.parent, false);
        // Quoting fixes the whole problem unless a continuation line also
        // has a `: ` (then it is a multi-line key, reported on its own).
        // A character a quoted scalar cannot hold (a C0 control read into
        // the lenient first line) keeps the quoted value invalid.
        let quotable = src
            .get(start..full.end)
            .is_some_and(|t| t.chars().all(|c| is_nb_json(c) || c == '\n' || c == '\r'));
        // ... and so does anything left on the line after the scalar
        // (it stopped before a character a plain scalar cannot hold).
        let rest = skip_white(src, full.end);
        let clean_rest = match byte(src, rest) {
            None | Some(b'\n' | b'\r') => true,
            Some(b'#') => rest > full.end,
            _ => false,
        };
        let applicability = if !quotable
            || !clean_rest
            || full.multiline && indicator_at(src, skip_white(src, full.end), b':')
        {
            Applicability::MaybeIncorrect
        } else {
            Applicability::MachineApplicable
        };
        // Unit 46d: a byte order mark ends the plain scalar (it is no
        // `ns-char`), but the text glued after it belongs to the value the
        // writer meant (`a: b\u{feff}c`): the fix quotes through the rest
        // of the line (before a comment), leaving the mark out. It overlaps
        // the mark's own fix, so it is only offered (maybe-incorrect).
        let fix = match glued_after_mark(src, full.end).filter(|_| !full.multiline) {
            Some(end) => quote_fix(src, start, end, Applicability::MaybeIncorrect),
            None => quote_fix(src, start, full.end, applicability),
        };
        let colon_label = Span::new(colon, colon.saturating_add(1));
        if !self.lenient_inline_colons {
            let message = "`: ` inside an unquoted value starts a nested mapping, which cannot begin on this line";
            return self.fail_with(INLINE_COLON_IN_PLAIN_VALUE, message, true, |d| {
                d.with_secondary(Label::new(colon_label, "this `: ` starts a mapping"))
                    .with_help(
                        "to keep the `: ` as text, quote the whole value; a nested mapping \
                         must start on its own, more indented line \
                         (Settings::lenient_inline_colons accepts such values with a warning)",
                    )
                    .with_suggestion(fix)
            });
        }
        // Q2.1 leniency: the rest of the line is part of the plain scalar.
        key.content = Content::Scalar {
            style: ScalarStyle::Plain,
            len: first_end.saturating_sub(start),
            multiline: false,
            first_end,
        };
        key.content_end = first_end;
        if self.emit_node(key, ctx) {
            let warning = Diagnostic::warning(
                LENIENT_INLINE_COLON,
                "`: ` inside an unquoted value was read as text (lenient_inline_colons)",
                Label::new(colon_label, "read as text, not as the start of a mapping"),
            )
            .with_help("quote the value to make the document portable YAML")
            .with_suggestion(fix);
            self.zero(EventKind::Warning(Box::new(warning)));
        }
    }

    /// A key line without `:` (`MISSING_MAPPING_COLON`). When the key is a
    /// plain scalar holding a `:` glued to the next character (`b:2`), the
    /// diagnostic points at that `:` and offers the space (Q3.4 polish).
    fn missing_colon(&mut self, key: &KeyHead<'a>) {
        let src = self.src;
        let glued = match key.content {
            Content::Scalar {
                style: ScalarStyle::Plain,
                first_end,
                ..
            } => src.get(key.content_start..first_end).and_then(|text| {
                let k = text.char_indices().skip(1).find(|(_, c)| *c == ':')?.0;
                key.content_start.checked_add(k)
            }),
            _ => None,
        };
        let message = "a mapping key needs `:`";
        let Some(at) = glued else {
            return self.fail(MISSING_MAPPING_COLON, message, false);
        };
        let after = at.saturating_add(1);
        self.fail_with(MISSING_MAPPING_COLON, message, false, |d| {
            d.with_secondary(Label::new(Span::new(at, after), "no space after this `:`"))
                .with_help(
                    "a `:` separates a key from its value only when a space or the end of \
                     the line follows it, so this line is one plain scalar, not `key: value`",
                )
                .with_suggestion(Suggestion::new(
                    "add a space after the `:`",
                    vec![Replacement::new(Span::empty(after), " ")],
                    Applicability::MaybeIncorrect,
                ))
        });
    }

    /// A failed node head's content at `at` (Q3.4 polish, `polish.rs`):
    /// the same diagnostic, with labels, help, and fixes where they apply.
    fn fail_content(
        &mut self,
        code: Code,
        message: &str,
        slot_open: bool,
        at: usize,
        place: Place,
    ) {
        match polish::content(self.src, code, at, place) {
            Some(p) => self.fail_with(code, message, slot_open, |d| p.apply(d)),
            None => self.fail(code, message, slot_open),
        }
    }

    /// Content right after a node (`UNEXPECTED_CONTENT`), polished when it
    /// is glued to the closing quote of the quoted scalar just parsed
    /// (`'it's'`, `"a"#c`).
    pub(super) fn unexpected_content(&mut self) {
        let message = "unexpected content after a node";
        let parent = match self.stack.last() {
            Some(Frame::Map { indent, .. } | Frame::Seq { indent }) => Some(*indent),
            _ => None,
        };
        match polish::after_quote(self.src, self.last_quoted, self.pos, parent) {
            Some(p) => self.fail_with(UNEXPECTED_CONTENT, message, false, |d| p.apply(d)),
            None => self.fail(UNEXPECTED_CONTENT, message, false),
        }
    }

    /// After an implicit key's `:`: the value (`c-l-block-map-implicit-value`,
    /// production 194) of the mapping with keys at `indent`.
    pub(super) fn set_key_value(&mut self, indent: usize) {
        if let Some(Frame::Map { phase, .. }) = self.stack.last_mut() {
            *phase = MapPhase::Value;
        }
        self.state = State::Node(NodeCtx {
            parent: Some(indent),
            compact: false,
            seq_at_parent: true,
        });
    }

    /// Emits a key head: properties, the key node, white space, and `:`.
    fn commit_key(&mut self, key: KeyHead<'a>) {
        let KeyHead {
            mut events,
            props,
            content,
            content_start,
            content_end,
            props_end,
            colon,
        } = key;
        let node = match content {
            Content::Alias(_) => event(EventKind::Alias, content_start, content_end),
            Content::Scalar { style, .. } => {
                Event::scalar(Span::new(content_start, content_end), style, props, None)
            }
            Content::Empty => {
                // Placement rule 1: right after the last property.
                if content_start > props_end {
                    pop_white(&mut events);
                }
                Event::scalar(Span::empty(props_end), ScalarStyle::Plain, props, None)
            }
        };
        events.push(node);
        // White space, and byte order marks skipped as if absent (unit
        // 41d), between the key and its `:`.
        self.mark_before_error = None;
        let src = self.src;
        let mut at = content_end;
        while at < colon {
            let white = skip_white(src, at);
            if white > at {
                events.push(event(EventKind::Trivia(TriviaKind::Whitespace), at, white));
                at = white;
            } else if mark_at(src, at) {
                let end = at.saturating_add('\u{feff}'.len_utf8());
                events.push(event(
                    EventKind::Error(super::misplaced_bom_diagnostic(at, end)),
                    at,
                    end,
                ));
                at = end;
            } else {
                // `colon` was found by skipping only these (NN-4).
                debug_assert!(false, "unexpected byte before a key's `:`");
                events.push(event(
                    EventKind::Error(super::internal(
                        "bytes before a key's `:`",
                        Span::new(at, colon),
                    )),
                    at,
                    colon,
                ));
                at = colon;
            }
        }
        events.push(event(
            EventKind::Trivia(TriviaKind::Indicator(Indicator::MappingValue)),
            colon,
            colon.saturating_add(1),
        ));
        self.commit_scratch(events);
    }
}