Skip to content

Commit b445aa4

Browse files
committed
Teach SyntaxClassifier to use the new visitor pattern.
1 parent b48aabe commit b445aa4

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

Sources/SwiftSyntax/SyntaxClassifier.swift.gyb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
5050
force: Bool = false
5151
) {
5252
contextStack.append((classification: classification, force: force))
53-
visit(node)
53+
node.walk(self)
5454
contextStack.removeLast()
5555
}
5656

@@ -71,6 +71,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
7171
}
7272

7373
override func visit(_ token: TokenSyntax) {
74+
assert(token.isPresent)
7475
// FIXME: We need to come up with some way in which the SyntaxClassifier can
7576
// classify trivia (i.e. comments). In particular we need to be able to
7677
// look into the comments to find things like URLs or keywords like MARK.
@@ -94,12 +95,12 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
9495

9596
% for node in SYNTAX_NODES:
9697
% if is_visitable(node):
97-
override func visit(_ node: ${node.name}) {
98+
override func visit(_ node: ${node.name}) -> Bool {
9899
if skipNodeIds.contains(node.raw.id) {
99-
return
100+
return false
100101
}
101102
% if node.is_unknown() or node.is_syntax_collection():
102-
super.visit(node)
103+
return true
103104
% else:
104105
% for child in node.children:
105106
% if child.is_optional:
@@ -109,7 +110,7 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
109110
classification: .${child.classification.swift_name},
110111
force: ${"true" if child.force_classification else "false"})
111112
% else:
112-
visit(${child.swift_name})
113+
${child.swift_name}.walk(self)
113114
% end
114115
}
115116
% else:
@@ -118,12 +119,12 @@ fileprivate class _SyntaxClassifier: SyntaxVisitor {
118119
classification: .${child.classification.swift_name},
119120
force: ${"true" if child.force_classification else "false"})
120121
% else:
121-
visit(node.${child.swift_name})
122+
node.${child.swift_name}.walk(self)
122123
% end
123124
% end
124125
% end
126+
return false
125127
% end
126-
127128
}
128129
% end
129130
% end
@@ -139,7 +140,7 @@ public enum SyntaxClassifier {
139140
) -> [TokenSyntax: SyntaxClassification] {
140141
let classifier = _SyntaxClassifier()
141142
classifier.skipNodeIds = skipNodes
142-
classifier.visit(syntaxTree)
143+
syntaxTree.walk(classifier)
143144
return classifier.classifications
144145
}
145146
}

Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ClassifiedSyntaxTreePrinter: SyntaxVisitor {
5252

5353
func print(tree: SourceFileSyntax) -> String {
5454
result = ""
55-
visit(tree)
55+
tree.walk(self)
5656
// Emit the last closing tag
5757
recordCurrentClassification(.none)
5858
return result

0 commit comments

Comments
 (0)