blob: c18e48ab945e9573b044f35181ae59465b262158 [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"
Markus Armbrusterda34e652016-03-14 09:01:28 +010015#include "qapi/error.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060016#include "qemu-common.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060017#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/json-parser.h"
19#include "qapi/qmp/json-lexer.h"
Paolo Bonzini9bada892015-11-25 22:23:32 +010020#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060021
22typedef struct JSONParserContext
23{
Anthony Liguorief749d02011-06-01 12:14:50 -050024 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010025 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010026 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060027} JSONParserContext;
28
29#define BUG_ON(cond) assert(!(cond))
30
31/**
32 * TODO
33 *
34 * 0) make errors meaningful again
35 * 1) add geometry information to tokens
36 * 3) should we return a parsed size?
37 * 4) deal with premature EOI
38 */
39
Michael Roth65c0f1e2012-08-15 13:45:43 -050040static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060041
42/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060043 * Error handler
44 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020045static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010046 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060047{
Amos Kongc96c84a2010-03-24 23:12:05 +080048 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050049 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080050 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050051 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080052 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050053 if (ctxt->err) {
54 error_free(ctxt->err);
55 ctxt->err = NULL;
56 }
Cole Robinsonf231b882014-03-21 19:42:26 -040057 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060058}
59
60/**
61 * String helpers
62 *
63 * These helpers are used to unescape strings.
64 */
65static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
66{
67 if (wchar <= 0x007F) {
68 BUG_ON(buffer_length < 2);
69
70 buffer[0] = wchar & 0x7F;
71 buffer[1] = 0;
72 } else if (wchar <= 0x07FF) {
73 BUG_ON(buffer_length < 3);
74
75 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
76 buffer[1] = 0x80 | (wchar & 0x3F);
77 buffer[2] = 0;
78 } else {
79 BUG_ON(buffer_length < 4);
80
81 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
82 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
83 buffer[2] = 0x80 | (wchar & 0x3F);
84 buffer[3] = 0;
85 }
86}
87
88static int hex2decimal(char ch)
89{
90 if (ch >= '0' && ch <= '9') {
91 return (ch - '0');
92 } else if (ch >= 'a' && ch <= 'f') {
93 return 10 + (ch - 'a');
94 } else if (ch >= 'A' && ch <= 'F') {
95 return 10 + (ch - 'A');
96 }
97
98 return -1;
99}
100
101/**
102 * parse_string(): Parse a json string and return a QObject
103 *
104 * string
105 * ""
106 * " chars "
107 * chars
108 * char
109 * char chars
110 * char
111 * any-Unicode-character-
112 * except-"-or-\-or-
113 * control-character
114 * \"
115 * \\
116 * \/
117 * \b
118 * \f
119 * \n
120 * \r
121 * \t
122 * \u four-hex-digits
123 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100124static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
125 JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600126{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100127 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600128 QString *str;
129 int double_quote = 1;
130
131 if (*ptr == '"') {
132 double_quote = 1;
133 } else {
134 double_quote = 0;
135 }
136 ptr++;
137
138 str = qstring_new();
139 while (*ptr &&
140 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
141 if (*ptr == '\\') {
142 ptr++;
143
144 switch (*ptr) {
145 case '"':
146 qstring_append(str, "\"");
147 ptr++;
148 break;
149 case '\'':
150 qstring_append(str, "'");
151 ptr++;
152 break;
153 case '\\':
154 qstring_append(str, "\\");
155 ptr++;
156 break;
157 case '/':
158 qstring_append(str, "/");
159 ptr++;
160 break;
161 case 'b':
162 qstring_append(str, "\b");
163 ptr++;
164 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300165 case 'f':
166 qstring_append(str, "\f");
167 ptr++;
168 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600169 case 'n':
170 qstring_append(str, "\n");
171 ptr++;
172 break;
173 case 'r':
174 qstring_append(str, "\r");
175 ptr++;
176 break;
177 case 't':
178 qstring_append(str, "\t");
179 ptr++;
180 break;
181 case 'u': {
182 uint16_t unicode_char = 0;
183 char utf8_char[4];
184 int i = 0;
185
186 ptr++;
187
188 for (i = 0; i < 4; i++) {
189 if (qemu_isxdigit(*ptr)) {
190 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
191 } else {
192 parse_error(ctxt, token,
193 "invalid hex escape sequence in string");
194 goto out;
195 }
196 ptr++;
197 }
198
199 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
200 qstring_append(str, utf8_char);
201 } break;
202 default:
203 parse_error(ctxt, token, "invalid escape sequence in string");
204 goto out;
205 }
206 } else {
207 char dummy[2];
208
209 dummy[0] = *ptr++;
210 dummy[1] = 0;
211
212 qstring_append(str, dummy);
213 }
214 }
215
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600216 return str;
217
218out:
219 QDECREF(str);
220 return NULL;
221}
222
Paolo Bonzini9bada892015-11-25 22:23:32 +0100223/* Note: the token object returned by parser_context_peek_token or
224 * parser_context_pop_token is deleted as soon as parser_context_pop_token
225 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100226 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100227static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500228{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100229 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100230 assert(!g_queue_is_empty(ctxt->buf));
231 ctxt->current = g_queue_pop_head(ctxt->buf);
232 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500233}
234
Paolo Bonzini9bada892015-11-25 22:23:32 +0100235static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500236{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100237 assert(!g_queue_is_empty(ctxt->buf));
238 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500239}
240
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100241static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500242{
243 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500244
245 if (!tokens) {
246 return NULL;
247 }
248
Michael Roth65c0f1e2012-08-15 13:45:43 -0500249 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100250 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500251
252 return ctxt;
253}
254
255/* to support error propagation, ctxt->err must be freed separately */
256static void parser_context_free(JSONParserContext *ctxt)
257{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500258 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100259 while (!g_queue_is_empty(ctxt->buf)) {
260 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500261 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100262 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100263 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500264 g_free(ctxt);
265 }
266}
267
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600268/**
269 * Parsing rules
270 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500271static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600272{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100273 QObject *key = NULL, *value;
274 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600275
Michael Roth65c0f1e2012-08-15 13:45:43 -0500276 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500277 if (peek == NULL) {
278 parse_error(ctxt, NULL, "premature EOI");
279 goto out;
280 }
281
Michael Roth65c0f1e2012-08-15 13:45:43 -0500282 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100283 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600284 parse_error(ctxt, peek, "key is not a string in object");
285 goto out;
286 }
287
Michael Roth65c0f1e2012-08-15 13:45:43 -0500288 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500289 if (token == NULL) {
290 parse_error(ctxt, NULL, "premature EOI");
291 goto out;
292 }
293
Paolo Bonzini9bada892015-11-25 22:23:32 +0100294 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600295 parse_error(ctxt, token, "missing : in object pair");
296 goto out;
297 }
298
Michael Roth65c0f1e2012-08-15 13:45:43 -0500299 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600300 if (value == NULL) {
301 parse_error(ctxt, token, "Missing value in dict");
302 goto out;
303 }
304
305 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
306
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600307 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600308
309 return 0;
310
311out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600312 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600313
314 return -1;
315}
316
Michael Roth65c0f1e2012-08-15 13:45:43 -0500317static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600318{
319 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100320 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600321
Michael Roth65c0f1e2012-08-15 13:45:43 -0500322 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100323 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600324
325 dict = qdict_new();
326
Michael Roth65c0f1e2012-08-15 13:45:43 -0500327 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500328 if (peek == NULL) {
329 parse_error(ctxt, NULL, "premature EOI");
330 goto out;
331 }
332
Paolo Bonzini9bada892015-11-25 22:23:32 +0100333 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500334 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600335 goto out;
336 }
337
Michael Roth65c0f1e2012-08-15 13:45:43 -0500338 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500339 if (token == NULL) {
340 parse_error(ctxt, NULL, "premature EOI");
341 goto out;
342 }
343
Paolo Bonzini9bada892015-11-25 22:23:32 +0100344 while (token->type != JSON_RCURLY) {
345 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600346 parse_error(ctxt, token, "expected separator in dict");
347 goto out;
348 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600349
Michael Roth65c0f1e2012-08-15 13:45:43 -0500350 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351 goto out;
352 }
353
Michael Roth65c0f1e2012-08-15 13:45:43 -0500354 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500355 if (token == NULL) {
356 parse_error(ctxt, NULL, "premature EOI");
357 goto out;
358 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600359 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600360 } else {
Gongleia491af42014-06-10 17:20:24 +0800361 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600362 }
363
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364 return QOBJECT(dict);
365
366out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600367 QDECREF(dict);
368 return NULL;
369}
370
Michael Roth65c0f1e2012-08-15 13:45:43 -0500371static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600372{
373 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100374 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600375
Michael Roth65c0f1e2012-08-15 13:45:43 -0500376 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100377 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600378
379 list = qlist_new();
380
Michael Roth65c0f1e2012-08-15 13:45:43 -0500381 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500382 if (peek == NULL) {
383 parse_error(ctxt, NULL, "premature EOI");
384 goto out;
385 }
386
Paolo Bonzini9bada892015-11-25 22:23:32 +0100387 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600388 QObject *obj;
389
Michael Roth65c0f1e2012-08-15 13:45:43 -0500390 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600391 if (obj == NULL) {
392 parse_error(ctxt, token, "expecting value");
393 goto out;
394 }
395
396 qlist_append_obj(list, obj);
397
Michael Roth65c0f1e2012-08-15 13:45:43 -0500398 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500399 if (token == NULL) {
400 parse_error(ctxt, NULL, "premature EOI");
401 goto out;
402 }
403
Paolo Bonzini9bada892015-11-25 22:23:32 +0100404 while (token->type != JSON_RSQUARE) {
405 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600406 parse_error(ctxt, token, "expected separator in list");
407 goto out;
408 }
409
Michael Roth65c0f1e2012-08-15 13:45:43 -0500410 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600411 if (obj == NULL) {
412 parse_error(ctxt, token, "expecting value");
413 goto out;
414 }
415
416 qlist_append_obj(list, obj);
417
Michael Roth65c0f1e2012-08-15 13:45:43 -0500418 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500419 if (token == NULL) {
420 parse_error(ctxt, NULL, "premature EOI");
421 goto out;
422 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600423 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600424 } else {
Gongleia491af42014-06-10 17:20:24 +0800425 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600426 }
427
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428 return QOBJECT(list);
429
430out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600431 QDECREF(list);
432 return NULL;
433}
434
Michael Roth65c0f1e2012-08-15 13:45:43 -0500435static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600436{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100437 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600438
Michael Roth65c0f1e2012-08-15 13:45:43 -0500439 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100440 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100441
Paolo Bonzini9bada892015-11-25 22:23:32 +0100442 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100443 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100444 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100445 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100446 } else if (!strcmp(token->str, "null")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100447 return qnull();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600448 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100449 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600450 return NULL;
451}
452
Michael Roth65c0f1e2012-08-15 13:45:43 -0500453static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600454{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100455 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600456
457 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100458 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600459 }
460
Michael Roth65c0f1e2012-08-15 13:45:43 -0500461 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100462 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100463
Paolo Bonzini9bada892015-11-25 22:23:32 +0100464 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100465 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100466 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100467 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100468 } else if (!strcmp(token->str, "%d")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100469 return QOBJECT(qint_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100470 } else if (!strcmp(token->str, "%ld")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100471 return QOBJECT(qint_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100472 } else if (!strcmp(token->str, "%lld") ||
473 !strcmp(token->str, "%I64d")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100474 return QOBJECT(qint_from_int(va_arg(*ap, long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100475 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100476 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100477 } else if (!strcmp(token->str, "%f")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100478 return QOBJECT(qfloat_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600479 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600480 return NULL;
481}
482
Michael Roth65c0f1e2012-08-15 13:45:43 -0500483static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600484{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100485 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600486
Michael Roth65c0f1e2012-08-15 13:45:43 -0500487 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100488 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500489
Paolo Bonzini9bada892015-11-25 22:23:32 +0100490 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600491 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100492 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500493 case JSON_INTEGER: {
494 /* A possibility exists that this is a whole-valued float where the
495 * fractional part was left out due to being 0 (.0). It's not a big
496 * deal to treat these as ints in the parser, so long as users of the
497 * resulting QObject know to expect a QInt in place of a QFloat in
498 * cases like these.
499 *
500 * However, in some cases these values will overflow/underflow a
501 * QInt/int64 container, thus we should assume these are to be handled
502 * as QFloats/doubles rather than silently changing their values.
503 *
504 * strtoll() indicates these instances by setting errno to ERANGE
505 */
506 int64_t value;
507
508 errno = 0; /* strtoll doesn't set errno on success */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100509 value = strtoll(token->str, NULL, 10);
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500510 if (errno != ERANGE) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100511 return QOBJECT(qint_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500512 }
513 /* fall through to JSON_FLOAT */
514 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600515 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700516 /* FIXME dependent on locale; a pervasive issue in QEMU */
517 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
518 * but those might be useful extensions beyond JSON */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100519 return QOBJECT(qfloat_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600520 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100521 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600522 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600523}
524
Michael Roth65c0f1e2012-08-15 13:45:43 -0500525static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600526{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100527 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600528
Markus Armbrusterd538b252015-11-25 22:23:30 +0100529 token = parser_context_peek_token(ctxt);
530 if (token == NULL) {
531 parse_error(ctxt, NULL, "premature EOI");
532 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600533 }
534
Paolo Bonzini9bada892015-11-25 22:23:32 +0100535 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100536 case JSON_LCURLY:
537 return parse_object(ctxt, ap);
538 case JSON_LSQUARE:
539 return parse_array(ctxt, ap);
540 case JSON_ESCAPE:
541 return parse_escape(ctxt, ap);
542 case JSON_INTEGER:
543 case JSON_FLOAT:
544 case JSON_STRING:
545 return parse_literal(ctxt);
546 case JSON_KEYWORD:
547 return parse_keyword(ctxt);
548 default:
549 parse_error(ctxt, token, "expecting value");
550 return NULL;
551 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600552}
553
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100554QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600555{
Anthony Liguorief749d02011-06-01 12:14:50 -0500556 return json_parser_parse_err(tokens, ap, NULL);
557}
558
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100559QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500560{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500561 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600562 QObject *result;
563
Michael Roth65c0f1e2012-08-15 13:45:43 -0500564 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500565 return NULL;
566 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600567
Michael Roth65c0f1e2012-08-15 13:45:43 -0500568 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600569
Michael Roth65c0f1e2012-08-15 13:45:43 -0500570 error_propagate(errp, ctxt->err);
571
572 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500573
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600574 return result;
575}