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

Side by Side Diff: src/parser.h

Issue 5545006: Optimized scanner to avoid virtual calls for every character read. (Closed)
Patch Set: Addressed review comments. Created 10 years 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
« no previous file with comments | « src/compiler.cc ('k') | src/parser.cc » ('j') | 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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 162
163 163
164 class ParserApi { 164 class ParserApi {
165 public: 165 public:
166 // Parses the source code represented by the compilation info and sets its 166 // Parses the source code represented by the compilation info and sets its
167 // function literal. Returns false (and deallocates any allocated AST 167 // function literal. Returns false (and deallocates any allocated AST
168 // nodes) if parsing failed. 168 // nodes) if parsing failed.
169 static bool Parse(CompilationInfo* info); 169 static bool Parse(CompilationInfo* info);
170 170
171 // Generic preparser generating full preparse data. 171 // Generic preparser generating full preparse data.
172 static ScriptDataImpl* PreParse(Handle<String> source, 172 static ScriptDataImpl* PreParse(UC16CharacterStream* source,
173 unibrow::CharacterStream* stream,
174 v8::Extension* extension); 173 v8::Extension* extension);
175 174
176 // Preparser that only does preprocessing that makes sense if only used 175 // Preparser that only does preprocessing that makes sense if only used
177 // immediately after. 176 // immediately after.
178 static ScriptDataImpl* PartialPreParse(Handle<String> source, 177 static ScriptDataImpl* PartialPreParse(UC16CharacterStream* source,
179 unibrow::CharacterStream* stream,
180 v8::Extension* extension); 178 v8::Extension* extension);
181 }; 179 };
182 180
183 // ---------------------------------------------------------------------------- 181 // ----------------------------------------------------------------------------
184 // REGEXP PARSING 182 // REGEXP PARSING
185 183
186 // A BuffferedZoneList is an automatically growing list, just like (and backed 184 // A BuffferedZoneList is an automatically growing list, just like (and backed
187 // by) a ZoneList, that is optimized for the case of adding and removing 185 // by) a ZoneList, that is optimized for the case of adding and removing
188 // a single element. The last element added is stored outside the backing list, 186 // a single element. The last element added is stored outside the backing list,
189 // and if no more than one element is ever added, the ZoneList isn't even 187 // and if no more than one element is ever added, the ZoneList isn't even
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 FunctionLiteral* ParseProgram(Handle<String> source, 426 FunctionLiteral* ParseProgram(Handle<String> source,
429 bool in_global_context); 427 bool in_global_context);
430 428
431 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info); 429 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info);
432 430
433 void ReportMessageAt(Scanner::Location loc, 431 void ReportMessageAt(Scanner::Location loc,
434 const char* message, 432 const char* message,
435 Vector<const char*> args); 433 Vector<const char*> args);
436 434
437 protected: 435 protected:
436 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info,
437 UC16CharacterStream* source,
438 ZoneScope* zone_scope);
438 enum Mode { 439 enum Mode {
439 PARSE_LAZILY, 440 PARSE_LAZILY,
440 PARSE_EAGERLY 441 PARSE_EAGERLY
441 }; 442 };
442 443
444 // Called by ParseProgram after setting up the scanner.
445 FunctionLiteral* DoParseProgram(Handle<String> source,
446 bool in_global_context,
447 ZoneScope* zone_scope);
448
443 // Report syntax error 449 // Report syntax error
444 void ReportUnexpectedToken(Token::Value token); 450 void ReportUnexpectedToken(Token::Value token);
445 void ReportInvalidPreparseData(Handle<String> name, bool* ok); 451 void ReportInvalidPreparseData(Handle<String> name, bool* ok);
446 void ReportMessage(const char* message, Vector<const char*> args); 452 void ReportMessage(const char* message, Vector<const char*> args);
447 453
448 bool inside_with() const { return with_nesting_level_ > 0; } 454 bool inside_with() const { return with_nesting_level_ > 0; }
449 Scanner& scanner() { return scanner_; } 455 V8JavaScriptScanner& scanner() { return scanner_; }
450 Mode mode() const { return mode_; } 456 Mode mode() const { return mode_; }
451 ScriptDataImpl* pre_data() const { return pre_data_; } 457 ScriptDataImpl* pre_data() const { return pre_data_; }
452 458
453 // All ParseXXX functions take as the last argument an *ok parameter 459 // All ParseXXX functions take as the last argument an *ok parameter
454 // which is set to false if parsing failed; it is unchanged otherwise. 460 // which is set to false if parsing failed; it is unchanged otherwise.
455 // By making the 'exception handling' explicit, we are forced to check 461 // By making the 'exception handling' explicit, we are forced to check
456 // for failure at the call sites. 462 // for failure at the call sites.
457 void* ParseSourceElements(ZoneList<Statement*>* processor, 463 void* ParseSourceElements(ZoneList<Statement*>* processor,
458 int end_token, bool* ok); 464 int end_token, bool* ok);
459 Statement* ParseStatement(ZoneStringList* labels, bool* ok); 465 Statement* ParseStatement(ZoneStringList* labels, bool* ok);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 int function_token_position, 547 int function_token_position,
542 FunctionLiteralType type, 548 FunctionLiteralType type,
543 bool* ok); 549 bool* ok);
544 550
545 551
546 // Magical syntax support. 552 // Magical syntax support.
547 Expression* ParseV8Intrinsic(bool* ok); 553 Expression* ParseV8Intrinsic(bool* ok);
548 554
549 INLINE(Token::Value peek()) { 555 INLINE(Token::Value peek()) {
550 if (stack_overflow_) return Token::ILLEGAL; 556 if (stack_overflow_) return Token::ILLEGAL;
551 return scanner_.peek(); 557 return scanner().peek();
552 } 558 }
553 559
554 INLINE(Token::Value Next()) { 560 INLINE(Token::Value Next()) {
555 // BUG 1215673: Find a thread safe way to set a stack limit in 561 // BUG 1215673: Find a thread safe way to set a stack limit in
556 // pre-parse mode. Otherwise, we cannot safely pre-parse from other 562 // pre-parse mode. Otherwise, we cannot safely pre-parse from other
557 // threads. 563 // threads.
558 if (stack_overflow_) { 564 if (stack_overflow_) {
559 return Token::ILLEGAL; 565 return Token::ILLEGAL;
560 } 566 }
561 if (StackLimitCheck().HasOverflowed()) { 567 if (StackLimitCheck().HasOverflowed()) {
562 // Any further calls to Next or peek will return the illegal token. 568 // Any further calls to Next or peek will return the illegal token.
569 // The current call must return the next token, which might already
570 // have been peek'ed.
563 stack_overflow_ = true; 571 stack_overflow_ = true;
564 } 572 }
565 return scanner_.Next(); 573 return scanner().Next();
566 } 574 }
567 575
568 INLINE(void Consume(Token::Value token)); 576 INLINE(void Consume(Token::Value token));
569 void Expect(Token::Value token, bool* ok); 577 void Expect(Token::Value token, bool* ok);
570 bool Check(Token::Value token); 578 bool Check(Token::Value token);
571 void ExpectSemicolon(bool* ok); 579 void ExpectSemicolon(bool* ok);
572 580
573 Handle<String> GetSymbol(bool* ok); 581 Handle<String> GetSymbol(bool* ok);
574 582
575 // Get odd-ball literals. 583 // Get odd-ball literals.
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 // JSON PARSING 703 // JSON PARSING
696 704
697 // JSON is a subset of JavaScript, as specified in, e.g., the ECMAScript 5 705 // JSON is a subset of JavaScript, as specified in, e.g., the ECMAScript 5
698 // specification section 15.12.1 (and appendix A.8). 706 // specification section 15.12.1 (and appendix A.8).
699 // The grammar is given section 15.12.1.2 (and appendix A.8.2). 707 // The grammar is given section 15.12.1.2 (and appendix A.8.2).
700 class JsonParser BASE_EMBEDDED { 708 class JsonParser BASE_EMBEDDED {
701 public: 709 public:
702 // Parse JSON input as a single JSON value. 710 // Parse JSON input as a single JSON value.
703 // Returns null handle and sets exception if parsing failed. 711 // Returns null handle and sets exception if parsing failed.
704 static Handle<Object> Parse(Handle<String> source) { 712 static Handle<Object> Parse(Handle<String> source) {
705 return JsonParser().ParseJson(source); 713 if (source->IsExternalTwoByteString()) {
714 ExternalTwoByteStringUC16CharacterStream stream(
715 Handle<ExternalTwoByteString>::cast(source), 0, source->length());
716 return JsonParser().ParseJson(source, &stream);
717 } else {
718 GenericStringUC16CharacterStream stream(source, 0, source->length());
719 return JsonParser().ParseJson(source, &stream);
720 }
706 } 721 }
707 722
708 private: 723 private:
709 JsonParser() { } 724 JsonParser() { }
710 ~JsonParser() { } 725 ~JsonParser() { }
711 726
712 // Parse a string containing a single JSON value. 727 // Parse a string containing a single JSON value.
713 Handle<Object> ParseJson(Handle<String>); 728 Handle<Object> ParseJson(Handle<String> script, UC16CharacterStream* source);
714 // Parse a single JSON value from input (grammar production JSONValue). 729 // Parse a single JSON value from input (grammar production JSONValue).
715 // A JSON value is either a (double-quoted) string literal, a number literal, 730 // A JSON value is either a (double-quoted) string literal, a number literal,
716 // one of "true", "false", or "null", or an object or array literal. 731 // one of "true", "false", or "null", or an object or array literal.
717 Handle<Object> ParseJsonValue(); 732 Handle<Object> ParseJsonValue();
718 // Parse a JSON object literal (grammar production JSONObject). 733 // Parse a JSON object literal (grammar production JSONObject).
719 // An object literal is a squiggly-braced and comma separated sequence 734 // An object literal is a squiggly-braced and comma separated sequence
720 // (possibly empty) of key/value pairs, where the key is a JSON string 735 // (possibly empty) of key/value pairs, where the key is a JSON string
721 // literal, the value is a JSON value, and the two are separated by a colon. 736 // literal, the value is a JSON value, and the two are separated by a colon.
722 // A JSON array dosn't allow numbers and identifiers as keys, like a 737 // A JSON array dosn't allow numbers and identifiers as keys, like a
723 // JavaScript array. 738 // JavaScript array.
(...skipping 10 matching lines...) Expand all
734 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); } 749 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); }
735 // Converts the currently parsed literal to a JavaScript String. 750 // Converts the currently parsed literal to a JavaScript String.
736 Handle<String> GetString(); 751 Handle<String> GetString();
737 752
738 JsonScanner scanner_; 753 JsonScanner scanner_;
739 bool stack_overflow_; 754 bool stack_overflow_;
740 }; 755 };
741 } } // namespace v8::internal 756 } } // namespace v8::internal
742 757
743 #endif // V8_PARSER_H_ 758 #endif // V8_PARSER_H_
OLDNEW
« no previous file with comments | « src/compiler.cc ('k') | src/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698