From 056095ca3d262e299459fc63d233cf2ddd8f9e8e Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Mon, 29 Jun 2026 08:32:26 +0200 Subject: [PATCH 1/3] fix(parsing): allow a repeated document-end marker A `...` after a document is already closed (only comments or blank lines since the last `...`) ends nothing: a repeated document-end marker is valid YAML, so consume it as a no-op rather than rejecting the stream as an unsupported multi-document case. A second documentEnd is not emitted, so no phantom null document is composed, and real content after the marker stays an error. Fixes suite case M7A3 (parse-level conformance 313 -> 315); adds focused tests. --- Sources/Parsing/Scanner.Directives.swift | 18 ++++++++++++-- Tests/RepeatedDocumentEndTests.swift | 30 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 Tests/RepeatedDocumentEndTests.swift diff --git a/Sources/Parsing/Scanner.Directives.swift b/Sources/Parsing/Scanner.Directives.swift index 5119ea7..c243f87 100644 --- a/Sources/Parsing/Scanner.Directives.swift +++ b/Sources/Parsing/Scanner.Directives.swift @@ -20,8 +20,22 @@ extension PureYAML.Parsing.Scanner { 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 `...` after a document is already closed (only comments or blank lines since the + // last `...`) ends nothing: it is a repeated document-end marker, which YAML allows. Skip + // it as a no-op rather than emitting a second documentEnd (which would compose a phantom + // null document) or rejecting a valid stream. Content after it is still an error. + if state.isDocumentClosed { + state.reader.advance() + state.reader.advance() + state.reader.advance() + while state.reader.peek() == " " { + state.reader.advance() + } + if let next = state.reader.peek(), !isLineBreak(next), next != "#" { + throw PureYAML.Parsing.ParseError.contentAfterDocumentEnd(line: state.reader.mark.line) + } + skipLine(&state) + return true } // A `...` ends a document, so a directive still awaiting its `---` never gets one. try enforceDirectiveHasDocument(state) diff --git a/Tests/RepeatedDocumentEndTests.swift b/Tests/RepeatedDocumentEndTests.swift new file mode 100644 index 0000000..a936f91 --- /dev/null +++ b/Tests/RepeatedDocumentEndTests.swift @@ -0,0 +1,30 @@ +@testable import PureYAML +import Testing + +/// A document-end marker (`...`) may appear repeatedly: once a document is closed, a further `...` +/// (with only comments or blank lines since the last one) ends nothing and is a no-op, rather than +/// being rejected. Content other than a comment after such a marker is still an error. +@Suite("Repeated document end") +struct RepeatedDocumentEndTests { + @Test("A second `...` after a closed document, separated by a comment, is a no-op") + func test_repeatedMarkerIsNoOp() throws { + let documents = try PureYAML.parseStream("a\n...\n# comment\n...\nb\n").map(\.value) + #expect(documents == [.string("a"), .string("b")]) + } + + @Test("A comment-only region between two `...` markers contributes no document") + func test_commentOnlyBetweenMarkers() throws { + // Mirrors suite case M7A3: a bare document, then `...`, a comment, a repeated `...`, and a + // root block scalar as the next document. + let yaml = "Bare\ndocument\n...\n# No document\n...\n|\n%!PS-Adobe-2.0 # Not the first line\n" + let documents = try PureYAML.parseStream(yaml).map(\.value) + #expect(documents == [.string("Bare document"), .string("%!PS-Adobe-2.0 # Not the first line\n")]) + } + + @Test("Real content after a repeated `...` marker is still rejected") + func test_contentAfterRepeatedMarkerRejected() { + #expect(throws: PureYAML.Parsing.ParseError.self) { + _ = try PureYAML.parseStream("a\n...\n... x\n") + } + } +} From 8112954f4c74a22072c310aa7fd2b7ab74ebdbd5 Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Mon, 29 Jun 2026 08:57:39 +0200 Subject: [PATCH 2/3] fix(parsing): end a root block scalar at a document marker A `---` or `...` at column 0 ends an active block scalar rather than being consumed as its content. The scanner now closes the scalar's indentation at the marker, dedenting back to the document level, so the token stream stays balanced: the dedent precedes the documentEnd. Previously the scalar's indent lingered past the marker and surfaced as a stray late dedent plus a phantom empty document, and a following directive was misread as a scalar. Fixes suite case W4TN (parse-level conformance 315 -> 316); adds tests. --- Sources/Parsing/Scanner.Indentation.swift | 16 ++++++++++++ Tests/BlockScalarDocumentBoundaryTests.swift | 27 ++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 Tests/BlockScalarDocumentBoundaryTests.swift diff --git a/Sources/Parsing/Scanner.Indentation.swift b/Sources/Parsing/Scanner.Indentation.swift index 259fa7d..fc101c6 100644 --- a/Sources/Parsing/Scanner.Indentation.swift +++ b/Sources/Parsing/Scanner.Indentation.swift @@ -11,6 +11,22 @@ extension PureYAML.Parsing.Scanner { let end = state.reader.mark let current = state.indentation.last ?? 0 let wasInBlockScalar = state.blockScalarIndentation != nil + + // A document marker (`---` / `...`) at column 0 ends an active block scalar rather than + // being its content. Close the scalar's indentation here, dedenting back to the document + // level, so the marker is then scanned balanced at the document level. Without this the + // scalar's indent lingers past the marker and surfaces later as a stray dedent plus a + // phantom empty node (the marker still ends the document, but the structure is unbalanced). + if state.blockScalarIndentation != nil, width == 0, isAtDocumentMarker(state.reader) { + state.blockScalarIndentation = nil + state.blockScalarContentPrefix = "" + while state.indentation.count > 1 { + state.indentation.removeLast() + state.append(.dedent(width: state.indentation.last ?? 0), mark: end, endMark: end) + } + return + } + if consumeBlockScalarContentIndentation(width: width, state: &state) { return } diff --git a/Tests/BlockScalarDocumentBoundaryTests.swift b/Tests/BlockScalarDocumentBoundaryTests.swift new file mode 100644 index 0000000..452f5cf --- /dev/null +++ b/Tests/BlockScalarDocumentBoundaryTests.swift @@ -0,0 +1,27 @@ +@testable import PureYAML +import Testing + +/// A root block scalar whose content runs to column 0 is ended by a document marker (`...` or +/// `---`), not continued into it. The scalar's indentation is closed at the marker, so the stream +/// stays balanced: no stray dedent and no phantom empty document afterward. +@Suite("Block scalar ended by document boundary") +struct BlockScalarDocumentBoundaryTests { + @Test("A `...` ends a root block scalar, yielding a single document") + func test_terminatedByDocumentEnd() throws { + let documents = try PureYAML.parseStream("--- |\n%!PS\n...\n").map(\.value) + #expect(documents == [.string("%!PS\n")]) + } + + @Test("A `---` ends a root block scalar and starts the next document") + func test_terminatedByNextDocumentStart() throws { + let documents = try PureYAML.parseStream("--- |\n%!PS\n--- next\n").map(\.value) + #expect(documents == [.string("%!PS\n"), .string("next")]) + } + + @Test("Suite case W4TN: a directive, a root literal block scalar, then a comment-only document") + func test_w4tnShape() throws { + let yaml = "%YAML 1.2\n--- |\n%!PS-Adobe-2.0\n...\n%YAML 1.2\n---\n# Empty\n...\n" + let documents = try PureYAML.parseStream(yaml).map(\.value) + #expect(documents == [.string("%!PS-Adobe-2.0\n"), .null]) + } +} From 990a2d2e428a2291d4d8d50269c855830439a6b6 Mon Sep 17 00:00:00 2001 From: Mihaela M Date: Mon, 29 Jun 2026 09:04:29 +0200 Subject: [PATCH 3/3] fix(parsing): parse an empty explicit key in a flow mapping An explicit key (`?`) may have no key node: `? : v`, a trailing `? }` / `? ]` / `? ,`, or `?` at the end of a flow mapping all denote a null key. The parser now emits an empty key in those positions instead of failing to find a node before the value indicator or terminator. Fixes suite case DFF7 (parse-level conformance 316 -> 317); adds tests. --- .../TokenEventParser.ExplicitKey.swift | 26 +++++++++++++++ .../Parsing/TokenEventParser.Helpers.swift | 2 +- Tests/FlowExplicitEmptyKeyTests.swift | 33 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 Sources/Parsing/TokenEventParser.ExplicitKey.swift create mode 100644 Tests/FlowExplicitEmptyKeyTests.swift diff --git a/Sources/Parsing/TokenEventParser.ExplicitKey.swift b/Sources/Parsing/TokenEventParser.ExplicitKey.swift new file mode 100644 index 0000000..dd436b0 --- /dev/null +++ b/Sources/Parsing/TokenEventParser.ExplicitKey.swift @@ -0,0 +1,26 @@ +extension PureYAML.Parsing.TokenEventParser { + /// The key node of an explicit (`?`) mapping entry. The key may be empty: `? : v`, a trailing + /// `? }` / `? ]` / `? ,`, or `?` at stream end all denote a null key. With no node and no + /// properties to anchor, emit a null scalar rather than failing to find a node. + mutating func parseExplicitMappingKey( + properties: PureYAML.Parsing.NodeProperties, + ) throws -> PureYAML.Parsing.NodeResult { + guard properties == .none, isAtEmptyExplicitKey else { + return try parseNode(properties: properties) + } + let mark = cursor.current?.mark ?? cursor.previous?.endMark ?? .start + try emit(.scalar(value: "", anchor: nil, tag: nil, style: .plain, mark: mark)) + return PureYAML.Parsing.NodeResult(kind: .scalar, endMark: mark) + } + + /// Whether an explicit key has no key node: the next token is the value indicator, a flow + /// terminator (`}`, `]`, `,`), or the stream has ended. + var isAtEmptyExplicitKey: Bool { + switch cursor.current?.kind { + case nil, .mappingValue, .flowMappingEnd, .flowSequenceEnd, .flowEntry: + true + default: + false + } + } +} diff --git a/Sources/Parsing/TokenEventParser.Helpers.swift b/Sources/Parsing/TokenEventParser.Helpers.swift index f92ab70..eb004cc 100644 --- a/Sources/Parsing/TokenEventParser.Helpers.swift +++ b/Sources/Parsing/TokenEventParser.Helpers.swift @@ -8,7 +8,7 @@ extension PureYAML.Parsing.TokenEventParser { var keyProperties = firstKeyProperties try keyProperties.mergeUnset(from: consumeProperties()) _ = try expect("mapping key") { $0.isMappingKey } - let key = try parseNode(properties: keyProperties) + let key = try parseExplicitMappingKey(properties: keyProperties) // An explicit key (`? key`) with no following `:` has an implicit null value, so several // `? key` entries in a row (a set, for instance) each map to null rather than being an error. guard case .mappingValue = cursor.current?.kind else { diff --git a/Tests/FlowExplicitEmptyKeyTests.swift b/Tests/FlowExplicitEmptyKeyTests.swift new file mode 100644 index 0000000..e3133e0 --- /dev/null +++ b/Tests/FlowExplicitEmptyKeyTests.swift @@ -0,0 +1,33 @@ +@testable import PureYAML +import Testing + +/// An explicit key (`?`) in a flow mapping may have no key node: `? : v`, a trailing `? }` or +/// `? ]` or `? ,`, all denote a null key. The parser emits an empty key rather than failing to +/// find a node before the value indicator or terminator. +@Suite("Flow explicit empty key") +struct FlowExplicitEmptyKeyTests { + @Test("An explicit key with a value but no key node is a null key") + func test_explicitEmptyKeyWithValue() throws { + #expect(try PureYAML.parse("{ ? : v }") == .mapping(.init([ + .init(keyNode: .string(""), value: .string("v")), + ]))) + } + + @Test("A trailing explicit key with no node maps a null key to null") + func test_trailingExplicitEmptyKey() throws { + #expect(try PureYAML.parse("{ a, ? }") == .mapping(.init([ + .init(key: "a", value: .null), + .init(keyNode: .string(""), value: .null), + ]))) + } + + @Test("Suite case DFF7: a flow mapping ending with an empty explicit key") + func test_dff7() throws { + let yaml = "{\n? explicit: entry,\nimplicit: entry,\n?\n}\n" + #expect(try PureYAML.parse(yaml) == .mapping(.init([ + .init(key: "explicit", value: .string("entry")), + .init(key: "implicit", value: .string("entry")), + .init(keyNode: .string(""), value: .null), + ]))) + } +}