Skip to content

Commit eef8258

Browse files
committed
Split SyntaxRewriter into three files
1 parent d47cce9 commit eef8258

File tree

7 files changed

+7075
-7011
lines changed

7 files changed

+7075
-7011
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 SyntaxFactory.swift.gyb.
7+
//// Do Not Edit Directly!
8+
//===--------- SyntaxAnyVisitor.swift - Syntax any visitor class ----------===//
9+
//
10+
// This source file is part of the Swift.org open source project
11+
//
12+
// Copyright (c) 2014 - 2017 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+
/// A `SyntaxVisitor` that can visit the nodes as generic `Syntax` values.
21+
///
22+
/// This subclass of `SyntaxVisitor` is slower than the type-specific visitation
23+
/// of `SyntaxVisitor`. Use `SyntaxAnyVisitor` if the `visitAny(_)` function
24+
/// would be useful to have, otherwise inherit from `SyntaxVisitor`.
25+
///
26+
/// This works by overriding the type-specific visit function that delegate to
27+
/// `visitAny(_)`. A subclass that provides a custom type-specific visit
28+
/// function, should also call `visitAny(_)` in its implementation, if calling
29+
/// `visitAny` is needed:
30+
///
31+
/// struct MyVisitor: SyntaxAnyVisitor {
32+
/// func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
33+
/// <code>
34+
/// }
35+
///
36+
/// func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
37+
/// <code>
38+
/// // Call this to pass tokens to `visitAny(_)` as well if needed
39+
/// visitAny(token)
40+
/// }
41+
///
42+
open class SyntaxAnyVisitor: SyntaxVisitor {
43+
/// Visiting `UnknownSyntax` specifically.
44+
/// - Parameter node: the node we are visiting.
45+
/// - Returns: how should we continue visiting.
46+
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
47+
return .visitChildren
48+
}
49+
50+
/// The function called after visiting the node and its descendents.
51+
/// - node: the node we just finished visiting.
52+
open func visitAnyPost(_ node: Syntax) {}
53+
54+
// MARK: Override type specific visit methods
55+
56+
override open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
57+
return visitAny(token._syntaxNode)
58+
}
59+
60+
override open func visitPost(_ node: TokenSyntax) {
61+
visitAnyPost(node._syntaxNode)
62+
}
63+
64+
override open func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind {
65+
return visitAny(node._syntaxNode)
66+
}
67+
68+
override open func visitPost(_ node: UnknownSyntax) {
69+
visitAnyPost(node._syntaxNode)
70+
}
71+
72+
% for node in SYNTAX_NODES:
73+
% if is_visitable(node):
74+
override open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
75+
return visitAny(node._syntaxNode)
76+
}
77+
78+
override open func visitPost(_ node: ${node.name}) {
79+
visitAnyPost(node._syntaxNode)
80+
}
81+
% end
82+
% end
83+
84+
}

