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_) { |
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 void AdvanceSkipWhitespace() { |
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 } |
68 | 69 |
69 // Scan a single JSON token. The JSON lexical grammar is specified in the | 70 inline void SkipWhitespace() { |
70 // ECMAScript 5 standard, section 15.12.1.1. | 71 while (c0_ == '\t' || c0_ == '\r' || c0_ == '\n' || c0_ == ' ') { |
71 // Recognizes all of the single-character tokens directly, or calls a function | 72 Advance(); |
72 // to scan a number, string or identifier literal. | 73 } |
73 // The only allowed whitespace characters between tokens are tab, | 74 } |
74 // carriage-return, newline and space. | 75 |
75 void ScanJson(); | 76 inline uc32 AdvanceGetChar() { |
| 77 Advance(); |
| 78 return c0_; |
| 79 } |
| 80 |
| 81 // Checks that current charater is c. |
| 82 // If so, then consume c and skip whitespace. |
| 83 inline bool MatchSkipWhiteSpace(uc32 c) { |
| 84 if (c0_ == c) { |
| 85 AdvanceSkipWhitespace(); |
| 86 return true; |
| 87 } |
| 88 return false; |
| 89 } |
76 | 90 |
77 // A JSON string (production JSONString) is subset of valid JavaScript string | 91 // A JSON string (production JSONString) is subset of valid JavaScript string |
78 // literals. The string must only be double-quoted (not single-quoted), and | 92 // 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 | 93 // 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. | 94 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. |
81 Token::Value ScanJsonString(); | 95 Handle<Object> ParseJsonString() { |
| 96 return ScanJsonString<false>(); |
| 97 } |
| 98 Handle<Object> ParseJsonSymbol() { |
| 99 return ScanJsonString<true>(); |
| 100 } |
| 101 template <bool is_symbol> |
| 102 Handle<Object> ScanJsonString(); |
82 // Slow version for unicode support, uses the first ascii_count characters, | 103 // Slow version for unicode support, uses the first ascii_count characters, |
83 // as first part of a ConsString | 104 // as first part of a ConsString |
84 Token::Value SlowScanJsonString(); | 105 Handle<Object> SlowScanJsonString(); |
85 | 106 |
86 // A JSON number (production JSONNumber) is a subset of the valid JavaScript | 107 // A JSON number (production JSONNumber) is a subset of the valid JavaScript |
87 // decimal number literals. | 108 // decimal number literals. |
88 // It includes an optional minus sign, must have at least one | 109 // 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 | 110 // 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"). | 111 // the integer part is zero), and may include an exponent part (e.g., "e-10"). |
91 // Hexadecimal and octal numbers are not allowed. | 112 // Hexadecimal and octal numbers are not allowed. |
92 Token::Value ScanJsonNumber(); | 113 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 | 114 |
99 // Parse a single JSON value from input (grammar production JSONValue). | 115 // Parse a single JSON value from input (grammar production JSONValue). |
100 // A JSON value is either a (double-quoted) string literal, a number literal, | 116 // 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. | 117 // one of "true", "false", or "null", or an object or array literal. |
102 Handle<Object> ParseJsonValue(); | 118 Handle<Object> ParseJsonValue(); |
103 | 119 |
104 // Parse a JSON object literal (grammar production JSONObject). | 120 // Parse a JSON object literal (grammar production JSONObject). |
105 // An object literal is a squiggly-braced and comma separated sequence | 121 // 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 | 122 // (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. | 123 // 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 | 124 // A JSON array dosn't allow numbers and identifiers as keys, like a |
109 // JavaScript array. | 125 // JavaScript array. |
110 Handle<Object> ParseJsonObject(); | 126 Handle<Object> ParseJsonObject(); |
111 | 127 |
112 // Parses a JSON array literal (grammar production JSONArray). An array | 128 // Parses a JSON array literal (grammar production JSONArray). An array |
113 // literal is a square-bracketed and comma separated sequence (possibly empty) | 129 // literal is a square-bracketed and comma separated sequence (possibly empty) |
114 // of JSON values. | 130 // of JSON values. |
115 // A JSON array doesn't allow leaving out values from the sequence, nor does | 131 // 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. | 132 // it allow a terminal comma, like a JavaScript array does. |
117 Handle<Object> ParseJsonArray(); | 133 Handle<Object> ParseJsonArray(); |
118 | 134 |
119 | 135 |
120 // Mark that a parsing error has happened at the current token, and | 136 // Mark that a parsing error has happened at the current token, and |
121 // return a null handle. Primarily for readability. | 137 // return a null handle. Primarily for readability. |
122 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); } | 138 inline Handle<Object> ReportUnexpectedCharacter() { |
| 139 return Handle<Object>::null(); |
| 140 } |
123 | 141 |
124 // Peek at the next token. | 142 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 | 143 |
138 static const int kInitialSpecialStringSize = 1024; | 144 static const int kInitialSpecialStringSize = 1024; |
139 | 145 |
140 | 146 |
141 private: | 147 private: |
142 Handle<String> source_; | 148 Handle<String> source_; |
143 int source_length_; | 149 int source_length_; |
144 Handle<SeqAsciiString> seq_source_; | 150 Handle<SeqAsciiString> seq_source_; |
145 | 151 |
146 bool is_sequential_ascii_; | 152 bool is_sequential_ascii_; |
147 // Current and next token | 153 // begin and end position of scanned string or number |
148 TokenInfo current_; | 154 int beg_pos_; |
149 TokenInfo next_; | 155 int end_pos_; |
| 156 |
150 Isolate* isolate_; | 157 Isolate* isolate_; |
151 uc32 c0_; | 158 uc32 c0_; |
152 int position_; | 159 int position_; |
153 | 160 |
154 | |
155 Handle<String> string_val_; | |
156 double number_; | 161 double number_; |
157 }; | 162 }; |
158 | 163 |
159 } } // namespace v8::internal | 164 } } // namespace v8::internal |
160 | 165 |
161 #endif // V8_JSON_PARSER_H_ | 166 #endif // V8_JSON_PARSER_H_ |
OLD | NEW |