Skip to content

Commit 171b497

Browse files
committed
Address review comments.
1 parent 37a3426 commit 171b497

File tree

6 files changed

+4919
-4886
lines changed

6 files changed

+4919
-4886
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",
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 visit(_ token: TokenSyntax) -> [ResultType]
24+
func visit(_ node: UnknownSyntax) -> [ResultType]
25+
26+
% for node in SYNTAX_NODES:
27+
% if is_visitable(node):
28+
/// Visiting `${node.name}` specifically.
29+
/// - Parameter node: the node we are visiting.
30+
/// - Returns: the sum of whatever the child visitors return.
31+
func visit(_ node: ${node.name}) -> [ResultType]
32+
% end
33+
% end
34+
}
35+
36+
extension SyntaxTransformVisitor {
37+
public func visit(_ token: TokenSyntax) -> [ResultType] { [] }
38+
public func visit(_ node: UnknownSyntax) -> [ResultType] { [] }
39+
40+
% for node in SYNTAX_NODES:
41+
% if is_visitable(node):
42+
/// Visiting `${node.name}` specifically.
43+
/// - Parameter node: the node we are visiting.
44+
/// - Returns: nil by default.
45+
public func visit(_ node: ${node.name}) -> [ResultType] {
46+
% if node.is_base():
47+
// Avoid calling into visitChildren if possible.
48+
if !node.raw.layoutView!.children.isEmpty {
49+
return visitChildren(node)
50+
}
51+
return []
52+
% else:
53+
// Avoid calling into visitChildren if possible.
54+
if !node.raw.layoutView!.children.isEmpty {
55+
return visitChildren(node)
56+
}
57+
return []
58+
% end
59+
}
60+
% end
61+
% end
62+
63+
public func visit(_ data: Syntax) -> [ResultType] {
64+
switch data.raw.kind {
65+
case .token:
66+
let node = TokenSyntax(data)!
67+
return visit(node)
68+
case .unknown:
69+
let node = UnknownSyntax(data)!
70+
return visit(node)
71+
// The implementation of every generated case goes into its own function. This
72+
// circumvents an issue where the compiler allocates stack space for every
73+
// case statement next to each other in debug builds, causing it to allocate
74+
// ~50KB per call to this function. rdar://55929175
75+
% for node in NON_BASE_SYNTAX_NODES:
76+
case .${node.swift_syntax_kind}:
77+
let node = ${node.name}(data)!
78+
return visit(node)
79+
% end
80+
}
81+
}
82+
83+
public func visit(_ data: ExprSyntax) -> [ResultType] {
84+
switch data.raw.kind {
85+
% for node in NON_BASE_SYNTAX_NODES:
86+
% if node.base_kind == "Expr":
87+
case .${node.swift_syntax_kind}:
88+
let node = data.as(${node.name}.self)!
89+
return visit(node)
90+
% end
91+
% end
92+
default:
93+
fatalError("Not expression?")
94+
}
95+
}
96+
97+
public func visit(_ data: PatternSyntax) -> [ResultType] {
98+
switch data.raw.kind {
99+
% for node in NON_BASE_SYNTAX_NODES:
100+
% if node.base_kind == "Pattern":
101+
case .${node.swift_syntax_kind}:
102+
let node = data.as(${node.name}.self)!
103+
return visit(node)
104+
% end
105+
% end
106+
default:
107+
fatalError("Not expression?")
108+
}
109+
}
110+
111+
public func visit(_ data: TypeSyntax) -> [ResultType] {
112+
switch data.raw.kind {
113+
% for node in NON_BASE_SYNTAX_NODES:
114+
% if node.base_kind == "Type":
115+
case .${node.swift_syntax_kind}:
116+
let node = data.as(${node.name}.self)!
117+
return visit(node)
118+
% end
119+
% end
120+
default:
121+
fatalError("Not expression?")
122+
}
123+
}
124+
125+
public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {
126+
let syntaxNode = Syntax(node)
127+
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).flatMap { rawChild in
128+
let child = Syntax(SyntaxData(rawChild, parent: syntaxNode))
129+
return visit(child)
130+
}
131+
}
132+
}

