Merge parse-gap fixes onto main
Fix three should-parse conformance gaps: a repeated document-end marker (M7A3), a root block scalar ended by a document marker (W4TN), and an empty explicit key in a flow mapping (DFF7). Threaded the located parse end mark through the new explicit-key emit. Parse-level suite conformance reaches 318/333.
This commit is contained in:
commit
af4a00f229
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
26
Sources/Parsing/TokenEventParser.ExplicitKey.swift
Normal file
26
Sources/Parsing/TokenEventParser.ExplicitKey.swift
Normal file
@ -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, endMark: 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
27
Tests/BlockScalarDocumentBoundaryTests.swift
Normal file
27
Tests/BlockScalarDocumentBoundaryTests.swift
Normal file
@ -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])
|
||||
}
|
||||
}
|
||||
33
Tests/FlowExplicitEmptyKeyTests.swift
Normal file
33
Tests/FlowExplicitEmptyKeyTests.swift
Normal file
@ -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),
|
||||
])))
|
||||
}
|
||||
}
|
||||
30
Tests/RepeatedDocumentEndTests.swift
Normal file
30
Tests/RepeatedDocumentEndTests.swift
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user