Skip to content

Instantly share code, notes, and snippets.

@abl
Created June 16, 2018 02:39
Show Gist options
  • Save abl/a9307488d0b4284153806aab35039abb to your computer and use it in GitHub Desktop.
Save abl/a9307488d0b4284153806aab35039abb to your computer and use it in GitHub Desktop.
// Copyright 2018 Google LLC.
// SPDX-License-Identifier: Apache-2.0
import Foundation
import SwiftSyntax
public class IncrementIntegers: SyntaxRewriter {
public override func visit(_ token: TokenSyntax) -> Syntax {
guard case .integerLiteral(let intToken) = token.tokenKind else {
return token
}
guard let integerValue = Int(intToken) else {
return token
}
let newValue = integerValue + 1
let newKind = TokenKind.integerLiteral(newValue.description)
return token.withKind(newKind)
}
}
func main() throws {
let testURL = URL(fileURLWithPath: CommandLine.arguments[1])
let testFile = try SourceFileSyntax.parse(testURL)
let incrementer = IncrementIntegers()
let rewritten = incrementer.visit(testFile)
print(rewritten)
}
do {
try main()
} catch {
print("error: \(error)")
exit(-1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment