Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/parser.cc

Issue 3538005: Cleanup of the parser. (Closed)
Patch Set: Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/parser.h ('K') | « src/parser.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 108
109 void ReportMessage(const char* message, Vector<const char*> args); 109 void ReportMessage(const char* message, Vector<const char*> args);
110 virtual void ReportMessageAt(Scanner::Location loc, 110 virtual void ReportMessageAt(Scanner::Location loc,
111 const char* message, 111 const char* message,
112 Vector<const char*> args) = 0; 112 Vector<const char*> args) = 0;
113 113
114 114
115 // Returns NULL if parsing failed. 115 // Returns NULL if parsing failed.
116 FunctionLiteral* ParseProgram(Handle<String> source, 116 FunctionLiteral* ParseProgram(Handle<String> source,
117 bool in_global_context); 117 bool in_global_context);
118 FunctionLiteral* ParseLazy(Handle<String> source, 118 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info);
119 Handle<String> name,
120 int start_position,
121 int end_position,
122 bool is_expression);
123 FunctionLiteral* ParseJson(Handle<String> source); 119 FunctionLiteral* ParseJson(Handle<String> source);
124 120
125 // The minimum number of contiguous assignment that will 121 // The minimum number of contiguous assignment that will
126 // be treated as an initialization block. Benchmarks show that 122 // be treated as an initialization block. Benchmarks show that
127 // the overhead exceeds the savings below this limit. 123 // the overhead exceeds the savings below this limit.
128 static const int kMinInitializationBlock = 3; 124 static const int kMinInitializationBlock = 3;
129 125
130 protected: 126 protected:
131 127
132 enum Mode { 128 enum Mode {
(...skipping 1447 matching lines...) Expand 10 before | Expand all | Expand 10 after
1580 // Make sure the target stack is empty. 1576 // Make sure the target stack is empty.
1581 ASSERT(target_stack_ == NULL); 1577 ASSERT(target_stack_ == NULL);
1582 1578
1583 // If there was a syntax error we have to get rid of the AST 1579 // If there was a syntax error we have to get rid of the AST
1584 // and it is not safe to do so before the scope has been deleted. 1580 // and it is not safe to do so before the scope has been deleted.
1585 if (result == NULL) zone_scope.DeleteOnExit(); 1581 if (result == NULL) zone_scope.DeleteOnExit();
1586 return result; 1582 return result;
1587 } 1583 }
1588 1584
1589 1585
1590 FunctionLiteral* Parser::ParseLazy(Handle<String> source, 1586 FunctionLiteral* Parser::ParseLazy(Handle<SharedFunctionInfo> info) {
1591 Handle<String> name,
1592 int start_position,
1593 int end_position,
1594 bool is_expression) {
1595 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); 1587 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
1596 HistogramTimerScope timer(&Counters::parse_lazy); 1588 HistogramTimerScope timer(&Counters::parse_lazy);
1589 Handle<String> source(String::cast(script_->source()));
1597 Counters::total_parse_size.Increment(source->length()); 1590 Counters::total_parse_size.Increment(source->length());
1598 1591
1592 Handle<String> name(String::cast(info->name()));
1599 fni_ = new FuncNameInferrer(); 1593 fni_ = new FuncNameInferrer();
1600 fni_->PushEnclosingName(name); 1594 fni_->PushEnclosingName(name);
1601 1595
1602 // Initialize parser state. 1596 // Initialize parser state.
1603 source->TryFlatten(); 1597 source->TryFlatten();
1604 scanner_.Initialize(source, start_position, end_position, JAVASCRIPT); 1598 scanner_.Initialize(source, info->start_position(), info->end_position(),
1599 JAVASCRIPT);
1605 ASSERT(target_stack_ == NULL); 1600 ASSERT(target_stack_ == NULL);
1606 mode_ = PARSE_EAGERLY; 1601 mode_ = PARSE_EAGERLY;
1607 1602
1608 // Place holder for the result. 1603 // Place holder for the result.
1609 FunctionLiteral* result = NULL; 1604 FunctionLiteral* result = NULL;
1610 1605
1611 { 1606 {
1612 // Parse the function literal. 1607 // Parse the function literal.
1613 Handle<String> no_name = factory()->EmptySymbol(); 1608 Handle<String> no_name = factory()->EmptySymbol();
1614 Scope* scope = 1609 Scope* scope =
1615 factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with()); 1610 factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with());
1616 LexicalScope lexical_scope(this, scope); 1611 LexicalScope lexical_scope(this, scope);
1617 TemporaryScope temp_scope(this); 1612 TemporaryScope temp_scope(this);
1618 1613
1619 FunctionLiteralType type = is_expression ? EXPRESSION : DECLARATION; 1614 FunctionLiteralType type =
1615 info->is_expression() ? EXPRESSION : DECLARATION;
1620 bool ok = true; 1616 bool ok = true;
1621 result = ParseFunctionLiteral(name, RelocInfo::kNoPosition, type, &ok); 1617 result = ParseFunctionLiteral(name, RelocInfo::kNoPosition, type, &ok);
1622 // Make sure the results agree. 1618 // Make sure the results agree.
1623 ASSERT(ok == (result != NULL)); 1619 ASSERT(ok == (result != NULL));
1624 // The only errors should be stack overflows. 1620 // The only errors should be stack overflows.
1625 ASSERT(ok || scanner_.stack_overflow()); 1621 ASSERT(ok || scanner_.stack_overflow());
1626 } 1622 }
1627 1623
1628 // Make sure the target stack is empty. 1624 // Make sure the target stack is empty.
1629 ASSERT(target_stack_ == NULL); 1625 ASSERT(target_stack_ == NULL);
1630 1626
1631 // If there was a stack overflow we have to get rid of AST and it is 1627 // If there was a stack overflow we have to get rid of AST and it is
1632 // not safe to do before scope has been deleted. 1628 // not safe to do before scope has been deleted.
1633 if (result == NULL) { 1629 if (result == NULL) {
1634 Top::StackOverflow(); 1630 Top::StackOverflow();
1635 zone_scope.DeleteOnExit(); 1631 zone_scope.DeleteOnExit();
1636 } 1632 }
1637 return result; 1633 return result;
1638 } 1634 }
1639 1635
1636
1640 FunctionLiteral* Parser::ParseJson(Handle<String> source) { 1637 FunctionLiteral* Parser::ParseJson(Handle<String> source) {
1641 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); 1638 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
1642 1639
1643 HistogramTimerScope timer(&Counters::parse); 1640 HistogramTimerScope timer(&Counters::parse);
1644 Counters::total_parse_size.Increment(source->length()); 1641 Counters::total_parse_size.Increment(source->length());
1645 1642
1646 // Initialize parser state. 1643 // Initialize parser state.
1647 source->TryFlatten(TENURED); 1644 source->TryFlatten(TENURED);
1648 scanner_.Initialize(source, JSON); 1645 scanner_.Initialize(source, JSON);
1649 ASSERT(target_stack_ == NULL); 1646 ASSERT(target_stack_ == NULL);
(...skipping 3818 matching lines...) Expand 10 before | Expand all | Expand 10 after
5468 ranges->Add(CharacterRange::Everything()); 5465 ranges->Add(CharacterRange::Everything());
5469 is_negated = !is_negated; 5466 is_negated = !is_negated;
5470 } 5467 }
5471 return new RegExpCharacterClass(ranges, is_negated); 5468 return new RegExpCharacterClass(ranges, is_negated);
5472 } 5469 }
5473 5470
5474 5471
5475 // ---------------------------------------------------------------------------- 5472 // ----------------------------------------------------------------------------
5476 // The Parser interface. 5473 // The Parser interface.
5477 5474
5478 // MakeAST() is just a wrapper for the corresponding Parser calls
5479 // so we don't have to expose the entire Parser class in the .h file.
5480
5481 static bool always_allow_natives_syntax = false;
5482
5483
5484 ParserMessage::~ParserMessage() { 5475 ParserMessage::~ParserMessage() {
5485 for (int i = 0; i < args().length(); i++) 5476 for (int i = 0; i < args().length(); i++)
5486 DeleteArray(args()[i]); 5477 DeleteArray(args()[i]);
5487 DeleteArray(args().start()); 5478 DeleteArray(args().start());
5488 } 5479 }
5489 5480
5490 5481
5491 ScriptDataImpl::~ScriptDataImpl() { 5482 ScriptDataImpl::~ScriptDataImpl() {
5492 if (owns_store_) store_.Dispose(); 5483 if (owns_store_) store_.Dispose();
5493 } 5484 }
(...skipping 14 matching lines...) Expand all
5508 } 5499 }
5509 5500
5510 5501
5511 // Preparse, but only collect data that is immediately useful, 5502 // Preparse, but only collect data that is immediately useful,
5512 // even if the preparser data is only used once. 5503 // even if the preparser data is only used once.
5513 ScriptDataImpl* PartialPreParse(Handle<String> source, 5504 ScriptDataImpl* PartialPreParse(Handle<String> source,
5514 unibrow::CharacterStream* stream, 5505 unibrow::CharacterStream* stream,
5515 v8::Extension* extension) { 5506 v8::Extension* extension) {
5516 Handle<Script> no_script; 5507 Handle<Script> no_script;
5517 bool allow_natives_syntax = 5508 bool allow_natives_syntax =
5518 always_allow_natives_syntax || 5509 FLAG_allow_natives_syntax || Bootstrapper::IsActive();
5519 FLAG_allow_natives_syntax ||
5520 Bootstrapper::IsActive();
5521 PartialPreParser parser(no_script, allow_natives_syntax, extension); 5510 PartialPreParser parser(no_script, allow_natives_syntax, extension);
5522 if (!parser.PreParseProgram(source, stream)) return NULL; 5511 if (!parser.PreParseProgram(source, stream)) return NULL;
5523 // Extract the accumulated data from the recorder as a single 5512 // Extract the accumulated data from the recorder as a single
5524 // contiguous vector that we are responsible for disposing. 5513 // contiguous vector that we are responsible for disposing.
5525 Vector<unsigned> store = parser.recorder()->ExtractData(); 5514 Vector<unsigned> store = parser.recorder()->ExtractData();
5526 return new ScriptDataImpl(store); 5515 return new ScriptDataImpl(store);
5527 } 5516 }
5528 5517
5529 5518
5530 void ScriptDataImpl::Initialize() { 5519 void ScriptDataImpl::Initialize() {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
5568 *source = data; 5557 *source = data;
5569 return result; 5558 return result;
5570 } 5559 }
5571 5560
5572 5561
5573 ScriptDataImpl* PreParse(Handle<String> source, 5562 ScriptDataImpl* PreParse(Handle<String> source,
5574 unibrow::CharacterStream* stream, 5563 unibrow::CharacterStream* stream,
5575 v8::Extension* extension) { 5564 v8::Extension* extension) {
5576 Handle<Script> no_script; 5565 Handle<Script> no_script;
5577 bool allow_natives_syntax = 5566 bool allow_natives_syntax =
5578 always_allow_natives_syntax || 5567 FLAG_allow_natives_syntax || Bootstrapper::IsActive();
5579 FLAG_allow_natives_syntax ||
5580 Bootstrapper::IsActive();
5581 CompletePreParser parser(no_script, allow_natives_syntax, extension); 5568 CompletePreParser parser(no_script, allow_natives_syntax, extension);
5582 if (!parser.PreParseProgram(source, stream)) return NULL; 5569 if (!parser.PreParseProgram(source, stream)) return NULL;
5583 // Extract the accumulated data from the recorder as a single 5570 // Extract the accumulated data from the recorder as a single
5584 // contiguous vector that we are responsible for disposing. 5571 // contiguous vector that we are responsible for disposing.
5585 Vector<unsigned> store = parser.recorder()->ExtractData(); 5572 Vector<unsigned> store = parser.recorder()->ExtractData();
5586 return new ScriptDataImpl(store); 5573 return new ScriptDataImpl(store);
5587 } 5574 }
5588 5575
5589 5576
5590 bool ParseRegExp(FlatStringReader* input, 5577 bool ParseRegExp(FlatStringReader* input,
(...skipping 11 matching lines...) Expand all
5602 result->tree = tree; 5589 result->tree = tree;
5603 int capture_count = parser.captures_started(); 5590 int capture_count = parser.captures_started();
5604 result->simple = tree->IsAtom() && parser.simple() && capture_count == 0; 5591 result->simple = tree->IsAtom() && parser.simple() && capture_count == 0;
5605 result->contains_anchor = parser.contains_anchor(); 5592 result->contains_anchor = parser.contains_anchor();
5606 result->capture_count = capture_count; 5593 result->capture_count = capture_count;
5607 } 5594 }
5608 return !parser.failed(); 5595 return !parser.failed();
5609 } 5596 }
5610 5597
5611 5598
5599 // MakeAST is just a wrapper for the corresponding Parser calls so we don't
5600 // have to expose the entire Parser class in the .h file.
5612 FunctionLiteral* MakeAST(bool compile_in_global_context, 5601 FunctionLiteral* MakeAST(bool compile_in_global_context,
5613 Handle<Script> script, 5602 Handle<Script> script,
5614 v8::Extension* extension, 5603 v8::Extension* extension,
5615 ScriptDataImpl* pre_data, 5604 ScriptDataImpl* pre_data,
5616 bool is_json) { 5605 bool is_json) {
5617 bool allow_natives_syntax = 5606 bool allow_natives_syntax =
5618 always_allow_natives_syntax || 5607 FLAG_allow_natives_syntax || Bootstrapper::IsActive();
5619 FLAG_allow_natives_syntax ||
5620 Bootstrapper::IsActive();
5621 AstBuildingParser parser(script, allow_natives_syntax, extension, pre_data); 5608 AstBuildingParser parser(script, allow_natives_syntax, extension, pre_data);
5622 if (pre_data != NULL && pre_data->has_error()) { 5609 if (pre_data != NULL && pre_data->has_error()) {
5623 Scanner::Location loc = pre_data->MessageLocation(); 5610 Scanner::Location loc = pre_data->MessageLocation();
5624 const char* message = pre_data->BuildMessage(); 5611 const char* message = pre_data->BuildMessage();
5625 Vector<const char*> args = pre_data->BuildArgs(); 5612 Vector<const char*> args = pre_data->BuildArgs();
5626 parser.ReportMessageAt(loc, message, args); 5613 parser.ReportMessageAt(loc, message, args);
5627 DeleteArray(message); 5614 DeleteArray(message);
5628 for (int i = 0; i < args.length(); i++) { 5615 for (int i = 0; i < args.length(); i++) {
5629 DeleteArray(args[i]); 5616 DeleteArray(args[i]);
5630 } 5617 }
5631 DeleteArray(args.start()); 5618 DeleteArray(args.start());
5632 return NULL; 5619 return NULL;
5633 } 5620 }
5634 Handle<String> source = Handle<String>(String::cast(script->source())); 5621 Handle<String> source = Handle<String>(String::cast(script->source()));
5635 FunctionLiteral* result; 5622 FunctionLiteral* result;
5636 if (is_json) { 5623 if (is_json) {
5637 ASSERT(compile_in_global_context); 5624 ASSERT(compile_in_global_context);
5638 result = parser.ParseJson(source); 5625 result = parser.ParseJson(source);
5639 } else { 5626 } else {
5640 result = parser.ParseProgram(source, compile_in_global_context); 5627 result = parser.ParseProgram(source, compile_in_global_context);
5641 } 5628 }
5642 return result; 5629 return result;
5643 } 5630 }
5644 5631
5645 5632
5646 FunctionLiteral* MakeLazyAST(Handle<Script> script, 5633 FunctionLiteral* MakeLazyAST(Handle<SharedFunctionInfo> info) {
5647 Handle<String> name, 5634 Handle<Script> script(Script::cast(info->script()));
5648 int start_position, 5635 AstBuildingParser parser(script, true, NULL, NULL);
5649 int end_position, 5636 FunctionLiteral* result = parser.ParseLazy(info);
5650 bool is_expression) {
5651 bool allow_natives_syntax_before = always_allow_natives_syntax;
5652 always_allow_natives_syntax = true;
5653 AstBuildingParser parser(script, true, NULL, NULL); // always allow
5654 always_allow_natives_syntax = allow_natives_syntax_before;
5655 // Parse the function by pointing to the function source in the script source.
5656 Handle<String> script_source(String::cast(script->source()));
5657 FunctionLiteral* result =
5658 parser.ParseLazy(script_source, name,
5659 start_position, end_position, is_expression);
5660 return result; 5637 return result;
5661 } 5638 }
5662 5639
5663 #undef NEW 5640 #undef NEW
5664 5641
5665 } } // namespace v8::internal 5642 } } // namespace v8::internal
OLDNEW
« src/parser.h ('K') | « src/parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698