Created
June 16, 2018 02:39
-
-
Save abl/a9307488d0b4284153806aab35039abb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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