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: !