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