blob: df76cc3f37c16f89984d40b1d619d19e21661a48 [file] [log] [blame]
Anthony Liguori4a5fcab2009-11-11 10:39:23 -06001/*
2 * JSON Parser
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
Amos Kongc96c84a2010-03-24 23:12:05 +080014#include <stdarg.h>
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060015
16#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/qstring.h"
18#include "qapi/qmp/qint.h"
19#include "qapi/qmp/qdict.h"
20#include "qapi/qmp/qlist.h"
21#include "qapi/qmp/qfloat.h"
22#include "qapi/qmp/qbool.h"
23#include "qapi/qmp/json-parser.h"
24#include "qapi/qmp/json-lexer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060025
26typedef struct JSONParserContext
27{
Anthony Liguorief749d02011-06-01 12:14:50 -050028 Error *err;
Michael Roth65c0f1e2012-08-15 13:45:43 -050029 struct {
30 QObject **buf;
31 size_t pos;
32 size_t count;
33 } tokens;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060034} JSONParserContext;
35
36#define BUG_ON(cond) assert(!(cond))
37
38/**
39 * TODO
40 *
41 * 0) make errors meaningful again
42 * 1) add geometry information to tokens
43 * 3) should we return a parsed size?
44 * 4) deal with premature EOI
45 */
46
Michael Roth65c0f1e2012-08-15 13:45:43 -050047static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060048
49/**
50 * Token manipulators
51 *
52 * tokens are dictionaries that contain a type, a string value, and geometry information
53 * about a token identified by the lexer. These are routines that make working with
54 * these objects a bit easier.
55 */
56static const char *token_get_value(QObject *obj)
57{
58 return qdict_get_str(qobject_to_qdict(obj), "token");
59}
60
61static JSONTokenType token_get_type(QObject *obj)
62{
63 return qdict_get_int(qobject_to_qdict(obj), "type");
64}
65
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060066static int token_is_escape(QObject *obj, const char *value)
67{
68 if (token_get_type(obj) != JSON_ESCAPE) {
69 return 0;
70 }
71
72 return (strcmp(token_get_value(obj), value) == 0);
73}
74
75/**
76 * Error handler
77 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020078static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
79 QObject *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060080{
Amos Kongc96c84a2010-03-24 23:12:05 +080081 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050082 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080083 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050084 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080085 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050086 if (ctxt->err) {
87 error_free(ctxt->err);
88 ctxt->err = NULL;
89 }
Cole Robinsonf231b882014-03-21 19:42:26 -040090 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060091}
92
93/**
94 * String helpers
95 *
96 * These helpers are used to unescape strings.
97 */
98static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
99{
100 if (wchar <= 0x007F) {
101 BUG_ON(buffer_length < 2);
102
103 buffer[0] = wchar & 0x7F;
104 buffer[1] = 0;
105 } else if (wchar <= 0x07FF) {
106 BUG_ON(buffer_length < 3);
107
108 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
109 buffer[1] = 0x80 | (wchar & 0x3F);
110 buffer[2] = 0;
111 } else {
112 BUG_ON(buffer_length < 4);
113
114 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
115 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
116 buffer[2] = 0x80 | (wchar & 0x3F);
117 buffer[3] = 0;
118 }
119}
120
121static int hex2decimal(char ch)
122{
123 if (ch >= '0' && ch <= '9') {
124 return (ch - '0');
125 } else if (ch >= 'a' && ch <= 'f') {
126 return 10 + (ch - 'a');
127 } else if (ch >= 'A' && ch <= 'F') {
128 return 10 + (ch - 'A');
129 }
130
131 return -1;
132}
133
134/**
135 * parse_string(): Parse a json string and return a QObject
136 *
137 * string
138 * ""
139 * " chars "
140 * chars
141 * char
142 * char chars
143 * char
144 * any-Unicode-character-
145 * except-"-or-\-or-
146 * control-character
147 * \"
148 * \\
149 * \/
150 * \b
151 * \f
152 * \n
153 * \r
154 * \t
155 * \u four-hex-digits
156 */
157static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *token)
158{
159 const char *ptr = token_get_value(token);
160 QString *str;
161 int double_quote = 1;
162
163 if (*ptr == '"') {
164 double_quote = 1;
165 } else {
166 double_quote = 0;
167 }
168 ptr++;
169
170 str = qstring_new();
171 while (*ptr &&
172 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
173 if (*ptr == '\\') {
174 ptr++;
175
176 switch (*ptr) {
177 case '"':
178 qstring_append(str, "\"");
179 ptr++;
180 break;
181 case '\'':
182 qstring_append(str, "'");
183 ptr++;
184 break;
185 case '\\':
186 qstring_append(str, "\\");
187 ptr++;
188 break;
189 case '/':
190 qstring_append(str, "/");
191 ptr++;
192 break;
193 case 'b':
194 qstring_append(str, "\b");
195 ptr++;
196 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300197 case 'f':
198 qstring_append(str, "\f");
199 ptr++;
200 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600201 case 'n':
202 qstring_append(str, "\n");
203 ptr++;
204 break;
205 case 'r':
206 qstring_append(str, "\r");
207 ptr++;
208 break;
209 case 't':
210 qstring_append(str, "\t");
211 ptr++;
212 break;
213 case 'u': {
214 uint16_t unicode_char = 0;
215 char utf8_char[4];
216 int i = 0;
217
218 ptr++;
219
220 for (i = 0; i < 4; i++) {
221 if (qemu_isxdigit(*ptr)) {
222 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
223 } else {
224 parse_error(ctxt, token,
225 "invalid hex escape sequence in string");
226 goto out;
227 }
228 ptr++;
229 }
230
231 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
232 qstring_append(str, utf8_char);
233 } break;
234 default:
235 parse_error(ctxt, token, "invalid escape sequence in string");
236 goto out;
237 }
238 } else {
239 char dummy[2];
240
241 dummy[0] = *ptr++;
242 dummy[1] = 0;
243
244 qstring_append(str, dummy);
245 }
246 }
247
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600248 return str;
249
250out:
251 QDECREF(str);
252 return NULL;
253}
254
Michael Roth65c0f1e2012-08-15 13:45:43 -0500255static QObject *parser_context_pop_token(JSONParserContext *ctxt)
256{
257 QObject *token;
258 g_assert(ctxt->tokens.pos < ctxt->tokens.count);
259 token = ctxt->tokens.buf[ctxt->tokens.pos];
260 ctxt->tokens.pos++;
261 return token;
262}
263
264/* Note: parser_context_{peek|pop}_token do not increment the
265 * token object's refcount. In both cases the references will continue
266 * to be tracked and cleaned up in parser_context_free(), so do not
267 * attempt to free the token object.
268 */
269static QObject *parser_context_peek_token(JSONParserContext *ctxt)
270{
271 QObject *token;
272 g_assert(ctxt->tokens.pos < ctxt->tokens.count);
273 token = ctxt->tokens.buf[ctxt->tokens.pos];
274 return token;
275}
276
277static JSONParserContext parser_context_save(JSONParserContext *ctxt)
278{
279 JSONParserContext saved_ctxt = {0};
280 saved_ctxt.tokens.pos = ctxt->tokens.pos;
281 saved_ctxt.tokens.count = ctxt->tokens.count;
282 saved_ctxt.tokens.buf = ctxt->tokens.buf;
283 return saved_ctxt;
284}
285
286static void parser_context_restore(JSONParserContext *ctxt,
287 JSONParserContext saved_ctxt)
288{
289 ctxt->tokens.pos = saved_ctxt.tokens.pos;
290 ctxt->tokens.count = saved_ctxt.tokens.count;
291 ctxt->tokens.buf = saved_ctxt.tokens.buf;
292}
293
294static void tokens_append_from_iter(QObject *obj, void *opaque)
295{
296 JSONParserContext *ctxt = opaque;
297 g_assert(ctxt->tokens.pos < ctxt->tokens.count);
298 ctxt->tokens.buf[ctxt->tokens.pos++] = obj;
299 qobject_incref(obj);
300}
301
302static JSONParserContext *parser_context_new(QList *tokens)
303{
304 JSONParserContext *ctxt;
305 size_t count;
306
307 if (!tokens) {
308 return NULL;
309 }
310
311 count = qlist_size(tokens);
312 if (count == 0) {
313 return NULL;
314 }
315
316 ctxt = g_malloc0(sizeof(JSONParserContext));
317 ctxt->tokens.pos = 0;
318 ctxt->tokens.count = count;
319 ctxt->tokens.buf = g_malloc(count * sizeof(QObject *));
320 qlist_iter(tokens, tokens_append_from_iter, ctxt);
321 ctxt->tokens.pos = 0;
322
323 return ctxt;
324}
325
326/* to support error propagation, ctxt->err must be freed separately */
327static void parser_context_free(JSONParserContext *ctxt)
328{
329 int i;
330 if (ctxt) {
331 for (i = 0; i < ctxt->tokens.count; i++) {
332 qobject_decref(ctxt->tokens.buf[i]);
333 }
334 g_free(ctxt->tokens.buf);
335 g_free(ctxt);
336 }
337}
338
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600339/**
340 * Parsing rules
341 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500342static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600343{
Anthony Liguori11e8a462011-06-01 12:14:55 -0500344 QObject *key = NULL, *token = NULL, *value, *peek;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500345 JSONParserContext saved_ctxt = parser_context_save(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600346
Michael Roth65c0f1e2012-08-15 13:45:43 -0500347 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500348 if (peek == NULL) {
349 parse_error(ctxt, NULL, "premature EOI");
350 goto out;
351 }
352
Michael Roth65c0f1e2012-08-15 13:45:43 -0500353 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100354 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600355 parse_error(ctxt, peek, "key is not a string in object");
356 goto out;
357 }
358
Michael Roth65c0f1e2012-08-15 13:45:43 -0500359 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500360 if (token == NULL) {
361 parse_error(ctxt, NULL, "premature EOI");
362 goto out;
363 }
364
Markus Armbrusterc5461662015-11-25 22:23:26 +0100365 if (token_get_type(token) != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600366 parse_error(ctxt, token, "missing : in object pair");
367 goto out;
368 }
369
Michael Roth65c0f1e2012-08-15 13:45:43 -0500370 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600371 if (value == NULL) {
372 parse_error(ctxt, token, "Missing value in dict");
373 goto out;
374 }
375
376 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
377
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600378 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600379
380 return 0;
381
382out:
Michael Roth65c0f1e2012-08-15 13:45:43 -0500383 parser_context_restore(ctxt, saved_ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600384 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600385
386 return -1;
387}
388
Michael Roth65c0f1e2012-08-15 13:45:43 -0500389static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600390{
391 QDict *dict = NULL;
392 QObject *token, *peek;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500393 JSONParserContext saved_ctxt = parser_context_save(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600394
Michael Roth65c0f1e2012-08-15 13:45:43 -0500395 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500396 if (token == NULL) {
397 goto out;
398 }
399
Markus Armbrusterc5461662015-11-25 22:23:26 +0100400 if (token_get_type(token) != JSON_LCURLY) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600401 goto out;
402 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600403
404 dict = qdict_new();
405
Michael Roth65c0f1e2012-08-15 13:45:43 -0500406 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500407 if (peek == NULL) {
408 parse_error(ctxt, NULL, "premature EOI");
409 goto out;
410 }
411
Markus Armbrusterc5461662015-11-25 22:23:26 +0100412 if (token_get_type(peek) != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500413 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600414 goto out;
415 }
416
Michael Roth65c0f1e2012-08-15 13:45:43 -0500417 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500418 if (token == NULL) {
419 parse_error(ctxt, NULL, "premature EOI");
420 goto out;
421 }
422
Markus Armbrusterc5461662015-11-25 22:23:26 +0100423 while (token_get_type(token) != JSON_RCURLY) {
424 if (token_get_type(token) != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600425 parse_error(ctxt, token, "expected separator in dict");
426 goto out;
427 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428
Michael Roth65c0f1e2012-08-15 13:45:43 -0500429 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430 goto out;
431 }
432
Michael Roth65c0f1e2012-08-15 13:45:43 -0500433 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500434 if (token == NULL) {
435 parse_error(ctxt, NULL, "premature EOI");
436 goto out;
437 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600438 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600439 } else {
Gongleia491af42014-06-10 17:20:24 +0800440 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600441 }
442
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600443 return QOBJECT(dict);
444
445out:
Michael Roth65c0f1e2012-08-15 13:45:43 -0500446 parser_context_restore(ctxt, saved_ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600447 QDECREF(dict);
448 return NULL;
449}
450
Michael Roth65c0f1e2012-08-15 13:45:43 -0500451static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600452{
453 QList *list = NULL;
454 QObject *token, *peek;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500455 JSONParserContext saved_ctxt = parser_context_save(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600456
Michael Roth65c0f1e2012-08-15 13:45:43 -0500457 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500458 if (token == NULL) {
459 goto out;
460 }
461
Markus Armbrusterc5461662015-11-25 22:23:26 +0100462 if (token_get_type(token) != JSON_LSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600463 goto out;
464 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600465
466 list = qlist_new();
467
Michael Roth65c0f1e2012-08-15 13:45:43 -0500468 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500469 if (peek == NULL) {
470 parse_error(ctxt, NULL, "premature EOI");
471 goto out;
472 }
473
Markus Armbrusterc5461662015-11-25 22:23:26 +0100474 if (token_get_type(peek) != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600475 QObject *obj;
476
Michael Roth65c0f1e2012-08-15 13:45:43 -0500477 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600478 if (obj == NULL) {
479 parse_error(ctxt, token, "expecting value");
480 goto out;
481 }
482
483 qlist_append_obj(list, obj);
484
Michael Roth65c0f1e2012-08-15 13:45:43 -0500485 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500486 if (token == NULL) {
487 parse_error(ctxt, NULL, "premature EOI");
488 goto out;
489 }
490
Markus Armbrusterc5461662015-11-25 22:23:26 +0100491 while (token_get_type(token) != JSON_RSQUARE) {
492 if (token_get_type(token) != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600493 parse_error(ctxt, token, "expected separator in list");
494 goto out;
495 }
496
Michael Roth65c0f1e2012-08-15 13:45:43 -0500497 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600498 if (obj == NULL) {
499 parse_error(ctxt, token, "expecting value");
500 goto out;
501 }
502
503 qlist_append_obj(list, obj);
504
Michael Roth65c0f1e2012-08-15 13:45:43 -0500505 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500506 if (token == NULL) {
507 parse_error(ctxt, NULL, "premature EOI");
508 goto out;
509 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600510 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600511 } else {
Gongleia491af42014-06-10 17:20:24 +0800512 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600513 }
514
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600515 return QOBJECT(list);
516
517out:
Michael Roth65c0f1e2012-08-15 13:45:43 -0500518 parser_context_restore(ctxt, saved_ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600519 QDECREF(list);
520 return NULL;
521}
522
Michael Roth65c0f1e2012-08-15 13:45:43 -0500523static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600524{
525 QObject *token, *ret;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500526 JSONParserContext saved_ctxt = parser_context_save(ctxt);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100527 const char *val;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600528
Michael Roth65c0f1e2012-08-15 13:45:43 -0500529 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500530 if (token == NULL) {
531 goto out;
532 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600533
534 if (token_get_type(token) != JSON_KEYWORD) {
535 goto out;
536 }
537
Markus Armbruster50e2a462015-11-25 22:23:27 +0100538 val = token_get_value(token);
539
540 if (!strcmp(val, "true")) {
Eric Blakefc48ffc2015-05-15 16:24:59 -0600541 ret = QOBJECT(qbool_from_bool(true));
Markus Armbruster50e2a462015-11-25 22:23:27 +0100542 } else if (!strcmp(val, "false")) {
Eric Blakefc48ffc2015-05-15 16:24:59 -0600543 ret = QOBJECT(qbool_from_bool(false));
Markus Armbruster50e2a462015-11-25 22:23:27 +0100544 } else if (!strcmp(val, "null")) {
Eric Blakee549e712015-04-29 15:35:06 -0600545 ret = qnull();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600546 } else {
Markus Armbruster50e2a462015-11-25 22:23:27 +0100547 parse_error(ctxt, token, "invalid keyword '%s'", val);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600548 goto out;
549 }
550
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600551 return ret;
552
553out:
Michael Roth65c0f1e2012-08-15 13:45:43 -0500554 parser_context_restore(ctxt, saved_ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600555
556 return NULL;
557}
558
Michael Roth65c0f1e2012-08-15 13:45:43 -0500559static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600560{
561 QObject *token = NULL, *obj;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500562 JSONParserContext saved_ctxt = parser_context_save(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600563
564 if (ap == NULL) {
565 goto out;
566 }
567
Michael Roth65c0f1e2012-08-15 13:45:43 -0500568 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500569 if (token == NULL) {
570 goto out;
571 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600572
573 if (token_is_escape(token, "%p")) {
574 obj = va_arg(*ap, QObject *);
575 } else if (token_is_escape(token, "%i")) {
Eric Blakefc48ffc2015-05-15 16:24:59 -0600576 obj = QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600577 } else if (token_is_escape(token, "%d")) {
578 obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
579 } else if (token_is_escape(token, "%ld")) {
580 obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
Roy Tam2c0d4b32010-02-04 10:30:30 +0800581 } else if (token_is_escape(token, "%lld") ||
582 token_is_escape(token, "%I64d")) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600583 obj = QOBJECT(qint_from_int(va_arg(*ap, long long)));
584 } else if (token_is_escape(token, "%s")) {
585 obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
586 } else if (token_is_escape(token, "%f")) {
587 obj = QOBJECT(qfloat_from_double(va_arg(*ap, double)));
588 } else {
589 goto out;
590 }
591
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600592 return obj;
593
594out:
Michael Roth65c0f1e2012-08-15 13:45:43 -0500595 parser_context_restore(ctxt, saved_ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600596
597 return NULL;
598}
599
Michael Roth65c0f1e2012-08-15 13:45:43 -0500600static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600601{
602 QObject *token, *obj;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500603 JSONParserContext saved_ctxt = parser_context_save(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600604
Michael Roth65c0f1e2012-08-15 13:45:43 -0500605 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500606 if (token == NULL) {
607 goto out;
608 }
609
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600610 switch (token_get_type(token)) {
611 case JSON_STRING:
612 obj = QOBJECT(qstring_from_escaped_str(ctxt, token));
613 break;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500614 case JSON_INTEGER: {
615 /* A possibility exists that this is a whole-valued float where the
616 * fractional part was left out due to being 0 (.0). It's not a big
617 * deal to treat these as ints in the parser, so long as users of the
618 * resulting QObject know to expect a QInt in place of a QFloat in
619 * cases like these.
620 *
621 * However, in some cases these values will overflow/underflow a
622 * QInt/int64 container, thus we should assume these are to be handled
623 * as QFloats/doubles rather than silently changing their values.
624 *
625 * strtoll() indicates these instances by setting errno to ERANGE
626 */
627 int64_t value;
628
629 errno = 0; /* strtoll doesn't set errno on success */
630 value = strtoll(token_get_value(token), NULL, 10);
631 if (errno != ERANGE) {
632 obj = QOBJECT(qint_from_int(value));
633 break;
634 }
635 /* fall through to JSON_FLOAT */
636 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600637 case JSON_FLOAT:
638 /* FIXME dependent on locale */
639 obj = QOBJECT(qfloat_from_double(strtod(token_get_value(token), NULL)));
640 break;
641 default:
642 goto out;
643 }
644
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600645 return obj;
646
647out:
Michael Roth65c0f1e2012-08-15 13:45:43 -0500648 parser_context_restore(ctxt, saved_ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600649
650 return NULL;
651}
652
Michael Roth65c0f1e2012-08-15 13:45:43 -0500653static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600654{
655 QObject *obj;
656
Michael Roth65c0f1e2012-08-15 13:45:43 -0500657 obj = parse_object(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600658 if (obj == NULL) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500659 obj = parse_array(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600660 }
661 if (obj == NULL) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500662 obj = parse_escape(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600663 }
664 if (obj == NULL) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500665 obj = parse_keyword(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600666 }
667 if (obj == NULL) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500668 obj = parse_literal(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600669 }
670
671 return obj;
672}
673
674QObject *json_parser_parse(QList *tokens, va_list *ap)
675{
Anthony Liguorief749d02011-06-01 12:14:50 -0500676 return json_parser_parse_err(tokens, ap, NULL);
677}
678
679QObject *json_parser_parse_err(QList *tokens, va_list *ap, Error **errp)
680{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500681 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600682 QObject *result;
683
Michael Roth65c0f1e2012-08-15 13:45:43 -0500684 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500685 return NULL;
686 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600687
Michael Roth65c0f1e2012-08-15 13:45:43 -0500688 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600689
Michael Roth65c0f1e2012-08-15 13:45:43 -0500690 error_propagate(errp, ctxt->err);
691
692 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500693
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600694 return result;
695}