blob: 7bfa08200cf4e7a49474627a1238931d74a9d020 [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 assert(!g_queue_is_empty(ctxt->buf));
230 ctxt->current = g_queue_pop_head(ctxt->buf);
231 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500232}
233
Paolo Bonzini9bada892015-11-25 22:23:32 +0100234static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500235{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100236 assert(!g_queue_is_empty(ctxt->buf));
237 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500238}
239
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600240/**
241 * Parsing rules
242 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500243static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600244{
Max Reitz532fb532018-03-10 16:14:36 -0600245 QObject *value;
246 QString *key = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100247 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600248
Michael Roth65c0f1e2012-08-15 13:45:43 -0500249 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500250 if (peek == NULL) {
251 parse_error(ctxt, NULL, "premature EOI");
252 goto out;
253 }
254
Max Reitz532fb532018-03-10 16:14:36 -0600255 key = qobject_to(QString, parse_value(ctxt, ap));
256 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600257 parse_error(ctxt, peek, "key is not a string in object");
258 goto out;
259 }
260
Michael Roth65c0f1e2012-08-15 13:45:43 -0500261 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500262 if (token == NULL) {
263 parse_error(ctxt, NULL, "premature EOI");
264 goto out;
265 }
266
Paolo Bonzini9bada892015-11-25 22:23:32 +0100267 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600268 parse_error(ctxt, token, "missing : in object pair");
269 goto out;
270 }
271
Michael Roth65c0f1e2012-08-15 13:45:43 -0500272 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600273 if (value == NULL) {
274 parse_error(ctxt, token, "Missing value in dict");
275 goto out;
276 }
277
Max Reitz532fb532018-03-10 16:14:36 -0600278 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600279
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200280 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600281
282 return 0;
283
284out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200285 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600286
287 return -1;
288}
289
Michael Roth65c0f1e2012-08-15 13:45:43 -0500290static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600291{
292 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100293 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600294
Michael Roth65c0f1e2012-08-15 13:45:43 -0500295 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100296 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600297
298 dict = qdict_new();
299
Michael Roth65c0f1e2012-08-15 13:45:43 -0500300 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500301 if (peek == NULL) {
302 parse_error(ctxt, NULL, "premature EOI");
303 goto out;
304 }
305
Paolo Bonzini9bada892015-11-25 22:23:32 +0100306 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500307 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600308 goto out;
309 }
310
Michael Roth65c0f1e2012-08-15 13:45:43 -0500311 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500312 if (token == NULL) {
313 parse_error(ctxt, NULL, "premature EOI");
314 goto out;
315 }
316
Paolo Bonzini9bada892015-11-25 22:23:32 +0100317 while (token->type != JSON_RCURLY) {
318 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600319 parse_error(ctxt, token, "expected separator in dict");
320 goto out;
321 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600322
Michael Roth65c0f1e2012-08-15 13:45:43 -0500323 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600324 goto out;
325 }
326
Michael Roth65c0f1e2012-08-15 13:45:43 -0500327 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500328 if (token == NULL) {
329 parse_error(ctxt, NULL, "premature EOI");
330 goto out;
331 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600332 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600333 } else {
Gongleia491af42014-06-10 17:20:24 +0800334 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600335 }
336
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600337 return QOBJECT(dict);
338
339out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200340 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600341 return NULL;
342}
343
Michael Roth65c0f1e2012-08-15 13:45:43 -0500344static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600345{
346 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100347 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600348
Michael Roth65c0f1e2012-08-15 13:45:43 -0500349 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100350 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351
352 list = qlist_new();
353
Michael Roth65c0f1e2012-08-15 13:45:43 -0500354 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500355 if (peek == NULL) {
356 parse_error(ctxt, NULL, "premature EOI");
357 goto out;
358 }
359
Paolo Bonzini9bada892015-11-25 22:23:32 +0100360 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600361 QObject *obj;
362
Michael Roth65c0f1e2012-08-15 13:45:43 -0500363 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364 if (obj == NULL) {
365 parse_error(ctxt, token, "expecting value");
366 goto out;
367 }
368
369 qlist_append_obj(list, obj);
370
Michael Roth65c0f1e2012-08-15 13:45:43 -0500371 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500372 if (token == NULL) {
373 parse_error(ctxt, NULL, "premature EOI");
374 goto out;
375 }
376
Paolo Bonzini9bada892015-11-25 22:23:32 +0100377 while (token->type != JSON_RSQUARE) {
378 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600379 parse_error(ctxt, token, "expected separator in list");
380 goto out;
381 }
382
Michael Roth65c0f1e2012-08-15 13:45:43 -0500383 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600384 if (obj == NULL) {
385 parse_error(ctxt, token, "expecting value");
386 goto out;
387 }
388
389 qlist_append_obj(list, obj);
390
Michael Roth65c0f1e2012-08-15 13:45:43 -0500391 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500392 if (token == NULL) {
393 parse_error(ctxt, NULL, "premature EOI");
394 goto out;
395 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600396 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600397 } else {
Gongleia491af42014-06-10 17:20:24 +0800398 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600399 }
400
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600401 return QOBJECT(list);
402
403out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200404 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600405 return NULL;
406}
407
Michael Roth65c0f1e2012-08-15 13:45:43 -0500408static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600409{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100410 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600411
Michael Roth65c0f1e2012-08-15 13:45:43 -0500412 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100413 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100414
Paolo Bonzini9bada892015-11-25 22:23:32 +0100415 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100416 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100417 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100418 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100419 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200420 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600421 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100422 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600423 return NULL;
424}
425
Michael Roth65c0f1e2012-08-15 13:45:43 -0500426static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600427{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100428 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600429
430 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100431 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600432 }
433
Michael Roth65c0f1e2012-08-15 13:45:43 -0500434 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100435 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100436
Paolo Bonzini9bada892015-11-25 22:23:32 +0100437 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100438 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100439 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100440 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100441 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400442 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100443 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400444 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100445 } else if (!strcmp(token->str, "%lld") ||
446 !strcmp(token->str, "%I64d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400447 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400448 } else if (!strcmp(token->str, "%u")) {
449 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
450 } else if (!strcmp(token->str, "%lu")) {
451 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
452 } else if (!strcmp(token->str, "%llu") ||
453 !strcmp(token->str, "%I64u")) {
454 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100455 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100456 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100457 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400458 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600459 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600460 return NULL;
461}
462
Michael Roth65c0f1e2012-08-15 13:45:43 -0500463static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600464{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100465 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600466
Michael Roth65c0f1e2012-08-15 13:45:43 -0500467 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100468 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500469
Paolo Bonzini9bada892015-11-25 22:23:32 +0100470 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600471 case JSON_STRING:
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200472 return QOBJECT(parse_string(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500473 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400474 /*
475 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400476 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
477 * and qemu_strtou64() fail with ERANGE when it's not
478 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500479 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400480 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400481 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
482 * integer, and qnum_get_double() both for any JSON_INTEGER
483 * and any JSON_FLOAT (with precision loss for integers beyond
484 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500485 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400486 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500487 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400488 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500489
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400490 ret = qemu_strtoi64(token->str, NULL, 10, &value);
491 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400492 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500493 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400494 assert(ret == -ERANGE);
495
496 if (token->str[0] != '-') {
497 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
498 if (!ret) {
499 return QOBJECT(qnum_from_uint(uvalue));
500 }
501 assert(ret == -ERANGE);
502 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500503 /* fall through to JSON_FLOAT */
504 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600505 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700506 /* FIXME dependent on locale; a pervasive issue in QEMU */
507 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
508 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400509 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600510 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100511 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600512 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600513}
514
Michael Roth65c0f1e2012-08-15 13:45:43 -0500515static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600516{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100517 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600518
Markus Armbrusterd538b252015-11-25 22:23:30 +0100519 token = parser_context_peek_token(ctxt);
520 if (token == NULL) {
521 parse_error(ctxt, NULL, "premature EOI");
522 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600523 }
524
Paolo Bonzini9bada892015-11-25 22:23:32 +0100525 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100526 case JSON_LCURLY:
527 return parse_object(ctxt, ap);
528 case JSON_LSQUARE:
529 return parse_array(ctxt, ap);
530 case JSON_ESCAPE:
531 return parse_escape(ctxt, ap);
532 case JSON_INTEGER:
533 case JSON_FLOAT:
534 case JSON_STRING:
535 return parse_literal(ctxt);
536 case JSON_KEYWORD:
537 return parse_keyword(ctxt);
538 default:
539 parse_error(ctxt, token, "expecting value");
540 return NULL;
541 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600542}
543
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100544QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600545{
Anthony Liguorief749d02011-06-01 12:14:50 -0500546 return json_parser_parse_err(tokens, ap, NULL);
547}
548
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100549QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500550{
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200551 JSONParserContext ctxt = { .buf = tokens };
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600552 QObject *result;
553
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200554 if (!tokens) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500555 return NULL;
556 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600557
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200558 result = parse_value(&ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600559
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200560 error_propagate(errp, ctxt.err);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500561
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200562 while (!g_queue_is_empty(ctxt.buf)) {
563 parser_context_pop_token(&ctxt);
564 }
565 g_free(ctxt.current);
566 g_queue_free(ctxt.buf);
Anthony Liguorief749d02011-06-01 12:14:50 -0500567
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600568 return result;
569}