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:
5156public 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 {
204214public 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:
246259public 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 }
0 commit comments