libyaml makes every ':' in a flow collection a value indicator, so it mis-scans ':x', 'http://foo.com', '::vector', and 'omitted value:,', and drops the value after an adjacent quoted or collection key. Per the spec a ':' only ends a node when followed by whitespace, end of input, or a flow indicator, except an adjacent ':' after a JSON key (a quoted flow scalar or a flow collection used as the key). Gate the ':' value-indicator dispatch on that rule, tracking on the pending simple key whether it is a JSON node (set for quoted flow scalars and flow-collection starts). End the plain scalar at a ':' before a flow indicator instead of rejecting it as unexpected. Surpasses libyaml on 4ABK, 58MP, 5T43, DBG4, FRK4, HM87/00, with 9MMW (mixing adjacent and separated keys) still correct. Port score 292 -> 298.
140 lines
5.6 KiB
Swift
140 lines
5.6 KiB
Swift
extension PureYAML.Parsing.LibYAML.Scanner {
|
|
private typealias Classify = PureYAML.Parsing.LibYAML.CharacterClass
|
|
|
|
/// The folding state carried through `scan_plain_scalar`: the accumulated
|
|
/// value plus the pending leading break, trailing breaks, and whitespace that
|
|
/// are folded in before the next content character.
|
|
private struct PlainState {
|
|
var string = ""
|
|
var leadingBreak = ""
|
|
var trailingBreaks = ""
|
|
var whitespaces = ""
|
|
var leadingBlanks = false
|
|
var endMark: PureYAML.Parsing.Mark
|
|
}
|
|
|
|
/// Port of `yaml_parser_scan_plain_scalar`. Consumes a plain scalar, folding
|
|
/// line breaks: a single break between non-empty lines becomes a space, blank
|
|
/// lines become literal newlines, and leading/trailing whitespace on folded
|
|
/// lines is dropped. Ends at a `: ` value indicator, a comment, a less-indented
|
|
/// line (block context), a flow indicator (flow context), or a document marker.
|
|
mutating func scanPlainScalar() throws -> PureYAML.Parsing.LibYAML.Token {
|
|
let indent = indentEngine.indent + 1
|
|
let startMark = reader.mark
|
|
var state = PlainState(endMark: reader.mark)
|
|
|
|
while true {
|
|
if reader.column == 0, documentMarkerAhead() {
|
|
break
|
|
}
|
|
if reader.peek() == "#" {
|
|
break
|
|
}
|
|
try consumePlainContent(&state, startMark: startMark)
|
|
guard Classify.isBlank(reader.peek()) || Classify.isBreak(reader.peek()) else {
|
|
break
|
|
}
|
|
try consumePlainBreaks(&state, indent: indent, startMark: startMark)
|
|
if flowLevel == 0, reader.column < indent {
|
|
break
|
|
}
|
|
}
|
|
|
|
if state.leadingBlanks {
|
|
simpleKeyAllowed = true
|
|
}
|
|
return PureYAML.Parsing.LibYAML.Token(
|
|
type: .scalar(value: state.string, style: .plain),
|
|
startMark: startMark,
|
|
endMark: state.endMark,
|
|
)
|
|
}
|
|
|
|
/// Consume the run of non-blank characters, folding any pending breaks or
|
|
/// whitespace ahead of each one.
|
|
private mutating func consumePlainContent(_ state: inout PlainState, startMark _: PureYAML.Parsing.Mark) throws {
|
|
while let character = reader.peek(), !Classify.isBlankZ(character) {
|
|
if character == ":", Classify.isBlankZ(reader.peek(1)) {
|
|
break
|
|
}
|
|
if flowLevel > 0, character == ":", Classify.isFlowIndicator(reader.peek(1)) {
|
|
// A `:` before a flow indicator ends the key; the value is empty.
|
|
// libyaml rejects this as an unexpected ':', but it is a valid
|
|
// value indicator in flow context.
|
|
break
|
|
}
|
|
if flowLevel > 0, Classify.isFlowIndicator(character) {
|
|
break
|
|
}
|
|
foldPending(&state)
|
|
state.string.unicodeScalars.append(character)
|
|
reader.skip()
|
|
state.endMark = reader.mark
|
|
}
|
|
}
|
|
|
|
/// Fold the pending leading break, trailing breaks, and whitespace into the
|
|
/// accumulated value (the join logic from libyaml's inner loop).
|
|
private func foldPending(_ state: inout PlainState) {
|
|
guard state.leadingBlanks || !state.whitespaces.isEmpty else {
|
|
return
|
|
}
|
|
guard state.leadingBlanks else {
|
|
state.string += state.whitespaces
|
|
state.whitespaces = ""
|
|
return
|
|
}
|
|
if state.leadingBreak.first == "\n" {
|
|
if state.trailingBreaks.isEmpty {
|
|
state.string += " "
|
|
} else {
|
|
state.string += state.trailingBreaks
|
|
state.trailingBreaks = ""
|
|
}
|
|
state.leadingBreak = ""
|
|
} else {
|
|
state.string += state.leadingBreak
|
|
state.string += state.trailingBreaks
|
|
state.leadingBreak = ""
|
|
state.trailingBreaks = ""
|
|
}
|
|
state.leadingBlanks = false
|
|
}
|
|
|
|
/// Consume the run of blanks and line breaks between content, recording them as
|
|
/// pending whitespace or folded breaks.
|
|
private mutating func consumePlainBreaks(_ state: inout PlainState, indent: Int, startMark: PureYAML.Parsing.Mark) throws {
|
|
while Classify.isBlank(reader.peek()) || Classify.isBreak(reader.peek()) {
|
|
if Classify.isBlank(reader.peek()) {
|
|
if state.leadingBlanks, reader.column < indent, Classify.isTab(reader.peek()) {
|
|
throw scannerError("found a tab character that violates indentation", at: startMark)
|
|
}
|
|
if !state.leadingBlanks, let blank = reader.peek() {
|
|
state.whitespaces.unicodeScalars.append(blank)
|
|
}
|
|
reader.skip()
|
|
} else if !state.leadingBlanks {
|
|
state.whitespaces = ""
|
|
reader.skipLine()
|
|
state.leadingBreak = "\n"
|
|
state.leadingBlanks = true
|
|
} else {
|
|
reader.skipLine()
|
|
state.trailingBreaks += "\n"
|
|
}
|
|
}
|
|
}
|
|
|
|
private func scannerError(_ problem: String, at mark: PureYAML.Parsing.Mark) -> PureYAML.Parsing.LibYAML.ScannerError {
|
|
PureYAML.Parsing.LibYAML.ScannerError(context: "while scanning a plain scalar", problem: problem, mark: mark)
|
|
}
|
|
|
|
private func documentMarkerAhead() -> Bool {
|
|
let first = reader.peek()
|
|
guard first == "-" || first == "." else {
|
|
return false
|
|
}
|
|
return reader.peek(1) == first && reader.peek(2) == first && Classify.isBlankZ(reader.peek(3))
|
|
}
|
|
}
|