PureYAML/Sources/Parsing/LibYAML.Parser.Flow.swift
Mihaela Mihaljevic 521062d974 feat(parsing): port libyaml parser state machine to Swift
Ports parser.c: the one-event-at-a-time state machine that turns the
scanner's token stream into the YAML event stream. Covers stream and
document framing, directive processing with %TAG handle resolution and
default handles, the node property/tag/anchor production, block and
indentless sequences, block mappings, and flow sequences and mappings
including the single-pair `?` mapping forms.

Output verified against the libyaml reference event dumper: the empty
mapping value, %TAG resolution, and shorthand tags all match byte for
byte. 331 tests, no regression.
2026-06-24 17:05:59 +02:00

142 lines
5.2 KiB
Swift

extension PureYAML.Parsing.LibYAML.Parser {
/// Port of `yaml_parser_parse_flow_sequence_entry`. A `?` key inside a flow
/// sequence opens a single-pair flow mapping.
mutating func parseFlowSequenceEntry(first: Bool) throws -> Event {
if first {
marks.append(peekToken().startMark)
skipToken()
}
var token = peekToken()
if !isFlowSequenceEnd(token) {
if !first {
guard isFlowEntry(token) else {
throw ParserError(
context: "while parsing a flow sequence",
problem: "did not find expected ',' or ']'",
mark: marks.popLast() ?? token.startMark,
)
}
skipToken()
token = peekToken()
}
if isKey(token) {
state = .flowSequenceEntryMappingKey
skipToken()
return .mappingStart(anchor: nil, tag: nil, style: .flow, mark: token.startMark)
}
if !isFlowSequenceEnd(token) {
states.append(.flowSequenceEntry)
return try parseNode(block: false, indentlessSequence: false)
}
}
state = states.removeLast()
_ = marks.popLast()
skipToken()
return .sequenceEnd(mark: token.startMark)
}
/// Port of `yaml_parser_parse_flow_sequence_entry_mapping_key`.
mutating func parseFlowSequenceEntryMappingKey() throws -> Event {
let token = peekToken()
if !isValue(token), !isFlowEntry(token), !isFlowSequenceEnd(token) {
states.append(.flowSequenceEntryMappingValue)
return try parseNode(block: false, indentlessSequence: false)
}
if isFlowSequenceEnd(token) {
state = .flowSequenceEntryMappingValue
return processEmptyScalar(at: token.startMark)
}
let mark = token.endMark
skipToken()
state = .flowSequenceEntryMappingValue
return processEmptyScalar(at: mark)
}
/// Port of `yaml_parser_parse_flow_sequence_entry_mapping_value`.
mutating func parseFlowSequenceEntryMappingValue() throws -> Event {
var token = peekToken()
if isValue(token) {
skipToken()
token = peekToken()
if !isFlowEntry(token), !isFlowSequenceEnd(token) {
states.append(.flowSequenceEntryMappingEnd)
return try parseNode(block: false, indentlessSequence: false)
}
}
state = .flowSequenceEntryMappingEnd
return processEmptyScalar(at: token.startMark)
}
/// Port of `yaml_parser_parse_flow_sequence_entry_mapping_end`.
mutating func parseFlowSequenceEntryMappingEnd() throws -> Event {
let token = peekToken()
state = .flowSequenceEntry
return .mappingEnd(mark: token.startMark)
}
/// Port of `yaml_parser_parse_flow_mapping_key`.
mutating func parseFlowMappingKey(first: Bool) throws -> Event {
if first {
marks.append(peekToken().startMark)
skipToken()
}
var token = peekToken()
if !isFlowMappingEnd(token) {
if !first {
guard isFlowEntry(token) else {
throw ParserError(
context: "while parsing a flow mapping",
problem: "did not find expected ',' or '}'",
mark: marks.popLast() ?? token.startMark,
)
}
skipToken()
token = peekToken()
}
if isKey(token) {
return try flowMappingKeyEvent()
}
if !isFlowMappingEnd(token) {
states.append(.flowMappingEmptyValue)
return try parseNode(block: false, indentlessSequence: false)
}
}
state = states.removeLast()
_ = marks.popLast()
skipToken()
return .mappingEnd(mark: token.startMark)
}
/// The KEY branch of `parse_flow_mapping_key`: parse the key node, or an empty
/// scalar when a value/entry/end follows the `?` immediately.
private mutating func flowMappingKeyEvent() throws -> Event {
skipToken()
let token = peekToken()
if !isValue(token), !isFlowEntry(token), !isFlowMappingEnd(token) {
states.append(.flowMappingValue)
return try parseNode(block: false, indentlessSequence: false)
}
state = .flowMappingValue
return processEmptyScalar(at: token.startMark)
}
/// Port of `yaml_parser_parse_flow_mapping_value`.
mutating func parseFlowMappingValue(empty: Bool) throws -> Event {
var token = peekToken()
if empty {
state = .flowMappingKey
return processEmptyScalar(at: token.startMark)
}
if isValue(token) {
skipToken()
token = peekToken()
if !isFlowEntry(token), !isFlowMappingEnd(token) {
states.append(.flowMappingKey)
return try parseNode(block: false, indentlessSequence: false)
}
}
state = .flowMappingKey
return processEmptyScalar(at: token.startMark)
}
}