/src/qemu/qobject/json-parser.c
Line | Count | Source |
1 | | /* |
2 | | * JSON Parser |
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 | | |
14 | | #include "qemu/osdep.h" |
15 | | #include "qemu/ctype.h" |
16 | | #include "qemu/cutils.h" |
17 | | #include "qemu/unicode.h" |
18 | | #include "qapi/error.h" |
19 | | #include "qobject/qbool.h" |
20 | | #include "qobject/qdict.h" |
21 | | #include "qobject/qlist.h" |
22 | | #include "qobject/qnull.h" |
23 | | #include "qobject/qnum.h" |
24 | | #include "qobject/qstring.h" |
25 | | #include "json-parser-int.h" |
26 | | |
27 | | /* |
28 | | * The JSON parser is a push parser, returning a completed top-level |
29 | | * object, an error, or NULL (if the object is incomplete and no error |
30 | | * happened) after every token. Therefore it has an explicit |
31 | | * representation of its parser stack; each stack entry consists of a |
32 | | * parser state and a QObject: |
33 | | * - a QList, for an array that is being added to |
34 | | * - a QDict, for a dictionary that is being added to |
35 | | * - a QString, for the key of the next pair that will be added to a QDict |
36 | | * |
37 | | * The stack represents an arbitrary nesting of arrays and dictionaries |
38 | | * (whose next key has been parsed); it can also have a dictionary whose |
39 | | * next key has not been parsed, but that can only happen at the top level. |
40 | | * Because of this, the stack contents are always of the form |
41 | | * "(QList | QDict QString)* QDict?". |
42 | | * |
43 | | * An empty stack represents the beginning of the parsing process, with |
44 | | * start state BEFORE_VALUE. |
45 | | */ |
46 | | |
47 | | typedef enum JSONParserState { |
48 | | AFTER_LCURLY, |
49 | | AFTER_LSQUARE, |
50 | | BEFORE_KEY, |
51 | | BEFORE_VALUE, |
52 | | END_OF_KEY, |
53 | | END_OF_VALUE, |
54 | | } JSONParserState; |
55 | | |
56 | | typedef struct JSONParserStackEntry { |
57 | | /* |
58 | | * State when the container is completed or, for the top of the stack, |
59 | | * entry state for the next token. |
60 | | */ |
61 | | JSONParserState state; |
62 | | |
63 | | /* |
64 | | * A QString with the last parsed key, or a QList/QDict for the current |
65 | | * container. |
66 | | */ |
67 | | QObject *partial; |
68 | | } JSONParserStackEntry; |
69 | | |
70 | | /* |
71 | | * This is the JSON grammar that's parsed, with the state transition and |
72 | | * action at each point of the grammar. While this is not a formal |
73 | | * description, "-> action" represents the pseudocode of the action |
74 | | * and "-> STATE" sets the top stack entry's state to STATE. |
75 | | * |
76 | | * The state alone is enough to tell you what to parse; the state plus |
77 | | * the type of the top of stack tells you which action to take. |
78 | | * |
79 | | * // The initial state is BEFORE_VALUE. |
80 | | * input := value -> END_OF_VALUE -> return parsed value |
81 | | * (input | END_OF_INPUT) |
82 | | * |
83 | | * // entered on BEFORE_VALUE; after any of these rules are processed, the |
84 | | * // parser has completed a QObject and is in the END_OF_VALUE state. |
85 | | * // |
86 | | * // When the parser reaches the END_OF_VALUE state, it examines the |
87 | | * // top of the stack to see if it's coming from "input" (stack empty), |
88 | | * // "array_items" (TOS is a QList) or "dict_pairs" (TOS is a QString; the |
89 | | * // item below will be a QDict). It then proceeds with the corresponding |
90 | | * // actions, which will be one of: |
91 | | * // - return parsed value |
92 | | * // - add value to QList |
93 | | * // - pop QString with the key, add key/value to the QDict |
94 | | * value := literal -> END_OF_VALUE |
95 | | * | '[' -> push empty QList -> AFTER_LSQUARE |
96 | | * after_lsquare -> END_OF_VALUE |
97 | | * | '{' -> push empty QDict -> AFTER_LCURLY |
98 | | * after_lcurly -> END_OF_VALUE |
99 | | * |
100 | | * // non-recursive values, entered on BEFORE_VALUE |
101 | | * literal := INTEGER -> END_OF_VALUE |
102 | | * | FLOAT -> END_OF_VALUE |
103 | | * | KEYWORD -> END_OF_VALUE |
104 | | * | STRING -> END_OF_VALUE |
105 | | * | INTERP -> END_OF_VALUE |
106 | | * |
107 | | * // entered on AFTER_LSQUARE |
108 | | * after_lsquare := ']' -> pop completed QList -> END_OF_VALUE |
109 | | * | ϵ -> BEFORE_VALUE |
110 | | * array_items -> END_OF_VALUE |
111 | | * |
112 | | * // entered on BEFORE_VALUE, with TOS being a QList |
113 | | * array_items := value -> add value to QList -> END_OF_VALUE |
114 | | * (']' -> pop completed QList -> END_OF_VALUE |
115 | | * | ',' -> BEFORE_VALUE |
116 | | * array_items) -> END_OF_VALUE |
117 | | * |
118 | | * // entered on AFTER_LCURLY |
119 | | * after_lcurly := '}' -> pop completed QDict -> END_OF_VALUE |
120 | | * | ϵ -> BEFORE_KEY |
121 | | * dict_pairs -> END_OF_VALUE |
122 | | * |
123 | | * // entered on BEFORE_KEY, with TOS being a QDict |
124 | | * dict_pairs := (STRING | INTERP) -> push QString -> END_OF_KEY |
125 | | * ':' -> BEFORE_VALUE |
126 | | * value -> pop QString + add pair to QDict -> END_OF_VALUE |
127 | | * ('}' -> pop completed QDict -> END_OF_VALUE |
128 | | * | ',' -> BEFORE_KEY |
129 | | * dict_pairs) -> END_OF_VALUE |
130 | | * |
131 | | * Parse errors ignore the token. json_parser_reset() can be |
132 | | * called to restart parsing from scratch, with an empty stack. |
133 | | */ |
134 | | |
135 | | #define BUG_ON(cond) assert(!(cond)) |
136 | | |
137 | | static inline JSONParserStackEntry *current_entry(JSONParserContext *ctxt) |
138 | 2.01M | { |
139 | 2.01M | return g_queue_peek_tail(ctxt->stack); |
140 | 2.01M | } |
141 | | |
142 | | static void push_entry(JSONParserContext *ctxt, QObject *partial, |
143 | | JSONParserState state) |
144 | 504k | { |
145 | 504k | JSONParserStackEntry *entry = g_new(JSONParserStackEntry, 1); |
146 | 504k | entry->partial = partial; |
147 | 504k | entry->state = state; |
148 | 504k | g_queue_push_tail(ctxt->stack, entry); |
149 | 504k | } |
150 | | |
151 | | /* Drop the top entry and return the new top entry. */ |
152 | | static JSONParserStackEntry *pop_entry(JSONParserContext *ctxt) |
153 | 504k | { |
154 | 504k | JSONParserStackEntry *entry = g_queue_pop_tail(ctxt->stack); |
155 | 504k | g_free(entry); |
156 | 504k | return current_entry(ctxt); |
157 | 504k | } |
158 | | |
159 | | /** |
160 | | * Error handler |
161 | | */ |
162 | | static void G_GNUC_PRINTF(3, 4) parse_error(JSONParserContext *ctxt, |
163 | | const JSONToken *token, |
164 | | const char *msg, ...) |
165 | 0 | { |
166 | 0 | va_list ap; |
167 | 0 | char message[1024]; |
168 | |
|
169 | 0 | if (ctxt->err) { |
170 | 0 | return; |
171 | 0 | } |
172 | 0 | va_start(ap, msg); |
173 | 0 | vsnprintf(message, sizeof(message), msg, ap); |
174 | 0 | va_end(ap); |
175 | 0 | error_setg(&ctxt->err, "%d:%d: JSON parse error, %s", |
176 | 0 | token->y, token->x, message); |
177 | 0 | } |
178 | | |
179 | | static int cvt4hex(const char *s) |
180 | 0 | { |
181 | 0 | int cp, i; |
182 | |
|
183 | 0 | cp = 0; |
184 | 0 | for (i = 0; i < 4; i++) { |
185 | 0 | if (!qemu_isxdigit(s[i])) { |
186 | 0 | return -1; |
187 | 0 | } |
188 | 0 | cp <<= 4; |
189 | 0 | if (s[i] >= '0' && s[i] <= '9') { |
190 | 0 | cp |= s[i] - '0'; |
191 | 0 | } else if (s[i] >= 'a' && s[i] <= 'f') { |
192 | 0 | cp |= 10 + s[i] - 'a'; |
193 | 0 | } else if (s[i] >= 'A' && s[i] <= 'F') { |
194 | 0 | cp |= 10 + s[i] - 'A'; |
195 | 0 | } else { |
196 | 0 | return -1; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | return cp; |
200 | 0 | } |
201 | | |
202 | | /** |
203 | | * parse_string(): Parse a JSON string |
204 | | * |
205 | | * From RFC 8259 "The JavaScript Object Notation (JSON) Data |
206 | | * Interchange Format": |
207 | | * |
208 | | * char = unescaped / |
209 | | * escape ( |
210 | | * %x22 / ; " quotation mark U+0022 |
211 | | * %x5C / ; \ reverse solidus U+005C |
212 | | * %x2F / ; / solidus U+002F |
213 | | * %x62 / ; b backspace U+0008 |
214 | | * %x66 / ; f form feed U+000C |
215 | | * %x6E / ; n line feed U+000A |
216 | | * %x72 / ; r carriage return U+000D |
217 | | * %x74 / ; t tab U+0009 |
218 | | * %x75 4HEXDIG ) ; uXXXX U+XXXX |
219 | | * escape = %x5C ; \ |
220 | | * quotation-mark = %x22 ; " |
221 | | * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF |
222 | | * |
223 | | * Extensions over RFC 8259: |
224 | | * - Extra escape sequence in strings: |
225 | | * 0x27 (apostrophe) is recognized after escape, too |
226 | | * - Single-quoted strings: |
227 | | * Like double-quoted strings, except they're delimited by %x27 |
228 | | * (apostrophe) instead of %x22 (quotation mark), and can't contain |
229 | | * unescaped apostrophe, but can contain unescaped quotation mark. |
230 | | * |
231 | | * Note: |
232 | | * - Encoding is modified UTF-8. |
233 | | * - Invalid Unicode characters are rejected. |
234 | | * - Control characters \x00..\x1F are rejected by the lexer. |
235 | | */ |
236 | | static QString *parse_string(JSONParserContext *ctxt, const JSONToken *token) |
237 | 336k | { |
238 | 336k | const char *ptr = token->str; |
239 | 336k | GString *str; |
240 | 336k | char quote; |
241 | 336k | const char *beg; |
242 | 336k | int cp, trailing; |
243 | 336k | char *end; |
244 | 336k | ssize_t len; |
245 | 336k | char utf8_buf[5]; |
246 | | |
247 | 336k | assert(*ptr == '"' || *ptr == '\''); |
248 | 336k | quote = *ptr++; |
249 | 336k | str = g_string_new(NULL); |
250 | | |
251 | 3.53M | while (*ptr != quote) { |
252 | 3.19M | assert(*ptr); |
253 | 3.19M | switch (*ptr) { |
254 | 0 | case '\\': |
255 | 0 | beg = ptr++; |
256 | 0 | switch (*ptr++) { |
257 | 0 | case '"': |
258 | 0 | g_string_append_c(str, '"'); |
259 | 0 | break; |
260 | 0 | case '\'': |
261 | 0 | g_string_append_c(str, '\''); |
262 | 0 | break; |
263 | 0 | case '\\': |
264 | 0 | g_string_append_c(str, '\\'); |
265 | 0 | break; |
266 | 0 | case '/': |
267 | 0 | g_string_append_c(str, '/'); |
268 | 0 | break; |
269 | 0 | case 'b': |
270 | 0 | g_string_append_c(str, '\b'); |
271 | 0 | break; |
272 | 0 | case 'f': |
273 | 0 | g_string_append_c(str, '\f'); |
274 | 0 | break; |
275 | 0 | case 'n': |
276 | 0 | g_string_append_c(str, '\n'); |
277 | 0 | break; |
278 | 0 | case 'r': |
279 | 0 | g_string_append_c(str, '\r'); |
280 | 0 | break; |
281 | 0 | case 't': |
282 | 0 | g_string_append_c(str, '\t'); |
283 | 0 | break; |
284 | 0 | case 'u': |
285 | 0 | cp = cvt4hex(ptr); |
286 | 0 | ptr += 4; |
287 | | |
288 | | /* handle surrogate pairs */ |
289 | 0 | if (cp >= 0xD800 && cp <= 0xDBFF |
290 | 0 | && ptr[0] == '\\' && ptr[1] == 'u') { |
291 | | /* leading surrogate followed by \u */ |
292 | 0 | cp = 0x10000 + ((cp & 0x3FF) << 10); |
293 | 0 | trailing = cvt4hex(ptr + 2); |
294 | 0 | if (trailing >= 0xDC00 && trailing <= 0xDFFF) { |
295 | | /* followed by trailing surrogate */ |
296 | 0 | cp |= trailing & 0x3FF; |
297 | 0 | ptr += 6; |
298 | 0 | } else { |
299 | 0 | cp = -1; /* invalid */ |
300 | 0 | } |
301 | 0 | } |
302 | |
|
303 | 0 | if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) { |
304 | 0 | parse_error(ctxt, token, |
305 | 0 | "%.*s is not a valid Unicode character", |
306 | 0 | (int)(ptr - beg), beg); |
307 | 0 | goto out; |
308 | 0 | } |
309 | 0 | g_string_append(str, utf8_buf); |
310 | 0 | break; |
311 | 0 | default: |
312 | 0 | parse_error(ctxt, token, "invalid escape sequence in string"); |
313 | 0 | goto out; |
314 | 0 | } |
315 | 0 | break; |
316 | 0 | case '%': |
317 | 0 | if (ctxt->ap) { |
318 | 0 | if (ptr[1] != '%') { |
319 | 0 | parse_error(ctxt, token, "can't interpolate into string"); |
320 | 0 | goto out; |
321 | 0 | } |
322 | 0 | ptr++; |
323 | 0 | } |
324 | | /* fall through */ |
325 | 3.19M | default: |
326 | 3.19M | cp = mod_utf8_codepoint(ptr, 6, &end); |
327 | 3.19M | if (cp < 0) { |
328 | 0 | parse_error(ctxt, token, "invalid UTF-8 sequence in string"); |
329 | 0 | goto out; |
330 | 0 | } |
331 | 3.19M | ptr = end; |
332 | 3.19M | len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp); |
333 | 3.19M | assert(len >= 0); |
334 | 3.19M | g_string_append(str, utf8_buf); |
335 | 3.19M | } |
336 | 3.19M | } |
337 | | |
338 | 336k | return qstring_from_gstring(str); |
339 | | |
340 | 0 | out: |
341 | 0 | g_string_free(str, true); |
342 | 0 | return NULL; |
343 | 336k | } |
344 | | |
345 | | /* Terminals */ |
346 | | |
347 | | static QObject *parse_keyword(JSONParserContext *ctxt, const JSONToken *token) |
348 | 0 | { |
349 | 0 | assert(token && token->type == JSON_KEYWORD); |
350 | |
|
351 | 0 | if (!strcmp(token->str, "true")) { |
352 | 0 | return QOBJECT(qbool_from_bool(true)); |
353 | 0 | } else if (!strcmp(token->str, "false")) { |
354 | 0 | return QOBJECT(qbool_from_bool(false)); |
355 | 0 | } else if (!strcmp(token->str, "null")) { |
356 | 0 | return QOBJECT(qnull()); |
357 | 0 | } |
358 | 0 | parse_error(ctxt, token, "invalid keyword '%s'", token->str); |
359 | 0 | return NULL; |
360 | 0 | } |
361 | | |
362 | | static QObject *parse_interpolation(JSONParserContext *ctxt, |
363 | | const JSONToken *token) |
364 | 336k | { |
365 | 336k | assert(token && token->type == JSON_INTERP); |
366 | | |
367 | 336k | if (!strcmp(token->str, "%p")) { |
368 | 0 | return va_arg(*ctxt->ap, QObject *); |
369 | 336k | } else if (!strcmp(token->str, "%i")) { |
370 | 0 | return QOBJECT(qbool_from_bool(va_arg(*ctxt->ap, int))); |
371 | 336k | } else if (!strcmp(token->str, "%d")) { |
372 | 0 | return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int))); |
373 | 336k | } else if (!strcmp(token->str, "%ld")) { |
374 | 0 | return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long))); |
375 | 336k | } else if (!strcmp(token->str, "%lld")) { |
376 | 336k | return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long long))); |
377 | 336k | } else if (!strcmp(token->str, "%" PRId64)) { |
378 | 0 | return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int64_t))); |
379 | 0 | } else if (!strcmp(token->str, "%u")) { |
380 | 0 | return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned int))); |
381 | 0 | } else if (!strcmp(token->str, "%lu")) { |
382 | 0 | return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long))); |
383 | 0 | } else if (!strcmp(token->str, "%llu")) { |
384 | 0 | return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long long))); |
385 | 0 | } else if (!strcmp(token->str, "%" PRIu64)) { |
386 | 0 | return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, uint64_t))); |
387 | 0 | } else if (!strcmp(token->str, "%s")) { |
388 | 0 | return QOBJECT(qstring_from_str(va_arg(*ctxt->ap, const char *))); |
389 | 0 | } else if (!strcmp(token->str, "%f")) { |
390 | 0 | return QOBJECT(qnum_from_double(va_arg(*ctxt->ap, double))); |
391 | 0 | } |
392 | 0 | parse_error(ctxt, token, "invalid interpolation '%s'", token->str); |
393 | 0 | return NULL; |
394 | 336k | } |
395 | | |
396 | | static QObject *parse_literal(JSONParserContext *ctxt, const JSONToken *token) |
397 | 336k | { |
398 | 336k | assert(token); |
399 | | |
400 | 336k | switch (token->type) { |
401 | 336k | case JSON_STRING: |
402 | 336k | return QOBJECT(parse_string(ctxt, token)); |
403 | 0 | case JSON_INTEGER: { |
404 | | /* |
405 | | * Represent JSON_INTEGER as QNUM_I64 if possible, else as |
406 | | * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64() |
407 | | * and qemu_strtou64() fail with ERANGE when it's not |
408 | | * possible. |
409 | | * |
410 | | * qnum_get_int() will then work for any signed 64-bit |
411 | | * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit |
412 | | * integer, and qnum_get_double() both for any JSON_INTEGER |
413 | | * and any JSON_FLOAT (with precision loss for integers beyond |
414 | | * 53 bits) |
415 | | */ |
416 | 0 | int ret; |
417 | 0 | int64_t value; |
418 | 0 | uint64_t uvalue; |
419 | |
|
420 | 0 | ret = qemu_strtoi64(token->str, NULL, 10, &value); |
421 | 0 | if (!ret) { |
422 | 0 | return QOBJECT(qnum_from_int(value)); |
423 | 0 | } |
424 | 0 | assert(ret == -ERANGE); |
425 | |
|
426 | 0 | if (token->str[0] != '-') { |
427 | 0 | ret = qemu_strtou64(token->str, NULL, 10, &uvalue); |
428 | 0 | if (!ret) { |
429 | 0 | return QOBJECT(qnum_from_uint(uvalue)); |
430 | 0 | } |
431 | 0 | assert(ret == -ERANGE); |
432 | 0 | } |
433 | 0 | } |
434 | | /* fall through to JSON_FLOAT */ |
435 | 0 | case JSON_FLOAT: |
436 | | /* FIXME dependent on locale; a pervasive issue in QEMU */ |
437 | | /* FIXME our lexer matches RFC 8259 in forbidding Inf or NaN, |
438 | | * but those might be useful extensions beyond JSON */ |
439 | 0 | return QOBJECT(qnum_from_double(strtod(token->str, NULL))); |
440 | 0 | default: |
441 | 0 | abort(); |
442 | 336k | } |
443 | 336k | } |
444 | | |
445 | | /* Parsing state machine */ |
446 | | |
447 | | static QObject *parse_begin_value(JSONParserContext *ctxt, |
448 | | const JSONToken *token) |
449 | 840k | { |
450 | 840k | switch (token->type) { |
451 | 168k | case JSON_LCURLY: |
452 | 168k | push_entry(ctxt, QOBJECT(qdict_new()), AFTER_LCURLY); |
453 | 168k | return NULL; |
454 | 0 | case JSON_LSQUARE: |
455 | 0 | push_entry(ctxt, QOBJECT(qlist_new()), AFTER_LSQUARE); |
456 | 0 | return NULL; |
457 | 336k | case JSON_INTERP: |
458 | 336k | return parse_interpolation(ctxt, token); |
459 | 0 | case JSON_INTEGER: |
460 | 0 | case JSON_FLOAT: |
461 | 336k | case JSON_STRING: |
462 | 336k | return parse_literal(ctxt, token); |
463 | 0 | case JSON_KEYWORD: |
464 | 0 | return parse_keyword(ctxt, token); |
465 | 0 | default: |
466 | 0 | parse_error(ctxt, token, "expecting value"); |
467 | 0 | return NULL; |
468 | 840k | } |
469 | 840k | } |
470 | | |
471 | | static QObject *parse_token(JSONParserContext *ctxt, const JSONToken *token) |
472 | 1.51M | { |
473 | 1.51M | JSONParserStackEntry *entry; |
474 | 1.51M | JSONParserState state; |
475 | 1.51M | QString *key; |
476 | 1.51M | QObject *key_obj = NULL, *value = NULL; |
477 | | |
478 | 1.51M | entry = current_entry(ctxt); |
479 | 1.51M | state = entry ? entry->state : BEFORE_VALUE; |
480 | 1.51M | switch (state) { |
481 | 168k | case AFTER_LCURLY: |
482 | | /* Grab '}' for empty object or fall through to BEFORE_KEY */ |
483 | 168k | assert(qobject_type(entry->partial) == QTYPE_QDICT); |
484 | 168k | if (token->type == JSON_RCURLY) { |
485 | 0 | value = entry->partial; |
486 | 0 | entry = pop_entry(ctxt); |
487 | 0 | break; |
488 | 0 | } |
489 | 168k | entry->state = BEFORE_KEY; |
490 | | /* fall through */ |
491 | | |
492 | 336k | case BEFORE_KEY: |
493 | | /* Expecting object key */ |
494 | 336k | assert(qobject_type(entry->partial) == QTYPE_QDICT); |
495 | 336k | if (token->type != JSON_STRING && token->type != JSON_INTERP) { |
496 | 0 | parse_error(ctxt, token, "expecting key"); |
497 | 0 | return NULL; |
498 | 0 | } |
499 | | |
500 | 336k | key_obj = parse_begin_value(ctxt, token); |
501 | 336k | if (!key_obj) { |
502 | | /* Parse error already reported */ |
503 | 336k | } else if (qobject_type(key_obj) != QTYPE_QSTRING) { |
504 | | /* An interpolation was valid syntactically but not %s */ |
505 | 0 | parse_error(ctxt, token, "key is not a string in object"); |
506 | 336k | } else { |
507 | | /* Store key in a special entry on the stack */ |
508 | 336k | push_entry(ctxt, key_obj, END_OF_KEY); |
509 | 336k | } |
510 | 336k | return NULL; |
511 | | |
512 | 336k | case END_OF_KEY: |
513 | | /* Expecting ':' after key */ |
514 | 336k | assert(qobject_type(entry->partial) == QTYPE_QSTRING); |
515 | 336k | if (token->type == JSON_COLON) { |
516 | 336k | entry->state = BEFORE_VALUE; |
517 | 336k | } else { |
518 | 0 | parse_error(ctxt, token, "expecting ':'"); |
519 | 0 | } |
520 | 336k | return NULL; |
521 | | |
522 | 0 | case AFTER_LSQUARE: |
523 | | /* Grab ']' for empty array or fall through to BEFORE_VALUE */ |
524 | 0 | assert(qobject_type(entry->partial) == QTYPE_QLIST); |
525 | 0 | if (token->type == JSON_RSQUARE) { |
526 | 0 | value = entry->partial; |
527 | 0 | entry = pop_entry(ctxt); |
528 | 0 | break; |
529 | 0 | } |
530 | 0 | entry->state = BEFORE_VALUE; |
531 | | /* fall through */ |
532 | |
|
533 | 504k | case BEFORE_VALUE: |
534 | | /* Expecting value */ |
535 | 504k | assert(!entry || qobject_type(entry->partial) != QTYPE_QDICT); |
536 | 504k | value = parse_begin_value(ctxt, token); |
537 | 504k | if (!value) { |
538 | | /* Error or '['/'{' */ |
539 | 168k | return NULL; |
540 | 168k | } |
541 | | /* Return value or insert it into a container */ |
542 | 336k | break; |
543 | | |
544 | 336k | case END_OF_VALUE: |
545 | | /* Grab ',' or ']' for array; ',' or '}' for object */ |
546 | 336k | if (qobject_to(QList, entry->partial)) { |
547 | | /* Array */ |
548 | 0 | if (token->type != JSON_RSQUARE) { |
549 | 0 | if (token->type == JSON_COMMA) { |
550 | 0 | entry->state = BEFORE_VALUE; |
551 | 0 | } else { |
552 | 0 | parse_error(ctxt, token, "expected ',' or ']'"); |
553 | 0 | } |
554 | 0 | return NULL; |
555 | 0 | } |
556 | 336k | } else if (qobject_to(QDict, entry->partial)) { |
557 | | /* Object */ |
558 | 336k | if (token->type != JSON_RCURLY) { |
559 | 168k | if (token->type == JSON_COMMA) { |
560 | 168k | entry->state = BEFORE_KEY; |
561 | 168k | } else { |
562 | 0 | parse_error(ctxt, token, "expected ',' or '}'"); |
563 | 0 | } |
564 | 168k | return NULL; |
565 | 168k | } |
566 | 336k | } else { |
567 | 0 | g_assert_not_reached(); |
568 | 0 | } |
569 | | |
570 | | /* Got ']' or '}'; return full value or insert into parent container */ |
571 | 168k | value = entry->partial; |
572 | 168k | entry = pop_entry(ctxt); |
573 | 168k | break; |
574 | 1.51M | } |
575 | | |
576 | 1.51M | assert(value); |
577 | 504k | if (entry == NULL) { |
578 | | /* Parse stack now empty, the top-level value is complete. */ |
579 | 168k | return value; |
580 | 168k | } |
581 | | |
582 | | /* |
583 | | * Parse stack is not empty and entry->partial is the top of stack. |
584 | | * It's a QString with the key (and a QDict is below it) if we're |
585 | | * parsing an object, or a QList if we're parsing an array. |
586 | | */ |
587 | 336k | key = qobject_to(QString, entry->partial); |
588 | 336k | if (key) { |
589 | 336k | const char *key_str; |
590 | 336k | QDict *dict; |
591 | | |
592 | | /* Pop off key, and store (key, value) in QDict. */ |
593 | 336k | entry = pop_entry(ctxt); |
594 | 336k | dict = qobject_to(QDict, entry->partial); |
595 | 336k | assert(dict); |
596 | 336k | key_str = qstring_get_str(key); |
597 | 336k | if (qdict_haskey(dict, key_str)) { |
598 | 0 | parse_error(ctxt, token, "duplicate key"); |
599 | 0 | qobject_unref(value); |
600 | 0 | return NULL; |
601 | 0 | } |
602 | 336k | qdict_put_obj(dict, key_str, value); |
603 | 336k | qobject_unref(key); |
604 | 336k | } else { |
605 | | /* Array, just store value in the QList. */ |
606 | 0 | qlist_append_obj(qobject_to(QList, entry->partial), value); |
607 | 0 | } |
608 | | |
609 | 336k | entry->state = END_OF_VALUE; |
610 | 336k | return NULL; |
611 | 336k | } |
612 | | |
613 | | |
614 | | void json_parser_reset(JSONParserContext *ctxt) |
615 | 672k | { |
616 | 672k | JSONParserStackEntry *entry; |
617 | | |
618 | 672k | ctxt->err = NULL; |
619 | 672k | while ((entry = g_queue_pop_tail(ctxt->stack)) != NULL) { |
620 | 0 | qobject_unref(entry->partial); |
621 | 0 | g_free(entry); |
622 | 0 | } |
623 | 672k | } |
624 | | |
625 | | void json_parser_init(JSONParserContext *ctxt, va_list *ap) |
626 | 168k | { |
627 | 168k | ctxt->stack = g_queue_new(); |
628 | 168k | ctxt->ap = ap; |
629 | 168k | json_parser_reset(ctxt); |
630 | 168k | } |
631 | | |
632 | | void json_parser_destroy(JSONParserContext *ctxt) |
633 | 168k | { |
634 | 168k | json_parser_reset(ctxt); |
635 | 168k | g_queue_free(ctxt->stack); |
636 | 168k | ctxt->stack = NULL; |
637 | 168k | } |
638 | | |
639 | | /* |
640 | | * Advance the parser based on the token that is passed. |
641 | | * Return the finished top-level value if the token completes it, else |
642 | | * NULL. |
643 | | * Once an error is returned, the function must not be called again |
644 | | * without first resetting the parser. |
645 | | */ |
646 | | QObject *json_parser_feed(JSONParserContext *ctxt, const JSONToken *token, |
647 | | Error **errp) |
648 | 1.68M | { |
649 | 1.68M | QObject *result = NULL; |
650 | | |
651 | 1.68M | assert(!ctxt->err); |
652 | 1.68M | switch (token->type) { |
653 | 0 | case JSON_ERROR: |
654 | 0 | parse_error(ctxt, token, "stray '%s'", token->str); |
655 | 0 | break; |
656 | | |
657 | 168k | case JSON_END_OF_INPUT: |
658 | | /* Check for premature end of input */ |
659 | 168k | if (!g_queue_is_empty(ctxt->stack)) { |
660 | 0 | parse_error(ctxt, token, "premature end of input"); |
661 | 0 | } |
662 | 168k | break; |
663 | | |
664 | 1.51M | default: |
665 | 1.51M | result = parse_token(ctxt, token); |
666 | 1.51M | break; |
667 | 1.68M | } |
668 | | |
669 | 1.68M | error_propagate(errp, ctxt->err); |
670 | 1.68M | return result; |
671 | 1.68M | } |