blob: 7449684f1c503b68f38d444ea9f8489e20b8cda5 [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"
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
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060034typedef struct JSONParserContext
35{
Anthony Liguorief749d02011-06-01 12:14:50 -050036 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010037 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010038 GQueue *buf;
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
Michael Roth65c0f1e2012-08-15 13:45:43 -050052static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060053
54/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060055 * Error handler
56 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020057static void GCC_FMT_ATTR(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;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600132 QString *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++;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600142 str = qstring_new();
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200143
144 while (*ptr != quote) {
145 assert(*ptr);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600146 if (*ptr == '\\') {
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200147 beg = ptr++;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200148 switch (*ptr++) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600149 case '"':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200150 qstring_append_chr(str, '"');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600151 break;
152 case '\'':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200153 qstring_append_chr(str, '\'');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600154 break;
155 case '\\':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200156 qstring_append_chr(str, '\\');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600157 break;
158 case '/':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200159 qstring_append_chr(str, '/');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600160 break;
161 case 'b':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200162 qstring_append_chr(str, '\b');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600163 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300164 case 'f':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200165 qstring_append_chr(str, '\f');
Luiz Capitulinobd032692010-05-19 17:06:15 -0300166 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600167 case 'n':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200168 qstring_append_chr(str, '\n');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600169 break;
170 case 'r':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200171 qstring_append_chr(str, '\r');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600172 break;
173 case 't':
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200174 qstring_append_chr(str, '\t');
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600175 break;
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200176 case 'u':
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200177 cp = cvt4hex(ptr);
178 ptr += 4;
179
180 /* handle surrogate pairs */
181 if (cp >= 0xD800 && cp <= 0xDBFF
182 && ptr[0] == '\\' && ptr[1] == 'u') {
183 /* leading surrogate followed by \u */
184 cp = 0x10000 + ((cp & 0x3FF) << 10);
185 trailing = cvt4hex(ptr + 2);
186 if (trailing >= 0xDC00 && trailing <= 0xDFFF) {
187 /* followed by trailing surrogate */
188 cp |= trailing & 0x3FF;
189 ptr += 6;
190 } else {
191 cp = -1; /* invalid */
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600192 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600193 }
194
Markus Armbruster46a628b2018-08-23 18:39:55 +0200195 if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) {
196 parse_error(ctxt, token,
Markus Armbrusterdc45a072018-08-23 18:39:56 +0200197 "%.*s is not a valid Unicode character",
198 (int)(ptr - beg), beg);
Markus Armbruster46a628b2018-08-23 18:39:55 +0200199 goto out;
200 }
Markus Armbrusterde6decf2018-08-23 18:39:54 +0200201 qstring_append(str, utf8_buf);
202 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600203 default:
204 parse_error(ctxt, token, "invalid escape sequence in string");
205 goto out;
206 }
207 } else {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200208 cp = mod_utf8_codepoint(ptr, 6, &end);
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +0200209 if (cp < 0) {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200210 parse_error(ctxt, token, "invalid UTF-8 sequence in string");
211 goto out;
212 }
213 ptr = end;
214 len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
215 assert(len >= 0);
216 qstring_append(str, utf8_buf);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600217 }
218 }
219
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600220 return str;
221
222out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200223 qobject_unref(str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600224 return NULL;
225}
226
Paolo Bonzini9bada892015-11-25 22:23:32 +0100227/* Note: the token object returned by parser_context_peek_token or
228 * parser_context_pop_token is deleted as soon as parser_context_pop_token
229 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100230 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100231static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500232{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100233 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100234 ctxt->current = g_queue_pop_head(ctxt->buf);
235 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500236}
237
Paolo Bonzini9bada892015-11-25 22:23:32 +0100238static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500239{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100240 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500241}
242
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600243/**
244 * Parsing rules
245 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500246static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600247{
Max Reitz532fb532018-03-10 16:14:36 -0600248 QObject *value;
249 QString *key = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100250 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600251
Michael Roth65c0f1e2012-08-15 13:45:43 -0500252 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500253 if (peek == NULL) {
254 parse_error(ctxt, NULL, "premature EOI");
255 goto out;
256 }
257
Max Reitz532fb532018-03-10 16:14:36 -0600258 key = qobject_to(QString, parse_value(ctxt, ap));
259 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600260 parse_error(ctxt, peek, "key is not a string in object");
261 goto out;
262 }
263
Michael Roth65c0f1e2012-08-15 13:45:43 -0500264 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500265 if (token == NULL) {
266 parse_error(ctxt, NULL, "premature EOI");
267 goto out;
268 }
269
Paolo Bonzini9bada892015-11-25 22:23:32 +0100270 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600271 parse_error(ctxt, token, "missing : in object pair");
272 goto out;
273 }
274
Michael Roth65c0f1e2012-08-15 13:45:43 -0500275 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600276 if (value == NULL) {
277 parse_error(ctxt, token, "Missing value in dict");
278 goto out;
279 }
280
Max Reitz532fb532018-03-10 16:14:36 -0600281 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600282
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200283 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600284
285 return 0;
286
287out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200288 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600289
290 return -1;
291}
292
Michael Roth65c0f1e2012-08-15 13:45:43 -0500293static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600294{
295 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100296 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600297
Michael Roth65c0f1e2012-08-15 13:45:43 -0500298 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100299 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600300
301 dict = qdict_new();
302
Michael Roth65c0f1e2012-08-15 13:45:43 -0500303 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500304 if (peek == NULL) {
305 parse_error(ctxt, NULL, "premature EOI");
306 goto out;
307 }
308
Paolo Bonzini9bada892015-11-25 22:23:32 +0100309 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500310 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600311 goto out;
312 }
313
Michael Roth65c0f1e2012-08-15 13:45:43 -0500314 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500315 if (token == NULL) {
316 parse_error(ctxt, NULL, "premature EOI");
317 goto out;
318 }
319
Paolo Bonzini9bada892015-11-25 22:23:32 +0100320 while (token->type != JSON_RCURLY) {
321 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600322 parse_error(ctxt, token, "expected separator in dict");
323 goto out;
324 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600325
Michael Roth65c0f1e2012-08-15 13:45:43 -0500326 if (parse_pair(ctxt, dict, ap) == -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 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600335 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600336 } else {
Gongleia491af42014-06-10 17:20:24 +0800337 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600338 }
339
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600340 return QOBJECT(dict);
341
342out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200343 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600344 return NULL;
345}
346
Michael Roth65c0f1e2012-08-15 13:45:43 -0500347static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600348{
349 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100350 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351
Michael Roth65c0f1e2012-08-15 13:45:43 -0500352 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100353 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600354
355 list = qlist_new();
356
Michael Roth65c0f1e2012-08-15 13:45:43 -0500357 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500358 if (peek == NULL) {
359 parse_error(ctxt, NULL, "premature EOI");
360 goto out;
361 }
362
Paolo Bonzini9bada892015-11-25 22:23:32 +0100363 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364 QObject *obj;
365
Michael Roth65c0f1e2012-08-15 13:45:43 -0500366 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600367 if (obj == NULL) {
368 parse_error(ctxt, token, "expecting value");
369 goto out;
370 }
371
372 qlist_append_obj(list, obj);
373
Michael Roth65c0f1e2012-08-15 13:45:43 -0500374 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500375 if (token == NULL) {
376 parse_error(ctxt, NULL, "premature EOI");
377 goto out;
378 }
379
Paolo Bonzini9bada892015-11-25 22:23:32 +0100380 while (token->type != JSON_RSQUARE) {
381 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600382 parse_error(ctxt, token, "expected separator in list");
383 goto out;
384 }
385
Michael Roth65c0f1e2012-08-15 13:45:43 -0500386 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600387 if (obj == NULL) {
388 parse_error(ctxt, token, "expecting value");
389 goto out;
390 }
391
392 qlist_append_obj(list, obj);
393
Michael Roth65c0f1e2012-08-15 13:45:43 -0500394 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500395 if (token == NULL) {
396 parse_error(ctxt, NULL, "premature EOI");
397 goto out;
398 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600399 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600400 } else {
Gongleia491af42014-06-10 17:20:24 +0800401 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600402 }
403
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600404 return QOBJECT(list);
405
406out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200407 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600408 return NULL;
409}
410
Michael Roth65c0f1e2012-08-15 13:45:43 -0500411static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600412{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100413 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600414
Michael Roth65c0f1e2012-08-15 13:45:43 -0500415 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100416 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100417
Paolo Bonzini9bada892015-11-25 22:23:32 +0100418 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100419 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100420 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100421 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100422 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200423 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600424 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100425 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600426 return NULL;
427}
428
Markus Armbruster61030282018-08-23 18:40:04 +0200429static QObject *parse_interpolation(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100431 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600432
Michael Roth65c0f1e2012-08-15 13:45:43 -0500433 token = parser_context_pop_token(ctxt);
Markus Armbruster61030282018-08-23 18:40:04 +0200434 assert(token && token->type == JSON_INTERP);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100435
Paolo Bonzini9bada892015-11-25 22:23:32 +0100436 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100437 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100438 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100439 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100440 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400441 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100442 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400443 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200444 } else if (!strcmp(token->str, "%lld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400445 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200446 } else if (!strcmp(token->str, "%" PRId64)) {
447 return QOBJECT(qnum_from_int(va_arg(*ap, int64_t)));
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)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200452 } else if (!strcmp(token->str, "%llu")) {
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400453 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Markus Armbruster53a0d612018-08-23 18:40:08 +0200454 } else if (!strcmp(token->str, "%" PRIu64)) {
455 return QOBJECT(qnum_from_uint(va_arg(*ap, uint64_t)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100456 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100457 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100458 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400459 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600460 }
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200461 parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600462 return NULL;
463}
464
Michael Roth65c0f1e2012-08-15 13:45:43 -0500465static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600466{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100467 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600468
Michael Roth65c0f1e2012-08-15 13:45:43 -0500469 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100470 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500471
Paolo Bonzini9bada892015-11-25 22:23:32 +0100472 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600473 case JSON_STRING:
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200474 return QOBJECT(parse_string(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500475 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400476 /*
477 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400478 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
479 * and qemu_strtou64() fail with ERANGE when it's not
480 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500481 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400482 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400483 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
484 * integer, and qnum_get_double() both for any JSON_INTEGER
485 * and any JSON_FLOAT (with precision loss for integers beyond
486 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500487 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400488 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500489 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400490 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500491
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400492 ret = qemu_strtoi64(token->str, NULL, 10, &value);
493 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400494 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500495 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400496 assert(ret == -ERANGE);
497
498 if (token->str[0] != '-') {
499 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
500 if (!ret) {
501 return QOBJECT(qnum_from_uint(uvalue));
502 }
503 assert(ret == -ERANGE);
504 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500505 /* fall through to JSON_FLOAT */
506 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600507 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700508 /* FIXME dependent on locale; a pervasive issue in QEMU */
509 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
510 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400511 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600512 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100513 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600514 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600515}
516
Michael Roth65c0f1e2012-08-15 13:45:43 -0500517static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600518{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100519 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600520
Markus Armbrusterd538b252015-11-25 22:23:30 +0100521 token = parser_context_peek_token(ctxt);
522 if (token == NULL) {
523 parse_error(ctxt, NULL, "premature EOI");
524 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600525 }
526
Paolo Bonzini9bada892015-11-25 22:23:32 +0100527 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100528 case JSON_LCURLY:
529 return parse_object(ctxt, ap);
530 case JSON_LSQUARE:
531 return parse_array(ctxt, ap);
Markus Armbruster61030282018-08-23 18:40:04 +0200532 case JSON_INTERP:
533 return parse_interpolation(ctxt, ap);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100534 case JSON_INTEGER:
535 case JSON_FLOAT:
536 case JSON_STRING:
537 return parse_literal(ctxt);
538 case JSON_KEYWORD:
539 return parse_keyword(ctxt);
540 default:
541 parse_error(ctxt, token, "expecting value");
542 return NULL;
543 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600544}
545
Markus Armbrusterabe7c202018-08-23 18:40:18 +0200546JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr)
547{
548 JSONToken *token = g_malloc(sizeof(JSONToken) + tokstr->len + 1);
549
550 token->type = type;
551 memcpy(token->str, tokstr->str, tokstr->len);
552 token->str[tokstr->len] = 0;
553 token->x = x;
554 token->y = y;
555 return token;
556}
557
Markus Armbruster62815d82018-08-23 18:40:01 +0200558QObject *json_parser_parse(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500559{
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200560 JSONParserContext ctxt = { .buf = tokens };
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600561 QObject *result;
562
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200563 result = parse_value(&ctxt, ap);
Markus Armbruster5d501132018-08-23 18:40:13 +0200564 assert(ctxt.err || g_queue_is_empty(ctxt.buf));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600565
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200566 error_propagate(errp, ctxt.err);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500567
Marc-André Lureaue8b19d72018-08-23 18:39:59 +0200568 while (!g_queue_is_empty(ctxt.buf)) {
569 parser_context_pop_token(&ctxt);
570 }
571 g_free(ctxt.current);
Anthony Liguorief749d02011-06-01 12:14:50 -0500572
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600573 return result;
574}