blob: acb9b47bd99cf9c3f848d4fdf7d74a3dc05da6b8 [file] [log] [blame]
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Zhu3e5fa292010-11-09 16:16:48 -080033#include "scanner-base.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35namespace v8 {
36namespace internal {
37
Steve Block6ded16b2010-05-10 14:33:55 +010038// UTF16 buffer to read characters from a character stream.
Steve Blocka7e24c12009-10-30 11:49:00 +000039class CharacterStreamUTF16Buffer: public UTF16Buffer {
40 public:
41 CharacterStreamUTF16Buffer();
42 virtual ~CharacterStreamUTF16Buffer() {}
Steve Block6ded16b2010-05-10 14:33:55 +010043 void Initialize(Handle<String> data,
44 unibrow::CharacterStream* stream,
45 int start_position,
46 int end_position);
Steve Blocka7e24c12009-10-30 11:49:00 +000047 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 Block6ded16b2010-05-10 14:33:55 +010060// UTF16 buffer to read characters from an external string.
61template <typename StringType, typename CharType>
62class ExternalStringUTF16Buffer: public UTF16Buffer {
Steve Blocka7e24c12009-10-30 11:49:00 +000063 public:
Steve Block6ded16b2010-05-10 14:33:55 +010064 ExternalStringUTF16Buffer();
65 virtual ~ExternalStringUTF16Buffer() {}
66 void Initialize(Handle<StringType> data,
67 int start_position,
68 int end_position);
Steve Blocka7e24c12009-10-30 11:49:00 +000069 virtual void PushBack(uc32 ch);
70 virtual uc32 Advance();
71 virtual void SeekForward(int pos);
72
73 private:
Steve Block6ded16b2010-05-10 14:33:55 +010074 const CharType* raw_data_; // Pointer to the actual array of characters.
Steve Blocka7e24c12009-10-30 11:49:00 +000075};
76
77
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080078// Initializes a UTF16Buffer as input stream, using one of a number
79// of strategies depending on the available character sources.
80class StreamInitializer {
Steve Blocka7e24c12009-10-30 11:49:00 +000081 public:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080082 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 Blocka7e24c12009-10-30 11:49:00 +000093
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080094 // 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 Monsen80d68ea2010-09-08 11:05:35 +010098
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080099// ----------------------------------------------------------------------------
100// V8JavaScriptScanner
101// JavaScript scanner getting its input from either a V8 String or a unicode
102// CharacterStream.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100103
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800104class V8JavaScriptScanner : public JavaScriptScanner {
105 public:
106 V8JavaScriptScanner() {}
107
108 Token::Value NextCheckStack();
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
Steve Block6ded16b2010-05-10 14:33:55 +0100110 // Initialize the Scanner to scan source.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800111 void Initialize(Handle<String> source, int literal_flags = kAllLiterals);
Steve Block6ded16b2010-05-10 14:33:55 +0100112 void Initialize(Handle<String> source,
113 unibrow::CharacterStream* stream,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800114 int literal_flags = kAllLiterals);
Steve Block6ded16b2010-05-10 14:33:55 +0100115 void Initialize(Handle<String> source,
116 int start_position, int end_position,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800117 int literal_flags = kAllLiterals);
118
119 protected:
120 StreamInitializer stream_initializer_;
121};
122
123
124class JsonScanner : public Scanner {
125 public:
126 JsonScanner();
127
128 // Initialize the Scanner to scan source.
129 void Initialize(Handle<String> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131 // Returns the next token.
132 Token::Value Next();
133
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800134 protected:
135 // Skip past JSON whitespace (only space, tab, newline and carrige-return).
Leon Clarke4515c472010-02-03 11:58:03 +0000136 bool SkipJsonWhiteSpace();
Leon Clarke4515c472010-02-03 11:58:03 +0000137
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 Monsen9dcf7e22010-06-28 14:14:28 +0100153
Leon Clarke4515c472010-02-03 11:58:03 +0000154 // 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 Monsen9dcf7e22010-06-28 14:14:28 +0100159
Leon Clarke4515c472010-02-03 11:58:03 +0000160 // 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) Wang8a31eba2010-12-06 19:01:33 -0800165 StreamInitializer stream_initializer_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000166};
167
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800168
169// ExternalStringUTF16Buffer
170template <typename StringType, typename CharType>
171ExternalStringUTF16Buffer<StringType, CharType>::ExternalStringUTF16Buffer()
172 : raw_data_(NULL) { }
173
174
175template <typename StringType, typename CharType>
176void 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
192template <typename StringType, typename CharType>
193uc32 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
205template <typename StringType, typename CharType>
206void ExternalStringUTF16Buffer<StringType, CharType>::PushBack(uc32 ch) {
207 pos_--;
208 ASSERT(pos_ >= Scanner::kCharacterLookaheadBufferSize);
209 ASSERT(raw_data_[pos_ - Scanner::kCharacterLookaheadBufferSize] == ch);
210}
211
212
213template <typename StringType, typename CharType>
214void ExternalStringUTF16Buffer<StringType, CharType>::SeekForward(int pos) {
215 pos_ = pos;
216}
217
Steve Blocka7e24c12009-10-30 11:49:00 +0000218} } // namespace v8::internal
219
220#endif // V8_SCANNER_H_