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.
16 #include "qemu-common.h"
17 #include "qapi/qmp/qstring.h"
18 #include "qapi/qmp/qint.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qfloat.h"
22 #include "qapi/qmp/qbool.h"
23 #include "qapi/qmp/json-parser.h"
24 #include "qapi/qmp/json-lexer.h"
25 #include "qapi/qmp/qerror.h"
27 typedef struct JSONParserContext
37 #define BUG_ON(cond) assert(!(cond))
42 * 0) make errors meaningful again
43 * 1) add geometry information to tokens
44 * 3) should we return a parsed size?
45 * 4) deal with premature EOI
48 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
53 * tokens are dictionaries that contain a type, a string value, and geometry information
54 * about a token identified by the lexer. These are routines that make working with
55 * these objects a bit easier.
57 static const char *token_get_value(QObject
*obj
)
59 return qdict_get_str(qobject_to_qdict(obj
), "token");
62 static JSONTokenType
token_get_type(QObject
*obj
)
64 return qdict_get_int(qobject_to_qdict(obj
), "type");
67 static int token_is_operator(QObject
*obj
, char op
)
71 if (token_get_type(obj
) != JSON_OPERATOR
) {
75 val
= token_get_value(obj
);
77 return (val
[0] == op
) && (val
[1] == 0);
80 static int token_is_keyword(QObject
*obj
, const char *value
)
82 if (token_get_type(obj
) != JSON_KEYWORD
) {
86 return strcmp(token_get_value(obj
), value
) == 0;
89 static int token_is_escape(QObject
*obj
, const char *value
)
91 if (token_get_type(obj
) != JSON_ESCAPE
) {
95 return (strcmp(token_get_value(obj
), value
) == 0);
101 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
102 QObject
*token
, const char *msg
, ...)
107 vsnprintf(message
, sizeof(message
), msg
, ap
);
110 error_free(ctxt
->err
);
113 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
119 * These helpers are used to unescape strings.
121 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
123 if (wchar
<= 0x007F) {
124 BUG_ON(buffer_length
< 2);
126 buffer
[0] = wchar
& 0x7F;
128 } else if (wchar
<= 0x07FF) {
129 BUG_ON(buffer_length
< 3);
131 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
132 buffer
[1] = 0x80 | (wchar
& 0x3F);
135 BUG_ON(buffer_length
< 4);
137 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
138 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
139 buffer
[2] = 0x80 | (wchar
& 0x3F);
144 static int hex2decimal(char ch
)
146 if (ch
>= '0' && ch
<= '9') {
148 } else if (ch
>= 'a' && ch
<= 'f') {
149 return 10 + (ch
- 'a');
150 } else if (ch
>= 'A' && ch
<= 'F') {
151 return 10 + (ch
- 'A');
158 * parse_string(): Parse a json string and return a QObject
167 * any-Unicode-character-
180 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
, QObject
*token
)
182 const char *ptr
= token_get_value(token
);
184 int double_quote
= 1;
195 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
201 qstring_append(str
, "\"");
205 qstring_append(str
, "'");
209 qstring_append(str
, "\\");
213 qstring_append(str
, "/");
217 qstring_append(str
, "\b");
221 qstring_append(str
, "\f");
225 qstring_append(str
, "\n");
229 qstring_append(str
, "\r");
233 qstring_append(str
, "\t");
237 uint16_t unicode_char
= 0;
243 for (i
= 0; i
< 4; i
++) {
244 if (qemu_isxdigit(*ptr
)) {
245 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
247 parse_error(ctxt
, token
,
248 "invalid hex escape sequence in string");
254 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
255 qstring_append(str
, utf8_char
);
258 parse_error(ctxt
, token
, "invalid escape sequence in string");
267 qstring_append(str
, dummy
);
278 static QObject
*parser_context_pop_token(JSONParserContext
*ctxt
)
281 g_assert(ctxt
->tokens
.pos
< ctxt
->tokens
.count
);
282 token
= ctxt
->tokens
.buf
[ctxt
->tokens
.pos
];
287 /* Note: parser_context_{peek|pop}_token do not increment the
288 * token object's refcount. In both cases the references will continue
289 * to be tracked and cleaned up in parser_context_free(), so do not
290 * attempt to free the token object.
292 static QObject
*parser_context_peek_token(JSONParserContext
*ctxt
)
295 g_assert(ctxt
->tokens
.pos
< ctxt
->tokens
.count
);
296 token
= ctxt
->tokens
.buf
[ctxt
->tokens
.pos
];
300 static JSONParserContext
parser_context_save(JSONParserContext
*ctxt
)
302 JSONParserContext saved_ctxt
= {0};
303 saved_ctxt
.tokens
.pos
= ctxt
->tokens
.pos
;
304 saved_ctxt
.tokens
.count
= ctxt
->tokens
.count
;
305 saved_ctxt
.tokens
.buf
= ctxt
->tokens
.buf
;
309 static void parser_context_restore(JSONParserContext
*ctxt
,
310 JSONParserContext saved_ctxt
)
312 ctxt
->tokens
.pos
= saved_ctxt
.tokens
.pos
;
313 ctxt
->tokens
.count
= saved_ctxt
.tokens
.count
;
314 ctxt
->tokens
.buf
= saved_ctxt
.tokens
.buf
;
317 static void tokens_append_from_iter(QObject
*obj
, void *opaque
)
319 JSONParserContext
*ctxt
= opaque
;
320 g_assert(ctxt
->tokens
.pos
< ctxt
->tokens
.count
);
321 ctxt
->tokens
.buf
[ctxt
->tokens
.pos
++] = obj
;
325 static JSONParserContext
*parser_context_new(QList
*tokens
)
327 JSONParserContext
*ctxt
;
334 count
= qlist_size(tokens
);
339 ctxt
= g_malloc0(sizeof(JSONParserContext
));
340 ctxt
->tokens
.pos
= 0;
341 ctxt
->tokens
.count
= count
;
342 ctxt
->tokens
.buf
= g_malloc(count
* sizeof(QObject
*));
343 qlist_iter(tokens
, tokens_append_from_iter
, ctxt
);
344 ctxt
->tokens
.pos
= 0;
349 /* to support error propagation, ctxt->err must be freed separately */
350 static void parser_context_free(JSONParserContext
*ctxt
)
354 for (i
= 0; i
< ctxt
->tokens
.count
; i
++) {
355 qobject_decref(ctxt
->tokens
.buf
[i
]);
357 g_free(ctxt
->tokens
.buf
);
365 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
367 QObject
*key
= NULL
, *token
= NULL
, *value
, *peek
;
368 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
370 peek
= parser_context_peek_token(ctxt
);
372 parse_error(ctxt
, NULL
, "premature EOI");
376 key
= parse_value(ctxt
, ap
);
377 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
378 parse_error(ctxt
, peek
, "key is not a string in object");
382 token
= parser_context_pop_token(ctxt
);
384 parse_error(ctxt
, NULL
, "premature EOI");
388 if (!token_is_operator(token
, ':')) {
389 parse_error(ctxt
, token
, "missing : in object pair");
393 value
= parse_value(ctxt
, ap
);
395 parse_error(ctxt
, token
, "Missing value in dict");
399 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
406 parser_context_restore(ctxt
, saved_ctxt
);
412 static QObject
*parse_object(JSONParserContext
*ctxt
, va_list *ap
)
415 QObject
*token
, *peek
;
416 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
418 token
= parser_context_pop_token(ctxt
);
423 if (!token_is_operator(token
, '{')) {
429 peek
= parser_context_peek_token(ctxt
);
431 parse_error(ctxt
, NULL
, "premature EOI");
435 if (!token_is_operator(peek
, '}')) {
436 if (parse_pair(ctxt
, dict
, ap
) == -1) {
440 token
= parser_context_pop_token(ctxt
);
442 parse_error(ctxt
, NULL
, "premature EOI");
446 while (!token_is_operator(token
, '}')) {
447 if (!token_is_operator(token
, ',')) {
448 parse_error(ctxt
, token
, "expected separator in dict");
452 if (parse_pair(ctxt
, dict
, ap
) == -1) {
456 token
= parser_context_pop_token(ctxt
);
458 parse_error(ctxt
, NULL
, "premature EOI");
463 (void)parser_context_pop_token(ctxt
);
466 return QOBJECT(dict
);
469 parser_context_restore(ctxt
, saved_ctxt
);
474 static QObject
*parse_array(JSONParserContext
*ctxt
, va_list *ap
)
477 QObject
*token
, *peek
;
478 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
480 token
= parser_context_pop_token(ctxt
);
485 if (!token_is_operator(token
, '[')) {
491 peek
= parser_context_peek_token(ctxt
);
493 parse_error(ctxt
, NULL
, "premature EOI");
497 if (!token_is_operator(peek
, ']')) {
500 obj
= parse_value(ctxt
, ap
);
502 parse_error(ctxt
, token
, "expecting value");
506 qlist_append_obj(list
, obj
);
508 token
= parser_context_pop_token(ctxt
);
510 parse_error(ctxt
, NULL
, "premature EOI");
514 while (!token_is_operator(token
, ']')) {
515 if (!token_is_operator(token
, ',')) {
516 parse_error(ctxt
, token
, "expected separator in list");
520 obj
= parse_value(ctxt
, ap
);
522 parse_error(ctxt
, token
, "expecting value");
526 qlist_append_obj(list
, obj
);
528 token
= parser_context_pop_token(ctxt
);
530 parse_error(ctxt
, NULL
, "premature EOI");
535 (void)parser_context_pop_token(ctxt
);
538 return QOBJECT(list
);
541 parser_context_restore(ctxt
, saved_ctxt
);
546 static QObject
*parse_keyword(JSONParserContext
*ctxt
)
548 QObject
*token
, *ret
;
549 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
551 token
= parser_context_pop_token(ctxt
);
556 if (token_get_type(token
) != JSON_KEYWORD
) {
560 if (token_is_keyword(token
, "true")) {
561 ret
= QOBJECT(qbool_from_int(true));
562 } else if (token_is_keyword(token
, "false")) {
563 ret
= QOBJECT(qbool_from_int(false));
564 } else if (token_is_keyword(token
, "null")) {
567 parse_error(ctxt
, token
, "invalid keyword `%s'", token_get_value(token
));
574 parser_context_restore(ctxt
, saved_ctxt
);
579 static QObject
*parse_escape(JSONParserContext
*ctxt
, va_list *ap
)
581 QObject
*token
= NULL
, *obj
;
582 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
588 token
= parser_context_pop_token(ctxt
);
593 if (token_is_escape(token
, "%p")) {
594 obj
= va_arg(*ap
, QObject
*);
595 } else if (token_is_escape(token
, "%i")) {
596 obj
= QOBJECT(qbool_from_int(va_arg(*ap
, int)));
597 } else if (token_is_escape(token
, "%d")) {
598 obj
= QOBJECT(qint_from_int(va_arg(*ap
, int)));
599 } else if (token_is_escape(token
, "%ld")) {
600 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long)));
601 } else if (token_is_escape(token
, "%lld") ||
602 token_is_escape(token
, "%I64d")) {
603 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long long)));
604 } else if (token_is_escape(token
, "%s")) {
605 obj
= QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
606 } else if (token_is_escape(token
, "%f")) {
607 obj
= QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
615 parser_context_restore(ctxt
, saved_ctxt
);
620 static QObject
*parse_literal(JSONParserContext
*ctxt
)
622 QObject
*token
, *obj
;
623 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
625 token
= parser_context_pop_token(ctxt
);
630 switch (token_get_type(token
)) {
632 obj
= QOBJECT(qstring_from_escaped_str(ctxt
, token
));
635 /* A possibility exists that this is a whole-valued float where the
636 * fractional part was left out due to being 0 (.0). It's not a big
637 * deal to treat these as ints in the parser, so long as users of the
638 * resulting QObject know to expect a QInt in place of a QFloat in
641 * However, in some cases these values will overflow/underflow a
642 * QInt/int64 container, thus we should assume these are to be handled
643 * as QFloats/doubles rather than silently changing their values.
645 * strtoll() indicates these instances by setting errno to ERANGE
649 errno
= 0; /* strtoll doesn't set errno on success */
650 value
= strtoll(token_get_value(token
), NULL
, 10);
651 if (errno
!= ERANGE
) {
652 obj
= QOBJECT(qint_from_int(value
));
655 /* fall through to JSON_FLOAT */
658 /* FIXME dependent on locale */
659 obj
= QOBJECT(qfloat_from_double(strtod(token_get_value(token
), NULL
)));
668 parser_context_restore(ctxt
, saved_ctxt
);
673 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
677 obj
= parse_object(ctxt
, ap
);
679 obj
= parse_array(ctxt
, ap
);
682 obj
= parse_escape(ctxt
, ap
);
685 obj
= parse_keyword(ctxt
);
688 obj
= parse_literal(ctxt
);
694 QObject
*json_parser_parse(QList
*tokens
, va_list *ap
)
696 return json_parser_parse_err(tokens
, ap
, NULL
);
699 QObject
*json_parser_parse_err(QList
*tokens
, va_list *ap
, Error
**errp
)
701 JSONParserContext
*ctxt
= parser_context_new(tokens
);
708 result
= parse_value(ctxt
, ap
);
710 error_propagate(errp
, ctxt
->err
);
712 parser_context_free(ctxt
);