|
5 | 5 | from gyb_syntax_support.kinds import syntax_buildable_child_type, syntax_buildable_default_init_value |
6 | 6 | from gyb_syntax_support.protocolsMap import SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES |
7 | 7 | SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES = syntax_collection_element_to_collection_mapping() |
| 8 | + NODE_MAP = create_node_map() |
8 | 9 | # -*- mode: Swift -*- |
9 | 10 | # Ignore the following admonition it applies to the resulting .swift file only |
10 | 11 | }% |
@@ -264,57 +265,19 @@ extension TokenSyntax: ExpressibleAsTokenSyntax { |
264 | 265 |
|
265 | 266 | % end |
266 | 267 | % end |
267 | | - |
268 | | -% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items(): |
269 | | -% if 'ExpressibleAsConditionElementList' in conformances: |
270 | | -extension ${protocol} { |
271 | | - public func createConditionElementList() -> ConditionElementList { |
272 | | - ConditionElementList([self]) |
273 | | - } |
274 | | -} |
275 | | - |
276 | | -% end |
277 | | -% end |
278 | | - |
279 | 268 | % for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items(): |
280 | | -% if 'ConditionElement' in conformances: |
| 269 | +% for conformance in conformances: |
| 270 | +% node = NODE_MAP.get(conformance) |
| 271 | +% if node and node.children: |
| 272 | +% non_defaulted_params = filter(lambda child : syntax_buildable_default_init_value(child, SYNTAX_TOKEN_MAP.get(child.syntax_kind)) == "", node.children) |
| 273 | +% assert len(non_defaulted_params) == 1, "ExpressibleAs conformances expects the conforming type to have an initializer with a single non-optional child" |
| 274 | +% param = non_defaulted_params[0].swift_name |
281 | 275 | extension ExpressibleAs${protocol} { |
282 | | - public func createConditionElement() -> ConditionElement { |
283 | | - ConditionElement(condition: self) |
284 | | - } |
285 | | -} |
286 | | - |
287 | | -% end |
288 | | -% if 'CodeBlockItem' in conformances: |
289 | | -extension ExpressibleAs${protocol} { |
290 | | - public func createCodeBlockItem() -> CodeBlockItem { |
291 | | - CodeBlockItem(item: self) |
292 | | - } |
293 | | -} |
294 | | - |
295 | | -% end |
296 | | -% if 'MemberDeclListItem' in conformances: |
297 | | -extension ExpressibleAs${protocol} { |
298 | | - public func createMemberDeclListItem() -> MemberDeclListItem { |
299 | | - MemberDeclListItem(decl: self) |
300 | | - } |
301 | | -} |
302 | | - |
303 | | -% end |
304 | | -% if 'TypeAnnotation' in conformances: |
305 | | -extension ExpressibleAs${protocol} { |
306 | | - public func createTypeAnnotation() -> TypeAnnotation { |
307 | | - TypeAnnotation(type: self) |
| 276 | + public func create${conformance}() -> ${conformance} { |
| 277 | + ${conformance}(${param}: self) |
308 | 278 | } |
309 | 279 | } |
310 | 280 |
|
| 281 | +% end |
311 | 282 | % end |
312 | | -% if 'TypeExpr' in conformances: |
313 | | - extension ExpressibleAs${protocol} { |
314 | | - public func createTypeExpr() -> TypeExpr { |
315 | | - TypeExpr(type: self) |
316 | | - } |
317 | | - } |
318 | | - |
319 | | - % end |
320 | 283 | % end |
0 commit comments