Skip to content

Generate TokenSyntax with SwiftSyntaxBuilderGeneration #493

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
Jul 14, 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 Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ let package = Package(
"BuildableCollectionNodes.swift.gyb",
"BuildableNodes.swift.gyb",
"ResultBuilders.swift.gyb",
"TokenSyntax.swift.gyb",
]
),
.target(
Expand Down
50 changes: 0 additions & 50 deletions Sources/SwiftSyntaxBuilder/TokenSyntax.swift.gyb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//// Automatically Generated From TokenSyntax.swift.gyb.

//// Automatically Generated by SwiftSyntaxBuilderGeneration
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
Expand All @@ -13,38 +14,35 @@
//===----------------------------------------------------------------------===//

import SwiftSyntax


extension TokenSyntax: ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier, ExpressibleAsIdentifierExpr {
/// Conformance to `ExpressibleAsTokenList`.
public func createTokenList() -> TokenList {
extension TokenSyntax: ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr, ExpressibleAsDeclModifier, ExpressibleAsIdentifierExpr{
/// Conformance to ExpressibleAsTokenList
public func createTokenList()-> TokenList{
return TokenList([self])
}
/// Conformance to `ExpressibleAsNonEmptyTokenList`.
public func createNonEmptyTokenList() -> NonEmptyTokenList {
/// Conformance to ExpressibleAsNonEmptyTokenList
public func createNonEmptyTokenList()-> NonEmptyTokenList{
return NonEmptyTokenList([self])
}
/// Conformance to `ExpressibleAsBinaryOperatorExpr`.
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
/// Conformance to ExpressibleAsBinaryOperatorExpr
public func createBinaryOperatorExpr()-> BinaryOperatorExpr{
return BinaryOperatorExpr(operatorToken: self)
}
/// Conformance to `ExpressibleAsDeclModifier`.
public func createDeclModifier() -> DeclModifier {
/// Conformance to ExpressibleAsDeclModifier
public func createDeclModifier()-> DeclModifier{
return DeclModifier(name: self)
}
/// Conformance to `ExpressibleAsIdentifierExpr`.
public func createIdentifierExpr() -> IdentifierExpr {
/// Conformance to ExpressibleAsIdentifierExpr
public func createIdentifierExpr()-> IdentifierExpr{
return IdentifierExpr(identifier: self)
}
}

/// `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`.
extension TokenSyntax {
public func createSyntaxBuildable() -> SyntaxBuildable {
/// `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`.
extension TokenSyntax{
public func createSyntaxBuildable()-> SyntaxBuildable{
return createIdentifierExpr()
}

public func createExprBuildable() -> ExprBuildable {
public func createExprBuildable()-> ExprBuildable{
return createIdentifierExpr()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import SwiftSyntax
import SwiftSyntaxBuilder

let tokenSyntaxFile = SourceFile {
ImportDecl(
leadingTrivia: .docLineComment(copyrightHeader),
path: "SwiftSyntax"
)

let tokenType = SyntaxBuildableType(syntaxKind: "Token")
let conformances = tokenType.generatedExpressibleAsConformances

ExtensionDecl(
extendedType: "TokenSyntax",
inheritanceClause: TypeInheritanceClause {
for conformance in conformances {
InheritedType(typeName: conformance.expressibleAs)
}
}
) {
for conformance in tokenType.elementInCollections {
FunctionDecl(
leadingTrivia: .docLineComment("/// Conformance to \(conformance.expressibleAs)") + .newline,
modifiers: [TokenSyntax.public],
identifier: .identifier("create\(conformance.buildableBaseName)"),
signature: FunctionSignature(
input: ParameterClause(),
output: conformance.buildable
)
) {
ReturnStmt(expression: FunctionCallExpr(conformance.buildable) {
TupleExprElement(expression: ArrayExpr {
ArrayElement(expression: "self")
})
})
}
}
for conformance in tokenType.convertibleToTypes {
let param = Node.from(type: conformance).singleNonDefaultedChild
FunctionDecl(
leadingTrivia: .docLineComment("/// Conformance to \(conformance.expressibleAs)") + .newline,
modifiers: [TokenSyntax.public],
identifier: .identifier("create\(conformance.buildableBaseName)"),
signature: FunctionSignature(
input: ParameterClause(),
output: conformance.buildable
)
) {
ReturnStmt(expression: FunctionCallExpr(conformance.buildable) {
TupleExprElement(label: param.swiftName, expression: "self")
})
}
}
}

ExtensionDecl(
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,
extendedType: "TokenSyntax"
) {
for buildable in ["SyntaxBuildable", "ExprBuildable"] {
FunctionDecl(
modifiers: [TokenSyntax.public],
identifier: .identifier("create\(buildable)"),
signature: FunctionSignature(
input: ParameterClause(),
output: buildable
)
) {
ReturnStmt(expression: FunctionCallExpr("createIdentifierExpr"))
}
}
}
}
1 change: 1 addition & 0 deletions Sources/SwiftSyntaxBuilderGeneration/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import SwiftSyntaxBuilder
/// SwiftSyntaxBuilder sources to be generated
let sourceTemplates = [
(tokensFile, "Tokens.swift"),
(tokenSyntaxFile, "TokenSyntax.swift"),
]

guard CommandLine.arguments.count > 1 else {
Expand Down