Skip to content

Commit 487aa08

Browse files
committed
Remove ExpressibleAsTokenSyntax protocol
1 parent 6859f75 commit 487aa08

19 files changed

+1271
-1257
lines changed

Sources/SwiftSyntaxBuilder/BooleanLiteralExprConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftSyntax
1414

1515
extension BooleanLiteralExpr {
1616
public init(_ value: Bool) {
17-
self.init(booleanLiteral: value ? TokenSyntax.true : .false)
17+
self.init(booleanLiteral: value ? .true : .false)
1818
}
1919
}
2020

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
NODE_MAP = create_node_map()
99
# -*- mode: Swift -*-
1010
# Ignore the following admonition it applies to the resulting .swift file only
11+
12+
# Use [:] to make sure we copy the conformances so that we don't modify
13+
# `SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES` when adding elements to `expressible_as_protocols` below.
14+
def get_expressible_as_conformances(conforming_type):
15+
expressible_as_protocols = (SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.get(conforming_type) or [])[:]
16+
expressible_as_protocols += (SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(conforming_type) or [])[:]
17+
return map(lambda x : 'ExpressibleAs' + x, expressible_as_protocols)
1118
}%
1219
//// Automatically Generated From DeclBuildables.swift.gyb.
1320
//// Do Not Edit Directly!
@@ -44,9 +51,7 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
4451

4552
% buildable_type = kind + 'Buildable'
4653
% expressible_as_type = 'ExpressibleAs' + buildable_type
47-
% expressible_as_protocols = SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.get(buildable_type) or []
48-
% expressible_as_protocols += SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(buildable_type) or []
49-
% expressible_as_protocols = map(lambda x : 'ExpressibleAs' + x, expressible_as_protocols)
54+
% expressible_as_protocols = get_expressible_as_conformances(buildable_type)
5055
% if expressible_as_protocols:
5156
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
5257
% else:
@@ -140,15 +145,20 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
140145
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
141146
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
142147
% default_value = syntax_buildable_default_init_value(child, child_token)
143-
% init_parameters.append("%s: ExpressibleAs%s%s" % (child.swift_name, param_type, default_value))
148+
% param_type = param_type if child.is_token() else "ExpressibleAs" + param_type
149+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
144150
% end
145151
${',\n '.join(init_parameters)}
146152
) {
147153
% for child in node.children:
148154
% create_method_dot = '?.' if child.is_optional else '.'
149155
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
150156
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token())
157+
% if param_type is 'TokenSyntax':
158+
self.${child.swift_name} = ${child.swift_name}
159+
% else:
151160
self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
161+
% end
152162
% end
153163
}
154164

