Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_JSON_PARSER_H_ | |
| 29 #define V8_JSON_PARSER_H_ | |
| 30 | |
| 31 #include "token.h" | |
| 32 | |
| 33 namespace v8 { | |
| 34 namespace internal { | |
| 35 | |
| 36 // A simple json parser. | |
| 37 class JsonParser BASE_EMBEDDED { | |
| 38 public: | |
| 39 static Handle<Object> Parse(Handle<String> source) { | |
| 40 return JsonParser().ParseJson(source); | |
| 41 } | |
| 42 | |
| 43 static const int kEndOfString = -1; | |
| 44 | |
| 45 private: | |
| 46 JsonParser() : isolate_(Isolate::Current()) {} | |
|
Lasse Reichstein
2011/05/19 07:27:40
Get the isolate from the input string instead.
Rico
2011/05/23 18:18:12
Done.
| |
| 47 | |
| 48 // Parse a string containing a single JSON value. | |
| 49 Handle<Object> ParseJson(Handle<String> source); | |
| 50 | |
| 51 inline void Advance() { | |
| 52 if (position_ >= source_length_) { | |
| 53 position_++; | |
| 54 c0_ = kEndOfString; | |
| 55 } else if (is_sequential_ascii_) { | |
| 56 position_++; | |
| 57 c0_ = seq_source_->SeqAsciiStringGet(position_); | |
| 58 } else { | |
| 59 position_++; | |
| 60 c0_ = source_->Get(position_); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 inline Isolate* isolate() { return isolate_; } | |
| 65 | |
| 66 // Get the string for the current string token. | |
| 67 Handle<String> GetString(bool is_symbol); | |
| 68 | |
| 69 // Scan a single JSON token. The JSON lexical grammar is specified in the | |
| 70 // ECMAScript 5 standard, section 15.12.1.1. | |
| 71 // Recognizes all of the single-character tokens directly, or calls a function | |
| 72 // to scan a number, string or identifier literal. | |
| 73 // The only allowed whitespace characters between tokens are tab, | |
| 74 // carriage-return, newline and space. | |
| 75 void ScanJson(); | |
| 76 | |
| 77 // A JSON string (production JSONString) is subset of valid JavaScript string | |
| 78 // literals. The string must only be double-quoted (not single-quoted), and | |
| 79 // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and | |
| 80 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. | |
| 81 Token::Value ScanJsonString(); | |
| 82 // Slow version for unicode support, uses the first ascii_count characters, | |
| 83 // as first part of a ConsString | |
| 84 Token::Value SlowScanJsonString(); | |
| 85 | |
| 86 // A JSON number (production JSONNumber) is a subset of the valid JavaScript | |
| 87 // decimal number literals. | |
| 88 // It includes an optional minus sign, must have at least one | |
| 89 // digit before and after a decimal point, may not have prefixed zeros (unless | |
| 90 // the integer part is zero), and may include an exponent part (e.g., "e-10"). | |
| 91 // Hexadecimal and octal numbers are not allowed. | |
| 92 Token::Value ScanJsonNumber(); | |
| 93 | |
| 94 // Used to recognizes one of the literals "true", "false", or "null". These | |
| 95 // are the only valid JSON identifiers (productions JSONBooleanLiteral, | |
| 96 // JSONNullLiteral). | |
| 97 Token::Value ScanJsonIdentifier(const char* text, Token::Value token); | |
| 98 | |
| 99 // Parse a single JSON value from input (grammar production JSONValue). | |
| 100 // A JSON value is either a (double-quoted) string literal, a number literal, | |
| 101 // one of "true", "false", or "null", or an object or array literal. | |
| 102 Handle<Object> ParseJsonValue(); | |
| 103 | |
| 104 // Parse a JSON object literal (grammar production JSONObject). | |
| 105 // An object literal is a squiggly-braced and comma separated sequence | |
| 106 // (possibly empty) of key/value pairs, where the key is a JSON string | |
| 107 // literal, the value is a JSON value, and the two are separated by a colon. | |
| 108 // A JSON array dosn't allow numbers and identifiers as keys, like a | |
| 109 // JavaScript array. | |
| 110 Handle<Object> ParseJsonObject(); | |
| 111 | |
| 112 // Parses a JSON array literal (grammar production JSONArray). An array | |
| 113 // literal is a square-bracketed and comma separated sequence (possibly empty) | |
| 114 // of JSON values. | |
| 115 // A JSON array doesn't allow leaving out values from the sequence, nor does | |
| 116 // it allow a terminal comma, like a JavaScript array does. | |
| 117 Handle<Object> ParseJsonArray(); | |
| 118 | |
| 119 | |
| 120 // Mark that a parsing error has happened at the current token, and | |
| 121 // return a null handle. Primarily for readability. | |
| 122 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); } | |
| 123 | |
| 124 // Peek at the next token. | |
| 125 Token::Value Peek() { return next_.token; } | |
| 126 // Scan the next token and return the token scanned on the last call. | |
| 127 Token::Value Next(); | |
| 128 | |
| 129 struct TokenInfo { | |
| 130 TokenInfo() : token(Token::ILLEGAL), | |
| 131 beg_pos(0), | |
| 132 end_pos(0) { } | |
| 133 Token::Value token; | |
| 134 int beg_pos; | |
| 135 int end_pos; | |
| 136 }; | |
| 137 | |
| 138 static const int kInitialSpecialStringSize = 100; | |
| 139 | |
| 140 | |
| 141 private: | |
| 142 Handle<String> source_; | |
| 143 int source_length_; | |
| 144 Handle<SeqAsciiString> seq_source_; | |
| 145 | |
| 146 bool is_sequential_ascii_; | |
| 147 // Current and next token | |
| 148 TokenInfo current_; | |
| 149 TokenInfo next_; | |
| 150 Isolate* isolate_; | |
| 151 uc32 c0_; | |
| 152 int position_; | |
| 153 | |
| 154 | |
| 155 Handle<String> string_val_; | |
| 156 double number_; | |
| 157 }; | |
| 158 | |
| 159 } } // namespace v8::internal | |
| 160 | |
| 161 #endif // V8_JSON_PARSER_H_ | |
| OLD | NEW |