OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/json.h" | 5 #include "platform/json.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
9 #include "platform/utils.h" | 9 #include "platform/utils.h" |
10 #include "vm/os.h" | 10 #include "vm/os.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 } | 195 } |
196 } | 196 } |
197 } | 197 } |
198 | 198 |
199 | 199 |
200 JSONReader::JSONReader(const char* json_object) | 200 JSONReader::JSONReader(const char* json_object) |
201 : scanner_(json_object) { | 201 : scanner_(json_object) { |
202 Set(json_object); | 202 Set(json_object); |
203 } | 203 } |
204 | 204 |
205 | |
205 void JSONReader::Set(const char* json_object) { | 206 void JSONReader::Set(const char* json_object) { |
206 scanner_.SetText(json_object); | 207 scanner_.SetText(json_object); |
207 json_object_ = json_object; | 208 json_object_ = json_object; |
208 error_ = false; | 209 error_ = false; |
209 } | 210 } |
210 | 211 |
211 | 212 |
213 bool JSONReader::CheckMessage() { | |
214 scanner_.SetText(json_object_); | |
215 scanner_.Scan(); | |
216 CheckObject(); | |
217 return true; | |
218 } | |
219 | |
220 | |
221 void JSONReader::CheckValue() { | |
222 switch (scanner_.CurrentToken()) { | |
223 case JSONScanner::TokenLBrace: | |
224 CheckObject(); | |
225 break; | |
226 case JSONScanner::TokenLBrack: | |
227 CheckArray(); | |
228 break; | |
229 case JSONScanner::TokenString: { | |
230 // Check the encoding. | |
231 const char* s = ValueChars(); | |
232 int remaining = ValueLen(); | |
233 while (remaining > 0) { | |
234 if ((*s == '\n') || (*s == '\t')) { | |
235 OS::Print("Un-escaped character in JSON string: '%s'\n", | |
236 ValueChars()); | |
237 ASSERT(!"illegal character in JSON string value"); | |
Tom Ball
2013/01/24 23:28:23
Assert when checking caller data? Seems an excepti
hausner
2013/01/24 23:35:15
We don't use exceptions in our C++ code. Asserting
| |
238 } | |
239 s++; | |
240 remaining--; | |
241 } | |
242 scanner_.Scan(); | |
243 break; | |
244 } | |
245 case JSONScanner::TokenInteger: | |
246 case JSONScanner::TokenTrue: | |
247 case JSONScanner::TokenFalse: | |
248 case JSONScanner::TokenNull: | |
249 scanner_.Scan(); | |
250 break; | |
251 default: | |
252 OS::Print("Malformed JSON: expected a value but got '%s'\n", | |
253 scanner_.TokenChars()); | |
254 ASSERT(!"illegal JSON value found"); | |
255 } | |
256 } | |
257 | |
258 #define CHECK_TOKEN(token) \ | |
259 if (scanner_.CurrentToken() != token) { \ | |
260 OS::Print("Malformed JSON: expected %s but got '%s'\n", \ | |
261 #token, scanner_.TokenChars()); \ | |
262 } \ | |
263 ASSERT(scanner_.CurrentToken() == token); | |
264 | |
265 void JSONReader::CheckArray() { | |
266 CHECK_TOKEN(JSONScanner::TokenLBrack); | |
267 scanner_.Scan(); | |
268 while (scanner_.CurrentToken() != JSONScanner::TokenRBrack) { | |
269 CheckValue(); | |
270 if (scanner_.CurrentToken() != JSONScanner::TokenComma) { | |
271 break; | |
272 } | |
273 scanner_.Scan(); | |
274 } | |
275 CHECK_TOKEN(JSONScanner::TokenRBrack); | |
276 scanner_.Scan(); | |
277 } | |
278 | |
279 | |
280 void JSONReader::CheckObject() { | |
281 CHECK_TOKEN(JSONScanner::TokenLBrace); | |
282 scanner_.Scan(); | |
283 while (scanner_.CurrentToken() == JSONScanner::TokenString) { | |
284 scanner_.Scan(); | |
285 CHECK_TOKEN(JSONScanner::TokenColon); | |
286 scanner_.Scan(); | |
287 CheckValue(); | |
288 if (scanner_.CurrentToken() != JSONScanner::TokenComma) { | |
289 break; | |
290 } | |
291 scanner_.Scan(); | |
292 } | |
293 CHECK_TOKEN(JSONScanner::TokenRBrace); | |
294 scanner_.Scan(); | |
295 } | |
296 | |
297 #undef CHECK_TOKEN | |
298 | |
299 | |
212 bool JSONReader::Seek(const char* name) { | 300 bool JSONReader::Seek(const char* name) { |
213 error_ = false; | 301 error_ = false; |
214 scanner_.SetText(json_object_); | 302 scanner_.SetText(json_object_); |
215 scanner_.Scan(); | 303 scanner_.Scan(); |
216 if (scanner_.CurrentToken() != JSONScanner::TokenLBrace) { | 304 if (scanner_.CurrentToken() != JSONScanner::TokenLBrace) { |
217 error_ = true; | 305 error_ = true; |
218 return false; | 306 return false; |
219 } | 307 } |
220 scanner_.Scan(); | 308 scanner_.Scan(); |
221 if (scanner_.CurrentToken() == JSONScanner::TokenRBrace) { | 309 if (scanner_.CurrentToken() == JSONScanner::TokenRBrace) { |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 // the debugger front-end. | 555 // the debugger front-end. |
468 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; | 556 intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; |
469 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); | 557 char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); |
470 ASSERT(new_buf != NULL); | 558 ASSERT(new_buf != NULL); |
471 buf_ = new_buf; | 559 buf_ = new_buf; |
472 buf_size_ = new_size; | 560 buf_size_ = new_size; |
473 } | 561 } |
474 } | 562 } |
475 | 563 |
476 } // namespace dart | 564 } // namespace dart |
OLD | NEW |