extension PureYAML.Parsing.Scanner { func scanFlowToken( _ character: Character, state: inout State, ) -> Bool { switch character { case "[": openFlowCollection(kind: .sequence, token: .flowSequenceStart, state: &state) return true case "]": closeFlowCollection(token: .flowSequenceEnd, state: &state) return true case "{": openFlowCollection(kind: .mapping, token: .flowMappingStart, state: &state) return true case "}": closeFlowCollection(token: .flowMappingEnd, state: &state) return true case ",": // A flow entry separator abandons any pending simple key and reopens the // door for a fresh one on the next entry (libyaml fetch_flow_entry). removeFlowSimpleKey(&state) scanFixedToken(.flowEntry, state: &state) state.simpleKeyAllowed = true if state.flowContexts.last?.kind == .mapping { state.flowContexts[state.flowContexts.count - 1].isExpectingKey = true } return true default: return false } } /// Opens a flow collection (`[` or `{`). The opener itself may be a simple key /// at the enclosing level, so a key is saved before the flow level rises; a /// fresh simple-key slot is then pushed and a key is allowed inside. func openFlowCollection( kind: PureYAML.Parsing.ScannerFlowContextKind, token: PureYAML.Parsing.TokenKind, state: inout State, ) { saveFlowSimpleKey(&state) // A flow collection opened at block level may itself be a block-mapping key // (`[a, b]: value`), so remember its opener position for a later `:` to // splice a `mappingKey` in front of. When properties precede it (`&a [..]:`) // the key was already saved at the first property and `simpleKeyAllowed` is // now false, so this save is a no-op and the splice lands before the anchor. saveBlockFlowSimpleKey(&state) state.flowDepth += 1 state.flowContexts.append(.init(kind: kind, isExpectingKey: kind == .mapping)) state.flowSimpleKeys.append(nil) scanFixedToken(token, state: &state) state.simpleKeyAllowed = true } /// Closes a flow collection (`]` or `}`): the pending simple key on this level is /// dropped, the level is popped, and no simple key may immediately follow the /// closing indicator (libyaml fetch_flow_collection_end). func closeFlowCollection( token: PureYAML.Parsing.TokenKind, state: inout State, ) { removeFlowSimpleKey(&state) state.flowDepth = max(0, state.flowDepth - 1) scanFixedToken(token, state: &state) _ = state.flowContexts.popLast() _ = state.flowSimpleKeys.popLast() state.simpleKeyAllowed = false } /// Records a potential simple key at the current reader position for the /// innermost flow level, if a simple key is allowed there. Mirrors libyaml's /// `yaml_parser_save_simple_key`: the saved key remembers the index of the /// next token to be appended, so a later `:` can splice a ``mappingKey`` in /// front of the node that starts here. No-op outside flow context. func saveFlowSimpleKey(_ state: inout State) { guard state.flowDepth > 0, state.simpleKeyAllowed, !state.flowSimpleKeys.isEmpty else { return } state.flowSimpleKeys[state.flowSimpleKeys.count - 1] = PureYAML.Parsing.ScannerFlowSimpleKey( tokenIndex: state.tokens.count, mark: state.reader.mark, ) } /// Discards any potential simple key on the innermost flow level. Mirrors /// libyaml's `yaml_parser_remove_simple_key` for the flow context, where a /// simple key is never *required*, so abandoning it is not an error. func removeFlowSimpleKey(_ state: inout State) { guard !state.flowSimpleKeys.isEmpty else { return } state.flowSimpleKeys[state.flowSimpleKeys.count - 1] = nil } /// Promotes a pending flow simple key to an actual mapping key by inserting a /// ``TokenKind/mappingKey`` token at the saved index, mirroring libyaml's /// `fetch_value` splicing a KEY token before a simple key. Returns true when a /// key was inserted so the caller can avoid emitting a redundant one. @discardableResult func promoteFlowSimpleKey(_ state: inout State) -> Bool { guard !state.flowSimpleKeys.isEmpty, let key = state.flowSimpleKeys[state.flowSimpleKeys.count - 1] else { return false } let token = PureYAML.Parsing.Token(kind: .mappingKey, mark: key.mark, endMark: key.mark) let index = min(key.tokenIndex, state.tokens.count) state.tokens.insert(token, at: index) state.flowSimpleKeys[state.flowSimpleKeys.count - 1] = nil return true } /// Records a potential block-level simple key at the current reader position, /// the block-context analogue of ``saveFlowSimpleKey``. Mirrors libyaml's /// `yaml_parser_save_simple_key`: a key is saved at the first node-starting /// token of a block line (a property, or a flow-collection opener) while a /// simple key is allowed. A later `:` on the same line splices a `mappingKey` /// before that token; a node that is plain/quoted is handled by the forward /// lookahead in ``appendMappingKeyIfNeeded`` instead. No-op in flow context or /// once a node has been emitted on the line. func saveBlockFlowSimpleKey(_ state: inout State) { guard state.flowDepth == 0, state.simpleKeyAllowed else { return } state.blockFlowSimpleKey = PureYAML.Parsing.ScannerFlowSimpleKey( tokenIndex: state.tokens.count, mark: state.reader.mark, ) } /// Promotes a block-level flow collection to a block-mapping key by inserting a /// ``TokenKind/mappingKey`` before its opener, the block-context analogue of /// ``promoteFlowSimpleKey``. Also rolls the block indent to the key column so the /// pair nests correctly. Returns true when a key was spliced. No-op when no /// block-level flow collection is pending. @discardableResult func promoteBlockFlowSimpleKey(_ state: inout State) -> Bool { guard let key = state.blockFlowSimpleKey else { return false } // A simple key is limited to a single line (libyaml's stale_simple_keys), so // a `:` on a later line than the flow collection's opener does not promote it: // `[23\n]: 42` is rejected rather than read as a two-line implicit key. guard key.mark.line == state.reader.mark.line else { state.blockFlowSimpleKey = nil return false } rollBlockIndent(toColumn: key.mark.column - 1, state: &state) let token = PureYAML.Parsing.Token(kind: .mappingKey, mark: key.mark, endMark: key.mark) let index = min(key.tokenIndex, state.tokens.count) state.tokens.insert(token, at: index) state.blockFlowSimpleKey = nil return true } /// Discards any pending block-level flow-collection simple key. Called when the /// key is no longer eligible (a new block line starts, or a non-`:` indicator /// follows), mirroring libyaml abandoning a stale simple key. func removeBlockFlowSimpleKey(_ state: inout State) { state.blockFlowSimpleKey = nil } }