| OLD | NEW |
| 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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 // The PreParser checks that the syntax follows the grammar for JavaScript, | 149 // The PreParser checks that the syntax follows the grammar for JavaScript, |
| 150 // and collects some information about the program along the way. | 150 // and collects some information about the program along the way. |
| 151 // The grammar check is only performed in order to understand the program | 151 // The grammar check is only performed in order to understand the program |
| 152 // sufficiently to deduce some information about it, that can be used | 152 // sufficiently to deduce some information about it, that can be used |
| 153 // to speed up later parsing. Finding errors is not the goal of pre-parsing, | 153 // to speed up later parsing. Finding errors is not the goal of pre-parsing, |
| 154 // rather it is to speed up properly written and correct programs. | 154 // rather it is to speed up properly written and correct programs. |
| 155 // That means that contextual checks (like a label being declared where | 155 // That means that contextual checks (like a label being declared where |
| 156 // it is used) are generally omitted. | 156 // it is used) are generally omitted. |
| 157 | 157 |
| 158 | 158 |
| 159 #define CHECK_OK ok); \ | 159 PreParser::Statement PreParser::ParseStatementListItem(bool* ok) { |
| 160 if (!*ok) return kUnknownSourceElements; \ | |
| 161 ((void)0 | |
| 162 #define DUMMY ) // to make indentation work | |
| 163 #undef DUMMY | |
| 164 | |
| 165 | |
| 166 PreParser::Statement PreParser::ParseSourceElement(bool* ok) { | |
| 167 // ECMA 262 6th Edition | 160 // ECMA 262 6th Edition |
| 168 // StatementListItem[Yield, Return] : | 161 // StatementListItem[Yield, Return] : |
| 169 // Statement[?Yield, ?Return] | 162 // Statement[?Yield, ?Return] |
| 170 // Declaration[?Yield] | 163 // Declaration[?Yield] |
| 171 // | 164 // |
| 172 // Declaration[Yield] : | 165 // Declaration[Yield] : |
| 173 // HoistableDeclaration[?Yield] | 166 // HoistableDeclaration[?Yield] |
| 174 // ClassDeclaration[?Yield] | 167 // ClassDeclaration[?Yield] |
| 175 // LexicalDeclaration[In, ?Yield] | 168 // LexicalDeclaration[In, ?Yield] |
| 176 // | 169 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 193 if (is_strict(language_mode())) { | 186 if (is_strict(language_mode())) { |
| 194 return ParseVariableStatement(kSourceElement, ok); | 187 return ParseVariableStatement(kSourceElement, ok); |
| 195 } | 188 } |
| 196 // Fall through. | 189 // Fall through. |
| 197 default: | 190 default: |
| 198 return ParseStatement(ok); | 191 return ParseStatement(ok); |
| 199 } | 192 } |
| 200 } | 193 } |
| 201 | 194 |
| 202 | 195 |
| 203 PreParser::SourceElements PreParser::ParseSourceElements(int end_token, | 196 void PreParser::ParseStatementList(int end_token, bool* ok) { |
| 204 bool* ok) { | |
| 205 // SourceElements :: | 197 // SourceElements :: |
| 206 // (Statement)* <end_token> | 198 // (Statement)* <end_token> |
| 207 | 199 |
| 208 bool directive_prologue = true; | 200 bool directive_prologue = true; |
| 209 while (peek() != end_token) { | 201 while (peek() != end_token) { |
| 210 if (directive_prologue && peek() != Token::STRING) { | 202 if (directive_prologue && peek() != Token::STRING) { |
| 211 directive_prologue = false; | 203 directive_prologue = false; |
| 212 } | 204 } |
| 213 Statement statement = ParseSourceElement(CHECK_OK); | 205 Statement statement = ParseStatementListItem(ok); |
| 206 if (!*ok) return; |
| 214 if (directive_prologue) { | 207 if (directive_prologue) { |
| 215 if (statement.IsUseStrictLiteral()) { | 208 if (statement.IsUseStrictLiteral()) { |
| 216 scope_->SetLanguageMode( | 209 scope_->SetLanguageMode( |
| 217 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT)); | 210 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT)); |
| 218 } else if (statement.IsUseStrongLiteral() && allow_strong_mode()) { | 211 } else if (statement.IsUseStrongLiteral() && allow_strong_mode()) { |
| 219 scope_->SetLanguageMode(static_cast<LanguageMode>( | 212 scope_->SetLanguageMode(static_cast<LanguageMode>( |
| 220 scope_->language_mode() | STRICT_BIT | STRONG_BIT)); | 213 scope_->language_mode() | STRICT_BIT | STRONG_BIT)); |
| 221 } else if (!statement.IsStringLiteral()) { | 214 } else if (!statement.IsStringLiteral()) { |
| 222 directive_prologue = false; | 215 directive_prologue = false; |
| 223 } | 216 } |
| 224 } | 217 } |
| 225 } | 218 } |
| 226 return kUnknownSourceElements; | |
| 227 } | 219 } |
| 228 | 220 |
| 229 | 221 |
| 230 #undef CHECK_OK | |
| 231 #define CHECK_OK ok); \ | 222 #define CHECK_OK ok); \ |
| 232 if (!*ok) return Statement::Default(); \ | 223 if (!*ok) return Statement::Default(); \ |
| 233 ((void)0 | 224 ((void)0 |
| 234 #define DUMMY ) // to make indentation work | 225 #define DUMMY ) // to make indentation work |
| 235 #undef DUMMY | 226 #undef DUMMY |
| 236 | 227 |
| 237 | 228 |
| 238 PreParser::Statement PreParser::ParseStatement(bool* ok) { | 229 PreParser::Statement PreParser::ParseStatement(bool* ok) { |
| 239 // Statement :: | 230 // Statement :: |
| 240 // Block | 231 // Block |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 PreParser::Statement PreParser::ParseBlock(bool* ok) { | 371 PreParser::Statement PreParser::ParseBlock(bool* ok) { |
| 381 // Block :: | 372 // Block :: |
| 382 // '{' Statement* '}' | 373 // '{' Statement* '}' |
| 383 | 374 |
| 384 // Note that a Block does not introduce a new execution scope! | 375 // Note that a Block does not introduce a new execution scope! |
| 385 // (ECMA-262, 3rd, 12.2) | 376 // (ECMA-262, 3rd, 12.2) |
| 386 // | 377 // |
| 387 Expect(Token::LBRACE, CHECK_OK); | 378 Expect(Token::LBRACE, CHECK_OK); |
| 388 while (peek() != Token::RBRACE) { | 379 while (peek() != Token::RBRACE) { |
| 389 if (allow_harmony_scoping() && is_strict(language_mode())) { | 380 if (allow_harmony_scoping() && is_strict(language_mode())) { |
| 390 ParseSourceElement(CHECK_OK); | 381 ParseStatementListItem(CHECK_OK); |
| 391 } else { | 382 } else { |
| 392 ParseStatement(CHECK_OK); | 383 ParseStatement(CHECK_OK); |
| 393 } | 384 } |
| 394 } | 385 } |
| 395 Expect(Token::RBRACE, ok); | 386 Expect(Token::RBRACE, ok); |
| 396 return Statement::Default(); | 387 return Statement::Default(); |
| 397 } | 388 } |
| 398 | 389 |
| 399 | 390 |
| 400 PreParser::Statement PreParser::ParseVariableStatement( | 391 PreParser::Statement PreParser::ParseVariableStatement( |
| (...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 // See Parser::ParseFunctionLiteral for more information about lazy parsing | 916 // See Parser::ParseFunctionLiteral for more information about lazy parsing |
| 926 // and lazy compilation. | 917 // and lazy compilation. |
| 927 bool is_lazily_parsed = (outer_scope_type == SCRIPT_SCOPE && allow_lazy() && | 918 bool is_lazily_parsed = (outer_scope_type == SCRIPT_SCOPE && allow_lazy() && |
| 928 !parenthesized_function_); | 919 !parenthesized_function_); |
| 929 parenthesized_function_ = false; | 920 parenthesized_function_ = false; |
| 930 | 921 |
| 931 Expect(Token::LBRACE, CHECK_OK); | 922 Expect(Token::LBRACE, CHECK_OK); |
| 932 if (is_lazily_parsed) { | 923 if (is_lazily_parsed) { |
| 933 ParseLazyFunctionLiteralBody(CHECK_OK); | 924 ParseLazyFunctionLiteralBody(CHECK_OK); |
| 934 } else { | 925 } else { |
| 935 ParseSourceElements(Token::RBRACE, ok); | 926 ParseStatementList(Token::RBRACE, CHECK_OK); |
| 936 } | 927 } |
| 937 Expect(Token::RBRACE, CHECK_OK); | 928 Expect(Token::RBRACE, CHECK_OK); |
| 938 | 929 |
| 939 // Validate name and parameter names. We can do this only after parsing the | 930 // Validate name and parameter names. We can do this only after parsing the |
| 940 // function, since the function can declare itself strict. | 931 // function, since the function can declare itself strict. |
| 941 CheckFunctionName(language_mode(), kind, function_name, | 932 CheckFunctionName(language_mode(), kind, function_name, |
| 942 name_is_strict_reserved, function_name_location, CHECK_OK); | 933 name_is_strict_reserved, function_name_location, CHECK_OK); |
| 943 const bool use_strict_params = is_rest || IsConciseMethod(kind); | 934 const bool use_strict_params = is_rest || IsConciseMethod(kind); |
| 944 CheckFunctionParameterNames(language_mode(), use_strict_params, | 935 CheckFunctionParameterNames(language_mode(), use_strict_params, |
| 945 eval_args_error_loc, dupe_error_loc, | 936 eval_args_error_loc, dupe_error_loc, |
| 946 reserved_error_loc, CHECK_OK); | 937 reserved_error_loc, CHECK_OK); |
| 947 | 938 |
| 948 if (is_strict(language_mode())) { | 939 if (is_strict(language_mode())) { |
| 949 int end_position = scanner()->location().end_pos; | 940 int end_position = scanner()->location().end_pos; |
| 950 CheckStrictOctalLiteral(start_position, end_position, CHECK_OK); | 941 CheckStrictOctalLiteral(start_position, end_position, CHECK_OK); |
| 951 } | 942 } |
| 952 | 943 |
| 953 return Expression::Default(); | 944 return Expression::Default(); |
| 954 } | 945 } |
| 955 | 946 |
| 956 | 947 |
| 957 void PreParser::ParseLazyFunctionLiteralBody(bool* ok) { | 948 void PreParser::ParseLazyFunctionLiteralBody(bool* ok) { |
| 958 int body_start = position(); | 949 int body_start = position(); |
| 959 ParseSourceElements(Token::RBRACE, ok); | 950 ParseStatementList(Token::RBRACE, ok); |
| 960 if (!*ok) return; | 951 if (!*ok) return; |
| 961 | 952 |
| 962 // Position right after terminal '}'. | 953 // Position right after terminal '}'. |
| 963 DCHECK_EQ(Token::RBRACE, scanner()->peek()); | 954 DCHECK_EQ(Token::RBRACE, scanner()->peek()); |
| 964 int body_end = scanner()->peek_location().end_pos; | 955 int body_end = scanner()->peek_location().end_pos; |
| 965 log_->LogFunction( | 956 log_->LogFunction( |
| 966 body_start, body_end, function_state_->materialized_literal_count(), | 957 body_start, body_end, function_state_->materialized_literal_count(), |
| 967 function_state_->expected_property_count(), language_mode()); | 958 function_state_->expected_property_count(), language_mode()); |
| 968 } | 959 } |
| 969 | 960 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1026 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); | 1017 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); |
| 1027 ParseArguments(ok); | 1018 ParseArguments(ok); |
| 1028 | 1019 |
| 1029 return Expression::Default(); | 1020 return Expression::Default(); |
| 1030 } | 1021 } |
| 1031 | 1022 |
| 1032 #undef CHECK_OK | 1023 #undef CHECK_OK |
| 1033 | 1024 |
| 1034 | 1025 |
| 1035 } } // v8::internal | 1026 } } // v8::internal |
| OLD | NEW |