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.
This commit is contained in:
Mihaela Mihaljevic 2026-06-24 19:22:33 +02:00
parent 825084dbff
commit d222b8cc53
3 changed files with 23 additions and 2 deletions

View File

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

View File

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

View File

@ -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<String> = [
// 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")