Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,24 @@ static ParserResult<Stmt> recoverFromInvalidCase(Parser &P) {
ParserResult<Stmt> Parser::parseStmt() {
AssertParserMadeProgressBeforeLeavingScopeRAII apmp(*this);

SyntaxParsingContext LocalContext(SyntaxContext, SyntaxContextKind::Stmt);

// Note that we're parsing a statement.
StructureMarkerRAII ParsingStmt(*this, Tok.getLoc(),
StructureMarkerKind::Statement);

LabeledStmtInfo LabelInfo;

// If this is a label on a loop/switch statement, consume it and pass it into
// parsing logic below.
SyntaxParsingContext LabelContext(SyntaxContext, SyntaxKind::LabeledStmt);
LabeledStmtInfo LabelInfo;
if (Tok.is(tok::identifier) && peekToken().is(tok::colon)) {
LabelInfo.Loc = consumeIdentifier(LabelInfo.Name,
/*diagnoseDollarPrefix=*/true);
consumeToken(tok::colon);
} else {
LabelContext.setTransparent();
}

SyntaxParsingContext LocalContext(SyntaxContext, SyntaxContextKind::Stmt);

// Note that we're parsing a statement.
StructureMarkerRAII ParsingStmt(*this, Tok.getLoc(),
StructureMarkerKind::Statement);

SourceLoc tryLoc;
(void)consumeIf(tok::kw_try, tryLoc);

Expand Down
14 changes: 7 additions & 7 deletions test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,19 @@ func statementTests<FunctionSignature><ParameterClause>() </ParameterClause></Fu
} </CodeBlock></CatchClause><CatchClause>catch <CatchItem><ExpressionPattern><FunctionCallExpr><MemberAccessExpr>.a</MemberAccessExpr>(<TupleExprElement><UnresolvedPatternExpr><ValueBindingPattern>let <IdentifierPattern>a</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr></TupleExprElement>)</FunctionCallExpr></ExpressionPattern>, </CatchItem><CatchItem><ExpressionPattern><FunctionCallExpr><MemberAccessExpr>.b</MemberAccessExpr>(<TupleExprElement><UnresolvedPatternExpr><ValueBindingPattern>let <IdentifierPattern>b</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr></TupleExprElement>) </FunctionCallExpr></ExpressionPattern><WhereClause>where <SequenceExpr><IdentifierExpr>b </IdentifierExpr><BinaryOperatorExpr>== </BinaryOperatorExpr><StringLiteralExpr>"<StringSegment></StringSegment>" </StringLiteralExpr></SequenceExpr></WhereClause></CatchItem><CodeBlock>{
} </CodeBlock></CatchClause><CatchClause>catch <CatchItem></CatchItem><CodeBlock>{
}</CodeBlock></CatchClause></DoStmt><RepeatWhileStmt>
repeat <CodeBlock>{ } </CodeBlock>while <BooleanLiteralExpr>true</BooleanLiteralExpr></RepeatWhileStmt><RepeatWhileStmt>
LABEL: repeat <CodeBlock>{ } </CodeBlock>while <BooleanLiteralExpr>false</BooleanLiteralExpr></RepeatWhileStmt><WhileStmt>
while <ConditionElement><BooleanLiteralExpr>true </BooleanLiteralExpr></ConditionElement><CodeBlock>{ }</CodeBlock></WhileStmt><WhileStmt>
LABEL: while <ConditionElement><BooleanLiteralExpr>true </BooleanLiteralExpr></ConditionElement><CodeBlock>{ }</CodeBlock></WhileStmt><DoStmt>
LABEL: do <CodeBlock>{}</CodeBlock></DoStmt><SwitchStmt>
LABEL: switch <IdentifierExpr>foo </IdentifierExpr>{<SwitchCase><SwitchCaseLabel>
repeat <CodeBlock>{ } </CodeBlock>while <BooleanLiteralExpr>true</BooleanLiteralExpr></RepeatWhileStmt><LabeledStmt>
LABEL: <RepeatWhileStmt>repeat <CodeBlock>{ } </CodeBlock>while <BooleanLiteralExpr>false</BooleanLiteralExpr></RepeatWhileStmt></LabeledStmt><WhileStmt>
while <ConditionElement><BooleanLiteralExpr>true </BooleanLiteralExpr></ConditionElement><CodeBlock>{ }</CodeBlock></WhileStmt><LabeledStmt>
LABEL: <WhileStmt>while <ConditionElement><BooleanLiteralExpr>true </BooleanLiteralExpr></ConditionElement><CodeBlock>{ }</CodeBlock></WhileStmt></LabeledStmt><LabeledStmt>
LABEL: <DoStmt>do <CodeBlock>{}</CodeBlock></DoStmt></LabeledStmt><LabeledStmt>
LABEL: <SwitchStmt>switch <IdentifierExpr>foo </IdentifierExpr>{<SwitchCase><SwitchCaseLabel>
case <CaseItem><ExpressionPattern><IntegerLiteralExpr>1</IntegerLiteralExpr></ExpressionPattern></CaseItem>:</SwitchCaseLabel><FallthroughStmt>
fallthrough</FallthroughStmt></SwitchCase><SwitchCase><SwitchCaseLabel>
case <CaseItem><ExpressionPattern><IntegerLiteralExpr>2</IntegerLiteralExpr></ExpressionPattern></CaseItem>:</SwitchCaseLabel><BreakStmt>
break LABEL</BreakStmt></SwitchCase><SwitchCase><SwitchCaseLabel>
case <CaseItem><ExpressionPattern><IntegerLiteralExpr>3</IntegerLiteralExpr></ExpressionPattern></CaseItem>:</SwitchCaseLabel><BreakStmt>
break</BreakStmt></SwitchCase>
}</SwitchStmt><ForInStmt>
}</SwitchStmt></LabeledStmt><ForInStmt>

for <IdentifierPattern>a </IdentifierPattern>in <IdentifierExpr>b </IdentifierExpr><CodeBlock>{<DeferStmt>
defer <CodeBlock>{ <TupleExpr>() </TupleExpr>}</CodeBlock></DeferStmt><IfStmt>
Expand Down
1 change: 1 addition & 0 deletions utils/gyb_syntax_support/NodeSerializationCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
'MissingType': 264,
'MissingPattern': 265,
'GarbageNodes' : 266,
'LabeledStmt': 267,
}


Expand Down
2 changes: 1 addition & 1 deletion utils/gyb_syntax_support/PatternNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

# tuple-pattern-element -> identifier? ':' pattern ','?
Node('TuplePatternElement', kind='Syntax',
traits=['WithTrailingComma', 'Labeled'],
traits=['WithTrailingComma'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand Down
44 changes: 14 additions & 30 deletions utils/gyb_syntax_support/StmtNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
from .Node import Node # noqa: I201

STMT_NODES = [
# labeled-stmt -> label ':' stmt
Node('LabeledStmt', kind='Stmt',
children=[
Child('LabelName', kind='IdentifierToken'),
Child('LabelColon', kind='ColonToken'),
Child('Statement', kind='Stmt'),
]),

# continue-stmt -> 'continue' label? ';'?
Node('ContinueStmt', kind='Stmt',
children=[
Expand All @@ -12,12 +20,8 @@

# while-stmt -> label? ':'? 'while' condition-list code-block ';'?
Node('WhileStmt', kind='Stmt',
traits=['WithCodeBlock', 'Labeled'],
traits=['WithCodeBlock'],
Comment on lines 21 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the specifications be updated as well?

Copy link
Contributor Author

@CodaFi CodaFi Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What specification are you referring to? The "grammar" is still correct - we don't necessarily want the notion of a "labeled statement" in the formal grammar since not all the statements support labels!

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to the comment in the above preview (ditto the other nodes you modified), not the actual grammar in the language reference, although, curiously, it does have a notion of a labeled statement too.

children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Child('LabelColon', kind='ColonToken',
is_optional=True),
Child('WhileKeyword', kind='WhileToken'),
Child('Conditions', kind='ConditionElementList',
collection_element_name='Condition'),
Expand Down Expand Up @@ -46,12 +50,8 @@

# repeat-while-stmt -> label? ':'? 'repeat' code-block 'while' expr ';'?
Node('RepeatWhileStmt', kind='Stmt',
traits=['WithCodeBlock', 'Labeled'],
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Child('LabelColon', kind='ColonToken',
is_optional=True),
Child('RepeatKeyword', kind='RepeatToken'),
Child('Body', kind='CodeBlock'),
Child('WhileKeyword', kind='WhileToken'),
Expand Down Expand Up @@ -79,12 +79,8 @@
# 'for' 'try'? 'await'? 'case'? pattern 'in' expr 'where'?
# expr code-block ';'?
Node('ForInStmt', kind='Stmt',
traits=['WithCodeBlock', 'Labeled'],
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Child('LabelColon', kind='ColonToken',
is_optional=True),
Child('ForKeyword', kind='ForToken'),
Child('TryKeyword', kind='TryToken',
is_optional=True),
Expand All @@ -106,12 +102,8 @@
# switch-stmt -> identifier? ':'? 'switch' expr '{'
# switch-case-list '}' ';'?
Node('SwitchStmt', kind='Stmt',
traits=['Braced', 'Labeled'],
traits=['Braced'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Child('LabelColon', kind='ColonToken',
is_optional=True),
Child('SwitchKeyword', kind='SwitchToken'),
Child('Expression', kind='Expr'),
Child('LeftBrace', kind='LeftBraceToken'),
Expand All @@ -127,12 +119,8 @@

# do-stmt -> identifier? ':'? 'do' code-block catch-clause-list ';'?
Node('DoStmt', kind='Stmt',
traits=['WithCodeBlock', 'Labeled'],
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Child('LabelColon', kind='ColonToken',
is_optional=True),
Child('DoKeyword', kind='DoToken'),
Child('Body', kind='CodeBlock'),
Child('CatchClauses', kind='CatchClauseList',
Expand Down Expand Up @@ -272,12 +260,8 @@
# if-stmt -> identifier? ':'? 'if' condition-list code-block
# else-clause ';'?
Node('IfStmt', kind='Stmt',
traits=['WithCodeBlock', 'Labeled'],
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Child('LabelColon', kind='ColonToken',
is_optional=True),
Child('IfKeyword', kind='IfToken'),
Child('Conditions', kind='ConditionElementList',
collection_element_name='Condition'),
Expand Down
6 changes: 0 additions & 6 deletions utils/gyb_syntax_support/Traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ def __init__(self, trait_name, description=None, children=None):
Child('TrailingComma', kind='CommaToken', is_optional=True),
]),

Trait('Labeled',
children=[
Child('LabelName', kind='IdentifierToken', is_optional=True),
Child('LabelColon', kind='ColonToken', is_optional=True),
]),

Trait('WithStatements',
children=[
Child('Statements', kind='CodeBlockItemList'),
Expand Down