Skip to content

Commit 1dfd8c2

Browse files
committed
Allow putting garbage nodes in between any two children of a Syntax node
1 parent fb0f401 commit 1dfd8c2

33 files changed

+38777
-3540
lines changed

Sources/SwiftSyntax/SyntaxAnyVisitor.swift.gyb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
/// }
4141
///
4242
open class SyntaxAnyVisitor: SyntaxVisitor {
43-
/// Visiting `UnknownSyntax` specifically.
44-
/// - Parameter node: the node we are visiting.
45-
/// - Returns: how should we continue visiting.
43+
4644
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
45+
if node.is(GarbageNodesSyntax.self) {
46+
return .skipChildren
47+
}
4748
return .visitChildren
4849
}
4950

Sources/SwiftSyntax/SyntaxBuilders.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct ${Builder} {
3434
Array<RawSyntax?>(repeating: nil, count: ${len(node.children)})
3535

3636
internal init() {}
37-
% for child in node.children:
37+
% for child in node.non_garbage_children:
3838
% child_node = NODE_MAP.get(child.syntax_kind)
3939
% if child_node and child_node.is_syntax_collection():
4040
% child_elt = child.collection_element_name

Sources/SwiftSyntax/SyntaxFactory.swift.gyb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public enum SyntaxFactory {
5252
% elif node.children:
5353
% child_params = []
5454
% for child in node.children:
55-
% param_type = child.type_name
56-
% if child.is_optional:
57-
% param_type = param_type + "?"
55+
% param_type = child.type_name
56+
% if child.is_optional:
57+
% param_type = param_type + "?"
58+
% end
59+
% if child.is_garbage_nodes():
60+
% child_params.append("garbage %s: %s = nil" % (child.swift_name, param_type))
61+
% else:
5862
% child_params.append("%s: %s" % (child.swift_name, param_type))
63+
% end
64+
% end
5965
% child_params = ', '.join(child_params)
6066
public static func make${node.syntax_kind}(${child_params}) -> ${node.name} {
6167
let layout: [RawSyntax?] = [

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
115115
% # ===============
116116
% # Adding children
117117
% # ===============
118-
% if child_node and child_node.is_syntax_collection():
118+
% if child_node and child_node.is_syntax_collection() and not child.is_garbage_nodes():
119119
% child_elt = child.collection_element_name
120120
% child_elt_type = child_node.collection_element_type
121121
% if not child_elt:

Sources/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ open class SyntaxRewriter {
3434
/// - Parameter node: the node that is being visited
3535
/// - Returns: the rewritten node
3636
open func visit(_ node: ${node.name}) -> ${node.base_type} {
37+
% if node.name == 'GarbageNodesSyntax':
38+
return ${node.base_type}(node)
39+
% else:
3740
return ${node.base_type}(visitChildren(node))
41+
% end
3842
}
3943

4044
% end

Sources/SwiftSyntax/SyntaxVisitor.swift.gyb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ open class SyntaxVisitor {
4343
/// - Parameter node: the node we are visiting.
4444
/// - Returns: how should we continue visiting.
4545
open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
46+
% if node.name == 'GarbageNodesSyntax':
47+
return .skipChildren
48+
% else:
4649
return .visitChildren
50+
% end
4751
}
4852

4953
/// The function called after visiting `${node.name}` and its descendents.

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ extension SyntaxNode {
6767
return CodeBlockSyntax(asSyntaxData)
6868
}
6969

70+
public var isGarbageNodes: Bool { return raw.kind == .garbageNodes }
71+
public var asGarbageNodes: GarbageNodesSyntax? {
72+
guard isGarbageNodes else { return nil }
73+
return GarbageNodesSyntax(asSyntaxData)
74+
}
75+
7076
public var isInOutExpr: Bool { return raw.kind == .inOutExpr }
7177
public var asInOutExpr: InOutExprSyntax? {
7278
guard isInOutExpr else { return nil }
@@ -1557,6 +1563,8 @@ extension Syntax {
15571563
return node
15581564
case .codeBlock(let node):
15591565
return node
1566+
case .garbageNodes(let node):
1567+
return node
15601568
case .inOutExpr(let node):
15611569
return node
15621570
case .poundColumnExpr(let node):

Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
/// }
3636
///
3737
open class SyntaxAnyVisitor: SyntaxVisitor {
38-
/// Visiting `UnknownSyntax` specifically.
39-
/// - Parameter node: the node we are visiting.
40-
/// - Returns: how should we continue visiting.
38+
4139
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
40+
if node.is(GarbageNodesSyntax.self) {
41+
return .skipChildren
42+
}
4243
return .visitChildren
4344
}
4445

@@ -120,6 +121,13 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
120121
override open func visitPost(_ node: CodeBlockSyntax) {
121122
visitAnyPost(node._syntaxNode)
122123
}
124+
override open func visit(_ node: GarbageNodesSyntax) -> SyntaxVisitorContinueKind {
125+
return visitAny(node._syntaxNode)
126+
}
127+
128+
override open func visitPost(_ node: GarbageNodesSyntax) {
129+
visitAnyPost(node._syntaxNode)
130+
}
123131
override open func visit(_ node: InOutExprSyntax) -> SyntaxVisitorContinueKind {
124132
return visitAny(node._syntaxNode)
125133
}

0 commit comments

Comments
 (0)