yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +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 | |
| 28 | #ifndef V8_PREPARSER_H |
| 29 | #define V8_PREPARSER_H |
| 30 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 31 | #include "hashmap.h" |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 32 | #include "token.h" |
| 33 | #include "scanner.h" |
| 34 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 35 | namespace v8 { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 36 | |
| 37 | namespace internal { |
| 38 | class UnicodeCache; |
| 39 | } |
| 40 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 41 | namespace preparser { |
| 42 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 43 | typedef uint8_t byte; |
| 44 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 45 | // Preparsing checks a JavaScript program and emits preparse-data that helps |
| 46 | // a later parsing to be faster. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 47 | // See preparse-data-format.h for the data format. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 48 | |
| 49 | // The PreParser checks that the syntax follows the grammar for JavaScript, |
| 50 | // and collects some information about the program along the way. |
| 51 | // The grammar check is only performed in order to understand the program |
| 52 | // sufficiently to deduce some information about it, that can be used |
| 53 | // to speed up later parsing. Finding errors is not the goal of pre-parsing, |
| 54 | // rather it is to speed up properly written and correct programs. |
| 55 | // That means that contextual checks (like a label being declared where |
| 56 | // it is used) are generally omitted. |
| 57 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 58 | namespace i = v8::internal; |
| 59 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 60 | class DuplicateFinder { |
| 61 | public: |
| 62 | explicit DuplicateFinder(i::UnicodeCache* constants) |
| 63 | : unicode_constants_(constants), |
| 64 | backing_store_(16), |
| 65 | map_(&Match) { } |
| 66 | |
| 67 | int AddAsciiSymbol(i::Vector<const char> key, int value); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 68 | int AddUtf16Symbol(i::Vector<const uint16_t> key, int value); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 69 | // Add a a number literal by converting it (if necessary) |
| 70 | // to the string that ToString(ToNumber(literal)) would generate. |
| 71 | // and then adding that string with AddAsciiSymbol. |
| 72 | // This string is the actual value used as key in an object literal, |
| 73 | // and the one that must be different from the other keys. |
| 74 | int AddNumber(i::Vector<const char> key, int value); |
| 75 | |
| 76 | private: |
| 77 | int AddSymbol(i::Vector<const byte> key, bool is_ascii, int value); |
| 78 | // Backs up the key and its length in the backing store. |
| 79 | // The backup is stored with a base 127 encoding of the |
| 80 | // length (plus a bit saying whether the string is ASCII), |
| 81 | // followed by the bytes of the key. |
| 82 | byte* BackupKey(i::Vector<const byte> key, bool is_ascii); |
| 83 | |
| 84 | // Compare two encoded keys (both pointing into the backing store) |
| 85 | // for having the same base-127 encoded lengths and ASCII-ness, |
| 86 | // and then having the same 'length' bytes following. |
| 87 | static bool Match(void* first, void* second); |
| 88 | // Creates a hash from a sequence of bytes. |
| 89 | static uint32_t Hash(i::Vector<const byte> key, bool is_ascii); |
| 90 | // Checks whether a string containing a JS number is its canonical |
| 91 | // form. |
| 92 | static bool IsNumberCanonical(i::Vector<const char> key); |
| 93 | |
| 94 | // Size of buffer. Sufficient for using it to call DoubleToCString in |
| 95 | // from conversions.h. |
| 96 | static const int kBufferSize = 100; |
| 97 | |
| 98 | i::UnicodeCache* unicode_constants_; |
| 99 | // Backing store used to store strings used as hashmap keys. |
| 100 | i::SequenceCollector<unsigned char> backing_store_; |
| 101 | i::HashMap map_; |
| 102 | // Buffer used for string->number->canonical string conversions. |
| 103 | char number_buffer_[kBufferSize]; |
| 104 | }; |
| 105 | |
| 106 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 107 | class PreParser { |
| 108 | public: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 109 | enum PreParseResult { |
| 110 | kPreParseStackOverflow, |
| 111 | kPreParseSuccess |
| 112 | }; |
| 113 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 114 | |
| 115 | PreParser(i::Scanner* scanner, |
| 116 | i::ParserRecorder* log, |
| 117 | uintptr_t stack_limit, |
| 118 | bool allow_lazy, |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 119 | bool allow_natives_syntax, |
| 120 | bool allow_modules) |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 121 | : scanner_(scanner), |
| 122 | log_(log), |
| 123 | scope_(NULL), |
| 124 | stack_limit_(stack_limit), |
| 125 | strict_mode_violation_location_(i::Scanner::Location::invalid()), |
| 126 | strict_mode_violation_type_(NULL), |
| 127 | stack_overflow_(false), |
| 128 | allow_lazy_(allow_lazy), |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 129 | allow_modules_(allow_modules), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 130 | allow_natives_syntax_(allow_natives_syntax), |
| 131 | parenthesized_function_(false), |
| 132 | harmony_scoping_(scanner->HarmonyScoping()) { } |
| 133 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 134 | ~PreParser() {} |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 135 | |
| 136 | // Pre-parse the program from the character stream; returns true on |
| 137 | // success (even if parsing failed, the pre-parse data successfully |
| 138 | // captured the syntax error), and false if a stack-overflow happened |
| 139 | // during parsing. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 140 | static PreParseResult PreParseProgram(i::Scanner* scanner, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 141 | i::ParserRecorder* log, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 142 | int flags, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 143 | uintptr_t stack_limit) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 144 | bool allow_lazy = (flags & i::kAllowLazy) != 0; |
| 145 | bool allow_natives_syntax = (flags & i::kAllowNativesSyntax) != 0; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 146 | bool allow_modules = (flags & i::kAllowModules) != 0; |
| 147 | return PreParser(scanner, log, stack_limit, allow_lazy, |
| 148 | allow_natives_syntax, allow_modules).PreParse(); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 149 | } |
| 150 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 151 | // Parses a single function literal, from the opening parentheses before |
| 152 | // parameters to the closing brace after the body. |
| 153 | // Returns a FunctionEntry describing the body of the funciton in enough |
| 154 | // detail that it can be lazily compiled. |
| 155 | // The scanner is expected to have matched the "function" keyword and |
| 156 | // parameters, and have consumed the initial '{'. |
| 157 | // At return, unless an error occured, the scanner is positioned before the |
| 158 | // the final '}'. |
| 159 | PreParseResult PreParseLazyFunction(i::LanguageMode mode, |
| 160 | i::ParserRecorder* log); |
| 161 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 162 | private: |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 163 | // Used to detect duplicates in object literals. Each of the values |
| 164 | // kGetterProperty, kSetterProperty and kValueProperty represents |
| 165 | // a type of object literal property. When parsing a property, its |
| 166 | // type value is stored in the DuplicateFinder for the property name. |
| 167 | // Values are chosen so that having intersection bits means the there is |
| 168 | // an incompatibility. |
| 169 | // I.e., you can add a getter to a property that already has a setter, since |
| 170 | // kGetterProperty and kSetterProperty doesn't intersect, but not if it |
| 171 | // already has a getter or a value. Adding the getter to an existing |
| 172 | // setter will store the value (kGetterProperty | kSetterProperty), which |
| 173 | // is incompatible with adding any further properties. |
| 174 | enum PropertyType { |
| 175 | kNone = 0, |
| 176 | // Bit patterns representing different object literal property types. |
| 177 | kGetterProperty = 1, |
| 178 | kSetterProperty = 2, |
| 179 | kValueProperty = 7, |
| 180 | // Helper constants. |
| 181 | kValueFlag = 4 |
| 182 | }; |
| 183 | |
| 184 | // Checks the type of conflict based on values coming from PropertyType. |
| 185 | bool HasConflict(int type1, int type2) { return (type1 & type2) != 0; } |
| 186 | bool IsDataDataConflict(int type1, int type2) { |
| 187 | return ((type1 & type2) & kValueFlag) != 0; |
| 188 | } |
| 189 | bool IsDataAccessorConflict(int type1, int type2) { |
| 190 | return ((type1 ^ type2) & kValueFlag) != 0; |
| 191 | } |
| 192 | bool IsAccessorAccessorConflict(int type1, int type2) { |
| 193 | return ((type1 | type2) & kValueFlag) == 0; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void CheckDuplicate(DuplicateFinder* finder, |
| 198 | i::Token::Value property, |
| 199 | int type, |
| 200 | bool* ok); |
| 201 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 202 | // These types form an algebra over syntactic categories that is just |
| 203 | // rich enough to let us recognize and propagate the constructs that |
| 204 | // are either being counted in the preparser data, or is important |
| 205 | // to throw the correct syntax error exceptions. |
| 206 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 207 | enum ScopeType { |
| 208 | kTopLevelScope, |
| 209 | kFunctionScope |
| 210 | }; |
| 211 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 212 | enum VariableDeclarationContext { |
| 213 | kSourceElement, |
| 214 | kStatement, |
| 215 | kForStatement |
| 216 | }; |
| 217 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 218 | // If a list of variable declarations includes any initializers. |
| 219 | enum VariableDeclarationProperties { |
| 220 | kHasInitializers, |
| 221 | kHasNoInitializers |
| 222 | }; |
| 223 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 224 | class Expression; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 225 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 226 | class Identifier { |
| 227 | public: |
| 228 | static Identifier Default() { |
| 229 | return Identifier(kUnknownIdentifier); |
| 230 | } |
| 231 | static Identifier Eval() { |
| 232 | return Identifier(kEvalIdentifier); |
| 233 | } |
| 234 | static Identifier Arguments() { |
| 235 | return Identifier(kArgumentsIdentifier); |
| 236 | } |
| 237 | static Identifier FutureReserved() { |
| 238 | return Identifier(kFutureReservedIdentifier); |
| 239 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 240 | static Identifier FutureStrictReserved() { |
| 241 | return Identifier(kFutureStrictReservedIdentifier); |
| 242 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 243 | bool IsEval() { return type_ == kEvalIdentifier; } |
| 244 | bool IsArguments() { return type_ == kArgumentsIdentifier; } |
| 245 | bool IsEvalOrArguments() { return type_ >= kEvalIdentifier; } |
| 246 | bool IsFutureReserved() { return type_ == kFutureReservedIdentifier; } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 247 | bool IsFutureStrictReserved() { |
| 248 | return type_ == kFutureStrictReservedIdentifier; |
| 249 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 250 | bool IsValidStrictVariable() { return type_ == kUnknownIdentifier; } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 251 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 252 | private: |
| 253 | enum Type { |
| 254 | kUnknownIdentifier, |
| 255 | kFutureReservedIdentifier, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 256 | kFutureStrictReservedIdentifier, |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 257 | kEvalIdentifier, |
| 258 | kArgumentsIdentifier |
| 259 | }; |
| 260 | explicit Identifier(Type type) : type_(type) { } |
| 261 | Type type_; |
| 262 | |
| 263 | friend class Expression; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 266 | // Bits 0 and 1 are used to identify the type of expression: |
| 267 | // If bit 0 is set, it's an identifier. |
| 268 | // if bit 1 is set, it's a string literal. |
| 269 | // If neither is set, it's no particular type, and both set isn't |
| 270 | // use yet. |
| 271 | // Bit 2 is used to mark the expression as being parenthesized, |
| 272 | // so "(foo)" isn't recognized as a pure identifier (and possible label). |
| 273 | class Expression { |
| 274 | public: |
| 275 | static Expression Default() { |
| 276 | return Expression(kUnknownExpression); |
| 277 | } |
| 278 | |
| 279 | static Expression FromIdentifier(Identifier id) { |
| 280 | return Expression(kIdentifierFlag | (id.type_ << kIdentifierShift)); |
| 281 | } |
| 282 | |
| 283 | static Expression StringLiteral() { |
| 284 | return Expression(kUnknownStringLiteral); |
| 285 | } |
| 286 | |
| 287 | static Expression UseStrictStringLiteral() { |
| 288 | return Expression(kUseStrictString); |
| 289 | } |
| 290 | |
| 291 | static Expression This() { |
| 292 | return Expression(kThisExpression); |
| 293 | } |
| 294 | |
| 295 | static Expression ThisProperty() { |
| 296 | return Expression(kThisPropertyExpression); |
| 297 | } |
| 298 | |
| 299 | static Expression StrictFunction() { |
| 300 | return Expression(kStrictFunctionExpression); |
| 301 | } |
| 302 | |
| 303 | bool IsIdentifier() { |
| 304 | return (code_ & kIdentifierFlag) != 0; |
| 305 | } |
| 306 | |
| 307 | // Only works corretly if it is actually an identifier expression. |
| 308 | PreParser::Identifier AsIdentifier() { |
| 309 | return PreParser::Identifier( |
| 310 | static_cast<PreParser::Identifier::Type>(code_ >> kIdentifierShift)); |
| 311 | } |
| 312 | |
| 313 | bool IsParenthesized() { |
| 314 | // If bit 0 or 1 is set, we interpret bit 2 as meaning parenthesized. |
| 315 | return (code_ & 7) > 4; |
| 316 | } |
| 317 | |
| 318 | bool IsRawIdentifier() { |
| 319 | return !IsParenthesized() && IsIdentifier(); |
| 320 | } |
| 321 | |
| 322 | bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; } |
| 323 | |
| 324 | bool IsRawStringLiteral() { |
| 325 | return !IsParenthesized() && IsStringLiteral(); |
| 326 | } |
| 327 | |
| 328 | bool IsUseStrictLiteral() { |
| 329 | return (code_ & kStringLiteralMask) == kUseStrictString; |
| 330 | } |
| 331 | |
| 332 | bool IsThis() { |
| 333 | return code_ == kThisExpression; |
| 334 | } |
| 335 | |
| 336 | bool IsThisProperty() { |
| 337 | return code_ == kThisPropertyExpression; |
| 338 | } |
| 339 | |
| 340 | bool IsStrictFunction() { |
| 341 | return code_ == kStrictFunctionExpression; |
| 342 | } |
| 343 | |
| 344 | Expression Parenthesize() { |
| 345 | int type = code_ & 3; |
| 346 | if (type != 0) { |
| 347 | // Identifiers and string literals can be parenthesized. |
| 348 | // They no longer work as labels or directive prologues, |
| 349 | // but are still recognized in other contexts. |
| 350 | return Expression(code_ | kParentesizedExpressionFlag); |
| 351 | } |
| 352 | // For other types of expressions, it's not important to remember |
| 353 | // the parentheses. |
| 354 | return *this; |
| 355 | } |
| 356 | |
| 357 | private: |
| 358 | // First two/three bits are used as flags. |
| 359 | // Bit 0 and 1 represent identifiers or strings literals, and are |
| 360 | // mutually exclusive, but can both be absent. |
| 361 | // If bit 0 or 1 are set, bit 2 marks that the expression has |
| 362 | // been wrapped in parentheses (a string literal can no longer |
| 363 | // be a directive prologue, and an identifier can no longer be |
| 364 | // a label. |
| 365 | enum { |
| 366 | kUnknownExpression = 0, |
| 367 | // Identifiers |
| 368 | kIdentifierFlag = 1, // Used to detect labels. |
| 369 | kIdentifierShift = 3, |
| 370 | |
| 371 | kStringLiteralFlag = 2, // Used to detect directive prologue. |
| 372 | kUnknownStringLiteral = kStringLiteralFlag, |
| 373 | kUseStrictString = kStringLiteralFlag | 8, |
| 374 | kStringLiteralMask = kUseStrictString, |
| 375 | |
| 376 | kParentesizedExpressionFlag = 4, // Only if identifier or string literal. |
| 377 | |
| 378 | // Below here applies if neither identifier nor string literal. |
| 379 | kThisExpression = 4, |
| 380 | kThisPropertyExpression = 8, |
| 381 | kStrictFunctionExpression = 12 |
| 382 | }; |
| 383 | |
| 384 | explicit Expression(int expression_code) : code_(expression_code) { } |
| 385 | |
| 386 | int code_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 389 | class Statement { |
| 390 | public: |
| 391 | static Statement Default() { |
| 392 | return Statement(kUnknownStatement); |
| 393 | } |
| 394 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 395 | static Statement FunctionDeclaration() { |
| 396 | return Statement(kFunctionDeclaration); |
| 397 | } |
| 398 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 399 | // Creates expression statement from expression. |
| 400 | // Preserves being an unparenthesized string literal, possibly |
| 401 | // "use strict". |
| 402 | static Statement ExpressionStatement(Expression expression) { |
| 403 | if (!expression.IsParenthesized()) { |
| 404 | if (expression.IsUseStrictLiteral()) { |
| 405 | return Statement(kUseStrictExpressionStatement); |
| 406 | } |
| 407 | if (expression.IsStringLiteral()) { |
| 408 | return Statement(kStringLiteralExpressionStatement); |
| 409 | } |
| 410 | } |
| 411 | return Default(); |
| 412 | } |
| 413 | |
| 414 | bool IsStringLiteral() { |
| 415 | return code_ != kUnknownStatement; |
| 416 | } |
| 417 | |
| 418 | bool IsUseStrictLiteral() { |
| 419 | return code_ == kUseStrictExpressionStatement; |
| 420 | } |
| 421 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 422 | bool IsFunctionDeclaration() { |
| 423 | return code_ == kFunctionDeclaration; |
| 424 | } |
| 425 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 426 | private: |
| 427 | enum Type { |
| 428 | kUnknownStatement, |
| 429 | kStringLiteralExpressionStatement, |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 430 | kUseStrictExpressionStatement, |
| 431 | kFunctionDeclaration |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | explicit Statement(Type code) : code_(code) {} |
| 435 | Type code_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 438 | enum SourceElements { |
| 439 | kUnknownSourceElements |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 442 | typedef int Arguments; |
| 443 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 444 | class Scope { |
| 445 | public: |
| 446 | Scope(Scope** variable, ScopeType type) |
| 447 | : variable_(variable), |
| 448 | prev_(*variable), |
| 449 | type_(type), |
| 450 | materialized_literal_count_(0), |
| 451 | expected_properties_(0), |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 452 | with_nesting_count_(0), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 453 | language_mode_( |
| 454 | (prev_ != NULL) ? prev_->language_mode() : i::CLASSIC_MODE) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 455 | *variable = this; |
| 456 | } |
| 457 | ~Scope() { *variable_ = prev_; } |
| 458 | void NextMaterializedLiteralIndex() { materialized_literal_count_++; } |
| 459 | void AddProperty() { expected_properties_++; } |
| 460 | ScopeType type() { return type_; } |
| 461 | int expected_properties() { return expected_properties_; } |
| 462 | int materialized_literal_count() { return materialized_literal_count_; } |
| 463 | bool IsInsideWith() { return with_nesting_count_ != 0; } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 464 | bool is_classic_mode() { |
| 465 | return language_mode_ == i::CLASSIC_MODE; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 466 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 467 | i::LanguageMode language_mode() { |
| 468 | return language_mode_; |
| 469 | } |
| 470 | void set_language_mode(i::LanguageMode language_mode) { |
| 471 | language_mode_ = language_mode; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 472 | } |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 473 | |
| 474 | class InsideWith { |
| 475 | public: |
| 476 | explicit InsideWith(Scope* scope) : scope_(scope) { |
| 477 | scope->with_nesting_count_++; |
| 478 | } |
| 479 | |
| 480 | ~InsideWith() { scope_->with_nesting_count_--; } |
| 481 | |
| 482 | private: |
| 483 | Scope* scope_; |
| 484 | DISALLOW_COPY_AND_ASSIGN(InsideWith); |
| 485 | }; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 486 | |
| 487 | private: |
| 488 | Scope** const variable_; |
| 489 | Scope* const prev_; |
| 490 | const ScopeType type_; |
| 491 | int materialized_literal_count_; |
| 492 | int expected_properties_; |
| 493 | int with_nesting_count_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 494 | i::LanguageMode language_mode_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 495 | }; |
| 496 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 497 | // Preparse the program. Only called in PreParseProgram after creating |
| 498 | // the instance. |
| 499 | PreParseResult PreParse() { |
| 500 | Scope top_scope(&scope_, kTopLevelScope); |
| 501 | bool ok = true; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 502 | int start_position = scanner_->peek_location().beg_pos; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 503 | ParseSourceElements(i::Token::EOS, &ok); |
| 504 | if (stack_overflow_) return kPreParseStackOverflow; |
| 505 | if (!ok) { |
| 506 | ReportUnexpectedToken(scanner_->current_token()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 507 | } else if (!scope_->is_classic_mode()) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 508 | CheckOctalLiteral(start_position, scanner_->location().end_pos, &ok); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 509 | } |
| 510 | return kPreParseSuccess; |
| 511 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 512 | |
| 513 | // Report syntax error |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 514 | void ReportUnexpectedToken(i::Token::Value token); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 515 | void ReportMessageAt(i::Scanner::Location location, |
| 516 | const char* type, |
| 517 | const char* name_opt) { |
| 518 | log_->LogMessage(location.beg_pos, location.end_pos, type, name_opt); |
| 519 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 520 | void ReportMessageAt(int start_pos, |
| 521 | int end_pos, |
| 522 | const char* type, |
| 523 | const char* name_opt) { |
| 524 | log_->LogMessage(start_pos, end_pos, type, name_opt); |
| 525 | } |
| 526 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 527 | void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok); |
| 528 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 529 | // All ParseXXX functions take as the last argument an *ok parameter |
| 530 | // which is set to false if parsing failed; it is unchanged otherwise. |
| 531 | // By making the 'exception handling' explicit, we are forced to check |
| 532 | // for failure at the call sites. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 533 | Statement ParseSourceElement(bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 534 | SourceElements ParseSourceElements(int end_token, bool* ok); |
| 535 | Statement ParseStatement(bool* ok); |
| 536 | Statement ParseFunctionDeclaration(bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 537 | Statement ParseBlock(bool* ok); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 538 | Statement ParseVariableStatement(VariableDeclarationContext var_context, |
| 539 | bool* ok); |
| 540 | Statement ParseVariableDeclarations(VariableDeclarationContext var_context, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 541 | VariableDeclarationProperties* decl_props, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 542 | int* num_decl, |
| 543 | bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 544 | Statement ParseExpressionOrLabelledStatement(bool* ok); |
| 545 | Statement ParseIfStatement(bool* ok); |
| 546 | Statement ParseContinueStatement(bool* ok); |
| 547 | Statement ParseBreakStatement(bool* ok); |
| 548 | Statement ParseReturnStatement(bool* ok); |
| 549 | Statement ParseWithStatement(bool* ok); |
| 550 | Statement ParseSwitchStatement(bool* ok); |
| 551 | Statement ParseDoWhileStatement(bool* ok); |
| 552 | Statement ParseWhileStatement(bool* ok); |
| 553 | Statement ParseForStatement(bool* ok); |
| 554 | Statement ParseThrowStatement(bool* ok); |
| 555 | Statement ParseTryStatement(bool* ok); |
| 556 | Statement ParseDebuggerStatement(bool* ok); |
| 557 | |
| 558 | Expression ParseExpression(bool accept_IN, bool* ok); |
| 559 | Expression ParseAssignmentExpression(bool accept_IN, bool* ok); |
| 560 | Expression ParseConditionalExpression(bool accept_IN, bool* ok); |
| 561 | Expression ParseBinaryExpression(int prec, bool accept_IN, bool* ok); |
| 562 | Expression ParseUnaryExpression(bool* ok); |
| 563 | Expression ParsePostfixExpression(bool* ok); |
| 564 | Expression ParseLeftHandSideExpression(bool* ok); |
| 565 | Expression ParseNewExpression(bool* ok); |
| 566 | Expression ParseMemberExpression(bool* ok); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 567 | Expression ParseMemberWithNewPrefixesExpression(unsigned new_count, bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 568 | Expression ParsePrimaryExpression(bool* ok); |
| 569 | Expression ParseArrayLiteral(bool* ok); |
| 570 | Expression ParseObjectLiteral(bool* ok); |
| 571 | Expression ParseRegExpLiteral(bool seen_equal, bool* ok); |
| 572 | Expression ParseV8Intrinsic(bool* ok); |
| 573 | |
| 574 | Arguments ParseArguments(bool* ok); |
| 575 | Expression ParseFunctionLiteral(bool* ok); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 576 | void ParseLazyFunctionLiteralBody(bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 577 | |
| 578 | Identifier ParseIdentifier(bool* ok); |
| 579 | Identifier ParseIdentifierName(bool* ok); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 580 | Identifier ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 581 | bool* is_set, |
| 582 | bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 583 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 584 | // Logs the currently parsed literal as a symbol in the preparser data. |
| 585 | void LogSymbol(); |
| 586 | // Log the currently parsed identifier. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 587 | Identifier GetIdentifierSymbol(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 588 | // Log the currently parsed string literal. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 589 | Expression GetStringSymbol(); |
| 590 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 591 | i::Token::Value peek() { |
| 592 | if (stack_overflow_) return i::Token::ILLEGAL; |
| 593 | return scanner_->peek(); |
| 594 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 595 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 596 | i::Token::Value Next() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 597 | if (stack_overflow_) return i::Token::ILLEGAL; |
| 598 | { |
| 599 | int marker; |
| 600 | if (reinterpret_cast<uintptr_t>(&marker) < stack_limit_) { |
| 601 | // Further calls to peek/Next will return illegal token. |
| 602 | // The current one will still be returned. It might already |
| 603 | // have been seen using peek. |
| 604 | stack_overflow_ = true; |
| 605 | } |
| 606 | } |
| 607 | return scanner_->Next(); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 608 | } |
| 609 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 610 | bool peek_any_identifier(); |
| 611 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 612 | void set_language_mode(i::LanguageMode language_mode) { |
| 613 | scope_->set_language_mode(language_mode); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 614 | } |
| 615 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 616 | bool is_classic_mode() { |
| 617 | return scope_->language_mode() == i::CLASSIC_MODE; |
| 618 | } |
erik.corry@gmail.com | 6e28b56 | 2011-10-27 14:20:17 +0000 | [diff] [blame] | 619 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 620 | bool is_extended_mode() { |
| 621 | return scope_->language_mode() == i::EXTENDED_MODE; |
| 622 | } |
| 623 | |
| 624 | i::LanguageMode language_mode() { return scope_->language_mode(); } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 625 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 626 | void Consume(i::Token::Value token) { Next(); } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 627 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 628 | void Expect(i::Token::Value token, bool* ok) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 629 | if (Next() != token) { |
| 630 | *ok = false; |
| 631 | } |
| 632 | } |
| 633 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 634 | bool Check(i::Token::Value token) { |
| 635 | i::Token::Value next = peek(); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 636 | if (next == token) { |
| 637 | Consume(next); |
| 638 | return true; |
| 639 | } |
| 640 | return false; |
| 641 | } |
| 642 | void ExpectSemicolon(bool* ok); |
| 643 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 644 | static int Precedence(i::Token::Value tok, bool accept_IN); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 645 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 646 | void SetStrictModeViolation(i::Scanner::Location, |
| 647 | const char* type, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 648 | bool* ok); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 649 | |
| 650 | void CheckDelayedStrictModeViolation(int beg_pos, int end_pos, bool* ok); |
| 651 | |
| 652 | void StrictModeIdentifierViolation(i::Scanner::Location, |
| 653 | const char* eval_args_type, |
| 654 | Identifier identifier, |
| 655 | bool* ok); |
| 656 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 657 | i::Scanner* scanner_; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 658 | i::ParserRecorder* log_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 659 | Scope* scope_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 660 | uintptr_t stack_limit_; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 661 | i::Scanner::Location strict_mode_violation_location_; |
| 662 | const char* strict_mode_violation_type_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 663 | bool stack_overflow_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 664 | bool allow_lazy_; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 665 | bool allow_modules_; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 666 | bool allow_natives_syntax_; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 667 | bool parenthesized_function_; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 668 | bool harmony_scoping_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 669 | }; |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 670 | } } // v8::preparser |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 671 | |
| 672 | #endif // V8_PREPARSER_H |