Skip to content

Commit b2694e2

Browse files
committed
Add Buildable gyb
1 parent 4cb05fc commit b2694e2

File tree

3 files changed

+5249
-4
lines changed

3 files changed

+5249
-4
lines changed

Sources/SwiftSyntaxBuilder/DeclBuildables.swift renamed to Sources/SwiftSyntaxBuilder/DeclBuildables.swift.gyb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import lowercase_first_word, kind_to_type
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From DeclBuildables.swift.gyb.
18
//===----------------------------------------------------------------------===//
29
//
310
// This source file is part of the Swift.org open source project
@@ -185,3 +192,55 @@ public struct Struct: DeclBuildable {
185192
return DeclSyntax(structDecl)
186193
}
187194
}
195+
196+
// MARK: Generated
197+
198+
% for node in SYNTAX_NODES:
199+
% if node.is_buildable():
200+
% protocol = 'SyntaxBuildable'
201+
% return_type = 'Syntax'
202+
% build_type = 'Syntax'
203+
% if node.base_kind == 'Expr':
204+
% protocol = 'ExprBuildable'
205+
% return_type = 'ExprSyntax'
206+
% build_type = 'Expr'
207+
% elif node.base_kind == 'Decl':
208+
% protocol = 'DeclBuildable'
209+
% return_type = 'DeclSyntax'
210+
% build_type = 'Decl'
211+
% end
212+
public struct ${node.syntax_kind}: ${protocol} {
213+
% child_params = []
214+
% for child in node.children:
215+
% param_type = child.type_name
216+
% if child.is_optional:
217+
% param_type = param_type + "?"
218+
% end
219+
% child_params.append("%s: %s" % (child.swift_name, param_type))
220+
% end
221+
% for child_param in child_params:
222+
let ${child_param}
223+
% end
224+
225+
public init(
226+
${',\n '.join(child_params)}
227+
) {
228+
% for child in node.children:
229+
self.${lowercase_first_word(child.name)} = ${lowercase_first_word(child.name)}
230+
% end
231+
}
232+
233+
public func build${build_type}(format: Format, leadingTrivia: Trivia) -> ${return_type} {
234+
let ${node.swift_syntax_kind} = SyntaxFactory.make${node.syntax_kind}(
235+
% child_params = []
236+
% for child in node.children:
237+
% child_params.append("%s: %s" % (child.swift_name, child.swift_name))
238+
% end
239+
${',\n '.join(child_params)}
240+
).withLeadingTrivia(leadingTrivia)
241+
return ${return_type}(${node.swift_syntax_kind})
242+
}
243+
}
244+
245+
% end
246+
% end

0 commit comments

Comments
 (0)