Skip to content

Commit c0f3642

Browse files
authored
Merge pull request swiftlang#647 from ahoppen/ahoppen/token-buildable
Use a dedicated `Token` type in SwiftSyntaxBuilder
2 parents bb62ba2 + 261f857 commit c0f3642

File tree

55 files changed

+2230
-2058
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2230
-2058
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let package = Package(
146146
"PatternNodes.swift.gyb",
147147
"StmtNodes.swift.gyb",
148148
"SyntaxBaseKinds.swift.gyb",
149-
"Tokens.swift.gyb",
149+
"TokenSpec.swift.gyb",
150150
"Traits.swift.gyb",
151151
"Trivia.swift.gyb",
152152
"TypeNodes.swift.gyb"

Sources/SwiftSyntaxBuilder/CatchClauseConvenienceInitializer.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/CatchClauseConvenienceInitializer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension CatchClause {
2121
) {
2222
self.init(
2323
leadingTrivia: leadingTrivia,
24-
catchKeyword: .catchKeyword(trailingTrivia: catchItems.elements.isEmpty ? [] : .space),
24+
catchKeyword: .catch.withTrailingTrivia(catchItems.elements.isEmpty ? [] : .space),
2525
catchItems: catchItems,
2626
body: bodyBuilder()
2727
)

Sources/SwiftSyntaxBuilder/DictionaryExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/DictionaryExprConvenienceInitializers.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ extension DictionaryExpr {
1616
/// A convenience initializer that allows passing in members using a result builder
1717
/// instead of having to wrap them in a `DictionaryElementList`.
1818
public init(
19-
leftSquare: TokenSyntax = .`leftSquareBracket`,
20-
rightSquare: TokenSyntax = .`rightSquareBracket`,
19+
leftSquare: Token = .`leftSquareBracket`,
20+
rightSquare: Token = .`rightSquareBracket`,
2121
@DictionaryElementListBuilder contentBuilder: () -> ExpressibleAsDictionaryElementList = { [] }
2222
) {
2323
let elementList = contentBuilder().createDictionaryElementList()
2424
self.init(
2525
leftSquare: leftSquare,
26-
content: elementList.elements.isEmpty ? TokenSyntax.colonToken(trailingTrivia: []) : elementList,
26+
content: elementList.elements.isEmpty ? Token.colon.withTrailingTrivia([]) : elementList,
2727
rightSquare: rightSquare
2828
)
2929
}

Sources/SwiftSyntaxBuilder/IfStmtConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/IfStmtConvenienceInitializers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public extension IfStmt {
2727
leadingTrivia: leadingTrivia,
2828
conditions: conditions,
2929
body: body(),
30-
elseKeyword: generatedElseBody == nil ? nil : TokenSyntax.elseKeyword(leadingTrivia: .space, trailingTrivia: []),
30+
elseKeyword: generatedElseBody == nil ? nil : Token.else.withLeadingTrivia(.space).withTrailingTrivia([]),
3131
elseBody: generatedElseBody.map { CodeBlock(statements: $0) }
3232
)
3333
}

Sources/SwiftSyntaxBuilder/MemberAccessExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/MemberAccessExprConvenienceInitializers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension MemberAccessExpr {
1616
/// Creates a `MemberAccessExpr` using the provided parameters.
1717
public init(
1818
base: ExpressibleAsExprBuildable? = nil,
19-
dot: TokenSyntax = .period,
19+
dot: Token = .period,
2020
name: String,
2121
declNameArguments: ExpressibleAsDeclNameArguments? = nil
2222
) {

Sources/SwiftSyntaxBuilder/StringLiteralExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/StringLiteralExprConvenienceInitializers.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ private let rawStringPotentialEscapesPattern = try! NSRegularExpression(
2626
extension StringLiteralExpr {
2727
/// Creates a string literal, optionally specifying quotes and delimiters.
2828
public init(
29-
openDelimiter: TokenSyntax? = nil,
30-
openQuote: TokenSyntax = .stringQuote,
29+
openDelimiter: Token? = nil,
30+
openQuote: Token = .stringQuote,
3131
_ value: String,
32-
closeQuote: TokenSyntax = .stringQuote,
33-
closeDelimiter: TokenSyntax? = nil
32+
closeQuote: Token = .stringQuote,
33+
closeDelimiter: Token? = nil
3434
) {
35-
let content = TokenSyntax.stringSegment(value)
35+
let content = Token.stringSegment(value)
3636
let segment = StringSegment(content: content)
3737
let segments = StringLiteralSegments([segment])
3838

@@ -57,7 +57,7 @@ extension StringLiteralExpr {
5757
.max() ?? 0
5858

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

6262
self.init(
6363
openDelimiter: delimiter,

Sources/SwiftSyntaxBuilder/TernaryExprConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/TernaryExprConvenienceInitializers.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ extension TernaryExpr {
2020
) {
2121
self.init(
2222
conditionExpression: condition,
23-
questionMark: .infixQuestionMarkToken(leadingTrivia: .space, trailingTrivia: .space),
23+
questionMark: .infixQuestionMark.withLeadingTrivia(.space).withTrailingTrivia(.space),
2424
firstChoice: firstChoice,
25-
colonMark: .colonToken(leadingTrivia: .space),
25+
colonMark: .colon.withLeadingTrivia(.space),
2626
secondChoice: secondChoice
2727
)
2828
}

Sources/SwiftSyntaxBuilder/TupleExprElementConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/TupleExprElementConvenienceInitializers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public extension TupleExprElement {
1717
/// The presence of the colon will be inferred based on the presence of the label.
1818
init(label: String? = nil, expression: ExpressibleAsExprBuildable) {
1919
self.init(
20-
label: label.map { TokenSyntax.identifier($0) }, colon: label == nil ? nil : .colon, expression: expression)
20+
label: label.map { Token.identifier($0) }, colon: label == nil ? nil : Token.colon, expression: expression)
2121
}
2222
}

Sources/SwiftSyntaxBuilder/VariableDeclConvenienceInitializers.swift renamed to Sources/SwiftSyntaxBuilder/ConvenienceInitializers/VariableDeclConvenienceInitializers.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension VariableDecl {
1818
leadingTrivia: Trivia = [],
1919
attributes: ExpressibleAsAttributeList? = nil,
2020
modifiers: ExpressibleAsModifierList? = nil,
21-
_ letOrVarKeyword: TokenSyntax,
21+
_ letOrVarKeyword: Token,
2222
name: ExpressibleAsIdentifierPattern,
2323
type: ExpressibleAsTypeAnnotation? = nil,
2424
initializer: ExpressibleAsInitializerClause? = nil
@@ -50,7 +50,7 @@ extension VariableDecl {
5050
leadingTrivia: leadingTrivia,
5151
attributes: attributes,
5252
modifiers: modifiers,
53-
letOrVarKeyword: .varKeyword(leadingTrivia: attributes != nil ? .space : .zero)
53+
letOrVarKeyword: .var.withLeadingTrivia(attributes != nil ? .space : .zero)
5454
) {
5555
PatternBinding(
5656
pattern: name,

Sources/SwiftSyntaxBuilder/HasTrailingComma.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SwiftSyntax
1414

1515
protocol HasTrailingComma {
16-
var trailingComma: TokenSyntax? { get }
16+
var trailingComma: Token? { get }
1717

1818
/// Returns this node overriding presence of the trailing comma
1919
func withTrailingComma(_ withComma: Bool) -> Self

Sources/SwiftSyntaxBuilder/generated/BuildableCollectionNodes.swift

+27-21
Original file line numberDiff line numberDiff line change
@@ -955,13 +955,13 @@ extension Array: ExpressibleAsEnumCaseElementList where Element == ExpressibleAs
955955
return EnumCaseElementList(self)
956956
}
957957
}
958-
/// `IdentifierList` represents a collection of `TokenSyntax`
958+
/// `IdentifierList` represents a collection of `Token`
959959
public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsIdentifierList {
960-
let elements: [TokenSyntax]
960+
let elements: [Token]
961961
/// Creates a `IdentifierList` with the provided list of elements.
962962
/// - Parameters:
963-
/// - elements: A list of `TokenSyntax`
964-
public init (_ elements: [TokenSyntax]) {
963+
/// - elements: A list of `Token`
964+
public init (_ elements: [Token]) {
965965
self.elements = elements
966966
}
967967
/// Creates a new `IdentifierList` by flattening the elements in `lists`
@@ -970,11 +970,13 @@ public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
970970
$0.createIdentifierList().elements
971971
}
972972
}
973-
public init (arrayLiteral elements: TokenSyntax...) {
973+
public init (arrayLiteral elements: Token...) {
974974
self.init(elements)
975975
}
976976
public func buildIdentifierList(format: Format) -> IdentifierListSyntax {
977-
let result = IdentifierListSyntax(elements)
977+
let result = IdentifierListSyntax(elements.map {
978+
$0.buildToken()
979+
})
978980
return format._format(syntax: result)
979981
}
980982
public func buildSyntax(format: Format) -> Syntax {
@@ -992,7 +994,7 @@ public struct IdentifierList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
992994
return self
993995
}
994996
}
995-
extension Array: ExpressibleAsIdentifierList where Element == TokenSyntax {
997+
extension Array: ExpressibleAsIdentifierList where Element == Token {
996998
public func createIdentifierList() -> IdentifierList {
997999
return IdentifierList(self)
9981000
}
@@ -1089,13 +1091,13 @@ extension Array: ExpressibleAsPrecedenceGroupNameList where Element == Expressib
10891091
return PrecedenceGroupNameList(self)
10901092
}
10911093
}
1092-
/// `TokenList` represents a collection of `TokenSyntax`
1094+
/// `TokenList` represents a collection of `Token`
10931095
public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsTokenList {
1094-
let elements: [TokenSyntax]
1096+
let elements: [Token]
10951097
/// Creates a `TokenList` with the provided list of elements.
10961098
/// - Parameters:
1097-
/// - elements: A list of `TokenSyntax`
1098-
public init (_ elements: [TokenSyntax]) {
1099+
/// - elements: A list of `Token`
1100+
public init (_ elements: [Token]) {
10991101
self.elements = elements
11001102
}
11011103
/// Creates a new `TokenList` by flattening the elements in `lists`
@@ -1104,11 +1106,13 @@ public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Expressible
11041106
$0.createTokenList().elements
11051107
}
11061108
}
1107-
public init (arrayLiteral elements: TokenSyntax...) {
1109+
public init (arrayLiteral elements: Token...) {
11081110
self.init(elements)
11091111
}
11101112
public func buildTokenList(format: Format) -> TokenListSyntax {
1111-
let result = TokenListSyntax(elements)
1113+
let result = TokenListSyntax(elements.map {
1114+
$0.buildToken()
1115+
})
11121116
return format._format(syntax: result)
11131117
}
11141118
public func buildSyntax(format: Format) -> Syntax {
@@ -1126,18 +1130,18 @@ public struct TokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Expressible
11261130
return self
11271131
}
11281132
}
1129-
extension Array: ExpressibleAsTokenList where Element == TokenSyntax {
1133+
extension Array: ExpressibleAsTokenList where Element == Token {
11301134
public func createTokenList() -> TokenList {
11311135
return TokenList(self)
11321136
}
11331137
}
1134-
/// `NonEmptyTokenList` represents a collection of `TokenSyntax`
1138+
/// `NonEmptyTokenList` represents a collection of `Token`
11351139
public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, ExpressibleAsNonEmptyTokenList {
1136-
let elements: [TokenSyntax]
1140+
let elements: [Token]
11371141
/// Creates a `NonEmptyTokenList` with the provided list of elements.
11381142
/// - Parameters:
1139-
/// - elements: A list of `TokenSyntax`
1140-
public init (_ elements: [TokenSyntax]) {
1143+
/// - elements: A list of `Token`
1144+
public init (_ elements: [Token]) {
11411145
self.elements = elements
11421146
}
11431147
/// Creates a new `NonEmptyTokenList` by flattening the elements in `lists`
@@ -1146,11 +1150,13 @@ public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Exp
11461150
$0.createNonEmptyTokenList().elements
11471151
}
11481152
}
1149-
public init (arrayLiteral elements: TokenSyntax...) {
1153+
public init (arrayLiteral elements: Token...) {
11501154
self.init(elements)
11511155
}
11521156
public func buildNonEmptyTokenList(format: Format) -> NonEmptyTokenListSyntax {
1153-
let result = NonEmptyTokenListSyntax(elements)
1157+
let result = NonEmptyTokenListSyntax(elements.map {
1158+
$0.buildToken()
1159+
})
11541160
return format._format(syntax: result)
11551161
}
11561162
public func buildSyntax(format: Format) -> Syntax {
@@ -1168,7 +1174,7 @@ public struct NonEmptyTokenList: ExpressibleByArrayLiteral, SyntaxBuildable, Exp
11681174
return self
11691175
}
11701176
}
1171-
extension Array: ExpressibleAsNonEmptyTokenList where Element == TokenSyntax {
1177+
extension Array: ExpressibleAsNonEmptyTokenList where Element == Token {
11721178
public func createNonEmptyTokenList() -> NonEmptyTokenList {
11731179
return NonEmptyTokenList(self)
11741180
}

0 commit comments

Comments
 (0)