Skip to content

Use a dedicated Token type in SwiftSyntaxBuilder #647

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 5 commits into from
Aug 29, 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ let package = Package(
"PatternNodes.swift.gyb",
"StmtNodes.swift.gyb",
"SyntaxBaseKinds.swift.gyb",
"Tokens.swift.gyb",
"TokenSpec.swift.gyb",
"Traits.swift.gyb",
"Trivia.swift.gyb",
"TypeNodes.swift.gyb"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension CatchClause {
) {
self.init(
leadingTrivia: leadingTrivia,
catchKeyword: .catchKeyword(trailingTrivia: catchItems.elements.isEmpty ? [] : .space),
catchKeyword: .catch.withTrailingTrivia(catchItems.elements.isEmpty ? [] : .space),
catchItems: catchItems,
body: bodyBuilder()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ extension DictionaryExpr {
/// A convenience initializer that allows passing in members using a result builder
/// instead of having to wrap them in a `DictionaryElementList`.
public init(
leftSquare: TokenSyntax = .`leftSquareBracket`,
rightSquare: TokenSyntax = .`rightSquareBracket`,
leftSquare: Token = .`leftSquareBracket`,
rightSquare: Token = .`rightSquareBracket`,
@DictionaryElementListBuilder contentBuilder: () -> ExpressibleAsDictionaryElementList = { [] }
) {
let elementList = contentBuilder().createDictionaryElementList()
self.init(
leftSquare: leftSquare,
content: elementList.elements.isEmpty ? TokenSyntax.colonToken(trailingTrivia: []) : elementList,
content: elementList.elements.isEmpty ? Token.colon.withTrailingTrivia([]) : elementList,
rightSquare: rightSquare
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public extension IfStmt {
leadingTrivia: leadingTrivia,
conditions: conditions,
body: body(),
elseKeyword: generatedElseBody == nil ? nil : TokenSyntax.elseKeyword(leadingTrivia: .space, trailingTrivia: []),
elseKeyword: generatedElseBody == nil ? nil : Token.else.withLeadingTrivia(.space).withTrailingTrivia([]),
elseBody: generatedElseBody.map { CodeBlock(statements: $0) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension MemberAccessExpr {
/// Creates a `MemberAccessExpr` using the provided parameters.
public init(
base: ExpressibleAsExprBuildable? = nil,
dot: TokenSyntax = .period,
dot: Token = .period,
name: String,
declNameArguments: ExpressibleAsDeclNameArguments? = nil
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ private let rawStringPotentialEscapesPattern = try! NSRegularExpression(
extension StringLiteralExpr {
/// Creates a string literal, optionally specifying quotes and delimiters.
public init(
openDelimiter: TokenSyntax? = nil,
openQuote: TokenSyntax = .stringQuote,
openDelimiter: Token? = nil,
openQuote: Token = .stringQuote,
_ value: String,
closeQuote: TokenSyntax = .stringQuote,
closeDelimiter: TokenSyntax? = nil
closeQuote: Token = .stringQuote,
closeDelimiter: Token? = nil
) {
let content = TokenSyntax.stringSegment(value)
let content = Token.stringSegment(value)
let segment = StringSegment(content: content)
let segments = StringLiteralSegments([segment])

Expand All @@ -57,7 +57,7 @@ extension StringLiteralExpr {
.max() ?? 0

// Use a delimiter that is exactly one longer
let delimiter = TokenSyntax.rawStringDelimiter(String(repeating: "#", count: 1 + maxPoundCount))
let delimiter = Token.rawStringDelimiter(String(repeating: "#", count: 1 + maxPoundCount))

self.init(
openDelimiter: delimiter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ extension TernaryExpr {
) {
self.init(
conditionExpression: condition,
questionMark: .infixQuestionMarkToken(leadingTrivia: .space, trailingTrivia: .space),
questionMark: .infixQuestionMark.withLeadingTrivia(.space).withTrailingTrivia(.space),
firstChoice: firstChoice,
colonMark: .colonToken(leadingTrivia: .space),
colonMark: .colon.withLeadingTrivia(.space),
secondChoice: secondChoice
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public extension TupleExprElement {
/// The presence of the colon will be inferred based on the presence of the label.
init(label: String? = nil, expression: ExpressibleAsExprBuildable) {
self.init(
label: label.map { TokenSyntax.identifier($0) }, colon: label == nil ? nil : .colon, expression: expression)
label: label.map { Token.identifier($0) }, colon: label == nil ? nil : Token.colon, expression: expression)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension VariableDecl {
leadingTrivia: Trivia = [],
attributes: ExpressibleAsAttributeList? = nil,
modifiers: ExpressibleAsModifierList? = nil,
_ letOrVarKeyword: TokenSyntax,
_ letOrVarKeyword: Token,
name: ExpressibleAsIdentifierPattern,
type: ExpressibleAsTypeAnnotation? = nil,
initializer: ExpressibleAsInitializerClause? = nil
Expand Down Expand Up @@ -50,7 +50,7 @@ extension VariableDecl {
leadingTrivia: leadingTrivia,
attributes: attributes,
modifiers: modifiers,
letOrVarKeyword: .varKeyword(leadingTrivia: attributes != nil ? .space : .zero)
letOrVarKeyword: .var.withLeadingTrivia(attributes != nil ? .space : .zero)
) {
PatternBinding(
pattern: name,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntaxBuilder/HasTrailingComma.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import SwiftSyntax

protocol HasTrailingComma {
var trailingComma: TokenSyntax? { get }
var trailingComma: Token? { get }

/// Returns this node overriding presence of the trailing comma
func withTrailingComma(_ withComma: Bool) -> Self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,13 @@ extension Array: ExpressibleAsEnumCaseElementList where Element == ExpressibleAs
return EnumCaseElementList(self)
}
}
/// `IdentifierList` represents a collection of `TokenSyntax`
/// `IdentifierList` represents a collection of `Token`
public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsIdentifierList {
let elements: [TokenSyntax]
let elements: [Token]
/// Creates a `IdentifierList` with the provided list of elements.
/// - Parameters:
/// - elements: A list of `TokenSyntax`
public init (_ elements: [TokenSyntax]) {
/// - elements: A list of `Token`
public init (_ elements: [Token]) {
self.elements = elements
}
/// Creates a new `IdentifierList` by flattening the elements in `lists`
Expand All @@ -970,11 +970,13 @@ public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
$0.createIdentifierList().elements
}
}
public init (arrayLiteral elements: TokenSyntax...) {
public init (arrayLiteral elements: Token...) {
self.init(elements)
}
public func buildIdentifierList(format: Format) -> IdentifierListSyntax {
let result = IdentifierListSyntax(elements)
let result = IdentifierListSyntax(elements.map {
$0.buildToken()
})
return format._format(syntax: result)
}
public func buildSyntax(format: Format) -> Syntax {
Expand All @@ -992,7 +994,7 @@ public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
return self
}
}
extension Array: ExpressibleAsIdentifierList where Element == TokenSyntax {
extension Array: ExpressibleAsIdentifierList where Element == Token {
public func createIdentifierList() -> IdentifierList {
return IdentifierList(self)
}
Expand Down Expand Up @@ -1089,13 +1091,13 @@ extension Array: ExpressibleAsPrecedenceGroupNameList where Element == Expressib
return PrecedenceGroupNameList(self)
}
}
/// `TokenList` represents a collection of `TokenSyntax`
/// `TokenList` represents a collection of `Token`
public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsTokenList {
let elements: [TokenSyntax]
let elements: [Token]
/// Creates a `TokenList` with the provided list of elements.
/// - Parameters:
/// - elements: A list of `TokenSyntax`
public init (_ elements: [TokenSyntax]) {
/// - elements: A list of `Token`
public init (_ elements: [Token]) {
self.elements = elements
}
/// Creates a new `TokenList` by flattening the elements in `lists`
Expand All @@ -1104,11 +1106,13 @@ public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Expressible
$0.createTokenList().elements
}
}
public init (arrayLiteral elements: TokenSyntax...) {
public init (arrayLiteral elements: Token...) {
self.init(elements)
}
public func buildTokenList(format: Format) -> TokenListSyntax {
let result = TokenListSyntax(elements)
let result = TokenListSyntax(elements.map {
$0.buildToken()
})
return format._format(syntax: result)
}
public func buildSyntax(format: Format) -> Syntax {
Expand All @@ -1126,18 +1130,18 @@ public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Expressible
return self
}
}
extension Array: ExpressibleAsTokenList where Element == TokenSyntax {
extension Array: ExpressibleAsTokenList where Element == Token {
public func createTokenList() -> TokenList {
return TokenList(self)
}
}
/// `NonEmptyTokenList` represents a collection of `TokenSyntax`
/// `NonEmptyTokenList` represents a collection of `Token`
public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsNonEmptyTokenList {
let elements: [TokenSyntax]
let elements: [Token]
/// Creates a `NonEmptyTokenList` with the provided list of elements.
/// - Parameters:
/// - elements: A list of `TokenSyntax`
public init (_ elements: [TokenSyntax]) {
/// - elements: A list of `Token`
public init (_ elements: [Token]) {
self.elements = elements
}
/// Creates a new `NonEmptyTokenList` by flattening the elements in `lists`
Expand All @@ -1146,11 +1150,13 @@ public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Exp
$0.createNonEmptyTokenList().elements
}
}
public init (arrayLiteral elements: TokenSyntax...) {
public init (arrayLiteral elements: Token...) {
self.init(elements)
}
public func buildNonEmptyTokenList(format: Format) -> NonEmptyTokenListSyntax {
let result = NonEmptyTokenListSyntax(elements)
let result = NonEmptyTokenListSyntax(elements.map {
$0.buildToken()
})
return format._format(syntax: result)
}
public func buildSyntax(format: Format) -> Syntax {
Expand All @@ -1168,7 +1174,7 @@ public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Exp
return self
}
}
extension Array: ExpressibleAsNonEmptyTokenList where Element == TokenSyntax {
extension Array: ExpressibleAsNonEmptyTokenList where Element == Token {
public func createNonEmptyTokenList() -> NonEmptyTokenList {
return NonEmptyTokenList(self)
}
Expand Down
Loading