Skip to content

Commit eb117dc

Browse files
authored
Merge pull request #1128 from kimdv/kimdv/add-codgen-for-swiftsyntax
Move swiftsyntax/Misc gyb to syntax generator
2 parents 819a40d + fa04041 commit eb117dc

File tree

10 files changed

+1514
-1154
lines changed

10 files changed

+1514
-1154
lines changed

CodeGeneration/Package.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let package = Package(
1111
.executable(name: "generate-swiftbasicformat", targets: ["generate-swiftbasicformat"]),
1212
.executable(name: "generate-swiftideutils", targets: ["generate-swiftideutils"]),
1313
.executable(name: "generate-swiftparser", targets: ["generate-swiftparser"]),
14+
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"]),
1415
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
1516
],
1617
dependencies: [
@@ -49,6 +50,16 @@ let package = Package(
4950
]
5051
),
5152
.executableTarget(
53+
name: "generate-swiftsyntax",
54+
dependencies: [
55+
.product(name: "SwiftSyntax", package: "swift-syntax"),
56+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
57+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
58+
"SyntaxSupport",
59+
"Utils"
60+
]
61+
),
62+
.executableTarget(
5263
name: "generate-swiftsyntaxbuilder",
5364
dependencies: [
5465
.product(name: "SwiftSyntax", package: "swift-syntax"),
@@ -62,6 +73,7 @@ let package = Package(
6273
name: "SyntaxSupport",
6374
exclude: [
6475
"gyb_helpers",
76+
"AttributeKinds.swift.gyb",
6577
"AttributeNodes.swift.gyb",
6678
"AvailabilityNodes.swift.gyb",
6779
"BuilderInitializableTypes.swift.gyb",

CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ public let SYNTAX_NODES: [Node] = (COMMON_NODES
2424
public let SYNTAX_NODE_MAP: [String: Node] = Dictionary(
2525
uniqueKeysWithValues: SYNTAX_NODES.map { node in (node.syntaxKind, node) }
2626
)
27+
28+
public let NON_BASE_SYNTAX_NODES = SYNTAX_NODES.filter { !$0.isBase }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import ArgumentParser
14+
import Foundation
15+
import SwiftSyntaxBuilder
16+
import Utils
17+
18+
@main
19+
struct GenerateSwiftSyntax: ParsableCommand {
20+
@Argument(help: "The path to the destination directory where the source files are to be generated")
21+
var generatedPath: String
22+
23+
@Flag(help: "Enable verbose output")
24+
var verbose: Bool = false
25+
26+
func run() throws {
27+
try generateTemplates(
28+
templates: [
29+
(miscFile, "Misc.swift"),
30+
],
31+
destination: URL(fileURLWithPath: generatedPath),
32+
verbose: verbose
33+
)
34+
}
35+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let miscFile = SourceFile {
19+
ExtensionDecl("""
20+
\(generateCopyrightHeader(for: "generate-swiftsyntax"))
21+
extension Syntax
22+
""") {
23+
VariableDecl(
24+
modifiers: [DeclModifierSyntax(name: Token.public), DeclModifier(name: Token.static)],
25+
name: IdentifierPattern("structure"),
26+
type: TypeAnnotation(
27+
colon: .colon,
28+
type: SimpleTypeIdentifier("SyntaxNodeStructure")
29+
)
30+
) {
31+
ReturnStmt(
32+
expression: FunctionCallExpr(callee: MemberAccessExpr(".choices")) {
33+
TupleExprElement(
34+
expression: ArrayExprSyntax {
35+
ArrayElement(
36+
expression: FunctionCallExpr("\n.node(TokenSyntax.self)"),
37+
trailingComma: Token.comma)
38+
for node in NON_BASE_SYNTAX_NODES {
39+
ArrayElement(
40+
expression: FunctionCallExpr("\n.node(\(raw: node.name).self)"),
41+
trailingComma: Token.comma)
42+
}
43+
})
44+
}
45+
)
46+
}
47+
}
48+
49+
ExtensionDecl("extension SyntaxKind") {
50+
VariableDecl(
51+
modifiers: [DeclModifier(name: Token.public)],
52+
name: IdentifierPattern("syntaxNodeType"),
53+
type: TypeAnnotation(
54+
colon: .colon,
55+
type: MemberTypeIdentifier("SyntaxProtocol.Type")
56+
)
57+
) {
58+
SwitchStmt(expression: Expr("self")) {
59+
SwitchCase("case .token:") {
60+
ReturnStmt("return TokenSyntax.self")
61+
}
62+
for node in NON_BASE_SYNTAX_NODES {
63+
SwitchCase("case .\(raw: node.swiftSyntaxKind):") {
64+
ReturnStmt("return \(raw: node.name).self")
65+
}
66+
}
67+
}
68+
}
69+
70+
VariableDecl(
71+
modifiers: [DeclModifier(name: Token.public)],
72+
name: IdentifierPattern("nameForDiagnostics"),
73+
type: TypeAnnotation(
74+
colon: .colon,
75+
type: OptionalType("String?")
76+
)
77+
) {
78+
SwitchStmt(expression: Expr("self")) {
79+
SwitchCase("case .token:") {
80+
ReturnStmt(#"return "token""#)
81+
}
82+
for node in NON_BASE_SYNTAX_NODES {
83+
SwitchCase("case .\(raw: node.swiftSyntaxKind):") {
84+
if let nameForDiagnostics = node.nameForDiagnostics {
85+
ReturnStmt("return \"\(raw: nameForDiagnostics)\"")
86+
} else {
87+
ReturnStmt("return nil")
88+
}
89+
}
90+
}
91+
}
92+
}
93+
}
94+
}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ let package = Package(
6363
dependencies: [],
6464
exclude: [
6565
"CMakeLists.txt",
66-
"Misc.swift.gyb",
6766
"Raw/RawSyntaxNodes.swift.gyb",
6867
"Raw/RawSyntaxValidation.swift.gyb",
6968
"SyntaxAnyVisitor.swift.gyb",

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ add_swift_host_library(SwiftSyntax
3131
Raw/gyb_generated/RawSyntaxNodes.swift
3232
Raw/gyb_generated/RawSyntaxValidation.swift
3333

34-
gyb_generated/Misc.swift
34+
generated/Misc.swift
3535
gyb_generated/SyntaxAnyVisitor.swift
3636
gyb_generated/SyntaxBaseNodes.swift
3737
gyb_generated/SyntaxCollections.swift

Sources/SwiftSyntax/Misc.swift.gyb

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

0 commit comments

Comments
 (0)