From 82d5b57fd6acae3d3bc40520c241c98257cc979a Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Fri, 26 Jun 2026 22:20:18 +0200 Subject: [PATCH] fix(parsing): ignore an unrecognized directive instead of rejecting it A directive the processor does not recognize (e.g. %FOO) before a document is now ignored, matching the YAML spec (emit a warning, otherwise no effect), rather than failing the whole document with an unsupportedDirective error. %YAML and %TAG handling is unchanged, and a directive after content, where a directive cannot legally appear, is still rejected via the existing guard. Two tests and a fixture that pinned the old reject-everything behavior are updated: the unknown-directive case moves from the unsupported-gaps list, and the scanner-error test now exercises a directive-after-content case (still an error) rather than an unknown directive before content. Measured against the official YAML test-suite, parse-level conformance rises from 227/333 (68.2%) to 229/333 (68.8%), with the two newly-parsed documents also value-correct and no regression. --- CHANGELOG.md | 5 +++++ Sources/Parsing/Scanner.Directives.swift | 5 ++++- Tests/DirectiveTests.swift | 16 ++++++++++++++++ Tests/ScannerTests.swift | 4 +++- Tests/UnsupportedYAMLGapsFixtures.swift | 9 --------- 5 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 Tests/DirectiveTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 238f7cf..f5e659d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- An unrecognized directive (e.g. `%FOO`) before a document is now ignored, as the YAML spec + requires (a warning, otherwise no effect), instead of being rejected with an error. `%YAML` + and `%TAG` are unchanged, and a directive after content (where one cannot appear) is still + rejected. Lifts official test-suite parse-level conformance from 227/333 (68.2%) to 229/333 + (68.8%). - A plain scalar at the document root now folds across lines that sit at column 1, so a bare multi-line value such as `a` / `b` / `c` becomes `"a b c"` instead of failing to parse. The continuation check required a continuation to be more indented than the scalar, but a root diff --git a/Sources/Parsing/Scanner.Directives.swift b/Sources/Parsing/Scanner.Directives.swift index c0ab35a..688fd35 100644 --- a/Sources/Parsing/Scanner.Directives.swift +++ b/Sources/Parsing/Scanner.Directives.swift @@ -89,7 +89,10 @@ extension PureYAML.Parsing.Scanner { } state.tagHandles[parts[1]] = parts[2] default: - throw PureYAML.Parsing.ParseError.unsupportedDirective(name: name, line: start.line) + // A YAML processor ignores a directive it does not recognize (the spec calls for a + // warning and otherwise no effect), rather than rejecting the document. The line has + // already been consumed, so there is nothing more to do. + return } } } diff --git a/Tests/DirectiveTests.swift b/Tests/DirectiveTests.swift new file mode 100644 index 0000000..cb8cc6d --- /dev/null +++ b/Tests/DirectiveTests.swift @@ -0,0 +1,16 @@ +@testable import PureYAML +import Testing + +@Suite("Directives") +struct DirectiveTests { + @Test("An unrecognized directive is ignored, not rejected") + func test_unknownDirectiveIsIgnored() throws { + // The YAML spec ignores a directive it does not recognize (with a warning), so the + // document still parses. + #expect(try PureYAML.parse("%FOO bar baz\n---\n\"foo\"\n") == .string("foo")) + #expect( + try PureYAML.parse("%FOO bar\nvalue: one\n") + == .mapping(.init([.init(key: "value", value: .string("one"))])), + ) + } +} diff --git a/Tests/ScannerTests.swift b/Tests/ScannerTests.swift index 7bc06cf..70b1443 100644 --- a/Tests/ScannerTests.swift +++ b/Tests/ScannerTests.swift @@ -265,7 +265,9 @@ struct ScannerTests { expectScannerError("alias: *", .expectedAliasName(line: 1)) expectScannerError("tag: !