@@ -94,24 +94,35 @@ open class SyntaxRewriter {
94
94
}
95
95
}
96
96
97
+ /// The enum describes how the SyntaxVistor should continue after visiting
98
+ /// the current node.
99
+ public enum SyntaxVisitorContinueKind {
100
+
101
+ /// The visitor should visit the descendents of the current node.
102
+ case visitChildren
103
+
104
+ /// The visitor should avoid visiting the descendents of the current node.
105
+ case skipChildren
106
+ }
107
+
97
108
open class SyntaxVisitor {
98
109
public init ( ) { }
99
110
% for node in SYNTAX_NODES:
100
111
% if is_visitable ( node) :
101
112
/// Visting ${node.name} specifically.
102
113
/// - Parameter node: the node we are visiting.
103
- /// - Returns: whether we should visit the descendents of node .
104
- open func visit( _ node: ${ node. name} ) -> Bool {
105
- return true
114
+ /// - Returns: how should we continue visiting .
115
+ open func visit( _ node: ${ node. name} ) -> SyntaxVis itorContinueKind {
116
+ return . visitChildren
106
117
}
107
118
% end
108
119
% end
109
120
110
121
/// Visting UnknownSyntax specifically.
111
122
/// - Parameter node: the node we are visiting.
112
- /// - Returns: whether we should visit the descendents of node .
113
- open func visit( _ node: UnknownSyntax ) -> Bool {
114
- return true
123
+ /// - Returns: how should we continue visiting .
124
+ open func visit( _ node: UnknownSyntax ) -> SyntaxVisitorContinueKind {
125
+ return . visitChildren
115
126
}
116
127
117
128
/// Whether we should ever visit a given syntax kind.
@@ -128,7 +139,9 @@ open class SyntaxVisitor {
128
139
return true
129
140
}
130
141
131
- open func visit( _ token: TokenSyntax ) { }
142
+ open func visit( _ token: TokenSyntax ) -> SyntaxVisitorContinueKind {
143
+ return . skipChildren
144
+ }
132
145
133
146
/// The function called before visiting the node and its descendents.
134
147
/// - node: the node we are about to visit.
@@ -138,18 +151,17 @@ open class SyntaxVisitor {
138
151
/// - node: the node we just finished visiting.
139
152
open func visitPost( _ node: Syntax ) { }
140
153
141
- public func visit( _ node: Syntax ) -> Bool {
154
+ public func visit( _ node: Syntax ) -> SyntaxVisitorContinueKind {
142
155
switch node. raw. kind {
143
- case . token: visit ( node as! TokenSyntax )
156
+ case . token: return visit ( node as! TokenSyntax )
144
157
% for node in SYNTAX_NODES:
145
158
% if is_visitable ( node) :
146
159
case . ${ node. swift_syntax_kind} : return visit ( node as! ${ node. name} )
147
160
% end
148
161
% end
149
162
case . unknown: return visit ( node as! UnknownSyntax )
150
- default : break
163
+ default : return . skipChildren
151
164
}
152
- return false
153
165
}
154
166
}
155
167
@@ -232,7 +244,7 @@ class RawSyntaxVisitor {
232
244
233
245
// The current raw syntax node is interesting for the user, so realize a
234
246
// correponding syntax node and feed it into the visitor.
235
- func visit( ) -> Bool {
247
+ func visit( ) -> SyntaxVisitorContinueKind {
236
248
return visitor. visit ( currentNode. node)
237
249
}
238
250
}
0 commit comments