Skip to content

Commit 8830fdd

Browse files
committed
[Syntax] Parse "do statement" syntax node
1 parent f102636 commit 8830fdd

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,10 @@ static void parseGuardedPattern(Parser &P, GuardedPattern &result,
998998
VD->setHasNonPatternBindingInit();
999999

10001000
// Parse the optional 'where' guard.
1001-
if (P.consumeIf(tok::kw_where, result.WhereLoc)) {
1001+
if (P.Tok.is(tok::kw_where)) {
1002+
SyntaxParsingContext WhereClauseCtxt(P.SyntaxContext,
1003+
SyntaxKind::WhereClause);
1004+
result.WhereLoc = P.consumeToken(tok::kw_where);
10021005
SourceLoc startOfGuard = P.Tok.getLoc();
10031006

10041007
auto diagKind = [=]() -> Diag<> {
@@ -1665,6 +1668,7 @@ ParserResult<Stmt> Parser::parseStmtRepeat(LabeledStmtInfo labelInfo) {
16651668
/// (identifier ':')? 'do' stmt-brace
16661669
/// (identifier ':')? 'do' stmt-brace stmt-catch+
16671670
ParserResult<Stmt> Parser::parseStmtDo(LabeledStmtInfo labelInfo) {
1671+
SyntaxContext->setCreateSyntax(SyntaxKind::DoStmt);
16681672
SourceLoc doLoc = consumeToken(tok::kw_do);
16691673

16701674
ParserStatus status;
@@ -1678,6 +1682,8 @@ ParserResult<Stmt> Parser::parseStmtDo(LabeledStmtInfo labelInfo) {
16781682

16791683
// If the next token is 'catch', this is a 'do'/'catch' statement.
16801684
if (Tok.is(tok::kw_catch)) {
1685+
SyntaxParsingContext CatchListCtxt(SyntaxContext,
1686+
SyntaxKind::CatchClauseList);
16811687
// Parse 'catch' clauses
16821688
SmallVector<CatchStmt*, 4> allClauses;
16831689
do {
@@ -1743,6 +1749,7 @@ ParserResult<Stmt> Parser::parseStmtDo(LabeledStmtInfo labelInfo) {
17431749
/// This routine promises to return a non-null result unless there was
17441750
/// a code-completion token in the pattern.
17451751
ParserResult<CatchStmt> Parser::parseStmtCatch() {
1752+
SyntaxParsingContext CatchClauseCtxt(SyntaxContext, SyntaxKind::CatchClause);
17461753
// A catch block has its own scope for variables bound out of the pattern.
17471754
Scope S(this, ScopeKind::CatchVars);
17481755

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class C <MemberDeclBlock>{<VariableDecl><Attribute>
221221
for <ValueBindingPattern>let <TuplePattern>(<TuplePatternElement><IdentifierPattern>x</IdentifierPattern>, </TuplePatternElement><TuplePatternElement><WildcardPattern>_</WildcardPattern></TuplePatternElement>) </TuplePattern></ValueBindingPattern>in <IdentifierExpr>foo </IdentifierExpr><CodeBlock>{}</CodeBlock>
222222
for <ValueBindingPattern>var <IdentifierPattern>x</IdentifierPattern></ValueBindingPattern><TypeAnnotation>: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier></TypeAnnotation>in <IdentifierExpr>foo </IdentifierExpr><CodeBlock>{}</CodeBlock>
223223
}</CodeBlock></FunctionDecl>
224-
}</MemberDeclBlock></ClassDecl>
224+
}</MemberDeclBlock></ClassDecl><DoStmt>
225225

226226
do <CodeBlock>{
227227
switch <IdentifierExpr>foo </IdentifierExpr>{
@@ -235,9 +235,16 @@ do <CodeBlock>{
235235
case <ValueBindingPattern>let <ExpressionPattern><SequenceExpr><UnresolvedPatternExpr><IdentifierPattern>a </IdentifierPattern></UnresolvedPatternExpr><AsExpr>as <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AsExpr></SequenceExpr></ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
236236
case <ValueBindingPattern>let <ExpressionPattern><UnresolvedPatternExpr><IdentifierPattern>a</IdentifierPattern></UnresolvedPatternExpr>?</ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
237237
}
238-
}</CodeBlock><FunctionDecl>
239-
240-
func statementTests<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{
238+
}</CodeBlock></DoStmt><FunctionDecl>
239+
240+
func statementTests<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{<DoStmt>
241+
do <CodeBlock>{
242+
} </CodeBlock><CatchClause>catch <ExpressionPattern><TupleExpr>(<TupleElement><UnresolvedPatternExpr><ValueBindingPattern>var <IdentifierPattern>x</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr>, </TupleElement><TupleElement><UnresolvedPatternExpr><ValueBindingPattern>let <IdentifierPattern>y</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr></TupleElement>) </TupleExpr></ExpressionPattern><CodeBlock>{
243+
} </CodeBlock></CatchClause><CatchClause>catch <WhereClause>where <BooleanLiteralExpr>false </BooleanLiteralExpr></WhereClause><CodeBlock>{
244+
} </CodeBlock></CatchClause><CatchClause>catch <ValueBindingPattern>let <IdentifierPattern>e </IdentifierPattern></ValueBindingPattern><WhereClause>where <SequenceExpr><MemberAccessExpr><IdentifierExpr>e</IdentifierExpr>.foo </MemberAccessExpr><BinaryOperatorExpr>== </BinaryOperatorExpr><IdentifierExpr>bar </IdentifierExpr></SequenceExpr></WhereClause><CodeBlock>{
245+
} </CodeBlock></CatchClause><CatchClause>catch <CodeBlock>{
246+
}</CodeBlock></CatchClause></DoStmt><DoStmt>
247+
LABEL: do <CodeBlock>{}</CodeBlock></DoStmt>
241248
LABEL: switch <IdentifierExpr>foo </IdentifierExpr>{
242249
case <ExpressionPattern><IntegerLiteralExpr>1</IntegerLiteralExpr></ExpressionPattern>:<FallthroughStmt>
243250
fallthrough</FallthroughStmt>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ do {
238238
}
239239

240240
func statementTests() {
241+
do {
242+
} catch (var x, let y) {
243+
} catch where false {
244+
} catch let e where e.foo == bar {
245+
} catch {
246+
}
247+
LABEL: do {}
241248
LABEL: switch foo {
242249
case 1:
243250
fallthrough

utils/gyb_syntax_support/ExprNodes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
Child('Elements', kind='ExprList'),
7575
]),
7676

77+
Node('ExprList', kind='SyntaxCollection',
78+
element='Expr',
79+
element_name='Expression'),
80+
7781
# A #line expression.
7882
Node('PoundLineExpr', kind='Expr',
7983
children=[

utils/gyb_syntax_support/StmtNodes.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@
7373
is_optional=True),
7474
]),
7575

76-
Node('ExprList', kind='SyntaxCollection',
77-
element='Expr',
78-
element_name='Expression'),
79-
8076
Node('WhereClause', kind='Syntax',
8177
children=[
8278
Child('WhereKeyword', kind='WhereToken'),
83-
Child('Expressions', kind='ExprList'),
79+
Child('GuardResult', kind='Expr'),
8480
]),
8581

8682
# for-in-stmt -> label? ':'? 'for' 'case'? pattern 'in' expr 'where'?
@@ -134,7 +130,8 @@
134130
is_optional=True),
135131
Child('DoKeyword', kind='DoToken'),
136132
Child('Body', kind='CodeBlock'),
137-
Child('CatchClauses', kind='CatchClauseList'),
133+
Child('CatchClauses', kind='CatchClauseList',
134+
is_optional=True),
138135
Child('Semicolon', kind='SemicolonToken',
139136
is_optional=True),
140137
]),

0 commit comments

Comments
 (0)