4 * Copyright IBM, Corp. 2009
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu-common.h"
17 #include "qapi/qmp/types.h"
18 #include "qapi/qmp/json-parser.h"
19 #include "qapi/qmp/json-lexer.h"
20 #include "qapi/qmp/json-streamer.h"
22 typedef struct JSONParserContext
29 #define BUG_ON(cond) assert(!(cond))
34 * 0) make errors meaningful again
35 * 1) add geometry information to tokens
36 * 3) should we return a parsed size?
37 * 4) deal with premature EOI
40 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
45 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
46 JSONToken
*token
, const char *msg
, ...)
51 vsnprintf(message
, sizeof(message
), msg
, ap
);
54 error_free(ctxt
->err
);
57 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
63 * These helpers are used to unescape strings.
65 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
67 if (wchar
<= 0x007F) {
68 BUG_ON(buffer_length
< 2);
70 buffer
[0] = wchar
& 0x7F;
72 } else if (wchar
<= 0x07FF) {
73 BUG_ON(buffer_length
< 3);
75 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
76 buffer
[1] = 0x80 | (wchar
& 0x3F);
79 BUG_ON(buffer_length
< 4);
81 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
82 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
83 buffer
[2] = 0x80 | (wchar
& 0x3F);
88 static int hex2decimal(char ch
)
90 if (ch
>= '0' && ch
<= '9') {
92 } else if (ch
>= 'a' && ch
<= 'f') {
93 return 10 + (ch
- 'a');
94 } else if (ch
>= 'A' && ch
<= 'F') {
95 return 10 + (ch
- 'A');
102 * parse_string(): Parse a json string and return a QObject
111 * any-Unicode-character-
124 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
,
127 const char *ptr
= token
->str
;
129 int double_quote
= 1;
140 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
146 qstring_append(str
, "\"");
150 qstring_append(str
, "'");
154 qstring_append(str
, "\\");
158 qstring_append(str
, "/");
162 qstring_append(str
, "\b");
166 qstring_append(str
, "\f");
170 qstring_append(str
, "\n");
174 qstring_append(str
, "\r");
178 qstring_append(str
, "\t");
182 uint16_t unicode_char
= 0;
188 for (i
= 0; i
< 4; i
++) {
189 if (qemu_isxdigit(*ptr
)) {
190 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
192 parse_error(ctxt
, token
,
193 "invalid hex escape sequence in string");
199 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
200 qstring_append(str
, utf8_char
);
203 parse_error(ctxt
, token
, "invalid escape sequence in string");
212 qstring_append(str
, dummy
);
223 /* Note: the token object returned by parser_context_peek_token or
224 * parser_context_pop_token is deleted as soon as parser_context_pop_token
227 static JSONToken
*parser_context_pop_token(JSONParserContext
*ctxt
)
229 g_free(ctxt
->current
);
230 assert(!g_queue_is_empty(ctxt
->buf
));
231 ctxt
->current
= g_queue_pop_head(ctxt
->buf
);
232 return ctxt
->current
;
235 static JSONToken
*parser_context_peek_token(JSONParserContext
*ctxt
)
237 assert(!g_queue_is_empty(ctxt
->buf
));
238 return g_queue_peek_head(ctxt
->buf
);
241 static JSONParserContext
*parser_context_new(GQueue
*tokens
)
243 JSONParserContext
*ctxt
;
249 ctxt
= g_malloc0(sizeof(JSONParserContext
));
255 /* to support error propagation, ctxt->err must be freed separately */
256 static void parser_context_free(JSONParserContext
*ctxt
)
259 while (!g_queue_is_empty(ctxt
->buf
)) {
260 parser_context_pop_token(ctxt
);
262 g_free(ctxt
->current
);
263 g_queue_free(ctxt
->buf
);
271 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
273 QObject
*key
= NULL
, *value
;
274 JSONToken
*peek
, *token
;
276 peek
= parser_context_peek_token(ctxt
);
278 parse_error(ctxt
, NULL
, "premature EOI");
282 key
= parse_value(ctxt
, ap
);
283 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
284 parse_error(ctxt
, peek
, "key is not a string in object");
288 token
= parser_context_pop_token(ctxt
);
290 parse_error(ctxt
, NULL
, "premature EOI");
294 if (token
->type
!= JSON_COLON
) {
295 parse_error(ctxt
, token
, "missing : in object pair");
299 value
= parse_value(ctxt
, ap
);
301 parse_error(ctxt
, token
, "Missing value in dict");
305 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
317 static QObject
*parse_object(JSONParserContext
*ctxt
, va_list *ap
)
320 JSONToken
*token
, *peek
;
322 token
= parser_context_pop_token(ctxt
);
323 assert(token
&& token
->type
== JSON_LCURLY
);
327 peek
= parser_context_peek_token(ctxt
);
329 parse_error(ctxt
, NULL
, "premature EOI");
333 if (peek
->type
!= JSON_RCURLY
) {
334 if (parse_pair(ctxt
, dict
, ap
) == -1) {
338 token
= parser_context_pop_token(ctxt
);
340 parse_error(ctxt
, NULL
, "premature EOI");
344 while (token
->type
!= JSON_RCURLY
) {
345 if (token
->type
!= JSON_COMMA
) {
346 parse_error(ctxt
, token
, "expected separator in dict");
350 if (parse_pair(ctxt
, dict
, ap
) == -1) {
354 token
= parser_context_pop_token(ctxt
);
356 parse_error(ctxt
, NULL
, "premature EOI");
361 (void)parser_context_pop_token(ctxt
);
364 return QOBJECT(dict
);
371 static QObject
*parse_array(JSONParserContext
*ctxt
, va_list *ap
)
374 JSONToken
*token
, *peek
;
376 token
= parser_context_pop_token(ctxt
);
377 assert(token
&& token
->type
== JSON_LSQUARE
);
381 peek
= parser_context_peek_token(ctxt
);
383 parse_error(ctxt
, NULL
, "premature EOI");
387 if (peek
->type
!= JSON_RSQUARE
) {
390 obj
= parse_value(ctxt
, ap
);
392 parse_error(ctxt
, token
, "expecting value");
396 qlist_append_obj(list
, obj
);
398 token
= parser_context_pop_token(ctxt
);
400 parse_error(ctxt
, NULL
, "premature EOI");
404 while (token
->type
!= JSON_RSQUARE
) {
405 if (token
->type
!= JSON_COMMA
) {
406 parse_error(ctxt
, token
, "expected separator in list");
410 obj
= parse_value(ctxt
, ap
);
412 parse_error(ctxt
, token
, "expecting value");
416 qlist_append_obj(list
, obj
);
418 token
= parser_context_pop_token(ctxt
);
420 parse_error(ctxt
, NULL
, "premature EOI");
425 (void)parser_context_pop_token(ctxt
);
428 return QOBJECT(list
);
435 static QObject
*parse_keyword(JSONParserContext
*ctxt
)
439 token
= parser_context_pop_token(ctxt
);
440 assert(token
&& token
->type
== JSON_KEYWORD
);
442 if (!strcmp(token
->str
, "true")) {
443 return QOBJECT(qbool_from_bool(true));
444 } else if (!strcmp(token
->str
, "false")) {
445 return QOBJECT(qbool_from_bool(false));
446 } else if (!strcmp(token
->str
, "null")) {
449 parse_error(ctxt
, token
, "invalid keyword '%s'", token
->str
);
453 static QObject
*parse_escape(JSONParserContext
*ctxt
, va_list *ap
)
461 token
= parser_context_pop_token(ctxt
);
462 assert(token
&& token
->type
== JSON_ESCAPE
);
464 if (!strcmp(token
->str
, "%p")) {
465 return va_arg(*ap
, QObject
*);
466 } else if (!strcmp(token
->str
, "%i")) {
467 return QOBJECT(qbool_from_bool(va_arg(*ap
, int)));
468 } else if (!strcmp(token
->str
, "%d")) {
469 return QOBJECT(qint_from_int(va_arg(*ap
, int)));
470 } else if (!strcmp(token
->str
, "%ld")) {
471 return QOBJECT(qint_from_int(va_arg(*ap
, long)));
472 } else if (!strcmp(token
->str
, "%lld") ||
473 !strcmp(token
->str
, "%I64d")) {
474 return QOBJECT(qint_from_int(va_arg(*ap
, long long)));
475 } else if (!strcmp(token
->str
, "%s")) {
476 return QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
477 } else if (!strcmp(token
->str
, "%f")) {
478 return QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
483 static QObject
*parse_literal(JSONParserContext
*ctxt
)
487 token
= parser_context_pop_token(ctxt
);
490 switch (token
->type
) {
492 return QOBJECT(qstring_from_escaped_str(ctxt
, token
));
494 /* A possibility exists that this is a whole-valued float where the
495 * fractional part was left out due to being 0 (.0). It's not a big
496 * deal to treat these as ints in the parser, so long as users of the
497 * resulting QObject know to expect a QInt in place of a QFloat in
500 * However, in some cases these values will overflow/underflow a
501 * QInt/int64 container, thus we should assume these are to be handled
502 * as QFloats/doubles rather than silently changing their values.
504 * strtoll() indicates these instances by setting errno to ERANGE
508 errno
= 0; /* strtoll doesn't set errno on success */
509 value
= strtoll(token
->str
, NULL
, 10);
510 if (errno
!= ERANGE
) {
511 return QOBJECT(qint_from_int(value
));
513 /* fall through to JSON_FLOAT */
516 /* FIXME dependent on locale; a pervasive issue in QEMU */
517 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
518 * but those might be useful extensions beyond JSON */
519 return QOBJECT(qfloat_from_double(strtod(token
->str
, NULL
)));
525 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
529 token
= parser_context_peek_token(ctxt
);
531 parse_error(ctxt
, NULL
, "premature EOI");
535 switch (token
->type
) {
537 return parse_object(ctxt
, ap
);
539 return parse_array(ctxt
, ap
);
541 return parse_escape(ctxt
, ap
);
545 return parse_literal(ctxt
);
547 return parse_keyword(ctxt
);
549 parse_error(ctxt
, token
, "expecting value");
554 QObject
*json_parser_parse(GQueue
*tokens
, va_list *ap
)
556 return json_parser_parse_err(tokens
, ap
, NULL
);
559 QObject
*json_parser_parse_err(GQueue
*tokens
, va_list *ap
, Error
**errp
)
561 JSONParserContext
*ctxt
= parser_context_new(tokens
);
568 result
= parse_value(ctxt
, ap
);
570 error_propagate(errp
, ctxt
->err
);
572 parser_context_free(ctxt
);