feat(parsing): port libyaml flow-collection fetchers to Swift

Stage 3 of the faithful libyaml scanner port, continued. Translates fetch_flow_collection_start, fetch_flow_collection_end, and fetch_flow_entry (the [ ] { } , indicators), with the flow-level push/pop already ported. Combined with the plain-scalar scanner's flow-aware termination, the port now tokenizes flow sequences and mappings of plain scalars, and the simple-key splice promotes a flow mapping key. Unit-tested for [a, b] and {k: v}. Remaining unported fetchers: quoted/block scalars, anchors, tags, directives. 307 tests, no regression.
This commit is contained in:
Mihaela Mihaljevic 2026-06-24 15:16:45 +02:00
parent 43b20ab7bc
commit 27514466d0
3 changed files with 81 additions and 1 deletions

View File

@ -90,6 +90,9 @@ extension PureYAML.Parsing.LibYAML.Scanner {
if let unported = unportedConstruct(character) {
throw notPorted(unported)
}
if try fetchFlowToken(character) {
return
}
switch character {
case "-" where Classify.isBlankZ(reader.peek(1)):
fetchBlockEntry()
@ -102,10 +105,23 @@ extension PureYAML.Parsing.LibYAML.Scanner {
}
}
/// Fetch a flow-collection indicator (`[` `]` `{` `}` `,`); returns false if
/// the character is not one.
private mutating func fetchFlowToken(_ character: Unicode.Scalar) throws -> Bool {
switch character {
case "[": fetchFlowCollectionStart(.flowSequenceStart)
case "{": fetchFlowCollectionStart(.flowMappingStart)
case "]": try fetchFlowCollectionEnd(.flowSequenceEnd)
case "}": try fetchFlowCollectionEnd(.flowMappingEnd)
case ",": try fetchFlowEntry()
default: return false
}
return true
}
/// The constructs whose fetchers are not yet ported, named for the error.
private func unportedConstruct(_ character: Unicode.Scalar) -> String? {
switch character {
case "[", "]", "{", "}", ",": "flow collection"
case "*", "&": "anchor or alias"
case "!": "tag"
case "|" where flowLevel == 0, ">" where flowLevel == 0: "block scalar"
@ -114,6 +130,36 @@ extension PureYAML.Parsing.LibYAML.Scanner {
}
}
/// Port of `yaml_parser_fetch_flow_collection_start`.
private mutating func fetchFlowCollectionStart(_ type: PureYAML.Parsing.LibYAML.TokenType) {
// saveSimpleKey cannot throw in flow: a required key needs block context.
try? saveSimpleKey()
increaseFlowLevel()
simpleKeyAllowed = true
let startMark = reader.mark
reader.skip()
indentEngine.enqueue(Token(type: type, startMark: startMark, endMark: reader.mark))
}
/// Port of `yaml_parser_fetch_flow_collection_end`.
private mutating func fetchFlowCollectionEnd(_ type: PureYAML.Parsing.LibYAML.TokenType) throws {
try removeSimpleKey()
decreaseFlowLevel()
simpleKeyAllowed = false
let startMark = reader.mark
reader.skip()
indentEngine.enqueue(Token(type: type, startMark: startMark, endMark: reader.mark))
}
/// Port of `yaml_parser_fetch_flow_entry`.
private mutating func fetchFlowEntry() throws {
try removeSimpleKey()
simpleKeyAllowed = true
let startMark = reader.mark
reader.skip()
indentEngine.enqueue(Token(type: .flowEntry, startMark: startMark, endMark: reader.mark))
}
/// Port of `yaml_parser_fetch_stream_start`.
private mutating func fetchStreamStart() {
// indent is already -1 and simpleKeys already holds the block-context slot.

View File

@ -11,6 +11,11 @@ extension PureYAML.Parsing.LibYAML {
case blockMappingStart
case blockEnd
case blockEntry
case flowSequenceStart
case flowMappingStart
case flowSequenceEnd
case flowMappingEnd
case flowEntry
case key
case value
case scalar(value: String, style: PureYAML.Parsing.ScalarStyle)

View File

@ -80,4 +80,33 @@ struct LibYAMLScannerFetchTests {
.streamEnd,
])
}
@Test("a flow sequence of plain scalars")
func test_flowSequence() throws {
let tokens = try tokenize("[a, b]\n")
#expect(tokens == [
.streamStart,
.flowSequenceStart,
.scalar(value: "a", style: .plain),
.flowEntry,
.scalar(value: "b", style: .plain),
.flowSequenceEnd,
.streamEnd,
])
}
@Test("a flow mapping with a key and value")
func test_flowMapping() throws {
let tokens = try tokenize("{k: v}\n")
#expect(tokens == [
.streamStart,
.flowMappingStart,
.key,
.scalar(value: "k", style: .plain),
.value,
.scalar(value: "v", style: .plain),
.flowMappingEnd,
.streamEnd,
])
}
}