Commit fa43f4c9 authored by nikolaos's avatar nikolaos Committed by Commit bot

Synchronize scopes between parser/preparser

This patch introduces new scopes in the preparser, just like they
are introduced by the parser, in the following places:

-   blocks
-   try statement
-   switch statement
-   scoped statements, in several places
-   for statement
-   eager function bodies

R=rossberg@chromium.org
BUG=
LOG=N

Review URL: https://codereview.chromium.org/1906793002

Cr-Commit-Position: refs/heads/master@{#35708}
parent 9bac4015
...@@ -278,6 +278,8 @@ PreParser::Statement PreParser::ParseScopedStatement(bool legacy, bool* ok) { ...@@ -278,6 +278,8 @@ PreParser::Statement PreParser::ParseScopedStatement(bool legacy, bool* ok) {
(legacy && allow_harmony_restrictive_declarations())) { (legacy && allow_harmony_restrictive_declarations())) {
return ParseSubStatement(kDisallowLabelledFunctionStatement, ok); return ParseSubStatement(kDisallowLabelledFunctionStatement, ok);
} else { } else {
Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
BlockState block_state(&scope_, body_scope);
return ParseFunctionDeclaration(CHECK_OK); return ParseFunctionDeclaration(CHECK_OK);
} }
} }
...@@ -415,10 +417,14 @@ PreParser::Statement PreParser::ParseBlock(bool* ok) { ...@@ -415,10 +417,14 @@ PreParser::Statement PreParser::ParseBlock(bool* ok) {
// Block :: // Block ::
// '{' StatementList '}' // '{' StatementList '}'
Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
Expect(Token::LBRACE, CHECK_OK); Expect(Token::LBRACE, CHECK_OK);
Statement final = Statement::Default(); Statement final = Statement::Default();
while (peek() != Token::RBRACE) { {
final = ParseStatementListItem(CHECK_OK); BlockState block_state(&scope_, block_scope);
while (peek() != Token::RBRACE) {
final = ParseStatementListItem(CHECK_OK);
}
} }
Expect(Token::RBRACE, ok); Expect(Token::RBRACE, ok);
return final; return final;
...@@ -714,23 +720,27 @@ PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) { ...@@ -714,23 +720,27 @@ PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) {
ParseExpression(true, CHECK_OK); ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
Expect(Token::LBRACE, CHECK_OK); Scope* cases_scope = NewScope(scope_, BLOCK_SCOPE);
Token::Value token = peek(); {
while (token != Token::RBRACE) { BlockState cases_block_state(&scope_, cases_scope);
if (token == Token::CASE) { Expect(Token::LBRACE, CHECK_OK);
Expect(Token::CASE, CHECK_OK); Token::Value token = peek();
ParseExpression(true, CHECK_OK); while (token != Token::RBRACE) {
} else { if (token == Token::CASE) {
Expect(Token::DEFAULT, CHECK_OK); Expect(Token::CASE, CHECK_OK);
} ParseExpression(true, CHECK_OK);
Expect(Token::COLON, CHECK_OK); } else {
token = peek(); Expect(Token::DEFAULT, CHECK_OK);
Statement statement = Statement::Jump(); }
while (token != Token::CASE && Expect(Token::COLON, CHECK_OK);
token != Token::DEFAULT &&
token != Token::RBRACE) {
statement = ParseStatementListItem(CHECK_OK);
token = peek(); token = peek();
Statement statement = Statement::Jump();
while (token != Token::CASE &&
token != Token::DEFAULT &&
token != Token::RBRACE) {
statement = ParseStatementListItem(CHECK_OK);
token = peek();
}
} }
} }
Expect(Token::RBRACE, ok); Expect(Token::RBRACE, ok);
...@@ -770,6 +780,11 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) { ...@@ -770,6 +780,11 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
// ForStatement :: // ForStatement ::
// 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
// Create an in-between scope for let-bound iteration variables.
Scope* for_scope = NewScope(scope_, BLOCK_SCOPE);
bool has_lexical = false;
BlockState block_state(&scope_, for_scope);
Expect(Token::FOR, CHECK_OK); Expect(Token::FOR, CHECK_OK);
Expect(Token::LPAREN, CHECK_OK); Expect(Token::LPAREN, CHECK_OK);
if (peek() != Token::SEMICOLON) { if (peek() != Token::SEMICOLON) {
...@@ -784,6 +799,7 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) { ...@@ -784,6 +799,7 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
ParseVariableDeclarations(kForStatement, &decl_count, &is_lexical, ParseVariableDeclarations(kForStatement, &decl_count, &is_lexical,
&is_binding_pattern, &first_initializer_loc, &is_binding_pattern, &first_initializer_loc,
&bindings_loc, CHECK_OK); &bindings_loc, CHECK_OK);
if (is_lexical) has_lexical = true;
if (CheckInOrOf(&mode, ok)) { if (CheckInOrOf(&mode, ok)) {
if (!*ok) return Statement::Default(); if (!*ok) return Statement::Default();
if (decl_count != 1) { if (decl_count != 1) {
...@@ -847,7 +863,11 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) { ...@@ -847,7 +863,11 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
} }
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
ParseScopedStatement(true, CHECK_OK); Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
{
BlockState block_state(&scope_, body_scope);
ParseScopedStatement(true, CHECK_OK);
}
return Statement::Default(); return Statement::Default();
} }
} }
...@@ -856,17 +876,26 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) { ...@@ -856,17 +876,26 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
// Parsed initializer at this point. // Parsed initializer at this point.
Expect(Token::SEMICOLON, CHECK_OK); Expect(Token::SEMICOLON, CHECK_OK);
if (peek() != Token::SEMICOLON) { // If there are let bindings, then condition and the next statement of the
ParseExpression(true, CHECK_OK); // for loop must be parsed in a new scope.
} Scope* inner_scope = scope_;
Expect(Token::SEMICOLON, CHECK_OK); if (has_lexical) inner_scope = NewScope(for_scope, BLOCK_SCOPE);
if (peek() != Token::RPAREN) { {
ParseExpression(true, CHECK_OK); BlockState block_state(&scope_, inner_scope);
}
Expect(Token::RPAREN, CHECK_OK);
ParseScopedStatement(true, ok); if (peek() != Token::SEMICOLON) {
ParseExpression(true, CHECK_OK);
}
Expect(Token::SEMICOLON, CHECK_OK);
if (peek() != Token::RPAREN) {
ParseExpression(true, CHECK_OK);
}
Expect(Token::RPAREN, CHECK_OK);
ParseScopedStatement(true, ok);
}
return Statement::Default(); return Statement::Default();
} }
...@@ -912,15 +941,18 @@ PreParser::Statement PreParser::ParseTryStatement(bool* ok) { ...@@ -912,15 +941,18 @@ PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
if (tok == Token::CATCH) { if (tok == Token::CATCH) {
Consume(Token::CATCH); Consume(Token::CATCH);
Expect(Token::LPAREN, CHECK_OK); Expect(Token::LPAREN, CHECK_OK);
Scope* catch_scope = NewScope(scope_, CATCH_SCOPE);
ExpressionClassifier pattern_classifier(this); ExpressionClassifier pattern_classifier(this);
ParsePrimaryExpression(&pattern_classifier, CHECK_OK); ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
ValidateBindingPattern(&pattern_classifier, CHECK_OK); ValidateBindingPattern(&pattern_classifier, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
{ {
// TODO(adamk): Make this CATCH_SCOPE BlockState block_state(&scope_, catch_scope);
Scope* with_scope = NewScope(scope_, WITH_SCOPE); Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
BlockState block_state(&scope_, with_scope); {
ParseBlock(CHECK_OK); BlockState block_state(&scope_, block_scope);
ParseBlock(CHECK_OK);
}
} }
tok = peek(); tok = peek();
} }
...@@ -1114,15 +1146,11 @@ PreParserExpression PreParser::ParseDoExpression(bool* ok) { ...@@ -1114,15 +1146,11 @@ PreParserExpression PreParser::ParseDoExpression(bool* ok) {
// do '{' StatementList '}' // do '{' StatementList '}'
Expect(Token::DO, CHECK_OK); Expect(Token::DO, CHECK_OK);
Expect(Token::LBRACE, CHECK_OK); Expect(Token::LBRACE, CHECK_OK);
Scope* block_scope = NewScope(scope_, BLOCK_SCOPE); while (peek() != Token::RBRACE) {
{ ParseStatementListItem(CHECK_OK);
BlockState block_state(&scope_, block_scope);
while (peek() != Token::RBRACE) {
ParseStatementListItem(CHECK_OK);
}
Expect(Token::RBRACE, CHECK_OK);
return PreParserExpression::Default();
} }
Expect(Token::RBRACE, CHECK_OK);
return PreParserExpression::Default();
} }
#undef CHECK_OK #undef CHECK_OK
......
...@@ -1149,8 +1149,14 @@ PreParserStatementList PreParser::ParseEagerFunctionBody( ...@@ -1149,8 +1149,14 @@ PreParserStatementList PreParser::ParseEagerFunctionBody(
FunctionLiteral::FunctionType function_type, bool* ok) { FunctionLiteral::FunctionType function_type, bool* ok) {
ParsingModeScope parsing_mode(this, PARSE_EAGERLY); ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
ParseStatementList(Token::RBRACE, ok); Scope* inner_scope = scope_;
if (!*ok) return PreParserStatementList(); if (!parameters.is_simple) inner_scope = NewScope(scope_, BLOCK_SCOPE);
{
BlockState block_state(&scope_, inner_scope);
ParseStatementList(Token::RBRACE, ok);
if (!*ok) return PreParserStatementList();
}
Expect(Token::RBRACE, ok); Expect(Token::RBRACE, ok);
return PreParserStatementList(); return PreParserStatementList();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment