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 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 } | 495 } |
496 | 496 |
497 void ReportMessageAt(Scanner::Location location, | 497 void ReportMessageAt(Scanner::Location location, |
498 MessageTemplate::Template message, | 498 MessageTemplate::Template message, |
499 ParseErrorType error_type = kSyntaxError) { | 499 ParseErrorType error_type = kSyntaxError) { |
500 Traits::ReportMessageAt(location, message, reinterpret_cast<const char*>(0), | 500 Traits::ReportMessageAt(location, message, reinterpret_cast<const char*>(0), |
501 error_type); | 501 error_type); |
502 } | 502 } |
503 | 503 |
504 void ReportUnexpectedToken(Token::Value token); | 504 void ReportUnexpectedToken(Token::Value token); |
505 void ReportUnexpectedTokenAt(Scanner::Location location, Token::Value token); | 505 void ReportUnexpectedTokenAt( |
| 506 Scanner::Location location, Token::Value token, |
| 507 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken); |
506 | 508 |
507 | 509 |
508 void ReportClassifierError(const ExpressionClassifier::Error& error) { | 510 void ReportClassifierError(const ExpressionClassifier::Error& error) { |
509 Traits::ReportMessageAt(error.location, error.message, error.arg, | 511 Traits::ReportMessageAt(error.location, error.message, error.arg, |
510 kSyntaxError); | 512 kSyntaxError); |
511 } | 513 } |
512 | 514 |
513 void ValidateExpression(const ExpressionClassifier* classifier, bool* ok) { | 515 void ValidateExpression(const ExpressionClassifier* classifier, bool* ok) { |
514 if (!classifier->is_valid_expression()) { | 516 if (!classifier->is_valid_expression()) { |
515 ReportClassifierError(classifier->expression_error()); | 517 ReportClassifierError(classifier->expression_error()); |
(...skipping 1291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1807 *function_state_stack_ = outer_function_state_; | 1809 *function_state_stack_ = outer_function_state_; |
1808 } | 1810 } |
1809 | 1811 |
1810 | 1812 |
1811 template<class Traits> | 1813 template<class Traits> |
1812 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { | 1814 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { |
1813 return ReportUnexpectedTokenAt(scanner_->location(), token); | 1815 return ReportUnexpectedTokenAt(scanner_->location(), token); |
1814 } | 1816 } |
1815 | 1817 |
1816 | 1818 |
1817 template<class Traits> | 1819 template <class Traits> |
1818 void ParserBase<Traits>::ReportUnexpectedTokenAt( | 1820 void ParserBase<Traits>::ReportUnexpectedTokenAt( |
1819 Scanner::Location source_location, Token::Value token) { | 1821 Scanner::Location source_location, Token::Value token, |
1820 | 1822 MessageTemplate::Template message) { |
1821 // Four of the tokens are treated specially | 1823 // Four of the tokens are treated specially |
1822 switch (token) { | 1824 switch (token) { |
1823 case Token::EOS: | 1825 case Token::EOS: |
1824 return ReportMessageAt(source_location, MessageTemplate::kUnexpectedEOS); | 1826 return ReportMessageAt(source_location, MessageTemplate::kUnexpectedEOS); |
1825 case Token::SMI: | 1827 case Token::SMI: |
1826 case Token::NUMBER: | 1828 case Token::NUMBER: |
1827 return ReportMessageAt(source_location, | 1829 return ReportMessageAt(source_location, |
1828 MessageTemplate::kUnexpectedTokenNumber); | 1830 MessageTemplate::kUnexpectedTokenNumber); |
1829 case Token::STRING: | 1831 case Token::STRING: |
1830 return ReportMessageAt(source_location, | 1832 return ReportMessageAt(source_location, |
(...skipping 12 matching lines...) Expand all Loading... |
1843 is_strict(language_mode()) | 1845 is_strict(language_mode()) |
1844 ? MessageTemplate::kUnexpectedStrictReserved | 1846 ? MessageTemplate::kUnexpectedStrictReserved |
1845 : MessageTemplate::kUnexpectedTokenIdentifier); | 1847 : MessageTemplate::kUnexpectedTokenIdentifier); |
1846 case Token::TEMPLATE_SPAN: | 1848 case Token::TEMPLATE_SPAN: |
1847 case Token::TEMPLATE_TAIL: | 1849 case Token::TEMPLATE_TAIL: |
1848 return Traits::ReportMessageAt( | 1850 return Traits::ReportMessageAt( |
1849 source_location, MessageTemplate::kUnexpectedTemplateString); | 1851 source_location, MessageTemplate::kUnexpectedTemplateString); |
1850 default: | 1852 default: |
1851 const char* name = Token::String(token); | 1853 const char* name = Token::String(token); |
1852 DCHECK(name != NULL); | 1854 DCHECK(name != NULL); |
1853 Traits::ReportMessageAt(source_location, | 1855 Traits::ReportMessageAt(source_location, message, name); |
1854 MessageTemplate::kUnexpectedToken, name); | |
1855 } | 1856 } |
1856 } | 1857 } |
1857 | 1858 |
1858 | 1859 |
1859 template <class Traits> | 1860 template <class Traits> |
1860 typename ParserBase<Traits>::IdentifierT ParserBase<Traits>::ParseIdentifier( | 1861 typename ParserBase<Traits>::IdentifierT ParserBase<Traits>::ParseIdentifier( |
1861 AllowRestrictedIdentifiers allow_restricted_identifiers, bool* ok) { | 1862 AllowRestrictedIdentifiers allow_restricted_identifiers, bool* ok) { |
1862 ExpressionClassifier classifier; | 1863 ExpressionClassifier classifier; |
1863 auto result = ParseAndClassifyIdentifier(&classifier, ok); | 1864 auto result = ParseAndClassifyIdentifier(&classifier, ok); |
1864 if (!*ok) return Traits::EmptyIdentifier(); | 1865 if (!*ok) return Traits::EmptyIdentifier(); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2145 if (!classifier->is_valid_binding_pattern()) { | 2146 if (!classifier->is_valid_binding_pattern()) { |
2146 ArrowFormalParametersUnexpectedToken(classifier); | 2147 ArrowFormalParametersUnexpectedToken(classifier); |
2147 } | 2148 } |
2148 BindingPatternUnexpectedToken(classifier); | 2149 BindingPatternUnexpectedToken(classifier); |
2149 Consume(Token::LPAREN); | 2150 Consume(Token::LPAREN); |
2150 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { | 2151 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { |
2151 // As a primary expression, the only thing that can follow "()" is "=>". | 2152 // As a primary expression, the only thing that can follow "()" is "=>". |
2152 classifier->RecordBindingPatternError(scanner()->location(), | 2153 classifier->RecordBindingPatternError(scanner()->location(), |
2153 MessageTemplate::kUnexpectedToken, | 2154 MessageTemplate::kUnexpectedToken, |
2154 Token::String(Token::RPAREN)); | 2155 Token::String(Token::RPAREN)); |
| 2156 // Give a good error to the user who might have typed e.g. "return();". |
| 2157 if (peek() != Token::ARROW) { |
| 2158 ReportUnexpectedTokenAt(scanner_->peek_location(), peek(), |
| 2159 MessageTemplate::kMissingArrow); |
| 2160 *ok = false; |
| 2161 return this->EmptyExpression(); |
| 2162 } |
2155 Scope* scope = | 2163 Scope* scope = |
2156 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); | 2164 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); |
2157 scope->set_start_position(beg_pos); | 2165 scope->set_start_position(beg_pos); |
2158 ExpressionClassifier args_classifier; | 2166 ExpressionClassifier args_classifier; |
2159 bool has_rest = false; | 2167 bool has_rest = false; |
2160 result = this->ParseArrowFunctionLiteral(scope, has_rest, | 2168 result = this->ParseArrowFunctionLiteral(scope, has_rest, |
2161 args_classifier, CHECK_OK); | 2169 args_classifier, CHECK_OK); |
2162 } else if (allow_harmony_arrow_functions() && | 2170 } else if (allow_harmony_arrow_functions() && |
2163 allow_harmony_rest_params() && Check(Token::ELLIPSIS)) { | 2171 allow_harmony_rest_params() && Check(Token::ELLIPSIS)) { |
2164 // (...x) => y | 2172 // (...x) => y |
(...skipping 1699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3864 *ok = false; | 3872 *ok = false; |
3865 return; | 3873 return; |
3866 } | 3874 } |
3867 has_seen_constructor_ = true; | 3875 has_seen_constructor_ = true; |
3868 return; | 3876 return; |
3869 } | 3877 } |
3870 } | 3878 } |
3871 } } // v8::internal | 3879 } } // v8::internal |
3872 | 3880 |
3873 #endif // V8_PREPARSER_H | 3881 #endif // V8_PREPARSER_H |
OLD | NEW |