feat(parsing): port libyaml anchor and alias scanner to Swift
Stage 3 of the faithful libyaml scanner port, continued. Translates fetch_anchor and scan_anchor: an anchor (&name) or alias (*name) whose name is a run of IS_ALPHA characters, validated to be non-empty and followed by a blank, end of input, or a terminating indicator. The flow/quoted/anchor fetchers are consolidated into one indicator dispatch to stay within the complexity limit. Unit-tested for an anchored scalar value and a later alias. Remaining unported fetchers: tags, block scalars, directives. 311 tests, no regression.
This commit is contained in:
parent
4e7ca8fcb2
commit
526abe9998
37
Sources/Parsing/LibYAML.Scanner.Anchor.swift
Normal file
37
Sources/Parsing/LibYAML.Scanner.Anchor.swift
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user