Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 1 | /* |
Eric Blake | 6e8e5cb | 2016-01-29 06:48:37 -0700 | [diff] [blame] | 2 | * JSON Parser |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 3 | * |
| 4 | * Copyright IBM, Corp. 2009 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 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. |
| 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 15 | #include "qemu/cutils.h" |
Markus Armbruster | e59f39d | 2018-08-23 18:39:49 +0200 | [diff] [blame] | 16 | #include "qemu/unicode.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 18 | #include "qemu-common.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 19 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 21 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 22 | #include "qapi/qmp/qnull.h" |
| 23 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 24 | #include "qapi/qmp/qstring.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 25 | #include "qapi/qmp/json-parser.h" |
| 26 | #include "qapi/qmp/json-lexer.h" |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 27 | #include "qapi/qmp/json-streamer.h" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 28 | |
| 29 | typedef struct JSONParserContext |
| 30 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 31 | Error *err; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 32 | JSONToken *current; |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 33 | GQueue *buf; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 34 | } JSONParserContext; |
| 35 | |
| 36 | #define BUG_ON(cond) assert(!(cond)) |
| 37 | |
| 38 | /** |
| 39 | * TODO |
| 40 | * |
| 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 |
| 45 | */ |
| 46 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 47 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 48 | |
| 49 | /** |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 50 | * Error handler |
| 51 | */ |
Stefan Weil | 8b7968f | 2010-09-23 21:28:05 +0200 | [diff] [blame] | 52 | static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt, |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 53 | JSONToken *token, const char *msg, ...) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 54 | { |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 55 | va_list ap; |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 56 | char message[1024]; |
Markus Armbruster | 574bf16 | 2018-08-23 18:39:50 +0200 | [diff] [blame] | 57 | |
| 58 | if (ctxt->err) { |
| 59 | return; |
| 60 | } |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 61 | va_start(ap, msg); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 62 | vsnprintf(message, sizeof(message), msg, ap); |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 63 | va_end(ap); |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 64 | error_setg(&ctxt->err, "JSON parse error, %s", message); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 67 | static int cvt4hex(const char *s) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 68 | { |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 69 | int cp, i; |
| 70 | |
| 71 | cp = 0; |
| 72 | for (i = 0; i < 4; i++) { |
| 73 | if (!qemu_isxdigit(s[i])) { |
| 74 | return -1; |
| 75 | } |
| 76 | cp <<= 4; |
| 77 | if (s[i] >= '0' && s[i] <= '9') { |
| 78 | cp |= s[i] - '0'; |
| 79 | } else if (s[i] >= 'a' && s[i] <= 'f') { |
| 80 | cp |= 10 + s[i] - 'a'; |
| 81 | } else if (s[i] >= 'A' && s[i] <= 'F') { |
| 82 | cp |= 10 + s[i] - 'A'; |
| 83 | } else { |
| 84 | return -1; |
| 85 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 86 | } |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 87 | return cp; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Markus Armbruster | b2da4a4 | 2018-08-23 18:39:53 +0200 | [diff] [blame] | 91 | * parse_string(): Parse a JSON string |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 92 | * |
Markus Armbruster | b2da4a4 | 2018-08-23 18:39:53 +0200 | [diff] [blame] | 93 | * From RFC 8259 "The JavaScript Object Notation (JSON) Data |
| 94 | * Interchange Format": |
| 95 | * |
| 96 | * char = unescaped / |
| 97 | * escape ( |
| 98 | * %x22 / ; " quotation mark U+0022 |
| 99 | * %x5C / ; \ reverse solidus U+005C |
| 100 | * %x2F / ; / solidus U+002F |
| 101 | * %x62 / ; b backspace U+0008 |
| 102 | * %x66 / ; f form feed U+000C |
| 103 | * %x6E / ; n line feed U+000A |
| 104 | * %x72 / ; r carriage return U+000D |
| 105 | * %x74 / ; t tab U+0009 |
| 106 | * %x75 4HEXDIG ) ; uXXXX U+XXXX |
| 107 | * escape = %x5C ; \ |
| 108 | * quotation-mark = %x22 ; " |
| 109 | * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF |
| 110 | * |
| 111 | * Extensions over RFC 8259: |
| 112 | * - Extra escape sequence in strings: |
| 113 | * 0x27 (apostrophe) is recognized after escape, too |
| 114 | * - Single-quoted strings: |
| 115 | * Like double-quoted strings, except they're delimited by %x27 |
| 116 | * (apostrophe) instead of %x22 (quotation mark), and can't contain |
| 117 | * unescaped apostrophe, but can contain unescaped quotation mark. |
| 118 | * |
| 119 | * Note: |
| 120 | * - Encoding is modified UTF-8. |
| 121 | * - Invalid Unicode characters are rejected. |
| 122 | * - Control characters \x00..\x1F are rejected by the lexer. |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 123 | */ |
Markus Armbruster | b2da4a4 | 2018-08-23 18:39:53 +0200 | [diff] [blame] | 124 | static QString *parse_string(JSONParserContext *ctxt, JSONToken *token) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 125 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 126 | const char *ptr = token->str; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 127 | QString *str; |
Markus Armbruster | 00ea57f | 2018-08-23 18:39:47 +0200 | [diff] [blame] | 128 | char quote; |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 129 | const char *beg; |
| 130 | int cp, trailing; |
Markus Armbruster | e59f39d | 2018-08-23 18:39:49 +0200 | [diff] [blame] | 131 | char *end; |
| 132 | ssize_t len; |
| 133 | char utf8_buf[5]; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 134 | |
Markus Armbruster | 00ea57f | 2018-08-23 18:39:47 +0200 | [diff] [blame] | 135 | assert(*ptr == '"' || *ptr == '\''); |
| 136 | quote = *ptr++; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 137 | str = qstring_new(); |
Markus Armbruster | 00ea57f | 2018-08-23 18:39:47 +0200 | [diff] [blame] | 138 | |
| 139 | while (*ptr != quote) { |
| 140 | assert(*ptr); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 141 | if (*ptr == '\\') { |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 142 | beg = ptr++; |
Markus Armbruster | 00ea57f | 2018-08-23 18:39:47 +0200 | [diff] [blame] | 143 | switch (*ptr++) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 144 | case '"': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 145 | qstring_append_chr(str, '"'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 146 | break; |
| 147 | case '\'': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 148 | qstring_append_chr(str, '\''); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 149 | break; |
| 150 | case '\\': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 151 | qstring_append_chr(str, '\\'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 152 | break; |
| 153 | case '/': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 154 | qstring_append_chr(str, '/'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 155 | break; |
| 156 | case 'b': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 157 | qstring_append_chr(str, '\b'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 158 | break; |
Luiz Capitulino | bd03269 | 2010-05-19 17:06:15 -0300 | [diff] [blame] | 159 | case 'f': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 160 | qstring_append_chr(str, '\f'); |
Luiz Capitulino | bd03269 | 2010-05-19 17:06:15 -0300 | [diff] [blame] | 161 | break; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 162 | case 'n': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 163 | qstring_append_chr(str, '\n'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 164 | break; |
| 165 | case 'r': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 166 | qstring_append_chr(str, '\r'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 167 | break; |
| 168 | case 't': |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 169 | qstring_append_chr(str, '\t'); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 170 | break; |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 171 | case 'u': |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 172 | cp = cvt4hex(ptr); |
| 173 | ptr += 4; |
| 174 | |
| 175 | /* handle surrogate pairs */ |
| 176 | if (cp >= 0xD800 && cp <= 0xDBFF |
| 177 | && ptr[0] == '\\' && ptr[1] == 'u') { |
| 178 | /* leading surrogate followed by \u */ |
| 179 | cp = 0x10000 + ((cp & 0x3FF) << 10); |
| 180 | trailing = cvt4hex(ptr + 2); |
| 181 | if (trailing >= 0xDC00 && trailing <= 0xDFFF) { |
| 182 | /* followed by trailing surrogate */ |
| 183 | cp |= trailing & 0x3FF; |
| 184 | ptr += 6; |
| 185 | } else { |
| 186 | cp = -1; /* invalid */ |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 187 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 188 | } |
| 189 | |
Markus Armbruster | 46a628b | 2018-08-23 18:39:55 +0200 | [diff] [blame] | 190 | if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) { |
| 191 | parse_error(ctxt, token, |
Markus Armbruster | dc45a07 | 2018-08-23 18:39:56 +0200 | [diff] [blame] | 192 | "%.*s is not a valid Unicode character", |
| 193 | (int)(ptr - beg), beg); |
Markus Armbruster | 46a628b | 2018-08-23 18:39:55 +0200 | [diff] [blame] | 194 | goto out; |
| 195 | } |
Markus Armbruster | de6decf | 2018-08-23 18:39:54 +0200 | [diff] [blame] | 196 | qstring_append(str, utf8_buf); |
| 197 | break; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 198 | default: |
| 199 | parse_error(ctxt, token, "invalid escape sequence in string"); |
| 200 | goto out; |
| 201 | } |
| 202 | } else { |
Markus Armbruster | e59f39d | 2018-08-23 18:39:49 +0200 | [diff] [blame] | 203 | cp = mod_utf8_codepoint(ptr, 6, &end); |
Markus Armbruster | 4b1c0cd | 2018-08-23 18:39:52 +0200 | [diff] [blame] | 204 | if (cp < 0) { |
Markus Armbruster | e59f39d | 2018-08-23 18:39:49 +0200 | [diff] [blame] | 205 | parse_error(ctxt, token, "invalid UTF-8 sequence in string"); |
| 206 | goto out; |
| 207 | } |
| 208 | ptr = end; |
| 209 | len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp); |
| 210 | assert(len >= 0); |
| 211 | qstring_append(str, utf8_buf); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 215 | return str; |
| 216 | |
| 217 | out: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 218 | qobject_unref(str); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 222 | /* Note: the token object returned by parser_context_peek_token or |
| 223 | * parser_context_pop_token is deleted as soon as parser_context_pop_token |
| 224 | * is called again. |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 225 | */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 226 | static JSONToken *parser_context_pop_token(JSONParserContext *ctxt) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 227 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 228 | g_free(ctxt->current); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 229 | assert(!g_queue_is_empty(ctxt->buf)); |
| 230 | ctxt->current = g_queue_pop_head(ctxt->buf); |
| 231 | return ctxt->current; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 234 | static JSONToken *parser_context_peek_token(JSONParserContext *ctxt) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 235 | { |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 236 | assert(!g_queue_is_empty(ctxt->buf)); |
| 237 | return g_queue_peek_head(ctxt->buf); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 240 | /** |
| 241 | * Parsing rules |
| 242 | */ |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 243 | static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 244 | { |
Max Reitz | 532fb53 | 2018-03-10 16:14:36 -0600 | [diff] [blame] | 245 | QObject *value; |
| 246 | QString *key = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 247 | JSONToken *peek, *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 248 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 249 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 250 | if (peek == NULL) { |
| 251 | parse_error(ctxt, NULL, "premature EOI"); |
| 252 | goto out; |
| 253 | } |
| 254 | |
Max Reitz | 532fb53 | 2018-03-10 16:14:36 -0600 | [diff] [blame] | 255 | key = qobject_to(QString, parse_value(ctxt, ap)); |
| 256 | if (!key) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 257 | parse_error(ctxt, peek, "key is not a string in object"); |
| 258 | goto out; |
| 259 | } |
| 260 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 261 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 262 | if (token == NULL) { |
| 263 | parse_error(ctxt, NULL, "premature EOI"); |
| 264 | goto out; |
| 265 | } |
| 266 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 267 | if (token->type != JSON_COLON) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 268 | parse_error(ctxt, token, "missing : in object pair"); |
| 269 | goto out; |
| 270 | } |
| 271 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 272 | value = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 273 | if (value == NULL) { |
| 274 | parse_error(ctxt, token, "Missing value in dict"); |
| 275 | goto out; |
| 276 | } |
| 277 | |
Max Reitz | 532fb53 | 2018-03-10 16:14:36 -0600 | [diff] [blame] | 278 | qdict_put_obj(dict, qstring_get_str(key), value); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 279 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 280 | qobject_unref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 281 | |
| 282 | return 0; |
| 283 | |
| 284 | out: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 285 | qobject_unref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 286 | |
| 287 | return -1; |
| 288 | } |
| 289 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 290 | static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 291 | { |
| 292 | QDict *dict = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 293 | JSONToken *token, *peek; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 294 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 295 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 296 | assert(token && token->type == JSON_LCURLY); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 297 | |
| 298 | dict = qdict_new(); |
| 299 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 300 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 301 | if (peek == NULL) { |
| 302 | parse_error(ctxt, NULL, "premature EOI"); |
| 303 | goto out; |
| 304 | } |
| 305 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 306 | if (peek->type != JSON_RCURLY) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 307 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 308 | goto out; |
| 309 | } |
| 310 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 311 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 312 | if (token == NULL) { |
| 313 | parse_error(ctxt, NULL, "premature EOI"); |
| 314 | goto out; |
| 315 | } |
| 316 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 317 | while (token->type != JSON_RCURLY) { |
| 318 | if (token->type != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 319 | parse_error(ctxt, token, "expected separator in dict"); |
| 320 | goto out; |
| 321 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 322 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 323 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 324 | goto out; |
| 325 | } |
| 326 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 327 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 328 | if (token == NULL) { |
| 329 | parse_error(ctxt, NULL, "premature EOI"); |
| 330 | goto out; |
| 331 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 332 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 333 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 334 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 335 | } |
| 336 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 337 | return QOBJECT(dict); |
| 338 | |
| 339 | out: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 340 | qobject_unref(dict); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
| 343 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 344 | static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 345 | { |
| 346 | QList *list = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 347 | JSONToken *token, *peek; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 348 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 349 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 350 | assert(token && token->type == JSON_LSQUARE); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 351 | |
| 352 | list = qlist_new(); |
| 353 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 354 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 355 | if (peek == NULL) { |
| 356 | parse_error(ctxt, NULL, "premature EOI"); |
| 357 | goto out; |
| 358 | } |
| 359 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 360 | if (peek->type != JSON_RSQUARE) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 361 | QObject *obj; |
| 362 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 363 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 364 | if (obj == NULL) { |
| 365 | parse_error(ctxt, token, "expecting value"); |
| 366 | goto out; |
| 367 | } |
| 368 | |
| 369 | qlist_append_obj(list, obj); |
| 370 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 371 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 372 | if (token == NULL) { |
| 373 | parse_error(ctxt, NULL, "premature EOI"); |
| 374 | goto out; |
| 375 | } |
| 376 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 377 | while (token->type != JSON_RSQUARE) { |
| 378 | if (token->type != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 379 | parse_error(ctxt, token, "expected separator in list"); |
| 380 | goto out; |
| 381 | } |
| 382 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 383 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 384 | if (obj == NULL) { |
| 385 | parse_error(ctxt, token, "expecting value"); |
| 386 | goto out; |
| 387 | } |
| 388 | |
| 389 | qlist_append_obj(list, obj); |
| 390 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 391 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 392 | if (token == NULL) { |
| 393 | parse_error(ctxt, NULL, "premature EOI"); |
| 394 | goto out; |
| 395 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 396 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 397 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 398 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 399 | } |
| 400 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 401 | return QOBJECT(list); |
| 402 | |
| 403 | out: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 404 | qobject_unref(list); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 405 | return NULL; |
| 406 | } |
| 407 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 408 | static QObject *parse_keyword(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 409 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 410 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 411 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 412 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 413 | assert(token && token->type == JSON_KEYWORD); |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 414 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 415 | if (!strcmp(token->str, "true")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 416 | return QOBJECT(qbool_from_bool(true)); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 417 | } else if (!strcmp(token->str, "false")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 418 | return QOBJECT(qbool_from_bool(false)); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 419 | } else if (!strcmp(token->str, "null")) { |
Markus Armbruster | 006ca09 | 2017-06-26 13:52:24 +0200 | [diff] [blame] | 420 | return QOBJECT(qnull()); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 421 | } |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 422 | parse_error(ctxt, token, "invalid keyword '%s'", token->str); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 423 | return NULL; |
| 424 | } |
| 425 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 426 | static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 427 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 428 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 429 | |
| 430 | if (ap == NULL) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 431 | return NULL; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 432 | } |
| 433 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 434 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 435 | assert(token && token->type == JSON_ESCAPE); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 436 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 437 | if (!strcmp(token->str, "%p")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 438 | return va_arg(*ap, QObject *); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 439 | } else if (!strcmp(token->str, "%i")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 440 | return QOBJECT(qbool_from_bool(va_arg(*ap, int))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 441 | } else if (!strcmp(token->str, "%d")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 442 | return QOBJECT(qnum_from_int(va_arg(*ap, int))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 443 | } else if (!strcmp(token->str, "%ld")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 444 | return QOBJECT(qnum_from_int(va_arg(*ap, long))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 445 | } else if (!strcmp(token->str, "%lld") || |
| 446 | !strcmp(token->str, "%I64d")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 447 | return QOBJECT(qnum_from_int(va_arg(*ap, long long))); |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 448 | } else if (!strcmp(token->str, "%u")) { |
| 449 | return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int))); |
| 450 | } else if (!strcmp(token->str, "%lu")) { |
| 451 | return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long))); |
| 452 | } else if (!strcmp(token->str, "%llu") || |
| 453 | !strcmp(token->str, "%I64u")) { |
| 454 | return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 455 | } else if (!strcmp(token->str, "%s")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 456 | return QOBJECT(qstring_from_str(va_arg(*ap, const char *))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 457 | } else if (!strcmp(token->str, "%f")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 458 | return QOBJECT(qnum_from_double(va_arg(*ap, double))); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 459 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 460 | return NULL; |
| 461 | } |
| 462 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 463 | static QObject *parse_literal(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 464 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 465 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 466 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 467 | token = parser_context_pop_token(ctxt); |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 468 | assert(token); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 469 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 470 | switch (token->type) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 471 | case JSON_STRING: |
Markus Armbruster | b2da4a4 | 2018-08-23 18:39:53 +0200 | [diff] [blame] | 472 | return QOBJECT(parse_string(ctxt, token)); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 473 | case JSON_INTEGER: { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 474 | /* |
| 475 | * Represent JSON_INTEGER as QNUM_I64 if possible, else as |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 476 | * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64() |
| 477 | * and qemu_strtou64() fail with ERANGE when it's not |
| 478 | * possible. |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 479 | * |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 480 | * qnum_get_int() will then work for any signed 64-bit |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 481 | * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit |
| 482 | * integer, and qnum_get_double() both for any JSON_INTEGER |
| 483 | * and any JSON_FLOAT (with precision loss for integers beyond |
| 484 | * 53 bits) |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 485 | */ |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 486 | int ret; |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 487 | int64_t value; |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 488 | uint64_t uvalue; |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 489 | |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 490 | ret = qemu_strtoi64(token->str, NULL, 10, &value); |
| 491 | if (!ret) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 492 | return QOBJECT(qnum_from_int(value)); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 493 | } |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 494 | assert(ret == -ERANGE); |
| 495 | |
| 496 | if (token->str[0] != '-') { |
| 497 | ret = qemu_strtou64(token->str, NULL, 10, &uvalue); |
| 498 | if (!ret) { |
| 499 | return QOBJECT(qnum_from_uint(uvalue)); |
| 500 | } |
| 501 | assert(ret == -ERANGE); |
| 502 | } |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 503 | /* fall through to JSON_FLOAT */ |
| 504 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 505 | case JSON_FLOAT: |
Eric Blake | 6e8e5cb | 2016-01-29 06:48:37 -0700 | [diff] [blame] | 506 | /* FIXME dependent on locale; a pervasive issue in QEMU */ |
| 507 | /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN, |
| 508 | * but those might be useful extensions beyond JSON */ |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 509 | return QOBJECT(qnum_from_double(strtod(token->str, NULL))); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 510 | default: |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 511 | abort(); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 512 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 513 | } |
| 514 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 515 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 516 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 517 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 518 | |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 519 | token = parser_context_peek_token(ctxt); |
| 520 | if (token == NULL) { |
| 521 | parse_error(ctxt, NULL, "premature EOI"); |
| 522 | return NULL; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 523 | } |
| 524 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 525 | switch (token->type) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 526 | case JSON_LCURLY: |
| 527 | return parse_object(ctxt, ap); |
| 528 | case JSON_LSQUARE: |
| 529 | return parse_array(ctxt, ap); |
| 530 | case JSON_ESCAPE: |
| 531 | return parse_escape(ctxt, ap); |
| 532 | case JSON_INTEGER: |
| 533 | case JSON_FLOAT: |
| 534 | case JSON_STRING: |
| 535 | return parse_literal(ctxt); |
| 536 | case JSON_KEYWORD: |
| 537 | return parse_keyword(ctxt); |
| 538 | default: |
| 539 | parse_error(ctxt, token, "expecting value"); |
| 540 | return NULL; |
| 541 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 542 | } |
| 543 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 544 | QObject *json_parser_parse(GQueue *tokens, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 545 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 546 | return json_parser_parse_err(tokens, ap, NULL); |
| 547 | } |
| 548 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 549 | QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp) |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 550 | { |
Marc-André Lureau | e8b19d7 | 2018-08-23 18:39:59 +0200 | [diff] [blame] | 551 | JSONParserContext ctxt = { .buf = tokens }; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 552 | QObject *result; |
| 553 | |
Marc-André Lureau | e8b19d7 | 2018-08-23 18:39:59 +0200 | [diff] [blame] | 554 | if (!tokens) { |
Michael Roth | c1990eb | 2011-06-01 12:15:00 -0500 | [diff] [blame] | 555 | return NULL; |
| 556 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 557 | |
Marc-André Lureau | e8b19d7 | 2018-08-23 18:39:59 +0200 | [diff] [blame] | 558 | result = parse_value(&ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 559 | |
Marc-André Lureau | e8b19d7 | 2018-08-23 18:39:59 +0200 | [diff] [blame] | 560 | error_propagate(errp, ctxt.err); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 561 | |
Marc-André Lureau | e8b19d7 | 2018-08-23 18:39:59 +0200 | [diff] [blame] | 562 | while (!g_queue_is_empty(ctxt.buf)) { |
| 563 | parser_context_pop_token(&ctxt); |
| 564 | } |
| 565 | g_free(ctxt.current); |
| 566 | g_queue_free(ctxt.buf); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 567 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 568 | return result; |
| 569 | } |