Add `Parser.singleRoot()`

It supports behavior that expected by `YamlParserTests.testParseInvalidStringThrows` in SwiftLint.
This commit is contained in:
Norio Nomura 2017-02-26 22:09:11 +09:00
parent 6b1473bee6
commit 5439041573
No known key found for this signature in database
GPG Key ID: D4A7318EB7F7138D
1 changed files with 13 additions and 2 deletions

View File

@ -39,7 +39,7 @@ public func load_all(yaml: String,
public func load(yaml: String,
_ resolver: Resolver = .default,
_ constructor: Constructor = .default) throws -> Any? {
return try Parser(yaml: yaml, resolver: resolver, constructor: constructor).nextRoot()?.any
return try Parser(yaml: yaml, resolver: resolver, constructor: constructor).singleRoot()?.any
}
/// Parse all YAML documents in a String
@ -70,7 +70,7 @@ public func compose_all(yaml: String,
public func compose(yaml: String,
_ resolver: Resolver = .default,
_ constructor: Constructor = .default) throws -> Node? {
return try Parser(yaml: yaml, resolver: resolver, constructor: constructor).nextRoot()
return try Parser(yaml: yaml, resolver: resolver, constructor: constructor).singleRoot()
}
/// Sequence that holds error
@ -155,6 +155,17 @@ public final class Parser {
}
}
public func singleRoot() throws -> Node? {
if streamEndProduced { return nil }
var node: Node?
if try expectNextEvent(oneOf: [YAML_DOCUMENT_START_EVENT, YAML_STREAM_END_EVENT]) != YAML_STREAM_END_EVENT {
node = try loadNode(from: parse())
try expectNextEvent(oneOf: [YAML_DOCUMENT_END_EVENT])
try expectNextEvent(oneOf: [YAML_STREAM_END_EVENT])
}
return node
}
// MARK: private
fileprivate var anchors = [String: Node]()
fileprivate var parser = yaml_parser_t()