|
9 | 9 | // |
10 | 10 | // This source file is part of the Swift.org open source project |
11 | 11 | // |
12 | | -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 12 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
13 | 13 | // Licensed under Apache License v2.0 with Runtime Library Exception |
14 | 14 | // |
15 | 15 | // See https://swift.org/LICENSE.txt for license information |
@@ -217,186 +217,3 @@ open class SyntaxRewriter { |
217 | 217 |
|
218 | 218 | } |
219 | 219 | } |
220 | | - |
221 | | -/// The enum describes how the SyntaxVistor should continue after visiting |
222 | | -/// the current node. |
223 | | -public enum SyntaxVisitorContinueKind { |
224 | | - |
225 | | - /// The visitor should visit the descendents of the current node. |
226 | | - case visitChildren |
227 | | - |
228 | | - /// The visitor should avoid visiting the descendents of the current node. |
229 | | - case skipChildren |
230 | | -} |
231 | | - |
232 | | -open class SyntaxVisitor { |
233 | | - public init() {} |
234 | | - |
235 | | - /// Walk all nodes of the given syntax tree, calling the corresponding `visit` |
236 | | - /// function for every node that is being visited. |
237 | | - public func walk<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) { |
238 | | - visit(node.data) |
239 | | - } |
240 | | - |
241 | | -% for node in SYNTAX_NODES: |
242 | | -% if is_visitable(node): |
243 | | - /// Visiting `${node.name}` specifically. |
244 | | - /// - Parameter node: the node we are visiting. |
245 | | - /// - Returns: how should we continue visiting. |
246 | | - open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind { |
247 | | - return .visitChildren |
248 | | - } |
249 | | - |
250 | | - /// The function called after visiting `${node.name}` and its descendents. |
251 | | - /// - node: the node we just finished visiting. |
252 | | - open func visitPost(_ node: ${node.name}) {} |
253 | | -% end |
254 | | -% end |
255 | | - |
256 | | - /// Visiting `TokenSyntax` specifically. |
257 | | - /// - Parameter node: the node we are visiting. |
258 | | - /// - Returns: how should we continue visiting. |
259 | | - open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { |
260 | | - return .visitChildren |
261 | | - } |
262 | | - |
263 | | - /// The function called after visiting the node and its descendents. |
264 | | - /// - node: the node we just finished visiting. |
265 | | - open func visitPost(_ node: TokenSyntax) {} |
266 | | - |
267 | | - /// Visiting `UnknownSyntax` specifically. |
268 | | - /// - Parameter node: the node we are visiting. |
269 | | - /// - Returns: how should we continue visiting. |
270 | | - open func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind { |
271 | | - return .visitChildren |
272 | | - } |
273 | | - |
274 | | - /// The function called after visiting the node and its descendents. |
275 | | - /// - node: the node we just finished visiting. |
276 | | - open func visitPost(_ node: UnknownSyntax) {} |
277 | | - |
278 | | -% for node in SYNTAX_NODES: |
279 | | - /// Implementation detail of doVisit(_:_:). Do not call directly. |
280 | | - private func visitImpl${node.name}(_ data: SyntaxData) { |
281 | | -% if node.is_base(): |
282 | | - let node = Unknown${node.name}(data) |
283 | | - let needsChildren = (visit(node) == .visitChildren) |
284 | | - // Avoid calling into visitChildren if possible. |
285 | | - if needsChildren && node.raw.numberOfChildren > 0 { |
286 | | - visitChildren(node) |
287 | | - } |
288 | | - visitPost(node) |
289 | | -% else: |
290 | | - let node = ${node.name}(data) |
291 | | - let needsChildren = (visit(node) == .visitChildren) |
292 | | - // Avoid calling into visitChildren if possible. |
293 | | - if needsChildren && node.raw.numberOfChildren > 0 { |
294 | | - visitChildren(node) |
295 | | - } |
296 | | - visitPost(node) |
297 | | -% end |
298 | | - } |
299 | | - |
300 | | -% end |
301 | | - |
302 | | - private func visit(_ data: SyntaxData) { |
303 | | - switch data.raw.kind { |
304 | | - case .token: |
305 | | - let node = TokenSyntax(data) |
306 | | - _ = visit(node) |
307 | | - // No children to visit. |
308 | | - visitPost(node) |
309 | | - case .unknown: |
310 | | - let node = UnknownSyntax(data) |
311 | | - let needsChildren = (visit(node) == .visitChildren) |
312 | | - // Avoid calling into visitChildren if possible. |
313 | | - if needsChildren && node.raw.numberOfChildren > 0 { |
314 | | - visitChildren(node) |
315 | | - } |
316 | | - visitPost(node) |
317 | | - // The implementation of every generated case goes into its own function. This |
318 | | - // circumvents an issue where the compiler allocates stack space for every |
319 | | - // case statement next to each other in debug builds, causing it to allocate |
320 | | - // ~50KB per call to this function. rdar://55929175 |
321 | | - % for node in SYNTAX_NODES: |
322 | | - case .${node.swift_syntax_kind}: |
323 | | - visitImpl${node.name}(data) |
324 | | - % end |
325 | | - } |
326 | | - } |
327 | | - |
328 | | - private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) { |
329 | | - let syntaxNode = Syntax(node) |
330 | | - let parentBox = SyntaxBox(syntaxNode) |
331 | | - for childRaw in PresentRawSyntaxChildren(syntaxNode) { |
332 | | - let childData = SyntaxData(childRaw, parentBox: parentBox) |
333 | | - visit(childData) |
334 | | - } |
335 | | - } |
336 | | -} |
337 | | - |
338 | | -/// A `SyntaxVisitor` that can visit the nodes as generic `Syntax` values. |
339 | | -/// |
340 | | -/// This subclass of `SyntaxVisitor` is slower than the type-specific visitation |
341 | | -/// of `SyntaxVisitor`. Use `SyntaxAnyVisitor` if the `visitAny(_)` function |
342 | | -/// would be useful to have, otherwise inherit from `SyntaxVisitor`. |
343 | | -/// |
344 | | -/// This works by overriding the type-specific visit function that delegate to |
345 | | -/// `visitAny(_)`. A subclass that provides a custom type-specific visit |
346 | | -/// function, should also call `visitAny(_)` in its implementation, if calling |
347 | | -/// `visitAny` is needed: |
348 | | -/// |
349 | | -/// struct MyVisitor: SyntaxAnyVisitor { |
350 | | -/// func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
351 | | -/// <code> |
352 | | -/// } |
353 | | -/// |
354 | | -/// func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { |
355 | | -/// <code> |
356 | | -/// // Call this to pass tokens to `visitAny(_)` as well if needed |
357 | | -/// visitAny(token) |
358 | | -/// } |
359 | | -/// |
360 | | -open class SyntaxAnyVisitor: SyntaxVisitor { |
361 | | - /// Visiting `UnknownSyntax` specifically. |
362 | | - /// - Parameter node: the node we are visiting. |
363 | | - /// - Returns: how should we continue visiting. |
364 | | - open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
365 | | - return .visitChildren |
366 | | - } |
367 | | - |
368 | | - /// The function called after visiting the node and its descendents. |
369 | | - /// - node: the node we just finished visiting. |
370 | | - open func visitAnyPost(_ node: Syntax) {} |
371 | | - |
372 | | - // MARK: Override type specific visit methods |
373 | | - |
374 | | - override open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { |
375 | | - return visitAny(token._syntaxNode) |
376 | | - } |
377 | | - |
378 | | - override open func visitPost(_ node: TokenSyntax) { |
379 | | - visitAnyPost(node._syntaxNode) |
380 | | - } |
381 | | - |
382 | | - override open func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind { |
383 | | - return visitAny(node._syntaxNode) |
384 | | - } |
385 | | - |
386 | | - override open func visitPost(_ node: UnknownSyntax) { |
387 | | - visitAnyPost(node._syntaxNode) |
388 | | - } |
389 | | - |
390 | | -% for node in SYNTAX_NODES: |
391 | | -% if is_visitable(node): |
392 | | - override open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind { |
393 | | - return visitAny(node._syntaxNode) |
394 | | - } |
395 | | - |
396 | | - override open func visitPost(_ node: ${node.name}) { |
397 | | - visitAnyPost(node._syntaxNode) |
398 | | - } |
399 | | -% end |
400 | | -% end |
401 | | - |
402 | | -} |
0 commit comments