From 57adb14f1532e56e0585ec419860de4d9cd82730 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 24 Jun 2026 21:44:27 +0200 Subject: [PATCH] fix(parsing): reject a block sequence that opens mid-line A block sequence must begin its own line. A '-' entry mid-line after an inline mapping value ('key: - a') or a node property ('&anchor - x') is invalid, but libyaml accepts both. A '-' after another '-' is still a valid compact sequence ('- - a'), and an explicit key whose ':' is on a later line may still open a sequence ('? k' / ': - a'). In fetchBlockEntry, when no line break was crossed, reject if the last token is an anchor/tag, or a value that closed an inline implicit key (told apart by a pending simple key: an explicit key's simple key has gone stale across its line break). Extract the scanner guard helpers into LibYAML.Scanner.Guards to stay within the file-length limit. Surpasses libyaml on 5U3A, SY6V; whole-suite conformance 396/402, canonical events 308/308. --- CHANGELOG.md | 6 +++ Sources/Parsing/LibYAML.Scanner.Fetch.swift | 55 ++++++------------- Sources/Parsing/LibYAML.Scanner.Guards.swift | 57 ++++++++++++++++++++ Sources/Parsing/LibYAML.Scanner.swift | 5 ++ Tests/Fixtures/yaml-test-suite-manifest.yaml | 8 +-- 5 files changed, 87 insertions(+), 44 deletions(-) create mode 100644 Sources/Parsing/LibYAML.Scanner.Guards.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index d4eb133..d4c9367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Reject a block sequence that opens on the same line as an earlier node. A `-` + entry mid-line after an inline mapping value (`key: - a`) or a node property + (`&anchor - x`) is invalid, while a `-` after another `-` stays a valid compact + sequence (`- - a`) and an explicit key's value on a later line (`? k`\n`: - a`) + stays valid. The inline case is told apart by a pending simple key (an explicit + key's has gone stale across the line break). Surpasses libyaml on 5U3A and SY6V. - Reject a flow-collection continuation line that is not indented past the parent block. YAML requires multi-line flow content to be more indented than the enclosing block context, so `flow: [a,` / `b]` (with `b` in column 0) and the diff --git a/Sources/Parsing/LibYAML.Scanner.Fetch.swift b/Sources/Parsing/LibYAML.Scanner.Fetch.swift index 8f92dbc..0a28372 100644 --- a/Sources/Parsing/LibYAML.Scanner.Fetch.swift +++ b/Sources/Parsing/LibYAML.Scanner.Fetch.swift @@ -107,46 +107,6 @@ extension PureYAML.Parsing.LibYAML.Scanner { } } - /// Reject a flow-collection continuation line that is not indented past the - /// enclosing block context. YAML requires `s-flow-line-prefix` to be more - /// indented than the parent block; libyaml does not enforce it, so multi-line - /// flow content in column 0 (`[a,\nb]` under a key) wrongly parses. - private mutating func checkFlowContinuationIndent() throws { - guard flowLevel > 0, crossedLineBreak, let next = reader.peek() else { - return - } - // A closing `]`/`}` (or a `,` separator) may sit at any indentation; only - // flow CONTENT must be indented past the parent block. - if next == "]" || next == "}" || next == "," { - return - } - if reader.column <= indentEngine.indent { - throw ScannerError( - context: "while parsing a flow node", - problem: "found insufficiently indented line in a flow collection", - mark: reader.mark, - ) - } - } - - /// The error raised when a tab is used where indentation is required (before a - /// block indicator or an implicit mapping key). A tab is valid separation but - /// never indentation. - private func tabIndentationError() -> PureYAML.Parsing.LibYAML.ScannerError { - ScannerError( - context: "while scanning for the next token", - problem: "found a tab character where an indentation space is expected", - mark: reader.mark, - ) - } - - /// Whether this flow level's pending simple key is a JSON (quoted) node, which - /// lets an adjacent `:` open its value without a separating space. - private var flowKeyIsJSON: Bool { - let key = simpleKeys[simpleKeys.count - 1] - return key.possible && key.isJSON - } - /// Fetch a flow-collection indicator (`[` `]` `{` `}` `,`); returns false /// otherwise. private mutating func fetchFlowToken(_ character: Unicode.Scalar) throws -> Bool { @@ -308,6 +268,17 @@ extension PureYAML.Parsing.LibYAML.Scanner { if tabbedSeparation { throw tabIndentationError() } + // A block sequence must begin its own line. A `-` mid-line after a mapping + // value (`key: - a`) or a node property (`&a - x`) is invalid, but a `-` + // after another `-` is a valid compact sequence (`- - a`). libyaml accepts + // all three. + if flowLevel == 0, !crossedLineBreak, lastTokenForbidsInlineSequence() { + throw ScannerError( + context: "while scanning a node", + problem: "found a block sequence on the same line as an earlier node", + mark: reader.mark, + ) + } if flowLevel == 0 { indentEngine.rollIndent(column: reader.column, number: -1, type: .blockSequenceStart, mark: reader.mark) } @@ -338,6 +309,10 @@ extension PureYAML.Parsing.LibYAML.Scanner { /// rolls the block mapping indent to the key's column. private mutating func fetchValue() throws { let top = simpleKeys.count - 1 + // A pending simple key means this `:` closes an inline implicit key (key + // and `:` on one line). An explicit `?` key's `:` has no pending simple key + // (it went stale across the line break), so its value may open a sequence. + lastValueWasInline = simpleKeys[top].possible if simpleKeys[top].possible { if simpleKeys[top].tabbed { throw tabIndentationError() diff --git a/Sources/Parsing/LibYAML.Scanner.Guards.swift b/Sources/Parsing/LibYAML.Scanner.Guards.swift new file mode 100644 index 0000000..40c0ed5 --- /dev/null +++ b/Sources/Parsing/LibYAML.Scanner.Guards.swift @@ -0,0 +1,57 @@ +extension PureYAML.Parsing.LibYAML.Scanner { + private typealias ScannerError = PureYAML.Parsing.LibYAML.ScannerError + + /// Reject a flow-collection continuation line that is not indented past the + /// enclosing block context. YAML requires `s-flow-line-prefix` to be more + /// indented than the parent block; libyaml does not enforce it, so multi-line + /// flow content in column 0 (`[a,\nb]` under a key) wrongly parses. + mutating func checkFlowContinuationIndent() throws { + guard flowLevel > 0, crossedLineBreak, let next = reader.peek() else { + return + } + // A closing `]`/`}` (or a `,` separator) may sit at any indentation; only + // flow CONTENT must be indented past the parent block. + if next == "]" || next == "}" || next == "," { + return + } + if reader.column <= indentEngine.indent { + throw ScannerError( + context: "while parsing a flow node", + problem: "found insufficiently indented line in a flow collection", + mark: reader.mark, + ) + } + } + + /// Whether the last emitted token bars a block sequence from opening on the + /// same line: a mapping value indicator or a node property (anchor/tag). After + /// another block entry a compact sequence is allowed, so that is not barred. + func lastTokenForbidsInlineSequence() -> Bool { + switch indentEngine.tokens.last?.type { + case .anchor, .tag: + true + case .value: + lastValueWasInline + default: + false + } + } + + /// The error raised when a tab is used where indentation is required (before a + /// block indicator or an implicit mapping key). A tab is valid separation but + /// never indentation. + func tabIndentationError() -> PureYAML.Parsing.LibYAML.ScannerError { + ScannerError( + context: "while scanning for the next token", + problem: "found a tab character where an indentation space is expected", + mark: reader.mark, + ) + } + + /// Whether this flow level's pending simple key is a JSON (quoted) node, which + /// lets an adjacent `:` open its value without a separating space. + var flowKeyIsJSON: Bool { + let key = simpleKeys[simpleKeys.count - 1] + return key.possible && key.isJSON + } +} diff --git a/Sources/Parsing/LibYAML.Scanner.swift b/Sources/Parsing/LibYAML.Scanner.swift index 290e24c..abc0344 100644 --- a/Sources/Parsing/LibYAML.Scanner.swift +++ b/Sources/Parsing/LibYAML.Scanner.swift @@ -22,6 +22,11 @@ extension PureYAML.Parsing.LibYAML { /// flow context a continuation line must be indented past the parent block, /// so a token on a new line at or below that indent is an error. var crossedLineBreak = false + /// Whether the most recent `:` value indicator sat on the same line as its + /// key (an inline implicit-key value). Such a value may not open a block + /// sequence on that line (`key: - a`), whereas an explicit key whose `:` is + /// on a later line may (`? k`\n`: - a`). + var lastValueWasInline = false /// `parser->mark`, kept in sync with the reader at each fetch step. var mark: PureYAML.Parsing.Mark = .start /// `parser->stream_start_produced`. diff --git a/Tests/Fixtures/yaml-test-suite-manifest.yaml b/Tests/Fixtures/yaml-test-suite-manifest.yaml index 64c49be..bf861bb 100644 --- a/Tests/Fixtures/yaml-test-suite-manifest.yaml +++ b/Tests/Fixtures/yaml-test-suite-manifest.yaml @@ -10,8 +10,8 @@ source: # oracle: json if in.json present, else none # status: passing (PureYAML conformant today) | gap (not yet conformant) total: 402 -passing: 394 -gap: 8 +passing: 396 +gap: 6 cases: - id: "229Q" name: "Spec Example 2.4. Sequence of Mappings" @@ -530,7 +530,7 @@ cases: localPath: yaml-test-suite/5U3A/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 18 lineCount: 2 - id: "5WE3" @@ -2738,7 +2738,7 @@ cases: localPath: yaml-test-suite/SY6V/in.yaml expected: reject oracle: none - status: gap + status: passing byteCount: 25 lineCount: 1 - id: "SYW4"