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