blob: 5a84951e37a5775b702b3f3ca6954754ba2c4526 [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;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010029 QObject *current;
30 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060031} JSONParserContext;
32
33#define BUG_ON(cond) assert(!(cond))
34
35/**
36 * TODO
37 *
38 * 0) make errors meaningful again
39 * 1) add geometry information to tokens
40 * 3) should we return a parsed size?
41 * 4) deal with premature EOI
42 */
43
Michael Roth65c0f1e2012-08-15 13:45:43 -050044static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060045
46/**
47 * Token manipulators
48 *
49 * tokens are dictionaries that contain a type, a string value, and geometry information
50 * about a token identified by the lexer. These are routines that make working with
51 * these objects a bit easier.
52 */
53static const char *token_get_value(QObject *obj)
54{
55 return qdict_get_str(qobject_to_qdict(obj), "token");
56}
57
58static JSONTokenType token_get_type(QObject *obj)
59{
60 return qdict_get_int(qobject_to_qdict(obj), "type");
61}
62
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060063/**
64 * Error handler
65 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020066static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
67 QObject *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060068{
Amos Kongc96c84a2010-03-24 23:12:05 +080069 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050070 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080071 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050072 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080073 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050074 if (ctxt->err) {
75 error_free(ctxt->err);
76 ctxt->err = NULL;
77 }
Cole Robinsonf231b882014-03-21 19:42:26 -040078 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060079}
80
81/**
82 * String helpers
83 *
84 * These helpers are used to unescape strings.
85 */
86static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
87{
88 if (wchar <= 0x007F) {
89 BUG_ON(buffer_length < 2);
90
91 buffer[0] = wchar & 0x7F;
92 buffer[1] = 0;
93 } else if (wchar <= 0x07FF) {
94 BUG_ON(buffer_length < 3);
95
96 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
97 buffer[1] = 0x80 | (wchar & 0x3F);
98 buffer[2] = 0;
99 } else {
100 BUG_ON(buffer_length < 4);
101
102 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
103 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
104 buffer[2] = 0x80 | (wchar & 0x3F);
105 buffer[3] = 0;
106 }
107}
108
109static int hex2decimal(char ch)
110{
111 if (ch >= '0' && ch <= '9') {
112 return (ch - '0');
113 } else if (ch >= 'a' && ch <= 'f') {
114 return 10 + (ch - 'a');
115 } else if (ch >= 'A' && ch <= 'F') {
116 return 10 + (ch - 'A');
117 }
118
119 return -1;
120}
121
122/**
123 * parse_string(): Parse a json string and return a QObject
124 *
125 * string
126 * ""
127 * " chars "
128 * chars
129 * char
130 * char chars
131 * char
132 * any-Unicode-character-
133 * except-"-or-\-or-
134 * control-character
135 * \"
136 * \\
137 * \/
138 * \b
139 * \f
140 * \n
141 * \r
142 * \t
143 * \u four-hex-digits
144 */
145static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *token)
146{
147 const char *ptr = token_get_value(token);
148 QString *str;
149 int double_quote = 1;
150
151 if (*ptr == '"') {
152 double_quote = 1;
153 } else {
154 double_quote = 0;
155 }
156 ptr++;
157
158 str = qstring_new();
159 while (*ptr &&
160 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
161 if (*ptr == '\\') {
162 ptr++;
163
164 switch (*ptr) {
165 case '"':
166 qstring_append(str, "\"");
167 ptr++;
168 break;
169 case '\'':
170 qstring_append(str, "'");
171 ptr++;
172 break;
173 case '\\':
174 qstring_append(str, "\\");
175 ptr++;
176 break;
177 case '/':
178 qstring_append(str, "/");
179 ptr++;
180 break;
181 case 'b':
182 qstring_append(str, "\b");
183 ptr++;
184 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300185 case 'f':
186 qstring_append(str, "\f");
187 ptr++;
188 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600189 case 'n':
190 qstring_append(str, "\n");
191 ptr++;
192 break;
193 case 'r':
194 qstring_append(str, "\r");
195 ptr++;
196 break;
197 case 't':
198 qstring_append(str, "\t");
199 ptr++;
200 break;
201 case 'u': {
202 uint16_t unicode_char = 0;
203 char utf8_char[4];
204 int i = 0;
205
206 ptr++;
207
208 for (i = 0; i < 4; i++) {
209 if (qemu_isxdigit(*ptr)) {
210 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
211 } else {
212 parse_error(ctxt, token,
213 "invalid hex escape sequence in string");
214 goto out;
215 }
216 ptr++;
217 }
218
219 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
220 qstring_append(str, utf8_char);
221 } break;
222 default:
223 parse_error(ctxt, token, "invalid escape sequence in string");
224 goto out;
225 }
226 } else {
227 char dummy[2];
228
229 dummy[0] = *ptr++;
230 dummy[1] = 0;
231
232 qstring_append(str, dummy);
233 }
234 }
235
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600236 return str;
237
238out:
239 QDECREF(str);
240 return NULL;
241}
242
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100243/* Note: unless the token object returned by parser_context_peek_token
244 * or parser_context_pop_token is explicitly incref'd, it will be
245 * deleted as soon as parser_context_pop_token is called again.
246 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500247static QObject *parser_context_pop_token(JSONParserContext *ctxt)
248{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100249 qobject_decref(ctxt->current);
250 assert(!g_queue_is_empty(ctxt->buf));
251 ctxt->current = g_queue_pop_head(ctxt->buf);
252 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500253}
254
Michael Roth65c0f1e2012-08-15 13:45:43 -0500255static QObject *parser_context_peek_token(JSONParserContext *ctxt)
256{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100257 assert(!g_queue_is_empty(ctxt->buf));
258 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500259}
260
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100261static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500262{
263 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500264
265 if (!tokens) {
266 return NULL;
267 }
268
Michael Roth65c0f1e2012-08-15 13:45:43 -0500269 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100270 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500271
272 return ctxt;
273}
274
275/* to support error propagation, ctxt->err must be freed separately */
276static void parser_context_free(JSONParserContext *ctxt)
277{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500278 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100279 while (!g_queue_is_empty(ctxt->buf)) {
280 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500281 }
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100282 qobject_decref(ctxt->current);
283 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500284 g_free(ctxt);
285 }
286}
287
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600288/**
289 * Parsing rules
290 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500291static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600292{
Anthony Liguori11e8a462011-06-01 12:14:55 -0500293 QObject *key = NULL, *token = NULL, *value, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600294
Michael Roth65c0f1e2012-08-15 13:45:43 -0500295 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500296 if (peek == NULL) {
297 parse_error(ctxt, NULL, "premature EOI");
298 goto out;
299 }
300
Michael Roth65c0f1e2012-08-15 13:45:43 -0500301 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100302 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600303 parse_error(ctxt, peek, "key is not a string in object");
304 goto out;
305 }
306
Michael Roth65c0f1e2012-08-15 13:45:43 -0500307 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500308 if (token == NULL) {
309 parse_error(ctxt, NULL, "premature EOI");
310 goto out;
311 }
312
Markus Armbrusterc5461662015-11-25 22:23:26 +0100313 if (token_get_type(token) != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600314 parse_error(ctxt, token, "missing : in object pair");
315 goto out;
316 }
317
Michael Roth65c0f1e2012-08-15 13:45:43 -0500318 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600319 if (value == NULL) {
320 parse_error(ctxt, token, "Missing value in dict");
321 goto out;
322 }
323
324 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
325
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600326 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600327
328 return 0;
329
330out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600331 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600332
333 return -1;
334}
335
Michael Roth65c0f1e2012-08-15 13:45:43 -0500336static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600337{
338 QDict *dict = NULL;
339 QObject *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600340
Michael Roth65c0f1e2012-08-15 13:45:43 -0500341 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100342 assert(token && token_get_type(token) == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600343
344 dict = qdict_new();
345
Michael Roth65c0f1e2012-08-15 13:45:43 -0500346 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500347 if (peek == NULL) {
348 parse_error(ctxt, NULL, "premature EOI");
349 goto out;
350 }
351
Markus Armbrusterc5461662015-11-25 22:23:26 +0100352 if (token_get_type(peek) != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500353 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600354 goto out;
355 }
356
Michael Roth65c0f1e2012-08-15 13:45:43 -0500357 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500358 if (token == NULL) {
359 parse_error(ctxt, NULL, "premature EOI");
360 goto out;
361 }
362
Markus Armbrusterc5461662015-11-25 22:23:26 +0100363 while (token_get_type(token) != JSON_RCURLY) {
364 if (token_get_type(token) != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600365 parse_error(ctxt, token, "expected separator in dict");
366 goto out;
367 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600368
Michael Roth65c0f1e2012-08-15 13:45:43 -0500369 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600370 goto out;
371 }
372
Michael Roth65c0f1e2012-08-15 13:45:43 -0500373 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500374 if (token == NULL) {
375 parse_error(ctxt, NULL, "premature EOI");
376 goto out;
377 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600378 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600379 } else {
Gongleia491af42014-06-10 17:20:24 +0800380 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600381 }
382
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600383 return QOBJECT(dict);
384
385out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600386 QDECREF(dict);
387 return NULL;
388}
389
Michael Roth65c0f1e2012-08-15 13:45:43 -0500390static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600391{
392 QList *list = NULL;
393 QObject *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600394
Michael Roth65c0f1e2012-08-15 13:45:43 -0500395 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100396 assert(token && token_get_type(token) == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600397
398 list = qlist_new();
399
Michael Roth65c0f1e2012-08-15 13:45:43 -0500400 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500401 if (peek == NULL) {
402 parse_error(ctxt, NULL, "premature EOI");
403 goto out;
404 }
405
Markus Armbrusterc5461662015-11-25 22:23:26 +0100406 if (token_get_type(peek) != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600407 QObject *obj;
408
Michael Roth65c0f1e2012-08-15 13:45:43 -0500409 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600410 if (obj == NULL) {
411 parse_error(ctxt, token, "expecting value");
412 goto out;
413 }
414
415 qlist_append_obj(list, obj);
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_RSQUARE) {
424 if (token_get_type(token) != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600425 parse_error(ctxt, token, "expected separator in list");
426 goto out;
427 }
428
Michael Roth65c0f1e2012-08-15 13:45:43 -0500429 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430 if (obj == NULL) {
431 parse_error(ctxt, token, "expecting value");
432 goto out;
433 }
434
435 qlist_append_obj(list, obj);
436
Michael Roth65c0f1e2012-08-15 13:45:43 -0500437 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500438 if (token == NULL) {
439 parse_error(ctxt, NULL, "premature EOI");
440 goto out;
441 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600442 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600443 } else {
Gongleia491af42014-06-10 17:20:24 +0800444 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600445 }
446
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600447 return QOBJECT(list);
448
449out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600450 QDECREF(list);
451 return NULL;
452}
453
Michael Roth65c0f1e2012-08-15 13:45:43 -0500454static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600455{
Markus Armbrusterd538b252015-11-25 22:23:30 +0100456 QObject *token;
Markus Armbruster50e2a462015-11-25 22:23:27 +0100457 const char *val;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600458
Michael Roth65c0f1e2012-08-15 13:45:43 -0500459 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100460 assert(token && token_get_type(token) == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100461 val = token_get_value(token);
462
463 if (!strcmp(val, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100464 return QOBJECT(qbool_from_bool(true));
Markus Armbruster50e2a462015-11-25 22:23:27 +0100465 } else if (!strcmp(val, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100466 return QOBJECT(qbool_from_bool(false));
Markus Armbruster50e2a462015-11-25 22:23:27 +0100467 } else if (!strcmp(val, "null")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100468 return qnull();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600469 }
Markus Armbrusterd538b252015-11-25 22:23:30 +0100470 parse_error(ctxt, token, "invalid keyword '%s'", val);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600471 return NULL;
472}
473
Michael Roth65c0f1e2012-08-15 13:45:43 -0500474static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600475{
Markus Armbrusterd538b252015-11-25 22:23:30 +0100476 QObject *token;
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100477 const char *val;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600478
479 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100480 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600481 }
482
Michael Roth65c0f1e2012-08-15 13:45:43 -0500483 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100484 assert(token && token_get_type(token) == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100485 val = token_get_value(token);
486
487 if (!strcmp(val, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100488 return va_arg(*ap, QObject *);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100489 } else if (!strcmp(val, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100490 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100491 } else if (!strcmp(val, "%d")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100492 return QOBJECT(qint_from_int(va_arg(*ap, int)));
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100493 } else if (!strcmp(val, "%ld")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100494 return QOBJECT(qint_from_int(va_arg(*ap, long)));
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100495 } else if (!strcmp(val, "%lld") ||
496 !strcmp(val, "%I64d")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100497 return QOBJECT(qint_from_int(va_arg(*ap, long long)));
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100498 } else if (!strcmp(val, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100499 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100500 } else if (!strcmp(val, "%f")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100501 return QOBJECT(qfloat_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600502 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600503 return NULL;
504}
505
Michael Roth65c0f1e2012-08-15 13:45:43 -0500506static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600507{
Markus Armbrusterd538b252015-11-25 22:23:30 +0100508 QObject *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600509
Michael Roth65c0f1e2012-08-15 13:45:43 -0500510 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100511 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500512
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600513 switch (token_get_type(token)) {
514 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100515 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500516 case JSON_INTEGER: {
517 /* A possibility exists that this is a whole-valued float where the
518 * fractional part was left out due to being 0 (.0). It's not a big
519 * deal to treat these as ints in the parser, so long as users of the
520 * resulting QObject know to expect a QInt in place of a QFloat in
521 * cases like these.
522 *
523 * However, in some cases these values will overflow/underflow a
524 * QInt/int64 container, thus we should assume these are to be handled
525 * as QFloats/doubles rather than silently changing their values.
526 *
527 * strtoll() indicates these instances by setting errno to ERANGE
528 */
529 int64_t value;
530
531 errno = 0; /* strtoll doesn't set errno on success */
532 value = strtoll(token_get_value(token), NULL, 10);
533 if (errno != ERANGE) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100534 return QOBJECT(qint_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500535 }
536 /* fall through to JSON_FLOAT */
537 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600538 case JSON_FLOAT:
539 /* FIXME dependent on locale */
Markus Armbrusterd538b252015-11-25 22:23:30 +0100540 return QOBJECT(qfloat_from_double(strtod(token_get_value(token),
541 NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600542 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100543 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600544 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600545}
546
Michael Roth65c0f1e2012-08-15 13:45:43 -0500547static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600548{
Markus Armbrusterd538b252015-11-25 22:23:30 +0100549 QObject *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600550
Markus Armbrusterd538b252015-11-25 22:23:30 +0100551 token = parser_context_peek_token(ctxt);
552 if (token == NULL) {
553 parse_error(ctxt, NULL, "premature EOI");
554 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600555 }
556
Markus Armbrusterd538b252015-11-25 22:23:30 +0100557 switch (token_get_type(token)) {
558 case JSON_LCURLY:
559 return parse_object(ctxt, ap);
560 case JSON_LSQUARE:
561 return parse_array(ctxt, ap);
562 case JSON_ESCAPE:
563 return parse_escape(ctxt, ap);
564 case JSON_INTEGER:
565 case JSON_FLOAT:
566 case JSON_STRING:
567 return parse_literal(ctxt);
568 case JSON_KEYWORD:
569 return parse_keyword(ctxt);
570 default:
571 parse_error(ctxt, token, "expecting value");
572 return NULL;
573 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600574}
575
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100576QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600577{
Anthony Liguorief749d02011-06-01 12:14:50 -0500578 return json_parser_parse_err(tokens, ap, NULL);
579}
580
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100581QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500582{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500583 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600584 QObject *result;
585
Michael Roth65c0f1e2012-08-15 13:45:43 -0500586 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500587 return NULL;
588 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600589
Michael Roth65c0f1e2012-08-15 13:45:43 -0500590 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600591
Michael Roth65c0f1e2012-08-15 13:45:43 -0500592 error_propagate(errp, ctxt->err);
593
594 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500595
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600596 return result;
597}