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.
This commit is contained in:
Mihaela M 2026-06-26 22:20:18 +02:00
parent 688f0d5c12
commit 82d5b57fd6
5 changed files with 28 additions and 11 deletions

View File

@ -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

View File

@ -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
}
}
}

View File

@ -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"))])),
)
}
}

View File

@ -265,7 +265,9 @@ struct ScannerTests {
expectScannerError("alias: *", .expectedAliasName(line: 1))
expectScannerError("tag: !<tag:example.com,2026:thing", .unterminatedTag(line: 1))
expectScannerError("%YAML 1.3\n---\nvalue: yes", .incompatibleYAMLDirective(line: 1))
expectScannerError("%FOO bar\nvalue: yes", .unsupportedDirective(name: "%FOO", line: 1))
// An unrecognized directive before the document is ignored, but one after content (where a
// directive cannot appear) is still rejected.
expectScannerError("value: yes\n%FOO bar", .unsupportedDirective(name: "%FOO", line: 2))
expectScannerError("---\n%TAG !e! tag:example.com,2026:", .unsupportedDirective(name: "%TAG", line: 2))
expectScannerError("value: one\n...\nvalue: two", .unsupportedMultiDocumentStream(line: 3))
}

View File

@ -57,15 +57,6 @@ enum UnsupportedYAMLGapsFixtures {
expected: .unsupportedMultiDocumentStream(line: 3),
expectedDescription: "multi-document streams are not supported at line 3",
),
ParseErrorCase(
name: "unknown directive",
yaml: """
%FOO bar
value: one
""",
expected: .unsupportedDirective(name: "%FOO", line: 1),
expectedDescription: "unsupported directive '%FOO' at line 1",
),
ParseErrorCase(
name: "tag directive after content",
yaml: """