Skip to content

Make examples runnable #900

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
/.build
25 changes: 16 additions & 9 deletions Examples/AddOneToIntegerLiterals.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import SwiftSyntax
import SwiftSyntaxParser
import SwiftParser
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:
/// For example, it will turn
/// ```
/// let x = 2
/// let y = 3_000
/// ```
/// into:
/// into
/// ```
/// let x = 3
/// let y = 3001
/// ```
class AddOneToIntegerLiterals: SyntaxRewriter {
///
private class AddOneToIntegerLiterals: SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> Syntax {
// Only transform integer literals.
guard case .integerLiteral(let text) = token.tokenKind else {
Expand All @@ -37,8 +38,14 @@ class AddOneToIntegerLiterals: SyntaxRewriter {
}
}

let file = CommandLine.arguments[1]
let url = URL(fileURLWithPath: file)
let sourceFile = try SyntaxParser.parse(url)
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
print(incremented)
@main
struct Main {
static func main() throws {
let file = CommandLine.arguments[1]
let url = URL(fileURLWithPath: file)
let source = try String(contentsOf: url, encoding: .utf8)
let sourceFile = try Parser.parse(source: source)
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
print(incremented)
}
}
35 changes: 20 additions & 15 deletions Examples/CodeGenerationUsingSwiftSyntaxBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import SwiftSyntaxBuilder

/// This example will print the following example:
/// This example will print the following code:
///
///```
/// ```
/// import Foundation
/// import UIKit
/// class SomeViewController{
/// let tableView: UITableView
/// }
///```

let source = SourceFile {
ImportDecl(path: "Foundation")
ImportDecl(path: "UIKit")
ClassDecl(identifier: "SomeViewController") {
VariableDecl(.let, name: "tableView", type: "UITableView")
}
}
/// ```
///
@main
struct Main {
static func main() {
let source = SourceFile {
ImportDecl(path: "Foundation")
ImportDecl(path: "UIKit")
ClassDecl(identifier: "SomeViewController") {
VariableDecl(.let, name: "tableView", type: "UITableView")
}
}

let syntax = source.buildSyntax(format: Format())
let syntax = source.build()

var text = ""
syntax.write(to: &text)
var text = ""
syntax.write(to: &text)

print(text)
print(text)
}
}
36 changes: 36 additions & 0 deletions Examples/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "Examples",
platforms: [
.macOS(.v10_15),
],
products: [
.executable(name: "AddOneToIntegerLiterals", targets: ["AddOneToIntegerLiterals"]),
.executable(name: "CodeGenerationUsingSwiftSyntaxBuilder", targets: ["CodeGenerationUsingSwiftSyntaxBuilder"]),
],
dependencies: [
.package(path: "../"),
],
targets: [
.executableTarget(
name: "AddOneToIntegerLiterals",
dependencies: [
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
],
path: ".",
sources: ["AddOneToIntegerLiterals.swift"]
),
.executableTarget(
name: "CodeGenerationUsingSwiftSyntaxBuilder",
dependencies: [
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
],
path: ".",
sources: ["CodeGenerationUsingSwiftSyntaxBuilder.swift"]
),
]
)
8 changes: 5 additions & 3 deletions Examples/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Snippets (Examples)
# Examples

- Command line tool to add one to every integer literal in a source file [AddOneToIntegerLiterals.swift](AddOneToIntegerLiterals.swift).
- Code-generate a simple source file using SwiftSyntaxBuilder [CodeGenerationUsingSwiftSyntaxBuilder.swift](CodeGenerationUsingSwiftSyntaxBuilder.swift)
Each example can be executed by navigating into this folder and running `swift run <example> <arguments>`. There is the following set of examples available:

- [AddOneToIntegerLiterals](AddOneToIntegerLiterals.swift): Command line tool to add 1 to every integer literal in a source file
- [CodeGenerationUsingSwiftSyntaxBuilder](CodeGenerationUsingSwiftSyntaxBuilder.swift): Code-generate a simple source file using SwiftSyntaxBuilder

## Some Example Usages

Expand Down