Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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_H_ |
| 29 | #define V8_SCANNER_H_ |
| 30 | |
| 31 | #include "token.h" |
| 32 | #include "char-predicates-inl.h" |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 33 | #include "scanner-base.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 38 | // UTF16 buffer to read characters from a character stream. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | class CharacterStreamUTF16Buffer: public UTF16Buffer { |
| 40 | public: |
| 41 | CharacterStreamUTF16Buffer(); |
| 42 | virtual ~CharacterStreamUTF16Buffer() {} |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 43 | void Initialize(Handle<String> data, |
| 44 | unibrow::CharacterStream* stream, |
| 45 | int start_position, |
| 46 | int end_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | virtual void PushBack(uc32 ch); |
| 48 | virtual uc32 Advance(); |
| 49 | virtual void SeekForward(int pos); |
| 50 | |
| 51 | private: |
| 52 | List<uc32> pushback_buffer_; |
| 53 | uc32 last_; |
| 54 | unibrow::CharacterStream* stream_; |
| 55 | |
| 56 | List<uc32>* pushback_buffer() { return &pushback_buffer_; } |
| 57 | }; |
| 58 | |
| 59 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 60 | // UTF16 buffer to read characters from an external string. |
| 61 | template <typename StringType, typename CharType> |
| 62 | class ExternalStringUTF16Buffer: public UTF16Buffer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | public: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 64 | ExternalStringUTF16Buffer(); |
| 65 | virtual ~ExternalStringUTF16Buffer() {} |
| 66 | void Initialize(Handle<StringType> data, |
| 67 | int start_position, |
| 68 | int end_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | virtual void PushBack(uc32 ch); |
| 70 | virtual uc32 Advance(); |
| 71 | virtual void SeekForward(int pos); |
| 72 | |
| 73 | private: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 74 | const CharType* raw_data_; // Pointer to the actual array of characters. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 78 | // Initializes a UTF16Buffer as input stream, using one of a number |
| 79 | // of strategies depending on the available character sources. |
| 80 | class StreamInitializer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | public: |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 82 | UTF16Buffer* Init(Handle<String> source, |
| 83 | unibrow::CharacterStream* stream, |
| 84 | int start_position, |
| 85 | int end_position); |
| 86 | private: |
| 87 | // Different UTF16 buffers used to pull characters from. Based on input one of |
| 88 | // these will be initialized as the actual data source. |
| 89 | CharacterStreamUTF16Buffer char_stream_buffer_; |
| 90 | ExternalStringUTF16Buffer<ExternalTwoByteString, uint16_t> |
| 91 | two_byte_string_buffer_; |
| 92 | ExternalStringUTF16Buffer<ExternalAsciiString, char> ascii_string_buffer_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 94 | // Used to convert the source string into a character stream when a stream |
| 95 | // is not passed to the scanner. |
| 96 | SafeStringInputBuffer safe_string_input_buffer_; |
| 97 | }; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 98 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 99 | // ---------------------------------------------------------------------------- |
| 100 | // V8JavaScriptScanner |
| 101 | // JavaScript scanner getting its input from either a V8 String or a unicode |
| 102 | // CharacterStream. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 103 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 104 | class V8JavaScriptScanner : public JavaScriptScanner { |
| 105 | public: |
| 106 | V8JavaScriptScanner() {} |
| 107 | |
| 108 | Token::Value NextCheckStack(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 110 | // Initialize the Scanner to scan source. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 111 | void Initialize(Handle<String> source, int literal_flags = kAllLiterals); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 112 | void Initialize(Handle<String> source, |
| 113 | unibrow::CharacterStream* stream, |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 114 | int literal_flags = kAllLiterals); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 115 | void Initialize(Handle<String> source, |
| 116 | int start_position, int end_position, |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 117 | int literal_flags = kAllLiterals); |
| 118 | |
| 119 | protected: |
| 120 | StreamInitializer stream_initializer_; |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | class JsonScanner : public Scanner { |
| 125 | public: |
| 126 | JsonScanner(); |
| 127 | |
| 128 | // Initialize the Scanner to scan source. |
| 129 | void Initialize(Handle<String> source); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | |
| 131 | // Returns the next token. |
| 132 | Token::Value Next(); |
| 133 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 134 | protected: |
| 135 | // Skip past JSON whitespace (only space, tab, newline and carrige-return). |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 136 | bool SkipJsonWhiteSpace(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 137 | |
| 138 | // Scan a single JSON token. The JSON lexical grammar is specified in the |
| 139 | // ECMAScript 5 standard, section 15.12.1.1. |
| 140 | // Recognizes all of the single-character tokens directly, or calls a function |
| 141 | // to scan a number, string or identifier literal. |
| 142 | // The only allowed whitespace characters between tokens are tab, |
| 143 | // carrige-return, newline and space. |
| 144 | void ScanJson(); |
| 145 | |
| 146 | // A JSON number (production JSONNumber) is a subset of the valid JavaScript |
| 147 | // decimal number literals. |
| 148 | // It includes an optional minus sign, must have at least one |
| 149 | // digit before and after a decimal point, may not have prefixed zeros (unless |
| 150 | // the integer part is zero), and may include an exponent part (e.g., "e-10"). |
| 151 | // Hexadecimal and octal numbers are not allowed. |
| 152 | Token::Value ScanJsonNumber(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 153 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 154 | // A JSON string (production JSONString) is subset of valid JavaScript string |
| 155 | // literals. The string must only be double-quoted (not single-quoted), and |
| 156 | // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and |
| 157 | // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. |
| 158 | Token::Value ScanJsonString(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 159 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 160 | // Used to recognizes one of the literals "true", "false", or "null". These |
| 161 | // are the only valid JSON identifiers (productions JSONBooleanLiteral, |
| 162 | // JSONNullLiteral). |
| 163 | Token::Value ScanJsonIdentifier(const char* text, Token::Value token); |
| 164 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 165 | StreamInitializer stream_initializer_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame^] | 168 | |
| 169 | // ExternalStringUTF16Buffer |
| 170 | template <typename StringType, typename CharType> |
| 171 | ExternalStringUTF16Buffer<StringType, CharType>::ExternalStringUTF16Buffer() |
| 172 | : raw_data_(NULL) { } |
| 173 | |
| 174 | |
| 175 | template <typename StringType, typename CharType> |
| 176 | void ExternalStringUTF16Buffer<StringType, CharType>::Initialize( |
| 177 | Handle<StringType> data, |
| 178 | int start_position, |
| 179 | int end_position) { |
| 180 | ASSERT(!data.is_null()); |
| 181 | raw_data_ = data->resource()->data(); |
| 182 | |
| 183 | ASSERT(end_position <= data->length()); |
| 184 | if (start_position > 0) { |
| 185 | SeekForward(start_position); |
| 186 | } |
| 187 | end_ = |
| 188 | end_position != kNoEndPosition ? end_position : data->length(); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | template <typename StringType, typename CharType> |
| 193 | uc32 ExternalStringUTF16Buffer<StringType, CharType>::Advance() { |
| 194 | if (pos_ < end_) { |
| 195 | return raw_data_[pos_++]; |
| 196 | } else { |
| 197 | // note: currently the following increment is necessary to avoid a |
| 198 | // test-parser problem! |
| 199 | pos_++; |
| 200 | return static_cast<uc32>(-1); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | template <typename StringType, typename CharType> |
| 206 | void ExternalStringUTF16Buffer<StringType, CharType>::PushBack(uc32 ch) { |
| 207 | pos_--; |
| 208 | ASSERT(pos_ >= Scanner::kCharacterLookaheadBufferSize); |
| 209 | ASSERT(raw_data_[pos_ - Scanner::kCharacterLookaheadBufferSize] == ch); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | template <typename StringType, typename CharType> |
| 214 | void ExternalStringUTF16Buffer<StringType, CharType>::SeekForward(int pos) { |
| 215 | pos_ = pos; |
| 216 | } |
| 217 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | } } // namespace v8::internal |
| 219 | |
| 220 | #endif // V8_SCANNER_H_ |