Sources/SwiftSyntax/SyntaxVisitor.swift.gyb

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -141,117 +141,3 @@ open class SyntaxVisitor {
141141
}
142142
}
143143
}
144-
145-
public protocol SyntaxTransformVisitor {
146-
associatedtype ResultType = Void
147-
148-
func visit(_ token: TokenSyntax) -> [ResultType]
149-
func visit(_ node: UnknownSyntax) -> [ResultType]
150-
151-
% for node in SYNTAX_NODES:
152-
% if is_visitable(node):
153-
/// Visiting `${node.name}` specifically.
154-
/// - Parameter node: the node we are visiting.
155-
/// - Returns: the sum of whatever the child visitors return.
156-
func visit(_ node: ${node.name}) -> [ResultType]
157-
% end
158-
% end
159-
}
160-
161-
extension SyntaxTransformVisitor {
162-
public func visit(_ token: TokenSyntax) -> [ResultType] { [] }
163-
public func visit(_ node: UnknownSyntax) -> [ResultType] { [] }
164-
165-
% for node in SYNTAX_NODES:
166-
% if is_visitable(node):
167-
/// Visiting `${node.name}` specifically.
168-
/// - Parameter node: the node we are visiting.
169-
/// - Returns: nil by default.
170-
public func visit(_ node: ${node.name}) -> [ResultType] {
171-
% if node.is_base():
172-
// Avoid calling into visitChildren if possible.
173-
if !node.raw.layoutView!.children.isEmpty {
174-
return visitChildren(node)
175-
}
176-
return []
177-
% else:
178-
// Avoid calling into visitChildren if possible.
179-
if !node.raw.layoutView!.children.isEmpty {
180-
return visitChildren(node)
181-
}
182-
return []
183-
% end
184-
}
185-
% end
186-
% end
187-
188-
public func visit(_ data: SyntaxData) -> [ResultType] {
189-
switch data.raw.kind {
190-
case .token:
191-
let node = TokenSyntax(data)
192-
return visit(node)
193-
case .unknown:
194-
let node = UnknownSyntax(data)
195-
return visit(node)
196-
// The implementation of every generated case goes into its own function. This
197-
// circumvents an issue where the compiler allocates stack space for every
198-
// case statement next to each other in debug builds, causing it to allocate
199-
// ~50KB per call to this function. rdar://55929175
200-
% for node in NON_BASE_SYNTAX_NODES:
201-
case .${node.swift_syntax_kind}:
202-
let node = ${node.name}(data)
203-
return visit(node)
204-
% end
205-
}
206-
}
207-
208-
public func visit(_ data: ExprSyntax) -> [ResultType] {
209-
switch data.raw.kind {
210-
% for node in NON_BASE_SYNTAX_NODES:
211-
% if node.base_kind == "Expr":
212-
case .${node.swift_syntax_kind}:
213-
let node = data.as(${node.name}.self)!
214-
return visit(node)
215-
% end
216-
% end
217-
default:
218-
fatalError("Not expression?")
219-
}
220-
}
221-
222-
public func visit(_ data: PatternSyntax) -> [ResultType] {
223-
switch data.raw.kind {
224-
% for node in NON_BASE_SYNTAX_NODES:
225-
% if node.base_kind == "Pattern":
226-
case .${node.swift_syntax_kind}:
227-
let node = data.as(${node.name}.self)!
228-
return visit(node)
229-
% end
230-
% end
231-
default:
232-
fatalError("Not expression?")
233-
}
234-
}
235-
236-
public func visit(_ data: TypeSyntax) -> [ResultType] {
237-
switch data.raw.kind {
238-
% for node in NON_BASE_SYNTAX_NODES:
239-
% if node.base_kind == "Type":
240-
case .${node.swift_syntax_kind}:
241-
let node = data.as(${node.name}.self)!
242-
return visit(node)
243-
% end
244-
% end
245-
default:
246-
fatalError("Not expression?")
247-
}
248-
}
249-
250-
public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {
251-
let syntaxNode = Syntax(node)
252-
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).flatMap { childRaw in
253-
let childData = SyntaxData(childRaw, parent: syntaxNode)
254-
return visit(childData)
255-
}
256-
}
257-
}

0 commit comments

Comments
 (0)