| 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_PREPARSER_H | 5 #ifndef V8_PREPARSER_H |
| 6 #define V8_PREPARSER_H | 6 #define V8_PREPARSER_H |
| 7 | 7 |
| 8 #include "src/v8.h" | 8 #include "src/v8.h" |
| 9 | 9 |
| 10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 } | 345 } |
| 346 ~ParsingModeScope() { | 346 ~ParsingModeScope() { |
| 347 parser_->mode_ = old_mode_; | 347 parser_->mode_ = old_mode_; |
| 348 } | 348 } |
| 349 | 349 |
| 350 private: | 350 private: |
| 351 ParserBase* parser_; | 351 ParserBase* parser_; |
| 352 Mode old_mode_; | 352 Mode old_mode_; |
| 353 }; | 353 }; |
| 354 | 354 |
| 355 Scope* NewScope(Scope* parent, ScopeType scope_type, | 355 Scope* NewScope(Scope* parent, ScopeType scope_type) { |
| 356 FunctionKind kind = kNormalFunction) { | 356 // Must always pass the function kind for FUNCTION_SCOPE and ARROW_SCOPE. |
| 357 DCHECK(scope_type != FUNCTION_SCOPE && scope_type != ARROW_SCOPE); |
| 358 return NewScope(parent, scope_type, kNormalFunction); |
| 359 } |
| 360 |
| 361 Scope* NewScope(Scope* parent, ScopeType scope_type, FunctionKind kind) { |
| 357 DCHECK(ast_value_factory()); | 362 DCHECK(ast_value_factory()); |
| 358 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules()); | 363 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules()); |
| 359 DCHECK((scope_type == FUNCTION_SCOPE && IsValidFunctionKind(kind)) || | 364 DCHECK(scope_type != ARROW_SCOPE || IsArrowFunction(kind)); |
| 360 kind == kNormalFunction); | |
| 361 Scope* result = new (zone()) | 365 Scope* result = new (zone()) |
| 362 Scope(zone(), parent, scope_type, ast_value_factory(), kind); | 366 Scope(zone(), parent, scope_type, ast_value_factory(), kind); |
| 363 result->Initialize(); | 367 result->Initialize(); |
| 364 return result; | 368 return result; |
| 365 } | 369 } |
| 366 | 370 |
| 367 Scanner* scanner() const { return scanner_; } | 371 Scanner* scanner() const { return scanner_; } |
| 368 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } | 372 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } |
| 369 int position() { return scanner_->location().beg_pos; } | 373 int position() { return scanner_->location().beg_pos; } |
| 370 int peek_position() { return scanner_->peek_location().beg_pos; } | 374 int peek_position() { return scanner_->peek_location().beg_pos; } |
| (...skipping 1264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1635 return PreParserIdentifier::Default(); | 1639 return PreParserIdentifier::Default(); |
| 1636 } | 1640 } |
| 1637 | 1641 |
| 1638 static PreParserExpression ThisExpression(Scope* scope, | 1642 static PreParserExpression ThisExpression(Scope* scope, |
| 1639 PreParserFactory* factory, | 1643 PreParserFactory* factory, |
| 1640 int pos) { | 1644 int pos) { |
| 1641 return PreParserExpression::This(); | 1645 return PreParserExpression::This(); |
| 1642 } | 1646 } |
| 1643 | 1647 |
| 1644 static PreParserExpression SuperReference(Scope* scope, | 1648 static PreParserExpression SuperReference(Scope* scope, |
| 1645 PreParserFactory* factory) { | 1649 PreParserFactory* factory, |
| 1650 int pos) { |
| 1646 return PreParserExpression::Default(); | 1651 return PreParserExpression::Default(); |
| 1647 } | 1652 } |
| 1648 | 1653 |
| 1649 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope, | 1654 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope, |
| 1650 int pos, int end_pos) { | 1655 int pos, int end_pos) { |
| 1651 return PreParserExpression::Default(); | 1656 return PreParserExpression::Default(); |
| 1652 } | 1657 } |
| 1653 | 1658 |
| 1654 static PreParserExpression ExpressionFromLiteral( | 1659 static PreParserExpression ExpressionFromLiteral( |
| 1655 Token::Value token, int pos, Scanner* scanner, | 1660 Token::Value token, int pos, Scanner* scanner, |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2275 | 2280 |
| 2276 case Token::LBRACE: | 2281 case Token::LBRACE: |
| 2277 result = this->ParseObjectLiteral(classifier, CHECK_OK); | 2282 result = this->ParseObjectLiteral(classifier, CHECK_OK); |
| 2278 break; | 2283 break; |
| 2279 | 2284 |
| 2280 case Token::LPAREN: | 2285 case Token::LPAREN: |
| 2281 BindingPatternUnexpectedToken(classifier); | 2286 BindingPatternUnexpectedToken(classifier); |
| 2282 Consume(Token::LPAREN); | 2287 Consume(Token::LPAREN); |
| 2283 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { | 2288 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { |
| 2284 // As a primary expression, the only thing that can follow "()" is "=>". | 2289 // As a primary expression, the only thing that can follow "()" is "=>". |
| 2285 Scope* scope = this->NewScope(scope_, ARROW_SCOPE); | 2290 Scope* scope = |
| 2291 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); |
| 2286 scope->set_start_position(beg_pos); | 2292 scope->set_start_position(beg_pos); |
| 2287 FormalParameterErrorLocations error_locs; | 2293 FormalParameterErrorLocations error_locs; |
| 2288 bool has_rest = false; | 2294 bool has_rest = false; |
| 2289 result = this->ParseArrowFunctionLiteral(scope, error_locs, has_rest, | 2295 result = this->ParseArrowFunctionLiteral(scope, error_locs, has_rest, |
| 2290 classifier, CHECK_OK); | 2296 classifier, CHECK_OK); |
| 2291 } else { | 2297 } else { |
| 2292 // Heuristically try to detect immediately called functions before | 2298 // Heuristically try to detect immediately called functions before |
| 2293 // seeing the call parentheses. | 2299 // seeing the call parentheses. |
| 2294 parenthesized_function_ = (peek() == Token::FUNCTION); | 2300 parenthesized_function_ = (peek() == Token::FUNCTION); |
| 2295 result = this->ParseExpression(true, classifier, CHECK_OK); | 2301 result = this->ParseExpression(true, classifier, CHECK_OK); |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2757 if (fni_ != NULL) fni_->Enter(); | 2763 if (fni_ != NULL) fni_->Enter(); |
| 2758 ParserBase<Traits>::Checkpoint checkpoint(this); | 2764 ParserBase<Traits>::Checkpoint checkpoint(this); |
| 2759 ExpressionT expression = | 2765 ExpressionT expression = |
| 2760 this->ParseConditionalExpression(accept_IN, classifier, CHECK_OK); | 2766 this->ParseConditionalExpression(accept_IN, classifier, CHECK_OK); |
| 2761 | 2767 |
| 2762 if (allow_harmony_arrow_functions() && peek() == Token::ARROW) { | 2768 if (allow_harmony_arrow_functions() && peek() == Token::ARROW) { |
| 2763 checkpoint.Restore(); | 2769 checkpoint.Restore(); |
| 2764 FormalParameterErrorLocations error_locs; | 2770 FormalParameterErrorLocations error_locs; |
| 2765 Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos); | 2771 Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos); |
| 2766 bool has_rest = false; | 2772 bool has_rest = false; |
| 2767 Scope* scope = this->NewScope(scope_, ARROW_SCOPE); | 2773 Scope* scope = |
| 2774 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); |
| 2768 scope->set_start_position(lhs_location.beg_pos); | 2775 scope->set_start_position(lhs_location.beg_pos); |
| 2769 this->ParseArrowFunctionFormalParameters(scope, expression, loc, | 2776 this->ParseArrowFunctionFormalParameters(scope, expression, loc, |
| 2770 &error_locs, &has_rest, CHECK_OK); | 2777 &error_locs, &has_rest, CHECK_OK); |
| 2771 expression = this->ParseArrowFunctionLiteral(scope, error_locs, has_rest, | 2778 expression = this->ParseArrowFunctionLiteral(scope, error_locs, has_rest, |
| 2772 classifier, CHECK_OK); | 2779 classifier, CHECK_OK); |
| 2773 return expression; | 2780 return expression; |
| 2774 } | 2781 } |
| 2775 | 2782 |
| 2776 if (!Token::IsAssignmentOp(peek())) { | 2783 if (!Token::IsAssignmentOp(peek())) { |
| 2777 if (fni_ != NULL) fni_->Leave(); | 2784 if (fni_ != NULL) fni_->Leave(); |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3317 typename ParserBase<Traits>::ExpressionT | 3324 typename ParserBase<Traits>::ExpressionT |
| 3318 ParserBase<Traits>::ParseStrongSuperCallExpression( | 3325 ParserBase<Traits>::ParseStrongSuperCallExpression( |
| 3319 ExpressionClassifier* classifier, bool* ok) { | 3326 ExpressionClassifier* classifier, bool* ok) { |
| 3320 // SuperCallExpression :: (strong mode) | 3327 // SuperCallExpression :: (strong mode) |
| 3321 // 'super' '(' ExpressionList ')' | 3328 // 'super' '(' ExpressionList ')' |
| 3322 BindingPatternUnexpectedToken(classifier); | 3329 BindingPatternUnexpectedToken(classifier); |
| 3323 | 3330 |
| 3324 Consume(Token::SUPER); | 3331 Consume(Token::SUPER); |
| 3325 int pos = position(); | 3332 int pos = position(); |
| 3326 Scanner::Location super_loc = scanner()->location(); | 3333 Scanner::Location super_loc = scanner()->location(); |
| 3327 ExpressionT expr = this->SuperReference(scope_, factory()); | 3334 ExpressionT expr = this->SuperReference(scope_, factory(), pos); |
| 3328 | 3335 |
| 3329 if (peek() != Token::LPAREN) { | 3336 if (peek() != Token::LPAREN) { |
| 3330 ReportMessage("strong_constructor_super"); | 3337 ReportMessage("strong_constructor_super"); |
| 3331 *ok = false; | 3338 *ok = false; |
| 3332 return this->EmptyExpression(); | 3339 return this->EmptyExpression(); |
| 3333 } | 3340 } |
| 3334 | 3341 |
| 3335 Scanner::Location spread_pos; | 3342 Scanner::Location spread_pos; |
| 3336 typename Traits::Type::ExpressionList args = | 3343 typename Traits::Type::ExpressionList args = |
| 3337 ParseArguments(&spread_pos, classifier, CHECK_OK); | 3344 ParseArguments(&spread_pos, classifier, CHECK_OK); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 3364 return factory()->NewCall(expr, args, pos); | 3371 return factory()->NewCall(expr, args, pos); |
| 3365 } | 3372 } |
| 3366 } | 3373 } |
| 3367 | 3374 |
| 3368 | 3375 |
| 3369 template <class Traits> | 3376 template <class Traits> |
| 3370 typename ParserBase<Traits>::ExpressionT | 3377 typename ParserBase<Traits>::ExpressionT |
| 3371 ParserBase<Traits>::ParseSuperExpression(bool is_new, | 3378 ParserBase<Traits>::ParseSuperExpression(bool is_new, |
| 3372 ExpressionClassifier* classifier, | 3379 ExpressionClassifier* classifier, |
| 3373 bool* ok) { | 3380 bool* ok) { |
| 3381 int pos = position(); |
| 3374 Expect(Token::SUPER, CHECK_OK); | 3382 Expect(Token::SUPER, CHECK_OK); |
| 3375 | 3383 |
| 3376 // TODO(wingo): Does this actually work with lazily compiled arrows? | 3384 Scope* scope = scope_->DeclarationScope(); |
| 3377 FunctionState* function_state = function_state_; | 3385 |
| 3378 while (IsArrowFunction(function_state->kind())) { | 3386 while (scope->is_eval_scope() || scope->is_arrow_scope()) { |
| 3379 function_state = function_state->outer(); | 3387 scope = scope->outer_scope(); |
| 3388 DCHECK_NOT_NULL(scope); |
| 3389 scope = scope->DeclarationScope(); |
| 3380 } | 3390 } |
| 3381 // TODO(arv): Handle eval scopes similarly. | |
| 3382 | 3391 |
| 3383 FunctionKind kind = function_state->kind(); | 3392 FunctionKind kind = scope->function_kind(); |
| 3384 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || | 3393 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || |
| 3385 i::IsConstructor(kind)) { | 3394 i::IsConstructor(kind)) { |
| 3386 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { | 3395 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { |
| 3387 scope_->RecordSuperPropertyUsage(); | 3396 scope->RecordSuperPropertyUsage(); |
| 3388 return this->SuperReference(scope_, factory()); | 3397 return this->SuperReference(scope_, factory(), pos); |
| 3389 } | 3398 } |
| 3390 // new super() is never allowed. | 3399 // new super() is never allowed. |
| 3391 // super() is only allowed in derived constructor | 3400 // super() is only allowed in derived constructor |
| 3392 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { | 3401 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { |
| 3393 if (is_strong(language_mode())) { | 3402 if (is_strong(language_mode())) { |
| 3394 // Super calls in strong mode are parsed separately. | 3403 // Super calls in strong mode are parsed separately. |
| 3395 ReportMessageAt(scanner()->location(), "strong_constructor_super"); | 3404 ReportMessageAt(scanner()->location(), "strong_constructor_super"); |
| 3396 *ok = false; | 3405 *ok = false; |
| 3397 return this->EmptyExpression(); | 3406 return this->EmptyExpression(); |
| 3398 } | 3407 } |
| 3399 function_state->set_super_location(scanner()->location()); | 3408 // TODO(rossberg): This might not be the correct FunctionState for the |
| 3400 return this->SuperReference(scope_, factory()); | 3409 // method here. |
| 3410 function_state_->set_super_location(scanner()->location()); |
| 3411 return this->SuperReference(scope_, factory(), pos); |
| 3401 } | 3412 } |
| 3402 } | 3413 } |
| 3403 | 3414 |
| 3404 ReportMessageAt(scanner()->location(), "unexpected_super"); | 3415 ReportMessageAt(scanner()->location(), "unexpected_super"); |
| 3405 *ok = false; | 3416 *ok = false; |
| 3406 return this->EmptyExpression(); | 3417 return this->EmptyExpression(); |
| 3407 } | 3418 } |
| 3408 | 3419 |
| 3409 | 3420 |
| 3410 template <class Traits> | 3421 template <class Traits> |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3843 *ok = false; | 3854 *ok = false; |
| 3844 return; | 3855 return; |
| 3845 } | 3856 } |
| 3846 has_seen_constructor_ = true; | 3857 has_seen_constructor_ = true; |
| 3847 return; | 3858 return; |
| 3848 } | 3859 } |
| 3849 } | 3860 } |
| 3850 } } // v8::internal | 3861 } } // v8::internal |
| 3851 | 3862 |
| 3852 #endif // V8_PREPARSER_H | 3863 #endif // V8_PREPARSER_H |
| OLD | NEW |