OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_PARSING_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/scopes.h" | 8 #include "src/ast/scopes.h" |
9 #include "src/bailout-reason.h" | 9 #include "src/bailout-reason.h" |
10 #include "src/hashmap.h" | 10 #include "src/hashmap.h" |
(...skipping 948 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 ParserBase<Traits>::FunctionState::~FunctionState() { | 959 ParserBase<Traits>::FunctionState::~FunctionState() { |
960 *scope_stack_ = outer_scope_; | 960 *scope_stack_ = outer_scope_; |
961 *function_state_stack_ = outer_function_state_; | 961 *function_state_stack_ = outer_function_state_; |
962 } | 962 } |
963 | 963 |
964 | 964 |
965 template <class Traits> | 965 template <class Traits> |
966 void ParserBase<Traits>::GetUnexpectedTokenMessage( | 966 void ParserBase<Traits>::GetUnexpectedTokenMessage( |
967 Token::Value token, MessageTemplate::Template* message, const char** arg, | 967 Token::Value token, MessageTemplate::Template* message, const char** arg, |
968 MessageTemplate::Template default_) { | 968 MessageTemplate::Template default_) { |
969 // Four of the tokens are treated specially | |
970 switch (token) { | 969 switch (token) { |
971 case Token::EOS: | 970 case Token::EOS: |
972 *message = MessageTemplate::kUnexpectedEOS; | 971 *message = MessageTemplate::kUnexpectedEOS; |
973 *arg = nullptr; | 972 *arg = nullptr; |
974 break; | 973 break; |
975 case Token::SMI: | 974 case Token::SMI: |
976 case Token::NUMBER: | 975 case Token::NUMBER: |
977 *message = MessageTemplate::kUnexpectedTokenNumber; | 976 *message = MessageTemplate::kUnexpectedTokenNumber; |
978 *arg = nullptr; | 977 *arg = nullptr; |
979 break; | 978 break; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 if (is_strong(language_mode())) { | 1082 if (is_strong(language_mode())) { |
1084 classifier->RecordExpressionError(scanner()->location(), | 1083 classifier->RecordExpressionError(scanner()->location(), |
1085 MessageTemplate::kStrongArguments); | 1084 MessageTemplate::kStrongArguments); |
1086 } | 1085 } |
1087 } | 1086 } |
1088 if (this->IsUndefined(name)) { | 1087 if (this->IsUndefined(name)) { |
1089 classifier->RecordStrongModeFormalParameterError( | 1088 classifier->RecordStrongModeFormalParameterError( |
1090 scanner()->location(), MessageTemplate::kStrongUndefined); | 1089 scanner()->location(), MessageTemplate::kStrongUndefined); |
1091 if (is_strong(language_mode())) { | 1090 if (is_strong(language_mode())) { |
1092 // TODO(dslomov): allow 'undefined' in nested patterns. | 1091 // TODO(dslomov): allow 'undefined' in nested patterns. |
1093 classifier->RecordBindingPatternError( | 1092 classifier->RecordPatternError(scanner()->location(), |
1094 scanner()->location(), MessageTemplate::kStrongUndefined); | 1093 MessageTemplate::kStrongUndefined); |
1095 classifier->RecordAssignmentPatternError( | |
1096 scanner()->location(), MessageTemplate::kStrongUndefined); | |
1097 } | 1094 } |
1098 } | 1095 } |
1099 | 1096 |
1100 if (classifier->duplicate_finder() != nullptr && | 1097 if (classifier->duplicate_finder() != nullptr && |
1101 scanner()->FindSymbol(classifier->duplicate_finder(), 1) != 0) { | 1098 scanner()->FindSymbol(classifier->duplicate_finder(), 1) != 0) { |
1102 classifier->RecordDuplicateFormalParameterError(scanner()->location()); | 1099 classifier->RecordDuplicateFormalParameterError(scanner()->location()); |
1103 } | 1100 } |
1104 return name; | 1101 return name; |
1105 } else if (is_sloppy(language_mode()) && | 1102 } else if (is_sloppy(language_mode()) && |
1106 (next == Token::FUTURE_STRICT_RESERVED_WORD || | 1103 (next == Token::FUTURE_STRICT_RESERVED_WORD || |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1251 return this->ThisExpression(scope_, factory(), beg_pos); | 1248 return this->ThisExpression(scope_, factory(), beg_pos); |
1252 } | 1249 } |
1253 | 1250 |
1254 case Token::NULL_LITERAL: | 1251 case Token::NULL_LITERAL: |
1255 case Token::TRUE_LITERAL: | 1252 case Token::TRUE_LITERAL: |
1256 case Token::FALSE_LITERAL: | 1253 case Token::FALSE_LITERAL: |
1257 BindingPatternUnexpectedToken(classifier); | 1254 BindingPatternUnexpectedToken(classifier); |
1258 return this->ExpressionFromLiteral(Next(), beg_pos, scanner(), factory()); | 1255 return this->ExpressionFromLiteral(Next(), beg_pos, scanner(), factory()); |
1259 case Token::SMI: | 1256 case Token::SMI: |
1260 case Token::NUMBER: | 1257 case Token::NUMBER: |
1261 classifier->RecordBindingPatternError( | 1258 BindingPatternUnexpectedToken(classifier); |
1262 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenNumber); | |
1263 return this->ExpressionFromLiteral(Next(), beg_pos, scanner(), factory()); | 1259 return this->ExpressionFromLiteral(Next(), beg_pos, scanner(), factory()); |
1264 | 1260 |
1265 case Token::IDENTIFIER: | 1261 case Token::IDENTIFIER: |
1266 case Token::LET: | 1262 case Token::LET: |
1267 case Token::STATIC: | 1263 case Token::STATIC: |
1268 case Token::YIELD: | 1264 case Token::YIELD: |
1269 case Token::ESCAPED_STRICT_RESERVED_WORD: | 1265 case Token::ESCAPED_STRICT_RESERVED_WORD: |
1270 case Token::FUTURE_STRICT_RESERVED_WORD: { | 1266 case Token::FUTURE_STRICT_RESERVED_WORD: { |
1271 // Using eval or arguments in this context is OK even in strict mode. | 1267 // Using eval or arguments in this context is OK even in strict mode. |
1272 IdentifierT name = ParseAndClassifyIdentifier(classifier, CHECK_OK); | 1268 IdentifierT name = ParseAndClassifyIdentifier(classifier, CHECK_OK); |
1273 return this->ExpressionFromIdentifier( | 1269 return this->ExpressionFromIdentifier( |
1274 name, beg_pos, scanner()->location().end_pos, scope_, factory()); | 1270 name, beg_pos, scanner()->location().end_pos, scope_, factory()); |
1275 } | 1271 } |
1276 | 1272 |
1277 case Token::STRING: { | 1273 case Token::STRING: { |
1278 classifier->RecordBindingPatternError( | 1274 BindingPatternUnexpectedToken(classifier); |
1279 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenString); | |
1280 Consume(Token::STRING); | 1275 Consume(Token::STRING); |
1281 return this->ExpressionFromString(beg_pos, scanner(), factory()); | 1276 return this->ExpressionFromString(beg_pos, scanner(), factory()); |
1282 } | 1277 } |
1283 | 1278 |
1284 case Token::ASSIGN_DIV: | 1279 case Token::ASSIGN_DIV: |
1285 classifier->RecordBindingPatternError( | 1280 classifier->RecordBindingPatternError( |
1286 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenRegExp); | 1281 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenRegExp); |
1287 return this->ParseRegExpLiteral(true, classifier, ok); | 1282 return this->ParseRegExpLiteral(true, classifier, ok); |
1288 | 1283 |
1289 case Token::DIV: | 1284 case Token::DIV: |
(...skipping 25 matching lines...) Expand all Loading... |
1315 classifier->RecordPatternError(scanner()->peek_location(), | 1310 classifier->RecordPatternError(scanner()->peek_location(), |
1316 MessageTemplate::kUnexpectedToken, | 1311 MessageTemplate::kUnexpectedToken, |
1317 Token::String(Token::LPAREN)); | 1312 Token::String(Token::LPAREN)); |
1318 Consume(Token::LPAREN); | 1313 Consume(Token::LPAREN); |
1319 if (Check(Token::RPAREN)) { | 1314 if (Check(Token::RPAREN)) { |
1320 // ()=>x. The continuation that looks for the => is in | 1315 // ()=>x. The continuation that looks for the => is in |
1321 // ParseAssignmentExpression. | 1316 // ParseAssignmentExpression. |
1322 classifier->RecordExpressionError(scanner()->location(), | 1317 classifier->RecordExpressionError(scanner()->location(), |
1323 MessageTemplate::kUnexpectedToken, | 1318 MessageTemplate::kUnexpectedToken, |
1324 Token::String(Token::RPAREN)); | 1319 Token::String(Token::RPAREN)); |
1325 classifier->RecordBindingPatternError(scanner()->location(), | |
1326 MessageTemplate::kUnexpectedToken, | |
1327 Token::String(Token::RPAREN)); | |
1328 return factory()->NewEmptyParentheses(beg_pos); | 1320 return factory()->NewEmptyParentheses(beg_pos); |
1329 } else if (Check(Token::ELLIPSIS)) { | 1321 } else if (Check(Token::ELLIPSIS)) { |
1330 // (...x)=>x. The continuation that looks for the => is in | 1322 // (...x)=>x. The continuation that looks for the => is in |
1331 // ParseAssignmentExpression. | 1323 // ParseAssignmentExpression. |
1332 int ellipsis_pos = position(); | 1324 int ellipsis_pos = position(); |
1333 int expr_pos = peek_position(); | 1325 int expr_pos = peek_position(); |
1334 classifier->RecordExpressionError(scanner()->location(), | 1326 classifier->RecordExpressionError(scanner()->location(), |
1335 MessageTemplate::kUnexpectedToken, | 1327 MessageTemplate::kUnexpectedToken, |
1336 Token::String(Token::ELLIPSIS)); | 1328 Token::String(Token::ELLIPSIS)); |
1337 classifier->RecordNonSimpleParameter(); | 1329 classifier->RecordNonSimpleParameter(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1378 CHECK_OK); | 1370 CHECK_OK); |
1379 class_name_location = scanner()->location(); | 1371 class_name_location = scanner()->location(); |
1380 } | 1372 } |
1381 return this->ParseClassLiteral(name, class_name_location, | 1373 return this->ParseClassLiteral(name, class_name_location, |
1382 is_strict_reserved_name, | 1374 is_strict_reserved_name, |
1383 class_token_position, ok); | 1375 class_token_position, ok); |
1384 } | 1376 } |
1385 | 1377 |
1386 case Token::TEMPLATE_SPAN: | 1378 case Token::TEMPLATE_SPAN: |
1387 case Token::TEMPLATE_TAIL: | 1379 case Token::TEMPLATE_TAIL: |
1388 classifier->RecordBindingPatternError( | 1380 BindingPatternUnexpectedToken(classifier); |
1389 scanner()->peek_location(), | |
1390 MessageTemplate::kUnexpectedTemplateString); | |
1391 return this->ParseTemplateLiteral(Traits::NoTemplateTag(), beg_pos, | 1381 return this->ParseTemplateLiteral(Traits::NoTemplateTag(), beg_pos, |
1392 classifier, ok); | 1382 classifier, ok); |
1393 | 1383 |
1394 case Token::MOD: | 1384 case Token::MOD: |
1395 if (allow_natives() || extension_ != NULL) { | 1385 if (allow_natives() || extension_ != NULL) { |
1396 BindingPatternUnexpectedToken(classifier); | 1386 BindingPatternUnexpectedToken(classifier); |
1397 return this->ParseV8Intrinsic(ok); | 1387 return this->ParseV8Intrinsic(ok); |
1398 } | 1388 } |
1399 break; | 1389 break; |
1400 | 1390 |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1965 FuncNameInferrer::State fni_state(fni_); | 1955 FuncNameInferrer::State fni_state(fni_); |
1966 ParserBase<Traits>::Checkpoint checkpoint(this); | 1956 ParserBase<Traits>::Checkpoint checkpoint(this); |
1967 ExpressionClassifier arrow_formals_classifier(classifier->duplicate_finder()); | 1957 ExpressionClassifier arrow_formals_classifier(classifier->duplicate_finder()); |
1968 bool parenthesized_formals = peek() == Token::LPAREN; | 1958 bool parenthesized_formals = peek() == Token::LPAREN; |
1969 if (!parenthesized_formals) { | 1959 if (!parenthesized_formals) { |
1970 ArrowFormalParametersUnexpectedToken(&arrow_formals_classifier); | 1960 ArrowFormalParametersUnexpectedToken(&arrow_formals_classifier); |
1971 } | 1961 } |
1972 ExpressionT expression = this->ParseConditionalExpression( | 1962 ExpressionT expression = this->ParseConditionalExpression( |
1973 accept_IN, &arrow_formals_classifier, CHECK_OK); | 1963 accept_IN, &arrow_formals_classifier, CHECK_OK); |
1974 if (peek() == Token::ARROW) { | 1964 if (peek() == Token::ARROW) { |
1975 BindingPatternUnexpectedToken(classifier); | 1965 classifier->RecordPatternError(scanner()->peek_location(), |
| 1966 MessageTemplate::kUnexpectedToken, |
| 1967 Token::String(Token::ARROW)); |
1976 ValidateArrowFormalParameters(&arrow_formals_classifier, expression, | 1968 ValidateArrowFormalParameters(&arrow_formals_classifier, expression, |
1977 parenthesized_formals, CHECK_OK); | 1969 parenthesized_formals, CHECK_OK); |
1978 Scanner::Location loc(lhs_beg_pos, scanner()->location().end_pos); | 1970 Scanner::Location loc(lhs_beg_pos, scanner()->location().end_pos); |
1979 Scope* scope = | 1971 Scope* scope = |
1980 this->NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction); | 1972 this->NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction); |
1981 // Because the arrow's parameters were parsed in the outer scope, any | 1973 // Because the arrow's parameters were parsed in the outer scope, any |
1982 // usage flags that might have been triggered there need to be copied | 1974 // usage flags that might have been triggered there need to be copied |
1983 // to the arrow scope. | 1975 // to the arrow scope. |
1984 scope_->PropagateUsageFlagsToScope(scope); | 1976 scope_->PropagateUsageFlagsToScope(scope); |
1985 FormalParametersT parameters(scope); | 1977 FormalParametersT parameters(scope); |
1986 if (!arrow_formals_classifier.is_simple_parameter_list()) { | 1978 if (!arrow_formals_classifier.is_simple_parameter_list()) { |
1987 scope->SetHasNonSimpleParameters(); | 1979 scope->SetHasNonSimpleParameters(); |
1988 parameters.is_simple = false; | 1980 parameters.is_simple = false; |
1989 } | 1981 } |
1990 | 1982 |
1991 checkpoint.Restore(¶meters.materialized_literals_count); | 1983 checkpoint.Restore(¶meters.materialized_literals_count); |
1992 | 1984 |
1993 scope->set_start_position(lhs_beg_pos); | 1985 scope->set_start_position(lhs_beg_pos); |
1994 Scanner::Location duplicate_loc = Scanner::Location::invalid(); | 1986 Scanner::Location duplicate_loc = Scanner::Location::invalid(); |
1995 this->ParseArrowFunctionFormalParameterList(¶meters, expression, loc, | 1987 this->ParseArrowFunctionFormalParameterList(¶meters, expression, loc, |
1996 &duplicate_loc, CHECK_OK); | 1988 &duplicate_loc, CHECK_OK); |
1997 if (duplicate_loc.IsValid()) { | 1989 if (duplicate_loc.IsValid()) { |
1998 arrow_formals_classifier.RecordDuplicateFormalParameterError( | 1990 arrow_formals_classifier.RecordDuplicateFormalParameterError( |
1999 duplicate_loc); | 1991 duplicate_loc); |
2000 } | 1992 } |
2001 expression = this->ParseArrowFunctionLiteral( | 1993 expression = this->ParseArrowFunctionLiteral( |
2002 accept_IN, parameters, arrow_formals_classifier, CHECK_OK); | 1994 accept_IN, parameters, arrow_formals_classifier, CHECK_OK); |
2003 if (maybe_pattern_element) { | |
2004 classifier->RecordPatternError( | |
2005 Scanner::Location(lhs_beg_pos, scanner()->location().end_pos), | |
2006 MessageTemplate::kInvalidDestructuringTarget); | |
2007 } | |
2008 | 1995 |
2009 if (fni_ != nullptr) fni_->Infer(); | 1996 if (fni_ != nullptr) fni_->Infer(); |
2010 | 1997 |
2011 return expression; | 1998 return expression; |
2012 } | 1999 } |
2013 | 2000 |
2014 if (this->IsValidReferenceExpression(expression)) { | 2001 if (this->IsValidReferenceExpression(expression)) { |
2015 arrow_formals_classifier.ForgiveAssignmentPatternError(); | 2002 arrow_formals_classifier.ForgiveAssignmentPatternError(); |
2016 } | 2003 } |
2017 | 2004 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2056 } | 2043 } |
2057 expression = this->CheckAndRewriteReferenceExpression( | 2044 expression = this->CheckAndRewriteReferenceExpression( |
2058 expression, lhs_beg_pos, scanner()->location().end_pos, | 2045 expression, lhs_beg_pos, scanner()->location().end_pos, |
2059 MessageTemplate::kInvalidLhsInAssignment, CHECK_OK); | 2046 MessageTemplate::kInvalidLhsInAssignment, CHECK_OK); |
2060 } | 2047 } |
2061 | 2048 |
2062 expression = this->MarkExpressionAsAssigned(expression); | 2049 expression = this->MarkExpressionAsAssigned(expression); |
2063 | 2050 |
2064 Token::Value op = Next(); // Get assignment operator. | 2051 Token::Value op = Next(); // Get assignment operator. |
2065 if (op != Token::ASSIGN) { | 2052 if (op != Token::ASSIGN) { |
2066 classifier->RecordBindingPatternError(scanner()->location(), | 2053 classifier->RecordPatternError(scanner()->location(), |
2067 MessageTemplate::kUnexpectedToken, | 2054 MessageTemplate::kUnexpectedToken, |
2068 Token::String(op)); | 2055 Token::String(op)); |
2069 } | 2056 } |
2070 int pos = position(); | 2057 int pos = position(); |
2071 | 2058 |
2072 ExpressionClassifier rhs_classifier; | 2059 ExpressionClassifier rhs_classifier; |
2073 | 2060 |
2074 ExpressionT right = | 2061 ExpressionT right = |
2075 this->ParseAssignmentExpression(accept_IN, &rhs_classifier, CHECK_OK); | 2062 this->ParseAssignmentExpression(accept_IN, &rhs_classifier, CHECK_OK); |
2076 right = Traits::RewriteNonPattern(right, &rhs_classifier, CHECK_OK); | 2063 right = Traits::RewriteNonPattern(right, &rhs_classifier, CHECK_OK); |
2077 classifier->Accumulate( | 2064 classifier->Accumulate( |
2078 rhs_classifier, ExpressionClassifier::ExpressionProductions | | 2065 rhs_classifier, ExpressionClassifier::ExpressionProductions | |
2079 ExpressionClassifier::CoverInitializedNameProduction); | 2066 ExpressionClassifier::CoverInitializedNameProduction); |
2080 | 2067 |
2081 // TODO(1231235): We try to estimate the set of properties set by | 2068 // TODO(1231235): We try to estimate the set of properties set by |
2082 // constructors. We define a new property whenever there is an | 2069 // constructors. We define a new property whenever there is an |
2083 // assignment to a property of 'this'. We should probably only add | 2070 // assignment to a property of 'this'. We should probably only add |
2084 // properties if we haven't seen them before. Otherwise we'll | 2071 // properties if we haven't seen them before. Otherwise we'll |
2085 // probably overestimate the number of properties. | 2072 // probably overestimate the number of properties. |
2086 if (op == Token::ASSIGN && this->IsThisProperty(expression)) { | 2073 if (op == Token::ASSIGN && this->IsThisProperty(expression)) { |
2087 function_state_->AddProperty(); | 2074 function_state_->AddProperty(); |
2088 } | 2075 } |
2089 | 2076 |
2090 if (op != Token::ASSIGN && maybe_pattern_element) { | |
2091 classifier->RecordAssignmentPatternError( | |
2092 Scanner::Location(lhs_beg_pos, scanner()->location().end_pos), | |
2093 MessageTemplate::kInvalidDestructuringTarget); | |
2094 } | |
2095 | |
2096 this->CheckAssigningFunctionLiteralToProperty(expression, right); | 2077 this->CheckAssigningFunctionLiteralToProperty(expression, right); |
2097 | 2078 |
2098 if (fni_ != NULL) { | 2079 if (fni_ != NULL) { |
2099 // Check if the right hand side is a call to avoid inferring a | 2080 // Check if the right hand side is a call to avoid inferring a |
2100 // name if we're dealing with "a = function(){...}();"-like | 2081 // name if we're dealing with "a = function(){...}();"-like |
2101 // expression. | 2082 // expression. |
2102 if ((op == Token::INIT || op == Token::ASSIGN) && | 2083 if ((op == Token::INIT || op == Token::ASSIGN) && |
2103 (!right->IsCall() && !right->IsCallNew())) { | 2084 (!right->IsCall() && !right->IsCallNew())) { |
2104 fni_->Infer(); | 2085 fni_->Infer(); |
2105 } else { | 2086 } else { |
(...skipping 1268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3374 return; | 3355 return; |
3375 } | 3356 } |
3376 has_seen_constructor_ = true; | 3357 has_seen_constructor_ = true; |
3377 return; | 3358 return; |
3378 } | 3359 } |
3379 } | 3360 } |
3380 } // namespace internal | 3361 } // namespace internal |
3381 } // namespace v8 | 3362 } // namespace v8 |
3382 | 3363 |
3383 #endif // V8_PARSING_PARSER_BASE_H | 3364 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |