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

Side by Side Diff: src/parsing/parser-base.h

Issue 1793913002: [parser] implement error reporting for Scanner errors (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: shape changing lines of code into new lines of code Created 4 years, 9 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
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_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 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 } 575 }
576 576
577 void ReportMessageAt(Scanner::Location location, 577 void ReportMessageAt(Scanner::Location location,
578 MessageTemplate::Template message, 578 MessageTemplate::Template message,
579 ParseErrorType error_type = kSyntaxError) { 579 ParseErrorType error_type = kSyntaxError) {
580 Traits::ReportMessageAt(location, message, reinterpret_cast<const char*>(0), 580 Traits::ReportMessageAt(location, message, reinterpret_cast<const char*>(0),
581 error_type); 581 error_type);
582 } 582 }
583 583
584 void GetUnexpectedTokenMessage( 584 void GetUnexpectedTokenMessage(
585 Token::Value token, MessageTemplate::Template* message, const char** arg, 585 Token::Value token, MessageTemplate::Template* message,
586 Scanner::Location* location, const char** arg,
586 MessageTemplate::Template default_ = MessageTemplate::kUnexpectedToken); 587 MessageTemplate::Template default_ = MessageTemplate::kUnexpectedToken);
587 588
588 void ReportUnexpectedToken(Token::Value token); 589 void ReportUnexpectedToken(Token::Value token);
589 void ReportUnexpectedTokenAt( 590 void ReportUnexpectedTokenAt(
590 Scanner::Location location, Token::Value token, 591 Scanner::Location location, Token::Value token,
591 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken); 592 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken);
592 593
593 void ReportClassifierError( 594 void ReportClassifierError(
594 const typename ExpressionClassifier::Error& error) { 595 const typename ExpressionClassifier::Error& error) {
595 Traits::ReportMessageAt(error.location, error.message, error.arg, 596 Traits::ReportMessageAt(error.location, error.message, error.arg,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 void ValidateLetPattern(const ExpressionClassifier* classifier, bool* ok) { 677 void ValidateLetPattern(const ExpressionClassifier* classifier, bool* ok) {
677 if (!classifier->is_valid_let_pattern()) { 678 if (!classifier->is_valid_let_pattern()) {
678 ReportClassifierError(classifier->let_pattern_error()); 679 ReportClassifierError(classifier->let_pattern_error());
679 *ok = false; 680 *ok = false;
680 } 681 }
681 } 682 }
682 683
683 void ExpressionUnexpectedToken(ExpressionClassifier* classifier) { 684 void ExpressionUnexpectedToken(ExpressionClassifier* classifier) {
684 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken; 685 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
685 const char* arg; 686 const char* arg;
686 GetUnexpectedTokenMessage(peek(), &message, &arg); 687 Scanner::Location location = scanner()->peek_location();
687 classifier->RecordExpressionError(scanner()->peek_location(), message, arg); 688 GetUnexpectedTokenMessage(peek(), &message, &location, &arg);
689 classifier->RecordExpressionError(location, message, arg);
688 } 690 }
689 691
690 void BindingPatternUnexpectedToken(ExpressionClassifier* classifier) { 692 void BindingPatternUnexpectedToken(ExpressionClassifier* classifier) {
691 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken; 693 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
692 const char* arg; 694 const char* arg;
693 GetUnexpectedTokenMessage(peek(), &message, &arg); 695 Scanner::Location location = scanner()->peek_location();
694 classifier->RecordBindingPatternError(scanner()->peek_location(), message, 696 GetUnexpectedTokenMessage(peek(), &message, &location, &arg);
695 arg); 697 classifier->RecordBindingPatternError(location, message, arg);
696 } 698 }
697 699
698 void ArrowFormalParametersUnexpectedToken(ExpressionClassifier* classifier) { 700 void ArrowFormalParametersUnexpectedToken(ExpressionClassifier* classifier) {
699 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken; 701 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
700 const char* arg; 702 const char* arg;
701 GetUnexpectedTokenMessage(peek(), &message, &arg); 703 Scanner::Location location = scanner()->peek_location();
702 classifier->RecordArrowFormalParametersError(scanner()->peek_location(), 704 GetUnexpectedTokenMessage(peek(), &message, &location, &arg);
703 message, arg); 705 classifier->RecordArrowFormalParametersError(location, message, arg);
704 } 706 }
705 707
706 void FormalParameterInitializerUnexpectedToken( 708 void FormalParameterInitializerUnexpectedToken(
707 ExpressionClassifier* classifier) { 709 ExpressionClassifier* classifier) {
708 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken; 710 MessageTemplate::Template message = MessageTemplate::kUnexpectedToken;
709 const char* arg; 711 const char* arg;
710 GetUnexpectedTokenMessage(peek(), &message, &arg); 712 Scanner::Location location = scanner()->peek_location();
711 classifier->RecordFormalParameterInitializerError( 713 GetUnexpectedTokenMessage(peek(), &message, &location, &arg);
712 scanner()->peek_location(), message, arg); 714 classifier->RecordFormalParameterInitializerError(location, message, arg);
713 } 715 }
714 716
715 // Recursive descent functions: 717 // Recursive descent functions:
716 718
717 // Parses an identifier that is valid for the current scope, in particular it 719 // Parses an identifier that is valid for the current scope, in particular it
718 // fails on strict mode future reserved keywords in a strict scope. If 720 // fails on strict mode future reserved keywords in a strict scope. If
719 // allow_eval_or_arguments is kAllowEvalOrArguments, we allow "eval" or 721 // allow_eval_or_arguments is kAllowEvalOrArguments, we allow "eval" or
720 // "arguments" as identifier even in strict mode (this is needed in cases like 722 // "arguments" as identifier even in strict mode (this is needed in cases like
721 // "var foo = eval;"). 723 // "var foo = eval;").
722 IdentifierT ParseIdentifier(AllowRestrictedIdentifiers, bool* ok); 724 IdentifierT ParseIdentifier(AllowRestrictedIdentifiers, bool* ok);
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 *function_state_stack = this; 952 *function_state_stack = this;
951 } 953 }
952 954
953 955
954 template <class Traits> 956 template <class Traits>
955 ParserBase<Traits>::FunctionState::~FunctionState() { 957 ParserBase<Traits>::FunctionState::~FunctionState() {
956 *scope_stack_ = outer_scope_; 958 *scope_stack_ = outer_scope_;
957 *function_state_stack_ = outer_function_state_; 959 *function_state_stack_ = outer_function_state_;
958 } 960 }
959 961
960
961 template <class Traits> 962 template <class Traits>
962 void ParserBase<Traits>::GetUnexpectedTokenMessage( 963 void ParserBase<Traits>::GetUnexpectedTokenMessage(
963 Token::Value token, MessageTemplate::Template* message, const char** arg, 964 Token::Value token, MessageTemplate::Template* message,
965 Scanner::Location* location, const char** arg,
964 MessageTemplate::Template default_) { 966 MessageTemplate::Template default_) {
965 *arg = nullptr; 967 *arg = nullptr;
966 switch (token) { 968 switch (token) {
967 case Token::EOS: 969 case Token::EOS:
968 *message = MessageTemplate::kUnexpectedEOS; 970 *message = MessageTemplate::kUnexpectedEOS;
969 break; 971 break;
970 case Token::SMI: 972 case Token::SMI:
971 case Token::NUMBER: 973 case Token::NUMBER:
972 *message = MessageTemplate::kUnexpectedTokenNumber; 974 *message = MessageTemplate::kUnexpectedTokenNumber;
973 break; 975 break;
(...skipping 16 matching lines...) Expand all
990 break; 992 break;
991 case Token::TEMPLATE_SPAN: 993 case Token::TEMPLATE_SPAN:
992 case Token::TEMPLATE_TAIL: 994 case Token::TEMPLATE_TAIL:
993 *message = MessageTemplate::kUnexpectedTemplateString; 995 *message = MessageTemplate::kUnexpectedTemplateString;
994 break; 996 break;
995 case Token::ESCAPED_STRICT_RESERVED_WORD: 997 case Token::ESCAPED_STRICT_RESERVED_WORD:
996 case Token::ESCAPED_KEYWORD: 998 case Token::ESCAPED_KEYWORD:
997 *message = MessageTemplate::kInvalidEscapedReservedWord; 999 *message = MessageTemplate::kInvalidEscapedReservedWord;
998 break; 1000 break;
999 case Token::ILLEGAL: 1001 case Token::ILLEGAL:
1000 *message = MessageTemplate::kInvalidOrUnexpectedToken; 1002 if (scanner()->has_error()) {
1003 *message = scanner()->error();
1004 *location = scanner()->error_location();
1005 } else {
1006 *message = MessageTemplate::kInvalidOrUnexpectedToken;
1007 }
1001 break; 1008 break;
1002 default: 1009 default:
1003 const char* name = Token::String(token); 1010 const char* name = Token::String(token);
1004 DCHECK(name != NULL); 1011 DCHECK(name != NULL);
1005 *arg = name; 1012 *arg = name;
1006 break; 1013 break;
1007 } 1014 }
1008 } 1015 }
1009 1016
1010 1017
1011 template <class Traits> 1018 template <class Traits>
1012 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { 1019 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) {
1013 return ReportUnexpectedTokenAt(scanner_->location(), token); 1020 return ReportUnexpectedTokenAt(scanner_->location(), token);
1014 } 1021 }
1015 1022
1016 1023
1017 template <class Traits> 1024 template <class Traits>
1018 void ParserBase<Traits>::ReportUnexpectedTokenAt( 1025 void ParserBase<Traits>::ReportUnexpectedTokenAt(
1019 Scanner::Location source_location, Token::Value token, 1026 Scanner::Location source_location, Token::Value token,
1020 MessageTemplate::Template message) { 1027 MessageTemplate::Template message) {
1021 const char* arg; 1028 const char* arg;
1022 GetUnexpectedTokenMessage(token, &message, &arg); 1029 GetUnexpectedTokenMessage(token, &message, &source_location, &arg);
1023 Traits::ReportMessageAt(source_location, message, arg); 1030 Traits::ReportMessageAt(source_location, message, arg);
1024 } 1031 }
1025 1032
1026 1033
1027 template <class Traits> 1034 template <class Traits>
1028 typename ParserBase<Traits>::IdentifierT ParserBase<Traits>::ParseIdentifier( 1035 typename ParserBase<Traits>::IdentifierT ParserBase<Traits>::ParseIdentifier(
1029 AllowRestrictedIdentifiers allow_restricted_identifiers, bool* ok) { 1036 AllowRestrictedIdentifiers allow_restricted_identifiers, bool* ok) {
1030 ExpressionClassifier classifier(this); 1037 ExpressionClassifier classifier(this);
1031 auto result = ParseAndClassifyIdentifier(&classifier, ok); 1038 auto result = ParseAndClassifyIdentifier(&classifier, ok);
1032 if (!*ok) return Traits::EmptyIdentifier(); 1039 if (!*ok) return Traits::EmptyIdentifier();
(...skipping 2078 matching lines...) Expand 10 before | Expand all | Expand 10 after
3111 has_seen_constructor_ = true; 3118 has_seen_constructor_ = true;
3112 return; 3119 return;
3113 } 3120 }
3114 } 3121 }
3115 3122
3116 3123
3117 } // namespace internal 3124 } // namespace internal
3118 } // namespace v8 3125 } // namespace v8
3119 3126
3120 #endif // V8_PARSING_PARSER_BASE_H 3127 #endif // V8_PARSING_PARSER_BASE_H
OLDNEW
« no previous file with comments | « src/messages.h ('k') | src/parsing/scanner.h » ('j') | src/parsing/scanner.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698