Skip to content

Commit 2ff1171

Browse files
authored
Merge pull request #514 from ahoppen/pr/garbage-syntax
Allow putting garbage nodes in between any two children of a Syntax node
2 parents 413fe13 + 1258671 commit 2ff1171

36 files changed

+30497
-2987
lines changed

Sources/SwiftSyntax/SyntaxAnyVisitor.swift.gyb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
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.
4643
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
4744
return .visitChildren
4845
}

Sources/SwiftSyntax/SyntaxBuilders.swift.gyb

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

3636
internal init() {}
37-
% for child in node.children:
37+
% # SyntaxBuilder doesn't support adding garbage nodes at the moment
38+
% # This could be added in the future, if needed.
39+
% for child in node.non_garbage_children:
3840
% child_node = NODE_MAP.get(child.syntax_kind)
3941
% if child_node and child_node.is_syntax_collection():
4042
% child_elt = child.collection_element_name

Sources/SwiftSyntax/SyntaxFactory.swift.gyb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ 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+
% # It would be nice if we could label all of these arguments 'garbage'.
61+
% # But if we do this, we hit https://github.com/apple/swift/issues/60274.
62+
% child_params.append("_ %s: %s = nil" % (child.swift_name, param_type))
63+
% else:
5864
% child_params.append("%s: %s" % (child.swift_name, param_type))
65+
% end
66+
% end
5967
% child_params = ', '.join(child_params)
6068
public static func make${node.syntax_kind}(${child_params}) -> ${node.name} {
6169
let layout: [RawSyntax?] = [

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
115115
% # ===============
116116
% # Adding children
117117
% # ===============
118-
% if child_node and child_node.is_syntax_collection():
118+
% # We don't currently support adding elements to a specific garbage collection.
119+
% # If needed, this could be added in the future, but for now withGarbage should be sufficient.
120+
% if child_node and child_node.is_syntax_collection() and not child.is_garbage_nodes():
119121
% child_elt = child.collection_element_name
120122
% child_elt_type = child_node.collection_element_type
121123
% if not child_elt:

Sources/SwiftSyntax/SyntaxTreeViewMode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public enum SyntaxTreeViewMode {
3535
case .sourceAccurate:
3636
return node.presence == .present
3737
case .fixedUp:
38-
return true
38+
return node.kind != .garbageNodes
3939
case ._all:
4040
return true
4141
}

Sources/SwiftSyntax/Trivia.swift.gyb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ extension Trivia: ExpressibleByArrayLiteral {
177177
}
178178
}
179179

180+
extension Trivia: TextOutputStreamable {
181+
/// Prints the provided trivia as they would be written in a source file.
182+
///
183+
/// - Parameter stream: The stream to which to print the trivia.
184+
public func write<Target>(to target: inout Target)
185+
where Target: TextOutputStream {
186+
for piece in pieces {
187+
piece.write(to: &target)
188+
}
189+
}
190+
}
191+
192+
extension Trivia: CustomStringConvertible {
193+
public var description: String {
194+
var description = ""
195+
self.write(to: &description)
196+
return description
197+
}
198+
}
199+
180200
/// Concatenates two collections of `Trivia` into one collection.
181201
public func +(lhs: Trivia, rhs: Trivia) -> Trivia {
182202
return Trivia(pieces: lhs.pieces + rhs.pieces)

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ extension SyntaxNode {
103103
return CodeBlockSyntax(asSyntaxData)
104104
}
105105

106+
public var isGarbageNodes: Bool { return raw.kind == .garbageNodes }
107+
public var asGarbageNodes: GarbageNodesSyntax? {
108+
guard isGarbageNodes else { return nil }
109+
return GarbageNodesSyntax(asSyntaxData)
110+
}
111+
106112
public var isInOutExpr: Bool { return raw.kind == .inOutExpr }
107113
public var asInOutExpr: InOutExprSyntax? {
108114
guard isInOutExpr else { return nil }
@@ -1601,6 +1607,8 @@ extension Syntax {
16011607
return node
16021608
case .codeBlock(let node):
16031609
return node
1610+
case .garbageNodes(let node):
1611+
return node
16041612
case .inOutExpr(let node):
16051613
return node
16061614
case .poundColumnExpr(let node):

Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
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.
4138
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
4239
return .visitChildren
4340
}
@@ -162,6 +159,13 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
162159
override open func visitPost(_ node: CodeBlockSyntax) {
163160
visitAnyPost(node._syntaxNode)
164161
}
162+
override open func visit(_ node: GarbageNodesSyntax) -> SyntaxVisitorContinueKind {
163+
return visitAny(node._syntaxNode)
164+
}
165+
166+
override open func visitPost(_ node: GarbageNodesSyntax) {
167+
visitAnyPost(node._syntaxNode)
168+
}
165169
override open func visit(_ node: InOutExprSyntax) -> SyntaxVisitorContinueKind {
166170
return visitAny(node._syntaxNode)
167171
}

0 commit comments

Comments
 (0)