Files
swift-mirror/tools/SourceKit/tools/swift-lang/SwiftLang.swift
Alex Hoppen 749a13a6fe [libSyntax] Remove the C++ SyntaxClassifier
The recommended way forward is to use the SyntaxClassifier on the Swift
side.

By removing the C++ SyntaxClassifier, we can also eliminate the
-force-libsyntax-based-processing option that was used to bootstrap
incremental parsing and would generate the syntax map from a syntax
tree.
2018-07-30 09:26:18 -07:00

81 lines
3.1 KiB
Swift

//===--------------------- SwiftLang.swift -------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This file provides Swift language support by invoking SourceKit internally.
//===----------------------------------------------------------------------===//
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 {
fileprivate static func parse(content: String, name: String, isURL: Bool) throws -> String {
let Service = SourceKitdService()
let Request = SourceKitdRequest(uid: .request_EditorOpen)
if isURL {
Request.addParameter(.key_SourceFile, value: content)
} else {
Request.addParameter(.key_SourceText, value: content)
}
Request.addParameter(.key_Name, value: name)
Request.addParameter(.key_SyntaxTreeTransferMode,
value: .kind_SyntaxTreeFull)
Request.addParameter(.key_EnableSyntaxMap, value: 0)
Request.addParameter(.key_EnableStructure, value: 0)
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)
}
}