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"
23 #include "json-parser.h"
24 #include "json-lexer.h"
27 typedef struct JSONParserContext
32 #define BUG_ON(cond) assert(!(cond))
37 * 0) make errors meaningful again
38 * 1) add geometry information to tokens
39 * 3) should we return a parsed size?
40 * 4) deal with premature EOI
43 static QObject
*parse_value(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
);
48 * tokens are dictionaries that contain a type, a string value, and geometry information
49 * about a token identified by the lexer. These are routines that make working with
50 * these objects a bit easier.
52 static const char *token_get_value(QObject
*obj
)
54 return qdict_get_str(qobject_to_qdict(obj
), "token");
57 static JSONTokenType
token_get_type(QObject
*obj
)
59 return qdict_get_int(qobject_to_qdict(obj
), "type");
62 static int token_is_operator(QObject
*obj
, char op
)
66 if (token_get_type(obj
) != JSON_OPERATOR
) {
70 val
= token_get_value(obj
);
72 return (val
[0] == op
) && (val
[1] == 0);
75 static int token_is_keyword(QObject
*obj
, const char *value
)
77 if (token_get_type(obj
) != JSON_KEYWORD
) {
81 return strcmp(token_get_value(obj
), value
) == 0;
84 static int token_is_escape(QObject
*obj
, const char *value
)
86 if (token_get_type(obj
) != JSON_ESCAPE
) {
90 return (strcmp(token_get_value(obj
), value
) == 0);
96 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
97 QObject
*token
, const char *msg
, ...)
102 vsnprintf(message
, sizeof(message
), msg
, ap
);
105 error_free(ctxt
->err
);
108 error_set(&ctxt
->err
, QERR_JSON_PARSE_ERROR
, message
);
114 * These helpers are used to unescape strings.
116 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
118 if (wchar
<= 0x007F) {
119 BUG_ON(buffer_length
< 2);
121 buffer
[0] = wchar
& 0x7F;
123 } else if (wchar
<= 0x07FF) {
124 BUG_ON(buffer_length
< 3);
126 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
127 buffer
[1] = 0x80 | (wchar
& 0x3F);
130 BUG_ON(buffer_length
< 4);
132 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
133 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
134 buffer
[2] = 0x80 | (wchar
& 0x3F);
139 static int hex2decimal(char ch
)
141 if (ch
>= '0' && ch
<= '9') {
143 } else if (ch
>= 'a' && ch
<= 'f') {
144 return 10 + (ch
- 'a');
145 } else if (ch
>= 'A' && ch
<= 'F') {
146 return 10 + (ch
- 'A');
153 * parse_string(): Parse a json string and return a QObject
162 * any-Unicode-character-
175 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
, QObject
*token
)
177 const char *ptr
= token_get_value(token
);
179 int double_quote
= 1;
190 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
196 qstring_append(str
, "\"");
200 qstring_append(str
, "'");
204 qstring_append(str
, "\\");
208 qstring_append(str
, "/");
212 qstring_append(str
, "\b");
216 qstring_append(str
, "\f");
220 qstring_append(str
, "\n");
224 qstring_append(str
, "\r");
228 qstring_append(str
, "\t");
232 uint16_t unicode_char
= 0;
238 for (i
= 0; i
< 4; i
++) {
239 if (qemu_isxdigit(*ptr
)) {
240 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
242 parse_error(ctxt
, token
,
243 "invalid hex escape sequence in string");
249 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
250 qstring_append(str
, utf8_char
);
253 parse_error(ctxt
, token
, "invalid escape sequence in string");
262 qstring_append(str
, dummy
);
276 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, QList
**tokens
, va_list *ap
)
278 QObject
*key
= NULL
, *token
= NULL
, *value
, *peek
;
279 QList
*working
= qlist_copy(*tokens
);
281 peek
= qlist_peek(working
);
283 parse_error(ctxt
, NULL
, "premature EOI");
287 key
= parse_value(ctxt
, &working
, ap
);
288 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
289 parse_error(ctxt
, peek
, "key is not a string in object");
293 token
= qlist_pop(working
);
295 parse_error(ctxt
, NULL
, "premature EOI");
299 if (!token_is_operator(token
, ':')) {
300 parse_error(ctxt
, token
, "missing : in object pair");
304 value
= parse_value(ctxt
, &working
, ap
);
306 parse_error(ctxt
, token
, "Missing value in dict");
310 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
312 qobject_decref(token
);
320 qobject_decref(token
);
327 static QObject
*parse_object(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
330 QObject
*token
, *peek
;
331 QList
*working
= qlist_copy(*tokens
);
333 token
= qlist_pop(working
);
338 if (!token_is_operator(token
, '{')) {
341 qobject_decref(token
);
346 peek
= qlist_peek(working
);
348 parse_error(ctxt
, NULL
, "premature EOI");
352 if (!token_is_operator(peek
, '}')) {
353 if (parse_pair(ctxt
, dict
, &working
, ap
) == -1) {
357 token
= qlist_pop(working
);
359 parse_error(ctxt
, NULL
, "premature EOI");
363 while (!token_is_operator(token
, '}')) {
364 if (!token_is_operator(token
, ',')) {
365 parse_error(ctxt
, token
, "expected separator in dict");
368 qobject_decref(token
);
371 if (parse_pair(ctxt
, dict
, &working
, ap
) == -1) {
375 token
= qlist_pop(working
);
377 parse_error(ctxt
, NULL
, "premature EOI");
381 qobject_decref(token
);
384 token
= qlist_pop(working
);
385 qobject_decref(token
);
392 return QOBJECT(dict
);
395 qobject_decref(token
);
401 static QObject
*parse_array(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
404 QObject
*token
, *peek
;
405 QList
*working
= qlist_copy(*tokens
);
407 token
= qlist_pop(working
);
412 if (!token_is_operator(token
, '[')) {
415 qobject_decref(token
);
420 peek
= qlist_peek(working
);
422 parse_error(ctxt
, NULL
, "premature EOI");
426 if (!token_is_operator(peek
, ']')) {
429 obj
= parse_value(ctxt
, &working
, ap
);
431 parse_error(ctxt
, token
, "expecting value");
435 qlist_append_obj(list
, obj
);
437 token
= qlist_pop(working
);
439 parse_error(ctxt
, NULL
, "premature EOI");
443 while (!token_is_operator(token
, ']')) {
444 if (!token_is_operator(token
, ',')) {
445 parse_error(ctxt
, token
, "expected separator in list");
449 qobject_decref(token
);
452 obj
= parse_value(ctxt
, &working
, ap
);
454 parse_error(ctxt
, token
, "expecting value");
458 qlist_append_obj(list
, obj
);
460 token
= qlist_pop(working
);
462 parse_error(ctxt
, NULL
, "premature EOI");
467 qobject_decref(token
);
470 token
= qlist_pop(working
);
471 qobject_decref(token
);
478 return QOBJECT(list
);
481 qobject_decref(token
);
487 static QObject
*parse_keyword(JSONParserContext
*ctxt
, QList
**tokens
)
489 QObject
*token
, *ret
;
490 QList
*working
= qlist_copy(*tokens
);
492 token
= qlist_pop(working
);
497 if (token_get_type(token
) != JSON_KEYWORD
) {
501 if (token_is_keyword(token
, "true")) {
502 ret
= QOBJECT(qbool_from_int(true));
503 } else if (token_is_keyword(token
, "false")) {
504 ret
= QOBJECT(qbool_from_int(false));
506 parse_error(ctxt
, token
, "invalid keyword `%s'", token_get_value(token
));
510 qobject_decref(token
);
517 qobject_decref(token
);
523 static QObject
*parse_escape(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
525 QObject
*token
= NULL
, *obj
;
526 QList
*working
= qlist_copy(*tokens
);
532 token
= qlist_pop(working
);
537 if (token_is_escape(token
, "%p")) {
538 obj
= va_arg(*ap
, QObject
*);
539 } else if (token_is_escape(token
, "%i")) {
540 obj
= QOBJECT(qbool_from_int(va_arg(*ap
, int)));
541 } else if (token_is_escape(token
, "%d")) {
542 obj
= QOBJECT(qint_from_int(va_arg(*ap
, int)));
543 } else if (token_is_escape(token
, "%ld")) {
544 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long)));
545 } else if (token_is_escape(token
, "%lld") ||
546 token_is_escape(token
, "%I64d")) {
547 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long long)));
548 } else if (token_is_escape(token
, "%s")) {
549 obj
= QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
550 } else if (token_is_escape(token
, "%f")) {
551 obj
= QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
556 qobject_decref(token
);
563 qobject_decref(token
);
569 static QObject
*parse_literal(JSONParserContext
*ctxt
, QList
**tokens
)
571 QObject
*token
, *obj
;
572 QList
*working
= qlist_copy(*tokens
);
574 token
= qlist_pop(working
);
579 switch (token_get_type(token
)) {
581 obj
= QOBJECT(qstring_from_escaped_str(ctxt
, token
));
584 obj
= QOBJECT(qint_from_int(strtoll(token_get_value(token
), NULL
, 10)));
587 /* FIXME dependent on locale */
588 obj
= QOBJECT(qfloat_from_double(strtod(token_get_value(token
), NULL
)));
594 qobject_decref(token
);
601 qobject_decref(token
);
607 static QObject
*parse_value(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
611 obj
= parse_object(ctxt
, tokens
, ap
);
613 obj
= parse_array(ctxt
, tokens
, ap
);
616 obj
= parse_escape(ctxt
, tokens
, ap
);
619 obj
= parse_keyword(ctxt
, tokens
);
622 obj
= parse_literal(ctxt
, tokens
);
628 QObject
*json_parser_parse(QList
*tokens
, va_list *ap
)
630 return json_parser_parse_err(tokens
, ap
, NULL
);
633 QObject
*json_parser_parse_err(QList
*tokens
, va_list *ap
, Error
**errp
)
635 JSONParserContext ctxt
= {};
642 working
= qlist_copy(tokens
);
643 result
= parse_value(&ctxt
, &working
, ap
);
647 error_propagate(errp
, ctxt
.err
);