blob: a247875f1490c76e113f576bd0fd14a52696713e [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
Markus Armbrusterabe7c202018-08-23 18:40:18 +020029struct JSONToken {
30 JSONTokenType type;
31 int x;
32 int y;
33 char str[];
34};
35
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060036typedef struct JSONParserContext
37{
Anthony Liguorief749d02011-06-01 12:14:50 -050038 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010039 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010040 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060041} JSONParserContext;
42
43#define BUG_ON(cond) assert(!(cond))
44
45/**
46 * TODO
47 *
48 * 0) make errors meaningful again
49 * 1) add geometry information to tokens
50 * 3) should we return a parsed size?
51 * 4) deal with premature EOI
52 */
53
Michael Roth65c0f1e2012-08-15 13:45:43 -050054static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060055
56/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060057 * Error handler
58 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020059static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010060 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060061{
Amos Kongc96c84a2010-03-24 23:12:05 +080062 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050063 char message[1024];
Markus Armbruster574bf162018-08-23 18:39:50 +020064
65 if (ctxt->err) {
66 return;
67 }
Amos Kongc96c84a2010-03-24 23:12:05 +080068 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050069 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080070 va_end(ap);
Cole Robinsonf231b882014-03-21 19:42:26 -040071 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060072}
73
Markus Armbrusterdc45a072018-08-23 18:39:56 +020074static int cvt4hex(const char *s)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060075{
Markus Armbrusterdc45a072018-08-23 18:39:56 +020076 int cp, i;
77
78 cp = 0;
79 for (i = 0; i < 4; i++) {
80 if (!qemu_isxdigit(s[i])) {
81 return -1;
82 }
83 cp <<= 4;
84 if (s[i] >= '0' && s[i] <= '9') {
85 cp |= s[i] - '0';
86 } else if (s[i] >= 'a' && s[i] <= 'f') {
87 cp |= 10 + s[i] - 'a';
88 } else if (s[i] >= 'A' && s[i] <= 'F') {
89 cp |= 10 + s[i] - 'A';
90 } else {
91 return -1;
92 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060093 }
Markus Armbrusterdc45a072018-08-23 18:39:56 +020094 return cp;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060095}
96
97/**
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020098 * parse_string(): Parse a JSON string
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060099 *
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200100 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
101 * Interchange Format":
102 *
103 * char = unescaped /
104 * escape (
105 * %x22 / ; " quotation mark U+0022
106 * %x5C / ; \ reverse solidus U+005C
107 * %x2F / ; / solidus U+002F
108 * %x62 / ; b backspace U+0008
109 * %x66 / ; f form feed U+000C
110 * %x6E / ; n line feed U+000A
111 * %x72 / ; r carriage return U+000D
112 * %x74 / ; t tab U+0009
113 * %x75 4HEXDIG ) ; uXXXX U+XXXX
114 * escape = %x5C ; \
115 * quotation-mark = %x22 ; "
116 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
117 *
118 * Extensions over RFC 8259:
119 * - Extra escape sequence in strings:
120 * 0x27 (apostrophe) is recognized after escape, too
121 * - Single-quoted strings:
122 * Like double-quoted strings, except they're delimited by %x27
123 * (apostrophe) instead of %x22 (quotation mark), and can't contain
124 * unescaped apostrophe, but can contain unescaped quotation mark.
125 *
126 * Note:
127 * - Encoding is modified UTF-8.
128 * - Invalid Unicode characters are rejected.
129 * - Control characters \x00..\x1F are rejected by the lexer.
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600130 */
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200131static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600132{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100133 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600134 QString *str;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200135 char quote;
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200136 const char *beg;
137 int cp, trailing;
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200138 char *end;
139 ssize_t len;
140 char utf8_buf[5];
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600141
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200142 assert(*ptr == '"' || *ptr == '\'');
143 quote = *ptr++;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600144 str = qstring_new();
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200145
146 while (*ptr != quote) {
147 assert(*ptr);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600148 if (*ptr == '\\') {
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200149 beg = ptr++;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200150 switch (*ptr++) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600151 case '"':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200152 qstring_append_chr(str, '"');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600153 break;
154 case '\'':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200155 qstring_append_chr(str, '\'');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600156 break;
157 case '\\':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200158 qstring_append_chr(str, '\\');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600159 break;
160 case '/':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200161 qstring_append_chr(str, '/');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600162 break;
163 case 'b':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200164 qstring_append_chr(str, '\b');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600165 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300166 case 'f':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200167 qstring_append_chr(str, '\f');
Luiz Capitulinobd032692010-05-19 17:06:15 -0300168 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600169 case 'n':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200170 qstring_append_chr(str, '\n');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600171 break;
172 case 'r':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200173 qstring_append_chr(str, '\r');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600174 break;
175 case 't':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200176 qstring_append_chr(str, '\t');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600177 break;
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200178 case 'u':
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200179 cp = cvt4hex(ptr);
180 ptr += 4;
181
182 /* handle surrogate pairs */
183 if (cp >= 0xD800 && cp <= 0xDBFF
184 && ptr[0] == '\\' && ptr[1] == 'u') {
185 /* leading surrogate followed by \u */
186 cp = 0x10000 + ((cp & 0x3FF) << 10);
187 trailing = cvt4hex(ptr + 2);
188 if (trailing >= 0xDC00 && trailing <= 0xDFFF) {
189 /* followed by trailing surrogate */
190 cp |= trailing & 0x3FF;
191 ptr += 6;
192 } else {
193 cp = -1; /* invalid */
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600194 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600195 }
196
Markus Armbruster46a628b2018-08-23 18:39:55 +0200197 if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) {
198 parse_error(ctxt, token,
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200199 "%.*s is not a valid Unicode character",
200 (int)(ptr - beg), beg);
Markus Armbruster46a628b2018-08-23 18:39:55 +0200201 goto out;
202 }
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200203 qstring_append(str, utf8_buf);
204 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600205 default:
206 parse_error(ctxt, token, "invalid escape sequence in string");
207 goto out;
208 }
209 } else {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200210 cp = mod_utf8_codepoint(ptr, 6, &end);
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +0200211 if (cp < 0) {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200212 parse_error(ctxt, token, "invalid UTF-8 sequence in string");
213 goto out;
214 }
215 ptr = end;
216 len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
217 assert(len >= 0);
218 qstring_append(str, utf8_buf);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600219 }
220 }
221
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600222 return str;
223
224out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200225 qobject_unref(str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600226 return NULL;
227}
228
Paolo Bonzini9bada892015-11-25 22:23:32 +0100229/* Note: the token object returned by parser_context_peek_token or
230 * parser_context_pop_token is deleted as soon as parser_context_pop_token
231 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100232 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100233static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500234{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100235 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100236 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 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500243}
244
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600245/**
246 * Parsing rules
247 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500248static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600249{
Max Reitz532fb532018-03-10 16:14:36 -0600250 QObject *value;
251 QString *key = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100252 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600253
Michael Roth65c0f1e2012-08-15 13:45:43 -0500254 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500255 if (peek == NULL) {
256 parse_error(ctxt, NULL, "premature EOI");
257 goto out;
258 }
259
Max Reitz532fb532018-03-10 16:14:36 -0600260 key = qobject_to(QString, parse_value(ctxt, ap));
261 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600262 parse_error(ctxt, peek, "key is not a string in object");
263 goto out;
264 }
265
Michael Roth65c0f1e2012-08-15 13:45:43 -0500266 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500267 if (token == NULL) {
268 parse_error(ctxt, NULL, "premature EOI");
269 goto out;
270 }
271
Paolo Bonzini9bada892015-11-25 22:23:32 +0100272 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600273 parse_error(ctxt, token, "missing : in object pair");
274 goto out;
275 }
276
Michael Roth65c0f1e2012-08-15 13:45:43 -0500277 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600278 if (value == NULL) {
279 parse_error(ctxt, token, "Missing value in dict");
280 goto out;
281 }
282
Max Reitz532fb532018-03-10 16:14:36 -0600283 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600284
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200285 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600286
287 return 0;
288
289out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200290 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600291
292 return -1;
293}
294
Michael Roth65c0f1e2012-08-15 13:45:43 -0500295static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600296{
297 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100298 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600299
Michael Roth65c0f1e2012-08-15 13:45:43 -0500300 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100301 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600302
303 dict = qdict_new();
304
Michael Roth65c0f1e2012-08-15 13:45:43 -0500305 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500306 if (peek == NULL) {
307 parse_error(ctxt, NULL, "premature EOI");
308 goto out;
309 }
310
Paolo Bonzini9bada892015-11-25 22:23:32 +0100311 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500312 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600313 goto out;
314 }
315
Michael Roth65c0f1e2012-08-15 13:45:43 -0500316 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500317 if (token == NULL) {
318 parse_error(ctxt, NULL, "premature EOI");
319 goto out;
320 }
321
Paolo Bonzini9bada892015-11-25 22:23:32 +0100322 while (token->type != JSON_RCURLY) {
323 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600324 parse_error(ctxt, token, "expected separator in dict");
325 goto out;
326 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600327
Michael Roth65c0f1e2012-08-15 13:45:43 -0500328 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600329 goto out;
330 }
331
Michael Roth65c0f1e2012-08-15 13:45:43 -0500332 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500333 if (token == NULL) {
334 parse_error(ctxt, NULL, "premature EOI");
335 goto out;
336 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600337 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600338 } else {
Gongleia491af42014-06-10 17:20:24 +0800339 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600340 }
341
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600342 return QOBJECT(dict);
343
344out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200345 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600346 return NULL;
347}
348
Michael Roth65c0f1e2012-08-15 13:45:43 -0500349static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600350{
351 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100352 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600353
Michael Roth65c0f1e2012-08-15 13:45:43 -0500354 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100355 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600356
357 list = qlist_new();
358
Michael Roth65c0f1e2012-08-15 13:45:43 -0500359 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500360 if (peek == NULL) {
361 parse_error(ctxt, NULL, "premature EOI");
362 goto out;
363 }
364
Paolo Bonzini9bada892015-11-25 22:23:32 +0100365 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600366 QObject *obj;
367
Michael Roth65c0f1e2012-08-15 13:45:43 -0500368 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600369 if (obj == NULL) {
370 parse_error(ctxt, token, "expecting value");
371 goto out;
372 }
373
374 qlist_append_obj(list, obj);
375
Michael Roth65c0f1e2012-08-15 13:45:43 -0500376 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500377 if (token == NULL) {
378 parse_error(ctxt, NULL, "premature EOI");
379 goto out;
380 }
381
Paolo Bonzini9bada892015-11-25 22:23:32 +0100382 while (token->type != JSON_RSQUARE) {
383 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600384 parse_error(ctxt, token, "expected separator in list");
385 goto out;
386 }
387
Michael Roth65c0f1e2012-08-15 13:45:43 -0500388 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600389 if (obj == NULL) {
390 parse_error(ctxt, token, "expecting value");
391 goto out;
392 }
393
394 qlist_append_obj(list, obj);
395
Michael Roth65c0f1e2012-08-15 13:45:43 -0500396 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500397 if (token == NULL) {
398 parse_error(ctxt, NULL, "premature EOI");
399 goto out;
400 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600401 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600402 } else {
Gongleia491af42014-06-10 17:20:24 +0800403 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600404 }
405
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600406 return QOBJECT(list);
407
408out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200409 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600410 return NULL;
411}
412
Michael Roth65c0f1e2012-08-15 13:45:43 -0500413static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600414{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100415 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600416
Michael Roth65c0f1e2012-08-15 13:45:43 -0500417 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100418 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100419
Paolo Bonzini9bada892015-11-25 22:23:32 +0100420 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100421 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100422 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100423 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100424 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200425 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600426 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100427 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428 return NULL;
429}
430
Markus Armbruster61030282018-08-23 18:40:04 +0200431static QObject *parse_interpolation(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600432{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100433 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600434
Michael Roth65c0f1e2012-08-15 13:45:43 -0500435 token = parser_context_pop_token(ctxt);
Markus Armbruster61030282018-08-23 18:40:04 +0200436 assert(token && token->type == JSON_INTERP);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100437
Paolo Bonzini9bada892015-11-25 22:23:32 +0100438 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100439 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100440 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100441 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100442 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400443 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100444 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400445 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200446 } else if (!strcmp(token->str, "%lld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400447 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200448 } else if (!strcmp(token->str, "%" PRId64)) {
449 return QOBJECT(qnum_from_int(va_arg(*ap, int64_t)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400450 } else if (!strcmp(token->str, "%u")) {
451 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
452 } else if (!strcmp(token->str, "%lu")) {
453 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200454 } else if (!strcmp(token->str, "%llu")) {
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400455 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200456 } else if (!strcmp(token->str, "%" PRIu64)) {
457 return QOBJECT(qnum_from_uint(va_arg(*ap, uint64_t)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100458 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100459 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100460 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400461 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600462 }
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200463 parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600464 return NULL;
465}
466
Michael Roth65c0f1e2012-08-15 13:45:43 -0500467static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600468{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100469 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600470
Michael Roth65c0f1e2012-08-15 13:45:43 -0500471 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100472 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500473
Paolo Bonzini9bada892015-11-25 22:23:32 +0100474 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600475 case JSON_STRING:
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200476 return QOBJECT(parse_string(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500477 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400478 /*
479 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400480 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
481 * and qemu_strtou64() fail with ERANGE when it's not
482 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500483 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400484 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400485 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
486 * integer, and qnum_get_double() both for any JSON_INTEGER
487 * and any JSON_FLOAT (with precision loss for integers beyond
488 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500489 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400490 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500491 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400492 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500493
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400494 ret = qemu_strtoi64(token->str, NULL, 10, &value);
495 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400496 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500497 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400498 assert(ret == -ERANGE);
499
500 if (token->str[0] != '-') {
501 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
502 if (!ret) {
503 return QOBJECT(qnum_from_uint(uvalue));
504 }
505 assert(ret == -ERANGE);
506 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500507 /* fall through to JSON_FLOAT */
508 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600509 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700510 /* FIXME dependent on locale; a pervasive issue in QEMU */
511 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
512 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400513 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600514 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100515 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600516 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600517}
518
Michael Roth65c0f1e2012-08-15 13:45:43 -0500519static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600520{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100521 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600522
Markus Armbrusterd538b252015-11-25 22:23:30 +0100523 token = parser_context_peek_token(ctxt);
524 if (token == NULL) {
525 parse_error(ctxt, NULL, "premature EOI");
526 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600527 }
528
Paolo Bonzini9bada892015-11-25 22:23:32 +0100529 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100530 case JSON_LCURLY:
531 return parse_object(ctxt, ap);
532 case JSON_LSQUARE:
533 return parse_array(ctxt, ap);
Markus Armbruster61030282018-08-23 18:40:04 +0200534 case JSON_INTERP:
535 return parse_interpolation(ctxt, ap);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100536 case JSON_INTEGER:
537 case JSON_FLOAT:
538 case JSON_STRING:
539 return parse_literal(ctxt);
540 case JSON_KEYWORD:
541 return parse_keyword(ctxt);
542 default:
543 parse_error(ctxt, token, "expecting value");
544 return NULL;
545 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600546}
547
Markus Armbrusterabe7c202018-08-23 18:40:18 +0200548JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr)
549{
550 JSONToken *token = g_malloc(sizeof(JSONToken) + tokstr->len + 1);
551
552 token->type = type;
553 memcpy(token->str, tokstr->str, tokstr->len);
554 token->str[tokstr->len] = 0;
555 token->x = x;
556 token->y = y;
557 return token;
558}
559
Markus Armbruster62815d82018-08-23 18:40:01 +0200560QObject *json_parser_parse(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500561{
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200562 JSONParserContext ctxt = { .buf = tokens };
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600563 QObject *result;
564
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200565 result = parse_value(&ctxt, ap);
Markus Armbruster5d501132018-08-23 18:40:13 +0200566 assert(ctxt.err || g_queue_is_empty(ctxt.buf));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600567
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200568 error_propagate(errp, ctxt.err);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500569
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200570 while (!g_queue_is_empty(ctxt.buf)) {
571 parser_context_pop_token(&ctxt);
572 }
573 g_free(ctxt.current);
Anthony Liguorief749d02011-06-01 12:14:50 -0500574
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600575 return result;
576}