tabnas-yaml 0.5.13

A core-subset YAML grammar plugin for the tabnas parsing engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/* Copyright (c) 2021-2026 Richard Rodger, MIT License */

//! Scalars: block scalars, plain scalars and typed tags.
//!
//! The canonical implementation puts the first two in `options.text.check`,
//! the hook the engine's text matcher runs before it reads a word. This
//! engine's check hook sees the lexer alone, and a YAML plain scalar needs
//! the per-parse flow-depth cache, so the same work is registered as a
//! second custom matcher in the band between the number matcher and the
//! text matcher: the position `text.check` occupies, with the context the
//! hook cannot supply.
//!
//! The typed-tag handler is called from [`crate::lex`] rather than from
//! here, because the canonical matcher reaches `!!str` and friends before
//! the text check ever sees them.

use std::sync::OnceLock;

use regex::Regex;
use tabnas::{Context, Lexer, Rule, Token, Value};

use crate::lex::{
    advance_cols, apply, at, blank, blank_line_end_or_eof, is, is_doc_marker, is_doc_marker_no_tab,
    js_substring, js_substring_less_one_unit, line_end, trim_end, Act, Tok,
};
use crate::state;

/// The reference name the grammar's `options.lex.match` entry uses.
pub(crate) const MATCHER: &str = "@yaml-text";

/// The matcher that stands in for `options.text.check`.
pub(crate) fn yaml_text(
    lexer: &mut Lexer<'_>,
    _rule: &mut Rule,
    context: &mut Context,
) -> Option<Token> {
    let (row, column) = crate::lex::virtual_point_for(lexer, context);
    let start = lexer.point().site.si;
    let act = {
        let source = lexer.source();
        check(source, (start, row, column), context)
    };
    let act = act?;
    apply(lexer, context, act)
}

/// The body of `options.text.check`: a block scalar, then the characters
/// other matchers own, then a plain scalar.
fn check(src: &str, cursor: (usize, usize, usize), context: &mut Context) -> Option<Act> {
    let fwd = &src[cursor.0.min(src.len())..];
    if fwd.is_empty() {
        return None;
    }
    let head = at(fwd, 0);

    if head == i32::from(b'|') || head == i32::from(b'>') {
        if let Some(act) = block_scalar(src, fwd, cursor) {
            return Some(act);
        }
        // A block scalar indicator followed by text on the same line
        // (`a: > x`) is invalid YAML. The canonical handler does not
        // refuse it: the branch simply falls through to plain-scalar
        // handling, which yields `{"a": "> x"}`.
        return Some(plain_scalar(src, fwd, cursor, context));
    }

    // Characters another matcher owns, and the anchor, alias and tag
    // markers the YAML matcher has already had its chance at.
    for owned in [
        b'{', b'}', b'[', b']', b',', b'#', b'\n', b'\r', b'"', b'\'', b'*', b'&', b'!',
    ] {
        if head == i32::from(owned) {
            return None;
        }
    }

    // A colon that separates a key from a value is not text.
    if head == i32::from(b':') && blank_line_end_or_eof(at(fwd, 1)) {
        return None;
    }

    // The number matcher runs between the YAML matcher and this check,
    // and the engine gates it on the character the lexer call started
    // on. When the YAML matcher moved the cursor past an anchor or a tag
    // and produced nothing, that character is the marker, so the number
    // matcher never sees the value, and a plain scalar would swallow the
    // next line as a continuation where the canonical parser stops at
    // the number. Stand in for it here, in the position it would have
    // run.
    if state::flag(context, state::MOVED) {
        if let Some(act) = number_run(src, fwd, cursor) {
            return Some(act);
        }
    }

    Some(plain_scalar(src, fwd, cursor, context))
}

