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