Skip to content

Commit 1b81fcb

Browse files
authored
SwiftSyntax: Add a trait for those statement nodes with code block as body. (#14726)
This patch also refactors SyntaxNodes code so that protocol conformances are declared as extensions.
1 parent c12d1db commit 1b81fcb

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

tools/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,6 @@ public struct UnknownSyntax: _SyntaxBase {
5454
}
5555
}
5656

57-
% for trait in TRAITS:
58-
public protocol ${trait.trait_name} {
59-
% for child in trait.children:
60-
% ret_type = child.type_name
61-
% if child.is_optional:
62-
% ret_type += '?'
63-
% end
64-
var ${child.swift_name}: ${ret_type} { get }
65-
func with${child.name}(_ newChild: ${child.type_name}?) -> Self
66-
% end
67-
}
68-
% end
69-
7057
% for node in SYNTAX_NODES:
7158
% base_type = node.base_type
7259
% if node.is_base():
@@ -75,12 +62,7 @@ public protocol ${node.name}: Syntax {}
7562
% elif node.collection_element:
7663
% pass
7764
% else:
78-
% traits_list = ""
79-
% if node.traits:
80-
% traits_list = ", ".join(node.traits)
81-
% traits_list = traits_list + ", "
82-
% end
83-
public struct ${node.name}: ${traits_list} ${base_type}, _SyntaxBase, Hashable {
65+
public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
8466
% if node.children:
8567
enum Cursor: Int {
8668
% for child in node.children:
@@ -192,6 +174,31 @@ public struct ${node.name}: ${traits_list} ${base_type}, _SyntaxBase, Hashable {
192174
% end
193175
% end
194176

177+
% for trait in TRAITS:
178+
public protocol ${trait.trait_name}Syntax {
179+
% for child in trait.children:
180+
% ret_type = child.type_name
181+
% if child.is_optional:
182+
% ret_type += '?'
183+
% end
184+
var ${child.swift_name}: ${ret_type} { get }
185+
func with${child.name}(_ newChild: ${child.type_name}?) -> Self
186+
% end
187+
}
188+
% end
189+
190+
% for node in SYNTAX_NODES:
191+
% base_type = node.base_type
192+
% if node.is_base():
193+
% pass
194+
% elif node.collection_element:
195+
% pass
196+
% elif node.traits:
197+
% traits_list = ", ".join(trait + 'Syntax' for trait in node.traits)
198+
extension ${node.name}: ${traits_list} {}
199+
% end
200+
% end
201+
195202
/// MARK: Convenience methods
196203

197204
extension StructDeclSyntax {

utils/gyb_syntax_support/CommonNodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
# code-block -> '{' stmt-list '}'
3434
Node('CodeBlock', kind='Syntax',
35-
traits=['BracedSyntax'],
35+
traits=['Braced'],
3636
children=[
3737
Child('LeftBrace', kind='LeftBraceToken'),
3838
Child('Statements', kind='CodeBlockItemList'),

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# typealias-name generic-parameter-clause?
1616
# type-assignment
1717
# typealias-name -> identifier
18-
Node('TypealiasDecl', kind='Decl', traits=['IdentifiedDeclSyntax'],
18+
Node('TypealiasDecl', kind='Decl', traits=['IdentifiedDecl'],
1919
children=[
2020
Child('Attributes', kind='AttributeList',
2121
is_optional=True),
@@ -36,7 +36,7 @@
3636
# inheritance-clause? type-assignment?
3737
# generic-where-clause?
3838
# associatedtype-name -> identifier
39-
Node('AssociatedtypeDecl', kind='Decl', traits=['IdentifiedDeclSyntax'],
39+
Node('AssociatedtypeDecl', kind='Decl', traits=['IdentifiedDecl'],
4040
children=[
4141
Child('Attributes', kind='AttributeList',
4242
is_optional=True),
@@ -158,7 +158,7 @@
158158
# '{' class-members '}'
159159
# class-name -> identifier
160160
Node('ClassDecl', kind='Decl',
161-
traits=['DeclGroupSyntax', 'IdentifiedDeclSyntax'],
161+
traits=['DeclGroup', 'IdentifiedDecl'],
162162
children=[
163163
Child('Attributes', kind='AttributeList',
164164
is_optional=True),
@@ -183,7 +183,7 @@
183183
# '{' struct-members '}'
184184
# struct-name -> identifier
185185
Node('StructDecl', kind='Decl',
186-
traits=['DeclGroupSyntax', 'IdentifiedDeclSyntax'],
186+
traits=['DeclGroup', 'IdentifiedDecl'],
187187
children=[
188188
Child('Attributes', kind='AttributeList',
189189
is_optional=True),
@@ -201,7 +201,7 @@
201201
]),
202202

203203
Node('ProtocolDecl', kind='Decl',
204-
traits=['DeclGroupSyntax', 'IdentifiedDeclSyntax'],
204+
traits=['DeclGroup', 'IdentifiedDecl'],
205205
children=[
206206
Child('Attributes', kind='AttributeList',
207207
is_optional=True),
@@ -222,7 +222,7 @@
222222
# generic-where-clause?
223223
# '{' extension-members '}'
224224
# extension-name -> identifier
225-
Node('ExtensionDecl', kind='Decl', traits=['DeclGroupSyntax'],
225+
Node('ExtensionDecl', kind='Decl', traits=['DeclGroup'],
226226
children=[
227227
Child('Attributes', kind='AttributeList',
228228
is_optional=True),
@@ -237,7 +237,7 @@
237237
Child('Members', kind='MemberDeclBlock'),
238238
]),
239239

240-
Node('MemberDeclBlock', kind='Syntax', traits=['BracedSyntax'],
240+
Node('MemberDeclBlock', kind='Syntax', traits=['Braced'],
241241
children=[
242242
Child('LeftBrace', kind='LeftBraceToken'),
243243
Child('Members', kind='DeclList'),
@@ -315,7 +315,7 @@
315315
element='Syntax',
316316
element_name='Modifier'),
317317

318-
Node('FunctionDecl', kind='Decl', traits=['IdentifiedDeclSyntax'],
318+
Node('FunctionDecl', kind='Decl', traits=['IdentifiedDecl'],
319319
children=[
320320
Child('Attributes', kind='AttributeList',
321321
is_optional=True),
@@ -467,7 +467,7 @@
467467

468468
Node('AccessorList', kind="SyntaxCollection", element='AccessorDecl'),
469469

470-
Node('AccessorBlock', kind="Syntax", traits=['BracedSyntax'],
470+
Node('AccessorBlock', kind="Syntax", traits=['Braced'],
471471
children=[
472472
Child('LeftBrace', kind='LeftBraceToken'),
473473
Child('AccessorListOrStmtList', kind='Syntax',

utils/gyb_syntax_support/ExprNodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
]),
385385

386386
Node('ClosureExpr', kind='Expr',
387-
traits=['BracedSyntax'],
387+
traits=['Braced'],
388388
children=[
389389
Child('LeftBrace', kind='LeftBraceToken'),
390390
Child('Signature', kind='ClosureSignature', is_optional=True),

utils/gyb_syntax_support/StmtNodes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# while-stmt -> label? ':'? 'while' condition-list code-block ';'?
1414
Node('WhileStmt', kind='Stmt',
15+
traits=['WithCodeBlock'],
1516
children=[
1617
Child('LabelName', kind='IdentifierToken',
1718
is_optional=True),
@@ -24,6 +25,7 @@
2425

2526
# defer-stmt -> 'defer' code-block ';'?
2627
Node('DeferStmt', kind='Stmt',
28+
traits=['WithCodeBlock'],
2729
children=[
2830
Child('DeferKeyword', kind='DeferToken'),
2931
Child('Body', kind='CodeBlock'),
@@ -41,6 +43,7 @@
4143

4244
# repeat-while-stmt -> label? ':'? 'repeat' code-block 'while' expr ';'?
4345
Node('RepeatWhileStmt', kind='Stmt',
46+
traits=['WithCodeBlock'],
4447
children=[
4548
Child('LabelName', kind='IdentifierToken',
4649
is_optional=True),
@@ -54,6 +57,7 @@
5457

5558
# guard-stmt -> 'guard' condition-list 'else' code-block ';'?
5659
Node('GuardStmt', kind='Stmt',
60+
traits=['WithCodeBlock'],
5761
children=[
5862
Child('GuardKeyword', kind='GuardToken'),
5963
Child('Conditions', kind='ConditionElementList'),
@@ -70,6 +74,7 @@
7074
# for-in-stmt -> label? ':'? 'for' 'case'? pattern 'in' expr 'where'?
7175
# expr code-block ';'?
7276
Node('ForInStmt', kind='Stmt',
77+
traits=['WithCodeBlock'],
7378
children=[
7479
Child('LabelName', kind='IdentifierToken',
7580
is_optional=True),
@@ -91,7 +96,7 @@
9196
# switch-stmt -> identifier? ':'? 'switch' expr '{'
9297
# switch-case-list '}' ';'?
9398
Node('SwitchStmt', kind='Stmt',
94-
traits=['BracedSyntax'],
99+
traits=['Braced'],
95100
children=[
96101
Child('LabelName', kind='IdentifierToken',
97102
is_optional=True),
@@ -110,6 +115,7 @@
110115

111116
# do-stmt -> identifier? ':'? 'do' code-block catch-clause-list ';'?
112117
Node('DoStmt', kind='Stmt',
118+
traits=['WithCodeBlock'],
113119
children=[
114120
Child('LabelName', kind='IdentifierToken',
115121
is_optional=True),
@@ -212,6 +218,7 @@
212218
# if-stmt -> identifier? ':'? 'if' condition-list code-block
213219
# else-clause ';'?
214220
Node('IfStmt', kind='Stmt',
221+
traits=['WithCodeBlock'],
215222
children=[
216223
Child('LabelName', kind='IdentifierToken',
217224
is_optional=True),
@@ -238,6 +245,7 @@
238245

239246
# else-clause -> 'else' code-block
240247
Node('ElseBlock', kind='Syntax',
248+
traits=['WithCodeBlock'],
241249
children=[
242250
Child('ElseKeyword', kind='ElseToken'),
243251
Child('Body', kind='CodeBlock'),

utils/gyb_syntax_support/Traits.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ def __init__(self, trait_name, children):
88

99

1010
TRAITS = [
11-
Trait('DeclGroupSyntax',
11+
Trait('DeclGroup',
1212
children=[
1313
Child('Attributes', kind='AttributeList', is_optional=True),
1414
Child('AccessLevelModifier', kind='DeclModifier',
1515
is_optional=True),
1616
Child('Members', kind='MemberDeclBlock'),
1717
]),
1818

19-
Trait('BracedSyntax',
19+
Trait('Braced',
2020
children=[
2121
Child('LeftBrace', kind='LeftBraceToken'),
2222
Child('RightBrace', kind='RightBraceToken'),
2323
]),
2424

25-
Trait('IdentifiedDeclSyntax',
25+
Trait('IdentifiedDecl',
2626
children=[
2727
Child('Identifier', kind='IdentifierToken'),
2828
]),
29+
30+
Trait('WithCodeBlock',
31+
children=[
32+
Child('Body', kind='CodeBlock'),
33+
]),
2934
]

0 commit comments

Comments
 (0)