Skip to content

Commit fecc81c

Browse files
committed
Introduce IfExprSyntax
This replaces IfStmtSyntax, and will be wrapped in an ExpressionStmtSyntax when used as a statement.
1 parent edd2d0c commit fecc81c

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

gyb_syntax_support/ExprNodes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,28 @@
285285
])
286286
]),
287287

288+
# if-expr -> identifier? ':'? 'if' condition-list code-block
289+
# else-clause ';'?
290+
#
291+
# This node represents both an 'if' expression, as well as an 'if' statement
292+
# when wrapped in a ExpressionStmt node.
293+
Node('IfExpr', name_for_diagnostics="'if' statement", kind='Expr',
294+
traits=['WithCodeBlock'],
295+
children=[
296+
Child('IfKeyword', kind='IfToken'),
297+
Child('Conditions', kind='ConditionElementList',
298+
collection_element_name='Condition'),
299+
Child('Body', kind='CodeBlock', name_for_diagnostics='body'),
300+
Child('ElseKeyword', kind='ElseToken',
301+
is_optional=True),
302+
Child('ElseBody', kind='Syntax', name_for_diagnostics='else body',
303+
node_choices=[
304+
Child('IfExpr', kind='IfExpr'),
305+
Child('CodeBlock', kind='CodeBlock'),
306+
],
307+
is_optional=True),
308+
]),
309+
288310
# ? expr :
289311
# Ternary expression without the condition and the second choice.
290312
# NOTE: This appears only in SequenceExpr.

gyb_syntax_support/StmtNodes.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,25 +271,6 @@
271271
Child('Expression', kind='Expr'),
272272
]),
273273

274-
# if-stmt -> identifier? ':'? 'if' condition-list code-block
275-
# else-clause ';'?
276-
Node('IfStmt', name_for_diagnostics="'if' statement", kind='Stmt',
277-
traits=['WithCodeBlock'],
278-
children=[
279-
Child('IfKeyword', kind='IfToken'),
280-
Child('Conditions', kind='ConditionElementList',
281-
collection_element_name='Condition'),
282-
Child('Body', kind='CodeBlock', name_for_diagnostics='body'),
283-
Child('ElseKeyword', kind='ElseToken',
284-
is_optional=True),
285-
Child('ElseBody', kind='Syntax', name_for_diagnostics='else body',
286-
node_choices=[
287-
Child('IfStmt', kind='IfStmt'),
288-
Child('CodeBlock', kind='CodeBlock'),
289-
],
290-
is_optional=True),
291-
]),
292-
293274
# switch-case -> unknown-attr? switch-case-label stmt-list
294275
# | unknown-attr? switch-default-label stmt-list
295276
Node('SwitchCase', name_for_diagnostics='switch case', kind='Syntax',

0 commit comments

Comments
 (0)