Skip to content

Significantly simplify SwiftSyntaxBuilder’s implementation #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CodeGeneration/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ let package = Package(
"Classification.swift.gyb",
"CommonNodes.swift.gyb",
"DeclNodes.swift.gyb",
"ExpressibleAsConformances.swift.gyb",
"ExprNodes.swift.gyb",
"GenericNodes.swift.gyb",
"NodeSerializationCodes.swift.gyb",
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
//
//===----------------------------------------------------------------------===//

public let SYNTAX_NODES: [Node] = COMMON_NODES
public let SYNTAX_NODES: [Node] = (COMMON_NODES
+ EXPR_NODES
+ DECL_NODES
+ ATTRIBUTE_NODES
+ STMT_NODES
+ GENERIC_NODES
+ TYPE_NODES
+ PATTERN_NODES
+ AVAILABILITY_NODES
+ AVAILABILITY_NODES).sorted { $0.name < $1.name }

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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .utils import make_swift_child, make_swift_node
from .BuilderInitializableTypes import BUILDER_INITIALIZABLE_TYPES
from .ExpressibleAsConformances import SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES
9 changes: 9 additions & 0 deletions CodeGeneration/Sources/Utils/CodeGenerationFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ public class CodeGenerationFormat: BasicFormat {
let formatted = super.visit(node)
return formatted.withLeadingTrivia(indentedNewline + (formatted.leadingTrivia ?? []))
}

public override func visit(_ node: CodeBlockItemSyntax) -> Syntax {
if node.parent?.parent?.is(SourceFileSyntax.self) == true, !node.item.is(ImportDeclSyntax.self) {
let formatted = super.visit(node)
return formatted.withLeadingTrivia(indentedNewline + indentedNewline + (formatted.leadingTrivia ?? []))
} else {
return super.visit(node)
}
}
}
12 changes: 0 additions & 12 deletions CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ public extension Child {
flattened(indentedDocumentation: description ?? "")
}

/// Generate a Swift expression that creates a proper SwiftSyntax node of type
/// `type.syntax` from a variable named `varName` of type `type.buildable` that
/// represents this child node.
func generateExprBuildSyntaxNode(varName: ExpressibleAsExprBuildable) -> ExpressibleAsExprBuildable {
if type.isToken {
return FunctionCallExpr(calledExpression: MemberAccessExpr(base: type.optionalChained(expr: varName), name: "buildToken"))
} else {
let expr = type.optionalChained(expr: varName)
return FunctionCallExpr(calledExpression: MemberAccessExpr(base: expr, name: "build\(type.baseName)"))
}
}

/// If this node is a token that can't contain arbitrary text, generate a Swift
/// `assert` statement that verifies the variable with name var_name and of type
/// `TokenSyntax` contains one of the supported text options. Otherwise return `nil`.
Expand Down
7 changes: 3 additions & 4 deletions CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public extension Node {
var documentation: String {
let description = self.description ?? ""
if description.isEmpty && isSyntaxCollection {
return "`\(syntaxKind)` represents a collection of `\(collectionElementType.buildableBaseName)`"
return "`\(syntaxKind)` represents a collection of `\(collectionElementType.syntaxBaseName)`"
} else {
return flattened(indentedDocumentation: description)
}
Expand All @@ -51,9 +51,8 @@ public extension Node {
}

static func from(type: SyntaxBuildableType) -> Node {
let baseName = type.baseName
guard let node = SYNTAX_NODE_MAP[baseName] else {
fatalError("Base name \(baseName) does not have a syntax node")
guard let node = SYNTAX_NODE_MAP[type.syntaxKind] else {
fatalError("Base name \(type.syntaxKind) does not have a syntax node")
}
return node
}
Expand Down
Loading