3
3
from gyb_syntax_support.kinds import lowercase_first_word
4
4
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
5
5
from gyb_syntax_support.kinds import syntax_buildable_child_type, syntax_buildable_default_init_value
6
+ from gyb_syntax_support.protocolsMap import SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES
6
7
# -*- mode: Swift -*-
7
8
# Ignore the following admonition it applies to the resulting .swift file only
8
9
}%
@@ -40,16 +41,18 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
40
41
}
41
42
42
43
% buildable_type = kind + 'Buildable'
43
- public protocol ExpressibleAs${buildable_type} {
44
+ % expressible_as_type = 'ExpressibleAs' + buildable_type
45
+ % expressible_as_protocols = SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(expressible_as_type)
46
+ % if expressible_as_protocols:
47
+ public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
48
+ % else:
49
+ public protocol ${expressible_as_type} {
50
+ % end
44
51
func create${buildable_type}() -> ${buildable_type}
45
52
}
46
53
47
54
% if kind == 'Syntax':
48
55
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, ${kind}ListBuildable {
49
- % elif kind == 'Decl':
50
- public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsMemberDeclListItem, ExpressibleAsCodeBlockItem {
51
- % elif kind == 'Stmt':
52
- public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable, ExpressibleAsCodeBlockItem {
53
56
% else:
54
57
public protocol ${buildable_type}: ExpressibleAs${buildable_type}, SyntaxBuildable, ${kind}ListBuildable {
55
58
% end
@@ -97,22 +100,6 @@ extension ${buildable_type} {
97
100
[build${kind}(format: format, leadingTrivia: leadingTrivia)]
98
101
}
99
102
}
100
- % if kind == 'Decl':
101
-
102
- extension DeclBuildable {
103
- public func createMemberDeclListItem() -> MemberDeclListItem {
104
- MemberDeclListItem(decl: self)
105
- }
106
- }
107
- % end
108
- % if kind in ['Decl', 'Stmt']:
109
-
110
- extension ${kind}Buildable {
111
- public func createCodeBlockItem() -> CodeBlockItem {
112
- CodeBlockItem(item: self)
113
- }
114
- }
115
- % end
116
103
117
104
% end
118
105
% end
@@ -137,12 +124,15 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
137
124
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
138
125
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
139
126
% default_value = syntax_buildable_default_init_value(child, child_token)
140
- % init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
127
+ % init_parameters.append("%s: ExpressibleAs %s%s" % (child.swift_name, param_type, default_value))
141
128
% end
142
129
${',\n '.join(init_parameters)}
143
130
) {
144
131
% for child in node.children:
145
- self.${child.swift_name} = ${child.swift_name}
132
+ % create_method_dot = '?.' if child.is_optional else '.'
133
+ % child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
134
+ % param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token())
135
+ self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
146
136
% end
147
137
}
148
138
@@ -198,8 +188,8 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
198
188
public struct ${node.syntax_kind}: SyntaxBuildable {
199
189
let elements: [${element_type}]
200
190
201
- public init(_ elements: [${element_type}]) {
202
- self.elements = elements
191
+ public init(_ elements: [ExpressibleAs ${element_type}]) {
192
+ self.elements = elements.map { $0.create${element_type}() }
203
193
}
204
194
205
195
public func build${node.syntax_kind}(format: Format) -> ${node.syntax_kind}Syntax {
@@ -229,11 +219,17 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
229
219
230
220
% end
231
221
% if node.is_buildable() or node.is_syntax_collection():
232
- public protocol ExpressibleAs${node.syntax_kind} {
222
+ % expressible_as_type = 'ExpressibleAs' + node.syntax_kind
223
+ % expressible_as_protocols = SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(expressible_as_type)
224
+ % if expressible_as_protocols:
225
+ public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
226
+ % else:
227
+ public protocol ${expressible_as_type} {
228
+ % end
233
229
func create${node.syntax_kind}() -> ${node.syntax_kind}
234
230
}
235
231
236
- extension ${node.syntax_kind}: ExpressibleAs${node.syntax_kind } {
232
+ extension ${node.syntax_kind}: ${expressible_as_type } {
237
233
public func create${node.syntax_kind}() -> ${node.syntax_kind} {
238
234
self
239
235
}
@@ -249,4 +245,41 @@ extension TokenSyntax: ExpressibleAsTokenSyntax {
249
245
public func createTokenSyntax() -> TokenSyntax {
250
246
self
251
247
}
252
- }
248
+ }
249
+
250
+ // MARK: - Syntax buildable expressible as conformances
251
+
252
+ % for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
253
+ % if 'ExpressibleAsConditionElementList' in conformances:
254
+ extension ${protocol} {
255
+ public func createConditionElementList() -> ConditionElementList {
256
+ ConditionElementList([self])
257
+ }
258
+ }
259
+
260
+ % end
261
+ % if 'ExpressibleAsConditionElement' in conformances:
262
+ extension ${protocol} {
263
+ public func createConditionElement() -> ConditionElement {
264
+ ConditionElement(condition: self)
265
+ }
266
+ }
267
+
268
+ % end
269
+ % if 'ExpressibleAsCodeBlockItem' in conformances:
270
+ extension ${protocol} {
271
+ public func createCodeBlockItem() -> CodeBlockItem {
272
+ CodeBlockItem(item: self)
273
+ }
274
+ }
275
+
276
+ % end
277
+ % if 'ExpressibleAsMemberDeclListItem' in conformances:
278
+ extension ${protocol} {
279
+ public func createMemberDeclListItem() -> MemberDeclListItem {
280
+ MemberDeclListItem(decl: self)
281
+ }
282
+ }
283
+
284
+ % end
285
+ % end
0 commit comments