Skip to content

Commit 44f25e9

Browse files
Merge pull request #3 from inamiy/SwiftSyntax-0.50100.0
Update SwiftSyntax to 0.50100.0
2 parents 504dbd7 + 1e66b20 commit 44f25e9

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"repositoryURL": "https://github.com/apple/swift-syntax.git",
1616
"state": {
1717
"branch": null,
18-
"revision": "43aa4a19b8105a803d8149ad2a86aa53a77efef3",
19-
"version": "0.50000.0"
18+
"revision": "3e3eb191fcdbecc6031522660c4ed6ce25282c25",
19+
"version": "0.50100.0"
2020
}
2121
}
2222
]

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let package = Package(
77
name: "swift-ast-explorer",
88
dependencies: [
99
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.3.0"),
10-
.package(url: "https://github.com/apple/swift-syntax.git", .exact("0.50000.0")),
10+
.package(url: "https://github.com/apple/swift-syntax.git", .exact("0.50100.0")),
1111
],
1212
targets: [
1313
.target(name: "swift-ast-explorer", dependencies: ["Utility", "SwiftSyntax"]),

Sources/swift-ast-explorer/main.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import Foundation
22
import Basic
33
import SwiftSyntax
44

5-
class TokenVisitor : SyntaxVisitor {
5+
struct TokenVisitor : SyntaxAnyVisitor {
66
var list = [String]()
77
var tree = [Node]()
88
var current: Node!
99

1010
var row = 0
1111
var column = 0
1212

13-
override func visitPre(_ node: Syntax) {
13+
mutating func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
1414
var syntax = "\(type(of: node))"
1515
if syntax.hasSuffix("Syntax") {
1616
syntax = String(syntax.dropLast(6))
@@ -28,9 +28,11 @@ class TokenVisitor : SyntaxVisitor {
2828
current.add(node: node)
2929
}
3030
current = node
31+
32+
return .visitChildren
3133
}
3234

33-
override func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
35+
mutating func visitPost(_ token: TokenSyntax) {
3436
current.text = escapeHtmlSpecialCharacters(token.text)
3537
current.token = Node.Token(kind: "\(token.tokenKind)", leadingTrivia: "", trailingTrivia: "")
3638

@@ -52,17 +54,17 @@ class TokenVisitor : SyntaxVisitor {
5254
current.range.endRow = row
5355
current.range.endColumn = column
5456

55-
return .visitChildren
57+
visitAnyPost(token) // NOTE: This is a required call.
5658
}
5759

58-
override func visitPost(_ node: Syntax) {
60+
mutating func visitAnyPost(_ node: Syntax) {
5961
list.append("</span>")
6062
current.range.endRow = row
6163
current.range.endColumn = column
6264
current = current.parent
6365
}
6466

65-
private func processToken(_ token: TokenSyntax) {
67+
private mutating func processToken(_ token: TokenSyntax) {
6668
var kind = "\(token.tokenKind)"
6769
if let index = kind.index(of: "(") {
6870
kind = String(kind.prefix(upTo: index))
@@ -75,7 +77,7 @@ class TokenVisitor : SyntaxVisitor {
7577
column += token.text.count
7678
}
7779

78-
private func processTriviaPiece(_ piece: TriviaPiece) -> String {
80+
private mutating func processTriviaPiece(_ piece: TriviaPiece) -> String {
7981
var trivia = ""
8082
switch piece {
8183
case .spaces(let count):
@@ -117,7 +119,7 @@ class TokenVisitor : SyntaxVisitor {
117119
return text.replacingOccurrences(of: "&nbsp;", with: "").replacingOccurrences(of: "<br>", with: "<br>↲")
118120
}
119121

120-
private func processComment(text: String) {
122+
private mutating func processComment(text: String) {
121123
let comments = text.split(separator: "\n", omittingEmptySubsequences: false)
122124
row += comments.count - 1
123125
column += comments.last!.count
@@ -187,9 +189,9 @@ class Node : Encodable {
187189
let arguments = Array(CommandLine.arguments.dropFirst())
188190
let filePath = URL(fileURLWithPath: arguments[0])
189191

190-
let sourceFile = try! SyntaxTreeParser.parse(filePath)
191-
let visitor = TokenVisitor()
192-
sourceFile.walk(visitor)
192+
let sourceFile = try! SyntaxParser.parse(filePath)
193+
var visitor = TokenVisitor()
194+
sourceFile.walk(&visitor)
193195
let html = "\(visitor.list.joined())"
194196

195197
let tree = visitor.tree

0 commit comments

Comments
 (0)