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

Side by Side Diff: src/preparser.cc

Issue 918203002: Get rid of PreParserIdentifier. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: . Created 5 years, 10 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
« no previous file with comments | « src/preparser.h ('k') | src/scanner.h » ('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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 #include <cmath> 5 #include <cmath>
6 6
7 #include "src/allocation.h" 7 #include "src/allocation.h"
8 #include "src/base/logging.h" 8 #include "src/base/logging.h"
9 #include "src/conversions-inl.h" 9 #include "src/conversions-inl.h"
10 #include "src/conversions.h" 10 #include "src/conversions.h"
(...skipping 24 matching lines...) Expand all
35 void PreParserTraits::ReportMessageAt(int start_pos, 35 void PreParserTraits::ReportMessageAt(int start_pos,
36 int end_pos, 36 int end_pos,
37 const char* message, 37 const char* message,
38 const char* arg, 38 const char* arg,
39 bool is_reference_error) { 39 bool is_reference_error) {
40 pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg, 40 pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg,
41 is_reference_error); 41 is_reference_error);
42 } 42 }
43 43
44 44
45 PreParserIdentifier PreParserTraits::GetSymbol(Scanner* scanner) { 45 PreParserExpression PreParserTraits::ExpressionFromIdentifier(
46 if (scanner->current_token() == Token::FUTURE_RESERVED_WORD) { 46 const AstRawString* name, int pos, Scope* scope,
47 return PreParserIdentifier::FutureReserved(); 47 PreParserFactory* factory) {
48 } else if (scanner->current_token() == 48 bool is_valid_arrow_function_param =
49 Token::FUTURE_STRICT_RESERVED_WORD) { 49 !(pre_parser_->IsEvalOrArguments(name) ||
50 return PreParserIdentifier::FutureStrictReserved(); 50 pre_parser_->scanner()->IdentifierIsFutureStrictReserved(name));
51 } else if (scanner->current_token() == Token::LET) { 51 return PreParserExpression::FromIdentifier(name,
52 return PreParserIdentifier::Let(); 52 is_valid_arrow_function_param);
53 } else if (scanner->current_token() == Token::STATIC) {
54 return PreParserIdentifier::Static();
55 } else if (scanner->current_token() == Token::YIELD) {
56 return PreParserIdentifier::Yield();
57 }
58 if (scanner->UnescapedLiteralMatches("eval", 4)) {
59 return PreParserIdentifier::Eval();
60 }
61 if (scanner->UnescapedLiteralMatches("arguments", 9)) {
62 return PreParserIdentifier::Arguments();
63 }
64 if (scanner->LiteralMatches("prototype", 9)) {
65 return PreParserIdentifier::Prototype();
66 }
67 if (scanner->LiteralMatches("constructor", 11)) {
68 return PreParserIdentifier::Constructor();
69 }
70 return PreParserIdentifier::Default();
71 } 53 }
72 54
73 55
74 PreParserIdentifier PreParserTraits::GetNumberAsSymbol(Scanner* scanner) {
75 return PreParserIdentifier::Default();
76 }
77
78
79 PreParserExpression PreParserTraits::ExpressionFromString( 56 PreParserExpression PreParserTraits::ExpressionFromString(
80 int pos, Scanner* scanner, PreParserFactory* factory) { 57 int pos, Scanner* scanner, PreParserFactory* factory) {
81 if (scanner->UnescapedLiteralMatches("use strict", 10)) { 58 if (scanner->UnescapedLiteralMatches("use strict", 10)) {
82 return PreParserExpression::UseStrictStringLiteral(); 59 return PreParserExpression::UseStrictStringLiteral();
83 } else if (scanner->UnescapedLiteralMatches("use strong", 10)) { 60 } else if (scanner->UnescapedLiteralMatches("use strong", 10)) {
84 return PreParserExpression::UseStrongStringLiteral(); 61 return PreParserExpression::UseStrongStringLiteral();
85 } 62 }
86 return PreParserExpression::StringLiteral(); 63 return PreParserExpression::StringLiteral();
87 } 64 }
88 65
89 66
90 PreParserExpression PreParserTraits::ParseV8Intrinsic(bool* ok) { 67 PreParserExpression PreParserTraits::ParseV8Intrinsic(bool* ok) {
91 return pre_parser_->ParseV8Intrinsic(ok); 68 return pre_parser_->ParseV8Intrinsic(ok);
92 } 69 }
93 70
94 71
95 PreParserExpression PreParserTraits::ParseFunctionLiteral( 72 PreParserExpression PreParserTraits::ParseFunctionLiteral(
96 PreParserIdentifier name, Scanner::Location function_name_location, 73 const AstRawString* name, Scanner::Location function_name_location,
97 bool name_is_strict_reserved, FunctionKind kind, 74 bool name_is_strict_reserved, FunctionKind kind,
98 int function_token_position, FunctionLiteral::FunctionType type, 75 int function_token_position, FunctionLiteral::FunctionType type,
99 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { 76 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
100 return pre_parser_->ParseFunctionLiteral( 77 return pre_parser_->ParseFunctionLiteral(
101 name, function_name_location, name_is_strict_reserved, kind, 78 name, function_name_location, name_is_strict_reserved, kind,
102 function_token_position, type, arity_restriction, ok); 79 function_token_position, type, arity_restriction, ok);
103 } 80 }
104 81
105 82
106 PreParser::PreParseResult PreParser::PreParseLazyFunction( 83 PreParser::PreParseResult PreParser::PreParseLazyFunction(
(...skipping 21 matching lines...) Expand all
128 if (is_strict(scope_->language_mode())) { 105 if (is_strict(scope_->language_mode())) {
129 int end_pos = scanner()->location().end_pos; 106 int end_pos = scanner()->location().end_pos;
130 CheckStrictOctalLiteral(start_position, end_pos, &ok); 107 CheckStrictOctalLiteral(start_position, end_pos, &ok);
131 } 108 }
132 } 109 }
133 return kPreParseSuccess; 110 return kPreParseSuccess;
134 } 111 }
135 112
136 113
137 PreParserExpression PreParserTraits::ParseClassLiteral( 114 PreParserExpression PreParserTraits::ParseClassLiteral(
138 PreParserIdentifier name, Scanner::Location class_name_location, 115 const AstRawString* name, Scanner::Location class_name_location,
139 bool name_is_strict_reserved, int pos, bool* ok) { 116 bool name_is_strict_reserved, int pos, bool* ok) {
140 return pre_parser_->ParseClassLiteral(name, class_name_location, 117 return pre_parser_->ParseClassLiteral(name, class_name_location,
141 name_is_strict_reserved, pos, ok); 118 name_is_strict_reserved, pos, ok);
142 } 119 }
143 120
144 121
145 // Preparsing checks a JavaScript program and emits preparse-data that helps 122 // Preparsing checks a JavaScript program and emits preparse-data that helps
146 // a later parsing to be faster. 123 // a later parsing to be faster.
147 // See preparser-data.h for the data. 124 // See preparser-data.h for the data.
148 125
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) { 309 PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
333 // FunctionDeclaration :: 310 // FunctionDeclaration ::
334 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' 311 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
335 // GeneratorDeclaration :: 312 // GeneratorDeclaration ::
336 // 'function' '*' Identifier '(' FormalParameterListopt ')' 313 // 'function' '*' Identifier '(' FormalParameterListopt ')'
337 // '{' FunctionBody '}' 314 // '{' FunctionBody '}'
338 Expect(Token::FUNCTION, CHECK_OK); 315 Expect(Token::FUNCTION, CHECK_OK);
339 int pos = position(); 316 int pos = position();
340 bool is_generator = Check(Token::MUL); 317 bool is_generator = Check(Token::MUL);
341 bool is_strict_reserved = false; 318 bool is_strict_reserved = false;
342 Identifier name = ParseIdentifierOrStrictReservedWord( 319 const AstRawString* name =
343 &is_strict_reserved, CHECK_OK); 320 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
344 ParseFunctionLiteral(name, scanner()->location(), is_strict_reserved, 321 ParseFunctionLiteral(name, scanner()->location(), is_strict_reserved,
345 is_generator ? FunctionKind::kGeneratorFunction 322 is_generator ? FunctionKind::kGeneratorFunction
346 : FunctionKind::kNormalFunction, 323 : FunctionKind::kNormalFunction,
347 pos, FunctionLiteral::DECLARATION, 324 pos, FunctionLiteral::DECLARATION,
348 FunctionLiteral::NORMAL_ARITY, CHECK_OK); 325 FunctionLiteral::NORMAL_ARITY, CHECK_OK);
349 return Statement::FunctionDeclaration(); 326 return Statement::FunctionDeclaration();
350 } 327 }
351 328
352 329
353 PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) { 330 PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) {
354 Expect(Token::CLASS, CHECK_OK); 331 Expect(Token::CLASS, CHECK_OK);
355 if (!allow_harmony_sloppy() && is_sloppy(language_mode())) { 332 if (!allow_harmony_sloppy() && is_sloppy(language_mode())) {
356 ReportMessage("sloppy_lexical"); 333 ReportMessage("sloppy_lexical");
357 *ok = false; 334 *ok = false;
358 return Statement::Default(); 335 return Statement::Default();
359 } 336 }
360 337
361 int pos = position(); 338 int pos = position();
362 bool is_strict_reserved = false; 339 bool is_strict_reserved = false;
363 Identifier name = 340 const AstRawString* name =
364 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); 341 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
365 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos, 342 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos,
366 CHECK_OK); 343 CHECK_OK);
367 return Statement::Default(); 344 return Statement::Default();
368 } 345 }
369 346
370 347
371 PreParser::Statement PreParser::ParseBlock(bool* ok) { 348 PreParser::Statement PreParser::ParseBlock(bool* ok) {
372 // Block :: 349 // Block ::
373 // '{' Statement* '}' 350 // '{' Statement* '}'
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 } 484 }
508 485
509 bool starts_with_identifier = peek_any_identifier(); 486 bool starts_with_identifier = peek_any_identifier();
510 Expression expr = ParseExpression(true, CHECK_OK); 487 Expression expr = ParseExpression(true, CHECK_OK);
511 // Even if the expression starts with an identifier, it is not necessarily an 488 // Even if the expression starts with an identifier, it is not necessarily an
512 // identifier. For example, "foo + bar" starts with an identifier but is not 489 // identifier. For example, "foo + bar" starts with an identifier but is not
513 // an identifier. 490 // an identifier.
514 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) { 491 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) {
515 // Expression is a single identifier, and not, e.g., a parenthesized 492 // Expression is a single identifier, and not, e.g., a parenthesized
516 // identifier. 493 // identifier.
517 DCHECK(!expr.AsIdentifier().IsFutureReserved()); 494 DCHECK(!scanner()->IdentifierIsFutureReserved(expr.AsIdentifier()));
518 DCHECK(is_sloppy(language_mode()) || 495 DCHECK(is_sloppy(language_mode()) ||
519 !IsFutureStrictReserved(expr.AsIdentifier())); 496 !scanner()->IdentifierIsFutureStrictReserved(expr.AsIdentifier()));
520 Consume(Token::COLON); 497 Consume(Token::COLON);
521 return ParseStatement(ok); 498 return ParseStatement(ok);
522 // Preparsing is disabled for extensions (because the extension details 499 // Preparsing is disabled for extensions (because the extension details
523 // aren't passed to lazily compiled functions), so we don't 500 // aren't passed to lazily compiled functions), so we don't
524 // accept "native function" in the preparser. 501 // accept "native function" in the preparser.
525 } 502 }
526 // Parsed expression statement. 503 // Parsed expression statement.
527 // Detect attempts at 'let' declarations in sloppy mode. 504 // Detect attempts at 'let' declarations in sloppy mode.
528 if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) && 505 if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) &&
529 expr.IsIdentifier() && expr.AsIdentifier().IsLet()) { 506 expr.IsIdentifier() && expr.AsIdentifier()->IsOneByteEqualTo("let")) {
530 ReportMessage("sloppy_lexical", NULL); 507 ReportMessage("sloppy_lexical", NULL);
531 *ok = false; 508 *ok = false;
532 return Statement::Default(); 509 return Statement::Default();
533 } 510 }
534 ExpectSemicolon(CHECK_OK); 511 ExpectSemicolon(CHECK_OK);
535 return Statement::ExpressionStatement(expr); 512 return Statement::ExpressionStatement(expr);
536 } 513 }
537 514
538 515
539 PreParser::Statement PreParser::ParseIfStatement(bool* ok) { 516 PreParser::Statement PreParser::ParseIfStatement(bool* ok) {
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 if (accept_IN && CheckInOrOf(accept_OF)) { 703 if (accept_IN && CheckInOrOf(accept_OF)) {
727 ParseExpression(true, CHECK_OK); 704 ParseExpression(true, CHECK_OK);
728 Expect(Token::RPAREN, CHECK_OK); 705 Expect(Token::RPAREN, CHECK_OK);
729 706
730 ParseStatement(CHECK_OK); 707 ParseStatement(CHECK_OK);
731 return Statement::Default(); 708 return Statement::Default();
732 } 709 }
733 } else { 710 } else {
734 Expression lhs = ParseExpression(false, CHECK_OK); 711 Expression lhs = ParseExpression(false, CHECK_OK);
735 is_let_identifier_expression = 712 is_let_identifier_expression =
736 lhs.IsIdentifier() && lhs.AsIdentifier().IsLet(); 713 lhs.IsIdentifier() && lhs.AsIdentifier()->IsOneByteEqualTo("let");
737 if (CheckInOrOf(lhs.IsIdentifier())) { 714 if (CheckInOrOf(lhs.IsIdentifier())) {
738 ParseExpression(true, CHECK_OK); 715 ParseExpression(true, CHECK_OK);
739 Expect(Token::RPAREN, CHECK_OK); 716 Expect(Token::RPAREN, CHECK_OK);
740 717
741 ParseStatement(CHECK_OK); 718 ParseStatement(CHECK_OK);
742 return Statement::Default(); 719 return Statement::Default();
743 } 720 }
744 } 721 }
745 } 722 }
746 723
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 819
843 #undef CHECK_OK 820 #undef CHECK_OK
844 #define CHECK_OK ok); \ 821 #define CHECK_OK ok); \
845 if (!*ok) return Expression::Default(); \ 822 if (!*ok) return Expression::Default(); \
846 ((void)0 823 ((void)0
847 #define DUMMY ) // to make indentation work 824 #define DUMMY ) // to make indentation work
848 #undef DUMMY 825 #undef DUMMY
849 826
850 827
851 PreParser::Expression PreParser::ParseFunctionLiteral( 828 PreParser::Expression PreParser::ParseFunctionLiteral(
852 Identifier function_name, Scanner::Location function_name_location, 829 const AstRawString* function_name, Scanner::Location function_name_location,
853 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos, 830 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
854 FunctionLiteral::FunctionType function_type, 831 FunctionLiteral::FunctionType function_type,
855 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { 832 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
856 // Function :: 833 // Function ::
857 // '(' FormalParameterList? ')' '{' FunctionBody '}' 834 // '(' FormalParameterList? ')' '{' FunctionBody '}'
858 835
859 // Parse function body. 836 // Parse function body.
860 bool outer_is_script_scope = scope_->is_script_scope(); 837 bool outer_is_script_scope = scope_->is_script_scope();
861 Scope* function_scope = NewScope(scope_, FUNCTION_SCOPE); 838 Scope* function_scope = NewScope(scope_, FUNCTION_SCOPE);
862 PreParserFactory factory(NULL); 839 PreParserFactory factory(NULL);
(...skipping 15 matching lines...) Expand all
878 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY || 855 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY ||
879 (peek() == Token::RPAREN && 856 (peek() == Token::RPAREN &&
880 arity_restriction != FunctionLiteral::SETTER_ARITY); 857 arity_restriction != FunctionLiteral::SETTER_ARITY);
881 while (!done) { 858 while (!done) {
882 bool is_strict_reserved = false; 859 bool is_strict_reserved = false;
883 is_rest = peek() == Token::ELLIPSIS && allow_harmony_rest_params(); 860 is_rest = peek() == Token::ELLIPSIS && allow_harmony_rest_params();
884 if (is_rest) { 861 if (is_rest) {
885 Consume(Token::ELLIPSIS); 862 Consume(Token::ELLIPSIS);
886 } 863 }
887 864
888 Identifier param_name = 865 const AstRawString* param_name =
889 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); 866 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
890 if (!eval_args_error_loc.IsValid() && param_name.IsEvalOrArguments()) { 867 if (!eval_args_error_loc.IsValid() && IsEvalOrArguments(param_name)) {
891 eval_args_error_loc = scanner()->location(); 868 eval_args_error_loc = scanner()->location();
892 } 869 }
893 if (!reserved_error_loc.IsValid() && is_strict_reserved) { 870 if (!reserved_error_loc.IsValid() && is_strict_reserved) {
894 reserved_error_loc = scanner()->location(); 871 reserved_error_loc = scanner()->location();
895 } 872 }
896 873
897 int prev_value = scanner()->FindSymbol(&duplicate_finder, 1); 874 int prev_value = scanner()->FindSymbol(&duplicate_finder, 1);
898 875
899 if (!dupe_error_loc.IsValid() && prev_value != 0) { 876 if (!dupe_error_loc.IsValid() && prev_value != 0) {
900 dupe_error_loc = scanner()->location(); 877 dupe_error_loc = scanner()->location();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 // Position right after terminal '}'. 930 // Position right after terminal '}'.
954 DCHECK_EQ(Token::RBRACE, scanner()->peek()); 931 DCHECK_EQ(Token::RBRACE, scanner()->peek());
955 int body_end = scanner()->peek_location().end_pos; 932 int body_end = scanner()->peek_location().end_pos;
956 log_->LogFunction( 933 log_->LogFunction(
957 body_start, body_end, function_state_->materialized_literal_count(), 934 body_start, body_end, function_state_->materialized_literal_count(),
958 function_state_->expected_property_count(), language_mode()); 935 function_state_->expected_property_count(), language_mode());
959 } 936 }
960 937
961 938
962 PreParserExpression PreParser::ParseClassLiteral( 939 PreParserExpression PreParser::ParseClassLiteral(
963 PreParserIdentifier name, Scanner::Location class_name_location, 940 const AstRawString* name, Scanner::Location class_name_location,
964 bool name_is_strict_reserved, int pos, bool* ok) { 941 bool name_is_strict_reserved, int pos, bool* ok) {
965 // All parts of a ClassDeclaration and ClassExpression are strict code. 942 // All parts of a ClassDeclaration and ClassExpression are strict code.
966 if (name_is_strict_reserved) { 943 if (name_is_strict_reserved) {
967 ReportMessageAt(class_name_location, "unexpected_strict_reserved"); 944 ReportMessageAt(class_name_location, "unexpected_strict_reserved");
968 *ok = false; 945 *ok = false;
969 return EmptyExpression(); 946 return EmptyExpression();
970 } 947 }
971 if (IsEvalOrArguments(name)) { 948 if (IsEvalOrArguments(name)) {
972 ReportMessageAt(class_name_location, "strict_eval_arguments"); 949 ReportMessageAt(class_name_location, "strict_eval_arguments");
973 *ok = false; 950 *ok = false;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); 995 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
1019 ParseArguments(ok); 996 ParseArguments(ok);
1020 997
1021 return Expression::Default(); 998 return Expression::Default();
1022 } 999 }
1023 1000
1024 #undef CHECK_OK 1001 #undef CHECK_OK
1025 1002
1026 1003
1027 } } // v8::internal 1004 } } // v8::internal
OLDNEW
« no previous file with comments | « src/preparser.h ('k') | src/scanner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698