diff --git a/CHANGELOG.md b/CHANGELOG.md index c6bcd71..ee5ea65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift b/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift index 9685c19..05def72 100644 --- a/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift +++ b/Sources/Parsing/LibYAML.Scanner.BlockScalar.swift @@ -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 - } } } diff --git a/Tests/CanonicalEventConformanceTests.swift b/Tests/CanonicalEventConformanceTests.swift index 2d4b87b..5237e00 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, - /// 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 = [ // 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")