/// The shape the engine's number matcher accepts, under the jsonic
/// options this plugin inherits: hexadecimal, octal and binary forms on,
/// and `_` as the digit separator. It is the canonical lexer's own
/// pattern, written out with that separator in place, so a separator is
/// legal only where the engine allows one: anywhere after a base prefix,
/// and between two digits of a decimal run.
fn engine_number() -> &'static Regex {
    static PATTERN: OnceLock<Regex> = OnceLock::new();
    PATTERN.get_or_init(|| {
        Regex::new(concat!(
            "^[-+]?(?:",
            "0(?:[xX][0-9a-fA-F_]+|[oO][0-7_]+|[bB][01_]+)",
            "|",
            r"\.?[0-9]+(?:[0-9_]*[0-9])?",
            r"(?:\.(?:[0-9](?:[0-9_]*[0-9])?)?)?",
            "(?:[eE][-+]?[0-9]+(?:[0-9_]*[0-9])?)?",
            ")$"
        ))
        .expect("the number pattern is a literal and compiles")
    })
}

/// The token the engine's number matcher would have produced: the run up
/// to the next delimiter, when the whole of it has the engine's number
/// shape.
///
/// The value is the engine's too. The canonical matcher strips the
/// separators and applies the unary plus, and where that is `NaN` on a
/// signed base-prefixed form, which `Number` does not accept, it
/// converts the unsigned rest and applies the sign itself.
fn number_run(src: &str, fwd: &str, cursor: (usize, usize, usize)) -> Option<Act> {
    let head = at(fwd, 0);
    let numeric = head == i32::from(b'-')
        || head == i32::from(b'+')
        || head == i32::from(b'.')
        || (head >= i32::from(b'0') && head <= i32::from(b'9'));
    if !numeric {
        return None;
    }
    let mut end = 0;
    while end < fwd.len() {
        let byte = at(fwd, end);
        if blank_line_end_or_eof(byte)
            || byte == i32::from(b',')
            || byte == i32::from(b'[')
            || byte == i32::from(b']')
            || byte == i32::from(b'{')
            || byte == i32::from(b'}')
            || byte == i32::from(b':')
            || byte == i32::from(b'#')
        {
            break;
        }
        end += 1;
    }
    let run = &fwd[..end];
    if !engine_number().is_match(run) {
        return None;
    }
    let stripped: String = run.chars().filter(|character| *character != '_').collect();
    let number = crate::js_to_number(&stripped).or_else(|| {
        let (sign, rest) = match stripped.as_bytes().first() {
            Some(b'-') => (-1.0, &stripped[1..]),
            Some(b'+') => (1.0, &stripped[1..]),
            _ => return None,
        };
        crate::js_to_number(rest).map(|value| sign * value)
    })?;
    let next = advance_cols(src, cursor, end);
    Some(Act::moved(
        next,
        Some(Tok::new("#NR", Value::Number(number), run, cursor)),
    ))
}

// ---------------------------------------------------------------------
// Block scalars
// ---------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Eq)]
enum Chomp {
    Clip,
    Strip,
    Keep,
}

