Skip to content

Commit 4735aff

Browse files
committed
Significantly simplify SwiftSyntaxBuilder’s implementation
This significantly simplifies the implementation of SwiftSyntaxBuilder. The main cleanups are: - Removing the separate syntax node hierarchy in SwiftSyntaxBuilder and instead re-using the SwiftSyntax hierarchy - This is possible since `BasicFormat` now formats an entire SwiftSyntax tree instead of requiring the careful intraction with SwiftSyntaxBuilder’s `Buildable` types - All convenience intializers form SwiftSyntaxBuilder are now extensions on the SwiftSyntax nodes. We can even consider sinking them down into SwiftSyntax again - Removing the `ExpressibleAs*` types - With the integration of the parser into SwiftSyntaxBuilder, you create way fewer syntax nodes explicitly. Looking at the test cases, I think the regressions on the client side aren’t too significant and this hugely simplifies the SwiftSyntaxBuilder’s implementation - Having written a fair amount of SwiftSyntaxBuilder code myself now, I never found the `ExpressibleAs*` protocols super useful because it was non-trivial to discover which types are expressible as other types.
1 parent 587d3ad commit 4735aff

File tree

82 files changed

+8089
-32757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+8089
-32757
lines changed

CodeGeneration/Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ let package = Package(
4646
"Classification.swift.gyb",
4747
"CommonNodes.swift.gyb",
4848
"DeclNodes.swift.gyb",
49-
"ExpressibleAsConformances.swift.gyb",
5049
"ExprNodes.swift.gyb",
5150
"GenericNodes.swift.gyb",
5251
"NodeSerializationCodes.swift.gyb",

CodeGeneration/Sources/SyntaxSupport/ExpressibleAsConformances.swift.gyb

Lines changed: 0 additions & 29 deletions
This file was deleted.

CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public let SYNTAX_NODES: [Node] = COMMON_NODES
13+
public let SYNTAX_NODES: [Node] = (COMMON_NODES
1414
+ EXPR_NODES
1515
+ DECL_NODES
1616
+ ATTRIBUTE_NODES
1717
+ STMT_NODES
1818
+ GENERIC_NODES
1919
+ TYPE_NODES
2020
+ PATTERN_NODES
21-
+ AVAILABILITY_NODES
21+
+ AVAILABILITY_NODES).sorted { $0.name < $1.name }
2222

2323
/// A lookup table of nodes indexed by their kind.
2424
public let SYNTAX_NODE_MAP: [String: Node] = Dictionary(

CodeGeneration/Sources/SyntaxSupport/gyb_generated/ExpressibleAsConformances.swift

Lines changed: 0 additions & 65 deletions
This file was deleted.

CodeGeneration/Sources/SyntaxSupport/gyb_helpers/ExpressibleAsConformances.py

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
from .utils import make_swift_child, make_swift_node
22
from .BuilderInitializableTypes import BUILDER_INITIALIZABLE_TYPES
3-
from .ExpressibleAsConformances import SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ public class CodeGenerationFormat: BasicFormat {
2121
let formatted = super.visit(node)
2222
return formatted.withLeadingTrivia(indentedNewline + (formatted.leadingTrivia ?? []))
2323
}
24+
25+
public override func visit(_ node: CodeBlockItemSyntax) -> Syntax {
26+
if node.parent?.parent?.is(SourceFileSyntax.self) == true, !node.item.is(ImportDeclSyntax.self) {
27+
let formatted = super.visit(node)
28+
return formatted.withLeadingTrivia(indentedNewline + indentedNewline + (formatted.leadingTrivia ?? []))
29+
} else {
30+
return super.visit(node)
31+
}
32+
}
2433
}

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ public extension Child {
3232
flattened(indentedDocumentation: description ?? "")
3333
}
3434

35-
/// Generate a Swift expression that creates a proper SwiftSyntax node of type
36-
/// `type.syntax` from a variable named `varName` of type `type.buildable` that
37-
/// represents this child node.
38-
func generateExprBuildSyntaxNode(varName: ExpressibleAsExprBuildable) -> ExpressibleAsExprBuildable {
39-
if type.isToken {
40-
return FunctionCallExpr(calledExpression: MemberAccessExpr(base: type.optionalChained(expr: varName), name: "buildToken"))
41-
} else {
42-
let expr = type.optionalChained(expr: varName)
43-
return FunctionCallExpr(calledExpression: MemberAccessExpr(base: expr, name: "build\(type.baseName)"))
44-
}
45-
}
46-
4735
/// If this node is a token that can't contain arbitrary text, generate a Swift
4836
/// `assert` statement that verifies the variable with name var_name and of type
4937
/// `TokenSyntax` contains one of the supported text options. Otherwise return `nil`.

CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public extension Node {
3131
var documentation: String {
3232
let description = self.description ?? ""
3333
if description.isEmpty && isSyntaxCollection {
34-
return "`\(syntaxKind)` represents a collection of `\(collectionElementType.buildableBaseName)`"
34+
return "`\(syntaxKind)` represents a collection of `\(collectionElementType.syntaxBaseName)`"
3535
} else {
3636
return flattened(indentedDocumentation: description)
3737
}
@@ -51,9 +51,8 @@ public extension Node {
5151
}
5252

5353
static func from(type: SyntaxBuildableType) -> Node {
54-
let baseName = type.baseName
55-
guard let node = SYNTAX_NODE_MAP[baseName] else {
56-
fatalError("Base name \(baseName) does not have a syntax node")
54+
guard let node = SYNTAX_NODE_MAP[type.syntaxKind] else {
55+
fatalError("Base name \(type.syntaxKind) does not have a syntax node")
5756
}
5857
return node
5958
}

0 commit comments

Comments
 (0)