root / json-parser.c @ c05c7a73
History | View | Annotate | Download (12.8 kB)
1 | 4a5fcab7 | Anthony Liguori | /*
|
---|---|---|---|
2 | 4a5fcab7 | Anthony Liguori | * JSON Parser
|
3 | 4a5fcab7 | Anthony Liguori | *
|
4 | 4a5fcab7 | Anthony Liguori | * Copyright IBM, Corp. 2009
|
5 | 4a5fcab7 | Anthony Liguori | *
|
6 | 4a5fcab7 | Anthony Liguori | * Authors:
|
7 | 4a5fcab7 | Anthony Liguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | 4a5fcab7 | Anthony Liguori | *
|
9 | 4a5fcab7 | Anthony Liguori | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
10 | 4a5fcab7 | Anthony Liguori | * See the COPYING.LIB file in the top-level directory.
|
11 | 4a5fcab7 | Anthony Liguori | *
|
12 | 4a5fcab7 | Anthony Liguori | */
|
13 | 4a5fcab7 | Anthony Liguori | |
14 | 4a5fcab7 | Anthony Liguori | #include <stdbool.h> |
15 | 4a5fcab7 | Anthony Liguori | |
16 | 4a5fcab7 | Anthony Liguori | #include "qemu-common.h" |
17 | 4a5fcab7 | Anthony Liguori | #include "qstring.h" |
18 | 4a5fcab7 | Anthony Liguori | #include "qint.h" |
19 | 4a5fcab7 | Anthony Liguori | #include "qdict.h" |
20 | 4a5fcab7 | Anthony Liguori | #include "qlist.h" |
21 | 4a5fcab7 | Anthony Liguori | #include "qfloat.h" |
22 | 4a5fcab7 | Anthony Liguori | #include "qbool.h" |
23 | 4a5fcab7 | Anthony Liguori | #include "json-parser.h" |
24 | 4a5fcab7 | Anthony Liguori | #include "json-lexer.h" |
25 | 4a5fcab7 | Anthony Liguori | |
26 | 4a5fcab7 | Anthony Liguori | typedef struct JSONParserContext |
27 | 4a5fcab7 | Anthony Liguori | { |
28 | 4a5fcab7 | Anthony Liguori | } JSONParserContext; |
29 | 4a5fcab7 | Anthony Liguori | |
30 | 4a5fcab7 | Anthony Liguori | #define BUG_ON(cond) assert(!(cond))
|
31 | 4a5fcab7 | Anthony Liguori | |
32 | 4a5fcab7 | Anthony Liguori | /**
|
33 | 4a5fcab7 | Anthony Liguori | * TODO
|
34 | 4a5fcab7 | Anthony Liguori | *
|
35 | 4a5fcab7 | Anthony Liguori | * 0) make errors meaningful again
|
36 | 4a5fcab7 | Anthony Liguori | * 1) add geometry information to tokens
|
37 | 4a5fcab7 | Anthony Liguori | * 3) should we return a parsed size?
|
38 | 4a5fcab7 | Anthony Liguori | * 4) deal with premature EOI
|
39 | 4a5fcab7 | Anthony Liguori | */
|
40 | 4a5fcab7 | Anthony Liguori | |
41 | 4a5fcab7 | Anthony Liguori | static QObject *parse_value(JSONParserContext *ctxt, QList **tokens, va_list *ap);
|
42 | 4a5fcab7 | Anthony Liguori | |
43 | 4a5fcab7 | Anthony Liguori | /**
|
44 | 4a5fcab7 | Anthony Liguori | * Token manipulators
|
45 | 4a5fcab7 | Anthony Liguori | *
|
46 | 4a5fcab7 | Anthony Liguori | * tokens are dictionaries that contain a type, a string value, and geometry information
|
47 | 4a5fcab7 | Anthony Liguori | * about a token identified by the lexer. These are routines that make working with
|
48 | 4a5fcab7 | Anthony Liguori | * these objects a bit easier.
|
49 | 4a5fcab7 | Anthony Liguori | */
|
50 | 4a5fcab7 | Anthony Liguori | static const char *token_get_value(QObject *obj) |
51 | 4a5fcab7 | Anthony Liguori | { |
52 | 4a5fcab7 | Anthony Liguori | return qdict_get_str(qobject_to_qdict(obj), "token"); |
53 | 4a5fcab7 | Anthony Liguori | } |
54 | 4a5fcab7 | Anthony Liguori | |
55 | 4a5fcab7 | Anthony Liguori | static JSONTokenType token_get_type(QObject *obj)
|
56 | 4a5fcab7 | Anthony Liguori | { |
57 | 4a5fcab7 | Anthony Liguori | return qdict_get_int(qobject_to_qdict(obj), "type"); |
58 | 4a5fcab7 | Anthony Liguori | } |
59 | 4a5fcab7 | Anthony Liguori | |
60 | 4a5fcab7 | Anthony Liguori | static int token_is_operator(QObject *obj, char op) |
61 | 4a5fcab7 | Anthony Liguori | { |
62 | 4a5fcab7 | Anthony Liguori | const char *val; |
63 | 4a5fcab7 | Anthony Liguori | |
64 | 4a5fcab7 | Anthony Liguori | if (token_get_type(obj) != JSON_OPERATOR) {
|
65 | 4a5fcab7 | Anthony Liguori | return 0; |
66 | 4a5fcab7 | Anthony Liguori | } |
67 | 4a5fcab7 | Anthony Liguori | |
68 | 4a5fcab7 | Anthony Liguori | val = token_get_value(obj); |
69 | 4a5fcab7 | Anthony Liguori | |
70 | 4a5fcab7 | Anthony Liguori | return (val[0] == op) && (val[1] == 0); |
71 | 4a5fcab7 | Anthony Liguori | } |
72 | 4a5fcab7 | Anthony Liguori | |
73 | 4a5fcab7 | Anthony Liguori | static int token_is_keyword(QObject *obj, const char *value) |
74 | 4a5fcab7 | Anthony Liguori | { |
75 | 4a5fcab7 | Anthony Liguori | if (token_get_type(obj) != JSON_KEYWORD) {
|
76 | 4a5fcab7 | Anthony Liguori | return 0; |
77 | 4a5fcab7 | Anthony Liguori | } |
78 | 4a5fcab7 | Anthony Liguori | |
79 | 4a5fcab7 | Anthony Liguori | return strcmp(token_get_value(obj), value) == 0; |
80 | 4a5fcab7 | Anthony Liguori | } |
81 | 4a5fcab7 | Anthony Liguori | |
82 | 4a5fcab7 | Anthony Liguori | static int token_is_escape(QObject *obj, const char *value) |
83 | 4a5fcab7 | Anthony Liguori | { |
84 | 4a5fcab7 | Anthony Liguori | if (token_get_type(obj) != JSON_ESCAPE) {
|
85 | 4a5fcab7 | Anthony Liguori | return 0; |
86 | 4a5fcab7 | Anthony Liguori | } |
87 | 4a5fcab7 | Anthony Liguori | |
88 | 4a5fcab7 | Anthony Liguori | return (strcmp(token_get_value(obj), value) == 0); |
89 | 4a5fcab7 | Anthony Liguori | } |
90 | 4a5fcab7 | Anthony Liguori | |
91 | 4a5fcab7 | Anthony Liguori | /**
|
92 | 4a5fcab7 | Anthony Liguori | * Error handler
|
93 | 4a5fcab7 | Anthony Liguori | */
|
94 | 4a5fcab7 | Anthony Liguori | static void parse_error(JSONParserContext *ctxt, QObject *token, const char *msg, ...) |
95 | 4a5fcab7 | Anthony Liguori | { |
96 | 4a5fcab7 | Anthony Liguori | fprintf(stderr, "parse error: %s\n", msg);
|
97 | 4a5fcab7 | Anthony Liguori | } |
98 | 4a5fcab7 | Anthony Liguori | |
99 | 4a5fcab7 | Anthony Liguori | /**
|
100 | 4a5fcab7 | Anthony Liguori | * String helpers
|
101 | 4a5fcab7 | Anthony Liguori | *
|
102 | 4a5fcab7 | Anthony Liguori | * These helpers are used to unescape strings.
|
103 | 4a5fcab7 | Anthony Liguori | */
|
104 | 4a5fcab7 | Anthony Liguori | static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length) |
105 | 4a5fcab7 | Anthony Liguori | { |
106 | 4a5fcab7 | Anthony Liguori | if (wchar <= 0x007F) { |
107 | 4a5fcab7 | Anthony Liguori | BUG_ON(buffer_length < 2);
|
108 | 4a5fcab7 | Anthony Liguori | |
109 | 4a5fcab7 | Anthony Liguori | buffer[0] = wchar & 0x7F; |
110 | 4a5fcab7 | Anthony Liguori | buffer[1] = 0; |
111 | 4a5fcab7 | Anthony Liguori | } else if (wchar <= 0x07FF) { |
112 | 4a5fcab7 | Anthony Liguori | BUG_ON(buffer_length < 3);
|
113 | 4a5fcab7 | Anthony Liguori | |
114 | 4a5fcab7 | Anthony Liguori | buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F); |
115 | 4a5fcab7 | Anthony Liguori | buffer[1] = 0x80 | (wchar & 0x3F); |
116 | 4a5fcab7 | Anthony Liguori | buffer[2] = 0; |
117 | 4a5fcab7 | Anthony Liguori | } else {
|
118 | 4a5fcab7 | Anthony Liguori | BUG_ON(buffer_length < 4);
|
119 | 4a5fcab7 | Anthony Liguori | |
120 | 4a5fcab7 | Anthony Liguori | buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F); |
121 | 4a5fcab7 | Anthony Liguori | buffer[1] = 0x80 | ((wchar >> 6) & 0x3F); |
122 | 4a5fcab7 | Anthony Liguori | buffer[2] = 0x80 | (wchar & 0x3F); |
123 | 4a5fcab7 | Anthony Liguori | buffer[3] = 0; |
124 | 4a5fcab7 | Anthony Liguori | } |
125 | 4a5fcab7 | Anthony Liguori | } |
126 | 4a5fcab7 | Anthony Liguori | |
127 | 4a5fcab7 | Anthony Liguori | static int hex2decimal(char ch) |
128 | 4a5fcab7 | Anthony Liguori | { |
129 | 4a5fcab7 | Anthony Liguori | if (ch >= '0' && ch <= '9') { |
130 | 4a5fcab7 | Anthony Liguori | return (ch - '0'); |
131 | 4a5fcab7 | Anthony Liguori | } else if (ch >= 'a' && ch <= 'f') { |
132 | 4a5fcab7 | Anthony Liguori | return 10 + (ch - 'a'); |
133 | 4a5fcab7 | Anthony Liguori | } else if (ch >= 'A' && ch <= 'F') { |
134 | 4a5fcab7 | Anthony Liguori | return 10 + (ch - 'A'); |
135 | 4a5fcab7 | Anthony Liguori | } |
136 | 4a5fcab7 | Anthony Liguori | |
137 | 4a5fcab7 | Anthony Liguori | return -1; |
138 | 4a5fcab7 | Anthony Liguori | } |
139 | 4a5fcab7 | Anthony Liguori | |
140 | 4a5fcab7 | Anthony Liguori | /**
|
141 | 4a5fcab7 | Anthony Liguori | * parse_string(): Parse a json string and return a QObject
|
142 | 4a5fcab7 | Anthony Liguori | *
|
143 | 4a5fcab7 | Anthony Liguori | * string
|
144 | 4a5fcab7 | Anthony Liguori | * ""
|
145 | 4a5fcab7 | Anthony Liguori | * " chars "
|
146 | 4a5fcab7 | Anthony Liguori | * chars
|
147 | 4a5fcab7 | Anthony Liguori | * char
|
148 | 4a5fcab7 | Anthony Liguori | * char chars
|
149 | 4a5fcab7 | Anthony Liguori | * char
|
150 | 4a5fcab7 | Anthony Liguori | * any-Unicode-character-
|
151 | 4a5fcab7 | Anthony Liguori | * except-"-or-\-or-
|
152 | 4a5fcab7 | Anthony Liguori | * control-character
|
153 | 4a5fcab7 | Anthony Liguori | * \"
|
154 | 4a5fcab7 | Anthony Liguori | * \\
|
155 | 4a5fcab7 | Anthony Liguori | * \/
|
156 | 4a5fcab7 | Anthony Liguori | * \b
|
157 | 4a5fcab7 | Anthony Liguori | * \f
|
158 | 4a5fcab7 | Anthony Liguori | * \n
|
159 | 4a5fcab7 | Anthony Liguori | * \r
|
160 | 4a5fcab7 | Anthony Liguori | * \t
|
161 | 4a5fcab7 | Anthony Liguori | * \u four-hex-digits
|
162 | 4a5fcab7 | Anthony Liguori | */
|
163 | 4a5fcab7 | Anthony Liguori | static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *token)
|
164 | 4a5fcab7 | Anthony Liguori | { |
165 | 4a5fcab7 | Anthony Liguori | const char *ptr = token_get_value(token); |
166 | 4a5fcab7 | Anthony Liguori | QString *str; |
167 | 4a5fcab7 | Anthony Liguori | int double_quote = 1; |
168 | 4a5fcab7 | Anthony Liguori | |
169 | 4a5fcab7 | Anthony Liguori | if (*ptr == '"') { |
170 | 4a5fcab7 | Anthony Liguori | double_quote = 1;
|
171 | 4a5fcab7 | Anthony Liguori | } else {
|
172 | 4a5fcab7 | Anthony Liguori | double_quote = 0;
|
173 | 4a5fcab7 | Anthony Liguori | } |
174 | 4a5fcab7 | Anthony Liguori | ptr++; |
175 | 4a5fcab7 | Anthony Liguori | |
176 | 4a5fcab7 | Anthony Liguori | str = qstring_new(); |
177 | 4a5fcab7 | Anthony Liguori | while (*ptr &&
|
178 | 4a5fcab7 | Anthony Liguori | ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) { |
179 | 4a5fcab7 | Anthony Liguori | if (*ptr == '\\') { |
180 | 4a5fcab7 | Anthony Liguori | ptr++; |
181 | 4a5fcab7 | Anthony Liguori | |
182 | 4a5fcab7 | Anthony Liguori | switch (*ptr) {
|
183 | 4a5fcab7 | Anthony Liguori | case '"': |
184 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "\"");
|
185 | 4a5fcab7 | Anthony Liguori | ptr++; |
186 | 4a5fcab7 | Anthony Liguori | break;
|
187 | 4a5fcab7 | Anthony Liguori | case '\'': |
188 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "'");
|
189 | 4a5fcab7 | Anthony Liguori | ptr++; |
190 | 4a5fcab7 | Anthony Liguori | break;
|
191 | 4a5fcab7 | Anthony Liguori | case '\\': |
192 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "\\");
|
193 | 4a5fcab7 | Anthony Liguori | ptr++; |
194 | 4a5fcab7 | Anthony Liguori | break;
|
195 | 4a5fcab7 | Anthony Liguori | case '/': |
196 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "/");
|
197 | 4a5fcab7 | Anthony Liguori | ptr++; |
198 | 4a5fcab7 | Anthony Liguori | break;
|
199 | 4a5fcab7 | Anthony Liguori | case 'b': |
200 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "\b");
|
201 | 4a5fcab7 | Anthony Liguori | ptr++; |
202 | 4a5fcab7 | Anthony Liguori | break;
|
203 | 4a5fcab7 | Anthony Liguori | case 'n': |
204 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "\n");
|
205 | 4a5fcab7 | Anthony Liguori | ptr++; |
206 | 4a5fcab7 | Anthony Liguori | break;
|
207 | 4a5fcab7 | Anthony Liguori | case 'r': |
208 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "\r");
|
209 | 4a5fcab7 | Anthony Liguori | ptr++; |
210 | 4a5fcab7 | Anthony Liguori | break;
|
211 | 4a5fcab7 | Anthony Liguori | case 't': |
212 | 4a5fcab7 | Anthony Liguori | qstring_append(str, "\t");
|
213 | 4a5fcab7 | Anthony Liguori | ptr++; |
214 | 4a5fcab7 | Anthony Liguori | break;
|
215 | 4a5fcab7 | Anthony Liguori | case 'u': { |
216 | 4a5fcab7 | Anthony Liguori | uint16_t unicode_char = 0;
|
217 | 4a5fcab7 | Anthony Liguori | char utf8_char[4]; |
218 | 4a5fcab7 | Anthony Liguori | int i = 0; |
219 | 4a5fcab7 | Anthony Liguori | |
220 | 4a5fcab7 | Anthony Liguori | ptr++; |
221 | 4a5fcab7 | Anthony Liguori | |
222 | 4a5fcab7 | Anthony Liguori | for (i = 0; i < 4; i++) { |
223 | 4a5fcab7 | Anthony Liguori | if (qemu_isxdigit(*ptr)) {
|
224 | 4a5fcab7 | Anthony Liguori | unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4); |
225 | 4a5fcab7 | Anthony Liguori | } else {
|
226 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, |
227 | 4a5fcab7 | Anthony Liguori | "invalid hex escape sequence in string");
|
228 | 4a5fcab7 | Anthony Liguori | goto out;
|
229 | 4a5fcab7 | Anthony Liguori | } |
230 | 4a5fcab7 | Anthony Liguori | ptr++; |
231 | 4a5fcab7 | Anthony Liguori | } |
232 | 4a5fcab7 | Anthony Liguori | |
233 | 4a5fcab7 | Anthony Liguori | wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
|
234 | 4a5fcab7 | Anthony Liguori | qstring_append(str, utf8_char); |
235 | 4a5fcab7 | Anthony Liguori | } break;
|
236 | 4a5fcab7 | Anthony Liguori | default:
|
237 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "invalid escape sequence in string");
|
238 | 4a5fcab7 | Anthony Liguori | goto out;
|
239 | 4a5fcab7 | Anthony Liguori | } |
240 | 4a5fcab7 | Anthony Liguori | } else {
|
241 | 4a5fcab7 | Anthony Liguori | char dummy[2]; |
242 | 4a5fcab7 | Anthony Liguori | |
243 | 4a5fcab7 | Anthony Liguori | dummy[0] = *ptr++;
|
244 | 4a5fcab7 | Anthony Liguori | dummy[1] = 0; |
245 | 4a5fcab7 | Anthony Liguori | |
246 | 4a5fcab7 | Anthony Liguori | qstring_append(str, dummy); |
247 | 4a5fcab7 | Anthony Liguori | } |
248 | 4a5fcab7 | Anthony Liguori | } |
249 | 4a5fcab7 | Anthony Liguori | |
250 | 4a5fcab7 | Anthony Liguori | return str;
|
251 | 4a5fcab7 | Anthony Liguori | |
252 | 4a5fcab7 | Anthony Liguori | out:
|
253 | 4a5fcab7 | Anthony Liguori | QDECREF(str); |
254 | 4a5fcab7 | Anthony Liguori | return NULL; |
255 | 4a5fcab7 | Anthony Liguori | } |
256 | 4a5fcab7 | Anthony Liguori | |
257 | 4a5fcab7 | Anthony Liguori | /**
|
258 | 4a5fcab7 | Anthony Liguori | * Parsing rules
|
259 | 4a5fcab7 | Anthony Liguori | */
|
260 | 4a5fcab7 | Anthony Liguori | static int parse_pair(JSONParserContext *ctxt, QDict *dict, QList **tokens, va_list *ap) |
261 | 4a5fcab7 | Anthony Liguori | { |
262 | 4a5fcab7 | Anthony Liguori | QObject *key, *token = NULL, *value, *peek;
|
263 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(*tokens); |
264 | 4a5fcab7 | Anthony Liguori | |
265 | 4a5fcab7 | Anthony Liguori | peek = qlist_peek(working); |
266 | 4a5fcab7 | Anthony Liguori | key = parse_value(ctxt, &working, ap); |
267 | d758d90f | Kevin Wolf | if (!key || qobject_type(key) != QTYPE_QSTRING) {
|
268 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, peek, "key is not a string in object");
|
269 | 4a5fcab7 | Anthony Liguori | goto out;
|
270 | 4a5fcab7 | Anthony Liguori | } |
271 | 4a5fcab7 | Anthony Liguori | |
272 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
273 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(token, ':')) { |
274 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "missing : in object pair");
|
275 | 4a5fcab7 | Anthony Liguori | goto out;
|
276 | 4a5fcab7 | Anthony Liguori | } |
277 | 4a5fcab7 | Anthony Liguori | |
278 | 4a5fcab7 | Anthony Liguori | value = parse_value(ctxt, &working, ap); |
279 | 4a5fcab7 | Anthony Liguori | if (value == NULL) { |
280 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "Missing value in dict");
|
281 | 4a5fcab7 | Anthony Liguori | goto out;
|
282 | 4a5fcab7 | Anthony Liguori | } |
283 | 4a5fcab7 | Anthony Liguori | |
284 | 4a5fcab7 | Anthony Liguori | qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value); |
285 | 4a5fcab7 | Anthony Liguori | |
286 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
287 | 4a5fcab7 | Anthony Liguori | qobject_decref(key); |
288 | 4a5fcab7 | Anthony Liguori | QDECREF(*tokens); |
289 | 4a5fcab7 | Anthony Liguori | *tokens = working; |
290 | 4a5fcab7 | Anthony Liguori | |
291 | 4a5fcab7 | Anthony Liguori | return 0; |
292 | 4a5fcab7 | Anthony Liguori | |
293 | 4a5fcab7 | Anthony Liguori | out:
|
294 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
295 | 4a5fcab7 | Anthony Liguori | qobject_decref(key); |
296 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
297 | 4a5fcab7 | Anthony Liguori | |
298 | 4a5fcab7 | Anthony Liguori | return -1; |
299 | 4a5fcab7 | Anthony Liguori | } |
300 | 4a5fcab7 | Anthony Liguori | |
301 | 4a5fcab7 | Anthony Liguori | static QObject *parse_object(JSONParserContext *ctxt, QList **tokens, va_list *ap)
|
302 | 4a5fcab7 | Anthony Liguori | { |
303 | 4a5fcab7 | Anthony Liguori | QDict *dict = NULL;
|
304 | 4a5fcab7 | Anthony Liguori | QObject *token, *peek; |
305 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(*tokens); |
306 | 4a5fcab7 | Anthony Liguori | |
307 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
308 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(token, '{')) { |
309 | 4a5fcab7 | Anthony Liguori | goto out;
|
310 | 4a5fcab7 | Anthony Liguori | } |
311 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
312 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
313 | 4a5fcab7 | Anthony Liguori | |
314 | 4a5fcab7 | Anthony Liguori | dict = qdict_new(); |
315 | 4a5fcab7 | Anthony Liguori | |
316 | 4a5fcab7 | Anthony Liguori | peek = qlist_peek(working); |
317 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(peek, '}')) { |
318 | 4a5fcab7 | Anthony Liguori | if (parse_pair(ctxt, dict, &working, ap) == -1) { |
319 | 4a5fcab7 | Anthony Liguori | goto out;
|
320 | 4a5fcab7 | Anthony Liguori | } |
321 | 4a5fcab7 | Anthony Liguori | |
322 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
323 | 4a5fcab7 | Anthony Liguori | while (!token_is_operator(token, '}')) { |
324 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(token, ',')) { |
325 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "expected separator in dict");
|
326 | 4a5fcab7 | Anthony Liguori | goto out;
|
327 | 4a5fcab7 | Anthony Liguori | } |
328 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
329 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
330 | 4a5fcab7 | Anthony Liguori | |
331 | 4a5fcab7 | Anthony Liguori | if (parse_pair(ctxt, dict, &working, ap) == -1) { |
332 | 4a5fcab7 | Anthony Liguori | goto out;
|
333 | 4a5fcab7 | Anthony Liguori | } |
334 | 4a5fcab7 | Anthony Liguori | |
335 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
336 | 4a5fcab7 | Anthony Liguori | } |
337 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
338 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
339 | 4a5fcab7 | Anthony Liguori | } else {
|
340 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
341 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
342 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
343 | 4a5fcab7 | Anthony Liguori | } |
344 | 4a5fcab7 | Anthony Liguori | |
345 | 4a5fcab7 | Anthony Liguori | QDECREF(*tokens); |
346 | 4a5fcab7 | Anthony Liguori | *tokens = working; |
347 | 4a5fcab7 | Anthony Liguori | |
348 | 4a5fcab7 | Anthony Liguori | return QOBJECT(dict);
|
349 | 4a5fcab7 | Anthony Liguori | |
350 | 4a5fcab7 | Anthony Liguori | out:
|
351 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
352 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
353 | 4a5fcab7 | Anthony Liguori | QDECREF(dict); |
354 | 4a5fcab7 | Anthony Liguori | return NULL; |
355 | 4a5fcab7 | Anthony Liguori | } |
356 | 4a5fcab7 | Anthony Liguori | |
357 | 4a5fcab7 | Anthony Liguori | static QObject *parse_array(JSONParserContext *ctxt, QList **tokens, va_list *ap)
|
358 | 4a5fcab7 | Anthony Liguori | { |
359 | 4a5fcab7 | Anthony Liguori | QList *list = NULL;
|
360 | 4a5fcab7 | Anthony Liguori | QObject *token, *peek; |
361 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(*tokens); |
362 | 4a5fcab7 | Anthony Liguori | |
363 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
364 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(token, '[')) { |
365 | 4a5fcab7 | Anthony Liguori | goto out;
|
366 | 4a5fcab7 | Anthony Liguori | } |
367 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
368 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
369 | 4a5fcab7 | Anthony Liguori | |
370 | 4a5fcab7 | Anthony Liguori | list = qlist_new(); |
371 | 4a5fcab7 | Anthony Liguori | |
372 | 4a5fcab7 | Anthony Liguori | peek = qlist_peek(working); |
373 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(peek, ']')) { |
374 | 4a5fcab7 | Anthony Liguori | QObject *obj; |
375 | 4a5fcab7 | Anthony Liguori | |
376 | 4a5fcab7 | Anthony Liguori | obj = parse_value(ctxt, &working, ap); |
377 | 4a5fcab7 | Anthony Liguori | if (obj == NULL) { |
378 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "expecting value");
|
379 | 4a5fcab7 | Anthony Liguori | goto out;
|
380 | 4a5fcab7 | Anthony Liguori | } |
381 | 4a5fcab7 | Anthony Liguori | |
382 | 4a5fcab7 | Anthony Liguori | qlist_append_obj(list, obj); |
383 | 4a5fcab7 | Anthony Liguori | |
384 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
385 | 4a5fcab7 | Anthony Liguori | while (!token_is_operator(token, ']')) { |
386 | 4a5fcab7 | Anthony Liguori | if (!token_is_operator(token, ',')) { |
387 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "expected separator in list");
|
388 | 4a5fcab7 | Anthony Liguori | goto out;
|
389 | 4a5fcab7 | Anthony Liguori | } |
390 | 4a5fcab7 | Anthony Liguori | |
391 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
392 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
393 | 4a5fcab7 | Anthony Liguori | |
394 | 4a5fcab7 | Anthony Liguori | obj = parse_value(ctxt, &working, ap); |
395 | 4a5fcab7 | Anthony Liguori | if (obj == NULL) { |
396 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "expecting value");
|
397 | 4a5fcab7 | Anthony Liguori | goto out;
|
398 | 4a5fcab7 | Anthony Liguori | } |
399 | 4a5fcab7 | Anthony Liguori | |
400 | 4a5fcab7 | Anthony Liguori | qlist_append_obj(list, obj); |
401 | 4a5fcab7 | Anthony Liguori | |
402 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
403 | 4a5fcab7 | Anthony Liguori | } |
404 | 4a5fcab7 | Anthony Liguori | |
405 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
406 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
407 | 4a5fcab7 | Anthony Liguori | } else {
|
408 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
409 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
410 | 4a5fcab7 | Anthony Liguori | token = NULL;
|
411 | 4a5fcab7 | Anthony Liguori | } |
412 | 4a5fcab7 | Anthony Liguori | |
413 | 4a5fcab7 | Anthony Liguori | QDECREF(*tokens); |
414 | 4a5fcab7 | Anthony Liguori | *tokens = working; |
415 | 4a5fcab7 | Anthony Liguori | |
416 | 4a5fcab7 | Anthony Liguori | return QOBJECT(list);
|
417 | 4a5fcab7 | Anthony Liguori | |
418 | 4a5fcab7 | Anthony Liguori | out:
|
419 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
420 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
421 | 4a5fcab7 | Anthony Liguori | QDECREF(list); |
422 | 4a5fcab7 | Anthony Liguori | return NULL; |
423 | 4a5fcab7 | Anthony Liguori | } |
424 | 4a5fcab7 | Anthony Liguori | |
425 | 4a5fcab7 | Anthony Liguori | static QObject *parse_keyword(JSONParserContext *ctxt, QList **tokens)
|
426 | 4a5fcab7 | Anthony Liguori | { |
427 | 4a5fcab7 | Anthony Liguori | QObject *token, *ret; |
428 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(*tokens); |
429 | 4a5fcab7 | Anthony Liguori | |
430 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
431 | 4a5fcab7 | Anthony Liguori | |
432 | 4a5fcab7 | Anthony Liguori | if (token_get_type(token) != JSON_KEYWORD) {
|
433 | 4a5fcab7 | Anthony Liguori | goto out;
|
434 | 4a5fcab7 | Anthony Liguori | } |
435 | 4a5fcab7 | Anthony Liguori | |
436 | 4a5fcab7 | Anthony Liguori | if (token_is_keyword(token, "true")) { |
437 | 4a5fcab7 | Anthony Liguori | ret = QOBJECT(qbool_from_int(true));
|
438 | 4a5fcab7 | Anthony Liguori | } else if (token_is_keyword(token, "false")) { |
439 | 4a5fcab7 | Anthony Liguori | ret = QOBJECT(qbool_from_int(false));
|
440 | 4a5fcab7 | Anthony Liguori | } else {
|
441 | 4a5fcab7 | Anthony Liguori | parse_error(ctxt, token, "invalid keyword `%s'", token_get_value(token));
|
442 | 4a5fcab7 | Anthony Liguori | goto out;
|
443 | 4a5fcab7 | Anthony Liguori | } |
444 | 4a5fcab7 | Anthony Liguori | |
445 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
446 | 4a5fcab7 | Anthony Liguori | QDECREF(*tokens); |
447 | 4a5fcab7 | Anthony Liguori | *tokens = working; |
448 | 4a5fcab7 | Anthony Liguori | |
449 | 4a5fcab7 | Anthony Liguori | return ret;
|
450 | 4a5fcab7 | Anthony Liguori | |
451 | 4a5fcab7 | Anthony Liguori | out:
|
452 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
453 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
454 | 4a5fcab7 | Anthony Liguori | |
455 | 4a5fcab7 | Anthony Liguori | return NULL; |
456 | 4a5fcab7 | Anthony Liguori | } |
457 | 4a5fcab7 | Anthony Liguori | |
458 | 4a5fcab7 | Anthony Liguori | static QObject *parse_escape(JSONParserContext *ctxt, QList **tokens, va_list *ap)
|
459 | 4a5fcab7 | Anthony Liguori | { |
460 | 4a5fcab7 | Anthony Liguori | QObject *token = NULL, *obj;
|
461 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(*tokens); |
462 | 4a5fcab7 | Anthony Liguori | |
463 | 4a5fcab7 | Anthony Liguori | if (ap == NULL) { |
464 | 4a5fcab7 | Anthony Liguori | goto out;
|
465 | 4a5fcab7 | Anthony Liguori | } |
466 | 4a5fcab7 | Anthony Liguori | |
467 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
468 | 4a5fcab7 | Anthony Liguori | |
469 | 4a5fcab7 | Anthony Liguori | if (token_is_escape(token, "%p")) { |
470 | 4a5fcab7 | Anthony Liguori | obj = va_arg(*ap, QObject *); |
471 | 4a5fcab7 | Anthony Liguori | } else if (token_is_escape(token, "%i")) { |
472 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qbool_from_int(va_arg(*ap, int)));
|
473 | 4a5fcab7 | Anthony Liguori | } else if (token_is_escape(token, "%d")) { |
474 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
|
475 | 4a5fcab7 | Anthony Liguori | } else if (token_is_escape(token, "%ld")) { |
476 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
|
477 | 2c0d4b36 | Roy Tam | } else if (token_is_escape(token, "%lld") || |
478 | 2c0d4b36 | Roy Tam | token_is_escape(token, "%I64d")) {
|
479 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qint_from_int(va_arg(*ap, long long))); |
480 | 4a5fcab7 | Anthony Liguori | } else if (token_is_escape(token, "%s")) { |
481 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *))); |
482 | 4a5fcab7 | Anthony Liguori | } else if (token_is_escape(token, "%f")) { |
483 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qfloat_from_double(va_arg(*ap, double)));
|
484 | 4a5fcab7 | Anthony Liguori | } else {
|
485 | 4a5fcab7 | Anthony Liguori | goto out;
|
486 | 4a5fcab7 | Anthony Liguori | } |
487 | 4a5fcab7 | Anthony Liguori | |
488 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
489 | 4a5fcab7 | Anthony Liguori | QDECREF(*tokens); |
490 | 4a5fcab7 | Anthony Liguori | *tokens = working; |
491 | 4a5fcab7 | Anthony Liguori | |
492 | 4a5fcab7 | Anthony Liguori | return obj;
|
493 | 4a5fcab7 | Anthony Liguori | |
494 | 4a5fcab7 | Anthony Liguori | out:
|
495 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
496 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
497 | 4a5fcab7 | Anthony Liguori | |
498 | 4a5fcab7 | Anthony Liguori | return NULL; |
499 | 4a5fcab7 | Anthony Liguori | } |
500 | 4a5fcab7 | Anthony Liguori | |
501 | 4a5fcab7 | Anthony Liguori | static QObject *parse_literal(JSONParserContext *ctxt, QList **tokens)
|
502 | 4a5fcab7 | Anthony Liguori | { |
503 | 4a5fcab7 | Anthony Liguori | QObject *token, *obj; |
504 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(*tokens); |
505 | 4a5fcab7 | Anthony Liguori | |
506 | 4a5fcab7 | Anthony Liguori | token = qlist_pop(working); |
507 | 4a5fcab7 | Anthony Liguori | switch (token_get_type(token)) {
|
508 | 4a5fcab7 | Anthony Liguori | case JSON_STRING:
|
509 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qstring_from_escaped_str(ctxt, token)); |
510 | 4a5fcab7 | Anthony Liguori | break;
|
511 | 4a5fcab7 | Anthony Liguori | case JSON_INTEGER:
|
512 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qint_from_int(strtoll(token_get_value(token), NULL, 10))); |
513 | 4a5fcab7 | Anthony Liguori | break;
|
514 | 4a5fcab7 | Anthony Liguori | case JSON_FLOAT:
|
515 | 4a5fcab7 | Anthony Liguori | /* FIXME dependent on locale */
|
516 | 4a5fcab7 | Anthony Liguori | obj = QOBJECT(qfloat_from_double(strtod(token_get_value(token), NULL)));
|
517 | 4a5fcab7 | Anthony Liguori | break;
|
518 | 4a5fcab7 | Anthony Liguori | default:
|
519 | 4a5fcab7 | Anthony Liguori | goto out;
|
520 | 4a5fcab7 | Anthony Liguori | } |
521 | 4a5fcab7 | Anthony Liguori | |
522 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
523 | 4a5fcab7 | Anthony Liguori | QDECREF(*tokens); |
524 | 4a5fcab7 | Anthony Liguori | *tokens = working; |
525 | 4a5fcab7 | Anthony Liguori | |
526 | 4a5fcab7 | Anthony Liguori | return obj;
|
527 | 4a5fcab7 | Anthony Liguori | |
528 | 4a5fcab7 | Anthony Liguori | out:
|
529 | 4a5fcab7 | Anthony Liguori | qobject_decref(token); |
530 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
531 | 4a5fcab7 | Anthony Liguori | |
532 | 4a5fcab7 | Anthony Liguori | return NULL; |
533 | 4a5fcab7 | Anthony Liguori | } |
534 | 4a5fcab7 | Anthony Liguori | |
535 | 4a5fcab7 | Anthony Liguori | static QObject *parse_value(JSONParserContext *ctxt, QList **tokens, va_list *ap)
|
536 | 4a5fcab7 | Anthony Liguori | { |
537 | 4a5fcab7 | Anthony Liguori | QObject *obj; |
538 | 4a5fcab7 | Anthony Liguori | |
539 | 4a5fcab7 | Anthony Liguori | obj = parse_object(ctxt, tokens, ap); |
540 | 4a5fcab7 | Anthony Liguori | if (obj == NULL) { |
541 | 4a5fcab7 | Anthony Liguori | obj = parse_array(ctxt, tokens, ap); |
542 | 4a5fcab7 | Anthony Liguori | } |
543 | 4a5fcab7 | Anthony Liguori | if (obj == NULL) { |
544 | 4a5fcab7 | Anthony Liguori | obj = parse_escape(ctxt, tokens, ap); |
545 | 4a5fcab7 | Anthony Liguori | } |
546 | 4a5fcab7 | Anthony Liguori | if (obj == NULL) { |
547 | 4a5fcab7 | Anthony Liguori | obj = parse_keyword(ctxt, tokens); |
548 | 4a5fcab7 | Anthony Liguori | } |
549 | 4a5fcab7 | Anthony Liguori | if (obj == NULL) { |
550 | 4a5fcab7 | Anthony Liguori | obj = parse_literal(ctxt, tokens); |
551 | 4a5fcab7 | Anthony Liguori | } |
552 | 4a5fcab7 | Anthony Liguori | |
553 | 4a5fcab7 | Anthony Liguori | return obj;
|
554 | 4a5fcab7 | Anthony Liguori | } |
555 | 4a5fcab7 | Anthony Liguori | |
556 | 4a5fcab7 | Anthony Liguori | QObject *json_parser_parse(QList *tokens, va_list *ap) |
557 | 4a5fcab7 | Anthony Liguori | { |
558 | 4a5fcab7 | Anthony Liguori | JSONParserContext ctxt = {}; |
559 | 4a5fcab7 | Anthony Liguori | QList *working = qlist_copy(tokens); |
560 | 4a5fcab7 | Anthony Liguori | QObject *result; |
561 | 4a5fcab7 | Anthony Liguori | |
562 | 4a5fcab7 | Anthony Liguori | result = parse_value(&ctxt, &working, ap); |
563 | 4a5fcab7 | Anthony Liguori | |
564 | 4a5fcab7 | Anthony Liguori | QDECREF(working); |
565 | 4a5fcab7 | Anthony Liguori | |
566 | 4a5fcab7 | Anthony Liguori | return result;
|
567 | 4a5fcab7 | Anthony Liguori | } |