diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f65558..71a531a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Parse block mappings with empty (null) keys. A `:` value indicator with no + preceding key on its line denotes a mapping entry whose key is empty, which + YAML 1.2 permits but libyaml rejects with "did not find expected key". The + block-mapping-key parser now emits the empty scalar key and lets the value + state consume the indicator, fixing the canonical event streams for `: a` + style mappings, explicit empty keys (`? &a a` / `: *a`), empty keys nested in + sequences (`- :`, `- ? : x`), and mixed empty-key/value entries. Surpasses + libyaml on these seven test-suite cases (2JQS, 6M2F, M2N8/00, NHX8, S3PD, + SM9W/01, UKK6/00). - Emit no document for a stream whose only content is comments or whitespace. A comment-only or empty stream has zero documents in YAML 1.2 (matching libyaml and the test suite), but the event stream was emitting a spurious empty null diff --git a/Sources/Parsing/LibYAML.Parser.Block.swift b/Sources/Parsing/LibYAML.Parser.Block.swift index 0607dec..2f92623 100644 --- a/Sources/Parsing/LibYAML.Parser.Block.swift +++ b/Sources/Parsing/LibYAML.Parser.Block.swift @@ -74,6 +74,14 @@ extension PureYAML.Parsing.LibYAML.Parser { skipToken() return .mappingEnd(mark: token.startMark) } + // YAML 1.2 permits an empty (null) key: a VALUE indicator with no + // preceding key denotes a mapping entry whose key is empty. libyaml + // rejects this here; instead emit the empty scalar key and leave the + // VALUE token for `parseBlockMappingValue` to consume. + if case .value = token.type { + state = .blockMappingValue + return processEmptyScalar(at: token.startMark) + } throw ParserError( context: "while parsing a block mapping", problem: "did not find expected key", diff --git a/Tests/CanonicalEventConformanceTests.swift b/Tests/CanonicalEventConformanceTests.swift index af1b12c..6dd3031 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 13 emit a wrong event stream. + /// 23 currently throw on valid input and 3 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 @@ -21,10 +21,10 @@ struct CanonicalEventConformanceTests { // itself handles. // // false-rejects (parseEvents throws on valid input, as libyaml does) - "2JQS", "4ABK", "58MP", "5T43", "6BCT", "6CA3", "6M2F", + "4ABK", "58MP", "5T43", "6BCT", "6CA3", "96NN/00", "96NN/01", "A2M4", "CFD4", "DBG4", "DK3J", "DK95/00", - "DK95/04", "FP8R", "FRK4", "HM87/00", "M2N8/00", "M7A3", "NHX8", - "NKF9", "Q5MG", "R4YG", "S3PD", "SM9W/01", "UKK6/00", "W4TN", + "DK95/04", "FP8R", "FRK4", "HM87/00", "M7A3", + "NKF9", "Q5MG", "R4YG", "W4TN", "Y79Y/001", "Y79Y/010", // wrong event stream: the port matches libyaml but both deviate from the // canonical events (libyaml quirks to be surpassed).