| OLD | NEW |
| 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 Loading... |
| 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 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 FunctionLiteral* ParseProgram(Handle<String> source, | 429 FunctionLiteral* ParseProgram(Handle<String> source, |
| 432 bool in_global_context); | 430 bool in_global_context); |
| 433 | 431 |
| 434 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info); | 432 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info); |
| 435 | 433 |
| 436 void ReportMessageAt(Scanner::Location loc, | 434 void ReportMessageAt(Scanner::Location loc, |
| 437 const char* message, | 435 const char* message, |
| 438 Vector<const char*> args); | 436 Vector<const char*> args); |
| 439 | 437 |
| 440 protected: | 438 protected: |
| 439 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info, |
| 440 UC16CharacterStream* source, |
| 441 ZoneScope* zone_scope); |
| 441 enum Mode { | 442 enum Mode { |
| 442 PARSE_LAZILY, | 443 PARSE_LAZILY, |
| 443 PARSE_EAGERLY | 444 PARSE_EAGERLY |
| 444 }; | 445 }; |
| 445 | 446 |
| 446 Isolate* isolate() { return isolate_; } | 447 Isolate* isolate() { return isolate_; } |
| 447 | 448 |
| 449 // Called by ParseProgram after setting up the scanner. |
| 450 FunctionLiteral* DoParseProgram(Handle<String> source, |
| 451 bool in_global_context, |
| 452 ZoneScope* zone_scope); |
| 453 |
| 448 // Report syntax error | 454 // Report syntax error |
| 449 void ReportUnexpectedToken(Token::Value token); | 455 void ReportUnexpectedToken(Token::Value token); |
| 450 void ReportInvalidPreparseData(Handle<String> name, bool* ok); | 456 void ReportInvalidPreparseData(Handle<String> name, bool* ok); |
| 451 void ReportMessage(const char* message, Vector<const char*> args); | 457 void ReportMessage(const char* message, Vector<const char*> args); |
| 452 | 458 |
| 453 bool inside_with() const { return with_nesting_level_ > 0; } | 459 bool inside_with() const { return with_nesting_level_ > 0; } |
| 454 Scanner& scanner() { return scanner_; } | 460 V8JavaScriptScanner& scanner() { return scanner_; } |
| 455 Mode mode() const { return mode_; } | 461 Mode mode() const { return mode_; } |
| 456 ScriptDataImpl* pre_data() const { return pre_data_; } | 462 ScriptDataImpl* pre_data() const { return pre_data_; } |
| 457 | 463 |
| 458 // All ParseXXX functions take as the last argument an *ok parameter | 464 // All ParseXXX functions take as the last argument an *ok parameter |
| 459 // which is set to false if parsing failed; it is unchanged otherwise. | 465 // which is set to false if parsing failed; it is unchanged otherwise. |
| 460 // By making the 'exception handling' explicit, we are forced to check | 466 // By making the 'exception handling' explicit, we are forced to check |
| 461 // for failure at the call sites. | 467 // for failure at the call sites. |
| 462 void* ParseSourceElements(ZoneList<Statement*>* processor, | 468 void* ParseSourceElements(ZoneList<Statement*>* processor, |
| 463 int end_token, bool* ok); | 469 int end_token, bool* ok); |
| 464 Statement* ParseStatement(ZoneStringList* labels, bool* ok); | 470 Statement* ParseStatement(ZoneStringList* labels, bool* ok); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 int function_token_position, | 552 int function_token_position, |
| 547 FunctionLiteralType type, | 553 FunctionLiteralType type, |
| 548 bool* ok); | 554 bool* ok); |
| 549 | 555 |
| 550 | 556 |
| 551 // Magical syntax support. | 557 // Magical syntax support. |
| 552 Expression* ParseV8Intrinsic(bool* ok); | 558 Expression* ParseV8Intrinsic(bool* ok); |
| 553 | 559 |
| 554 INLINE(Token::Value peek()) { | 560 INLINE(Token::Value peek()) { |
| 555 if (stack_overflow_) return Token::ILLEGAL; | 561 if (stack_overflow_) return Token::ILLEGAL; |
| 556 return scanner_.peek(); | 562 return scanner().peek(); |
| 557 } | 563 } |
| 558 | 564 |
| 559 INLINE(Token::Value Next()) { | 565 INLINE(Token::Value Next()) { |
| 560 // BUG 1215673: Find a thread safe way to set a stack limit in | 566 // BUG 1215673: Find a thread safe way to set a stack limit in |
| 561 // pre-parse mode. Otherwise, we cannot safely pre-parse from other | 567 // pre-parse mode. Otherwise, we cannot safely pre-parse from other |
| 562 // threads. | 568 // threads. |
| 563 if (stack_overflow_) { | 569 if (stack_overflow_) { |
| 564 return Token::ILLEGAL; | 570 return Token::ILLEGAL; |
| 565 } | 571 } |
| 566 if (StackLimitCheck(isolate()).HasOverflowed()) { | 572 if (StackLimitCheck(isolate()).HasOverflowed()) { |
| 567 // Any further calls to Next or peek will return the illegal token. | 573 // Any further calls to Next or peek will return the illegal token. |
| 574 // The current call must return the next token, which might already |
| 575 // have been peek'ed. |
| 568 stack_overflow_ = true; | 576 stack_overflow_ = true; |
| 569 } | 577 } |
| 570 return scanner_.Next(); | 578 return scanner().Next(); |
| 571 } | 579 } |
| 572 | 580 |
| 573 INLINE(void Consume(Token::Value token)); | 581 INLINE(void Consume(Token::Value token)); |
| 574 void Expect(Token::Value token, bool* ok); | 582 void Expect(Token::Value token, bool* ok); |
| 575 bool Check(Token::Value token); | 583 bool Check(Token::Value token); |
| 576 void ExpectSemicolon(bool* ok); | 584 void ExpectSemicolon(bool* ok); |
| 577 | 585 |
| 578 Handle<String> GetSymbol(bool* ok); | 586 Handle<String> GetSymbol(bool* ok); |
| 579 | 587 |
| 580 // Get odd-ball literals. | 588 // Get odd-ball literals. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 // JSON PARSING | 709 // JSON PARSING |
| 702 | 710 |
| 703 // JSON is a subset of JavaScript, as specified in, e.g., the ECMAScript 5 | 711 // JSON is a subset of JavaScript, as specified in, e.g., the ECMAScript 5 |
| 704 // specification section 15.12.1 (and appendix A.8). | 712 // specification section 15.12.1 (and appendix A.8). |
| 705 // The grammar is given section 15.12.1.2 (and appendix A.8.2). | 713 // The grammar is given section 15.12.1.2 (and appendix A.8.2). |
| 706 class JsonParser BASE_EMBEDDED { | 714 class JsonParser BASE_EMBEDDED { |
| 707 public: | 715 public: |
| 708 // Parse JSON input as a single JSON value. | 716 // Parse JSON input as a single JSON value. |
| 709 // Returns null handle and sets exception if parsing failed. | 717 // Returns null handle and sets exception if parsing failed. |
| 710 static Handle<Object> Parse(Handle<String> source) { | 718 static Handle<Object> Parse(Handle<String> source) { |
| 711 return JsonParser().ParseJson(source); | 719 if (source->IsExternalTwoByteString()) { |
| 720 ExternalTwoByteStringUC16CharacterStream stream( |
| 721 Handle<ExternalTwoByteString>::cast(source), 0, source->length()); |
| 722 return JsonParser().ParseJson(source, &stream); |
| 723 } else { |
| 724 GenericStringUC16CharacterStream stream(source, 0, source->length()); |
| 725 return JsonParser().ParseJson(source, &stream); |
| 726 } |
| 712 } | 727 } |
| 713 | 728 |
| 714 private: | 729 private: |
| 715 JsonParser() : isolate_(Isolate::Current()) { } | 730 JsonParser() : isolate_(Isolate::Current()), scanner_(isolate_) { } |
| 716 ~JsonParser() { } | 731 ~JsonParser() { } |
| 717 | 732 |
| 718 Isolate* isolate() { return isolate_; } | 733 Isolate* isolate() { return isolate_; } |
| 719 | 734 |
| 720 // Parse a string containing a single JSON value. | 735 // Parse a string containing a single JSON value. |
| 721 Handle<Object> ParseJson(Handle<String>); | 736 Handle<Object> ParseJson(Handle<String> script, UC16CharacterStream* source); |
| 722 // Parse a single JSON value from input (grammar production JSONValue). | 737 // Parse a single JSON value from input (grammar production JSONValue). |
| 723 // A JSON value is either a (double-quoted) string literal, a number literal, | 738 // A JSON value is either a (double-quoted) string literal, a number literal, |
| 724 // one of "true", "false", or "null", or an object or array literal. | 739 // one of "true", "false", or "null", or an object or array literal. |
| 725 Handle<Object> ParseJsonValue(); | 740 Handle<Object> ParseJsonValue(); |
| 726 // Parse a JSON object literal (grammar production JSONObject). | 741 // Parse a JSON object literal (grammar production JSONObject). |
| 727 // An object literal is a squiggly-braced and comma separated sequence | 742 // An object literal is a squiggly-braced and comma separated sequence |
| 728 // (possibly empty) of key/value pairs, where the key is a JSON string | 743 // (possibly empty) of key/value pairs, where the key is a JSON string |
| 729 // literal, the value is a JSON value, and the two are separated by a colon. | 744 // literal, the value is a JSON value, and the two are separated by a colon. |
| 730 // A JSON array dosn't allow numbers and identifiers as keys, like a | 745 // A JSON array dosn't allow numbers and identifiers as keys, like a |
| 731 // JavaScript array. | 746 // JavaScript array. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 743 // Converts the currently parsed literal to a JavaScript String. | 758 // Converts the currently parsed literal to a JavaScript String. |
| 744 Handle<String> GetString(); | 759 Handle<String> GetString(); |
| 745 | 760 |
| 746 Isolate* isolate_; | 761 Isolate* isolate_; |
| 747 JsonScanner scanner_; | 762 JsonScanner scanner_; |
| 748 bool stack_overflow_; | 763 bool stack_overflow_; |
| 749 }; | 764 }; |
| 750 } } // namespace v8::internal | 765 } } // namespace v8::internal |
| 751 | 766 |
| 752 #endif // V8_PARSER_H_ | 767 #endif // V8_PARSER_H_ |
| OLD | NEW |