Skip to content

Extract syntax builder generation templates into separate directory #456

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
Jun 7, 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
51 changes: 51 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/SyntaxUtilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// 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 SwiftSyntax
import SwiftSyntaxBuilder

let copyrightHeader = """
//// Automatically Generated by SwiftSyntaxBuilderGeneration
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//


"""

func createSpacingCall() -> FunctionCallExpr {
FunctionCallExpr(MemberAccessExpr(name: "spaces"), argumentListBuilder: { TupleExprElement(expression: IntegerLiteralExpr(1)) })
}

func createWithLeadingTriviaCall() -> FunctionCallExpr {
FunctionCallExpr(MemberAccessExpr(name: "withLeadingTrivia"), argumentListBuilder: { TupleExprElement(expression: createSpacingCall()) })
}

func createWithTrailingTriviaCall() -> FunctionCallExpr {
FunctionCallExpr(MemberAccessExpr(name: "withTrailingTrivia"), argumentListBuilder: { TupleExprElement(expression: createSpacingCall()) })
}

func createTokenSyntaxPatternBinding(_ pattern: ExpressibleAsPatternBuildable, accessor: ExpressibleAsSyntaxBuildable) -> PatternBinding {
PatternBinding(pattern: pattern,
typeAnnotation: "TokenSyntax",
initializer: nil,
accessor: accessor)
}
165 changes: 165 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/Templates/TokensFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//===----------------------------------------------------------------------===//
//
// 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 tokensFile = SourceFile {
ImportDecl(importTok: TokenSyntax.import.withLeadingTrivia(.docLineComment(copyrightHeader)), path: "SwiftSyntax")

ExtensionDecl(
extendedType: "TokenSyntax",
modifiersBuilder: {
TokenSyntax.public.withLeadingTrivia(.newlines(1) + .docLineComment("/// Namespace for commonly used tokens with default trivia.") + .newlines(1))
},
membersBuilder: {
for token in SYNTAX_TOKENS {
if token.isKeyword {
VariableDecl(
letOrVarKeyword: .var,
modifiersBuilder: {
if let text = token.text {
TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `\(text)` keyword") + .newlines(1))
} else {
TokenSyntax.static
}
},
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.

let accessor = CodeBlock {
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Keyword"))

if token.requiresLeadingSpace {
createWithLeadingTriviaCall()
}

if token.requiresTrailingSpace {
createWithTrailingTriviaCall()
}
}

createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor)
}
)
} else if token.text != nil {
VariableDecl(
letOrVarKeyword: .var,
modifiersBuilder: {
if let text = token.text {
TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `\(text)` token") + .newlines(1))
} else {
TokenSyntax.static
}
},
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.
let accessor = CodeBlock {
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Token"))

if token.requiresLeadingSpace {
createWithLeadingTriviaCall()
}

if token.requiresTrailingSpace {
createWithTrailingTriviaCall()
}
}

createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor)
}
)
} else {
let signature = FunctionSignature(
input: ParameterClause(
parameterList: FunctionParameter(
attributes: nil,
firstName: .wildcard,
secondName: .identifier("text"),
colon: .colon,
type: "String"
),
rightParen: .rightParen.withTrailingTrivia(.spaces(1))
),
output: "TokenSyntax"
)

FunctionDecl(
identifier: .identifier("`\(token.name.withFirstCharacterLowercased)`"),
signature: signature,
modifiersBuilder: {
if let text = token.text {
TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `\(text)` token"))
} else {
TokenSyntax.static
}
},
bodyBuilder: {
FunctionCallExpr(
MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)"),
argumentListBuilder: {
TupleExprElement(expression: IdentifierExpr("text"))
}
)

if token.requiresLeadingSpace {
createWithLeadingTriviaCall()
}

if token.requiresTrailingSpace {
createWithTrailingTriviaCall()
}
}
)
}
}
VariableDecl(
letOrVarKeyword: .var,
modifiersBuilder: { TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `eof` token") + .newlines(1)) },
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.
let body = CodeBlock {
FunctionCallExpr(
MemberAccessExpr(base: "SyntaxFactory", name: "makeToken"),
argumentListBuilder: {
TupleExprElement(expression: MemberAccessExpr(name: "eof"), trailingComma: .comma)
TupleExprElement(label: TokenSyntax.identifier("presence"), colon: .colon, expression: MemberAccessExpr(name: "present"))
}
)
}

createTokenSyntaxPatternBinding("eof", accessor: body)
}
)
VariableDecl(
letOrVarKeyword: .var,
modifiersBuilder: { TokenSyntax.static.withLeadingTrivia(.newlines(1) + .docLineComment("/// The `open` contextual token") + .newlines(1)) },
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.
let body = CodeBlock {
FunctionCallExpr(
MemberAccessExpr(base: "SyntaxFactory", name: "makeContextualKeyword"),
argumentListBuilder: {
TupleExprElement(expression: StringLiteralExpr("open"))
}
)

createWithTrailingTriviaCall()
}

createTokenSyntaxPatternBinding("open", accessor: body)
}
)
}
)
}
Loading