Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 return JsonParser().ParseJson(source); | 40 return JsonParser().ParseJson(source); |
| 41 } | 41 } |
| 42 | 42 |
| 43 static const int kEndOfString = -1; | 43 static const int kEndOfString = -1; |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 // Parse a string containing a single JSON value. | 46 // Parse a string containing a single JSON value. |
| 47 Handle<Object> ParseJson(Handle<String> source); | 47 Handle<Object> ParseJson(Handle<String> source); |
| 48 | 48 |
| 49 inline void Advance() { | 49 inline void Advance() { |
| 50 if (position_ >= source_length_) { | 50 position_++; |
| 51 position_++; | 51 if (position_ > source_length_) { |
| 52 c0_ = kEndOfString; | 52 c0_ = kEndOfString; |
| 53 } else if (is_sequential_ascii_) { | 53 } else if (is_sequential_ascii_) { |
|
Lasse Reichstein
2011/06/01 11:03:58
This was changed?
sandholm
2011/06/01 13:45:20
r8142 ensures this was not changed.
| |
| 54 position_++; | |
| 55 c0_ = seq_source_->SeqAsciiStringGet(position_); | 54 c0_ = seq_source_->SeqAsciiStringGet(position_); |
| 56 } else { | 55 } else { |
| 57 position_++; | |
| 58 c0_ = source_->Get(position_); | 56 c0_ = source_->Get(position_); |
| 59 } | 57 } |
| 60 } | 58 } |
| 61 | 59 |
| 62 inline Isolate* isolate() { return isolate_; } | 60 // The JSON lexical grammar is specified in the ECMAScript 5 standard, |
| 61 // section 15.12.1.1. The only allowed whitespace characters between tokens | |
| 62 // are tab, carriage-return, newline and space. | |
| 63 | 63 |
| 64 // Get the string for the current string token. | 64 inline bool AdvanceWS() { |
|
Lasse Reichstein
2011/06/01 11:03:58
Don't abbreviate unless necessary.
Call this Advan
sandholm
2011/06/01 13:45:20
Done.
| |
| 65 Handle<String> GetString(bool hint_symbol); | 65 do { |
| 66 Handle<String> GetString(); | 66 Advance(); |
| 67 Handle<String> GetSymbol(); | 67 } while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' '); |
| 68 return true; | |
|
Lasse Reichstein
2011/06/01 11:03:58
Why the return true? If it always returns the same
sandholm
2011/06/01 13:45:20
Done.
| |
| 69 } | |
| 68 | 70 |
| 69 // Scan a single JSON token. The JSON lexical grammar is specified in the | 71 inline void SkipWS() { |
|
Lasse Reichstein
2011/06/01 11:03:58
And just SkipWhitespace.
sandholm
2011/06/01 13:45:20
Done.
| |
| 70 // ECMAScript 5 standard, section 15.12.1.1. | 72 while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' ') { |
| 71 // Recognizes all of the single-character tokens directly, or calls a function | 73 Advance(); |
| 72 // to scan a number, string or identifier literal. | 74 } |
| 73 // The only allowed whitespace characters between tokens are tab, | 75 } |
| 74 // carriage-return, newline and space. | 76 |
| 75 void ScanJson(); | 77 inline uc32 AdvanceGetChar() { |
| 78 Advance(); | |
| 79 return c0_; | |
| 80 } | |
| 76 | 81 |
| 77 // A JSON string (production JSONString) is subset of valid JavaScript string | 82 // A JSON string (production JSONString) is subset of valid JavaScript string |
| 78 // literals. The string must only be double-quoted (not single-quoted), and | 83 // 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 | 84 // 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. | 85 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. |
| 81 Token::Value ScanJsonString(); | 86 Handle<Object> ParseJsonString() { |
| 87 return ScanJsonString<false>(); | |
| 88 } | |
| 89 Handle<Object> ParseJsonSymbol() { | |
| 90 return ScanJsonString<true>(); | |
| 91 } | |
| 92 template <bool is_symbol> | |
| 93 Handle<Object> ScanJsonString(); | |
| 82 // Slow version for unicode support, uses the first ascii_count characters, | 94 // Slow version for unicode support, uses the first ascii_count characters, |
| 83 // as first part of a ConsString | 95 // as first part of a ConsString |
| 84 Token::Value SlowScanJsonString(); | 96 Handle<Object> SlowScanJsonString(); |
| 85 | 97 |
| 86 // A JSON number (production JSONNumber) is a subset of the valid JavaScript | 98 // A JSON number (production JSONNumber) is a subset of the valid JavaScript |
| 87 // decimal number literals. | 99 // decimal number literals. |
| 88 // It includes an optional minus sign, must have at least one | 100 // 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 | 101 // 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"). | 102 // the integer part is zero), and may include an exponent part (e.g., "e-10"). |
| 91 // Hexadecimal and octal numbers are not allowed. | 103 // Hexadecimal and octal numbers are not allowed. |
| 92 Token::Value ScanJsonNumber(); | 104 Handle<Object> ParseJsonNumber(); |
| 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 | 105 |
| 99 // Parse a single JSON value from input (grammar production JSONValue). | 106 // Parse a single JSON value from input (grammar production JSONValue). |
| 100 // A JSON value is either a (double-quoted) string literal, a number literal, | 107 // 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. | 108 // one of "true", "false", or "null", or an object or array literal. |
| 102 Handle<Object> ParseJsonValue(); | 109 Handle<Object> ParseJsonValue(); |
| 103 | 110 |
| 104 // Parse a JSON object literal (grammar production JSONObject). | 111 // Parse a JSON object literal (grammar production JSONObject). |
| 105 // An object literal is a squiggly-braced and comma separated sequence | 112 // 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 | 113 // (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. | 114 // 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 | 115 // A JSON array dosn't allow numbers and identifiers as keys, like a |
| 109 // JavaScript array. | 116 // JavaScript array. |
| 110 Handle<Object> ParseJsonObject(); | 117 Handle<Object> ParseJsonObject(); |
| 111 | 118 |
| 112 // Parses a JSON array literal (grammar production JSONArray). An array | 119 // Parses a JSON array literal (grammar production JSONArray). An array |
| 113 // literal is a square-bracketed and comma separated sequence (possibly empty) | 120 // literal is a square-bracketed and comma separated sequence (possibly empty) |
| 114 // of JSON values. | 121 // of JSON values. |
| 115 // A JSON array doesn't allow leaving out values from the sequence, nor does | 122 // 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. | 123 // it allow a terminal comma, like a JavaScript array does. |
| 117 Handle<Object> ParseJsonArray(); | 124 Handle<Object> ParseJsonArray(); |
| 118 | 125 |
| 119 | 126 |
| 120 // Mark that a parsing error has happened at the current token, and | 127 // Mark that a parsing error has happened at the current token, and |
| 121 // return a null handle. Primarily for readability. | 128 // return a null handle. Primarily for readability. |
| 122 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); } | 129 inline Handle<Object> ReportUnexpectedToken() { |
| 130 return Handle<Object>::null(); | |
| 131 } | |
| 123 | 132 |
| 124 // Peek at the next token. | 133 inline Isolate* isolate() { return isolate_; } |
| 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 | 134 |
| 138 static const int kInitialSpecialStringSize = 1024; | 135 static const int kInitialSpecialStringSize = 1024; |
| 139 | 136 |
| 140 | 137 |
| 141 private: | 138 private: |
| 142 Handle<String> source_; | 139 Handle<String> source_; |
| 143 int source_length_; | 140 int source_length_; |
| 144 Handle<SeqAsciiString> seq_source_; | 141 Handle<SeqAsciiString> seq_source_; |
| 145 | 142 |
| 146 bool is_sequential_ascii_; | 143 bool is_sequential_ascii_; |
| 147 // Current and next token | 144 // begin and end position of scanned string or number |
| 148 TokenInfo current_; | 145 int beg_pos_; |
| 149 TokenInfo next_; | 146 int end_pos_; |
| 147 | |
| 150 Isolate* isolate_; | 148 Isolate* isolate_; |
| 151 uc32 c0_; | 149 uc32 c0_; |
| 152 int position_; | 150 int position_; |
| 153 | 151 |
| 154 | |
| 155 Handle<String> string_val_; | |
| 156 double number_; | 152 double number_; |
| 157 }; | 153 }; |
| 158 | 154 |
| 159 } } // namespace v8::internal | 155 } } // namespace v8::internal |
| 160 | 156 |
| 161 #endif // V8_JSON_PARSER_H_ | 157 #endif // V8_JSON_PARSER_H_ |
| OLD | NEW |