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"
26 typedef struct JSONParserContext
30 #define BUG_ON(cond) assert(!(cond))
35 * 0) make errors meaningful again
36 * 1) add geometry information to tokens
37 * 3) should we return a parsed size?
38 * 4) deal with premature EOI
41 static QObject
*parse_value(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
);
46 * tokens are dictionaries that contain a type, a string value, and geometry information
47 * about a token identified by the lexer. These are routines that make working with
48 * these objects a bit easier.
50 static const char *token_get_value(QObject
*obj
)
52 return qdict_get_str(qobject_to_qdict(obj
), "token");
55 static JSONTokenType
token_get_type(QObject
*obj
)
57 return qdict_get_int(qobject_to_qdict(obj
), "type");
60 static int token_is_operator(QObject
*obj
, char op
)
64 if (token_get_type(obj
) != JSON_OPERATOR
) {
68 val
= token_get_value(obj
);
70 return (val
[0] == op
) && (val
[1] == 0);
73 static int token_is_keyword(QObject
*obj
, const char *value
)
75 if (token_get_type(obj
) != JSON_KEYWORD
) {
79 return strcmp(token_get_value(obj
), value
) == 0;
82 static int token_is_escape(QObject
*obj
, const char *value
)
84 if (token_get_type(obj
) != JSON_ESCAPE
) {
88 return (strcmp(token_get_value(obj
), value
) == 0);
94 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
95 QObject
*token
, const char *msg
, ...)
99 fprintf(stderr
, "parse error: ");
100 vfprintf(stderr
, msg
, ap
);
101 fprintf(stderr
, "\n");
108 * These helpers are used to unescape strings.
110 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
112 if (wchar
<= 0x007F) {
113 BUG_ON(buffer_length
< 2);
115 buffer
[0] = wchar
& 0x7F;
117 } else if (wchar
<= 0x07FF) {
118 BUG_ON(buffer_length
< 3);
120 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
121 buffer
[1] = 0x80 | (wchar
& 0x3F);
124 BUG_ON(buffer_length
< 4);
126 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
127 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
128 buffer
[2] = 0x80 | (wchar
& 0x3F);
133 static int hex2decimal(char ch
)
135 if (ch
>= '0' && ch
<= '9') {
137 } else if (ch
>= 'a' && ch
<= 'f') {
138 return 10 + (ch
- 'a');
139 } else if (ch
>= 'A' && ch
<= 'F') {
140 return 10 + (ch
- 'A');
147 * parse_string(): Parse a json string and return a QObject
156 * any-Unicode-character-
169 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
, QObject
*token
)
171 const char *ptr
= token_get_value(token
);
173 int double_quote
= 1;
184 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
190 qstring_append(str
, "\"");
194 qstring_append(str
, "'");
198 qstring_append(str
, "\\");
202 qstring_append(str
, "/");
206 qstring_append(str
, "\b");
210 qstring_append(str
, "\f");
214 qstring_append(str
, "\n");
218 qstring_append(str
, "\r");
222 qstring_append(str
, "\t");
226 uint16_t unicode_char
= 0;
232 for (i
= 0; i
< 4; i
++) {
233 if (qemu_isxdigit(*ptr
)) {
234 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
236 parse_error(ctxt
, token
,
237 "invalid hex escape sequence in string");
243 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
244 qstring_append(str
, utf8_char
);
247 parse_error(ctxt
, token
, "invalid escape sequence in string");
256 qstring_append(str
, dummy
);
270 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, QList
**tokens
, va_list *ap
)
272 QObject
*key
, *token
= NULL
, *value
, *peek
;
273 QList
*working
= qlist_copy(*tokens
);
275 peek
= qlist_peek(working
);
276 key
= parse_value(ctxt
, &working
, ap
);
277 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
278 parse_error(ctxt
, peek
, "key is not a string in object");
282 token
= qlist_pop(working
);
283 if (!token_is_operator(token
, ':')) {
284 parse_error(ctxt
, token
, "missing : in object pair");
288 value
= parse_value(ctxt
, &working
, ap
);
290 parse_error(ctxt
, token
, "Missing value in dict");
294 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
296 qobject_decref(token
);
304 qobject_decref(token
);
311 static QObject
*parse_object(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
314 QObject
*token
, *peek
;
315 QList
*working
= qlist_copy(*tokens
);
317 token
= qlist_pop(working
);
318 if (!token_is_operator(token
, '{')) {
321 qobject_decref(token
);
326 peek
= qlist_peek(working
);
327 if (!token_is_operator(peek
, '}')) {
328 if (parse_pair(ctxt
, dict
, &working
, ap
) == -1) {
332 token
= qlist_pop(working
);
333 while (!token_is_operator(token
, '}')) {
334 if (!token_is_operator(token
, ',')) {
335 parse_error(ctxt
, token
, "expected separator in dict");
338 qobject_decref(token
);
341 if (parse_pair(ctxt
, dict
, &working
, ap
) == -1) {
345 token
= qlist_pop(working
);
347 qobject_decref(token
);
350 token
= qlist_pop(working
);
351 qobject_decref(token
);
358 return QOBJECT(dict
);
361 qobject_decref(token
);
367 static QObject
*parse_array(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
370 QObject
*token
, *peek
;
371 QList
*working
= qlist_copy(*tokens
);
373 token
= qlist_pop(working
);
374 if (!token_is_operator(token
, '[')) {
377 qobject_decref(token
);
382 peek
= qlist_peek(working
);
383 if (!token_is_operator(peek
, ']')) {
386 obj
= parse_value(ctxt
, &working
, ap
);
388 parse_error(ctxt
, token
, "expecting value");
392 qlist_append_obj(list
, obj
);
394 token
= qlist_pop(working
);
395 while (!token_is_operator(token
, ']')) {
396 if (!token_is_operator(token
, ',')) {
397 parse_error(ctxt
, token
, "expected separator in list");
401 qobject_decref(token
);
404 obj
= parse_value(ctxt
, &working
, ap
);
406 parse_error(ctxt
, token
, "expecting value");
410 qlist_append_obj(list
, obj
);
412 token
= qlist_pop(working
);
415 qobject_decref(token
);
418 token
= qlist_pop(working
);
419 qobject_decref(token
);
426 return QOBJECT(list
);
429 qobject_decref(token
);
435 static QObject
*parse_keyword(JSONParserContext
*ctxt
, QList
**tokens
)
437 QObject
*token
, *ret
;
438 QList
*working
= qlist_copy(*tokens
);
440 token
= qlist_pop(working
);
442 if (token_get_type(token
) != JSON_KEYWORD
) {
446 if (token_is_keyword(token
, "true")) {
447 ret
= QOBJECT(qbool_from_int(true));
448 } else if (token_is_keyword(token
, "false")) {
449 ret
= QOBJECT(qbool_from_int(false));
451 parse_error(ctxt
, token
, "invalid keyword `%s'", token_get_value(token
));
455 qobject_decref(token
);
462 qobject_decref(token
);
468 static QObject
*parse_escape(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
470 QObject
*token
= NULL
, *obj
;
471 QList
*working
= qlist_copy(*tokens
);
477 token
= qlist_pop(working
);
479 if (token_is_escape(token
, "%p")) {
480 obj
= va_arg(*ap
, QObject
*);
481 } else if (token_is_escape(token
, "%i")) {
482 obj
= QOBJECT(qbool_from_int(va_arg(*ap
, int)));
483 } else if (token_is_escape(token
, "%d")) {
484 obj
= QOBJECT(qint_from_int(va_arg(*ap
, int)));
485 } else if (token_is_escape(token
, "%ld")) {
486 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long)));
487 } else if (token_is_escape(token
, "%lld") ||
488 token_is_escape(token
, "%I64d")) {
489 obj
= QOBJECT(qint_from_int(va_arg(*ap
, long long)));
490 } else if (token_is_escape(token
, "%s")) {
491 obj
= QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
492 } else if (token_is_escape(token
, "%f")) {
493 obj
= QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
498 qobject_decref(token
);
505 qobject_decref(token
);
511 static QObject
*parse_literal(JSONParserContext
*ctxt
, QList
**tokens
)
513 QObject
*token
, *obj
;
514 QList
*working
= qlist_copy(*tokens
);
516 token
= qlist_pop(working
);
517 switch (token_get_type(token
)) {
519 obj
= QOBJECT(qstring_from_escaped_str(ctxt
, token
));
522 obj
= QOBJECT(qint_from_int(strtoll(token_get_value(token
), NULL
, 10)));
525 /* FIXME dependent on locale */
526 obj
= QOBJECT(qfloat_from_double(strtod(token_get_value(token
), NULL
)));
532 qobject_decref(token
);
539 qobject_decref(token
);
545 static QObject
*parse_value(JSONParserContext
*ctxt
, QList
**tokens
, va_list *ap
)
549 obj
= parse_object(ctxt
, tokens
, ap
);
551 obj
= parse_array(ctxt
, tokens
, ap
);
554 obj
= parse_escape(ctxt
, tokens
, ap
);
557 obj
= parse_keyword(ctxt
, tokens
);
560 obj
= parse_literal(ctxt
, tokens
);
566 QObject
*json_parser_parse(QList
*tokens
, va_list *ap
)
568 JSONParserContext ctxt
= {};
569 QList
*working
= qlist_copy(tokens
);
572 result
= parse_value(&ctxt
, &working
, ap
);