From d222b8cc533a3b4a16c1da219bf4ce18b0219c0c Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 24 Jun 2026 19:22:33 +0200 Subject: [PATCH] fix(parsing): end a root block scalar at a column-0 document marker Now that a root-level block scalar can auto-detect indentation 0, its content loop reaches lines in column 0, so a '---' or '...' document marker there must end the scalar. Without the guard a '--- |' literal folded the following '...', the next '%YAML' directive, and every later document into its value. Break out of the block-scalar content loop on a column-0 document marker. The guard only fires at indent 0 (column == indent == 0), so nested block scalars are unaffected. Surpasses libyaml on W4TN. Port score 306 -> 307. --- CHANGELOG.md | 5 +++++ .../Parsing/LibYAML.Scanner.BlockScalar.swift | 16 ++++++++++++++++ Tests/CanonicalEventConformanceTests.swift | 4 ++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6be472..6b5d52c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- End a root-level block scalar at a `---` or `...` document marker in column 0. + Now that a root block scalar can take indentation 0, its content loop reaches + column-0 lines, so a marker line must end the scalar instead of being folded + into it. Without this a `--- |` literal swallowed the following `...`, the next + directives, and later documents. Surpasses libyaml on W4TN. - Fold over a tab-only blank line inside a multi-line plain scalar. A plain scalar may span blank lines, and a blank line that contains a tab is still blank, so the tab is not indentation. libyaml rejects any tab below the scalar diff --git a/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift b/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift index 81b0bc4..50f64cc 100644 --- a/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift +++ b/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift @@ -32,6 +32,12 @@ extension PureYAML.Parsing.LibYAML.Scanner { try scanBlockScalarBreaks(indent: &indent, state: &state, startMark: startMark, endMark: &endMark) while reader.column == indent, reader.peek() != nil { + // A root block scalar (indent 0) ends at a `---`/`...` document marker + // in column 0. libyaml never needs this because it floors the indent + // at 1, but indent 0 lets the loop reach a column-0 marker line. + if reader.column == 0, documentMarkerAhead() { + break + } foldBlockLine(&state, literal: literal) while let character = reader.peek(), !Classify.isBreakZ(character) { state.string.unicodeScalars.append(character) @@ -159,6 +165,16 @@ extension PureYAML.Parsing.LibYAML.Scanner { return "\n" } + /// Whether a `---` or `...` document marker (three repeats then a blank, break, + /// or end of input) starts at the current position. + private func documentMarkerAhead() -> Bool { + let first = reader.peek() + guard first == "-" || first == "." else { + return false + } + return reader.peek(1) == first && reader.peek(2) == first && Classify.isBlankZ(reader.peek(3)) + } + private func digitValue(_ scalar: Unicode.Scalar?) -> Int { guard let scalar, scalar >= "0", scalar <= "9" else { return 0 diff --git a/Tests/CanonicalEventConformanceTests.swift b/Tests/CanonicalEventConformanceTests.swift index 31e6b6e..786f131 100644 --- a/Tests/CanonicalEventConformanceTests.swift +++ b/Tests/CanonicalEventConformanceTests.swift @@ -12,7 +12,7 @@ struct CanonicalEventConformanceTests { /// Valid suite cases whose event stream PureYAML does NOT yet emit exactly. /// This is the authoritative gap set: a fix removes an id (and the test fails /// until it is removed), a regression adds one (and the test fails). Of these, - /// All remaining gaps emit a wrong event stream (none throw on valid input). + /// The one remaining gap emits a wrong event stream (it does not throw). static let knownEventGaps: Set = [ // Gap set for the ported libyaml engine. Verified against the libyaml // reference dumper: on all but four of these the port matches libyaml @@ -22,7 +22,7 @@ struct CanonicalEventConformanceTests { // // wrong event stream: the port matches libyaml but both deviate from the // canonical events (libyaml quirks to be surpassed). - "JEF9/02", "W4TN", + "JEF9/02", ] @Test("Every valid case emits canonical events, except the recorded gaps")