PureYAML/Sources/Parsing/LibYAML.IndentEngine.swift
Mihaela Mihaljevic 43b20ab7bc feat(parsing): port libyaml token-fetch loop (block + plain core)
Stage 3 of the faithful libyaml scanner port: the token producer. Translates scan_to_next_token, fetch_more_tokens/fetch_next_token dispatch, and the fetchers for stream start/end, document indicators, block entry, key, value (with the simple-key splice and roll-indent), plus scan_plain_scalar with its line-break folding. Reuses the ported indent engine, simple-key mechanism, character classes, and reader. Constructs not yet ported (flow collections, quoted scalars, anchors, tags, directives, block scalars) throw an explicit 'not yet ported' error rather than mis-scan; the port is not yet wired to the public API.

Milestone: the ported scanner tokenizes '- a:' followed by a same-column 'b:' as siblings (one BLOCK-MAPPING-START), matching libyaml, fixing the original block-mapping bug by construction; a more-indented value still nests. Unit-tested for the sibling case, the nested case, a simple pair, and multi-line plain-scalar folding. 305 tests, no regression.
2026-06-24 10:37:35 +02:00

86 lines
3.7 KiB
Swift

extension PureYAML.Parsing.LibYAML {
/// Port of the indentation stack from `yaml_parser_t` plus
/// `yaml_parser_roll_indent` and `yaml_parser_unroll_indent`. libyaml tracks a
/// single `indent` column (the current block collection's indentation, -1 at
/// the document root) and a stack of the enclosing levels. A block collection
/// is opened only when content is strictly more indented than the current
/// level, which is exactly why `- a:` followed by a same-column `b:` keeps `b`
/// a sibling and leaves `a` an empty value.
struct IndentEngine {
/// `parser->indent`. -1 at the document root, matching libyaml.
private(set) var indent = -1
/// `parser->indents`, the stack of enclosing indentation levels.
private(set) var indents: [Int] = []
/// `parser->flow_level`; indentation is inert inside flow collections.
var flowLevel = 0
/// The token queue (`parser->tokens`) the engine appends to or, for
/// `roll_indent`, inserts a start token into at a recorded position.
private(set) var tokens: [Token] = []
/// `parser->tokens_parsed`, the count already dequeued, used to turn an
/// absolute token number into an index into the live queue.
var tokensParsed = 0
/// Port of `yaml_parser_roll_indent`. In flow context it does nothing.
/// When the column is greater than the current indent, it pushes the
/// current indent, adopts the new column, and emits the block start token.
/// `number == -1` appends; otherwise the token is inserted at the recorded
/// queue position so a `BLOCK-MAPPING-START` precedes a simple key that was
/// already queued.
mutating func rollIndent(
column: Int,
number: Int,
type: TokenType,
mark: PureYAML.Parsing.Mark,
) {
if flowLevel != 0 {
return
}
if indent < column {
indents.append(indent)
indent = column
let token = Token(type: type, startMark: mark, endMark: mark)
if number == -1 {
tokens.append(token)
} else {
tokens.insert(token, at: number - tokensParsed)
}
}
}
/// Port of `yaml_parser_unroll_indent`. In flow context it does nothing.
/// While the current indent is deeper than the column, it emits a
/// `BLOCK-END` and pops the enclosing level, closing every block collection
/// that the dedent ends.
mutating func unrollIndent(column: Int, mark: PureYAML.Parsing.Mark) {
if flowLevel != 0 {
return
}
while indent > column {
tokens.append(Token(type: .blockEnd, startMark: mark, endMark: mark))
indent = indents.removeLast()
}
}
/// `ENQUEUE`: append a token to the queue.
mutating func enqueue(_ token: Token) {
tokens.append(token)
}
/// `QUEUE_INSERT`: insert a token at an absolute token number, used to
/// splice a `KEY` token in front of a node already queued.
mutating func insert(_ token: Token, atTokenNumber number: Int) {
tokens.insert(token, at: number - tokensParsed)
}
/// Dequeue the front token, advancing `tokensParsed` like libyaml's
/// `yaml_parser_scan` does when it hands a token to the parser.
mutating func dequeue() -> Token? {
guard !tokens.isEmpty else {
return nil
}
tokensParsed += 1
return tokens.removeFirst()
}
}
}