Sources/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 1 addition & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
// This source file is part of the Swift.org open source project
1111
//
12-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
12+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
1313
// Licensed under Apache License v2.0 with Runtime Library Exception
1414
//
1515
// See https://swift.org/LICENSE.txt for license information
@@ -217,186 +217,3 @@ open class SyntaxRewriter {
217217

218218
}
219219
}
220-
221-
/// The enum describes how the SyntaxVistor should continue after visiting
222-
/// the current node.
223-
public enum SyntaxVisitorContinueKind {
224-
225-
/// The visitor should visit the descendents of the current node.
226-
case visitChildren
227-
228-
/// The visitor should avoid visiting the descendents of the current node.
229-
case skipChildren
230-
}
231-
232-
open class SyntaxVisitor {
233-
public init() {}
234-
235-
/// Walk all nodes of the given syntax tree, calling the corresponding `visit`
236-
/// function for every node that is being visited.
237-
public func walk<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
238-
visit(node.data)
239-
}
240-
241-
% for node in SYNTAX_NODES:
242-
% if is_visitable(node):
243-
/// Visiting `${node.name}` specifically.
244-
/// - Parameter node: the node we are visiting.
245-
/// - Returns: how should we continue visiting.
246-
open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
247-
return .visitChildren
248-
}
249-
250-
/// The function called after visiting `${node.name}` and its descendents.
251-
/// - node: the node we just finished visiting.
252-
open func visitPost(_ node: ${node.name}) {}
253-
% end
254-
% end
255-
256-
/// Visiting `TokenSyntax` specifically.
257-
/// - Parameter node: the node we are visiting.
258-
/// - Returns: how should we continue visiting.
259-
open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
260-
return .visitChildren
261-
}
262-
263-
/// The function called after visiting the node and its descendents.
264-
/// - node: the node we just finished visiting.
265-
open func visitPost(_ node: TokenSyntax) {}
266-
267-
/// Visiting `UnknownSyntax` specifically.
268-
/// - Parameter node: the node we are visiting.
269-
/// - Returns: how should we continue visiting.
270-
open func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind {
271-
return .visitChildren
272-
}
273-
274-
/// The function called after visiting the node and its descendents.
275-
/// - node: the node we just finished visiting.
276-
open func visitPost(_ node: UnknownSyntax) {}
277-
278-
% for node in SYNTAX_NODES:
279-
/// Implementation detail of doVisit(_:_:). Do not call directly.
280-
private func visitImpl${node.name}(_ data: SyntaxData) {
281-
% if node.is_base():
282-
let node = Unknown${node.name}(data)
283-
let needsChildren = (visit(node) == .visitChildren)
284-
// Avoid calling into visitChildren if possible.
285-
if needsChildren && node.raw.numberOfChildren > 0 {
286-
visitChildren(node)
287-
}
288-
visitPost(node)
289-
% else:
290-
let node = ${node.name}(data)
291-
let needsChildren = (visit(node) == .visitChildren)
292-
// Avoid calling into visitChildren if possible.
293-
if needsChildren && node.raw.numberOfChildren > 0 {
294-
visitChildren(node)
295-
}
296-
visitPost(node)
297-
% end
298-
}
299-
300-
% end
301-
302-
private func visit(_ data: SyntaxData) {
303-
switch data.raw.kind {
304-
case .token:
305-
let node = TokenSyntax(data)
306-
_ = visit(node)
307-
// No children to visit.
308-
visitPost(node)
309-
case .unknown:
310-
let node = UnknownSyntax(data)
311-
let needsChildren = (visit(node) == .visitChildren)
312-
// Avoid calling into visitChildren if possible.
313-
if needsChildren && node.raw.numberOfChildren > 0 {
314-
visitChildren(node)
315-
}
316-
visitPost(node)
317-
// The implementation of every generated case goes into its own function. This
318-
// circumvents an issue where the compiler allocates stack space for every
319-
// case statement next to each other in debug builds, causing it to allocate
320-
// ~50KB per call to this function. rdar://55929175
321-
% for node in SYNTAX_NODES:
322-
case .${node.swift_syntax_kind}:
323-
visitImpl${node.name}(data)
324-
% end
325-
}
326-
}
327-
328-
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
329-
let syntaxNode = Syntax(node)
330-
let parentBox = SyntaxBox(syntaxNode)
331-
for childRaw in PresentRawSyntaxChildren(syntaxNode) {
332-
let childData = SyntaxData(childRaw, parentBox: parentBox)
333-
visit(childData)
334-
}
335-
}
336-
}
337-
338-
/// A `SyntaxVisitor` that can visit the nodes as generic `Syntax` values.
339-
///
340-
/// This subclass of `SyntaxVisitor` is slower than the type-specific visitation
341-
/// of `SyntaxVisitor`. Use `SyntaxAnyVisitor` if the `visitAny(_)` function
342-
/// would be useful to have, otherwise inherit from `SyntaxVisitor`.
343-
///
344-
/// This works by overriding the type-specific visit function that delegate to
345-
/// `visitAny(_)`. A subclass that provides a custom type-specific visit
346-
/// function, should also call `visitAny(_)` in its implementation, if calling
347-
/// `visitAny` is needed:
348-
///
349-
/// struct MyVisitor: SyntaxAnyVisitor {
350-
/// func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
351-
/// <code>
352-
/// }
353-
///
354-
/// func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
355-
/// <code>
356-
/// // Call this to pass tokens to `visitAny(_)` as well if needed
357-
/// visitAny(token)
358-
/// }
359-
///
360-
open class SyntaxAnyVisitor: SyntaxVisitor {
361-
/// Visiting `UnknownSyntax` specifically.
362-
/// - Parameter node: the node we are visiting.
363-
/// - Returns: how should we continue visiting.
364-
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
365-
return .visitChildren
366-
}
367-
368-
/// The function called after visiting the node and its descendents.
369-
/// - node: the node we just finished visiting.
370-
open func visitAnyPost(_ node: Syntax) {}
371-
372-
// MARK: Override type specific visit methods
373-
374-
override open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
375-
return visitAny(token._syntaxNode)
376-
}
377-
378-
override open func visitPost(_ node: TokenSyntax) {
379-
visitAnyPost(node._syntaxNode)
380-
}
381-
382-
override open func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind {
383-
return visitAny(node._syntaxNode)
384-
}
385-
386-
override open func visitPost(_ node: UnknownSyntax) {
387-
visitAnyPost(node._syntaxNode)
388-
}
389-
390-
% for node in SYNTAX_NODES:
391-
% if is_visitable(node):
392-
override open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
393-
return visitAny(node._syntaxNode)
394-
}
395-
396-
override open func visitPost(_ node: ${node.name}) {
397-
visitAnyPost(node._syntaxNode)
398-
}
399-
% end
400-
% end
401-
402-
}

0 commit comments

Comments
 (0)