| 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 30 matching lines...) Expand all Loading... |
| 41 BufferedUC16CharacterStream::BufferedUC16CharacterStream() | 41 BufferedUC16CharacterStream::BufferedUC16CharacterStream() |
| 42 : UC16CharacterStream(), | 42 : UC16CharacterStream(), |
| 43 pushback_limit_(NULL) { | 43 pushback_limit_(NULL) { |
| 44 // Initialize buffer as being empty. First read will fill the buffer. | 44 // Initialize buffer as being empty. First read will fill the buffer. |
| 45 buffer_cursor_ = buffer_; | 45 buffer_cursor_ = buffer_; |
| 46 buffer_end_ = buffer_; | 46 buffer_end_ = buffer_; |
| 47 } | 47 } |
| 48 | 48 |
| 49 BufferedUC16CharacterStream::~BufferedUC16CharacterStream() { } | 49 BufferedUC16CharacterStream::~BufferedUC16CharacterStream() { } |
| 50 | 50 |
| 51 void BufferedUC16CharacterStream::PushBack(uc16 character) { | 51 void BufferedUC16CharacterStream::PushBack(uc32 character) { |
| 52 if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) { | 52 if (character == kEndOfInput) { |
| 53 // buffer_ is writable, buffer_cursor_ is const pointer. | |
| 54 buffer_[--buffer_cursor_ - buffer_] = character; | |
| 55 pos_--; | 53 pos_--; |
| 56 return; | 54 return; |
| 57 } | 55 } |
| 58 SlowPushBack(character); | 56 if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) { |
| 57 // buffer_ is writable, buffer_cursor_ is const pointer. |
| 58 buffer_[--buffer_cursor_ - buffer_] = static_cast<uc16>(character); |
| 59 pos_--; |
| 60 return; |
| 61 } |
| 62 SlowPushBack(static_cast<uc16>(character)); |
| 59 } | 63 } |
| 60 | 64 |
| 61 | 65 |
| 62 void BufferedUC16CharacterStream::SlowPushBack(uc16 character) { | 66 void BufferedUC16CharacterStream::SlowPushBack(uc16 character) { |
| 63 // In pushback mode, the end of the buffer contains pushback, | 67 // In pushback mode, the end of the buffer contains pushback, |
| 64 // and the start of the buffer (from buffer start to pushback_limit_) | 68 // and the start of the buffer (from buffer start to pushback_limit_) |
| 65 // contains valid data that comes just after the pushback. | 69 // contains valid data that comes just after the pushback. |
| 66 // We NULL the pushback_limit_ if pushing all the way back to the | 70 // We NULL the pushback_limit_ if pushing all the way back to the |
| 67 // start of the buffer. | 71 // start of the buffer. |
| 68 | 72 |
| 69 if (pushback_limit_ == NULL) { | 73 if (pushback_limit_ == NULL) { |
| 70 // Enter pushback mode. | 74 // Enter pushback mode. |
| 71 pushback_limit_ = buffer_end_; | 75 pushback_limit_ = buffer_end_; |
| 72 buffer_end_ = buffer_ + kBufferSize; | 76 buffer_end_ = buffer_ + kBufferSize; |
| 73 buffer_cursor_ = buffer_end_; | 77 buffer_cursor_ = buffer_end_; |
| 74 } | 78 } |
| 75 ASSERT(pushback_limit_ > buffer_); | 79 // Ensure that there is room for at least one pushback. |
| 80 ASSERT(buffer_cursor_ > buffer_); |
| 76 ASSERT(pos_ > 0); | 81 ASSERT(pos_ > 0); |
| 77 buffer_[--buffer_cursor_ - buffer_] = character; | 82 buffer_[--buffer_cursor_ - buffer_] = character; |
| 78 if (buffer_cursor_ == buffer_) { | 83 if (buffer_cursor_ == buffer_) { |
| 79 pushback_limit_ = NULL; | 84 pushback_limit_ = NULL; |
| 80 } else if (buffer_cursor_ < pushback_limit_) { | 85 } else if (buffer_cursor_ < pushback_limit_) { |
| 81 pushback_limit_ = buffer_cursor_; | 86 pushback_limit_ = buffer_cursor_; |
| 82 } | 87 } |
| 83 pos_--; | 88 pos_--; |
| 84 } | 89 } |
| 85 | 90 |
| 86 | 91 |
| 87 bool BufferedUC16CharacterStream::ReadBlock() { | 92 bool BufferedUC16CharacterStream::ReadBlock() { |
| 93 buffer_cursor_ = buffer_; |
| 88 if (pushback_limit_ != NULL) { | 94 if (pushback_limit_ != NULL) { |
| 89 buffer_cursor_ = buffer_; | 95 // Leave pushback mode. |
| 90 buffer_end_ = pushback_limit_; | 96 buffer_end_ = pushback_limit_; |
| 91 pushback_limit_ = NULL; | 97 pushback_limit_ = NULL; |
| 92 ASSERT(buffer_cursor_ != buffer_end_); | 98 // If there were any valid characters left at the |
| 93 return true; | 99 // start of the buffer, use those. |
| 100 if (buffer_cursor_ < buffer_end_) return true; |
| 101 // Otherwise read a new block. |
| 94 } | 102 } |
| 95 unsigned length = FillBuffer(pos_, kBufferSize); | 103 unsigned length = FillBuffer(pos_, kBufferSize); |
| 96 buffer_cursor_ = buffer_; | |
| 97 buffer_end_ = buffer_ + length; | 104 buffer_end_ = buffer_ + length; |
| 98 return length > 0; | 105 return length > 0; |
| 99 } | 106 } |
| 100 | 107 |
| 101 | 108 |
| 102 unsigned BufferedUC16CharacterStream::SlowSeekForward(unsigned delta) { | 109 unsigned BufferedUC16CharacterStream::SlowSeekForward(unsigned delta) { |
| 103 // Leave pushback mode (i.e., ignore that there might be valid data | 110 // Leave pushback mode (i.e., ignore that there might be valid data |
| 104 // in the buffer before the pushback_limit_ point). | 111 // in the buffer before the pushback_limit_ point). |
| 105 pushback_limit_ = NULL; | 112 pushback_limit_ = NULL; |
| 106 return BufferSeekForward(delta); | 113 return BufferSeekForward(delta); |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 } | 512 } |
| 506 } | 513 } |
| 507 literal.Complete(); | 514 literal.Complete(); |
| 508 Advance(); | 515 Advance(); |
| 509 return Token::STRING; | 516 return Token::STRING; |
| 510 } | 517 } |
| 511 | 518 |
| 512 | 519 |
| 513 Token::Value JsonScanner::ScanJsonNumber() { | 520 Token::Value JsonScanner::ScanJsonNumber() { |
| 514 LiteralScope literal(this); | 521 LiteralScope literal(this); |
| 515 if (c0_ == '-') AddLiteralCharAdvance(); | 522 bool negative = false; |
| 523 |
| 524 if (c0_ == '-') { |
| 525 AddLiteralCharAdvance(); |
| 526 negative = true; |
| 527 } |
| 516 if (c0_ == '0') { | 528 if (c0_ == '0') { |
| 517 AddLiteralCharAdvance(); | 529 AddLiteralCharAdvance(); |
| 518 // Prefix zero is only allowed if it's the only digit before | 530 // Prefix zero is only allowed if it's the only digit before |
| 519 // a decimal point or exponent. | 531 // a decimal point or exponent. |
| 520 if ('0' <= c0_ && c0_ <= '9') return Token::ILLEGAL; | 532 if ('0' <= c0_ && c0_ <= '9') return Token::ILLEGAL; |
| 521 } else { | 533 } else { |
| 534 int i = 0; |
| 535 int digits = 0; |
| 522 if (c0_ < '1' || c0_ > '9') return Token::ILLEGAL; | 536 if (c0_ < '1' || c0_ > '9') return Token::ILLEGAL; |
| 523 do { | 537 do { |
| 538 i = i * 10 + c0_ - '0'; |
| 539 digits++; |
| 524 AddLiteralCharAdvance(); | 540 AddLiteralCharAdvance(); |
| 525 } while (c0_ >= '0' && c0_ <= '9'); | 541 } while (c0_ >= '0' && c0_ <= '9'); |
| 542 if (c0_ != '.' && c0_ != 'e' && c0_ != 'E' && digits < 10) { |
| 543 number_ = (negative ? -i : i); |
| 544 return Token::NUMBER; |
| 545 } |
| 526 } | 546 } |
| 527 if (c0_ == '.') { | 547 if (c0_ == '.') { |
| 528 AddLiteralCharAdvance(); | 548 AddLiteralCharAdvance(); |
| 529 if (c0_ < '0' || c0_ > '9') return Token::ILLEGAL; | 549 if (c0_ < '0' || c0_ > '9') return Token::ILLEGAL; |
| 530 do { | 550 do { |
| 531 AddLiteralCharAdvance(); | 551 AddLiteralCharAdvance(); |
| 532 } while (c0_ >= '0' && c0_ <= '9'); | 552 } while (c0_ >= '0' && c0_ <= '9'); |
| 533 } | 553 } |
| 534 if (AsciiAlphaToLower(c0_) == 'e') { | 554 if (AsciiAlphaToLower(c0_) == 'e') { |
| 535 AddLiteralCharAdvance(); | 555 AddLiteralCharAdvance(); |
| 536 if (c0_ == '-' || c0_ == '+') AddLiteralCharAdvance(); | 556 if (c0_ == '-' || c0_ == '+') AddLiteralCharAdvance(); |
| 537 if (c0_ < '0' || c0_ > '9') return Token::ILLEGAL; | 557 if (c0_ < '0' || c0_ > '9') return Token::ILLEGAL; |
| 538 do { | 558 do { |
| 539 AddLiteralCharAdvance(); | 559 AddLiteralCharAdvance(); |
| 540 } while (c0_ >= '0' && c0_ <= '9'); | 560 } while (c0_ >= '0' && c0_ <= '9'); |
| 541 } | 561 } |
| 542 literal.Complete(); | 562 literal.Complete(); |
| 563 ASSERT_NOT_NULL(next_.literal_chars); |
| 564 number_ = StringToDouble(next_.literal_chars->ascii_literal(), |
| 565 NO_FLAGS, // Hex, octal or trailing junk. |
| 566 OS::nan_value()); |
| 543 return Token::NUMBER; | 567 return Token::NUMBER; |
| 544 } | 568 } |
| 545 | 569 |
| 546 | 570 |
| 547 Token::Value JsonScanner::ScanJsonIdentifier(const char* text, | 571 Token::Value JsonScanner::ScanJsonIdentifier(const char* text, |
| 548 Token::Value token) { | 572 Token::Value token) { |
| 549 LiteralScope literal(this); | 573 LiteralScope literal(this); |
| 550 while (*text != '\0') { | 574 while (*text != '\0') { |
| 551 if (c0_ != *text) return Token::ILLEGAL; | 575 if (c0_ != *text) return Token::ILLEGAL; |
| 552 Advance(); | 576 Advance(); |
| 553 text++; | 577 text++; |
| 554 } | 578 } |
| 555 if (ScannerConstants::kIsIdentifierPart.get(c0_)) return Token::ILLEGAL; | 579 if (ScannerConstants::kIsIdentifierPart.get(c0_)) return Token::ILLEGAL; |
| 556 literal.Complete(); | 580 literal.Complete(); |
| 557 return token; | 581 return token; |
| 558 } | 582 } |
| 559 | 583 |
| 560 | 584 |
| 561 | 585 |
| 562 } } // namespace v8::internal | 586 } } // namespace v8::internal |
| OLD | NEW |