/// A literal (`|`) or folded (`>`) block scalar. `None` means the
/// indicator is not one, and the caller falls through to a plain scalar.
#[allow(clippy::too_many_lines)]
fn block_scalar(src: &str, fwd: &str, cursor: (usize, usize, usize)) -> Option<Act> {
    let fold = is(fwd, 0, b'>');
    let mut chomp = Chomp::Clip;
    let mut explicit_indent = 0usize;
    let mut idx = 1;
    // The chomping and indentation indicators, in either order.
    for _ in 0..2 {
        let byte = at(fwd, idx);
        if byte == i32::from(b'+') {
            chomp = Chomp::Keep;
            idx += 1;
        } else if byte == i32::from(b'-') {
            chomp = Chomp::Strip;
            idx += 1;
        } else if (i32::from(b'1')..=i32::from(b'9')).contains(&byte) {
            explicit_indent = (byte - i32::from(b'0')) as usize;
            idx += 1;
        }
    }

    while is(fwd, idx, b' ') {
        idx += 1;
    }
    if is(fwd, idx, b'#') {
        while idx < fwd.len() && !line_end(at(fwd, idx)) {
            idx += 1;
        }
    }
    if !line_end(at(fwd, idx)) && idx < fwd.len() {
        // Text on the indicator's own line: not a block scalar.
        return None;
    }

    if is(fwd, idx, b'\r') {
        idx += 1;
    }
    if is(fwd, idx, b'\n') {
        idx += 1;
    }

    // The block's indent: the first content line's, unless given.
    let mut block_indent = 0usize;
    if explicit_indent == 0 {
        let mut probe = idx;
        while probe < fwd.len() {
            let mut spaces = 0;
            while is(fwd, probe + spaces, b' ') {
                spaces += 1;
            }
            let after = probe + spaces;
            if after >= fwd.len() || line_end(at(fwd, after)) {
                probe = after;
                if is(fwd, probe, b'\r') {
                    probe += 1;
                }
                if is(fwd, probe, b'\n') {
                    probe += 1;
                }
                continue;
            }
            block_indent = spaces;
            break;
        }
    }

    // The indent of the line the indicator sits on. Content must be more
    // indented than that, or the scalar is empty. A document's root node
    // has parent indent -1, so column 0 content under a column 0
    // indicator, or one after `---`, is not empty.
    let mut containing_indent = 0usize;
    let is_doc_start;
    let is_doc_root;
    {
        let mut index = crate::lex::line_start_of(src, cursor.0);
        let line_start = index;
        while index < cursor.0 && is(src, index, b' ') {
            containing_indent += 1;
            index += 1;
        }
        is_doc_start = is(src, line_start, b'-')
            && is(src, line_start + 1, b'-')
            && is(src, line_start + 2, b'-');
        is_doc_root = containing_indent == 0 && index == cursor.0;
    }

    if explicit_indent > 0 {
        let mut index = cursor.0;
        while index > 0 && !line_end(at(src, index - 1)) {
            index -= 1;
        }
        let mut key_column = containing_indent;
        let mut has_colon = false;
        for probe in index + containing_indent..cursor.0 {
            if is(src, probe, b':') && blank(at(src, probe + 1)) {
                has_colon = true;
                break;
            }
        }
        if has_colon {
            // `key: |2`: each `- ` before the key adds to the indent.
            let mut scan = index + containing_indent;
            while scan < cursor.0 && is(src, scan, b'-') && blank(at(src, scan + 1)) {
                key_column += 2;
                scan += 2;
                while scan < cursor.0 && is(src, scan, b' ') {
                    key_column += 1;
                    scan += 1;
                }
            }
            block_indent = key_column + explicit_indent;
        } else {
            // The indicator is alone on its line: the parent mapping key
            // is on the line before, and its indent is the base.
            let mut parent_indent = 0usize;
            let mut search = index as isize - 1;
            if search > 0 {
                if is(src, search as usize, b'\n') {
                    search -= 1;
                }
                if search >= 0 && is(src, search as usize, b'\r') {
                    search -= 1;
                }
                let previous_end = (search + 1).max(0) as usize;
                let mut previous_start = search.max(0) as usize;
                while previous_start > 0 && !line_end(at(src, previous_start - 1)) {
                    previous_start -= 1;
                }
                for probe in previous_start..previous_end {
                    if is(src, probe, b':')
                        && (blank(at(src, probe + 1))
                            || line_end(at(src, probe + 1))
                            || probe + 1 >= previous_end)
                    {
                        parent_indent = 0;
                        let mut leading = previous_start;
                        while leading < previous_end && is(src, leading, b' ') {
                            parent_indent += 1;
                            leading += 1;
                        }
                        break;
                    }
                }
            }
            block_indent = parent_indent + explicit_indent;
            containing_indent = parent_indent;
        }
    }

    if block_indent <= containing_indent && !is_doc_start && !is_doc_root && idx < fwd.len() {
        // The content is not indented enough: the scalar is empty. With
        // keep chomping the trailing blank lines still count.
        let mut value = String::new();
        let mut end = idx;
        if chomp == Chomp::Keep {
            let mut blanks = 0;
            while end < fwd.len() {
                if is(fwd, end, b'\n') {
                    blanks += 1;
                    end += 1;
                } else if is(fwd, end, b'\r') {
                    end += 1;
                    if is(fwd, end, b'\n') {
                        end += 1;
                    }
                    blanks += 1;
                } else {
                    break;
                }
            }
            value = "\n".repeat(blanks.max(1));
        }
        let source = fwd[..end].to_string();
        return Some(Act::moved(
            (cursor.0 + end, cursor.1 + 1, 0),
            Some(Tok::new("#TX", Value::String(value), source, cursor)),
        ));
    }

    // The content lines, with the block indent stripped.
    let mut lines: Vec<String> = Vec::new();
    let mut pos = idx;
    let mut rows = 1usize;
    let mut last_newline = idx;
    while pos < fwd.len() {
        let mut line_indent = 0;
        while is(fwd, pos + line_indent, b' ') {
            line_indent += 1;
        }
        let after = pos + line_indent;
        if after >= fwd.len() || line_end(at(fwd, after)) {
            // A blank line keeps whatever sits past the block indent.
            if line_indent > block_indent {
                lines.push(fwd[pos + block_indent..after].to_string());
            } else {
                lines.push(String::new());
            }
            last_newline = after;
            pos = after;
            if is(fwd, pos, b'\r') {
                pos += 1;
            }
            if is(fwd, pos, b'\n') {
                pos += 1;
            }
            rows += 1;
            continue;
        }
        if line_indent < block_indent {
            break;
        }
        // `fwd[pos+3] === '\n' || '\r' || ' ' || undefined`, spelled
        // out and WITHOUT a tab, unlike the marker test everywhere else.
        if line_indent == 0 && is_doc_marker_no_tab(fwd, pos) {
            break;
        }
        let line_start = pos + block_indent;
        let mut line_stop = line_start;
        while line_stop < fwd.len() && !line_end(at(fwd, line_stop)) {
            line_stop += 1;
        }
        lines.push(fwd[line_start..line_stop].to_string());
        last_newline = line_stop;
        pos = line_stop;
        if is(fwd, pos, b'\r') {
            pos += 1;
        }
        if is(fwd, pos, b'\n') {
            pos += 1;
        }
        rows += 1;
    }

    let mut value = if fold {
        fold_lines(&lines)
    } else {
        lines.join("\n")
    };

    if lines.is_empty() {
        value = String::new();
    } else {
        match chomp {
            Chomp::Strip => {
                while value.ends_with('\n') {
                    value.pop();
                }
            }
            Chomp::Clip => {
                while value.ends_with('\n') {
                    value.pop();
                }
                value.push('\n');
            }
            Chomp::Keep => value.push('\n'),
        }
    }

    // When the block ended because the indent dropped, and what follows
    // is not a document marker, leave the final newline for the indent
    // handler so the grammar still sees where the next line starts.
    let mut end_pos = pos;
    let mut end_rows = rows;
    if pos < fwd.len() && pos > last_newline {
        let mut next_indent = 0;
        let mut probe = pos;
        while is(fwd, probe, b' ') {
            next_indent += 1;
            probe += 1;
        }
        let marker = next_indent == 0
            && ((is(fwd, probe, b'-') && is(fwd, probe + 1, b'-') && is(fwd, probe + 2, b'-'))
                || (is(fwd, probe, b'.') && is(fwd, probe + 1, b'.') && is(fwd, probe + 2, b'.')));
        if !marker {
            end_pos = last_newline;
            end_rows = rows - 1;
        }
    }

    let source = fwd[..end_pos].to_string();
    Some(Act::moved(
        (cursor.0 + end_pos, cursor.1 + end_rows, 0),
        Some(Tok::new("#TX", Value::String(value), source, cursor)),
    ))
}

