File tree Expand file tree Collapse file tree 7 files changed +28
-24
lines changed Expand file tree Collapse file tree 7 files changed +28
-24
lines changed Original file line number Diff line number Diff line change 6
6
import StdlibUnittest
7
7
import Foundation
8
8
import SwiftSyntax
9
+ import SwiftSourceKit
9
10
10
11
func getInput( _ file: String ) -> URL {
11
12
var result = URL ( fileURLWithPath: #file)
Original file line number Diff line number Diff line change 6
6
import Foundation
7
7
import StdlibUnittest
8
8
import SwiftSyntax
9
+ import SwiftSourceKit
9
10
10
11
var ParseFile = TestSuite ( " ParseFile " )
11
12
@@ -30,7 +31,8 @@ ParseFile.test("ParseSingleFile") {
30
31
let currentFile = URL ( fileURLWithPath: #file)
31
32
expectDoesNotThrow ( {
32
33
let currentFileContents = try String ( contentsOf: currentFile)
33
- let parsed = try SourceFileSyntax . parse ( currentFile)
34
+ let parsed = SourceFileSyntax . decodeSourceFileSyntax ( try SourceKitdService .
35
+ encodeSourceFileSyntax ( currentFile) )
34
36
expectEqual ( " \( parsed) " , currentFileContents)
35
37
} )
36
38
}
Original file line number Diff line number Diff line change 6
6
import StdlibUnittest
7
7
import Foundation
8
8
import SwiftSyntax
9
+ import SwiftSourceKit
9
10
10
11
func getInput( _ file: String ) -> URL {
11
12
var result = URL ( fileURLWithPath: #file)
@@ -26,7 +27,8 @@ VisitorTests.test("Basic") {
26
27
}
27
28
}
28
29
expectDoesNotThrow ( {
29
- let parsed = try SourceFileSyntax . parse ( getInput ( " visitor.swift " ) )
30
+ let parsed = SourceFileSyntax . decodeSourceFileSyntax ( try SourceKitdService .
31
+ encodeSourceFileSyntax ( getInput ( " visitor.swift " ) ) )
30
32
let counter = FuncCounter ( )
31
33
let hashBefore = parsed. hashValue
32
34
counter. visit ( parsed)
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ if(SWIFT_HOST_VARIANT STREQUAL "macosx")
35
35
# SwiftSyntax depends on both the standard library (because it's a
36
36
# Swift module), and the SDK overlays (because it depends on Foundation).
37
37
# Ensure we only build SwiftSyntax when we're building both.
38
- if (BUILD_SOURCEKIT_SWIFT_CLIENT )
38
+ if (SWIFT_BUILD_STDLIB AND SWIFT_BUILD_SDK_OVERLAY )
39
39
add_subdirectory (SwiftSyntax )
40
40
endif ()
41
41
endif ()
Original file line number Diff line number Diff line change @@ -26,3 +26,22 @@ public class SourceKitdService {
26
26
return SourceKitdResponse ( resp: sourcekitd_send_request_sync ( request. rawRequest) )
27
27
}
28
28
}
29
+
30
+ extension SourceKitdService {
31
+ /// Parses the Swift file at the provided URL into a `Syntax` tree in Json
32
+ /// serialization format by querying SourceKitd service.
33
+ /// - Parameter url: The URL you wish to parse.
34
+ /// - Returns: The syntax tree in Json format string.
35
+ public static func encodeSourceFileSyntax( _ url: URL ) throws -> String {
36
+ let Service = SourceKitdService ( )
37
+ let Request = SourceKitdRequest ( uid: . source_request_editor_open)
38
+ let Path = url. path
39
+ Request . addParameter ( . key_sourcefile, value: Path)
40
+ Request . addParameter ( . key_name, value: Path)
41
+ Request . addParameter ( . key_enable_syntax_tree, value: 1 )
42
+
43
+ // FIXME: SourceKitd error handling.
44
+ let Resp = Service . sendSyn ( request: Request)
45
+ return Resp . value. getString ( . key_serialized_syntax_tree)
46
+ }
47
+ }
Original file line number Diff line number Diff line change 1
- set (EXTRA_COMPILE_FLAGS "-F" "${SWIFT_LIBRARY_OUTPUT_INTDIR} " )
2
- set (EXTRA_LINKER_FLAGS "-Xlinker" "-rpath" "-Xlinker" "${SWIFT_LIBRARY_OUTPUT_INTDIR} "
3
- "-Xlinker" "-F" "-Xlinker" "${SWIFT_LIBRARY_OUTPUT_INTDIR} " )
4
-
5
1
add_swift_library (swiftSwiftSyntax SHARED
6
2
# This file should be listed the first. Module name is inferred from the
7
3
# filename.
@@ -27,9 +23,7 @@ add_swift_library(swiftSwiftSyntax SHARED
27
23
TokenKind.swift.gyb
28
24
Trivia.swift
29
25
30
- SWIFT_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
31
- LINK_FLAGS ${EXTRA_LINKER_FLAGS}
32
- SWIFT_MODULE_DEPENDS SwiftSourceKit
26
+ SWIFT_MODULE_DEPENDS Foundation
33
27
INSTALL_IN_COMPONENT swift-syntax
34
28
TARGET_SDKS OSX
35
29
IS_STDLIB )
Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ import Foundation
16
16
17
17
#if os(macOS)
18
18
import Darwin
19
- import SwiftSourceKit
20
19
#elseif os(Linux)
21
20
import Glibc
22
21
#endif
@@ -30,25 +29,12 @@ public enum ParserError: Error {
30
29
31
30
extension Syntax {
32
31
fileprivate static func encodeSourceFileSyntaxInternal( _ url: URL ) throws -> Data {
33
- #if os(macOS)
34
- let Service = SourceKitdService ( )
35
- let Request = SourceKitdRequest ( uid: . source_request_editor_open)
36
- let Path = url. path
37
- Request . addParameter ( . key_sourcefile, value: Path)
38
- Request . addParameter ( . key_name, value: Path)
39
- Request . addParameter ( . key_enable_syntax_tree, value: 1 )
40
-
41
- // FIXME: SourceKitd error handling.
42
- let Resp = Service . sendSyn ( request: Request)
43
- return Resp . value. getString ( . key_serialized_syntax_tree) . data ( using: . utf8) !
44
- #else
45
32
let swiftcRunner = try SwiftcRunner ( sourceFile: url)
46
33
let result = try swiftcRunner. invoke ( )
47
34
guard result. wasSuccessful else {
48
35
throw ParserError . swiftcFailed ( result. exitCode, result. stderr)
49
36
}
50
37
return result. stdoutData
51
- #endif
52
38
}
53
39
54
40
/// Parses the Swift file at the provided URL into a `Syntax` tree in Json
You can’t perform that action at this time.
0 commit comments