Skip to content

Commit 4806fc6

Browse files
authored
Merge pull request #493 from fwcd/token-syntax-swift-syntax-gen
Generate `TokenSyntax` with `SwiftSyntaxBuilderGeneration`
2 parents 97268b1 + e918c45 commit 4806fc6

File tree

5 files changed

+105
-71
lines changed

5 files changed

+105
-71
lines changed

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ let package = Package(
8282
"BuildableCollectionNodes.swift.gyb",
8383
"BuildableNodes.swift.gyb",
8484
"ResultBuilders.swift.gyb",
85-
"TokenSyntax.swift.gyb",
8685
]
8786
),
8887
.target(

Sources/SwiftSyntaxBuilder/TokenSyntax.swift.gyb

Lines changed: 0 additions & 50 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//// Automatically Generated From TokenSyntax.swift.gyb.
1+
2+
//// Automatically Generated by SwiftSyntaxBuilderGeneration
23
//// Do Not Edit Directly!
34
//===----------------------------------------------------------------------===//
45
//
@@ -13,38 +14,35 @@
1314
//===----------------------------------------------------------------------===//
1415

1516
import SwiftSyntax
16-
17-
18-
extension TokenSyntax: ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier, ExpressibleAsIdentifierExpr {
19-
/// Conformance to `ExpressibleAsTokenList`.
20-
public func createTokenList() -> TokenList {
17+
extension TokenSyntax: ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier, ExpressibleAsIdentifierExpr{
18+
/// Conformance to ExpressibleAsTokenList
19+
public func createTokenList()-> TokenList{
2120
return TokenList([self])
2221
}
23-
/// Conformance to `ExpressibleAsNonEmptyTokenList`.
24-
public func createNonEmptyTokenList() -> NonEmptyTokenList {
22+
/// Conformance to ExpressibleAsNonEmptyTokenList
23+
public func createNonEmptyTokenList()-> NonEmptyTokenList{
2524
return NonEmptyTokenList([self])
2625
}
27-
/// Conformance to `ExpressibleAsBinaryOperatorExpr`.
28-
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
26+
/// Conformance to ExpressibleAsBinaryOperatorExpr
27+
public func createBinaryOperatorExpr()-> BinaryOperatorExpr{
2928
return BinaryOperatorExpr(operatorToken: self)
3029
}
31-
/// Conformance to `ExpressibleAsDeclModifier`.
32-
public func createDeclModifier() -> DeclModifier {
30+
/// Conformance to ExpressibleAsDeclModifier
31+
public func createDeclModifier()-> DeclModifier{
3332
return DeclModifier(name: self)
3433
}
35-
/// Conformance to `ExpressibleAsIdentifierExpr`.
36-
public func createIdentifierExpr() -> IdentifierExpr {
34+
/// Conformance to ExpressibleAsIdentifierExpr
35+
public func createIdentifierExpr()-> IdentifierExpr{
3736
return IdentifierExpr(identifier: self)
3837
}
3938
}
4039

41-
/// `TokenSyntax` conforms to `SyntaxBuildable` and `ExprBuildable` via different paths, so we need to pick one default conversion path to create an an `ExprSyntax` (and `Syntax`) from a `String`. We choose `IdentifierExpr`.
42-
extension TokenSyntax {
43-
public func createSyntaxBuildable() -> SyntaxBuildable {
40+
/// `TokenSyntax` conforms to `SyntaxBuildable` and `ExprBuildable` via different paths, so we need to pick one default conversion path to create an `ExprSyntax` (and `Syntax`) from a `String`. We choose `IdentifierExpr`.
41+
extension TokenSyntax{
42+
public func createSyntaxBuildable()-> SyntaxBuildable{
4443
return createIdentifierExpr()
4544
}
46-
47-
public func createExprBuildable() -> ExprBuildable {
45+
public func createExprBuildable()-> ExprBuildable{
4846
return createIdentifierExpr()
4947
}
50-
}
48+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 Foundation
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
17+
let tokenSyntaxFile = SourceFile {
18+
ImportDecl(
19+
leadingTrivia: .docLineComment(copyrightHeader),
20+
path: "SwiftSyntax"
21+
)
22+
23+
let tokenType = SyntaxBuildableType(syntaxKind: "Token")
24+
let conformances = tokenType.generatedExpressibleAsConformances
25+
26+
ExtensionDecl(
27+
extendedType: "TokenSyntax",
28+
inheritanceClause: TypeInheritanceClause {
29+
for conformance in conformances {
30+
InheritedType(typeName: conformance.expressibleAs)
31+
}
32+
}
33+
) {
34+
for conformance in tokenType.elementInCollections {
35+
FunctionDecl(
36+
leadingTrivia: .docLineComment("/// Conformance to \(conformance.expressibleAs)") + .newline,
37+
modifiers: [TokenSyntax.public],
38+
identifier: .identifier("create\(conformance.buildableBaseName)"),
39+
signature: FunctionSignature(
40+
input: ParameterClause(),
41+
output: conformance.buildable
42+
)
43+
) {
44+
ReturnStmt(expression: FunctionCallExpr(conformance.buildable) {
45+
TupleExprElement(expression: ArrayExpr {
46+
ArrayElement(expression: "self")
47+
})
48+
})
49+
}
50+
}
51+
for conformance in tokenType.convertibleToTypes {
52+
let param = Node.from(type: conformance).singleNonDefaultedChild
53+
FunctionDecl(
54+
leadingTrivia: .docLineComment("/// Conformance to \(conformance.expressibleAs)") + .newline,
55+
modifiers: [TokenSyntax.public],
56+
identifier: .identifier("create\(conformance.buildableBaseName)"),
57+
signature: FunctionSignature(
58+
input: ParameterClause(),
59+
output: conformance.buildable
60+
)
61+
) {
62+
ReturnStmt(expression: FunctionCallExpr(conformance.buildable) {
63+
TupleExprElement(label: param.swiftName, expression: "self")
64+
})
65+
}
66+
}
67+
}
68+
69+
ExtensionDecl(
70+
leadingTrivia: .newline + .docLineComment("/// `TokenSyntax` conforms to `SyntaxBuildable` and `ExprBuildable` via different paths, so we need to pick one default conversion path to create an `ExprSyntax` (and `Syntax`) from a `String`. We choose `IdentifierExpr`.") + .newline,
71+
extendedType: "TokenSyntax"
72+
) {
73+
for buildable in ["SyntaxBuildable", "ExprBuildable"] {
74+
FunctionDecl(
75+
modifiers: [TokenSyntax.public],
76+
identifier: .identifier("create\(buildable)"),
77+
signature: FunctionSignature(
78+
input: ParameterClause(),
79+
output: buildable
80+
)
81+
) {
82+
ReturnStmt(expression: FunctionCallExpr("createIdentifierExpr"))
83+
}
84+
}
85+
}
86+
}

Sources/SwiftSyntaxBuilderGeneration/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import SwiftSyntaxBuilder
1717
/// SwiftSyntaxBuilder sources to be generated
1818
let sourceTemplates = [
1919
(tokensFile, "Tokens.swift"),
20+
(tokenSyntaxFile, "TokenSyntax.swift"),
2021
]
2122

2223
guard CommandLine.arguments.count > 1 else {

0 commit comments

Comments
 (0)