/// Folded-scalar line joining: a run of normal lines becomes one line
/// joined by spaces, blank lines become newlines, and a more-indented
/// line is kept verbatim with newlines around it.
fn fold_lines(lines: &[String]) -> String {
    let mut result = String::new();
    let mut previous_normal = false;
    let mut pending_empty = 0usize;
    for line in lines {
        let more_indented = line.starts_with(' ') || line.starts_with('\t');
        if line.is_empty() {
            pending_empty += 1;
        } else if more_indented {
            if previous_normal && !result.is_empty() {
                result.push('\n');
            }
            for _ in 0..pending_empty {
                result.push('\n');
            }
            pending_empty = 0;
            if !result.is_empty() && !result.ends_with('\n') {
                result.push('\n');
            }
            result.push_str(line);
            result.push('\n');
            previous_normal = false;
        } else {
            if pending_empty > 0 {
                if previous_normal && !result.is_empty() {
                    result.push('\n');
                    for _ in 1..pending_empty {
                        result.push('\n');
                    }
                } else {
                    for _ in 0..pending_empty {
                        result.push('\n');
                    }
                }
                pending_empty = 0;
            }
            if previous_normal && !result.is_empty() && !result.ends_with('\n') {
                result.push(' ');
            }
            result.push_str(line);
            previous_normal = true;
        }
    }
    for _ in 0..pending_empty {
        result.push('\n');
    }
    result
}

