fix(parsing): allow an empty key in a flow mapping

A flow mapping entry may begin with its `:` indicator for an empty key
(`{ : value }`) or carry only properties before the `:` (`{ !!str : value }`).
Both resolve to a null key, but parseFlowMappingPair required a node before
the `:` and raised "expected a YAML node".

Emit a null key in that position instead, mirroring the empty-value handling
already present after the `:`. The change consumes the key's properties first
so a property-only key keeps its tag or anchor, and is confined to flow
mapping key position, where a null key is unambiguously valid; an entry with
no `:` at all (`{ a, }`) still errors.

Raises yaml-test-suite parse-level to 297/333 (89.2%) and value-level to
174/192, with the should-error rejection rate unchanged (no invalid document
newly accepted). 422 tests pass.
This commit is contained in:
Mihaela M 2026-06-28 19:23:49 +02:00
parent 5a0e6c732e
commit 956f42bbb7
3 changed files with 69 additions and 1 deletions

View File

@ -84,6 +84,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- A flow mapping entry may now begin with its `:` indicator, giving an empty (null) key, e.g.
`{ : value }` or `{ a: b, : c }`, or carry only properties before the `:`, e.g. `{ !!str : value }`.
The parser required a node before every flow mapping `:` and raised "expected a YAML node"; it now
emits a null key in that position, the same way an empty value after the `:` is already handled. The
allowance is local to flow mapping key position, so an entry with no `:` at all (`{ a, }`) stays an
error. Raises yaml-test-suite parse-level conformance to 297/333 (89.2%) and value-level to 174/192,
with the should-error rejection rate unchanged.
- A flow collection followed by a `: ` indicator on the same line is now parsed as an implicit block
mapping key, e.g. `[a, b]: value` or `{a: 1}: value`. The scanner looks ahead from the flow opener
to its matching close (skipping quoted scalars so their brackets and colons do not miscount) and,

View File

@ -103,7 +103,16 @@ extension PureYAML.Parsing.TokenEventParser {
return try parseMappingPair()
}
_ = try parseNode()
// A flow mapping key may be empty (`{ : v }`) or carry only properties (`{ !!str : v }`):
// both resolve to a null key, so emit one rather than requiring a node before the `:`. This is
// valid only in flow mapping key position, so the empty-node allowance stays local here.
let keyProperties = try consumeProperties()
if case .mappingValue = cursor.current?.kind {
let mark = keyProperties.mark ?? cursor.current?.mark ?? .start
try emit(.scalar(value: "", anchor: keyProperties.anchor, tag: keyProperties.tag, style: .plain, mark: mark))
} else {
_ = try parseNode(properties: keyProperties)
}
let valueIndicator = try expect("mapping value") { kind in
if case .mappingValue = kind {
return true

View File

@ -0,0 +1,51 @@
@testable import PureYAML
import Testing
/// A flow mapping entry may begin with its `:` indicator, giving an empty (null) key, e.g. `{ : v }`,
/// or carry only properties before the `:`, e.g. `{ !!str : v }`. Both resolve to a null key, which
/// the parser now emits rather than demanding a node before the `:`. The allowance is local to flow
/// mapping key position, so an entry with no `:` at all (`{ a, }`) is still an error.
@Suite("Flow mapping empty key")
struct FlowMappingEmptyKeyTests {
@Test("A flow mapping entry that begins with a colon has an empty key")
func test_emptyKey() throws {
#expect(try PureYAML.parse("{ : a }\n") == .mapping(.init([
.init(keyNode: .string(""), value: .string("a")),
])))
}
@Test("An empty key sits alongside ordinary entries")
func test_emptyKeyAmongEntries() throws {
#expect(try PureYAML.parse("{ a: b, : c }\n") == .mapping(.init([
.init(key: "a", value: .string("b")),
.init(keyNode: .string(""), value: .string("c")),
])))
}
@Test("Both key and value may be empty in one entry")
func test_emptyKeyAndValue() throws {
#expect(try PureYAML.parse("{ : }\n") == .mapping(.init([
.init(keyNode: .string(""), value: .null),
])))
}
@Test("A key carrying only a tag is a null key and parses")
func test_propertyOnlyKey() throws {
// `{ foo : !!str, !!str : bar }`: the first value and the second key are property-only nulls.
_ = try PureYAML.parse("{ foo : !!str, !!str : bar }\n")
}
@Test("An empty value is still accepted, unchanged")
func test_emptyValueUnchanged() throws {
#expect(try PureYAML.parse("{ a: }\n") == .mapping(.init([
.init(key: "a", value: .null),
])))
}
@Test("A flow mapping entry with no colon at all is still an error")
func test_entryWithoutColonRejected() {
#expect(throws: PureYAML.Parsing.ParseError.self) {
try PureYAML.parse("{ a, }\n")
}
}
}