fix(parsing): auto-detect indentation 0 for a root block scalar

The block-scalar scanner used 0 as the 'indentation not yet detected'
sentinel and floored the auto-detected indent at 1, mirroring libyaml's
scan_block_scalar_breaks. A root-level block scalar (parent indent -1)
whose content starts in column 0 therefore detected no content, and the
first content line was reparsed as a fresh document, raising 'did not
find expected document start'.

Switch the sentinel to -1 so 0 is a representable detected indent, and
floor the auto-detected indent at parent indent + 1 (0 at the document
root) instead of 1. Nested block scalars keep a parent indent >= 0, so
their floor stays >= 1 and they are unaffected.

Surpasses libyaml on DK3J, FP8R, and M7A3. Port score 283 -> 286.
This commit is contained in:
Mihaela Mihaljevic 2026-06-24 18:36:17 +02:00
parent 3fc3072544
commit 254905fe67
3 changed files with 23 additions and 12 deletions

View File

@ -22,6 +22,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Auto-detect indentation 0 for a root-level block scalar. The block-scalar
scanner used 0 as the "indentation not yet detected" sentinel and floored the
detected indent at 1 (as libyaml does in `scan_block_scalar_breaks`), so a
`--- >` or `--- |` whose content begins in column 0 detected no content and the
following line was misread as a new document ("did not find expected document
start"). The sentinel is now -1, leaving 0 a representable detected indent, and
the floor is the parent indent + 1 (0 at the document root). Surpasses libyaml
on DK3J, FP8R, and M7A3.
- Parse block mappings with empty (null) keys. A `:` value indicator with no
preceding key on its line denotes a mapping entry whose key is empty, which
YAML 1.2 permits but libyaml rejects with "did not find expected key". The

View File

@ -21,7 +21,10 @@ extension PureYAML.Parsing.LibYAML.Scanner {
try consumeBlockHeaderTail(startMark: startMark)
var endMark = reader.mark
var indent = 0
// -1 is the "indentation not yet auto-detected" sentinel. libyaml uses 0,
// but a root-level block scalar legitimately detects indentation 0, so 0
// must be a representable detected value rather than the sentinel.
var indent = -1
if header.increment != 0 {
indent = indentEngine.indent >= 0 ? indentEngine.indent + header.increment : header.increment
}
@ -105,13 +108,13 @@ extension PureYAML.Parsing.LibYAML.Scanner {
var maxIndent = 0
endMark = reader.mark
while true {
while indent == 0 || reader.column < indent, reader.peek() == " " {
while indent < 0 || reader.column < indent, reader.peek() == " " {
reader.skip()
}
if reader.column > maxIndent {
maxIndent = reader.column
}
if indent == 0 || reader.column < indent, Classify.isTab(reader.peek()) {
if indent < 0 || reader.column < indent, Classify.isTab(reader.peek()) {
throw blockError("found a tab character where an indentation space is expected", startMark)
}
if !Classify.isBreak(reader.peek()) {
@ -120,11 +123,11 @@ extension PureYAML.Parsing.LibYAML.Scanner {
state.trailingBreaks += readBlockLine()
endMark = reader.mark
}
if indent == 0 {
if indent < 0 {
// Auto-detected indentation. A root-level block scalar has a parent
// indent of -1, so the floor is 0 (allowing column-0 content), not
// libyaml's 1. maxIndent >= 0 keeps the result non-negative.
indent = max(maxIndent, indentEngine.indent + 1)
if indent < 1 {
indent = 1
}
}
}

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,
/// 22 currently throw on valid input and 3 emit a wrong event stream.
/// 18 currently throw on valid input and 4 emit a wrong event stream.
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,13 +22,13 @@ struct CanonicalEventConformanceTests {
//
// false-rejects (parseEvents throws on valid input, as libyaml does)
"4ABK", "58MP", "5T43", "6BCT", "6CA3",
"96NN/00", "96NN/01", "A2M4", "CFD4", "DBG4", "DK3J", "DK95/00",
"DK95/04", "FP8R", "FRK4", "HM87/00", "M7A3",
"Q5MG", "R4YG", "W4TN",
"96NN/00", "96NN/01", "A2M4", "CFD4", "DBG4", "DK95/00",
"DK95/04", "FRK4", "HM87/00",
"Q5MG", "R4YG",
"Y79Y/001", "Y79Y/010",
// wrong event stream: the port matches libyaml but both deviate from the
// canonical events (libyaml quirks to be surpassed).
"652Z", "HM87/01", "JEF9/02",
"652Z", "HM87/01", "JEF9/02", "W4TN",
]
@Test("Every valid case emits canonical events, except the recorded gaps")