// ---------------------------------------------------------------------
// Plain scalars
// ---------------------------------------------------------------------

/// A YAML plain scalar, to the end of the line, with the continuation
/// lines a multi-line plain scalar folds in.
#[allow(clippy::too_many_lines)]
pub(crate) fn plain_scalar(
    src: &str,
    fwd: &str,
    cursor: (usize, usize, usize),
    context: &mut Context,
) -> Act {
    let in_flow = state::flow_depth_at(context, src, cursor.0) > 0;

    let line_start = crate::lex::line_start_of(src, cursor.0);

    let mut current_line_indent = 0usize;
    {
        let mut index = line_start;
        while index < cursor.0 && is(src, index, b' ') {
            current_line_indent += 1;
            index += 1;
        }
    }

    // Preceded by `: ` on the same line, so this is a mapping value.
    let mut is_map_value = false;
    {
        let mut index = cursor.0 as isize - 1;
        while index >= line_start as isize && blank(at(src, index as usize)) {
            index -= 1;
        }
        if index >= line_start as isize && is(src, index as usize, b':') {
            is_map_value = true;
        }
    }

    let minimum_indent = if is_map_value {
        current_line_indent + 1
    } else {
        current_line_indent
    };

    let mut index = 0usize;
    let scan_line = |fwd: &str, index: &mut usize| -> String {
        let start = *index;
        let mut stop = *index;
        while stop < fwd.len() {
            let byte = at(fwd, stop);
            if line_end(byte) {
                break;
            }
            if byte == i32::from(b':') && blank_line_end_or_eof(at(fwd, stop + 1)) {
                break;
            }
            if blank(byte) && is(fwd, stop + 1, b'#') {
                break;
            }
            if in_flow && (byte == i32::from(b']') || byte == i32::from(b'}')) {
                break;
            }
            if byte == i32::from(b',') && in_flow {
                break;
            }
            stop += 1;
        }
        *index = stop;
        trim_end(&fwd[start..stop]).to_string()
    };

    let mut text = scan_line(fwd, &mut index);
    let mut total_consumed = index;
    let mut rows = 0usize;

    while index < fwd.len() && line_end(at(fwd, index)) {
        let mut blank_lines = 0usize;
        while index < fwd.len() && line_end(at(fwd, index)) {
            if is(fwd, index, b'\r') {
                index += 1;
            }
            if is(fwd, index, b'\n') {
                index += 1;
            }
            let mut leading = 0;
            while blank(at(fwd, index + leading)) {
                leading += 1;
            }
            if index + leading >= fwd.len() || line_end(at(fwd, index + leading)) {
                blank_lines += 1;
                index += leading;
                continue;
            }
            break;
        }
        let mut line_indent = 0usize;
        while blank(at(fwd, index)) {
            line_indent += 1;
            index += 1;
        }

        let doc_marker = line_indent == 0 && is_doc_marker(fwd, index);

        // A `- ` only starts a new sequence entry when its indent matches
        // an enclosing sequence's.
        let mut sequence_marker = false;
        if is(fwd, index, b'-') && blank_line_end_or_eof(at(fwd, index + 1)) {
            let mut sequence_indent: isize = -1;
            let mut probe = cursor.0 as isize - 1;
            while probe >= line_start as isize {
                if is(src, probe as usize, b'-') && blank(at(src, probe as usize + 1)) {
                    sequence_indent = probe - line_start as isize;
                    break;
                }
                probe -= 1;
            }
            sequence_marker = (sequence_indent >= 0 && line_indent as isize == sequence_indent)
                || (sequence_indent < 0 && line_indent <= current_line_indent);
        }

        let can_continue = if in_flow {
            index < fwd.len()
                && !line_end(at(fwd, index))
                && !is(fwd, index, b'#')
                && !is(fwd, index, b'{')
                && !is(fwd, index, b'}')
                && !is(fwd, index, b'[')
                && !is(fwd, index, b']')
        } else {
            line_indent >= minimum_indent
                && index < fwd.len()
                && !line_end(at(fwd, index))
                && !is(fwd, index, b'#')
                && !doc_marker
                && !sequence_marker
        };

        if can_continue {
            // A line holding `: ` is a new pair, not a continuation.
            let mut peek = index;
            let mut is_pair = false;
            while peek < fwd.len() && !line_end(at(fwd, peek)) {
                if is(fwd, peek, b':') && blank_line_end_or_eof(at(fwd, peek + 1)) {
                    is_pair = true;
                    break;
                }
                if is(fwd, peek, b'}') || is(fwd, peek, b']') || is(fwd, peek, b',') {
                    break;
                }
                peek += 1;
            }
            if !is_pair || in_flow {
                let continuation = scan_line(fwd, &mut index);
                if !continuation.is_empty() {
                    if blank_lines > 0 {
                        for _ in 0..blank_lines {
                            text.push('\n');
                        }
                    } else {
                        text.push(' ');
                    }
                    text.push_str(&continuation);
                    total_consumed = index;
                    rows += 1;
                    continue;
                }
            }
        }
        // Not a continuation. The canonical handler puts `i` back to
        // the newline here; nothing reads it after the loop, because the
        // token's span is `total_consumed`, which was last set at the
        // end of a line that WAS taken.
        break;
    }

    let text = trim_end(&text).to_string();
    if text.is_empty() {
        return Act::still(cursor);
    }

    if let Some(value) = crate::yaml_keyword(&text) {
        let next = advance_cols(src, cursor, text.len());
        return Act::moved(next, Some(Tok::new("#VL", value, text, cursor)));
    }

    if let Some(number) = crate::js_to_number(&text) {
        if !number.is_nan() {
            let next = advance_cols(src, cursor, text.len());
            return Act::moved(
                next,
                Some(Tok::new("#NR", Value::Number(number), text, cursor)),
            );
        }
    }

    let source = fwd[..total_consumed].to_string();
    let columns = src[cursor.0..(cursor.0 + total_consumed).min(src.len())]
        .chars()
        .count();
    Act::moved(
        (
            cursor.0 + total_consumed,
            cursor.1 + rows,
            cursor.2 + columns,
        ),
        Some(Tok::new("#TX", Value::String(text), source, cursor)),
    )
}

