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

Side by Side Diff: src/preparser.h

Issue 812163003: Simplify scanner and generate better error message for legacy octals in templates (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase it Created 6 years 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 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 void ExpectContextualKeyword(Vector<const char> keyword, bool* ok) { 378 void ExpectContextualKeyword(Vector<const char> keyword, bool* ok) {
379 Expect(Token::IDENTIFIER, ok); 379 Expect(Token::IDENTIFIER, ok);
380 if (!*ok) return; 380 if (!*ok) return;
381 if (!scanner()->is_literal_contextual_keyword(keyword)) { 381 if (!scanner()->is_literal_contextual_keyword(keyword)) {
382 ReportUnexpectedToken(scanner()->current_token()); 382 ReportUnexpectedToken(scanner()->current_token());
383 *ok = false; 383 *ok = false;
384 } 384 }
385 } 385 }
386 386
387 // Checks whether an octal literal was last seen between beg_pos and end_pos. 387 // Checks whether an octal literal was last seen between beg_pos and end_pos.
388 // If so, reports an error. Only called for strict mode. 388 // If so, reports an error. Only called for strict mode and template strings.
389 void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) { 389 void CheckOctalLiteral(int beg_pos, int end_pos, const char* error,
390 bool* ok) {
390 Scanner::Location octal = scanner()->octal_position(); 391 Scanner::Location octal = scanner()->octal_position();
391 if (octal.IsValid() && beg_pos <= octal.beg_pos && 392 if (octal.IsValid() && beg_pos <= octal.beg_pos &&
392 octal.end_pos <= end_pos) { 393 octal.end_pos <= end_pos) {
393 ReportMessageAt(octal, "strict_octal_literal"); 394 ReportMessageAt(octal, error);
394 scanner()->clear_octal_position(); 395 scanner()->clear_octal_position();
395 *ok = false; 396 *ok = false;
396 } 397 }
397 } 398 }
398 399
400 inline void CheckStrictOctalLiteral(int beg_pos, int end_pos, bool* ok) {
401 CheckOctalLiteral(beg_pos, end_pos, "strict_octal_literal", ok);
402 }
403
404 inline void CheckTemplateOctalLiteral(int beg_pos, int end_pos, bool* ok) {
405 CheckOctalLiteral(beg_pos, end_pos, "template_octal_literal", ok);
406 }
407
399 // Validates strict mode for function parameter lists. This has to be 408 // Validates strict mode for function parameter lists. This has to be
400 // done after parsing the function, since the function can declare 409 // done after parsing the function, since the function can declare
401 // itself strict. 410 // itself strict.
402 void CheckStrictFunctionNameAndParameters( 411 void CheckStrictFunctionNameAndParameters(
403 IdentifierT function_name, 412 IdentifierT function_name,
404 bool function_name_is_strict_reserved, 413 bool function_name_is_strict_reserved,
405 const Scanner::Location& function_name_loc, 414 const Scanner::Location& function_name_loc,
406 const Scanner::Location& eval_args_error_loc, 415 const Scanner::Location& eval_args_error_loc,
407 const Scanner::Location& dupe_error_loc, 416 const Scanner::Location& dupe_error_loc,
408 const Scanner::Location& reserved_loc, 417 const Scanner::Location& reserved_loc,
(...skipping 1073 matching lines...) Expand 10 before | Expand all | Expand 10 after
1482 PreParserScope scope(scope_, SCRIPT_SCOPE); 1491 PreParserScope scope(scope_, SCRIPT_SCOPE);
1483 PreParserFactory factory(NULL); 1492 PreParserFactory factory(NULL);
1484 FunctionState top_scope(&function_state_, &scope_, &scope, &factory); 1493 FunctionState top_scope(&function_state_, &scope_, &scope, &factory);
1485 bool ok = true; 1494 bool ok = true;
1486 int start_position = scanner()->peek_location().beg_pos; 1495 int start_position = scanner()->peek_location().beg_pos;
1487 ParseSourceElements(Token::EOS, &ok); 1496 ParseSourceElements(Token::EOS, &ok);
1488 if (stack_overflow()) return kPreParseStackOverflow; 1497 if (stack_overflow()) return kPreParseStackOverflow;
1489 if (!ok) { 1498 if (!ok) {
1490 ReportUnexpectedToken(scanner()->current_token()); 1499 ReportUnexpectedToken(scanner()->current_token());
1491 } else if (scope_->strict_mode() == STRICT) { 1500 } else if (scope_->strict_mode() == STRICT) {
1492 CheckOctalLiteral(start_position, scanner()->location().end_pos, &ok); 1501 CheckStrictOctalLiteral(start_position, scanner()->location().end_pos,
1502 &ok);
1493 } 1503 }
1494 if (materialized_literals) { 1504 if (materialized_literals) {
1495 *materialized_literals = function_state_->materialized_literal_count(); 1505 *materialized_literals = function_state_->materialized_literal_count();
1496 } 1506 }
1497 return kPreParseSuccess; 1507 return kPreParseSuccess;
1498 } 1508 }
1499 1509
1500 // Parses a single function literal, from the opening parentheses before 1510 // Parses a single function literal, from the opening parentheses before
1501 // parameters to the closing brace after the body. 1511 // parameters to the closing brace after the body.
1502 // Returns a FunctionEntry describing the body of the function in enough 1512 // Returns a FunctionEntry describing the body of the function in enough
(...skipping 1292 matching lines...) Expand 10 before | Expand all | Expand 10 after
2795 Scanner::Location function_name_loc = Scanner::Location::invalid(); 2805 Scanner::Location function_name_loc = Scanner::Location::invalid();
2796 Scanner::Location eval_args_error_loc = Scanner::Location::invalid(); 2806 Scanner::Location eval_args_error_loc = Scanner::Location::invalid();
2797 Scanner::Location reserved_loc = Scanner::Location::invalid(); 2807 Scanner::Location reserved_loc = Scanner::Location::invalid();
2798 this->CheckStrictFunctionNameAndParameters( 2808 this->CheckStrictFunctionNameAndParameters(
2799 this->EmptyIdentifier(), function_name_is_strict_reserved, 2809 this->EmptyIdentifier(), function_name_is_strict_reserved,
2800 function_name_loc, eval_args_error_loc, dupe_error_loc, reserved_loc, 2810 function_name_loc, eval_args_error_loc, dupe_error_loc, reserved_loc,
2801 CHECK_OK); 2811 CHECK_OK);
2802 2812
2803 // Validate strict mode. 2813 // Validate strict mode.
2804 if (strict_mode() == STRICT) { 2814 if (strict_mode() == STRICT) {
2805 CheckOctalLiteral(start_pos, scanner()->location().end_pos, CHECK_OK); 2815 CheckStrictOctalLiteral(start_pos, scanner()->location().end_pos,
2816 CHECK_OK);
2806 } 2817 }
2807 2818
2808 if (allow_harmony_scoping() && strict_mode() == STRICT) 2819 if (allow_harmony_scoping() && strict_mode() == STRICT)
2809 this->CheckConflictingVarDeclarations(scope, CHECK_OK); 2820 this->CheckConflictingVarDeclarations(scope, CHECK_OK);
2810 } 2821 }
2811 2822
2812 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( 2823 FunctionLiteralT function_literal = factory()->NewFunctionLiteral(
2813 this->EmptyIdentifierString(), this->ast_value_factory(), scope, body, 2824 this->EmptyIdentifierString(), this->ast_value_factory(), scope, body,
2814 materialized_literal_count, expected_property_count, handler_count, 2825 materialized_literal_count, expected_property_count, handler_count,
2815 num_parameters, FunctionLiteral::kNoDuplicateParameters, 2826 num_parameters, FunctionLiteral::kNoDuplicateParameters,
(...skipping 23 matching lines...) Expand all
2839 // When parsing a TemplateLiteral, we must have scanned either an initial 2850 // When parsing a TemplateLiteral, we must have scanned either an initial
2840 // TEMPLATE_SPAN, or a TEMPLATE_TAIL. 2851 // TEMPLATE_SPAN, or a TEMPLATE_TAIL.
2841 CHECK(peek() == Token::TEMPLATE_SPAN || peek() == Token::TEMPLATE_TAIL); 2852 CHECK(peek() == Token::TEMPLATE_SPAN || peek() == Token::TEMPLATE_TAIL);
2842 2853
2843 // If we reach a TEMPLATE_TAIL first, we are parsing a NoSubstitutionTemplate. 2854 // If we reach a TEMPLATE_TAIL first, we are parsing a NoSubstitutionTemplate.
2844 // In this case we may simply consume the token and build a template with a 2855 // In this case we may simply consume the token and build a template with a
2845 // single TEMPLATE_SPAN and no expressions. 2856 // single TEMPLATE_SPAN and no expressions.
2846 if (peek() == Token::TEMPLATE_TAIL) { 2857 if (peek() == Token::TEMPLATE_TAIL) {
2847 Consume(Token::TEMPLATE_TAIL); 2858 Consume(Token::TEMPLATE_TAIL);
2848 int pos = position(); 2859 int pos = position();
2860 CheckTemplateOctalLiteral(pos, peek_position(), CHECK_OK);
2849 typename Traits::TemplateLiteralState ts = Traits::OpenTemplateLiteral(pos); 2861 typename Traits::TemplateLiteralState ts = Traits::OpenTemplateLiteral(pos);
2850 Traits::AddTemplateSpan(&ts, true); 2862 Traits::AddTemplateSpan(&ts, true);
2851 return Traits::CloseTemplateLiteral(&ts, start, tag); 2863 return Traits::CloseTemplateLiteral(&ts, start, tag);
2852 } 2864 }
2853 2865
2854 Consume(Token::TEMPLATE_SPAN); 2866 Consume(Token::TEMPLATE_SPAN);
2855 int pos = position(); 2867 int pos = position();
2856 typename Traits::TemplateLiteralState ts = Traits::OpenTemplateLiteral(pos); 2868 typename Traits::TemplateLiteralState ts = Traits::OpenTemplateLiteral(pos);
2857 Traits::AddTemplateSpan(&ts, false); 2869 Traits::AddTemplateSpan(&ts, false);
2858 Token::Value next; 2870 Token::Value next;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2890 ReportMessageAt(Scanner::Location(start, position()), 2902 ReportMessageAt(Scanner::Location(start, position()),
2891 "unterminated_template"); 2903 "unterminated_template");
2892 *ok = false; 2904 *ok = false;
2893 return Traits::EmptyExpression(); 2905 return Traits::EmptyExpression();
2894 } 2906 }
2895 2907
2896 Traits::AddTemplateSpan(&ts, next == Token::TEMPLATE_TAIL); 2908 Traits::AddTemplateSpan(&ts, next == Token::TEMPLATE_TAIL);
2897 } while (next == Token::TEMPLATE_SPAN); 2909 } while (next == Token::TEMPLATE_SPAN);
2898 2910
2899 DCHECK_EQ(next, Token::TEMPLATE_TAIL); 2911 DCHECK_EQ(next, Token::TEMPLATE_TAIL);
2912 CheckTemplateOctalLiteral(pos, peek_position(), CHECK_OK);
2900 // Once we've reached a TEMPLATE_TAIL, we can close the TemplateLiteral. 2913 // Once we've reached a TEMPLATE_TAIL, we can close the TemplateLiteral.
2901 return Traits::CloseTemplateLiteral(&ts, start, tag); 2914 return Traits::CloseTemplateLiteral(&ts, start, tag);
2902 } 2915 }
2903 2916
2904 2917
2905 template <typename Traits> 2918 template <typename Traits>
2906 typename ParserBase<Traits>::ExpressionT ParserBase< 2919 typename ParserBase<Traits>::ExpressionT ParserBase<
2907 Traits>::CheckAndRewriteReferenceExpression(ExpressionT expression, 2920 Traits>::CheckAndRewriteReferenceExpression(ExpressionT expression,
2908 Scanner::Location location, 2921 Scanner::Location location,
2909 const char* message, bool* ok) { 2922 const char* message, bool* ok) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
2954 DCHECK(IsAccessorAccessorConflict(old_type, type)); 2967 DCHECK(IsAccessorAccessorConflict(old_type, type));
2955 // Both accessors of the same type. 2968 // Both accessors of the same type.
2956 parser()->ReportMessage("accessor_get_set"); 2969 parser()->ReportMessage("accessor_get_set");
2957 } 2970 }
2958 *ok = false; 2971 *ok = false;
2959 } 2972 }
2960 } 2973 }
2961 } } // v8::internal 2974 } } // v8::internal
2962 2975
2963 #endif // V8_PREPARSER_H 2976 #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