@@ -204,11 +214,16 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
204214
public struct ${node.syntax_kind}: SyntaxBuildable {
205215
let elements: [${element_type}]
206216

217+
% param_type = element_type if node.is_token() else 'ExpressibleAs' + element_type
207218
/// Creates a `${node.syntax_kind}` with the provided list of elements.
208219
/// - Parameters:
209220
/// - elements: A list of `ExpressibleAs${element_type}`
210-
public init(_ elements: [ExpressibleAs${element_type}]) {
221+
public init(_ elements: [${param_type}]) {
222+
% if node.is_token():
223+
self.elements = elements
224+
% else:
211225
self.elements = elements.map { $0.create${element_type}() }
226+
% end
212227
}
213228

214229
public func build${node.syntax_kind}(format: Format) -> ${node.syntax_kind}Syntax {
@@ -239,9 +254,7 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
239254
% end
240255
% if node.is_buildable() or node.is_syntax_collection():
241256
% expressible_as_type = 'ExpressibleAs' + node.syntax_kind
242-
% expressible_as_protocols = SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.get(node.syntax_kind) or []
243-
% expressible_as_protocols += SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(node.syntax_kind) or []
244-
% expressible_as_protocols = map(lambda x : 'ExpressibleAs' + x, expressible_as_protocols)
257+
% expressible_as_protocols = get_expressible_as_conformances(node.syntax_kind)
245258
% if expressible_as_protocols:
246259
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
247260
% else:
@@ -258,36 +271,36 @@ extension ${node.syntax_kind}: ${expressible_as_type} {
258271

259272
% end
260273
% end
261-
public protocol ExpressibleAsTokenSyntax {
262-
func createTokenSyntax() -> TokenSyntax
274+
% expressible_as_protocols = get_expressible_as_conformances('TokenSyntax')
275+
% if expressible_as_protocols:
276+
extension TokenSyntax: ${', '.join(expressible_as_protocols)} {
263277
}
278+
% end
264279

265-
extension TokenSyntax: ExpressibleAsTokenSyntax {
266-
public func createTokenSyntax() -> TokenSyntax {
267-
self
280+
// MARK: - Syntax Collection buildable expressible as conformances
281+
282+
% for protocol, conformances in SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.items():
283+
% for conformance in conformances:
284+
% extension_protocol = protocol if protocol is 'TokenSyntax' else 'ExpressibleAs' + protocol
285+
extension ${extension_protocol} {
286+
public func create${conformance}() -> ${conformance} {
287+
${conformance}([self])
268288
}
269289
}
270290

291+
% end
292+
% end
271293
// MARK: - Syntax buildable expressible as conformances
272294

273-
% for protocol, conformances in SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.items():
274-
% for conformance in conformances:
275-
extension ExpressibleAs${protocol} {
276-
public func create${conformance}() -> ${conformance} {
277-
${conformance}([self])
278-
}
279-
}
280-
281-
% end
282-
% end
283295
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
284296
% for conformance in conformances:
285297
% node = NODE_MAP.get(conformance)
286298
% if node and node.children:
287299
% non_defaulted_params = filter(lambda child : syntax_buildable_default_init_value(child, SYNTAX_TOKEN_MAP.get(child.syntax_kind)) == "", node.children)
288300
% assert len(non_defaulted_params) == 1, "ExpressibleAs conformances expects the conforming type to have an initializer with a single non-optional child"
289301
% param = non_defaulted_params[0].swift_name
290-
extension ExpressibleAs${protocol} {
302+
% extension_protocol = protocol if protocol is 'TokenSyntax' else 'ExpressibleAs' + protocol
303+
extension ${extension_protocol} {
291304
public func create${conformance}() -> ${conformance} {
292305
${conformance}(${param}: self)
293306
}

Sources/SwiftSyntaxBuilder/BuildablesConvenienceInitializers.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ extension ${node.syntax_kind} {
5252
% else:
5353
% # When type is not handled above, use default value
5454
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
55+
% param_type = param_type if child.is_token() else "ExpressibleAs" + param_type
5556
% default_value = syntax_buildable_default_init_value(child, child_token)
56-
% init_parameters.append("%s: ExpressibleAs%s%s" % (child.swift_name, param_type, default_value))
57+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
5758
% end
5859
% end
5960
${',\n '.join(init_parameters + init_result_builder_parameters)}

Sources/SwiftSyntaxBuilder/FunctionCallExprConvenienceInitializers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
//===----------------------------------------------------------------------===//
1212

1313

14-
import Foundation
14+
import SwiftSyntax
1515

1616
extension FunctionCallExpr {
1717
public init(
1818
_ calledExpression: ExpressibleAsIdentifierExpr,
19-
leftParen: ExpressibleAsTokenSyntax? = nil,
20-
rightParen: ExpressibleAsTokenSyntax? = nil,
19+
leftParen: TokenSyntax? = nil,
20+
rightParen: TokenSyntax? = nil,
2121
trailingClosure: ExpressibleAsClosureExpr? = nil,
2222
@TupleExprElementListBuilder argumentListBuilder: () -> TupleExprElementList = { .empty },
2323
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }

Sources/SwiftSyntaxBuilder/ResultBuilders.swift.gyb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import SwiftSyntax
2626
@resultBuilder
2727
public struct ${node.syntax_kind}Builder {
2828
% element_type = syntax_buildable_child_type(node.collection_element_type, node.collection_element, node.is_token())
29-
29+
% component_type = element_type if node.is_token() else 'ExpressibleAs' + element_type
30+
3031
/// The type of individual statement expressions in the transformed function,
3132
/// which defaults to Component if buildExpression() is not provided.
32-
public typealias Expression = ExpressibleAs${element_type}
33+
public typealias Expression = ${component_type}
3334

3435
/// The type of a partial result, which will be carried through all of the
3536
/// build methods.
36-
public typealias Component = [ExpressibleAs${element_type}]
37+
public typealias Component = [${component_type}]
3738

3839
/// The type of the final returned result, which defaults to Component if
3940
/// buildFinalResult() is not provided.
@@ -84,7 +85,11 @@ public struct ${node.syntax_kind}Builder {
8485
/// If declared, this will be called on the partial result from the outermost
8586
/// block statement to produce the final returned result.
8687
public static func buildFinalResult(_ component: Component) -> FinalResult {
88+
% if node.is_token():
89+
.init(component)
90+
% else:
8791
.init(component.map { $0.create${element_type}() })
92+
% end
8893
}
8994
}
9095

Sources/SwiftSyntaxBuilder/Tokens.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import SwiftSyntax
2222

2323
/// Namespace for commonly used tokens with default trivia.
24-
public extension ExpressibleAsTokenSyntax where Self == TokenSyntax {
24+
public extension TokenSyntax {
2525
% for token in SYNTAX_TOKENS:
2626
% if token.is_keyword:
2727
/// The `${token.text.encode('utf-8').decode('unicode_escape')}` keyword

0 commit comments

Comments
 (0)