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
+ # 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)
11
18
}%
12
19
//// Automatically Generated From DeclBuildables.swift.gyb.
13
20
//// Do Not Edit Directly!
@@ -44,9 +51,7 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
44
51
45
52
% buildable_type = kind + 'Buildable'
46
53
% 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)
50
55
% if expressible_as_protocols:
51
56
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
52
57
% else:
@@ -140,15 +145,20 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
140
145
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
141
146
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
142
147
% 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))
144
150
% end
145
151
${',\n '.join(init_parameters)}
146
152
) {
147
153
% for child in node.children:
148
154
% create_method_dot = '?.' if child.is_optional else '.'
149
155
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
150
156
% 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:
151
160
self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
161
+ % end
152
162
% end
153
163
}
154
164
@@ -204,11 +214,16 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
204
214
public struct ${node.syntax_kind}: SyntaxBuildable {
205
215
let elements: [${element_type}]
206
216
217
+ % param_type = element_type if node.is_token() else 'ExpressibleAs' + element_type
207
218
/// Creates a `${node.syntax_kind}` with the provided list of elements.
208
219
/// - Parameters:
209
220
/// - 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:
211
225
self.elements = elements.map { $0.create${element_type}() }
226
+ % end
212
227
}
213
228
214
229
public func build${node.syntax_kind}(format: Format) -> ${node.syntax_kind}Syntax {
@@ -239,9 +254,7 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
239
254
% end
240
255
% if node.is_buildable() or node.is_syntax_collection():
241
256
% 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)
245
258
% if expressible_as_protocols:
246
259
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
247
260
% else:
@@ -258,36 +271,36 @@ extension ${node.syntax_kind}: ${expressible_as_type} {
258
271
259
272
% end
260
273
% 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)} {
263
277
}
278
+ % end
264
279
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])
268
288
}
269
289
}
270
290
291
+ % end
292
+ % end
271
293
// MARK: - Syntax buildable expressible as conformances
272
294
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
295
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
284
296
% for conformance in conformances:
285
297
% node = NODE_MAP.get(conformance)
286
298
% if node and node.children:
287
299
% non_defaulted_params = filter(lambda child : syntax_buildable_default_init_value(child, SYNTAX_TOKEN_MAP.get(child.syntax_kind)) == "", node.children)
288
300
% assert len(non_defaulted_params) == 1, "ExpressibleAs conformances expects the conforming type to have an initializer with a single non-optional child"
289
301
% 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} {
291
304
public func create${conformance}() -> ${conformance} {
292
305
${conformance}(${param}: self)
293
306
}
0 commit comments