Skip to content

SwiftSourceKit: add API to generate a syntax tree from a given source buffer. #16927

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 1 commit into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions test/SwiftSyntax/ParseFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ ParseFile.test("ParseSingleFile") {
})
}

ParseFile.test("ParseBuffer") {
expectDoesNotThrow({
let content = "func foo() {}"
let parsed = try SourceFileSyntax.decodeSourceFileSyntax(try
SwiftLang.parse(content))
expectEqual("\(parsed)", content)
})
}

runAllTests()
60 changes: 51 additions & 9 deletions tools/SourceKit/tools/swift-lang/SwiftLang.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,64 @@

import Foundation

enum SourceKitdError: Error, CustomStringConvertible {
case EditorOpenError(message: String)
case EditorCloseError(message: String)

var description: String {
switch self {
case .EditorOpenError(let message):
return "cannot open document: \(message)"
case .EditorCloseError(let message):
return "cannot close document: \(message)"
}
}
}

public class SwiftLang {

/// Parses the Swift file at the provided URL into a `Syntax` tree in Json
/// serialization format by querying SourceKitd service.
/// - Parameter url: The URL you wish to parse.
/// - Returns: The syntax tree in Json format string.
public static func parse(_ url: URL) throws -> String {
fileprivate static func parse(content: String, name: String, isURL: Bool) throws -> String {
let Service = SourceKitdService()
let Request = SourceKitdRequest(uid: .request_EditorOpen)
let Path = url.path
Request.addParameter(.key_SourceFile, value: Path)
Request.addParameter(.key_Name, value: Path)
if isURL {
Request.addParameter(.key_SourceFile, value: content)
} else {
Request.addParameter(.key_SourceText, value: content)
}
Request.addParameter(.key_Name, value: name)
Request.addParameter(.key_EnableSyntaxTree, value: 1)
Request.addParameter(.key_SyntacticOnly, value: 1)

// FIXME: SourceKitd error handling.
let Resp = Service.sendSyn(request: Request)
if Resp.isError {
throw SourceKitdError.EditorOpenError(message: Resp.description)
}

let CloseReq = SourceKitdRequest(uid: .request_EditorClose)
CloseReq.addParameter(.key_Name, value: name)
let CloseResp = Service.sendSyn(request: CloseReq)
if CloseResp.isError {
throw SourceKitdError.EditorCloseError(message: CloseResp.description)
}
return Resp.value.getString(.key_SerializedSyntaxTree)
}
}

/// Parses the Swift file at the provided URL into a `Syntax` tree in Json
/// serialization format by querying SourceKitd service. This function isn't
/// thread safe.
/// - Parameter url: The URL you wish to parse.
/// - Returns: The syntax tree in Json format string.
public static func parse(_ url: URL) throws -> String {
let Path = url.path
return try parse(content: Path, name: Path, isURL: true)
}

/// Parses a given source buffer into a `Syntax` tree in Json serialization
/// format by querying SourceKitd service. This function isn't thread safe.
/// - Parameter source: The source buffer you wish to parse.
/// - Returns: The syntax tree in Json format string.
public static func parse(_ source: String) throws -> String {
return try parse(content: source, name: "foo", isURL: false)
}
}