4 * Copyright IBM, Corp. 2009
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 parse_error(JSONParserContext *ctxt, QObject *token, const char *msg, ...)
98 fprintf(stderr, "parse error: ");
99 vfprintf(stderr, msg, ap);
100 fprintf(stderr, "\n");
107 * These helpers are used to unescape strings.
109 static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
111 if (wchar <= 0x007F) {
112 BUG_ON(buffer_length < 2);
114 buffer[0] = wchar & 0x7F;
116 } else if (wchar <= 0x07FF) {
117 BUG_ON(buffer_length < 3);
119 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
120 buffer[1] = 0x80 | (wchar & 0x3F);
123 BUG_ON(buffer_length < 4);
125 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
126 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
127 buffer[2] = 0x80 | (wchar & 0x3F);
132 static int hex2decimal(char ch)
134 if (ch >= '0' && ch <= '9') {
136 } else if (ch >= 'a' && ch <= 'f') {
137 return 10 + (ch - 'a');
138 } else if (ch >= 'A' && ch <= 'F') {
139 return 10 + (ch - 'A');
146 * parse_string(): Parse a json string and return a QObject
155 * any-Unicode-character-
168 static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *token)
170 const char *ptr = token_get_value(token);
172 int double_quote = 1;
183 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
189 qstring_append(str, "\"");
193 qstring_append(str, "'");
197 qstring_append(str, "\\");
201 qstring_append(str, "/");
205 qstring_append(str, "\b");
209 qstring_append(str, "\f");
213 qstring_append(str, "\n");
217 qstring_append(str, "\r");
221 qstring_append(str, "\t");
225 uint16_t unicode_char = 0;
231 for (i = 0; i < 4; i++) {
232 if (qemu_isxdigit(*ptr)) {
233 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
235 parse_error(ctxt, token,
236 "invalid hex escape sequence in string");
242 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
243 qstring_append(str, utf8_char);
246 parse_error(ctxt, token, "invalid escape sequence in string");
255 qstring_append(str, dummy);
269 static int parse_pair(JSONParserContext *ctxt, QDict *dict, QList **tokens, va_list *ap)
271 QObject *key, *token = NULL, *value, *peek;
272 QList *working = qlist_copy(*tokens);
274 peek = qlist_peek(working);
275 key = parse_value(ctxt, &working, ap);
276 if (!key || qobject_type(key) != QTYPE_QSTRING) {
277 parse_error(ctxt, peek, "key is not a string in object");
281 token = qlist_pop(working);
282 if (!token_is_operator(token, ':')) {
283 parse_error(ctxt, token, "missing : in object pair");
287 value = parse_value(ctxt, &working, ap);
289 parse_error(ctxt, token, "Missing value in dict");
293 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
295 qobject_decref(token);
303 qobject_decref(token);
310 static QObject *parse_object(JSONParserContext *ctxt, QList **tokens, va_list *ap)
313 QObject *token, *peek;
314 QList *working = qlist_copy(*tokens);
316 token = qlist_pop(working);
317 if (!token_is_operator(token, '{')) {
320 qobject_decref(token);
325 peek = qlist_peek(working);
326 if (!token_is_operator(peek, '}')) {
327 if (parse_pair(ctxt, dict, &working, ap) == -1) {
331 token = qlist_pop(working);
332 while (!token_is_operator(token, '}')) {
333 if (!token_is_operator(token, ',')) {
334 parse_error(ctxt, token, "expected separator in dict");
337 qobject_decref(token);
340 if (parse_pair(ctxt, dict, &working, ap) == -1) {
344 token = qlist_pop(working);
346 qobject_decref(token);
349 token = qlist_pop(working);
350 qobject_decref(token);
357 return QOBJECT(dict);
360 qobject_decref(token);
366 static QObject *parse_array(JSONParserContext *ctxt, QList **tokens, va_list *ap)
369 QObject *token, *peek;
370 QList *working = qlist_copy(*tokens);
372 token = qlist_pop(working);
373 if (!token_is_operator(token, '[')) {
376 qobject_decref(token);
381 peek = qlist_peek(working);
382 if (!token_is_operator(peek, ']')) {
385 obj = parse_value(ctxt, &working, ap);
387 parse_error(ctxt, token, "expecting value");
391 qlist_append_obj(list, obj);
393 token = qlist_pop(working);
394 while (!token_is_operator(token, ']')) {
395 if (!token_is_operator(token, ',')) {
396 parse_error(ctxt, token, "expected separator in list");
400 qobject_decref(token);
403 obj = parse_value(ctxt, &working, ap);
405 parse_error(ctxt, token, "expecting value");
409 qlist_append_obj(list, obj);
411 token = qlist_pop(working);
414 qobject_decref(token);
417 token = qlist_pop(working);
418 qobject_decref(token);
425 return QOBJECT(list);
428 qobject_decref(token);
434 static QObject *parse_keyword(JSONParserContext *ctxt, QList **tokens)
436 QObject *token, *ret;
437 QList *working = qlist_copy(*tokens);
439 token = qlist_pop(working);
441 if (token_get_type(token) != JSON_KEYWORD) {
445 if (token_is_keyword(token, "true")) {
446 ret = QOBJECT(qbool_from_int(true));
447 } else if (token_is_keyword(token, "false")) {
448 ret = QOBJECT(qbool_from_int(false));
450 parse_error(ctxt, token, "invalid keyword `%s'", token_get_value(token));
454 qobject_decref(token);
461 qobject_decref(token);
467 static QObject *parse_escape(JSONParserContext *ctxt, QList **tokens, va_list *ap)
469 QObject *token = NULL, *obj;
470 QList *working = qlist_copy(*tokens);
476 token = qlist_pop(working);
478 if (token_is_escape(token, "%p")) {
479 obj = va_arg(*ap, QObject *);
480 } else if (token_is_escape(token, "%i")) {
481 obj = QOBJECT(qbool_from_int(va_arg(*ap, int)));
482 } else if (token_is_escape(token, "%d")) {
483 obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
484 } else if (token_is_escape(token, "%ld")) {
485 obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
486 } else if (token_is_escape(token, "%lld") ||
487 token_is_escape(token, "%I64d")) {
488 obj = QOBJECT(qint_from_int(va_arg(*ap, long long)));
489 } else if (token_is_escape(token, "%s")) {
490 obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
491 } else if (token_is_escape(token, "%f")) {
492 obj = QOBJECT(qfloat_from_double(va_arg(*ap, double)));
497 qobject_decref(token);
504 qobject_decref(token);
510 static QObject *parse_literal(JSONParserContext *ctxt, QList **tokens)
512 QObject *token, *obj;
513 QList *working = qlist_copy(*tokens);
515 token = qlist_pop(working);
516 switch (token_get_type(token)) {
518 obj = QOBJECT(qstring_from_escaped_str(ctxt, token));
521 obj = QOBJECT(qint_from_int(strtoll(token_get_value(token), NULL, 10)));
524 /* FIXME dependent on locale */
525 obj = QOBJECT(qfloat_from_double(strtod(token_get_value(token), NULL)));
531 qobject_decref(token);
538 qobject_decref(token);
544 static QObject *parse_value(JSONParserContext *ctxt, QList **tokens, va_list *ap)
548 obj = parse_object(ctxt, tokens, ap);
550 obj = parse_array(ctxt, tokens, ap);
553 obj = parse_escape(ctxt, tokens, ap);
556 obj = parse_keyword(ctxt, tokens);
559 obj = parse_literal(ctxt, tokens);
565 QObject *json_parser_parse(QList *tokens, va_list *ap)
567 JSONParserContext ctxt = {};
568 QList *working = qlist_copy(tokens);
571 result = parse_value(&ctxt, &working, ap);