danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 31 | #include "ast.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 33 | #include "char-predicates-inl.h" |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 34 | #include "codegen.h" |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 35 | #include "compiler.h" |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 36 | #include "func-name-inferrer.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 37 | #include "messages.h" |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 38 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include "platform.h" |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 40 | #include "preparser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #include "runtime.h" |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 42 | #include "scanner-character-streams.h" |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 43 | #include "scopeinfo.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 44 | #include "string-stream.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | namespace v8 { |
| 47 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 49 | // PositionStack is used for on-stack allocation of token positions for |
| 50 | // new expressions. Please look at ParseNewExpression. |
| 51 | |
| 52 | class PositionStack { |
| 53 | public: |
| 54 | explicit PositionStack(bool* ok) : top_(NULL), ok_(ok) {} |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 55 | ~PositionStack() { |
| 56 | ASSERT(!*ok_ || is_empty()); |
| 57 | USE(ok_); |
| 58 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 59 | |
| 60 | class Element { |
| 61 | public: |
| 62 | Element(PositionStack* stack, int value) { |
| 63 | previous_ = stack->top(); |
| 64 | value_ = value; |
| 65 | stack->set_top(this); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | Element* previous() { return previous_; } |
| 70 | int value() { return value_; } |
| 71 | friend class PositionStack; |
| 72 | Element* previous_; |
| 73 | int value_; |
| 74 | }; |
| 75 | |
| 76 | bool is_empty() { return top_ == NULL; } |
| 77 | int pop() { |
| 78 | ASSERT(!is_empty()); |
| 79 | int result = top_->value(); |
| 80 | top_ = top_->previous(); |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | Element* top() { return top_; } |
| 86 | void set_top(Element* value) { top_ = value; } |
| 87 | Element* top_; |
| 88 | bool* ok_; |
| 89 | }; |
| 90 | |
| 91 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 92 | RegExpBuilder::RegExpBuilder(Zone* zone) |
| 93 | : zone_(zone), |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 94 | pending_empty_(false), |
| 95 | characters_(NULL), |
| 96 | terms_(), |
| 97 | alternatives_() |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 98 | #ifdef DEBUG |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 99 | , last_added_(ADD_NONE) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 100 | #endif |
| 101 | {} |
| 102 | |
| 103 | |
| 104 | void RegExpBuilder::FlushCharacters() { |
| 105 | pending_empty_ = false; |
| 106 | if (characters_ != NULL) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 107 | RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 108 | characters_ = NULL; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 109 | text_.Add(atom, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 110 | LAST(ADD_ATOM); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void RegExpBuilder::FlushText() { |
| 116 | FlushCharacters(); |
| 117 | int num_text = text_.length(); |
| 118 | if (num_text == 0) { |
| 119 | return; |
| 120 | } else if (num_text == 1) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 121 | terms_.Add(text_.last(), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 122 | } else { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 123 | RegExpText* text = new(zone()) RegExpText(zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 124 | for (int i = 0; i < num_text; i++) |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 125 | text_.Get(i)->AppendToText(text, zone()); |
| 126 | terms_.Add(text, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 127 | } |
| 128 | text_.Clear(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void RegExpBuilder::AddCharacter(uc16 c) { |
| 133 | pending_empty_ = false; |
| 134 | if (characters_ == NULL) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 135 | characters_ = new(zone()) ZoneList<uc16>(4, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 136 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 137 | characters_->Add(c, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 138 | LAST(ADD_CHAR); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void RegExpBuilder::AddEmpty() { |
| 143 | pending_empty_ = true; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void RegExpBuilder::AddAtom(RegExpTree* term) { |
| 148 | if (term->IsEmpty()) { |
| 149 | AddEmpty(); |
| 150 | return; |
| 151 | } |
| 152 | if (term->IsTextElement()) { |
| 153 | FlushCharacters(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 154 | text_.Add(term, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 155 | } else { |
| 156 | FlushText(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 157 | terms_.Add(term, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 158 | } |
| 159 | LAST(ADD_ATOM); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void RegExpBuilder::AddAssertion(RegExpTree* assert) { |
| 164 | FlushText(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 165 | terms_.Add(assert, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 166 | LAST(ADD_ASSERT); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void RegExpBuilder::NewAlternative() { |
| 171 | FlushTerms(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void RegExpBuilder::FlushTerms() { |
| 176 | FlushText(); |
| 177 | int num_terms = terms_.length(); |
| 178 | RegExpTree* alternative; |
| 179 | if (num_terms == 0) { |
| 180 | alternative = RegExpEmpty::GetInstance(); |
| 181 | } else if (num_terms == 1) { |
| 182 | alternative = terms_.last(); |
| 183 | } else { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 184 | alternative = new(zone()) RegExpAlternative(terms_.GetList(zone())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 185 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 186 | alternatives_.Add(alternative, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 187 | terms_.Clear(); |
| 188 | LAST(ADD_NONE); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | RegExpTree* RegExpBuilder::ToRegExp() { |
| 193 | FlushTerms(); |
| 194 | int num_alternatives = alternatives_.length(); |
| 195 | if (num_alternatives == 0) { |
| 196 | return RegExpEmpty::GetInstance(); |
| 197 | } |
| 198 | if (num_alternatives == 1) { |
| 199 | return alternatives_.last(); |
| 200 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 201 | return new(zone()) RegExpDisjunction(alternatives_.GetList(zone())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 205 | void RegExpBuilder::AddQuantifierToAtom(int min, |
| 206 | int max, |
| 207 | RegExpQuantifier::Type type) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 208 | if (pending_empty_) { |
| 209 | pending_empty_ = false; |
| 210 | return; |
| 211 | } |
| 212 | RegExpTree* atom; |
| 213 | if (characters_ != NULL) { |
| 214 | ASSERT(last_added_ == ADD_CHAR); |
| 215 | // Last atom was character. |
| 216 | Vector<const uc16> char_vector = characters_->ToConstVector(); |
| 217 | int num_chars = char_vector.length(); |
| 218 | if (num_chars > 1) { |
| 219 | Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 220 | text_.Add(new(zone()) RegExpAtom(prefix), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 221 | char_vector = char_vector.SubVector(num_chars - 1, num_chars); |
| 222 | } |
| 223 | characters_ = NULL; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 224 | atom = new(zone()) RegExpAtom(char_vector); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 225 | FlushText(); |
| 226 | } else if (text_.length() > 0) { |
| 227 | ASSERT(last_added_ == ADD_ATOM); |
| 228 | atom = text_.RemoveLast(); |
| 229 | FlushText(); |
| 230 | } else if (terms_.length() > 0) { |
| 231 | ASSERT(last_added_ == ADD_ATOM); |
| 232 | atom = terms_.RemoveLast(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 233 | if (atom->max_match() == 0) { |
| 234 | // Guaranteed to only match an empty string. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 235 | LAST(ADD_TERM); |
| 236 | if (min == 0) { |
| 237 | return; |
| 238 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 239 | terms_.Add(atom, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | } else { |
| 243 | // Only call immediately after adding an atom or character! |
| 244 | UNREACHABLE(); |
| 245 | return; |
| 246 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 247 | terms_.Add(new(zone()) RegExpQuantifier(min, max, type, atom), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 248 | LAST(ADD_TERM); |
| 249 | } |
| 250 | |
| 251 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 252 | Handle<String> Parser::LookupSymbol(int symbol_id) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 253 | // Length of symbol cache is the number of identified symbols. |
| 254 | // If we are larger than that, or negative, it's not a cached symbol. |
| 255 | // This might also happen if there is no preparser symbol data, even |
| 256 | // if there is some preparser data. |
| 257 | if (static_cast<unsigned>(symbol_id) |
| 258 | >= static_cast<unsigned>(symbol_cache_.length())) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 259 | if (scanner().is_literal_ascii()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 260 | return isolate()->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 261 | Vector<const uint8_t>::cast(scanner().literal_ascii_string())); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 262 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 263 | return isolate()->factory()->InternalizeTwoByteString( |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 264 | scanner().literal_utf16_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 265 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 266 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 267 | return LookupCachedSymbol(symbol_id); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 271 | Handle<String> Parser::LookupCachedSymbol(int symbol_id) { |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 272 | // Make sure the cache is large enough to hold the symbol identifier. |
| 273 | if (symbol_cache_.length() <= symbol_id) { |
| 274 | // Increase length to index + 1. |
| 275 | symbol_cache_.AddBlock(Handle<String>::null(), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 276 | symbol_id + 1 - symbol_cache_.length(), zone()); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 277 | } |
| 278 | Handle<String> result = symbol_cache_.at(symbol_id); |
| 279 | if (result.is_null()) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 280 | if (scanner().is_literal_ascii()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 281 | result = isolate()->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 282 | Vector<const uint8_t>::cast(scanner().literal_ascii_string())); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 283 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 284 | result = isolate()->factory()->InternalizeTwoByteString( |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 285 | scanner().literal_utf16_string()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 286 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 287 | symbol_cache_.at(symbol_id) = result; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 288 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 289 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 290 | isolate()->counters()->total_preparse_symbols_skipped()->Increment(); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 291 | return result; |
| 292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 294 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 295 | FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) { |
| 296 | // The current pre-data entry must be a FunctionEntry with the given |
| 297 | // start position. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 298 | if ((function_index_ + FunctionEntry::kSize <= store_.length()) |
| 299 | && (static_cast<int>(store_[function_index_]) == start)) { |
| 300 | int index = function_index_; |
| 301 | function_index_ += FunctionEntry::kSize; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 302 | return FunctionEntry(store_.SubVector(index, |
| 303 | index + FunctionEntry::kSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | } |
| 305 | return FunctionEntry(); |
| 306 | } |
| 307 | |
| 308 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 309 | int ScriptDataImpl::GetSymbolIdentifier() { |
| 310 | return ReadNumber(&symbol_data_); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 314 | bool ScriptDataImpl::SanityCheck() { |
| 315 | // Check that the header data is valid and doesn't specify |
| 316 | // point to positions outside the store. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 317 | if (store_.length() < PreparseDataConstants::kHeaderSize) return false; |
| 318 | if (magic() != PreparseDataConstants::kMagicNumber) return false; |
| 319 | if (version() != PreparseDataConstants::kCurrentVersion) return false; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 320 | if (has_error()) { |
| 321 | // Extra sane sanity check for error message encoding. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 322 | if (store_.length() <= PreparseDataConstants::kHeaderSize |
| 323 | + PreparseDataConstants::kMessageTextPos) { |
| 324 | return false; |
| 325 | } |
| 326 | if (Read(PreparseDataConstants::kMessageStartPos) > |
| 327 | Read(PreparseDataConstants::kMessageEndPos)) { |
| 328 | return false; |
| 329 | } |
| 330 | unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos); |
| 331 | int pos = PreparseDataConstants::kMessageTextPos; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 332 | for (unsigned int i = 0; i <= arg_count; i++) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 333 | if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) { |
| 334 | return false; |
| 335 | } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 336 | int length = static_cast<int>(Read(pos)); |
| 337 | if (length < 0) return false; |
| 338 | pos += 1 + length; |
| 339 | } |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 340 | if (store_.length() < PreparseDataConstants::kHeaderSize + pos) { |
| 341 | return false; |
| 342 | } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | // Check that the space allocated for function entries is sane. |
| 346 | int functions_size = |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 347 | static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 348 | if (functions_size < 0) return false; |
| 349 | if (functions_size % FunctionEntry::kSize != 0) return false; |
| 350 | // Check that the count of symbols is non-negative. |
| 351 | int symbol_count = |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 352 | static_cast<int>(store_[PreparseDataConstants::kSymbolCountOffset]); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 353 | if (symbol_count < 0) return false; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 354 | // Check that the total size has room for header and function entries. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 355 | int minimum_size = |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 356 | PreparseDataConstants::kHeaderSize + functions_size; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 357 | if (store_.length() < minimum_size) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
| 361 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 362 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 363 | const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | int length = start[0]; |
| 365 | char* result = NewArray<char>(length + 1); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 366 | for (int i = 0; i < length; i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 367 | result[i] = start[i + 1]; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 368 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | result[length] = '\0'; |
| 370 | if (chars != NULL) *chars = length; |
| 371 | return result; |
| 372 | } |
| 373 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 374 | Scanner::Location ScriptDataImpl::MessageLocation() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 375 | int beg_pos = Read(PreparseDataConstants::kMessageStartPos); |
| 376 | int end_pos = Read(PreparseDataConstants::kMessageEndPos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 377 | return Scanner::Location(beg_pos, end_pos); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | const char* ScriptDataImpl::BuildMessage() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 382 | unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 383 | return ReadString(start, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
| 387 | Vector<const char*> ScriptDataImpl::BuildArgs() { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 388 | int arg_count = Read(PreparseDataConstants::kMessageArgCountPos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 389 | const char** array = NewArray<const char*>(arg_count); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 390 | // Position after text found by skipping past length field and |
| 391 | // length field content words. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 392 | int pos = PreparseDataConstants::kMessageTextPos + 1 |
| 393 | + Read(PreparseDataConstants::kMessageTextPos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | for (int i = 0; i < arg_count; i++) { |
| 395 | int count = 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 396 | array[i] = ReadString(ReadAddress(pos), &count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 397 | pos += count + 1; |
| 398 | } |
| 399 | return Vector<const char*>(array, arg_count); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | unsigned ScriptDataImpl::Read(int position) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 404 | return store_[PreparseDataConstants::kHeaderSize + position]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
| 408 | unsigned* ScriptDataImpl::ReadAddress(int position) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 409 | return &store_[PreparseDataConstants::kHeaderSize + position]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 413 | Scope* Parser::NewScope(Scope* parent, ScopeType type) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 414 | Scope* result = new(zone()) Scope(parent, type, zone()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 415 | result->Initialize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | return result; |
| 417 | } |
| 418 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 419 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 420 | // ---------------------------------------------------------------------------- |
| 421 | // Target is a support class to facilitate manipulation of the |
| 422 | // Parser's target_stack_ (the stack of potential 'break' and |
| 423 | // 'continue' statement targets). Upon construction, a new target is |
| 424 | // added; it is removed upon destruction. |
| 425 | |
| 426 | class Target BASE_EMBEDDED { |
| 427 | public: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 428 | Target(Target** variable, AstNode* node) |
| 429 | : variable_(variable), node_(node), previous_(*variable) { |
| 430 | *variable = this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | ~Target() { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 434 | *variable_ = previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | } |
| 436 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 437 | Target* previous() { return previous_; } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 438 | AstNode* node() { return node_; } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 439 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | private: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 441 | Target** variable_; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 442 | AstNode* node_; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 443 | Target* previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | |
| 447 | class TargetScope BASE_EMBEDDED { |
| 448 | public: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 449 | explicit TargetScope(Target** variable) |
| 450 | : variable_(variable), previous_(*variable) { |
| 451 | *variable = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | ~TargetScope() { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 455 | *variable_ = previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | private: |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 459 | Target** variable_; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 460 | Target* previous_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | |
| 464 | // ---------------------------------------------------------------------------- |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 465 | // FunctionState and BlockState together implement the parser's scope stack. |
| 466 | // The parser's current scope is in top_scope_. The BlockState and |
| 467 | // FunctionState constructors push on the scope stack and the destructors |
| 468 | // pop. They are also used to hold the parser's per-function and per-block |
| 469 | // state. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 470 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 471 | class Parser::BlockState BASE_EMBEDDED { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 472 | public: |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 473 | BlockState(Parser* parser, Scope* scope) |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 474 | : parser_(parser), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 475 | outer_scope_(parser->top_scope_) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 476 | parser->top_scope_ = scope; |
| 477 | } |
| 478 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 479 | ~BlockState() { parser_->top_scope_ = outer_scope_; } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 480 | |
| 481 | private: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 482 | Parser* parser_; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 483 | Scope* outer_scope_; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 487 | Parser::FunctionState::FunctionState(Parser* parser, |
| 488 | Scope* scope, |
| 489 | Isolate* isolate) |
| 490 | : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 491 | next_handler_index_(0), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 492 | expected_property_count_(0), |
| 493 | only_simple_this_property_assignments_(false), |
| 494 | this_property_assignments_(isolate->factory()->empty_fixed_array()), |
| 495 | parser_(parser), |
| 496 | outer_function_state_(parser->current_function_state_), |
| 497 | outer_scope_(parser->top_scope_), |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 498 | saved_ast_node_id_(isolate->ast_node_id()), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 499 | factory_(isolate, parser->zone()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 500 | parser->top_scope_ = scope; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 501 | parser->current_function_state_ = this; |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 502 | isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 506 | Parser::FunctionState::~FunctionState() { |
| 507 | parser_->top_scope_ = outer_scope_; |
| 508 | parser_->current_function_state_ = outer_function_state_; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 509 | if (outer_function_state_ != NULL) { |
| 510 | parser_->isolate()->set_ast_node_id(saved_ast_node_id_); |
| 511 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | // ---------------------------------------------------------------------------- |
| 516 | // The CHECK_OK macro is a convenient macro to enforce error |
| 517 | // handling for functions that may fail (by returning !*ok). |
| 518 | // |
| 519 | // CAUTION: This macro appends extra statements after a call, |
| 520 | // thus it must never be used where only a single statement |
| 521 | // is correct (e.g. an if statement branch w/o braces)! |
| 522 | |
| 523 | #define CHECK_OK ok); \ |
| 524 | if (!*ok) return NULL; \ |
| 525 | ((void)0 |
| 526 | #define DUMMY ) // to make indentation work |
| 527 | #undef DUMMY |
| 528 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 529 | #define CHECK_FAILED /**/); \ |
| 530 | if (failed_) return NULL; \ |
| 531 | ((void)0 |
| 532 | #define DUMMY ) // to make indentation work |
| 533 | #undef DUMMY |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | |
| 535 | // ---------------------------------------------------------------------------- |
| 536 | // Implementation of Parser |
| 537 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 538 | Parser::Parser(CompilationInfo* info, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 539 | int parser_flags, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | v8::Extension* extension, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 541 | ScriptDataImpl* pre_data) |
| 542 | : isolate_(info->isolate()), |
| 543 | symbol_cache_(pre_data ? pre_data->symbol_count() : 0, info->zone()), |
| 544 | script_(info->script()), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 545 | scanner_(isolate_->unicode_cache()), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 546 | reusable_preparser_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | top_scope_(NULL), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 548 | current_function_state_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 549 | target_stack_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | extension_(extension), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 551 | pre_data_(pre_data), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 552 | fni_(NULL), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 553 | allow_natives_syntax_((parser_flags & kAllowNativesSyntax) != 0), |
| 554 | allow_lazy_((parser_flags & kAllowLazy) != 0), |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 555 | allow_modules_((parser_flags & kAllowModules) != 0), |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 556 | stack_overflow_(false), |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 557 | parenthesized_function_(false), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 558 | zone_(info->zone()), |
| 559 | info_(info) { |
| 560 | ASSERT(!script_.is_null()); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 561 | isolate_->set_ast_node_id(0); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 562 | if ((parser_flags & kLanguageModeMask) == EXTENDED_MODE) { |
| 563 | scanner().SetHarmonyScoping(true); |
| 564 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 565 | if ((parser_flags & kAllowModules) != 0) { |
| 566 | scanner().SetHarmonyModules(true); |
| 567 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 571 | FunctionLiteral* Parser::ParseProgram() { |
| 572 | ZoneScope zone_scope(zone(), DONT_DELETE_ON_EXIT); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 573 | HistogramTimerScope timer(isolate()->counters()->parse()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 574 | Handle<String> source(String::cast(script_->source())); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 575 | isolate()->counters()->total_parse_size()->Increment(source->length()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 576 | int64_t start = FLAG_trace_parse ? OS::Ticks() : 0; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 577 | fni_ = new(zone()) FuncNameInferrer(isolate(), zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | |
| 579 | // Initialize parser state. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 580 | source->TryFlatten(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 581 | FunctionLiteral* result; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 582 | if (source->IsExternalTwoByteString()) { |
| 583 | // Notice that the stream is destroyed at the end of the branch block. |
| 584 | // The last line of the blocks can't be moved outside, even though they're |
| 585 | // identical calls. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 586 | ExternalTwoByteStringUtf16CharacterStream stream( |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 587 | Handle<ExternalTwoByteString>::cast(source), 0, source->length()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 588 | scanner_.Initialize(&stream); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 589 | result = DoParseProgram(info(), source, &zone_scope); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 590 | } else { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 591 | GenericStringUtf16CharacterStream stream(source, 0, source->length()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 592 | scanner_.Initialize(&stream); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 593 | result = DoParseProgram(info(), source, &zone_scope); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 594 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 595 | |
| 596 | if (FLAG_trace_parse && result != NULL) { |
| 597 | double ms = static_cast<double>(OS::Ticks() - start) / 1000; |
| 598 | if (info()->is_eval()) { |
| 599 | PrintF("[parsing eval"); |
| 600 | } else if (info()->script()->name()->IsString()) { |
| 601 | String* name = String::cast(info()->script()->name()); |
| 602 | SmartArrayPointer<char> name_chars = name->ToCString(); |
| 603 | PrintF("[parsing script: %s", *name_chars); |
| 604 | } else { |
| 605 | PrintF("[parsing script"); |
| 606 | } |
| 607 | PrintF(" - took %0.3f ms]\n", ms); |
| 608 | } |
| 609 | return result; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 613 | FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info, |
| 614 | Handle<String> source, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 615 | ZoneScope* zone_scope) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 616 | ASSERT(top_scope_ == NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | ASSERT(target_stack_ == NULL); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 618 | if (pre_data_ != NULL) pre_data_->Initialize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 620 | Handle<String> no_name = isolate()->factory()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | |
| 622 | FunctionLiteral* result = NULL; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 623 | { Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); |
| 624 | info->SetGlobalScope(scope); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 625 | if (!info->context().is_null()) { |
| 626 | scope = Scope::DeserializeScopeChain(*info->context(), scope, zone()); |
| 627 | } |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 628 | if (info->is_eval()) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 629 | if (!scope->is_global_scope() || info->language_mode() != CLASSIC_MODE) { |
| 630 | scope = NewScope(scope, EVAL_SCOPE); |
| 631 | } |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 632 | } else if (info->is_global()) { |
| 633 | scope = NewScope(scope, GLOBAL_SCOPE); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 634 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 635 | scope->set_start_position(0); |
| 636 | scope->set_end_position(source->length()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 637 | |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 638 | // Compute the parsing mode. |
| 639 | Mode mode = (FLAG_lazy && allow_lazy_) ? PARSE_LAZILY : PARSE_EAGERLY; |
| 640 | if (allow_natives_syntax_ || extension_ != NULL || scope->is_eval_scope()) { |
| 641 | mode = PARSE_EAGERLY; |
| 642 | } |
| 643 | ParsingModeScope parsing_mode(this, mode); |
| 644 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 645 | FunctionState function_state(this, scope, isolate()); // Enters 'scope'. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 646 | top_scope_->SetLanguageMode(info->language_mode()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 647 | ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | bool ok = true; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 649 | int beg_loc = scanner().location().beg_pos; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 650 | ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 651 | if (ok && !top_scope_->is_classic_mode()) { |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 652 | CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok); |
| 653 | } |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 654 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 655 | if (ok && is_extended_mode()) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 656 | CheckConflictingVarDeclarations(top_scope_, &ok); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 657 | } |
| 658 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 659 | if (ok) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 660 | result = factory()->NewFunctionLiteral( |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 661 | no_name, |
| 662 | top_scope_, |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 663 | body, |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 664 | function_state.materialized_literal_count(), |
| 665 | function_state.expected_property_count(), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 666 | function_state.handler_count(), |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 667 | function_state.only_simple_this_property_assignments(), |
| 668 | function_state.this_property_assignments(), |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 669 | 0, |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 670 | FunctionLiteral::kNoDuplicateParameters, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 671 | FunctionLiteral::ANONYMOUS_EXPRESSION, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 672 | FunctionLiteral::kGlobalOrEval, |
| 673 | FunctionLiteral::kNotParenthesized); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 674 | result->set_ast_properties(factory()->visitor()->ast_properties()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 675 | } else if (stack_overflow_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 676 | isolate()->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | // Make sure the target stack is empty. |
| 681 | ASSERT(target_stack_ == NULL); |
| 682 | |
| 683 | // If there was a syntax error we have to get rid of the AST |
| 684 | // and it is not safe to do so before the scope has been deleted. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 685 | if (result == NULL) zone_scope->DeleteOnExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | return result; |
| 687 | } |
| 688 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 689 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 690 | FunctionLiteral* Parser::ParseLazy() { |
| 691 | ZoneScope zone_scope(zone(), DONT_DELETE_ON_EXIT); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 692 | HistogramTimerScope timer(isolate()->counters()->parse_lazy()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 693 | Handle<String> source(String::cast(script_->source())); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 694 | isolate()->counters()->total_parse_size()->Increment(source->length()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 695 | int64_t start = FLAG_trace_parse ? OS::Ticks() : 0; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 696 | Handle<SharedFunctionInfo> shared_info = info()->shared_info(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 697 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 698 | // Initialize parser state. |
| 699 | source->TryFlatten(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 700 | FunctionLiteral* result; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 701 | if (source->IsExternalTwoByteString()) { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 702 | ExternalTwoByteStringUtf16CharacterStream stream( |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 703 | Handle<ExternalTwoByteString>::cast(source), |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 704 | shared_info->start_position(), |
| 705 | shared_info->end_position()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 706 | result = ParseLazy(&stream, &zone_scope); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 707 | } else { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 708 | GenericStringUtf16CharacterStream stream(source, |
| 709 | shared_info->start_position(), |
| 710 | shared_info->end_position()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 711 | result = ParseLazy(&stream, &zone_scope); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 712 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 713 | |
| 714 | if (FLAG_trace_parse && result != NULL) { |
| 715 | double ms = static_cast<double>(OS::Ticks() - start) / 1000; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 716 | SmartArrayPointer<char> name_chars = result->debug_name()->ToCString(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 717 | PrintF("[parsing function: %s - took %0.3f ms]\n", *name_chars, ms); |
| 718 | } |
| 719 | return result; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 723 | FunctionLiteral* Parser::ParseLazy(Utf16CharacterStream* source, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 724 | ZoneScope* zone_scope) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 725 | Handle<SharedFunctionInfo> shared_info = info()->shared_info(); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 726 | scanner_.Initialize(source); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 727 | ASSERT(top_scope_ == NULL); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 728 | ASSERT(target_stack_ == NULL); |
| 729 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 730 | Handle<String> name(String::cast(shared_info->name())); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 731 | fni_ = new(zone()) FuncNameInferrer(isolate(), zone()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 732 | fni_->PushEnclosingName(name); |
| 733 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 734 | ParsingModeScope parsing_mode(this, PARSE_EAGERLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | |
| 736 | // Place holder for the result. |
| 737 | FunctionLiteral* result = NULL; |
| 738 | |
| 739 | { |
| 740 | // Parse the function literal. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 741 | Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 742 | info()->SetGlobalScope(scope); |
| 743 | if (!info()->closure().is_null()) { |
| 744 | scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 745 | zone()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 746 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 747 | FunctionState function_state(this, scope, isolate()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 748 | ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 749 | ASSERT(scope->language_mode() != EXTENDED_MODE || |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 750 | info()->is_extended_mode()); |
| 751 | ASSERT(info()->language_mode() == shared_info->language_mode()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 752 | scope->SetLanguageMode(shared_info->language_mode()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 753 | FunctionLiteral::Type type = shared_info->is_expression() |
| 754 | ? (shared_info->is_anonymous() |
| 755 | ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 756 | : FunctionLiteral::NAMED_EXPRESSION) |
| 757 | : FunctionLiteral::DECLARATION; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | bool ok = true; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 759 | result = ParseFunctionLiteral(name, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 760 | false, // Strict mode name already checked. |
| 761 | RelocInfo::kNoPosition, |
| 762 | type, |
| 763 | &ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 764 | // Make sure the results agree. |
| 765 | ASSERT(ok == (result != NULL)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | // Make sure the target stack is empty. |
| 769 | ASSERT(target_stack_ == NULL); |
| 770 | |
| 771 | // If there was a stack overflow we have to get rid of AST and it is |
| 772 | // not safe to do before scope has been deleted. |
| 773 | if (result == NULL) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 774 | zone_scope->DeleteOnExit(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 775 | if (stack_overflow_) isolate()->StackOverflow(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 776 | } else { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 777 | Handle<String> inferred_name(shared_info->inferred_name()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 778 | result->set_inferred_name(inferred_name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 779 | } |
| 780 | return result; |
| 781 | } |
| 782 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 783 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 784 | Handle<String> Parser::GetSymbol(bool* ok) { |
| 785 | int symbol_id = -1; |
| 786 | if (pre_data() != NULL) { |
| 787 | symbol_id = pre_data()->GetSymbolIdentifier(); |
| 788 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 789 | return LookupSymbol(symbol_id); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 793 | void Parser::ReportMessage(const char* type, Vector<const char*> args) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 794 | Scanner::Location source_location = scanner().location(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 795 | ReportMessageAt(source_location, type, args); |
| 796 | } |
| 797 | |
| 798 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 799 | void Parser::ReportMessage(const char* type, Vector<Handle<String> > args) { |
| 800 | Scanner::Location source_location = scanner().location(); |
| 801 | ReportMessageAt(source_location, type, args); |
| 802 | } |
| 803 | |
| 804 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 805 | void Parser::ReportMessageAt(Scanner::Location source_location, |
| 806 | const char* type, |
| 807 | Vector<const char*> args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 808 | MessageLocation location(script_, |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 809 | source_location.beg_pos, |
| 810 | source_location.end_pos); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 811 | Factory* factory = isolate()->factory(); |
| 812 | Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 813 | for (int i = 0; i < args.length(); i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 814 | Handle<String> arg_string = factory->NewStringFromUtf8(CStrVector(args[i])); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 815 | elements->set(i, *arg_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 816 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 817 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
| 818 | Handle<Object> result = factory->NewSyntaxError(type, array); |
| 819 | isolate()->Throw(*result, &location); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 823 | void Parser::ReportMessageAt(Scanner::Location source_location, |
| 824 | const char* type, |
| 825 | Vector<Handle<String> > args) { |
| 826 | MessageLocation location(script_, |
| 827 | source_location.beg_pos, |
| 828 | source_location.end_pos); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 829 | Factory* factory = isolate()->factory(); |
| 830 | Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 831 | for (int i = 0; i < args.length(); i++) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 832 | elements->set(i, *args[i]); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 833 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 834 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
| 835 | Handle<Object> result = factory->NewSyntaxError(type, array); |
| 836 | isolate()->Throw(*result, &location); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 840 | // A ThisNamedPropertyAssignmentFinder finds and marks statements of the form |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 841 | // this.x = ...;, where x is a named property. It also determines whether a |
| 842 | // function contains only assignments of this type. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 843 | class ThisNamedPropertyAssignmentFinder { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 844 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 845 | ThisNamedPropertyAssignmentFinder(Isolate* isolate, Zone* zone) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 846 | : isolate_(isolate), |
| 847 | only_simple_this_property_assignments_(true), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 848 | names_(0, zone), |
| 849 | assigned_arguments_(0, zone), |
| 850 | assigned_constants_(0, zone), |
| 851 | zone_(zone) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 852 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 853 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 854 | static Assignment* AsAssignment(Statement* stat) { |
| 855 | if (stat == NULL) return NULL; |
| 856 | ExpressionStatement* exp_stat = stat->AsExpressionStatement(); |
| 857 | if (exp_stat == NULL) return NULL; |
| 858 | return exp_stat->expression()->AsAssignment(); |
| 859 | } |
| 860 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 861 | void Update(Scope* scope, Statement* stat) { |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 862 | // Bail out if function already has property assignment that are |
| 863 | // not simple this property assignments. |
| 864 | if (!only_simple_this_property_assignments_) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 865 | return; |
| 866 | } |
| 867 | |
| 868 | // Check whether this statement is of the form this.x = ...; |
| 869 | Assignment* assignment = AsAssignment(stat); |
| 870 | if (IsThisPropertyAssignment(assignment)) { |
| 871 | HandleThisPropertyAssignment(scope, assignment); |
| 872 | } else { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 873 | only_simple_this_property_assignments_ = false; |
| 874 | } |
| 875 | } |
| 876 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 877 | // Returns whether only statements of the form this.x = y; where y is either a |
| 878 | // constant or a function argument was encountered. |
| 879 | bool only_simple_this_property_assignments() { |
| 880 | return only_simple_this_property_assignments_; |
| 881 | } |
| 882 | |
| 883 | // Returns a fixed array containing three elements for each assignment of the |
| 884 | // form this.x = y; |
| 885 | Handle<FixedArray> GetThisPropertyAssignments() { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 886 | if (names_.is_empty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 887 | return isolate_->factory()->empty_fixed_array(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 888 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 889 | ASSERT_EQ(names_.length(), assigned_arguments_.length()); |
| 890 | ASSERT_EQ(names_.length(), assigned_constants_.length()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 891 | Handle<FixedArray> assignments = |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 892 | isolate_->factory()->NewFixedArray(names_.length() * 3); |
| 893 | for (int i = 0; i < names_.length(); ++i) { |
| 894 | assignments->set(i * 3, *names_[i]); |
| 895 | assignments->set(i * 3 + 1, Smi::FromInt(assigned_arguments_[i])); |
| 896 | assignments->set(i * 3 + 2, *assigned_constants_[i]); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 897 | } |
| 898 | return assignments; |
| 899 | } |
| 900 | |
| 901 | private: |
| 902 | bool IsThisPropertyAssignment(Assignment* assignment) { |
| 903 | if (assignment != NULL) { |
| 904 | Property* property = assignment->target()->AsProperty(); |
| 905 | return assignment->op() == Token::ASSIGN |
| 906 | && property != NULL |
| 907 | && property->obj()->AsVariableProxy() != NULL |
| 908 | && property->obj()->AsVariableProxy()->is_this(); |
| 909 | } |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | void HandleThisPropertyAssignment(Scope* scope, Assignment* assignment) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 914 | // Check that the property assigned to is a named property, which is not |
| 915 | // __proto__. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 916 | Property* property = assignment->target()->AsProperty(); |
| 917 | ASSERT(property != NULL); |
| 918 | Literal* literal = property->key()->AsLiteral(); |
| 919 | uint32_t dummy; |
| 920 | if (literal != NULL && |
| 921 | literal->handle()->IsString() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 922 | !String::cast(*(literal->handle()))->Equals( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 923 | isolate_->heap()->proto_string()) && |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 924 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
| 925 | Handle<String> key = Handle<String>::cast(literal->handle()); |
| 926 | |
| 927 | // Check whether the value assigned is either a constant or matches the |
| 928 | // name of one of the arguments to the function. |
| 929 | if (assignment->value()->AsLiteral() != NULL) { |
| 930 | // Constant assigned. |
| 931 | Literal* literal = assignment->value()->AsLiteral(); |
| 932 | AssignmentFromConstant(key, literal->handle()); |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 933 | return; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 934 | } else if (assignment->value()->AsVariableProxy() != NULL) { |
| 935 | // Variable assigned. |
| 936 | Handle<String> name = |
| 937 | assignment->value()->AsVariableProxy()->name(); |
| 938 | // Check whether the variable assigned matches an argument name. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 939 | for (int i = 0; i < scope->num_parameters(); i++) { |
| 940 | if (*scope->parameter(i)->name() == *name) { |
| 941 | // Assigned from function argument. |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 942 | AssignmentFromParameter(key, i); |
| 943 | return; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 948 | // It is not a simple "this.x = value;" assignment with a constant |
| 949 | // or parameter value. |
| 950 | AssignmentFromSomethingElse(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 951 | } |
| 952 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 953 | |
| 954 | |
| 955 | |
| 956 | // We will potentially reorder the property assignments, so they must be |
| 957 | // simple enough that the ordering does not matter. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 958 | void AssignmentFromParameter(Handle<String> name, int index) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 959 | EnsureInitialized(); |
| 960 | for (int i = 0; i < names_.length(); ++i) { |
| 961 | if (name->Equals(*names_[i])) { |
| 962 | assigned_arguments_[i] = index; |
| 963 | assigned_constants_[i] = isolate_->factory()->undefined_value(); |
| 964 | return; |
| 965 | } |
| 966 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 967 | names_.Add(name, zone()); |
| 968 | assigned_arguments_.Add(index, zone()); |
| 969 | assigned_constants_.Add(isolate_->factory()->undefined_value(), zone()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | void AssignmentFromConstant(Handle<String> name, Handle<Object> value) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 973 | EnsureInitialized(); |
| 974 | for (int i = 0; i < names_.length(); ++i) { |
| 975 | if (name->Equals(*names_[i])) { |
| 976 | assigned_arguments_[i] = -1; |
| 977 | assigned_constants_[i] = value; |
| 978 | return; |
| 979 | } |
| 980 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 981 | names_.Add(name, zone()); |
| 982 | assigned_arguments_.Add(-1, zone()); |
| 983 | assigned_constants_.Add(value, zone()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 984 | } |
| 985 | |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 986 | void AssignmentFromSomethingElse() { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 987 | // The this assignment is not a simple one. |
| 988 | only_simple_this_property_assignments_ = false; |
| 989 | } |
| 990 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 991 | void EnsureInitialized() { |
| 992 | if (names_.capacity() == 0) { |
| 993 | ASSERT(assigned_arguments_.capacity() == 0); |
| 994 | ASSERT(assigned_constants_.capacity() == 0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 995 | names_.Initialize(4, zone()); |
| 996 | assigned_arguments_.Initialize(4, zone()); |
| 997 | assigned_constants_.Initialize(4, zone()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1001 | Zone* zone() const { return zone_; } |
| 1002 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1003 | Isolate* isolate_; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1004 | bool only_simple_this_property_assignments_; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1005 | ZoneStringList names_; |
| 1006 | ZoneList<int> assigned_arguments_; |
| 1007 | ZoneObjectList assigned_constants_; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1008 | Zone* zone_; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1009 | }; |
| 1010 | |
| 1011 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 1012 | void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | int end_token, |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 1014 | bool is_eval, |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 1015 | bool is_global, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | bool* ok) { |
| 1017 | // SourceElements :: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1018 | // (ModuleElement)* <end_token> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Allocate a target stack to use for this set of source |
| 1021 | // elements. This way, all scripts and functions get their own |
| 1022 | // target stack thus avoiding illegal breaks and continues across |
| 1023 | // functions. |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1024 | TargetScope scope(&this->target_stack_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1025 | |
| 1026 | ASSERT(processor != NULL); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1027 | ThisNamedPropertyAssignmentFinder this_property_assignment_finder(isolate(), |
| 1028 | zone()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1029 | bool directive_prologue = true; // Parsing directive prologue. |
| 1030 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | while (peek() != end_token) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1032 | if (directive_prologue && peek() != Token::STRING) { |
| 1033 | directive_prologue = false; |
| 1034 | } |
| 1035 | |
| 1036 | Scanner::Location token_loc = scanner().peek_location(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 1037 | Statement* stat; |
| 1038 | if (is_global && !is_eval) { |
| 1039 | stat = ParseModuleElement(NULL, CHECK_OK); |
| 1040 | } else { |
| 1041 | stat = ParseBlockElement(NULL, CHECK_OK); |
| 1042 | } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1043 | if (stat == NULL || stat->IsEmpty()) { |
| 1044 | directive_prologue = false; // End of directive prologue. |
| 1045 | continue; |
| 1046 | } |
| 1047 | |
| 1048 | if (directive_prologue) { |
| 1049 | // A shot at a directive. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1050 | ExpressionStatement* e_stat; |
| 1051 | Literal* literal; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1052 | // Still processing directive prologue? |
| 1053 | if ((e_stat = stat->AsExpressionStatement()) != NULL && |
| 1054 | (literal = e_stat->expression()->AsLiteral()) != NULL && |
| 1055 | literal->handle()->IsString()) { |
| 1056 | Handle<String> directive = Handle<String>::cast(literal->handle()); |
| 1057 | |
| 1058 | // Check "use strict" directive (ES5 14.1). |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1059 | if (top_scope_->is_classic_mode() && |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1060 | directive->Equals(isolate()->heap()->use_strict_string()) && |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1061 | token_loc.end_pos - token_loc.beg_pos == |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1062 | isolate()->heap()->use_strict_string()->length() + 2) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 1063 | // TODO(mstarzinger): Global strict eval calls, need their own scope |
| 1064 | // as specified in ES5 10.4.2(3). The correct fix would be to always |
| 1065 | // add this scope in DoParseProgram(), but that requires adaptations |
| 1066 | // all over the code base, so we go with a quick-fix for now. |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 1067 | // In the same manner, we have to patch the parsing mode. |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 1068 | if (is_eval && !top_scope_->is_eval_scope()) { |
| 1069 | ASSERT(top_scope_->is_global_scope()); |
| 1070 | Scope* scope = NewScope(top_scope_, EVAL_SCOPE); |
| 1071 | scope->set_start_position(top_scope_->start_position()); |
| 1072 | scope->set_end_position(top_scope_->end_position()); |
| 1073 | top_scope_ = scope; |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 1074 | mode_ = PARSE_EAGERLY; |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 1075 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1076 | // TODO(ES6): Fix entering extended mode, once it is specified. |
| 1077 | top_scope_->SetLanguageMode(FLAG_harmony_scoping |
| 1078 | ? EXTENDED_MODE : STRICT_MODE); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1079 | // "use strict" is the only directive for now. |
| 1080 | directive_prologue = false; |
| 1081 | } |
| 1082 | } else { |
| 1083 | // End of the directive prologue. |
| 1084 | directive_prologue = false; |
| 1085 | } |
| 1086 | } |
| 1087 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1088 | // Find and mark all assignments to named properties in this (this.x =) |
| 1089 | if (top_scope_->is_function_scope()) { |
| 1090 | this_property_assignment_finder.Update(top_scope_, stat); |
| 1091 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1092 | processor->Add(stat, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1093 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1094 | |
| 1095 | // Propagate the collected information on this property assignments. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 1096 | if (top_scope_->is_function_scope()) { |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1097 | bool only_simple_this_property_assignments = |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1098 | this_property_assignment_finder.only_simple_this_property_assignments() |
| 1099 | && top_scope_->declarations()->length() == 0; |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1100 | if (only_simple_this_property_assignments) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1101 | current_function_state_->SetThisPropertyAssignmentInfo( |
ager@chromium.org | b9a1545 | 2009-11-11 09:55:05 +0000 | [diff] [blame] | 1102 | only_simple_this_property_assignments, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1103 | this_property_assignment_finder.GetThisPropertyAssignments()); |
| 1104 | } |
| 1105 | } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1106 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1111 | Statement* Parser::ParseModuleElement(ZoneStringList* labels, |
| 1112 | bool* ok) { |
| 1113 | // (Ecma 262 5th Edition, clause 14): |
| 1114 | // SourceElement: |
| 1115 | // Statement |
| 1116 | // FunctionDeclaration |
| 1117 | // |
| 1118 | // In harmony mode we allow additionally the following productions |
| 1119 | // ModuleElement: |
| 1120 | // LetDeclaration |
| 1121 | // ConstDeclaration |
| 1122 | // ModuleDeclaration |
| 1123 | // ImportDeclaration |
| 1124 | // ExportDeclaration |
| 1125 | |
| 1126 | switch (peek()) { |
| 1127 | case Token::FUNCTION: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1128 | return ParseFunctionDeclaration(NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1129 | case Token::LET: |
| 1130 | case Token::CONST: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1131 | return ParseVariableStatement(kModuleElement, NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1132 | case Token::IMPORT: |
| 1133 | return ParseImportDeclaration(ok); |
| 1134 | case Token::EXPORT: |
| 1135 | return ParseExportDeclaration(ok); |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 1136 | default: { |
| 1137 | Statement* stmt = ParseStatement(labels, CHECK_OK); |
| 1138 | // Handle 'module' as a context-sensitive keyword. |
| 1139 | if (FLAG_harmony_modules && |
| 1140 | peek() == Token::IDENTIFIER && |
| 1141 | !scanner().HasAnyLineTerminatorBeforeNext() && |
| 1142 | stmt != NULL) { |
| 1143 | ExpressionStatement* estmt = stmt->AsExpressionStatement(); |
| 1144 | if (estmt != NULL && |
| 1145 | estmt->expression()->AsVariableProxy() != NULL && |
| 1146 | estmt->expression()->AsVariableProxy()->name()->Equals( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1147 | isolate()->heap()->module_string()) && |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 1148 | !scanner().literal_contains_escapes()) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1149 | return ParseModuleDeclaration(NULL, ok); |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | return stmt; |
| 1153 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1158 | Statement* Parser::ParseModuleDeclaration(ZoneStringList* names, bool* ok) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1159 | // ModuleDeclaration: |
| 1160 | // 'module' Identifier Module |
| 1161 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1162 | Handle<String> name = ParseIdentifier(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1163 | |
| 1164 | #ifdef DEBUG |
| 1165 | if (FLAG_print_interface_details) |
| 1166 | PrintF("# Module %s...\n", name->ToAsciiArray()); |
| 1167 | #endif |
| 1168 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1169 | Module* module = ParseModule(CHECK_OK); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1170 | VariableProxy* proxy = NewUnresolved(name, MODULE, module->interface()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1171 | Declaration* declaration = |
| 1172 | factory()->NewModuleDeclaration(proxy, module, top_scope_); |
| 1173 | Declare(declaration, true, CHECK_OK); |
| 1174 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1175 | #ifdef DEBUG |
| 1176 | if (FLAG_print_interface_details) |
| 1177 | PrintF("# Module %s.\n", name->ToAsciiArray()); |
| 1178 | |
| 1179 | if (FLAG_print_interfaces) { |
| 1180 | PrintF("module %s : ", name->ToAsciiArray()); |
| 1181 | module->interface()->Print(); |
| 1182 | } |
| 1183 | #endif |
| 1184 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1185 | if (names) names->Add(name, zone()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1186 | if (module->body() == NULL) |
| 1187 | return factory()->NewEmptyStatement(); |
| 1188 | else |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1189 | return factory()->NewModuleStatement(proxy, module->body()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | |
| 1193 | Module* Parser::ParseModule(bool* ok) { |
| 1194 | // Module: |
| 1195 | // '{' ModuleElement '}' |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1196 | // '=' ModulePath ';' |
| 1197 | // 'at' String ';' |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1198 | |
| 1199 | switch (peek()) { |
| 1200 | case Token::LBRACE: |
| 1201 | return ParseModuleLiteral(ok); |
| 1202 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1203 | case Token::ASSIGN: { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1204 | Expect(Token::ASSIGN, CHECK_OK); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1205 | Module* result = ParseModulePath(CHECK_OK); |
| 1206 | ExpectSemicolon(CHECK_OK); |
| 1207 | return result; |
| 1208 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1209 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1210 | default: { |
| 1211 | ExpectContextualKeyword("at", CHECK_OK); |
| 1212 | Module* result = ParseModuleUrl(CHECK_OK); |
| 1213 | ExpectSemicolon(CHECK_OK); |
| 1214 | return result; |
| 1215 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | Module* Parser::ParseModuleLiteral(bool* ok) { |
| 1221 | // Module: |
| 1222 | // '{' ModuleElement '}' |
| 1223 | |
| 1224 | // Construct block expecting 16 statements. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1225 | Block* body = factory()->NewBlock(NULL, 16, false); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1226 | #ifdef DEBUG |
| 1227 | if (FLAG_print_interface_details) PrintF("# Literal "); |
| 1228 | #endif |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1229 | Scope* scope = NewScope(top_scope_, MODULE_SCOPE); |
| 1230 | |
| 1231 | Expect(Token::LBRACE, CHECK_OK); |
| 1232 | scope->set_start_position(scanner().location().beg_pos); |
| 1233 | scope->SetLanguageMode(EXTENDED_MODE); |
| 1234 | |
| 1235 | { |
| 1236 | BlockState block_state(this, scope); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1237 | TargetCollector collector(zone()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1238 | Target target(&this->target_stack_, &collector); |
| 1239 | Target target_body(&this->target_stack_, body); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1240 | |
| 1241 | while (peek() != Token::RBRACE) { |
| 1242 | Statement* stat = ParseModuleElement(NULL, CHECK_OK); |
| 1243 | if (stat && !stat->IsEmpty()) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1244 | body->AddStatement(stat, zone()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | Expect(Token::RBRACE, CHECK_OK); |
| 1250 | scope->set_end_position(scanner().location().end_pos); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1251 | body->set_scope(scope); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1252 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1253 | // Check that all exports are bound. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1254 | Interface* interface = scope->interface(); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1255 | for (Interface::Iterator it = interface->iterator(); |
| 1256 | !it.done(); it.Advance()) { |
| 1257 | if (scope->LocalLookup(it.name()) == NULL) { |
| 1258 | Handle<String> name(it.name()); |
| 1259 | ReportMessage("module_export_undefined", |
| 1260 | Vector<Handle<String> >(&name, 1)); |
| 1261 | *ok = false; |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | } |
| 1265 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1266 | interface->MakeModule(ok); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1267 | ASSERT(*ok); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1268 | interface->Freeze(ok); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1269 | ASSERT(*ok); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1270 | return factory()->NewModuleLiteral(body, interface); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | Module* Parser::ParseModulePath(bool* ok) { |
| 1275 | // ModulePath: |
| 1276 | // Identifier |
| 1277 | // ModulePath '.' Identifier |
| 1278 | |
| 1279 | Module* result = ParseModuleVariable(CHECK_OK); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1280 | while (Check(Token::PERIOD)) { |
| 1281 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1282 | #ifdef DEBUG |
| 1283 | if (FLAG_print_interface_details) |
| 1284 | PrintF("# Path .%s ", name->ToAsciiArray()); |
| 1285 | #endif |
| 1286 | Module* member = factory()->NewModulePath(result, name); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1287 | result->interface()->Add(name, member->interface(), zone(), ok); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1288 | if (!*ok) { |
| 1289 | #ifdef DEBUG |
| 1290 | if (FLAG_print_interfaces) { |
| 1291 | PrintF("PATH TYPE ERROR at '%s'\n", name->ToAsciiArray()); |
| 1292 | PrintF("result: "); |
| 1293 | result->interface()->Print(); |
| 1294 | PrintF("member: "); |
| 1295 | member->interface()->Print(); |
| 1296 | } |
| 1297 | #endif |
| 1298 | ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1)); |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | result = member; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | return result; |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | Module* Parser::ParseModuleVariable(bool* ok) { |
| 1309 | // ModulePath: |
| 1310 | // Identifier |
| 1311 | |
| 1312 | Handle<String> name = ParseIdentifier(CHECK_OK); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1313 | #ifdef DEBUG |
| 1314 | if (FLAG_print_interface_details) |
| 1315 | PrintF("# Module variable %s ", name->ToAsciiArray()); |
| 1316 | #endif |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1317 | VariableProxy* proxy = top_scope_->NewUnresolved( |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1318 | factory(), name, Interface::NewModule(zone()), |
| 1319 | scanner().location().beg_pos); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1320 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1321 | return factory()->NewModuleVariable(proxy); |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | Module* Parser::ParseModuleUrl(bool* ok) { |
| 1326 | // Module: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1327 | // String |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1328 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1329 | Expect(Token::STRING, CHECK_OK); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1330 | Handle<String> symbol = GetSymbol(CHECK_OK); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1331 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1332 | // TODO(ES6): Request JS resource from environment... |
| 1333 | |
| 1334 | #ifdef DEBUG |
| 1335 | if (FLAG_print_interface_details) PrintF("# Url "); |
| 1336 | #endif |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1337 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1338 | // Create an empty literal as long as the feature isn't finished. |
| 1339 | USE(symbol); |
| 1340 | Scope* scope = NewScope(top_scope_, MODULE_SCOPE); |
| 1341 | Block* body = factory()->NewBlock(NULL, 1, false); |
| 1342 | body->set_scope(scope); |
| 1343 | Interface* interface = scope->interface(); |
| 1344 | Module* result = factory()->NewModuleLiteral(body, interface); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1345 | interface->Freeze(ok); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1346 | ASSERT(*ok); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1347 | interface->Unify(scope->interface(), zone(), ok); |
| 1348 | ASSERT(*ok); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1349 | return result; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1353 | Module* Parser::ParseModuleSpecifier(bool* ok) { |
| 1354 | // ModuleSpecifier: |
| 1355 | // String |
| 1356 | // ModulePath |
| 1357 | |
| 1358 | if (peek() == Token::STRING) { |
| 1359 | return ParseModuleUrl(ok); |
| 1360 | } else { |
| 1361 | return ParseModulePath(ok); |
| 1362 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1366 | Block* Parser::ParseImportDeclaration(bool* ok) { |
| 1367 | // ImportDeclaration: |
| 1368 | // 'import' IdentifierName (',' IdentifierName)* 'from' ModuleSpecifier ';' |
| 1369 | // |
| 1370 | // TODO(ES6): implement destructuring ImportSpecifiers |
| 1371 | |
| 1372 | Expect(Token::IMPORT, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1373 | ZoneStringList names(1, zone()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1374 | |
| 1375 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1376 | names.Add(name, zone()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1377 | while (peek() == Token::COMMA) { |
| 1378 | Consume(Token::COMMA); |
| 1379 | name = ParseIdentifierName(CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1380 | names.Add(name, zone()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | ExpectContextualKeyword("from", CHECK_OK); |
| 1384 | Module* module = ParseModuleSpecifier(CHECK_OK); |
| 1385 | ExpectSemicolon(CHECK_OK); |
| 1386 | |
| 1387 | // Generate a separate declaration for each identifier. |
| 1388 | // TODO(ES6): once we implement destructuring, make that one declaration. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1389 | Block* block = factory()->NewBlock(NULL, 1, true); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1390 | for (int i = 0; i < names.length(); ++i) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1391 | #ifdef DEBUG |
| 1392 | if (FLAG_print_interface_details) |
| 1393 | PrintF("# Import %s ", names[i]->ToAsciiArray()); |
| 1394 | #endif |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1395 | Interface* interface = Interface::NewUnknown(zone()); |
| 1396 | module->interface()->Add(names[i], interface, zone(), ok); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1397 | if (!*ok) { |
| 1398 | #ifdef DEBUG |
| 1399 | if (FLAG_print_interfaces) { |
| 1400 | PrintF("IMPORT TYPE ERROR at '%s'\n", names[i]->ToAsciiArray()); |
| 1401 | PrintF("module: "); |
| 1402 | module->interface()->Print(); |
| 1403 | } |
| 1404 | #endif |
| 1405 | ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1)); |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | VariableProxy* proxy = NewUnresolved(names[i], LET, interface); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1409 | Declaration* declaration = |
| 1410 | factory()->NewImportDeclaration(proxy, module, top_scope_); |
| 1411 | Declare(declaration, true, CHECK_OK); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1414 | return block; |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | Statement* Parser::ParseExportDeclaration(bool* ok) { |
| 1419 | // ExportDeclaration: |
| 1420 | // 'export' Identifier (',' Identifier)* ';' |
| 1421 | // 'export' VariableDeclaration |
| 1422 | // 'export' FunctionDeclaration |
| 1423 | // 'export' ModuleDeclaration |
| 1424 | // |
| 1425 | // TODO(ES6): implement structuring ExportSpecifiers |
| 1426 | |
| 1427 | Expect(Token::EXPORT, CHECK_OK); |
| 1428 | |
| 1429 | Statement* result = NULL; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1430 | ZoneStringList names(1, zone()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1431 | switch (peek()) { |
| 1432 | case Token::IDENTIFIER: { |
| 1433 | Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1434 | // Handle 'module' as a context-sensitive keyword. |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 1435 | if (!name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("module"))) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1436 | names.Add(name, zone()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1437 | while (peek() == Token::COMMA) { |
| 1438 | Consume(Token::COMMA); |
| 1439 | name = ParseIdentifier(CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1440 | names.Add(name, zone()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1441 | } |
| 1442 | ExpectSemicolon(CHECK_OK); |
| 1443 | result = factory()->NewEmptyStatement(); |
| 1444 | } else { |
| 1445 | result = ParseModuleDeclaration(&names, CHECK_OK); |
| 1446 | } |
| 1447 | break; |
| 1448 | } |
| 1449 | |
| 1450 | case Token::FUNCTION: |
| 1451 | result = ParseFunctionDeclaration(&names, CHECK_OK); |
| 1452 | break; |
| 1453 | |
| 1454 | case Token::VAR: |
| 1455 | case Token::LET: |
| 1456 | case Token::CONST: |
| 1457 | result = ParseVariableStatement(kModuleElement, &names, CHECK_OK); |
| 1458 | break; |
| 1459 | |
| 1460 | default: |
| 1461 | *ok = false; |
| 1462 | ReportUnexpectedToken(scanner().current_token()); |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1466 | // Extract declared names into export declarations and interface. |
| 1467 | Interface* interface = top_scope_->interface(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1468 | for (int i = 0; i < names.length(); ++i) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1469 | #ifdef DEBUG |
| 1470 | if (FLAG_print_interface_details) |
| 1471 | PrintF("# Export %s ", names[i]->ToAsciiArray()); |
| 1472 | #endif |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1473 | Interface* inner = Interface::NewUnknown(zone()); |
| 1474 | interface->Add(names[i], inner, zone(), CHECK_OK); |
| 1475 | if (!*ok) |
| 1476 | return NULL; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1477 | VariableProxy* proxy = NewUnresolved(names[i], LET, inner); |
| 1478 | USE(proxy); |
| 1479 | // TODO(rossberg): Rethink whether we actually need to store export |
| 1480 | // declarations (for compilation?). |
| 1481 | // ExportDeclaration* declaration = |
| 1482 | // factory()->NewExportDeclaration(proxy, top_scope_); |
| 1483 | // top_scope_->AddDeclaration(declaration); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | ASSERT(result != NULL); |
| 1487 | return result; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | Statement* Parser::ParseBlockElement(ZoneStringList* labels, |
| 1492 | bool* ok) { |
| 1493 | // (Ecma 262 5th Edition, clause 14): |
| 1494 | // SourceElement: |
| 1495 | // Statement |
| 1496 | // FunctionDeclaration |
| 1497 | // |
| 1498 | // In harmony mode we allow additionally the following productions |
| 1499 | // BlockElement (aka SourceElement): |
| 1500 | // LetDeclaration |
| 1501 | // ConstDeclaration |
| 1502 | |
| 1503 | switch (peek()) { |
| 1504 | case Token::FUNCTION: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1505 | return ParseFunctionDeclaration(NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1506 | case Token::LET: |
| 1507 | case Token::CONST: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1508 | return ParseVariableStatement(kModuleElement, NULL, ok); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1509 | default: |
| 1510 | return ParseStatement(labels, ok); |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1515 | Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) { |
| 1516 | // Statement :: |
| 1517 | // Block |
| 1518 | // VariableStatement |
| 1519 | // EmptyStatement |
| 1520 | // ExpressionStatement |
| 1521 | // IfStatement |
| 1522 | // IterationStatement |
| 1523 | // ContinueStatement |
| 1524 | // BreakStatement |
| 1525 | // ReturnStatement |
| 1526 | // WithStatement |
| 1527 | // LabelledStatement |
| 1528 | // SwitchStatement |
| 1529 | // ThrowStatement |
| 1530 | // TryStatement |
| 1531 | // DebuggerStatement |
| 1532 | |
| 1533 | // Note: Since labels can only be used by 'break' and 'continue' |
| 1534 | // statements, which themselves are only valid within blocks, |
| 1535 | // iterations or 'switch' statements (i.e., BreakableStatements), |
| 1536 | // labels can be simply ignored in all other cases; except for |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1537 | // trivial labeled break statements 'label: break label' which is |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1538 | // parsed into an empty statement. |
| 1539 | |
| 1540 | // Keep the source position of the statement |
| 1541 | int statement_pos = scanner().peek_location().beg_pos; |
| 1542 | Statement* stmt = NULL; |
| 1543 | switch (peek()) { |
| 1544 | case Token::LBRACE: |
| 1545 | return ParseBlock(labels, ok); |
| 1546 | |
| 1547 | case Token::CONST: // fall through |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1548 | case Token::LET: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1549 | case Token::VAR: |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1550 | stmt = ParseVariableStatement(kStatement, NULL, ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1551 | break; |
| 1552 | |
| 1553 | case Token::SEMICOLON: |
| 1554 | Next(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1555 | return factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1556 | |
| 1557 | case Token::IF: |
| 1558 | stmt = ParseIfStatement(labels, ok); |
| 1559 | break; |
| 1560 | |
| 1561 | case Token::DO: |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1562 | stmt = ParseDoWhileStatement(labels, ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | break; |
| 1564 | |
| 1565 | case Token::WHILE: |
| 1566 | stmt = ParseWhileStatement(labels, ok); |
| 1567 | break; |
| 1568 | |
| 1569 | case Token::FOR: |
| 1570 | stmt = ParseForStatement(labels, ok); |
| 1571 | break; |
| 1572 | |
| 1573 | case Token::CONTINUE: |
| 1574 | stmt = ParseContinueStatement(ok); |
| 1575 | break; |
| 1576 | |
| 1577 | case Token::BREAK: |
| 1578 | stmt = ParseBreakStatement(labels, ok); |
| 1579 | break; |
| 1580 | |
| 1581 | case Token::RETURN: |
| 1582 | stmt = ParseReturnStatement(ok); |
| 1583 | break; |
| 1584 | |
| 1585 | case Token::WITH: |
| 1586 | stmt = ParseWithStatement(labels, ok); |
| 1587 | break; |
| 1588 | |
| 1589 | case Token::SWITCH: |
| 1590 | stmt = ParseSwitchStatement(labels, ok); |
| 1591 | break; |
| 1592 | |
| 1593 | case Token::THROW: |
| 1594 | stmt = ParseThrowStatement(ok); |
| 1595 | break; |
| 1596 | |
| 1597 | case Token::TRY: { |
| 1598 | // NOTE: It is somewhat complicated to have labels on |
| 1599 | // try-statements. When breaking out of a try-finally statement, |
| 1600 | // one must take great care not to treat it as a |
| 1601 | // fall-through. It is much easier just to wrap the entire |
| 1602 | // try-statement in a statement block and put the labels there |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1603 | Block* result = factory()->NewBlock(labels, 1, false); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1604 | Target target(&this->target_stack_, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1605 | TryStatement* statement = ParseTryStatement(CHECK_OK); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1606 | if (statement) { |
| 1607 | statement->set_statement_pos(statement_pos); |
| 1608 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1609 | if (result) result->AddStatement(statement, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1610 | return result; |
| 1611 | } |
| 1612 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1613 | case Token::FUNCTION: { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1614 | // FunctionDeclaration is only allowed in the context of SourceElements |
| 1615 | // (Ecma 262 5th Edition, clause 14): |
| 1616 | // SourceElement: |
| 1617 | // Statement |
| 1618 | // FunctionDeclaration |
| 1619 | // Common language extension is to allow function declaration in place |
| 1620 | // of any statement. This language extension is disabled in strict mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1621 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1622 | ReportMessageAt(scanner().peek_location(), "strict_function", |
| 1623 | Vector<const char*>::empty()); |
| 1624 | *ok = false; |
| 1625 | return NULL; |
| 1626 | } |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1627 | return ParseFunctionDeclaration(NULL, ok); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1628 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1629 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1630 | case Token::DEBUGGER: |
| 1631 | stmt = ParseDebuggerStatement(ok); |
| 1632 | break; |
| 1633 | |
| 1634 | default: |
| 1635 | stmt = ParseExpressionOrLabelledStatement(labels, ok); |
| 1636 | } |
| 1637 | |
| 1638 | // Store the source position of the statement |
| 1639 | if (stmt != NULL) stmt->set_statement_pos(statement_pos); |
| 1640 | return stmt; |
| 1641 | } |
| 1642 | |
| 1643 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1644 | VariableProxy* Parser::NewUnresolved( |
| 1645 | Handle<String> name, VariableMode mode, Interface* interface) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1646 | // If we are inside a function, a declaration of a var/const variable is a |
| 1647 | // truly local variable, and the scope of the variable is always the function |
| 1648 | // scope. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1649 | // Let/const variables in harmony mode are always added to the immediately |
| 1650 | // enclosing scope. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1651 | return DeclarationScope(mode)->NewUnresolved( |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1652 | factory(), name, interface, scanner().location().beg_pos); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | |
| 1656 | void Parser::Declare(Declaration* declaration, bool resolve, bool* ok) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1657 | VariableProxy* proxy = declaration->proxy(); |
| 1658 | Handle<String> name = proxy->name(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1659 | VariableMode mode = declaration->mode(); |
| 1660 | Scope* declaration_scope = DeclarationScope(mode); |
| 1661 | Variable* var = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1662 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1663 | // If a suitable scope exists, then we can statically declare this |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1664 | // variable and also set its mode. In any case, a Declaration node |
| 1665 | // will be added to the scope so that the declaration can be added |
| 1666 | // to the corresponding activation frame at runtime if necessary. |
| 1667 | // For instance declarations inside an eval scope need to be added |
| 1668 | // to the calling function context. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 1669 | // Similarly, strict mode eval scope does not leak variable declarations to |
| 1670 | // the caller's scope so we declare all locals, too. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1671 | if (declaration_scope->is_function_scope() || |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1672 | declaration_scope->is_strict_or_extended_eval_scope() || |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1673 | declaration_scope->is_block_scope() || |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1674 | declaration_scope->is_module_scope() || |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1675 | declaration_scope->is_global_scope()) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1676 | // Declare the variable in the declaration scope. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1677 | // For the global scope, we have to check for collisions with earlier |
| 1678 | // (i.e., enclosing) global scopes, to maintain the illusion of a single |
| 1679 | // global scope. |
| 1680 | var = declaration_scope->is_global_scope() |
| 1681 | ? declaration_scope->Lookup(name) |
| 1682 | : declaration_scope->LocalLookup(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1683 | if (var == NULL) { |
| 1684 | // Declare the name. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1685 | var = declaration_scope->DeclareLocal( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1686 | name, mode, declaration->initialization(), proxy->interface()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1687 | } else if ((mode != VAR || var->mode() != VAR) && |
| 1688 | (!declaration_scope->is_global_scope() || |
| 1689 | IsLexicalVariableMode(mode) || |
| 1690 | IsLexicalVariableMode(var->mode()))) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1691 | // The name was declared in this scope before; check for conflicting |
| 1692 | // re-declarations. We have a conflict if either of the declarations is |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1693 | // not a var (in the global scope, we also have to ignore legacy const for |
| 1694 | // compatibility). There is similar code in runtime.cc in the Declare |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1695 | // functions. The function CheckNonConflictingScope checks for conflicting |
| 1696 | // var and let bindings from different scopes whereas this is a check for |
| 1697 | // conflicting declarations within the same scope. This check also covers |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1698 | // the special case |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1699 | // |
| 1700 | // function () { let x; { var x; } } |
| 1701 | // |
| 1702 | // because the var declaration is hoisted to the function scope where 'x' |
| 1703 | // is already bound. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1704 | ASSERT(IsDeclaredVariableMode(var->mode())); |
| 1705 | if (is_extended_mode()) { |
| 1706 | // In harmony mode we treat re-declarations as early errors. See |
| 1707 | // ES5 16 for a definition of early errors. |
| 1708 | SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS); |
| 1709 | const char* elms[2] = { "Variable", *c_string }; |
| 1710 | Vector<const char*> args(elms, 2); |
| 1711 | ReportMessage("redeclaration", args); |
| 1712 | *ok = false; |
| 1713 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1714 | } |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1715 | Handle<String> type_string = |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1716 | isolate()->factory()->NewStringFromUtf8(CStrVector("Variable"), |
| 1717 | TENURED); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1718 | Expression* expression = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1719 | NewThrowTypeError(isolate()->factory()->redeclaration_string(), |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1720 | type_string, name); |
| 1721 | declaration_scope->SetIllegalRedeclaration(expression); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | // We add a declaration node for every declaration. The compiler |
| 1726 | // will only generate code if necessary. In particular, declarations |
| 1727 | // for inner local variables that do not represent functions won't |
| 1728 | // result in any generated code. |
| 1729 | // |
| 1730 | // Note that we always add an unresolved proxy even if it's not |
| 1731 | // used, simply because we don't know in this method (w/o extra |
| 1732 | // parameters) if the proxy is needed or not. The proxy will be |
| 1733 | // bound during variable resolution time unless it was pre-bound |
| 1734 | // below. |
| 1735 | // |
| 1736 | // WARNING: This will lead to multiple declaration nodes for the |
| 1737 | // same variable if it is declared several times. This is not a |
| 1738 | // semantic issue as long as we keep the source order, but it may be |
| 1739 | // a performance issue since it may lead to repeated |
| 1740 | // Runtime::DeclareContextSlot() calls. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1741 | declaration_scope->AddDeclaration(declaration); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1742 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1743 | if (mode == CONST && declaration_scope->is_global_scope()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1744 | // For global const variables we bind the proxy to a variable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1745 | ASSERT(resolve); // should be set by all callers |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1746 | Variable::Kind kind = Variable::NORMAL; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1747 | var = new(zone()) Variable(declaration_scope, |
| 1748 | name, |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1749 | mode, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1750 | true, |
| 1751 | kind, |
| 1752 | kNeedsInitialization); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1753 | } else if (declaration_scope->is_eval_scope() && |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1754 | declaration_scope->is_classic_mode()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1755 | // For variable declarations in a non-strict eval scope the proxy is bound |
| 1756 | // to a lookup variable to force a dynamic declaration using the |
| 1757 | // DeclareContextSlot runtime function. |
| 1758 | Variable::Kind kind = Variable::NORMAL; |
| 1759 | var = new(zone()) Variable(declaration_scope, |
| 1760 | name, |
| 1761 | mode, |
| 1762 | true, |
| 1763 | kind, |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1764 | declaration->initialization()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1765 | var->AllocateTo(Variable::LOOKUP, -1); |
| 1766 | resolve = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | // If requested and we have a local variable, bind the proxy to the variable |
| 1770 | // at parse-time. This is used for functions (and consts) declared inside |
| 1771 | // statements: the corresponding function (or const) variable must be in the |
| 1772 | // function scope and not a statement-local scope, e.g. as provided with a |
| 1773 | // 'with' statement: |
| 1774 | // |
| 1775 | // with (obj) { |
| 1776 | // function f() {} |
| 1777 | // } |
| 1778 | // |
| 1779 | // which is translated into: |
| 1780 | // |
| 1781 | // with (obj) { |
| 1782 | // // in this case this is not: 'var f; f = function () {};' |
| 1783 | // var f = function () {}; |
| 1784 | // } |
| 1785 | // |
| 1786 | // Note that if 'f' is accessed from inside the 'with' statement, it |
| 1787 | // will be allocated in the context (because we must be able to look |
| 1788 | // it up dynamically) but it will also be accessed statically, i.e., |
| 1789 | // with a context slot index and a context chain length for this |
| 1790 | // initialization code. Thus, inside the 'with' statement, we need |
| 1791 | // both access to the static and the dynamic context chain; the |
| 1792 | // runtime needs to provide both. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1793 | if (resolve && var != NULL) { |
| 1794 | proxy->BindTo(var); |
| 1795 | |
| 1796 | if (FLAG_harmony_modules) { |
| 1797 | bool ok; |
| 1798 | #ifdef DEBUG |
| 1799 | if (FLAG_print_interface_details) |
| 1800 | PrintF("# Declare %s\n", var->name()->ToAsciiArray()); |
| 1801 | #endif |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1802 | proxy->interface()->Unify(var->interface(), zone(), &ok); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1803 | if (!ok) { |
| 1804 | #ifdef DEBUG |
| 1805 | if (FLAG_print_interfaces) { |
| 1806 | PrintF("DECLARE TYPE ERROR\n"); |
| 1807 | PrintF("proxy: "); |
| 1808 | proxy->interface()->Print(); |
| 1809 | PrintF("var: "); |
| 1810 | var->interface()->Print(); |
| 1811 | } |
| 1812 | #endif |
| 1813 | ReportMessage("module_type_error", Vector<Handle<String> >(&name, 1)); |
| 1814 | } |
| 1815 | } |
| 1816 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | |
| 1820 | // Language extension which is only enabled for source files loaded |
| 1821 | // through the API's extension mechanism. A native function |
| 1822 | // declaration is resolved by looking up the function through a |
| 1823 | // callback provided by the extension. |
| 1824 | Statement* Parser::ParseNativeDeclaration(bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | Expect(Token::FUNCTION, CHECK_OK); |
| 1826 | Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1827 | Expect(Token::LPAREN, CHECK_OK); |
| 1828 | bool done = (peek() == Token::RPAREN); |
| 1829 | while (!done) { |
| 1830 | ParseIdentifier(CHECK_OK); |
| 1831 | done = (peek() == Token::RPAREN); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 1832 | if (!done) { |
| 1833 | Expect(Token::COMMA, CHECK_OK); |
| 1834 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1835 | } |
| 1836 | Expect(Token::RPAREN, CHECK_OK); |
| 1837 | Expect(Token::SEMICOLON, CHECK_OK); |
| 1838 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1839 | // Make sure that the function containing the native declaration |
| 1840 | // isn't lazily compiled. The extension structures are only |
| 1841 | // accessible while parsing the first time not when reparsing |
| 1842 | // because of lazy compilation. |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1843 | DeclarationScope(VAR)->ForceEagerCompilation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | |
| 1845 | // Compute the function template for the native function. |
| 1846 | v8::Handle<v8::FunctionTemplate> fun_template = |
| 1847 | extension_->GetNativeFunction(v8::Utils::ToLocal(name)); |
| 1848 | ASSERT(!fun_template.IsEmpty()); |
| 1849 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1850 | // Instantiate the function and create a shared function info from it. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); |
| 1852 | const int literals = fun->NumberOfLiterals(); |
| 1853 | Handle<Code> code = Handle<Code>(fun->shared()->code()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1854 | Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 1855 | Handle<SharedFunctionInfo> shared = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1856 | isolate()->factory()->NewSharedFunctionInfo(name, literals, code, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1857 | Handle<ScopeInfo>(fun->shared()->scope_info())); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1858 | shared->set_construct_stub(*construct_stub); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1860 | // Copy the function data to the shared function info. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1861 | shared->set_function_data(fun->shared()->function_data()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1862 | int parameters = fun->shared()->formal_parameter_count(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1863 | shared->set_formal_parameter_count(parameters); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | |
| 1865 | // TODO(1240846): It's weird that native function declarations are |
| 1866 | // introduced dynamically when we meet their declarations, whereas |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1867 | // other functions are set up when entering the surrounding scope. |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1868 | VariableProxy* proxy = NewUnresolved(name, VAR, Interface::NewValue()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1869 | Declaration* declaration = |
| 1870 | factory()->NewVariableDeclaration(proxy, VAR, top_scope_); |
| 1871 | Declare(declaration, true, CHECK_OK); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1872 | SharedFunctionInfoLiteral* lit = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1873 | factory()->NewSharedFunctionInfoLiteral(shared); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1874 | return factory()->NewExpressionStatement( |
| 1875 | factory()->NewAssignment( |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1876 | Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1880 | Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1881 | // FunctionDeclaration :: |
| 1882 | // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1883 | Expect(Token::FUNCTION, CHECK_OK); |
| 1884 | int function_token_position = scanner().location().beg_pos; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1885 | bool is_strict_reserved = false; |
| 1886 | Handle<String> name = ParseIdentifierOrStrictReservedWord( |
| 1887 | &is_strict_reserved, CHECK_OK); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1888 | FunctionLiteral* fun = ParseFunctionLiteral(name, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1889 | is_strict_reserved, |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1890 | function_token_position, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1891 | FunctionLiteral::DECLARATION, |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1892 | CHECK_OK); |
| 1893 | // Even if we're not at the top-level of the global or a function |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1894 | // scope, we treat it as such and introduce the function with its |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1895 | // initial value upon entering the corresponding scope. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1896 | // In extended mode, a function behaves as a lexical binding, except in the |
| 1897 | // global scope. |
| 1898 | VariableMode mode = |
| 1899 | is_extended_mode() && !top_scope_->is_global_scope() ? LET : VAR; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1900 | VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1901 | Declaration* declaration = |
| 1902 | factory()->NewFunctionDeclaration(proxy, mode, fun, top_scope_); |
| 1903 | Declare(declaration, true, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1904 | if (names) names->Add(name, zone()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1905 | return factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | |
| 1909 | Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1910 | if (top_scope_->is_extended_mode()) return ParseScopedBlock(labels, ok); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1911 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1912 | // Block :: |
| 1913 | // '{' Statement* '}' |
| 1914 | |
| 1915 | // Note that a Block does not introduce a new execution scope! |
| 1916 | // (ECMA-262, 3rd, 12.2) |
| 1917 | // |
| 1918 | // Construct block expecting 16 statements. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1919 | Block* result = factory()->NewBlock(labels, 16, false); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1920 | Target target(&this->target_stack_, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1921 | Expect(Token::LBRACE, CHECK_OK); |
| 1922 | while (peek() != Token::RBRACE) { |
| 1923 | Statement* stat = ParseStatement(NULL, CHECK_OK); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1924 | if (stat && !stat->IsEmpty()) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1925 | result->AddStatement(stat, zone()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 1926 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | } |
| 1928 | Expect(Token::RBRACE, CHECK_OK); |
| 1929 | return result; |
| 1930 | } |
| 1931 | |
| 1932 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1933 | Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1934 | // The harmony mode uses block elements instead of statements. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1935 | // |
| 1936 | // Block :: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1937 | // '{' BlockElement* '}' |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1938 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1939 | // Construct block expecting 16 statements. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1940 | Block* body = factory()->NewBlock(labels, 16, false); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1941 | Scope* block_scope = NewScope(top_scope_, BLOCK_SCOPE); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1942 | |
| 1943 | // Parse the statements and collect escaping labels. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1944 | Expect(Token::LBRACE, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1945 | block_scope->set_start_position(scanner().location().beg_pos); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1946 | { BlockState block_state(this, block_scope); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1947 | TargetCollector collector(zone()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1948 | Target target(&this->target_stack_, &collector); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1949 | Target target_body(&this->target_stack_, body); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1950 | |
| 1951 | while (peek() != Token::RBRACE) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1952 | Statement* stat = ParseBlockElement(NULL, CHECK_OK); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1953 | if (stat && !stat->IsEmpty()) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 1954 | body->AddStatement(stat, zone()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | Expect(Token::RBRACE, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1959 | block_scope->set_end_position(scanner().location().end_pos); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1960 | block_scope = block_scope->FinalizeBlockScope(); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1961 | body->set_scope(block_scope); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1962 | return body; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1966 | Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context, |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1967 | ZoneStringList* names, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1968 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1969 | // VariableStatement :: |
| 1970 | // VariableDeclarations ';' |
| 1971 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1972 | Handle<String> ignore; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1973 | Block* result = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1974 | ParseVariableDeclarations(var_context, NULL, names, &ignore, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1975 | ExpectSemicolon(CHECK_OK); |
| 1976 | return result; |
| 1977 | } |
| 1978 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1979 | |
| 1980 | bool Parser::IsEvalOrArguments(Handle<String> string) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1981 | return string.is_identical_to(isolate()->factory()->eval_string()) || |
| 1982 | string.is_identical_to(isolate()->factory()->arguments_string()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1983 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1984 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1985 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1986 | // If the variable declaration declares exactly one non-const |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1987 | // variable, then *out is set to that variable. In all other cases, |
| 1988 | // *out is untouched; in particular, it is the caller's responsibility |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1989 | // to initialize it properly. This mechanism is used for the parsing |
| 1990 | // of 'for-in' loops. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1991 | Block* Parser::ParseVariableDeclarations( |
| 1992 | VariableDeclarationContext var_context, |
| 1993 | VariableDeclarationProperties* decl_props, |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 1994 | ZoneStringList* names, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1995 | Handle<String>* out, |
| 1996 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1997 | // VariableDeclarations :: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1998 | // ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[','] |
| 1999 | // |
| 2000 | // The ES6 Draft Rev3 specifies the following grammar for const declarations |
| 2001 | // |
| 2002 | // ConstDeclaration :: |
| 2003 | // const ConstBinding (',' ConstBinding)* ';' |
| 2004 | // ConstBinding :: |
| 2005 | // Identifier '=' AssignmentExpression |
| 2006 | // |
| 2007 | // TODO(ES6): |
| 2008 | // ConstBinding :: |
| 2009 | // BindingPattern '=' AssignmentExpression |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2010 | VariableMode mode = VAR; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2011 | // True if the binding needs initialization. 'let' and 'const' declared |
| 2012 | // bindings are created uninitialized by their declaration nodes and |
| 2013 | // need initialization. 'var' declared bindings are always initialized |
| 2014 | // immediately by their declaration nodes. |
| 2015 | bool needs_init = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2016 | bool is_const = false; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2017 | Token::Value init_op = Token::INIT_VAR; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2018 | if (peek() == Token::VAR) { |
| 2019 | Consume(Token::VAR); |
| 2020 | } else if (peek() == Token::CONST) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2021 | // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: |
| 2022 | // |
| 2023 | // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' |
| 2024 | // |
| 2025 | // * It is a Syntax Error if the code that matches this production is not |
| 2026 | // contained in extended code. |
| 2027 | // |
| 2028 | // However disallowing const in classic mode will break compatibility with |
| 2029 | // existing pages. Therefore we keep allowing const with the old |
| 2030 | // non-harmony semantics in classic mode. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2031 | Consume(Token::CONST); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2032 | switch (top_scope_->language_mode()) { |
| 2033 | case CLASSIC_MODE: |
| 2034 | mode = CONST; |
| 2035 | init_op = Token::INIT_CONST; |
| 2036 | break; |
| 2037 | case STRICT_MODE: |
| 2038 | ReportMessage("strict_const", Vector<const char*>::empty()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2039 | *ok = false; |
| 2040 | return NULL; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2041 | case EXTENDED_MODE: |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2042 | if (var_context == kStatement) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2043 | // In extended mode 'const' declarations are only allowed in source |
| 2044 | // element positions. |
| 2045 | ReportMessage("unprotected_const", Vector<const char*>::empty()); |
| 2046 | *ok = false; |
| 2047 | return NULL; |
| 2048 | } |
| 2049 | mode = CONST_HARMONY; |
| 2050 | init_op = Token::INIT_CONST_HARMONY; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2051 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2052 | is_const = true; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2053 | needs_init = true; |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2054 | } else if (peek() == Token::LET) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2055 | // ES6 Draft Rev4 section 12.2.1: |
| 2056 | // |
| 2057 | // LetDeclaration : let LetBindingList ; |
| 2058 | // |
| 2059 | // * It is a Syntax Error if the code that matches this production is not |
| 2060 | // contained in extended code. |
| 2061 | if (!is_extended_mode()) { |
| 2062 | ReportMessage("illegal_let", Vector<const char*>::empty()); |
| 2063 | *ok = false; |
| 2064 | return NULL; |
| 2065 | } |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2066 | Consume(Token::LET); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2067 | if (var_context == kStatement) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2068 | // Let declarations are only allowed in source element positions. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2069 | ReportMessage("unprotected_let", Vector<const char*>::empty()); |
| 2070 | *ok = false; |
| 2071 | return NULL; |
| 2072 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 2073 | mode = LET; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2074 | needs_init = true; |
| 2075 | init_op = Token::INIT_LET; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2076 | } else { |
| 2077 | UNREACHABLE(); // by current callers |
| 2078 | } |
| 2079 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2080 | Scope* declaration_scope = DeclarationScope(mode); |
| 2081 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2082 | // The scope of a var/const declared variable anywhere inside a function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2084 | // transform a source-level var/const declaration into a (Function) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2085 | // Scope declaration, and rewrite the source-level initialization into an |
| 2086 | // assignment statement. We use a block to collect multiple assignments. |
| 2087 | // |
| 2088 | // We mark the block as initializer block because we don't want the |
| 2089 | // rewriter to add a '.result' assignment to such a block (to get compliant |
| 2090 | // behavior for code such as print(eval('var x = 7')), and for cosmetic |
| 2091 | // reasons when pretty-printing. Also, unless an assignment (initialization) |
| 2092 | // is inside an initializer block, it is ignored. |
| 2093 | // |
| 2094 | // Create new block with one expected declaration. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 2095 | Block* block = factory()->NewBlock(NULL, 1, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2096 | int nvars = 0; // the number of variables declared |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2097 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2098 | do { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2099 | if (fni_ != NULL) fni_->Enter(); |
| 2100 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | // Parse variable name. |
| 2102 | if (nvars > 0) Consume(Token::COMMA); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2103 | name = ParseIdentifier(CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2104 | if (fni_ != NULL) fni_->PushVariableName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2105 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2106 | // Strict mode variables may not be named eval or arguments |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2107 | if (!declaration_scope->is_classic_mode() && IsEvalOrArguments(name)) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2108 | ReportMessage("strict_var_name", Vector<const char*>::empty()); |
| 2109 | *ok = false; |
| 2110 | return NULL; |
| 2111 | } |
| 2112 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | // Declare variable. |
| 2114 | // Note that we *always* must treat the initial value via a separate init |
| 2115 | // assignment for variables and constants because the value must be assigned |
| 2116 | // when the variable is encountered in the source. But the variable/constant |
| 2117 | // is declared (and set to 'undefined') upon entering the function within |
| 2118 | // which the variable or constant is declared. Only function variables have |
| 2119 | // an initial value in the declaration (because they are initialized upon |
| 2120 | // entering the function). |
| 2121 | // |
| 2122 | // If we have a const declaration, in an inner scope, the proxy is always |
| 2123 | // bound to the declared variable (independent of possibly surrounding with |
| 2124 | // statements). |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2125 | // For let/const declarations in harmony mode, we can also immediately |
| 2126 | // pre-resolve the proxy because it resides in the same scope as the |
| 2127 | // declaration. |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2128 | Interface* interface = |
| 2129 | is_const ? Interface::NewConst() : Interface::NewValue(); |
| 2130 | VariableProxy* proxy = NewUnresolved(name, mode, interface); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2131 | Declaration* declaration = |
| 2132 | factory()->NewVariableDeclaration(proxy, mode, top_scope_); |
| 2133 | Declare(declaration, mode != VAR, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2134 | nvars++; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2135 | if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 2136 | ReportMessageAt(scanner().location(), "too_many_variables", |
| 2137 | Vector<const char*>::empty()); |
| 2138 | *ok = false; |
| 2139 | return NULL; |
| 2140 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2141 | if (names) names->Add(name, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Parse initialization expression if present and/or needed. A |
| 2144 | // declaration of the form: |
| 2145 | // |
| 2146 | // var v = x; |
| 2147 | // |
| 2148 | // is syntactic sugar for: |
| 2149 | // |
| 2150 | // var v; v = x; |
| 2151 | // |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2152 | // In particular, we need to re-lookup 'v' (in top_scope_, not |
| 2153 | // declaration_scope) as it may be a different 'v' than the 'v' in the |
| 2154 | // declaration (e.g., if we are inside a 'with' statement or 'catch' |
| 2155 | // block). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | // |
| 2157 | // However, note that const declarations are different! A const |
| 2158 | // declaration of the form: |
| 2159 | // |
| 2160 | // const c = x; |
| 2161 | // |
| 2162 | // is *not* syntactic sugar for: |
| 2163 | // |
| 2164 | // const c; c = x; |
| 2165 | // |
| 2166 | // The "variable" c initialized to x is the same as the declared |
| 2167 | // one - there is no re-lookup (see the last parameter of the |
| 2168 | // Declare() call above). |
| 2169 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2170 | Scope* initialization_scope = is_const ? declaration_scope : top_scope_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2171 | Expression* value = NULL; |
| 2172 | int position = -1; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2173 | // Harmony consts have non-optional initializers. |
| 2174 | if (peek() == Token::ASSIGN || mode == CONST_HARMONY) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | Expect(Token::ASSIGN, CHECK_OK); |
| 2176 | position = scanner().location().beg_pos; |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 2177 | value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2178 | // Don't infer if it is "a = function(){...}();"-like expression. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2179 | if (fni_ != NULL && |
| 2180 | value->AsCall() == NULL && |
| 2181 | value->AsCallNew() == NULL) { |
| 2182 | fni_->Infer(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2183 | } else { |
| 2184 | fni_->RemoveLastFunction(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2185 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2186 | if (decl_props != NULL) *decl_props = kHasInitializers; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2189 | // Record the end position of the initializer. |
| 2190 | if (proxy->var() != NULL) { |
| 2191 | proxy->var()->set_initializer_position(scanner().location().end_pos); |
| 2192 | } |
| 2193 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 2194 | // Make sure that 'const x' and 'let x' initialize 'x' to undefined. |
| 2195 | if (value == NULL && needs_init) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2196 | value = GetLiteralUndefined(); |
| 2197 | } |
| 2198 | |
| 2199 | // Global variable declarations must be compiled in a specific |
| 2200 | // way. When the script containing the global variable declaration |
| 2201 | // is entered, the global variable must be declared, so that if it |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 2202 | // doesn't exist (on the global object itself, see ES5 errata) it |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2203 | // gets created with an initial undefined value. This is handled |
| 2204 | // by the declarations part of the function representing the |
| 2205 | // top-level global code; see Runtime::DeclareGlobalVariable. If |
| 2206 | // it already exists (in the object or in a prototype), it is |
| 2207 | // *not* touched until the variable declaration statement is |
| 2208 | // executed. |
| 2209 | // |
| 2210 | // Executing the variable declaration statement will always |
| 2211 | // guarantee to give the global object a "local" variable; a |
| 2212 | // variable defined in the global object and not in any |
| 2213 | // prototype. This way, global variable declarations can shadow |
| 2214 | // properties in the prototype chain, but only after the variable |
| 2215 | // declaration statement has been executed. This is important in |
| 2216 | // browsers where the global object (window) has lots of |
| 2217 | // properties defined in prototype objects. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 2218 | if (initialization_scope->is_global_scope() && |
| 2219 | !IsLexicalVariableMode(mode)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2220 | // Compute the arguments for the runtime call. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2221 | ZoneList<Expression*>* arguments = |
| 2222 | new(zone()) ZoneList<Expression*>(3, zone()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2223 | // We have at least 1 parameter. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2224 | arguments->Add(factory()->NewLiteral(name), zone()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2225 | CallRuntime* initialize; |
| 2226 | |
| 2227 | if (is_const) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2228 | arguments->Add(value, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | value = NULL; // zap the value to avoid the unnecessary assignment |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2230 | |
| 2231 | // Construct the call to Runtime_InitializeConstGlobal |
| 2232 | // and add it to the initialization statement block. |
| 2233 | // Note that the function does different things depending on |
| 2234 | // the number of arguments (1 or 2). |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2235 | initialize = factory()->NewCallRuntime( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2236 | isolate()->factory()->InitializeConstGlobal_string(), |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2237 | Runtime::FunctionForId(Runtime::kInitializeConstGlobal), |
| 2238 | arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2239 | } else { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2240 | // Add strict mode. |
| 2241 | // We may want to pass singleton to avoid Literal allocations. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2242 | LanguageMode language_mode = initialization_scope->language_mode(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2243 | arguments->Add(factory()->NewNumberLiteral(language_mode), zone()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2244 | |
| 2245 | // Be careful not to assign a value to the global variable if |
| 2246 | // we're in a with. The initialization value should not |
| 2247 | // necessarily be stored in the global object in that case, |
| 2248 | // which is why we need to generate a separate assignment node. |
| 2249 | if (value != NULL && !inside_with()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2250 | arguments->Add(value, zone()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2251 | value = NULL; // zap the value to avoid the unnecessary assignment |
| 2252 | } |
| 2253 | |
| 2254 | // Construct the call to Runtime_InitializeVarGlobal |
| 2255 | // and add it to the initialization statement block. |
| 2256 | // Note that the function does different things depending on |
| 2257 | // the number of arguments (2 or 3). |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2258 | initialize = factory()->NewCallRuntime( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2259 | isolate()->factory()->InitializeVarGlobal_string(), |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2260 | Runtime::FunctionForId(Runtime::kInitializeVarGlobal), |
| 2261 | arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2262 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2263 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2264 | block->AddStatement(factory()->NewExpressionStatement(initialize), |
| 2265 | zone()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2266 | } else if (needs_init) { |
| 2267 | // Constant initializations always assign to the declared constant which |
| 2268 | // is always at the function scope level. This is only relevant for |
| 2269 | // dynamically looked-up variables and constants (the start context for |
| 2270 | // constant lookups is always the function context, while it is the top |
| 2271 | // context for var declared variables). Sigh... |
| 2272 | // For 'let' and 'const' declared variables in harmony mode the |
| 2273 | // initialization also always assigns to the declared variable. |
| 2274 | ASSERT(proxy != NULL); |
| 2275 | ASSERT(proxy->var() != NULL); |
| 2276 | ASSERT(value != NULL); |
| 2277 | Assignment* assignment = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2278 | factory()->NewAssignment(init_op, proxy, value, position); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2279 | block->AddStatement(factory()->NewExpressionStatement(assignment), |
| 2280 | zone()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2281 | value = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 2284 | // Add an assignment node to the initialization statement block if we still |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2285 | // have a pending initialization value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2286 | if (value != NULL) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2287 | ASSERT(mode == VAR); |
| 2288 | // 'var' initializations are simply assignments (with all the consequences |
| 2289 | // if they are inside a 'with' statement - they may change a 'with' object |
| 2290 | // property). |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2291 | VariableProxy* proxy = |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2292 | initialization_scope->NewUnresolved(factory(), name, interface); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2293 | Assignment* assignment = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2294 | factory()->NewAssignment(init_op, proxy, value, position); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2295 | block->AddStatement(factory()->NewExpressionStatement(assignment), |
| 2296 | zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2297 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2298 | |
| 2299 | if (fni_ != NULL) fni_->Leave(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2300 | } while (peek() == Token::COMMA); |
| 2301 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2302 | // If there was a single non-const declaration, return it in the output |
| 2303 | // parameter for possible use by for/in. |
| 2304 | if (nvars == 1 && !is_const) { |
| 2305 | *out = name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | return block; |
| 2309 | } |
| 2310 | |
| 2311 | |
| 2312 | static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) { |
| 2313 | ASSERT(!label.is_null()); |
| 2314 | if (labels != NULL) |
| 2315 | for (int i = labels->length(); i-- > 0; ) |
| 2316 | if (labels->at(i).is_identical_to(label)) |
| 2317 | return true; |
| 2318 | |
| 2319 | return false; |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels, |
| 2324 | bool* ok) { |
| 2325 | // ExpressionStatement | LabelledStatement :: |
| 2326 | // Expression ';' |
| 2327 | // Identifier ':' Statement |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2328 | bool starts_with_idenfifier = peek_any_identifier(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2329 | Expression* expr = ParseExpression(true, CHECK_OK); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2330 | if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2331 | expr->AsVariableProxy() != NULL && |
| 2332 | !expr->AsVariableProxy()->is_this()) { |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2333 | // Expression is a single identifier, and not, e.g., a parenthesized |
| 2334 | // identifier. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2335 | VariableProxy* var = expr->AsVariableProxy(); |
| 2336 | Handle<String> label = var->name(); |
| 2337 | // TODO(1240780): We don't check for redeclaration of labels |
| 2338 | // during preparsing since keeping track of the set of active |
| 2339 | // labels requires nontrivial changes to the way scopes are |
| 2340 | // structured. However, these are probably changes we want to |
| 2341 | // make later anyway so we should go back and fix this then. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2342 | if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 2343 | SmartArrayPointer<char> c_string = label->ToCString(DISALLOW_NULLS); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2344 | const char* elms[2] = { "Label", *c_string }; |
| 2345 | Vector<const char*> args(elms, 2); |
| 2346 | ReportMessage("redeclaration", args); |
| 2347 | *ok = false; |
| 2348 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2349 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2350 | if (labels == NULL) { |
| 2351 | labels = new(zone()) ZoneStringList(4, zone()); |
| 2352 | } |
| 2353 | labels->Add(label, zone()); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2354 | // Remove the "ghost" variable that turned out to be a label |
| 2355 | // from the top scope. This way, we don't try to resolve it |
| 2356 | // during the scope processing. |
| 2357 | top_scope_->RemoveUnresolved(var); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2358 | Expect(Token::COLON, CHECK_OK); |
| 2359 | return ParseStatement(labels, ok); |
| 2360 | } |
| 2361 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2362 | // If we have an extension, we allow a native function declaration. |
| 2363 | // A native function declaration starts with "native function" with |
| 2364 | // no line-terminator between the two words. |
| 2365 | if (extension_ != NULL && |
| 2366 | peek() == Token::FUNCTION && |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2367 | !scanner().HasAnyLineTerminatorBeforeNext() && |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2368 | expr != NULL && |
| 2369 | expr->AsVariableProxy() != NULL && |
| 2370 | expr->AsVariableProxy()->name()->Equals( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2371 | isolate()->heap()->native_string()) && |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2372 | !scanner().literal_contains_escapes()) { |
| 2373 | return ParseNativeDeclaration(ok); |
| 2374 | } |
| 2375 | |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 2376 | // Parsed expression statement, or the context-sensitive 'module' keyword. |
| 2377 | // Only expect semicolon in the former case. |
| 2378 | if (!FLAG_harmony_modules || |
| 2379 | peek() != Token::IDENTIFIER || |
| 2380 | scanner().HasAnyLineTerminatorBeforeNext() || |
| 2381 | expr->AsVariableProxy() == NULL || |
| 2382 | !expr->AsVariableProxy()->name()->Equals( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2383 | isolate()->heap()->module_string()) || |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 2384 | scanner().literal_contains_escapes()) { |
| 2385 | ExpectSemicolon(CHECK_OK); |
| 2386 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2387 | return factory()->NewExpressionStatement(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | |
| 2391 | IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) { |
| 2392 | // IfStatement :: |
| 2393 | // 'if' '(' Expression ')' Statement ('else' Statement)? |
| 2394 | |
| 2395 | Expect(Token::IF, CHECK_OK); |
| 2396 | Expect(Token::LPAREN, CHECK_OK); |
| 2397 | Expression* condition = ParseExpression(true, CHECK_OK); |
| 2398 | Expect(Token::RPAREN, CHECK_OK); |
| 2399 | Statement* then_statement = ParseStatement(labels, CHECK_OK); |
| 2400 | Statement* else_statement = NULL; |
| 2401 | if (peek() == Token::ELSE) { |
| 2402 | Next(); |
| 2403 | else_statement = ParseStatement(labels, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2404 | } else { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2405 | else_statement = factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2406 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2407 | return factory()->NewIfStatement(condition, then_statement, else_statement); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | |
| 2411 | Statement* Parser::ParseContinueStatement(bool* ok) { |
| 2412 | // ContinueStatement :: |
| 2413 | // 'continue' Identifier? ';' |
| 2414 | |
| 2415 | Expect(Token::CONTINUE, CHECK_OK); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2416 | Handle<String> label = Handle<String>::null(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2417 | Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2418 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2419 | tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2420 | label = ParseIdentifier(CHECK_OK); |
| 2421 | } |
| 2422 | IterationStatement* target = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2423 | target = LookupContinueTarget(label, CHECK_OK); |
| 2424 | if (target == NULL) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2425 | // Illegal continue statement. |
| 2426 | const char* message = "illegal_continue"; |
| 2427 | Vector<Handle<String> > args; |
| 2428 | if (!label.is_null()) { |
| 2429 | message = "unknown_label"; |
| 2430 | args = Vector<Handle<String> >(&label, 1); |
| 2431 | } |
| 2432 | ReportMessageAt(scanner().location(), message, args); |
| 2433 | *ok = false; |
| 2434 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2435 | } |
| 2436 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2437 | return factory()->NewContinueStatement(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | |
| 2441 | Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) { |
| 2442 | // BreakStatement :: |
| 2443 | // 'break' Identifier? ';' |
| 2444 | |
| 2445 | Expect(Token::BREAK, CHECK_OK); |
| 2446 | Handle<String> label; |
| 2447 | Token::Value tok = peek(); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2448 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2449 | tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2450 | label = ParseIdentifier(CHECK_OK); |
| 2451 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2452 | // Parse labeled break statements that target themselves into |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2453 | // empty statements, e.g. 'l1: l2: l3: break l2;' |
| 2454 | if (!label.is_null() && ContainsLabel(labels, label)) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2455 | ExpectSemicolon(CHECK_OK); |
| 2456 | return factory()->NewEmptyStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2457 | } |
| 2458 | BreakableStatement* target = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2459 | target = LookupBreakTarget(label, CHECK_OK); |
| 2460 | if (target == NULL) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2461 | // Illegal break statement. |
| 2462 | const char* message = "illegal_break"; |
| 2463 | Vector<Handle<String> > args; |
| 2464 | if (!label.is_null()) { |
| 2465 | message = "unknown_label"; |
| 2466 | args = Vector<Handle<String> >(&label, 1); |
| 2467 | } |
| 2468 | ReportMessageAt(scanner().location(), message, args); |
| 2469 | *ok = false; |
| 2470 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2471 | } |
| 2472 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2473 | return factory()->NewBreakStatement(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | Statement* Parser::ParseReturnStatement(bool* ok) { |
| 2478 | // ReturnStatement :: |
| 2479 | // 'return' Expression? ';' |
| 2480 | |
| 2481 | // Consume the return token. It is necessary to do the before |
| 2482 | // reporting any errors on it, because of the way errors are |
| 2483 | // reported (underlining). |
| 2484 | Expect(Token::RETURN, CHECK_OK); |
| 2485 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2486 | Token::Value tok = peek(); |
| 2487 | Statement* result; |
| 2488 | if (scanner().HasAnyLineTerminatorBeforeNext() || |
| 2489 | tok == Token::SEMICOLON || |
| 2490 | tok == Token::RBRACE || |
| 2491 | tok == Token::EOS) { |
| 2492 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2493 | result = factory()->NewReturnStatement(GetLiteralUndefined()); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2494 | } else { |
| 2495 | Expression* expr = ParseExpression(true, CHECK_OK); |
| 2496 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2497 | result = factory()->NewReturnStatement(expr); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2500 | // An ECMAScript program is considered syntactically incorrect if it |
| 2501 | // contains a return statement that is not within the body of a |
| 2502 | // function. See ECMA-262, section 12.9, page 67. |
| 2503 | // |
| 2504 | // To be consistent with KJS we report the syntax error at runtime. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2505 | Scope* declaration_scope = top_scope_->DeclarationScope(); |
| 2506 | if (declaration_scope->is_global_scope() || |
| 2507 | declaration_scope->is_eval_scope()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2508 | Handle<String> type = isolate()->factory()->illegal_return_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2509 | Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2510 | return factory()->NewExpressionStatement(throw_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2511 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2512 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2516 | Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) { |
| 2517 | // WithStatement :: |
| 2518 | // 'with' '(' Expression ')' Statement |
| 2519 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2520 | Expect(Token::WITH, CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2521 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2522 | if (!top_scope_->is_classic_mode()) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2523 | ReportMessage("strict_mode_with", Vector<const char*>::empty()); |
| 2524 | *ok = false; |
| 2525 | return NULL; |
| 2526 | } |
| 2527 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2528 | Expect(Token::LPAREN, CHECK_OK); |
| 2529 | Expression* expr = ParseExpression(true, CHECK_OK); |
| 2530 | Expect(Token::RPAREN, CHECK_OK); |
| 2531 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2532 | top_scope_->DeclarationScope()->RecordWithStatement(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2533 | Scope* with_scope = NewScope(top_scope_, WITH_SCOPE); |
| 2534 | Statement* stmt; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2535 | { BlockState block_state(this, with_scope); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2536 | with_scope->set_start_position(scanner().peek_location().beg_pos); |
| 2537 | stmt = ParseStatement(labels, CHECK_OK); |
| 2538 | with_scope->set_end_position(scanner().location().end_pos); |
| 2539 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2540 | return factory()->NewWithStatement(expr, stmt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | |
| 2544 | CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) { |
| 2545 | // CaseClause :: |
| 2546 | // 'case' Expression ':' Statement* |
| 2547 | // 'default' ':' Statement* |
| 2548 | |
| 2549 | Expression* label = NULL; // NULL expression indicates default case |
| 2550 | if (peek() == Token::CASE) { |
| 2551 | Expect(Token::CASE, CHECK_OK); |
| 2552 | label = ParseExpression(true, CHECK_OK); |
| 2553 | } else { |
| 2554 | Expect(Token::DEFAULT, CHECK_OK); |
| 2555 | if (*default_seen_ptr) { |
| 2556 | ReportMessage("multiple_defaults_in_switch", |
| 2557 | Vector<const char*>::empty()); |
| 2558 | *ok = false; |
| 2559 | return NULL; |
| 2560 | } |
| 2561 | *default_seen_ptr = true; |
| 2562 | } |
| 2563 | Expect(Token::COLON, CHECK_OK); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2564 | int pos = scanner().location().beg_pos; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2565 | ZoneList<Statement*>* statements = |
| 2566 | new(zone()) ZoneList<Statement*>(5, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2567 | while (peek() != Token::CASE && |
| 2568 | peek() != Token::DEFAULT && |
| 2569 | peek() != Token::RBRACE) { |
| 2570 | Statement* stat = ParseStatement(NULL, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2571 | statements->Add(stat, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 2574 | return new(zone()) CaseClause(isolate(), label, statements, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | |
| 2578 | SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels, |
| 2579 | bool* ok) { |
| 2580 | // SwitchStatement :: |
| 2581 | // 'switch' '(' Expression ')' '{' CaseClause* '}' |
| 2582 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2583 | SwitchStatement* statement = factory()->NewSwitchStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2584 | Target target(&this->target_stack_, statement); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2585 | |
| 2586 | Expect(Token::SWITCH, CHECK_OK); |
| 2587 | Expect(Token::LPAREN, CHECK_OK); |
| 2588 | Expression* tag = ParseExpression(true, CHECK_OK); |
| 2589 | Expect(Token::RPAREN, CHECK_OK); |
| 2590 | |
| 2591 | bool default_seen = false; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2592 | ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2593 | Expect(Token::LBRACE, CHECK_OK); |
| 2594 | while (peek() != Token::RBRACE) { |
| 2595 | CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2596 | cases->Add(clause, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2597 | } |
| 2598 | Expect(Token::RBRACE, CHECK_OK); |
| 2599 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2600 | if (statement) statement->Initialize(tag, cases); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2601 | return statement; |
| 2602 | } |
| 2603 | |
| 2604 | |
| 2605 | Statement* Parser::ParseThrowStatement(bool* ok) { |
| 2606 | // ThrowStatement :: |
| 2607 | // 'throw' Expression ';' |
| 2608 | |
| 2609 | Expect(Token::THROW, CHECK_OK); |
| 2610 | int pos = scanner().location().beg_pos; |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 2611 | if (scanner().HasAnyLineTerminatorBeforeNext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2612 | ReportMessage("newline_after_throw", Vector<const char*>::empty()); |
| 2613 | *ok = false; |
| 2614 | return NULL; |
| 2615 | } |
| 2616 | Expression* exception = ParseExpression(true, CHECK_OK); |
| 2617 | ExpectSemicolon(CHECK_OK); |
| 2618 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2619 | return factory()->NewExpressionStatement(factory()->NewThrow(exception, pos)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2623 | TryStatement* Parser::ParseTryStatement(bool* ok) { |
| 2624 | // TryStatement :: |
| 2625 | // 'try' Block Catch |
| 2626 | // 'try' Block Finally |
| 2627 | // 'try' Block Catch Finally |
| 2628 | // |
| 2629 | // Catch :: |
| 2630 | // 'catch' '(' Identifier ')' Block |
| 2631 | // |
| 2632 | // Finally :: |
| 2633 | // 'finally' Block |
| 2634 | |
| 2635 | Expect(Token::TRY, CHECK_OK); |
| 2636 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2637 | TargetCollector try_collector(zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2638 | Block* try_block; |
| 2639 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2640 | { Target target(&this->target_stack_, &try_collector); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2641 | try_block = ParseBlock(NULL, CHECK_OK); |
| 2642 | } |
| 2643 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2644 | Token::Value tok = peek(); |
| 2645 | if (tok != Token::CATCH && tok != Token::FINALLY) { |
| 2646 | ReportMessage("no_catch_or_finally", Vector<const char*>::empty()); |
| 2647 | *ok = false; |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | |
| 2651 | // If we can break out from the catch block and there is a finally block, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2652 | // then we will need to collect escaping targets from the catch |
| 2653 | // block. Since we don't know yet if there will be a finally block, we |
| 2654 | // always collect the targets. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2655 | TargetCollector catch_collector(zone()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2656 | Scope* catch_scope = NULL; |
| 2657 | Variable* catch_variable = NULL; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2658 | Block* catch_block = NULL; |
| 2659 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2660 | if (tok == Token::CATCH) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2661 | Consume(Token::CATCH); |
| 2662 | |
| 2663 | Expect(Token::LPAREN, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2664 | catch_scope = NewScope(top_scope_, CATCH_SCOPE); |
| 2665 | catch_scope->set_start_position(scanner().location().beg_pos); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2666 | name = ParseIdentifier(CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2667 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2668 | if (!top_scope_->is_classic_mode() && IsEvalOrArguments(name)) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2669 | ReportMessage("strict_catch_variable", Vector<const char*>::empty()); |
| 2670 | *ok = false; |
| 2671 | return NULL; |
| 2672 | } |
| 2673 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2674 | Expect(Token::RPAREN, CHECK_OK); |
| 2675 | |
| 2676 | if (peek() == Token::LBRACE) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 2677 | Target target(&this->target_stack_, &catch_collector); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2678 | VariableMode mode = is_extended_mode() ? LET : VAR; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2679 | catch_variable = |
| 2680 | catch_scope->DeclareLocal(name, mode, kCreatedInitialized); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2681 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2682 | BlockState block_state(this, catch_scope); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2683 | catch_block = ParseBlock(NULL, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | } else { |
| 2685 | Expect(Token::LBRACE, CHECK_OK); |
| 2686 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2687 | catch_scope->set_end_position(scanner().location().end_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2688 | tok = peek(); |
| 2689 | } |
| 2690 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2691 | Block* finally_block = NULL; |
| 2692 | if (tok == Token::FINALLY || catch_block == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2693 | Consume(Token::FINALLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2694 | finally_block = ParseBlock(NULL, CHECK_OK); |
| 2695 | } |
| 2696 | |
| 2697 | // Simplify the AST nodes by converting: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2698 | // 'try B0 catch B1 finally B2' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2699 | // to: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2700 | // 'try { try B0 catch B1 } finally B2' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2702 | if (catch_block != NULL && finally_block != NULL) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2703 | // If we have both, create an inner try/catch. |
| 2704 | ASSERT(catch_scope != NULL && catch_variable != NULL); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2705 | int index = current_function_state_->NextHandlerIndex(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2706 | TryCatchStatement* statement = factory()->NewTryCatchStatement( |
| 2707 | index, try_block, catch_scope, catch_variable, catch_block); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2708 | statement->set_escaping_targets(try_collector.targets()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 2709 | try_block = factory()->NewBlock(NULL, 1, false); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2710 | try_block->AddStatement(statement, zone()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2711 | catch_block = NULL; // Clear to indicate it's been handled. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | TryStatement* result = NULL; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2715 | if (catch_block != NULL) { |
| 2716 | ASSERT(finally_block == NULL); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2717 | ASSERT(catch_scope != NULL && catch_variable != NULL); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2718 | int index = current_function_state_->NextHandlerIndex(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2719 | result = factory()->NewTryCatchStatement( |
| 2720 | index, try_block, catch_scope, catch_variable, catch_block); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2721 | } else { |
| 2722 | ASSERT(finally_block != NULL); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2723 | int index = current_function_state_->NextHandlerIndex(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2724 | result = factory()->NewTryFinallyStatement(index, try_block, finally_block); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2725 | // Combine the jump targets of the try block and the possible catch block. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2726 | try_collector.targets()->AddAll(*catch_collector.targets(), zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2729 | result->set_escaping_targets(try_collector.targets()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2730 | return result; |
| 2731 | } |
| 2732 | |
| 2733 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2734 | DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels, |
| 2735 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2736 | // DoStatement :: |
| 2737 | // 'do' Statement 'while' '(' Expression ')' ';' |
| 2738 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2739 | DoWhileStatement* loop = factory()->NewDoWhileStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2740 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | |
| 2742 | Expect(Token::DO, CHECK_OK); |
| 2743 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2744 | Expect(Token::WHILE, CHECK_OK); |
| 2745 | Expect(Token::LPAREN, CHECK_OK); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2746 | |
| 2747 | if (loop != NULL) { |
| 2748 | int position = scanner().location().beg_pos; |
| 2749 | loop->set_condition_position(position); |
| 2750 | } |
| 2751 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2752 | Expression* cond = ParseExpression(true, CHECK_OK); |
| 2753 | Expect(Token::RPAREN, CHECK_OK); |
| 2754 | |
| 2755 | // Allow do-statements to be terminated with and without |
| 2756 | // semi-colons. This allows code such as 'do;while(0)return' to |
| 2757 | // parse, which would not be the case if we had used the |
| 2758 | // ExpectSemicolon() functionality here. |
| 2759 | if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON); |
| 2760 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2761 | if (loop != NULL) loop->Initialize(cond, body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2762 | return loop; |
| 2763 | } |
| 2764 | |
| 2765 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2766 | WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | // WhileStatement :: |
| 2768 | // 'while' '(' Expression ')' Statement |
| 2769 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2770 | WhileStatement* loop = factory()->NewWhileStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2771 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2772 | |
| 2773 | Expect(Token::WHILE, CHECK_OK); |
| 2774 | Expect(Token::LPAREN, CHECK_OK); |
| 2775 | Expression* cond = ParseExpression(true, CHECK_OK); |
| 2776 | Expect(Token::RPAREN, CHECK_OK); |
| 2777 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2778 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2779 | if (loop != NULL) loop->Initialize(cond, body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2780 | return loop; |
| 2781 | } |
| 2782 | |
| 2783 | |
| 2784 | Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { |
| 2785 | // ForStatement :: |
| 2786 | // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement |
| 2787 | |
| 2788 | Statement* init = NULL; |
| 2789 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2790 | // Create an in-between scope for let-bound iteration variables. |
| 2791 | Scope* saved_scope = top_scope_; |
| 2792 | Scope* for_scope = NewScope(top_scope_, BLOCK_SCOPE); |
| 2793 | top_scope_ = for_scope; |
| 2794 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2795 | Expect(Token::FOR, CHECK_OK); |
| 2796 | Expect(Token::LPAREN, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2797 | for_scope->set_start_position(scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2798 | if (peek() != Token::SEMICOLON) { |
| 2799 | if (peek() == Token::VAR || peek() == Token::CONST) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2800 | bool is_const = peek() == Token::CONST; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2801 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2802 | Block* variable_statement = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2803 | ParseVariableDeclarations(kForStatement, NULL, NULL, &name, CHECK_OK); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2804 | |
| 2805 | if (peek() == Token::IN && !name.is_null()) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2806 | Interface* interface = |
| 2807 | is_const ? Interface::NewConst() : Interface::NewValue(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2808 | ForInStatement* loop = factory()->NewForInStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2809 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2810 | |
| 2811 | Expect(Token::IN, CHECK_OK); |
| 2812 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2813 | Expect(Token::RPAREN, CHECK_OK); |
| 2814 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2815 | VariableProxy* each = |
| 2816 | top_scope_->NewUnresolved(factory(), name, interface); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2817 | Statement* body = ParseStatement(NULL, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2818 | loop->Initialize(each, enumerable, body); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 2819 | Block* result = factory()->NewBlock(NULL, 2, false); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2820 | result->AddStatement(variable_statement, zone()); |
| 2821 | result->AddStatement(loop, zone()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2822 | top_scope_ = saved_scope; |
| 2823 | for_scope->set_end_position(scanner().location().end_pos); |
| 2824 | for_scope = for_scope->FinalizeBlockScope(); |
| 2825 | ASSERT(for_scope == NULL); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 2826 | // Parsed for-in loop w/ variable/const declaration. |
| 2827 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2828 | } else { |
| 2829 | init = variable_statement; |
| 2830 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2831 | } else if (peek() == Token::LET) { |
| 2832 | Handle<String> name; |
| 2833 | VariableDeclarationProperties decl_props = kHasNoInitializers; |
| 2834 | Block* variable_statement = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 2835 | ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name, |
| 2836 | CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2837 | bool accept_IN = !name.is_null() && decl_props != kHasInitializers; |
| 2838 | if (peek() == Token::IN && accept_IN) { |
| 2839 | // Rewrite a for-in statement of the form |
| 2840 | // |
| 2841 | // for (let x in e) b |
| 2842 | // |
| 2843 | // into |
| 2844 | // |
| 2845 | // <let x' be a temporary variable> |
| 2846 | // for (x' in e) { |
| 2847 | // let x; |
| 2848 | // x = x'; |
| 2849 | // b; |
| 2850 | // } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2851 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2852 | // TODO(keuchel): Move the temporary variable to the block scope, after |
| 2853 | // implementing stack allocated block scoped variables. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2854 | Factory* heap_factory = isolate()->factory(); |
| 2855 | Handle<String> tempstr = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2856 | heap_factory->NewConsString(heap_factory->dot_for_string(), name); |
| 2857 | Handle<String> tempname = heap_factory->InternalizeString(tempstr); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2858 | Variable* temp = top_scope_->DeclarationScope()->NewTemporary(tempname); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2859 | VariableProxy* temp_proxy = factory()->NewVariableProxy(temp); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2860 | ForInStatement* loop = factory()->NewForInStatement(labels); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2861 | Target target(&this->target_stack_, loop); |
| 2862 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2863 | // The expression does not see the loop variable. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2864 | Expect(Token::IN, CHECK_OK); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2865 | top_scope_ = saved_scope; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2866 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2867 | top_scope_ = for_scope; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2868 | Expect(Token::RPAREN, CHECK_OK); |
| 2869 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2870 | VariableProxy* each = |
| 2871 | top_scope_->NewUnresolved(factory(), name, Interface::NewValue()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2872 | Statement* body = ParseStatement(NULL, CHECK_OK); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 2873 | Block* body_block = factory()->NewBlock(NULL, 3, false); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2874 | Assignment* assignment = factory()->NewAssignment( |
| 2875 | Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2876 | Statement* assignment_statement = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2877 | factory()->NewExpressionStatement(assignment); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2878 | body_block->AddStatement(variable_statement, zone()); |
| 2879 | body_block->AddStatement(assignment_statement, zone()); |
| 2880 | body_block->AddStatement(body, zone()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2881 | loop->Initialize(temp_proxy, enumerable, body_block); |
| 2882 | top_scope_ = saved_scope; |
| 2883 | for_scope->set_end_position(scanner().location().end_pos); |
| 2884 | for_scope = for_scope->FinalizeBlockScope(); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 2885 | body_block->set_scope(for_scope); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2886 | // Parsed for-in loop w/ let declaration. |
| 2887 | return loop; |
| 2888 | |
| 2889 | } else { |
| 2890 | init = variable_statement; |
| 2891 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2892 | } else { |
| 2893 | Expression* expression = ParseExpression(false, CHECK_OK); |
| 2894 | if (peek() == Token::IN) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2895 | // Signal a reference error if the expression is an invalid |
| 2896 | // left-hand side expression. We could report this as a syntax |
| 2897 | // error here but for compatibility with JSC we choose to report |
| 2898 | // the error at runtime. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2899 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2900 | Handle<String> type = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 2901 | isolate()->factory()->invalid_lhs_in_for_in_string(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2902 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2903 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2904 | ForInStatement* loop = factory()->NewForInStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2905 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2906 | |
| 2907 | Expect(Token::IN, CHECK_OK); |
| 2908 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2909 | Expect(Token::RPAREN, CHECK_OK); |
| 2910 | |
| 2911 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2912 | if (loop) loop->Initialize(expression, enumerable, body); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2913 | top_scope_ = saved_scope; |
| 2914 | for_scope->set_end_position(scanner().location().end_pos); |
| 2915 | for_scope = for_scope->FinalizeBlockScope(); |
| 2916 | ASSERT(for_scope == NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2917 | // Parsed for-in loop. |
| 2918 | return loop; |
| 2919 | |
| 2920 | } else { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2921 | init = factory()->NewExpressionStatement(expression); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | // Standard 'for' loop |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2927 | ForStatement* loop = factory()->NewForStatement(labels); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 2928 | Target target(&this->target_stack_, loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Parsed initializer at this point. |
| 2931 | Expect(Token::SEMICOLON, CHECK_OK); |
| 2932 | |
| 2933 | Expression* cond = NULL; |
| 2934 | if (peek() != Token::SEMICOLON) { |
| 2935 | cond = ParseExpression(true, CHECK_OK); |
| 2936 | } |
| 2937 | Expect(Token::SEMICOLON, CHECK_OK); |
| 2938 | |
| 2939 | Statement* next = NULL; |
| 2940 | if (peek() != Token::RPAREN) { |
| 2941 | Expression* exp = ParseExpression(true, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2942 | next = factory()->NewExpressionStatement(exp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2943 | } |
| 2944 | Expect(Token::RPAREN, CHECK_OK); |
| 2945 | |
| 2946 | Statement* body = ParseStatement(NULL, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2947 | top_scope_ = saved_scope; |
| 2948 | for_scope->set_end_position(scanner().location().end_pos); |
| 2949 | for_scope = for_scope->FinalizeBlockScope(); |
| 2950 | if (for_scope != NULL) { |
| 2951 | // Rewrite a for statement of the form |
| 2952 | // |
| 2953 | // for (let x = i; c; n) b |
| 2954 | // |
| 2955 | // into |
| 2956 | // |
| 2957 | // { |
| 2958 | // let x = i; |
| 2959 | // for (; c; n) b |
| 2960 | // } |
| 2961 | ASSERT(init != NULL); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 2962 | Block* result = factory()->NewBlock(NULL, 2, false); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 2963 | result->AddStatement(init, zone()); |
| 2964 | result->AddStatement(loop, zone()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 2965 | result->set_scope(for_scope); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2966 | if (loop) loop->Initialize(NULL, cond, next, body); |
| 2967 | return result; |
| 2968 | } else { |
| 2969 | if (loop) loop->Initialize(init, cond, next, body); |
| 2970 | return loop; |
| 2971 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | |
| 2975 | // Precedence = 1 |
| 2976 | Expression* Parser::ParseExpression(bool accept_IN, bool* ok) { |
| 2977 | // Expression :: |
| 2978 | // AssignmentExpression |
| 2979 | // Expression ',' AssignmentExpression |
| 2980 | |
| 2981 | Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 2982 | while (peek() == Token::COMMA) { |
| 2983 | Expect(Token::COMMA, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2984 | int position = scanner().location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2985 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 2986 | result = |
| 2987 | factory()->NewBinaryOperation(Token::COMMA, result, right, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | } |
| 2989 | return result; |
| 2990 | } |
| 2991 | |
| 2992 | |
| 2993 | // Precedence = 2 |
| 2994 | Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) { |
| 2995 | // AssignmentExpression :: |
| 2996 | // ConditionalExpression |
| 2997 | // LeftHandSideExpression AssignmentOperator AssignmentExpression |
| 2998 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 2999 | if (fni_ != NULL) fni_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK); |
| 3001 | |
| 3002 | if (!Token::IsAssignmentOp(peek())) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3003 | if (fni_ != NULL) fni_->Leave(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3004 | // Parsed conditional expression only (no assignment). |
| 3005 | return expression; |
| 3006 | } |
| 3007 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3008 | // Signal a reference error if the expression is an invalid left-hand |
| 3009 | // side expression. We could report this as a syntax error here but |
| 3010 | // for compatibility with JSC we choose to report the error at |
| 3011 | // runtime. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3012 | // TODO(ES5): Should change parsing for spec conformance. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3013 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3014 | Handle<String> type = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 3015 | isolate()->factory()->invalid_lhs_in_assignment_string(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3016 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3019 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3020 | // Assignment to eval or arguments is disallowed in strict mode. |
| 3021 | CheckStrictModeLValue(expression, "strict_lhs_assignment", CHECK_OK); |
| 3022 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3023 | MarkAsLValue(expression); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3024 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3025 | Token::Value op = Next(); // Get assignment operator. |
| 3026 | int pos = scanner().location().beg_pos; |
| 3027 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 3028 | |
| 3029 | // TODO(1231235): We try to estimate the set of properties set by |
| 3030 | // constructors. We define a new property whenever there is an |
| 3031 | // assignment to a property of 'this'. We should probably only add |
| 3032 | // properties if we haven't seen them before. Otherwise we'll |
| 3033 | // probably overestimate the number of properties. |
| 3034 | Property* property = expression ? expression->AsProperty() : NULL; |
| 3035 | if (op == Token::ASSIGN && |
| 3036 | property != NULL && |
| 3037 | property->obj()->AsVariableProxy() != NULL && |
| 3038 | property->obj()->AsVariableProxy()->is_this()) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3039 | current_function_state_->AddProperty(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 3042 | // If we assign a function literal to a property we pretenure the |
| 3043 | // literal so it can be added as a constant function property. |
| 3044 | if (property != NULL && right->AsFunctionLiteral() != NULL) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3045 | right->AsFunctionLiteral()->set_pretenure(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3048 | if (fni_ != NULL) { |
| 3049 | // Check if the right hand side is a call to avoid inferring a |
| 3050 | // name if we're dealing with "a = function(){...}();"-like |
| 3051 | // expression. |
| 3052 | if ((op == Token::INIT_VAR |
| 3053 | || op == Token::INIT_CONST |
| 3054 | || op == Token::ASSIGN) |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3055 | && (right->AsCall() == NULL && right->AsCallNew() == NULL)) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3056 | fni_->Infer(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3057 | } else { |
| 3058 | fni_->RemoveLastFunction(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3059 | } |
| 3060 | fni_->Leave(); |
| 3061 | } |
| 3062 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3063 | return factory()->NewAssignment(op, expression, right, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | // Precedence = 3 |
| 3068 | Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { |
| 3069 | // ConditionalExpression :: |
| 3070 | // LogicalOrExpression |
| 3071 | // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression |
| 3072 | |
| 3073 | // We start using the binary expression parser for prec >= 4 only! |
| 3074 | Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK); |
| 3075 | if (peek() != Token::CONDITIONAL) return expression; |
| 3076 | Consume(Token::CONDITIONAL); |
| 3077 | // In parsing the first assignment expression in conditional |
| 3078 | // expressions we always accept the 'in' keyword; see ECMA-262, |
| 3079 | // section 11.12, page 58. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3080 | int left_position = scanner().peek_location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3081 | Expression* left = ParseAssignmentExpression(true, CHECK_OK); |
| 3082 | Expect(Token::COLON, CHECK_OK); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 3083 | int right_position = scanner().peek_location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3084 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3085 | return factory()->NewConditional( |
| 3086 | expression, left, right, left_position, right_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | |
| 3090 | static int Precedence(Token::Value tok, bool accept_IN) { |
| 3091 | if (tok == Token::IN && !accept_IN) |
| 3092 | return 0; // 0 precedence will terminate binary expression parsing |
| 3093 | |
| 3094 | return Token::Precedence(tok); |
| 3095 | } |
| 3096 | |
| 3097 | |
| 3098 | // Precedence >= 4 |
| 3099 | Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) { |
| 3100 | ASSERT(prec >= 4); |
| 3101 | Expression* x = ParseUnaryExpression(CHECK_OK); |
| 3102 | for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) { |
| 3103 | // prec1 >= 4 |
| 3104 | while (Precedence(peek(), accept_IN) == prec1) { |
| 3105 | Token::Value op = Next(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3106 | int position = scanner().location().beg_pos; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3107 | Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK); |
| 3108 | |
| 3109 | // Compute some expressions involving only number literals. |
| 3110 | if (x && x->AsLiteral() && x->AsLiteral()->handle()->IsNumber() && |
| 3111 | y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) { |
| 3112 | double x_val = x->AsLiteral()->handle()->Number(); |
| 3113 | double y_val = y->AsLiteral()->handle()->Number(); |
| 3114 | |
| 3115 | switch (op) { |
| 3116 | case Token::ADD: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3117 | x = factory()->NewNumberLiteral(x_val + y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3118 | continue; |
| 3119 | case Token::SUB: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3120 | x = factory()->NewNumberLiteral(x_val - y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3121 | continue; |
| 3122 | case Token::MUL: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3123 | x = factory()->NewNumberLiteral(x_val * y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3124 | continue; |
| 3125 | case Token::DIV: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3126 | x = factory()->NewNumberLiteral(x_val / y_val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3127 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3128 | case Token::BIT_OR: { |
| 3129 | int value = DoubleToInt32(x_val) | DoubleToInt32(y_val); |
| 3130 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3131 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3132 | } |
| 3133 | case Token::BIT_AND: { |
| 3134 | int value = DoubleToInt32(x_val) & DoubleToInt32(y_val); |
| 3135 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3136 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3137 | } |
| 3138 | case Token::BIT_XOR: { |
| 3139 | int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val); |
| 3140 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3141 | continue; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3142 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3143 | case Token::SHL: { |
| 3144 | int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3145 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3146 | continue; |
| 3147 | } |
| 3148 | case Token::SHR: { |
| 3149 | uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
| 3150 | uint32_t value = DoubleToUint32(x_val) >> shift; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3151 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3152 | continue; |
| 3153 | } |
| 3154 | case Token::SAR: { |
| 3155 | uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
| 3156 | int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3157 | x = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3158 | continue; |
| 3159 | } |
| 3160 | default: |
| 3161 | break; |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | // For now we distinguish between comparisons and other binary |
| 3166 | // operations. (We could combine the two and get rid of this |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3167 | // code and AST node eventually.) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3168 | if (Token::IsCompareOp(op)) { |
| 3169 | // We have a comparison. |
| 3170 | Token::Value cmp = op; |
| 3171 | switch (op) { |
| 3172 | case Token::NE: cmp = Token::EQ; break; |
| 3173 | case Token::NE_STRICT: cmp = Token::EQ_STRICT; break; |
| 3174 | default: break; |
| 3175 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3176 | x = factory()->NewCompareOperation(cmp, x, y, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3177 | if (cmp != op) { |
| 3178 | // The comparison was negated - add a NOT. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3179 | x = factory()->NewUnaryOperation(Token::NOT, x, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | } else { |
| 3183 | // We have a "normal" binary operation. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3184 | x = factory()->NewBinaryOperation(op, x, y, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | } |
| 3188 | return x; |
| 3189 | } |
| 3190 | |
| 3191 | |
| 3192 | Expression* Parser::ParseUnaryExpression(bool* ok) { |
| 3193 | // UnaryExpression :: |
| 3194 | // PostfixExpression |
| 3195 | // 'delete' UnaryExpression |
| 3196 | // 'void' UnaryExpression |
| 3197 | // 'typeof' UnaryExpression |
| 3198 | // '++' UnaryExpression |
| 3199 | // '--' UnaryExpression |
| 3200 | // '+' UnaryExpression |
| 3201 | // '-' UnaryExpression |
| 3202 | // '~' UnaryExpression |
| 3203 | // '!' UnaryExpression |
| 3204 | |
| 3205 | Token::Value op = peek(); |
| 3206 | if (Token::IsUnaryOp(op)) { |
| 3207 | op = Next(); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 3208 | int position = scanner().location().beg_pos; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3209 | Expression* expression = ParseUnaryExpression(CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3210 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3211 | if (expression != NULL && (expression->AsLiteral() != NULL)) { |
| 3212 | Handle<Object> literal = expression->AsLiteral()->handle(); |
| 3213 | if (op == Token::NOT) { |
| 3214 | // Convert the literal to a boolean condition and negate it. |
| 3215 | bool condition = literal->ToBoolean()->IsTrue(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3216 | Handle<Object> result(isolate()->heap()->ToBoolean(!condition), |
| 3217 | isolate()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3218 | return factory()->NewLiteral(result); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3219 | } else if (literal->IsNumber()) { |
| 3220 | // Compute some expressions involving only number literals. |
| 3221 | double value = literal->Number(); |
| 3222 | switch (op) { |
| 3223 | case Token::ADD: |
| 3224 | return expression; |
| 3225 | case Token::SUB: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3226 | return factory()->NewNumberLiteral(-value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3227 | case Token::BIT_NOT: |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3228 | return factory()->NewNumberLiteral(~DoubleToInt32(value)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3229 | default: |
| 3230 | break; |
| 3231 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3235 | // "delete identifier" is a syntax error in strict mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3236 | if (op == Token::DELETE && !top_scope_->is_classic_mode()) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3237 | VariableProxy* operand = expression->AsVariableProxy(); |
| 3238 | if (operand != NULL && !operand->is_this()) { |
| 3239 | ReportMessage("strict_delete", Vector<const char*>::empty()); |
| 3240 | *ok = false; |
| 3241 | return NULL; |
| 3242 | } |
| 3243 | } |
| 3244 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3245 | return factory()->NewUnaryOperation(op, expression, position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3246 | |
| 3247 | } else if (Token::IsCountOp(op)) { |
| 3248 | op = Next(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3249 | Expression* expression = ParseUnaryExpression(CHECK_OK); |
| 3250 | // Signal a reference error if the expression is an invalid |
| 3251 | // left-hand side expression. We could report this as a syntax |
| 3252 | // error here but for compatibility with JSC we choose to report the |
| 3253 | // error at runtime. |
| 3254 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3255 | Handle<String> type = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 3256 | isolate()->factory()->invalid_lhs_in_prefix_op_string(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3257 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3258 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3259 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3260 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3261 | // Prefix expression operand in strict mode may not be eval or arguments. |
| 3262 | CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK); |
| 3263 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3264 | MarkAsLValue(expression); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3265 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3266 | int position = scanner().location().beg_pos; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3267 | return factory()->NewCountOperation(op, |
| 3268 | true /* prefix */, |
| 3269 | expression, |
| 3270 | position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3271 | |
| 3272 | } else { |
| 3273 | return ParsePostfixExpression(ok); |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | |
| 3278 | Expression* Parser::ParsePostfixExpression(bool* ok) { |
| 3279 | // PostfixExpression :: |
| 3280 | // LeftHandSideExpression ('++' | '--')? |
| 3281 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3282 | Expression* expression = ParseLeftHandSideExpression(CHECK_OK); |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 3283 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3284 | Token::IsCountOp(peek())) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3285 | // Signal a reference error if the expression is an invalid |
| 3286 | // left-hand side expression. We could report this as a syntax |
| 3287 | // error here but for compatibility with JSC we choose to report the |
| 3288 | // error at runtime. |
| 3289 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3290 | Handle<String> type = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 3291 | isolate()->factory()->invalid_lhs_in_postfix_op_string(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3292 | expression = NewThrowReferenceError(type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3293 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3294 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3295 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3296 | // Postfix expression operand in strict mode may not be eval or arguments. |
| 3297 | CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK); |
| 3298 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3299 | MarkAsLValue(expression); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3300 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3301 | Token::Value next = Next(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3302 | int position = scanner().location().beg_pos; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3303 | expression = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3304 | factory()->NewCountOperation(next, |
| 3305 | false /* postfix */, |
| 3306 | expression, |
| 3307 | position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3308 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3309 | return expression; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | |
| 3313 | Expression* Parser::ParseLeftHandSideExpression(bool* ok) { |
| 3314 | // LeftHandSideExpression :: |
| 3315 | // (NewExpression | MemberExpression) ... |
| 3316 | |
| 3317 | Expression* result; |
| 3318 | if (peek() == Token::NEW) { |
| 3319 | result = ParseNewExpression(CHECK_OK); |
| 3320 | } else { |
| 3321 | result = ParseMemberExpression(CHECK_OK); |
| 3322 | } |
| 3323 | |
| 3324 | while (true) { |
| 3325 | switch (peek()) { |
| 3326 | case Token::LBRACK: { |
| 3327 | Consume(Token::LBRACK); |
| 3328 | int pos = scanner().location().beg_pos; |
| 3329 | Expression* index = ParseExpression(true, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3330 | result = factory()->NewProperty(result, index, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3331 | Expect(Token::RBRACK, CHECK_OK); |
| 3332 | break; |
| 3333 | } |
| 3334 | |
| 3335 | case Token::LPAREN: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3336 | int pos; |
| 3337 | if (scanner().current_token() == Token::IDENTIFIER) { |
| 3338 | // For call of an identifier we want to report position of |
| 3339 | // the identifier as position of the call in the stack trace. |
| 3340 | pos = scanner().location().beg_pos; |
| 3341 | } else { |
| 3342 | // For other kinds of calls we record position of the parenthesis as |
| 3343 | // position of the call. Note that this is extremely important for |
| 3344 | // expressions of the form function(){...}() for which call position |
| 3345 | // should not point to the closing brace otherwise it will intersect |
| 3346 | // with positions recorded for function literal and confuse debugger. |
| 3347 | pos = scanner().peek_location().beg_pos; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 3348 | // Also the trailing parenthesis are a hint that the function will |
| 3349 | // be called immediately. If we happen to have parsed a preceding |
| 3350 | // function literal eagerly, we can also compile it eagerly. |
| 3351 | if (result->IsFunctionLiteral() && mode() == PARSE_EAGERLY) { |
| 3352 | result->AsFunctionLiteral()->set_parenthesized(); |
| 3353 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3354 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3355 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
| 3356 | |
| 3357 | // Keep track of eval() calls since they disable all local variable |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3358 | // optimizations. |
| 3359 | // The calls that need special treatment are the |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3360 | // direct eval calls. These calls are all of the form eval(...), with |
| 3361 | // no explicit receiver. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3362 | // These calls are marked as potentially direct eval calls. Whether |
| 3363 | // they are actually direct calls to eval is determined at run time. |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3364 | VariableProxy* callee = result->AsVariableProxy(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3365 | if (callee != NULL && |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 3366 | callee->IsVariable(isolate()->factory()->eval_string())) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3367 | top_scope_->DeclarationScope()->RecordEvalCall(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3368 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3369 | result = factory()->NewCall(result, args, pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3370 | break; |
| 3371 | } |
| 3372 | |
| 3373 | case Token::PERIOD: { |
| 3374 | Consume(Token::PERIOD); |
| 3375 | int pos = scanner().location().beg_pos; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 3376 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3377 | result = |
| 3378 | factory()->NewProperty(result, factory()->NewLiteral(name), pos); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3379 | if (fni_ != NULL) fni_->PushLiteralName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3380 | break; |
| 3381 | } |
| 3382 | |
| 3383 | default: |
| 3384 | return result; |
| 3385 | } |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3390 | Expression* Parser::ParseNewPrefix(PositionStack* stack, bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3391 | // NewExpression :: |
| 3392 | // ('new')+ MemberExpression |
| 3393 | |
| 3394 | // The grammar for new expressions is pretty warped. The keyword |
| 3395 | // 'new' can either be a part of the new expression (where it isn't |
| 3396 | // followed by an argument list) or a part of the member expression, |
| 3397 | // where it must be followed by an argument list. To accommodate |
| 3398 | // this, we parse the 'new' keywords greedily and keep track of how |
| 3399 | // many we have parsed. This information is then passed on to the |
| 3400 | // member expression parser, which is only allowed to match argument |
| 3401 | // lists as long as it has 'new' prefixes left |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3402 | Expect(Token::NEW, CHECK_OK); |
| 3403 | PositionStack::Element pos(stack, scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3404 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3405 | Expression* result; |
| 3406 | if (peek() == Token::NEW) { |
| 3407 | result = ParseNewPrefix(stack, CHECK_OK); |
| 3408 | } else { |
| 3409 | result = ParseMemberWithNewPrefixesExpression(stack, CHECK_OK); |
| 3410 | } |
| 3411 | |
| 3412 | if (!stack->is_empty()) { |
| 3413 | int last = stack->pop(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3414 | result = factory()->NewCallNew( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3415 | result, new(zone()) ZoneList<Expression*>(0, zone()), last); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3416 | } |
| 3417 | return result; |
| 3418 | } |
| 3419 | |
| 3420 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3421 | Expression* Parser::ParseNewExpression(bool* ok) { |
| 3422 | PositionStack stack(ok); |
| 3423 | return ParseNewPrefix(&stack, ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3427 | Expression* Parser::ParseMemberExpression(bool* ok) { |
| 3428 | return ParseMemberWithNewPrefixesExpression(NULL, ok); |
| 3429 | } |
| 3430 | |
| 3431 | |
| 3432 | Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack, |
| 3433 | bool* ok) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3434 | // MemberExpression :: |
| 3435 | // (PrimaryExpression | FunctionLiteral) |
| 3436 | // ('[' Expression ']' | '.' Identifier | Arguments)* |
| 3437 | |
| 3438 | // Parse the initial primary or function expression. |
| 3439 | Expression* result = NULL; |
| 3440 | if (peek() == Token::FUNCTION) { |
| 3441 | Expect(Token::FUNCTION, CHECK_OK); |
| 3442 | int function_token_position = scanner().location().beg_pos; |
| 3443 | Handle<String> name; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3444 | bool is_strict_reserved_name = false; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3445 | if (peek_any_identifier()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3446 | name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, |
| 3447 | CHECK_OK); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3448 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 3449 | FunctionLiteral::Type type = name.is_null() |
| 3450 | ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 3451 | : FunctionLiteral::NAMED_EXPRESSION; |
| 3452 | result = ParseFunctionLiteral(name, |
| 3453 | is_strict_reserved_name, |
| 3454 | function_token_position, |
| 3455 | type, |
| 3456 | CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3457 | } else { |
| 3458 | result = ParsePrimaryExpression(CHECK_OK); |
| 3459 | } |
| 3460 | |
| 3461 | while (true) { |
| 3462 | switch (peek()) { |
| 3463 | case Token::LBRACK: { |
| 3464 | Consume(Token::LBRACK); |
| 3465 | int pos = scanner().location().beg_pos; |
| 3466 | Expression* index = ParseExpression(true, CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3467 | result = factory()->NewProperty(result, index, pos); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3468 | if (fni_ != NULL) { |
| 3469 | if (index->IsPropertyName()) { |
| 3470 | fni_->PushLiteralName(index->AsLiteral()->AsPropertyName()); |
| 3471 | } else { |
| 3472 | fni_->PushLiteralName( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 3473 | isolate()->factory()->anonymous_function_string()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3476 | Expect(Token::RBRACK, CHECK_OK); |
| 3477 | break; |
| 3478 | } |
| 3479 | case Token::PERIOD: { |
| 3480 | Consume(Token::PERIOD); |
| 3481 | int pos = scanner().location().beg_pos; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 3482 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3483 | result = |
| 3484 | factory()->NewProperty(result, factory()->NewLiteral(name), pos); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3485 | if (fni_ != NULL) fni_->PushLiteralName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3486 | break; |
| 3487 | } |
| 3488 | case Token::LPAREN: { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3489 | if ((stack == NULL) || stack->is_empty()) return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3490 | // Consume one of the new prefixes (already parsed). |
| 3491 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3492 | int last = stack->pop(); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3493 | result = factory()->NewCallNew(result, args, last); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3494 | break; |
| 3495 | } |
| 3496 | default: |
| 3497 | return result; |
| 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | |
| 3503 | DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) { |
| 3504 | // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser |
| 3505 | // contexts this is used as a statement which invokes the debugger as i a |
| 3506 | // break point is present. |
| 3507 | // DebuggerStatement :: |
| 3508 | // 'debugger' ';' |
| 3509 | |
| 3510 | Expect(Token::DEBUGGER, CHECK_OK); |
| 3511 | ExpectSemicolon(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3512 | return factory()->NewDebuggerStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | |
| 3516 | void Parser::ReportUnexpectedToken(Token::Value token) { |
| 3517 | // We don't report stack overflows here, to avoid increasing the |
| 3518 | // stack depth even further. Instead we report it after parsing is |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3519 | // over, in ParseProgram/ParseJson. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3520 | if (token == Token::ILLEGAL && stack_overflow_) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3521 | // Four of the tokens are treated specially |
| 3522 | switch (token) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3523 | case Token::EOS: |
| 3524 | return ReportMessage("unexpected_eos", Vector<const char*>::empty()); |
| 3525 | case Token::NUMBER: |
| 3526 | return ReportMessage("unexpected_token_number", |
| 3527 | Vector<const char*>::empty()); |
| 3528 | case Token::STRING: |
| 3529 | return ReportMessage("unexpected_token_string", |
| 3530 | Vector<const char*>::empty()); |
| 3531 | case Token::IDENTIFIER: |
| 3532 | return ReportMessage("unexpected_token_identifier", |
| 3533 | Vector<const char*>::empty()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3534 | case Token::FUTURE_RESERVED_WORD: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3535 | return ReportMessage("unexpected_reserved", |
| 3536 | Vector<const char*>::empty()); |
| 3537 | case Token::FUTURE_STRICT_RESERVED_WORD: |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3538 | return ReportMessage(top_scope_->is_classic_mode() ? |
| 3539 | "unexpected_token_identifier" : |
| 3540 | "unexpected_strict_reserved", |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3541 | Vector<const char*>::empty()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3542 | default: |
| 3543 | const char* name = Token::String(token); |
| 3544 | ASSERT(name != NULL); |
| 3545 | ReportMessage("unexpected_token", Vector<const char*>(&name, 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3550 | void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 3551 | SmartArrayPointer<char> name_string = name->ToCString(DISALLOW_NULLS); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3552 | const char* element[1] = { *name_string }; |
| 3553 | ReportMessage("invalid_preparser_data", |
| 3554 | Vector<const char*>(element, 1)); |
| 3555 | *ok = false; |
| 3556 | } |
| 3557 | |
| 3558 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3559 | Expression* Parser::ParsePrimaryExpression(bool* ok) { |
| 3560 | // PrimaryExpression :: |
| 3561 | // 'this' |
| 3562 | // 'null' |
| 3563 | // 'true' |
| 3564 | // 'false' |
| 3565 | // Identifier |
| 3566 | // Number |
| 3567 | // String |
| 3568 | // ArrayLiteral |
| 3569 | // ObjectLiteral |
| 3570 | // RegExpLiteral |
| 3571 | // '(' Expression ')' |
| 3572 | |
| 3573 | Expression* result = NULL; |
| 3574 | switch (peek()) { |
| 3575 | case Token::THIS: { |
| 3576 | Consume(Token::THIS); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3577 | result = factory()->NewVariableProxy(top_scope_->receiver()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3578 | break; |
| 3579 | } |
| 3580 | |
| 3581 | case Token::NULL_LITERAL: |
| 3582 | Consume(Token::NULL_LITERAL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3583 | result = factory()->NewLiteral(isolate()->factory()->null_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3584 | break; |
| 3585 | |
| 3586 | case Token::TRUE_LITERAL: |
| 3587 | Consume(Token::TRUE_LITERAL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3588 | result = factory()->NewLiteral(isolate()->factory()->true_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3589 | break; |
| 3590 | |
| 3591 | case Token::FALSE_LITERAL: |
| 3592 | Consume(Token::FALSE_LITERAL); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3593 | result = factory()->NewLiteral(isolate()->factory()->false_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | break; |
| 3595 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3596 | case Token::IDENTIFIER: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3597 | case Token::FUTURE_STRICT_RESERVED_WORD: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3598 | Handle<String> name = ParseIdentifier(CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3599 | if (fni_ != NULL) fni_->PushVariableName(name); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3600 | // The name may refer to a module instance object, so its type is unknown. |
| 3601 | #ifdef DEBUG |
| 3602 | if (FLAG_print_interface_details) |
| 3603 | PrintF("# Variable %s ", name->ToAsciiArray()); |
| 3604 | #endif |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3605 | Interface* interface = Interface::NewUnknown(zone()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3606 | result = top_scope_->NewUnresolved( |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 3607 | factory(), name, interface, scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3608 | break; |
| 3609 | } |
| 3610 | |
| 3611 | case Token::NUMBER: { |
| 3612 | Consume(Token::NUMBER); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 3613 | ASSERT(scanner().is_literal_ascii()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 3614 | double value = StringToDouble(isolate()->unicode_cache(), |
| 3615 | scanner().literal_ascii_string(), |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 3616 | ALLOW_HEX | ALLOW_OCTALS); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3617 | result = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3618 | break; |
| 3619 | } |
| 3620 | |
| 3621 | case Token::STRING: { |
| 3622 | Consume(Token::STRING); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 3623 | Handle<String> symbol = GetSymbol(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3624 | result = factory()->NewLiteral(symbol); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 3625 | if (fni_ != NULL) fni_->PushLiteralName(symbol); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3626 | break; |
| 3627 | } |
| 3628 | |
| 3629 | case Token::ASSIGN_DIV: |
| 3630 | result = ParseRegExpLiteral(true, CHECK_OK); |
| 3631 | break; |
| 3632 | |
| 3633 | case Token::DIV: |
| 3634 | result = ParseRegExpLiteral(false, CHECK_OK); |
| 3635 | break; |
| 3636 | |
| 3637 | case Token::LBRACK: |
| 3638 | result = ParseArrayLiteral(CHECK_OK); |
| 3639 | break; |
| 3640 | |
| 3641 | case Token::LBRACE: |
| 3642 | result = ParseObjectLiteral(CHECK_OK); |
| 3643 | break; |
| 3644 | |
| 3645 | case Token::LPAREN: |
| 3646 | Consume(Token::LPAREN); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3647 | // Heuristically try to detect immediately called functions before |
| 3648 | // seeing the call parentheses. |
| 3649 | parenthesized_function_ = (peek() == Token::FUNCTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3650 | result = ParseExpression(true, CHECK_OK); |
| 3651 | Expect(Token::RPAREN, CHECK_OK); |
| 3652 | break; |
| 3653 | |
| 3654 | case Token::MOD: |
| 3655 | if (allow_natives_syntax_ || extension_ != NULL) { |
| 3656 | result = ParseV8Intrinsic(CHECK_OK); |
| 3657 | break; |
| 3658 | } |
| 3659 | // If we're not allowing special syntax we fall-through to the |
| 3660 | // default case. |
| 3661 | |
| 3662 | default: { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3663 | Token::Value tok = Next(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3664 | ReportUnexpectedToken(tok); |
| 3665 | *ok = false; |
| 3666 | return NULL; |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | return result; |
| 3671 | } |
| 3672 | |
| 3673 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3674 | void Parser::BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* values, |
| 3675 | Handle<FixedArray> literals, |
| 3676 | bool* is_simple, |
| 3677 | int* depth) { |
| 3678 | // Fill in the literals. |
| 3679 | // Accumulate output values in local variables. |
| 3680 | bool is_simple_acc = true; |
| 3681 | int depth_acc = 1; |
| 3682 | for (int i = 0; i < values->length(); i++) { |
| 3683 | MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral(); |
| 3684 | if (m_literal != NULL && m_literal->depth() >= depth_acc) { |
| 3685 | depth_acc = m_literal->depth() + 1; |
| 3686 | } |
| 3687 | Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i)); |
| 3688 | if (boilerplate_value->IsUndefined()) { |
| 3689 | literals->set_the_hole(i); |
| 3690 | is_simple_acc = false; |
| 3691 | } else { |
| 3692 | literals->set(i, *boilerplate_value); |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | *is_simple = is_simple_acc; |
| 3697 | *depth = depth_acc; |
| 3698 | } |
| 3699 | |
| 3700 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3701 | Expression* Parser::ParseArrayLiteral(bool* ok) { |
| 3702 | // ArrayLiteral :: |
| 3703 | // '[' Expression? (',' Expression?)* ']' |
| 3704 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3705 | ZoneList<Expression*>* values = new(zone()) ZoneList<Expression*>(4, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3706 | Expect(Token::LBRACK, CHECK_OK); |
| 3707 | while (peek() != Token::RBRACK) { |
| 3708 | Expression* elem; |
| 3709 | if (peek() == Token::COMMA) { |
| 3710 | elem = GetLiteralTheHole(); |
| 3711 | } else { |
| 3712 | elem = ParseAssignmentExpression(true, CHECK_OK); |
| 3713 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3714 | values->Add(elem, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3715 | if (peek() != Token::RBRACK) { |
| 3716 | Expect(Token::COMMA, CHECK_OK); |
| 3717 | } |
| 3718 | } |
| 3719 | Expect(Token::RBRACK, CHECK_OK); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3720 | |
| 3721 | // Update the scope information before the pre-parsing bailout. |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3722 | int literal_index = current_function_state_->NextMaterializedLiteralIndex(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3723 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3724 | // Allocate a fixed array to hold all the object literals. |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3725 | Handle<JSArray> array = |
| 3726 | isolate()->factory()->NewJSArray(0, FAST_HOLEY_SMI_ELEMENTS); |
| 3727 | isolate()->factory()->SetElementsCapacityAndLength( |
| 3728 | array, values->length(), values->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3729 | |
| 3730 | // Fill in the literals. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3731 | Heap* heap = isolate()->heap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3732 | bool is_simple = true; |
| 3733 | int depth = 1; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3734 | bool is_holey = false; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3735 | for (int i = 0, n = values->length(); i < n; i++) { |
| 3736 | MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3737 | if (m_literal != NULL && m_literal->depth() + 1 > depth) { |
| 3738 | depth = m_literal->depth() + 1; |
| 3739 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 3740 | Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3741 | if (boilerplate_value->IsTheHole()) { |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3742 | is_holey = true; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3743 | } else if (boilerplate_value->IsUndefined()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3744 | is_simple = false; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3745 | JSObject::SetOwnElement( |
| 3746 | array, i, handle(Smi::FromInt(0), isolate()), kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3747 | } else { |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3748 | JSObject::SetOwnElement(array, i, boilerplate_value, kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3752 | Handle<FixedArrayBase> element_values(array->elements()); |
| 3753 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 3754 | // Simple and shallow arrays can be lazily copied, we transform the |
| 3755 | // elements array to a copy-on-write array. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3756 | if (is_simple && depth == 1 && values->length() > 0 && |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3757 | array->HasFastSmiOrObjectElements()) { |
| 3758 | element_values->set_map(heap->fixed_cow_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3761 | // Remember both the literal's constant values as well as the ElementsKind |
| 3762 | // in a 2-element FixedArray. |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3763 | Handle<FixedArray> literals = isolate()->factory()->NewFixedArray(2, TENURED); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3764 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3765 | ElementsKind kind = array->GetElementsKind(); |
| 3766 | kind = is_holey ? GetHoleyElementsKind(kind) : GetPackedElementsKind(kind); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3767 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 3768 | literals->set(0, Smi::FromInt(kind)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3769 | literals->set(1, *element_values); |
| 3770 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 3771 | return factory()->NewArrayLiteral( |
| 3772 | literals, values, literal_index, is_simple, depth); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3776 | bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { |
| 3777 | return property != NULL && |
| 3778 | property->kind() != ObjectLiteral::Property::PROTOTYPE; |
| 3779 | } |
| 3780 | |
| 3781 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3782 | bool CompileTimeValue::IsCompileTimeValue(Expression* expression) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3783 | if (expression->AsLiteral() != NULL) return true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3784 | MaterializedLiteral* lit = expression->AsMaterializedLiteral(); |
| 3785 | return lit != NULL && lit->is_simple(); |
| 3786 | } |
| 3787 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 3788 | |
| 3789 | bool CompileTimeValue::ArrayLiteralElementNeedsInitialization( |
| 3790 | Expression* value) { |
| 3791 | // If value is a literal the property value is already set in the |
| 3792 | // boilerplate object. |
| 3793 | if (value->AsLiteral() != NULL) return false; |
| 3794 | // If value is a materialized literal the property value is already set |
| 3795 | // in the boilerplate object if it is simple. |
| 3796 | if (CompileTimeValue::IsCompileTimeValue(value)) return false; |
| 3797 | return true; |
| 3798 | } |
| 3799 | |
| 3800 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3801 | Handle<FixedArray> CompileTimeValue::GetValue(Expression* expression) { |
| 3802 | ASSERT(IsCompileTimeValue(expression)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3803 | Handle<FixedArray> result = FACTORY->NewFixedArray(2, TENURED); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3804 | ObjectLiteral* object_literal = expression->AsObjectLiteral(); |
| 3805 | if (object_literal != NULL) { |
| 3806 | ASSERT(object_literal->is_simple()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3807 | if (object_literal->fast_elements()) { |
| 3808 | result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS)); |
| 3809 | } else { |
| 3810 | result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS)); |
| 3811 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3812 | result->set(kElementsSlot, *object_literal->constant_properties()); |
| 3813 | } else { |
| 3814 | ArrayLiteral* array_literal = expression->AsArrayLiteral(); |
| 3815 | ASSERT(array_literal != NULL && array_literal->is_simple()); |
| 3816 | result->set(kTypeSlot, Smi::FromInt(ARRAY_LITERAL)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3817 | result->set(kElementsSlot, *array_literal->constant_elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3818 | } |
| 3819 | return result; |
| 3820 | } |
| 3821 | |
| 3822 | |
| 3823 | CompileTimeValue::Type CompileTimeValue::GetType(Handle<FixedArray> value) { |
| 3824 | Smi* type_value = Smi::cast(value->get(kTypeSlot)); |
| 3825 | return static_cast<Type>(type_value->value()); |
| 3826 | } |
| 3827 | |
| 3828 | |
| 3829 | Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
| 3830 | return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
| 3831 | } |
| 3832 | |
| 3833 | |
| 3834 | Handle<Object> Parser::GetBoilerplateValue(Expression* expression) { |
| 3835 | if (expression->AsLiteral() != NULL) { |
| 3836 | return expression->AsLiteral()->handle(); |
| 3837 | } |
| 3838 | if (CompileTimeValue::IsCompileTimeValue(expression)) { |
| 3839 | return CompileTimeValue::GetValue(expression); |
| 3840 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3841 | return isolate()->factory()->undefined_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3844 | // Validation per 11.1.5 Object Initialiser |
| 3845 | class ObjectLiteralPropertyChecker { |
| 3846 | public: |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3847 | ObjectLiteralPropertyChecker(Parser* parser, LanguageMode language_mode) : |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3848 | props_(Literal::Match), |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3849 | parser_(parser), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3850 | language_mode_(language_mode) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | void CheckProperty( |
| 3854 | ObjectLiteral::Property* property, |
| 3855 | Scanner::Location loc, |
| 3856 | bool* ok); |
| 3857 | |
| 3858 | private: |
| 3859 | enum PropertyKind { |
| 3860 | kGetAccessor = 0x01, |
| 3861 | kSetAccessor = 0x02, |
| 3862 | kAccessor = kGetAccessor | kSetAccessor, |
| 3863 | kData = 0x04 |
| 3864 | }; |
| 3865 | |
| 3866 | static intptr_t GetPropertyKind(ObjectLiteral::Property* property) { |
| 3867 | switch (property->kind()) { |
| 3868 | case ObjectLiteral::Property::GETTER: |
| 3869 | return kGetAccessor; |
| 3870 | case ObjectLiteral::Property::SETTER: |
| 3871 | return kSetAccessor; |
| 3872 | default: |
| 3873 | return kData; |
| 3874 | } |
| 3875 | } |
| 3876 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3877 | HashMap props_; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3878 | Parser* parser_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3879 | LanguageMode language_mode_; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3880 | }; |
| 3881 | |
| 3882 | |
| 3883 | void ObjectLiteralPropertyChecker::CheckProperty( |
| 3884 | ObjectLiteral::Property* property, |
| 3885 | Scanner::Location loc, |
| 3886 | bool* ok) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3887 | ASSERT(property != NULL); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 3888 | Literal* literal = property->key(); |
| 3889 | HashMap::Entry* entry = props_.Lookup(literal, literal->Hash(), true); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3890 | intptr_t prev = reinterpret_cast<intptr_t> (entry->value); |
| 3891 | intptr_t curr = GetPropertyKind(property); |
| 3892 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3893 | // Duplicate data properties are illegal in strict or extended mode. |
| 3894 | if (language_mode_ != CLASSIC_MODE && (curr & prev & kData) != 0) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 3895 | parser_->ReportMessageAt(loc, "strict_duplicate_property", |
| 3896 | Vector<const char*>::empty()); |
| 3897 | *ok = false; |
| 3898 | return; |
| 3899 | } |
| 3900 | // Data property conflicting with an accessor. |
| 3901 | if (((curr & kData) && (prev & kAccessor)) || |
| 3902 | ((prev & kData) && (curr & kAccessor))) { |
| 3903 | parser_->ReportMessageAt(loc, "accessor_data_property", |
| 3904 | Vector<const char*>::empty()); |
| 3905 | *ok = false; |
| 3906 | return; |
| 3907 | } |
| 3908 | // Two accessors of the same type conflicting |
| 3909 | if ((curr & prev & kAccessor) != 0) { |
| 3910 | parser_->ReportMessageAt(loc, "accessor_get_set", |
| 3911 | Vector<const char*>::empty()); |
| 3912 | *ok = false; |
| 3913 | return; |
| 3914 | } |
| 3915 | |
| 3916 | // Update map |
| 3917 | entry->value = reinterpret_cast<void*> (prev | curr); |
| 3918 | *ok = true; |
| 3919 | } |
| 3920 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3921 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3922 | void Parser::BuildObjectLiteralConstantProperties( |
| 3923 | ZoneList<ObjectLiteral::Property*>* properties, |
| 3924 | Handle<FixedArray> constant_properties, |
| 3925 | bool* is_simple, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3926 | bool* fast_elements, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3927 | int* depth) { |
| 3928 | int position = 0; |
| 3929 | // Accumulate the value in local variables and store it at the end. |
| 3930 | bool is_simple_acc = true; |
| 3931 | int depth_acc = 1; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3932 | uint32_t max_element_index = 0; |
| 3933 | uint32_t elements = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3934 | for (int i = 0; i < properties->length(); i++) { |
| 3935 | ObjectLiteral::Property* property = properties->at(i); |
| 3936 | if (!IsBoilerplateProperty(property)) { |
| 3937 | is_simple_acc = false; |
| 3938 | continue; |
| 3939 | } |
| 3940 | MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral(); |
| 3941 | if (m_literal != NULL && m_literal->depth() >= depth_acc) { |
| 3942 | depth_acc = m_literal->depth() + 1; |
| 3943 | } |
| 3944 | |
| 3945 | // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined |
| 3946 | // value for COMPUTED properties, the real value is filled in at |
| 3947 | // runtime. The enumeration order is maintained. |
| 3948 | Handle<Object> key = property->key()->handle(); |
| 3949 | Handle<Object> value = GetBoilerplateValue(property->value()); |
| 3950 | is_simple_acc = is_simple_acc && !value->IsUndefined(); |
| 3951 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3952 | // Keep track of the number of elements in the object literal and |
| 3953 | // the largest element index. If the largest element index is |
| 3954 | // much larger than the number of elements, creating an object |
| 3955 | // literal with fast elements will be a waste of space. |
| 3956 | uint32_t element_index = 0; |
| 3957 | if (key->IsString() |
| 3958 | && Handle<String>::cast(key)->AsArrayIndex(&element_index) |
| 3959 | && element_index > max_element_index) { |
| 3960 | max_element_index = element_index; |
| 3961 | elements++; |
| 3962 | } else if (key->IsSmi()) { |
| 3963 | int key_value = Smi::cast(*key)->value(); |
| 3964 | if (key_value > 0 |
| 3965 | && static_cast<uint32_t>(key_value) > max_element_index) { |
| 3966 | max_element_index = key_value; |
| 3967 | } |
| 3968 | elements++; |
| 3969 | } |
| 3970 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3971 | // Add name, value pair to the fixed array. |
| 3972 | constant_properties->set(position++, *key); |
| 3973 | constant_properties->set(position++, *value); |
| 3974 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3975 | *fast_elements = |
| 3976 | (max_element_index <= 32) || ((2 * elements) >= max_element_index); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3977 | *is_simple = is_simple_acc; |
| 3978 | *depth = depth_acc; |
| 3979 | } |
| 3980 | |
| 3981 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3982 | ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter, |
| 3983 | bool* ok) { |
| 3984 | // Special handling of getter and setter syntax: |
| 3985 | // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... } |
| 3986 | // We have already read the "get" or "set" keyword. |
| 3987 | Token::Value next = Next(); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 3988 | bool is_keyword = Token::IsKeyword(next); |
| 3989 | if (next == Token::IDENTIFIER || next == Token::NUMBER || |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3990 | next == Token::FUTURE_RESERVED_WORD || |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3991 | next == Token::FUTURE_STRICT_RESERVED_WORD || |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 3992 | next == Token::STRING || is_keyword) { |
| 3993 | Handle<String> name; |
| 3994 | if (is_keyword) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 3995 | name = isolate_->factory()->InternalizeUtf8String(Token::String(next)); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 3996 | } else { |
| 3997 | name = GetSymbol(CHECK_OK); |
| 3998 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3999 | FunctionLiteral* value = |
| 4000 | ParseFunctionLiteral(name, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4001 | false, // reserved words are allowed here |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4002 | RelocInfo::kNoPosition, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4003 | FunctionLiteral::ANONYMOUS_EXPRESSION, |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4004 | CHECK_OK); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4005 | // Allow any number of parameters for compatibilty with JSC. |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 4006 | // Specification only allows zero parameters for get and one for set. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4007 | return factory()->NewObjectLiteralProperty(is_getter, value); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4008 | } else { |
| 4009 | ReportUnexpectedToken(next); |
| 4010 | *ok = false; |
| 4011 | return NULL; |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4016 | Expression* Parser::ParseObjectLiteral(bool* ok) { |
| 4017 | // ObjectLiteral :: |
| 4018 | // '{' ( |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4019 | // ((IdentifierName | String | Number) ':' AssignmentExpression) |
| 4020 | // | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | // )*[','] '}' |
| 4022 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4023 | ZoneList<ObjectLiteral::Property*>* properties = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4024 | new(zone()) ZoneList<ObjectLiteral::Property*>(4, zone()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4025 | int number_of_boilerplate_properties = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4026 | bool has_function = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4027 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4028 | ObjectLiteralPropertyChecker checker(this, top_scope_->language_mode()); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4029 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4030 | Expect(Token::LBRACE, CHECK_OK); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4031 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4032 | while (peek() != Token::RBRACE) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4033 | if (fni_ != NULL) fni_->Enter(); |
| 4034 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4035 | Literal* key = NULL; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4036 | Token::Value next = peek(); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4037 | |
| 4038 | // Location of the property name token |
| 4039 | Scanner::Location loc = scanner().peek_location(); |
| 4040 | |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4041 | switch (next) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4042 | case Token::FUTURE_RESERVED_WORD: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4043 | case Token::FUTURE_STRICT_RESERVED_WORD: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4044 | case Token::IDENTIFIER: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4045 | bool is_getter = false; |
| 4046 | bool is_setter = false; |
| 4047 | Handle<String> id = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4048 | ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4049 | if (fni_ != NULL) fni_->PushLiteralName(id); |
| 4050 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4051 | if ((is_getter || is_setter) && peek() != Token::COLON) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4052 | // Update loc to point to the identifier |
| 4053 | loc = scanner().peek_location(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | ObjectLiteral::Property* property = |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4055 | ParseObjectLiteralGetSet(is_getter, CHECK_OK); |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4056 | if (IsBoilerplateProperty(property)) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4057 | number_of_boilerplate_properties++; |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4058 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4059 | // Validate the property. |
| 4060 | checker.CheckProperty(property, loc, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4061 | properties->Add(property, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4062 | if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4063 | |
| 4064 | if (fni_ != NULL) { |
| 4065 | fni_->Infer(); |
| 4066 | fni_->Leave(); |
| 4067 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4068 | continue; // restart the while |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4069 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4070 | // Failed to parse as get/set property, so it's just a property |
| 4071 | // called "get" or "set". |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4072 | key = factory()->NewLiteral(id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4073 | break; |
| 4074 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4075 | case Token::STRING: { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4076 | Consume(Token::STRING); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4077 | Handle<String> string = GetSymbol(CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4078 | if (fni_ != NULL) fni_->PushLiteralName(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4079 | uint32_t index; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4080 | if (!string.is_null() && string->AsArrayIndex(&index)) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4081 | key = factory()->NewNumberLiteral(index); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4082 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4083 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4084 | key = factory()->NewLiteral(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4085 | break; |
| 4086 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4087 | case Token::NUMBER: { |
| 4088 | Consume(Token::NUMBER); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4089 | ASSERT(scanner().is_literal_ascii()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 4090 | double value = StringToDouble(isolate()->unicode_cache(), |
| 4091 | scanner().literal_ascii_string(), |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4092 | ALLOW_HEX | ALLOW_OCTALS); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4093 | key = factory()->NewNumberLiteral(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4094 | break; |
| 4095 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4096 | default: |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4097 | if (Token::IsKeyword(next)) { |
| 4098 | Consume(next); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4099 | Handle<String> string = GetSymbol(CHECK_OK); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4100 | key = factory()->NewLiteral(string); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4101 | } else { |
| 4102 | // Unexpected token. |
| 4103 | Token::Value next = Next(); |
| 4104 | ReportUnexpectedToken(next); |
| 4105 | *ok = false; |
| 4106 | return NULL; |
| 4107 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | Expect(Token::COLON, CHECK_OK); |
| 4111 | Expression* value = ParseAssignmentExpression(true, CHECK_OK); |
| 4112 | |
| 4113 | ObjectLiteral::Property* property = |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 4114 | new(zone()) ObjectLiteral::Property(key, value, isolate()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4115 | |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 4116 | // Mark top-level object literals that contain function literals and |
| 4117 | // pretenure the literal so it can be added as a constant function |
| 4118 | // property. |
| 4119 | if (top_scope_->DeclarationScope()->is_global_scope() && |
| 4120 | value->AsFunctionLiteral() != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4121 | has_function = true; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4122 | value->AsFunctionLiteral()->set_pretenure(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4125 | // Count CONSTANT or COMPUTED properties to maintain the enumeration order. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 4126 | if (IsBoilerplateProperty(property)) number_of_boilerplate_properties++; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4127 | // Validate the property |
| 4128 | checker.CheckProperty(property, loc, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4129 | properties->Add(property, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4130 | |
| 4131 | // TODO(1240767): Consider allowing trailing comma. |
| 4132 | if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4133 | |
| 4134 | if (fni_ != NULL) { |
| 4135 | fni_->Infer(); |
| 4136 | fni_->Leave(); |
| 4137 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4138 | } |
| 4139 | Expect(Token::RBRACE, CHECK_OK); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4140 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4141 | // Computation of literal_index must happen before pre parse bailout. |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4142 | int literal_index = current_function_state_->NextMaterializedLiteralIndex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4143 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4144 | Handle<FixedArray> constant_properties = isolate()->factory()->NewFixedArray( |
| 4145 | number_of_boilerplate_properties * 2, TENURED); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4146 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4147 | bool is_simple = true; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4148 | bool fast_elements = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4149 | int depth = 1; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4150 | BuildObjectLiteralConstantProperties(properties, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4151 | constant_properties, |
| 4152 | &is_simple, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4153 | &fast_elements, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4154 | &depth); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4155 | return factory()->NewObjectLiteral(constant_properties, |
| 4156 | properties, |
| 4157 | literal_index, |
| 4158 | is_simple, |
| 4159 | fast_elements, |
| 4160 | depth, |
| 4161 | has_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | |
| 4165 | Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4166 | if (!scanner().ScanRegExpPattern(seen_equal)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4167 | Next(); |
| 4168 | ReportMessage("unterminated_regexp", Vector<const char*>::empty()); |
| 4169 | *ok = false; |
| 4170 | return NULL; |
| 4171 | } |
| 4172 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4173 | int literal_index = current_function_state_->NextMaterializedLiteralIndex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4174 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4175 | Handle<String> js_pattern = NextLiteralString(TENURED); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4176 | scanner().ScanRegExpFlags(); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4177 | Handle<String> js_flags = NextLiteralString(TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4178 | Next(); |
| 4179 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4180 | return factory()->NewRegExpLiteral(js_pattern, js_flags, literal_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | |
| 4184 | ZoneList<Expression*>* Parser::ParseArguments(bool* ok) { |
| 4185 | // Arguments :: |
| 4186 | // '(' (AssignmentExpression)*[','] ')' |
| 4187 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4188 | ZoneList<Expression*>* result = new(zone()) ZoneList<Expression*>(4, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4189 | Expect(Token::LPAREN, CHECK_OK); |
| 4190 | bool done = (peek() == Token::RPAREN); |
| 4191 | while (!done) { |
| 4192 | Expression* argument = ParseAssignmentExpression(true, CHECK_OK); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4193 | result->Add(argument, zone()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4194 | if (result->length() > kMaxNumFunctionParameters) { |
| 4195 | ReportMessageAt(scanner().location(), "too_many_arguments", |
| 4196 | Vector<const char*>::empty()); |
| 4197 | *ok = false; |
| 4198 | return NULL; |
| 4199 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4200 | done = (peek() == Token::RPAREN); |
| 4201 | if (!done) Expect(Token::COMMA, CHECK_OK); |
| 4202 | } |
| 4203 | Expect(Token::RPAREN, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4204 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4208 | class SingletonLogger : public ParserRecorder { |
| 4209 | public: |
| 4210 | SingletonLogger() : has_error_(false), start_(-1), end_(-1) { } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4211 | virtual ~SingletonLogger() { } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4212 | |
| 4213 | void Reset() { has_error_ = false; } |
| 4214 | |
| 4215 | virtual void LogFunction(int start, |
| 4216 | int end, |
| 4217 | int literals, |
| 4218 | int properties, |
| 4219 | LanguageMode mode) { |
| 4220 | ASSERT(!has_error_); |
| 4221 | start_ = start; |
| 4222 | end_ = end; |
| 4223 | literals_ = literals; |
| 4224 | properties_ = properties; |
| 4225 | mode_ = mode; |
| 4226 | }; |
| 4227 | |
| 4228 | // Logs a symbol creation of a literal or identifier. |
| 4229 | virtual void LogAsciiSymbol(int start, Vector<const char> literal) { } |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 4230 | virtual void LogUtf16Symbol(int start, Vector<const uc16> literal) { } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4231 | |
| 4232 | // Logs an error message and marks the log as containing an error. |
| 4233 | // Further logging will be ignored, and ExtractData will return a vector |
| 4234 | // representing the error only. |
| 4235 | virtual void LogMessage(int start, |
| 4236 | int end, |
| 4237 | const char* message, |
| 4238 | const char* argument_opt) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 4239 | if (has_error_) return; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4240 | has_error_ = true; |
| 4241 | start_ = start; |
| 4242 | end_ = end; |
| 4243 | message_ = message; |
| 4244 | argument_opt_ = argument_opt; |
| 4245 | } |
| 4246 | |
| 4247 | virtual int function_position() { return 0; } |
| 4248 | |
| 4249 | virtual int symbol_position() { return 0; } |
| 4250 | |
| 4251 | virtual int symbol_ids() { return -1; } |
| 4252 | |
| 4253 | virtual Vector<unsigned> ExtractData() { |
| 4254 | UNREACHABLE(); |
| 4255 | return Vector<unsigned>(); |
| 4256 | } |
| 4257 | |
| 4258 | virtual void PauseRecording() { } |
| 4259 | |
| 4260 | virtual void ResumeRecording() { } |
| 4261 | |
| 4262 | bool has_error() { return has_error_; } |
| 4263 | |
| 4264 | int start() { return start_; } |
| 4265 | int end() { return end_; } |
| 4266 | int literals() { |
| 4267 | ASSERT(!has_error_); |
| 4268 | return literals_; |
| 4269 | } |
| 4270 | int properties() { |
| 4271 | ASSERT(!has_error_); |
| 4272 | return properties_; |
| 4273 | } |
| 4274 | LanguageMode language_mode() { |
| 4275 | ASSERT(!has_error_); |
| 4276 | return mode_; |
| 4277 | } |
| 4278 | const char* message() { |
| 4279 | ASSERT(has_error_); |
| 4280 | return message_; |
| 4281 | } |
| 4282 | const char* argument_opt() { |
| 4283 | ASSERT(has_error_); |
| 4284 | return argument_opt_; |
| 4285 | } |
| 4286 | |
| 4287 | private: |
| 4288 | bool has_error_; |
| 4289 | int start_; |
| 4290 | int end_; |
| 4291 | // For function entries. |
| 4292 | int literals_; |
| 4293 | int properties_; |
| 4294 | LanguageMode mode_; |
| 4295 | // For error messages. |
| 4296 | const char* message_; |
| 4297 | const char* argument_opt_; |
| 4298 | }; |
| 4299 | |
| 4300 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4301 | FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name, |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4302 | bool name_is_strict_reserved, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4303 | int function_token_position, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4304 | FunctionLiteral::Type type, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4305 | bool* ok) { |
| 4306 | // Function :: |
| 4307 | // '(' FormalParameterList? ')' '{' FunctionBody '}' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4308 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4309 | // Anonymous functions were passed either the empty symbol or a null |
| 4310 | // handle as the function name. Remember if we were passed a non-empty |
| 4311 | // handle to decide whether to invoke function name inference. |
| 4312 | bool should_infer_name = function_name.is_null(); |
| 4313 | |
| 4314 | // We want a non-null handle as the function name. |
| 4315 | if (should_infer_name) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 4316 | function_name = isolate()->factory()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | int num_parameters = 0; |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 4320 | // Function declarations are function scoped in normal mode, so they are |
| 4321 | // hoisted. In harmony block scoping mode they are block scoped, so they |
| 4322 | // are not hoisted. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4323 | Scope* scope = (type == FunctionLiteral::DECLARATION && !is_extended_mode()) |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4324 | ? NewScope(top_scope_->DeclarationScope(), FUNCTION_SCOPE) |
| 4325 | : NewScope(top_scope_, FUNCTION_SCOPE); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4326 | ZoneList<Statement*>* body = NULL; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4327 | int materialized_literal_count = -1; |
| 4328 | int expected_property_count = -1; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4329 | int handler_count = 0; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4330 | bool only_simple_this_property_assignments; |
| 4331 | Handle<FixedArray> this_property_assignments; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4332 | FunctionLiteral::ParameterFlag duplicate_parameters = |
| 4333 | FunctionLiteral::kNoDuplicateParameters; |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 4334 | FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ |
| 4335 | ? FunctionLiteral::kIsParenthesized |
| 4336 | : FunctionLiteral::kNotParenthesized; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4337 | AstProperties ast_properties; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4338 | // Parse function body. |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4339 | { FunctionState function_state(this, scope, isolate()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4340 | top_scope_->SetScopeName(function_name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4341 | |
| 4342 | // FormalParameterList :: |
| 4343 | // '(' (Identifier)*[','] ')' |
| 4344 | Expect(Token::LPAREN, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4345 | scope->set_start_position(scanner().location().beg_pos); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4346 | Scanner::Location name_loc = Scanner::Location::invalid(); |
| 4347 | Scanner::Location dupe_loc = Scanner::Location::invalid(); |
| 4348 | Scanner::Location reserved_loc = Scanner::Location::invalid(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4349 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4350 | bool done = (peek() == Token::RPAREN); |
| 4351 | while (!done) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4352 | bool is_strict_reserved = false; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4353 | Handle<String> param_name = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4354 | ParseIdentifierOrStrictReservedWord(&is_strict_reserved, |
| 4355 | CHECK_OK); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4356 | |
| 4357 | // Store locations for possible future error reports. |
| 4358 | if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) { |
| 4359 | name_loc = scanner().location(); |
| 4360 | } |
| 4361 | if (!dupe_loc.IsValid() && top_scope_->IsDeclared(param_name)) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4362 | duplicate_parameters = FunctionLiteral::kHasDuplicateParameters; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4363 | dupe_loc = scanner().location(); |
| 4364 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4365 | if (!reserved_loc.IsValid() && is_strict_reserved) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4366 | reserved_loc = scanner().location(); |
| 4367 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4368 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4369 | top_scope_->DeclareParameter(param_name, VAR); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4370 | num_parameters++; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4371 | if (num_parameters > kMaxNumFunctionParameters) { |
| 4372 | ReportMessageAt(scanner().location(), "too_many_parameters", |
| 4373 | Vector<const char*>::empty()); |
| 4374 | *ok = false; |
| 4375 | return NULL; |
| 4376 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4377 | done = (peek() == Token::RPAREN); |
| 4378 | if (!done) Expect(Token::COMMA, CHECK_OK); |
| 4379 | } |
| 4380 | Expect(Token::RPAREN, CHECK_OK); |
| 4381 | |
| 4382 | Expect(Token::LBRACE, CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4383 | |
| 4384 | // If we have a named function expression, we add a local variable |
| 4385 | // declaration to the body of the function with the name of the |
| 4386 | // function and let it refer to the function itself (closure). |
| 4387 | // NOTE: We create a proxy and resolve it here so that in the |
| 4388 | // future we can change the AST to only refer to VariableProxies |
| 4389 | // instead of Variables and Proxis as is the case now. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4390 | Variable* fvar = NULL; |
| 4391 | Token::Value fvar_init_op = Token::INIT_CONST; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4392 | if (type == FunctionLiteral::NAMED_EXPRESSION) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4393 | if (is_extended_mode()) fvar_init_op = Token::INIT_CONST_HARMONY; |
| 4394 | VariableMode fvar_mode = is_extended_mode() ? CONST_HARMONY : CONST; |
| 4395 | fvar = new(zone()) Variable(top_scope_, |
| 4396 | function_name, fvar_mode, true /* is valid LHS */, |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 4397 | Variable::NORMAL, kCreatedInitialized, Interface::NewConst()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4398 | VariableProxy* proxy = factory()->NewVariableProxy(fvar); |
| 4399 | VariableDeclaration* fvar_declaration = |
| 4400 | factory()->NewVariableDeclaration(proxy, fvar_mode, top_scope_); |
| 4401 | top_scope_->DeclareFunctionVar(fvar_declaration); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4404 | // Determine whether the function will be lazily compiled. |
| 4405 | // The heuristics are: |
| 4406 | // - It must not have been prohibited by the caller to Parse (some callers |
| 4407 | // need a full AST). |
yangguo@chromium.org | 9c741c8 | 2012-06-28 15:04:22 +0000 | [diff] [blame] | 4408 | // - The outer scope must allow lazy compilation of inner functions. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4409 | // - The function mustn't be a function expression with an open parenthesis |
| 4410 | // before; we consider that a hint that the function will be called |
| 4411 | // immediately, and it would be a waste of time to make it lazily |
| 4412 | // compiled. |
| 4413 | // These are all things we can know at this point, without looking at the |
| 4414 | // function itself. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4415 | bool is_lazily_compiled = (mode() == PARSE_LAZILY && |
yangguo@chromium.org | 9c741c8 | 2012-06-28 15:04:22 +0000 | [diff] [blame] | 4416 | top_scope_->AllowsLazyCompilation() && |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4417 | !parenthesized_function_); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4418 | parenthesized_function_ = false; // The bit was set for this function only. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4419 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4420 | if (is_lazily_compiled) { |
| 4421 | int function_block_pos = scanner().location().beg_pos; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4422 | FunctionEntry entry; |
| 4423 | if (pre_data_ != NULL) { |
| 4424 | // If we have pre_data_, we use it to skip parsing the function body. |
| 4425 | // the preparser data contains the information we need to construct the |
| 4426 | // lazy function. |
| 4427 | entry = pre_data()->GetFunctionEntry(function_block_pos); |
| 4428 | if (entry.is_valid()) { |
| 4429 | if (entry.end_pos() <= function_block_pos) { |
| 4430 | // End position greater than end of stream is safe, and hard |
| 4431 | // to check. |
| 4432 | ReportInvalidPreparseData(function_name, CHECK_OK); |
| 4433 | } |
| 4434 | scanner().SeekForward(entry.end_pos() - 1); |
| 4435 | |
| 4436 | scope->set_end_position(entry.end_pos()); |
| 4437 | Expect(Token::RBRACE, CHECK_OK); |
| 4438 | isolate()->counters()->total_preparse_skipped()->Increment( |
| 4439 | scope->end_position() - function_block_pos); |
| 4440 | materialized_literal_count = entry.literal_count(); |
| 4441 | expected_property_count = entry.property_count(); |
| 4442 | top_scope_->SetLanguageMode(entry.language_mode()); |
| 4443 | only_simple_this_property_assignments = false; |
| 4444 | this_property_assignments = isolate()->factory()->empty_fixed_array(); |
| 4445 | } else { |
| 4446 | is_lazily_compiled = false; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4447 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4448 | } else { |
| 4449 | // With no preparser data, we partially parse the function, without |
| 4450 | // building an AST. This gathers the data needed to build a lazy |
| 4451 | // function. |
| 4452 | SingletonLogger logger; |
| 4453 | preparser::PreParser::PreParseResult result = |
| 4454 | LazyParseFunctionLiteral(&logger); |
| 4455 | if (result == preparser::PreParser::kPreParseStackOverflow) { |
| 4456 | // Propagate stack overflow. |
| 4457 | stack_overflow_ = true; |
| 4458 | *ok = false; |
| 4459 | return NULL; |
| 4460 | } |
| 4461 | if (logger.has_error()) { |
| 4462 | const char* arg = logger.argument_opt(); |
| 4463 | Vector<const char*> args; |
| 4464 | if (arg != NULL) { |
| 4465 | args = Vector<const char*>(&arg, 1); |
| 4466 | } |
| 4467 | ReportMessageAt(Scanner::Location(logger.start(), logger.end()), |
| 4468 | logger.message(), args); |
| 4469 | *ok = false; |
| 4470 | return NULL; |
| 4471 | } |
| 4472 | scope->set_end_position(logger.end()); |
| 4473 | Expect(Token::RBRACE, CHECK_OK); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4474 | isolate()->counters()->total_preparse_skipped()->Increment( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4475 | scope->end_position() - function_block_pos); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4476 | materialized_literal_count = logger.literals(); |
| 4477 | expected_property_count = logger.properties(); |
| 4478 | top_scope_->SetLanguageMode(logger.language_mode()); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4479 | only_simple_this_property_assignments = false; |
| 4480 | this_property_assignments = isolate()->factory()->empty_fixed_array(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 4481 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | if (!is_lazily_compiled) { |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 4485 | ParsingModeScope parsing_mode(this, PARSE_EAGERLY); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4486 | body = new(zone()) ZoneList<Statement*>(8, zone()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4487 | if (fvar != NULL) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 4488 | VariableProxy* fproxy = top_scope_->NewUnresolved( |
| 4489 | factory(), function_name, Interface::NewConst()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4490 | fproxy->BindTo(fvar); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4491 | body->Add(factory()->NewExpressionStatement( |
| 4492 | factory()->NewAssignment(fvar_init_op, |
| 4493 | fproxy, |
| 4494 | factory()->NewThisFunction(), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4495 | RelocInfo::kNoPosition)), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 4496 | zone()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4497 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 4498 | ParseSourceElements(body, Token::RBRACE, false, false, CHECK_OK); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 4499 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4500 | materialized_literal_count = function_state.materialized_literal_count(); |
| 4501 | expected_property_count = function_state.expected_property_count(); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 4502 | handler_count = function_state.handler_count(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 4503 | only_simple_this_property_assignments = |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4504 | function_state.only_simple_this_property_assignments(); |
| 4505 | this_property_assignments = function_state.this_property_assignments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4506 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4507 | Expect(Token::RBRACE, CHECK_OK); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4508 | scope->set_end_position(scanner().location().end_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4511 | // Validate strict mode. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4512 | if (!top_scope_->is_classic_mode()) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4513 | if (IsEvalOrArguments(function_name)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4514 | int start_pos = scope->start_position(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4515 | int position = function_token_position != RelocInfo::kNoPosition |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4516 | ? function_token_position |
| 4517 | : (start_pos > 0 ? start_pos - 1 : start_pos); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4518 | Scanner::Location location = Scanner::Location(position, start_pos); |
| 4519 | ReportMessageAt(location, |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4520 | "strict_function_name", Vector<const char*>::empty()); |
| 4521 | *ok = false; |
| 4522 | return NULL; |
| 4523 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4524 | if (name_loc.IsValid()) { |
| 4525 | ReportMessageAt(name_loc, "strict_param_name", |
| 4526 | Vector<const char*>::empty()); |
| 4527 | *ok = false; |
| 4528 | return NULL; |
| 4529 | } |
| 4530 | if (dupe_loc.IsValid()) { |
| 4531 | ReportMessageAt(dupe_loc, "strict_param_dupe", |
| 4532 | Vector<const char*>::empty()); |
| 4533 | *ok = false; |
| 4534 | return NULL; |
| 4535 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4536 | if (name_is_strict_reserved) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4537 | int start_pos = scope->start_position(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4538 | int position = function_token_position != RelocInfo::kNoPosition |
| 4539 | ? function_token_position |
| 4540 | : (start_pos > 0 ? start_pos - 1 : start_pos); |
| 4541 | Scanner::Location location = Scanner::Location(position, start_pos); |
| 4542 | ReportMessageAt(location, "strict_reserved_word", |
| 4543 | Vector<const char*>::empty()); |
| 4544 | *ok = false; |
| 4545 | return NULL; |
| 4546 | } |
| 4547 | if (reserved_loc.IsValid()) { |
| 4548 | ReportMessageAt(reserved_loc, "strict_reserved_word", |
| 4549 | Vector<const char*>::empty()); |
| 4550 | *ok = false; |
| 4551 | return NULL; |
| 4552 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4553 | CheckOctalLiteral(scope->start_position(), |
| 4554 | scope->end_position(), |
| 4555 | CHECK_OK); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4556 | } |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4557 | ast_properties = *factory()->visitor()->ast_properties(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4558 | } |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4559 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4560 | if (is_extended_mode()) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 4561 | CheckConflictingVarDeclarations(scope, CHECK_OK); |
| 4562 | } |
| 4563 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4564 | FunctionLiteral* function_literal = |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4565 | factory()->NewFunctionLiteral(function_name, |
| 4566 | scope, |
| 4567 | body, |
| 4568 | materialized_literal_count, |
| 4569 | expected_property_count, |
| 4570 | handler_count, |
| 4571 | only_simple_this_property_assignments, |
| 4572 | this_property_assignments, |
| 4573 | num_parameters, |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 4574 | duplicate_parameters, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4575 | type, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 4576 | FunctionLiteral::kIsFunction, |
| 4577 | parenthesized); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4578 | function_literal->set_function_token_position(function_token_position); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4579 | function_literal->set_ast_properties(&ast_properties); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4580 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4581 | if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4582 | return function_literal; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4586 | preparser::PreParser::PreParseResult Parser::LazyParseFunctionLiteral( |
| 4587 | SingletonLogger* logger) { |
| 4588 | HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse()); |
| 4589 | ASSERT_EQ(Token::LBRACE, scanner().current_token()); |
| 4590 | |
| 4591 | if (reusable_preparser_ == NULL) { |
| 4592 | intptr_t stack_limit = isolate()->stack_guard()->real_climit(); |
| 4593 | bool do_allow_lazy = true; |
| 4594 | reusable_preparser_ = new preparser::PreParser(&scanner_, |
| 4595 | NULL, |
| 4596 | stack_limit, |
| 4597 | do_allow_lazy, |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 4598 | allow_natives_syntax_, |
| 4599 | allow_modules_); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4600 | } |
| 4601 | preparser::PreParser::PreParseResult result = |
| 4602 | reusable_preparser_->PreParseLazyFunction(top_scope_->language_mode(), |
| 4603 | logger); |
| 4604 | return result; |
| 4605 | } |
| 4606 | |
| 4607 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4608 | Expression* Parser::ParseV8Intrinsic(bool* ok) { |
| 4609 | // CallRuntime :: |
| 4610 | // '%' Identifier Arguments |
| 4611 | |
| 4612 | Expect(Token::MOD, CHECK_OK); |
| 4613 | Handle<String> name = ParseIdentifier(CHECK_OK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4614 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4615 | |
| 4616 | if (extension_ != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4617 | // The extension structures are only accessible while parsing the |
| 4618 | // very first time not when reparsing because of lazy compilation. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4619 | top_scope_->DeclarationScope()->ForceEagerCompilation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 4622 | const Runtime::Function* function = Runtime::FunctionForName(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4623 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4624 | // Check for built-in IS_VAR macro. |
| 4625 | if (function != NULL && |
| 4626 | function->intrinsic_type == Runtime::RUNTIME && |
| 4627 | function->function_id == Runtime::kIS_VAR) { |
| 4628 | // %IS_VAR(x) evaluates to x if x is a variable, |
| 4629 | // leads to a parse error otherwise. Could be implemented as an |
| 4630 | // inline function %_IS_VAR(x) to eliminate this special case. |
| 4631 | if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) { |
| 4632 | return args->at(0); |
| 4633 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4634 | ReportMessage("unable_to_parse", Vector<const char*>::empty()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4635 | *ok = false; |
| 4636 | return NULL; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4640 | // Check that the expected number of arguments are being passed. |
| 4641 | if (function != NULL && |
| 4642 | function->nargs != -1 && |
| 4643 | function->nargs != args->length()) { |
| 4644 | ReportMessage("illegal_access", Vector<const char*>::empty()); |
| 4645 | *ok = false; |
| 4646 | return NULL; |
| 4647 | } |
| 4648 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 4649 | // Check that the function is defined if it's an inline runtime call. |
| 4650 | if (function == NULL && name->Get(0) == '_') { |
| 4651 | ReportMessage("not_defined", Vector<Handle<String> >(&name, 1)); |
| 4652 | *ok = false; |
| 4653 | return NULL; |
| 4654 | } |
| 4655 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 4656 | // We have a valid intrinsics call or a call to a builtin. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4657 | return factory()->NewCallRuntime(name, function, args); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4661 | bool Parser::peek_any_identifier() { |
| 4662 | Token::Value next = peek(); |
| 4663 | return next == Token::IDENTIFIER || |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4664 | next == Token::FUTURE_RESERVED_WORD || |
| 4665 | next == Token::FUTURE_STRICT_RESERVED_WORD; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4669 | void Parser::Consume(Token::Value token) { |
| 4670 | Token::Value next = Next(); |
| 4671 | USE(next); |
| 4672 | USE(token); |
| 4673 | ASSERT(next == token); |
| 4674 | } |
| 4675 | |
| 4676 | |
| 4677 | void Parser::Expect(Token::Value token, bool* ok) { |
| 4678 | Token::Value next = Next(); |
| 4679 | if (next == token) return; |
| 4680 | ReportUnexpectedToken(next); |
| 4681 | *ok = false; |
| 4682 | } |
| 4683 | |
| 4684 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4685 | bool Parser::Check(Token::Value token) { |
| 4686 | Token::Value next = peek(); |
| 4687 | if (next == token) { |
| 4688 | Consume(next); |
| 4689 | return true; |
| 4690 | } |
| 4691 | return false; |
| 4692 | } |
| 4693 | |
| 4694 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4695 | void Parser::ExpectSemicolon(bool* ok) { |
| 4696 | // Check for automatic semicolon insertion according to |
| 4697 | // the rules given in ECMA-262, section 7.9, page 21. |
| 4698 | Token::Value tok = peek(); |
| 4699 | if (tok == Token::SEMICOLON) { |
| 4700 | Next(); |
| 4701 | return; |
| 4702 | } |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 4703 | if (scanner().HasAnyLineTerminatorBeforeNext() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4704 | tok == Token::RBRACE || |
| 4705 | tok == Token::EOS) { |
| 4706 | return; |
| 4707 | } |
| 4708 | Expect(Token::SEMICOLON, ok); |
| 4709 | } |
| 4710 | |
| 4711 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 4712 | void Parser::ExpectContextualKeyword(const char* keyword, bool* ok) { |
| 4713 | Expect(Token::IDENTIFIER, ok); |
| 4714 | if (!*ok) return; |
| 4715 | Handle<String> symbol = GetSymbol(ok); |
| 4716 | if (!*ok) return; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 4717 | if (!symbol->IsUtf8EqualTo(CStrVector(keyword))) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 4718 | *ok = false; |
| 4719 | ReportUnexpectedToken(scanner().current_token()); |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4724 | Literal* Parser::GetLiteralUndefined() { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4725 | return factory()->NewLiteral(isolate()->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | |
| 4729 | Literal* Parser::GetLiteralTheHole() { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4730 | return factory()->NewLiteral(isolate()->factory()->the_hole_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 4734 | // Parses an identifier that is valid for the current scope, in particular it |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4735 | // fails on strict mode future reserved keywords in a strict scope. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4736 | Handle<String> Parser::ParseIdentifier(bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4737 | if (!top_scope_->is_classic_mode()) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4738 | Expect(Token::IDENTIFIER, ok); |
| 4739 | } else if (!Check(Token::IDENTIFIER)) { |
| 4740 | Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); |
| 4741 | } |
| 4742 | if (!*ok) return Handle<String>(); |
| 4743 | return GetSymbol(ok); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4747 | // Parses and identifier or a strict mode future reserved word, and indicate |
| 4748 | // whether it is strict mode future reserved. |
| 4749 | Handle<String> Parser::ParseIdentifierOrStrictReservedWord( |
| 4750 | bool* is_strict_reserved, bool* ok) { |
| 4751 | *is_strict_reserved = false; |
| 4752 | if (!Check(Token::IDENTIFIER)) { |
| 4753 | Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); |
| 4754 | *is_strict_reserved = true; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4755 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4756 | if (!*ok) return Handle<String>(); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4757 | return GetSymbol(ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4760 | |
| 4761 | Handle<String> Parser::ParseIdentifierName(bool* ok) { |
| 4762 | Token::Value next = Next(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4763 | if (next != Token::IDENTIFIER && |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4764 | next != Token::FUTURE_RESERVED_WORD && |
| 4765 | next != Token::FUTURE_STRICT_RESERVED_WORD && |
| 4766 | !Token::IsKeyword(next)) { |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4767 | ReportUnexpectedToken(next); |
| 4768 | *ok = false; |
| 4769 | return Handle<String>(); |
| 4770 | } |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4771 | return GetSymbol(ok); |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4774 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 4775 | void Parser::MarkAsLValue(Expression* expression) { |
| 4776 | VariableProxy* proxy = expression != NULL |
| 4777 | ? expression->AsVariableProxy() |
| 4778 | : NULL; |
| 4779 | |
| 4780 | if (proxy != NULL) proxy->MarkAsLValue(); |
| 4781 | } |
| 4782 | |
| 4783 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4784 | // Checks LHS expression for assignment and prefix/postfix increment/decrement |
| 4785 | // in strict mode. |
| 4786 | void Parser::CheckStrictModeLValue(Expression* expression, |
| 4787 | const char* error, |
| 4788 | bool* ok) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4789 | ASSERT(!top_scope_->is_classic_mode()); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4790 | VariableProxy* lhs = expression != NULL |
| 4791 | ? expression->AsVariableProxy() |
| 4792 | : NULL; |
| 4793 | |
| 4794 | if (lhs != NULL && !lhs->is_this() && IsEvalOrArguments(lhs->name())) { |
| 4795 | ReportMessage(error, Vector<const char*>::empty()); |
| 4796 | *ok = false; |
| 4797 | } |
| 4798 | } |
| 4799 | |
| 4800 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4801 | // Checks whether an octal literal was last seen between beg_pos and end_pos. |
| 4802 | // If so, reports an error. Only called for strict mode. |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 4803 | void Parser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4804 | Scanner::Location octal = scanner().octal_position(); |
| 4805 | if (octal.IsValid() && |
| 4806 | beg_pos <= octal.beg_pos && |
| 4807 | octal.end_pos <= end_pos) { |
| 4808 | ReportMessageAt(octal, "strict_octal_literal", |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 4809 | Vector<const char*>::empty()); |
| 4810 | scanner().clear_octal_position(); |
| 4811 | *ok = false; |
| 4812 | } |
| 4813 | } |
| 4814 | |
sgjesse@chromium.org | d3b3be0 | 2010-08-06 08:09:27 +0000 | [diff] [blame] | 4815 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 4816 | void Parser::CheckConflictingVarDeclarations(Scope* scope, bool* ok) { |
| 4817 | Declaration* decl = scope->CheckConflictingVarDeclarations(); |
| 4818 | if (decl != NULL) { |
| 4819 | // In harmony mode we treat conflicting variable bindinds as early |
| 4820 | // errors. See ES5 16 for a definition of early errors. |
| 4821 | Handle<String> name = decl->proxy()->name(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4822 | SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 4823 | const char* elms[2] = { "Variable", *c_string }; |
| 4824 | Vector<const char*> args(elms, 2); |
| 4825 | int position = decl->proxy()->position(); |
| 4826 | Scanner::Location location = position == RelocInfo::kNoPosition |
| 4827 | ? Scanner::Location::invalid() |
| 4828 | : Scanner::Location(position, position + 1); |
| 4829 | ReportMessageAt(location, "redeclaration", args); |
| 4830 | *ok = false; |
| 4831 | } |
| 4832 | } |
| 4833 | |
| 4834 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4835 | // This function reads an identifier name and determines whether or not it |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4836 | // is 'get' or 'set'. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 4837 | Handle<String> Parser::ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 4838 | bool* is_set, |
| 4839 | bool* ok) { |
| 4840 | Handle<String> result = ParseIdentifierName(ok); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4841 | if (!*ok) return Handle<String>(); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 4842 | if (scanner().is_literal_ascii() && scanner().literal_length() == 3) { |
| 4843 | const char* token = scanner().literal_ascii_string().start(); |
| 4844 | *is_get = strncmp(token, "get", 3) == 0; |
| 4845 | *is_set = !*is_get && strncmp(token, "set", 3) == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4846 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4847 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | |
| 4851 | // ---------------------------------------------------------------------------- |
| 4852 | // Parser support |
| 4853 | |
| 4854 | |
| 4855 | bool Parser::TargetStackContainsLabel(Handle<String> label) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4856 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4857 | BreakableStatement* stat = t->node()->AsBreakableStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4858 | if (stat != NULL && ContainsLabel(stat->labels(), label)) |
| 4859 | return true; |
| 4860 | } |
| 4861 | return false; |
| 4862 | } |
| 4863 | |
| 4864 | |
| 4865 | BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) { |
| 4866 | bool anonymous = label.is_null(); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4867 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4868 | BreakableStatement* stat = t->node()->AsBreakableStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4869 | if (stat == NULL) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4870 | if ((anonymous && stat->is_target_for_anonymous()) || |
| 4871 | (!anonymous && ContainsLabel(stat->labels(), label))) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4872 | RegisterTargetUse(stat->break_target(), t->previous()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4873 | return stat; |
| 4874 | } |
| 4875 | } |
| 4876 | return NULL; |
| 4877 | } |
| 4878 | |
| 4879 | |
| 4880 | IterationStatement* Parser::LookupContinueTarget(Handle<String> label, |
| 4881 | bool* ok) { |
| 4882 | bool anonymous = label.is_null(); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4883 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4884 | IterationStatement* stat = t->node()->AsIterationStatement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4885 | if (stat == NULL) continue; |
| 4886 | |
| 4887 | ASSERT(stat->is_target_for_anonymous()); |
| 4888 | if (anonymous || ContainsLabel(stat->labels(), label)) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4889 | RegisterTargetUse(stat->continue_target(), t->previous()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4890 | return stat; |
| 4891 | } |
| 4892 | } |
| 4893 | return NULL; |
| 4894 | } |
| 4895 | |
| 4896 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4897 | void Parser::RegisterTargetUse(Label* target, Target* stop) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4898 | // Register that a break target found at the given stop in the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4899 | // target stack has been used from the top of the target stack. Add |
| 4900 | // the break target to any TargetCollectors passed on the stack. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4901 | for (Target* t = target_stack_; t != stop; t = t->previous()) { |
| 4902 | TargetCollector* collector = t->node()->AsTargetCollector(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4903 | if (collector != NULL) collector->AddTarget(target, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4904 | } |
| 4905 | } |
| 4906 | |
| 4907 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4908 | Expression* Parser::NewThrowReferenceError(Handle<String> type) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 4909 | return NewThrowError(isolate()->factory()->MakeReferenceError_string(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4910 | type, HandleVector<Object>(NULL, 0)); |
| 4911 | } |
| 4912 | |
| 4913 | |
| 4914 | Expression* Parser::NewThrowSyntaxError(Handle<String> type, |
| 4915 | Handle<Object> first) { |
| 4916 | int argc = first.is_null() ? 0 : 1; |
| 4917 | Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4918 | return NewThrowError( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 4919 | isolate()->factory()->MakeSyntaxError_string(), type, arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | |
| 4923 | Expression* Parser::NewThrowTypeError(Handle<String> type, |
| 4924 | Handle<Object> first, |
| 4925 | Handle<Object> second) { |
| 4926 | ASSERT(!first.is_null() && !second.is_null()); |
| 4927 | Handle<Object> elements[] = { first, second }; |
| 4928 | Vector< Handle<Object> > arguments = |
| 4929 | HandleVector<Object>(elements, ARRAY_SIZE(elements)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4930 | return NewThrowError( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 4931 | isolate()->factory()->MakeTypeError_string(), type, arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | |
| 4935 | Expression* Parser::NewThrowError(Handle<String> constructor, |
| 4936 | Handle<String> type, |
| 4937 | Vector< Handle<Object> > arguments) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4938 | int argc = arguments.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4939 | Handle<FixedArray> elements = isolate()->factory()->NewFixedArray(argc, |
| 4940 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4941 | for (int i = 0; i < argc; i++) { |
| 4942 | Handle<Object> element = arguments[i]; |
| 4943 | if (!element.is_null()) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4944 | elements->set(i, *element); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4945 | } |
| 4946 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4947 | Handle<JSArray> array = isolate()->factory()->NewJSArrayWithElements( |
| 4948 | elements, FAST_ELEMENTS, TENURED); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4949 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4950 | ZoneList<Expression*>* args = new(zone()) ZoneList<Expression*>(2, zone()); |
| 4951 | args->Add(factory()->NewLiteral(type), zone()); |
| 4952 | args->Add(factory()->NewLiteral(array), zone()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 4953 | CallRuntime* call_constructor = |
| 4954 | factory()->NewCallRuntime(constructor, NULL, args); |
| 4955 | return factory()->NewThrow(call_constructor, scanner().location().beg_pos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4958 | // ---------------------------------------------------------------------------- |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 4959 | // Regular expressions |
| 4960 | |
| 4961 | |
| 4962 | RegExpParser::RegExpParser(FlatStringReader* in, |
| 4963 | Handle<String>* error, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 4964 | bool multiline, |
| 4965 | Zone* zone) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4966 | : isolate_(Isolate::Current()), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 4967 | zone_(zone), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4968 | error_(error), |
| 4969 | captures_(NULL), |
| 4970 | in_(in), |
| 4971 | current_(kEndMarker), |
| 4972 | next_pos_(0), |
| 4973 | capture_count_(0), |
| 4974 | has_more_(true), |
| 4975 | multiline_(multiline), |
| 4976 | simple_(false), |
| 4977 | contains_anchor_(false), |
| 4978 | is_scanned_for_captures_(false), |
| 4979 | failed_(false) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 4980 | Advance(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | |
| 4984 | uc32 RegExpParser::Next() { |
| 4985 | if (has_next()) { |
| 4986 | return in()->Get(next_pos_); |
| 4987 | } else { |
| 4988 | return kEndMarker; |
| 4989 | } |
| 4990 | } |
| 4991 | |
| 4992 | |
| 4993 | void RegExpParser::Advance() { |
| 4994 | if (next_pos_ < in()->length()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4995 | StackLimitCheck check(isolate()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 4996 | if (check.HasOverflowed()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4997 | ReportError(CStrVector(Isolate::kStackOverflowMessage)); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 4998 | } else if (zone()->excess_allocation()) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 4999 | ReportError(CStrVector("Regular expression too large")); |
| 5000 | } else { |
| 5001 | current_ = in()->Get(next_pos_); |
| 5002 | next_pos_++; |
| 5003 | } |
| 5004 | } else { |
| 5005 | current_ = kEndMarker; |
| 5006 | has_more_ = false; |
| 5007 | } |
| 5008 | } |
| 5009 | |
| 5010 | |
| 5011 | void RegExpParser::Reset(int pos) { |
| 5012 | next_pos_ = pos; |
| 5013 | Advance(); |
| 5014 | } |
| 5015 | |
| 5016 | |
| 5017 | void RegExpParser::Advance(int dist) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5018 | next_pos_ += dist - 1; |
| 5019 | Advance(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5023 | bool RegExpParser::simple() { |
| 5024 | return simple_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | RegExpTree* RegExpParser::ReportError(Vector<const char> message) { |
| 5028 | failed_ = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5029 | *error_ = isolate()->factory()->NewStringFromAscii(message, NOT_TENURED); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5030 | // Zip to the end to make sure the no more input is read. |
| 5031 | current_ = kEndMarker; |
| 5032 | next_pos_ = in()->length(); |
| 5033 | return NULL; |
| 5034 | } |
| 5035 | |
| 5036 | |
| 5037 | // Pattern :: |
| 5038 | // Disjunction |
| 5039 | RegExpTree* RegExpParser::ParsePattern() { |
| 5040 | RegExpTree* result = ParseDisjunction(CHECK_FAILED); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5041 | ASSERT(!has_more()); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5042 | // If the result of parsing is a literal string atom, and it has the |
| 5043 | // same length as the input, then the atom is identical to the input. |
| 5044 | if (result->IsAtom() && result->AsAtom()->length() == in()->length()) { |
| 5045 | simple_ = true; |
| 5046 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5047 | return result; |
| 5048 | } |
| 5049 | |
| 5050 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5051 | // Disjunction :: |
| 5052 | // Alternative |
| 5053 | // Alternative | Disjunction |
| 5054 | // Alternative :: |
| 5055 | // [empty] |
| 5056 | // Term Alternative |
| 5057 | // Term :: |
| 5058 | // Assertion |
| 5059 | // Atom |
| 5060 | // Atom Quantifier |
| 5061 | RegExpTree* RegExpParser::ParseDisjunction() { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5062 | // Used to store current state while parsing subexpressions. |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 5063 | RegExpParserState initial_state(NULL, INITIAL, 0, zone()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5064 | RegExpParserState* stored_state = &initial_state; |
| 5065 | // Cache the builder in a local variable for quick access. |
| 5066 | RegExpBuilder* builder = initial_state.builder(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5067 | while (true) { |
| 5068 | switch (current()) { |
| 5069 | case kEndMarker: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5070 | if (stored_state->IsSubexpression()) { |
| 5071 | // Inside a parenthesized group when hitting end of input. |
| 5072 | ReportError(CStrVector("Unterminated group") CHECK_FAILED); |
| 5073 | } |
| 5074 | ASSERT_EQ(INITIAL, stored_state->group_type()); |
| 5075 | // Parsing completed successfully. |
| 5076 | return builder->ToRegExp(); |
| 5077 | case ')': { |
| 5078 | if (!stored_state->IsSubexpression()) { |
| 5079 | ReportError(CStrVector("Unmatched ')'") CHECK_FAILED); |
| 5080 | } |
| 5081 | ASSERT_NE(INITIAL, stored_state->group_type()); |
| 5082 | |
| 5083 | Advance(); |
| 5084 | // End disjunction parsing and convert builder content to new single |
| 5085 | // regexp atom. |
| 5086 | RegExpTree* body = builder->ToRegExp(); |
| 5087 | |
| 5088 | int end_capture_index = captures_started(); |
| 5089 | |
| 5090 | int capture_index = stored_state->capture_index(); |
| 5091 | SubexpressionType type = stored_state->group_type(); |
| 5092 | |
| 5093 | // Restore previous state. |
| 5094 | stored_state = stored_state->previous_state(); |
| 5095 | builder = stored_state->builder(); |
| 5096 | |
| 5097 | // Build result of subexpression. |
| 5098 | if (type == CAPTURE) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5099 | RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5100 | captures_->at(capture_index - 1) = capture; |
| 5101 | body = capture; |
| 5102 | } else if (type != GROUPING) { |
| 5103 | ASSERT(type == POSITIVE_LOOKAHEAD || type == NEGATIVE_LOOKAHEAD); |
| 5104 | bool is_positive = (type == POSITIVE_LOOKAHEAD); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5105 | body = new(zone()) RegExpLookahead(body, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5106 | is_positive, |
| 5107 | end_capture_index - capture_index, |
| 5108 | capture_index); |
| 5109 | } |
| 5110 | builder->AddAtom(body); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 5111 | // For compatability with JSC and ES3, we allow quantifiers after |
| 5112 | // lookaheads, and break in all cases. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5113 | break; |
| 5114 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5115 | case '|': { |
| 5116 | Advance(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5117 | builder->NewAlternative(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5118 | continue; |
| 5119 | } |
| 5120 | case '*': |
| 5121 | case '+': |
| 5122 | case '?': |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5123 | return ReportError(CStrVector("Nothing to repeat")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5124 | case '^': { |
| 5125 | Advance(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5126 | if (multiline_) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5127 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5128 | new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE)); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5129 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5130 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5131 | new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT)); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5132 | set_contains_anchor(); |
| 5133 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5134 | continue; |
| 5135 | } |
| 5136 | case '$': { |
| 5137 | Advance(); |
| 5138 | RegExpAssertion::Type type = |
| 5139 | multiline_ ? RegExpAssertion::END_OF_LINE : |
| 5140 | RegExpAssertion::END_OF_INPUT; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5141 | builder->AddAssertion(new(zone()) RegExpAssertion(type)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5142 | continue; |
| 5143 | } |
| 5144 | case '.': { |
| 5145 | Advance(); |
| 5146 | // everything except \x0a, \x0d, \u2028 and \u2029 |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5147 | ZoneList<CharacterRange>* ranges = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5148 | new(zone()) ZoneList<CharacterRange>(2, zone()); |
| 5149 | CharacterRange::AddClassEscape('.', ranges, zone()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5150 | RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5151 | builder->AddAtom(atom); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5152 | break; |
| 5153 | } |
| 5154 | case '(': { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5155 | SubexpressionType type = CAPTURE; |
| 5156 | Advance(); |
| 5157 | if (current() == '?') { |
| 5158 | switch (Next()) { |
| 5159 | case ':': |
| 5160 | type = GROUPING; |
| 5161 | break; |
| 5162 | case '=': |
| 5163 | type = POSITIVE_LOOKAHEAD; |
| 5164 | break; |
| 5165 | case '!': |
| 5166 | type = NEGATIVE_LOOKAHEAD; |
| 5167 | break; |
| 5168 | default: |
| 5169 | ReportError(CStrVector("Invalid group") CHECK_FAILED); |
| 5170 | break; |
| 5171 | } |
| 5172 | Advance(2); |
| 5173 | } else { |
| 5174 | if (captures_ == NULL) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5175 | captures_ = new(zone()) ZoneList<RegExpCapture*>(2, zone()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5176 | } |
| 5177 | if (captures_started() >= kMaxCaptures) { |
| 5178 | ReportError(CStrVector("Too many captures") CHECK_FAILED); |
| 5179 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5180 | captures_->Add(NULL, zone()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5181 | } |
| 5182 | // Store current state and begin new disjunction parsing. |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 5183 | stored_state = new(zone()) RegExpParserState(stored_state, type, |
| 5184 | captures_started(), zone()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5185 | builder = stored_state->builder(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 5186 | continue; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5187 | } |
| 5188 | case '[': { |
| 5189 | RegExpTree* atom = ParseCharacterClass(CHECK_FAILED); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5190 | builder->AddAtom(atom); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5191 | break; |
| 5192 | } |
| 5193 | // Atom :: |
| 5194 | // \ AtomEscape |
| 5195 | case '\\': |
| 5196 | switch (Next()) { |
| 5197 | case kEndMarker: |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5198 | return ReportError(CStrVector("\\ at end of pattern")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5199 | case 'b': |
| 5200 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5201 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5202 | new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5203 | continue; |
| 5204 | case 'B': |
| 5205 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5206 | builder->AddAssertion( |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5207 | new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5208 | continue; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 5209 | // AtomEscape :: |
| 5210 | // CharacterClassEscape |
| 5211 | // |
| 5212 | // CharacterClassEscape :: one of |
| 5213 | // d D s S w W |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5214 | case 'd': case 'D': case 's': case 'S': case 'w': case 'W': { |
| 5215 | uc32 c = Next(); |
| 5216 | Advance(2); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5217 | ZoneList<CharacterRange>* ranges = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5218 | new(zone()) ZoneList<CharacterRange>(2, zone()); |
| 5219 | CharacterRange::AddClassEscape(c, ranges, zone()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5220 | RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5221 | builder->AddAtom(atom); |
| 5222 | break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5223 | } |
| 5224 | case '1': case '2': case '3': case '4': case '5': case '6': |
| 5225 | case '7': case '8': case '9': { |
| 5226 | int index = 0; |
| 5227 | if (ParseBackReferenceIndex(&index)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5228 | RegExpCapture* capture = NULL; |
| 5229 | if (captures_ != NULL && index <= captures_->length()) { |
| 5230 | capture = captures_->at(index - 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5231 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5232 | if (capture == NULL) { |
| 5233 | builder->AddEmpty(); |
| 5234 | break; |
| 5235 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5236 | RegExpTree* atom = new(zone()) RegExpBackReference(capture); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5237 | builder->AddAtom(atom); |
| 5238 | break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5239 | } |
| 5240 | uc32 first_digit = Next(); |
| 5241 | if (first_digit == '8' || first_digit == '9') { |
| 5242 | // Treat as identity escape |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5243 | builder->AddCharacter(first_digit); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5244 | Advance(2); |
| 5245 | break; |
| 5246 | } |
| 5247 | } |
| 5248 | // FALLTHROUGH |
| 5249 | case '0': { |
| 5250 | Advance(); |
| 5251 | uc32 octal = ParseOctalLiteral(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5252 | builder->AddCharacter(octal); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5253 | break; |
| 5254 | } |
| 5255 | // ControlEscape :: one of |
| 5256 | // f n r t v |
| 5257 | case 'f': |
| 5258 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5259 | builder->AddCharacter('\f'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5260 | break; |
| 5261 | case 'n': |
| 5262 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5263 | builder->AddCharacter('\n'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5264 | break; |
| 5265 | case 'r': |
| 5266 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5267 | builder->AddCharacter('\r'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5268 | break; |
| 5269 | case 't': |
| 5270 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5271 | builder->AddCharacter('\t'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5272 | break; |
| 5273 | case 'v': |
| 5274 | Advance(2); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5275 | builder->AddCharacter('\v'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5276 | break; |
| 5277 | case 'c': { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 5278 | Advance(); |
| 5279 | uc32 controlLetter = Next(); |
| 5280 | // Special case if it is an ASCII letter. |
| 5281 | // Convert lower case letters to uppercase. |
| 5282 | uc32 letter = controlLetter & ~('a' ^ 'A'); |
| 5283 | if (letter < 'A' || 'Z' < letter) { |
| 5284 | // controlLetter is not in range 'A'-'Z' or 'a'-'z'. |
| 5285 | // This is outside the specification. We match JSC in |
| 5286 | // reading the backslash as a literal character instead |
| 5287 | // of as starting an escape. |
| 5288 | builder->AddCharacter('\\'); |
| 5289 | } else { |
| 5290 | Advance(2); |
| 5291 | builder->AddCharacter(controlLetter & 0x1f); |
| 5292 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5293 | break; |
| 5294 | } |
| 5295 | case 'x': { |
| 5296 | Advance(2); |
| 5297 | uc32 value; |
| 5298 | if (ParseHexEscape(2, &value)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5299 | builder->AddCharacter(value); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5300 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5301 | builder->AddCharacter('x'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5302 | } |
| 5303 | break; |
| 5304 | } |
| 5305 | case 'u': { |
| 5306 | Advance(2); |
| 5307 | uc32 value; |
| 5308 | if (ParseHexEscape(4, &value)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5309 | builder->AddCharacter(value); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5310 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5311 | builder->AddCharacter('u'); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5312 | } |
| 5313 | break; |
| 5314 | } |
| 5315 | default: |
| 5316 | // Identity escape. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5317 | builder->AddCharacter(Next()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5318 | Advance(2); |
| 5319 | break; |
| 5320 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5321 | break; |
| 5322 | case '{': { |
| 5323 | int dummy; |
| 5324 | if (ParseIntervalQuantifier(&dummy, &dummy)) { |
| 5325 | ReportError(CStrVector("Nothing to repeat") CHECK_FAILED); |
| 5326 | } |
| 5327 | // fallthrough |
| 5328 | } |
| 5329 | default: |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 5330 | builder->AddCharacter(current()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5331 | Advance(); |
| 5332 | break; |
| 5333 | } // end switch(current()) |
| 5334 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5335 | int min; |
| 5336 | int max; |
| 5337 | switch (current()) { |
| 5338 | // QuantifierPrefix :: |
| 5339 | // * |
| 5340 | // + |
| 5341 | // ? |
| 5342 | // { |
| 5343 | case '*': |
| 5344 | min = 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5345 | max = RegExpTree::kInfinity; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5346 | Advance(); |
| 5347 | break; |
| 5348 | case '+': |
| 5349 | min = 1; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5350 | max = RegExpTree::kInfinity; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5351 | Advance(); |
| 5352 | break; |
| 5353 | case '?': |
| 5354 | min = 0; |
| 5355 | max = 1; |
| 5356 | Advance(); |
| 5357 | break; |
| 5358 | case '{': |
| 5359 | if (ParseIntervalQuantifier(&min, &max)) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5360 | if (max < min) { |
| 5361 | ReportError(CStrVector("numbers out of order in {} quantifier.") |
| 5362 | CHECK_FAILED); |
| 5363 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5364 | break; |
| 5365 | } else { |
| 5366 | continue; |
| 5367 | } |
| 5368 | default: |
| 5369 | continue; |
| 5370 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5371 | RegExpQuantifier::Type type = RegExpQuantifier::GREEDY; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5372 | if (current() == '?') { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5373 | type = RegExpQuantifier::NON_GREEDY; |
| 5374 | Advance(); |
| 5375 | } else if (FLAG_regexp_possessive_quantifier && current() == '+') { |
| 5376 | // FLAG_regexp_possessive_quantifier is a debug-only flag. |
| 5377 | type = RegExpQuantifier::POSSESSIVE; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5378 | Advance(); |
| 5379 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5380 | builder->AddQuantifierToAtom(min, max, type); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5381 | } |
| 5382 | } |
| 5383 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5384 | |
| 5385 | #ifdef DEBUG |
| 5386 | // Currently only used in an ASSERT. |
| 5387 | static bool IsSpecialClassEscape(uc32 c) { |
| 5388 | switch (c) { |
| 5389 | case 'd': case 'D': |
| 5390 | case 's': case 'S': |
| 5391 | case 'w': case 'W': |
| 5392 | return true; |
| 5393 | default: |
| 5394 | return false; |
| 5395 | } |
| 5396 | } |
| 5397 | #endif |
| 5398 | |
| 5399 | |
| 5400 | // In order to know whether an escape is a backreference or not we have to scan |
| 5401 | // the entire regexp and find the number of capturing parentheses. However we |
| 5402 | // don't want to scan the regexp twice unless it is necessary. This mini-parser |
| 5403 | // is called when needed. It can see the difference between capturing and |
| 5404 | // noncapturing parentheses and can skip character classes and backslash-escaped |
| 5405 | // characters. |
| 5406 | void RegExpParser::ScanForCaptures() { |
| 5407 | // Start with captures started previous to current position |
| 5408 | int capture_count = captures_started(); |
| 5409 | // Add count of captures after this position. |
| 5410 | int n; |
| 5411 | while ((n = current()) != kEndMarker) { |
| 5412 | Advance(); |
| 5413 | switch (n) { |
| 5414 | case '\\': |
| 5415 | Advance(); |
| 5416 | break; |
| 5417 | case '[': { |
| 5418 | int c; |
| 5419 | while ((c = current()) != kEndMarker) { |
| 5420 | Advance(); |
| 5421 | if (c == '\\') { |
| 5422 | Advance(); |
| 5423 | } else { |
| 5424 | if (c == ']') break; |
| 5425 | } |
| 5426 | } |
| 5427 | break; |
| 5428 | } |
| 5429 | case '(': |
| 5430 | if (current() != '?') capture_count++; |
| 5431 | break; |
| 5432 | } |
| 5433 | } |
| 5434 | capture_count_ = capture_count; |
| 5435 | is_scanned_for_captures_ = true; |
| 5436 | } |
| 5437 | |
| 5438 | |
| 5439 | bool RegExpParser::ParseBackReferenceIndex(int* index_out) { |
| 5440 | ASSERT_EQ('\\', current()); |
| 5441 | ASSERT('1' <= Next() && Next() <= '9'); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5442 | // Try to parse a decimal literal that is no greater than the total number |
| 5443 | // of left capturing parentheses in the input. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5444 | int start = position(); |
| 5445 | int value = Next() - '0'; |
| 5446 | Advance(2); |
| 5447 | while (true) { |
| 5448 | uc32 c = current(); |
| 5449 | if (IsDecimalDigit(c)) { |
| 5450 | value = 10 * value + (c - '0'); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5451 | if (value > kMaxCaptures) { |
| 5452 | Reset(start); |
| 5453 | return false; |
| 5454 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5455 | Advance(); |
| 5456 | } else { |
| 5457 | break; |
| 5458 | } |
| 5459 | } |
| 5460 | if (value > captures_started()) { |
| 5461 | if (!is_scanned_for_captures_) { |
| 5462 | int saved_position = position(); |
| 5463 | ScanForCaptures(); |
| 5464 | Reset(saved_position); |
| 5465 | } |
| 5466 | if (value > capture_count_) { |
| 5467 | Reset(start); |
| 5468 | return false; |
| 5469 | } |
| 5470 | } |
| 5471 | *index_out = value; |
| 5472 | return true; |
| 5473 | } |
| 5474 | |
| 5475 | |
| 5476 | // QuantifierPrefix :: |
| 5477 | // { DecimalDigits } |
| 5478 | // { DecimalDigits , } |
| 5479 | // { DecimalDigits , DecimalDigits } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5480 | // |
| 5481 | // Returns true if parsing succeeds, and set the min_out and max_out |
| 5482 | // values. Values are truncated to RegExpTree::kInfinity if they overflow. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5483 | bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) { |
| 5484 | ASSERT_EQ(current(), '{'); |
| 5485 | int start = position(); |
| 5486 | Advance(); |
| 5487 | int min = 0; |
| 5488 | if (!IsDecimalDigit(current())) { |
| 5489 | Reset(start); |
| 5490 | return false; |
| 5491 | } |
| 5492 | while (IsDecimalDigit(current())) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5493 | int next = current() - '0'; |
| 5494 | if (min > (RegExpTree::kInfinity - next) / 10) { |
| 5495 | // Overflow. Skip past remaining decimal digits and return -1. |
| 5496 | do { |
| 5497 | Advance(); |
| 5498 | } while (IsDecimalDigit(current())); |
| 5499 | min = RegExpTree::kInfinity; |
| 5500 | break; |
| 5501 | } |
| 5502 | min = 10 * min + next; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5503 | Advance(); |
| 5504 | } |
| 5505 | int max = 0; |
| 5506 | if (current() == '}') { |
| 5507 | max = min; |
| 5508 | Advance(); |
| 5509 | } else if (current() == ',') { |
| 5510 | Advance(); |
| 5511 | if (current() == '}') { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5512 | max = RegExpTree::kInfinity; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5513 | Advance(); |
| 5514 | } else { |
| 5515 | while (IsDecimalDigit(current())) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5516 | int next = current() - '0'; |
| 5517 | if (max > (RegExpTree::kInfinity - next) / 10) { |
| 5518 | do { |
| 5519 | Advance(); |
| 5520 | } while (IsDecimalDigit(current())); |
| 5521 | max = RegExpTree::kInfinity; |
| 5522 | break; |
| 5523 | } |
| 5524 | max = 10 * max + next; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5525 | Advance(); |
| 5526 | } |
| 5527 | if (current() != '}') { |
| 5528 | Reset(start); |
| 5529 | return false; |
| 5530 | } |
| 5531 | Advance(); |
| 5532 | } |
| 5533 | } else { |
| 5534 | Reset(start); |
| 5535 | return false; |
| 5536 | } |
| 5537 | *min_out = min; |
| 5538 | *max_out = max; |
| 5539 | return true; |
| 5540 | } |
| 5541 | |
| 5542 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5543 | uc32 RegExpParser::ParseOctalLiteral() { |
| 5544 | ASSERT('0' <= current() && current() <= '7'); |
| 5545 | // For compatibility with some other browsers (not all), we parse |
| 5546 | // up to three octal digits with a value below 256. |
| 5547 | uc32 value = current() - '0'; |
| 5548 | Advance(); |
| 5549 | if ('0' <= current() && current() <= '7') { |
| 5550 | value = value * 8 + current() - '0'; |
| 5551 | Advance(); |
| 5552 | if (value < 32 && '0' <= current() && current() <= '7') { |
| 5553 | value = value * 8 + current() - '0'; |
| 5554 | Advance(); |
| 5555 | } |
| 5556 | } |
| 5557 | return value; |
| 5558 | } |
| 5559 | |
| 5560 | |
| 5561 | bool RegExpParser::ParseHexEscape(int length, uc32 *value) { |
| 5562 | int start = position(); |
| 5563 | uc32 val = 0; |
| 5564 | bool done = false; |
| 5565 | for (int i = 0; !done; i++) { |
| 5566 | uc32 c = current(); |
| 5567 | int d = HexValue(c); |
| 5568 | if (d < 0) { |
| 5569 | Reset(start); |
| 5570 | return false; |
| 5571 | } |
| 5572 | val = val * 16 + d; |
| 5573 | Advance(); |
| 5574 | if (i == length - 1) { |
| 5575 | done = true; |
| 5576 | } |
| 5577 | } |
| 5578 | *value = val; |
| 5579 | return true; |
| 5580 | } |
| 5581 | |
| 5582 | |
| 5583 | uc32 RegExpParser::ParseClassCharacterEscape() { |
| 5584 | ASSERT(current() == '\\'); |
| 5585 | ASSERT(has_next() && !IsSpecialClassEscape(Next())); |
| 5586 | Advance(); |
| 5587 | switch (current()) { |
| 5588 | case 'b': |
| 5589 | Advance(); |
| 5590 | return '\b'; |
| 5591 | // ControlEscape :: one of |
| 5592 | // f n r t v |
| 5593 | case 'f': |
| 5594 | Advance(); |
| 5595 | return '\f'; |
| 5596 | case 'n': |
| 5597 | Advance(); |
| 5598 | return '\n'; |
| 5599 | case 'r': |
| 5600 | Advance(); |
| 5601 | return '\r'; |
| 5602 | case 't': |
| 5603 | Advance(); |
| 5604 | return '\t'; |
| 5605 | case 'v': |
| 5606 | Advance(); |
| 5607 | return '\v'; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 5608 | case 'c': { |
| 5609 | uc32 controlLetter = Next(); |
| 5610 | uc32 letter = controlLetter & ~('A' ^ 'a'); |
| 5611 | // For compatibility with JSC, inside a character class |
| 5612 | // we also accept digits and underscore as control characters. |
| 5613 | if ((controlLetter >= '0' && controlLetter <= '9') || |
| 5614 | controlLetter == '_' || |
| 5615 | (letter >= 'A' && letter <= 'Z')) { |
| 5616 | Advance(2); |
| 5617 | // Control letters mapped to ASCII control characters in the range |
| 5618 | // 0x00-0x1f. |
| 5619 | return controlLetter & 0x1f; |
| 5620 | } |
| 5621 | // We match JSC in reading the backslash as a literal |
| 5622 | // character instead of as starting an escape. |
| 5623 | return '\\'; |
| 5624 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5625 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 5626 | case '6': case '7': |
| 5627 | // For compatibility, we interpret a decimal escape that isn't |
| 5628 | // a back reference (and therefore either \0 or not valid according |
| 5629 | // to the specification) as a 1..3 digit octal character code. |
| 5630 | return ParseOctalLiteral(); |
| 5631 | case 'x': { |
| 5632 | Advance(); |
| 5633 | uc32 value; |
| 5634 | if (ParseHexEscape(2, &value)) { |
| 5635 | return value; |
| 5636 | } |
| 5637 | // If \x is not followed by a two-digit hexadecimal, treat it |
| 5638 | // as an identity escape. |
| 5639 | return 'x'; |
| 5640 | } |
| 5641 | case 'u': { |
| 5642 | Advance(); |
| 5643 | uc32 value; |
| 5644 | if (ParseHexEscape(4, &value)) { |
| 5645 | return value; |
| 5646 | } |
| 5647 | // If \u is not followed by a four-digit hexadecimal, treat it |
| 5648 | // as an identity escape. |
| 5649 | return 'u'; |
| 5650 | } |
| 5651 | default: { |
| 5652 | // Extended identity escape. We accept any character that hasn't |
| 5653 | // been matched by a more specific case, not just the subset required |
| 5654 | // by the ECMAScript specification. |
| 5655 | uc32 result = current(); |
| 5656 | Advance(); |
| 5657 | return result; |
| 5658 | } |
| 5659 | } |
| 5660 | return 0; |
| 5661 | } |
| 5662 | |
| 5663 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5664 | CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) { |
| 5665 | ASSERT_EQ(0, *char_class); |
| 5666 | uc32 first = current(); |
| 5667 | if (first == '\\') { |
| 5668 | switch (Next()) { |
| 5669 | case 'w': case 'W': case 'd': case 'D': case 's': case 'S': { |
| 5670 | *char_class = Next(); |
| 5671 | Advance(2); |
| 5672 | return CharacterRange::Singleton(0); // Return dummy value. |
| 5673 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5674 | case kEndMarker: |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5675 | return ReportError(CStrVector("\\ at end of pattern")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5676 | default: |
| 5677 | uc32 c = ParseClassCharacterEscape(CHECK_FAILED); |
| 5678 | return CharacterRange::Singleton(c); |
| 5679 | } |
| 5680 | } else { |
| 5681 | Advance(); |
| 5682 | return CharacterRange::Singleton(first); |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5687 | static const uc16 kNoCharClass = 0; |
| 5688 | |
| 5689 | // Adds range or pre-defined character class to character ranges. |
| 5690 | // If char_class is not kInvalidClass, it's interpreted as a class |
| 5691 | // escape (i.e., 's' means whitespace, from '\s'). |
| 5692 | static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges, |
| 5693 | uc16 char_class, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5694 | CharacterRange range, |
| 5695 | Zone* zone) { |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5696 | if (char_class != kNoCharClass) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5697 | CharacterRange::AddClassEscape(char_class, ranges, zone); |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5698 | } else { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5699 | ranges->Add(range, zone); |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5700 | } |
| 5701 | } |
| 5702 | |
| 5703 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5704 | RegExpTree* RegExpParser::ParseCharacterClass() { |
| 5705 | static const char* kUnterminated = "Unterminated character class"; |
| 5706 | static const char* kRangeOutOfOrder = "Range out of order in character class"; |
| 5707 | |
| 5708 | ASSERT_EQ(current(), '['); |
| 5709 | Advance(); |
| 5710 | bool is_negated = false; |
| 5711 | if (current() == '^') { |
| 5712 | is_negated = true; |
| 5713 | Advance(); |
| 5714 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5715 | ZoneList<CharacterRange>* ranges = |
| 5716 | new(zone()) ZoneList<CharacterRange>(2, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5717 | while (has_more() && current() != ']') { |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5718 | uc16 char_class = kNoCharClass; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5719 | CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5720 | if (current() == '-') { |
| 5721 | Advance(); |
| 5722 | if (current() == kEndMarker) { |
| 5723 | // If we reach the end we break out of the loop and let the |
| 5724 | // following code report an error. |
| 5725 | break; |
| 5726 | } else if (current() == ']') { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5727 | AddRangeOrEscape(ranges, char_class, first, zone()); |
| 5728 | ranges->Add(CharacterRange::Singleton('-'), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5729 | break; |
| 5730 | } |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5731 | uc16 char_class_2 = kNoCharClass; |
| 5732 | CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED); |
| 5733 | if (char_class != kNoCharClass || char_class_2 != kNoCharClass) { |
| 5734 | // Either end is an escaped character class. Treat the '-' verbatim. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5735 | AddRangeOrEscape(ranges, char_class, first, zone()); |
| 5736 | ranges->Add(CharacterRange::Singleton('-'), zone()); |
| 5737 | AddRangeOrEscape(ranges, char_class_2, next, zone()); |
lrn@chromium.org | 14a7035 | 2010-12-15 12:43:21 +0000 | [diff] [blame] | 5738 | continue; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5739 | } |
| 5740 | if (first.from() > next.to()) { |
| 5741 | return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED); |
| 5742 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5743 | ranges->Add(CharacterRange::Range(first.from(), next.to()), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5744 | } else { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5745 | AddRangeOrEscape(ranges, char_class, first, zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5746 | } |
| 5747 | } |
| 5748 | if (!has_more()) { |
| 5749 | return ReportError(CStrVector(kUnterminated) CHECK_FAILED); |
| 5750 | } |
| 5751 | Advance(); |
| 5752 | if (ranges->length() == 0) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 5753 | ranges->Add(CharacterRange::Everything(), zone()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5754 | is_negated = !is_negated; |
| 5755 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5756 | return new(zone()) RegExpCharacterClass(ranges, is_negated); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | |
| 5760 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5761 | // The Parser interface. |
| 5762 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5763 | ParserMessage::~ParserMessage() { |
| 5764 | for (int i = 0; i < args().length(); i++) |
| 5765 | DeleteArray(args()[i]); |
| 5766 | DeleteArray(args().start()); |
| 5767 | } |
| 5768 | |
| 5769 | |
| 5770 | ScriptDataImpl::~ScriptDataImpl() { |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 5771 | if (owns_store_) store_.Dispose(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | |
| 5775 | int ScriptDataImpl::Length() { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5776 | return store_.length() * sizeof(unsigned); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5780 | const char* ScriptDataImpl::Data() { |
| 5781 | return reinterpret_cast<const char*>(store_.start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5785 | bool ScriptDataImpl::HasError() { |
| 5786 | return has_error(); |
| 5787 | } |
| 5788 | |
| 5789 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 5790 | void ScriptDataImpl::Initialize() { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 5791 | // Prepares state for use. |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5792 | if (store_.length() >= PreparseDataConstants::kHeaderSize) { |
| 5793 | function_index_ = PreparseDataConstants::kHeaderSize; |
| 5794 | int symbol_data_offset = PreparseDataConstants::kHeaderSize |
| 5795 | + store_[PreparseDataConstants::kFunctionsSizeOffset]; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 5796 | if (store_.length() > symbol_data_offset) { |
| 5797 | symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]); |
| 5798 | } else { |
| 5799 | // Partial preparse causes no symbol information. |
| 5800 | symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length()); |
| 5801 | } |
| 5802 | symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length()); |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | |
| 5807 | int ScriptDataImpl::ReadNumber(byte** source) { |
| 5808 | // Reads a number from symbol_data_ in base 128. The most significant |
| 5809 | // bit marks that there are more digits. |
| 5810 | // If the first byte is 0x80 (kNumberTerminator), it would normally |
| 5811 | // represent a leading zero. Since that is useless, and therefore won't |
| 5812 | // appear as the first digit of any actual value, it is used to |
| 5813 | // mark the end of the input stream. |
| 5814 | byte* data = *source; |
| 5815 | if (data >= symbol_data_end_) return -1; |
| 5816 | byte input = *data; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5817 | if (input == PreparseDataConstants::kNumberTerminator) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 5818 | // End of stream marker. |
| 5819 | return -1; |
| 5820 | } |
| 5821 | int result = input & 0x7f; |
| 5822 | data++; |
| 5823 | while ((input & 0x80u) != 0) { |
| 5824 | if (data >= symbol_data_end_) return -1; |
| 5825 | input = *data; |
| 5826 | result = (result << 7) | (input & 0x7f); |
| 5827 | data++; |
| 5828 | } |
| 5829 | *source = data; |
| 5830 | return result; |
| 5831 | } |
| 5832 | |
| 5833 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 5834 | // Create a Scanner for the preparser to use as input, and preparse the source. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 5835 | static ScriptDataImpl* DoPreParse(Utf16CharacterStream* source, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5836 | int flags, |
| 5837 | ParserRecorder* recorder) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5838 | Isolate* isolate = Isolate::Current(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5839 | HistogramTimerScope timer(isolate->counters()->pre_parse()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 5840 | Scanner scanner(isolate->unicode_cache()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5841 | scanner.SetHarmonyScoping(FLAG_harmony_scoping); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 5842 | scanner.Initialize(source); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5843 | intptr_t stack_limit = isolate->stack_guard()->real_climit(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5844 | preparser::PreParser::PreParseResult result = |
| 5845 | preparser::PreParser::PreParseProgram(&scanner, |
| 5846 | recorder, |
| 5847 | flags, |
| 5848 | stack_limit); |
| 5849 | if (result == preparser::PreParser::kPreParseStackOverflow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5850 | isolate->StackOverflow(); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 5851 | return NULL; |
| 5852 | } |
| 5853 | |
| 5854 | // Extract the accumulated data from the recorder as a single |
| 5855 | // contiguous vector that we are responsible for disposing. |
| 5856 | Vector<unsigned> store = recorder->ExtractData(); |
| 5857 | return new ScriptDataImpl(store); |
| 5858 | } |
| 5859 | |
| 5860 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 5861 | ScriptDataImpl* ParserApi::PreParse(Utf16CharacterStream* source, |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 5862 | v8::Extension* extension, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5863 | int flags) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5864 | Handle<Script> no_script; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5865 | if (FLAG_lazy && (extension == NULL)) { |
| 5866 | flags |= kAllowLazy; |
| 5867 | } |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 5868 | CompleteParserRecorder recorder; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5869 | return DoPreParse(source, flags, &recorder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 5873 | bool RegExpParser::ParseRegExp(FlatStringReader* input, |
| 5874 | bool multiline, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5875 | RegExpCompileData* result, |
| 5876 | Zone* zone) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5877 | ASSERT(result != NULL); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5878 | RegExpParser parser(input, &result->error, multiline, zone); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5879 | RegExpTree* tree = parser.ParsePattern(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5880 | if (parser.failed()) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5881 | ASSERT(tree == NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5882 | ASSERT(!result->error.is_null()); |
| 5883 | } else { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5884 | ASSERT(tree != NULL); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5885 | ASSERT(result->error.is_null()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5886 | result->tree = tree; |
| 5887 | int capture_count = parser.captures_started(); |
| 5888 | result->simple = tree->IsAtom() && parser.simple() && capture_count == 0; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 5889 | result->contains_anchor = parser.contains_anchor(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 5890 | result->capture_count = capture_count; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5891 | } |
| 5892 | return !parser.failed(); |
| 5893 | } |
| 5894 | |
| 5895 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5896 | bool ParserApi::Parse(CompilationInfo* info, int parsing_flags) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5897 | ASSERT(info->function() == NULL); |
| 5898 | FunctionLiteral* result = NULL; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5899 | ASSERT((parsing_flags & kLanguageModeMask) == CLASSIC_MODE); |
| 5900 | if (!info->is_native() && FLAG_harmony_scoping) { |
| 5901 | // Harmony scoping is requested. |
| 5902 | parsing_flags |= EXTENDED_MODE; |
| 5903 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 5904 | if (!info->is_native() && FLAG_harmony_modules) { |
| 5905 | parsing_flags |= kAllowModules; |
| 5906 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5907 | if (FLAG_allow_natives_syntax || info->is_native()) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 5908 | // We require %identifier(..) syntax. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5909 | parsing_flags |= kAllowNativesSyntax; |
| 5910 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5911 | if (info->is_lazy()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 5912 | ASSERT(!info->is_eval()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5913 | Parser parser(info, parsing_flags, NULL, NULL); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 5914 | if (info->shared_info()->is_function()) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5915 | result = parser.ParseLazy(); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 5916 | } else { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5917 | result = parser.ParseProgram(); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 5918 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5919 | } else { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5920 | ScriptDataImpl* pre_data = info->pre_parse_data(); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5921 | Parser parser(info, parsing_flags, info->extension(), pre_data); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5922 | if (pre_data != NULL && pre_data->has_error()) { |
| 5923 | Scanner::Location loc = pre_data->MessageLocation(); |
| 5924 | const char* message = pre_data->BuildMessage(); |
| 5925 | Vector<const char*> args = pre_data->BuildArgs(); |
| 5926 | parser.ReportMessageAt(loc, message, args); |
| 5927 | DeleteArray(message); |
| 5928 | for (int i = 0; i < args.length(); i++) { |
| 5929 | DeleteArray(args[i]); |
| 5930 | } |
| 5931 | DeleteArray(args.start()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5932 | ASSERT(info->isolate()->has_pending_exception()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5933 | } else { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 5934 | result = parser.ParseProgram(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5935 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5936 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5937 | info->SetFunction(result); |
| 5938 | return (result != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5941 | } } // namespace v8::internal |