PureYAML/Sources/Parsing/TokenEventParser.ExplicitKey.swift
Mihaela M 990a2d2e42 fix(parsing): parse an empty explicit key in a flow mapping
An explicit key (`?`) may have no key node: `? : v`, a trailing `? }` / `? ]` /
`? ,`, or `?` at the end of a flow mapping all denote a null key. The parser now
emits an empty key in those positions instead of failing to find a node before
the value indicator or terminator.

Fixes suite case DFF7 (parse-level conformance 316 -> 317); adds tests.
2026-06-29 09:04:29 +02:00

27 lines
1.2 KiB
Swift

extension PureYAML.Parsing.TokenEventParser {
/// The key node of an explicit (`?`) mapping entry. The key may be empty: `? : v`, a trailing
/// `? }` / `? ]` / `? ,`, or `?` at stream end all denote a null key. With no node and no
/// properties to anchor, emit a null scalar rather than failing to find a node.
mutating func parseExplicitMappingKey(
properties: PureYAML.Parsing.NodeProperties,
) throws -> PureYAML.Parsing.NodeResult {
guard properties == .none, isAtEmptyExplicitKey else {
return try parseNode(properties: properties)
}
let mark = cursor.current?.mark ?? cursor.previous?.endMark ?? .start
try emit(.scalar(value: "", anchor: nil, tag: nil, style: .plain, mark: mark))
return PureYAML.Parsing.NodeResult(kind: .scalar, endMark: mark)
}
/// Whether an explicit key has no key node: the next token is the value indicator, a flow
/// terminator (`}`, `]`, `,`), or the stream has ended.
var isAtEmptyExplicitKey: Bool {
switch cursor.current?.kind {
case nil, .mappingValue, .flowMappingEnd, .flowSequenceEnd, .flowEntry:
true
default:
false
}
}
}