blob: 41907d12eb18e24027590faebc4ecae412cabebd [file] [log] [blame]
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001// Copyright 2012 the V8 project authors. All rights reserved.
lrn@chromium.orgfa943b72010-11-03 08:14:36 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_PREPARSER_H
29#define V8_PREPARSER_H
30
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000031#include "hashmap.h"
ricow@chromium.org55ee8072011-09-08 16:33:10 +000032#include "token.h"
33#include "scanner.h"
34
lrn@chromium.orgfa943b72010-11-03 08:14:36 +000035namespace v8 {
ricow@chromium.org55ee8072011-09-08 16:33:10 +000036
37namespace internal {
38class UnicodeCache;
39}
40
lrn@chromium.orgfa943b72010-11-03 08:14:36 +000041namespace preparser {
42
ricow@chromium.org55ee8072011-09-08 16:33:10 +000043typedef uint8_t byte;
44
lrn@chromium.orgfa943b72010-11-03 08:14:36 +000045// Preparsing checks a JavaScript program and emits preparse-data that helps
46// a later parsing to be faster.
lrn@chromium.org1c092762011-05-09 09:42:16 +000047// See preparse-data-format.h for the data format.
lrn@chromium.orgfa943b72010-11-03 08:14:36 +000048
49// The PreParser checks that the syntax follows the grammar for JavaScript,
50// and collects some information about the program along the way.
51// The grammar check is only performed in order to understand the program
52// sufficiently to deduce some information about it, that can be used
53// to speed up later parsing. Finding errors is not the goal of pre-parsing,
54// rather it is to speed up properly written and correct programs.
55// That means that contextual checks (like a label being declared where
56// it is used) are generally omitted.
57
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000058namespace i = v8::internal;
59
ricow@chromium.org55ee8072011-09-08 16:33:10 +000060class DuplicateFinder {
61 public:
62 explicit DuplicateFinder(i::UnicodeCache* constants)
63 : unicode_constants_(constants),
64 backing_store_(16),
65 map_(&Match) { }
66
67 int AddAsciiSymbol(i::Vector<const char> key, int value);
yangguo@chromium.org154ff992012-03-13 08:09:54 +000068 int AddUtf16Symbol(i::Vector<const uint16_t> key, int value);
ricow@chromium.org55ee8072011-09-08 16:33:10 +000069 // Add a a number literal by converting it (if necessary)
70 // to the string that ToString(ToNumber(literal)) would generate.
71 // and then adding that string with AddAsciiSymbol.
72 // This string is the actual value used as key in an object literal,
73 // and the one that must be different from the other keys.
74 int AddNumber(i::Vector<const char> key, int value);
75
76 private:
77 int AddSymbol(i::Vector<const byte> key, bool is_ascii, int value);
78 // Backs up the key and its length in the backing store.
79 // The backup is stored with a base 127 encoding of the
80 // length (plus a bit saying whether the string is ASCII),
81 // followed by the bytes of the key.
82 byte* BackupKey(i::Vector<const byte> key, bool is_ascii);
83
84 // Compare two encoded keys (both pointing into the backing store)
85 // for having the same base-127 encoded lengths and ASCII-ness,
86 // and then having the same 'length' bytes following.
87 static bool Match(void* first, void* second);
88 // Creates a hash from a sequence of bytes.
89 static uint32_t Hash(i::Vector<const byte> key, bool is_ascii);
90 // Checks whether a string containing a JS number is its canonical
91 // form.
92 static bool IsNumberCanonical(i::Vector<const char> key);
93
94 // Size of buffer. Sufficient for using it to call DoubleToCString in
95 // from conversions.h.
96 static const int kBufferSize = 100;
97
98 i::UnicodeCache* unicode_constants_;
99 // Backing store used to store strings used as hashmap keys.
100 i::SequenceCollector<unsigned char> backing_store_;
101 i::HashMap map_;
102 // Buffer used for string->number->canonical string conversions.
103 char number_buffer_[kBufferSize];
104};
105
106
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000107#ifdef WIN32
108#undef Yield
109#endif
110
111
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000112class PreParser {
113 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000114 enum PreParseResult {
115 kPreParseStackOverflow,
116 kPreParseSuccess
117 };
118
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000119
120 PreParser(i::Scanner* scanner,
121 i::ParserRecorder* log,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000122 uintptr_t stack_limit)
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000123 : scanner_(scanner),
124 log_(log),
125 scope_(NULL),
126 stack_limit_(stack_limit),
127 strict_mode_violation_location_(i::Scanner::Location::invalid()),
128 strict_mode_violation_type_(NULL),
129 stack_overflow_(false),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000130 allow_lazy_(false),
131 allow_natives_syntax_(false),
132 allow_generators_(false),
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000133 allow_for_of_(false),
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000134 parenthesized_function_(false) { }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000135
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000136 ~PreParser() {}
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000137
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000138 bool allow_natives_syntax() const { return allow_natives_syntax_; }
139 bool allow_lazy() const { return allow_lazy_; }
140 bool allow_modules() const { return scanner_->HarmonyModules(); }
141 bool allow_harmony_scoping() const { return scanner_->HarmonyScoping(); }
142 bool allow_generators() const { return allow_generators_; }
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000143 bool allow_for_of() const { return allow_for_of_; }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000144
145 void set_allow_natives_syntax(bool allow) { allow_natives_syntax_ = allow; }
146 void set_allow_lazy(bool allow) { allow_lazy_ = allow; }
147 void set_allow_modules(bool allow) { scanner_->SetHarmonyModules(allow); }
148 void set_allow_harmony_scoping(bool allow) {
149 scanner_->SetHarmonyScoping(allow);
150 }
151 void set_allow_generators(bool allow) { allow_generators_ = allow; }
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000152 void set_allow_for_of(bool allow) { allow_for_of_ = allow; }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000153
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000154 // Pre-parse the program from the character stream; returns true on
155 // success (even if parsing failed, the pre-parse data successfully
156 // captured the syntax error), and false if a stack-overflow happened
157 // during parsing.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000158 PreParseResult PreParseProgram() {
159 Scope top_scope(&scope_, kTopLevelScope);
160 bool ok = true;
161 int start_position = scanner_->peek_location().beg_pos;
162 ParseSourceElements(i::Token::EOS, &ok);
163 if (stack_overflow_) return kPreParseStackOverflow;
164 if (!ok) {
165 ReportUnexpectedToken(scanner_->current_token());
166 } else if (!scope_->is_classic_mode()) {
167 CheckOctalLiteral(start_position, scanner_->location().end_pos, &ok);
168 }
169 return kPreParseSuccess;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000170 }
171
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000172 // Parses a single function literal, from the opening parentheses before
173 // parameters to the closing brace after the body.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000174 // Returns a FunctionEntry describing the body of the function in enough
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000175 // detail that it can be lazily compiled.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000176 // The scanner is expected to have matched the "function" or "function*"
177 // keyword and parameters, and have consumed the initial '{'.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000178 // At return, unless an error occurred, the scanner is positioned before the
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000179 // the final '}'.
180 PreParseResult PreParseLazyFunction(i::LanguageMode mode,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000181 bool is_generator,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000182 i::ParserRecorder* log);
183
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000184 private:
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000185 // Used to detect duplicates in object literals. Each of the values
186 // kGetterProperty, kSetterProperty and kValueProperty represents
187 // a type of object literal property. When parsing a property, its
188 // type value is stored in the DuplicateFinder for the property name.
189 // Values are chosen so that having intersection bits means the there is
190 // an incompatibility.
191 // I.e., you can add a getter to a property that already has a setter, since
192 // kGetterProperty and kSetterProperty doesn't intersect, but not if it
193 // already has a getter or a value. Adding the getter to an existing
194 // setter will store the value (kGetterProperty | kSetterProperty), which
195 // is incompatible with adding any further properties.
196 enum PropertyType {
197 kNone = 0,
198 // Bit patterns representing different object literal property types.
199 kGetterProperty = 1,
200 kSetterProperty = 2,
201 kValueProperty = 7,
202 // Helper constants.
203 kValueFlag = 4
204 };
205
206 // Checks the type of conflict based on values coming from PropertyType.
207 bool HasConflict(int type1, int type2) { return (type1 & type2) != 0; }
208 bool IsDataDataConflict(int type1, int type2) {
209 return ((type1 & type2) & kValueFlag) != 0;
210 }
211 bool IsDataAccessorConflict(int type1, int type2) {
212 return ((type1 ^ type2) & kValueFlag) != 0;
213 }
214 bool IsAccessorAccessorConflict(int type1, int type2) {
215 return ((type1 | type2) & kValueFlag) == 0;
216 }
217
218
219 void CheckDuplicate(DuplicateFinder* finder,
220 i::Token::Value property,
221 int type,
222 bool* ok);
223
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000224 // These types form an algebra over syntactic categories that is just
225 // rich enough to let us recognize and propagate the constructs that
226 // are either being counted in the preparser data, or is important
227 // to throw the correct syntax error exceptions.
228
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000229 enum ScopeType {
230 kTopLevelScope,
231 kFunctionScope
232 };
233
danno@chromium.orgb6451162011-08-17 14:33:23 +0000234 enum VariableDeclarationContext {
235 kSourceElement,
236 kStatement,
237 kForStatement
238 };
239
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000240 // If a list of variable declarations includes any initializers.
241 enum VariableDeclarationProperties {
242 kHasInitializers,
243 kHasNoInitializers
244 };
245
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000246 class Expression;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000247
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000248 class Identifier {
249 public:
250 static Identifier Default() {
251 return Identifier(kUnknownIdentifier);
252 }
253 static Identifier Eval() {
254 return Identifier(kEvalIdentifier);
255 }
256 static Identifier Arguments() {
257 return Identifier(kArgumentsIdentifier);
258 }
259 static Identifier FutureReserved() {
260 return Identifier(kFutureReservedIdentifier);
261 }
ager@chromium.org04921a82011-06-27 13:21:41 +0000262 static Identifier FutureStrictReserved() {
263 return Identifier(kFutureStrictReservedIdentifier);
264 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000265 static Identifier Yield() {
266 return Identifier(kYieldIdentifier);
267 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000268 bool IsEval() { return type_ == kEvalIdentifier; }
269 bool IsArguments() { return type_ == kArgumentsIdentifier; }
270 bool IsEvalOrArguments() { return type_ >= kEvalIdentifier; }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000271 bool IsYield() { return type_ == kYieldIdentifier; }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000272 bool IsFutureReserved() { return type_ == kFutureReservedIdentifier; }
ager@chromium.org04921a82011-06-27 13:21:41 +0000273 bool IsFutureStrictReserved() {
274 return type_ == kFutureStrictReservedIdentifier;
275 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000276 bool IsValidStrictVariable() { return type_ == kUnknownIdentifier; }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000277
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000278 private:
279 enum Type {
280 kUnknownIdentifier,
281 kFutureReservedIdentifier,
ager@chromium.org04921a82011-06-27 13:21:41 +0000282 kFutureStrictReservedIdentifier,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000283 kYieldIdentifier,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000284 kEvalIdentifier,
285 kArgumentsIdentifier
286 };
287 explicit Identifier(Type type) : type_(type) { }
288 Type type_;
289
290 friend class Expression;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000291 };
292
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000293 // Bits 0 and 1 are used to identify the type of expression:
294 // If bit 0 is set, it's an identifier.
295 // if bit 1 is set, it's a string literal.
296 // If neither is set, it's no particular type, and both set isn't
297 // use yet.
298 // Bit 2 is used to mark the expression as being parenthesized,
299 // so "(foo)" isn't recognized as a pure identifier (and possible label).
300 class Expression {
301 public:
302 static Expression Default() {
303 return Expression(kUnknownExpression);
304 }
305
306 static Expression FromIdentifier(Identifier id) {
307 return Expression(kIdentifierFlag | (id.type_ << kIdentifierShift));
308 }
309
310 static Expression StringLiteral() {
311 return Expression(kUnknownStringLiteral);
312 }
313
314 static Expression UseStrictStringLiteral() {
315 return Expression(kUseStrictString);
316 }
317
318 static Expression This() {
319 return Expression(kThisExpression);
320 }
321
322 static Expression ThisProperty() {
323 return Expression(kThisPropertyExpression);
324 }
325
326 static Expression StrictFunction() {
327 return Expression(kStrictFunctionExpression);
328 }
329
330 bool IsIdentifier() {
331 return (code_ & kIdentifierFlag) != 0;
332 }
333
334 // Only works corretly if it is actually an identifier expression.
335 PreParser::Identifier AsIdentifier() {
336 return PreParser::Identifier(
337 static_cast<PreParser::Identifier::Type>(code_ >> kIdentifierShift));
338 }
339
340 bool IsParenthesized() {
341 // If bit 0 or 1 is set, we interpret bit 2 as meaning parenthesized.
342 return (code_ & 7) > 4;
343 }
344
345 bool IsRawIdentifier() {
346 return !IsParenthesized() && IsIdentifier();
347 }
348
349 bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; }
350
351 bool IsRawStringLiteral() {
352 return !IsParenthesized() && IsStringLiteral();
353 }
354
355 bool IsUseStrictLiteral() {
356 return (code_ & kStringLiteralMask) == kUseStrictString;
357 }
358
359 bool IsThis() {
360 return code_ == kThisExpression;
361 }
362
363 bool IsThisProperty() {
364 return code_ == kThisPropertyExpression;
365 }
366
367 bool IsStrictFunction() {
368 return code_ == kStrictFunctionExpression;
369 }
370
371 Expression Parenthesize() {
372 int type = code_ & 3;
373 if (type != 0) {
374 // Identifiers and string literals can be parenthesized.
375 // They no longer work as labels or directive prologues,
376 // but are still recognized in other contexts.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000377 return Expression(code_ | kParenthesizedExpressionFlag);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000378 }
379 // For other types of expressions, it's not important to remember
380 // the parentheses.
381 return *this;
382 }
383
384 private:
385 // First two/three bits are used as flags.
386 // Bit 0 and 1 represent identifiers or strings literals, and are
387 // mutually exclusive, but can both be absent.
388 // If bit 0 or 1 are set, bit 2 marks that the expression has
389 // been wrapped in parentheses (a string literal can no longer
390 // be a directive prologue, and an identifier can no longer be
391 // a label.
392 enum {
393 kUnknownExpression = 0,
394 // Identifiers
395 kIdentifierFlag = 1, // Used to detect labels.
396 kIdentifierShift = 3,
397
398 kStringLiteralFlag = 2, // Used to detect directive prologue.
399 kUnknownStringLiteral = kStringLiteralFlag,
400 kUseStrictString = kStringLiteralFlag | 8,
401 kStringLiteralMask = kUseStrictString,
402
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000403 // Only if identifier or string literal.
404 kParenthesizedExpressionFlag = 4,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000405
406 // Below here applies if neither identifier nor string literal.
407 kThisExpression = 4,
408 kThisPropertyExpression = 8,
409 kStrictFunctionExpression = 12
410 };
411
412 explicit Expression(int expression_code) : code_(expression_code) { }
413
414 int code_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000415 };
416
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000417 class Statement {
418 public:
419 static Statement Default() {
420 return Statement(kUnknownStatement);
421 }
422
danno@chromium.org40cb8782011-05-25 07:58:50 +0000423 static Statement FunctionDeclaration() {
424 return Statement(kFunctionDeclaration);
425 }
426
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000427 // Creates expression statement from expression.
428 // Preserves being an unparenthesized string literal, possibly
429 // "use strict".
430 static Statement ExpressionStatement(Expression expression) {
431 if (!expression.IsParenthesized()) {
432 if (expression.IsUseStrictLiteral()) {
433 return Statement(kUseStrictExpressionStatement);
434 }
435 if (expression.IsStringLiteral()) {
436 return Statement(kStringLiteralExpressionStatement);
437 }
438 }
439 return Default();
440 }
441
442 bool IsStringLiteral() {
443 return code_ != kUnknownStatement;
444 }
445
446 bool IsUseStrictLiteral() {
447 return code_ == kUseStrictExpressionStatement;
448 }
449
danno@chromium.org40cb8782011-05-25 07:58:50 +0000450 bool IsFunctionDeclaration() {
451 return code_ == kFunctionDeclaration;
452 }
453
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000454 private:
455 enum Type {
456 kUnknownStatement,
457 kStringLiteralExpressionStatement,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000458 kUseStrictExpressionStatement,
459 kFunctionDeclaration
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000460 };
461
462 explicit Statement(Type code) : code_(code) {}
463 Type code_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000464 };
465
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000466 enum SourceElements {
467 kUnknownSourceElements
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000468 };
469
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000470 typedef int Arguments;
471
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000472 class Scope {
473 public:
474 Scope(Scope** variable, ScopeType type)
475 : variable_(variable),
476 prev_(*variable),
477 type_(type),
478 materialized_literal_count_(0),
479 expected_properties_(0),
lrn@chromium.org1c092762011-05-09 09:42:16 +0000480 with_nesting_count_(0),
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000481 language_mode_(
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000482 (prev_ != NULL) ? prev_->language_mode() : i::CLASSIC_MODE),
483 is_generator_(false) {
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000484 *variable = this;
485 }
486 ~Scope() { *variable_ = prev_; }
487 void NextMaterializedLiteralIndex() { materialized_literal_count_++; }
488 void AddProperty() { expected_properties_++; }
489 ScopeType type() { return type_; }
490 int expected_properties() { return expected_properties_; }
491 int materialized_literal_count() { return materialized_literal_count_; }
492 bool IsInsideWith() { return with_nesting_count_ != 0; }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000493 bool is_generator() { return is_generator_; }
494 void set_is_generator(bool is_generator) { is_generator_ = is_generator; }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000495 bool is_classic_mode() {
496 return language_mode_ == i::CLASSIC_MODE;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000497 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000498 i::LanguageMode language_mode() {
499 return language_mode_;
500 }
501 void set_language_mode(i::LanguageMode language_mode) {
502 language_mode_ = language_mode;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000503 }
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000504
505 class InsideWith {
506 public:
507 explicit InsideWith(Scope* scope) : scope_(scope) {
508 scope->with_nesting_count_++;
509 }
510
511 ~InsideWith() { scope_->with_nesting_count_--; }
512
513 private:
514 Scope* scope_;
515 DISALLOW_COPY_AND_ASSIGN(InsideWith);
516 };
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000517
518 private:
519 Scope** const variable_;
520 Scope* const prev_;
521 const ScopeType type_;
522 int materialized_literal_count_;
523 int expected_properties_;
524 int with_nesting_count_;
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000525 i::LanguageMode language_mode_;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000526 bool is_generator_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000527 };
528
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000529 // Report syntax error
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000530 void ReportUnexpectedToken(i::Token::Value token);
ricow@chromium.org55ee8072011-09-08 16:33:10 +0000531 void ReportMessageAt(i::Scanner::Location location,
532 const char* type,
533 const char* name_opt) {
534 log_->LogMessage(location.beg_pos, location.end_pos, type, name_opt);
535 }
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000536 void ReportMessageAt(int start_pos,
537 int end_pos,
538 const char* type,
539 const char* name_opt) {
540 log_->LogMessage(start_pos, end_pos, type, name_opt);
541 }
542
lrn@chromium.org1c092762011-05-09 09:42:16 +0000543 void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok);
544
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000545 // All ParseXXX functions take as the last argument an *ok parameter
546 // which is set to false if parsing failed; it is unchanged otherwise.
547 // By making the 'exception handling' explicit, we are forced to check
548 // for failure at the call sites.
danno@chromium.orgb6451162011-08-17 14:33:23 +0000549 Statement ParseSourceElement(bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000550 SourceElements ParseSourceElements(int end_token, bool* ok);
551 Statement ParseStatement(bool* ok);
552 Statement ParseFunctionDeclaration(bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000553 Statement ParseBlock(bool* ok);
danno@chromium.orgb6451162011-08-17 14:33:23 +0000554 Statement ParseVariableStatement(VariableDeclarationContext var_context,
555 bool* ok);
556 Statement ParseVariableDeclarations(VariableDeclarationContext var_context,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000557 VariableDeclarationProperties* decl_props,
danno@chromium.orgb6451162011-08-17 14:33:23 +0000558 int* num_decl,
559 bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000560 Statement ParseExpressionOrLabelledStatement(bool* ok);
561 Statement ParseIfStatement(bool* ok);
562 Statement ParseContinueStatement(bool* ok);
563 Statement ParseBreakStatement(bool* ok);
564 Statement ParseReturnStatement(bool* ok);
565 Statement ParseWithStatement(bool* ok);
566 Statement ParseSwitchStatement(bool* ok);
567 Statement ParseDoWhileStatement(bool* ok);
568 Statement ParseWhileStatement(bool* ok);
569 Statement ParseForStatement(bool* ok);
570 Statement ParseThrowStatement(bool* ok);
571 Statement ParseTryStatement(bool* ok);
572 Statement ParseDebuggerStatement(bool* ok);
573
574 Expression ParseExpression(bool accept_IN, bool* ok);
575 Expression ParseAssignmentExpression(bool accept_IN, bool* ok);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000576 Expression ParseYieldExpression(bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000577 Expression ParseConditionalExpression(bool accept_IN, bool* ok);
578 Expression ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
579 Expression ParseUnaryExpression(bool* ok);
580 Expression ParsePostfixExpression(bool* ok);
581 Expression ParseLeftHandSideExpression(bool* ok);
582 Expression ParseNewExpression(bool* ok);
583 Expression ParseMemberExpression(bool* ok);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000584 Expression ParseMemberWithNewPrefixesExpression(unsigned new_count, bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000585 Expression ParsePrimaryExpression(bool* ok);
586 Expression ParseArrayLiteral(bool* ok);
587 Expression ParseObjectLiteral(bool* ok);
588 Expression ParseRegExpLiteral(bool seen_equal, bool* ok);
589 Expression ParseV8Intrinsic(bool* ok);
590
591 Arguments ParseArguments(bool* ok);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000592 Expression ParseFunctionLiteral(bool is_generator, bool* ok);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000593 void ParseLazyFunctionLiteralBody(bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000594
595 Identifier ParseIdentifier(bool* ok);
596 Identifier ParseIdentifierName(bool* ok);
ager@chromium.org04921a82011-06-27 13:21:41 +0000597 Identifier ParseIdentifierNameOrGetOrSet(bool* is_get,
598 bool* is_set,
599 bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000600
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000601 // Logs the currently parsed literal as a symbol in the preparser data.
602 void LogSymbol();
603 // Log the currently parsed identifier.
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000604 Identifier GetIdentifierSymbol();
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000605 // Log the currently parsed string literal.
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000606 Expression GetStringSymbol();
607
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000608 i::Token::Value peek() {
609 if (stack_overflow_) return i::Token::ILLEGAL;
610 return scanner_->peek();
611 }
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000612
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000613 i::Token::Value Next() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000614 if (stack_overflow_) return i::Token::ILLEGAL;
615 {
616 int marker;
617 if (reinterpret_cast<uintptr_t>(&marker) < stack_limit_) {
618 // Further calls to peek/Next will return illegal token.
619 // The current one will still be returned. It might already
620 // have been seen using peek.
621 stack_overflow_ = true;
622 }
623 }
624 return scanner_->Next();
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000625 }
626
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000627 bool peek_any_identifier();
628
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000629 void set_language_mode(i::LanguageMode language_mode) {
630 scope_->set_language_mode(language_mode);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000631 }
632
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000633 bool is_classic_mode() {
634 return scope_->language_mode() == i::CLASSIC_MODE;
635 }
erik.corry@gmail.com6e28b562011-10-27 14:20:17 +0000636
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000637 bool is_extended_mode() {
638 return scope_->language_mode() == i::EXTENDED_MODE;
639 }
640
641 i::LanguageMode language_mode() { return scope_->language_mode(); }
lrn@chromium.org1c092762011-05-09 09:42:16 +0000642
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000643 void Consume(i::Token::Value token) { Next(); }
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000644
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000645 void Expect(i::Token::Value token, bool* ok) {
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000646 if (Next() != token) {
647 *ok = false;
648 }
649 }
650
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000651 bool Check(i::Token::Value token) {
652 i::Token::Value next = peek();
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000653 if (next == token) {
654 Consume(next);
655 return true;
656 }
657 return false;
658 }
659 void ExpectSemicolon(bool* ok);
660
danno@chromium.org41728482013-06-12 22:31:22 +0000661 bool CheckInOrOf(bool accept_OF);
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000662
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000663 static int Precedence(i::Token::Value tok, bool accept_IN);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000664
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000665 void SetStrictModeViolation(i::Scanner::Location,
666 const char* type,
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000667 bool* ok);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000668
669 void CheckDelayedStrictModeViolation(int beg_pos, int end_pos, bool* ok);
670
671 void StrictModeIdentifierViolation(i::Scanner::Location,
672 const char* eval_args_type,
673 Identifier identifier,
674 bool* ok);
675
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000676 i::Scanner* scanner_;
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000677 i::ParserRecorder* log_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000678 Scope* scope_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000679 uintptr_t stack_limit_;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000680 i::Scanner::Location strict_mode_violation_location_;
681 const char* strict_mode_violation_type_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000682 bool stack_overflow_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000683 bool allow_lazy_;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000684 bool allow_natives_syntax_;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000685 bool allow_generators_;
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000686 bool allow_for_of_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000687 bool parenthesized_function_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000688};
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000689} } // v8::preparser
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000690
691#endif // V8_PREPARSER_H