未验证 提交 04f56799 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #41657 from CyrusNajmabadi/ifParsing2

Use less stack space while parsing if-statements
......@@ -6421,8 +6421,10 @@ private StatementSyntax ParseStatementNoDeclaration(bool allowAnyExpression)
case SyntaxKind.GotoKeyword:
return this.ParseGotoStatement();
case SyntaxKind.IfKeyword:
case SyntaxKind.ElseKeyword: // Including 'else' keyword to handle 'else without if' error cases
return this.ParseIfStatement();
case SyntaxKind.ElseKeyword:
// Including 'else' keyword to handle 'else without if' error cases
return this.ParseMisplacedElse();
case SyntaxKind.LockKeyword:
return this.ParseLockStatement();
case SyntaxKind.ReturnKeyword:
......@@ -7686,19 +7688,28 @@ private GotoStatementSyntax ParseGotoStatement()
private IfStatementSyntax ParseIfStatement()
{
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.IfKeyword || this.CurrentToken.Kind == SyntaxKind.ElseKeyword);
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.IfKeyword);
bool firstTokenIsElse = this.CurrentToken.Kind == SyntaxKind.ElseKeyword;
var @if = firstTokenIsElse
? this.EatToken(SyntaxKind.IfKeyword, ErrorCode.ERR_ElseCannotStartStatement)
: this.EatToken(SyntaxKind.IfKeyword);
var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var condition = this.ParseExpressionCore();
var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
var statement = firstTokenIsElse ? this.ParseExpressionStatement() : this.ParseEmbeddedStatement();
var elseClause = this.ParseElseClauseOpt();
return _syntaxFactory.IfStatement(
this.EatToken(SyntaxKind.IfKeyword),
this.EatToken(SyntaxKind.OpenParenToken),
this.ParseExpressionCore(),
this.EatToken(SyntaxKind.CloseParenToken),
this.ParseEmbeddedStatement(),
this.ParseElseClauseOpt());
}
private IfStatementSyntax ParseMisplacedElse()
{
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.ElseKeyword);
return _syntaxFactory.IfStatement(@if, openParen, condition, closeParen, statement, elseClause);
return _syntaxFactory.IfStatement(
this.EatToken(SyntaxKind.IfKeyword, ErrorCode.ERR_ElseCannotStartStatement),
this.EatToken(SyntaxKind.OpenParenToken),
this.ParseExpressionCore(),
this.EatToken(SyntaxKind.CloseParenToken),
this.ParseExpressionStatement(),
this.ParseElseClauseOpt());
}
private ElseClauseSyntax ParseElseClauseOpt()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册