ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_SCANNER_CHARACTER_STREAMS_H_ |
| 29 | #define V8_SCANNER_CHARACTER_STREAMS_H_ |
| 30 | |
| 31 | #include "scanner.h" |
| 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | // A buffered character stream based on a random access character |
| 37 | // source (ReadBlock can be called with pos_ pointing to any position, |
| 38 | // even positions before the current). |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 39 | class BufferedUtf16CharacterStream: public Utf16CharacterStream { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 40 | public: |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 41 | BufferedUtf16CharacterStream(); |
| 42 | virtual ~BufferedUtf16CharacterStream(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 43 | |
| 44 | virtual void PushBack(uc32 character); |
| 45 | |
| 46 | protected: |
| 47 | static const unsigned kBufferSize = 512; |
| 48 | static const unsigned kPushBackStepSize = 16; |
| 49 | |
| 50 | virtual unsigned SlowSeekForward(unsigned delta); |
| 51 | virtual bool ReadBlock(); |
| 52 | virtual void SlowPushBack(uc16 character); |
| 53 | |
| 54 | virtual unsigned BufferSeekForward(unsigned delta) = 0; |
| 55 | virtual unsigned FillBuffer(unsigned position, unsigned length) = 0; |
| 56 | |
| 57 | const uc16* pushback_limit_; |
| 58 | uc16 buffer_[kBufferSize]; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | // Generic string stream. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 63 | class GenericStringUtf16CharacterStream: public BufferedUtf16CharacterStream { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 64 | public: |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 65 | GenericStringUtf16CharacterStream(Handle<String> data, |
| 66 | unsigned start_position, |
| 67 | unsigned end_position); |
| 68 | virtual ~GenericStringUtf16CharacterStream(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 69 | |
| 70 | protected: |
| 71 | virtual unsigned BufferSeekForward(unsigned delta); |
| 72 | virtual unsigned FillBuffer(unsigned position, unsigned length); |
| 73 | |
| 74 | Handle<String> string_; |
| 75 | unsigned start_position_; |
| 76 | unsigned length_; |
| 77 | }; |
| 78 | |
| 79 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 80 | // Utf16 stream based on a literal UTF-8 string. |
| 81 | class Utf8ToUtf16CharacterStream: public BufferedUtf16CharacterStream { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 82 | public: |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 83 | Utf8ToUtf16CharacterStream(const byte* data, unsigned length); |
| 84 | virtual ~Utf8ToUtf16CharacterStream(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 85 | |
| 86 | protected: |
| 87 | virtual unsigned BufferSeekForward(unsigned delta); |
| 88 | virtual unsigned FillBuffer(unsigned char_position, unsigned length); |
| 89 | void SetRawPosition(unsigned char_position); |
| 90 | |
| 91 | const byte* raw_data_; |
| 92 | unsigned raw_data_length_; // Measured in bytes, not characters. |
| 93 | unsigned raw_data_pos_; |
| 94 | // The character position of the character at raw_data[raw_data_pos_]. |
| 95 | // Not necessarily the same as pos_. |
| 96 | unsigned raw_character_position_; |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | // UTF16 buffer to read characters from an external string. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 101 | class ExternalTwoByteStringUtf16CharacterStream: public Utf16CharacterStream { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 102 | public: |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 103 | ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, |
| 104 | int start_position, |
| 105 | int end_position); |
| 106 | virtual ~ExternalTwoByteStringUtf16CharacterStream(); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 107 | |
| 108 | virtual void PushBack(uc32 character) { |
| 109 | ASSERT(buffer_cursor_ > raw_data_); |
| 110 | buffer_cursor_--; |
| 111 | pos_--; |
| 112 | } |
| 113 | |
| 114 | protected: |
| 115 | virtual unsigned SlowSeekForward(unsigned delta) { |
| 116 | // Fast case always handles seeking. |
| 117 | return 0; |
| 118 | } |
| 119 | virtual bool ReadBlock() { |
| 120 | // Entire string is read at start. |
| 121 | return false; |
| 122 | } |
| 123 | Handle<ExternalTwoByteString> source_; |
| 124 | const uc16* raw_data_; // Pointer to the actual array of characters. |
| 125 | }; |
| 126 | |
| 127 | } } // namespace v8::internal |
| 128 | |
| 129 | #endif // V8_SCANNER_CHARACTER_STREAMS_H_ |