blob: e3ee2a273ad3b566dfb2aaac92cce60b27904ba5 [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"
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +040015#include "qemu/cutils.h"
Markus Armbrustere59f39d2018-08-23 18:39:49 +020016#include "qemu/unicode.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060018#include "qemu-common.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010019#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010020#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010021#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010022#include "qapi/qmp/qnull.h"
23#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010024#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010025#include "qapi/qmp/json-parser.h"
26#include "qapi/qmp/json-lexer.h"
Paolo Bonzini9bada892015-11-25 22:23:32 +010027#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060028
29typedef struct JSONParserContext
30{
Anthony Liguorief749d02011-06-01 12:14:50 -050031 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010032 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010033 GQueue *buf;
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/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060050 * Error handler
51 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020052static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010053 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060054{
Amos Kongc96c84a2010-03-24 23:12:05 +080055 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050056 char message[1024];
Markus Armbruster574bf162018-08-23 18:39:50 +020057
58 if (ctxt->err) {
59 return;
60 }
Amos Kongc96c84a2010-03-24 23:12:05 +080061 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050062 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080063 va_end(ap);
Cole Robinsonf231b882014-03-21 19:42:26 -040064 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060065}
66
Markus Armbrusterdc45a072018-08-23 18:39:56 +020067static int cvt4hex(const char *s)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060068{
Markus Armbrusterdc45a072018-08-23 18:39:56 +020069 int cp, i;
70
71 cp = 0;
72 for (i = 0; i < 4; i++) {
73 if (!qemu_isxdigit(s[i])) {
74 return -1;
75 }
76 cp <<= 4;
77 if (s[i] >= '0' && s[i] <= '9') {
78 cp |= s[i] - '0';
79 } else if (s[i] >= 'a' && s[i] <= 'f') {
80 cp |= 10 + s[i] - 'a';
81 } else if (s[i] >= 'A' && s[i] <= 'F') {
82 cp |= 10 + s[i] - 'A';
83 } else {
84 return -1;
85 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060086 }
Markus Armbrusterdc45a072018-08-23 18:39:56 +020087 return cp;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060088}
89
90/**
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020091 * parse_string(): Parse a JSON string
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060092 *
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020093 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
94 * Interchange Format":
95 *
96 * char = unescaped /
97 * escape (
98 * %x22 / ; " quotation mark U+0022
99 * %x5C / ; \ reverse solidus U+005C
100 * %x2F / ; / solidus U+002F
101 * %x62 / ; b backspace U+0008
102 * %x66 / ; f form feed U+000C
103 * %x6E / ; n line feed U+000A
104 * %x72 / ; r carriage return U+000D
105 * %x74 / ; t tab U+0009
106 * %x75 4HEXDIG ) ; uXXXX U+XXXX
107 * escape = %x5C ; \
108 * quotation-mark = %x22 ; "
109 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
110 *
111 * Extensions over RFC 8259:
112 * - Extra escape sequence in strings:
113 * 0x27 (apostrophe) is recognized after escape, too
114 * - Single-quoted strings:
115 * Like double-quoted strings, except they're delimited by %x27
116 * (apostrophe) instead of %x22 (quotation mark), and can't contain
117 * unescaped apostrophe, but can contain unescaped quotation mark.
118 *
119 * Note:
120 * - Encoding is modified UTF-8.
121 * - Invalid Unicode characters are rejected.
122 * - Control characters \x00..\x1F are rejected by the lexer.
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600123 */
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200124static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600125{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100126 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600127 QString *str;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200128 char quote;
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200129 const char *beg;
130 int cp, trailing;
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200131 char *end;
132 ssize_t len;
133 char utf8_buf[5];
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600134
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200135 assert(*ptr == '"' || *ptr == '\'');
136 quote = *ptr++;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600137 str = qstring_new();
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200138
139 while (*ptr != quote) {
140 assert(*ptr);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600141 if (*ptr == '\\') {
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200142 beg = ptr++;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200143 switch (*ptr++) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600144 case '"':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200145 qstring_append_chr(str, '"');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600146 break;
147 case '\'':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200148 qstring_append_chr(str, '\'');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600149 break;
150 case '\\':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200151 qstring_append_chr(str, '\\');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600152 break;
153 case '/':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200154 qstring_append_chr(str, '/');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600155 break;
156 case 'b':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200157 qstring_append_chr(str, '\b');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600158 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300159 case 'f':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200160 qstring_append_chr(str, '\f');
Luiz Capitulinobd032692010-05-19 17:06:15 -0300161 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600162 case 'n':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200163 qstring_append_chr(str, '\n');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600164 break;
165 case 'r':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200166 qstring_append_chr(str, '\r');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600167 break;
168 case 't':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200169 qstring_append_chr(str, '\t');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600170 break;
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200171 case 'u':
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200172 cp = cvt4hex(ptr);
173 ptr += 4;
174
175 /* handle surrogate pairs */
176 if (cp >= 0xD800 && cp <= 0xDBFF
177 && ptr[0] == '\\' && ptr[1] == 'u') {
178 /* leading surrogate followed by \u */
179 cp = 0x10000 + ((cp & 0x3FF) << 10);
180 trailing = cvt4hex(ptr + 2);
181 if (trailing >= 0xDC00 && trailing <= 0xDFFF) {
182 /* followed by trailing surrogate */
183 cp |= trailing & 0x3FF;
184 ptr += 6;
185 } else {
186 cp = -1; /* invalid */
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600187 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600188 }
189
Markus Armbruster46a628b2018-08-23 18:39:55 +0200190 if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) {
191 parse_error(ctxt, token,
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200192 "%.*s is not a valid Unicode character",
193 (int)(ptr - beg), beg);
Markus Armbruster46a628b2018-08-23 18:39:55 +0200194 goto out;
195 }
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200196 qstring_append(str, utf8_buf);
197 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600198 default:
199 parse_error(ctxt, token, "invalid escape sequence in string");
200 goto out;
201 }
202 } else {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200203 cp = mod_utf8_codepoint(ptr, 6, &end);
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +0200204 if (cp < 0) {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200205 parse_error(ctxt, token, "invalid UTF-8 sequence in string");
206 goto out;
207 }
208 ptr = end;
209 len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
210 assert(len >= 0);
211 qstring_append(str, utf8_buf);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600212 }
213 }
214
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600215 return str;
216
217out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200218 qobject_unref(str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600219 return NULL;
220}
221
Paolo Bonzini9bada892015-11-25 22:23:32 +0100222/* Note: the token object returned by parser_context_peek_token or
223 * parser_context_pop_token is deleted as soon as parser_context_pop_token
224 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100225 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100226static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500227{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100228 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100229 ctxt->current = g_queue_pop_head(ctxt->buf);
230 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500231}
232
Paolo Bonzini9bada892015-11-25 22:23:32 +0100233static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500234{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100235 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500236}
237
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600238/**
239 * Parsing rules
240 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500241static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600242{
Max Reitz532fb532018-03-10 16:14:36 -0600243 QObject *value;
244 QString *key = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100245 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600246
Michael Roth65c0f1e2012-08-15 13:45:43 -0500247 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500248 if (peek == NULL) {
249 parse_error(ctxt, NULL, "premature EOI");
250 goto out;
251 }
252
Max Reitz532fb532018-03-10 16:14:36 -0600253 key = qobject_to(QString, parse_value(ctxt, ap));
254 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600255 parse_error(ctxt, peek, "key is not a string in object");
256 goto out;
257 }
258
Michael Roth65c0f1e2012-08-15 13:45:43 -0500259 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500260 if (token == NULL) {
261 parse_error(ctxt, NULL, "premature EOI");
262 goto out;
263 }
264
Paolo Bonzini9bada892015-11-25 22:23:32 +0100265 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600266 parse_error(ctxt, token, "missing : in object pair");
267 goto out;
268 }
269
Michael Roth65c0f1e2012-08-15 13:45:43 -0500270 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600271 if (value == NULL) {
272 parse_error(ctxt, token, "Missing value in dict");
273 goto out;
274 }
275
Max Reitz532fb532018-03-10 16:14:36 -0600276 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600277
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200278 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600279
280 return 0;
281
282out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200283 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600284
285 return -1;
286}
287
Michael Roth65c0f1e2012-08-15 13:45:43 -0500288static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600289{
290 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100291 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600292
Michael Roth65c0f1e2012-08-15 13:45:43 -0500293 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100294 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600295
296 dict = qdict_new();
297
Michael Roth65c0f1e2012-08-15 13:45:43 -0500298 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500299 if (peek == NULL) {
300 parse_error(ctxt, NULL, "premature EOI");
301 goto out;
302 }
303
Paolo Bonzini9bada892015-11-25 22:23:32 +0100304 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500305 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600306 goto out;
307 }
308
Michael Roth65c0f1e2012-08-15 13:45:43 -0500309 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500310 if (token == NULL) {
311 parse_error(ctxt, NULL, "premature EOI");
312 goto out;
313 }
314
Paolo Bonzini9bada892015-11-25 22:23:32 +0100315 while (token->type != JSON_RCURLY) {
316 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600317 parse_error(ctxt, token, "expected separator in dict");
318 goto out;
319 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600320
Michael Roth65c0f1e2012-08-15 13:45:43 -0500321 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600322 goto out;
323 }
324
Michael Roth65c0f1e2012-08-15 13:45:43 -0500325 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500326 if (token == NULL) {
327 parse_error(ctxt, NULL, "premature EOI");
328 goto out;
329 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600330 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600331 } else {
Gongleia491af42014-06-10 17:20:24 +0800332 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600333 }
334
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600335 return QOBJECT(dict);
336
337out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200338 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600339 return NULL;
340}
341
Michael Roth65c0f1e2012-08-15 13:45:43 -0500342static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600343{
344 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100345 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600346
Michael Roth65c0f1e2012-08-15 13:45:43 -0500347 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100348 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600349
350 list = qlist_new();
351
Michael Roth65c0f1e2012-08-15 13:45:43 -0500352 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500353 if (peek == NULL) {
354 parse_error(ctxt, NULL, "premature EOI");
355 goto out;
356 }
357
Paolo Bonzini9bada892015-11-25 22:23:32 +0100358 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600359 QObject *obj;
360
Michael Roth65c0f1e2012-08-15 13:45:43 -0500361 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600362 if (obj == NULL) {
363 parse_error(ctxt, token, "expecting value");
364 goto out;
365 }
366
367 qlist_append_obj(list, obj);
368
Michael Roth65c0f1e2012-08-15 13:45:43 -0500369 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500370 if (token == NULL) {
371 parse_error(ctxt, NULL, "premature EOI");
372 goto out;
373 }
374
Paolo Bonzini9bada892015-11-25 22:23:32 +0100375 while (token->type != JSON_RSQUARE) {
376 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600377 parse_error(ctxt, token, "expected separator in list");
378 goto out;
379 }
380
Michael Roth65c0f1e2012-08-15 13:45:43 -0500381 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600382 if (obj == NULL) {
383 parse_error(ctxt, token, "expecting value");
384 goto out;
385 }
386
387 qlist_append_obj(list, obj);
388
Michael Roth65c0f1e2012-08-15 13:45:43 -0500389 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500390 if (token == NULL) {
391 parse_error(ctxt, NULL, "premature EOI");
392 goto out;
393 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600394 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600395 } else {
Gongleia491af42014-06-10 17:20:24 +0800396 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600397 }
398
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600399 return QOBJECT(list);
400
401out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200402 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600403 return NULL;
404}
405
Michael Roth65c0f1e2012-08-15 13:45:43 -0500406static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600407{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100408 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600409
Michael Roth65c0f1e2012-08-15 13:45:43 -0500410 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100411 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100412
Paolo Bonzini9bada892015-11-25 22:23:32 +0100413 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100414 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100415 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100416 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100417 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200418 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600419 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100420 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600421 return NULL;
422}
423
Markus Armbruster61030282018-08-23 18:40:04 +0200424static QObject *parse_interpolation(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600425{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100426 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600427
Michael Roth65c0f1e2012-08-15 13:45:43 -0500428 token = parser_context_pop_token(ctxt);
Markus Armbruster61030282018-08-23 18:40:04 +0200429 assert(token && token->type == JSON_INTERP);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100430
Paolo Bonzini9bada892015-11-25 22:23:32 +0100431 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100432 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100433 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100434 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100435 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400436 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100437 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400438 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200439 } else if (!strcmp(token->str, "%lld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400440 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200441 } else if (!strcmp(token->str, "%" PRId64)) {
442 return QOBJECT(qnum_from_int(va_arg(*ap, int64_t)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400443 } else if (!strcmp(token->str, "%u")) {
444 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
445 } else if (!strcmp(token->str, "%lu")) {
446 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200447 } else if (!strcmp(token->str, "%llu")) {
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400448 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200449 } else if (!strcmp(token->str, "%" PRIu64)) {
450 return QOBJECT(qnum_from_uint(va_arg(*ap, uint64_t)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100451 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100452 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100453 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400454 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600455 }
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200456 parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600457 return NULL;
458}
459
Michael Roth65c0f1e2012-08-15 13:45:43 -0500460static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600461{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100462 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600463
Michael Roth65c0f1e2012-08-15 13:45:43 -0500464 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100465 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500466
Paolo Bonzini9bada892015-11-25 22:23:32 +0100467 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600468 case JSON_STRING:
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200469 return QOBJECT(parse_string(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500470 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400471 /*
472 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400473 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
474 * and qemu_strtou64() fail with ERANGE when it's not
475 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500476 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400477 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400478 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
479 * integer, and qnum_get_double() both for any JSON_INTEGER
480 * and any JSON_FLOAT (with precision loss for integers beyond
481 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500482 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400483 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500484 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400485 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500486
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400487 ret = qemu_strtoi64(token->str, NULL, 10, &value);
488 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400489 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500490 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400491 assert(ret == -ERANGE);
492
493 if (token->str[0] != '-') {
494 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
495 if (!ret) {
496 return QOBJECT(qnum_from_uint(uvalue));
497 }
498 assert(ret == -ERANGE);
499 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500500 /* fall through to JSON_FLOAT */
501 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600502 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700503 /* FIXME dependent on locale; a pervasive issue in QEMU */
504 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
505 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400506 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600507 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100508 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600509 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600510}
511
Michael Roth65c0f1e2012-08-15 13:45:43 -0500512static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600513{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100514 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600515
Markus Armbrusterd538b252015-11-25 22:23:30 +0100516 token = parser_context_peek_token(ctxt);
517 if (token == NULL) {
518 parse_error(ctxt, NULL, "premature EOI");
519 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600520 }
521
Paolo Bonzini9bada892015-11-25 22:23:32 +0100522 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100523 case JSON_LCURLY:
524 return parse_object(ctxt, ap);
525 case JSON_LSQUARE:
526 return parse_array(ctxt, ap);
Markus Armbruster61030282018-08-23 18:40:04 +0200527 case JSON_INTERP:
528 return parse_interpolation(ctxt, ap);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100529 case JSON_INTEGER:
530 case JSON_FLOAT:
531 case JSON_STRING:
532 return parse_literal(ctxt);
533 case JSON_KEYWORD:
534 return parse_keyword(ctxt);
535 default:
536 parse_error(ctxt, token, "expecting value");
537 return NULL;
538 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600539}
540
Markus Armbruster62815d82018-08-23 18:40:01 +0200541QObject *json_parser_parse(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500542{
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200543 JSONParserContext ctxt = { .buf = tokens };
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600544 QObject *result;
545
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200546 result = parse_value(&ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600547
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200548 error_propagate(errp, ctxt.err);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500549
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200550 while (!g_queue_is_empty(ctxt.buf)) {
551 parser_context_pop_token(&ctxt);
552 }
553 g_free(ctxt.current);
554 g_queue_free(ctxt.buf);
Anthony Liguorief749d02011-06-01 12:14:50 -0500555
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600556 return result;
557}