Skip to content

Commit f102636

Browse files
committed
[Syntax] Parse basic statement nodes
* ReturnStmt -> 'return' expr? * BreakStmt -> 'break' identifier? * ContinueStmt -> 'continue' identifier? * FallthroughStmt -> 'fallthrough' * ThrowStmt -> 'throw' expr * DeferStmt -> 'defer' code-block
1 parent c283a69 commit f102636

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,15 @@ ParserResult<Stmt> Parser::parseStmt() {
569569
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
570570
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
571571
return parseStmtContinue();
572-
case tok::kw_fallthrough:
572+
case tok::kw_fallthrough: {
573573
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
574574
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
575+
576+
SyntaxContext->setCreateSyntax(SyntaxKind::FallthroughStmt);
575577
return makeParserResult(
576578
new (Context) FallthroughStmt(consumeToken(tok::kw_fallthrough)));
577579
}
580+
}
578581
}
579582

580583
/// parseBraceItemList - A brace enclosed expression/statement/decl list. For
@@ -615,6 +618,7 @@ ParserResult<BraceStmt> Parser::parseBraceItemList(Diag<> ID) {
615618
/// 'break' identifier?
616619
///
617620
ParserResult<Stmt> Parser::parseStmtBreak() {
621+
SyntaxContext->setCreateSyntax(SyntaxKind::BreakStmt);
618622
SourceLoc Loc = consumeToken(tok::kw_break);
619623
SourceLoc TargetLoc;
620624
Identifier Target;
@@ -637,6 +641,7 @@ ParserResult<Stmt> Parser::parseStmtBreak() {
637641
/// 'continue' identifier?
638642
///
639643
ParserResult<Stmt> Parser::parseStmtContinue() {
644+
SyntaxContext->setCreateSyntax(SyntaxKind::ContinueStmt);
640645
SourceLoc Loc = consumeToken(tok::kw_continue);
641646
SourceLoc TargetLoc;
642647
Identifier Target;
@@ -660,7 +665,7 @@ ParserResult<Stmt> Parser::parseStmtContinue() {
660665
/// 'return' expr?
661666
///
662667
ParserResult<Stmt> Parser::parseStmtReturn(SourceLoc tryLoc) {
663-
SyntaxParsingContext LocalContext(SyntaxContext, SyntaxKind::ReturnStmt);
668+
SyntaxContext->setCreateSyntax(SyntaxKind::ReturnStmt);
664669
SourceLoc ReturnLoc = consumeToken(tok::kw_return);
665670

666671
if (Tok.is(tok::code_complete)) {
@@ -725,6 +730,7 @@ ParserResult<Stmt> Parser::parseStmtReturn(SourceLoc tryLoc) {
725730
/// 'throw' expr
726731
///
727732
ParserResult<Stmt> Parser::parseStmtThrow(SourceLoc tryLoc) {
733+
SyntaxContext->setCreateSyntax(SyntaxKind::ThrowStmt);
728734
SourceLoc throwLoc = consumeToken(tok::kw_throw);
729735
SourceLoc exprLoc;
730736
if (Tok.isNot(tok::eof))
@@ -759,6 +765,7 @@ ParserResult<Stmt> Parser::parseStmtThrow(SourceLoc tryLoc) {
759765
/// 'defer' brace-stmt
760766
///
761767
ParserResult<Stmt> Parser::parseStmtDefer() {
768+
SyntaxContext->setCreateSyntax(SyntaxKind::DeferStmt);
762769
SourceLoc DeferLoc = consumeToken(tok::kw_defer);
763770

764771
// Macro expand out the defer into a closure and call, which we can typecheck

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,36 @@ class C <MemberDeclBlock>{<VariableDecl><Attribute>
225225

226226
do <CodeBlock>{
227227
switch <IdentifierExpr>foo </IdentifierExpr>{
228-
case <ValueBindingPattern>let <IdentifierPattern>a</IdentifierPattern></ValueBindingPattern>: break
229-
case <ValueBindingPattern>let <ExpressionPattern><SequenceExpr><UnresolvedPatternExpr><IdentifierPattern>a </IdentifierPattern></UnresolvedPatternExpr><AsExpr>as <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AsExpr></SequenceExpr></ExpressionPattern></ValueBindingPattern>: break
230-
case <ValueBindingPattern>let <ExpressionPattern><TupleExpr>(<TupleElement><UnresolvedPatternExpr><IdentifierPattern>a</IdentifierPattern></UnresolvedPatternExpr>, </TupleElement><TupleElement><UnresolvedPatternExpr><IdentifierPattern>b</IdentifierPattern></UnresolvedPatternExpr></TupleElement>)</TupleExpr></ExpressionPattern></ValueBindingPattern>: break
231-
case <ExpressionPattern><TupleExpr>(<TupleElement><UnresolvedPatternExpr><ValueBindingPattern>let <IdentifierPattern>a</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr>, </TupleElement><TupleElement><UnresolvedPatternExpr><ValueBindingPattern>var <IdentifierPattern>b</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr></TupleElement>)</TupleExpr></ExpressionPattern>: break
232-
case <IsTypePattern>is <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></IsTypePattern>: break
233-
case <ValueBindingPattern>let <ExpressionPattern>.bar(<FunctionCallArgument><UnresolvedPatternExpr><IdentifierPattern>x</IdentifierPattern></UnresolvedPatternExpr></FunctionCallArgument>)</ExpressionPattern></ValueBindingPattern>: break
234-
case <ExpressionPattern><MemberAccessExpr><IdentifierExpr>MyEnum</IdentifierExpr>.foo</MemberAccessExpr></ExpressionPattern>: break
235-
case <ValueBindingPattern>let <ExpressionPattern><SequenceExpr><UnresolvedPatternExpr><IdentifierPattern>a </IdentifierPattern></UnresolvedPatternExpr><AsExpr>as <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AsExpr></SequenceExpr></ExpressionPattern></ValueBindingPattern>: break
236-
case <ValueBindingPattern>let <ExpressionPattern><UnresolvedPatternExpr><IdentifierPattern>a</IdentifierPattern></UnresolvedPatternExpr>?</ExpressionPattern></ValueBindingPattern>: break
228+
case <ValueBindingPattern>let <IdentifierPattern>a</IdentifierPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
229+
case <ValueBindingPattern>let <ExpressionPattern><SequenceExpr><UnresolvedPatternExpr><IdentifierPattern>a </IdentifierPattern></UnresolvedPatternExpr><AsExpr>as <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AsExpr></SequenceExpr></ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
230+
case <ValueBindingPattern>let <ExpressionPattern><TupleExpr>(<TupleElement><UnresolvedPatternExpr><IdentifierPattern>a</IdentifierPattern></UnresolvedPatternExpr>, </TupleElement><TupleElement><UnresolvedPatternExpr><IdentifierPattern>b</IdentifierPattern></UnresolvedPatternExpr></TupleElement>)</TupleExpr></ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
231+
case <ExpressionPattern><TupleExpr>(<TupleElement><UnresolvedPatternExpr><ValueBindingPattern>let <IdentifierPattern>a</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr>, </TupleElement><TupleElement><UnresolvedPatternExpr><ValueBindingPattern>var <IdentifierPattern>b</IdentifierPattern></ValueBindingPattern></UnresolvedPatternExpr></TupleElement>)</TupleExpr></ExpressionPattern>: <BreakStmt>break</BreakStmt>
232+
case <IsTypePattern>is <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></IsTypePattern>: <BreakStmt>break</BreakStmt>
233+
case <ValueBindingPattern>let <ExpressionPattern>.bar(<FunctionCallArgument><UnresolvedPatternExpr><IdentifierPattern>x</IdentifierPattern></UnresolvedPatternExpr></FunctionCallArgument>)</ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
234+
case <ExpressionPattern><MemberAccessExpr><IdentifierExpr>MyEnum</IdentifierExpr>.foo</MemberAccessExpr></ExpressionPattern>: <BreakStmt>break</BreakStmt>
235+
case <ValueBindingPattern>let <ExpressionPattern><SequenceExpr><UnresolvedPatternExpr><IdentifierPattern>a </IdentifierPattern></UnresolvedPatternExpr><AsExpr>as <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AsExpr></SequenceExpr></ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
236+
case <ValueBindingPattern>let <ExpressionPattern><UnresolvedPatternExpr><IdentifierPattern>a</IdentifierPattern></UnresolvedPatternExpr>?</ExpressionPattern></ValueBindingPattern>: <BreakStmt>break</BreakStmt>
237237
}
238-
}</CodeBlock>
238+
}</CodeBlock><FunctionDecl>
239+
240+
func statementTests<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{
241+
LABEL: switch <IdentifierExpr>foo </IdentifierExpr>{
242+
case <ExpressionPattern><IntegerLiteralExpr>1</IntegerLiteralExpr></ExpressionPattern>:<FallthroughStmt>
243+
fallthrough</FallthroughStmt>
244+
case <ExpressionPattern><IntegerLiteralExpr>2</IntegerLiteralExpr></ExpressionPattern>:<BreakStmt>
245+
break LABEL</BreakStmt>
246+
case <ExpressionPattern><IntegerLiteralExpr>3</IntegerLiteralExpr></ExpressionPattern>:<BreakStmt>
247+
break</BreakStmt>
248+
}
249+
250+
for <IdentifierPattern>a </IdentifierPattern>in <IdentifierExpr>b </IdentifierExpr><CodeBlock>{<DeferStmt>
251+
defer <CodeBlock>{ <TupleExpr>() </TupleExpr>}</CodeBlock></DeferStmt>
252+
if <IdentifierExpr>c </IdentifierExpr><CodeBlock>{<ThrowStmt>
253+
throw <IdentifierExpr>MyError</IdentifierExpr>()</ThrowStmt><ContinueStmt>
254+
continue</ContinueStmt>
255+
} </CodeBlock>else <CodeBlock>{<ContinueStmt>
256+
continue LABEL</ContinueStmt>
257+
}</CodeBlock>
258+
}</CodeBlock>
259+
260+
}</CodeBlock></FunctionDecl>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,25 @@ do {
236236
case let a?: break
237237
}
238238
}
239+
240+
func statementTests() {
241+
LABEL: switch foo {
242+
case 1:
243+
fallthrough
244+
case 2:
245+
break LABEL
246+
case 3:
247+
break
248+
}
249+
250+
for a in b {
251+
defer { () }
252+
if c {
253+
throw MyError()
254+
continue
255+
} else {
256+
continue LABEL
257+
}
258+
}
259+
260+
}

0 commit comments

Comments
 (0)