|
| 1 | +%{ |
| 2 | + from gyb_syntax_support import * |
| 3 | + # -*- mode: Swift -*- |
| 4 | + # Ignore the following admonition it applies to the resulting .swift file only |
| 5 | +}% |
| 6 | +//// Automatically Generated From SyntaxTransform.swift.gyb. |
| 7 | +//// Do Not Edit Directly! |
| 8 | +//===------ SyntaxTransform.swift - Syntax Transform Visitor Protocol -----===// |
| 9 | +// |
| 10 | +// This source file is part of the Swift.org open source project |
| 11 | +// |
| 12 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 13 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 14 | +// |
| 15 | +// See https://swift.org/LICENSE.txt for license information |
| 16 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +public protocol SyntaxTransformVisitor { |
| 21 | + associatedtype ResultType = Void |
| 22 | + |
| 23 | + func visitAny(_ node: Syntax) -> ResultType |
| 24 | + |
| 25 | + func visit(_ token: TokenSyntax) -> ResultType |
| 26 | + func visit(_ node: UnknownSyntax) -> ResultType |
| 27 | + |
| 28 | +% for node in SYNTAX_NODES: |
| 29 | +% if is_visitable(node): |
| 30 | + /// Visiting `${node.name}` specifically. |
| 31 | + /// - Parameter node: the node we are visiting. |
| 32 | + /// - Returns: the sum of whatever the child visitors return. |
| 33 | + func visit(_ node: ${node.name}) -> ResultType |
| 34 | +% end |
| 35 | +% end |
| 36 | +} |
| 37 | + |
| 38 | +extension SyntaxTransformVisitor { |
| 39 | + public func visit(_ token: TokenSyntax) -> ResultType { |
| 40 | + visitAny(Syntax(token)) |
| 41 | + } |
| 42 | + |
| 43 | + public func visit(_ node: UnknownSyntax) -> ResultType { |
| 44 | + visitAny(Syntax(node)) |
| 45 | + } |
| 46 | + |
| 47 | +% for node in SYNTAX_NODES: |
| 48 | +% if is_visitable(node): |
| 49 | + /// Visiting `${node.name}` specifically. |
| 50 | + /// - Parameter node: the node we are visiting. |
| 51 | + /// - Returns: nil by default. |
| 52 | + public func visit(_ node: ${node.name}) -> ResultType { |
| 53 | + visitAny(Syntax(node)) |
| 54 | + } |
| 55 | +% end |
| 56 | +% end |
| 57 | + |
| 58 | + public func visit(_ node: Syntax) -> ResultType { |
| 59 | + switch node.as(SyntaxEnum.self) { |
| 60 | + case .token(let node): |
| 61 | + return visit(node) |
| 62 | + case .unknown(let node): |
| 63 | + return visit(node) |
| 64 | +% for node in NON_BASE_SYNTAX_NODES: |
| 65 | + case .${node.swift_syntax_kind}(let derived): |
| 66 | + return visit(derived) |
| 67 | +% end |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public func visit(_ node: ExprSyntax) -> ResultType { |
| 72 | + visit(Syntax(node)) |
| 73 | + } |
| 74 | + |
| 75 | + public func visit(_ node: PatternSyntax) -> ResultType { |
| 76 | + visit(Syntax(node)) |
| 77 | + } |
| 78 | + |
| 79 | + public func visit(_ node: TypeSyntax) -> ResultType { |
| 80 | + visit(Syntax(node)) |
| 81 | + } |
| 82 | + |
| 83 | + public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] { |
| 84 | + let syntaxNode = Syntax(node) |
| 85 | + return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).map { rawChild in |
| 86 | + let child = Syntax(SyntaxData(rawChild, parent: syntaxNode)) |
| 87 | + return visit(child) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments