Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Side by Side Diff: src/preparser.h

Issue 1168393008: Revert of Revert of [es6] Parsing of new.target (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 scanner_(scanner), 90 scanner_(scanner),
91 stack_overflow_(false), 91 stack_overflow_(false),
92 allow_lazy_(false), 92 allow_lazy_(false),
93 allow_natives_(false), 93 allow_natives_(false),
94 allow_harmony_arrow_functions_(false), 94 allow_harmony_arrow_functions_(false),
95 allow_harmony_object_literals_(false), 95 allow_harmony_object_literals_(false),
96 allow_harmony_sloppy_(false), 96 allow_harmony_sloppy_(false),
97 allow_harmony_computed_property_names_(false), 97 allow_harmony_computed_property_names_(false),
98 allow_harmony_rest_params_(false), 98 allow_harmony_rest_params_(false),
99 allow_harmony_spreadcalls_(false), 99 allow_harmony_spreadcalls_(false),
100 allow_harmony_destructuring_(false),
101 allow_harmony_spread_arrays_(false),
102 allow_harmony_new_target_(false),
100 allow_strong_mode_(false) {} 103 allow_strong_mode_(false) {}
101 104
102 // Getters that indicate whether certain syntactical constructs are 105 #define ALLOW_ACCESSORS(name) \
103 // allowed to be parsed by this instance of the parser. 106 bool allow_##name() const { return allow_##name##_; } \
104 bool allow_lazy() const { return allow_lazy_; } 107 void set_allow_##name(bool allow) { allow_##name##_ = allow; }
105 bool allow_natives() const { return allow_natives_; } 108
106 bool allow_harmony_arrow_functions() const { 109 ALLOW_ACCESSORS(lazy);
107 return allow_harmony_arrow_functions_; 110 ALLOW_ACCESSORS(natives);
108 } 111 ALLOW_ACCESSORS(harmony_arrow_functions);
112 ALLOW_ACCESSORS(harmony_object_literals);
113 ALLOW_ACCESSORS(harmony_sloppy);
114 ALLOW_ACCESSORS(harmony_computed_property_names);
115 ALLOW_ACCESSORS(harmony_rest_params);
116 ALLOW_ACCESSORS(harmony_spreadcalls);
117 ALLOW_ACCESSORS(harmony_destructuring);
118 ALLOW_ACCESSORS(harmony_spread_arrays);
119 ALLOW_ACCESSORS(harmony_new_target);
120 ALLOW_ACCESSORS(strong_mode);
121 #undef ALLOW_ACCESSORS
122
109 bool allow_harmony_modules() const { return scanner()->HarmonyModules(); } 123 bool allow_harmony_modules() const { return scanner()->HarmonyModules(); }
110 bool allow_harmony_classes() const { return scanner()->HarmonyClasses(); } 124 bool allow_harmony_classes() const { return scanner()->HarmonyClasses(); }
111 bool allow_harmony_object_literals() const {
112 return allow_harmony_object_literals_;
113 }
114 bool allow_harmony_sloppy() const { return allow_harmony_sloppy_; }
115 bool allow_harmony_unicode() const { return scanner()->HarmonyUnicode(); } 125 bool allow_harmony_unicode() const { return scanner()->HarmonyUnicode(); }
116 bool allow_harmony_computed_property_names() const {
117 return allow_harmony_computed_property_names_;
118 }
119 bool allow_harmony_rest_params() const {
120 return allow_harmony_rest_params_;
121 }
122 bool allow_harmony_spreadcalls() const { return allow_harmony_spreadcalls_; }
123 bool allow_harmony_destructuring() const {
124 return allow_harmony_destructuring_;
125 }
126 bool allow_harmony_spread_arrays() const {
127 return allow_harmony_spread_arrays_;
128 }
129 126
130 bool allow_strong_mode() const { return allow_strong_mode_; } 127 void set_allow_harmony_modules(bool a) { scanner()->SetHarmonyModules(a); }
131 128 void set_allow_harmony_classes(bool a) { scanner()->SetHarmonyClasses(a); }
132 // Setters that determine whether certain syntactical constructs are 129 void set_allow_harmony_unicode(bool a) { scanner()->SetHarmonyUnicode(a); }
133 // allowed to be parsed by this instance of the parser.
134 void set_allow_lazy(bool allow) { allow_lazy_ = allow; }
135 void set_allow_natives(bool allow) { allow_natives_ = allow; }
136 void set_allow_harmony_arrow_functions(bool allow) {
137 allow_harmony_arrow_functions_ = allow;
138 }
139 void set_allow_harmony_modules(bool allow) {
140 scanner()->SetHarmonyModules(allow);
141 }
142 void set_allow_harmony_classes(bool allow) {
143 scanner()->SetHarmonyClasses(allow);
144 }
145 void set_allow_harmony_object_literals(bool allow) {
146 allow_harmony_object_literals_ = allow;
147 }
148 void set_allow_harmony_sloppy(bool allow) {
149 allow_harmony_sloppy_ = allow;
150 }
151 void set_allow_harmony_unicode(bool allow) {
152 scanner()->SetHarmonyUnicode(allow);
153 }
154 void set_allow_harmony_computed_property_names(bool allow) {
155 allow_harmony_computed_property_names_ = allow;
156 }
157 void set_allow_harmony_rest_params(bool allow) {
158 allow_harmony_rest_params_ = allow;
159 }
160 void set_allow_harmony_spreadcalls(bool allow) {
161 allow_harmony_spreadcalls_ = allow;
162 }
163 void set_allow_strong_mode(bool allow) { allow_strong_mode_ = allow; }
164 void set_allow_harmony_destructuring(bool allow) {
165 allow_harmony_destructuring_ = allow;
166 }
167 void set_allow_harmony_spread_arrays(bool allow) {
168 allow_harmony_spread_arrays_ = allow;
169 }
170 130
171 protected: 131 protected:
172 enum AllowRestrictedIdentifiers { 132 enum AllowRestrictedIdentifiers {
173 kAllowRestrictedIdentifiers, 133 kAllowRestrictedIdentifiers,
174 kDontAllowRestrictedIdentifiers 134 kDontAllowRestrictedIdentifiers
175 }; 135 };
176 136
177 enum Mode { 137 enum Mode {
178 PARSE_LAZILY, 138 PARSE_LAZILY,
179 PARSE_EAGERLY 139 PARSE_EAGERLY
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 Scope* NewScope(Scope* parent, ScopeType scope_type) { 302 Scope* NewScope(Scope* parent, ScopeType scope_type) {
343 // Must always pass the function kind for FUNCTION_SCOPE and ARROW_SCOPE. 303 // Must always pass the function kind for FUNCTION_SCOPE and ARROW_SCOPE.
344 DCHECK(scope_type != FUNCTION_SCOPE); 304 DCHECK(scope_type != FUNCTION_SCOPE);
345 DCHECK(scope_type != ARROW_SCOPE); 305 DCHECK(scope_type != ARROW_SCOPE);
346 return NewScope(parent, scope_type, kNormalFunction); 306 return NewScope(parent, scope_type, kNormalFunction);
347 } 307 }
348 308
349 Scope* NewScope(Scope* parent, ScopeType scope_type, FunctionKind kind) { 309 Scope* NewScope(Scope* parent, ScopeType scope_type, FunctionKind kind) {
350 DCHECK(ast_value_factory()); 310 DCHECK(ast_value_factory());
351 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules()); 311 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules());
352 DCHECK(scope_type != ARROW_SCOPE || IsArrowFunction(kind)); 312 DCHECK(!IsArrowFunction(kind) || scope_type == ARROW_SCOPE);
353 Scope* result = new (zone()) 313 Scope* result = new (zone())
354 Scope(zone(), parent, scope_type, ast_value_factory(), kind); 314 Scope(zone(), parent, scope_type, ast_value_factory(), kind);
355 result->Initialize(); 315 result->Initialize();
356 return result; 316 return result;
357 } 317 }
358 318
359 Scanner* scanner() const { return scanner_; } 319 Scanner* scanner() const { return scanner_; }
360 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } 320 AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
361 int position() { return scanner_->location().beg_pos; } 321 int position() { return scanner_->location().beg_pos; }
362 int peek_position() { return scanner_->peek_location().beg_pos; } 322 int peek_position() { return scanner_->peek_location().beg_pos; }
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
905 ExpressionT ParseMemberExpressionContinuation( 865 ExpressionT ParseMemberExpressionContinuation(
906 ExpressionT expression, ExpressionClassifier* classifier, bool* ok); 866 ExpressionT expression, ExpressionClassifier* classifier, bool* ok);
907 ExpressionT ParseArrowFunctionLiteral(Scope* function_scope, bool has_rest, 867 ExpressionT ParseArrowFunctionLiteral(Scope* function_scope, bool has_rest,
908 const ExpressionClassifier& classifier, 868 const ExpressionClassifier& classifier,
909 bool* ok); 869 bool* ok);
910 ExpressionT ParseTemplateLiteral(ExpressionT tag, int start, 870 ExpressionT ParseTemplateLiteral(ExpressionT tag, int start,
911 ExpressionClassifier* classifier, bool* ok); 871 ExpressionClassifier* classifier, bool* ok);
912 void AddTemplateExpression(ExpressionT); 872 void AddTemplateExpression(ExpressionT);
913 ExpressionT ParseSuperExpression(bool is_new, 873 ExpressionT ParseSuperExpression(bool is_new,
914 ExpressionClassifier* classifier, bool* ok); 874 ExpressionClassifier* classifier, bool* ok);
875 ExpressionT ParseNewTargetExpression(bool* ok);
915 ExpressionT ParseStrongInitializationExpression( 876 ExpressionT ParseStrongInitializationExpression(
916 ExpressionClassifier* classifier, bool* ok); 877 ExpressionClassifier* classifier, bool* ok);
917 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier, 878 ExpressionT ParseStrongSuperCallExpression(ExpressionClassifier* classifier,
918 bool* ok); 879 bool* ok);
919 880
920 void ParseFormalParameter(FormalParameterScopeT* scope, bool is_rest, 881 void ParseFormalParameter(FormalParameterScopeT* scope, bool is_rest,
921 ExpressionClassifier* classifier, bool* ok); 882 ExpressionClassifier* classifier, bool* ok);
922 int ParseFormalParameterList(FormalParameterScopeT* scope, bool* has_rest, 883 int ParseFormalParameterList(FormalParameterScopeT* scope, bool* has_rest,
923 ExpressionClassifier* classifier, bool* ok); 884 ExpressionClassifier* classifier, bool* ok);
924 void CheckArityRestrictions( 885 void CheckArityRestrictions(
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 bool allow_lazy_; 976 bool allow_lazy_;
1016 bool allow_natives_; 977 bool allow_natives_;
1017 bool allow_harmony_arrow_functions_; 978 bool allow_harmony_arrow_functions_;
1018 bool allow_harmony_object_literals_; 979 bool allow_harmony_object_literals_;
1019 bool allow_harmony_sloppy_; 980 bool allow_harmony_sloppy_;
1020 bool allow_harmony_computed_property_names_; 981 bool allow_harmony_computed_property_names_;
1021 bool allow_harmony_rest_params_; 982 bool allow_harmony_rest_params_;
1022 bool allow_harmony_spreadcalls_; 983 bool allow_harmony_spreadcalls_;
1023 bool allow_harmony_destructuring_; 984 bool allow_harmony_destructuring_;
1024 bool allow_harmony_spread_arrays_; 985 bool allow_harmony_spread_arrays_;
986 bool allow_harmony_new_target_;
1025 bool allow_strong_mode_; 987 bool allow_strong_mode_;
1026 }; 988 };
1027 989
1028 990
1029 class PreParserIdentifier { 991 class PreParserIdentifier {
1030 public: 992 public:
1031 PreParserIdentifier() : type_(kUnknownIdentifier) {} 993 PreParserIdentifier() : type_(kUnknownIdentifier) {}
1032 static PreParserIdentifier Default() { 994 static PreParserIdentifier Default() {
1033 return PreParserIdentifier(kUnknownIdentifier); 995 return PreParserIdentifier(kUnknownIdentifier);
1034 } 996 }
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after
1724 int pos) { 1686 int pos) {
1725 return PreParserExpression::Default(); 1687 return PreParserExpression::Default();
1726 } 1688 }
1727 1689
1728 static PreParserExpression SuperCallReference(Scope* scope, 1690 static PreParserExpression SuperCallReference(Scope* scope,
1729 PreParserFactory* factory, 1691 PreParserFactory* factory,
1730 int pos) { 1692 int pos) {
1731 return PreParserExpression::Default(); 1693 return PreParserExpression::Default();
1732 } 1694 }
1733 1695
1696 static PreParserExpression NewTargetExpression(Scope* scope,
1697 PreParserFactory* factory,
1698 int pos) {
1699 return PreParserExpression::Default();
1700 }
1701
1734 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope, 1702 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope,
1735 int pos, int end_pos) { 1703 int pos, int end_pos) {
1736 return PreParserExpression::Default(); 1704 return PreParserExpression::Default();
1737 } 1705 }
1738 1706
1739 static PreParserExpression ExpressionFromLiteral( 1707 static PreParserExpression ExpressionFromLiteral(
1740 Token::Value token, int pos, Scanner* scanner, 1708 Token::Value token, int pos, Scanner* scanner,
1741 PreParserFactory* factory) { 1709 PreParserFactory* factory) {
1742 return PreParserExpression::Default(); 1710 return PreParserExpression::Default();
1743 } 1711 }
(...skipping 1581 matching lines...) Expand 10 before | Expand all | Expand 10 after
3325 } 3293 }
3326 } 3294 }
3327 3295
3328 3296
3329 template <class Traits> 3297 template <class Traits>
3330 typename ParserBase<Traits>::ExpressionT 3298 typename ParserBase<Traits>::ExpressionT
3331 ParserBase<Traits>::ParseMemberWithNewPrefixesExpression( 3299 ParserBase<Traits>::ParseMemberWithNewPrefixesExpression(
3332 ExpressionClassifier* classifier, bool* ok) { 3300 ExpressionClassifier* classifier, bool* ok) {
3333 // NewExpression :: 3301 // NewExpression ::
3334 // ('new')+ MemberExpression 3302 // ('new')+ MemberExpression
3303 //
3304 // NewTarget ::
3305 // 'new' '.' 'target'
3335 3306
3336 // The grammar for new expressions is pretty warped. We can have several 'new' 3307 // The grammar for new expressions is pretty warped. We can have several 'new'
3337 // keywords following each other, and then a MemberExpression. When we see '(' 3308 // keywords following each other, and then a MemberExpression. When we see '('
3338 // after the MemberExpression, it's associated with the rightmost unassociated 3309 // after the MemberExpression, it's associated with the rightmost unassociated
3339 // 'new' to create a NewExpression with arguments. However, a NewExpression 3310 // 'new' to create a NewExpression with arguments. However, a NewExpression
3340 // can also occur without arguments. 3311 // can also occur without arguments.
3341 3312
3342 // Examples of new expression: 3313 // Examples of new expression:
3343 // new foo.bar().baz means (new (foo.bar)()).baz 3314 // new foo.bar().baz means (new (foo.bar)()).baz
3344 // new foo()() means (new foo())() 3315 // new foo()() means (new foo())()
3345 // new new foo()() means (new (new foo())()) 3316 // new new foo()() means (new (new foo())())
3346 // new new foo means new (new foo) 3317 // new new foo means new (new foo)
3347 // new new foo() means new (new foo()) 3318 // new new foo() means new (new foo())
3348 // new new foo().bar().baz means (new (new foo()).bar()).baz 3319 // new new foo().bar().baz means (new (new foo()).bar()).baz
3349 3320
3350 if (peek() == Token::NEW) { 3321 if (peek() == Token::NEW) {
3351 BindingPatternUnexpectedToken(classifier); 3322 BindingPatternUnexpectedToken(classifier);
3352 Consume(Token::NEW); 3323 Consume(Token::NEW);
3353 int new_pos = position(); 3324 int new_pos = position();
3354 ExpressionT result = this->EmptyExpression(); 3325 ExpressionT result = this->EmptyExpression();
3355 if (peek() == Token::SUPER) { 3326 if (peek() == Token::SUPER) {
3356 const bool is_new = true; 3327 const bool is_new = true;
3357 result = ParseSuperExpression(is_new, classifier, CHECK_OK); 3328 result = ParseSuperExpression(is_new, classifier, CHECK_OK);
3329 } else if (allow_harmony_new_target() && peek() == Token::PERIOD) {
3330 return ParseNewTargetExpression(CHECK_OK);
3358 } else { 3331 } else {
3359 result = this->ParseMemberWithNewPrefixesExpression(classifier, CHECK_OK); 3332 result = this->ParseMemberWithNewPrefixesExpression(classifier, CHECK_OK);
3360 } 3333 }
3361 if (peek() == Token::LPAREN) { 3334 if (peek() == Token::LPAREN) {
3362 // NewExpression with arguments. 3335 // NewExpression with arguments.
3363 Scanner::Location spread_pos; 3336 Scanner::Location spread_pos;
3364 typename Traits::Type::ExpressionList args = 3337 typename Traits::Type::ExpressionList args =
3365 this->ParseArguments(&spread_pos, classifier, CHECK_OK); 3338 this->ParseArguments(&spread_pos, classifier, CHECK_OK);
3366 3339
3367 if (spread_pos.IsValid()) { 3340 if (spread_pos.IsValid()) {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
3571 3544
3572 template <class Traits> 3545 template <class Traits>
3573 typename ParserBase<Traits>::ExpressionT 3546 typename ParserBase<Traits>::ExpressionT
3574 ParserBase<Traits>::ParseSuperExpression(bool is_new, 3547 ParserBase<Traits>::ParseSuperExpression(bool is_new,
3575 ExpressionClassifier* classifier, 3548 ExpressionClassifier* classifier,
3576 bool* ok) { 3549 bool* ok) {
3577 int pos = position(); 3550 int pos = position();
3578 Expect(Token::SUPER, CHECK_OK); 3551 Expect(Token::SUPER, CHECK_OK);
3579 3552
3580 Scope* scope = scope_->DeclarationScope(); 3553 Scope* scope = scope_->DeclarationScope();
3581
3582 while (scope->is_eval_scope() || scope->is_arrow_scope()) { 3554 while (scope->is_eval_scope() || scope->is_arrow_scope()) {
3583 scope = scope->outer_scope(); 3555 scope = scope->outer_scope();
3584 DCHECK_NOT_NULL(scope); 3556 DCHECK_NOT_NULL(scope);
3585 scope = scope->DeclarationScope(); 3557 scope = scope->DeclarationScope();
3586 } 3558 }
3587 3559
3588 FunctionKind kind = scope->function_kind(); 3560 FunctionKind kind = scope->function_kind();
3589 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || 3561 if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
3590 i::IsConstructor(kind)) { 3562 i::IsConstructor(kind)) {
3591 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { 3563 if (peek() == Token::PERIOD || peek() == Token::LBRACK) {
(...skipping 16 matching lines...) Expand all
3608 return this->SuperCallReference(scope_, factory(), pos); 3580 return this->SuperCallReference(scope_, factory(), pos);
3609 } 3581 }
3610 } 3582 }
3611 3583
3612 ReportMessageAt(scanner()->location(), MessageTemplate::kUnexpectedSuper); 3584 ReportMessageAt(scanner()->location(), MessageTemplate::kUnexpectedSuper);
3613 *ok = false; 3585 *ok = false;
3614 return this->EmptyExpression(); 3586 return this->EmptyExpression();
3615 } 3587 }
3616 3588
3617 3589
3590 template <class Traits>
3591 typename ParserBase<Traits>::ExpressionT
3592 ParserBase<Traits>::ParseNewTargetExpression(bool* ok) {
3593 int pos = position();
3594 Consume(Token::PERIOD);
3595 ExpectContextualKeyword(CStrVector("target"), CHECK_OK);
3596
3597 Scope* scope = scope_->DeclarationScope();
3598 while (scope->is_eval_scope() || scope->is_arrow_scope()) {
3599 scope = scope->outer_scope();
3600 DCHECK_NOT_NULL(scope);
3601 scope = scope->DeclarationScope();
3602 }
3603
3604 if (!scope->is_function_scope()) {
3605 ReportMessageAt(scanner()->location(),
3606 MessageTemplate::kUnexpectedNewTarget);
3607 *ok = false;
3608 return this->EmptyExpression();
3609 }
3610
3611 return this->NewTargetExpression(scope_, factory(), pos);
3612 }
3613
3614
3618 template <class Traits> 3615 template <class Traits>
3619 typename ParserBase<Traits>::ExpressionT 3616 typename ParserBase<Traits>::ExpressionT
3620 ParserBase<Traits>::ParseMemberExpressionContinuation( 3617 ParserBase<Traits>::ParseMemberExpressionContinuation(
3621 ExpressionT expression, ExpressionClassifier* classifier, bool* ok) { 3618 ExpressionT expression, ExpressionClassifier* classifier, bool* ok) {
3622 // Parses this part of MemberExpression: 3619 // Parses this part of MemberExpression:
3623 // ('[' Expression ']' | '.' Identifier | TemplateLiteral)* 3620 // ('[' Expression ']' | '.' Identifier | TemplateLiteral)*
3624 while (true) { 3621 while (true) {
3625 switch (peek()) { 3622 switch (peek()) {
3626 case Token::LBRACK: { 3623 case Token::LBRACK: {
3627 BindingPatternUnexpectedToken(classifier); 3624 BindingPatternUnexpectedToken(classifier);
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
4049 *ok = false; 4046 *ok = false;
4050 return; 4047 return;
4051 } 4048 }
4052 has_seen_constructor_ = true; 4049 has_seen_constructor_ = true;
4053 return; 4050 return;
4054 } 4051 }
4055 } 4052 }
4056 } } // v8::internal 4053 } } // v8::internal
4057 4054
4058 #endif // V8_PREPARSER_H 4055 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parser.cc ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698