1
1
% {
2
2
from gyb_syntax_support import *
3
- from gyb_syntax_support. kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
3
+ from gyb_syntax_support. kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type, syntax_buildable_default_init_value
4
4
NODE_MAP = create_node_map ( )
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
//
@@ -23,36 +23,36 @@ import SwiftSyntax
23
23
24
24
% for node in SYNTAX_NODES:
25
25
% if node. is_buildable ( ) :
26
- % h as _syntax_collection_child = False
26
+ % should_create_convenience_initializer = False
27
27
% for child in node. children:
28
28
% child_node = NODE_MAP . get ( child. syntax_kind)
29
- % if child_node and child_node. is_syntax_collection ( ) :
30
- % has_syntax_collection_child = True
29
+ % child_token = SYNTAX_TOKEN_MAP . get ( child. syntax_kind)
30
+ % if ( child_node and child_node. is_syntax_collection ( ) ) or ( child_token and not child_token. text) or ( child. syntax_kind is " IdentifierToken " ) :
31
+ % # If node have syntax collection as child , we can create a convenient initializer
32
+ % # If the child token have do nt have a text, it takes a String as parameter , we can create a convenient initializer
33
+ % # If child token is an IdentifierToken it can be made as a String, we can create a convenient initializer
34
+ % should_create_convenience_initializer = True
31
35
% end
32
36
% end
33
- % if has_syntax_collection_child :
37
+ % if should_create_convenience_initializer :
34
38
extension ${ node. syntax_kind} {
35
39
public init(
36
40
% init_parameters = [ ]
37
41
% for child in node. children:
38
42
% child_node = NODE_MAP . get ( child. syntax_kind)
39
- % token = SYNTAX_TOKEN_MAP . get ( child. syntax_kind)
43
+ % child_token = SYNTAX_TOKEN_MAP . get ( child. syntax_kind)
40
44
% if child_node and child_node. is_syntax_collection ( ) :
41
45
% # Allow initializing syntax collections with result builders
42
46
% default_value = " ? = { nil } " if child. is_optional else " = { .empty } "
43
47
% init_parameters. append ( " @%sBuilder %sBuilder: () -> %s%s " % ( child. syntax_kind, child. swift_name, child. syntax_kind, default_value) )
44
- % elif child. syntax_kind is " IdentifierToken " or ( token and not token . text) :
48
+ % elif child. syntax_kind is " IdentifierToken " or ( child_token and not child_token . text) :
45
49
% # Allow initializing identifier or a token without a text with String value
46
50
% param_type = " String? " if child. is_optional else " String "
47
51
% init_parameters. append ( " %s: %s " % ( child. swift_name, param_type) )
48
52
% else:
49
53
% # When type is not handled above, use default value
50
54
% param_type = syntax_buildable_child_type ( child. type_name, child. syntax_kind, child. is_token ( ) , child. is_optional)
51
- % default_value = " "
52
- % if token and token. text and not child. is_optional:
53
- % default_value = " = Tokens.`%s` " % lowercase_first_word( token. name)
54
- % elif child. is_optional:
55
- % default_value = " = nil "
55
+ % default_value = syntax_buildable_default_init_value ( child, child_token)
56
56
% init_parameters. append ( " %s: %s%s " % ( child. swift_name, param_type, default_value) )
57
57
% end
58
58
% end
@@ -75,7 +75,7 @@ extension ${node.syntax_kind} {
75
75
% if child. is_optional:
76
76
% init_parameters . append( " %s: %s.map(Tokens.%s) " % ( child. swift_name, child. swift_name, lowercase_first_word ( token. name) ) )
77
77
% else:
78
- % init_parameters . append( " %s: Tokens.%s(%s) " % ( child. swift_name, token. name, child. swift_name) )
78
+ % init_parameters . append( " %s: Tokens.%s(%s) " % ( child. swift_name, lowercase_first_word ( token. name) , child. swift_name) )
79
79
% end
80
80
% else:
81
81
% init_parameters . append( " %s: %s " % ( child. swift_name, child. swift_name) )
@@ -88,4 +88,4 @@ extension ${node.syntax_kind} {
88
88
89
89
% end
90
90
% end
91
- % end
91
+ % end
0 commit comments