mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Based on the feedbacks from our early adopters, separating syntax tree analysis with transformation is a common pattern. Thus, we introduce a read-only syntax tree visitor to help the analysis phase. This visitor never alters the content of a tree being visited, in contrast to SyntaxRewriter which always does.
91 lines
2.6 KiB
Swift
91 lines
2.6 KiB
Swift
%{
|
|
from gyb_syntax_support import *
|
|
# -*- mode: Swift -*-
|
|
# Ignore the following admonition it applies to the resulting .swift file only
|
|
def is_visitable(node):
|
|
return not node.is_base() and not node.collection_element
|
|
}%
|
|
//// 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
|
|
}
|
|
public func visit(_ node: Syntax) -> Syntax {
|
|
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 Syntax.fromRaw(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) {}
|
|
|
|
public func visit(_ node: Syntax) {
|
|
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) }
|
|
}
|
|
}
|