Swift 编译器:使用第三方导入打印 Swift 文件的 AST
Swift compiler: Printing the AST of a Swift file with 3rd party imports
我正在尝试 print the Abstract Syntax Tree (AST) from a Swift file 使用带有 -print-ast
标志的 Swift 编译器。这没有 Xcode & xcodebuild
.
我无法处理通过 Carthage 构建的第 3 方框架的导入。给定一个包含以下代码的源文件:
来源
import Foundation
import BrightFutures // 3rd party framework
class MyAsyncService {
func asyncFunc() -> Future<String, NSError> {
return Promise<String, NSError>().future
}
}
为 MacOS 编译工作
通过指定框架搜索路径(-F
)以下命令:
swiftc -print-ast Source.swift -F Carthage/Build/Mac
产生预期的输出:
import Foundation
import BrightFutures
internal class MyAsyncService {
internal func asyncFunc() -> Future<String, NSError>
@objc deinit
internal init()
}
为 iOS 编译?
我需要为仅针对 iOS 的项目打印 AST,并为 iOS.
构建依赖项
当我尝试指向为 iOS 构建的框架时:
swiftc -print-ast Source.swift -F Carthage/Build/iOS
命令失败:
Source.swift:2:12: error: module file was created for incompatible target x86_64-apple-ios8.0: [...]/Carthage/Build/iOS/BrightFutures.framework/Modules/BrightFutures.swiftmodule/x86_64.swiftmodule
import BrightFutures // 3rd party framework
^
import Foundation
import BrightFutures
internal class MyAsyncService {
internal func asyncFunc() -> <<error type>>
@objc deinit
internal init()
}
我也尝试过添加 -sdk
标志:
-sdk $(xcrun --sdk iphoneos --show-sdk-path)
到 specify the iphoneos SDK,但这会吐出一个关于不支持的架构的错误。
我怎样才能构建它?
看起来用 Swift 编译器打印 AST 的替代方法是使用 SourceKit.
有一个名为 SourceKitten 的 CLI 工具,它允许您将 Swift 源文件的结构解析为 JSON 格式,而无需处理第 3 方框架和其他框架的导入源文件。
我正在尝试 print the Abstract Syntax Tree (AST) from a Swift file 使用带有 -print-ast
标志的 Swift 编译器。这没有 Xcode & xcodebuild
.
我无法处理通过 Carthage 构建的第 3 方框架的导入。给定一个包含以下代码的源文件:
来源
import Foundation
import BrightFutures // 3rd party framework
class MyAsyncService {
func asyncFunc() -> Future<String, NSError> {
return Promise<String, NSError>().future
}
}
为 MacOS 编译工作
通过指定框架搜索路径(-F
)以下命令:
swiftc -print-ast Source.swift -F Carthage/Build/Mac
产生预期的输出:
import Foundation
import BrightFutures
internal class MyAsyncService {
internal func asyncFunc() -> Future<String, NSError>
@objc deinit
internal init()
}
为 iOS 编译?
我需要为仅针对 iOS 的项目打印 AST,并为 iOS.
构建依赖项当我尝试指向为 iOS 构建的框架时:
swiftc -print-ast Source.swift -F Carthage/Build/iOS
命令失败:
Source.swift:2:12: error: module file was created for incompatible target x86_64-apple-ios8.0: [...]/Carthage/Build/iOS/BrightFutures.framework/Modules/BrightFutures.swiftmodule/x86_64.swiftmodule
import BrightFutures // 3rd party framework
^
import Foundation
import BrightFutures
internal class MyAsyncService {
internal func asyncFunc() -> <<error type>>
@objc deinit
internal init()
}
我也尝试过添加 -sdk
标志:
-sdk $(xcrun --sdk iphoneos --show-sdk-path)
到 specify the iphoneos SDK,但这会吐出一个关于不支持的架构的错误。
我怎样才能构建它?
看起来用 Swift 编译器打印 AST 的替代方法是使用 SourceKit.
有一个名为 SourceKitten 的 CLI 工具,它允许您将 Swift 源文件的结构解析为 JSON 格式,而无需处理第 3 方框架和其他框架的导入源文件。