blob: 15ba796b0bd79baabf46be2c012e3e787595d4ef [file] [log] [blame]
%{
from gyb_syntax_support import *
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From SyntaxFactory.swift.gyb.
//// Do Not Edit Directly!
//===------------ SyntaxRewriter.swift - Syntax Rewriter class ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the SyntaxRewriter, a class that performs a standard walk
// and tree-rebuilding pattern.
//
// Subclassers of this class can override the walking behavior for any syntax
// node and transform nodes however they like.
//
//===----------------------------------------------------------------------===//
open class SyntaxRewriter {
public init() {}
% for node in SYNTAX_NODES:
% if is_visitable(node):
open func visit(_ node: ${node.name}) -> ${node.base_type} {
% cast = ('as! ' + node.base_type) if node.base_type != 'Syntax' else ''
return visitChildren(node) ${cast}
}
% end
% end
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:
% if is_visitable(node):
case .${node.swift_syntax_kind}: return visit(node as! ${node.name})
% end
% end
default: return visitChildren(node)
}
}
func visitChildren(_ node: Syntax) -> Syntax {
let newLayout = node.children.map { visit($0).raw }
return makeSyntax(node.raw.replacingLayout(newLayout))
}
}
open class SyntaxVisitor {
public init() {}
% for node in SYNTAX_NODES:
% if is_visitable(node):
open func visit(_ node: ${node.name}) {
visitChildren(node)
}
% end
% end
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:
% if is_visitable(node):
case .${node.swift_syntax_kind}: visit(node as! ${node.name})
% end
% end
default: visitChildren(node)
}
}
func visitChildren(_ node: Syntax) {
node.children.forEach { visit($0) }
}
}