diff --git a/Sources/Parsing/LibYAML.Scanner.Anchor.swift b/Sources/Parsing/LibYAML.Scanner.Anchor.swift new file mode 100644 index 0000000..d8d2efa --- /dev/null +++ b/Sources/Parsing/LibYAML.Scanner.Anchor.swift @@ -0,0 +1,37 @@ +extension PureYAML.Parsing.LibYAML.Scanner { + private typealias Classify = PureYAML.Parsing.LibYAML.CharacterClass + + /// Port of `yaml_parser_scan_anchor`. Scans an anchor (`&name`) or alias + /// (`*name`): the name is a run of `IS_ALPHA` characters and must be non-empty + /// and followed by a blank, the end of input, or one of the indicators that may + /// terminate it. + mutating func scanAnchor(isAnchor: Bool) throws -> PureYAML.Parsing.LibYAML.Token { + let startMark = reader.mark + reader.skip() + var name = "" + while let character = reader.peek(), Classify.isAlpha(character) { + name.unicodeScalars.append(character) + reader.skip() + } + let endMark = reader.mark + guard !name.isEmpty, Classify.isBlankZ(reader.peek()) || isAnchorTerminator(reader.peek()) else { + throw PureYAML.Parsing.LibYAML.ScannerError( + context: isAnchor ? "while scanning an anchor" : "while scanning an alias", + problem: "did not find expected alphabetic or numeric character", + mark: startMark, + ) + } + return PureYAML.Parsing.LibYAML.Token( + type: isAnchor ? .anchor(name: name) : .alias(name: name), + startMark: startMark, + endMark: endMark, + ) + } + + private func isAnchorTerminator(_ scalar: Unicode.Scalar?) -> Bool { + switch scalar { + case "?", ":", ",", "]", "}", "%", "@", "`": true + default: false + } + } +} diff --git a/Sources/Parsing/LibYAML.Scanner.Fetch.swift b/Sources/Parsing/LibYAML.Scanner.Fetch.swift index 4d54a6e..34c0cbc 100644 --- a/Sources/Parsing/LibYAML.Scanner.Fetch.swift +++ b/Sources/Parsing/LibYAML.Scanner.Fetch.swift @@ -90,10 +90,7 @@ extension PureYAML.Parsing.LibYAML.Scanner { if let unported = unportedConstruct(character) { throw notPorted(unported) } - if try fetchFlowToken(character) { - return - } - if try fetchQuotedScalar(character) { + if try fetchIndicatorToken(character) { return } switch character { @@ -108,12 +105,19 @@ extension PureYAML.Parsing.LibYAML.Scanner { } } - /// Fetch a single- or double-quoted scalar; returns false if the character is - /// not a quote. - private mutating func fetchQuotedScalar(_ character: Unicode.Scalar) throws -> Bool { + /// Fetch a flow-collection indicator, a quoted scalar, or an anchor/alias; + /// returns false if the character starts none of these. + private mutating func fetchIndicatorToken(_ character: Unicode.Scalar) throws -> Bool { switch character { + case "[": fetchFlowCollectionStart(.flowSequenceStart) + case "{": fetchFlowCollectionStart(.flowMappingStart) + case "]": try fetchFlowCollectionEnd(.flowSequenceEnd) + case "}": try fetchFlowCollectionEnd(.flowMappingEnd) + case ",": try fetchFlowEntry() case "'": try fetchFlowScalar(single: true) case "\"": try fetchFlowScalar(single: false) + case "&": try fetchAnchor(isAnchor: true) + case "*": try fetchAnchor(isAnchor: false) default: return false } return true @@ -128,24 +132,18 @@ extension PureYAML.Parsing.LibYAML.Scanner { indentEngine.enqueue(token) } - /// 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 + /// Port of `yaml_parser_fetch_anchor`. + private mutating func fetchAnchor(isAnchor: Bool) throws { + mark = reader.mark + try saveSimpleKey() + simpleKeyAllowed = false + let token = try scanAnchor(isAnchor: isAnchor) + indentEngine.enqueue(token) } /// The constructs whose fetchers are not yet ported, named for the error. private func unportedConstruct(_ character: Unicode.Scalar) -> String? { switch character { - case "*", "&": "anchor or alias" case "!": "tag" case "|" where flowLevel == 0, ">" where flowLevel == 0: "block scalar" default: nil diff --git a/Sources/Parsing/LibYAML.TokenType.swift b/Sources/Parsing/LibYAML.TokenType.swift index bb66402..5c28cf1 100644 --- a/Sources/Parsing/LibYAML.TokenType.swift +++ b/Sources/Parsing/LibYAML.TokenType.swift @@ -18,6 +18,8 @@ extension PureYAML.Parsing.LibYAML { case flowEntry case key case value + case anchor(name: String) + case alias(name: String) case scalar(value: String, style: PureYAML.Parsing.ScalarStyle) } } diff --git a/Tests/LibYAMLScannerFetchTests.swift b/Tests/LibYAMLScannerFetchTests.swift index ff69126..524e606 100644 --- a/Tests/LibYAMLScannerFetchTests.swift +++ b/Tests/LibYAMLScannerFetchTests.swift @@ -139,4 +139,24 @@ struct LibYAMLScannerFetchTests { .streamEnd, ]) } + + @Test("an anchored scalar value and an alias") + func test_anchorAndAlias() throws { + let tokens = try tokenize("a: &x 1\nb: *x\n") + #expect(tokens == [ + .streamStart, + .blockMappingStart, + .key, + .scalar(value: "a", style: .plain), + .value, + .anchor(name: "x"), + .scalar(value: "1", style: .plain), + .key, + .scalar(value: "b", style: .plain), + .value, + .alias(name: "x"), + .blockEnd, + .streamEnd, + ]) + } }