@@ -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
@@ -158,7 +170,7 @@ open class SyntaxVisitor {
158
170
/// otherwise the node is represented as a child index list from a realized
159
171
/// ancestor.
160
172
class PendingSyntaxNode {
161
- let parent : PendingSyntaxNode ?
173
+ let parent : PendingSyntaxNode !
162
174
private var kind : PendingSyntaxNodeKind
163
175
164
176
private enum PendingSyntaxNodeKind {
@@ -174,7 +186,7 @@ class PendingSyntaxNode {
174
186
case . realized( let node) :
175
187
return node
176
188
case . virtual( let index) :
177
- let _node = parent! . node. child ( at: index) !
189
+ let _node = parent. node. child ( at: index) !
178
190
kind = . realized( node: _node)
179
191
return _node
180
192
}
@@ -199,7 +211,7 @@ class PendingSyntaxNode {
199
211
/// not interesting to users' SyntaxVisitor.
200
212
class RawSyntaxVisitor {
201
213
private let visitor : SyntaxVisitor
202
- private var currentNode : PendingSyntaxNode ?
214
+ private var currentNode : PendingSyntaxNode !
203
215
204
216
required init ( _ visitor: SyntaxVisitor , _ root: Syntax ) {
205
217
self . visitor = visitor
@@ -215,25 +227,25 @@ class RawSyntaxVisitor {
215
227
}
216
228
217
229
func addChildIdx( _ idx: Int ) {
218
- currentNode = PendingSyntaxNode ( currentNode! , idx)
230
+ currentNode = PendingSyntaxNode ( currentNode, idx)
219
231
}
220
232
221
233
func moveUp( ) {
222
- currentNode = currentNode! . parent
234
+ currentNode = currentNode. parent
223
235
}
224
236
225
237
func visitPre( ) {
226
- visitor. visitPre ( currentNode! . node)
238
+ visitor. visitPre ( currentNode. node)
227
239
}
228
240
229
241
func visitPost( ) {
230
- visitor. visitPost ( currentNode! . node)
242
+ visitor. visitPost ( currentNode. node)
231
243
}
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 {
236
- return visitor. visit ( currentNode! . node)
247
+ func visit( ) -> SyntaxVisitorContinueKind {
248
+ return visitor. visit ( currentNode. node)
237
249
}
238
250
}
239
251
0 commit comments