Skip to content

SwiftSyntax: Single out an API to deserialize SourceFileSyntax. NFC #14164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tools/SwiftSyntax/SwiftSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!)
}
}