Skip to content

Commit 1d40ebd

Browse files
committed
Update readme
1 parent b12be78 commit 1d40ebd

File tree

11 files changed

+267
-124
lines changed

11 files changed

+267
-124
lines changed

Documentation/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Documentation

Documentation/SwiftSyntax/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Swift Syntax
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.
7+
8+
```swift
9+
import SwiftSyntaxBuilder
10+
11+
let source = SourceFile {
12+
ImportDecl(path: "Foundation")
13+
ImportDecl(path: "UIKit")
14+
ClassDecl(classOrActorKeyword: .class, identifier: "SomeViewController", membersBuilder: {
15+
VariableDecl(.let, name: "tableView", type: "UITableView")
16+
})
17+
}
18+
19+
let syntax = source.buildSyntax(format: Format())
20+
21+
var text = ""
22+
syntax.write(to: &text)
23+
24+
print(text)
25+
```
26+
27+
prints:
28+
29+
```swift
30+
import Foundation
31+
import UIKit
32+
class SomeViewController{
33+
let tableView: UITableView
34+
}
35+
```
36+
37+
## Usage
38+
39+
### Declare SwiftPM dependency with release tag
40+
41+
Add this repository to the `Package.swift` manifest of your project:
42+
43+
```swift
44+
// swift-tools-version:5.3
45+
import PackageDescription
46+
47+
let package = Package(
48+
name: "MyTool",
49+
dependencies: [
50+
.package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax.git", .exact("<#Specify Release tag#>")),
51+
],
52+
targets: [
53+
.target(name: "MyTool", dependencies: ["SwiftSyntaxBuilder"]),
54+
]
55+
)
56+
```
57+
58+
Replace `<#Specify Release tag#>` by the version of SwiftSyntaxBuilder that you want to use. See this following [table](https://github.com/apple/swift-syntax#declare-swiftpm-dependency-with-release-tag) for mapping details.
59+
60+
Then, import `SwiftSyntaxBuilder` in your Swift code.
61+
62+
Note that SwiftSyntaxBuilder has limited support for pretty-printing code. We generally suggest running a formatter like [swift-format](https://github.com/apple/swift-format) on the generated code.
63+
64+
### Reporting Issues
65+
66+
If you should hit any issues while using SwiftSyntaxBuilder, we appreciate bug reports on [bugs.swift.org](https://bugs.swift.org) in the [SwiftSyntax component](https://bugs.swift.org/issues/?jql=component%20%3D%20SwiftSyntax).
67+
68+
## License
69+
70+
Please see [LICENSE](https://github.com/apple/swift-syntax#license) for more information.
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
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+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.
7+
8+
```swift
9+
import SwiftSyntaxBuilder
10+
11+
let source = SourceFile {
12+
ImportDecl(path: "Foundation")
13+
ImportDecl(path: "UIKit")
14+
ClassDecl(classOrActorKeyword: .class, identifier: "SomeViewController", membersBuilder: {
15+
VariableDecl(.let, name: "tableView", type: "UITableView")
16+
})
17+
}
18+
19+
let syntax = source.buildSyntax(format: Format())
20+
21+
var text = ""
22+
syntax.write(to: &text)
23+
24+
print(text)
25+
```
26+
27+
prints:
28+
29+
```swift
30+
import Foundation
31+
import UIKit
32+
class SomeViewController{
33+
let tableView: UITableView
34+
}
35+
```
36+
37+
## Usage
38+
39+
### Declare SwiftPM dependency with release tag
40+
41+
Add this repository to the `Package.swift` manifest of your project:
42+
43+
```swift
44+
// swift-tools-version:5.3
45+
import PackageDescription
46+
47+
let package = Package(
48+
name: "MyTool",
49+
dependencies: [
50+
.package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax.git", .exact("<#Specify Release tag#>")),
51+
],
52+
targets: [
53+
.target(name: "MyTool", dependencies: ["SwiftSyntaxBuilder"]),
54+
]
55+
)
56+
```
57+
58+
Replace `<#Specify Release tag#>` by the version of SwiftSyntaxBuilder that you want to use. See this following [table](https://github.com/apple/swift-syntax#declare-swiftpm-dependency-with-release-tag) for mapping details.
59+
60+
Then, import `SwiftSyntaxBuilder` in your Swift code.
61+
62+
Note that SwiftSyntaxBuilder has limited support for pretty-printing code. We generally suggest running a formatter like [swift-format](https://github.com/apple/swift-format) on the generated code.
63+
64+
### Reporting Issues
65+
66+
If you should hit any issues while using SwiftSyntaxBuilder, we appreciate bug reports on [bugs.swift.org](https://bugs.swift.org) in the [SwiftSyntax component](https://bugs.swift.org/issues/?jql=component%20%3D%20SwiftSyntax).
67+
68+
## License
69+
70+
Please see [LICENSE](https://github.com/apple/swift-syntax#license) for more information.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Swift Syntax Parser

0 commit comments

Comments
 (0)