Skip to content

SwiftSyntax: add pre and post visit function so that client can override a general visiting logic. SR-6902 #14365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tools/SwiftSyntax/SyntaxRewriter.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ open class SyntaxRewriter {
open func visit(_ token: TokenSyntax) -> Syntax {
return token
}

/// The function called before visiting the node and its descendents.
/// - node: the node we are about to visit.
open func visitPre(_ node: Syntax) {}

/// The function called after visting the node and its descendents.
/// - node: the node we just finished visiting.
open func visitPost(_ node: Syntax) {}

public func visit(_ node: Syntax) -> Syntax {
visitPre(node)
defer { visitPost(node) }
switch node.raw.kind {
case .token: return visit(node as! TokenSyntax)
% for node in SYNTAX_NODES:
Expand Down Expand Up @@ -70,7 +81,17 @@ open class SyntaxVisitor {

open func visit(_ token: TokenSyntax) {}

/// The function called before visiting the node and its descendents.
/// - node: the node we are about to visit.
open func visitPre(_ node: Syntax) {}

/// The function called after visting the node and its descendents.
/// - node: the node we just finished visiting.
open func visitPost(_ node: Syntax) {}

public func visit(_ node: Syntax) {
visitPre(node)
defer { visitPost(node) }
switch node.raw.kind {
case .token: visit(node as! TokenSyntax)
% for node in SYNTAX_NODES:
Expand Down