mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
* Create Swift libSyntax API This patch is an initial implementation of the Swift libSyntax API. It aims to provide all features of the C++ API but exposed to Swift. It currently resides in SwiftExperimental and will likely exist in a molten state for a while. * Only build SwiftSyntax on macOS
62 lines
2.0 KiB
Swift
62 lines
2.0 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))
|
|
}
|
|
}
|