-
Notifications
You must be signed in to change notification settings - Fork 440
Update readme #363
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
Update readme #363
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Contributing | ||
|
||
## Building SwiftSyntax from `main` | ||
|
||
Since SwiftSyntax relies on definitions in the main Swift repository to generate the layout of the syntax tree using `gyb`, a checkout of [apple/swift](https://github.com/apple/swift) is still required to build the latest development snapshot of SwiftSyntax. | ||
|
||
To build the `main` branch of SwiftSyntax, follow the following instructions: | ||
|
||
1. Check `swift-syntax` and `swift` out side by side: | ||
``` | ||
- (enclosing directory) | ||
- swift | ||
- swift-syntax | ||
``` | ||
|
||
2. Make sure you have a recent [Trunk Swift Toolchain](https://swift.org/download/#snapshots) installed. | ||
3. Define the `TOOLCHAINS` environment variable as below to have the `swift` command point inside the toolchain: | ||
|
||
```bash | ||
$ export TOOLCHAINS=swift | ||
``` | ||
|
||
4. To make sure everything is set up correctly, check the return statement of `xcrun --find swift`. It should point inside the latest installed trunk development toolchain. If it points inside an Xcode toolchain, check that you exported the `TOOLCHAINS` environment variable correctly. If it points inside a version-specific toolchain (like Swift 5.0-dev), you'll need to remove that toolchain. | ||
5. Run `swift-syntax/build-script.py`. | ||
If despite following those instructions, you get compiler errors, the Swift toolchain might be too old to contain recent changes in Swift's SwiftSyntaxParser C library. In that case, you'll have to build the compiler and SwiftSyntax together with the following command: | ||
|
||
```bash | ||
$ swift/utils/build-script --swiftsyntax --swiftpm --llbuild | ||
``` | ||
|
||
Swift-CI will automatically run the code generation step whenever a new toolchain (development snapshot or release) is published. It should thus almost never be necessary to perform the above build yourself. | ||
|
||
Afterward, SwiftPM can also generate an Xcode project to develop SwiftSyntax by running `swift package generate-xcodeproj`. | ||
|
||
If you also want to run tests locally, read the section below as testing has additional requirements. | ||
|
||
## Local Testing | ||
|
||
SwiftSyntax uses some test utilities that need to be built as part of the Swift compiler project. To build the most recent version of SwiftSyntax and test it, follow the steps in [swift/README.md](https://github.com/apple/swift/blob/main/README.md) and pass `--llbuild --swiftpm --swiftsyntax` to the build script invocation to build SwiftSyntax and all its dependencies using the current trunk (`main`) compiler. | ||
|
||
SwiftSyntax can then be tested using the build script in `apple/swift` by running | ||
|
||
```bash | ||
swift/utils/build-script --swiftsyntax --swiftpm --llbuild -t --skip-test-cmark --skip-test-swift --skip-test-llbuild --skip-test-swiftpm | ||
``` | ||
|
||
This command will build SwiftSyntax and all its dependencies, tell the build script to run tests, but skip all tests but the SwiftSyntax tests. | ||
|
||
Note that it is not currently supported by SwiftSyntax while building the Swift compiler using Xcode. | ||
|
||
## CI Testing | ||
|
||
Running `@swift-ci Please test` on the main Swift repository will also test the most recent version of SwiftSyntax. | ||
|
||
Testing SwiftSyntax from its own repository is now available by commenting `@swift-ci Please test macOS platform`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Documentation | ||
|
||
## Usage | ||
kimdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Declare SwiftPM dependency with nightly build | ||
|
||
1. Download and install the latest Trunk Development [toolchain](https://swift.org/download/#snapshots). | ||
|
||
2. Define the `TOOLCHAINS` environment variable as below to have the `swift` command point inside the toolchain: | ||
```bash | ||
$ export TOOLCHAINS=swift | ||
``` | ||
|
||
3. To make sure everything is set up correctly, check the result of `xcrun --find swift`. It should point inside the OSS toolchain. | ||
|
||
4. Add this entry to the `Package.swift` manifest of your project: | ||
|
||
```swift | ||
// swift-tools-version:5.3 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyTool", | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-syntax.git", .revision("swift-DEVELOPMENT-SNAPSHOT-2019-02-26")), | ||
], | ||
targets: [ | ||
.target(name: "MyTool", dependencies: ["SwiftSyntax"]), | ||
] | ||
) | ||
``` | ||
|
||
Tags will be created for every nightly build in the form of `swift-DEVELOPMENT-SNAPSHOT-<DATE>`. The revision field | ||
should be specified with the intended tag. | ||
|
||
Different from building SwiftSyntax from source, declaring SwiftSyntax as a SwiftPM dependency doesn't require | ||
the Swift compiler source because we always push gyb-generated files to a tag. | ||
|
||
### Embedding SwiftSyntax in an Application | ||
|
||
SwiftSyntax depends on the `lib_InternalSwiftSyntaxParser.dylib/.so` library which provides a C interface to the underlying Swift C++ parser. When you do `swift build` SwiftSyntax links and uses the library included in the Swift toolchain. If you are building an application make sure to embed `_InternalSwiftSyntaxParser` as part of your application's libraries. | ||
|
||
You can either copy `lib_InternalSwiftSyntaxParser.dylib/.so` directly from the toolchain or even build it yourself from the [Swift repository](https://github.com/apple/swift), as long as you are matching the same tags or branches in both the SwiftSyntax and Swift repositories. To build it for the host os (macOS/linux) use the following steps: | ||
|
||
```bash | ||
git clone https://github.com/apple/swift.git | ||
./swift/utils/update-checkout --clone | ||
./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build | ||
``` | ||
|
||
### Embedding in an iOS Application | ||
|
||
You need to build `lib_InternalSwiftSyntaxParser.dylib` yourself, you cannot copy it from the toolchain. Follow the instructions above and change the invocation of `build-parser-lib` accordingly: | ||
|
||
```bash | ||
./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build-iossim --host iphonesimulator --architectures x86_64 | ||
./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build-ios --host iphoneos --architectures arm64 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import SwiftSyntax | ||
import Foundation | ||
|
||
/// AddOneToIntegerLiterals will visit each token in the Syntax tree, and | ||
/// (if it is an integer literal token) add 1 to the integer and return the | ||
/// new integer literal token. | ||
/// | ||
/// For example will it turn: | ||
/// ``` | ||
/// let x = 2 | ||
/// let y = 3_000 | ||
/// ``` | ||
/// into: | ||
/// ``` | ||
/// let x = 3 | ||
/// let y = 3001 | ||
/// ``` | ||
class AddOneToIntegerLiterals: SyntaxRewriter { | ||
override func visit(_ token: TokenSyntax) -> Syntax { | ||
// Only transform integer literals. | ||
guard case .integerLiteral(let text) = token.tokenKind else { | ||
return Syntax(token) | ||
} | ||
|
||
// Remove underscores from the original text. | ||
let integerText = String(text.filter { ("0"..."9").contains($0) }) | ||
|
||
// Parse out the integer. | ||
let int = Int(integerText)! | ||
|
||
// Create a new integer literal token with `int + 1` as its text. | ||
let newIntegerLiteralToken = token.withKind(.integerLiteral("\(int + 1)")) | ||
|
||
// Return the new integer literal. | ||
return Syntax(newIntegerLiteralToken) | ||
} | ||
} | ||
|
||
let file = CommandLine.arguments[1] | ||
let url = URL(fileURLWithPath: file) | ||
let sourceFile = try SyntaxParser.parse(url) | ||
let incremented = AddOneToIntegerLiterals().visit(sourceFile) | ||
print(incremented) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import SwiftSyntaxBuilder | ||
|
||
/// This example will print the following example: | ||
/// | ||
///``` | ||
/// import Foundation | ||
/// import UIKit | ||
/// class SomeViewController{ | ||
/// let tableView: UITableView | ||
/// } | ||
///``` | ||
|
||
let source = SourceFile { | ||
ImportDecl(path: "Foundation") | ||
ImportDecl(path: "UIKit") | ||
ClassDecl(classOrActorKeyword: .class, identifier: "SomeViewController", membersBuilder: { | ||
VariableDecl(.let, name: "tableView", type: "UITableView") | ||
}) | ||
} | ||
|
||
let syntax = source.buildSyntax(format: Format()) | ||
|
||
var text = "" | ||
syntax.write(to: &text) | ||
|
||
print(text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Examples | ||
|
||
kimdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Command line tool to add one to every integer literal in a source file [AddOneToIntegerLiterals.swift](https://github.com/apple/swift-syntax/blob/main/Examples/AddOneToIntegerLiterals.swift). | ||
- Code-generate a simple source file using SwiftSyntaxBuilder [CodeGenerationUsingSwiftSyntaxBuilder.swift](https://github.com/apple/swift-syntax/blob/95fe6182b755346eee74b499109b4ab9eaf38651/Examples/CodeGenerationUsingSwiftSyntaxBuilder.swift) | ||
|
||
## Some Example Usages | ||
|
||
[**Swift AST Explorer**](https://swift-ast-explorer.kishikawakatsumi.com/): a Swift AST visualizer. | ||
|
||
[**swift-format**](https://github.com/apple/swift-format): formatting technology for Swift source code. | ||
|
||
[**Swift Stress Tester**](https://github.com/apple/swift-stress-tester): a test driver for sourcekitd and Swift evolution. | ||
|
||
[**SwiftSemantics**](https://github.com/SwiftDocOrg/SwiftSemantics): parses Swift code into its constituent declarations. | ||
|
||
[**Sitrep**](https://github.com/twostraws/Sitrep): A source code analyzer for Swift projects | ||
|
||
[**SwiftRewriter**](https://github.com/inamiy/SwiftRewriter): a Swift code formatter. | ||
|
||
[**SwiftPack**](https://github.com/omochi/SwiftPack): a tool for automatically embedding Swift library source. | ||
|
||
[**Periphery**](https://github.com/peripheryapp/periphery): a tool to detect unused code. | ||
|
||
[**BartyCrouch**](https://github.com/Flinesoft/BartyCrouch): a tool to incrementally update strings files to help App localization. | ||
|
||
[**Muter**](https://github.com/muter-mutation-testing/muter): Automated mutation testing for Swift | ||
|
||
[**Swift Variable Injector**](https://github.com/LucianoPAlmeida/variable-injector): a tool to replace string literals with environment variables values. | ||
|
||
[**Pecker**](https://github.com/woshiccm/Pecker): a tool to detect unused code based on [SwiftSyntax](https://github.com/apple/swift-syntax.git) and [IndexStoreDB](https://github.com/apple/indexstore-db.git). | ||
|
||
[**Piranha**](https://github.com/uber/piranha): a tool for refactoring code related to feature flags. | ||
|
||
[**STAR**](https://github.com/thumbtack/star): a tool to find how often specified Swift type(s) are used in a project.## Some Example Users | ||
|
||
[**Swift AST Explorer**](https://swift-ast-explorer.kishikawakatsumi.com/): a Swift AST visualizer. | ||
|
||
[**swift-format**](https://github.com/apple/swift-format): formatting technology for Swift source code. | ||
|
||
[**Swift Stress Tester**](https://github.com/apple/swift-stress-tester): a test driver for sourcekitd and Swift evolution. | ||
|
||
[**SwiftSemantics**](https://github.com/SwiftDocOrg/SwiftSemantics): parses Swift code into its constituent declarations. | ||
|
||
[**Sitrep**](https://github.com/twostraws/Sitrep): A source code analyzer for Swift projects | ||
|
||
[**SwiftRewriter**](https://github.com/inamiy/SwiftRewriter): a Swift code formatter. | ||
|
||
[**SwiftPack**](https://github.com/omochi/SwiftPack): a tool for automatically embedding Swift library source. | ||
|
||
[**Periphery**](https://github.com/peripheryapp/periphery): a tool to detect unused code. | ||
|
||
[**BartyCrouch**](https://github.com/Flinesoft/BartyCrouch): a tool to incrementally update strings files to help App localization. | ||
|
||
[**Muter**](https://github.com/muter-mutation-testing/muter): Automated mutation testing for Swift | ||
|
||
[**Swift Variable Injector**](https://github.com/LucianoPAlmeida/variable-injector): a tool to replace string literals with environment variables values. | ||
|
||
[**Pecker**](https://github.com/woshiccm/Pecker): a tool to detect unused code based on [SwiftSyntax](https://github.com/apple/swift-syntax.git) and [IndexStoreDB](https://github.com/apple/indexstore-db.git). | ||
|
||
[**Piranha**](https://github.com/uber/piranha): a tool for refactoring code related to feature flags. | ||
|
||
[**STAR**](https://github.com/thumbtack/star): a tool to find how often specified Swift type(s) are used in a project. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.