Skip to content

Update examples #929

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
10 changes: 6 additions & 4 deletions Examples/AddOneToIntegerLiterals.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
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 {
override func visit(_ token: TokenSyntax) -> Syntax {
// Only transform integer literals.
Expand All @@ -39,6 +40,7 @@ class AddOneToIntegerLiterals: SyntaxRewriter {

let file = CommandLine.arguments[1]
let url = URL(fileURLWithPath: file)
let sourceFile = try SyntaxParser.parse(url)
let source = try String(contentsOf: url, encoding: .utf8)
let sourceFile = try Parser.parse(source: source)
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
print(incremented)
9 changes: 5 additions & 4 deletions Examples/CodeGenerationUsingSwiftSyntaxBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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")
Expand All @@ -18,7 +19,7 @@ let source = SourceFile {
}
}

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

var text = ""
syntax.write(to: &text)
Expand Down
10 changes: 6 additions & 4 deletions Examples/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# 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)
There is the following set of examples available:

## Some Example Usages
- [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 Projects Using SwiftSyntax

[**BartyCrouch**](https://github.com/Flinesoft/BartyCrouch): A tool to incrementally update strings files to help with localization.

Expand Down