#! @file scanner.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the scanner, which turns YAML's text into tokens for the reader, built as #! libyaml's is, to YAML 1.2's rules: indentation becomes where block collections #! begin and end, and a `:` makes what came before it on its line a key, so the #! tokens from where a key could have begun are held back until that is known. import core::swar; module std::yaml; # The tokens the scanner gives the reader. enum token_kind: u8 { stream_start, stream_end, version_directive, #! `%YAML`, with its major and minor version. tag_directive, #! `%TAG`, with its handle and prefix. document_start, #! `---` document_end, #! `...` block_sequence_start, #! Indentation beginning a block sequence. block_mapping_start, #! Indentation beginning a block mapping. block_end, #! Indentation ending a block collection. flow_sequence_start, #! `[` flow_sequence_end, #! `]` flow_mapping_start, #! `{` flow_mapping_end, #! `}` block_entry, #! `-` flow_entry, #! `,` key, #! `?`, or before an implicit key. value, #! `:` alias, #! `*name` anchor, #! `&name` tag, #! `!handle!suffix`, or `!`. scalar, } # A token, with up to two texts: a scalar's text, an alias's or anchor's name, a # tag's handle and suffix, a `%TAG` directive's handle and prefix, or a `%YAML` # directive's major and minor version. A scalar's text is in the scanner's buffer # where it is decoded, and every other text is in the text read. struct token { var kind: token_kind; var style: style; #! A scalar's. var decoded: bool; #! Whether a scalar's text is in the buffer. var start: u64; #! The byte the token begins at. var end: u64; #! The byte just past it. var first: u64; var length: u64; var second: u64; var extent: u64; } # Where an implicit key may begin, at one flow level. struct possible_key { var possible: bool; var required: bool; #! Whether it must be a key, since it begins a line of a block mapping. var tabbed: bool; #! Whether a tab is among the blanks before it. var number: u64; #! The token it begins at, counted from the first the scanner gave. var at: u64; var line: u64; var column: u64; } struct scanner { var _text: strview; var _at: u64; #! The next byte. var _line: u64; #! Its line, counted from 0. var _column: u64; #! Its column: the bytes since its line began. var _begun: u64; #! Where its line begins. var _tokens: vector; var _head: u64; #! The first token the reader has not taken. var _taken: u64; #! How many tokens the reader has taken. var _started: bool; var _ended: bool; var _indent: i64; #! The column of the innermost block collection, or -1. var _indents: vector; var _flow: u64; #! How many flow collections are open. var _allowed: bool; #! Whether an implicit key may begin here. var _keys: vector; #! Where a key may begin, for each flow level. var _mappings: vector; #! Whether each open flow collection is a mapping. var _json: bool; #! Whether the last token ends a quoted scalar or flow collection, after which `:` is a value within flow. var _tabbed: bool; #! Whether a tab is among the blanks just before the next token. var _scanned: u64; #! The line the last token began on, past the last line before any has. var _buffer: string; #! The decoded text of the scalars not yet taken. var _failure: maybe; } def scanner::new(text: strview): scanner { return scanner { _text = text, _tokens = vector::::new(), _indents = vector::::new(), _keys = vector::::new(), _mappings = vector::::new(), _scanned = 0xFFFFFFFFFFFFFFFFu, _buffer = string::new(nothing {}), _indent = -1 }; } # The next token, scanning as far as is needed to know it, or `nothing` where the # text is not YAML, as `_failure` then says. def scanner::peek(&mut this): maybe { if !this.fill() { return nothing {}; } return this._tokens.at(this._head); } # Takes the next token, which `peek` gave. def scanner::take(&mut this): void { this._head++; this._taken++; } # A token's first text: a scalar's text, or a name, handle, or version. def scanner::first_of(&this, read: &token): strview { if read.decoded { return this._buffer.substr(read.first, read.length); } return this._text.substr(read.first, read.length); } # A token's second text: a tag's suffix, a directive's prefix, or a minor version. def scanner::second_of(&this, read: &token): strview { return this._text.substr(read.second, read.extent); } # Keeps the first failure, giving `false` so a caller returns it. def scanner::fail(&mut this, kind: error_kind, offset: u64): bool { if this._failure is none { this._failure = error_at(kind, this._text, offset); } return false; } # Scans until the next token is known: until one is held, and no implicit key could # still make the next a key. def scanner::fill(&mut this): bool { while true { if this._failure is some(_) { return false; } var needed := this._head == this._tokens.length; if !needed { if !this.stale_keys() { return false; } for each in this._keys { if each.possible && each.number == this._taken { needed = true; break; } } } if !needed { return true; } # Every token held has been taken, so their memory, and their decoded text, is # used again. if this._head == this._tokens.length { this._tokens.clear(); this._head = 0; this._buffer.clear(); } if !this.fetch() { return false; } } return true; } # Scans the next token. def scanner::fetch(&mut this): bool { if !this._started { return this.start_stream(); } if this._ended { return this.fail(error_kind::unexpected_end, this._at); } if !this.to_next_token() || !this.stale_keys() { return false; } # A line is indented by spaces alone: within a flow collection in a block, past the # block's indentation, and where a tab follows them, past that too. val begins := this._scanned != this._line; this._scanned = this._line; if this._at < this._text.length && this._indent >= 0 && begins { val spaces := this.leading_spaces() as i64; if this._flow > 0 && spaces <= this._indent { return this.fail(error_kind::wrong_indent, this._at); } if this._flow == 0 && this._tabbed && spaces <= this._indent { return this.fail(error_kind::tab_indent, this._at); } } this.unroll(this._column as i64); if this._at >= this._text.length { return this.end_stream(); } val c := this.byte(0); val next := this.byte(1); if this._column == 0 { if c == '%' { return this.fetch_directive(); } if this.marker('-') { return this.fetch_marker(token_kind::document_start); } if this.marker('.') { return this.fetch_marker(token_kind::document_end); } } val flow := this._flow > 0; if c == '[' { return this.fetch_flow_start(token_kind::flow_sequence_start); } if c == '{' { return this.fetch_flow_start(token_kind::flow_mapping_start); } if c == ']' { return this.fetch_flow_end(token_kind::flow_sequence_end); } if c == '}' { return this.fetch_flow_end(token_kind::flow_mapping_end); } if c == ',' { return this.fetch_flow_entry(); } if c == '-' && (is_blankz(next) || (flow && is_flow_indicator(next))) { return this.fetch_block_entry(); } if c == '?' && (is_blankz(next) || (flow && is_flow_indicator(next))) { return this.fetch_key(); } if c == ':' && (is_blankz(next) || (flow && (is_flow_indicator(next) || this._json))) { return this.fetch_value(); } if c == '*' { return this.fetch_anchor(token_kind::alias); } if c == '&' { return this.fetch_anchor(token_kind::anchor); } if c == '!' { return this.fetch_tag(); } if c == '|' && !flow { return this.fetch_block_scalar(style::literal); } if c == '>' && !flow { return this.fetch_block_scalar(style::folded); } if c == '\'' { return this.fetch_flow_scalar(style::single); } if c == '"' { return this.fetch_flow_scalar(style::double); } if this.plain_begins() { return this.fetch_plain_scalar(); } if c == '\t' { return this.fail(error_kind::tab_indent, this._at); } return this.fail(error_kind::unexpected_byte, this._at); } # The byte some bytes past the next, or 0 past the end, which the text holds none of. def scanner::byte(&this, ahead: u64): u8 { val at := this._at + ahead; return at < this._text.length ? this._text[at] : 0; } # Moves past bytes of one line. def scanner::skip(&mut this, count: u64): void { this._at += count; this._column += count; } # Moves past a line break, `\r\n` being one. def scanner::skip_break(&mut this): void { if this.byte(0) == '\r' && this.byte(1) == '\n' { this._at += 2; } else { this._at += 1; } this._line++; this._column = 0; this._begun = this._at; } # Where a comment from a place ends: at the line break or zero byte ending it, # or the end of the text, found eight bytes at a time, the last few read without # passing the end. def comment_end(text: strview, from: u64): u64 { val data := text.data; var at := from; while at + 8 <= text.length { val ends := comment_ends(swar::load(data + at)); return at + ends.first() if ends.any(); at += 8; } if at < text.length { val ends: swar::mask8 = comment_ends(swar::load_last(data + at, text.length - at)) & swar::mask8::leading(text.length - at); return at + ends.first() if ends.any(); } return text.length; } # The bytes of a word that end a comment. def comment_ends(word: swar::bytes8): swar::mask8 { return word.equal('\n') | word.equal('\r') | word.zero(); } # Appends a line break as `\n`, and moves past it. def scanner::take_break(&mut this, into: &mut string): void { into.push('\n'); this.skip_break(); } # How many spaces begin the line, before anything else. def scanner::leading_spaces(&this): u64 { var at := this._begun; while at < this._at && this._text[at] == ' ' { at++; } return at - this._begun; } # Whether a document marker, `---` or `...`, begins a line here. def scanner::marker(&this, of: u8): bool { return this._column == 0 && this.byte(0) == of && this.byte(1) == of && this.byte(2) == of && is_blankz(this.byte(3)); } # Whether a byte order mark is next. def scanner::at_order_mark(&this): bool { return this.byte(0) == 0xEF && this.byte(1) == 0xBB && this.byte(2) == 0xBF; } # Moves past blanks, comments, and line breaks to where the next token begins. A tab # may not indent a line of a block, but may stand on a line holding nothing else. def scanner::to_next_token(&mut this): bool { while true { if this._column == 0 && this.at_order_mark() { this._at += 3; this._begun = this._at; } var spaced := this._at == this._begun || this._at == 0 || is_blank(this._text[this._at - 1]) || is_break(this._text[this._at - 1]); this._tabbed = false; while is_blank(this.byte(0)) { if this.byte(0) == '\t' { this._tabbed = true; } this.skip(1); spaced = true; } if this.byte(0) == '#' { if !spaced { return this.fail(error_kind::unexpected_byte, this._at); } this.skip(comment_end(this._text, this._at) - this._at); } if is_break(this.byte(0)) { this.skip_break(); if this._flow == 0 { this._allowed = true; } continue; } break; } return true; } # Whether the rest of the line is blanks, or blanks and a comment. def scanner::rest_is_blank(&this): bool { var ahead: u64 = 0; while is_blank(this.byte(ahead)) { ahead++; } return is_breakz(this.byte(ahead)) || this.byte(ahead) == '#'; } # Whether a plain scalar begins here: no indicator but `-`, `?`, or `:` before what is # plain, which is anything but a blank, or a flow indicator within flow. def scanner::plain_begins(&this): bool { val c := this.byte(0); if is_blankz(c) { return false; } val next := this.byte(1); match c { '-', '?', ':' => return !is_blankz(next) && !(this._flow > 0 && is_flow_indicator(next)); ',', '[', ']', '{', '}', '#', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`' => return false; else => return true; } return true; } # Implicit keys that can no longer be keys, since the line has ended or more than # 1,024 characters have passed, are no longer possible: one that had to be a key # fails. Within a flow mapping, a key's `:` may follow on a later line. def scanner::stale_keys(&mut this): bool { for index in 0..this._keys.length { val key := &mut this._keys.at(index); if !key.possible { continue; } if index > 0 && this._mappings.at(index - 1) { continue; } if key.line < this._line || this.characters_since(key.at) > 1024 { if key.required { return this.fail(error_kind::missing_colon, key.at); } key.possible = false; } } return true; } # How many characters lie between a byte and the next, counting each UTF-8 sequence # once, and stopping early where there are more than 1,024. def scanner::characters_since(&this, start: u64): u64 { if this._at - start <= 1024 { return this._at - start; } var count: u64 = 0; for at in start..this._at { if (this._text[at] & 0xC0) != 0x80 { count++; if count > 1024 { return count; } } } return count; } # Notes that an implicit key may begin here, where one may. def scanner::save_key(&mut this): bool { if !this._allowed { return true; } val made := possible_key { possible = true, required = this._flow == 0 && this._indent == this._column as i64, tabbed = this._tabbed, number = this._taken + (this._tokens.length - this._head), at = this._at, line = this._line, column = this._column }; if !this.remove_key() { return false; } this._keys.set(this._keys.length - 1, made); return true; } # Notes that no implicit key begins where one might have, which fails where one had # to. def scanner::remove_key(&mut this): bool { val key := &mut this._keys.at(this._keys.length - 1); if key.possible && key.required { return this.fail(error_kind::missing_colon, key.at); } key.possible = false; return true; } # Begins a block collection where its first entry is further in than the innermost, # placing its token before the token given, or last. def scanner::roll(&mut this, column: u64, number: i64, kind: token_kind, at: u64): void { if this._flow > 0 || this._indent >= column as i64 { return; } this._indents.append(this._indent); this._indent = column as i64; val made := token { kind = kind, start = at, end = at }; if number < 0 { this._tokens.append(made); return; } this._tokens.insert_at(this._head + (number as u64 - this._taken), made); } # Ends each block collection further in than a column. def scanner::unroll(&mut this, column: i64): void { if this._flow > 0 { return; } while this._indent > column { this._tokens.append(token { kind = token_kind::block_end, start = this._at, end = this._at }); this._indent = this._indents.at(this._indents.length - 1); this._indents.remove_at(this._indents.length - 1); } } # Appends a token of a kind from a byte to the next. def scanner::add(&mut this, kind: token_kind, start: u64): void { this._tokens.append(token { kind = kind, start = start, end = this._at }); } def scanner::start_stream(&mut this): bool { this._started = true; this._indent = -1; this._allowed = true; this._keys.append(possible_key {}); this.add(token_kind::stream_start, 0); return true; } def scanner::end_stream(&mut this): bool { if this._column != 0 { this._column = 0; this._line++; } this.unroll(-1); if !this.remove_key() { return false; } this._allowed = false; this._ended = true; this.add(token_kind::stream_end, this._at); return true; } # Scans a directive: `%YAML` with its version, `%TAG` with a handle and a prefix, or # one YAML reserves, which is read past. def scanner::fetch_directive(&mut this): bool { this.unroll(-1); if !this.remove_key() { return false; } this._allowed = false; this._json = false; val start := this._at; this.skip(1); val named := this._at; while !is_blankz(this.byte(0)) { this.skip(1); } val name := this._text.substr(named, this._at - named); if name.length == 0 { return this.fail(error_kind::invalid_directive, start); } if name == "YAML" { if !this.skip_blanks() { return this.fail(error_kind::invalid_directive, this._at); } val major := this._at; while this.byte(0).is_digit { this.skip(1); } val majors := this._at - major; if majors == 0 || this.byte(0) != '.' { return this.fail(error_kind::invalid_directive, this._at); } this.skip(1); val minor := this._at; while this.byte(0).is_digit { this.skip(1); } if this._at == minor { return this.fail(error_kind::invalid_directive, this._at); } this._tokens.append(token { kind = token_kind::version_directive, start = start, end = this._at, first = major, length = majors, second = minor, extent = this._at - minor }); return this.end_directive(); } if name == "TAG" { if !this.skip_blanks() { return this.fail(error_kind::invalid_directive, this._at); } val handle := this._at; if !this.scan_handle(true) { return this.fail(error_kind::invalid_directive, handle); } val handles := this._at - handle; if !this.skip_blanks() { return this.fail(error_kind::invalid_directive, this._at); } val prefix := this._at; while is_uri_char(this.byte(0)) || this.byte(0) == '%' { if this.byte(0) == '%' && (!this.byte(1).is_hex || !this.byte(2).is_hex) { return this.fail(error_kind::invalid_directive, this._at); } this.skip(1); } if this._at == prefix { return this.fail(error_kind::invalid_directive, this._at); } this._tokens.append(token { kind = token_kind::tag_directive, start = start, end = this._at, first = handle, length = handles, second = prefix, extent = this._at - prefix }); return this.end_directive(); } # A directive YAML reserves is read past, with what it holds. while !is_breakz(this.byte(0)) { if this.byte(0) == '#' && is_blank(this._text[this._at - 1]) { break; } this.skip(1); } return true; } # Moves past at least one blank, giving whether there was one. def scanner::skip_blanks(&mut this): bool { if !is_blank(this.byte(0)) { return false; } while is_blank(this.byte(0)) { this.skip(1); } return true; } # Checks that only blanks and a comment follow a directive on its line. def scanner::end_directive(&mut this): bool { val spaced := is_blank(this.byte(0)); while is_blank(this.byte(0)) { this.skip(1); } if this.byte(0) == '#' && spaced { return true; } if !is_breakz(this.byte(0)) { return this.fail(error_kind::invalid_directive, this._at); } return true; } # Scans a tag handle: `!`, `!!`, or `!name!`. A directive's must be one of those; a # tag's handle is `!` alone where no second `!` ends its name. def scanner::scan_handle(&mut this, directive: bool): bool { if this.byte(0) != '!' { return false; } var ahead: u64 = 1; while is_word(this.byte(ahead)) { ahead++; } if this.byte(ahead) == '!' { this.skip(ahead + 1); return true; } if directive && ahead > 1 { return false; } this.skip(1); return true; } def scanner::fetch_marker(&mut this, kind: token_kind): bool { this.unroll(-1); if !this.remove_key() { return false; } this._allowed = false; this._json = false; val start := this._at; this.skip(3); this.add(kind, start); # Only a comment follows `...` on its line. if kind == token_kind::document_end { while is_blank(this.byte(0)) { this.skip(1); } if !is_breakz(this.byte(0)) && this.byte(0) != '#' { return this.fail(error_kind::unexpected_byte, this._at); } } return true; } def scanner::fetch_flow_start(&mut this, kind: token_kind): bool { if !this.save_key() { return false; } this._keys.append(possible_key {}); this._mappings.append(kind == token_kind::flow_mapping_start); this._flow++; this._allowed = true; this._json = false; val start := this._at; this.skip(1); this.add(kind, start); return true; } def scanner::fetch_flow_end(&mut this, kind: token_kind): bool { if !this.remove_key() { return false; } if this._flow > 0 { this._flow--; this._keys.remove_at(this._keys.length - 1); this._mappings.remove_at(this._mappings.length - 1); } this._allowed = false; this._json = true; val start := this._at; this.skip(1); this.add(kind, start); return true; } def scanner::fetch_flow_entry(&mut this): bool { if !this.remove_key() { return false; } this._allowed = true; this._json = false; val start := this._at; this.skip(1); this.add(token_kind::flow_entry, start); return true; } def scanner::fetch_block_entry(&mut this): bool { if this._flow == 0 { if this._tabbed { return this.fail(error_kind::tab_indent, this._at); } if !this._allowed { return this.fail(error_kind::unexpected_byte, this._at); } this.roll(this._column, -1, token_kind::block_sequence_start, this._at); } if !this.remove_key() { return false; } this._allowed = true; this._json = false; val start := this._at; this.skip(1); this.add(token_kind::block_entry, start); return true; } def scanner::fetch_key(&mut this): bool { if this._flow == 0 { if this._tabbed { return this.fail(error_kind::tab_indent, this._at); } if !this._allowed { return this.fail(error_kind::unexpected_byte, this._at); } this.roll(this._column, -1, token_kind::block_mapping_start, this._at); } if !this.remove_key() { return false; } this._allowed = this._flow == 0; this._json = false; val start := this._at; this.skip(1); this.add(token_kind::key, start); return true; } def scanner::fetch_value(&mut this): bool { val key := &mut this._keys.at(this._keys.length - 1); if key.possible { # A key in a block is indented by spaces alone. if this._flow == 0 && key.tabbed { return this.fail(error_kind::tab_indent, key.at); } # What began where the key could have is a key, so the tokens held back from # there are told so, and a block mapping begins where it is further in. this._tokens.insert_at(this._head + (key.number - this._taken), token { kind = token_kind::key, start = key.at, end = key.at }); this.roll(key.column, key.number as i64, token_kind::block_mapping_start, key.at); key.possible = false; this._allowed = false; } else { if this._flow == 0 { if !this._allowed { return this.fail(error_kind::unexpected_byte, this._at); } this.roll(this._column, -1, token_kind::block_mapping_start, this._at); } this._allowed = this._flow == 0; } this._json = false; val start := this._at; this.skip(1); this.add(token_kind::value, start); return true; } # Scans an alias, `*name`, or an anchor, `&name`, whose name runs to a blank or a # flow indicator. def scanner::fetch_anchor(&mut this, kind: token_kind): bool { if !this.save_key() { return false; } this._allowed = false; this._json = false; val start := this._at; this.skip(1); val name := this._at; while !is_blankz(this.byte(0)) && !is_flow_indicator(this.byte(0)) { this.skip(1); } if this._at == name { return this.fail(error_kind::unexpected_byte, start); } this._tokens.append(token { kind = kind, start = start, end = this._at, first = name, length = this._at - name }); return true; } # Scans a tag: `!`, `!!suffix`, `!handle!suffix`, `!suffix`, or `!` alone, # which is the non-specific tag. def scanner::fetch_tag(&mut this): bool { if !this.save_key() { return false; } this._allowed = false; this._json = false; val start := this._at; var handle: u64 = 0; var handles: u64 = 0; var suffix: u64 = 0; if this.byte(1) == '<' { this.skip(2); suffix = this._at; while is_uri_char(this.byte(0)) || this.byte(0) == '%' { if !this.skip_uri_char() { return false; } } if this.byte(0) != '>' || this._at == suffix { return this.fail(error_kind::unexpected_byte, this._at); } val suffixes := this._at - suffix; this.skip(1); this._tokens.append(token { kind = token_kind::tag, start = start, end = this._at, second = suffix, extent = suffixes }); return this.end_tag(); } handle = this._at; this.scan_handle(false); handles = this._at - handle; suffix = this._at; while is_tag_char(this.byte(0)) || this.byte(0) == '%' { if !this.skip_uri_char() { return false; } } # Only `!` stands alone: a named handle needs a suffix. if this._at == suffix && handles > 1 { return this.fail(error_kind::unexpected_byte, this._at); } this._tokens.append(token { kind = token_kind::tag, start = start, end = this._at, first = handle, length = handles, second = suffix, extent = this._at - suffix }); return this.end_tag(); } # Moves past a character of a URI, checking a `%` escape's digits. def scanner::skip_uri_char(&mut this): bool { if this.byte(0) == '%' && (!this.byte(1).is_hex || !this.byte(2).is_hex) { return this.fail(error_kind::unexpected_byte, this._at); } this.skip(this.byte(0) == '%' ? 3 : 1); return true; } # Checks that a blank, the end of the line, or within flow a flow indicator, follows # a tag. def scanner::end_tag(&mut this): bool { val after := this.byte(0); if is_blankz(after) || (this._flow > 0 && is_flow_indicator(after)) { return true; } return this.fail(error_kind::unexpected_byte, this._at); } def scanner::fetch_block_scalar(&mut this, kind: style): bool { if !this.remove_key() { return false; } this._allowed = true; this._json = false; return this.scan_block_scalar(kind); } def scanner::fetch_flow_scalar(&mut this, kind: style): bool { if !this.save_key() { return false; } this._allowed = false; if !this.scan_flow_scalar(kind) { return false; } this._json = true; return true; } def scanner::fetch_plain_scalar(&mut this): bool { if !this.save_key() { return false; } this._allowed = false; this._json = false; return this.scan_plain_scalar(); } # Scans a literal or folded block scalar: its header, then its lines, from which its # indentation is taken away, folded where it is folded, and chomped as its header says. def scanner::scan_block_scalar(&mut this, kind: style): bool { val start := this._at; this.skip(1); # The header: a chomping indicator and an indentation indicator, in either order. var chomping: i64 = 0; # -1 strips the final line breaks, 1 keeps them, and 0 keeps one. var increment: u64 = 0; for _ in 0..2 { val c := this.byte(0); if (c == '+' || c == '-') && chomping == 0 { chomping = c == '+' ? 1 : -1; this.skip(1); } else if c.is_digit && increment == 0 { if c == '0' { return this.fail(error_kind::unexpected_byte, this._at); } increment = c.digit_value; this.skip(1); } } val spaced := is_blank(this.byte(0)); while is_blank(this.byte(0)) { this.skip(1); } if this.byte(0) == '#' { if !spaced { return this.fail(error_kind::unexpected_byte, this._at); } while !is_breakz(this.byte(0)) { this.skip(1); } } if !is_breakz(this.byte(0)) { return this.fail(error_kind::unexpected_byte, this._at); } # A header the text ends after leaves the scalar empty. if this._at >= this._text.length { this._tokens.append(token { kind = token_kind::scalar, style = kind, decoded = true, start = start, end = this._at, first = this._buffer.length }); return true; } this.skip_break(); # The content is indented past the block the scalar is in, by the indicator where # one is given. val parent: i64 = this._indent; var indent: u64 = 0; var known := increment > 0; if known { indent = parent >= 0 ? parent as u64 + increment : increment - 1; } val first := this._buffer.length; var leading := string::new(nothing {}); # The line break that ended the last line. var trailing := string::new(nothing {}); # The empty lines after it. var leading_blank := false; if !this.block_breaks(&mut indent, &mut known, parent, &mut trailing) { return false; } while this._column == indent && this._at < this._text.length && !this.marker('-') && !this.marker('.') { # A folded scalar joins a line to the last with a space, where neither is more # indented and no empty line lies between them. val trailing_blank := is_blank(this.byte(0)); if kind == style::folded && leading.length > 0 && !leading_blank && !trailing_blank { if trailing.length == 0 { this._buffer.push(' '); } leading.clear(); } else { this._buffer.append(leading.view()); leading.clear(); } this._buffer.append(trailing.view()); trailing.clear(); leading_blank = is_blank(this.byte(0)); val line := this._at; while !is_breakz(this.byte(0)) { this._at++; } this._column += this._at - line; this._buffer.append(this._text.substr(line, this._at - line)); if this._at >= this._text.length { # A last line of spaces ends as though a line break followed it. var spaces := true; for at in line..this._at { spaces = spaces && this._text[at] == ' '; } if spaces { leading.push('\n'); } break; } this.take_break(&mut leading); if !this.block_breaks(&mut indent, &mut known, parent, &mut trailing) { return false; } } # Chomping: `-` strips every final line break, the default keeps one, and `+` # keeps them all. if chomping != -1 { this._buffer.append(leading.view()); } if chomping == 1 { this._buffer.append(trailing.view()); } this._tokens.append(token { kind = token_kind::scalar, style = kind, decoded = true, start = start, end = this._at, first = first, length = this._buffer.length - first }); return true; } # Moves past the indentation of a block scalar's lines, and the empty lines among # them, keeping their line breaks. Where the indentation is not yet known, it is that # of the first line holding anything, which no empty line before it may pass. def scanner::block_breaks(&mut this, indent: &mut u64, known: &mut bool, parent: i64, trailing: &mut string): bool { # A scalar at the top of a document may begin its lines at their start. val least: u64 = parent >= 0 ? parent as u64 + 1 : 0; var widest: u64 = 0; var widest_at: u64 = 0; while true { while (!known || this._column < indent) && this.byte(0) == ' ' { this.skip(1); } if this._column > widest { widest = this._column; widest_at = this._at; } # A tab past the indentation is the line's first character, but within it is no # indentation at all. if this.byte(0) == '\t' && this._column < (known ? indent : least) { return this.fail(error_kind::tab_indent, this._at); } # A last line of spaces, without a line break after it, is still an empty line. if this._at >= this._text.length && this._column > 0 { trailing.push('\n'); break; } if !is_break(this.byte(0)) { break; } this.take_break(trailing); } if !known { var found := this._column; if is_breakz(this.byte(0)) || this.marker('-') || this.marker('.') { found = widest; } if found < least { found = least; } if widest > found { return this.fail(error_kind::wrong_indent, widest_at); } indent = found; known = true; } return true; } # Scans a single- or double-quoted scalar, decoding its escapes and folding its lines. def scanner::scan_flow_scalar(&mut this, kind: style): bool { val start := this._at; val double := kind == style::double; val quote: u8 = double ? '"' : '\''; this.skip(1); # The text is viewed where it is, until an escape or a folded line makes it differ, # and is then decoded into the buffer. val from := this._at; var decoded := false; val first := this._buffer.length; var whitespace := string::new(nothing {}); var leading := string::new(nothing {}); var trailing := string::new(nothing {}); while true { if this._column == 0 && (this.marker('-') || this.marker('.')) { return this.fail(error_kind::unexpected_byte, this._at); } if this._at >= this._text.length { return this.fail(error_kind::unexpected_end, start); } var leading_blanks := false; while !is_blankz(this.byte(0)) { val c := this.byte(0); if !double && c == '\'' && this.byte(1) == '\'' { if !decoded { this._buffer.append(this._text.substr(from, this._at - from)); decoded = true; } this._buffer.push('\''); this.skip(2); continue; } if c == quote { break; } if double && c == '\\' && is_break(this.byte(1)) { if !decoded { this._buffer.append(this._text.substr(from, this._at - from)); decoded = true; } this.skip(1); this.skip_break(); leading_blanks = true; break; } if double && c == '\\' { if !decoded { this._buffer.append(this._text.substr(from, this._at - from)); decoded = true; } if !this.escape() { return false; } continue; } if decoded { this._buffer.push(c); } this.skip(1); } if this.byte(0) == quote { break; } # The blanks and line breaks before what follows. var blanks_from := this._at; while is_blank(this.byte(0)) || is_break(this.byte(0)) { if is_blank(this.byte(0)) { if !leading_blanks { whitespace.push(this.byte(0)); } this.skip(1); continue; } if !leading_blanks { whitespace.clear(); this.take_break(&mut leading); leading_blanks = true; } else { this.take_break(&mut trailing); } # A line of a quoted scalar within a block goes on past the block's indentation. while this.byte(0) == ' ' { this.skip(1); } if this._flow == 0 && this._column as i64 <= this._indent && !is_breakz(this.byte(0)) && !this.marker('-') && !this.marker('.') { return this.fail(error_kind::wrong_indent, this._at); } } # Blanks between words on one line are kept as they are, which text viewed where # it is already holds. if !leading_blanks { if decoded { this._buffer.append(whitespace.view()); } whitespace.clear(); continue; } if !decoded { this._buffer.append(this._text.substr(from, blanks_from - from)); decoded = true; } if leading.length > 0 && trailing.length == 0 { this._buffer.push(' '); } else { this._buffer.append(trailing.view()); } leading.clear(); trailing.clear(); whitespace.clear(); } val end := this._at; this.skip(1); if decoded { this._tokens.append(token { kind = token_kind::scalar, style = kind, decoded = true, start = start, end = this._at, first = first, length = this._buffer.length - first }); return true; } this._tokens.append(token { kind = token_kind::scalar, style = kind, start = start, end = this._at, first = from, length = end - from }); return true; } # Decodes an escape of a double-quoted scalar into the buffer. def scanner::escape(&mut this): bool { val at := this._at; val marker := this.byte(1); var digits: u64 = 0; match marker { '0' => this._buffer.push(0); 'a' => this._buffer.push(0x07); 'b' => this._buffer.push(0x08); 't' => this._buffer.push('\t'); '\t' => this._buffer.push('\t'); 'n' => this._buffer.push('\n'); 'v' => this._buffer.push(0x0B); 'f' => this._buffer.push(0x0C); 'r' => this._buffer.push('\r'); 'e' => this._buffer.push(0x1B); ' ' => this._buffer.push(' '); '"' => this._buffer.push('"'); '/' => this._buffer.push('/'); '\\' => this._buffer.push('\\'); 'N' => serial::append_utf8(0x85, &mut this._buffer); '_' => serial::append_utf8(0xA0, &mut this._buffer); 'L' => serial::append_utf8(0x2028, &mut this._buffer); 'P' => serial::append_utf8(0x2029, &mut this._buffer); 'x' => digits = 2; 'u' => digits = 4; 'U' => digits = 8; else => return this.fail(error_kind::invalid_escape, at); } this.skip(2); if digits == 0 { return true; } var code: u64 = 0; for index in 0..digits { val c := this.byte(index); if !c.is_hex { return this.fail(error_kind::invalid_escape, at); } code = code * 16 + c.digit_value; } if (code >= 0xD800 && code <= 0xDFFF) || code > 0x10FFFF { return this.fail(error_kind::invalid_escape, at); } serial::append_utf8(code, &mut this._buffer); this.skip(digits); return true; } # Scans a plain scalar, which ends at `: `, ` #`, a flow indicator within flow, a line # indented no further than the block it is in, or a document marker, and whose lines # are folded. def scanner::scan_plain_scalar(&mut this): bool { val start := this._at; val indent: i64 = this._indent + 1; val flow := this._flow > 0; var end := this._at; var decoded := false; val first := this._buffer.length; var leading_blanks := false; var whitespace := string::new(nothing {}); var leading := string::new(nothing {}); var trailing := string::new(nothing {}); while true { if this._column == 0 && (this.marker('-') || this.marker('.')) { break; } if this.byte(0) == '#' { break; } # The word runs to a blank, or to what ends the scalar. val word := this._at; val text := this._text; var at := word; while at < text.length { val c := text[at]; if c == ' ' || c == '\t' || c == '\n' || c == '\r' { break; } if c == ':' { val after: u8 = at + 1 < text.length ? text[at + 1] : 0; if is_blankz(after) || (flow && is_flow_indicator(after)) { break; } } if flow && is_flow_indicator(c) { break; } at++; } this._column += at - word; this._at = at; if this._at == word { break; } # What lay between this word and the last joins them: the blanks as they are on # one line, and a line break folded. if leading_blanks { if !decoded { this._buffer.append(this._text.substr(start, end - start)); decoded = true; } if leading.length > 0 && trailing.length == 0 { this._buffer.push(' '); } else { this._buffer.append(trailing.view()); } leading.clear(); trailing.clear(); leading_blanks = false; } else if decoded { this._buffer.append(whitespace.view()); } whitespace.clear(); if decoded { this._buffer.append(this._text.substr(word, this._at - word)); } end = this._at; if !is_blank(this.byte(0)) && !is_break(this.byte(0)) { break; } while is_blank(this.byte(0)) || is_break(this.byte(0)) { if is_blank(this.byte(0)) { if leading_blanks && (this._column as i64) < indent && this.byte(0) == '\t' { if !this.rest_is_blank() { return this.fail(error_kind::tab_indent, this._at); } } if !leading_blanks { whitespace.push(this.byte(0)); } this.skip(1); continue; } if !leading_blanks { whitespace.clear(); this.take_break(&mut leading); leading_blanks = true; } else { this.take_break(&mut trailing); } } if !flow && (this._column as i64) < indent { break; } } if decoded { this._tokens.append(token { kind = token_kind::scalar, style = style::plain, decoded = true, start = start, end = end, first = first, length = this._buffer.length - first }); } else { this._tokens.append(token { kind = token_kind::scalar, style = style::plain, start = start, end = end, first = start, length = end - start }); } if leading_blanks { this._allowed = true; } return true; }