Skip to content

Commit 7b2b829

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

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
@@ -278,6 +278,28 @@
278278
Child("BooleanLiteral", kind='KeywordToken', token_choices=['KeywordToken|true', 'KeywordToken|false'])
279279
]),
280280

281+
# if-expr -> identifier? ':'? 'if' condition-list code-block
282+
# else-clause ';'?
283+
#
284+
# This node represents both an 'if' expression, as well as an 'if' statement
285+
# when wrapped in a ExpressionStmt node.
286+
Node('IfExpr', name_for_diagnostics="'if' statement", kind='Expr',
287+
traits=['WithCodeBlock'],
288+
children=[
289+
Child('IfKeyword', kind='KeywordToken', token_choices=['KeywordToken|if']),
290+
Child('Conditions', kind='ConditionElementList',
291+
collection_element_name='Condition'),
292+
Child('Body', kind='CodeBlock', name_for_diagnostics='body'),
293+
Child('ElseKeyword', kind='ElseToken',
294+
is_optional=True),
295+
Child('ElseBody', kind='Syntax', name_for_diagnostics='else body',
296+
node_choices=[
297+
Child('IfExpr', kind='IfExpr'),
298+
Child('CodeBlock', kind='CodeBlock'),
299+
],
300+
is_optional=True),
301+
]),
302+
281303
# ? expr :
282304
# Ternary expression without the condition and the second choice.
283305
# 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
@@ -239,25 +239,6 @@
239239
Child('Expression', kind='Expr'),
240240
]),
241241

242-
# if-stmt -> identifier? ':'? 'if' condition-list code-block
243-
# else-clause ';'?
244-
Node('IfStmt', name_for_diagnostics="'if' statement", kind='Stmt',
245-
traits=['WithCodeBlock'],
246-
children=[
247-
Child('IfKeyword', kind='KeywordToken', token_choices=['KeywordToken|if']),
248-
Child('Conditions', kind='ConditionElementList',
249-
collection_element_name='Condition'),
250-
Child('Body', kind='CodeBlock', name_for_diagnostics='body'),
251-
Child('ElseKeyword', kind='ElseToken',
252-
is_optional=True),
253-
Child('ElseBody', kind='Syntax', name_for_diagnostics='else body',
254-
node_choices=[
255-
Child('IfStmt', kind='IfStmt'),
256-
Child('CodeBlock', kind='CodeBlock'),
257-
],
258-
is_optional=True),
259-
]),
260-
261242
# switch-case -> unknown-attr? switch-case-label stmt-list
262243
# | unknown-attr? switch-default-label stmt-list
263244
Node('SwitchCase', name_for_diagnostics='switch case', kind='Syntax',

0 commit comments

Comments
 (0)