Skip to content

Commit 8c46f0b

Browse files
committed
Update readme
1 parent b12be78 commit 8c46f0b

File tree

13 files changed

+297
-241
lines changed

13 files changed

+297
-241
lines changed

CONTRIBUTING.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Contributing
2+
3+
## Building SwiftSyntax from `main`
4+
5+
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.
6+
7+
To build the `main` branch of SwiftSyntax, follow the following instructions:
8+
9+
1. Check `swift-syntax` and `swift` out side by side:
10+
```
11+
- (enclosing directory)
12+
- swift
13+
- swift-syntax
14+
```
15+
16+
2. Make sure you have a recent [Trunk Swift Toolchain](https://swift.org/download/#snapshots) installed.
17+
3. Define the `TOOLCHAINS` environment variable as below to have the `swift` command point inside the toolchain:
18+
19+
```bash
20+
$ export TOOLCHAINS=swift
21+
```
22+
23+
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.
24+
5. Run `swift-syntax/build-script.py`.
25+
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:
26+
27+
```bash
28+
$ swift/utils/build-script --swiftsyntax --swiftpm --llbuild
29+
```
30+
31+
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.
32+
33+
Afterward, SwiftPM can also generate an Xcode project to develop SwiftSyntax by running `swift package generate-xcodeproj`.
34+
35+
If you also want to run tests locally, read the section below as testing has additional requirements.
36+
37+
## Local Testing
38+
39+
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.
40+
41+
SwiftSyntax can then be tested using the build script in `apple/swift` by running
42+
43+
```bash
44+
swift/utils/build-script --swiftsyntax --swiftpm --llbuild -t --skip-test-cmark --skip-test-swift --skip-test-llbuild --skip-test-swiftpm
45+
```
46+
47+
This command will build SwiftSyntax and all its dependencies, tell the build script to run tests, but skip all tests but the SwiftSyntax tests.
48+
49+
Note that it is not currently supported by SwiftSyntax while building the Swift compiler using Xcode.
50+
51+
## CI Testing
52+
53+
Running `@swift-ci Please test` on the main Swift repository will also test the most recent version of SwiftSyntax.
54+
55+
Testing SwiftSyntax from its own repository is now available by commenting `@swift-ci Please test macOS platform`.

Documentation/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Documentation
2+
3+
## Usage
4+
5+
### Declare SwiftPM dependency with nightly build
6+
7+
1. Download and install the latest Trunk Development [toolchain](https://swift.org/download/#snapshots).
8+
9+
2. Define the `TOOLCHAINS` environment variable as below to have the `swift` command point inside the toolchain:
10+
```bash
11+
$ export TOOLCHAINS=swift
12+
```
13+
14+
3. To make sure everything is set up correctly, check the result of `xcrun --find swift`. It should point inside the OSS toolchain.
15+
16+
4. Add this entry to the `Package.swift` manifest of your project:
17+
18+
```swift
19+
// swift-tools-version:5.3
20+
import PackageDescription
21+
22+
let package = Package(
23+
name: "MyTool",
24+
dependencies: [
25+
.package(url: "https://github.com/apple/swift-syntax.git", .revision("swift-DEVELOPMENT-SNAPSHOT-2019-02-26")),
26+
],
27+
targets: [
28+
.target(name: "MyTool", dependencies: ["SwiftSyntax"]),
29+
]
30+
)
31+
```
32+
33+
Tags will be created for every nightly build in the form of `swift-DEVELOPMENT-SNAPSHOT-<DATE>`. The revision field
34+
should be specified with the intended tag.
35+
36+
Different from building SwiftSyntax from source, declaring SwiftSyntax as a SwiftPM dependency doesn't require
37+
the Swift compiler source because we always push gyb-generated files to a tag.
38+
39+
### Embedding SwiftSyntax in an Application
40+
41+
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.
42+
43+
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:
44+
45+
```bash
46+
git clone https://github.com/apple/swift.git
47+
./swift/utils/update-checkout --clone
48+
./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build
49+
```
50+
51+
### Embedding in an iOS Application
52+
53+
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:
54+
55+
```bash
56+
./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build-iossim --host iphonesimulator --architectures x86_64
57+
./swift/utils/build-parser-lib --release --no-assertions --build-dir /tmp/parser-lib-build-ios --host iphoneos --architectures arm64
58+
```

Documentation/SwiftSyntax/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Swift Syntax
2+
3+
SwiftSyntax is a set of Swift bindings for the
4+
[libSyntax](https://github.com/apple/swift/tree/main/lib/Syntax) library. It
5+
allows Swift tools to parse, inspect, generate, and transform Swift source
6+
code.
7+
8+
Its API is designed for performance-critical applications. It uses value types almost exclusively and aims to avoid existential conversions where possible.
9+
10+
> Note: SwiftSyntax is still in development, and the API is not guaranteed to
11+
> be stable. It's subject to change without warning.
12+
13+
## Some Example Users
14+
15+
[**Swift AST Explorer**](https://swift-ast-explorer.kishikawakatsumi.com/): a Swift AST visualizer.
16+
17+
[**swift-format**](https://github.com/apple/swift-format): formatting technology for Swift source code.
18+
19+
[**Swift Stress Tester**](https://github.com/apple/swift-stress-tester): a test driver for sourcekitd and Swift evolution.
20+
21+
[**SwiftSemantics**](https://github.com/SwiftDocOrg/SwiftSemantics): parses Swift code into its constituent declarations.
22+
23+
[**Sitrep**](https://github.com/twostraws/Sitrep): A source code analyzer for Swift projects
24+
25+
[**SwiftRewriter**](https://github.com/inamiy/SwiftRewriter): a Swift code formatter.
26+
27+
[**SwiftPack**](https://github.com/omochi/SwiftPack): a tool for automatically embedding Swift library source.
28+
29+
[**Periphery**](https://github.com/peripheryapp/periphery): a tool to detect unused code.
30+
31+
[**BartyCrouch**](https://github.com/Flinesoft/BartyCrouch): a tool to incrementally update strings files to help App localization.
32+
33+
[**Muter**](https://github.com/muter-mutation-testing/muter): Automated mutation testing for Swift
34+
35+
[**Swift Variable Injector**](https://github.com/LucianoPAlmeida/variable-injector): a tool to replace string literals with environment variables values.
36+
37+
[**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).
38+
39+
[**Piranha**](https://github.com/uber/piranha): a tool for refactoring code related to feature flags.
40+
41+
[**STAR**](https://github.com/thumbtack/star): a tool to find how often specified Swift type(s) are used in a project.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Swift Syntax Builder
2+
3+
**Swift Syntax Builder** is an open-source package for generating Swift code in a declarative way.
4+
5+
> Note: SwiftSyntaxBuilder is still in development, and the API is not guaranteed to
6+
> be stable. It's subject to change without warning.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Swift Syntax Parser

Examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Examples
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import SwiftSyntax
2+
import Foundation
3+
4+
/// AddOneToIntegerLiterals will visit each token in the Syntax tree, and
5+
/// (if it is an integer literal token) add 1 to the integer and return the
6+
/// new integer literal token.
7+
class AddOneToIntegerLiterals: SyntaxRewriter {
8+
override func visit(_ token: TokenSyntax) -> Syntax {
9+
// Only transform integer literals.
10+
guard case .integerLiteral(let text) = token.tokenKind else {
11+
return Syntax(token)
12+
}
13+
14+
// Remove underscores from the original text.
15+
let integerText = String(text.filter { ("0"..."9").contains($0) })
16+
17+
// Parse out the integer.
18+
let int = Int(integerText)!
19+
20+
// Create a new integer literal token with `int + 1` as its text.
21+
let newIntegerLiteralToken = token.withKind(.integerLiteral("\(int + 1)"))
22+
23+
// Return the new integer literal.
24+
return Syntax(newIntegerLiteralToken)
25+
}
26+
}
27+
28+
let file = CommandLine.arguments[1]
29+
let url = URL(fileURLWithPath: file)
30+
let sourceFile = try SyntaxParser.parse(url)
31+
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
32+
print(incremented)

Examples/SwiftSyntax/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Swift Syntax Examples
2+
3+
This is a program that adds 1 to every integer literal in a Swift file.
4+
5+
```swift
6+
import SwiftSyntax
7+
import Foundation
8+
9+
/// AddOneToIntegerLiterals will visit each token in the Syntax tree, and
10+
/// (if it is an integer literal token) add 1 to the integer and return the
11+
/// new integer literal token.
12+
class AddOneToIntegerLiterals: SyntaxRewriter {
13+
override func visit(_ token: TokenSyntax) -> Syntax {
14+
// Only transform integer literals.
15+
guard case .integerLiteral(let text) = token.tokenKind else {
16+
return Syntax(token)
17+
}
18+
19+
// Remove underscores from the original text.
20+
let integerText = String(text.filter { ("0"..."9").contains($0) })
21+
22+
// Parse out the integer.
23+
let int = Int(integerText)!
24+
25+
// Create a new integer literal token with `int + 1` as its text.
26+
let newIntegerLiteralToken = token.withKind(.integerLiteral("\(int + 1)"))
27+
28+
// Return the new integer literal.
29+
return Syntax(newIntegerLiteralToken)
30+
}
31+
}
32+
33+
let file = CommandLine.arguments[1]
34+
let url = URL(fileURLWithPath: file)
35+
let sourceFile = try SyntaxParser.parse(url)
36+
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
37+
print(incremented)
38+
```
39+
40+
This example turns this:
41+
42+
```swift
43+
let x = 2
44+
let y = 3_000
45+
```
46+
47+
into:
48+
49+
```swift
50+
let x = 3
51+
let y = 3001
52+
```

Examples/SwiftSyntaxBuilder/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Swift Syntax Builder Examples
2+
3+
```swift
4+
import SwiftSyntaxBuilder
5+
6+
let source = SourceFile {
7+
ImportDecl(path: "Foundation")
8+
ImportDecl(path: "UIKit")
9+
ClassDecl(classOrActorKeyword: .class, identifier: "SomeViewController", membersBuilder: {
10+
VariableDecl(.let, name: "tableView", type: "UITableView")
11+
})
12+
}
13+
14+
let syntax = source.buildSyntax(format: Format())
15+
16+
var text = ""
17+
syntax.write(to: &text)
18+
19+
print(text)
20+
```
21+
22+
prints:
23+
24+
```swift
25+
import Foundation
26+
import UIKit
27+
class SomeViewController{
28+
let tableView: UITableView
29+
}
30+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SwiftSyntaxBuilder
2+
3+
let source = SourceFile {
4+
ImportDecl(path: "Foundation")
5+
ImportDecl(path: "UIKit")
6+
ClassDecl(classOrActorKeyword: .class, identifier: "SomeViewController", membersBuilder: {
7+
VariableDecl(.let, name: "tableView", type: "UITableView")
8+
})
9+
}
10+
11+
let syntax = source.buildSyntax(format: Format())
12+
13+
var text = ""
14+
syntax.write(to: &text)
15+
16+
print(text)

Examples/SwiftSyntaxParser/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Swift Syntax Parser Examples

0 commit comments

Comments
 (0)