diff --git a/Sources/Parsing/LibYAML.Scanner.Fetch.swift b/Sources/Parsing/LibYAML.Scanner.Fetch.swift index 0ef9ca4..33a8799 100644 --- a/Sources/Parsing/LibYAML.Scanner.Fetch.swift +++ b/Sources/Parsing/LibYAML.Scanner.Fetch.swift @@ -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. diff --git a/Sources/Parsing/LibYAML.TokenType.swift b/Sources/Parsing/LibYAML.TokenType.swift index 7d04fae..bb66402 100644 --- a/Sources/Parsing/LibYAML.TokenType.swift +++ b/Sources/Parsing/LibYAML.TokenType.swift @@ -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) diff --git a/Tests/LibYAMLScannerFetchTests.swift b/Tests/LibYAMLScannerFetchTests.swift index e3a0e90..90a2cd0 100644 --- a/Tests/LibYAMLScannerFetchTests.swift +++ b/Tests/LibYAMLScannerFetchTests.swift @@ -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, + ]) + } }