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/bailout-reason.h" | 8 #include "src/bailout-reason.h" |
9 #include "src/expression-classifier.h" | 9 #include "src/expression-classifier.h" |
10 #include "src/func-name-inferrer.h" | 10 #include "src/func-name-inferrer.h" |
(...skipping 1290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1301 FunctionLiteral::IsFunctionFlag is_function, | 1301 FunctionLiteral::IsFunctionFlag is_function, |
1302 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind, | 1302 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind, |
1303 int position) { | 1303 int position) { |
1304 return PreParserExpression::Default(); | 1304 return PreParserExpression::Default(); |
1305 } | 1305 } |
1306 | 1306 |
1307 PreParserExpression NewSpread(PreParserExpression expression, int pos) { | 1307 PreParserExpression NewSpread(PreParserExpression expression, int pos) { |
1308 return PreParserExpression::Spread(expression); | 1308 return PreParserExpression::Spread(expression); |
1309 } | 1309 } |
1310 | 1310 |
1311 PreParserExpression NewEmptyParentheses(int pos) { | |
1312 return PreParserExpression::Default(); | |
1313 } | |
1314 | |
1311 // Return the object itself as AstVisitor and implement the needed | 1315 // Return the object itself as AstVisitor and implement the needed |
1312 // dummy method right in this class. | 1316 // dummy method right in this class. |
1313 PreParserFactory* visitor() { return this; } | 1317 PreParserFactory* visitor() { return this; } |
1314 int* ast_properties() { | 1318 int* ast_properties() { |
1315 static int dummy = 42; | 1319 static int dummy = 42; |
1316 return &dummy; | 1320 return &dummy; |
1317 } | 1321 } |
1318 }; | 1322 }; |
1319 | 1323 |
1320 | 1324 |
(...skipping 934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2255 // Arrow function formal parameters are either a single identifier or a | 2259 // Arrow function formal parameters are either a single identifier or a |
2256 // list of BindingPattern productions enclosed in parentheses. | 2260 // list of BindingPattern productions enclosed in parentheses. |
2257 // Parentheses are not valid on the LHS of a BindingPattern, so we use the | 2261 // Parentheses are not valid on the LHS of a BindingPattern, so we use the |
2258 // is_valid_binding_pattern() check to detect multiple levels of | 2262 // is_valid_binding_pattern() check to detect multiple levels of |
2259 // parenthesization. | 2263 // parenthesization. |
2260 if (!classifier->is_valid_binding_pattern()) { | 2264 if (!classifier->is_valid_binding_pattern()) { |
2261 ArrowFormalParametersUnexpectedToken(classifier); | 2265 ArrowFormalParametersUnexpectedToken(classifier); |
2262 } | 2266 } |
2263 BindingPatternUnexpectedToken(classifier); | 2267 BindingPatternUnexpectedToken(classifier); |
2264 Consume(Token::LPAREN); | 2268 Consume(Token::LPAREN); |
2265 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { | 2269 if (Check(Token::RPAREN)) { |
2266 // As a primary expression, the only thing that can follow "()" is "=>". | 2270 // ()=>x. The continuation that looks for the => is in |
2271 // ParseAssignmentExpression. | |
2272 classifier->RecordExpressionError(scanner()->location(), | |
2273 MessageTemplate::kUnexpectedToken, | |
2274 Token::String(Token::RPAREN)); | |
2267 classifier->RecordBindingPatternError(scanner()->location(), | 2275 classifier->RecordBindingPatternError(scanner()->location(), |
2268 MessageTemplate::kUnexpectedToken, | 2276 MessageTemplate::kUnexpectedToken, |
2269 Token::String(Token::RPAREN)); | 2277 Token::String(Token::RPAREN)); |
2270 // Give a good error to the user who might have typed e.g. "return();". | 2278 result = factory()->NewEmptyParentheses(beg_pos); |
2271 if (peek() != Token::ARROW) { | 2279 } else if (allow_harmony_rest_parameters() && Check(Token::ELLIPSIS)) { |
2272 ReportUnexpectedTokenAt(scanner_->peek_location(), peek(), | 2280 // (...x)=>x. The continuation that looks for the => is in |
2273 MessageTemplate::kMissingArrow); | 2281 // ParseAssignmentExpression. |
2282 int ellipsis_pos = scanner()->location().beg_pos; | |
2283 classifier->RecordExpressionError(scanner()->location(), | |
2284 MessageTemplate::kUnexpectedToken, | |
2285 Token::String(Token::ELLIPSIS)); | |
2286 Scanner::Location expr_loc = scanner()->peek_location(); | |
2287 Token::Value tok = peek(); | |
2288 result = | |
rossberg
2015/08/20 16:50:00
Nit: unnecessary line break?
wingo
2015/08/21 09:50:48
Done.
| |
2289 this->ParseAssignmentExpression(true, classifier, CHECK_OK); | |
2290 // Patterns not allowed as rest parameters. There is no way we can | |
rossberg
2015/08/20 16:50:00
Nit: insert "are".
FWIW, Microsoft just proposed
wingo
2015/08/21 09:50:49
Done.
| |
2291 // succeed so go ahead and use the convenient ReportUnexpectedToken | |
2292 // interface. | |
2293 if (!Traits::IsIdentifier(result)) { | |
2294 ReportUnexpectedTokenAt(expr_loc, tok); | |
2274 *ok = false; | 2295 *ok = false; |
2275 return this->EmptyExpression(); | 2296 return this->EmptyExpression(); |
2276 } | 2297 } |
2277 Scope* scope = | 2298 result = factory()->NewSpread(result, ellipsis_pos); |
2278 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); | |
2279 FormalParametersT parameters(scope); | |
2280 scope->set_start_position(beg_pos); | |
2281 ExpressionClassifier args_classifier; | |
2282 result = this->ParseArrowFunctionLiteral(parameters, args_classifier, | |
2283 CHECK_OK); | |
2284 } else if (allow_harmony_arrow_functions() && | |
2285 allow_harmony_rest_parameters() && Check(Token::ELLIPSIS)) { | |
2286 // (...x) => y | |
2287 Scope* scope = | |
2288 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); | |
2289 FormalParametersT formals(scope); | |
2290 scope->set_start_position(beg_pos); | |
2291 ExpressionClassifier formals_classifier; | |
2292 formals.has_rest = true; | |
2293 this->ParseFormalParameter(&formals, &formals_classifier, CHECK_OK); | |
2294 Traits::DeclareFormalParameter( | |
2295 formals.scope, formals.at(0), formals.is_simple, | |
2296 &formals_classifier); | |
2297 if (peek() == Token::COMMA) { | 2299 if (peek() == Token::COMMA) { |
2298 ReportMessageAt(scanner()->peek_location(), | 2300 ReportMessageAt(scanner()->peek_location(), |
2299 MessageTemplate::kParamAfterRest); | 2301 MessageTemplate::kParamAfterRest); |
2300 *ok = false; | 2302 *ok = false; |
2301 return this->EmptyExpression(); | 2303 return this->EmptyExpression(); |
2302 } | 2304 } |
2303 Expect(Token::RPAREN, CHECK_OK); | 2305 Expect(Token::RPAREN, CHECK_OK); |
2304 result = this->ParseArrowFunctionLiteral(formals, formals_classifier, | |
2305 CHECK_OK); | |
2306 } else { | 2306 } else { |
2307 // Heuristically try to detect immediately called functions before | 2307 // Heuristically try to detect immediately called functions before |
2308 // seeing the call parentheses. | 2308 // seeing the call parentheses. |
2309 parenthesized_function_ = (peek() == Token::FUNCTION); | 2309 parenthesized_function_ = (peek() == Token::FUNCTION); |
2310 result = this->ParseExpression(true, classifier, CHECK_OK); | 2310 result = this->ParseExpression(true, classifier, CHECK_OK); |
2311 Expect(Token::RPAREN, CHECK_OK); | 2311 Expect(Token::RPAREN, CHECK_OK); |
2312 } | 2312 } |
2313 break; | 2313 break; |
2314 | 2314 |
2315 case Token::CLASS: { | 2315 case Token::CLASS: { |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2857 &duplicate_loc, CHECK_OK); | 2857 &duplicate_loc, CHECK_OK); |
2858 if (duplicate_loc.IsValid()) { | 2858 if (duplicate_loc.IsValid()) { |
2859 arrow_formals_classifier.RecordDuplicateFormalParameterError( | 2859 arrow_formals_classifier.RecordDuplicateFormalParameterError( |
2860 duplicate_loc); | 2860 duplicate_loc); |
2861 } | 2861 } |
2862 expression = this->ParseArrowFunctionLiteral( | 2862 expression = this->ParseArrowFunctionLiteral( |
2863 parameters, arrow_formals_classifier, CHECK_OK); | 2863 parameters, arrow_formals_classifier, CHECK_OK); |
2864 return expression; | 2864 return expression; |
2865 } | 2865 } |
2866 | 2866 |
2867 // It could be that ParseConditionalExpression returned EmptyParentheses if it | |
2868 // saw () but we didn't see a => afterward. This is a sufficiently rare case | |
2869 // that we should probably make sure it results in an exception and sets OK to | |
2870 // false. | |
2871 // ValidateExpression(&arrow_formals_classifier, CHECK_OK); | |
rossberg
2015/08/20 16:50:00
Hm?
wingo
2015/08/21 09:50:48
A vestige of a previous patch, oops! Removed.
| |
2872 | |
2867 // "expression" was not itself an arrow function parameter list, but it might | 2873 // "expression" was not itself an arrow function parameter list, but it might |
2868 // form part of one. Propagate speculative formal parameter error locations. | 2874 // form part of one. Propagate speculative formal parameter error locations. |
2869 classifier->Accumulate(arrow_formals_classifier, | 2875 classifier->Accumulate(arrow_formals_classifier, |
2870 ExpressionClassifier::StandardProductions | | 2876 ExpressionClassifier::StandardProductions | |
2871 ExpressionClassifier::FormalParametersProductions); | 2877 ExpressionClassifier::FormalParametersProductions); |
2872 | 2878 |
2873 if (!Token::IsAssignmentOp(peek())) { | 2879 if (!Token::IsAssignmentOp(peek())) { |
2874 if (fni_ != NULL) fni_->Leave(); | 2880 if (fni_ != NULL) fni_->Leave(); |
2875 // Parsed conditional expression only (no assignment). | 2881 // Parsed conditional expression only (no assignment). |
2876 return expression; | 2882 return expression; |
(...skipping 1169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4046 *ok = false; | 4052 *ok = false; |
4047 return; | 4053 return; |
4048 } | 4054 } |
4049 has_seen_constructor_ = true; | 4055 has_seen_constructor_ = true; |
4050 return; | 4056 return; |
4051 } | 4057 } |
4052 } | 4058 } |
4053 } } // v8::internal | 4059 } } // v8::internal |
4054 | 4060 |
4055 #endif // V8_PREPARSER_H | 4061 #endif // V8_PREPARSER_H |
OLD | NEW |