SwiftSyntax: Factor out an API to decode serialized SourceFileSyntax. NFC (#14164)

Since our recent integration with SourceKit, clients may get a SourceFileSyntax
in a serialized form from SourceKit response.  We need an API in the
SwiftSyntax framework to decode this form. This'll be a more common
pattern to get a Swift side syntax tree than invoking swiftc internally.
This commit is contained in:
Xi Ge
2018-01-25 13:40:42 -08:00
committed by GitHub
parent 9c37cb8636
commit d13f83df14

View File

@@ -42,11 +42,27 @@ extension Syntax {
guard result.wasSuccessful else {
throw ParserError.swiftcFailed(result.exitCode, result.stderr)
}
return try decodeSourceFileSyntax(result.stdoutData)
}
/// Decode a serialized form of SourceFileSyntax to a syntax node.
/// - Parameter content: The data of the serialized SourceFileSyntax.
/// - Returns: A top-level Syntax node representing the contents of the tree,
/// if the parse was successful.
fileprivate static func decodeSourceFileSyntax(_ content: Data) throws -> SourceFileSyntax {
let decoder = JSONDecoder()
let raw = try decoder.decode(RawSyntax.self, from: result.stdoutData)
let raw = try decoder.decode(RawSyntax.self, from: content)
guard let file = makeSyntax(raw) as? SourceFileSyntax else {
throw ParserError.invalidFile
}
return file
}
/// Decode a serialized form of SourceFileSyntax to a syntax node.
/// - Parameter content: The string content of the serialized SourceFileSyntax.
/// - Returns: A top-level Syntax node representing the contents of the tree,
/// if the parse was successful.
public static func decodeSourceFileSyntax(_ content: String) throws -> SourceFileSyntax {
return try decodeSourceFileSyntax(content.data(using: .utf8)!)
}
}