ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 28 | #include <cmath> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 29 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 30 | #include "../include/v8stdint.h" |
| 31 | |
| 32 | #include "allocation.h" |
| 33 | #include "checks.h" |
| 34 | #include "conversions.h" |
| 35 | #include "conversions-inl.h" |
| 36 | #include "globals.h" |
| 37 | #include "hashmap.h" |
| 38 | #include "list.h" |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 39 | #include "preparse-data-format.h" |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 40 | #include "preparse-data.h" |
| 41 | #include "preparser.h" |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 42 | #include "unicode.h" |
| 43 | #include "utils.h" |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 44 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 45 | #ifdef _MSC_VER |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 46 | namespace std { |
| 47 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 48 | // Usually defined in math.h, but not in MSVC. |
| 49 | // Abstracted to work |
| 50 | int isfinite(double value); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 51 | |
| 52 | } // namespace std |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 55 | namespace v8 { |
| 56 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 57 | namespace preparser { |
| 58 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 59 | PreParser::PreParseResult PreParser::PreParseLazyFunction( |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 60 | i::LanguageMode mode, bool is_generator, i::ParserRecorder* log) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 61 | log_ = log; |
| 62 | // Lazy functions always have trivial outer scopes (no with/catch scopes). |
| 63 | Scope top_scope(&scope_, kTopLevelScope); |
| 64 | set_language_mode(mode); |
| 65 | Scope function_scope(&scope_, kFunctionScope); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 66 | function_scope.set_is_generator(is_generator); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 67 | ASSERT_EQ(i::Token::LBRACE, scanner_->current_token()); |
| 68 | bool ok = true; |
| 69 | int start_position = scanner_->peek_location().beg_pos; |
| 70 | ParseLazyFunctionLiteralBody(&ok); |
| 71 | if (stack_overflow_) return kPreParseStackOverflow; |
| 72 | if (!ok) { |
| 73 | ReportUnexpectedToken(scanner_->current_token()); |
| 74 | } else { |
| 75 | ASSERT_EQ(i::Token::RBRACE, scanner_->peek()); |
| 76 | if (!is_classic_mode()) { |
| 77 | int end_pos = scanner_->location().end_pos; |
| 78 | CheckOctalLiteral(start_position, end_pos, &ok); |
| 79 | if (ok) { |
| 80 | CheckDelayedStrictModeViolation(start_position, end_pos, &ok); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return kPreParseSuccess; |
| 85 | } |
| 86 | |
| 87 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 88 | // Preparsing checks a JavaScript program and emits preparse-data that helps |
| 89 | // a later parsing to be faster. |
| 90 | // See preparser-data.h for the data. |
| 91 | |
| 92 | // The PreParser checks that the syntax follows the grammar for JavaScript, |
| 93 | // and collects some information about the program along the way. |
| 94 | // The grammar check is only performed in order to understand the program |
| 95 | // sufficiently to deduce some information about it, that can be used |
| 96 | // to speed up later parsing. Finding errors is not the goal of pre-parsing, |
| 97 | // rather it is to speed up properly written and correct programs. |
| 98 | // That means that contextual checks (like a label being declared where |
| 99 | // it is used) are generally omitted. |
| 100 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 101 | void PreParser::ReportUnexpectedToken(i::Token::Value token) { |
| 102 | // We don't report stack overflows here, to avoid increasing the |
| 103 | // stack depth even further. Instead we report it after parsing is |
| 104 | // over, in ParseProgram. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 105 | if (token == i::Token::ILLEGAL && stack_overflow_) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 108 | i::Scanner::Location source_location = scanner_->location(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 109 | |
| 110 | // Four of the tokens are treated specially |
| 111 | switch (token) { |
| 112 | case i::Token::EOS: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 113 | return ReportMessageAt(source_location, "unexpected_eos", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 114 | case i::Token::NUMBER: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 115 | return ReportMessageAt(source_location, "unexpected_token_number", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 116 | case i::Token::STRING: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 117 | return ReportMessageAt(source_location, "unexpected_token_string", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 118 | case i::Token::IDENTIFIER: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 119 | return ReportMessageAt(source_location, |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 120 | "unexpected_token_identifier", NULL); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 121 | case i::Token::FUTURE_RESERVED_WORD: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 122 | return ReportMessageAt(source_location, "unexpected_reserved", NULL); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 123 | case i::Token::FUTURE_STRICT_RESERVED_WORD: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 124 | return ReportMessageAt(source_location, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 125 | "unexpected_strict_reserved", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 126 | default: |
| 127 | const char* name = i::Token::String(token); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 128 | ReportMessageAt(source_location, "unexpected_token", name); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 133 | // Checks whether octal literal last seen is between beg_pos and end_pos. |
| 134 | // If so, reports an error. |
| 135 | void PreParser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) { |
| 136 | i::Scanner::Location octal = scanner_->octal_position(); |
| 137 | if (beg_pos <= octal.beg_pos && octal.end_pos <= end_pos) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 138 | ReportMessageAt(octal, "strict_octal_literal", NULL); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 139 | scanner_->clear_octal_position(); |
| 140 | *ok = false; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 145 | #define CHECK_OK ok); \ |
| 146 | if (!*ok) return kUnknownSourceElements; \ |
| 147 | ((void)0 |
| 148 | #define DUMMY ) // to make indentation work |
| 149 | #undef DUMMY |
| 150 | |
| 151 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 152 | PreParser::Statement PreParser::ParseSourceElement(bool* ok) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 153 | // (Ecma 262 5th Edition, clause 14): |
| 154 | // SourceElement: |
| 155 | // Statement |
| 156 | // FunctionDeclaration |
| 157 | // |
| 158 | // In harmony mode we allow additionally the following productions |
| 159 | // SourceElement: |
| 160 | // LetDeclaration |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 161 | // ConstDeclaration |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 162 | // GeneratorDeclaration |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 163 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 164 | switch (peek()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 165 | case i::Token::FUNCTION: |
| 166 | return ParseFunctionDeclaration(ok); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 167 | case i::Token::LET: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 168 | case i::Token::CONST: |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 169 | return ParseVariableStatement(kSourceElement, ok); |
| 170 | default: |
| 171 | return ParseStatement(ok); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 176 | PreParser::SourceElements PreParser::ParseSourceElements(int end_token, |
| 177 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 178 | // SourceElements :: |
| 179 | // (Statement)* <end_token> |
| 180 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 181 | bool allow_directive_prologue = true; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 182 | while (peek() != end_token) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 183 | Statement statement = ParseSourceElement(CHECK_OK); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 184 | if (allow_directive_prologue) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 185 | if (statement.IsUseStrictLiteral()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 186 | set_language_mode(allow_harmony_scoping() ? |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 187 | i::EXTENDED_MODE : i::STRICT_MODE); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 188 | } else if (!statement.IsStringLiteral()) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 189 | allow_directive_prologue = false; |
| 190 | } |
| 191 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 192 | } |
| 193 | return kUnknownSourceElements; |
| 194 | } |
| 195 | |
| 196 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 197 | #undef CHECK_OK |
| 198 | #define CHECK_OK ok); \ |
| 199 | if (!*ok) return Statement::Default(); \ |
| 200 | ((void)0 |
| 201 | #define DUMMY ) // to make indentation work |
| 202 | #undef DUMMY |
| 203 | |
| 204 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 205 | PreParser::Statement PreParser::ParseStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 206 | // Statement :: |
| 207 | // Block |
| 208 | // VariableStatement |
| 209 | // EmptyStatement |
| 210 | // ExpressionStatement |
| 211 | // IfStatement |
| 212 | // IterationStatement |
| 213 | // ContinueStatement |
| 214 | // BreakStatement |
| 215 | // ReturnStatement |
| 216 | // WithStatement |
| 217 | // LabelledStatement |
| 218 | // SwitchStatement |
| 219 | // ThrowStatement |
| 220 | // TryStatement |
| 221 | // DebuggerStatement |
| 222 | |
| 223 | // Note: Since labels can only be used by 'break' and 'continue' |
| 224 | // statements, which themselves are only valid within blocks, |
| 225 | // iterations or 'switch' statements (i.e., BreakableStatements), |
| 226 | // labels can be simply ignored in all other cases; except for |
| 227 | // trivial labeled break statements 'label: break label' which is |
| 228 | // parsed into an empty statement. |
| 229 | |
| 230 | // Keep the source position of the statement |
| 231 | switch (peek()) { |
| 232 | case i::Token::LBRACE: |
| 233 | return ParseBlock(ok); |
| 234 | |
| 235 | case i::Token::CONST: |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 236 | case i::Token::LET: |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 237 | case i::Token::VAR: |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 238 | return ParseVariableStatement(kStatement, ok); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 239 | |
| 240 | case i::Token::SEMICOLON: |
| 241 | Next(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 242 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 243 | |
| 244 | case i::Token::IF: |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 245 | return ParseIfStatement(ok); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 246 | |
| 247 | case i::Token::DO: |
| 248 | return ParseDoWhileStatement(ok); |
| 249 | |
| 250 | case i::Token::WHILE: |
| 251 | return ParseWhileStatement(ok); |
| 252 | |
| 253 | case i::Token::FOR: |
| 254 | return ParseForStatement(ok); |
| 255 | |
| 256 | case i::Token::CONTINUE: |
| 257 | return ParseContinueStatement(ok); |
| 258 | |
| 259 | case i::Token::BREAK: |
| 260 | return ParseBreakStatement(ok); |
| 261 | |
| 262 | case i::Token::RETURN: |
| 263 | return ParseReturnStatement(ok); |
| 264 | |
| 265 | case i::Token::WITH: |
| 266 | return ParseWithStatement(ok); |
| 267 | |
| 268 | case i::Token::SWITCH: |
| 269 | return ParseSwitchStatement(ok); |
| 270 | |
| 271 | case i::Token::THROW: |
| 272 | return ParseThrowStatement(ok); |
| 273 | |
| 274 | case i::Token::TRY: |
| 275 | return ParseTryStatement(ok); |
| 276 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 277 | case i::Token::FUNCTION: { |
| 278 | i::Scanner::Location start_location = scanner_->peek_location(); |
| 279 | Statement statement = ParseFunctionDeclaration(CHECK_OK); |
| 280 | i::Scanner::Location end_location = scanner_->location(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 281 | if (!is_classic_mode()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 282 | ReportMessageAt(start_location.beg_pos, end_location.end_pos, |
| 283 | "strict_function", NULL); |
| 284 | *ok = false; |
| 285 | return Statement::Default(); |
| 286 | } else { |
| 287 | return statement; |
| 288 | } |
| 289 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 290 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 291 | case i::Token::DEBUGGER: |
| 292 | return ParseDebuggerStatement(ok); |
| 293 | |
| 294 | default: |
| 295 | return ParseExpressionOrLabelledStatement(ok); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 300 | PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 301 | // FunctionDeclaration :: |
| 302 | // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 303 | // GeneratorDeclaration :: |
| 304 | // 'function' '*' Identifier '(' FormalParameterListopt ')' |
| 305 | // '{' FunctionBody '}' |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 306 | Expect(i::Token::FUNCTION, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 307 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 308 | bool is_generator = allow_generators_ && Check(i::Token::MUL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 309 | Identifier identifier = ParseIdentifier(CHECK_OK); |
| 310 | i::Scanner::Location location = scanner_->location(); |
| 311 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 312 | Expression function_value = ParseFunctionLiteral(is_generator, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 313 | |
| 314 | if (function_value.IsStrictFunction() && |
| 315 | !identifier.IsValidStrictVariable()) { |
| 316 | // Strict mode violation, using either reserved word or eval/arguments |
| 317 | // as name of strict function. |
| 318 | const char* type = "strict_function_name"; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 319 | if (identifier.IsFutureStrictReserved() || identifier.IsYield()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 320 | type = "strict_reserved_word"; |
| 321 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 322 | ReportMessageAt(location, type, NULL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 323 | *ok = false; |
| 324 | } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 325 | return Statement::FunctionDeclaration(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 329 | PreParser::Statement PreParser::ParseBlock(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 330 | // Block :: |
| 331 | // '{' Statement* '}' |
| 332 | |
| 333 | // Note that a Block does not introduce a new execution scope! |
| 334 | // (ECMA-262, 3rd, 12.2) |
| 335 | // |
| 336 | Expect(i::Token::LBRACE, CHECK_OK); |
| 337 | while (peek() != i::Token::RBRACE) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 338 | if (is_extended_mode()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 339 | ParseSourceElement(CHECK_OK); |
| 340 | } else { |
| 341 | ParseStatement(CHECK_OK); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 342 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 343 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 344 | Expect(i::Token::RBRACE, ok); |
| 345 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 349 | PreParser::Statement PreParser::ParseVariableStatement( |
| 350 | VariableDeclarationContext var_context, |
| 351 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 352 | // VariableStatement :: |
| 353 | // VariableDeclarations ';' |
| 354 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 355 | Statement result = ParseVariableDeclarations(var_context, |
| 356 | NULL, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 357 | NULL, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 358 | CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 359 | ExpectSemicolon(CHECK_OK); |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | // If the variable declaration declares exactly one non-const |
| 365 | // variable, then *var is set to that variable. In all other cases, |
| 366 | // *var is untouched; in particular, it is the caller's responsibility |
| 367 | // to initialize it properly. This mechanism is also used for the parsing |
| 368 | // of 'for-in' loops. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 369 | PreParser::Statement PreParser::ParseVariableDeclarations( |
| 370 | VariableDeclarationContext var_context, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 371 | VariableDeclarationProperties* decl_props, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 372 | int* num_decl, |
| 373 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 374 | // VariableDeclarations :: |
| 375 | // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[','] |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 376 | // |
| 377 | // The ES6 Draft Rev3 specifies the following grammar for const declarations |
| 378 | // |
| 379 | // ConstDeclaration :: |
| 380 | // const ConstBinding (',' ConstBinding)* ';' |
| 381 | // ConstBinding :: |
| 382 | // Identifier '=' AssignmentExpression |
| 383 | // |
| 384 | // TODO(ES6): |
| 385 | // ConstBinding :: |
| 386 | // BindingPattern '=' AssignmentExpression |
| 387 | bool require_initializer = false; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 388 | if (peek() == i::Token::VAR) { |
| 389 | Consume(i::Token::VAR); |
| 390 | } else if (peek() == i::Token::CONST) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 391 | // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: |
| 392 | // |
| 393 | // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' |
| 394 | // |
| 395 | // * It is a Syntax Error if the code that matches this production is not |
| 396 | // contained in extended code. |
| 397 | // |
| 398 | // However disallowing const in classic mode will break compatibility with |
| 399 | // existing pages. Therefore we keep allowing const with the old |
| 400 | // non-harmony semantics in classic mode. |
| 401 | Consume(i::Token::CONST); |
| 402 | switch (language_mode()) { |
| 403 | case i::CLASSIC_MODE: |
| 404 | break; |
| 405 | case i::STRICT_MODE: { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 406 | i::Scanner::Location location = scanner_->peek_location(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 407 | ReportMessageAt(location, "strict_const", NULL); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 408 | *ok = false; |
| 409 | return Statement::Default(); |
| 410 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 411 | case i::EXTENDED_MODE: |
| 412 | if (var_context != kSourceElement && |
| 413 | var_context != kForStatement) { |
| 414 | i::Scanner::Location location = scanner_->peek_location(); |
| 415 | ReportMessageAt(location.beg_pos, location.end_pos, |
| 416 | "unprotected_const", NULL); |
| 417 | *ok = false; |
| 418 | return Statement::Default(); |
| 419 | } |
| 420 | require_initializer = true; |
| 421 | break; |
| 422 | } |
| 423 | } else if (peek() == i::Token::LET) { |
| 424 | // ES6 Draft Rev4 section 12.2.1: |
| 425 | // |
| 426 | // LetDeclaration : let LetBindingList ; |
| 427 | // |
| 428 | // * It is a Syntax Error if the code that matches this production is not |
| 429 | // contained in extended code. |
| 430 | if (!is_extended_mode()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 431 | i::Scanner::Location location = scanner_->peek_location(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 432 | ReportMessageAt(location.beg_pos, location.end_pos, |
| 433 | "illegal_let", NULL); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 434 | *ok = false; |
| 435 | return Statement::Default(); |
| 436 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 437 | Consume(i::Token::LET); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 438 | if (var_context != kSourceElement && |
| 439 | var_context != kForStatement) { |
| 440 | i::Scanner::Location location = scanner_->peek_location(); |
| 441 | ReportMessageAt(location.beg_pos, location.end_pos, |
| 442 | "unprotected_let", NULL); |
| 443 | *ok = false; |
| 444 | return Statement::Default(); |
| 445 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 446 | } else { |
| 447 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 448 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 449 | } |
| 450 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 451 | // The scope of a var/const declared variable anywhere inside a function |
| 452 | // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope |
| 453 | // of a let declared variable is the scope of the immediately enclosing |
| 454 | // block. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 455 | int nvars = 0; // the number of variables declared |
| 456 | do { |
| 457 | // Parse variable name. |
| 458 | if (nvars > 0) Consume(i::Token::COMMA); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 459 | Identifier identifier = ParseIdentifier(CHECK_OK); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 460 | if (!is_classic_mode() && !identifier.IsValidStrictVariable()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 461 | StrictModeIdentifierViolation(scanner_->location(), |
| 462 | "strict_var_name", |
| 463 | identifier, |
| 464 | ok); |
| 465 | return Statement::Default(); |
| 466 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 467 | nvars++; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 468 | if (peek() == i::Token::ASSIGN || require_initializer) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 469 | Expect(i::Token::ASSIGN, CHECK_OK); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 470 | ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 471 | if (decl_props != NULL) *decl_props = kHasInitializers; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 472 | } |
| 473 | } while (peek() == i::Token::COMMA); |
| 474 | |
| 475 | if (num_decl != NULL) *num_decl = nvars; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 476 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 480 | PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 481 | // ExpressionStatement | LabelledStatement :: |
| 482 | // Expression ';' |
| 483 | // Identifier ':' Statement |
| 484 | |
| 485 | Expression expr = ParseExpression(true, CHECK_OK); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 486 | if (expr.IsRawIdentifier()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 487 | ASSERT(!expr.AsIdentifier().IsFutureReserved()); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 488 | ASSERT(is_classic_mode() || |
| 489 | (!expr.AsIdentifier().IsFutureStrictReserved() && |
| 490 | !expr.AsIdentifier().IsYield())); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 491 | if (peek() == i::Token::COLON) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 492 | Consume(i::Token::COLON); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 493 | return ParseStatement(ok); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 494 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 495 | // Preparsing is disabled for extensions (because the extension details |
| 496 | // aren't passed to lazily compiled functions), so we don't |
| 497 | // accept "native function" in the preparser. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 498 | } |
| 499 | // Parsed expression statement. |
| 500 | ExpectSemicolon(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 501 | return Statement::ExpressionStatement(expr); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 505 | PreParser::Statement PreParser::ParseIfStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 506 | // IfStatement :: |
| 507 | // 'if' '(' Expression ')' Statement ('else' Statement)? |
| 508 | |
| 509 | Expect(i::Token::IF, CHECK_OK); |
| 510 | Expect(i::Token::LPAREN, CHECK_OK); |
| 511 | ParseExpression(true, CHECK_OK); |
| 512 | Expect(i::Token::RPAREN, CHECK_OK); |
| 513 | ParseStatement(CHECK_OK); |
| 514 | if (peek() == i::Token::ELSE) { |
| 515 | Next(); |
| 516 | ParseStatement(CHECK_OK); |
| 517 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 518 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 522 | PreParser::Statement PreParser::ParseContinueStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 523 | // ContinueStatement :: |
| 524 | // 'continue' [no line terminator] Identifier? ';' |
| 525 | |
| 526 | Expect(i::Token::CONTINUE, CHECK_OK); |
| 527 | i::Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 528 | if (!scanner_->HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 529 | tok != i::Token::SEMICOLON && |
| 530 | tok != i::Token::RBRACE && |
| 531 | tok != i::Token::EOS) { |
| 532 | ParseIdentifier(CHECK_OK); |
| 533 | } |
| 534 | ExpectSemicolon(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 535 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 539 | PreParser::Statement PreParser::ParseBreakStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 540 | // BreakStatement :: |
| 541 | // 'break' [no line terminator] Identifier? ';' |
| 542 | |
| 543 | Expect(i::Token::BREAK, CHECK_OK); |
| 544 | i::Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 545 | if (!scanner_->HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 546 | tok != i::Token::SEMICOLON && |
| 547 | tok != i::Token::RBRACE && |
| 548 | tok != i::Token::EOS) { |
| 549 | ParseIdentifier(CHECK_OK); |
| 550 | } |
| 551 | ExpectSemicolon(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 552 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 556 | PreParser::Statement PreParser::ParseReturnStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 557 | // ReturnStatement :: |
| 558 | // 'return' [no line terminator] Expression? ';' |
| 559 | |
| 560 | // Consume the return token. It is necessary to do the before |
| 561 | // reporting any errors on it, because of the way errors are |
| 562 | // reported (underlining). |
| 563 | Expect(i::Token::RETURN, CHECK_OK); |
| 564 | |
| 565 | // An ECMAScript program is considered syntactically incorrect if it |
| 566 | // contains a return statement that is not within the body of a |
| 567 | // function. See ECMA-262, section 12.9, page 67. |
| 568 | // This is not handled during preparsing. |
| 569 | |
| 570 | i::Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 571 | if (!scanner_->HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 572 | tok != i::Token::SEMICOLON && |
| 573 | tok != i::Token::RBRACE && |
| 574 | tok != i::Token::EOS) { |
| 575 | ParseExpression(true, CHECK_OK); |
| 576 | } |
| 577 | ExpectSemicolon(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 578 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 582 | PreParser::Statement PreParser::ParseWithStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 583 | // WithStatement :: |
| 584 | // 'with' '(' Expression ')' Statement |
| 585 | Expect(i::Token::WITH, CHECK_OK); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 586 | if (!is_classic_mode()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 587 | i::Scanner::Location location = scanner_->location(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 588 | ReportMessageAt(location, "strict_mode_with", NULL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 589 | *ok = false; |
| 590 | return Statement::Default(); |
| 591 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 592 | Expect(i::Token::LPAREN, CHECK_OK); |
| 593 | ParseExpression(true, CHECK_OK); |
| 594 | Expect(i::Token::RPAREN, CHECK_OK); |
| 595 | |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 596 | Scope::InsideWith iw(scope_); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 597 | ParseStatement(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 598 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 602 | PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 603 | // SwitchStatement :: |
| 604 | // 'switch' '(' Expression ')' '{' CaseClause* '}' |
| 605 | |
| 606 | Expect(i::Token::SWITCH, CHECK_OK); |
| 607 | Expect(i::Token::LPAREN, CHECK_OK); |
| 608 | ParseExpression(true, CHECK_OK); |
| 609 | Expect(i::Token::RPAREN, CHECK_OK); |
| 610 | |
| 611 | Expect(i::Token::LBRACE, CHECK_OK); |
| 612 | i::Token::Value token = peek(); |
| 613 | while (token != i::Token::RBRACE) { |
| 614 | if (token == i::Token::CASE) { |
| 615 | Expect(i::Token::CASE, CHECK_OK); |
| 616 | ParseExpression(true, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 617 | } else { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 618 | Expect(i::Token::DEFAULT, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 619 | } |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 620 | Expect(i::Token::COLON, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 621 | token = peek(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 622 | while (token != i::Token::CASE && |
| 623 | token != i::Token::DEFAULT && |
| 624 | token != i::Token::RBRACE) { |
| 625 | ParseStatement(CHECK_OK); |
| 626 | token = peek(); |
| 627 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 628 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 629 | Expect(i::Token::RBRACE, ok); |
| 630 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 634 | PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 635 | // DoStatement :: |
| 636 | // 'do' Statement 'while' '(' Expression ')' ';' |
| 637 | |
| 638 | Expect(i::Token::DO, CHECK_OK); |
| 639 | ParseStatement(CHECK_OK); |
| 640 | Expect(i::Token::WHILE, CHECK_OK); |
| 641 | Expect(i::Token::LPAREN, CHECK_OK); |
| 642 | ParseExpression(true, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 643 | Expect(i::Token::RPAREN, ok); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 644 | if (peek() == i::Token::SEMICOLON) Consume(i::Token::SEMICOLON); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 645 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 649 | PreParser::Statement PreParser::ParseWhileStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 650 | // WhileStatement :: |
| 651 | // 'while' '(' Expression ')' Statement |
| 652 | |
| 653 | Expect(i::Token::WHILE, CHECK_OK); |
| 654 | Expect(i::Token::LPAREN, CHECK_OK); |
| 655 | ParseExpression(true, CHECK_OK); |
| 656 | Expect(i::Token::RPAREN, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 657 | ParseStatement(ok); |
| 658 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 662 | bool PreParser::CheckInOrOf(bool accept_OF) { |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 663 | if (peek() == i::Token::IN || |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 664 | (allow_for_of() && accept_OF && peek() == i::Token::IDENTIFIER && |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 665 | scanner_->is_next_contextual_keyword(v8::internal::CStrVector("of")))) { |
| 666 | Next(); |
| 667 | return true; |
| 668 | } |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 673 | PreParser::Statement PreParser::ParseForStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 674 | // ForStatement :: |
| 675 | // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement |
| 676 | |
| 677 | Expect(i::Token::FOR, CHECK_OK); |
| 678 | Expect(i::Token::LPAREN, CHECK_OK); |
| 679 | if (peek() != i::Token::SEMICOLON) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 680 | if (peek() == i::Token::VAR || peek() == i::Token::CONST || |
| 681 | peek() == i::Token::LET) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 682 | bool is_let = peek() == i::Token::LET; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 683 | int decl_count; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 684 | VariableDeclarationProperties decl_props = kHasNoInitializers; |
| 685 | ParseVariableDeclarations( |
| 686 | kForStatement, &decl_props, &decl_count, CHECK_OK); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 687 | bool has_initializers = decl_props == kHasInitializers; |
| 688 | bool accept_IN = decl_count == 1 && !(is_let && has_initializers); |
| 689 | bool accept_OF = !has_initializers; |
| 690 | if (accept_IN && CheckInOrOf(accept_OF)) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 691 | ParseExpression(true, CHECK_OK); |
| 692 | Expect(i::Token::RPAREN, CHECK_OK); |
| 693 | |
| 694 | ParseStatement(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 695 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 696 | } |
| 697 | } else { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 698 | Expression lhs = ParseExpression(false, CHECK_OK); |
| 699 | if (CheckInOrOf(lhs.IsIdentifier())) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 700 | ParseExpression(true, CHECK_OK); |
| 701 | Expect(i::Token::RPAREN, CHECK_OK); |
| 702 | |
| 703 | ParseStatement(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 704 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | // Parsed initializer at this point. |
| 710 | Expect(i::Token::SEMICOLON, CHECK_OK); |
| 711 | |
| 712 | if (peek() != i::Token::SEMICOLON) { |
| 713 | ParseExpression(true, CHECK_OK); |
| 714 | } |
| 715 | Expect(i::Token::SEMICOLON, CHECK_OK); |
| 716 | |
| 717 | if (peek() != i::Token::RPAREN) { |
| 718 | ParseExpression(true, CHECK_OK); |
| 719 | } |
| 720 | Expect(i::Token::RPAREN, CHECK_OK); |
| 721 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 722 | ParseStatement(ok); |
| 723 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 727 | PreParser::Statement PreParser::ParseThrowStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 728 | // ThrowStatement :: |
| 729 | // 'throw' [no line terminator] Expression ';' |
| 730 | |
| 731 | Expect(i::Token::THROW, CHECK_OK); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 732 | if (scanner_->HasAnyLineTerminatorBeforeNext()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 733 | i::Scanner::Location pos = scanner_->location(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 734 | ReportMessageAt(pos, "newline_after_throw", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 735 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 736 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 737 | } |
| 738 | ParseExpression(true, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 739 | ExpectSemicolon(ok); |
| 740 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 744 | PreParser::Statement PreParser::ParseTryStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 745 | // TryStatement :: |
| 746 | // 'try' Block Catch |
| 747 | // 'try' Block Finally |
| 748 | // 'try' Block Catch Finally |
| 749 | // |
| 750 | // Catch :: |
| 751 | // 'catch' '(' Identifier ')' Block |
| 752 | // |
| 753 | // Finally :: |
| 754 | // 'finally' Block |
| 755 | |
| 756 | // In preparsing, allow any number of catch/finally blocks, including zero |
| 757 | // of both. |
| 758 | |
| 759 | Expect(i::Token::TRY, CHECK_OK); |
| 760 | |
| 761 | ParseBlock(CHECK_OK); |
| 762 | |
| 763 | bool catch_or_finally_seen = false; |
| 764 | if (peek() == i::Token::CATCH) { |
| 765 | Consume(i::Token::CATCH); |
| 766 | Expect(i::Token::LPAREN, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 767 | Identifier id = ParseIdentifier(CHECK_OK); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 768 | if (!is_classic_mode() && !id.IsValidStrictVariable()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 769 | StrictModeIdentifierViolation(scanner_->location(), |
| 770 | "strict_catch_variable", |
| 771 | id, |
| 772 | ok); |
| 773 | return Statement::Default(); |
| 774 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 775 | Expect(i::Token::RPAREN, CHECK_OK); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 776 | { Scope::InsideWith iw(scope_); |
| 777 | ParseBlock(CHECK_OK); |
| 778 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 779 | catch_or_finally_seen = true; |
| 780 | } |
| 781 | if (peek() == i::Token::FINALLY) { |
| 782 | Consume(i::Token::FINALLY); |
| 783 | ParseBlock(CHECK_OK); |
| 784 | catch_or_finally_seen = true; |
| 785 | } |
| 786 | if (!catch_or_finally_seen) { |
| 787 | *ok = false; |
| 788 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 789 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 793 | PreParser::Statement PreParser::ParseDebuggerStatement(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 794 | // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser |
| 795 | // contexts this is used as a statement which invokes the debugger as if a |
| 796 | // break point is present. |
| 797 | // DebuggerStatement :: |
| 798 | // 'debugger' ';' |
| 799 | |
| 800 | Expect(i::Token::DEBUGGER, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 801 | ExpectSemicolon(ok); |
| 802 | return Statement::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 806 | #undef CHECK_OK |
| 807 | #define CHECK_OK ok); \ |
| 808 | if (!*ok) return Expression::Default(); \ |
| 809 | ((void)0 |
| 810 | #define DUMMY ) // to make indentation work |
| 811 | #undef DUMMY |
| 812 | |
| 813 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 814 | // Precedence = 1 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 815 | PreParser::Expression PreParser::ParseExpression(bool accept_IN, bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 816 | // Expression :: |
| 817 | // AssignmentExpression |
| 818 | // Expression ',' AssignmentExpression |
| 819 | |
| 820 | Expression result = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 821 | while (peek() == i::Token::COMMA) { |
| 822 | Expect(i::Token::COMMA, CHECK_OK); |
| 823 | ParseAssignmentExpression(accept_IN, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 824 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 825 | } |
| 826 | return result; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | // Precedence = 2 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 831 | PreParser::Expression PreParser::ParseAssignmentExpression(bool accept_IN, |
| 832 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 833 | // AssignmentExpression :: |
| 834 | // ConditionalExpression |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 835 | // YieldExpression |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 836 | // LeftHandSideExpression AssignmentOperator AssignmentExpression |
| 837 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 838 | if (scope_->is_generator() && peek() == i::Token::YIELD) { |
| 839 | return ParseYieldExpression(ok); |
| 840 | } |
| 841 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 842 | i::Scanner::Location before = scanner_->peek_location(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 843 | Expression expression = ParseConditionalExpression(accept_IN, CHECK_OK); |
| 844 | |
| 845 | if (!i::Token::IsAssignmentOp(peek())) { |
| 846 | // Parsed conditional expression only (no assignment). |
| 847 | return expression; |
| 848 | } |
| 849 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 850 | if (!is_classic_mode() && |
| 851 | expression.IsIdentifier() && |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 852 | expression.AsIdentifier().IsEvalOrArguments()) { |
| 853 | i::Scanner::Location after = scanner_->location(); |
| 854 | ReportMessageAt(before.beg_pos, after.end_pos, |
| 855 | "strict_lhs_assignment", NULL); |
| 856 | *ok = false; |
| 857 | return Expression::Default(); |
| 858 | } |
| 859 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 860 | i::Token::Value op = Next(); // Get assignment operator. |
| 861 | ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 862 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 863 | if ((op == i::Token::ASSIGN) && expression.IsThisProperty()) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 864 | scope_->AddProperty(); |
| 865 | } |
| 866 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 867 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | |
| 871 | // Precedence = 3 |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 872 | PreParser::Expression PreParser::ParseYieldExpression(bool* ok) { |
| 873 | // YieldExpression :: |
| 874 | // 'yield' '*'? AssignmentExpression |
| 875 | Consume(i::Token::YIELD); |
| 876 | Check(i::Token::MUL); |
| 877 | |
| 878 | ParseAssignmentExpression(false, CHECK_OK); |
| 879 | |
| 880 | return Expression::Default(); |
| 881 | } |
| 882 | |
| 883 | |
| 884 | // Precedence = 3 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 885 | PreParser::Expression PreParser::ParseConditionalExpression(bool accept_IN, |
| 886 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 887 | // ConditionalExpression :: |
| 888 | // LogicalOrExpression |
| 889 | // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression |
| 890 | |
| 891 | // We start using the binary expression parser for prec >= 4 only! |
| 892 | Expression expression = ParseBinaryExpression(4, accept_IN, CHECK_OK); |
| 893 | if (peek() != i::Token::CONDITIONAL) return expression; |
| 894 | Consume(i::Token::CONDITIONAL); |
| 895 | // In parsing the first assignment expression in conditional |
| 896 | // expressions we always accept the 'in' keyword; see ECMA-262, |
| 897 | // section 11.12, page 58. |
| 898 | ParseAssignmentExpression(true, CHECK_OK); |
| 899 | Expect(i::Token::COLON, CHECK_OK); |
| 900 | ParseAssignmentExpression(accept_IN, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 901 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | |
| 905 | int PreParser::Precedence(i::Token::Value tok, bool accept_IN) { |
| 906 | if (tok == i::Token::IN && !accept_IN) |
| 907 | return 0; // 0 precedence will terminate binary expression parsing |
| 908 | |
| 909 | return i::Token::Precedence(tok); |
| 910 | } |
| 911 | |
| 912 | |
| 913 | // Precedence >= 4 |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 914 | PreParser::Expression PreParser::ParseBinaryExpression(int prec, |
| 915 | bool accept_IN, |
| 916 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 917 | Expression result = ParseUnaryExpression(CHECK_OK); |
| 918 | for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) { |
| 919 | // prec1 >= 4 |
| 920 | while (Precedence(peek(), accept_IN) == prec1) { |
| 921 | Next(); |
| 922 | ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 923 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | return result; |
| 927 | } |
| 928 | |
| 929 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 930 | PreParser::Expression PreParser::ParseUnaryExpression(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 931 | // UnaryExpression :: |
| 932 | // PostfixExpression |
| 933 | // 'delete' UnaryExpression |
| 934 | // 'void' UnaryExpression |
| 935 | // 'typeof' UnaryExpression |
| 936 | // '++' UnaryExpression |
| 937 | // '--' UnaryExpression |
| 938 | // '+' UnaryExpression |
| 939 | // '-' UnaryExpression |
| 940 | // '~' UnaryExpression |
| 941 | // '!' UnaryExpression |
| 942 | |
| 943 | i::Token::Value op = peek(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 944 | if (i::Token::IsUnaryOp(op)) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 945 | op = Next(); |
| 946 | ParseUnaryExpression(ok); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 947 | return Expression::Default(); |
| 948 | } else if (i::Token::IsCountOp(op)) { |
| 949 | op = Next(); |
| 950 | i::Scanner::Location before = scanner_->peek_location(); |
| 951 | Expression expression = ParseUnaryExpression(CHECK_OK); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 952 | if (!is_classic_mode() && |
| 953 | expression.IsIdentifier() && |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 954 | expression.AsIdentifier().IsEvalOrArguments()) { |
| 955 | i::Scanner::Location after = scanner_->location(); |
| 956 | ReportMessageAt(before.beg_pos, after.end_pos, |
| 957 | "strict_lhs_prefix", NULL); |
| 958 | *ok = false; |
| 959 | } |
| 960 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 961 | } else { |
| 962 | return ParsePostfixExpression(ok); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 967 | PreParser::Expression PreParser::ParsePostfixExpression(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 968 | // PostfixExpression :: |
| 969 | // LeftHandSideExpression ('++' | '--')? |
| 970 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 971 | i::Scanner::Location before = scanner_->peek_location(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 972 | Expression expression = ParseLeftHandSideExpression(CHECK_OK); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 973 | if (!scanner_->HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 974 | i::Token::IsCountOp(peek())) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 975 | if (!is_classic_mode() && |
| 976 | expression.IsIdentifier() && |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 977 | expression.AsIdentifier().IsEvalOrArguments()) { |
| 978 | i::Scanner::Location after = scanner_->location(); |
| 979 | ReportMessageAt(before.beg_pos, after.end_pos, |
| 980 | "strict_lhs_postfix", NULL); |
| 981 | *ok = false; |
| 982 | return Expression::Default(); |
| 983 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 984 | Next(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 985 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 986 | } |
| 987 | return expression; |
| 988 | } |
| 989 | |
| 990 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 991 | PreParser::Expression PreParser::ParseLeftHandSideExpression(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 992 | // LeftHandSideExpression :: |
| 993 | // (NewExpression | MemberExpression) ... |
| 994 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 995 | Expression result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 996 | if (peek() == i::Token::NEW) { |
| 997 | result = ParseNewExpression(CHECK_OK); |
| 998 | } else { |
| 999 | result = ParseMemberExpression(CHECK_OK); |
| 1000 | } |
| 1001 | |
| 1002 | while (true) { |
| 1003 | switch (peek()) { |
| 1004 | case i::Token::LBRACK: { |
| 1005 | Consume(i::Token::LBRACK); |
| 1006 | ParseExpression(true, CHECK_OK); |
| 1007 | Expect(i::Token::RBRACK, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1008 | if (result.IsThis()) { |
| 1009 | result = Expression::ThisProperty(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1010 | } else { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1011 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1012 | } |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | case i::Token::LPAREN: { |
| 1017 | ParseArguments(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1018 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | case i::Token::PERIOD: { |
| 1023 | Consume(i::Token::PERIOD); |
| 1024 | ParseIdentifierName(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1025 | if (result.IsThis()) { |
| 1026 | result = Expression::ThisProperty(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1027 | } else { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1028 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1029 | } |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | default: |
| 1034 | return result; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1040 | PreParser::Expression PreParser::ParseNewExpression(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1041 | // NewExpression :: |
| 1042 | // ('new')+ MemberExpression |
| 1043 | |
| 1044 | // The grammar for new expressions is pretty warped. The keyword |
| 1045 | // 'new' can either be a part of the new expression (where it isn't |
| 1046 | // followed by an argument list) or a part of the member expression, |
| 1047 | // where it must be followed by an argument list. To accommodate |
| 1048 | // this, we parse the 'new' keywords greedily and keep track of how |
| 1049 | // many we have parsed. This information is then passed on to the |
| 1050 | // member expression parser, which is only allowed to match argument |
| 1051 | // lists as long as it has 'new' prefixes left |
| 1052 | unsigned new_count = 0; |
| 1053 | do { |
| 1054 | Consume(i::Token::NEW); |
| 1055 | new_count++; |
| 1056 | } while (peek() == i::Token::NEW); |
| 1057 | |
| 1058 | return ParseMemberWithNewPrefixesExpression(new_count, ok); |
| 1059 | } |
| 1060 | |
| 1061 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1062 | PreParser::Expression PreParser::ParseMemberExpression(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1063 | return ParseMemberWithNewPrefixesExpression(0, ok); |
| 1064 | } |
| 1065 | |
| 1066 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1067 | PreParser::Expression PreParser::ParseMemberWithNewPrefixesExpression( |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1068 | unsigned new_count, bool* ok) { |
| 1069 | // MemberExpression :: |
| 1070 | // (PrimaryExpression | FunctionLiteral) |
| 1071 | // ('[' Expression ']' | '.' Identifier | Arguments)* |
| 1072 | |
| 1073 | // Parse the initial primary or function expression. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1074 | Expression result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1075 | if (peek() == i::Token::FUNCTION) { |
| 1076 | Consume(i::Token::FUNCTION); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1077 | |
| 1078 | bool is_generator = allow_generators_ && Check(i::Token::MUL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1079 | Identifier identifier = Identifier::Default(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1080 | if (peek_any_identifier()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1081 | identifier = ParseIdentifier(CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1082 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1083 | result = ParseFunctionLiteral(is_generator, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1084 | if (result.IsStrictFunction() && !identifier.IsValidStrictVariable()) { |
| 1085 | StrictModeIdentifierViolation(scanner_->location(), |
| 1086 | "strict_function_name", |
| 1087 | identifier, |
| 1088 | ok); |
| 1089 | return Expression::Default(); |
| 1090 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1091 | } else { |
| 1092 | result = ParsePrimaryExpression(CHECK_OK); |
| 1093 | } |
| 1094 | |
| 1095 | while (true) { |
| 1096 | switch (peek()) { |
| 1097 | case i::Token::LBRACK: { |
| 1098 | Consume(i::Token::LBRACK); |
| 1099 | ParseExpression(true, CHECK_OK); |
| 1100 | Expect(i::Token::RBRACK, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1101 | if (result.IsThis()) { |
| 1102 | result = Expression::ThisProperty(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1103 | } else { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1104 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1105 | } |
| 1106 | break; |
| 1107 | } |
| 1108 | case i::Token::PERIOD: { |
| 1109 | Consume(i::Token::PERIOD); |
| 1110 | ParseIdentifierName(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1111 | if (result.IsThis()) { |
| 1112 | result = Expression::ThisProperty(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1113 | } else { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1114 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | case i::Token::LPAREN: { |
| 1119 | if (new_count == 0) return result; |
| 1120 | // Consume one of the new prefixes (already parsed). |
| 1121 | ParseArguments(CHECK_OK); |
| 1122 | new_count--; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1123 | result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | } |
| 1126 | default: |
| 1127 | return result; |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1133 | PreParser::Expression PreParser::ParsePrimaryExpression(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1134 | // PrimaryExpression :: |
| 1135 | // 'this' |
| 1136 | // 'null' |
| 1137 | // 'true' |
| 1138 | // 'false' |
| 1139 | // Identifier |
| 1140 | // Number |
| 1141 | // String |
| 1142 | // ArrayLiteral |
| 1143 | // ObjectLiteral |
| 1144 | // RegExpLiteral |
| 1145 | // '(' Expression ')' |
| 1146 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1147 | Expression result = Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1148 | switch (peek()) { |
| 1149 | case i::Token::THIS: { |
| 1150 | Next(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1151 | result = Expression::This(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1155 | case i::Token::FUTURE_RESERVED_WORD: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1156 | case i::Token::FUTURE_STRICT_RESERVED_WORD: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1157 | case i::Token::YIELD: |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1158 | case i::Token::IDENTIFIER: { |
| 1159 | Identifier id = ParseIdentifier(CHECK_OK); |
| 1160 | result = Expression::FromIdentifier(id); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | case i::Token::NULL_LITERAL: |
| 1165 | case i::Token::TRUE_LITERAL: |
| 1166 | case i::Token::FALSE_LITERAL: |
| 1167 | case i::Token::NUMBER: { |
| 1168 | Next(); |
| 1169 | break; |
| 1170 | } |
| 1171 | case i::Token::STRING: { |
| 1172 | Next(); |
| 1173 | result = GetStringSymbol(); |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | case i::Token::ASSIGN_DIV: |
| 1178 | result = ParseRegExpLiteral(true, CHECK_OK); |
| 1179 | break; |
| 1180 | |
| 1181 | case i::Token::DIV: |
| 1182 | result = ParseRegExpLiteral(false, CHECK_OK); |
| 1183 | break; |
| 1184 | |
| 1185 | case i::Token::LBRACK: |
| 1186 | result = ParseArrayLiteral(CHECK_OK); |
| 1187 | break; |
| 1188 | |
| 1189 | case i::Token::LBRACE: |
| 1190 | result = ParseObjectLiteral(CHECK_OK); |
| 1191 | break; |
| 1192 | |
| 1193 | case i::Token::LPAREN: |
| 1194 | Consume(i::Token::LPAREN); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1195 | parenthesized_function_ = (peek() == i::Token::FUNCTION); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1196 | result = ParseExpression(true, CHECK_OK); |
| 1197 | Expect(i::Token::RPAREN, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1198 | result = result.Parenthesize(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | |
| 1201 | case i::Token::MOD: |
| 1202 | result = ParseV8Intrinsic(CHECK_OK); |
| 1203 | break; |
| 1204 | |
| 1205 | default: { |
| 1206 | Next(); |
| 1207 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1208 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | return result; |
| 1213 | } |
| 1214 | |
| 1215 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1216 | PreParser::Expression PreParser::ParseArrayLiteral(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1217 | // ArrayLiteral :: |
| 1218 | // '[' Expression? (',' Expression?)* ']' |
| 1219 | Expect(i::Token::LBRACK, CHECK_OK); |
| 1220 | while (peek() != i::Token::RBRACK) { |
| 1221 | if (peek() != i::Token::COMMA) { |
| 1222 | ParseAssignmentExpression(true, CHECK_OK); |
| 1223 | } |
| 1224 | if (peek() != i::Token::RBRACK) { |
| 1225 | Expect(i::Token::COMMA, CHECK_OK); |
| 1226 | } |
| 1227 | } |
| 1228 | Expect(i::Token::RBRACK, CHECK_OK); |
| 1229 | |
| 1230 | scope_->NextMaterializedLiteralIndex(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1231 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1234 | void PreParser::CheckDuplicate(DuplicateFinder* finder, |
| 1235 | i::Token::Value property, |
| 1236 | int type, |
| 1237 | bool* ok) { |
| 1238 | int old_type; |
| 1239 | if (property == i::Token::NUMBER) { |
| 1240 | old_type = finder->AddNumber(scanner_->literal_ascii_string(), type); |
| 1241 | } else if (scanner_->is_literal_ascii()) { |
| 1242 | old_type = finder->AddAsciiSymbol(scanner_->literal_ascii_string(), |
| 1243 | type); |
| 1244 | } else { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1245 | old_type = finder->AddUtf16Symbol(scanner_->literal_utf16_string(), type); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1246 | } |
| 1247 | if (HasConflict(old_type, type)) { |
| 1248 | if (IsDataDataConflict(old_type, type)) { |
| 1249 | // Both are data properties. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1250 | if (is_classic_mode()) return; |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1251 | ReportMessageAt(scanner_->location(), |
| 1252 | "strict_duplicate_property", NULL); |
| 1253 | } else if (IsDataAccessorConflict(old_type, type)) { |
| 1254 | // Both a data and an accessor property with the same name. |
| 1255 | ReportMessageAt(scanner_->location(), |
| 1256 | "accessor_data_property", NULL); |
| 1257 | } else { |
| 1258 | ASSERT(IsAccessorAccessorConflict(old_type, type)); |
| 1259 | // Both accessors of the same type. |
| 1260 | ReportMessageAt(scanner_->location(), |
| 1261 | "accessor_get_set", NULL); |
| 1262 | } |
| 1263 | *ok = false; |
| 1264 | } |
| 1265 | } |
| 1266 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1267 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1268 | PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1269 | // ObjectLiteral :: |
| 1270 | // '{' ( |
| 1271 | // ((IdentifierName | String | Number) ':' AssignmentExpression) |
| 1272 | // | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral) |
| 1273 | // )*[','] '}' |
| 1274 | |
| 1275 | Expect(i::Token::LBRACE, CHECK_OK); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1276 | DuplicateFinder duplicate_finder(scanner_->unicode_cache()); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1277 | while (peek() != i::Token::RBRACE) { |
| 1278 | i::Token::Value next = peek(); |
| 1279 | switch (next) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1280 | case i::Token::IDENTIFIER: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1281 | case i::Token::FUTURE_RESERVED_WORD: |
| 1282 | case i::Token::FUTURE_STRICT_RESERVED_WORD: { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1283 | bool is_getter = false; |
| 1284 | bool is_setter = false; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1285 | ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1286 | if ((is_getter || is_setter) && peek() != i::Token::COLON) { |
| 1287 | i::Token::Value name = Next(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1288 | bool is_keyword = i::Token::IsKeyword(name); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1289 | if (name != i::Token::IDENTIFIER && |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1290 | name != i::Token::FUTURE_RESERVED_WORD && |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1291 | name != i::Token::FUTURE_STRICT_RESERVED_WORD && |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1292 | name != i::Token::NUMBER && |
| 1293 | name != i::Token::STRING && |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1294 | !is_keyword) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1295 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1296 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1297 | } |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1298 | if (!is_keyword) { |
| 1299 | LogSymbol(); |
| 1300 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1301 | PropertyType type = is_getter ? kGetterProperty : kSetterProperty; |
| 1302 | CheckDuplicate(&duplicate_finder, name, type, CHECK_OK); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1303 | ParseFunctionLiteral(false, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1304 | if (peek() != i::Token::RBRACE) { |
| 1305 | Expect(i::Token::COMMA, CHECK_OK); |
| 1306 | } |
| 1307 | continue; // restart the while |
| 1308 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1309 | CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1310 | break; |
| 1311 | } |
| 1312 | case i::Token::STRING: |
| 1313 | Consume(next); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1314 | CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1315 | GetStringSymbol(); |
| 1316 | break; |
| 1317 | case i::Token::NUMBER: |
| 1318 | Consume(next); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1319 | CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | default: |
| 1322 | if (i::Token::IsKeyword(next)) { |
| 1323 | Consume(next); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1324 | CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1325 | } else { |
| 1326 | // Unexpected token. |
| 1327 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1328 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | Expect(i::Token::COLON, CHECK_OK); |
| 1333 | ParseAssignmentExpression(true, CHECK_OK); |
| 1334 | |
| 1335 | // TODO(1240767): Consider allowing trailing comma. |
| 1336 | if (peek() != i::Token::RBRACE) Expect(i::Token::COMMA, CHECK_OK); |
| 1337 | } |
| 1338 | Expect(i::Token::RBRACE, CHECK_OK); |
| 1339 | |
| 1340 | scope_->NextMaterializedLiteralIndex(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1341 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1345 | PreParser::Expression PreParser::ParseRegExpLiteral(bool seen_equal, |
| 1346 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1347 | if (!scanner_->ScanRegExpPattern(seen_equal)) { |
| 1348 | Next(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1349 | ReportMessageAt(scanner_->location(), "unterminated_regexp", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1350 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1351 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | scope_->NextMaterializedLiteralIndex(); |
| 1355 | |
| 1356 | if (!scanner_->ScanRegExpFlags()) { |
| 1357 | Next(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1358 | ReportMessageAt(scanner_->location(), "invalid_regexp_flags", NULL); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1359 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1360 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1361 | } |
| 1362 | Next(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1363 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1367 | PreParser::Arguments PreParser::ParseArguments(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1368 | // Arguments :: |
| 1369 | // '(' (AssignmentExpression)*[','] ')' |
| 1370 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1371 | Expect(i::Token::LPAREN, ok); |
| 1372 | if (!*ok) return -1; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1373 | bool done = (peek() == i::Token::RPAREN); |
| 1374 | int argc = 0; |
| 1375 | while (!done) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1376 | ParseAssignmentExpression(true, ok); |
| 1377 | if (!*ok) return -1; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1378 | argc++; |
| 1379 | done = (peek() == i::Token::RPAREN); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1380 | if (!done) { |
| 1381 | Expect(i::Token::COMMA, ok); |
| 1382 | if (!*ok) return -1; |
| 1383 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1384 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1385 | Expect(i::Token::RPAREN, ok); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1386 | return argc; |
| 1387 | } |
| 1388 | |
| 1389 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1390 | PreParser::Expression PreParser::ParseFunctionLiteral(bool is_generator, |
| 1391 | bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1392 | // Function :: |
| 1393 | // '(' FormalParameterList? ')' '{' FunctionBody '}' |
| 1394 | |
| 1395 | // Parse function body. |
| 1396 | ScopeType outer_scope_type = scope_->type(); |
| 1397 | bool inside_with = scope_->IsInsideWith(); |
| 1398 | Scope function_scope(&scope_, kFunctionScope); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1399 | function_scope.set_is_generator(is_generator); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1400 | // FormalParameterList :: |
| 1401 | // '(' (Identifier)*[','] ')' |
| 1402 | Expect(i::Token::LPAREN, CHECK_OK); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1403 | int start_position = scanner_->location().beg_pos; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1404 | bool done = (peek() == i::Token::RPAREN); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1405 | DuplicateFinder duplicate_finder(scanner_->unicode_cache()); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1406 | while (!done) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1407 | Identifier id = ParseIdentifier(CHECK_OK); |
| 1408 | if (!id.IsValidStrictVariable()) { |
| 1409 | StrictModeIdentifierViolation(scanner_->location(), |
| 1410 | "strict_param_name", |
| 1411 | id, |
| 1412 | CHECK_OK); |
| 1413 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1414 | int prev_value; |
| 1415 | if (scanner_->is_literal_ascii()) { |
| 1416 | prev_value = |
| 1417 | duplicate_finder.AddAsciiSymbol(scanner_->literal_ascii_string(), 1); |
| 1418 | } else { |
| 1419 | prev_value = |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1420 | duplicate_finder.AddUtf16Symbol(scanner_->literal_utf16_string(), 1); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | if (prev_value != 0) { |
| 1424 | SetStrictModeViolation(scanner_->location(), |
| 1425 | "strict_param_dupe", |
| 1426 | CHECK_OK); |
| 1427 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1428 | done = (peek() == i::Token::RPAREN); |
| 1429 | if (!done) { |
| 1430 | Expect(i::Token::COMMA, CHECK_OK); |
| 1431 | } |
| 1432 | } |
| 1433 | Expect(i::Token::RPAREN, CHECK_OK); |
| 1434 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1435 | // Determine if the function will be lazily compiled. |
| 1436 | // Currently only happens to top-level functions. |
| 1437 | // Optimistically assume that all top-level functions are lazily compiled. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1438 | bool is_lazily_compiled = (outer_scope_type == kTopLevelScope && |
| 1439 | !inside_with && allow_lazy_ && |
| 1440 | !parenthesized_function_); |
| 1441 | parenthesized_function_ = false; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1442 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1443 | Expect(i::Token::LBRACE, CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1444 | if (is_lazily_compiled) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1445 | ParseLazyFunctionLiteralBody(CHECK_OK); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1446 | } else { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1447 | ParseSourceElements(i::Token::RBRACE, ok); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1448 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1449 | Expect(i::Token::RBRACE, CHECK_OK); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1450 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1451 | if (!is_classic_mode()) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1452 | int end_position = scanner_->location().end_pos; |
| 1453 | CheckOctalLiteral(start_position, end_position, CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1454 | CheckDelayedStrictModeViolation(start_position, end_position, CHECK_OK); |
| 1455 | return Expression::StrictFunction(); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1458 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1462 | void PreParser::ParseLazyFunctionLiteralBody(bool* ok) { |
| 1463 | int body_start = scanner_->location().beg_pos; |
| 1464 | log_->PauseRecording(); |
| 1465 | ParseSourceElements(i::Token::RBRACE, ok); |
| 1466 | log_->ResumeRecording(); |
| 1467 | if (!*ok) return; |
| 1468 | |
| 1469 | // Position right after terminal '}'. |
| 1470 | ASSERT_EQ(i::Token::RBRACE, scanner_->peek()); |
| 1471 | int body_end = scanner_->peek_location().end_pos; |
| 1472 | log_->LogFunction(body_start, body_end, |
| 1473 | scope_->materialized_literal_count(), |
| 1474 | scope_->expected_properties(), |
| 1475 | language_mode()); |
| 1476 | } |
| 1477 | |
| 1478 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1479 | PreParser::Expression PreParser::ParseV8Intrinsic(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1480 | // CallRuntime :: |
| 1481 | // '%' Identifier Arguments |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1482 | Expect(i::Token::MOD, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1483 | if (!allow_natives_syntax_) { |
| 1484 | *ok = false; |
| 1485 | return Expression::Default(); |
| 1486 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1487 | ParseIdentifier(CHECK_OK); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1488 | ParseArguments(ok); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1489 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1490 | return Expression::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1493 | #undef CHECK_OK |
| 1494 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1495 | |
| 1496 | void PreParser::ExpectSemicolon(bool* ok) { |
| 1497 | // Check for automatic semicolon insertion according to |
| 1498 | // the rules given in ECMA-262, section 7.9, page 21. |
| 1499 | i::Token::Value tok = peek(); |
| 1500 | if (tok == i::Token::SEMICOLON) { |
| 1501 | Next(); |
| 1502 | return; |
| 1503 | } |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 1504 | if (scanner_->HasAnyLineTerminatorBeforeNext() || |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1505 | tok == i::Token::RBRACE || |
| 1506 | tok == i::Token::EOS) { |
| 1507 | return; |
| 1508 | } |
| 1509 | Expect(i::Token::SEMICOLON, ok); |
| 1510 | } |
| 1511 | |
| 1512 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1513 | void PreParser::LogSymbol() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1514 | int identifier_pos = scanner_->location().beg_pos; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1515 | if (scanner_->is_literal_ascii()) { |
| 1516 | log_->LogAsciiSymbol(identifier_pos, scanner_->literal_ascii_string()); |
| 1517 | } else { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1518 | log_->LogUtf16Symbol(identifier_pos, scanner_->literal_utf16_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1519 | } |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1523 | PreParser::Expression PreParser::GetStringSymbol() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1524 | const int kUseStrictLength = 10; |
| 1525 | const char* kUseStrictChars = "use strict"; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1526 | LogSymbol(); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1527 | if (scanner_->is_literal_ascii() && |
| 1528 | scanner_->literal_length() == kUseStrictLength && |
| 1529 | !scanner_->literal_contains_escapes() && |
| 1530 | !strncmp(scanner_->literal_ascii_string().start(), kUseStrictChars, |
| 1531 | kUseStrictLength)) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1532 | return Expression::UseStrictStringLiteral(); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1533 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1534 | return Expression::StringLiteral(); |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | PreParser::Identifier PreParser::GetIdentifierSymbol() { |
| 1539 | LogSymbol(); |
| 1540 | if (scanner_->current_token() == i::Token::FUTURE_RESERVED_WORD) { |
| 1541 | return Identifier::FutureReserved(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1542 | } else if (scanner_->current_token() == |
| 1543 | i::Token::FUTURE_STRICT_RESERVED_WORD) { |
| 1544 | return Identifier::FutureStrictReserved(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1545 | } else if (scanner_->current_token() == i::Token::YIELD) { |
| 1546 | return Identifier::Yield(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1547 | } |
| 1548 | if (scanner_->is_literal_ascii()) { |
| 1549 | // Detect strict-mode poison words. |
| 1550 | if (scanner_->literal_length() == 4 && |
| 1551 | !strncmp(scanner_->literal_ascii_string().start(), "eval", 4)) { |
| 1552 | return Identifier::Eval(); |
| 1553 | } |
| 1554 | if (scanner_->literal_length() == 9 && |
| 1555 | !strncmp(scanner_->literal_ascii_string().start(), "arguments", 9)) { |
| 1556 | return Identifier::Arguments(); |
| 1557 | } |
| 1558 | } |
| 1559 | return Identifier::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1563 | PreParser::Identifier PreParser::ParseIdentifier(bool* ok) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1564 | i::Token::Value next = Next(); |
| 1565 | switch (next) { |
| 1566 | case i::Token::FUTURE_RESERVED_WORD: { |
| 1567 | i::Scanner::Location location = scanner_->location(); |
| 1568 | ReportMessageAt(location.beg_pos, location.end_pos, |
| 1569 | "reserved_word", NULL); |
| 1570 | *ok = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1571 | return GetIdentifierSymbol(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1572 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1573 | case i::Token::YIELD: |
| 1574 | if (scope_->is_generator()) { |
| 1575 | // 'yield' in a generator is only valid as part of a YieldExpression. |
| 1576 | ReportMessageAt(scanner_->location(), "unexpected_token", "yield"); |
| 1577 | *ok = false; |
| 1578 | return Identifier::Yield(); |
| 1579 | } |
| 1580 | // FALLTHROUGH |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1581 | case i::Token::FUTURE_STRICT_RESERVED_WORD: |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1582 | if (!is_classic_mode()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1583 | i::Scanner::Location location = scanner_->location(); |
| 1584 | ReportMessageAt(location.beg_pos, location.end_pos, |
| 1585 | "strict_reserved_word", NULL); |
| 1586 | *ok = false; |
| 1587 | } |
| 1588 | // FALLTHROUGH |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1589 | case i::Token::IDENTIFIER: |
| 1590 | return GetIdentifierSymbol(); |
| 1591 | default: |
| 1592 | *ok = false; |
| 1593 | return Identifier::Default(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1594 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1598 | void PreParser::SetStrictModeViolation(i::Scanner::Location location, |
| 1599 | const char* type, |
| 1600 | bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1601 | if (!is_classic_mode()) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1602 | ReportMessageAt(location, type, NULL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1603 | *ok = false; |
| 1604 | return; |
| 1605 | } |
| 1606 | // Delay report in case this later turns out to be strict code |
| 1607 | // (i.e., for function names and parameters prior to a "use strict" |
| 1608 | // directive). |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1609 | // It's safe to overwrite an existing violation. |
| 1610 | // It's either from a function that turned out to be non-strict, |
| 1611 | // or it's in the current function (and we just need to report |
| 1612 | // one error), or it's in a unclosed nesting function that wasn't |
| 1613 | // strict (otherwise we would already be in strict mode). |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1614 | strict_mode_violation_location_ = location; |
| 1615 | strict_mode_violation_type_ = type; |
| 1616 | } |
| 1617 | |
| 1618 | |
| 1619 | void PreParser::CheckDelayedStrictModeViolation(int beg_pos, |
| 1620 | int end_pos, |
| 1621 | bool* ok) { |
| 1622 | i::Scanner::Location location = strict_mode_violation_location_; |
| 1623 | if (location.IsValid() && |
| 1624 | location.beg_pos > beg_pos && location.end_pos < end_pos) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1625 | ReportMessageAt(location, strict_mode_violation_type_, NULL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1626 | *ok = false; |
| 1627 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | void PreParser::StrictModeIdentifierViolation(i::Scanner::Location location, |
| 1632 | const char* eval_args_type, |
| 1633 | Identifier identifier, |
| 1634 | bool* ok) { |
| 1635 | const char* type = eval_args_type; |
| 1636 | if (identifier.IsFutureReserved()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1637 | type = "reserved_word"; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1638 | } else if (identifier.IsFutureStrictReserved() || identifier.IsYield()) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1639 | type = "strict_reserved_word"; |
| 1640 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1641 | if (!is_classic_mode()) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1642 | ReportMessageAt(location, type, NULL); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1643 | *ok = false; |
| 1644 | return; |
| 1645 | } |
| 1646 | strict_mode_violation_location_ = location; |
| 1647 | strict_mode_violation_type_ = type; |
| 1648 | } |
| 1649 | |
| 1650 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1651 | PreParser::Identifier PreParser::ParseIdentifierName(bool* ok) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1652 | i::Token::Value next = Next(); |
| 1653 | if (i::Token::IsKeyword(next)) { |
| 1654 | int pos = scanner_->location().beg_pos; |
| 1655 | const char* keyword = i::Token::String(next); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1656 | log_->LogAsciiSymbol(pos, i::Vector<const char>(keyword, |
| 1657 | i::StrLength(keyword))); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1658 | return Identifier::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1659 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1660 | if (next == i::Token::IDENTIFIER || |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1661 | next == i::Token::FUTURE_RESERVED_WORD || |
| 1662 | next == i::Token::FUTURE_STRICT_RESERVED_WORD) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1663 | return GetIdentifierSymbol(); |
| 1664 | } |
| 1665 | *ok = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1666 | return Identifier::Default(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1669 | #undef CHECK_OK |
| 1670 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1671 | |
| 1672 | // This function reads an identifier and determines whether or not it |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1673 | // is 'get' or 'set'. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1674 | PreParser::Identifier PreParser::ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 1675 | bool* is_set, |
| 1676 | bool* ok) { |
| 1677 | Identifier result = ParseIdentifierName(ok); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1678 | if (!*ok) return Identifier::Default(); |
| 1679 | if (scanner_->is_literal_ascii() && |
| 1680 | scanner_->literal_length() == 3) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1681 | const char* token = scanner_->literal_ascii_string().start(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1682 | *is_get = strncmp(token, "get", 3) == 0; |
| 1683 | *is_set = !*is_get && strncmp(token, "set", 3) == 0; |
| 1684 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1685 | return result; |
| 1686 | } |
| 1687 | |
| 1688 | bool PreParser::peek_any_identifier() { |
| 1689 | i::Token::Value next = peek(); |
| 1690 | return next == i::Token::IDENTIFIER || |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1691 | next == i::Token::FUTURE_RESERVED_WORD || |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1692 | next == i::Token::FUTURE_STRICT_RESERVED_WORD || |
| 1693 | next == i::Token::YIELD; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1694 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1695 | |
| 1696 | |
| 1697 | int DuplicateFinder::AddAsciiSymbol(i::Vector<const char> key, int value) { |
| 1698 | return AddSymbol(i::Vector<const byte>::cast(key), true, value); |
| 1699 | } |
| 1700 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1701 | int DuplicateFinder::AddUtf16Symbol(i::Vector<const uint16_t> key, int value) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1702 | return AddSymbol(i::Vector<const byte>::cast(key), false, value); |
| 1703 | } |
| 1704 | |
| 1705 | int DuplicateFinder::AddSymbol(i::Vector<const byte> key, |
| 1706 | bool is_ascii, |
| 1707 | int value) { |
| 1708 | uint32_t hash = Hash(key, is_ascii); |
| 1709 | byte* encoding = BackupKey(key, is_ascii); |
| 1710 | i::HashMap::Entry* entry = map_.Lookup(encoding, hash, true); |
| 1711 | int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 1712 | entry->value = |
| 1713 | reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value)); |
| 1714 | return old_value; |
| 1715 | } |
| 1716 | |
| 1717 | |
| 1718 | int DuplicateFinder::AddNumber(i::Vector<const char> key, int value) { |
| 1719 | ASSERT(key.length() > 0); |
| 1720 | // Quick check for already being in canonical form. |
| 1721 | if (IsNumberCanonical(key)) { |
| 1722 | return AddAsciiSymbol(key, value); |
| 1723 | } |
| 1724 | |
| 1725 | int flags = i::ALLOW_HEX | i::ALLOW_OCTALS; |
| 1726 | double double_value = StringToDouble(unicode_constants_, key, flags, 0.0); |
| 1727 | int length; |
| 1728 | const char* string; |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 1729 | if (!std::isfinite(double_value)) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1730 | string = "Infinity"; |
| 1731 | length = 8; // strlen("Infinity"); |
| 1732 | } else { |
| 1733 | string = DoubleToCString(double_value, |
| 1734 | i::Vector<char>(number_buffer_, kBufferSize)); |
| 1735 | length = i::StrLength(string); |
| 1736 | } |
| 1737 | return AddSymbol(i::Vector<const byte>(reinterpret_cast<const byte*>(string), |
| 1738 | length), true, value); |
| 1739 | } |
| 1740 | |
| 1741 | |
| 1742 | bool DuplicateFinder::IsNumberCanonical(i::Vector<const char> number) { |
| 1743 | // Test for a safe approximation of number literals that are already |
| 1744 | // in canonical form: max 15 digits, no leading zeroes, except an |
| 1745 | // integer part that is a single zero, and no trailing zeros below |
| 1746 | // the decimal point. |
| 1747 | int pos = 0; |
| 1748 | int length = number.length(); |
| 1749 | if (number.length() > 15) return false; |
| 1750 | if (number[pos] == '0') { |
| 1751 | pos++; |
| 1752 | } else { |
| 1753 | while (pos < length && |
| 1754 | static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++; |
| 1755 | } |
| 1756 | if (length == pos) return true; |
| 1757 | if (number[pos] != '.') return false; |
| 1758 | pos++; |
| 1759 | bool invalid_last_digit = true; |
| 1760 | while (pos < length) { |
| 1761 | byte digit = number[pos] - '0'; |
| 1762 | if (digit > '9' - '0') return false; |
| 1763 | invalid_last_digit = (digit == 0); |
| 1764 | pos++; |
| 1765 | } |
| 1766 | return !invalid_last_digit; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | uint32_t DuplicateFinder::Hash(i::Vector<const byte> key, bool is_ascii) { |
| 1771 | // Primitive hash function, almost identical to the one used |
| 1772 | // for strings (except that it's seeded by the length and ASCII-ness). |
| 1773 | int length = key.length(); |
| 1774 | uint32_t hash = (length << 1) | (is_ascii ? 1 : 0) ; |
| 1775 | for (int i = 0; i < length; i++) { |
| 1776 | uint32_t c = key[i]; |
| 1777 | hash = (hash + c) * 1025; |
| 1778 | hash ^= (hash >> 6); |
| 1779 | } |
| 1780 | return hash; |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | bool DuplicateFinder::Match(void* first, void* second) { |
| 1785 | // Decode lengths. |
| 1786 | // Length + ASCII-bit is encoded as base 128, most significant heptet first, |
| 1787 | // with a 8th bit being non-zero while there are more heptets. |
| 1788 | // The value encodes the number of bytes following, and whether the original |
| 1789 | // was ASCII. |
| 1790 | byte* s1 = reinterpret_cast<byte*>(first); |
| 1791 | byte* s2 = reinterpret_cast<byte*>(second); |
| 1792 | uint32_t length_ascii_field = 0; |
| 1793 | byte c1; |
| 1794 | do { |
| 1795 | c1 = *s1; |
| 1796 | if (c1 != *s2) return false; |
| 1797 | length_ascii_field = (length_ascii_field << 7) | (c1 & 0x7f); |
| 1798 | s1++; |
| 1799 | s2++; |
| 1800 | } while ((c1 & 0x80) != 0); |
| 1801 | int length = static_cast<int>(length_ascii_field >> 1); |
| 1802 | return memcmp(s1, s2, length) == 0; |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | byte* DuplicateFinder::BackupKey(i::Vector<const byte> bytes, |
| 1807 | bool is_ascii) { |
| 1808 | uint32_t ascii_length = (bytes.length() << 1) | (is_ascii ? 1 : 0); |
| 1809 | backing_store_.StartSequence(); |
| 1810 | // Emit ascii_length as base-128 encoded number, with the 7th bit set |
| 1811 | // on the byte of every heptet except the last, least significant, one. |
| 1812 | if (ascii_length >= (1 << 7)) { |
| 1813 | if (ascii_length >= (1 << 14)) { |
| 1814 | if (ascii_length >= (1 << 21)) { |
| 1815 | if (ascii_length >= (1 << 28)) { |
| 1816 | backing_store_.Add(static_cast<byte>((ascii_length >> 28) | 0x80)); |
| 1817 | } |
| 1818 | backing_store_.Add(static_cast<byte>((ascii_length >> 21) | 0x80u)); |
| 1819 | } |
| 1820 | backing_store_.Add(static_cast<byte>((ascii_length >> 14) | 0x80u)); |
| 1821 | } |
| 1822 | backing_store_.Add(static_cast<byte>((ascii_length >> 7) | 0x80u)); |
| 1823 | } |
| 1824 | backing_store_.Add(static_cast<byte>(ascii_length & 0x7f)); |
| 1825 | |
| 1826 | backing_store_.AddBlock(bytes); |
| 1827 | return backing_store_.EndSequence().start(); |
| 1828 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1829 | } } // v8::preparser |