Skip to content

Commit bc3bf11

Browse files
authored
Merge pull request swiftlang#77 from akyrtzi/opt4-visitor
Optimize `SyntaxVisitor` and make it a protocol
2 parents 4e9d84c + d696393 commit bc3bf11

File tree

8 files changed

+210
-217
lines changed

8 files changed

+210
-217
lines changed

Sources/SwiftSyntax/RawSyntax.swift

-36
Original file line numberDiff line numberDiff line change
@@ -1116,39 +1116,3 @@ extension RawSyntax {
11161116
trailingTrivia: trailingTrivia, length: length, presence: presence)
11171117
}
11181118
}
1119-
1120-
extension RawSyntax {
1121-
func accept(_ visitor: RawSyntaxVisitor) {
1122-
// FIXME: Simplify visitation, this deferred 'shouldVisit` visitation
1123-
// mechanism is unnecessary now.
1124-
defer { visitor.moveUp() }
1125-
guard isPresent else { return }
1126-
if let tokKind = self.formTokenKind() {
1127-
if visitor.shouldVisit(tokKind) {
1128-
visitor.visitPre()
1129-
_ = visitor.visit()
1130-
visitor.visitPost()
1131-
}
1132-
} else {
1133-
let shouldVisit = visitor.shouldVisit(kind)
1134-
var visitChildren = true
1135-
if shouldVisit {
1136-
// Visit this node realizes a syntax node.
1137-
visitor.visitPre()
1138-
visitChildren = visitor.visit() == .visitChildren
1139-
}
1140-
if visitChildren {
1141-
for offset in 0..<numberOfChildren {
1142-
let child = self.child(at: offset)
1143-
guard let element = child else { continue }
1144-
// Teach the visitor to navigate to this child.
1145-
visitor.addChildIdx(offset)
1146-
element.accept(visitor)
1147-
}
1148-
}
1149-
if shouldVisit {
1150-
visitor.visitPost()
1151-
}
1152-
}
1153-
}
1154-
}

Sources/SwiftSyntax/SyntaxClassifier.swift.gyb

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public enum SyntaxClassification {
2828
% end
2929
}
3030

31-
fileprivate class _SyntaxClassifier: SyntaxVisitor {
31+
fileprivate struct _SyntaxClassifier: SyntaxVisitor {
3232

3333
/// The top of the `contextStack` determines the classification for all tokens
3434
/// encountered that do not have a native classification. If `force` is `true`
@@ -41,13 +41,13 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
4141
/// array.
4242
var classifications: [(TokenSyntax, SyntaxClassification)] = []
4343

44-
private func visit(
44+
mutating private func visit(
4545
_ node: Syntax,
4646
classification: SyntaxClassification,
4747
force: Bool = false
4848
) {
4949
contextStack.append((classification: classification, force: force))
50-
node.walk(self)
50+
node.walk(&self)
5151
contextStack.removeLast()
5252
}
5353

@@ -67,7 +67,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
6767
}
6868
}
6969

70-
override func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
70+
mutating func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
7171
assert(token.isPresent)
7272
// FIXME: We need to come up with some way in which the SyntaxClassifier can
7373
// classify trivia (i.e. comments). In particular we need to be able to
@@ -91,7 +91,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
9191

9292
% for node in SYNTAX_NODES:
9393
% if is_visitable(node):
94-
override func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
94+
mutating func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
9595
% if node.is_unknown() or node.is_syntax_collection():
9696
return .visitChildren
9797
% else:
@@ -103,7 +103,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
103103
classification: .${child.classification.swift_name},
104104
force: ${"true" if child.force_classification else "false"})
105105
% else:
106-
${child.swift_name}.walk(self)
106+
${child.swift_name}.walk(&self)
107107
% end
108108
}
109109
% else:
@@ -112,7 +112,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
112112
classification: .${child.classification.swift_name},
113113
force: ${"true" if child.force_classification else "false"})
114114
% else:
115-
node.${child.swift_name}.walk(self)
115+
node.${child.swift_name}.walk(&self)
116116
% end
117117
% end
118118
% end
@@ -130,8 +130,8 @@ public enum SyntaxClassifier {
130130
public static func classifyTokensInTree(
131131
_ syntaxTree: SourceFileSyntax
132132
) -> [(TokenSyntax, SyntaxClassification)] {
133-
let classifier = _SyntaxClassifier()
134-
syntaxTree.walk(classifier)
133+
var classifier = _SyntaxClassifier()
134+
syntaxTree.walk(&classifier)
135135
return classifier.classifications
136136
}
137137
}

0 commit comments

Comments
 (0)