PureYAML/Sources/Parsing/Scanner.Directives.swift
Mihaela M 27c423dbb9 fix(parsing): require an explicit document after a directive
The YAML grammar requires every directive to head an explicit document,
so a directive must be followed by a '---' marker. The scanner accepted a
directive with no document at all: '%YAML 1.2' alone, or '%YAML 1.2' then
a '...' document-end marker, parsed without error.

The scanner now records the line of a directive awaiting its document,
clears it at a document start, and rejects a still-pending directive at a
document-end marker and at stream end. Both scan drivers finalize through
the same stream-end path, so the check lives in one helper called from
each. The rule applies to any directive, including an unrecognized one
(which is otherwise still ignored); the directive test that relied on an
unrecognized directive parsing without a '---' is corrected to include
one, since that leniency is not permitted by the grammar.

Measured against the official YAML test-suite, parse-level conformance
rises from 253/333 (76.0%) to 255/333 (76.6%) with no regression.
2026-06-27 17:18:47 +02:00

123 lines
5.1 KiB
Swift

extension PureYAML.Parsing.Scanner {
func scanDirectiveOrDocumentMarker(_ state: inout State) throws -> Bool {
guard state.indentation.last == 0 else {
return false
}
if state.reader.peek() == "%" {
try scanDirective(&state)
return true
}
if state.reader.peek() == "#" {
return false
}
if startsWith("---", state.reader), isIndicatorBoundary(state.reader.peek(offset: 3)) {
scanDocumentMarker(.documentStart, state: &state)
resetForDocumentStart(&state)
return true
}
if startsWith("...", state.reader), isIndicatorBoundary(state.reader.peek(offset: 3)) {
guard !state.isDocumentClosed else {
throw PureYAML.Parsing.ParseError.unsupportedMultiDocumentStream(line: state.reader.mark.line)
}
// A `...` ends a document, so a directive still awaiting its `---` never gets one.
try enforceDirectiveHasDocument(state)
scanDocumentMarker(.documentEnd, state: &state)
resetForDocumentEnd(&state)
return true
}
guard !state.isDocumentClosed else {
throw PureYAML.Parsing.ParseError.unsupportedMultiDocumentStream(line: state.reader.mark.line)
}
return false
}
func scanDocumentMarker(
_ kind: PureYAML.Parsing.TokenKind,
state: inout State,
) {
let start = state.reader.mark
state.reader.advance()
state.reader.advance()
state.reader.advance()
state.append(kind, mark: start, endMark: state.reader.mark)
// The document's root node may begin on the marker's own line (e.g. `--- >`, `--- foo`,
// `--- {a: 1}`, `--- !!str x`). After a document start, skip only the spaces and leave any
// real content for the main loop to scan; an empty rest-of-line or a comment is consumed.
// A document end (`...`) takes no same-line node, so its line is always skipped.
if case .documentStart = kind {
while state.reader.peek() == " " {
state.reader.advance()
}
if let next = state.reader.peek(), !isLineBreak(next), next != "#" {
state.isAtLineStart = false
return
}
}
skipLine(&state)
}
func resetForDocumentStart(_ state: inout State) {
state.hasDocumentContent = false
state.hasDocumentStartMarker = true
state.isDocumentClosed = false
state.hasYAMLDirective = false
state.pendingDirectiveLine = nil
}
/// A directive must be followed by an explicit `---` document start. Called at a document end
/// and at stream end, where a still-pending directive means none ever came.
func enforceDirectiveHasDocument(_ state: State) throws {
if let line = state.pendingDirectiveLine {
throw PureYAML.Parsing.ParseError.directiveWithoutDocument(line: line)
}
}
func resetForDocumentEnd(_ state: inout State) {
state.hasDocumentContent = false
state.hasDocumentStartMarker = false
state.isDocumentClosed = true
state.hasYAMLDirective = false
}
func scanDirective(_ state: inout State) throws {
let start = state.reader.mark
let line = state.reader.consume { character in
!isLineBreak(character)
}
let parts = line.split(separator: " ").map(String.init)
guard let name = parts.first else {
return
}
guard !state.hasDocumentContent, !state.hasDocumentStartMarker, !state.isDocumentClosed else {
throw PureYAML.Parsing.ParseError.unsupportedDirective(name: name, line: start.line)
}
// Every directive must be followed by an explicit `---`; record that one is now awaiting it.
state.pendingDirectiveLine = start.line
switch name {
case "%YAML":
guard parts.indices.contains(1), parts[1] == "1.1" || parts[1] == "1.2" else {
throw PureYAML.Parsing.ParseError.incompatibleYAMLDirective(line: start.line)
}
// `%YAML` takes exactly one parameter, the version; only a comment may follow it.
guard parts.count == 2 || parts[2].hasPrefix("#") else {
throw PureYAML.Parsing.ParseError.malformedDirective(name: name, line: start.line)
}
// At most one `%YAML` directive is allowed per document.
guard !state.hasYAMLDirective else {
throw PureYAML.Parsing.ParseError.duplicateDirective(name: name, line: start.line)
}
state.hasYAMLDirective = true
case "%TAG":
guard parts.count >= 3 else {
return
}
state.tagHandles[parts[1]] = parts[2]
default:
// 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
}
}
}