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

Side by Side Diff: src/lexer/lexer.cc

Issue 180743019: Experimental parser: more cleanup after rebase (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « src/lexer/lexer.h ('k') | src/lexer/lexer-shell.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 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 reinterpret_cast<i::Isolate*>(isolate)-> 106 reinterpret_cast<i::Isolate*>(isolate)->
107 lexer_gc_handler()->UpdateLexersAfterGC(); 107 lexer_gc_handler()->UpdateLexersAfterGC();
108 } 108 }
109 109
110 110
111 void LexerGCHandler::AddLexer(LexerBase* lexer) { 111 void LexerGCHandler::AddLexer(LexerBase* lexer) {
112 if (lexers_.empty()) { 112 if (lexers_.empty()) {
113 isolate_->heap()->AddGCEpilogueCallback( 113 isolate_->heap()->AddGCEpilogueCallback(
114 &i::UpdateLexersAfterGC, kGCTypeAll, true); 114 &i::UpdateLexersAfterGC, kGCTypeAll, true);
115 } 115 }
116 lexers_.insert(lexer); 116 std::pair<LexerSet::iterator, bool> res = lexers_.insert(lexer);
117 USE(res);
118 ASSERT(res.second);
117 } 119 }
118 120
119 121
120 void LexerGCHandler::RemoveLexer(LexerBase* lexer) { 122 void LexerGCHandler::RemoveLexer(LexerBase* lexer) {
121 lexers_.erase(lexer); 123 LexerSet::iterator it = lexers_.find(lexer);
124 ASSERT(it != lexers_.end());
125 lexers_.erase(it);
122 if (lexers_.empty()) { 126 if (lexers_.empty()) {
123 isolate_->heap()->RemoveGCEpilogueCallback(&i::UpdateLexersAfterGC); 127 isolate_->heap()->RemoveGCEpilogueCallback(&i::UpdateLexersAfterGC);
124 } 128 }
125 } 129 }
126 130
127 131
128 void LexerGCHandler::UpdateLexersAfterGC() { 132 void LexerGCHandler::UpdateLexersAfterGC() {
129 typedef std::set<LexerBase*>::const_iterator It; 133 typedef std::set<LexerBase*>::const_iterator It;
130 for (It it = lexers_.begin(); it != lexers_.end(); ++it) { 134 for (It it = lexers_.begin(); it != lexers_.end(); ++it) {
131 (*it)->UpdateBufferBasedOnHandle(); 135 (*it)->UpdateBufferBasedOnHandle();
132 } 136 }
133 } 137 }
134 138
135 139
140 LexerBase::LexerBase(UnicodeCache* unicode_cache)
141 : unicode_cache_(unicode_cache),
142 has_line_terminator_before_next_(true),
143 has_multiline_comment_before_next_(false),
144 current_literal_(&literals_[0]),
145 next_literal_(&literals_[1]),
146 harmony_numeric_literals_(false),
147 harmony_modules_(false),
148 harmony_scoping_(false) {
149 }
150
151
136 LexerBase::~LexerBase() {} 152 LexerBase::~LexerBase() {}
137 153
138 154
139 // Returns the next token and advances input. 155 // Returns the next token and advances input.
140 Token::Value LexerBase::Next() { 156 Token::Value LexerBase::Next() {
141 has_line_terminator_before_next_ = false; 157 has_line_terminator_before_next_ = false;
142 has_multiline_comment_before_next_ = false; 158 has_multiline_comment_before_next_ = false;
143 current_ = next_; 159 current_ = next_;
144 std::swap(current_literal_, next_literal_); 160 std::swap(current_literal_, next_literal_);
145 Scan(); 161 Scan();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 start_(NULL), 197 start_(NULL),
182 cursor_(NULL), 198 cursor_(NULL),
183 last_octal_end_(NULL) { 199 last_octal_end_(NULL) {
184 UpdateBufferBasedOnHandle(); 200 UpdateBufferBasedOnHandle();
185 current_.beg_pos = current_.end_pos = next_.beg_pos = next_.end_pos = 0; 201 current_.beg_pos = current_.end_pos = next_.beg_pos = next_.end_pos = 0;
186 isolate_->lexer_gc_handler()->AddLexer(this); 202 isolate_->lexer_gc_handler()->AddLexer(this);
187 // TODO(dcarney): move this to UpdateBufferBasedOnHandle 203 // TODO(dcarney): move this to UpdateBufferBasedOnHandle
188 cursor_ = buffer_ + start_position; 204 cursor_ = buffer_ + start_position;
189 buffer_end_ = buffer_ + end_position; 205 buffer_end_ = buffer_ + end_position;
190 start_ = cursor_; 206 start_ = cursor_;
191 has_line_terminator_before_next_ = false;
192 has_multiline_comment_before_next_ = false;
193 } 207 }
194 208
195 209
196 template<typename Char> 210 template<typename Char>
197 Lexer<Char>::~Lexer() { 211 Lexer<Char>::~Lexer() {
198 if (!source_handle_.is_null()) { 212 if (!source_handle_.is_null()) {
199 isolate_->lexer_gc_handler()->RemoveLexer(this); 213 isolate_->lexer_gc_handler()->RemoveLexer(this);
200 } 214 }
201 } 215 }
202 216
203 217
204 template<typename Char> 218 template<typename Char>
205 void Lexer<Char>::SeekForward(int pos) { 219 void Lexer<Char>::SeekForward(int pos) {
206 cursor_ = buffer_ + pos; 220 cursor_ = buffer_ + pos;
207 start_ = cursor_; 221 start_ = cursor_;
208 has_line_terminator_before_next_ = false; 222 has_line_terminator_before_next_ = false;
209 has_multiline_comment_before_next_ = false; 223 has_multiline_comment_before_next_ = false;
210 Scan(); // Fills in next_. 224 Scan(); // Fills in next_.
211 } 225 }
212 226
213 227
214 template<typename Char> 228 template<typename Char>
215 void Lexer<Char>::SetEnd(int pos) {
216 buffer_end_ = buffer_ + pos;
217 }
218
219
220 template<typename Char>
221 bool Lexer<Char>::ScanRegExpPattern(bool seen_equal) { 229 bool Lexer<Char>::ScanRegExpPattern(bool seen_equal) {
222 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags 230 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags
223 bool in_character_class = false; 231 bool in_character_class = false;
224 232
225 // Previous token is either '/' or '/=', in the second case, the 233 // Previous token is either '/' or '/=', in the second case, the
226 // pattern starts at =. 234 // pattern starts at =.
227 next_.beg_pos = next_.end_pos = (cursor_ - buffer_) - (seen_equal ? 1 : 0); 235 next_.beg_pos = next_.end_pos = (cursor_ - buffer_) - (seen_equal ? 1 : 0);
228 236
229 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5, 237 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5,
230 // the scanner should pass uninterpreted bodies to the RegExp 238 // the scanner should pass uninterpreted bodies to the RegExp
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 bool Lexer<uint8_t>::FillLiteral( 519 bool Lexer<uint8_t>::FillLiteral(
512 const TokenDesc& token, LiteralDesc* literal) { 520 const TokenDesc& token, LiteralDesc* literal) {
513 literal->beg_pos = token.beg_pos; 521 literal->beg_pos = token.beg_pos;
514 const uint8_t* start = buffer_ + token.beg_pos; 522 const uint8_t* start = buffer_ + token.beg_pos;
515 const uint8_t* end = buffer_ + token.end_pos; 523 const uint8_t* end = buffer_ + token.end_pos;
516 if (token.token == Token::STRING) { 524 if (token.token == Token::STRING) {
517 ++start; 525 ++start;
518 --end; 526 --end;
519 } 527 }
520 if (IsSubstringOfSource(token)) { 528 if (IsSubstringOfSource(token)) {
521 literal->is_ascii = true; 529 literal->is_one_byte = true;
522 literal->is_in_buffer = false; 530 literal->is_in_buffer = false;
523 literal->offset = start - buffer_; 531 literal->offset = start - buffer_;
524 literal->length = end - start; 532 literal->length = end - start;
525 literal->ascii_string = Vector<const char>( 533 literal->one_byte_string = Vector<const uint8_t>(start, literal->length);
526 reinterpret_cast<const char*>(start), literal->length);
527 return true; 534 return true;
528 } 535 }
529 return CopyToLiteralBuffer(start, end, token, literal); 536 return CopyToLiteralBuffer(start, end, token, literal);
530 } 537 }
531 538
532 539
533 template<> 540 template<>
534 bool Lexer<uint16_t>::FillLiteral( 541 bool Lexer<uint16_t>::FillLiteral(
535 const TokenDesc& token, LiteralDesc* literal) { 542 const TokenDesc& token, LiteralDesc* literal) {
536 literal->beg_pos = token.beg_pos; 543 literal->beg_pos = token.beg_pos;
537 const uint16_t* start = buffer_ + token.beg_pos; 544 const uint16_t* start = buffer_ + token.beg_pos;
538 const uint16_t* end = buffer_ + token.end_pos; 545 const uint16_t* end = buffer_ + token.end_pos;
539 if (token.token == Token::STRING) { 546 if (token.token == Token::STRING) {
540 ++start; 547 ++start;
541 --end; 548 --end;
542 } 549 }
543 if (IsSubstringOfSource(token)) { 550 if (IsSubstringOfSource(token)) {
544 literal->is_ascii = false; 551 literal->is_one_byte = false;
545 literal->is_in_buffer = false; 552 literal->is_in_buffer = false;
546 literal->offset = start - buffer_; 553 literal->offset = start - buffer_;
547 literal->length = end - start; 554 literal->length = end - start;
548 literal->utf16_string = Vector<const uint16_t>(start, literal->length); 555 literal->two_byte_string = Vector<const uint16_t>(start, literal->length);
549 return true; 556 return true;
550 } 557 }
551 return CopyToLiteralBuffer(start, end, token, literal); 558 return CopyToLiteralBuffer(start, end, token, literal);
552 } 559 }
553 560
554 561
555 template<> 562 template<>
556 bool Lexer<int8_t>::FillLiteral( 563 bool Lexer<int8_t>::FillLiteral(
557 const TokenDesc& token, LiteralDesc* literal) { 564 const TokenDesc& token, LiteralDesc* literal) {
558 // FIXME: implement. 565 // FIXME: implement.
(...skipping 22 matching lines...) Expand all
581 cursor = ScanEscape(cursor, end, &literal->buffer); 588 cursor = ScanEscape(cursor, end, &literal->buffer);
582 ASSERT(cursor != NULL); 589 ASSERT(cursor != NULL);
583 if (cursor == NULL) return false; 590 if (cursor == NULL) return false;
584 } 591 }
585 } 592 }
586 } else { 593 } else {
587 for (const Char* cursor = start; cursor != end;) { 594 for (const Char* cursor = start; cursor != end;) {
588 literal->buffer.AddChar(*cursor++); 595 literal->buffer.AddChar(*cursor++);
589 } 596 }
590 } 597 }
591 literal->is_ascii = literal->buffer.is_ascii(); 598 literal->is_one_byte = literal->buffer.is_ascii();
592 literal->is_in_buffer = true; 599 literal->is_in_buffer = true;
593 literal->length = literal->buffer.length(); 600 literal->length = literal->buffer.length();
594 if (literal->is_ascii) { 601 if (literal->is_one_byte) {
595 literal->ascii_string = literal->buffer.ascii_literal(); 602 literal->one_byte_string =
603 Vector<const uint8_t>::cast(literal->buffer.ascii_literal());
596 } else { 604 } else {
597 literal->utf16_string = literal->buffer.utf16_literal(); 605 literal->two_byte_string = literal->buffer.utf16_literal();
598 } 606 }
599 return true; 607 return true;
600 } 608 }
601 609
602 610
603 template<class Char> 611 template<class Char>
604 Handle<String> Lexer<Char>::InternalizeLiteral( 612 Handle<String> Lexer<Char>::InternalizeLiteral(
605 LiteralDesc* literal) { 613 LiteralDesc* literal) {
606 Factory* factory = isolate_->factory(); 614 Factory* factory = isolate_->factory();
607 if (literal->is_in_buffer) { 615 if (literal->is_in_buffer) {
608 return literal->is_ascii 616 return literal->is_one_byte
609 ? factory->InternalizeOneByteString( 617 ? factory->InternalizeOneByteString(
610 Vector<const uint8_t>::cast(literal->ascii_string)) 618 Vector<const uint8_t>::cast(literal->one_byte_string))
611 : factory->InternalizeTwoByteString(literal->utf16_string); 619 : factory->InternalizeTwoByteString(literal->two_byte_string);
612 } 620 }
613 if (sizeof(Char) == 1) { 621 if (sizeof(Char) == 1) {
614 SubStringKey<uint8_t> key( 622 SubStringKey<uint8_t> key(
615 source_handle_, literal->offset, literal->length); 623 source_handle_, literal->offset, literal->length);
616 return factory->InternalizeStringWithKey(&key); 624 return factory->InternalizeStringWithKey(&key);
617 } else { 625 } else {
618 SubStringKey<uint16_t> key( 626 SubStringKey<uint16_t> key(
619 source_handle_, literal->offset, literal->length); 627 source_handle_, literal->offset, literal->length);
620 return factory->InternalizeStringWithKey(&key); 628 return factory->InternalizeStringWithKey(&key);
621 } 629 }
622 } 630 }
623 631
624 632
625 template<> 633 template<>
626 Handle<String> Lexer<uint8_t>::AllocateLiteral( 634 Handle<String> Lexer<uint8_t>::AllocateLiteral(
627 LiteralDesc* literal, PretenureFlag pretenured) { 635 LiteralDesc* literal, PretenureFlag pretenured) {
628 Factory* factory = isolate_->factory(); 636 Factory* factory = isolate_->factory();
629 if (literal->is_in_buffer) { 637 if (literal->is_in_buffer) {
630 return literal->is_ascii 638 return literal->is_one_byte
631 ? factory->NewStringFromAscii(literal->ascii_string, pretenured) 639 ? factory->NewStringFromOneByte(literal->one_byte_string, pretenured)
632 : factory->NewStringFromTwoByte(literal->utf16_string, pretenured); 640 : factory->NewStringFromTwoByte(literal->two_byte_string, pretenured);
633 } 641 }
634 int from = literal->offset; 642 int from = literal->offset;
635 int length = literal->length; 643 int length = literal->length;
636 // Save the offset and the length before allocating the string as it may 644 // Save the offset and the length before allocating the string as it may
637 // cause a GC, invalidate the literal, and move the source. 645 // cause a GC, invalidate the literal, and move the source.
638 Handle<String> result = factory->NewRawOneByteString(length, pretenured); 646 Handle<String> result = factory->NewRawOneByteString(length, pretenured);
639 uint8_t* chars = SeqOneByteString::cast(*result)->GetChars(); 647 uint8_t* chars = SeqOneByteString::cast(*result)->GetChars();
640 String::WriteToFlat(*source_handle_, chars, from, from + length); 648 String::WriteToFlat(*source_handle_, chars, from, from + length);
641 return result; 649 return result;
642 } 650 }
643 651
644 652
645 template<> 653 template<>
646 Handle<String> Lexer<uint16_t>::AllocateLiteral( 654 Handle<String> Lexer<uint16_t>::AllocateLiteral(
647 LiteralDesc* literal, PretenureFlag pretenured) { 655 LiteralDesc* literal, PretenureFlag pretenured) {
648 Factory* factory = isolate_->factory(); 656 Factory* factory = isolate_->factory();
649 if (literal->is_in_buffer) { 657 if (literal->is_in_buffer) {
650 return literal->is_ascii 658 return literal->is_one_byte
651 ? factory->NewStringFromAscii(literal->ascii_string, pretenured) 659 ? factory->NewStringFromOneByte(literal->one_byte_string, pretenured)
652 : factory->NewStringFromTwoByte(literal->utf16_string, pretenured); 660 : factory->NewStringFromTwoByte(literal->two_byte_string, pretenured);
653 } 661 }
654 // Save the offset and the length before allocating the string as it may 662 // Save the offset and the length before allocating the string as it may
655 // cause a GC, invalidate the literal, and move the source. 663 // cause a GC, invalidate the literal, and move the source.
656 int from = literal->offset; 664 int from = literal->offset;
657 int length = literal->length; 665 int length = literal->length;
658 Handle<String> result = factory->NewRawTwoByteString(length, pretenured); 666 Handle<String> result = factory->NewRawTwoByteString(length, pretenured);
659 uint16_t* chars = SeqTwoByteString::cast(*result)->GetChars(); 667 uint16_t* chars = SeqTwoByteString::cast(*result)->GetChars();
660 String::WriteToFlat(*source_handle_, chars, from, from + length); 668 String::WriteToFlat(*source_handle_, chars, from, from + length);
661 return result; 669 return result;
662 } 670 }
663 671
664 672
665 template<> 673 template<>
666 Handle<String> Lexer<int8_t>::AllocateLiteral( 674 Handle<String> Lexer<int8_t>::AllocateLiteral(
667 LiteralDesc* literal, PretenureFlag pretenured) { 675 LiteralDesc* literal, PretenureFlag pretenured) {
668 // FIXME: implement 676 // FIXME: implement
669 UNREACHABLE(); 677 UNREACHABLE();
670 return Handle<String>(); 678 return Handle<String>();
671 } 679 }
672 680
673 template class Lexer<uint8_t>; 681 template class Lexer<uint8_t>;
674 template class Lexer<uint16_t>; 682 template class Lexer<uint16_t>;
675 template class Lexer<int8_t>; 683 template class Lexer<int8_t>;
676 684
677 } } // v8::internal 685 } } // v8::internal
OLDNEW
« no previous file with comments | « src/lexer/lexer.h ('k') | src/lexer/lexer-shell.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698