Skip to content

Commit 6cd32e4

Browse files
authored
Merge pull request #643 from zoecarver/transform-visitor
Add a new visitor: `SyntaxTransformVisitor`.
2 parents f905feb + cdb390d commit 6cd32e4

File tree

6 files changed

+3427
-0
lines changed

6 files changed

+3427
-0
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ let package = Package(
7676
"SyntaxKind.swift.gyb",
7777
"SyntaxNodes.swift.gyb.template",
7878
"SyntaxRewriter.swift.gyb",
79+
"SyntaxTransform.swift.gyb",
7980
"SyntaxTraits.swift.gyb",
8081
"SyntaxVisitor.swift.gyb",
8182
"TokenKind.swift.gyb",

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_library(SwiftSyntax STATIC
4242
gyb_generated/SyntaxKind.swift
4343
gyb_generated/SyntaxRewriter.swift
4444
gyb_generated/SyntaxTraits.swift
45+
gyb_generated/SyntaxTransform.swift
4546
gyb_generated/SyntaxVisitor.swift
4647
gyb_generated/TokenKind.swift
4748
gyb_generated/Tokens.swift
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)