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"
26 typedef struct JSONParserContext
36 #define BUG_ON(cond) assert(!(cond))
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
47 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
52 * tokens are dictionaries that contain a type, a string value, and geometry information
53 * about a token identified by the lexer. These are routines that make working with
54 * these objects a bit easier.
56 static const char *token_get_value(QObject
*obj
)
58 return qdict_get_str(qobject_to_qdict(obj
), "token");
61 static JSONTokenType
token_get_type(QObject
*obj
)
63 return qdict_get_int(qobject_to_qdict(obj
), "type");
66 static int token_is_operator(QObject
*obj
, char op
)
70 if (token_get_type(obj
) != JSON_OPERATOR
) {
74 val
= token_get_value(obj
);
76 return (val
[0] == op
) && (val
[1] == 0);
79 static int token_is_keyword(QObject
*obj
, const char *value
)
81 if (token_get_type(obj
) != JSON_KEYWORD
) {
85 return strcmp(token_get_value(obj
), value
) == 0;
88 static int token_is_escape(QObject
*obj
, const char *value
)
90 if (token_get_type(obj
) != JSON_ESCAPE
) {
94 return (strcmp(token_get_value(obj
), value
) == 0);
100 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
101 QObject
*token
, const char *msg
, ...)
106 vsnprintf(message
, sizeof(message
), msg
, ap
);
109 error_free(ctxt
->err
);
112 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
118 * These helpers are used to unescape strings.
120 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
122 if (wchar
<= 0x007F) {
123 BUG_ON(buffer_length
< 2);
125 buffer
[0] = wchar
& 0x7F;
127 } else if (wchar
<= 0x07FF) {
128 BUG_ON(buffer_length
< 3);
130 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
131 buffer
[1] = 0x80 | (wchar
& 0x3F);
134 BUG_ON(buffer_length
< 4);
136 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
137 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
138 buffer
[2] = 0x80 | (wchar
& 0x3F);
143 static int hex2decimal(char ch
)
145 if (ch
>= '0' && ch
<= '9') {
147 } else if (ch
>= 'a' && ch
<= 'f') {
148 return 10 + (ch
- 'a');
149 } else if (ch
>= 'A' && ch
<= 'F') {
150 return 10 + (ch
- 'A');
157 * parse_string(): Parse a json string and return a QObject
166 * any-Unicode-character-
179 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
, QObject
*token
)
181 const char *ptr
= token_get_value(token
);
183 int double_quote
= 1;
194 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
200 qstring_append(str
, "\"");
204 qstring_append(str
, "'");
208 qstring_append(str
, "\\");
212 qstring_append(str
, "/");
216 qstring_append(str
, "\b");
220 qstring_append(str
, "\f");
224 qstring_append(str
, "\n");
228 qstring_append(str
, "\r");
232 qstring_append(str
, "\t");
236 uint16_t unicode_char
= 0;
242 for (i
= 0; i
< 4; i
++) {
243 if (qemu_isxdigit(*ptr
)) {
244 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
246 parse_error(ctxt
, token
,
247 "invalid hex escape sequence in string");
253 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
254 qstring_append(str
, utf8_char
);
257 parse_error(ctxt
, token
, "invalid escape sequence in string");
266 qstring_append(str
, dummy
);
277 static QObject
*parser_context_pop_token(JSONParserContext
*ctxt
)
280 g_assert(ctxt
->tokens
.pos
< ctxt
->tokens
.count
);
281 token
= ctxt
->tokens
.buf
[ctxt
->tokens
.pos
];
286 /* Note: parser_context_{peek|pop}_token do not increment the
287 * token object's refcount. In both cases the references will continue
288 * to be tracked and cleaned up in parser_context_free(), so do not
289 * attempt to free the token object.
291 static QObject
*parser_context_peek_token(JSONParserContext
*ctxt
)
294 g_assert(ctxt
->tokens
.pos
< ctxt
->tokens
.count
);
295 token
= ctxt
->tokens
.buf
[ctxt
->tokens
.pos
];
299 static JSONParserContext
parser_context_save(JSONParserContext
*ctxt
)
301 JSONParserContext saved_ctxt
= {0};
302 saved_ctxt
.tokens
.pos
= ctxt
->tokens
.pos
;
303 saved_ctxt
.tokens
.count
= ctxt
->tokens
.count
;
304 saved_ctxt
.tokens
.buf
= ctxt
->tokens
.buf
;
308 static void parser_context_restore(JSONParserContext
*ctxt
,
309 JSONParserContext saved_ctxt
)
311 ctxt
->tokens
.pos
= saved_ctxt
.tokens
.pos
;
312 ctxt
->tokens
.count
= saved_ctxt
.tokens
.count
;
313 ctxt
->tokens
.buf
= saved_ctxt
.tokens
.buf
;
316 static void tokens_append_from_iter(QObject
*obj
, void *opaque
)
318 JSONParserContext
*ctxt
= opaque
;
319 g_assert(ctxt
->tokens
.pos
< ctxt
->tokens
.count
);
320 ctxt
->tokens
.buf
[ctxt
->tokens
.pos
++] = obj
;
324 static JSONParserContext
*parser_context_new(QList
*tokens
)
326 JSONParserContext
*ctxt
;
333 count
= qlist_size(tokens
);
338 ctxt
= g_malloc0(sizeof(JSONParserContext
));
339 ctxt
->tokens
.pos
= 0;
340 ctxt
->tokens
.count
= count
;
341 ctxt
->tokens
.buf
= g_malloc(count
* sizeof(QObject
*));
342 qlist_iter(tokens
, tokens_append_from_iter
, ctxt
);
343 ctxt
->tokens
.pos
= 0;
348 /* to support error propagation, ctxt->err must be freed separately */
349 static void parser_context_free(JSONParserContext
*ctxt
)
353 for (i
= 0; i
< ctxt
->tokens
.count
; i
++) {
354 qobject_decref(ctxt
->tokens
.buf
[i
]);
356 g_free(ctxt
->tokens
.buf
);
364 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
366 QObject
*key
= NULL
, *token
= NULL
, *value
, *peek
;
367 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
369 peek
= parser_context_peek_token(ctxt
);
371 parse_error(ctxt
, NULL
, "premature EOI");
375 key
= parse_value(ctxt
, ap
);
376 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
377 parse_error(ctxt
, peek
, "key is not a string in object");
381 token
= parser_context_pop_token(ctxt
);
383 parse_error(ctxt
, NULL
, "premature EOI");
387 if (!token_is_operator(token
, ':')) {
388 parse_error(ctxt
, token
, "missing : in object pair");
392 value
= parse_value(ctxt
, ap
);
394 parse_error(ctxt
, token
, "Missing value in dict");
398 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
405 parser_context_restore(ctxt
, saved_ctxt
);
411 static QObject
*parse_object(JSONParserContext
*ctxt
, va_list *ap
)
414 QObject
*token
, *peek
;
415 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
417 token
= parser_context_pop_token(ctxt
);
422 if (!token_is_operator(token
, '{')) {
428 peek
= parser_context_peek_token(ctxt
);
430 parse_error(ctxt
, NULL
, "premature EOI");
434 if (!token_is_operator(peek
, '}')) {
435 if (parse_pair(ctxt
, dict
, ap
) == -1) {
439 token
= parser_context_pop_token(ctxt
);
441 parse_error(ctxt
, NULL
, "premature EOI");
445 while (!token_is_operator(token
, '}')) {
446 if (!token_is_operator(token
, ',')) {
447 parse_error(ctxt
, token
, "expected separator in dict");
451 if (parse_pair(ctxt
, dict
, ap
) == -1) {
455 token
= parser_context_pop_token(ctxt
);
457 parse_error(ctxt
, NULL
, "premature EOI");
462 (void)parser_context_pop_token(ctxt
);
465 return QOBJECT(dict
);
468 parser_context_restore(ctxt
, saved_ctxt
);
473 static QObject
*parse_array(JSONParserContext
*ctxt
, va_list *ap
)
476 QObject
*token
, *peek
;
477 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
479 token
= parser_context_pop_token(ctxt
);
484 if (!token_is_operator(token
, '[')) {
490 peek
= parser_context_peek_token(ctxt
);
492 parse_error(ctxt
, NULL
, "premature EOI");
496 if (!token_is_operator(peek
, ']')) {
499 obj
= parse_value(ctxt
, ap
);
501 parse_error(ctxt
, token
, "expecting value");
505 qlist_append_obj(list
, obj
);
507 token
= parser_context_pop_token(ctxt
);
509 parse_error(ctxt
, NULL
, "premature EOI");
513 while (!token_is_operator(token
, ']')) {
514 if (!token_is_operator(token
, ',')) {
515 parse_error(ctxt
, token
, "expected separator in list");
519 obj
= parse_value(ctxt
, ap
);
521 parse_error(ctxt
, token
, "expecting value");
525 qlist_append_obj(list
, obj
);
527 token
= parser_context_pop_token(ctxt
);
529 parse_error(ctxt
, NULL
, "premature EOI");
534 (void)parser_context_pop_token(ctxt
);
537 return QOBJECT(list
);
540 parser_context_restore(ctxt
, saved_ctxt
);
545 static QObject
*parse_keyword(JSONParserContext
*ctxt
)
547 QObject
*token
, *ret
;
548 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
550 token
= parser_context_pop_token(ctxt
);
555 if (token_get_type(token
) != JSON_KEYWORD
) {
559 if (token_is_keyword(token
, "true")) {
560 ret
= QOBJECT(qbool_from_bool(true));
561 } else if (token_is_keyword(token
, "false")) {
562 ret
= QOBJECT(qbool_from_bool(false));
563 } else if (token_is_keyword(token
, "null")) {
566 parse_error(ctxt
, token
, "invalid keyword `%s'", token_get_value(token
));
573 parser_context_restore(ctxt
, saved_ctxt
);
578 static QObject
*parse_escape(JSONParserContext
*ctxt
, va_list *ap
)
580 QObject
*token
= NULL
, *obj
;
581 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
587 token
= parser_context_pop_token(ctxt
);
592 if (token_is_escape(token
, "%p")) {
593 obj
= va_arg(*ap
, QObject
*);
594 } else if (token_is_escape(token
, "%i")) {
595 obj
= QOBJECT(qbool_from_bool(va_arg(*ap
, int)));
596 } else if (token_is_escape(token
, "%d")) {
597 obj
= QOBJECT(qint_from_int(va_arg(*ap
, int)));
598 } else if (token_is_escape(token
, "%ld")) {
599 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long)));
600 } else if (token_is_escape(token
, "%lld") ||
601 token_is_escape(token
, "%I64d")) {
602 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long long)));
603 } else if (token_is_escape(token
, "%s")) {
604 obj
= QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
605 } else if (token_is_escape(token
, "%f")) {
606 obj
= QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
614 parser_context_restore(ctxt
, saved_ctxt
);
619 static QObject
*parse_literal(JSONParserContext
*ctxt
)
621 QObject
*token
, *obj
;
622 JSONParserContext saved_ctxt
= parser_context_save(ctxt
);
624 token
= parser_context_pop_token(ctxt
);
629 switch (token_get_type(token
)) {
631 obj
= QOBJECT(qstring_from_escaped_str(ctxt
, token
));
634 /* A possibility exists that this is a whole-valued float where the
635 * fractional part was left out due to being 0 (.0). It's not a big
636 * deal to treat these as ints in the parser, so long as users of the
637 * resulting QObject know to expect a QInt in place of a QFloat in
640 * However, in some cases these values will overflow/underflow a
641 * QInt/int64 container, thus we should assume these are to be handled
642 * as QFloats/doubles rather than silently changing their values.
644 * strtoll() indicates these instances by setting errno to ERANGE
648 errno
= 0; /* strtoll doesn't set errno on success */
649 value
= strtoll(token_get_value(token
), NULL
, 10);
650 if (errno
!= ERANGE
) {
651 obj
= QOBJECT(qint_from_int(value
));
654 /* fall through to JSON_FLOAT */
657 /* FIXME dependent on locale */
658 obj
= QOBJECT(qfloat_from_double(strtod(token_get_value(token
), NULL
)));
667 parser_context_restore(ctxt
, saved_ctxt
);
672 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
676 obj
= parse_object(ctxt
, ap
);
678 obj
= parse_array(ctxt
, ap
);
681 obj
= parse_escape(ctxt
, ap
);
684 obj
= parse_keyword(ctxt
);
687 obj
= parse_literal(ctxt
);
693 QObject
*json_parser_parse(QList
*tokens
, va_list *ap
)
695 return json_parser_parse_err(tokens
, ap
, NULL
);
698 QObject
*json_parser_parse_err(QList
*tokens
, va_list *ap
, Error
**errp
)
700 JSONParserContext
*ctxt
= parser_context_new(tokens
);
707 result
= parse_value(ctxt
, ap
);
709 error_propagate(errp
, ctxt
->err
);
711 parser_context_free(ctxt
);