diff --git a/CHANGELOG.md b/CHANGELOG.md index 73da913..84999d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Treat `?` in a flow collection as plain-scalar content unless it is followed by + whitespace, the end of input, or a flow indicator. libyaml makes every `?` in + flow context a mapping-key indicator, dropping the `?` from `?foo`, but + `ns-plain-first` lets a `?` followed by a flow-safe character begin a plain + scalar. So `{ ?foo: bar }` keys on `?foo` and `[?x]` is the scalar `?x` rather + than an empty single-pair mapping. Surpasses libyaml on 652Z and HM87/01. - Accept a tab as block-scalar content once the indentation is satisfied. The block-scalar break scanner rejected any tab seen while auto-detecting the content indentation, but a tab at or beyond the content column is content, not diff --git a/Sources/Parsing/LibYAML.CharacterClass.swift b/Sources/Parsing/LibYAML.CharacterClass.swift index 77153de..b8053a9 100644 --- a/Sources/Parsing/LibYAML.CharacterClass.swift +++ b/Sources/Parsing/LibYAML.CharacterClass.swift @@ -48,6 +48,15 @@ extension PureYAML.Parsing.LibYAML { isBlank(scalar) || isBreakZ(scalar) } + /// A flow collection indicator (`,`, `[`, `]`, `{`, `}`); the YAML + /// `c-flow-indicator` set excluded from `ns-plain-safe` in flow context. + static func isFlowIndicator(_ scalar: Unicode.Scalar?) -> Bool { + switch scalar { + case ",", "[", "]", "{", "}": true + default: false + } + } + /// `IS_DIGIT`. static func isDigit(_ scalar: Unicode.Scalar?) -> Bool { guard let scalar else { return false } diff --git a/Sources/Parsing/LibYAML.Scanner.Fetch.swift b/Sources/Parsing/LibYAML.Scanner.Fetch.swift index 86724cf..58f3567 100644 --- a/Sources/Parsing/LibYAML.Scanner.Fetch.swift +++ b/Sources/Parsing/LibYAML.Scanner.Fetch.swift @@ -106,7 +106,10 @@ extension PureYAML.Parsing.LibYAML.Scanner { switch character { case "-" where Classify.isBlankZ(reader.peek(1)): fetchBlockEntry() - case "?" where flowLevel > 0 || Classify.isBlankZ(reader.peek(1)): + case "?" where Classify.isBlankZ(reader.peek(1)) || (flowLevel > 0 && Classify.isFlowIndicator(reader.peek(1))): + // In flow context libyaml treats every `?` as a key indicator, but per + // `ns-plain-first` a `?` followed by a flow-safe character (anything but + // whitespace, end, or a flow indicator) begins a plain scalar (`?foo`). fetchKey() case ":" where flowLevel > 0 || Classify.isBlankZ(reader.peek(1)): try fetchValue() diff --git a/Sources/Parsing/LibYAML.Scanner.PlainScalar.swift b/Sources/Parsing/LibYAML.Scanner.PlainScalar.swift index 0da6474..d6be040 100644 --- a/Sources/Parsing/LibYAML.Scanner.PlainScalar.swift +++ b/Sources/Parsing/LibYAML.Scanner.PlainScalar.swift @@ -57,10 +57,10 @@ extension PureYAML.Parsing.LibYAML.Scanner { if character == ":", Classify.isBlankZ(reader.peek(1)) { break } - if flowLevel > 0, character == ":", isFlowIndicator(reader.peek(1)) { + if flowLevel > 0, character == ":", Classify.isFlowIndicator(reader.peek(1)) { throw scannerError("found unexpected ':'", at: startMark) } - if flowLevel > 0, isFlowIndicator(character) { + if flowLevel > 0, Classify.isFlowIndicator(character) { break } foldPending(&state) @@ -133,11 +133,4 @@ extension PureYAML.Parsing.LibYAML.Scanner { } return reader.peek(1) == first && reader.peek(2) == first && Classify.isBlankZ(reader.peek(3)) } - - private func isFlowIndicator(_ scalar: Unicode.Scalar?) -> Bool { - switch scalar { - case ",", "[", "]", "{", "}": true - default: false - } - } } diff --git a/Tests/CanonicalEventConformanceTests.swift b/Tests/CanonicalEventConformanceTests.swift index 81bbb6c..059bfdb 100644 --- a/Tests/CanonicalEventConformanceTests.swift +++ b/Tests/CanonicalEventConformanceTests.swift @@ -12,7 +12,7 @@ struct CanonicalEventConformanceTests { /// Valid suite cases whose event stream PureYAML does NOT yet emit exactly. /// This is the authoritative gap set: a fix removes an id (and the test fails /// until it is removed), a regression adds one (and the test fails). Of these, - /// 14 currently throw on valid input and 4 emit a wrong event stream. + /// 14 currently throw on valid input and 2 emit a wrong event stream. static let knownEventGaps: Set = [ // Gap set for the ported libyaml engine. Verified against the libyaml // reference dumper: on all but four of these the port matches libyaml @@ -28,7 +28,7 @@ struct CanonicalEventConformanceTests { "Y79Y/010", // wrong event stream: the port matches libyaml but both deviate from the // canonical events (libyaml quirks to be surpassed). - "652Z", "HM87/01", "JEF9/02", "W4TN", + "JEF9/02", "W4TN", ] @Test("Every valid case emits canonical events, except the recorded gaps")