@@ -29,7 +29,14 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
29
29
"""
30
30
)
31
31
32
- try ! ClassDeclSyntax ( " open class SyntaxVisitor " ) {
32
+ DeclSyntax (
33
+ """
34
+ /// An instance of `ThrowingSyntaxVisitor` that never throws.
35
+ public typealias SyntaxVisitor = ThrowingSyntaxVisitor<Never>
36
+ """
37
+ )
38
+
39
+ try ! ClassDeclSyntax ( " open class ThrowingSyntaxVisitor<E: Error> " ) {
33
40
DeclSyntax ( " public let viewMode: SyntaxTreeViewMode " )
34
41
35
42
DeclSyntax (
@@ -44,8 +51,8 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
44
51
"""
45
52
/// Walk all nodes of the given syntax tree, calling the corresponding `visit`
46
53
/// function for every node that is being visited.
47
- public func walk(_ node: some SyntaxProtocol) {
48
- dispatchVisit(Syntax(node))
54
+ public func walk(_ node: some SyntaxProtocol) throws (E) {
55
+ try dispatchVisit(Syntax(node))
49
56
}
50
57
"""
51
58
)
@@ -57,7 +64,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
57
64
/// - Parameter node: the node we are visiting.
58
65
/// - Returns: how should we continue visiting.
59
66
\( node. apiAttributes ( ) ) \
60
- open func visit(_ node: \( node. kind. syntaxType) ) -> SyntaxVisitorContinueKind {
67
+ open func visit(_ node: \( node. kind. syntaxType) ) throws (E) -> SyntaxVisitorContinueKind {
61
68
return .visitChildren
62
69
}
63
70
"""
@@ -68,7 +75,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
68
75
/// The function called after visiting \( raw: node. kind. doccLink) and its descendants.
69
76
/// - node: the node we just finished visiting.
70
77
\( node. apiAttributes ( ) ) \
71
- open func visitPost(_ node: \( node. kind. syntaxType) ) {}
78
+ open func visitPost(_ node: \( node. kind. syntaxType) ) throws (E) {}
72
79
"""
73
80
)
74
81
}
@@ -78,7 +85,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
78
85
/// Visiting ``TokenSyntax`` specifically.
79
86
/// - Parameter token: the token we are visiting.
80
87
/// - Returns: how should we continue visiting.
81
- open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
88
+ open func visit(_ token: TokenSyntax) throws (E) -> SyntaxVisitorContinueKind {
82
89
return .visitChildren
83
90
}
84
91
"""
@@ -88,7 +95,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
88
95
"""
89
96
/// The function called after visiting the node and its descendants.
90
97
/// - node: the node we just finished visiting.
91
- open func visitPost(_ node: TokenSyntax) {}
98
+ open func visitPost(_ node: TokenSyntax) throws (E) {}
92
99
"""
93
100
)
94
101
@@ -98,10 +105,10 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
98
105
DeclSyntax (
99
106
"""
100
107
@inline(never)
101
- private func visitTokenSyntaxImpl(_ node: Syntax) {
102
- _ = visit(TokenSyntax(unsafeCasting: node))
108
+ private func visitTokenSyntaxImpl(_ node: Syntax) throws (E) {
109
+ _ = try visit(TokenSyntax(unsafeCasting: node))
103
110
// No children to visit.
104
- visitPost(TokenSyntax(unsafeCasting: node))
111
+ try visitPost(TokenSyntax(unsafeCasting: node))
105
112
}
106
113
"""
107
114
)
@@ -110,11 +117,11 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
110
117
DeclSyntax (
111
118
"""
112
119
@inline(never)
113
- private func visit \( node. kind. syntaxType) Impl(_ node: Syntax) {
114
- if visit( \( node. kind. syntaxType) (unsafeCasting: node)) == .visitChildren {
115
- visitChildren(node)
120
+ private func visit \( node. kind. syntaxType) Impl(_ node: Syntax) throws (E) {
121
+ if try visit( \( node. kind. syntaxType) (unsafeCasting: node)) == .visitChildren {
122
+ try visitChildren(node)
116
123
}
117
- visitPost( \( node. kind. syntaxType) (unsafeCasting: node))
124
+ try visitPost( \( node. kind. syntaxType) (unsafeCasting: node))
118
125
}
119
126
"""
120
127
)
@@ -136,7 +143,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
136
143
condition: ExprSyntax ( " DEBUG " ) ,
137
144
elements: . statements(
138
145
try CodeBlockItemListSyntax {
139
- try FunctionDeclSyntax (
146
+ try FunctionDeclSyntax (
140
147
"""
141
148
/// Implementation detail of visit(_:). Do not call directly.
142
149
///
@@ -156,28 +163,28 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
156
163
/// that determines the correct visitation function will be popped of the
157
164
/// stack before the function is being called, making the switch's stack
158
165
/// space transient instead of having it linger in the call stack.
159
- private func visitationFunc(for node: Syntax) -> (Syntax) -> Void
166
+ private func visitationFunc(for node: Syntax) -> (Syntax) throws (E) -> Void
160
167
"""
161
- ) {
162
- try SwitchExprSyntax ( " switch node.raw.kind " ) {
163
- SwitchCaseSyntax ( " case .token: " ) {
164
- StmtSyntax ( " return self.visitTokenSyntaxImpl(_:) " )
165
- }
168
+ ) {
169
+ try SwitchExprSyntax ( " switch node.raw.kind " ) {
170
+ SwitchCaseSyntax ( " case .token: " ) {
171
+ StmtSyntax ( " return self.visitTokenSyntaxImpl(_:) " )
172
+ }
166
173
167
- for node in NON_BASE_SYNTAX_NODES {
168
- SwitchCaseSyntax ( " case . \( node. enumCaseCallName) : " ) {
169
- StmtSyntax (
170
- " return self.visit \( node. kind. syntaxType) Impl(_:) "
171
- )
174
+ for node in NON_BASE_SYNTAX_NODES {
175
+ SwitchCaseSyntax ( " case . \( node. enumCaseCallName) : " ) {
176
+ StmtSyntax (
177
+ " return self.visit \( node. kind. syntaxType) Impl(_:) "
178
+ )
179
+ }
172
180
}
173
181
}
174
182
}
175
- }
176
183
177
184
DeclSyntax (
178
185
"""
179
- private func dispatchVisit(_ node: Syntax) {
180
- return visitationFunc(for: node)(node)
186
+ private func dispatchVisit(_ node: Syntax) throws (E) {
187
+ return try visitationFunc(for: node)(node)
181
188
}
182
189
"""
183
190
)
@@ -190,17 +197,17 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
190
197
CodeBlockItemListSyntax {
191
198
try ! FunctionDeclSyntax (
192
199
"""
193
- private func dispatchVisit(_ node: Syntax)
200
+ private func dispatchVisit(_ node: Syntax) throws (E)
194
201
"""
195
202
) {
196
203
try SwitchExprSyntax ( " switch node.raw.kind " ) {
197
204
SwitchCaseSyntax ( " case .token: " ) {
198
- ExprSyntax ( " self.visitTokenSyntaxImpl(node) " )
205
+ ExprSyntax ( " try self.visitTokenSyntaxImpl(node)" )
199
206
}
200
207
201
208
for node in NON_BASE_SYNTAX_NODES {
202
209
SwitchCaseSyntax ( " case . \( node. enumCaseCallName) : " ) {
203
- ExprSyntax ( " self.visit \( node. kind. syntaxType) Impl(node) " )
210
+ ExprSyntax ( " try self.visit\( node. kind. syntaxType) Impl(node) " )
204
211
}
205
212
}
206
213
}
@@ -213,9 +220,9 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
213
220
214
221
DeclSyntax (
215
222
"""
216
- private func visitChildren(_ node: Syntax) {
223
+ private func visitChildren(_ node: Syntax) throws (E) {
217
224
for case let childDataRef? in node.layoutBuffer where viewMode.shouldTraverse(node: childDataRef.pointee.raw) {
218
- dispatchVisit(Syntax(arena: node.arena, dataRef: childDataRef))
225
+ try dispatchVisit(Syntax(arena: node.arena, dataRef: childDataRef))
219
226
}
220
227
}
221
228
"""
0 commit comments