// ---------------------------------------------------------------------
// Typed tags
// ---------------------------------------------------------------------

/// `!!str`, `!!int`, `!!float`, `!!bool`, `!!null` and any other typed
/// tag, applied to the value that follows. `Ok` means the tag was
/// consumed and the value is on a later line; `Err` carries the token.
pub(crate) fn type_tag(
    src: &str,
    fwd: &str,
    cursor: (usize, usize, usize),
    context: &mut Context,
) -> Result<(usize, usize, usize), Box<Act>> {
    let mut tag_end = 2;
    while tag_end < fwd.len() {
        let byte = at(fwd, tag_end);
        // A tab does not end a tag name here either.
        if byte == i32::from(b' ')
            || line_end(byte)
            || byte == i32::from(b',')
            || byte == i32::from(b'}')
            || byte == i32::from(b']')
            || byte == i32::from(b':')
        {
            break;
        }
        tag_end += 1;
    }
    let tag = fwd[2..tag_end].to_string();
    let mut value_start = tag_end;
    if is(fwd, value_start, b' ') {
        value_start += 1;
    }
    let mut value_end = value_start;

    // An anchor between the tag and the value.
    let mut anchor_name = String::new();
    if is(fwd, value_start, b'&') {
        // `fwd[anchorEnd] !== ' ' && !== '\n' && !== '\r'`: a SPACE or
        // a line end ends the name here, and a TAB does not. It is not
        // the anchor scan the lexer's own `&name` handler runs, which
        // stops at a tab and at a flow indicator too.
        let mut anchor_end = value_start + 1;
        while anchor_end < fwd.len() && !is(fwd, anchor_end, b' ') && !line_end(at(fwd, anchor_end))
        {
            anchor_end += 1;
        }
        anchor_name = fwd[value_start + 1..anchor_end].to_string();
        let mut pending = indexmap::IndexMap::new();
        pending.insert("n".to_string(), Value::String(anchor_name.clone()));
        pending.insert("i".to_string(), Value::Bool(true));
        state::list_push(context, state::PENDING_ANCHORS, Value::object(pending));
        if is(fwd, anchor_end, b' ') {
            anchor_end += 1;
        }
        value_start = anchor_end;
        value_end = value_start;
    }

    let redefined = !matches!(
        state::map_get(context, state::TAG_HANDLES, "!!"),
        None | Some(Value::Undefined)
    );

    // A quoted value.
    if is(fwd, value_start, b'"') || is(fwd, value_start, b'\'') {
        let quote = at(fwd, value_start);
        value_end = value_start + 1;
        while value_end < fwd.len() && at(fwd, value_end) != quote {
            if is(fwd, value_end, b'\\') && quote == i32::from(b'"') {
                value_end += 1;
            }
            value_end += 1;
        }
        if at(fwd, value_end) == quote {
            value_end += 1;
        }
        // `fwd.substring(valStart + 1, valEnd - 1)`. An unterminated quote
        // leaves `value_end` at the end of the source, and an escape at
        // the very end leaves it one past: neither index can be sliced
        // directly, and stepping one BYTE back from the end lands inside
        // a multibyte character where the canonical steps one UTF-16
        // unit.
        let raw = js_substring_less_one_unit(fwd, value_start + 1, value_end);
        let value = convert_tag(&tag, &raw, redefined, true);
        if !anchor_name.is_empty() {
            state::map_set(context, state::ANCHORS, anchor_name, value.clone());
        }
        let name = token_name_for(&value, false);
        let source = js_substring(fwd, 0, value_end).to_string();
        let next = advance_cols(src, cursor, value_end);
        return Err(Box::new(Act::moved(
            next,
            Some(Tok::new(name, value, source, cursor)),
        )));
    }

    // The value is on the next line: consume the tag and look again.
    if line_end(at(fwd, value_start)) && value_start + 1 < fwd.len() {
        let mut newline = value_start;
        if is(fwd, newline, b'\r') {
            newline += 1;
        }
        if is(fwd, newline, b'\n') {
            newline += 1;
        }
        return Ok((cursor.0 + newline, cursor.1 + 1, 0));
    }

    // An unquoted value, to `: `, ` #`, the line end or a flow indicator.
    // A `#` straight after the tag's space starts a comment, so the value
    // is empty (`!!str #c` is "").
    while value_end < fwd.len() {
        let byte = at(fwd, value_end);
        if value_end == value_start
            && byte == i32::from(b'#')
            && value_start > 0
            && blank(at(fwd, value_start - 1))
        {
            break;
        }
        if line_end(byte)
            || byte == i32::from(b',')
            || byte == i32::from(b'}')
            || byte == i32::from(b']')
        {
            break;
        }
        // `fwd[valEnd+1] === ' ' || '\n' || '\r' || undefined`: a
        // literal SPACE, spelled out, with no tab among them. A tab
        // after the colon leaves the colon inside the value.
        if byte == i32::from(b':')
            && (is(fwd, value_end + 1, b' ')
                || line_end(at(fwd, value_end + 1))
                || at(fwd, value_end + 1) < 0)
        {
            break;
        }
        if byte == i32::from(b' ') && is(fwd, value_end + 1, b'#') {
            break;
        }
        value_end += 1;
    }
    let raw = trim_end(&fwd[value_start..value_end]).to_string();
    let value = convert_tag(&tag, &raw, redefined, false);
    if !anchor_name.is_empty() {
        state::map_set(context, state::ANCHORS, anchor_name, value.clone());
    }
    let name = token_name_for(&value, true);
    let source = fwd[..value_end].to_string();
    let next = advance_cols(src, cursor, value_end);
    Err(Box::new(Act::moved(
        next,
        Some(Tok::new(name, value, source, cursor)),
    )))
}

