blob: 6c05f6cc7088da47d302979c8fe236fd48f3c52a [file] [log] [blame]
Anthony Liguori4a5fcab2009-11-11 10:39:23 -06001/*
Eric Blake6e8e5cb2016-01-29 06:48:37 -07002 * JSON Parser
Anthony Liguori4a5fcab2009-11-11 10:39:23 -06003 *
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
Peter Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.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"
Paolo Bonzini9bada892015-11-25 22:23:32 +010025#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060026
27typedef struct JSONParserContext
28{
Anthony Liguorief749d02011-06-01 12:14:50 -050029 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010030 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010031 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060032} JSONParserContext;
33
34#define BUG_ON(cond) assert(!(cond))
35
36/**
37 * TODO
38 *
39 * 0) make errors meaningful again
40 * 1) add geometry information to tokens
41 * 3) should we return a parsed size?
42 * 4) deal with premature EOI
43 */
44
Michael Roth65c0f1e2012-08-15 13:45:43 -050045static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060046
47/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060048 * Error handler
49 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020050static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010051 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060052{
Amos Kongc96c84a2010-03-24 23:12:05 +080053 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050054 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080055 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050056 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080057 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050058 if (ctxt->err) {
59 error_free(ctxt->err);
60 ctxt->err = NULL;
61 }
Cole Robinsonf231b882014-03-21 19:42:26 -040062 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060063}
64
65/**
66 * String helpers
67 *
68 * These helpers are used to unescape strings.
69 */
70static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
71{
72 if (wchar <= 0x007F) {
73 BUG_ON(buffer_length < 2);
74
75 buffer[0] = wchar & 0x7F;
76 buffer[1] = 0;
77 } else if (wchar <= 0x07FF) {
78 BUG_ON(buffer_length < 3);
79
80 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
81 buffer[1] = 0x80 | (wchar & 0x3F);
82 buffer[2] = 0;
83 } else {
84 BUG_ON(buffer_length < 4);
85
86 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
87 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
88 buffer[2] = 0x80 | (wchar & 0x3F);
89 buffer[3] = 0;
90 }
91}
92
93static int hex2decimal(char ch)
94{
95 if (ch >= '0' && ch <= '9') {
96 return (ch - '0');
97 } else if (ch >= 'a' && ch <= 'f') {
98 return 10 + (ch - 'a');
99 } else if (ch >= 'A' && ch <= 'F') {
100 return 10 + (ch - 'A');
101 }
102
103 return -1;
104}
105
106/**
107 * parse_string(): Parse a json string and return a QObject
108 *
109 * string
110 * ""
111 * " chars "
112 * chars
113 * char
114 * char chars
115 * char
116 * any-Unicode-character-
117 * except-"-or-\-or-
118 * control-character
119 * \"
120 * \\
121 * \/
122 * \b
123 * \f
124 * \n
125 * \r
126 * \t
127 * \u four-hex-digits
128 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100129static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
130 JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600131{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100132 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600133 QString *str;
134 int double_quote = 1;
135
136 if (*ptr == '"') {
137 double_quote = 1;
138 } else {
139 double_quote = 0;
140 }
141 ptr++;
142
143 str = qstring_new();
144 while (*ptr &&
145 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
146 if (*ptr == '\\') {
147 ptr++;
148
149 switch (*ptr) {
150 case '"':
151 qstring_append(str, "\"");
152 ptr++;
153 break;
154 case '\'':
155 qstring_append(str, "'");
156 ptr++;
157 break;
158 case '\\':
159 qstring_append(str, "\\");
160 ptr++;
161 break;
162 case '/':
163 qstring_append(str, "/");
164 ptr++;
165 break;
166 case 'b':
167 qstring_append(str, "\b");
168 ptr++;
169 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300170 case 'f':
171 qstring_append(str, "\f");
172 ptr++;
173 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600174 case 'n':
175 qstring_append(str, "\n");
176 ptr++;
177 break;
178 case 'r':
179 qstring_append(str, "\r");
180 ptr++;
181 break;
182 case 't':
183 qstring_append(str, "\t");
184 ptr++;
185 break;
186 case 'u': {
187 uint16_t unicode_char = 0;
188 char utf8_char[4];
189 int i = 0;
190
191 ptr++;
192
193 for (i = 0; i < 4; i++) {
194 if (qemu_isxdigit(*ptr)) {
195 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
196 } else {
197 parse_error(ctxt, token,
198 "invalid hex escape sequence in string");
199 goto out;
200 }
201 ptr++;
202 }
203
204 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
205 qstring_append(str, utf8_char);
206 } break;
207 default:
208 parse_error(ctxt, token, "invalid escape sequence in string");
209 goto out;
210 }
211 } else {
212 char dummy[2];
213
214 dummy[0] = *ptr++;
215 dummy[1] = 0;
216
217 qstring_append(str, dummy);
218 }
219 }
220
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600221 return str;
222
223out:
224 QDECREF(str);
225 return NULL;
226}
227
Paolo Bonzini9bada892015-11-25 22:23:32 +0100228/* Note: the token object returned by parser_context_peek_token or
229 * parser_context_pop_token is deleted as soon as parser_context_pop_token
230 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100231 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100232static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500233{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100234 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100235 assert(!g_queue_is_empty(ctxt->buf));
236 ctxt->current = g_queue_pop_head(ctxt->buf);
237 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500238}
239
Paolo Bonzini9bada892015-11-25 22:23:32 +0100240static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500241{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100242 assert(!g_queue_is_empty(ctxt->buf));
243 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500244}
245
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100246static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500247{
248 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500249
250 if (!tokens) {
251 return NULL;
252 }
253
Michael Roth65c0f1e2012-08-15 13:45:43 -0500254 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100255 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500256
257 return ctxt;
258}
259
260/* to support error propagation, ctxt->err must be freed separately */
261static void parser_context_free(JSONParserContext *ctxt)
262{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500263 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100264 while (!g_queue_is_empty(ctxt->buf)) {
265 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500266 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100267 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100268 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500269 g_free(ctxt);
270 }
271}
272
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600273/**
274 * Parsing rules
275 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500276static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600277{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100278 QObject *key = NULL, *value;
279 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600280
Michael Roth65c0f1e2012-08-15 13:45:43 -0500281 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500282 if (peek == NULL) {
283 parse_error(ctxt, NULL, "premature EOI");
284 goto out;
285 }
286
Michael Roth65c0f1e2012-08-15 13:45:43 -0500287 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100288 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600289 parse_error(ctxt, peek, "key is not a string in object");
290 goto out;
291 }
292
Michael Roth65c0f1e2012-08-15 13:45:43 -0500293 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500294 if (token == NULL) {
295 parse_error(ctxt, NULL, "premature EOI");
296 goto out;
297 }
298
Paolo Bonzini9bada892015-11-25 22:23:32 +0100299 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600300 parse_error(ctxt, token, "missing : in object pair");
301 goto out;
302 }
303
Michael Roth65c0f1e2012-08-15 13:45:43 -0500304 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600305 if (value == NULL) {
306 parse_error(ctxt, token, "Missing value in dict");
307 goto out;
308 }
309
310 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
311
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600312 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600313
314 return 0;
315
316out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600317 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600318
319 return -1;
320}
321
Michael Roth65c0f1e2012-08-15 13:45:43 -0500322static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600323{
324 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100325 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600326
Michael Roth65c0f1e2012-08-15 13:45:43 -0500327 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100328 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600329
330 dict = qdict_new();
331
Michael Roth65c0f1e2012-08-15 13:45:43 -0500332 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500333 if (peek == NULL) {
334 parse_error(ctxt, NULL, "premature EOI");
335 goto out;
336 }
337
Paolo Bonzini9bada892015-11-25 22:23:32 +0100338 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500339 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600340 goto out;
341 }
342
Michael Roth65c0f1e2012-08-15 13:45:43 -0500343 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500344 if (token == NULL) {
345 parse_error(ctxt, NULL, "premature EOI");
346 goto out;
347 }
348
Paolo Bonzini9bada892015-11-25 22:23:32 +0100349 while (token->type != JSON_RCURLY) {
350 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351 parse_error(ctxt, token, "expected separator in dict");
352 goto out;
353 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600354
Michael Roth65c0f1e2012-08-15 13:45:43 -0500355 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600356 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 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600365 } else {
Gongleia491af42014-06-10 17:20:24 +0800366 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600367 }
368
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600369 return QOBJECT(dict);
370
371out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600372 QDECREF(dict);
373 return NULL;
374}
375
Michael Roth65c0f1e2012-08-15 13:45:43 -0500376static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600377{
378 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100379 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600380
Michael Roth65c0f1e2012-08-15 13:45:43 -0500381 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100382 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600383
384 list = qlist_new();
385
Michael Roth65c0f1e2012-08-15 13:45:43 -0500386 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500387 if (peek == NULL) {
388 parse_error(ctxt, NULL, "premature EOI");
389 goto out;
390 }
391
Paolo Bonzini9bada892015-11-25 22:23:32 +0100392 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600393 QObject *obj;
394
Michael Roth65c0f1e2012-08-15 13:45:43 -0500395 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600396 if (obj == NULL) {
397 parse_error(ctxt, token, "expecting value");
398 goto out;
399 }
400
401 qlist_append_obj(list, obj);
402
Michael Roth65c0f1e2012-08-15 13:45:43 -0500403 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500404 if (token == NULL) {
405 parse_error(ctxt, NULL, "premature EOI");
406 goto out;
407 }
408
Paolo Bonzini9bada892015-11-25 22:23:32 +0100409 while (token->type != JSON_RSQUARE) {
410 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600411 parse_error(ctxt, token, "expected separator in list");
412 goto out;
413 }
414
Michael Roth65c0f1e2012-08-15 13:45:43 -0500415 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600416 if (obj == NULL) {
417 parse_error(ctxt, token, "expecting value");
418 goto out;
419 }
420
421 qlist_append_obj(list, obj);
422
Michael Roth65c0f1e2012-08-15 13:45:43 -0500423 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500424 if (token == NULL) {
425 parse_error(ctxt, NULL, "premature EOI");
426 goto out;
427 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600429 } else {
Gongleia491af42014-06-10 17:20:24 +0800430 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600431 }
432
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600433 return QOBJECT(list);
434
435out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600436 QDECREF(list);
437 return NULL;
438}
439
Michael Roth65c0f1e2012-08-15 13:45:43 -0500440static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600441{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100442 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600443
Michael Roth65c0f1e2012-08-15 13:45:43 -0500444 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100445 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100446
Paolo Bonzini9bada892015-11-25 22:23:32 +0100447 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100448 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100449 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100450 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100451 } else if (!strcmp(token->str, "null")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100452 return qnull();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600453 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100454 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600455 return NULL;
456}
457
Michael Roth65c0f1e2012-08-15 13:45:43 -0500458static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600459{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100460 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600461
462 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100463 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600464 }
465
Michael Roth65c0f1e2012-08-15 13:45:43 -0500466 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100467 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100468
Paolo Bonzini9bada892015-11-25 22:23:32 +0100469 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100470 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100471 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100472 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100473 } else if (!strcmp(token->str, "%d")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100474 return QOBJECT(qint_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100475 } else if (!strcmp(token->str, "%ld")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100476 return QOBJECT(qint_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100477 } else if (!strcmp(token->str, "%lld") ||
478 !strcmp(token->str, "%I64d")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100479 return QOBJECT(qint_from_int(va_arg(*ap, long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100480 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100481 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100482 } else if (!strcmp(token->str, "%f")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100483 return QOBJECT(qfloat_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600484 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600485 return NULL;
486}
487
Michael Roth65c0f1e2012-08-15 13:45:43 -0500488static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600489{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100490 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600491
Michael Roth65c0f1e2012-08-15 13:45:43 -0500492 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100493 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500494
Paolo Bonzini9bada892015-11-25 22:23:32 +0100495 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600496 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100497 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500498 case JSON_INTEGER: {
499 /* A possibility exists that this is a whole-valued float where the
500 * fractional part was left out due to being 0 (.0). It's not a big
501 * deal to treat these as ints in the parser, so long as users of the
502 * resulting QObject know to expect a QInt in place of a QFloat in
503 * cases like these.
504 *
505 * However, in some cases these values will overflow/underflow a
506 * QInt/int64 container, thus we should assume these are to be handled
507 * as QFloats/doubles rather than silently changing their values.
508 *
509 * strtoll() indicates these instances by setting errno to ERANGE
510 */
511 int64_t value;
512
513 errno = 0; /* strtoll doesn't set errno on success */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100514 value = strtoll(token->str, NULL, 10);
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500515 if (errno != ERANGE) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100516 return QOBJECT(qint_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500517 }
518 /* fall through to JSON_FLOAT */
519 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600520 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700521 /* FIXME dependent on locale; a pervasive issue in QEMU */
522 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
523 * but those might be useful extensions beyond JSON */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100524 return QOBJECT(qfloat_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600525 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100526 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600527 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600528}
529
Michael Roth65c0f1e2012-08-15 13:45:43 -0500530static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600531{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100532 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600533
Markus Armbrusterd538b252015-11-25 22:23:30 +0100534 token = parser_context_peek_token(ctxt);
535 if (token == NULL) {
536 parse_error(ctxt, NULL, "premature EOI");
537 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600538 }
539
Paolo Bonzini9bada892015-11-25 22:23:32 +0100540 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100541 case JSON_LCURLY:
542 return parse_object(ctxt, ap);
543 case JSON_LSQUARE:
544 return parse_array(ctxt, ap);
545 case JSON_ESCAPE:
546 return parse_escape(ctxt, ap);
547 case JSON_INTEGER:
548 case JSON_FLOAT:
549 case JSON_STRING:
550 return parse_literal(ctxt);
551 case JSON_KEYWORD:
552 return parse_keyword(ctxt);
553 default:
554 parse_error(ctxt, token, "expecting value");
555 return NULL;
556 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600557}
558
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100559QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600560{
Anthony Liguorief749d02011-06-01 12:14:50 -0500561 return json_parser_parse_err(tokens, ap, NULL);
562}
563
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100564QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500565{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500566 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600567 QObject *result;
568
Michael Roth65c0f1e2012-08-15 13:45:43 -0500569 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500570 return NULL;
571 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600572
Michael Roth65c0f1e2012-08-15 13:45:43 -0500573 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600574
Michael Roth65c0f1e2012-08-15 13:45:43 -0500575 error_propagate(errp, ctxt->err);
576
577 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500578
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600579 return result;
580}