fix(parsing): treat flow '?' before a non-indicator as scalar content

libyaml makes every '?' in flow context a mapping-key indicator, so it
parses '{ ?foo: bar }' as a key 'foo' (dropping the '?') and '[?x]' as an
empty single-pair mapping. Per ns-plain-first a '?' followed by a
flow-safe character (anything but whitespace, end of input, or a flow
indicator) begins a plain scalar.

Gate the '?' key-indicator dispatch on isBlankZ or, in flow context, a
flow indicator, so '?foo' and '?x' stay plain-scalar content. Hoist the
flow-indicator predicate into CharacterClass and reuse it in the plain
scalar scanner. Surpasses libyaml on 652Z and HM87/01. Port score
290 -> 292.
This commit is contained in:
Mihaela Mihaljevic 2026-06-24 18:50:46 +02:00
parent 26e09103b4
commit c3a1e6aaa4
5 changed files with 23 additions and 12 deletions

View File

@ -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

View File

@ -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 }

View File

@ -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()

View File

@ -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
}
}
}

View File

@ -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<String> = [
// 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")