/// Apply a typed tag's conversion. When `%TAG !!` has been redefined the
/// tag is a user handle, not a YAML core type, and only a quoted value's
/// raw text survives; an unquoted one still takes the `str` conversion,
/// as the canonical handler leaves it.
fn convert_tag(tag: &str, raw: &str, redefined: bool, quoted: bool) -> Value {
    if redefined {
        return Value::String(raw.to_string());
    }
    match tag {
        "str" if !quoted => Value::String(raw.to_string()),
        "int" => Value::Number(tag_int(raw)),
        "float" => Value::Number(tag_float(raw)),
        "bool" => Value::Bool(raw == "true" || raw == "True" || raw == "TRUE"),
        "null" => Value::Null,
        _ => Value::String(raw.to_string()),
    }
}

/// The value a `!!int` tag gives its text: YAML's core-schema hex
/// (`0x1f`) and octal (`0o17`) forms, then a decimal parse. Mirrors the
/// canonical `yamlTagInt`.
fn tag_int(raw: &str) -> f64 {
    let (sign, body) = match raw.as_bytes().first() {
        Some(b'-') => (-1.0, &raw[1..]),
        Some(b'+') => (1.0, &raw[1..]),
        _ => (1.0, raw),
    };
    let radix = if let Some(digits) = body.strip_prefix("0x") {
        Some((digits, 16))
    } else {
        body.strip_prefix("0o").map(|digits| (digits, 8))
    };
    if let Some((digits, radix)) = radix {
        if !digits.is_empty() && digits.chars().all(|c| c.is_digit(radix)) {
            return sign * radix_to_f64(digits, radix);
        }
    }
    crate::parse_int(raw)
}

