00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "v8.h"
00029
00030 #include "api.h"
00031 #include "ast.h"
00032 #include "bootstrapper.h"
00033 #include "platform.h"
00034 #include "runtime.h"
00035 #include "parser.h"
00036 #include "scopes.h"
00037
00038 namespace v8 { namespace internal {
00039
00040 class ParserFactory;
00041 class ParserLog;
00042 class TemporaryScope;
00043 template <typename T> class ZoneListWrapper;
00044
00045
00046 class Parser {
00047 public:
00048 Parser(Handle<Script> script, bool allow_natives_syntax,
00049 v8::Extension* extension, bool is_pre_parsing,
00050 ParserFactory* factory, ParserLog* log, ScriptDataImpl* pre_data);
00051 virtual ~Parser() { }
00052
00053
00054
00055 bool PreParseProgram(unibrow::CharacterStream* stream);
00056
00057 void ReportMessage(const char* message, Vector<const char*> args);
00058 virtual void ReportMessageAt(Scanner::Location loc,
00059 const char* message,
00060 Vector<const char*> args) = 0;
00061
00062
00063
00064 FunctionLiteral* ParseProgram(Handle<String> source,
00065 unibrow::CharacterStream* stream,
00066 bool in_global_context);
00067 FunctionLiteral* ParseLazy(Handle<String> source,
00068 Handle<String> name,
00069 int start_position, bool is_expression);
00070
00071 protected:
00072
00073 enum Mode {
00074 PARSE_LAZILY,
00075 PARSE_EAGERLY
00076 };
00077
00078
00079 void ReportUnexpectedToken(Token::Value token);
00080
00081 Handle<Script> script_;
00082 Scanner scanner_;
00083
00084 Scope* top_scope_;
00085 int with_nesting_level_;
00086
00087 TemporaryScope* temp_scope_;
00088 Mode mode_;
00089 List<Node*>* target_stack_;
00090 bool allow_natives_syntax_;
00091 v8::Extension* extension_;
00092 ParserFactory* factory_;
00093 ParserLog* log_;
00094 bool is_pre_parsing_;
00095 ScriptDataImpl* pre_data_;
00096
00097 bool inside_with() const { return with_nesting_level_ > 0; }
00098 ParserFactory* factory() const { return factory_; }
00099 ParserLog* log() const { return log_; }
00100 Scanner& scanner() { return scanner_; }
00101 Mode mode() const { return mode_; }
00102 ScriptDataImpl* pre_data() const { return pre_data_; }
00103
00104
00105
00106
00107
00108 void* ParseSourceElements(ZoneListWrapper<Statement>* processor,
00109 int end_token, bool* ok);
00110 Statement* ParseStatement(ZoneStringList* labels, bool* ok);
00111 Statement* ParseFunctionDeclaration(bool* ok);
00112 Statement* ParseNativeDeclaration(bool* ok);
00113 Block* ParseBlock(ZoneStringList* labels, bool* ok);
00114 Block* ParseVariableStatement(bool* ok);
00115 Block* ParseVariableDeclarations(bool accept_IN, Expression** var, bool* ok);
00116 Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels,
00117 bool* ok);
00118 IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok);
00119 Statement* ParseContinueStatement(bool* ok);
00120 Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok);
00121 Statement* ParseReturnStatement(bool* ok);
00122 Block* WithHelper(Expression* obj, ZoneStringList* labels, bool* ok);
00123 Statement* ParseWithStatement(ZoneStringList* labels, bool* ok);
00124 CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
00125 SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok);
00126 LoopStatement* ParseDoStatement(ZoneStringList* labels, bool* ok);
00127 LoopStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok);
00128 Statement* ParseForStatement(ZoneStringList* labels, bool* ok);
00129 Statement* ParseThrowStatement(bool* ok);
00130 Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
00131 TryStatement* ParseTryStatement(bool* ok);
00132 DebuggerStatement* ParseDebuggerStatement(bool* ok);
00133
00134 Expression* ParseExpression(bool accept_IN, bool* ok);
00135 Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
00136 Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
00137 Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
00138 Expression* ParseUnaryExpression(bool* ok);
00139 Expression* ParsePostfixExpression(bool* ok);
00140 Expression* ParseLeftHandSideExpression(bool* ok);
00141 Expression* ParseNewExpression(bool* ok);
00142 Expression* ParseMemberExpression(bool* ok);
00143 Expression* ParseMemberWithNewPrefixesExpression(List<int>* new_prefixes,
00144 bool* ok);
00145 Expression* ParsePrimaryExpression(bool* ok);
00146 Expression* ParseArrayLiteral(bool* ok);
00147 Expression* ParseObjectLiteral(bool* ok);
00148 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok);
00149
00150
00151 bool IsBoilerplateProperty(ObjectLiteral::Property* property);
00152
00153
00154
00155 Literal* GetBoilerplateValue(ObjectLiteral::Property* property);
00156
00157 enum FunctionLiteralType {
00158 EXPRESSION,
00159 DECLARATION,
00160 NESTED
00161 };
00162
00163 ZoneList<Expression*>* ParseArguments(bool* ok);
00164 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name,
00165 int function_token_position,
00166 FunctionLiteralType type,
00167 bool* ok);
00168
00169
00170
00171 Expression* ParseV8Intrinsic(bool* ok);
00172
00173 INLINE(Token::Value peek()) { return scanner_.peek(); }
00174 INLINE(Token::Value Next()) { return scanner_.Next(); }
00175 INLINE(void Consume(Token::Value token));
00176 void Expect(Token::Value token, bool* ok);
00177 void ExpectSemicolon(bool* ok);
00178
00179
00180 Literal* GetLiteralUndefined();
00181 Literal* GetLiteralTheHole();
00182 Literal* GetLiteralNumber(double value);
00183
00184 Handle<String> ParseIdentifier(bool* ok);
00185 Handle<String> ParseIdentifierOrGetOrSet(bool* is_get,
00186 bool* is_set,
00187 bool* ok);
00188
00189
00190 virtual VariableProxy* Declare(Handle<String> name, Variable::Mode mode,
00191 FunctionLiteral* fun,
00192 bool resolve,
00193 bool* ok) = 0;
00194
00195 bool TargetStackContainsLabel(Handle<String> label);
00196 BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok);
00197 IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok);
00198
00199 void RegisterLabelUse(Label* label, int index);
00200
00201
00202 Literal* NewNumberLiteral(double value);
00203
00204
00205 Expression* NewThrowReferenceError(Handle<String> type);
00206
00207
00208
00209
00210 Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first);
00211
00212
00213
00214 Expression* NewThrowTypeError(Handle<String> type,
00215 Handle<Object> first,
00216 Handle<Object> second);
00217
00218
00219 Expression* NewThrowError(Handle<String> constructor,
00220 Handle<String> type,
00221 Vector< Handle<Object> > arguments);
00222
00223 friend class Target;
00224 friend class TargetScope;
00225 friend class LexicalScope;
00226 friend class TemporaryScope;
00227 };
00228
00229
00230
00231
00232
00233
00234 class TemporaryScope BASE_EMBEDDED {
00235 public:
00236 explicit TemporaryScope(Parser* parser);
00237 ~TemporaryScope();
00238
00239 int NextMaterializedLiteralIndex() {
00240 int next_index =
00241 materialized_literal_count_ + JSFunction::kLiteralsPrefixSize;
00242 materialized_literal_count_++;
00243 return next_index;
00244 }
00245 int materialized_literal_count() { return materialized_literal_count_; }
00246
00247 void set_contains_array_literal() { contains_array_literal_ = true; }
00248 bool contains_array_literal() { return contains_array_literal_; }
00249
00250 void AddProperty() { expected_property_count_++; }
00251 int expected_property_count() { return expected_property_count_; }
00252 private:
00253
00254
00255 int materialized_literal_count_;
00256
00257
00258
00259
00260
00261
00262 bool contains_array_literal_;
00263
00264
00265 int expected_property_count_;
00266
00267
00268 Parser* parser_;
00269 TemporaryScope* parent_;
00270
00271 friend class Parser;
00272 };
00273
00274
00275 TemporaryScope::TemporaryScope(Parser* parser)
00276 : materialized_literal_count_(0),
00277 contains_array_literal_(false),
00278 expected_property_count_(0),
00279 parser_(parser),
00280 parent_(parser->temp_scope_) {
00281 parser->temp_scope_ = this;
00282 }
00283
00284
00285 TemporaryScope::~TemporaryScope() {
00286 parser_->temp_scope_ = parent_;
00287 }
00288
00289
00290
00291
00292 template <typename T>
00293 class ZoneListWrapper {
00294 public:
00295 ZoneListWrapper() : list_(NULL) { }
00296 explicit ZoneListWrapper(int size) : list_(new ZoneList<T*>(size)) { }
00297 void Add(T* that) { if (list_) list_->Add(that); }
00298 int length() { return list_->length(); }
00299 ZoneList<T*>* elements() { return list_; }
00300 T* at(int index) { return list_->at(index); }
00301 private:
00302 ZoneList<T*>* list_;
00303 };
00304
00305
00306
00307
00308
00309
00310 #define NEW(expr) (is_pre_parsing_ ? NULL : new expr)
00311
00312
00313 class ParserFactory BASE_EMBEDDED {
00314 public:
00315 explicit ParserFactory(bool is_pre_parsing) :
00316 is_pre_parsing_(is_pre_parsing) { }
00317
00318 virtual ~ParserFactory() { }
00319
00320 virtual Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with);
00321
00322 virtual Handle<String> LookupSymbol(const char* string, int length) {
00323 return Handle<String>();
00324 }
00325
00326 virtual Handle<String> EmptySymbol() {
00327 return Handle<String>();
00328 }
00329
00330 virtual Expression* NewProperty(Expression* obj, Expression* key, int pos) {
00331 if (obj == VariableProxySentinel::this_proxy()) {
00332 return Property::this_property();
00333 } else {
00334 return ValidLeftHandSideSentinel::instance();
00335 }
00336 }
00337
00338 virtual Expression* NewCall(Expression* expression,
00339 ZoneList<Expression*>* arguments,
00340 bool is_eval, int pos) {
00341 return Call::sentinel();
00342 }
00343
00344 virtual Statement* EmptyStatement() {
00345 return NULL;
00346 }
00347
00348 template <typename T> ZoneListWrapper<T> NewList(int size) {
00349 return is_pre_parsing_ ? ZoneListWrapper<T>() : ZoneListWrapper<T>(size);
00350 }
00351
00352 private:
00353 bool is_pre_parsing_;
00354 };
00355
00356
00357 class ParserLog BASE_EMBEDDED {
00358 public:
00359 virtual ~ParserLog() { }
00360
00361
00362
00363
00364 virtual FunctionEntry LogFunction(int start) { return FunctionEntry(); }
00365
00366 virtual void LogError() { }
00367 };
00368
00369
00370 class AstBuildingParserFactory : public ParserFactory {
00371 public:
00372 AstBuildingParserFactory() : ParserFactory(false) { }
00373
00374 virtual Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with);
00375
00376 virtual Handle<String> LookupSymbol(const char* string, int length) {
00377 return Factory::LookupSymbol(Vector<const char>(string, length));
00378 }
00379
00380 virtual Handle<String> EmptySymbol() {
00381 return Factory::empty_symbol();
00382 }
00383
00384 virtual Expression* NewProperty(Expression* obj, Expression* key, int pos) {
00385 return new Property(obj, key, pos);
00386 }
00387
00388 virtual Expression* NewCall(Expression* expression,
00389 ZoneList<Expression*>* arguments,
00390 bool is_eval, int pos) {
00391 return new Call(expression, arguments, is_eval, pos);
00392 }
00393
00394 virtual Statement* EmptyStatement() {
00395
00396
00397 static v8::internal::EmptyStatement empty;
00398 return ∅
00399 }
00400 };
00401
00402
00403 class ParserRecorder: public ParserLog {
00404 public:
00405 ParserRecorder();
00406 virtual FunctionEntry LogFunction(int start);
00407 virtual void LogError() { }
00408 virtual void LogMessage(Scanner::Location loc,
00409 const char* message,
00410 Vector<const char*> args);
00411 void WriteString(Vector<const char> str);
00412 static const char* ReadString(unsigned* start, int* chars);
00413 List<unsigned>* store() { return &store_; }
00414 private:
00415 bool has_error_;
00416 List<unsigned> store_;
00417 };
00418
00419
00420 FunctionEntry ScriptDataImpl::GetFunctionEnd(int start) {
00421 if (nth(last_entry_).start_pos() > start) {
00422
00423
00424 last_entry_ = 0;
00425 }
00426 for (int i = last_entry_; i < EntryCount(); i++) {
00427 FunctionEntry entry = nth(i);
00428 if (entry.start_pos() == start) {
00429 last_entry_ = i;
00430 return entry;
00431 }
00432 }
00433 return FunctionEntry();
00434 }
00435
00436
00437 bool ScriptDataImpl::SanityCheck() {
00438 if (store_.length() < static_cast<int>(ScriptDataImpl::kHeaderSize))
00439 return false;
00440 if (magic() != ScriptDataImpl::kMagicNumber)
00441 return false;
00442 if (version() != ScriptDataImpl::kCurrentVersion)
00443 return false;
00444 return true;
00445 }
00446
00447
00448 int ScriptDataImpl::EntryCount() {
00449 return (store_.length() - kHeaderSize) / FunctionEntry::kSize;
00450 }
00451
00452
00453 FunctionEntry ScriptDataImpl::nth(int n) {
00454 int offset = kHeaderSize + n * FunctionEntry::kSize;
00455 return FunctionEntry(Vector<unsigned>(store_.start() + offset,
00456 FunctionEntry::kSize));
00457 }
00458
00459
00460 ParserRecorder::ParserRecorder()
00461 : has_error_(false), store_(4) {
00462 Vector<unsigned> preamble = store()->AddBlock(0, ScriptDataImpl::kHeaderSize);
00463 preamble[ScriptDataImpl::kMagicOffset] = ScriptDataImpl::kMagicNumber;
00464 preamble[ScriptDataImpl::kVersionOffset] = ScriptDataImpl::kCurrentVersion;
00465 preamble[ScriptDataImpl::kHasErrorOffset] = false;
00466 }
00467
00468
00469 void ParserRecorder::WriteString(Vector<const char> str) {
00470 store()->Add(str.length());
00471 for (int i = 0; i < str.length(); i++)
00472 store()->Add(str[i]);
00473 }
00474
00475
00476 const char* ParserRecorder::ReadString(unsigned* start, int* chars) {
00477 int length = start[0];
00478 char* result = NewArray<char>(length + 1);
00479 for (int i = 0; i < length; i++)
00480 result[i] = start[i + 1];
00481 result[length] = '\0';
00482 if (chars != NULL) *chars = length;
00483 return result;
00484 }
00485
00486
00487 void ParserRecorder::LogMessage(Scanner::Location loc, const char* message,
00488 Vector<const char*> args) {
00489 if (has_error_) return;
00490 store()->Rewind(ScriptDataImpl::kHeaderSize);
00491 store()->at(ScriptDataImpl::kHasErrorOffset) = true;
00492 store()->Add(loc.beg_pos);
00493 store()->Add(loc.end_pos);
00494 store()->Add(args.length());
00495 WriteString(CStrVector(message));
00496 for (int i = 0; i < args.length(); i++)
00497 WriteString(CStrVector(args[i]));
00498 }
00499
00500
00501 Scanner::Location ScriptDataImpl::MessageLocation() {
00502 int beg_pos = Read(0);
00503 int end_pos = Read(1);
00504 return Scanner::Location(beg_pos, end_pos);
00505 }
00506
00507
00508 const char* ScriptDataImpl::BuildMessage() {
00509 unsigned* start = ReadAddress(3);
00510 return ParserRecorder::ReadString(start, NULL);
00511 }
00512
00513
00514 Vector<const char*> ScriptDataImpl::BuildArgs() {
00515 int arg_count = Read(2);
00516 const char** array = NewArray<const char*>(arg_count);
00517 int pos = ScriptDataImpl::kHeaderSize + Read(3);
00518 for (int i = 0; i < arg_count; i++) {
00519 int count = 0;
00520 array[i] = ParserRecorder::ReadString(ReadAddress(pos), &count);
00521 pos += count + 1;
00522 }
00523 return Vector<const char*>(array, arg_count);
00524 }
00525
00526
00527 unsigned ScriptDataImpl::Read(int position) {
00528 return store_[ScriptDataImpl::kHeaderSize + position];
00529 }
00530
00531
00532 unsigned* ScriptDataImpl::ReadAddress(int position) {
00533 return &store_[ScriptDataImpl::kHeaderSize + position];
00534 }
00535
00536
00537 FunctionEntry ParserRecorder::LogFunction(int start) {
00538 if (has_error_) return FunctionEntry();
00539 FunctionEntry result(store()->AddBlock(0, FunctionEntry::kSize));
00540 result.set_start_pos(start);
00541 return result;
00542 }
00543
00544
00545 class AstBuildingParser : public Parser {
00546 public:
00547 AstBuildingParser(Handle<Script> script, bool allow_natives_syntax,
00548 v8::Extension* extension, ScriptDataImpl* pre_data)
00549 : Parser(script, allow_natives_syntax, extension, false,
00550 factory(), log(), pre_data) { }
00551 virtual void ReportMessageAt(Scanner::Location loc, const char* message,
00552 Vector<const char*> args);
00553 virtual VariableProxy* Declare(Handle<String> name, Variable::Mode mode,
00554 FunctionLiteral* fun, bool resolve, bool* ok);
00555 AstBuildingParserFactory* factory() { return &factory_; }
00556 ParserLog* log() { return &log_; }
00557
00558 private:
00559 ParserLog log_;
00560 AstBuildingParserFactory factory_;
00561 };
00562
00563
00564 class PreParser : public Parser {
00565 public:
00566 PreParser(Handle<Script> script, bool allow_natives_syntax,
00567 v8::Extension* extension)
00568 : Parser(script, allow_natives_syntax, extension, true,
00569 factory(), recorder(), NULL)
00570 , factory_(true) { }
00571 virtual void ReportMessageAt(Scanner::Location loc, const char* message,
00572 Vector<const char*> args);
00573 virtual VariableProxy* Declare(Handle<String> name, Variable::Mode mode,
00574 FunctionLiteral* fun, bool resolve, bool* ok);
00575 ParserFactory* factory() { return &factory_; }
00576 ParserRecorder* recorder() { return &recorder_; }
00577
00578 private:
00579 ParserRecorder recorder_;
00580 ParserFactory factory_;
00581 };
00582
00583
00584 Scope* AstBuildingParserFactory::NewScope(Scope* parent, Scope::Type type,
00585 bool inside_with) {
00586 Scope* result = new Scope(parent, type);
00587 result->Initialize(inside_with);
00588 return result;
00589 }
00590
00591
00592 Scope* ParserFactory::NewScope(Scope* parent, Scope::Type type,
00593 bool inside_with) {
00594 ASSERT(parent != NULL);
00595 parent->type_ = type;
00596 return parent;
00597 }
00598
00599
00600 VariableProxy* PreParser::Declare(Handle<String> name, Variable::Mode mode,
00601 FunctionLiteral* fun, bool resolve,
00602 bool* ok) {
00603 return NULL;
00604 }
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614 class Target BASE_EMBEDDED {
00615 public:
00616 Target(Parser* parser, Node* node) : parser_(parser) {
00617 parser_->target_stack_->Add(node);
00618 }
00619
00620 ~Target() {
00621 parser_->target_stack_->RemoveLast();
00622 }
00623
00624 private:
00625 Parser* parser_;
00626 };
00627
00628
00629 class TargetScope BASE_EMBEDDED {
00630 public:
00631 explicit TargetScope(Parser* parser)
00632 : parser_(parser), previous_(parser->target_stack_), stack_(0) {
00633 parser_->target_stack_ = &stack_;
00634 }
00635
00636 ~TargetScope() {
00637 ASSERT(stack_.is_empty());
00638 parser_->target_stack_ = previous_;
00639 }
00640
00641 private:
00642 Parser* parser_;
00643 List<Node*>* previous_;
00644 List<Node*> stack_;
00645 };
00646
00647
00648
00649
00650
00651
00652
00653 class LexicalScope BASE_EMBEDDED {
00654 public:
00655 LexicalScope(Parser* parser, Scope* scope)
00656 : parser_(parser),
00657 prev_scope_(parser->top_scope_),
00658 prev_level_(parser->with_nesting_level_) {
00659 parser_->top_scope_ = scope;
00660 parser_->with_nesting_level_ = 0;
00661 }
00662
00663 ~LexicalScope() {
00664 parser_->top_scope_ = prev_scope_;
00665 parser_->with_nesting_level_ = prev_level_;
00666 }
00667
00668 private:
00669 Parser* parser_;
00670 Scope* prev_scope_;
00671 int prev_level_;
00672 };
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683 #define CHECK_OK ok); \
00684 if (!*ok) return NULL; \
00685 ((void)0
00686 #define DUMMY ) // to make indentation work
00687 #undef DUMMY
00688
00689
00690
00691
00692
00693 Parser::Parser(Handle<Script> script,
00694 bool allow_natives_syntax,
00695 v8::Extension* extension,
00696 bool is_pre_parsing,
00697 ParserFactory* factory,
00698 ParserLog* log,
00699 ScriptDataImpl* pre_data)
00700 : script_(script),
00701 scanner_(is_pre_parsing),
00702 top_scope_(NULL),
00703 with_nesting_level_(0),
00704 temp_scope_(NULL),
00705 target_stack_(NULL),
00706 allow_natives_syntax_(allow_natives_syntax),
00707 extension_(extension),
00708 factory_(factory),
00709 log_(log),
00710 is_pre_parsing_(is_pre_parsing),
00711 pre_data_(pre_data) {
00712 }
00713
00714
00715 bool Parser::PreParseProgram(unibrow::CharacterStream* stream) {
00716 StatsRateScope timer(&Counters::pre_parse);
00717 StackGuard guard;
00718 AssertNoZoneAllocation assert_no_zone_allocation;
00719 AssertNoAllocation assert_no_allocation;
00720 NoHandleAllocation no_handle_allocation;
00721 scanner_.Init(Handle<String>(), stream, 0);
00722 ASSERT(target_stack_ == NULL);
00723 mode_ = PARSE_EAGERLY;
00724 DummyScope top_scope;
00725 LexicalScope scope(this, &top_scope);
00726 TemporaryScope temp_scope(this);
00727 ZoneListWrapper<Statement> processor;
00728 bool ok = true;
00729 ParseSourceElements(&processor, Token::EOS, &ok);
00730 return !scanner().stack_overflow();
00731 }
00732
00733
00734 FunctionLiteral* Parser::ParseProgram(Handle<String> source,
00735 unibrow::CharacterStream* stream,
00736 bool in_global_context) {
00737 ZoneScope zone_scope(DONT_DELETE_ON_EXIT);
00738
00739 StatsRateScope timer(&Counters::parse);
00740 Counters::total_parse_size.Increment(source->length());
00741
00742
00743 source->TryFlatten();
00744 scanner_.Init(source, stream, 0);
00745 ASSERT(target_stack_ == NULL);
00746
00747
00748 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY;
00749 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY;
00750
00751 Scope::Type type =
00752 in_global_context
00753 ? Scope::GLOBAL_SCOPE
00754 : Scope::EVAL_SCOPE;
00755 Handle<String> no_name = factory()->EmptySymbol();
00756
00757 FunctionLiteral* result = NULL;
00758 { Scope* scope = factory()->NewScope(top_scope_, type, inside_with());
00759 LexicalScope lexical_scope(this, scope);
00760 TemporaryScope temp_scope(this);
00761 ZoneListWrapper<Statement> body(16);
00762 bool ok = true;
00763 ParseSourceElements(&body, Token::EOS, &ok);
00764 if (ok) {
00765 result = NEW(FunctionLiteral(no_name, top_scope_,
00766 body.elements(),
00767 temp_scope.materialized_literal_count(),
00768 temp_scope.contains_array_literal(),
00769 temp_scope.expected_property_count(),
00770 0, 0, source->length(), false));
00771 } else if (scanner().stack_overflow()) {
00772 Top::StackOverflow();
00773 }
00774 }
00775
00776
00777 ASSERT(target_stack_ == NULL);
00778
00779
00780
00781 if (result == NULL) zone_scope.DeleteOnExit();
00782 return result;
00783 }
00784
00785
00786 FunctionLiteral* Parser::ParseLazy(Handle<String> source,
00787 Handle<String> name,
00788 int start_position,
00789 bool is_expression) {
00790 ZoneScope zone_scope(DONT_DELETE_ON_EXIT);
00791 StatsRateScope timer(&Counters::parse_lazy);
00792 Counters::total_parse_size.Increment(source->length());
00793 SafeStringInputBuffer buffer(source.location());
00794
00795
00796 source->TryFlatten();
00797 scanner_.Init(source, &buffer, start_position);
00798 ASSERT(target_stack_ == NULL);
00799 mode_ = PARSE_EAGERLY;
00800
00801
00802 FunctionLiteral* result = NULL;
00803
00804 {
00805
00806 Handle<String> no_name = factory()->EmptySymbol();
00807 Scope* scope =
00808 factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with());
00809 LexicalScope lexical_scope(this, scope);
00810 TemporaryScope temp_scope(this);
00811
00812 FunctionLiteralType type = is_expression ? EXPRESSION : DECLARATION;
00813 bool ok = true;
00814 result = ParseFunctionLiteral(name, RelocInfo::kNoPosition, type, &ok);
00815
00816 ASSERT(ok == (result != NULL));
00817
00818 ASSERT(ok || scanner_.stack_overflow());
00819 }
00820
00821
00822 ASSERT(target_stack_ == NULL);
00823
00824
00825
00826 if (result == NULL) {
00827 Top::StackOverflow();
00828 zone_scope.DeleteOnExit();
00829 }
00830 return result;
00831 }
00832
00833
00834 void Parser::ReportMessage(const char* type, Vector<const char*> args) {
00835 Scanner::Location source_location = scanner_.location();
00836 ReportMessageAt(source_location, type, args);
00837 }
00838
00839
00840 void AstBuildingParser::ReportMessageAt(Scanner::Location source_location,
00841 const char* type,
00842 Vector<const char*> args) {
00843 MessageLocation location(script_,
00844 source_location.beg_pos, source_location.end_pos);
00845 Handle<JSArray> array = Factory::NewJSArray(args.length());
00846 for (int i = 0; i < args.length(); i++) {
00847 SetElement(array, i, Factory::NewStringFromUtf8(CStrVector(args[i])));
00848 }
00849 Handle<Object> result = Factory::NewSyntaxError(type, array);
00850 Top::Throw(*result, &location);
00851 }
00852
00853
00854 void PreParser::ReportMessageAt(Scanner::Location source_location,
00855 const char* type,
00856 Vector<const char*> args) {
00857 recorder()->LogMessage(source_location, type, args);
00858 }
00859
00860
00861 void* Parser::ParseSourceElements(ZoneListWrapper<Statement>* processor,
00862 int end_token,
00863 bool* ok) {
00864
00865
00866
00867
00868
00869
00870
00871 TargetScope scope(this);
00872
00873 ASSERT(processor != NULL);
00874 while (peek() != end_token) {
00875 Statement* stat = ParseStatement(NULL, CHECK_OK);
00876 if (stat && !stat->IsEmpty()) processor->Add(stat);
00877 }
00878 return 0;
00879 }
00880
00881
00882 Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908 int statement_pos = scanner().peek_location().beg_pos;
00909 Statement* stmt = NULL;
00910 switch (peek()) {
00911 case Token::LBRACE:
00912 return ParseBlock(labels, ok);
00913
00914 case Token::CONST:
00915 case Token::VAR:
00916 stmt = ParseVariableStatement(ok);
00917 break;
00918
00919 case Token::SEMICOLON:
00920 Next();
00921 return factory()->EmptyStatement();
00922
00923 case Token::IF:
00924 stmt = ParseIfStatement(labels, ok);
00925 break;
00926
00927 case Token::DO:
00928 stmt = ParseDoStatement(labels, ok);
00929 break;
00930
00931 case Token::WHILE:
00932 stmt = ParseWhileStatement(labels, ok);
00933 break;
00934
00935 case Token::FOR:
00936 stmt = ParseForStatement(labels, ok);
00937 break;
00938
00939 case Token::CONTINUE:
00940 stmt = ParseContinueStatement(ok);
00941 break;
00942
00943 case Token::BREAK:
00944 stmt = ParseBreakStatement(labels, ok);
00945 break;
00946
00947 case Token::RETURN:
00948 stmt = ParseReturnStatement(ok);
00949 break;
00950
00951 case Token::WITH:
00952 stmt = ParseWithStatement(labels, ok);
00953 break;
00954
00955 case Token::SWITCH:
00956 stmt = ParseSwitchStatement(labels, ok);
00957 break;
00958
00959 case Token::THROW:
00960 stmt = ParseThrowStatement(ok);
00961 break;
00962
00963 case Token::TRY: {
00964
00965
00966
00967
00968
00969 Block* result = NEW(Block(labels, 1, false));
00970 Target target(this, result);
00971 TryStatement* statement = ParseTryStatement(CHECK_OK);
00972 if (result) result->AddStatement(statement);
00973 return result;
00974 }
00975
00976 case Token::FUNCTION:
00977 return ParseFunctionDeclaration(ok);
00978
00979 case Token::NATIVE:
00980 return ParseNativeDeclaration(ok);
00981
00982 case Token::DEBUGGER:
00983 stmt = ParseDebuggerStatement(ok);
00984 break;
00985
00986 default:
00987 stmt = ParseExpressionOrLabelledStatement(labels, ok);
00988 }
00989
00990
00991 if (stmt != NULL) stmt->set_statement_pos(statement_pos);
00992 return stmt;
00993 }
00994
00995
00996 VariableProxy* AstBuildingParser::Declare(Handle<String> name,
00997 Variable::Mode mode,
00998 FunctionLiteral* fun,
00999 bool resolve,
01000 bool* ok) {
01001 Variable* var = NULL;
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012 if (top_scope_->is_function_scope()) {
01013
01014 var = top_scope_->Lookup(name);
01015 if (var == NULL) {
01016
01017 var = top_scope_->Declare(name, mode);
01018 } else {
01019
01020
01021
01022
01023 if ((mode == Variable::CONST) || (var->mode() == Variable::CONST)) {
01024
01025 ASSERT(var->mode() == Variable::VAR ||
01026 var->mode() == Variable::CONST);
01027 const char* type = (var->mode() == Variable::VAR) ? "var" : "const";
01028 Handle<String> type_string =
01029 Factory::NewStringFromUtf8(CStrVector(type), TENURED);
01030 Expression* expression =
01031 NewThrowTypeError(Factory::redeclaration_symbol(),
01032 type_string, name);
01033 top_scope_->SetIllegalRedeclaration(expression);
01034 }
01035 }
01036 }
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054 VariableProxy* proxy = top_scope_->NewUnresolved(name, inside_with());
01055 top_scope_->AddDeclaration(NEW(Declaration(proxy, mode, fun)));
01056
01057
01058 if (mode == Variable::CONST && top_scope_->is_global_scope()) {
01059 ASSERT(resolve);
01060 var = NEW(Variable(top_scope_, name, Variable::CONST, true, false));
01061 }
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087 if (resolve && var != NULL) proxy->BindTo(var);
01088
01089 return proxy;
01090 }
01091
01092
01093
01094
01095
01096
01097 Statement* Parser::ParseNativeDeclaration(bool* ok) {
01098 if (extension_ == NULL) {
01099 ReportUnexpectedToken(Token::NATIVE);
01100 *ok = false;
01101 return NULL;
01102 }
01103
01104 Expect(Token::NATIVE, CHECK_OK);
01105 Expect(Token::FUNCTION, CHECK_OK);
01106 Handle<String> name = ParseIdentifier(CHECK_OK);
01107 Expect(Token::LPAREN, CHECK_OK);
01108 bool done = (peek() == Token::RPAREN);
01109 while (!done) {
01110 ParseIdentifier(CHECK_OK);
01111 done = (peek() == Token::RPAREN);
01112 if (!done) Expect(Token::COMMA, CHECK_OK);
01113 }
01114 Expect(Token::RPAREN, CHECK_OK);
01115 Expect(Token::SEMICOLON, CHECK_OK);
01116
01117 if (is_pre_parsing_) return NULL;
01118
01119
01120
01121
01122
01123 top_scope_->ForceEagerCompilation();
01124
01125
01126 v8::Handle<v8::FunctionTemplate> fun_template =
01127 extension_->GetNativeFunction(v8::Utils::ToLocal(name));
01128 ASSERT(!fun_template.IsEmpty());
01129
01130
01131 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
01132 const int literals = fun->NumberOfLiterals();
01133 Handle<Code> code = Handle<Code>(fun->shared()->code());
01134 Handle<JSFunction> boilerplate =
01135 Factory::NewFunctionBoilerplate(name, literals, false, code);
01136
01137
01138
01139
01140 boilerplate->shared()->set_function_data(fun->shared()->function_data());
01141 int parameters = fun->shared()->formal_parameter_count();
01142 boilerplate->shared()->set_formal_parameter_count(parameters);
01143
01144
01145
01146
01147 FunctionBoilerplateLiteral* lit =
01148 NEW(FunctionBoilerplateLiteral(boilerplate));
01149 VariableProxy* var = Declare(name, Variable::VAR, NULL, true, CHECK_OK);
01150 return NEW(ExpressionStatement(
01151 new Assignment(Token::INIT_VAR, var, lit, RelocInfo::kNoPosition)));
01152 }
01153
01154
01155 Statement* Parser::ParseFunctionDeclaration(bool* ok) {
01156
01157
01158
01159
01160
01161 Expect(Token::FUNCTION, CHECK_OK);
01162 int function_token_position = scanner().location().beg_pos;
01163
01164 Handle<String> name;
01165 if (peek() == Token::IDENTIFIER) name = ParseIdentifier(CHECK_OK);
01166 FunctionLiteral* fun = ParseFunctionLiteral(name, function_token_position,
01167 DECLARATION, CHECK_OK);
01168
01169 if (name.is_null()) {
01170
01171
01172 return NEW(ExpressionStatement(fun));
01173 } else {
01174
01175
01176
01177
01178 Declare(name, Variable::VAR, fun, true, CHECK_OK);
01179 return factory()->EmptyStatement();
01180 }
01181 }
01182
01183
01184 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) {
01185
01186
01187
01188
01189
01190
01191
01192 Block* result = NEW(Block(labels, 16, false));
01193 Target target(this, result);
01194 Expect(Token::LBRACE, CHECK_OK);
01195 while (peek() != Token::RBRACE) {
01196 Statement* stat = ParseStatement(NULL, CHECK_OK);
01197 if (stat && !stat->IsEmpty()) result->AddStatement(stat);
01198 }
01199 Expect(Token::RBRACE, CHECK_OK);
01200 return result;
01201 }
01202
01203
01204 Block* Parser::ParseVariableStatement(bool* ok) {
01205
01206
01207
01208 Expression* dummy;
01209 Block* result = ParseVariableDeclarations(true, &dummy, CHECK_OK);
01210 ExpectSemicolon(CHECK_OK);
01211 return result;
01212 }
01213
01214
01215
01216
01217
01218
01219
01220 Block* Parser::ParseVariableDeclarations(bool accept_IN,
01221 Expression** var,
01222 bool* ok) {
01223
01224
01225
01226 Variable::Mode mode = Variable::VAR;
01227 bool is_const = false;
01228 if (peek() == Token::VAR) {
01229 Consume(Token::VAR);
01230 } else if (peek() == Token::CONST) {
01231 Consume(Token::CONST);
01232 mode = Variable::CONST;
01233 is_const = true;
01234 } else {
01235 UNREACHABLE();
01236 }
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251 Block* block = NEW(Block(NULL, 1, true));
01252 VariableProxy* last_var = NULL;
01253 int nvars = 0;
01254 do {
01255
01256 if (nvars > 0) Consume(Token::COMMA);
01257 Handle<String> name = ParseIdentifier(CHECK_OK);
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271 last_var = Declare(name, mode, NULL,
01272 is_const ,
01273 CHECK_OK);
01274 nvars++;
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303 Expression* value = NULL;
01304 int position = -1;
01305 if (peek() == Token::ASSIGN) {
01306 Expect(Token::ASSIGN, CHECK_OK);
01307 position = scanner().location().beg_pos;
01308 value = ParseAssignmentExpression(accept_IN, CHECK_OK);
01309 }
01310
01311
01312
01313 if (value == NULL && is_const) {
01314 value = GetLiteralUndefined();
01315 }
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337 if (!is_pre_parsing_ && top_scope_->is_global_scope()) {
01338
01339 ZoneList<Expression*>* arguments = new ZoneList<Expression*>(2);
01340
01341
01342
01343
01344 arguments->Add(NEW(Literal(name)));
01345 if (is_const || (value != NULL && !inside_with())) {
01346 arguments->Add(value);
01347 value = NULL;
01348 }
01349
01350
01351
01352
01353 CallRuntime* initialize;
01354 if (is_const) {
01355 initialize =
01356 NEW(CallRuntime(
01357 Factory::InitializeConstGlobal_symbol(),
01358 Runtime::FunctionForId(Runtime::kInitializeConstGlobal),
01359 arguments));
01360 } else {
01361 initialize =
01362 NEW(CallRuntime(
01363 Factory::InitializeVarGlobal_symbol(),
01364 Runtime::FunctionForId(Runtime::kInitializeVarGlobal),
01365 arguments));
01366 }
01367 block->AddStatement(NEW(ExpressionStatement(initialize)));
01368 }
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380 if (value != NULL) {
01381 Token::Value op = (is_const ? Token::INIT_CONST : Token::INIT_VAR);
01382 Assignment* assignment = NEW(Assignment(op, last_var, value, position));
01383 if (block) block->AddStatement(NEW(ExpressionStatement(assignment)));
01384 }
01385 } while (peek() == Token::COMMA);
01386
01387 if (!is_const && nvars == 1) {
01388
01389 if (is_pre_parsing_) {
01390
01391
01392 *var = ValidLeftHandSideSentinel::instance();
01393 } else {
01394 ASSERT(last_var != NULL);
01395 *var = last_var;
01396 }
01397 }
01398
01399 return block;
01400 }
01401
01402
01403 static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) {
01404 ASSERT(!label.is_null());
01405 if (labels != NULL)
01406 for (int i = labels->length(); i-- > 0; )
01407 if (labels->at(i).is_identical_to(label))
01408 return true;
01409
01410 return false;
01411 }
01412
01413
01414 Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels,
01415 bool* ok) {
01416
01417
01418
01419
01420 Expression* expr = ParseExpression(true, CHECK_OK);
01421 if (peek() == Token::COLON && expr &&
01422 expr->AsVariableProxy() != NULL &&
01423 !expr->AsVariableProxy()->is_this()) {
01424 VariableProxy* var = expr->AsVariableProxy();
01425 Handle<String> label = var->name();
01426
01427
01428
01429
01430
01431 if (!is_pre_parsing_) {
01432 if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) {
01433 SmartPointer<char> c_string = label->ToCString(DISALLOW_NULLS);
01434 const char* elms[2] = { "Label", *c_string };
01435 Vector<const char*> args(elms, 2);
01436 ReportMessage("redeclaration", args);
01437 *ok = false;
01438 return NULL;
01439 }
01440 if (labels == NULL) labels = new ZoneStringList(4);
01441 labels->Add(label);
01442
01443
01444
01445 top_scope_->RemoveUnresolved(var);
01446 }
01447 Expect(Token::COLON, CHECK_OK);
01448 return ParseStatement(labels, ok);
01449 }
01450
01451
01452 ExpectSemicolon(CHECK_OK);
01453 return NEW(ExpressionStatement(expr));
01454 }
01455
01456
01457 IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) {
01458
01459
01460
01461 Expect(Token::IF, CHECK_OK);
01462 Expect(Token::LPAREN, CHECK_OK);
01463 Expression* condition = ParseExpression(true, CHECK_OK);
01464 Expect(Token::RPAREN, CHECK_OK);
01465 Statement* then_statement = ParseStatement(labels, CHECK_OK);
01466 Statement* else_statement = NULL;
01467 if (peek() == Token::ELSE) {
01468 Next();
01469 else_statement = ParseStatement(labels, CHECK_OK);
01470 } else if (!is_pre_parsing_) {
01471 else_statement = factory()->EmptyStatement();
01472 }
01473 return NEW(IfStatement(condition, then_statement, else_statement));
01474 }
01475
01476
01477 Statement* Parser::ParseContinueStatement(bool* ok) {
01478
01479
01480
01481 Expect(Token::CONTINUE, CHECK_OK);
01482 Handle<String> label(static_cast<String**>(NULL));
01483 Token::Value tok = peek();
01484 if (!scanner_.has_line_terminator_before_next() &&
01485 tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
01486 label = ParseIdentifier(CHECK_OK);
01487 }
01488 IterationStatement* target = NULL;
01489 if (!is_pre_parsing_) {
01490 target = LookupContinueTarget(label, CHECK_OK);
01491 if (target == NULL) {
01492
01493
01494 Handle<String> error_type = Factory::illegal_continue_symbol();
01495 if (!label.is_null()) error_type = Factory::unknown_label_symbol();
01496 Expression* throw_error = NewThrowSyntaxError(error_type, label);
01497 return NEW(ExpressionStatement(throw_error));
01498 }
01499 }
01500 ExpectSemicolon(CHECK_OK);
01501 return NEW(ContinueStatement(target));
01502 }
01503
01504
01505 Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) {
01506
01507
01508
01509 Expect(Token::BREAK, CHECK_OK);
01510 Handle<String> label;
01511 Token::Value tok = peek();
01512 if (!scanner_.has_line_terminator_before_next() &&
01513 tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
01514 label = ParseIdentifier(CHECK_OK);
01515 }
01516
01517
01518 if (!label.is_null() && ContainsLabel(labels, label)) {
01519 return factory()->EmptyStatement();
01520 }
01521 BreakableStatement* target = NULL;
01522 if (!is_pre_parsing_) {
01523 target = LookupBreakTarget(label, CHECK_OK);
01524 if (target == NULL) {
01525
01526
01527 Handle<String> error_type = Factory::illegal_break_symbol();
01528 if (!label.is_null()) error_type = Factory::unknown_label_symbol();
01529 Expression* throw_error = NewThrowSyntaxError(error_type, label);
01530 return NEW(ExpressionStatement(throw_error));
01531 }
01532 }
01533 ExpectSemicolon(CHECK_OK);
01534 return NEW(BreakStatement(target));
01535 }
01536
01537
01538 Statement* Parser::ParseReturnStatement(bool* ok) {
01539
01540
01541
01542
01543
01544
01545 Expect(Token::RETURN, CHECK_OK);
01546
01547
01548
01549
01550
01551
01552 if (!is_pre_parsing_ && !top_scope_->is_function_scope()) {
01553 Handle<String> type = Factory::illegal_return_symbol();
01554 Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null());
01555 return NEW(ExpressionStatement(throw_error));
01556 }
01557
01558 Token::Value tok = peek();
01559 if (scanner_.has_line_terminator_before_next() ||
01560 tok == Token::SEMICOLON ||
01561 tok == Token::RBRACE ||
01562 tok == Token::EOS) {
01563 ExpectSemicolon(CHECK_OK);
01564 return NEW(ReturnStatement(GetLiteralUndefined()));
01565 }
01566
01567 Expression* expr = ParseExpression(true, CHECK_OK);
01568 ExpectSemicolon(CHECK_OK);
01569 return NEW(ReturnStatement(expr));
01570 }
01571
01572
01573 Block* Parser::WithHelper(Expression* obj, ZoneStringList* labels, bool* ok) {
01574
01575 ZoneList<Label*>* label_list = NEW(ZoneList<Label*>(0));
01576 LabelCollector collector(label_list);
01577 Statement* stat;
01578 { Target target(this, &collector);
01579 with_nesting_level_++;
01580 top_scope_->RecordWithStatement();
01581 stat = ParseStatement(labels, CHECK_OK);
01582 with_nesting_level_--;
01583 }
01584
01585
01586
01587 Block* result = NEW(Block(NULL, 2, false));
01588
01589 if (result) {
01590 result->AddStatement(NEW(WithEnterStatement(obj)));
01591
01592
01593 Block* body = NEW(Block(NULL, 1, false));
01594 body->AddStatement(stat);
01595
01596
01597 Block* exit = NEW(Block(NULL, 1, false));
01598 exit->AddStatement(NEW(WithExitStatement()));
01599
01600
01601 TryFinally* wrapper = NEW(TryFinally(body, exit));
01602 wrapper->set_escaping_labels(collector.labels());
01603 result->AddStatement(wrapper);
01604 return result;
01605 } else {
01606 return NULL;
01607 }
01608 }
01609
01610
01611 Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) {
01612
01613
01614
01615
01616
01617
01618
01619 ASSERT(!Bootstrapper::IsActive());
01620
01621 Expect(Token::WITH, CHECK_OK);
01622 Expect(Token::LPAREN, CHECK_OK);
01623 Expression* expr = ParseExpression(true, CHECK_OK);
01624 Expect(Token::RPAREN, CHECK_OK);
01625
01626 return WithHelper(expr, labels, CHECK_OK);
01627 }
01628
01629
01630 CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) {
01631
01632
01633
01634
01635 Expression* label = NULL;
01636 if (peek() == Token::CASE) {
01637 Expect(Token::CASE, CHECK_OK);
01638 label = ParseExpression(true, CHECK_OK);
01639 } else {
01640 Expect(Token::DEFAULT, CHECK_OK);
01641 if (*default_seen_ptr) {
01642 ReportMessage("multiple_defaults_in_switch",
01643 Vector<const char*>::empty());
01644 *ok = false;
01645 return NULL;
01646 }
01647 *default_seen_ptr = true;
01648 }
01649 Expect(Token::COLON, CHECK_OK);
01650
01651 ZoneListWrapper<Statement> statements = factory()->NewList<Statement>(5);
01652 while (peek() != Token::CASE &&
01653 peek() != Token::DEFAULT &&
01654 peek() != Token::RBRACE) {
01655 Statement* stat = ParseStatement(NULL, CHECK_OK);
01656 statements.Add(stat);
01657 }
01658
01659 return NEW(CaseClause(label, statements.elements()));
01660 }
01661
01662
01663 SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels,
01664 bool* ok) {
01665
01666
01667
01668 SwitchStatement* statement = NEW(SwitchStatement(labels));
01669 Target target(this, statement);
01670
01671 Expect(Token::SWITCH, CHECK_OK);
01672 Expect(Token::LPAREN, CHECK_OK);
01673 Expression* tag = ParseExpression(true, CHECK_OK);
01674 Expect(Token::RPAREN, CHECK_OK);
01675
01676 bool default_seen = false;
01677 ZoneListWrapper<CaseClause> cases = factory()->NewList<CaseClause>(4);
01678 Expect(Token::LBRACE, CHECK_OK);
01679 while (peek() != Token::RBRACE) {
01680 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
01681 cases.Add(clause);
01682 }
01683 Expect(Token::RBRACE, CHECK_OK);
01684
01685 if (statement) statement->Initialize(tag, cases.elements());
01686 return statement;
01687 }
01688
01689
01690 Statement* Parser::ParseThrowStatement(bool* ok) {
01691
01692
01693
01694 Expect(Token::THROW, CHECK_OK);
01695 int pos = scanner().location().beg_pos;
01696 if (scanner_.has_line_terminator_before_next()) {
01697 ReportMessage("newline_after_throw", Vector<const char*>::empty());
01698 *ok = false;
01699 return NULL;
01700 }
01701 Expression* exception = ParseExpression(true, CHECK_OK);
01702 ExpectSemicolon(CHECK_OK);
01703
01704 return NEW(ExpressionStatement(new Throw(exception, pos)));
01705 }
01706
01707
01708 Expression* Parser::MakeCatchContext(Handle<String> id, VariableProxy* value) {
01709 ZoneListWrapper<ObjectLiteral::Property> properties =
01710 factory()->NewList<ObjectLiteral::Property>(1);
01711 Literal* key = NEW(Literal(id));
01712 ObjectLiteral::Property* property = NEW(ObjectLiteral::Property(key, value));
01713 properties.Add(property);
01714
01715
01716
01717 int literal_index = temp_scope_->NextMaterializedLiteralIndex();
01718 if (is_pre_parsing_) {
01719 return NULL;
01720 }
01721
01722
01723
01724 Handle<FixedArray> constant_properties = Factory::empty_fixed_array();
01725 ZoneList<Expression*>* arguments = new ZoneList<Expression*>(1);
01726 arguments->Add(new Literal(constant_properties));
01727
01728 return new ObjectLiteral(constant_properties,
01729 properties.elements(),
01730 literal_index);
01731 }
01732
01733
01734 TryStatement* Parser::ParseTryStatement(bool* ok) {
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746 Expect(Token::TRY, CHECK_OK);
01747
01748 ZoneList<Label*>* label_list = NEW(ZoneList<Label*>(0));
01749 LabelCollector collector(label_list);
01750 Block* try_block;
01751
01752 { Target target(this, &collector);
01753 try_block = ParseBlock(NULL, CHECK_OK);
01754 }
01755
01756 Block* catch_block = NULL;
01757 VariableProxy* catch_var = NULL;
01758 Block* finally_block = NULL;
01759
01760 Token::Value tok = peek();
01761 if (tok != Token::CATCH && tok != Token::FINALLY) {
01762 ReportMessage("no_catch_or_finally", Vector<const char*>::empty());
01763 *ok = false;
01764 return NULL;
01765 }
01766
01767
01768
01769
01770 ZoneList<Label*>* catch_label_list = NEW(ZoneList<Label*>(0));
01771 LabelCollector catch_collector(catch_label_list);
01772 bool has_catch = false;
01773 if (tok == Token::CATCH) {
01774 has_catch = true;
01775 Consume(Token::CATCH);
01776
01777 Expect(Token::LPAREN, CHECK_OK);
01778 Handle<String> name = ParseIdentifier(CHECK_OK);
01779 Expect(Token::RPAREN, CHECK_OK);
01780
01781 if (peek() == Token::LBRACE) {
01782
01783
01784 catch_var = top_scope_->NewTemporary(Factory::catch_var_symbol());
01785 Expression* obj = MakeCatchContext(name, catch_var);
01786 { Target target(this, &catch_collector);
01787 catch_block = WithHelper(obj, NULL, CHECK_OK);
01788 }
01789 } else {
01790 Expect(Token::LBRACE, CHECK_OK);
01791 }
01792
01793 tok = peek();
01794 }
01795
01796 if (tok == Token::FINALLY || !has_catch) {
01797 Consume(Token::FINALLY);
01798
01799
01800 finally_block = ParseBlock(NULL, CHECK_OK);
01801 }
01802
01803
01804
01805
01806
01807
01808 if (!is_pre_parsing_ && catch_block != NULL && finally_block != NULL) {
01809 TryCatch* statement = NEW(TryCatch(try_block, catch_var, catch_block));
01810 statement->set_escaping_labels(collector.labels());
01811 try_block = NEW(Block(NULL, 1, false));
01812 try_block->AddStatement(statement);
01813 catch_block = NULL;
01814 }
01815
01816 TryStatement* result = NULL;
01817 if (!is_pre_parsing_) {
01818 if (catch_block != NULL) {
01819 ASSERT(finally_block == NULL);
01820 result = NEW(TryCatch(try_block, catch_var, catch_block));
01821 result->set_escaping_labels(collector.labels());
01822 } else {
01823 ASSERT(finally_block != NULL);
01824 result = NEW(TryFinally(try_block, finally_block));
01825
01826 for (int i = 0; i < collector.labels()->length(); i++) {
01827 catch_collector.labels()->Add(collector.labels()->at(i));
01828 }
01829 result->set_escaping_labels(catch_collector.labels());
01830 }
01831 }
01832
01833 return result;
01834 }
01835
01836
01837 LoopStatement* Parser::ParseDoStatement(ZoneStringList* labels, bool* ok) {
01838
01839
01840
01841 LoopStatement* loop = NEW(LoopStatement(labels, LoopStatement::DO_LOOP));
01842 Target target(this, loop);
01843
01844 Expect(Token::DO, CHECK_OK);
01845 Statement* body = ParseStatement(NULL, CHECK_OK);
01846 Expect(Token::WHILE, CHECK_OK);
01847 Expect(Token::LPAREN, CHECK_OK);
01848 Expression* cond = ParseExpression(true, CHECK_OK);
01849 Expect(Token::RPAREN, CHECK_OK);
01850
01851
01852
01853
01854
01855 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
01856
01857 if (loop) loop->Initialize(NULL, cond, NULL, body);
01858 return loop;
01859 }
01860
01861
01862 LoopStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) {
01863
01864
01865
01866 LoopStatement* loop = NEW(LoopStatement(labels, LoopStatement::WHILE_LOOP));
01867 Target target(this, loop);
01868
01869 Expect(Token::WHILE, CHECK_OK);
01870 Expect(Token::LPAREN, CHECK_OK);
01871 Expression* cond = ParseExpression(true, CHECK_OK);
01872 Expect(Token::RPAREN, CHECK_OK);
01873 Statement* body = ParseStatement(NULL, CHECK_OK);
01874
01875 if (loop) loop->Initialize(NULL, cond, NULL, body);
01876 return loop;
01877 }
01878
01879
01880 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
01881
01882
01883
01884 Statement* init = NULL;
01885
01886 Expect(Token::FOR, CHECK_OK);
01887 Expect(Token::LPAREN, CHECK_OK);
01888 if (peek() != Token::SEMICOLON) {
01889 if (peek() == Token::VAR || peek() == Token::CONST) {
01890 Expression* each = NULL;
01891 Block* variable_statement =
01892 ParseVariableDeclarations(false, &each, CHECK_OK);
01893 if (peek() == Token::IN && each != NULL) {
01894 ForInStatement* loop = NEW(ForInStatement(labels));
01895 Target target(this, loop);
01896
01897 Expect(Token::IN, CHECK_OK);
01898 Expression* enumerable = ParseExpression(true, CHECK_OK);
01899 Expect(Token::RPAREN, CHECK_OK);
01900
01901 Statement* body = ParseStatement(NULL, CHECK_OK);
01902 if (is_pre_parsing_) {
01903 return NULL;
01904 } else {
01905 loop->Initialize(each, enumerable, body);
01906 Block* result = NEW(Block(NULL, 2, false));
01907 result->AddStatement(variable_statement);
01908 result->AddStatement(loop);
01909
01910 return result;
01911 }
01912
01913 } else {
01914 init = variable_statement;
01915 }
01916
01917 } else {
01918 Expression* expression = ParseExpression(false, CHECK_OK);
01919 if (peek() == Token::IN) {
01920
01921
01922 if (expression == NULL || !expression->IsValidLeftHandSide()) {
01923 if (expression != NULL && expression->AsCall() != NULL) {
01924
01925
01926
01927
01928
01929 Handle<String> type = Factory::invalid_lhs_in_for_in_symbol();
01930 expression = NewThrowReferenceError(type);
01931 } else {
01932
01933
01934 ReportMessage("invalid_lhs_in_for_in",
01935 Vector<const char*>::empty());
01936 *ok = false;
01937 return NULL;
01938 }
01939 }
01940 ForInStatement* loop = NEW(ForInStatement(labels));
01941 Target target(this, loop);
01942
01943 Expect(Token::IN, CHECK_OK);
01944 Expression* enumerable = ParseExpression(true, CHECK_OK);
01945 Expect(Token::RPAREN, CHECK_OK);
01946
01947 Statement* body = ParseStatement(NULL, CHECK_OK);
01948 if (loop) loop->Initialize(expression, enumerable, body);
01949
01950
01951 return loop;
01952
01953 } else {
01954 init = NEW(ExpressionStatement(expression));
01955 }
01956 }
01957 }
01958
01959
01960 LoopStatement* loop = NEW(LoopStatement(labels, LoopStatement::FOR_LOOP));
01961 Target target(this, loop);
01962
01963
01964 Expect(Token::SEMICOLON, CHECK_OK);
01965
01966 Expression* cond = NULL;
01967 if (peek() != Token::SEMICOLON) {
01968 cond = ParseExpression(true, CHECK_OK);
01969 }
01970 Expect(Token::SEMICOLON, CHECK_OK);
01971
01972 Statement* next = NULL;
01973 if (peek() != Token::RPAREN) {
01974 Expression* exp = ParseExpression(true, CHECK_OK);
01975 next = NEW(ExpressionStatement(exp));
01976 }
01977 Expect(Token::RPAREN, CHECK_OK);
01978
01979 Statement* body = ParseStatement(NULL, CHECK_OK);
01980
01981 if (loop) loop->Initialize(init, cond, next, body);
01982 return loop;
01983 }
01984
01985
01986
01987 Expression* Parser::ParseExpression(bool accept_IN, bool* ok) {
01988
01989
01990
01991
01992 Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK);
01993 while (peek() == Token::COMMA) {
01994 Expect(Token::COMMA, CHECK_OK);
01995 Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
01996 result = NEW(BinaryOperation(Token::COMMA, result, right));
01997 }
01998 return result;
01999 }
02000
02001
02002
02003 Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) {
02004
02005
02006
02007
02008 Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK);
02009
02010 if (!Token::IsAssignmentOp(peek())) {
02011
02012 return expression;
02013 }
02014
02015 if (expression == NULL || !expression->IsValidLeftHandSide()) {
02016 if (expression != NULL && expression->AsCall() != NULL) {
02017
02018
02019
02020
02021
02022 Handle<String> type = Factory::invalid_lhs_in_assignment_symbol();
02023 expression = NewThrowReferenceError(type);
02024 } else {
02025
02026
02027
02028
02029
02030
02031
02032 ReportMessage("invalid_lhs_in_assignment", Vector<const char*>::empty());
02033 *ok = false;
02034 return NULL;
02035 }
02036 }
02037
02038
02039 Token::Value op = Next();
02040 int pos = scanner().location().beg_pos;
02041 Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
02042
02043
02044
02045
02046
02047
02048 Property* property = expression ? expression->AsProperty() : NULL;
02049 if (op == Token::ASSIGN &&
02050 property != NULL &&
02051 property->obj()->AsVariableProxy() != NULL &&
02052 property->obj()->AsVariableProxy()->is_this()) {
02053 temp_scope_->AddProperty();
02054 }
02055
02056 return NEW(Assignment(op, expression, right, pos));
02057 }
02058
02059
02060
02061 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) {
02062
02063
02064
02065
02066
02067 Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK);
02068 if (peek() != Token::CONDITIONAL) return expression;
02069 Consume(Token::CONDITIONAL);
02070
02071
02072
02073 Expression* left = ParseAssignmentExpression(true, CHECK_OK);
02074 Expect(Token::COLON, CHECK_OK);
02075 Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
02076 return NEW(Conditional(expression, left, right));
02077 }
02078
02079
02080 static int Precedence(Token::Value tok, bool accept_IN) {
02081 if (tok == Token::IN && !accept_IN)
02082 return 0;
02083
02084 return Token::Precedence(tok);
02085 }
02086
02087
02088
02089 Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) {
02090 ASSERT(prec >= 4);
02091 Expression* x = ParseUnaryExpression(CHECK_OK);
02092 for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) {
02093
02094 while (Precedence(peek(), accept_IN) == prec1) {
02095 Token::Value op = Next();
02096 Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK);
02097
02098
02099 if (x && x->AsLiteral() && x->AsLiteral()->handle()->IsNumber() &&
02100 y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) {
02101 double x_val = x->AsLiteral()->handle()->Number();
02102 double y_val = y->AsLiteral()->handle()->Number();
02103
02104 switch (op) {
02105 case Token::ADD:
02106 x = NewNumberLiteral(x_val + y_val);
02107 continue;
02108 case Token::SUB:
02109 x = NewNumberLiteral(x_val - y_val);
02110 continue;
02111 case Token::MUL:
02112 x = NewNumberLiteral(x_val * y_val);
02113 continue;
02114 case Token::DIV:
02115 x = NewNumberLiteral(x_val / y_val);
02116 continue;
02117 case Token::BIT_OR:
02118 x = NewNumberLiteral(DoubleToInt32(x_val) | DoubleToInt32(y_val));
02119 continue;
02120 case Token::BIT_AND:
02121 x = NewNumberLiteral(DoubleToInt32(x_val) & DoubleToInt32(y_val));
02122 continue;
02123 case Token::BIT_XOR:
02124 x = NewNumberLiteral(DoubleToInt32(x_val) ^ DoubleToInt32(y_val));
02125 continue;
02126 case Token::SHL: {
02127 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f);
02128 x = NewNumberLiteral(value);
02129 continue;
02130 }
02131 case Token::SHR: {
02132 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
02133 uint32_t value = DoubleToUint32(x_val) >> shift;
02134 x = NewNumberLiteral(value);
02135 continue;
02136 }
02137 case Token::SAR: {
02138 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
02139 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
02140 x = NewNumberLiteral(value);
02141 continue;
02142 }
02143 default:
02144 break;
02145 }
02146 }
02147
02148
02149
02150
02151 if (Token::IsCompareOp(op)) {
02152
02153 Token::Value cmp = op;
02154 switch (op) {
02155 case Token::NE: cmp = Token::EQ; break;
02156 case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
02157 default: break;
02158 }
02159 x = NEW(CompareOperation(cmp, x, y));
02160 if (cmp != op) {
02161
02162 x = NEW(UnaryOperation(Token::NOT, x));
02163 }
02164
02165 } else {
02166
02167 x = NEW(BinaryOperation(op, x, y));
02168 }
02169 }
02170 }
02171 return x;
02172 }
02173
02174
02175 Expression* Parser::ParseUnaryExpression(bool* ok) {
02176
02177
02178
02179
02180
02181
02182
02183
02184
02185
02186
02187
02188 Token::Value op = peek();
02189 if (Token::IsUnaryOp(op)) {
02190 op = Next();
02191 Expression* x = ParseUnaryExpression(CHECK_OK);
02192
02193
02194 if (x && x->AsLiteral() && x->AsLiteral()->handle()->IsNumber()) {
02195 double x_val = x->AsLiteral()->handle()->Number();
02196 switch (op) {
02197 case Token::ADD:
02198 return x;
02199 case Token::SUB:
02200 return NewNumberLiteral(-x_val);
02201 case Token::BIT_NOT:
02202 return NewNumberLiteral(~DoubleToInt32(x_val));
02203 default: break;
02204 }
02205 }
02206
02207 return NEW(UnaryOperation(op, x));
02208
02209 } else if (Token::IsCountOp(op)) {
02210 op = Next();
02211 Expression* x = ParseUnaryExpression(CHECK_OK);
02212 if (x == NULL || !x->IsValidLeftHandSide()) {
02213 if (x != NULL && x->AsCall() != NULL) {
02214
02215
02216
02217
02218
02219 Handle<String> type = Factory::invalid_lhs_in_prefix_op_symbol();
02220 x = NewThrowReferenceError(type);
02221 } else {
02222
02223
02224 ReportMessage("invalid_lhs_in_prefix_op", Vector<const char*>::empty());
02225 *ok = false;
02226 return NULL;
02227 }
02228 }
02229 return NEW(CountOperation(true , op, x));
02230
02231 } else {
02232 return ParsePostfixExpression(ok);
02233 }
02234 }
02235
02236
02237 Expression* Parser::ParsePostfixExpression(bool* ok) {
02238
02239
02240
02241 Expression* result = ParseLeftHandSideExpression(CHECK_OK);
02242 if (!scanner_.has_line_terminator_before_next() && Token::IsCountOp(peek())) {
02243 if (result == NULL || !result->IsValidLeftHandSide()) {
02244 if (result != NULL && result->AsCall() != NULL) {
02245
02246
02247
02248
02249
02250 Handle<String> type = Factory::invalid_lhs_in_postfix_op_symbol();
02251 result = NewThrowReferenceError(type);
02252 } else {
02253
02254
02255 ReportMessage("invalid_lhs_in_postfix_op",
02256 Vector<const char*>::empty());
02257 *ok = false;
02258 return NULL;
02259 }
02260 }
02261 Token::Value next = Next();
02262 result = NEW(CountOperation(false , next, result));
02263 }
02264 return result;
02265 }
02266
02267
02268 Expression* Parser::ParseLeftHandSideExpression(bool* ok) {
02269
02270
02271
02272 Expression* result;
02273 if (peek() == Token::NEW) {
02274 result = ParseNewExpression(CHECK_OK);
02275 } else {
02276 result = ParseMemberExpression(CHECK_OK);
02277 }
02278
02279 while (true) {
02280 switch (peek()) {
02281 case Token::LBRACK: {
02282 Consume(Token::LBRACK);
02283 int pos = scanner().location().beg_pos;
02284 Expression* index = ParseExpression(true, CHECK_OK);
02285 result = factory()->NewProperty(result, index, pos);
02286 Expect(Token::RBRACK, CHECK_OK);
02287 break;
02288 }
02289
02290 case Token::LPAREN: {
02291 int pos = scanner().location().beg_pos;
02292 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
02293
02294
02295
02296
02297
02298
02299 if (!is_pre_parsing_) {
02300
02301
02302
02303 VariableProxy* callee = result->AsVariableProxy();
02304 if (callee != NULL && callee->IsVariable(Factory::eval_symbol())) {
02305
02306
02307 ASSERT(!Bootstrapper::IsActive());
02308 top_scope_->RecordEvalCall();
02309 } else {
02310
02311
02312
02313
02314
02315
02316
02317 Property* property = result->AsProperty();
02318 if (property != NULL) {
02319 Literal* key = property->key()->AsLiteral();
02320 if (key != NULL &&
02321 key->handle().is_identical_to(Factory::eval_symbol())) {
02322
02323
02324 ASSERT(!Bootstrapper::IsActive());
02325 top_scope_->RecordEvalCall();
02326 }
02327 }
02328 }
02329 }
02330
02331
02332
02333
02334
02335
02336
02337
02338
02339 const bool is_eval = false;
02340 if (is_eval && args->length() == 0) {
02341 result = NEW(Literal(Factory::undefined_value()));
02342 } else {
02343 result = factory()->NewCall(result, args, is_eval, pos);
02344 }
02345 break;
02346 }
02347
02348 case Token::PERIOD: {
02349 Consume(Token::PERIOD);
02350 int pos = scanner().location().beg_pos;
02351 Handle<String> name = ParseIdentifier(CHECK_OK);
02352 result = factory()->NewProperty(result, NEW(Literal(name)), pos);
02353 break;
02354 }
02355
02356 default:
02357 return result;
02358 }
02359 }
02360 }
02361
02362
02363 Expression* Parser::ParseNewExpression(bool* ok) {
02364
02365
02366
02367
02368
02369
02370
02371
02372
02373
02374
02375 List<int> new_positions(4);
02376 while (peek() == Token::NEW) {
02377 Consume(Token::NEW);
02378 new_positions.Add(scanner().location().beg_pos);
02379 }
02380 ASSERT(new_positions.length() > 0);
02381
02382 Expression* result =
02383 ParseMemberWithNewPrefixesExpression(&new_positions, CHECK_OK);
02384 while (!new_positions.is_empty()) {
02385 int last = new_positions.RemoveLast();
02386 result = NEW(CallNew(result, new ZoneList<Expression*>(0), last));
02387 }
02388 return result;
02389 }
02390
02391
02392 Expression* Parser::ParseMemberExpression(bool* ok) {
02393 static List<int> new_positions(0);
02394 return ParseMemberWithNewPrefixesExpression(&new_positions, ok);
02395 }
02396
02397
02398 Expression* Parser::ParseMemberWithNewPrefixesExpression(
02399 List<int>* new_positions,
02400 bool* ok) {
02401
02402
02403
02404
02405
02406 Expression* result = NULL;
02407 if (peek() == Token::FUNCTION) {
02408 Expect(Token::FUNCTION, CHECK_OK);
02409 int function_token_position = scanner().location().beg_pos;
02410 Handle<String> name;
02411 if (peek() == Token::IDENTIFIER) name = ParseIdentifier(CHECK_OK);
02412 result = ParseFunctionLiteral(name, function_token_position,
02413 NESTED, CHECK_OK);
02414 } else {
02415 result = ParsePrimaryExpression(CHECK_OK);
02416 }
02417
02418 while (true) {
02419 switch (peek()) {
02420 case Token::LBRACK: {
02421 Consume(Token::LBRACK);
02422 int pos = scanner().location().beg_pos;
02423 Expression* index = ParseExpression(true, CHECK_OK);
02424 result = factory()->NewProperty(result, index, pos);
02425 Expect(Token::RBRACK, CHECK_OK);
02426 break;
02427 }
02428 case Token::PERIOD: {
02429 Consume(Token::PERIOD);
02430 int pos = scanner().location().beg_pos;
02431 Handle<String> name = ParseIdentifier(CHECK_OK);
02432 result = factory()->NewProperty(result, NEW(Literal(name)), pos);
02433 break;
02434 }
02435 case Token::LPAREN: {
02436 if (new_positions->is_empty()) return result;
02437
02438 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
02439 int last = new_positions->RemoveLast();
02440 result = NEW(CallNew(result, args, last));
02441 break;
02442 }
02443 default:
02444 return result;
02445 }
02446 }
02447 }
02448
02449
02450 DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
02451
02452
02453
02454
02455
02456
02457 Expect(Token::DEBUGGER, CHECK_OK);
02458 ExpectSemicolon(CHECK_OK);
02459 return NEW(DebuggerStatement());
02460 }
02461
02462
02463 void Parser::ReportUnexpectedToken(Token::Value token) {
02464
02465
02466
02467 if (token == Token::ILLEGAL && scanner().stack_overflow())
02468 return;
02469
02470 switch (token) {
02471 case Token::EOS:
02472 return ReportMessage("unexpected_eos", Vector<const char*>::empty());
02473 case Token::NUMBER:
02474 return ReportMessage("unexpected_token_number",
02475 Vector<const char*>::empty());
02476 case Token::STRING:
02477 return ReportMessage("unexpected_token_string",
02478 Vector<const char*>::empty());
02479 case Token::IDENTIFIER:
02480 return ReportMessage("unexpected_token_identifier",
02481 Vector<const char*>::empty());
02482 default:
02483 const char* name = Token::String(token);
02484 ASSERT(name != NULL);
02485 ReportMessage("unexpected_token", Vector<const char*>(&name, 1));
02486 }
02487 }
02488
02489
02490 Expression* Parser::ParsePrimaryExpression(bool* ok) {
02491
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501
02502
02503
02504 Expression* result = NULL;
02505 switch (peek()) {
02506 case Token::THIS: {
02507 Consume(Token::THIS);
02508 if (is_pre_parsing_) {
02509 result = VariableProxySentinel::this_proxy();
02510 } else {
02511 VariableProxy* recv = top_scope_->receiver();
02512 recv->var_uses()->RecordRead(1);
02513 result = recv;
02514 }
02515 break;
02516 }
02517
02518 case Token::NULL_LITERAL:
02519 Consume(Token::NULL_LITERAL);
02520 result = NEW(Literal(Factory::null_value()));
02521 break;
02522
02523 case Token::TRUE_LITERAL:
02524 Consume(Token::TRUE_LITERAL);
02525 result = NEW(Literal(Factory::true_value()));
02526 break;
02527
02528 case Token::FALSE_LITERAL:
02529 Consume(Token::FALSE_LITERAL);
02530 result = NEW(Literal(Factory::false_value()));
02531 break;
02532
02533 case Token::IDENTIFIER: {
02534 Handle<String> name = ParseIdentifier(CHECK_OK);
02535 if (is_pre_parsing_) {
02536 result = VariableProxySentinel::identifier_proxy();
02537 } else {
02538 result = top_scope_->NewUnresolved(name, inside_with());
02539 }
02540 break;
02541 }
02542
02543 case Token::NUMBER: {
02544 Consume(Token::NUMBER);
02545 double value =
02546 StringToDouble(scanner_.literal_string(), ALLOW_HEX | ALLOW_OCTALS);
02547 result = NewNumberLiteral(value);
02548 break;
02549 }
02550
02551 case Token::STRING: {
02552 Consume(Token::STRING);
02553 Handle<String> symbol =
02554 factory()->LookupSymbol(scanner_.literal_string(),
02555 scanner_.literal_length());
02556 result = NEW(Literal(symbol));
02557 break;
02558 }
02559
02560 case Token::ASSIGN_DIV:
02561 result = ParseRegExpLiteral(true, CHECK_OK);
02562 break;
02563
02564 case Token::DIV:
02565 result = ParseRegExpLiteral(false, CHECK_OK);
02566 break;
02567
02568 case Token::LBRACK:
02569 result = ParseArrayLiteral(CHECK_OK);
02570 break;
02571
02572 case Token::LBRACE:
02573 result = ParseObjectLiteral(CHECK_OK);
02574 break;
02575
02576 case Token::LPAREN:
02577 Consume(Token::LPAREN);
02578 result = ParseExpression(true, CHECK_OK);
02579 Expect(Token::RPAREN, CHECK_OK);
02580 break;
02581
02582 case Token::MOD:
02583 if (allow_natives_syntax_ || extension_ != NULL) {
02584 result = ParseV8Intrinsic(CHECK_OK);
02585 break;
02586 }
02587
02588
02589
02590 default: {
02591 Token::Value tok = peek();
02592
02593
02594
02595 Next();
02596 ReportUnexpectedToken(tok);
02597 *ok = false;
02598 return NULL;
02599 }
02600 }
02601
02602 return result;
02603 }
02604
02605
02606 Expression* Parser::ParseArrayLiteral(bool* ok) {
02607
02608
02609
02610 ZoneListWrapper<Expression> values = factory()->NewList<Expression>(4);
02611 Expect(Token::LBRACK, CHECK_OK);
02612 while (peek() != Token::RBRACK) {
02613 Expression* elem;
02614 if (peek() == Token::COMMA) {
02615 elem = GetLiteralTheHole();
02616 } else {
02617 elem = ParseAssignmentExpression(true, CHECK_OK);
02618 }
02619 values.Add(elem);
02620 if (peek() != Token::RBRACK) {
02621 Expect(Token::COMMA, CHECK_OK);
02622 }
02623 }
02624 Expect(Token::RBRACK, CHECK_OK);
02625
02626
02627 temp_scope_->set_contains_array_literal();
02628
02629 if (is_pre_parsing_) return NULL;
02630
02631
02632 Handle<FixedArray> literals =
02633 Factory::NewFixedArray(values.length(), TENURED);
02634
02635
02636 for (int i = 0; i < values.length(); i++) {
02637 Literal* literal = values.at(i)->AsLiteral();
02638 if (literal == NULL) {
02639 literals->set_the_hole(i);
02640 } else {
02641 literals->set(i, *literal->handle());
02642 }
02643 }
02644
02645 return NEW(ArrayLiteral(literals, values.elements()));
02646 }
02647
02648
02649 bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) {
02650 return property != NULL &&
02651 property->kind() != ObjectLiteral::Property::PROTOTYPE;
02652 }
02653
02654
02655 Literal* Parser::GetBoilerplateValue(ObjectLiteral::Property* property) {
02656 if (property->kind() == ObjectLiteral::Property::CONSTANT)
02657 return property->value()->AsLiteral();
02658 return GetLiteralUndefined();
02659 }
02660
02661
02662 Expression* Parser::ParseObjectLiteral(bool* ok) {
02663
02664
02665
02666
02667
02668
02669 ZoneListWrapper<ObjectLiteral::Property> properties =
02670 factory()->NewList<ObjectLiteral::Property>(4);
02671 int number_of_boilerplate_properties = 0;
02672
02673 Expect(Token::LBRACE, CHECK_OK);
02674 while (peek() != Token::RBRACE) {
02675 Literal* key = NULL;
02676 switch (peek()) {
02677 case Token::IDENTIFIER: {
02678
02679
02680
02681 bool is_getter = false;
02682 bool is_setter = false;
02683 Handle<String> id =
02684 ParseIdentifierOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
02685 if (is_getter || is_setter) {
02686
02687 if (peek() == Token::IDENTIFIER) {
02688 Handle<String> name = ParseIdentifier(CHECK_OK);
02689 FunctionLiteral* value =
02690 ParseFunctionLiteral(name, RelocInfo::kNoPosition,
02691 DECLARATION, CHECK_OK);
02692 ObjectLiteral::Property* property =
02693 NEW(ObjectLiteral::Property(is_getter, value));
02694 if (IsBoilerplateProperty(property))
02695 number_of_boilerplate_properties++;
02696 properties.Add(property);
02697 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
02698 continue;
02699 }
02700 }
02701 key = NEW(Literal(id));
02702 break;
02703 }
02704
02705 case Token::STRING: {
02706 Consume(Token::STRING);
02707 Handle<String> string =
02708 factory()->LookupSymbol(scanner_.literal_string(),
02709 scanner_.literal_length());
02710 uint32_t index;
02711 if (!string.is_null() && string->AsArrayIndex(&index)) {
02712 key = NewNumberLiteral(index);
02713 } else {
02714 key = NEW(Literal(string));
02715 }
02716 break;
02717 }
02718
02719 case Token::NUMBER: {
02720 Consume(Token::NUMBER);
02721 double value =
02722 StringToDouble(scanner_.literal_string(), ALLOW_HEX | ALLOW_OCTALS);
02723 key = NewNumberLiteral(value);
02724 break;
02725 }
02726
02727 default:
02728 Expect(Token::RBRACE, CHECK_OK);
02729 break;
02730 }
02731
02732 Expect(Token::COLON, CHECK_OK);
02733 Expression* value = ParseAssignmentExpression(true, CHECK_OK);
02734
02735 ObjectLiteral::Property* property =
02736 NEW(ObjectLiteral::Property(key, value));
02737
02738
02739 if (IsBoilerplateProperty(property)) number_of_boilerplate_properties++;
02740 properties.Add(property);
02741
02742
02743 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
02744 }
02745 Expect(Token::RBRACE, CHECK_OK);
02746
02747 int literal_index = temp_scope_->NextMaterializedLiteralIndex();
02748 if (is_pre_parsing_) return NULL;
02749
02750 Handle<FixedArray> constant_properties =
02751 Factory::NewFixedArray(number_of_boilerplate_properties * 2, TENURED);
02752 int position = 0;
02753 for (int i = 0; i < properties.length(); i++) {
02754 ObjectLiteral::Property* property = properties.at(i);
02755 if (!IsBoilerplateProperty(property)) continue;
02756
02757
02758
02759
02760 Handle<Object> key = property->key()->handle();
02761 Literal* literal = GetBoilerplateValue(property);
02762
02763
02764 constant_properties->set(position++, *key);
02765 constant_properties->set(position++, *literal->handle());
02766 }
02767
02768
02769
02770 ZoneList<Expression*>* arguments = new ZoneList<Expression*>(1);
02771 arguments->Add(new Literal(constant_properties));
02772 return new ObjectLiteral(constant_properties,
02773 properties.elements(),
02774 literal_index);
02775 }
02776
02777
02778 Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) {
02779 if (!scanner_.ScanRegExpPattern(seen_equal)) {
02780 Next();
02781 ReportMessage("unterminated_regexp", Vector<const char*>::empty());
02782 *ok = false;
02783 return NULL;
02784 }
02785
02786 int literal_index = temp_scope_->NextMaterializedLiteralIndex();
02787
02788 if (is_pre_parsing_) {
02789
02790
02791 scanner_.ScanRegExpFlags();
02792 Next();
02793 return NULL;
02794 }
02795
02796 Handle<String> js_pattern =
02797 Factory::NewStringFromUtf8(scanner_.next_literal(), TENURED);
02798 scanner_.ScanRegExpFlags();
02799 Handle<String> js_flags =
02800 Factory::NewStringFromUtf8(scanner_.next_literal(), TENURED);
02801 Next();
02802
02803 return new RegExpLiteral(js_pattern, js_flags, literal_index);
02804 }
02805
02806
02807 ZoneList<Expression*>* Parser::ParseArguments(bool* ok) {
02808
02809
02810
02811 ZoneListWrapper<Expression> result = factory()->NewList<Expression>(4);
02812 Expect(Token::LPAREN, CHECK_OK);
02813 bool done = (peek() == Token::RPAREN);
02814 while (!done) {
02815 Expression* argument = ParseAssignmentExpression(true, CHECK_OK);
02816 result.Add(argument);
02817 done = (peek() == Token::RPAREN);
02818 if (!done) Expect(Token::COMMA, CHECK_OK);
02819 }
02820 Expect(Token::RPAREN, CHECK_OK);
02821 return result.elements();
02822 }
02823
02824
02825 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
02826 int function_token_position,
02827 FunctionLiteralType type,
02828 bool* ok) {
02829
02830
02831
02832 bool is_named = !var_name.is_null();
02833
02834
02835
02836
02837
02838 Handle<String> name = is_named ? var_name : factory()->EmptySymbol();
02839
02840 Handle<String> function_name = factory()->EmptySymbol();
02841 if (is_named && (type == EXPRESSION || type == NESTED)) {
02842 function_name = name;
02843 }
02844
02845 int num_parameters = 0;
02846
02847 { Scope::Type type = Scope::FUNCTION_SCOPE;
02848 Scope* scope = factory()->NewScope(top_scope_, type, inside_with());
02849 LexicalScope lexical_scope(this, scope);
02850 TemporaryScope temp_scope(this);
02851 top_scope_->SetScopeName(name);
02852
02853
02854
02855 Expect(Token::LPAREN, CHECK_OK);
02856 int start_pos = scanner_.location().beg_pos;
02857 bool done = (peek() == Token::RPAREN);
02858 while (!done) {
02859 Handle<String> param_name = ParseIdentifier(CHECK_OK);
02860 if (!is_pre_parsing_) {
02861 top_scope_->AddParameter(top_scope_->Declare(param_name,
02862 Variable::VAR));
02863 num_parameters++;
02864 }
02865 done = (peek() == Token::RPAREN);
02866 if (!done) Expect(Token::COMMA, CHECK_OK);
02867 }
02868 Expect(Token::RPAREN, CHECK_OK);
02869
02870 Expect(Token::LBRACE, CHECK_OK);
02871 ZoneListWrapper<Statement> body = factory()->NewList<Statement>(8);
02872
02873
02874
02875
02876
02877
02878
02879 if (!function_name.is_null() && function_name->length() > 0) {
02880 Variable* fvar = top_scope_->DeclareFunctionVar(function_name);
02881 VariableProxy* fproxy =
02882 top_scope_->NewUnresolved(function_name, inside_with());
02883 fproxy->BindTo(fvar);
02884 body.Add(new ExpressionStatement(
02885 new Assignment(Token::INIT_VAR, fproxy,
02886 NEW(ThisFunction()),
02887 RelocInfo::kNoPosition)));
02888 }
02889
02890
02891
02892 bool is_lazily_compiled =
02893 mode() == PARSE_LAZILY && top_scope_->HasTrivialOuterContext();
02894
02895 int materialized_literal_count;
02896 int expected_property_count;
02897 bool contains_array_literal;
02898 if (is_lazily_compiled && pre_data() != NULL) {
02899 FunctionEntry entry = pre_data()->GetFunctionEnd(start_pos);
02900 int end_pos = entry.end_pos();
02901 Counters::total_preparse_skipped.Increment(end_pos - start_pos);
02902 scanner_.SeekForward(end_pos);
02903 materialized_literal_count = entry.literal_count();
02904 expected_property_count = entry.property_count();
02905 contains_array_literal = entry.contains_array_literal();
02906 } else {
02907 ParseSourceElements(&body, Token::RBRACE, CHECK_OK);
02908 materialized_literal_count = temp_scope.materialized_literal_count();
02909 expected_property_count = temp_scope.expected_property_count();
02910 contains_array_literal = temp_scope.contains_array_literal();
02911 }
02912
02913 Expect(Token::RBRACE, CHECK_OK);
02914 int end_pos = scanner_.location().end_pos;
02915
02916 FunctionEntry entry = log()->LogFunction(start_pos);
02917 if (entry.is_valid()) {
02918 entry.set_end_pos(end_pos);
02919 entry.set_literal_count(materialized_literal_count);
02920 entry.set_property_count(expected_property_count);
02921 entry.set_contains_array_literal(contains_array_literal);
02922 }
02923
02924 FunctionLiteral* function_literal =
02925 NEW(FunctionLiteral(name, top_scope_,
02926 body.elements(), materialized_literal_count,
02927 contains_array_literal, expected_property_count,
02928 num_parameters, start_pos, end_pos,
02929 function_name->length() > 0));
02930 if (!is_pre_parsing_) {
02931 function_literal->set_function_token_position(function_token_position);
02932 }
02933 return function_literal;
02934 }
02935 }
02936
02937
02938 Expression* Parser::ParseV8Intrinsic(bool* ok) {
02939
02940
02941
02942 Expect(Token::MOD, CHECK_OK);
02943 Handle<String> name = ParseIdentifier(CHECK_OK);
02944 Runtime::Function* function =
02945 Runtime::FunctionForName(scanner_.literal_string());
02946 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
02947 if (function == NULL && extension_ != NULL) {
02948
02949
02950 top_scope_->ForceEagerCompilation();
02951 }
02952
02953
02954 if (!is_pre_parsing_) {
02955 if (function == Runtime::FunctionForId(Runtime::kIS_VAR)) {
02956
02957
02958
02959 if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) {
02960 return args->at(0);
02961 }
02962 *ok = false;
02963
02964
02965
02966 }
02967
02968 if (!*ok) {
02969
02970 ReportMessage("unable_to_parse", Vector<const char*>::empty());
02971 return NULL;
02972 }
02973 }
02974
02975
02976 return NEW(CallRuntime(name, function, args));
02977 }
02978
02979
02980 void Parser::Consume(Token::Value token) {
02981 Token::Value next = Next();
02982 USE(next);
02983 USE(token);
02984 ASSERT(next == token);
02985 }
02986
02987
02988 void Parser::Expect(Token::Value token, bool* ok) {
02989 Token::Value next = Next();
02990 if (next == token) return;
02991 ReportUnexpectedToken(next);
02992 *ok = false;
02993 }
02994
02995
02996 void Parser::ExpectSemicolon(bool* ok) {
02997
02998
02999 Token::Value tok = peek();
03000 if (tok == Token::SEMICOLON) {
03001 Next();
03002 return;
03003 }
03004 if (scanner_.has_line_terminator_before_next() ||
03005 tok == Token::RBRACE ||
03006 tok == Token::EOS) {
03007 return;
03008 }
03009 Expect(Token::SEMICOLON, ok);
03010 }
03011
03012
03013 Literal* Parser::GetLiteralUndefined() {
03014 return NEW(Literal(Factory::undefined_value()));
03015 }
03016
03017
03018 Literal* Parser::GetLiteralTheHole() {
03019 return NEW(Literal(Factory::the_hole_value()));
03020 }
03021
03022
03023 Literal* Parser::GetLiteralNumber(double value) {
03024 return NewNumberLiteral(value);
03025 }
03026
03027
03028 Handle<String> Parser::ParseIdentifier(bool* ok) {
03029 Expect(Token::IDENTIFIER, ok);
03030 if (!*ok) return Handle<String>();
03031 return factory()->LookupSymbol(scanner_.literal_string(),
03032 scanner_.literal_length());
03033 }
03034
03035
03036
03037
03038
03039 Handle<String> Parser::ParseIdentifierOrGetOrSet(bool* is_get,
03040 bool* is_set,
03041 bool* ok) {
03042 Expect(Token::IDENTIFIER, ok);
03043 if (!*ok) return Handle<String>();
03044 if (scanner_.literal_length() == 3) {
03045 const char* token = scanner_.literal_string();
03046 *is_get = strcmp(token, "get") == 0;
03047 *is_set = !*is_get && strcmp(token, "set") == 0;
03048 }
03049 return factory()->LookupSymbol(scanner_.literal_string(),
03050 scanner_.literal_length());
03051 }
03052
03053
03054
03055
03056
03057
03058 bool Parser::TargetStackContainsLabel(Handle<String> label) {
03059 for (int i = target_stack_->length(); i-- > 0;) {
03060 BreakableStatement* stat = target_stack_->at(i)->AsBreakableStatement();
03061 if (stat != NULL && ContainsLabel(stat->labels(), label))
03062 return true;
03063 }
03064 return false;
03065 }
03066
03067
03068 BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) {
03069 bool anonymous = label.is_null();
03070 for (int i = target_stack_->length(); i-- > 0;) {
03071 BreakableStatement* stat = target_stack_->at(i)->AsBreakableStatement();
03072 if (stat == NULL) continue;
03073
03074 if ((anonymous && stat->is_target_for_anonymous()) ||
03075 (!anonymous && ContainsLabel(stat->labels(), label))) {
03076 RegisterLabelUse(stat->break_target(), i);
03077 return stat;
03078 }
03079 }
03080 return NULL;
03081 }
03082
03083
03084 IterationStatement* Parser::LookupContinueTarget(Handle<String> label,
03085 bool* ok) {
03086 bool anonymous = label.is_null();
03087 for (int i = target_stack_->length(); i-- > 0;) {
03088 IterationStatement* stat = target_stack_->at(i)->AsIterationStatement();
03089 if (stat == NULL) continue;
03090
03091 ASSERT(stat->is_target_for_anonymous());
03092 if (anonymous || ContainsLabel(stat->labels(), label)) {
03093 RegisterLabelUse(stat->continue_target(), i);
03094 return stat;
03095 }
03096 }
03097 return NULL;
03098 }
03099
03100
03101 void Parser::RegisterLabelUse(Label* label, int index) {
03102
03103
03104
03105 for (int i = target_stack_->length(); i-- > index;) {
03106 LabelCollector* collector = target_stack_->at(i)->AsLabelCollector();
03107 if (collector != NULL) collector->AddLabel(label);
03108 }
03109 }
03110
03111
03112 Literal* Parser::NewNumberLiteral(double number) {
03113 return NEW(Literal(Factory::NewNumber(number, TENURED)));
03114 }
03115
03116
03117 Expression* Parser::NewThrowReferenceError(Handle<String> type) {
03118 return NewThrowError(Factory::MakeReferenceError_symbol(),
03119 type, HandleVector<Object>(NULL, 0));
03120 }
03121
03122
03123 Expression* Parser::NewThrowSyntaxError(Handle<String> type,
03124 Handle<Object> first) {
03125 int argc = first.is_null() ? 0 : 1;
03126 Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc);
03127 return NewThrowError(Factory::MakeSyntaxError_symbol(), type, arguments);
03128 }
03129
03130
03131 Expression* Parser::NewThrowTypeError(Handle<String> type,
03132 Handle<Object> first,
03133 Handle<Object> second) {
03134 ASSERT(!first.is_null() && !second.is_null());
03135 Handle<Object> elements[] = { first, second };
03136 Vector< Handle<Object> > arguments =
03137 HandleVector<Object>(elements, ARRAY_SIZE(elements));
03138 return NewThrowError(Factory::MakeTypeError_symbol(), type, arguments);
03139 }
03140
03141
03142 Expression* Parser::NewThrowError(Handle<String> constructor,
03143 Handle<String> type,
03144 Vector< Handle<Object> > arguments) {
03145 if (is_pre_parsing_) return NULL;
03146
03147 int argc = arguments.length();
03148 Handle<JSArray> array = Factory::NewJSArray(argc, TENURED);
03149 ASSERT(array->IsJSArray() && array->HasFastElements());
03150 for (int i = 0; i < argc; i++) {
03151 Handle<Object> element = arguments[i];
03152 if (!element.is_null()) {
03153 array->SetFastElement(i, *element);
03154 }
03155 }
03156 ZoneList<Expression*>* args = new ZoneList<Expression*>(2);
03157 args->Add(new Literal(type));
03158 args->Add(new Literal(array));
03159 return new Throw(new CallRuntime(constructor, NULL, args),
03160 scanner().location().beg_pos);
03161 }
03162
03163
03164
03165
03166
03167
03168
03169
03170 static bool always_allow_natives_syntax = false;
03171
03172
03173 ParserMessage::~ParserMessage() {
03174 for (int i = 0; i < args().length(); i++)
03175 DeleteArray(args()[i]);
03176 DeleteArray(args().start());
03177 }
03178
03179
03180 ScriptDataImpl::~ScriptDataImpl() {
03181 store_.Dispose();
03182 }
03183
03184
03185 int ScriptDataImpl::Length() {
03186 return store_.length();
03187 }
03188
03189
03190 unsigned* ScriptDataImpl::Data() {
03191 return store_.start();
03192 }
03193
03194
03195 ScriptDataImpl* PreParse(unibrow::CharacterStream* stream,
03196 v8::Extension* extension) {
03197 Handle<Script> no_script;
03198 bool allow_natives_syntax =
03199 always_allow_natives_syntax ||
03200 FLAG_allow_natives_syntax ||
03201 Bootstrapper::IsActive();
03202 PreParser parser(no_script, allow_natives_syntax, extension);
03203 if (!parser.PreParseProgram(stream)) return NULL;
03204
03205
03206
03207 Vector<unsigned> store = parser.recorder()->store()->ToVector().Clone();
03208 return new ScriptDataImpl(store);
03209 }
03210
03211
03212 FunctionLiteral* MakeAST(bool compile_in_global_context,
03213 Handle<Script> script,
03214 v8::Extension* extension,
03215 ScriptDataImpl* pre_data) {
03216 bool allow_natives_syntax =
03217 always_allow_natives_syntax ||
03218 FLAG_allow_natives_syntax ||
03219 Bootstrapper::IsActive();
03220 AstBuildingParser parser(script, allow_natives_syntax, extension, pre_data);
03221 if (pre_data != NULL && pre_data->has_error()) {
03222 Scanner::Location loc = pre_data->MessageLocation();
03223 const char* message = pre_data->BuildMessage();
03224 Vector<const char*> args = pre_data->BuildArgs();
03225 parser.ReportMessageAt(loc, message, args);
03226 DeleteArray(message);
03227 for (int i = 0; i < args.length(); i++)
03228 DeleteArray(args[i]);
03229 DeleteArray(args.start());
03230 return NULL;
03231 }
03232 Handle<String> source = Handle<String>(String::cast(script->source()));
03233 SafeStringInputBuffer input(source.location());
03234 FunctionLiteral* result = parser.ParseProgram(source,
03235 &input, compile_in_global_context);
03236 return result;
03237 }
03238
03239
03240 FunctionLiteral* MakeLazyAST(Handle<Script> script,
03241 Handle<String> name,
03242 int start_position,
03243 int end_position,
03244 bool is_expression) {
03245 bool allow_natives_syntax_before = always_allow_natives_syntax;
03246 always_allow_natives_syntax = true;
03247 AstBuildingParser parser(script, true, NULL, NULL);
03248 always_allow_natives_syntax = allow_natives_syntax_before;
03249
03250 Handle<String> script_source(String::cast(script->source()));
03251 FunctionLiteral* result =
03252 parser.ParseLazy(SubString(script_source, start_position, end_position),
03253 name,
03254 start_position,
03255 is_expression);
03256 return result;
03257 }
03258
03259
03260 #undef NEW
03261
03262
03263 } }