Skip to content

Commit 1603369

Browse files
authored
Merge pull request #284 from kimdv/kimdv/add-convenience-inits
Add convenience inits
2 parents 08ef05b + 9eda615 commit 1603369

File tree

9 files changed

+1785
-25
lines changed

9 files changed

+1785
-25
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22
from gyb_syntax_support import *
33
from gyb_syntax_support.kinds import lowercase_first_word
44
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
5+
from gyb_syntax_support.kinds import syntax_buildable_child_type
56
# -*- mode: Swift -*-
67
# Ignore the following admonition it applies to the resulting .swift file only
7-
8-
def syntax_buildable_child_type(type_name, syntax_kind, is_token, is_optional=False):
9-
if syntax_kind in SYNTAX_BASE_KINDS:
10-
buildable_type = syntax_kind + 'Buildable'
11-
elif not is_token:
12-
buildable_type = syntax_kind
13-
else:
14-
buildable_type = type_name
15-
16-
if is_optional:
17-
buildable_type += '?'
18-
19-
return buildable_type
208
}%
219
//// Automatically Generated From DeclBuildables.swift.gyb.
10+
//// Do Not Edit Directly!
2211
//===----------------------------------------------------------------------===//
2312
//
2413
// This source file is part of the Swift.org open source project
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
4+
NODE_MAP = create_node_map()
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From DeclBuildables.swift.gyb.
9+
//// Do Not Edit Directly!
10+
//===----------------------------------------------------------------------===//
11+
//
12+
// This source file is part of the Swift.org open source project
13+
//
14+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
15+
// Licensed under Apache License v2.0 with Runtime Library Exception
16+
//
17+
// See https://swift.org/LICENSE.txt for license information
18+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
import SwiftSyntax
23+
24+
% for node in SYNTAX_NODES:
25+
% if node.is_buildable():
26+
% has_syntax_collection_child = False
27+
% for child in node.children:
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
31+
% end
32+
% end
33+
% if has_syntax_collection_child:
34+
extension ${node.syntax_kind} {
35+
public init(
36+
% init_parameters = []
37+
% for child in node.children:
38+
% child_node = NODE_MAP.get(child.syntax_kind)
39+
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
40+
% if child_node and child_node.is_syntax_collection():
41+
% # Allow initializing syntax collections with result builders
42+
% default_value = "? = { nil }" if child.is_optional else " = { .empty }"
43+
% 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):
45+
% # Allow initializing identifier or a token without a text with String value
46+
% param_type = "String?" if child.is_optional else "String"
47+
% init_parameters.append("%s: %s" % (child.swift_name, param_type))
48+
% else:
49+
% # When type is not handled above, use default value
50+
% 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"
56+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
57+
% end
58+
% end
59+
${',\n '.join(init_parameters)}
60+
) {
61+
self.init(
62+
% init_parameters = []
63+
% for child in node.children:
64+
% child_node = NODE_MAP.get(child.syntax_kind)
65+
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
66+
% if child_node and child_node.is_syntax_collection():
67+
% init_parameters.append("%s: %sBuilder()" % (child.swift_name, child.swift_name))
68+
% elif child.syntax_kind is "IdentifierToken":
69+
% if child.is_optional:
70+
% init_parameters.append("%s: %s.map({ SyntaxFactory.makeIdentifier($0) })" % (child.swift_name, child.swift_name))
71+
% else:
72+
% init_parameters.append("%s: SyntaxFactory.makeIdentifier(%s)" % (child.swift_name, child.swift_name))
73+
% end
74+
% elif token and not token.text:
75+
% if child.is_optional:
76+
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, child.swift_name, lowercase_first_word(token.name)))
77+
% else:
78+
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, token.name, child.swift_name))
79+
% end
80+
% else:
81+
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
82+
% end
83+
% end
84+
${',\n '.join(init_parameters)}
85+
)
86+
}
87+
}
88+
89+
% end
90+
% end
91+
% end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
4+
NODE_MAP = create_node_map()
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From ResultBuilders.swift.gyb.
9+
//// Do Not Edit Directly!
10+
//===----------------------------------------------------------------------===//
11+
//
12+
// This source file is part of the Swift.org open source project
13+
//
14+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
15+
// Licensed under Apache License v2.0 with Runtime Library Exception
16+
//
17+
// See https://swift.org/LICENSE.txt for license information
18+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
import SwiftSyntax
23+
24+
% for node in SYNTAX_NODES:
25+
% if node.is_syntax_collection():
26+
@resultBuilder
27+
public struct ${node.syntax_kind}Builder {
28+
% element_type = syntax_buildable_child_type(node.collection_element_type, node.collection_element, node.is_token())
29+
public static func buildBlock(_ elements: ${element_type}...) -> ${node.syntax_kind} {
30+
${node.syntax_kind}(elements)
31+
}
32+
}
33+
34+
extension ${node.syntax_kind} {
35+
public static let empty: ${node.syntax_kind} = ${node.syntax_kind}([])
36+
}
37+
38+
% end
39+
% end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
extension SimpleTypeIdentifier {
16+
public init(_ name: String) {
17+
self.init(name: SyntaxFactory.makeIdentifier(name),
18+
genericArgumentClause: nil)
19+
}
20+
}

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//// Automatically Generated From DeclBuildables.swift.gyb.
2+
//// Do Not Edit Directly!
23
//===----------------------------------------------------------------------===//
34
//
45
// This source file is part of the Swift.org open source project

0 commit comments

Comments
 (0)