blob: d498db6e7027e644521d6dbf10597a2f98efd476 [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 Armbruster856dfd82019-05-23 16:35:06 +020015#include "qemu/ctype.h"
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +040016#include "qemu/cutils.h"
Markus Armbrustere59f39d2018-08-23 18:39:49 +020017#include "qemu/unicode.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.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"
Markus Armbruster86cdf9e2018-08-23 18:40:20 +020025#include "json-parser-int.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060026
Markus Armbrusterabe7c202018-08-23 18:40:18 +020027struct JSONToken {
28 JSONTokenType type;
29 int x;
30 int y;
31 char str[];
32};
33
Zhang Han5086c992020-12-28 15:11:26 +080034typedef struct JSONParserContext {
Anthony Liguorief749d02011-06-01 12:14:50 -050035 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010036 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010037 GQueue *buf;
Markus Armbrusterada74c32018-08-23 18:40:22 +020038 va_list *ap;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060039} JSONParserContext;
40
41#define BUG_ON(cond) assert(!(cond))
42
43/**
44 * TODO
45 *
46 * 0) make errors meaningful again
47 * 1) add geometry information to tokens
48 * 3) should we return a parsed size?
49 * 4) deal with premature EOI
50 */
51
Markus Armbrusterada74c32018-08-23 18:40:22 +020052static QObject *parse_value(JSONParserContext *ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060053
54/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060055 * Error handler
56 */
Marc-André Lureau9edc6312022-02-20 20:39:25 +040057static void G_GNUC_PRINTF(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010058 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060059{
Amos Kongc96c84a2010-03-24 23:12:05 +080060 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050061 char message[1024];
Markus Armbruster574bf162018-08-23 18:39:50 +020062
63 if (ctxt->err) {
64 return;
65 }
Amos Kongc96c84a2010-03-24 23:12:05 +080066 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050067 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080068 va_end(ap);
Cole Robinsonf231b882014-03-21 19:42:26 -040069 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060070}
71
Markus Armbrusterdc45a072018-08-23 18:39:56 +020072static int cvt4hex(const char *s)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060073{
Markus Armbrusterdc45a072018-08-23 18:39:56 +020074 int cp, i;
75
76 cp = 0;
77 for (i = 0; i < 4; i++) {
78 if (!qemu_isxdigit(s[i])) {
79 return -1;
80 }
81 cp <<= 4;
82 if (s[i] >= '0' && s[i] <= '9') {
83 cp |= s[i] - '0';
84 } else if (s[i] >= 'a' && s[i] <= 'f') {
85 cp |= 10 + s[i] - 'a';
86 } else if (s[i] >= 'A' && s[i] <= 'F') {
87 cp |= 10 + s[i] - 'A';
88 } else {
89 return -1;
90 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060091 }
Markus Armbrusterdc45a072018-08-23 18:39:56 +020092 return cp;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060093}
94
95/**
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020096 * parse_string(): Parse a JSON string
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060097 *
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020098 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
99 * Interchange Format":
100 *
101 * char = unescaped /
102 * escape (
103 * %x22 / ; " quotation mark U+0022
104 * %x5C / ; \ reverse solidus U+005C
105 * %x2F / ; / solidus U+002F
106 * %x62 / ; b backspace U+0008
107 * %x66 / ; f form feed U+000C
108 * %x6E / ; n line feed U+000A
109 * %x72 / ; r carriage return U+000D
110 * %x74 / ; t tab U+0009
111 * %x75 4HEXDIG ) ; uXXXX U+XXXX
112 * escape = %x5C ; \
113 * quotation-mark = %x22 ; "
114 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
115 *
116 * Extensions over RFC 8259:
117 * - Extra escape sequence in strings:
118 * 0x27 (apostrophe) is recognized after escape, too
119 * - Single-quoted strings:
120 * Like double-quoted strings, except they're delimited by %x27
121 * (apostrophe) instead of %x22 (quotation mark), and can't contain
122 * unescaped apostrophe, but can contain unescaped quotation mark.
123 *
124 * Note:
125 * - Encoding is modified UTF-8.
126 * - Invalid Unicode characters are rejected.
127 * - Control characters \x00..\x1F are rejected by the lexer.
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600128 */
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200129static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600130{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100131 const char *ptr = token->str;
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100132 GString *str;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200133 char quote;
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200134 const char *beg;
135 int cp, trailing;
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200136 char *end;
137 ssize_t len;
138 char utf8_buf[5];
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600139
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200140 assert(*ptr == '"' || *ptr == '\'');
141 quote = *ptr++;
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100142 str = g_string_new(NULL);
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200143
144 while (*ptr != quote) {
145 assert(*ptr);
Markus Armbruster16a48592018-08-23 18:40:23 +0200146 switch (*ptr) {
147 case '\\':
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200148 beg = ptr++;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200149 switch (*ptr++) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600150 case '"':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100151 g_string_append_c(str, '"');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600152 break;
153 case '\'':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100154 g_string_append_c(str, '\'');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600155 break;
156 case '\\':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100157 g_string_append_c(str, '\\');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600158 break;
159 case '/':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100160 g_string_append_c(str, '/');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600161 break;
162 case 'b':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100163 g_string_append_c(str, '\b');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600164 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300165 case 'f':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100166 g_string_append_c(str, '\f');
Luiz Capitulinobd032692010-05-19 17:06:15 -0300167 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600168 case 'n':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100169 g_string_append_c(str, '\n');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600170 break;
171 case 'r':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100172 g_string_append_c(str, '\r');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600173 break;
174 case 't':
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100175 g_string_append_c(str, '\t');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600176 break;
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200177 case 'u':
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200178 cp = cvt4hex(ptr);
179 ptr += 4;
180
181 /* handle surrogate pairs */
182 if (cp >= 0xD800 && cp <= 0xDBFF
183 && ptr[0] == '\\' && ptr[1] == 'u') {
184 /* leading surrogate followed by \u */
185 cp = 0x10000 + ((cp & 0x3FF) << 10);
186 trailing = cvt4hex(ptr + 2);
187 if (trailing >= 0xDC00 && trailing <= 0xDFFF) {
188 /* followed by trailing surrogate */
189 cp |= trailing & 0x3FF;
190 ptr += 6;
191 } else {
192 cp = -1; /* invalid */
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600193 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600194 }
195
Markus Armbruster46a628b2018-08-23 18:39:55 +0200196 if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) {
197 parse_error(ctxt, token,
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200198 "%.*s is not a valid Unicode character",
199 (int)(ptr - beg), beg);
Markus Armbruster46a628b2018-08-23 18:39:55 +0200200 goto out;
201 }
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100202 g_string_append(str, utf8_buf);
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200203 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600204 default:
205 parse_error(ctxt, token, "invalid escape sequence in string");
206 goto out;
207 }
Markus Armbruster16a48592018-08-23 18:40:23 +0200208 break;
209 case '%':
Christophe Fergeaubbc05862019-01-02 15:05:35 +0100210 if (ctxt->ap) {
211 if (ptr[1] != '%') {
212 parse_error(ctxt, token, "can't interpolate into string");
213 goto out;
214 }
215 ptr++;
Markus Armbruster16a48592018-08-23 18:40:23 +0200216 }
217 /* fall through */
218 default:
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200219 cp = mod_utf8_codepoint(ptr, 6, &end);
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +0200220 if (cp < 0) {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200221 parse_error(ctxt, token, "invalid UTF-8 sequence in string");
222 goto out;
223 }
224 ptr = end;
225 len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
226 assert(len >= 0);
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100227 g_string_append(str, utf8_buf);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600228 }
229 }
230
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100231 return qstring_from_gstring(str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600232
233out:
Markus Armbruster68af4cc2020-12-11 18:11:49 +0100234 g_string_free(str, true);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600235 return NULL;
236}
237
Paolo Bonzini9bada892015-11-25 22:23:32 +0100238/* Note: the token object returned by parser_context_peek_token or
239 * parser_context_pop_token is deleted as soon as parser_context_pop_token
240 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100241 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100242static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500243{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100244 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100245 ctxt->current = g_queue_pop_head(ctxt->buf);
246 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500247}
248
Paolo Bonzini9bada892015-11-25 22:23:32 +0100249static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500250{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100251 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500252}
253
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600254/**
255 * Parsing rules
256 */
Markus Armbrusterada74c32018-08-23 18:40:22 +0200257static int parse_pair(JSONParserContext *ctxt, QDict *dict)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600258{
Alex Chen922d42b2020-11-13 14:55:25 +0000259 QObject *key_obj = NULL;
260 QString *key;
Max Reitz532fb532018-03-10 16:14:36 -0600261 QObject *value;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100262 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600263
Michael Roth65c0f1e2012-08-15 13:45:43 -0500264 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500265 if (peek == NULL) {
266 parse_error(ctxt, NULL, "premature EOI");
267 goto out;
268 }
269
Alex Chen922d42b2020-11-13 14:55:25 +0000270 key_obj = parse_value(ctxt);
271 key = qobject_to(QString, key_obj);
Max Reitz532fb532018-03-10 16:14:36 -0600272 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600273 parse_error(ctxt, peek, "key is not a string in object");
274 goto out;
275 }
276
Michael Roth65c0f1e2012-08-15 13:45:43 -0500277 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500278 if (token == NULL) {
279 parse_error(ctxt, NULL, "premature EOI");
280 goto out;
281 }
282
Paolo Bonzini9bada892015-11-25 22:23:32 +0100283 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600284 parse_error(ctxt, token, "missing : in object pair");
285 goto out;
286 }
287
Markus Armbrusterada74c32018-08-23 18:40:22 +0200288 value = parse_value(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600289 if (value == NULL) {
290 parse_error(ctxt, token, "Missing value in dict");
291 goto out;
292 }
293
Markus Armbruster00382fa2018-12-06 13:17:43 +0100294 if (qdict_haskey(dict, qstring_get_str(key))) {
295 parse_error(ctxt, token, "duplicate key");
296 goto out;
297 }
298
Max Reitz532fb532018-03-10 16:14:36 -0600299 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600300
Alex Chen922d42b2020-11-13 14:55:25 +0000301 qobject_unref(key_obj);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600302 return 0;
303
304out:
Alex Chen922d42b2020-11-13 14:55:25 +0000305 qobject_unref(key_obj);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600306 return -1;
307}
308
Markus Armbrusterada74c32018-08-23 18:40:22 +0200309static QObject *parse_object(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600310{
311 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100312 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600313
Michael Roth65c0f1e2012-08-15 13:45:43 -0500314 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100315 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600316
317 dict = qdict_new();
318
Michael Roth65c0f1e2012-08-15 13:45:43 -0500319 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500320 if (peek == NULL) {
321 parse_error(ctxt, NULL, "premature EOI");
322 goto out;
323 }
324
Paolo Bonzini9bada892015-11-25 22:23:32 +0100325 if (peek->type != JSON_RCURLY) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200326 if (parse_pair(ctxt, dict) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600327 goto out;
328 }
329
Michael Roth65c0f1e2012-08-15 13:45:43 -0500330 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500331 if (token == NULL) {
332 parse_error(ctxt, NULL, "premature EOI");
333 goto out;
334 }
335
Paolo Bonzini9bada892015-11-25 22:23:32 +0100336 while (token->type != JSON_RCURLY) {
337 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600338 parse_error(ctxt, token, "expected separator in dict");
339 goto out;
340 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600341
Markus Armbrusterada74c32018-08-23 18:40:22 +0200342 if (parse_pair(ctxt, dict) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600343 goto out;
344 }
345
Michael Roth65c0f1e2012-08-15 13:45:43 -0500346 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500347 if (token == NULL) {
348 parse_error(ctxt, NULL, "premature EOI");
349 goto out;
350 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600352 } else {
Gongleia491af42014-06-10 17:20:24 +0800353 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600354 }
355
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600356 return QOBJECT(dict);
357
358out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200359 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600360 return NULL;
361}
362
Markus Armbrusterada74c32018-08-23 18:40:22 +0200363static QObject *parse_array(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364{
365 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100366 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600367
Michael Roth65c0f1e2012-08-15 13:45:43 -0500368 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100369 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600370
371 list = qlist_new();
372
Michael Roth65c0f1e2012-08-15 13:45:43 -0500373 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500374 if (peek == NULL) {
375 parse_error(ctxt, NULL, "premature EOI");
376 goto out;
377 }
378
Paolo Bonzini9bada892015-11-25 22:23:32 +0100379 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600380 QObject *obj;
381
Markus Armbrusterada74c32018-08-23 18:40:22 +0200382 obj = parse_value(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600383 if (obj == NULL) {
384 parse_error(ctxt, token, "expecting value");
385 goto out;
386 }
387
388 qlist_append_obj(list, obj);
389
Michael Roth65c0f1e2012-08-15 13:45:43 -0500390 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500391 if (token == NULL) {
392 parse_error(ctxt, NULL, "premature EOI");
393 goto out;
394 }
395
Paolo Bonzini9bada892015-11-25 22:23:32 +0100396 while (token->type != JSON_RSQUARE) {
397 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600398 parse_error(ctxt, token, "expected separator in list");
399 goto out;
400 }
401
Markus Armbrusterada74c32018-08-23 18:40:22 +0200402 obj = parse_value(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600403 if (obj == NULL) {
404 parse_error(ctxt, token, "expecting value");
405 goto out;
406 }
407
408 qlist_append_obj(list, obj);
409
Michael Roth65c0f1e2012-08-15 13:45:43 -0500410 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500411 if (token == NULL) {
412 parse_error(ctxt, NULL, "premature EOI");
413 goto out;
414 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600415 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600416 } else {
Gongleia491af42014-06-10 17:20:24 +0800417 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600418 }
419
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600420 return QOBJECT(list);
421
422out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200423 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600424 return NULL;
425}
426
Michael Roth65c0f1e2012-08-15 13:45:43 -0500427static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100429 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430
Michael Roth65c0f1e2012-08-15 13:45:43 -0500431 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100432 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100433
Paolo Bonzini9bada892015-11-25 22:23:32 +0100434 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100435 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100436 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100437 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100438 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200439 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600440 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100441 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600442 return NULL;
443}
444
Markus Armbrusterada74c32018-08-23 18:40:22 +0200445static QObject *parse_interpolation(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600446{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100447 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600448
Michael Roth65c0f1e2012-08-15 13:45:43 -0500449 token = parser_context_pop_token(ctxt);
Markus Armbruster61030282018-08-23 18:40:04 +0200450 assert(token && token->type == JSON_INTERP);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100451
Paolo Bonzini9bada892015-11-25 22:23:32 +0100452 if (!strcmp(token->str, "%p")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200453 return va_arg(*ctxt->ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100454 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200455 return QOBJECT(qbool_from_bool(va_arg(*ctxt->ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100456 } else if (!strcmp(token->str, "%d")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200457 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100458 } else if (!strcmp(token->str, "%ld")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200459 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200460 } else if (!strcmp(token->str, "%lld")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200461 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200462 } else if (!strcmp(token->str, "%" PRId64)) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200463 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int64_t)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400464 } else if (!strcmp(token->str, "%u")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200465 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned int)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400466 } else if (!strcmp(token->str, "%lu")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200467 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200468 } else if (!strcmp(token->str, "%llu")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200469 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200470 } else if (!strcmp(token->str, "%" PRIu64)) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200471 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, uint64_t)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100472 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200473 return QOBJECT(qstring_from_str(va_arg(*ctxt->ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100474 } else if (!strcmp(token->str, "%f")) {
Markus Armbrusterada74c32018-08-23 18:40:22 +0200475 return QOBJECT(qnum_from_double(va_arg(*ctxt->ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600476 }
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200477 parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600478 return NULL;
479}
480
Michael Roth65c0f1e2012-08-15 13:45:43 -0500481static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600482{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100483 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600484
Michael Roth65c0f1e2012-08-15 13:45:43 -0500485 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100486 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500487
Paolo Bonzini9bada892015-11-25 22:23:32 +0100488 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600489 case JSON_STRING:
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200490 return QOBJECT(parse_string(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500491 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400492 /*
493 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400494 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
495 * and qemu_strtou64() fail with ERANGE when it's not
496 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500497 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400498 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400499 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
500 * integer, and qnum_get_double() both for any JSON_INTEGER
501 * and any JSON_FLOAT (with precision loss for integers beyond
502 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500503 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400504 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500505 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400506 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500507
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400508 ret = qemu_strtoi64(token->str, NULL, 10, &value);
509 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400510 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500511 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400512 assert(ret == -ERANGE);
513
514 if (token->str[0] != '-') {
515 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
516 if (!ret) {
517 return QOBJECT(qnum_from_uint(uvalue));
518 }
519 assert(ret == -ERANGE);
520 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500521 }
Philippe Mathieu-Daudé6f0dd6c2019-07-19 15:14:19 +0200522 /* fall through to JSON_FLOAT */
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600523 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700524 /* FIXME dependent on locale; a pervasive issue in QEMU */
Markus Armbruster37aded92018-08-23 18:40:25 +0200525 /* FIXME our lexer matches RFC 8259 in forbidding Inf or NaN,
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700526 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400527 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600528 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100529 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600530 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600531}
532
Markus Armbrusterada74c32018-08-23 18:40:22 +0200533static QObject *parse_value(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600534{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100535 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600536
Markus Armbrusterd538b252015-11-25 22:23:30 +0100537 token = parser_context_peek_token(ctxt);
538 if (token == NULL) {
539 parse_error(ctxt, NULL, "premature EOI");
540 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600541 }
542
Paolo Bonzini9bada892015-11-25 22:23:32 +0100543 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100544 case JSON_LCURLY:
Markus Armbrusterada74c32018-08-23 18:40:22 +0200545 return parse_object(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100546 case JSON_LSQUARE:
Markus Armbrusterada74c32018-08-23 18:40:22 +0200547 return parse_array(ctxt);
Markus Armbruster61030282018-08-23 18:40:04 +0200548 case JSON_INTERP:
Markus Armbrusterada74c32018-08-23 18:40:22 +0200549 return parse_interpolation(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100550 case JSON_INTEGER:
551 case JSON_FLOAT:
552 case JSON_STRING:
553 return parse_literal(ctxt);
554 case JSON_KEYWORD:
555 return parse_keyword(ctxt);
556 default:
557 parse_error(ctxt, token, "expecting value");
558 return NULL;
559 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600560}
561
Markus Armbrusterabe7c202018-08-23 18:40:18 +0200562JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr)
563{
564 JSONToken *token = g_malloc(sizeof(JSONToken) + tokstr->len + 1);
565
566 token->type = type;
567 memcpy(token->str, tokstr->str, tokstr->len);
568 token->str[tokstr->len] = 0;
569 token->x = x;
570 token->y = y;
571 return token;
572}
573
Markus Armbruster62815d82018-08-23 18:40:01 +0200574QObject *json_parser_parse(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500575{
Markus Armbrusterada74c32018-08-23 18:40:22 +0200576 JSONParserContext ctxt = { .buf = tokens, .ap = ap };
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600577 QObject *result;
578
Markus Armbrusterada74c32018-08-23 18:40:22 +0200579 result = parse_value(&ctxt);
Markus Armbruster5d501132018-08-23 18:40:13 +0200580 assert(ctxt.err || g_queue_is_empty(ctxt.buf));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600581
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200582 error_propagate(errp, ctxt.err);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500583
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200584 while (!g_queue_is_empty(ctxt.buf)) {
585 parser_context_pop_token(&ctxt);
586 }
587 g_free(ctxt.current);
Anthony Liguorief749d02011-06-01 12:14:50 -0500588
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600589 return result;
590}