8
8
NODE_MAP = create_node_map()
9
9
# -*- mode: Swift -*-
10
10
# Ignore the following admonition it applies to the resulting .swift file only
11
+
12
+ def get_expressible_as_conformances(conforming_type):
13
+ expressible_as_protocols = (SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.get(conforming_type) or [])[:]
14
+ expressible_as_protocols += (SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(conforming_type) or [])[:]
15
+ return map(lambda x : 'ExpressibleAs' + x, expressible_as_protocols)
11
16
}%
12
17
//// Automatically Generated From DeclBuildables.swift.gyb.
13
18
//// Do Not Edit Directly!
@@ -44,9 +49,7 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
44
49
45
50
% buildable_type = kind + 'Buildable'
46
51
% 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)
52
+ % expressible_as_protocols = get_expressible_as_conformances(buildable_type)
50
53
% if expressible_as_protocols:
51
54
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
52
55
% else:
@@ -140,15 +143,20 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
140
143
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
141
144
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
142
145
% 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))
146
+ % param_type = param_type if child.is_token() else "ExpressibleAs" + param_type
147
+ % init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
144
148
% end
145
149
${',\n '.join(init_parameters)}
146
150
) {
147
151
% for child in node.children:
148
152
% create_method_dot = '?.' if child.is_optional else '.'
149
153
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
150
154
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token())
155
+ % if param_type is 'TokenSyntax':
156
+ self.${child.swift_name} = ${child.swift_name}
157
+ % else:
151
158
self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
159
+ % end
152
160
% end
153
161
}
154
162
@@ -204,11 +212,16 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
204
212
public struct ${node.syntax_kind}: SyntaxBuildable {
205
213
let elements: [${element_type}]
206
214
215
+ % param_type = element_type if node.is_token() else 'ExpressibleAs' + element_type
207
216
/// Creates a `${node.syntax_kind}` with the provided list of elements.
208
217
/// - Parameters:
209
218
/// - elements: A list of `ExpressibleAs${element_type}`
210
- public init(_ elements: [ExpressibleAs${element_type}]) {
219
+ public init(_ elements: [${param_type}]) {
220
+ % if node.is_token():
221
+ self.elements = elements
222
+ % else:
211
223
self.elements = elements.map { $0.create${element_type}() }
224
+ % end
212
225
}
213
226
214
227
public func build${node.syntax_kind}(format: Format) -> ${node.syntax_kind}Syntax {
@@ -239,9 +252,7 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
239
252
% end
240
253
% if node.is_buildable() or node.is_syntax_collection():
241
254
% 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)
255
+ % expressible_as_protocols = get_expressible_as_conformances(node.syntax_kind)
245
256
% if expressible_as_protocols:
246
257
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
247
258
% else:
@@ -258,36 +269,36 @@ extension ${node.syntax_kind}: ${expressible_as_type} {
258
269
259
270
% end
260
271
% end
261
- public protocol ExpressibleAsTokenSyntax {
262
- func createTokenSyntax() -> TokenSyntax
272
+ % expressible_as_protocols = get_expressible_as_conformances('TokenSyntax')
273
+ % if expressible_as_protocols:
274
+ extension TokenSyntax: ${', '.join(expressible_as_protocols)} {
263
275
}
276
+ % end
264
277
265
- extension TokenSyntax: ExpressibleAsTokenSyntax {
266
- public func createTokenSyntax() -> TokenSyntax {
267
- self
278
+ // MARK: - Syntax Collection buildable expressible as conformances
279
+
280
+ % for protocol, conformances in SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.items():
281
+ % for conformance in conformances:
282
+ % extension_protocol = protocol if protocol is 'TokenSyntax' else 'ExpressibleAs' + protocol
283
+ extension ${extension_protocol} {
284
+ public func create${conformance}() -> ${conformance} {
285
+ ${conformance}([self])
268
286
}
269
287
}
270
288
289
+ % end
290
+ % end
271
291
// MARK: - Syntax buildable expressible as conformances
272
292
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
283
293
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
284
294
% for conformance in conformances:
285
295
% node = NODE_MAP.get(conformance)
286
296
% if node and node.children:
287
297
% non_defaulted_params = filter(lambda child : syntax_buildable_default_init_value(child, SYNTAX_TOKEN_MAP.get(child.syntax_kind)) == "", node.children)
288
298
% assert len(non_defaulted_params) == 1, "ExpressibleAs conformances expects the conforming type to have an initializer with a single non-optional child"
289
299
% param = non_defaulted_params[0].swift_name
290
- extension ExpressibleAs${protocol} {
300
+ % extension_protocol = protocol if protocol is 'TokenSyntax' else 'ExpressibleAs' + protocol
301
+ extension ${extension_protocol} {
291
302
public func create${conformance}() -> ${conformance} {
292
303
${conformance}(${param}: self)
293
304
}
0 commit comments