Fix bug when the scanner does a pushback at the end of input.

We don't advance the input cursor past the end of input, so we shouldn't
decrease it when we pushback the kEndOfInput marker.

Review URL: http://codereview.chromium.org/6246004

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6308 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
lrn@chromium.org 2011-01-14 10:49:18 +00:00
parent 463d722a8b
commit 2cccc4009f
4 changed files with 26 additions and 15 deletions

View File

@ -69,8 +69,12 @@ class InputStreamUTF16Buffer : public UC16CharacterStream {
} }
} }
virtual void PushBack(uc16 ch) { virtual void PushBack(uc32 ch) {
ASSERT(pos_ > 0); ASSERT(pos_ > 0);
if (ch == kEndOfInput) {
pos_--;
return;
}
if (buffer_cursor_ <= pushback_buffer_) { if (buffer_cursor_ <= pushback_buffer_) {
// No more room in the current buffer to do pushbacks. // No more room in the current buffer to do pushbacks.
if (pushback_buffer_end_cache_ == NULL) { if (pushback_buffer_end_cache_ == NULL) {
@ -98,7 +102,8 @@ class InputStreamUTF16Buffer : public UC16CharacterStream {
buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_; buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_;
} }
} }
pushback_buffer_[buffer_cursor_ - pushback_buffer_- 1] = ch; pushback_buffer_[buffer_cursor_ - pushback_buffer_- 1] =
static_cast<uc16>(ch);
pos_--; pos_--;
} }

View File

@ -64,10 +64,10 @@ class UC16CharacterStream {
// Returns and advances past the next UC16 character in the input // Returns and advances past the next UC16 character in the input
// stream. If there are no more characters, it returns a negative // stream. If there are no more characters, it returns a negative
// value. // value.
inline int32_t Advance() { inline uc32 Advance() {
if (buffer_cursor_ < buffer_end_ || ReadBlock()) { if (buffer_cursor_ < buffer_end_ || ReadBlock()) {
pos_++; pos_++;
return *(buffer_cursor_++); return static_cast<uc32>(*(buffer_cursor_++));
} }
// Note: currently the following increment is necessary to avoid a // Note: currently the following increment is necessary to avoid a
// parser problem! The scanner treats the final kEndOfInput as // parser problem! The scanner treats the final kEndOfInput as
@ -97,13 +97,14 @@ class UC16CharacterStream {
return SlowSeekForward(character_count); return SlowSeekForward(character_count);
} }
// Pushes back the most recently read UC16 character, i.e., // Pushes back the most recently read UC16 character (or negative
// the value returned by the most recent call to Advance. // value if at end of input), i.e., the value returned by the most recent
// call to Advance.
// Must not be used right after calling SeekForward. // Must not be used right after calling SeekForward.
virtual void PushBack(uc16 character) = 0; virtual void PushBack(int32_t character) = 0;
protected: protected:
static const int32_t kEndOfInput = -1; static const uc32 kEndOfInput = -1;
// Ensures that the buffer_cursor_ points to the character at // Ensures that the buffer_cursor_ points to the character at
// position pos_ of the input, if possible. If the position // position pos_ of the input, if possible. If the position

View File

@ -48,14 +48,18 @@ BufferedUC16CharacterStream::BufferedUC16CharacterStream()
BufferedUC16CharacterStream::~BufferedUC16CharacterStream() { } BufferedUC16CharacterStream::~BufferedUC16CharacterStream() { }
void BufferedUC16CharacterStream::PushBack(uc16 character) { void BufferedUC16CharacterStream::PushBack(uc32 character) {
if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) { if (character == kEndOfInput) {
// buffer_ is writable, buffer_cursor_ is const pointer.
buffer_[--buffer_cursor_ - buffer_] = character;
pos_--; pos_--;
return; return;
} }
SlowPushBack(character); if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) {
// buffer_ is writable, buffer_cursor_ is const pointer.
buffer_[--buffer_cursor_ - buffer_] = static_cast<uc16>(character);
pos_--;
return;
}
SlowPushBack(static_cast<uc16>(character));
} }

View File

@ -43,7 +43,7 @@ class BufferedUC16CharacterStream: public UC16CharacterStream {
BufferedUC16CharacterStream(); BufferedUC16CharacterStream();
virtual ~BufferedUC16CharacterStream(); virtual ~BufferedUC16CharacterStream();
virtual void PushBack(uc16 character); virtual void PushBack(uc32 character);
protected: protected:
static const unsigned kBufferSize = 512; static const unsigned kBufferSize = 512;
@ -107,11 +107,12 @@ class ExternalTwoByteStringUC16CharacterStream: public UC16CharacterStream {
int end_position); int end_position);
virtual ~ExternalTwoByteStringUC16CharacterStream(); virtual ~ExternalTwoByteStringUC16CharacterStream();
virtual void PushBack(uc16 character) { virtual void PushBack(uc32 character) {
ASSERT(buffer_cursor_ > raw_data_); ASSERT(buffer_cursor_ > raw_data_);
buffer_cursor_--; buffer_cursor_--;
pos_--; pos_--;
} }
protected: protected:
virtual unsigned SlowSeekForward(unsigned delta) { virtual unsigned SlowSeekForward(unsigned delta) {
// Fast case always handles seeking. // Fast case always handles seeking.