Port the located value tree onto the hand-rolled engine, reusing the
engine-agnostic located layer (Model.LocatedValue, LocatedPair,
SourcePosition, SourceRange, and the located composer helper) authored
on the conformance branch. The streaming composer now builds a parallel
located tree behind an opt-in tracksLocations flag, exposed as
parseLocated / parseStreamLocated; the plain Value path stays
byte-identical, gated behind the flag.
The scalar event carries the scanner token's end mark, so a located
scalar spans the whole token (end exclusive, one past the last
character) rather than being start-anchored. The hand-rolled scanner
already tracked token end marks, so this surfaces information the engine
was already computing.
Also stop emitting a spurious null document for a comment-only or empty
stream: YAML 1.2 gives such a stream zero documents, while the
single-document value API still maps it to null, so parse("") is
unchanged.
A whole-suite test proves the located projection equals a plain parse
for every passing case; the value, event-dump, and conformance suites
stay green (507 tests).
74 lines
2.4 KiB
Swift
74 lines
2.4 KiB
Swift
extension PureYAML.Parsing.Parser {
|
|
func parseEvents(_ yaml: String) throws -> [PureYAML.Parsing.Event] {
|
|
let tokens = try PureYAML.Parsing.Scanner().scan(yaml)
|
|
var parser = PureYAML.Parsing.TokenEventParser(tokens: tokens, scalarParser: self)
|
|
return try parser.parse()
|
|
}
|
|
|
|
func parseStreaming(_ yaml: String) throws -> PureYAML.Model.Value {
|
|
var composer = PureYAML.Parsing.EventStreamComposer(
|
|
scalarParser: self,
|
|
mode: .singleDocument,
|
|
)
|
|
var parser = PureYAML.Parsing.TokenEventParser(
|
|
yaml: yaml,
|
|
scalarParser: self,
|
|
eventSink: { event in
|
|
try composer.consume(event)
|
|
},
|
|
)
|
|
try parser.parseStreaming()
|
|
return try composer.composedValue()
|
|
}
|
|
|
|
func parseStreamStreaming(_ yaml: String) throws -> [PureYAML.Stream.Document] {
|
|
var composer = PureYAML.Parsing.EventStreamComposer(
|
|
scalarParser: self,
|
|
mode: .stream,
|
|
)
|
|
var parser = PureYAML.Parsing.TokenEventParser(
|
|
yaml: yaml,
|
|
scalarParser: self,
|
|
eventSink: { event in
|
|
try composer.consume(event)
|
|
},
|
|
)
|
|
try parser.parseStreaming()
|
|
return try composer.composedStream()
|
|
}
|
|
|
|
func parseLocatedStreaming(_ yaml: String) throws -> PureYAML.Model.LocatedValue {
|
|
var composer = PureYAML.Parsing.EventStreamComposer(
|
|
scalarParser: self,
|
|
mode: .singleDocument,
|
|
tracksLocations: true,
|
|
)
|
|
var parser = PureYAML.Parsing.TokenEventParser(
|
|
yaml: yaml,
|
|
scalarParser: self,
|
|
eventSink: { event in
|
|
try composer.consume(event)
|
|
},
|
|
)
|
|
try parser.parseStreaming()
|
|
return try composer.composedLocated()
|
|
}
|
|
|
|
func parseStreamLocatedStreaming(_ yaml: String) throws -> [PureYAML.Model.LocatedValue] {
|
|
var composer = PureYAML.Parsing.EventStreamComposer(
|
|
scalarParser: self,
|
|
mode: .stream,
|
|
tracksLocations: true,
|
|
)
|
|
var parser = PureYAML.Parsing.TokenEventParser(
|
|
yaml: yaml,
|
|
scalarParser: self,
|
|
eventSink: { event in
|
|
try composer.consume(event)
|
|
},
|
|
)
|
|
try parser.parseStreaming()
|
|
return try composer.composedLocatedStream()
|
|
}
|
|
}
|