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" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 15 | #include "qapi/error.h" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 16 | #include "qemu-common.h" |
Eric Blake | c7eb39c | 2016-06-09 10:48:32 -0600 | [diff] [blame] | 17 | #include "qapi/qmp/types.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/json-parser.h" |
| 19 | #include "qapi/qmp/json-lexer.h" |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/json-streamer.h" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 21 | |
| 22 | typedef struct JSONParserContext |
| 23 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 24 | Error *err; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 25 | JSONToken *current; |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 26 | GQueue *buf; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 27 | } JSONParserContext; |
| 28 | |
| 29 | #define BUG_ON(cond) assert(!(cond)) |
| 30 | |
| 31 | /** |
| 32 | * TODO |
| 33 | * |
| 34 | * 0) make errors meaningful again |
| 35 | * 1) add geometry information to tokens |
| 36 | * 3) should we return a parsed size? |
| 37 | * 4) deal with premature EOI |
| 38 | */ |
| 39 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 40 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 41 | |
| 42 | /** |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 43 | * Error handler |
| 44 | */ |
Stefan Weil | 8b7968f | 2010-09-23 21:28:05 +0200 | [diff] [blame] | 45 | static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt, |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 46 | JSONToken *token, const char *msg, ...) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 47 | { |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 48 | va_list ap; |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 49 | char message[1024]; |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 50 | va_start(ap, msg); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 51 | vsnprintf(message, sizeof(message), msg, ap); |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 52 | va_end(ap); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 53 | if (ctxt->err) { |
| 54 | error_free(ctxt->err); |
| 55 | ctxt->err = NULL; |
| 56 | } |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 57 | error_setg(&ctxt->err, "JSON parse error, %s", message); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * String helpers |
| 62 | * |
| 63 | * These helpers are used to unescape strings. |
| 64 | */ |
| 65 | static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length) |
| 66 | { |
| 67 | if (wchar <= 0x007F) { |
| 68 | BUG_ON(buffer_length < 2); |
| 69 | |
| 70 | buffer[0] = wchar & 0x7F; |
| 71 | buffer[1] = 0; |
| 72 | } else if (wchar <= 0x07FF) { |
| 73 | BUG_ON(buffer_length < 3); |
| 74 | |
| 75 | buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F); |
| 76 | buffer[1] = 0x80 | (wchar & 0x3F); |
| 77 | buffer[2] = 0; |
| 78 | } else { |
| 79 | BUG_ON(buffer_length < 4); |
| 80 | |
| 81 | buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F); |
| 82 | buffer[1] = 0x80 | ((wchar >> 6) & 0x3F); |
| 83 | buffer[2] = 0x80 | (wchar & 0x3F); |
| 84 | buffer[3] = 0; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static int hex2decimal(char ch) |
| 89 | { |
| 90 | if (ch >= '0' && ch <= '9') { |
| 91 | return (ch - '0'); |
| 92 | } else if (ch >= 'a' && ch <= 'f') { |
| 93 | return 10 + (ch - 'a'); |
| 94 | } else if (ch >= 'A' && ch <= 'F') { |
| 95 | return 10 + (ch - 'A'); |
| 96 | } |
| 97 | |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * parse_string(): Parse a json string and return a QObject |
| 103 | * |
| 104 | * string |
| 105 | * "" |
| 106 | * " chars " |
| 107 | * chars |
| 108 | * char |
| 109 | * char chars |
| 110 | * char |
| 111 | * any-Unicode-character- |
| 112 | * except-"-or-\-or- |
| 113 | * control-character |
| 114 | * \" |
| 115 | * \\ |
| 116 | * \/ |
| 117 | * \b |
| 118 | * \f |
| 119 | * \n |
| 120 | * \r |
| 121 | * \t |
| 122 | * \u four-hex-digits |
| 123 | */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 124 | static QString *qstring_from_escaped_str(JSONParserContext *ctxt, |
| 125 | JSONToken *token) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 126 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 127 | const char *ptr = token->str; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 128 | QString *str; |
| 129 | int double_quote = 1; |
| 130 | |
| 131 | if (*ptr == '"') { |
| 132 | double_quote = 1; |
| 133 | } else { |
| 134 | double_quote = 0; |
| 135 | } |
| 136 | ptr++; |
| 137 | |
| 138 | str = qstring_new(); |
| 139 | while (*ptr && |
| 140 | ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) { |
| 141 | if (*ptr == '\\') { |
| 142 | ptr++; |
| 143 | |
| 144 | switch (*ptr) { |
| 145 | case '"': |
| 146 | qstring_append(str, "\""); |
| 147 | ptr++; |
| 148 | break; |
| 149 | case '\'': |
| 150 | qstring_append(str, "'"); |
| 151 | ptr++; |
| 152 | break; |
| 153 | case '\\': |
| 154 | qstring_append(str, "\\"); |
| 155 | ptr++; |
| 156 | break; |
| 157 | case '/': |
| 158 | qstring_append(str, "/"); |
| 159 | ptr++; |
| 160 | break; |
| 161 | case 'b': |
| 162 | qstring_append(str, "\b"); |
| 163 | ptr++; |
| 164 | break; |
Luiz Capitulino | bd03269 | 2010-05-19 17:06:15 -0300 | [diff] [blame] | 165 | case 'f': |
| 166 | qstring_append(str, "\f"); |
| 167 | ptr++; |
| 168 | break; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 169 | case 'n': |
| 170 | qstring_append(str, "\n"); |
| 171 | ptr++; |
| 172 | break; |
| 173 | case 'r': |
| 174 | qstring_append(str, "\r"); |
| 175 | ptr++; |
| 176 | break; |
| 177 | case 't': |
| 178 | qstring_append(str, "\t"); |
| 179 | ptr++; |
| 180 | break; |
| 181 | case 'u': { |
| 182 | uint16_t unicode_char = 0; |
| 183 | char utf8_char[4]; |
| 184 | int i = 0; |
| 185 | |
| 186 | ptr++; |
| 187 | |
| 188 | for (i = 0; i < 4; i++) { |
| 189 | if (qemu_isxdigit(*ptr)) { |
| 190 | unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4); |
| 191 | } else { |
| 192 | parse_error(ctxt, token, |
| 193 | "invalid hex escape sequence in string"); |
| 194 | goto out; |
| 195 | } |
| 196 | ptr++; |
| 197 | } |
| 198 | |
| 199 | wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char)); |
| 200 | qstring_append(str, utf8_char); |
| 201 | } break; |
| 202 | default: |
| 203 | parse_error(ctxt, token, "invalid escape sequence in string"); |
| 204 | goto out; |
| 205 | } |
| 206 | } else { |
| 207 | char dummy[2]; |
| 208 | |
| 209 | dummy[0] = *ptr++; |
| 210 | dummy[1] = 0; |
| 211 | |
| 212 | qstring_append(str, dummy); |
| 213 | } |
| 214 | } |
| 215 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 216 | return str; |
| 217 | |
| 218 | out: |
| 219 | QDECREF(str); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 223 | /* Note: the token object returned by parser_context_peek_token or |
| 224 | * parser_context_pop_token is deleted as soon as parser_context_pop_token |
| 225 | * is called again. |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 226 | */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 227 | static JSONToken *parser_context_pop_token(JSONParserContext *ctxt) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 228 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 229 | g_free(ctxt->current); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 230 | assert(!g_queue_is_empty(ctxt->buf)); |
| 231 | ctxt->current = g_queue_pop_head(ctxt->buf); |
| 232 | return ctxt->current; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 235 | static JSONToken *parser_context_peek_token(JSONParserContext *ctxt) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 236 | { |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 237 | assert(!g_queue_is_empty(ctxt->buf)); |
| 238 | return g_queue_peek_head(ctxt->buf); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 241 | static JSONParserContext *parser_context_new(GQueue *tokens) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 242 | { |
| 243 | JSONParserContext *ctxt; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 244 | |
| 245 | if (!tokens) { |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 249 | ctxt = g_malloc0(sizeof(JSONParserContext)); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 250 | ctxt->buf = tokens; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 251 | |
| 252 | return ctxt; |
| 253 | } |
| 254 | |
| 255 | /* to support error propagation, ctxt->err must be freed separately */ |
| 256 | static void parser_context_free(JSONParserContext *ctxt) |
| 257 | { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 258 | if (ctxt) { |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 259 | while (!g_queue_is_empty(ctxt->buf)) { |
| 260 | parser_context_pop_token(ctxt); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 261 | } |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 262 | g_free(ctxt->current); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 263 | g_queue_free(ctxt->buf); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 264 | g_free(ctxt); |
| 265 | } |
| 266 | } |
| 267 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 268 | /** |
| 269 | * Parsing rules |
| 270 | */ |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 271 | static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 272 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 273 | QObject *key = NULL, *value; |
| 274 | JSONToken *peek, *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 275 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 276 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 277 | if (peek == NULL) { |
| 278 | parse_error(ctxt, NULL, "premature EOI"); |
| 279 | goto out; |
| 280 | } |
| 281 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 282 | key = parse_value(ctxt, ap); |
Kevin Wolf | d758d90 | 2010-02-24 16:17:58 +0100 | [diff] [blame] | 283 | if (!key || qobject_type(key) != QTYPE_QSTRING) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 284 | parse_error(ctxt, peek, "key is not a string in object"); |
| 285 | goto out; |
| 286 | } |
| 287 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 288 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 289 | if (token == NULL) { |
| 290 | parse_error(ctxt, NULL, "premature EOI"); |
| 291 | goto out; |
| 292 | } |
| 293 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 294 | if (token->type != JSON_COLON) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 295 | parse_error(ctxt, token, "missing : in object pair"); |
| 296 | goto out; |
| 297 | } |
| 298 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 299 | value = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 300 | if (value == NULL) { |
| 301 | parse_error(ctxt, token, "Missing value in dict"); |
| 302 | goto out; |
| 303 | } |
| 304 | |
| 305 | qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value); |
| 306 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 307 | qobject_decref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | |
| 311 | out: |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 312 | qobject_decref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 313 | |
| 314 | return -1; |
| 315 | } |
| 316 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 317 | static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 318 | { |
| 319 | QDict *dict = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 320 | JSONToken *token, *peek; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 321 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 322 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 323 | assert(token && token->type == JSON_LCURLY); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 324 | |
| 325 | dict = qdict_new(); |
| 326 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 327 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 328 | if (peek == NULL) { |
| 329 | parse_error(ctxt, NULL, "premature EOI"); |
| 330 | goto out; |
| 331 | } |
| 332 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 333 | if (peek->type != JSON_RCURLY) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 334 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 335 | goto out; |
| 336 | } |
| 337 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 338 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 339 | if (token == NULL) { |
| 340 | parse_error(ctxt, NULL, "premature EOI"); |
| 341 | goto out; |
| 342 | } |
| 343 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 344 | while (token->type != JSON_RCURLY) { |
| 345 | if (token->type != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 346 | parse_error(ctxt, token, "expected separator in dict"); |
| 347 | goto out; |
| 348 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 349 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 350 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 351 | goto out; |
| 352 | } |
| 353 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 354 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 355 | if (token == NULL) { |
| 356 | parse_error(ctxt, NULL, "premature EOI"); |
| 357 | goto out; |
| 358 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 359 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 360 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 361 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 362 | } |
| 363 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 364 | return QOBJECT(dict); |
| 365 | |
| 366 | out: |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 367 | QDECREF(dict); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 371 | static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 372 | { |
| 373 | QList *list = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 374 | JSONToken *token, *peek; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 375 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 376 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 377 | assert(token && token->type == JSON_LSQUARE); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 378 | |
| 379 | list = qlist_new(); |
| 380 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 381 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 382 | if (peek == NULL) { |
| 383 | parse_error(ctxt, NULL, "premature EOI"); |
| 384 | goto out; |
| 385 | } |
| 386 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 387 | if (peek->type != JSON_RSQUARE) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 388 | QObject *obj; |
| 389 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 390 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 391 | if (obj == NULL) { |
| 392 | parse_error(ctxt, token, "expecting value"); |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | qlist_append_obj(list, obj); |
| 397 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 398 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 399 | if (token == NULL) { |
| 400 | parse_error(ctxt, NULL, "premature EOI"); |
| 401 | goto out; |
| 402 | } |
| 403 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 404 | while (token->type != JSON_RSQUARE) { |
| 405 | if (token->type != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 406 | parse_error(ctxt, token, "expected separator in list"); |
| 407 | goto out; |
| 408 | } |
| 409 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 410 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 411 | if (obj == NULL) { |
| 412 | parse_error(ctxt, token, "expecting value"); |
| 413 | goto out; |
| 414 | } |
| 415 | |
| 416 | qlist_append_obj(list, obj); |
| 417 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 418 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 419 | if (token == NULL) { |
| 420 | parse_error(ctxt, NULL, "premature EOI"); |
| 421 | goto out; |
| 422 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 423 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 424 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 425 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 426 | } |
| 427 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 428 | return QOBJECT(list); |
| 429 | |
| 430 | out: |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 431 | QDECREF(list); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 435 | static QObject *parse_keyword(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 436 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 437 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 438 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 439 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 440 | assert(token && token->type == JSON_KEYWORD); |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 441 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 442 | if (!strcmp(token->str, "true")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 443 | return QOBJECT(qbool_from_bool(true)); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 444 | } else if (!strcmp(token->str, "false")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 445 | return QOBJECT(qbool_from_bool(false)); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 446 | } else if (!strcmp(token->str, "null")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 447 | return qnull(); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 448 | } |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 449 | parse_error(ctxt, token, "invalid keyword '%s'", token->str); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
| 452 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 453 | static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 454 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 455 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 456 | |
| 457 | if (ap == NULL) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 458 | return NULL; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 459 | } |
| 460 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 461 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 462 | assert(token && token->type == JSON_ESCAPE); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 463 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 464 | if (!strcmp(token->str, "%p")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 465 | return va_arg(*ap, QObject *); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 466 | } else if (!strcmp(token->str, "%i")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 467 | return QOBJECT(qbool_from_bool(va_arg(*ap, int))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 468 | } else if (!strcmp(token->str, "%d")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 469 | return QOBJECT(qint_from_int(va_arg(*ap, int))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 470 | } else if (!strcmp(token->str, "%ld")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 471 | return QOBJECT(qint_from_int(va_arg(*ap, long))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 472 | } else if (!strcmp(token->str, "%lld") || |
| 473 | !strcmp(token->str, "%I64d")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 474 | return QOBJECT(qint_from_int(va_arg(*ap, long long))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 475 | } else if (!strcmp(token->str, "%s")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 476 | return QOBJECT(qstring_from_str(va_arg(*ap, const char *))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 477 | } else if (!strcmp(token->str, "%f")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 478 | return QOBJECT(qfloat_from_double(va_arg(*ap, double))); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 479 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 480 | return NULL; |
| 481 | } |
| 482 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 483 | static QObject *parse_literal(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 484 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 485 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 486 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 487 | token = parser_context_pop_token(ctxt); |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 488 | assert(token); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 489 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 490 | switch (token->type) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 491 | case JSON_STRING: |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 492 | return QOBJECT(qstring_from_escaped_str(ctxt, token)); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 493 | case JSON_INTEGER: { |
| 494 | /* A possibility exists that this is a whole-valued float where the |
| 495 | * fractional part was left out due to being 0 (.0). It's not a big |
| 496 | * deal to treat these as ints in the parser, so long as users of the |
| 497 | * resulting QObject know to expect a QInt in place of a QFloat in |
| 498 | * cases like these. |
| 499 | * |
| 500 | * However, in some cases these values will overflow/underflow a |
| 501 | * QInt/int64 container, thus we should assume these are to be handled |
| 502 | * as QFloats/doubles rather than silently changing their values. |
| 503 | * |
| 504 | * strtoll() indicates these instances by setting errno to ERANGE |
| 505 | */ |
| 506 | int64_t value; |
| 507 | |
| 508 | errno = 0; /* strtoll doesn't set errno on success */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 509 | value = strtoll(token->str, NULL, 10); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 510 | if (errno != ERANGE) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 511 | return QOBJECT(qint_from_int(value)); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 512 | } |
| 513 | /* fall through to JSON_FLOAT */ |
| 514 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 515 | case JSON_FLOAT: |
Eric Blake | 6e8e5cb | 2016-01-29 06:48:37 -0700 | [diff] [blame] | 516 | /* FIXME dependent on locale; a pervasive issue in QEMU */ |
| 517 | /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN, |
| 518 | * but those might be useful extensions beyond JSON */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 519 | return QOBJECT(qfloat_from_double(strtod(token->str, NULL))); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 520 | default: |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 521 | abort(); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 522 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 523 | } |
| 524 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 525 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 526 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 527 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 528 | |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 529 | token = parser_context_peek_token(ctxt); |
| 530 | if (token == NULL) { |
| 531 | parse_error(ctxt, NULL, "premature EOI"); |
| 532 | return NULL; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 533 | } |
| 534 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 535 | switch (token->type) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 536 | case JSON_LCURLY: |
| 537 | return parse_object(ctxt, ap); |
| 538 | case JSON_LSQUARE: |
| 539 | return parse_array(ctxt, ap); |
| 540 | case JSON_ESCAPE: |
| 541 | return parse_escape(ctxt, ap); |
| 542 | case JSON_INTEGER: |
| 543 | case JSON_FLOAT: |
| 544 | case JSON_STRING: |
| 545 | return parse_literal(ctxt); |
| 546 | case JSON_KEYWORD: |
| 547 | return parse_keyword(ctxt); |
| 548 | default: |
| 549 | parse_error(ctxt, token, "expecting value"); |
| 550 | return NULL; |
| 551 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 552 | } |
| 553 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 554 | QObject *json_parser_parse(GQueue *tokens, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 555 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 556 | return json_parser_parse_err(tokens, ap, NULL); |
| 557 | } |
| 558 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 559 | QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp) |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 560 | { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 561 | JSONParserContext *ctxt = parser_context_new(tokens); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 562 | QObject *result; |
| 563 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 564 | if (!ctxt) { |
Michael Roth | c1990eb | 2011-06-01 12:15:00 -0500 | [diff] [blame] | 565 | return NULL; |
| 566 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 567 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 568 | result = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 569 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 570 | error_propagate(errp, ctxt->err); |
| 571 | |
| 572 | parser_context_free(ctxt); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 573 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 574 | return result; |
| 575 | } |