5
5
# -*- mode: Swift -*-
6
6
# Ignore the following admonition it applies to the resulting . swift file only
7
7
} %
8
- //// Automatically Generated From DeclBuildables .swift.gyb.
8
+ //// Automatically Generated From BuildablesConvenienceInitializers .swift.gyb.
9
9
//// Do Not Edit Directly!
10
10
//===----------------------------------------------------------------------===//
11
11
//
@@ -24,13 +24,19 @@ import SwiftSyntax
24
24
% for node in SYNTAX_NODES:
25
25
% if node. is_buildable ( ) :
26
26
% has _syntax_collection_child = False
27
+ % has _token_child = False
27
28
% for child in node. children:
28
29
% child_node = NODE_MAP . get ( child. syntax_kind)
30
+ % child_token = SYNTAX_TOKEN_MAP . get ( child. syntax_kind)
29
31
% if child_node and child_node. is_syntax_collection ( ) :
32
+ % # If node have syntax collection as child , we can create a convenient initializer
30
33
% has_syntax_collection_child = True
34
+ % elif ( child_token and ( not child. is_optional or not child_token. text) ) or child. syntax_kind is " IdentifierToken " :
35
+ % # If the node have at least one child that is a token or identifier token, we can create a Cconvenient initializer
36
+ % has_token_child = True
31
37
% end
32
38
% end
33
- % if has_syntax_collection_child:
39
+ % if has_syntax_collection_child or h as _token_child :
34
40
extension ${ node. syntax_kind} {
35
41
public init(
36
42
% init_parameters = [ ]
@@ -45,18 +51,27 @@ extension ${node.syntax_kind} {
45
51
% # Allow initializing identifier or a token without a text with String value
46
52
% param_type = " String? " if child. is_optional else " String "
47
53
% init_parameters. append ( " %s: %s " % ( child. swift_name, param_type) )
48
- % else :
49
- % # When type is not handled above , use default value
54
+ % elif h as _syntax_collection_child :
55
+ % # When node have a syntax collection as child we can use all parameters
50
56
% param_type = syntax_buildable_child_type ( child. type_name, child. syntax_kind, child. is_token ( ) , child. is_optional)
51
57
% default_value = " "
52
58
% if token and token. text and not child. is_optional:
53
59
% default_value = " = Tokens.`%s` " % lowercase_first_word( token. name)
54
60
% elif child. is_optional:
55
61
% default_value = " = nil "
56
62
% init_parameters. append ( " %s: %s%s " % ( child. swift_name, param_type, default_value) )
63
+ % elif not token or child. is_optional:
64
+ % # Allow initializing with parameter when there is no token or the parameter is optional
65
+ % param_type = syntax_buildable_child_type ( child. type_name, child. syntax_kind, child. is_token ( ) , child. is_optional)
66
+ % default_value = " "
67
+ % if child. is_optional:
68
+ % default_value = " = nil "
69
+ % init_parameters. append ( " %s: %s%s " % ( child. swift_name, param_type, default_value) )
57
70
% end
58
71
% end
72
+ % if bool ( init_parameters) :
59
73
${ ', \n '. join ( init_parameters) }
74
+ % end
60
75
) {
61
76
self . init(
62
77
% init_parameters = [ ]
@@ -71,11 +86,13 @@ extension ${node.syntax_kind} {
71
86
% else:
72
87
% init_parameters . append( " %s: SyntaxFactory.makeIdentifier(%s) " % ( child. swift_name, child. swift_name) )
73
88
% end
89
+ % elif not child. is_optional and not has _syntax_collection_child and token and token. text :
90
+ % init_parameters . append( " %s: Tokens.`%s` " % ( child. swift_name, lowercase_first_word ( token. name) ) )
74
91
% elif token and not token. text:
75
92
% if child. is_optional:
76
93
% init_parameters . append( " %s: %s.map(Tokens.%s) " % ( child. swift_name, child. swift_name, lowercase_first_word ( token. name) ) )
77
94
% else:
78
- % init_parameters . append( " %s: Tokens.%s(%s) " % ( child. swift_name, token. name, child. swift_name) )
95
+ % init_parameters . append( " %s: Tokens.%s(%s) " % ( child. swift_name, lowercase_first_word ( token. name) , child. swift_name) )
79
96
% end
80
97
% else:
81
98
% init_parameters . append( " %s: %s " % ( child. swift_name, child. swift_name) )
@@ -88,4 +105,4 @@ extension ${node.syntax_kind} {
88
105
89
106
% end
90
107
% end
91
- % end
108
+ % end
0 commit comments