From 4941eac7113381642944da8a59de5ecc2af53a32 Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Sun, 28 Jun 2026 02:53:45 +0200 Subject: [PATCH] fix(parsing): parse implicit null mapping keys A ':' that opens a line with no key before it is a mapping entry with a null key (': value', a bare ':', or a null-keyed entry among others). The scanner emitted the ':' with no preceding key, so the parser rejected it as a missing node. The scanner now synthesizes the empty key and its node for such a ':'. To avoid mistaking an explicit key's value indicator for a null key, it tracks a pending block-level '?' key: a ':' opening a line is that key's value when one is pending, and only a synthesized null key otherwise. The flag is set and cleared only at flow depth zero, so a ':' inside a flow complex key ('? {a: b}' then ': c') does not disturb the pending block key. Measured against the official YAML test-suite, parse-level conformance rises from 283/333 (85.0%) to 287/333 (86.2%) with no regression. --- CHANGELOG.md | 6 +++++ Sources/Parsing/Scanner.swift | 38 +++++++++++++++++++++++++++++--- Tests/ImplicitNullKeyTests.swift | 33 +++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Tests/ImplicitNullKeyTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index dac17bf..81efa4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- An implicit null mapping key is now parsed. A `:` that opens a line with no key before it + (`: value`, a bare `:`, or a null key among other entries) is an entry with a null key, which + the parser previously rejected as a missing node. The scanner synthesizes the empty key, tracking + a pending explicit `?` key so that an explicit key's value indicator on the next line, including + after a flow complex key (`? {a: b}` then `: c`), is not mistaken for a null key. Lifts official + test-suite parse-level conformance from 283/333 (85.0%) to 287/333 (86.2%), with no regression. - A directive after a document-end marker (`...`) now begins the next document instead of being rejected. A multi-document stream where each document carries its own directives (`%TAG ...` / `--- doc` / `...` / `%TAG ...` / `--- doc`) is now accepted, since a directive diff --git a/Sources/Parsing/Scanner.swift b/Sources/Parsing/Scanner.swift index aa036e4..27d1922 100644 --- a/Sources/Parsing/Scanner.swift +++ b/Sources/Parsing/Scanner.swift @@ -84,10 +84,14 @@ extension PureYAML.Parsing.Scanner { // (`a: b: c`). Reset at each line break. Flow `:` do not set it, since flow allows several // pairs on a line (`{a: b, c: d}`). var sawMappingValueOnLine = false - // Whether a flow collection has closed on the current line at the top level. A block - // sequence entry cannot follow a flow node on the same line (`{a: b}- x`), unlike a nested - // block entry (`- -`), which is not preceded by a flow close. Reset at each line break. + /// Whether a flow collection has closed on the current line at the top level. A block + /// sequence entry cannot follow a flow node on the same line (`{a: b}- x`), unlike a nested + /// block entry (`- -`), which is not preceded by a flow close. Reset at each line break. var sawFlowCloseOnLine = false + // Whether an explicit `?` mapping key is awaiting its `:` value indicator. A `:` that opens + // a line is that key's value when one is pending, otherwise it is an implicit null key + // (`: value`). Only the `?` indicator sets this; a synthesized implicit key does not. + var pendingExplicitKey = false var tagHandles = [ "!": "!", "!!": "tag:yaml.org,2002:", @@ -157,6 +161,7 @@ extension PureYAML.Parsing.Scanner { if character == ":", hadContentOnLine, state.flowDepth == 0, isAtMappingValueIndicator(state: state) { state.sawMappingValueOnLine = true } + synthesizeNullMappingKeyIfNeeded(character, hadContentOnLine: hadContentOnLine, state: &state) if try scanLineIndicatorToken(character, state: &state) { return } @@ -182,6 +187,24 @@ extension PureYAML.Parsing.Scanner { scanPlainScalar(&state) } + /// A `:` that opens a line with no key before it is an implicit null mapping key (`: value`), + /// unless it is the value indicator of a pending explicit `?` key. Synthesizes the empty key and + /// its node so the block mapping machinery treats the pair uniformly. + func synthesizeNullMappingKeyIfNeeded( + _ character: Character, + hadContentOnLine: Bool, + state: inout State, + ) { + guard character == ":", !hadContentOnLine, state.flowDepth == 0, !state.pendingExplicitKey, + isAtMappingValueIndicator(state: state) + else { + return + } + let mark = state.reader.mark + state.append(.mappingKey, mark: mark, endMark: mark) + state.append(.scalar(value: "", style: .plain), mark: mark, endMark: mark) + } + func scanLineIndicatorToken( _ character: Character, state: inout State, @@ -331,5 +354,14 @@ extension PureYAML.Parsing.Scanner { if case .mappingValue = kind, state.flowContexts.last?.kind == .mapping { state.flowContexts[state.flowContexts.count - 1].isExpectingKey = false } + // Only the explicit `?` indicator emits a mapping key through this path (an implicit key is + // synthesized directly), so track a block-level one as pending until its `:` value indicator + // is scanned. Flow `:`/`?` (inside a complex key, say) do not affect a pending block key. + if state.flowDepth == 0, case .mappingKey = kind { + state.pendingExplicitKey = true + } + if state.flowDepth == 0, case .mappingValue = kind { + state.pendingExplicitKey = false + } } } diff --git a/Tests/ImplicitNullKeyTests.swift b/Tests/ImplicitNullKeyTests.swift new file mode 100644 index 0000000..e61c076 --- /dev/null +++ b/Tests/ImplicitNullKeyTests.swift @@ -0,0 +1,33 @@ +@testable import PureYAML +import Testing + +@Suite("Implicit null mapping keys") +struct ImplicitNullKeyTests { + @Test("A `:` opening a line is an implicit null key") + func test_implicitNullKey() throws { + #expect(try PureYAML.parse(": value\n") == .mapping(.init([ + .init(keyNode: .string(""), value: .string("value")), + ]))) + } + + @Test("Several null-keyed entries and a bare `:` parse") + func test_multipleNullKeys() throws { + _ = try PureYAML.parse(": a\n: b\n") + _ = try PureYAML.parse(":\n") + #expect(try PureYAML.parse("a: 1\n: b\n") == .mapping(.init([ + .init(key: "a", value: .int(1)), + .init(keyNode: .string(""), value: .string("b")), + ]))) + } + + @Test("Explicit keys, including a complex one, are not mistaken for null keys") + func test_explicitKeysUnaffected() throws { + #expect(try PureYAML.parse("? a\n: b\n") == .mapping(.init([ + .init(key: "a", value: .string("b")), + ]))) + // The `:` after a flow complex key is the key's value, not a null key. + #expect(try PureYAML.parse("? {a: b}\n: c\n") == .mapping(.init([ + .init(keyNode: .mapping(.init([.init(key: "a", value: .string("b"))])), value: .string("c")), + ]))) + } +}