karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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_PARSER_H_ |
| 29 | #define V8_PARSER_H_ |
| 30 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 32 | #include "ast.h" |
| 33 | #include "scanner.h" |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 34 | #include "scopes.h" |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 35 | #include "preparse-data-format.h" |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 36 | #include "preparse-data.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 38 | namespace v8 { |
| 39 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 41 | class CompilationInfo; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 42 | class FuncNameInferrer; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 43 | class ParserLog; |
| 44 | class PositionStack; |
| 45 | class Target; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 46 | class LexicalScope; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 47 | |
| 48 | template <typename T> class ZoneListWrapper; |
| 49 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | |
| 51 | class ParserMessage : public Malloced { |
| 52 | public: |
| 53 | ParserMessage(Scanner::Location loc, const char* message, |
| 54 | Vector<const char*> args) |
| 55 | : loc_(loc), |
| 56 | message_(message), |
| 57 | args_(args) { } |
| 58 | ~ParserMessage(); |
| 59 | Scanner::Location location() { return loc_; } |
| 60 | const char* message() { return message_; } |
| 61 | Vector<const char*> args() { return args_; } |
| 62 | private: |
| 63 | Scanner::Location loc_; |
| 64 | const char* message_; |
| 65 | Vector<const char*> args_; |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | class FunctionEntry BASE_EMBEDDED { |
| 70 | public: |
| 71 | explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { } |
| 72 | FunctionEntry() : backing_(Vector<unsigned>::empty()) { } |
| 73 | |
| 74 | int start_pos() { return backing_[kStartPosOffset]; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | int end_pos() { return backing_[kEndPosOffset]; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | int literal_count() { return backing_[kLiteralCountOffset]; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | int property_count() { return backing_[kPropertyCountOffset]; } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 78 | bool strict_mode() { return backing_[kStrictModeOffset] != 0; } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 79 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | bool is_valid() { return backing_.length() > 0; } |
| 81 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 82 | static const int kSize = 5; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | Vector<unsigned> backing_; |
| 86 | static const int kStartPosOffset = 0; |
| 87 | static const int kEndPosOffset = 1; |
| 88 | static const int kLiteralCountOffset = 2; |
| 89 | static const int kPropertyCountOffset = 3; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 90 | static const int kStrictModeOffset = 4; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | |
| 94 | class ScriptDataImpl : public ScriptData { |
| 95 | public: |
| 96 | explicit ScriptDataImpl(Vector<unsigned> store) |
| 97 | : store_(store), |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 98 | owns_store_(true) { } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 99 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 100 | // Create an empty ScriptDataImpl that is guaranteed to not satisfy |
| 101 | // a SanityCheck. |
| 102 | ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { } |
| 103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | virtual ~ScriptDataImpl(); |
| 105 | virtual int Length(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 106 | virtual const char* Data(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 107 | virtual bool HasError(); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 108 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 109 | void Initialize(); |
| 110 | void ReadNextSymbolPosition(); |
| 111 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 112 | FunctionEntry GetFunctionEntry(int start); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 113 | int GetSymbolIdentifier(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | bool SanityCheck(); |
| 115 | |
| 116 | Scanner::Location MessageLocation(); |
| 117 | const char* BuildMessage(); |
| 118 | Vector<const char*> BuildArgs(); |
| 119 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 120 | int symbol_count() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 121 | return (store_.length() > PreparseDataConstants::kHeaderSize) |
| 122 | ? store_[PreparseDataConstants::kSymbolCountOffset] |
| 123 | : 0; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 124 | } |
| 125 | // The following functions should only be called if SanityCheck has |
| 126 | // returned true. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 127 | bool has_error() { return store_[PreparseDataConstants::kHasErrorOffset]; } |
| 128 | unsigned magic() { return store_[PreparseDataConstants::kMagicOffset]; } |
| 129 | unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | private: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 132 | Vector<unsigned> store_; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 133 | unsigned char* symbol_data_; |
| 134 | unsigned char* symbol_data_end_; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 135 | int function_index_; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 136 | bool owns_store_; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 137 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | unsigned Read(int position); |
| 139 | unsigned* ReadAddress(int position); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 140 | // Reads a number from the current symbols |
| 141 | int ReadNumber(byte** source); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 143 | ScriptDataImpl(const char* backing_store, int length) |
| 144 | : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 145 | length / static_cast<int>(sizeof(unsigned))), |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 146 | owns_store_(false) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 147 | ASSERT_EQ(0, static_cast<int>( |
| 148 | reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned))); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 149 | } |
| 150 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 151 | // Read strings written by ParserRecorder::WriteString. |
| 152 | static const char* ReadString(unsigned* start, int* chars); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 153 | |
| 154 | friend class ScriptData; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 158 | class ParserApi { |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 159 | public: |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 160 | // Parses the source code represented by the compilation info and sets its |
| 161 | // function literal. Returns false (and deallocates any allocated AST |
| 162 | // nodes) if parsing failed. |
| 163 | static bool Parse(CompilationInfo* info); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 164 | |
| 165 | // Generic preparser generating full preparse data. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 166 | static ScriptDataImpl* PreParse(UC16CharacterStream* source, |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 167 | v8::Extension* extension); |
| 168 | |
| 169 | // Preparser that only does preprocessing that makes sense if only used |
| 170 | // immediately after. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 171 | static ScriptDataImpl* PartialPreParse(UC16CharacterStream* source, |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 172 | v8::Extension* extension); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 175 | // ---------------------------------------------------------------------------- |
| 176 | // REGEXP PARSING |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 177 | |
| 178 | // A BuffferedZoneList is an automatically growing list, just like (and backed |
| 179 | // by) a ZoneList, that is optimized for the case of adding and removing |
| 180 | // a single element. The last element added is stored outside the backing list, |
| 181 | // and if no more than one element is ever added, the ZoneList isn't even |
| 182 | // allocated. |
| 183 | // Elements must not be NULL pointers. |
| 184 | template <typename T, int initial_size> |
| 185 | class BufferedZoneList { |
| 186 | public: |
| 187 | BufferedZoneList() : list_(NULL), last_(NULL) {} |
| 188 | |
| 189 | // Adds element at end of list. This element is buffered and can |
| 190 | // be read using last() or removed using RemoveLast until a new Add or until |
| 191 | // RemoveLast or GetList has been called. |
| 192 | void Add(T* value) { |
| 193 | if (last_ != NULL) { |
| 194 | if (list_ == NULL) { |
| 195 | list_ = new ZoneList<T*>(initial_size); |
| 196 | } |
| 197 | list_->Add(last_); |
| 198 | } |
| 199 | last_ = value; |
| 200 | } |
| 201 | |
| 202 | T* last() { |
| 203 | ASSERT(last_ != NULL); |
| 204 | return last_; |
| 205 | } |
| 206 | |
| 207 | T* RemoveLast() { |
| 208 | ASSERT(last_ != NULL); |
| 209 | T* result = last_; |
| 210 | if ((list_ != NULL) && (list_->length() > 0)) |
| 211 | last_ = list_->RemoveLast(); |
| 212 | else |
| 213 | last_ = NULL; |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | T* Get(int i) { |
| 218 | ASSERT((0 <= i) && (i < length())); |
| 219 | if (list_ == NULL) { |
| 220 | ASSERT_EQ(0, i); |
| 221 | return last_; |
| 222 | } else { |
| 223 | if (i == list_->length()) { |
| 224 | ASSERT(last_ != NULL); |
| 225 | return last_; |
| 226 | } else { |
| 227 | return list_->at(i); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void Clear() { |
| 233 | list_ = NULL; |
| 234 | last_ = NULL; |
| 235 | } |
| 236 | |
| 237 | int length() { |
| 238 | int length = (list_ == NULL) ? 0 : list_->length(); |
| 239 | return length + ((last_ == NULL) ? 0 : 1); |
| 240 | } |
| 241 | |
| 242 | ZoneList<T*>* GetList() { |
| 243 | if (list_ == NULL) { |
| 244 | list_ = new ZoneList<T*>(initial_size); |
| 245 | } |
| 246 | if (last_ != NULL) { |
| 247 | list_->Add(last_); |
| 248 | last_ = NULL; |
| 249 | } |
| 250 | return list_; |
| 251 | } |
| 252 | |
| 253 | private: |
| 254 | ZoneList<T*>* list_; |
| 255 | T* last_; |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | // Accumulates RegExp atoms and assertions into lists of terms and alternatives. |
| 260 | class RegExpBuilder: public ZoneObject { |
| 261 | public: |
| 262 | RegExpBuilder(); |
| 263 | void AddCharacter(uc16 character); |
| 264 | // "Adds" an empty expression. Does nothing except consume a |
| 265 | // following quantifier |
| 266 | void AddEmpty(); |
| 267 | void AddAtom(RegExpTree* tree); |
| 268 | void AddAssertion(RegExpTree* tree); |
| 269 | void NewAlternative(); // '|' |
| 270 | void AddQuantifierToAtom(int min, int max, RegExpQuantifier::Type type); |
| 271 | RegExpTree* ToRegExp(); |
| 272 | |
| 273 | private: |
| 274 | void FlushCharacters(); |
| 275 | void FlushText(); |
| 276 | void FlushTerms(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 277 | Zone* zone() { return zone_; } |
| 278 | |
| 279 | Zone* zone_; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 280 | bool pending_empty_; |
| 281 | ZoneList<uc16>* characters_; |
| 282 | BufferedZoneList<RegExpTree, 2> terms_; |
| 283 | BufferedZoneList<RegExpTree, 2> text_; |
| 284 | BufferedZoneList<RegExpTree, 2> alternatives_; |
| 285 | #ifdef DEBUG |
| 286 | enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_; |
| 287 | #define LAST(x) last_added_ = x; |
| 288 | #else |
| 289 | #define LAST(x) |
| 290 | #endif |
| 291 | }; |
| 292 | |
| 293 | |
| 294 | class RegExpParser { |
| 295 | public: |
| 296 | RegExpParser(FlatStringReader* in, |
| 297 | Handle<String>* error, |
| 298 | bool multiline_mode); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 299 | |
| 300 | static bool ParseRegExp(FlatStringReader* input, |
| 301 | bool multiline, |
| 302 | RegExpCompileData* result); |
| 303 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 304 | RegExpTree* ParsePattern(); |
| 305 | RegExpTree* ParseDisjunction(); |
| 306 | RegExpTree* ParseGroup(); |
| 307 | RegExpTree* ParseCharacterClass(); |
| 308 | |
| 309 | // Parses a {...,...} quantifier and stores the range in the given |
| 310 | // out parameters. |
| 311 | bool ParseIntervalQuantifier(int* min_out, int* max_out); |
| 312 | |
| 313 | // Parses and returns a single escaped character. The character |
| 314 | // must not be 'b' or 'B' since they are usually handle specially. |
| 315 | uc32 ParseClassCharacterEscape(); |
| 316 | |
| 317 | // Checks whether the following is a length-digit hexadecimal number, |
| 318 | // and sets the value if it is. |
| 319 | bool ParseHexEscape(int length, uc32* value); |
| 320 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 321 | uc32 ParseOctalLiteral(); |
| 322 | |
| 323 | // Tries to parse the input as a back reference. If successful it |
| 324 | // stores the result in the output parameter and returns true. If |
| 325 | // it fails it will push back the characters read so the same characters |
| 326 | // can be reparsed. |
| 327 | bool ParseBackReferenceIndex(int* index_out); |
| 328 | |
| 329 | CharacterRange ParseClassAtom(uc16* char_class); |
| 330 | RegExpTree* ReportError(Vector<const char> message); |
| 331 | void Advance(); |
| 332 | void Advance(int dist); |
| 333 | void Reset(int pos); |
| 334 | |
| 335 | // Reports whether the pattern might be used as a literal search string. |
| 336 | // Only use if the result of the parse is a single atom node. |
| 337 | bool simple(); |
| 338 | bool contains_anchor() { return contains_anchor_; } |
| 339 | void set_contains_anchor() { contains_anchor_ = true; } |
| 340 | int captures_started() { return captures_ == NULL ? 0 : captures_->length(); } |
| 341 | int position() { return next_pos_ - 1; } |
| 342 | bool failed() { return failed_; } |
| 343 | |
| 344 | static const int kMaxCaptures = 1 << 16; |
| 345 | static const uc32 kEndMarker = (1 << 21); |
| 346 | |
| 347 | private: |
| 348 | enum SubexpressionType { |
| 349 | INITIAL, |
| 350 | CAPTURE, // All positive values represent captures. |
| 351 | POSITIVE_LOOKAHEAD, |
| 352 | NEGATIVE_LOOKAHEAD, |
| 353 | GROUPING |
| 354 | }; |
| 355 | |
| 356 | class RegExpParserState : public ZoneObject { |
| 357 | public: |
| 358 | RegExpParserState(RegExpParserState* previous_state, |
| 359 | SubexpressionType group_type, |
| 360 | int disjunction_capture_index) |
| 361 | : previous_state_(previous_state), |
| 362 | builder_(new RegExpBuilder()), |
| 363 | group_type_(group_type), |
| 364 | disjunction_capture_index_(disjunction_capture_index) {} |
| 365 | // Parser state of containing expression, if any. |
| 366 | RegExpParserState* previous_state() { return previous_state_; } |
| 367 | bool IsSubexpression() { return previous_state_ != NULL; } |
| 368 | // RegExpBuilder building this regexp's AST. |
| 369 | RegExpBuilder* builder() { return builder_; } |
| 370 | // Type of regexp being parsed (parenthesized group or entire regexp). |
| 371 | SubexpressionType group_type() { return group_type_; } |
| 372 | // Index in captures array of first capture in this sub-expression, if any. |
| 373 | // Also the capture index of this sub-expression itself, if group_type |
| 374 | // is CAPTURE. |
| 375 | int capture_index() { return disjunction_capture_index_; } |
| 376 | |
| 377 | private: |
| 378 | // Linked list implementation of stack of states. |
| 379 | RegExpParserState* previous_state_; |
| 380 | // Builder for the stored disjunction. |
| 381 | RegExpBuilder* builder_; |
| 382 | // Stored disjunction type (capture, look-ahead or grouping), if any. |
| 383 | SubexpressionType group_type_; |
| 384 | // Stored disjunction's capture index (if any). |
| 385 | int disjunction_capture_index_; |
| 386 | }; |
| 387 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 388 | Isolate* isolate() { return isolate_; } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 389 | Zone* zone() { return isolate_->zone(); } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 390 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 391 | uc32 current() { return current_; } |
| 392 | bool has_more() { return has_more_; } |
| 393 | bool has_next() { return next_pos_ < in()->length(); } |
| 394 | uc32 Next(); |
| 395 | FlatStringReader* in() { return in_; } |
| 396 | void ScanForCaptures(); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 397 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 398 | Isolate* isolate_; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 399 | Handle<String>* error_; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 400 | ZoneList<RegExpCapture*>* captures_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 401 | FlatStringReader* in_; |
| 402 | uc32 current_; |
| 403 | int next_pos_; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 404 | // The capture count is only valid after we have scanned for captures. |
| 405 | int capture_count_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 406 | bool has_more_; |
| 407 | bool multiline_; |
| 408 | bool simple_; |
| 409 | bool contains_anchor_; |
| 410 | bool is_scanned_for_captures_; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 411 | bool failed_; |
| 412 | }; |
| 413 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 414 | // ---------------------------------------------------------------------------- |
| 415 | // JAVASCRIPT PARSING |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 416 | |
| 417 | class Parser { |
| 418 | public: |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 419 | Parser(Handle<Script> script, |
| 420 | bool allow_natives_syntax, |
| 421 | v8::Extension* extension, |
| 422 | ScriptDataImpl* pre_data); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 423 | virtual ~Parser() { } |
| 424 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 425 | // Returns NULL if parsing failed. |
| 426 | FunctionLiteral* ParseProgram(Handle<String> source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 427 | bool in_global_context, |
| 428 | StrictModeFlag strict_mode); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 429 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 430 | FunctionLiteral* ParseLazy(CompilationInfo* info); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 431 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 432 | void ReportMessageAt(Scanner::Location loc, |
| 433 | const char* message, |
| 434 | Vector<const char*> args); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 435 | void ReportMessageAt(Scanner::Location loc, |
| 436 | const char* message, |
| 437 | Vector<Handle<String> > args); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 438 | void SetHarmonyBlockScoping(bool block_scoping); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 439 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 440 | private: |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 441 | // Limit on number of function parameters is chosen arbitrarily. |
| 442 | // Code::Flags uses only the low 17 bits of num-parameters to |
| 443 | // construct a hashable id, so if more than 2^17 are allowed, this |
| 444 | // should be checked. |
| 445 | static const int kMaxNumFunctionParameters = 32766; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 446 | static const int kMaxNumFunctionLocals = 32767; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 447 | FunctionLiteral* ParseLazy(CompilationInfo* info, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 448 | UC16CharacterStream* source, |
| 449 | ZoneScope* zone_scope); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 450 | enum Mode { |
| 451 | PARSE_LAZILY, |
| 452 | PARSE_EAGERLY |
| 453 | }; |
| 454 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 455 | Isolate* isolate() { return isolate_; } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 456 | Zone* zone() { return isolate_->zone(); } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 457 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 458 | // Called by ParseProgram after setting up the scanner. |
| 459 | FunctionLiteral* DoParseProgram(Handle<String> source, |
| 460 | bool in_global_context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 461 | StrictModeFlag strict_mode, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 462 | ZoneScope* zone_scope); |
| 463 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 464 | // Report syntax error |
| 465 | void ReportUnexpectedToken(Token::Value token); |
| 466 | void ReportInvalidPreparseData(Handle<String> name, bool* ok); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 467 | void ReportMessage(const char* message, Vector<const char*> args); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 468 | |
| 469 | bool inside_with() const { return with_nesting_level_ > 0; } |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 470 | JavaScriptScanner& scanner() { return scanner_; } |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 471 | Mode mode() const { return mode_; } |
| 472 | ScriptDataImpl* pre_data() const { return pre_data_; } |
| 473 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 474 | // Check if the given string is 'eval' or 'arguments'. |
| 475 | bool IsEvalOrArguments(Handle<String> string); |
| 476 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 477 | // All ParseXXX functions take as the last argument an *ok parameter |
| 478 | // which is set to false if parsing failed; it is unchanged otherwise. |
| 479 | // By making the 'exception handling' explicit, we are forced to check |
| 480 | // for failure at the call sites. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 481 | void* ParseSourceElements(ZoneList<Statement*>* processor, |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 482 | int end_token, bool* ok); |
| 483 | Statement* ParseStatement(ZoneStringList* labels, bool* ok); |
| 484 | Statement* ParseFunctionDeclaration(bool* ok); |
| 485 | Statement* ParseNativeDeclaration(bool* ok); |
| 486 | Block* ParseBlock(ZoneStringList* labels, bool* ok); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 487 | Block* ParseScopedBlock(ZoneStringList* labels, bool* ok); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 488 | Block* ParseVariableStatement(bool* ok); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 489 | Block* ParseVariableDeclarations(bool accept_IN, |
| 490 | Handle<String>* out, |
| 491 | bool* ok); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 492 | Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels, |
| 493 | bool* ok); |
| 494 | IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok); |
| 495 | Statement* ParseContinueStatement(bool* ok); |
| 496 | Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok); |
| 497 | Statement* ParseReturnStatement(bool* ok); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 498 | Statement* ParseWithStatement(ZoneStringList* labels, bool* ok); |
| 499 | CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok); |
| 500 | SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok); |
| 501 | DoWhileStatement* ParseDoWhileStatement(ZoneStringList* labels, bool* ok); |
| 502 | WhileStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok); |
| 503 | Statement* ParseForStatement(ZoneStringList* labels, bool* ok); |
| 504 | Statement* ParseThrowStatement(bool* ok); |
| 505 | Expression* MakeCatchContext(Handle<String> id, VariableProxy* value); |
| 506 | TryStatement* ParseTryStatement(bool* ok); |
| 507 | DebuggerStatement* ParseDebuggerStatement(bool* ok); |
| 508 | |
| 509 | Expression* ParseExpression(bool accept_IN, bool* ok); |
| 510 | Expression* ParseAssignmentExpression(bool accept_IN, bool* ok); |
| 511 | Expression* ParseConditionalExpression(bool accept_IN, bool* ok); |
| 512 | Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok); |
| 513 | Expression* ParseUnaryExpression(bool* ok); |
| 514 | Expression* ParsePostfixExpression(bool* ok); |
| 515 | Expression* ParseLeftHandSideExpression(bool* ok); |
| 516 | Expression* ParseNewExpression(bool* ok); |
| 517 | Expression* ParseMemberExpression(bool* ok); |
| 518 | Expression* ParseNewPrefix(PositionStack* stack, bool* ok); |
| 519 | Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack, |
| 520 | bool* ok); |
| 521 | Expression* ParsePrimaryExpression(bool* ok); |
| 522 | Expression* ParseArrayLiteral(bool* ok); |
| 523 | Expression* ParseObjectLiteral(bool* ok); |
| 524 | ObjectLiteral::Property* ParseObjectLiteralGetSet(bool is_getter, bool* ok); |
| 525 | Expression* ParseRegExpLiteral(bool seen_equal, bool* ok); |
| 526 | |
| 527 | Expression* NewCompareNode(Token::Value op, |
| 528 | Expression* x, |
| 529 | Expression* y, |
| 530 | int position); |
| 531 | |
| 532 | // Populate the constant properties fixed array for a materialized object |
| 533 | // literal. |
| 534 | void BuildObjectLiteralConstantProperties( |
| 535 | ZoneList<ObjectLiteral::Property*>* properties, |
| 536 | Handle<FixedArray> constants, |
| 537 | bool* is_simple, |
| 538 | bool* fast_elements, |
| 539 | int* depth); |
| 540 | |
| 541 | // Populate the literals fixed array for a materialized array literal. |
| 542 | void BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* properties, |
| 543 | Handle<FixedArray> constants, |
| 544 | bool* is_simple, |
| 545 | int* depth); |
| 546 | |
| 547 | // Decide if a property should be in the object boilerplate. |
| 548 | bool IsBoilerplateProperty(ObjectLiteral::Property* property); |
| 549 | // If the expression is a literal, return the literal value; |
| 550 | // if the expression is a materialized literal and is simple return a |
| 551 | // compile time value as encoded by CompileTimeValue::GetValue(). |
| 552 | // Otherwise, return undefined literal as the placeholder |
| 553 | // in the object literal boilerplate. |
| 554 | Handle<Object> GetBoilerplateValue(Expression* expression); |
| 555 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 556 | ZoneList<Expression*>* ParseArguments(bool* ok); |
| 557 | FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 558 | bool name_is_reserved, |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 559 | int function_token_position, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 560 | FunctionLiteral::Type type, |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 561 | bool* ok); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 562 | |
| 563 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 564 | // Magical syntax support. |
| 565 | Expression* ParseV8Intrinsic(bool* ok); |
| 566 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 567 | INLINE(Token::Value peek()) { |
| 568 | if (stack_overflow_) return Token::ILLEGAL; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 569 | return scanner().peek(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | INLINE(Token::Value Next()) { |
| 573 | // BUG 1215673: Find a thread safe way to set a stack limit in |
| 574 | // pre-parse mode. Otherwise, we cannot safely pre-parse from other |
| 575 | // threads. |
| 576 | if (stack_overflow_) { |
| 577 | return Token::ILLEGAL; |
| 578 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 579 | if (StackLimitCheck(isolate()).HasOverflowed()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 580 | // Any further calls to Next or peek will return the illegal token. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 581 | // The current call must return the next token, which might already |
| 582 | // have been peek'ed. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 583 | stack_overflow_ = true; |
| 584 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 585 | return scanner().Next(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 586 | } |
| 587 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 588 | bool peek_any_identifier(); |
| 589 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 590 | INLINE(void Consume(Token::Value token)); |
| 591 | void Expect(Token::Value token, bool* ok); |
| 592 | bool Check(Token::Value token); |
| 593 | void ExpectSemicolon(bool* ok); |
| 594 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 595 | Handle<String> LiteralString(PretenureFlag tenured) { |
| 596 | if (scanner().is_literal_ascii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 597 | return isolate_->factory()->NewStringFromAscii( |
| 598 | scanner().literal_ascii_string(), tenured); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 599 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 600 | return isolate_->factory()->NewStringFromTwoByte( |
| 601 | scanner().literal_uc16_string(), tenured); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
| 605 | Handle<String> NextLiteralString(PretenureFlag tenured) { |
| 606 | if (scanner().is_next_literal_ascii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 607 | return isolate_->factory()->NewStringFromAscii( |
| 608 | scanner().next_literal_ascii_string(), tenured); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 609 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 610 | return isolate_->factory()->NewStringFromTwoByte( |
| 611 | scanner().next_literal_uc16_string(), tenured); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 615 | Handle<String> GetSymbol(bool* ok); |
| 616 | |
| 617 | // Get odd-ball literals. |
| 618 | Literal* GetLiteralUndefined(); |
| 619 | Literal* GetLiteralTheHole(); |
| 620 | Literal* GetLiteralNumber(double value); |
| 621 | |
| 622 | Handle<String> ParseIdentifier(bool* ok); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 623 | Handle<String> ParseIdentifierOrStrictReservedWord( |
| 624 | bool* is_strict_reserved, bool* ok); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 625 | Handle<String> ParseIdentifierName(bool* ok); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 626 | Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 627 | bool* is_set, |
| 628 | bool* ok); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 629 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 630 | // Strict mode validation of LValue expressions |
| 631 | void CheckStrictModeLValue(Expression* expression, |
| 632 | const char* error, |
| 633 | bool* ok); |
| 634 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 635 | // Strict mode octal literal validation. |
| 636 | void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok); |
| 637 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 638 | // Parser support |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 639 | VariableProxy* Declare(Handle<String> name, Variable::Mode mode, |
| 640 | FunctionLiteral* fun, |
| 641 | bool resolve, |
| 642 | bool* ok); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 643 | |
| 644 | bool TargetStackContainsLabel(Handle<String> label); |
| 645 | BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok); |
| 646 | IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok); |
| 647 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 648 | void RegisterTargetUse(Label* target, Target* stop); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 649 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 650 | // Factory methods. |
| 651 | |
| 652 | Statement* EmptyStatement() { |
| 653 | static v8::internal::EmptyStatement empty; |
| 654 | return ∅ |
| 655 | } |
| 656 | |
| 657 | Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with); |
| 658 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 659 | Handle<String> LookupSymbol(int symbol_id); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 660 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 661 | Handle<String> LookupCachedSymbol(int symbol_id); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 662 | |
| 663 | Expression* NewCall(Expression* expression, |
| 664 | ZoneList<Expression*>* arguments, |
| 665 | int pos) { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 666 | return new(zone()) Call(isolate(), expression, arguments, pos); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 667 | } |
| 668 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 669 | inline Literal* NewLiteral(Handle<Object> handle) { |
| 670 | return new(zone()) Literal(isolate(), handle); |
| 671 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 672 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 673 | // Create a number literal. |
| 674 | Literal* NewNumberLiteral(double value); |
| 675 | |
| 676 | // Generate AST node that throw a ReferenceError with the given type. |
| 677 | Expression* NewThrowReferenceError(Handle<String> type); |
| 678 | |
| 679 | // Generate AST node that throw a SyntaxError with the given |
| 680 | // type. The first argument may be null (in the handle sense) in |
| 681 | // which case no arguments are passed to the constructor. |
| 682 | Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first); |
| 683 | |
| 684 | // Generate AST node that throw a TypeError with the given |
| 685 | // type. Both arguments must be non-null (in the handle sense). |
| 686 | Expression* NewThrowTypeError(Handle<String> type, |
| 687 | Handle<Object> first, |
| 688 | Handle<Object> second); |
| 689 | |
| 690 | // Generic AST generator for throwing errors from compiled code. |
| 691 | Expression* NewThrowError(Handle<String> constructor, |
| 692 | Handle<String> type, |
| 693 | Vector< Handle<Object> > arguments); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 694 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 695 | Isolate* isolate_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 696 | ZoneList<Handle<String> > symbol_cache_; |
| 697 | |
| 698 | Handle<Script> script_; |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 699 | JavaScriptScanner scanner_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 700 | |
| 701 | Scope* top_scope_; |
| 702 | int with_nesting_level_; |
| 703 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 704 | LexicalScope* lexical_scope_; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 705 | Mode mode_; |
| 706 | |
| 707 | Target* target_stack_; // for break, continue statements |
| 708 | bool allow_natives_syntax_; |
| 709 | v8::Extension* extension_; |
| 710 | bool is_pre_parsing_; |
| 711 | ScriptDataImpl* pre_data_; |
| 712 | FuncNameInferrer* fni_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 713 | bool stack_overflow_; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 714 | // If true, the next (and immediately following) function literal is |
| 715 | // preceded by a parenthesis. |
| 716 | // Heuristically that means that the function will be called immediately, |
| 717 | // so never lazily compile it. |
| 718 | bool parenthesized_function_; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 719 | bool harmony_block_scoping_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 720 | |
| 721 | friend class LexicalScope; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 722 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 723 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 724 | |
| 725 | // Support for handling complex values (array and object literals) that |
| 726 | // can be fully handled at compile time. |
| 727 | class CompileTimeValue: public AllStatic { |
| 728 | public: |
| 729 | enum Type { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 730 | OBJECT_LITERAL_FAST_ELEMENTS, |
| 731 | OBJECT_LITERAL_SLOW_ELEMENTS, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 732 | ARRAY_LITERAL |
| 733 | }; |
| 734 | |
| 735 | static bool IsCompileTimeValue(Expression* expression); |
| 736 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 737 | static bool ArrayLiteralElementNeedsInitialization(Expression* value); |
| 738 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 739 | // Get the value as a compile time value. |
| 740 | static Handle<FixedArray> GetValue(Expression* expression); |
| 741 | |
| 742 | // Get the type of a compile time value returned by GetValue(). |
| 743 | static Type GetType(Handle<FixedArray> value); |
| 744 | |
| 745 | // Get the elements array of a compile time value returned by GetValue(). |
| 746 | static Handle<FixedArray> GetElements(Handle<FixedArray> value); |
| 747 | |
| 748 | private: |
| 749 | static const int kTypeSlot = 0; |
| 750 | static const int kElementsSlot = 1; |
| 751 | |
| 752 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 753 | }; |
| 754 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 755 | } } // namespace v8::internal |
| 756 | |
| 757 | #endif // V8_PARSER_H_ |