/// Hex or octal digits as the nearest `f64`, ties to even, as
/// JavaScript's `parseInt` reads them: exact however many digits there
/// are, and infinite past the largest finite value.
fn radix_to_f64(digits: &str, radix: u32) -> f64 {
    let width = if radix == 16 { 4 } else { 3 };
    // The value's bits, most significant first, without leading zeros.
    let mut bits = Vec::with_capacity(digits.len() * width);
    for digit in digits.chars().filter_map(|c| c.to_digit(radix)) {
        for shift in (0..width).rev() {
            let bit = (digit >> shift) & 1 == 1;
            if bit || !bits.is_empty() {
                bits.push(bit);
            }
        }
    }
    const MANTISSA: usize = 53;
    let kept = bits.len().min(MANTISSA);
    let mut mantissa = bits[..kept]
        .iter()
        .fold(0u64, |acc, &bit| (acc << 1) | u64::from(bit));
    if bits.len() > MANTISSA {
        let round = bits[MANTISSA];
        let sticky = bits[MANTISSA + 1..].iter().any(|&bit| bit);
        if round && (sticky || mantissa & 1 == 1) {
            mantissa += 1;
        }
    }
    let exponent = bits.len() - kept;
    if exponent > 1100 {
        return f64::INFINITY;
    }
    // Both factors are exact, so the product rounds only on overflow.
    mantissa as f64 * 2f64.powi(exponent as i32)
}

/// The value a `!!float` tag gives its text: the core schema's infinities
/// and not-a-number, then a decimal parse. Mirrors the canonical
/// `yamlTagFloat`.
fn tag_float(raw: &str) -> f64 {
    match raw {
        ".inf" | ".Inf" | ".INF" | "+.inf" | "+.Inf" | "+.INF" => f64::INFINITY,
        "-.inf" | "-.Inf" | "-.INF" => f64::NEG_INFINITY,
        ".nan" | ".NaN" | ".NAN" => f64::NAN,
        _ => crate::parse_float(raw),
    }
}

/// The token identity a converted value takes. An empty string becomes
/// `#ST` in the unquoted branch, which the canonical handler does because
/// the engine reads an empty `#TX` badly inside a flow collection.
fn token_name_for(value: &Value, empty_is_string_token: bool) -> &'static str {
    match value {
        Value::String(text) if empty_is_string_token && text.is_empty() => "#ST",
        Value::String(_) => "#TX",
        Value::Number(_) => "#NR",
        _ => "#VL",
    }
}