From b5129d71eecf25c65311bab920723210ea83873e Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 24 Jun 2026 19:51:39 +0200 Subject: [PATCH] fix(parsing): reject misplaced directives, refresh suite manifest Directives apply to the document that follows, so the previous document must be closed with an explicit '...' end marker before them. After an implicit end, a '%YAML'/'%TAG'/reserved directive is now rejected instead of being silently accepted. Also reject a '%YAML' version trailed directly by '#' with no separating space ('%YAML 1.1#...'), since a comment needs leading separation; libyaml accepts both. This restores reject-side conformance the suite manifest recorded as passing but the libyaml port had dropped (9HCY, EB22, MUS6/00, MUS6/01, RHX7), and updates the two PureYAML gap-error fixtures whose rejection now fires earlier and more precisely at the misplaced directive. Refresh yaml-test-suite-manifest.yaml to version 3: whole-suite conformance is 384/402 (up from the recorded 331); 53 cases still marked as gaps now pass, none regress. --- CHANGELOG.md | 11 ++ Sources/Parsing/LibYAML.Parser.Document.swift | 19 +++ .../Parsing/LibYAML.Scanner.Directive.swift | 6 + Tests/Fixtures/yaml-test-suite-manifest.yaml | 112 +++++++++--------- Tests/UnsupportedYAMLGapsFixtures.swift | 15 ++- Tests/YAMLTestSuiteGapsTests.swift | 2 +- 6 files changed, 102 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f1bb2f..defb752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Reject a directive that follows a document without an explicit `...` end marker. + Directives apply to the document that follows, so the previous document must be + closed with `...` before them; after an implicit end a `%YAML`/`%TAG`/reserved + directive is now an error. Also reject a `%YAML` version followed immediately by + `#` with no separating space (`%YAML 1.1#...`), since a comment requires leading + separation. Restores reject-side conformance lost when the libyaml port became + the sole engine (9HCY, EB22, MUS6/00, MUS6/01, RHX7). +- Refresh `yaml-test-suite-manifest.yaml` (now version 3): the ported engine's + whole-suite conformance is 384/402, up from the recorded 331, after the + canonical-event and reject-side work. 53 cases the manifest still marked as gaps + now pass. - Keep the final line break of a block scalar that ends at end of input without a trailing newline. A whitespace-only last line still counts as one line break under keep (`+`) chomping, so `- |+` followed by a spaces-only unterminated line diff --git a/Sources/Parsing/LibYAML.Parser.Document.swift b/Sources/Parsing/LibYAML.Parser.Document.swift index c670c15..6abe53d 100644 --- a/Sources/Parsing/LibYAML.Parser.Document.swift +++ b/Sources/Parsing/LibYAML.Parser.Document.swift @@ -21,6 +21,16 @@ extension PureYAML.Parsing.LibYAML.Parser { skipToken() token = peekToken() } + // Directives apply to the document that follows, so the previous document + // must be closed with an explicit `...` end marker before them. After an + // implicit end (`allowBareDocument` is false) a directive is an error. + if isDirective(token), !allowBareDocument { + throw ParserError( + context: nil, + problem: "directives require a preceding document end marker", + mark: token.startMark, + ) + } if allowBareDocument, !isDirectiveOrBoundary(token) { try processDirectives() states.append(.documentEnd) @@ -84,6 +94,15 @@ extension PureYAML.Parsing.LibYAML.Parser { } } + private func isDirective(_ token: Token) -> Bool { + switch token.type { + case .versionDirective, .tagDirective, .reservedDirective: + true + default: + false + } + } + private func isDocumentEnd(_ token: Token) -> Bool { if case .documentEnd = token.type { return true diff --git a/Sources/Parsing/LibYAML.Scanner.Directive.swift b/Sources/Parsing/LibYAML.Scanner.Directive.swift index 6cb1b1a..f289784 100644 --- a/Sources/Parsing/LibYAML.Scanner.Directive.swift +++ b/Sources/Parsing/LibYAML.Scanner.Directive.swift @@ -101,6 +101,12 @@ extension PureYAML.Parsing.LibYAML.Scanner { } reader.skip() let minor = try scanVersionDirectiveNumber(startMark: startMark) + // The version must be followed by separation; a comment needs a space + // before its `#`, so `%YAML 1.1#...` (no space) is malformed. libyaml + // accepts it, treating the `#` as an adjacent comment. + guard Classify.isBlankZ(reader.peek()) else { + throw directiveError("while scanning a %YAML directive", "did not find expected digit or '.' character", startMark) + } return (major, minor) } diff --git a/Tests/Fixtures/yaml-test-suite-manifest.yaml b/Tests/Fixtures/yaml-test-suite-manifest.yaml index 19bd4c8..d75ec62 100644 --- a/Tests/Fixtures/yaml-test-suite-manifest.yaml +++ b/Tests/Fixtures/yaml-test-suite-manifest.yaml @@ -1,4 +1,4 @@ -version: 2 +version: 3 description: PureYAML conformance status vs the official YAML test suite source: repository: yaml/yaml-test-suite @@ -10,8 +10,8 @@ source: # oracle: json if in.json present, else none # status: passing (PureYAML conformant today) | gap (not yet conformant) total: 402 -passing: 331 -gap: 71 +passing: 384 +gap: 18 cases: - id: "229Q" name: "Spec Example 2.4. Sequence of Mappings" @@ -74,7 +74,7 @@ cases: localPath: yaml-test-suite/2G84/00/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 7 lineCount: 1 - id: "2G84/01" @@ -82,7 +82,7 @@ cases: localPath: yaml-test-suite/2G84/01/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 8 lineCount: 1 - id: "2G84/02" @@ -122,7 +122,7 @@ cases: localPath: yaml-test-suite/2SXE/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 29 lineCount: 3 - id: "2XXW" @@ -314,7 +314,7 @@ cases: localPath: yaml-test-suite/4JVG/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 54 lineCount: 4 - id: "4MUZ/00" @@ -418,7 +418,7 @@ cases: localPath: yaml-test-suite/55WF/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 9 lineCount: 2 - id: "565N" @@ -434,7 +434,7 @@ cases: localPath: yaml-test-suite/57H4/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 67 lineCount: 6 - id: "58MP" @@ -442,7 +442,7 @@ cases: localPath: yaml-test-suite/58MP/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 8 lineCount: 1 - id: "5BVJ" @@ -506,7 +506,7 @@ cases: localPath: yaml-test-suite/5T43/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 37 lineCount: 2 - id: "5TRB" @@ -546,7 +546,7 @@ cases: localPath: yaml-test-suite/62EZ/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 25 lineCount: 2 - id: "652Z" @@ -554,7 +554,7 @@ cases: localPath: yaml-test-suite/652Z/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 23 lineCount: 3 - id: "65WH" @@ -578,7 +578,7 @@ cases: localPath: yaml-test-suite/6BFJ/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 43 lineCount: 3 - id: "6CA3" @@ -794,7 +794,7 @@ cases: localPath: yaml-test-suite/7LBH/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 20 lineCount: 3 - id: "7MNF" @@ -842,7 +842,7 @@ cases: localPath: yaml-test-suite/7ZZ5/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 58 lineCount: 6 - id: "82AN" @@ -874,7 +874,7 @@ cases: localPath: yaml-test-suite/8G76/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 18 lineCount: 4 - id: "8KB6" @@ -914,7 +914,7 @@ cases: localPath: yaml-test-suite/8XDJ/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 26 lineCount: 3 - id: "8XYN" @@ -922,7 +922,7 @@ cases: localPath: yaml-test-suite/8XYN/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 27 lineCount: 2 - id: "93JH" @@ -970,7 +970,7 @@ cases: localPath: yaml-test-suite/98YD/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 16 lineCount: 1 - id: "9BXH" @@ -1058,7 +1058,7 @@ cases: localPath: yaml-test-suite/9MAG/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 18 lineCount: 2 - id: "9MMA" @@ -1178,7 +1178,7 @@ cases: localPath: yaml-test-suite/AVM7/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 0 lineCount: 0 - id: "AZ63" @@ -1234,7 +1234,7 @@ cases: localPath: yaml-test-suite/BF9H/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 47 lineCount: 4 - id: "BS4K" @@ -1338,7 +1338,7 @@ cases: localPath: yaml-test-suite/CTN5/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 19 lineCount: 2 - id: "CUP7" @@ -1362,7 +1362,7 @@ cases: localPath: yaml-test-suite/CXX2/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 17 lineCount: 1 - id: "D49Q" @@ -1370,7 +1370,7 @@ cases: localPath: yaml-test-suite/D49Q/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 20 lineCount: 3 - id: "D83L" @@ -1402,7 +1402,7 @@ cases: localPath: yaml-test-suite/DBG4/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 218 lineCount: 12 - id: "DC7X" @@ -1410,7 +1410,7 @@ cases: localPath: yaml-test-suite/DC7X/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 26 lineCount: 4 - id: "DE56/00" @@ -1674,7 +1674,7 @@ cases: localPath: yaml-test-suite/FH7J/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 51 lineCount: 5 - id: "FP8R" @@ -1802,7 +1802,7 @@ cases: localPath: yaml-test-suite/H7J7/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 21 lineCount: 3 - id: "H7TQ" @@ -1818,7 +1818,7 @@ cases: localPath: yaml-test-suite/HM87/00/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 5 lineCount: 1 - id: "HM87/01" @@ -1826,7 +1826,7 @@ cases: localPath: yaml-test-suite/HM87/01/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 5 lineCount: 1 - id: "HMK4" @@ -1850,7 +1850,7 @@ cases: localPath: yaml-test-suite/HRE5/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 31 lineCount: 2 - id: "HS5T" @@ -1858,7 +1858,7 @@ cases: localPath: yaml-test-suite/HS5T/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 46 lineCount: 4 - id: "HU3P" @@ -1866,7 +1866,7 @@ cases: localPath: yaml-test-suite/HU3P/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 29 lineCount: 3 - id: "HWV9" @@ -1874,7 +1874,7 @@ cases: localPath: yaml-test-suite/HWV9/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 4 lineCount: 1 - id: "J3BT" @@ -1938,7 +1938,7 @@ cases: localPath: yaml-test-suite/JEF9/02/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 8 lineCount: 1 - id: "JHB9" @@ -1954,7 +1954,7 @@ cases: localPath: yaml-test-suite/JKF3/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 17 lineCount: 2 - id: "JQ4R" @@ -1994,7 +1994,7 @@ cases: localPath: yaml-test-suite/JY7Z/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 66 lineCount: 3 - id: "K3WX" @@ -2170,7 +2170,7 @@ cases: localPath: yaml-test-suite/LX3P/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 14 lineCount: 1 - id: "M29M" @@ -2202,7 +2202,7 @@ cases: localPath: yaml-test-suite/M5C3/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 48 lineCount: 6 - id: "M5DY" @@ -2210,7 +2210,7 @@ cases: localPath: yaml-test-suite/M5DY/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 142 lineCount: 9 - id: "M6YH" @@ -2226,7 +2226,7 @@ cases: localPath: yaml-test-suite/M7A3/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 74 lineCount: 7 - id: "M7NX" @@ -2362,7 +2362,7 @@ cases: localPath: yaml-test-suite/NB6Z/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 31 lineCount: 5 - id: "NHX8" @@ -2410,7 +2410,7 @@ cases: localPath: yaml-test-suite/P2EQ/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 24 lineCount: 2 - id: "P76L" @@ -2498,7 +2498,7 @@ cases: localPath: yaml-test-suite/Q9WF/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 97 lineCount: 6 - id: "QB6E" @@ -2522,7 +2522,7 @@ cases: localPath: yaml-test-suite/QLJ7/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 93 lineCount: 7 - id: "QT73" @@ -2530,7 +2530,7 @@ cases: localPath: yaml-test-suite/QT73/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 14 lineCount: 2 - id: "R4YG" @@ -2682,7 +2682,7 @@ cases: localPath: yaml-test-suite/SKE5/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 26 lineCount: 5 - id: "SM9W/00" @@ -2882,7 +2882,7 @@ cases: localPath: yaml-test-suite/UKK6/00/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 4 lineCount: 1 - id: "UKK6/01" @@ -2914,7 +2914,7 @@ cases: localPath: yaml-test-suite/UV7Q/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 13 lineCount: 3 - id: "V55R" @@ -2930,7 +2930,7 @@ cases: localPath: yaml-test-suite/V9D5/in.yaml expected: parse-ok oracle: none - status: gap + status: passing byteCount: 46 lineCount: 3 - id: "VJP3/00" @@ -2970,7 +2970,7 @@ cases: localPath: yaml-test-suite/W5VH/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 43 lineCount: 2 - id: "W9L4" @@ -3042,7 +3042,7 @@ cases: localPath: yaml-test-suite/Y2GN/in.yaml expected: parse-ok oracle: json - status: gap + status: passing byteCount: 24 lineCount: 2 - id: "Y79Y/000" @@ -3170,7 +3170,7 @@ cases: localPath: yaml-test-suite/ZCZ6/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 11 lineCount: 1 - id: "ZF4X" @@ -3202,7 +3202,7 @@ cases: localPath: yaml-test-suite/ZL4Z/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 14 lineCount: 2 - id: "ZVH3" diff --git a/Tests/UnsupportedYAMLGapsFixtures.swift b/Tests/UnsupportedYAMLGapsFixtures.swift index ad58da4..cd54f5e 100644 --- a/Tests/UnsupportedYAMLGapsFixtures.swift +++ b/Tests/UnsupportedYAMLGapsFixtures.swift @@ -71,25 +71,28 @@ enum UnsupportedYAMLGapsFixtures { ), ParseErrorCase( // After document content has begun, a line starting with `%` is a - // misplaced directive (a plain scalar may not start with `%`), so the - // document is rejected rather than reinterpreting `%` as plain text. + // misplaced directive (a plain scalar may not start with `%`). The + // first document ended implicitly, so the directive is rejected: a + // directive applies to the next document and requires a preceding `...`. name: "percent after content is plain text not a directive", yaml: """ value: one %TAG !e! tag:example.com,2026: """, - expected: .malformed(reason: "did not find expected ", line: 3, column: 1), - expectedDescription: "did not find expected at line 3, column 1", + expected: .malformed(reason: "directives require a preceding document end marker", line: 2, column: 1), + expectedDescription: "directives require a preceding document end marker at line 2, column 1", ), ParseErrorCase( + // A directive after `---` ends that document implicitly, so it applies + // to the next document and requires a preceding `...` end marker. name: "percent after document start is plain text not a directive", yaml: """ --- %TAG !e! tag:example.com,2026: value: !e!thing one """, - expected: .malformed(reason: "did not find expected ", line: 3, column: 1), - expectedDescription: "did not find expected at line 3, column 1", + expected: .malformed(reason: "directives require a preceding document end marker", line: 2, column: 1), + expectedDescription: "directives require a preceding document end marker at line 2, column 1", ), ] diff --git a/Tests/YAMLTestSuiteGapsTests.swift b/Tests/YAMLTestSuiteGapsTests.swift index be4519a..7ab9339 100644 --- a/Tests/YAMLTestSuiteGapsTests.swift +++ b/Tests/YAMLTestSuiteGapsTests.swift @@ -25,7 +25,7 @@ struct YAMLTestSuiteGapsTests { func test_manifestMatchesVendoredFixtureFiles() throws { let manifest = try YAMLTestSuiteManifest.load() - #expect(manifest.version == 2) + #expect(manifest.version == 3) #expect(manifest.cases.count == manifest.total) #expect(manifest.total == 402) #expect(Set(manifest.cases.map(\.id)).count == manifest.cases.count)