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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly. | 104 mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly. |
105 stack_limit_(stack_limit), | 105 stack_limit_(stack_limit), |
106 zone_(zone), | 106 zone_(zone), |
107 scanner_(scanner), | 107 scanner_(scanner), |
108 stack_overflow_(false), | 108 stack_overflow_(false), |
109 allow_lazy_(false), | 109 allow_lazy_(false), |
110 allow_natives_(false), | 110 allow_natives_(false), |
111 allow_harmony_sloppy_(false), | 111 allow_harmony_sloppy_(false), |
112 allow_harmony_sloppy_function_(false), | 112 allow_harmony_sloppy_function_(false), |
113 allow_harmony_sloppy_let_(false), | 113 allow_harmony_sloppy_let_(false), |
114 allow_harmony_default_parameters_(false), | |
115 allow_harmony_destructuring_bind_(false), | |
116 allow_harmony_destructuring_assignment_(false), | |
117 allow_harmony_restrictive_declarations_(false), | 114 allow_harmony_restrictive_declarations_(false), |
118 allow_legacy_const_(true), | 115 allow_legacy_const_(true), |
119 allow_harmony_do_expressions_(false), | 116 allow_harmony_do_expressions_(false), |
120 allow_harmony_function_name_(false), | 117 allow_harmony_function_name_(false), |
121 allow_harmony_function_sent_(false) {} | 118 allow_harmony_function_sent_(false) {} |
122 | 119 |
123 #define ALLOW_ACCESSORS(name) \ | 120 #define ALLOW_ACCESSORS(name) \ |
124 bool allow_##name() const { return allow_##name##_; } \ | 121 bool allow_##name() const { return allow_##name##_; } \ |
125 void set_allow_##name(bool allow) { allow_##name##_ = allow; } | 122 void set_allow_##name(bool allow) { allow_##name##_ = allow; } |
126 | 123 |
127 ALLOW_ACCESSORS(lazy); | 124 ALLOW_ACCESSORS(lazy); |
128 ALLOW_ACCESSORS(natives); | 125 ALLOW_ACCESSORS(natives); |
129 ALLOW_ACCESSORS(harmony_sloppy); | 126 ALLOW_ACCESSORS(harmony_sloppy); |
130 ALLOW_ACCESSORS(harmony_sloppy_function); | 127 ALLOW_ACCESSORS(harmony_sloppy_function); |
131 ALLOW_ACCESSORS(harmony_sloppy_let); | 128 ALLOW_ACCESSORS(harmony_sloppy_let); |
132 ALLOW_ACCESSORS(harmony_default_parameters); | |
133 ALLOW_ACCESSORS(harmony_destructuring_bind); | |
134 ALLOW_ACCESSORS(harmony_destructuring_assignment); | |
135 ALLOW_ACCESSORS(harmony_restrictive_declarations); | 129 ALLOW_ACCESSORS(harmony_restrictive_declarations); |
136 ALLOW_ACCESSORS(legacy_const); | 130 ALLOW_ACCESSORS(legacy_const); |
137 ALLOW_ACCESSORS(harmony_do_expressions); | 131 ALLOW_ACCESSORS(harmony_do_expressions); |
138 ALLOW_ACCESSORS(harmony_function_name); | 132 ALLOW_ACCESSORS(harmony_function_name); |
139 ALLOW_ACCESSORS(harmony_function_sent); | 133 ALLOW_ACCESSORS(harmony_function_sent); |
140 #undef ALLOW_ACCESSORS | 134 #undef ALLOW_ACCESSORS |
141 | 135 |
142 uintptr_t stack_limit() const { return stack_limit_; } | 136 uintptr_t stack_limit() const { return stack_limit_; } |
143 | 137 |
144 protected: | 138 protected: |
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 Zone* zone_; | 912 Zone* zone_; |
919 | 913 |
920 Scanner* scanner_; | 914 Scanner* scanner_; |
921 bool stack_overflow_; | 915 bool stack_overflow_; |
922 | 916 |
923 bool allow_lazy_; | 917 bool allow_lazy_; |
924 bool allow_natives_; | 918 bool allow_natives_; |
925 bool allow_harmony_sloppy_; | 919 bool allow_harmony_sloppy_; |
926 bool allow_harmony_sloppy_function_; | 920 bool allow_harmony_sloppy_function_; |
927 bool allow_harmony_sloppy_let_; | 921 bool allow_harmony_sloppy_let_; |
928 bool allow_harmony_default_parameters_; | |
929 bool allow_harmony_destructuring_bind_; | |
930 bool allow_harmony_destructuring_assignment_; | |
931 bool allow_harmony_restrictive_declarations_; | 922 bool allow_harmony_restrictive_declarations_; |
932 bool allow_legacy_const_; | 923 bool allow_legacy_const_; |
933 bool allow_harmony_do_expressions_; | 924 bool allow_harmony_do_expressions_; |
934 bool allow_harmony_function_name_; | 925 bool allow_harmony_function_name_; |
935 bool allow_harmony_function_sent_; | 926 bool allow_harmony_function_sent_; |
936 }; | 927 }; |
937 | 928 |
938 template <class Traits> | 929 template <class Traits> |
939 ParserBase<Traits>::FunctionState::FunctionState( | 930 ParserBase<Traits>::FunctionState::FunctionState( |
940 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, | 931 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1256 classifier->RecordBindingPatternError( | 1247 classifier->RecordBindingPatternError( |
1257 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenRegExp); | 1248 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenRegExp); |
1258 return this->ParseRegExpLiteral(true, classifier, ok); | 1249 return this->ParseRegExpLiteral(true, classifier, ok); |
1259 | 1250 |
1260 case Token::DIV: | 1251 case Token::DIV: |
1261 classifier->RecordBindingPatternError( | 1252 classifier->RecordBindingPatternError( |
1262 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenRegExp); | 1253 scanner()->peek_location(), MessageTemplate::kUnexpectedTokenRegExp); |
1263 return this->ParseRegExpLiteral(false, classifier, ok); | 1254 return this->ParseRegExpLiteral(false, classifier, ok); |
1264 | 1255 |
1265 case Token::LBRACK: | 1256 case Token::LBRACK: |
1266 if (!allow_harmony_destructuring_bind()) { | |
1267 BindingPatternUnexpectedToken(classifier); | |
1268 } | |
1269 return this->ParseArrayLiteral(classifier, ok); | 1257 return this->ParseArrayLiteral(classifier, ok); |
1270 | 1258 |
1271 case Token::LBRACE: | 1259 case Token::LBRACE: |
1272 if (!allow_harmony_destructuring_bind()) { | |
1273 BindingPatternUnexpectedToken(classifier); | |
1274 } | |
1275 return this->ParseObjectLiteral(classifier, ok); | 1260 return this->ParseObjectLiteral(classifier, ok); |
1276 | 1261 |
1277 case Token::LPAREN: { | 1262 case Token::LPAREN: { |
1278 // Arrow function formal parameters are either a single identifier or a | 1263 // Arrow function formal parameters are either a single identifier or a |
1279 // list of BindingPattern productions enclosed in parentheses. | 1264 // list of BindingPattern productions enclosed in parentheses. |
1280 // Parentheses are not valid on the LHS of a BindingPattern, so we use the | 1265 // Parentheses are not valid on the LHS of a BindingPattern, so we use the |
1281 // is_valid_binding_pattern() check to detect multiple levels of | 1266 // is_valid_binding_pattern() check to detect multiple levels of |
1282 // parenthesization. | 1267 // parenthesization. |
1283 if (!classifier->is_valid_binding_pattern()) { | 1268 if (!classifier->is_valid_binding_pattern()) { |
1284 ArrowFormalParametersUnexpectedToken(classifier); | 1269 ArrowFormalParametersUnexpectedToken(classifier); |
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1966 if (!Token::IsAssignmentOp(peek())) { | 1951 if (!Token::IsAssignmentOp(peek())) { |
1967 // Parsed conditional expression only (no assignment). | 1952 // Parsed conditional expression only (no assignment). |
1968 // Now pending non-pattern expressions must be merged. | 1953 // Now pending non-pattern expressions must be merged. |
1969 classifier->MergeNonPatterns(&arrow_formals_classifier); | 1954 classifier->MergeNonPatterns(&arrow_formals_classifier); |
1970 return expression; | 1955 return expression; |
1971 } | 1956 } |
1972 | 1957 |
1973 // Now pending non-pattern expressions must be discarded. | 1958 // Now pending non-pattern expressions must be discarded. |
1974 arrow_formals_classifier.Discard(); | 1959 arrow_formals_classifier.Discard(); |
1975 | 1960 |
1976 if (!(allow_harmony_destructuring_bind() || | 1961 if (IsValidPattern(expression) && peek() == Token::ASSIGN) { |
1977 allow_harmony_default_parameters())) { | |
1978 BindingPatternUnexpectedToken(classifier); | |
1979 } | |
1980 | |
1981 if (allow_harmony_destructuring_assignment() && IsValidPattern(expression) && | |
1982 peek() == Token::ASSIGN) { | |
1983 classifier->ForgiveCoverInitializedNameError(); | 1962 classifier->ForgiveCoverInitializedNameError(); |
1984 ValidateAssignmentPattern(classifier, CHECK_OK); | 1963 ValidateAssignmentPattern(classifier, CHECK_OK); |
1985 is_destructuring_assignment = true; | 1964 is_destructuring_assignment = true; |
1986 } else if (allow_harmony_default_parameters() && | |
1987 !allow_harmony_destructuring_assignment()) { | |
1988 // TODO(adamk): This branch should be removed once the destructuring | |
1989 // assignment and default parameter flags are removed. | |
1990 expression = this->ClassifyAndRewriteReferenceExpression( | |
1991 classifier, expression, lhs_beg_pos, scanner()->location().end_pos, | |
1992 MessageTemplate::kInvalidLhsInAssignment); | |
1993 } else { | 1965 } else { |
1994 expression = this->CheckAndRewriteReferenceExpression( | 1966 expression = this->CheckAndRewriteReferenceExpression( |
1995 expression, lhs_beg_pos, scanner()->location().end_pos, | 1967 expression, lhs_beg_pos, scanner()->location().end_pos, |
1996 MessageTemplate::kInvalidLhsInAssignment, CHECK_OK); | 1968 MessageTemplate::kInvalidLhsInAssignment, CHECK_OK); |
1997 } | 1969 } |
1998 | 1970 |
1999 expression = this->MarkExpressionAsAssigned(expression); | 1971 expression = this->MarkExpressionAsAssigned(expression); |
2000 | 1972 |
2001 Token::Value op = Next(); // Get assignment operator. | 1973 Token::Value op = Next(); // Get assignment operator. |
2002 if (op != Token::ASSIGN) { | 1974 if (op != Token::ASSIGN) { |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2658 } | 2630 } |
2659 | 2631 |
2660 | 2632 |
2661 template <class Traits> | 2633 template <class Traits> |
2662 void ParserBase<Traits>::ParseFormalParameter( | 2634 void ParserBase<Traits>::ParseFormalParameter( |
2663 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { | 2635 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { |
2664 // FormalParameter[Yield,GeneratorParameter] : | 2636 // FormalParameter[Yield,GeneratorParameter] : |
2665 // BindingElement[?Yield, ?GeneratorParameter] | 2637 // BindingElement[?Yield, ?GeneratorParameter] |
2666 bool is_rest = parameters->has_rest; | 2638 bool is_rest = parameters->has_rest; |
2667 | 2639 |
2668 Token::Value next = peek(); | |
2669 ExpressionT pattern = ParsePrimaryExpression(classifier, ok); | 2640 ExpressionT pattern = ParsePrimaryExpression(classifier, ok); |
2670 if (!*ok) return; | 2641 if (!*ok) return; |
2671 | 2642 |
2672 ValidateBindingPattern(classifier, ok); | 2643 ValidateBindingPattern(classifier, ok); |
2673 if (!*ok) return; | 2644 if (!*ok) return; |
2674 | 2645 |
2675 if (!Traits::IsIdentifier(pattern)) { | 2646 if (!Traits::IsIdentifier(pattern)) { |
2676 if (!allow_harmony_destructuring_bind()) { | |
2677 ReportUnexpectedToken(next); | |
2678 *ok = false; | |
2679 return; | |
2680 } | |
2681 parameters->is_simple = false; | 2647 parameters->is_simple = false; |
2682 ValidateFormalParameterInitializer(classifier, ok); | 2648 ValidateFormalParameterInitializer(classifier, ok); |
2683 if (!*ok) return; | 2649 if (!*ok) return; |
2684 classifier->RecordNonSimpleParameter(); | 2650 classifier->RecordNonSimpleParameter(); |
2685 } | 2651 } |
2686 | 2652 |
2687 ExpressionT initializer = Traits::EmptyExpression(); | 2653 ExpressionT initializer = Traits::EmptyExpression(); |
2688 if (!is_rest && allow_harmony_default_parameters() && Check(Token::ASSIGN)) { | 2654 if (!is_rest && Check(Token::ASSIGN)) { |
2689 ExpressionClassifier init_classifier(this); | 2655 ExpressionClassifier init_classifier(this); |
2690 initializer = ParseAssignmentExpression(true, &init_classifier, ok); | 2656 initializer = ParseAssignmentExpression(true, &init_classifier, ok); |
2691 if (!*ok) return; | 2657 if (!*ok) return; |
2692 Traits::RewriteNonPattern(&init_classifier, ok); | 2658 Traits::RewriteNonPattern(&init_classifier, ok); |
2693 ValidateFormalParameterInitializer(&init_classifier, ok); | 2659 ValidateFormalParameterInitializer(&init_classifier, ok); |
2694 if (!*ok) return; | 2660 if (!*ok) return; |
2695 parameters->is_simple = false; | 2661 parameters->is_simple = false; |
2696 init_classifier.Discard(); | 2662 init_classifier.Discard(); |
2697 classifier->RecordNonSimpleParameter(); | 2663 classifier->RecordNonSimpleParameter(); |
2698 | 2664 |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3137 has_seen_constructor_ = true; | 3103 has_seen_constructor_ = true; |
3138 return; | 3104 return; |
3139 } | 3105 } |
3140 } | 3106 } |
3141 | 3107 |
3142 | 3108 |
3143 } // namespace internal | 3109 } // namespace internal |
3144 } // namespace v8 | 3110 } // namespace v8 |
3145 | 3111 |
3146 #endif // V8_PARSING_PARSER_BASE_H | 3112 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |