Statistics
| Branch: | Revision:

root / json-parser.c @ c28fa5a0

History | View | Annotate | Download (16.4 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 c96c84a9 Amos Kong
#include <stdarg.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 ef749d07 Anthony Liguori
#include "qerror.h"
26 4a5fcab7 Anthony Liguori
27 4a5fcab7 Anthony Liguori
typedef struct JSONParserContext
28 4a5fcab7 Anthony Liguori
{
29 ef749d07 Anthony Liguori
    Error *err;
30 65c0f1e9 Michael Roth
    struct {
31 65c0f1e9 Michael Roth
        QObject **buf;
32 65c0f1e9 Michael Roth
        size_t pos;
33 65c0f1e9 Michael Roth
        size_t count;
34 65c0f1e9 Michael Roth
    } tokens;
35 4a5fcab7 Anthony Liguori
} JSONParserContext;
36 4a5fcab7 Anthony Liguori
37 4a5fcab7 Anthony Liguori
#define BUG_ON(cond) assert(!(cond))
38 4a5fcab7 Anthony Liguori
39 4a5fcab7 Anthony Liguori
/**
40 4a5fcab7 Anthony Liguori
 * TODO
41 4a5fcab7 Anthony Liguori
 *
42 4a5fcab7 Anthony Liguori
 * 0) make errors meaningful again
43 4a5fcab7 Anthony Liguori
 * 1) add geometry information to tokens
44 4a5fcab7 Anthony Liguori
 * 3) should we return a parsed size?
45 4a5fcab7 Anthony Liguori
 * 4) deal with premature EOI
46 4a5fcab7 Anthony Liguori
 */
47 4a5fcab7 Anthony Liguori
48 65c0f1e9 Michael Roth
static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
49 4a5fcab7 Anthony Liguori
50 4a5fcab7 Anthony Liguori
/**
51 4a5fcab7 Anthony Liguori
 * Token manipulators
52 4a5fcab7 Anthony Liguori
 *
53 4a5fcab7 Anthony Liguori
 * tokens are dictionaries that contain a type, a string value, and geometry information
54 4a5fcab7 Anthony Liguori
 * about a token identified by the lexer.  These are routines that make working with
55 4a5fcab7 Anthony Liguori
 * these objects a bit easier.
56 4a5fcab7 Anthony Liguori
 */
57 4a5fcab7 Anthony Liguori
static const char *token_get_value(QObject *obj)
58 4a5fcab7 Anthony Liguori
{
59 4a5fcab7 Anthony Liguori
    return qdict_get_str(qobject_to_qdict(obj), "token");
60 4a5fcab7 Anthony Liguori
}
61 4a5fcab7 Anthony Liguori
62 4a5fcab7 Anthony Liguori
static JSONTokenType token_get_type(QObject *obj)
63 4a5fcab7 Anthony Liguori
{
64 4a5fcab7 Anthony Liguori
    return qdict_get_int(qobject_to_qdict(obj), "type");
65 4a5fcab7 Anthony Liguori
}
66 4a5fcab7 Anthony Liguori
67 4a5fcab7 Anthony Liguori
static int token_is_operator(QObject *obj, char op)
68 4a5fcab7 Anthony Liguori
{
69 4a5fcab7 Anthony Liguori
    const char *val;
70 4a5fcab7 Anthony Liguori
71 4a5fcab7 Anthony Liguori
    if (token_get_type(obj) != JSON_OPERATOR) {
72 4a5fcab7 Anthony Liguori
        return 0;
73 4a5fcab7 Anthony Liguori
    }
74 4a5fcab7 Anthony Liguori
75 4a5fcab7 Anthony Liguori
    val = token_get_value(obj);
76 4a5fcab7 Anthony Liguori
77 4a5fcab7 Anthony Liguori
    return (val[0] == op) && (val[1] == 0);
78 4a5fcab7 Anthony Liguori
}
79 4a5fcab7 Anthony Liguori
80 4a5fcab7 Anthony Liguori
static int token_is_keyword(QObject *obj, const char *value)
81 4a5fcab7 Anthony Liguori
{
82 4a5fcab7 Anthony Liguori
    if (token_get_type(obj) != JSON_KEYWORD) {
83 4a5fcab7 Anthony Liguori
        return 0;
84 4a5fcab7 Anthony Liguori
    }
85 4a5fcab7 Anthony Liguori
86 4a5fcab7 Anthony Liguori
    return strcmp(token_get_value(obj), value) == 0;
87 4a5fcab7 Anthony Liguori
}
88 4a5fcab7 Anthony Liguori
89 4a5fcab7 Anthony Liguori
static int token_is_escape(QObject *obj, const char *value)
90 4a5fcab7 Anthony Liguori
{
91 4a5fcab7 Anthony Liguori
    if (token_get_type(obj) != JSON_ESCAPE) {
92 4a5fcab7 Anthony Liguori
        return 0;
93 4a5fcab7 Anthony Liguori
    }
94 4a5fcab7 Anthony Liguori
95 4a5fcab7 Anthony Liguori
    return (strcmp(token_get_value(obj), value) == 0);
96 4a5fcab7 Anthony Liguori
}
97 4a5fcab7 Anthony Liguori
98 4a5fcab7 Anthony Liguori
/**
99 4a5fcab7 Anthony Liguori
 * Error handler
100 4a5fcab7 Anthony Liguori
 */
101 8b7968f7 Stefan Weil
static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
102 8b7968f7 Stefan Weil
                                           QObject *token, const char *msg, ...)
103 4a5fcab7 Anthony Liguori
{
104 c96c84a9 Amos Kong
    va_list ap;
105 ef749d07 Anthony Liguori
    char message[1024];
106 c96c84a9 Amos Kong
    va_start(ap, msg);
107 ef749d07 Anthony Liguori
    vsnprintf(message, sizeof(message), msg, ap);
108 c96c84a9 Amos Kong
    va_end(ap);
109 ef749d07 Anthony Liguori
    if (ctxt->err) {
110 ef749d07 Anthony Liguori
        error_free(ctxt->err);
111 ef749d07 Anthony Liguori
        ctxt->err = NULL;
112 ef749d07 Anthony Liguori
    }
113 ef749d07 Anthony Liguori
    error_set(&ctxt->err, QERR_JSON_PARSE_ERROR, message);
114 4a5fcab7 Anthony Liguori
}
115 4a5fcab7 Anthony Liguori
116 4a5fcab7 Anthony Liguori
/**
117 4a5fcab7 Anthony Liguori
 * String helpers
118 4a5fcab7 Anthony Liguori
 *
119 4a5fcab7 Anthony Liguori
 * These helpers are used to unescape strings.
120 4a5fcab7 Anthony Liguori
 */
121 4a5fcab7 Anthony Liguori
static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
122 4a5fcab7 Anthony Liguori
{
123 4a5fcab7 Anthony Liguori
    if (wchar <= 0x007F) {
124 4a5fcab7 Anthony Liguori
        BUG_ON(buffer_length < 2);
125 4a5fcab7 Anthony Liguori
126 4a5fcab7 Anthony Liguori
        buffer[0] = wchar & 0x7F;
127 4a5fcab7 Anthony Liguori
        buffer[1] = 0;
128 4a5fcab7 Anthony Liguori
    } else if (wchar <= 0x07FF) {
129 4a5fcab7 Anthony Liguori
        BUG_ON(buffer_length < 3);
130 4a5fcab7 Anthony Liguori
131 4a5fcab7 Anthony Liguori
        buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
132 4a5fcab7 Anthony Liguori
        buffer[1] = 0x80 | (wchar & 0x3F);
133 4a5fcab7 Anthony Liguori
        buffer[2] = 0;
134 4a5fcab7 Anthony Liguori
    } else {
135 4a5fcab7 Anthony Liguori
        BUG_ON(buffer_length < 4);
136 4a5fcab7 Anthony Liguori
137 4a5fcab7 Anthony Liguori
        buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
138 4a5fcab7 Anthony Liguori
        buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
139 4a5fcab7 Anthony Liguori
        buffer[2] = 0x80 | (wchar & 0x3F);
140 4a5fcab7 Anthony Liguori
        buffer[3] = 0;
141 4a5fcab7 Anthony Liguori
    }
142 4a5fcab7 Anthony Liguori
}
143 4a5fcab7 Anthony Liguori
144 4a5fcab7 Anthony Liguori
static int hex2decimal(char ch)
145 4a5fcab7 Anthony Liguori
{
146 4a5fcab7 Anthony Liguori
    if (ch >= '0' && ch <= '9') {
147 4a5fcab7 Anthony Liguori
        return (ch - '0');
148 4a5fcab7 Anthony Liguori
    } else if (ch >= 'a' && ch <= 'f') {
149 4a5fcab7 Anthony Liguori
        return 10 + (ch - 'a');
150 4a5fcab7 Anthony Liguori
    } else if (ch >= 'A' && ch <= 'F') {
151 4a5fcab7 Anthony Liguori
        return 10 + (ch - 'A');
152 4a5fcab7 Anthony Liguori
    }
153 4a5fcab7 Anthony Liguori
154 4a5fcab7 Anthony Liguori
    return -1;
155 4a5fcab7 Anthony Liguori
}
156 4a5fcab7 Anthony Liguori
157 4a5fcab7 Anthony Liguori
/**
158 4a5fcab7 Anthony Liguori
 * parse_string(): Parse a json string and return a QObject
159 4a5fcab7 Anthony Liguori
 *
160 4a5fcab7 Anthony Liguori
 *  string
161 4a5fcab7 Anthony Liguori
 *      ""
162 4a5fcab7 Anthony Liguori
 *      " chars "
163 4a5fcab7 Anthony Liguori
 *  chars
164 4a5fcab7 Anthony Liguori
 *      char
165 4a5fcab7 Anthony Liguori
 *      char chars
166 4a5fcab7 Anthony Liguori
 *  char
167 4a5fcab7 Anthony Liguori
 *      any-Unicode-character-
168 4a5fcab7 Anthony Liguori
 *          except-"-or-\-or-
169 4a5fcab7 Anthony Liguori
 *          control-character
170 4a5fcab7 Anthony Liguori
 *      \"
171 4a5fcab7 Anthony Liguori
 *      \\
172 4a5fcab7 Anthony Liguori
 *      \/
173 4a5fcab7 Anthony Liguori
 *      \b
174 4a5fcab7 Anthony Liguori
 *      \f
175 4a5fcab7 Anthony Liguori
 *      \n
176 4a5fcab7 Anthony Liguori
 *      \r
177 4a5fcab7 Anthony Liguori
 *      \t
178 4a5fcab7 Anthony Liguori
 *      \u four-hex-digits 
179 4a5fcab7 Anthony Liguori
 */
180 4a5fcab7 Anthony Liguori
static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *token)
181 4a5fcab7 Anthony Liguori
{
182 4a5fcab7 Anthony Liguori
    const char *ptr = token_get_value(token);
183 4a5fcab7 Anthony Liguori
    QString *str;
184 4a5fcab7 Anthony Liguori
    int double_quote = 1;
185 4a5fcab7 Anthony Liguori
186 4a5fcab7 Anthony Liguori
    if (*ptr == '"') {
187 4a5fcab7 Anthony Liguori
        double_quote = 1;
188 4a5fcab7 Anthony Liguori
    } else {
189 4a5fcab7 Anthony Liguori
        double_quote = 0;
190 4a5fcab7 Anthony Liguori
    }
191 4a5fcab7 Anthony Liguori
    ptr++;
192 4a5fcab7 Anthony Liguori
193 4a5fcab7 Anthony Liguori
    str = qstring_new();
194 4a5fcab7 Anthony Liguori
    while (*ptr && 
195 4a5fcab7 Anthony Liguori
           ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
196 4a5fcab7 Anthony Liguori
        if (*ptr == '\\') {
197 4a5fcab7 Anthony Liguori
            ptr++;
198 4a5fcab7 Anthony Liguori
199 4a5fcab7 Anthony Liguori
            switch (*ptr) {
200 4a5fcab7 Anthony Liguori
            case '"':
201 4a5fcab7 Anthony Liguori
                qstring_append(str, "\"");
202 4a5fcab7 Anthony Liguori
                ptr++;
203 4a5fcab7 Anthony Liguori
                break;
204 4a5fcab7 Anthony Liguori
            case '\'':
205 4a5fcab7 Anthony Liguori
                qstring_append(str, "'");
206 4a5fcab7 Anthony Liguori
                ptr++;
207 4a5fcab7 Anthony Liguori
                break;
208 4a5fcab7 Anthony Liguori
            case '\\':
209 4a5fcab7 Anthony Liguori
                qstring_append(str, "\\");
210 4a5fcab7 Anthony Liguori
                ptr++;
211 4a5fcab7 Anthony Liguori
                break;
212 4a5fcab7 Anthony Liguori
            case '/':
213 4a5fcab7 Anthony Liguori
                qstring_append(str, "/");
214 4a5fcab7 Anthony Liguori
                ptr++;
215 4a5fcab7 Anthony Liguori
                break;
216 4a5fcab7 Anthony Liguori
            case 'b':
217 4a5fcab7 Anthony Liguori
                qstring_append(str, "\b");
218 4a5fcab7 Anthony Liguori
                ptr++;
219 4a5fcab7 Anthony Liguori
                break;
220 bd032695 Luiz Capitulino
            case 'f':
221 bd032695 Luiz Capitulino
                qstring_append(str, "\f");
222 bd032695 Luiz Capitulino
                ptr++;
223 bd032695 Luiz Capitulino
                break;
224 4a5fcab7 Anthony Liguori
            case 'n':
225 4a5fcab7 Anthony Liguori
                qstring_append(str, "\n");
226 4a5fcab7 Anthony Liguori
                ptr++;
227 4a5fcab7 Anthony Liguori
                break;
228 4a5fcab7 Anthony Liguori
            case 'r':
229 4a5fcab7 Anthony Liguori
                qstring_append(str, "\r");
230 4a5fcab7 Anthony Liguori
                ptr++;
231 4a5fcab7 Anthony Liguori
                break;
232 4a5fcab7 Anthony Liguori
            case 't':
233 4a5fcab7 Anthony Liguori
                qstring_append(str, "\t");
234 4a5fcab7 Anthony Liguori
                ptr++;
235 4a5fcab7 Anthony Liguori
                break;
236 4a5fcab7 Anthony Liguori
            case 'u': {
237 4a5fcab7 Anthony Liguori
                uint16_t unicode_char = 0;
238 4a5fcab7 Anthony Liguori
                char utf8_char[4];
239 4a5fcab7 Anthony Liguori
                int i = 0;
240 4a5fcab7 Anthony Liguori
241 4a5fcab7 Anthony Liguori
                ptr++;
242 4a5fcab7 Anthony Liguori
243 4a5fcab7 Anthony Liguori
                for (i = 0; i < 4; i++) {
244 4a5fcab7 Anthony Liguori
                    if (qemu_isxdigit(*ptr)) {
245 4a5fcab7 Anthony Liguori
                        unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
246 4a5fcab7 Anthony Liguori
                    } else {
247 4a5fcab7 Anthony Liguori
                        parse_error(ctxt, token,
248 4a5fcab7 Anthony Liguori
                                    "invalid hex escape sequence in string");
249 4a5fcab7 Anthony Liguori
                        goto out;
250 4a5fcab7 Anthony Liguori
                    }
251 4a5fcab7 Anthony Liguori
                    ptr++;
252 4a5fcab7 Anthony Liguori
                }
253 4a5fcab7 Anthony Liguori
254 4a5fcab7 Anthony Liguori
                wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
255 4a5fcab7 Anthony Liguori
                qstring_append(str, utf8_char);
256 4a5fcab7 Anthony Liguori
            }   break;
257 4a5fcab7 Anthony Liguori
            default:
258 4a5fcab7 Anthony Liguori
                parse_error(ctxt, token, "invalid escape sequence in string");
259 4a5fcab7 Anthony Liguori
                goto out;
260 4a5fcab7 Anthony Liguori
            }
261 4a5fcab7 Anthony Liguori
        } else {
262 4a5fcab7 Anthony Liguori
            char dummy[2];
263 4a5fcab7 Anthony Liguori
264 4a5fcab7 Anthony Liguori
            dummy[0] = *ptr++;
265 4a5fcab7 Anthony Liguori
            dummy[1] = 0;
266 4a5fcab7 Anthony Liguori
267 4a5fcab7 Anthony Liguori
            qstring_append(str, dummy);
268 4a5fcab7 Anthony Liguori
        }
269 4a5fcab7 Anthony Liguori
    }
270 4a5fcab7 Anthony Liguori
271 4a5fcab7 Anthony Liguori
    return str;
272 4a5fcab7 Anthony Liguori
273 4a5fcab7 Anthony Liguori
out:
274 4a5fcab7 Anthony Liguori
    QDECREF(str);
275 4a5fcab7 Anthony Liguori
    return NULL;
276 4a5fcab7 Anthony Liguori
}
277 4a5fcab7 Anthony Liguori
278 65c0f1e9 Michael Roth
static QObject *parser_context_pop_token(JSONParserContext *ctxt)
279 65c0f1e9 Michael Roth
{
280 65c0f1e9 Michael Roth
    QObject *token;
281 65c0f1e9 Michael Roth
    g_assert(ctxt->tokens.pos < ctxt->tokens.count);
282 65c0f1e9 Michael Roth
    token = ctxt->tokens.buf[ctxt->tokens.pos];
283 65c0f1e9 Michael Roth
    ctxt->tokens.pos++;
284 65c0f1e9 Michael Roth
    return token;
285 65c0f1e9 Michael Roth
}
286 65c0f1e9 Michael Roth
287 65c0f1e9 Michael Roth
/* Note: parser_context_{peek|pop}_token do not increment the
288 65c0f1e9 Michael Roth
 * token object's refcount. In both cases the references will continue
289 65c0f1e9 Michael Roth
 * to be tracked and cleaned up in parser_context_free(), so do not
290 65c0f1e9 Michael Roth
 * attempt to free the token object.
291 65c0f1e9 Michael Roth
 */
292 65c0f1e9 Michael Roth
static QObject *parser_context_peek_token(JSONParserContext *ctxt)
293 65c0f1e9 Michael Roth
{
294 65c0f1e9 Michael Roth
    QObject *token;
295 65c0f1e9 Michael Roth
    g_assert(ctxt->tokens.pos < ctxt->tokens.count);
296 65c0f1e9 Michael Roth
    token = ctxt->tokens.buf[ctxt->tokens.pos];
297 65c0f1e9 Michael Roth
    return token;
298 65c0f1e9 Michael Roth
}
299 65c0f1e9 Michael Roth
300 65c0f1e9 Michael Roth
static JSONParserContext parser_context_save(JSONParserContext *ctxt)
301 65c0f1e9 Michael Roth
{
302 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = {0};
303 65c0f1e9 Michael Roth
    saved_ctxt.tokens.pos = ctxt->tokens.pos;
304 65c0f1e9 Michael Roth
    saved_ctxt.tokens.count = ctxt->tokens.count;
305 65c0f1e9 Michael Roth
    saved_ctxt.tokens.buf = ctxt->tokens.buf;
306 65c0f1e9 Michael Roth
    return saved_ctxt;
307 65c0f1e9 Michael Roth
}
308 65c0f1e9 Michael Roth
309 65c0f1e9 Michael Roth
static void parser_context_restore(JSONParserContext *ctxt,
310 65c0f1e9 Michael Roth
                                   JSONParserContext saved_ctxt)
311 65c0f1e9 Michael Roth
{
312 65c0f1e9 Michael Roth
    ctxt->tokens.pos = saved_ctxt.tokens.pos;
313 65c0f1e9 Michael Roth
    ctxt->tokens.count = saved_ctxt.tokens.count;
314 65c0f1e9 Michael Roth
    ctxt->tokens.buf = saved_ctxt.tokens.buf;
315 65c0f1e9 Michael Roth
}
316 65c0f1e9 Michael Roth
317 65c0f1e9 Michael Roth
static void tokens_append_from_iter(QObject *obj, void *opaque)
318 65c0f1e9 Michael Roth
{
319 65c0f1e9 Michael Roth
    JSONParserContext *ctxt = opaque;
320 65c0f1e9 Michael Roth
    g_assert(ctxt->tokens.pos < ctxt->tokens.count);
321 65c0f1e9 Michael Roth
    ctxt->tokens.buf[ctxt->tokens.pos++] = obj;
322 65c0f1e9 Michael Roth
    qobject_incref(obj);
323 65c0f1e9 Michael Roth
}
324 65c0f1e9 Michael Roth
325 65c0f1e9 Michael Roth
static JSONParserContext *parser_context_new(QList *tokens)
326 65c0f1e9 Michael Roth
{
327 65c0f1e9 Michael Roth
    JSONParserContext *ctxt;
328 65c0f1e9 Michael Roth
    size_t count;
329 65c0f1e9 Michael Roth
330 65c0f1e9 Michael Roth
    if (!tokens) {
331 65c0f1e9 Michael Roth
        return NULL;
332 65c0f1e9 Michael Roth
    }
333 65c0f1e9 Michael Roth
334 65c0f1e9 Michael Roth
    count = qlist_size(tokens);
335 65c0f1e9 Michael Roth
    if (count == 0) {
336 65c0f1e9 Michael Roth
        return NULL;
337 65c0f1e9 Michael Roth
    }
338 65c0f1e9 Michael Roth
339 65c0f1e9 Michael Roth
    ctxt = g_malloc0(sizeof(JSONParserContext));
340 65c0f1e9 Michael Roth
    ctxt->tokens.pos = 0;
341 65c0f1e9 Michael Roth
    ctxt->tokens.count = count;
342 65c0f1e9 Michael Roth
    ctxt->tokens.buf = g_malloc(count * sizeof(QObject *));
343 65c0f1e9 Michael Roth
    qlist_iter(tokens, tokens_append_from_iter, ctxt);
344 65c0f1e9 Michael Roth
    ctxt->tokens.pos = 0;
345 65c0f1e9 Michael Roth
346 65c0f1e9 Michael Roth
    return ctxt;
347 65c0f1e9 Michael Roth
}
348 65c0f1e9 Michael Roth
349 65c0f1e9 Michael Roth
/* to support error propagation, ctxt->err must be freed separately */
350 65c0f1e9 Michael Roth
static void parser_context_free(JSONParserContext *ctxt)
351 65c0f1e9 Michael Roth
{
352 65c0f1e9 Michael Roth
    int i;
353 65c0f1e9 Michael Roth
    if (ctxt) {
354 65c0f1e9 Michael Roth
        for (i = 0; i < ctxt->tokens.count; i++) {
355 65c0f1e9 Michael Roth
            qobject_decref(ctxt->tokens.buf[i]);
356 65c0f1e9 Michael Roth
        }
357 65c0f1e9 Michael Roth
        g_free(ctxt->tokens.buf);
358 65c0f1e9 Michael Roth
        g_free(ctxt);
359 65c0f1e9 Michael Roth
    }
360 65c0f1e9 Michael Roth
}
361 65c0f1e9 Michael Roth
362 4a5fcab7 Anthony Liguori
/**
363 4a5fcab7 Anthony Liguori
 * Parsing rules
364 4a5fcab7 Anthony Liguori
 */
365 65c0f1e9 Michael Roth
static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
366 4a5fcab7 Anthony Liguori
{
367 11e8a46c Anthony Liguori
    QObject *key = NULL, *token = NULL, *value, *peek;
368 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = parser_context_save(ctxt);
369 4a5fcab7 Anthony Liguori
370 65c0f1e9 Michael Roth
    peek = parser_context_peek_token(ctxt);
371 11e8a46c Anthony Liguori
    if (peek == NULL) {
372 11e8a46c Anthony Liguori
        parse_error(ctxt, NULL, "premature EOI");
373 11e8a46c Anthony Liguori
        goto out;
374 11e8a46c Anthony Liguori
    }
375 11e8a46c Anthony Liguori
376 65c0f1e9 Michael Roth
    key = parse_value(ctxt, ap);
377 d758d90f Kevin Wolf
    if (!key || qobject_type(key) != QTYPE_QSTRING) {
378 4a5fcab7 Anthony Liguori
        parse_error(ctxt, peek, "key is not a string in object");
379 4a5fcab7 Anthony Liguori
        goto out;
380 4a5fcab7 Anthony Liguori
    }
381 4a5fcab7 Anthony Liguori
382 65c0f1e9 Michael Roth
    token = parser_context_pop_token(ctxt);
383 11e8a46c Anthony Liguori
    if (token == NULL) {
384 11e8a46c Anthony Liguori
        parse_error(ctxt, NULL, "premature EOI");
385 11e8a46c Anthony Liguori
        goto out;
386 11e8a46c Anthony Liguori
    }
387 11e8a46c Anthony Liguori
388 4a5fcab7 Anthony Liguori
    if (!token_is_operator(token, ':')) {
389 4a5fcab7 Anthony Liguori
        parse_error(ctxt, token, "missing : in object pair");
390 4a5fcab7 Anthony Liguori
        goto out;
391 4a5fcab7 Anthony Liguori
    }
392 4a5fcab7 Anthony Liguori
393 65c0f1e9 Michael Roth
    value = parse_value(ctxt, ap);
394 4a5fcab7 Anthony Liguori
    if (value == NULL) {
395 4a5fcab7 Anthony Liguori
        parse_error(ctxt, token, "Missing value in dict");
396 4a5fcab7 Anthony Liguori
        goto out;
397 4a5fcab7 Anthony Liguori
    }
398 4a5fcab7 Anthony Liguori
399 4a5fcab7 Anthony Liguori
    qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
400 4a5fcab7 Anthony Liguori
401 4a5fcab7 Anthony Liguori
    qobject_decref(key);
402 4a5fcab7 Anthony Liguori
403 4a5fcab7 Anthony Liguori
    return 0;
404 4a5fcab7 Anthony Liguori
405 4a5fcab7 Anthony Liguori
out:
406 65c0f1e9 Michael Roth
    parser_context_restore(ctxt, saved_ctxt);
407 4a5fcab7 Anthony Liguori
    qobject_decref(key);
408 4a5fcab7 Anthony Liguori
409 4a5fcab7 Anthony Liguori
    return -1;
410 4a5fcab7 Anthony Liguori
}
411 4a5fcab7 Anthony Liguori
412 65c0f1e9 Michael Roth
static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
413 4a5fcab7 Anthony Liguori
{
414 4a5fcab7 Anthony Liguori
    QDict *dict = NULL;
415 4a5fcab7 Anthony Liguori
    QObject *token, *peek;
416 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = parser_context_save(ctxt);
417 4a5fcab7 Anthony Liguori
418 65c0f1e9 Michael Roth
    token = parser_context_pop_token(ctxt);
419 11e8a46c Anthony Liguori
    if (token == NULL) {
420 11e8a46c Anthony Liguori
        goto out;
421 11e8a46c Anthony Liguori
    }
422 11e8a46c Anthony Liguori
423 4a5fcab7 Anthony Liguori
    if (!token_is_operator(token, '{')) {
424 4a5fcab7 Anthony Liguori
        goto out;
425 4a5fcab7 Anthony Liguori
    }
426 4a5fcab7 Anthony Liguori
    token = NULL;
427 4a5fcab7 Anthony Liguori
428 4a5fcab7 Anthony Liguori
    dict = qdict_new();
429 4a5fcab7 Anthony Liguori
430 65c0f1e9 Michael Roth
    peek = parser_context_peek_token(ctxt);
431 11e8a46c Anthony Liguori
    if (peek == NULL) {
432 11e8a46c Anthony Liguori
        parse_error(ctxt, NULL, "premature EOI");
433 11e8a46c Anthony Liguori
        goto out;
434 11e8a46c Anthony Liguori
    }
435 11e8a46c Anthony Liguori
436 4a5fcab7 Anthony Liguori
    if (!token_is_operator(peek, '}')) {
437 65c0f1e9 Michael Roth
        if (parse_pair(ctxt, dict, ap) == -1) {
438 4a5fcab7 Anthony Liguori
            goto out;
439 4a5fcab7 Anthony Liguori
        }
440 4a5fcab7 Anthony Liguori
441 65c0f1e9 Michael Roth
        token = parser_context_pop_token(ctxt);
442 11e8a46c Anthony Liguori
        if (token == NULL) {
443 11e8a46c Anthony Liguori
            parse_error(ctxt, NULL, "premature EOI");
444 11e8a46c Anthony Liguori
            goto out;
445 11e8a46c Anthony Liguori
        }
446 11e8a46c Anthony Liguori
447 4a5fcab7 Anthony Liguori
        while (!token_is_operator(token, '}')) {
448 4a5fcab7 Anthony Liguori
            if (!token_is_operator(token, ',')) {
449 4a5fcab7 Anthony Liguori
                parse_error(ctxt, token, "expected separator in dict");
450 4a5fcab7 Anthony Liguori
                goto out;
451 4a5fcab7 Anthony Liguori
            }
452 4a5fcab7 Anthony Liguori
            token = NULL;
453 4a5fcab7 Anthony Liguori
454 65c0f1e9 Michael Roth
            if (parse_pair(ctxt, dict, ap) == -1) {
455 4a5fcab7 Anthony Liguori
                goto out;
456 4a5fcab7 Anthony Liguori
            }
457 4a5fcab7 Anthony Liguori
458 65c0f1e9 Michael Roth
            token = parser_context_pop_token(ctxt);
459 11e8a46c Anthony Liguori
            if (token == NULL) {
460 11e8a46c Anthony Liguori
                parse_error(ctxt, NULL, "premature EOI");
461 11e8a46c Anthony Liguori
                goto out;
462 11e8a46c Anthony Liguori
            }
463 4a5fcab7 Anthony Liguori
        }
464 4a5fcab7 Anthony Liguori
        token = NULL;
465 4a5fcab7 Anthony Liguori
    } else {
466 65c0f1e9 Michael Roth
        token = parser_context_pop_token(ctxt);
467 4a5fcab7 Anthony Liguori
        token = NULL;
468 4a5fcab7 Anthony Liguori
    }
469 4a5fcab7 Anthony Liguori
470 4a5fcab7 Anthony Liguori
    return QOBJECT(dict);
471 4a5fcab7 Anthony Liguori
472 4a5fcab7 Anthony Liguori
out:
473 65c0f1e9 Michael Roth
    parser_context_restore(ctxt, saved_ctxt);
474 4a5fcab7 Anthony Liguori
    QDECREF(dict);
475 4a5fcab7 Anthony Liguori
    return NULL;
476 4a5fcab7 Anthony Liguori
}
477 4a5fcab7 Anthony Liguori
478 65c0f1e9 Michael Roth
static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
479 4a5fcab7 Anthony Liguori
{
480 4a5fcab7 Anthony Liguori
    QList *list = NULL;
481 4a5fcab7 Anthony Liguori
    QObject *token, *peek;
482 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = parser_context_save(ctxt);
483 4a5fcab7 Anthony Liguori
484 65c0f1e9 Michael Roth
    token = parser_context_pop_token(ctxt);
485 11e8a46c Anthony Liguori
    if (token == NULL) {
486 11e8a46c Anthony Liguori
        goto out;
487 11e8a46c Anthony Liguori
    }
488 11e8a46c Anthony Liguori
489 4a5fcab7 Anthony Liguori
    if (!token_is_operator(token, '[')) {
490 65c0f1e9 Michael Roth
        token = NULL;
491 4a5fcab7 Anthony Liguori
        goto out;
492 4a5fcab7 Anthony Liguori
    }
493 4a5fcab7 Anthony Liguori
    token = NULL;
494 4a5fcab7 Anthony Liguori
495 4a5fcab7 Anthony Liguori
    list = qlist_new();
496 4a5fcab7 Anthony Liguori
497 65c0f1e9 Michael Roth
    peek = parser_context_peek_token(ctxt);
498 11e8a46c Anthony Liguori
    if (peek == NULL) {
499 11e8a46c Anthony Liguori
        parse_error(ctxt, NULL, "premature EOI");
500 11e8a46c Anthony Liguori
        goto out;
501 11e8a46c Anthony Liguori
    }
502 11e8a46c Anthony Liguori
503 4a5fcab7 Anthony Liguori
    if (!token_is_operator(peek, ']')) {
504 4a5fcab7 Anthony Liguori
        QObject *obj;
505 4a5fcab7 Anthony Liguori
506 65c0f1e9 Michael Roth
        obj = parse_value(ctxt, ap);
507 4a5fcab7 Anthony Liguori
        if (obj == NULL) {
508 4a5fcab7 Anthony Liguori
            parse_error(ctxt, token, "expecting value");
509 4a5fcab7 Anthony Liguori
            goto out;
510 4a5fcab7 Anthony Liguori
        }
511 4a5fcab7 Anthony Liguori
512 4a5fcab7 Anthony Liguori
        qlist_append_obj(list, obj);
513 4a5fcab7 Anthony Liguori
514 65c0f1e9 Michael Roth
        token = parser_context_pop_token(ctxt);
515 11e8a46c Anthony Liguori
        if (token == NULL) {
516 11e8a46c Anthony Liguori
            parse_error(ctxt, NULL, "premature EOI");
517 11e8a46c Anthony Liguori
            goto out;
518 11e8a46c Anthony Liguori
        }
519 11e8a46c Anthony Liguori
520 4a5fcab7 Anthony Liguori
        while (!token_is_operator(token, ']')) {
521 4a5fcab7 Anthony Liguori
            if (!token_is_operator(token, ',')) {
522 4a5fcab7 Anthony Liguori
                parse_error(ctxt, token, "expected separator in list");
523 4a5fcab7 Anthony Liguori
                goto out;
524 4a5fcab7 Anthony Liguori
            }
525 4a5fcab7 Anthony Liguori
526 4a5fcab7 Anthony Liguori
            token = NULL;
527 4a5fcab7 Anthony Liguori
528 65c0f1e9 Michael Roth
            obj = parse_value(ctxt, ap);
529 4a5fcab7 Anthony Liguori
            if (obj == NULL) {
530 4a5fcab7 Anthony Liguori
                parse_error(ctxt, token, "expecting value");
531 4a5fcab7 Anthony Liguori
                goto out;
532 4a5fcab7 Anthony Liguori
            }
533 4a5fcab7 Anthony Liguori
534 4a5fcab7 Anthony Liguori
            qlist_append_obj(list, obj);
535 4a5fcab7 Anthony Liguori
536 65c0f1e9 Michael Roth
            token = parser_context_pop_token(ctxt);
537 11e8a46c Anthony Liguori
            if (token == NULL) {
538 11e8a46c Anthony Liguori
                parse_error(ctxt, NULL, "premature EOI");
539 11e8a46c Anthony Liguori
                goto out;
540 11e8a46c Anthony Liguori
            }
541 4a5fcab7 Anthony Liguori
        }
542 4a5fcab7 Anthony Liguori
543 4a5fcab7 Anthony Liguori
        token = NULL;
544 4a5fcab7 Anthony Liguori
    } else {
545 65c0f1e9 Michael Roth
        token = parser_context_pop_token(ctxt);
546 4a5fcab7 Anthony Liguori
        token = NULL;
547 4a5fcab7 Anthony Liguori
    }
548 4a5fcab7 Anthony Liguori
549 4a5fcab7 Anthony Liguori
    return QOBJECT(list);
550 4a5fcab7 Anthony Liguori
551 4a5fcab7 Anthony Liguori
out:
552 65c0f1e9 Michael Roth
    parser_context_restore(ctxt, saved_ctxt);
553 4a5fcab7 Anthony Liguori
    QDECREF(list);
554 4a5fcab7 Anthony Liguori
    return NULL;
555 4a5fcab7 Anthony Liguori
}
556 4a5fcab7 Anthony Liguori
557 65c0f1e9 Michael Roth
static QObject *parse_keyword(JSONParserContext *ctxt)
558 4a5fcab7 Anthony Liguori
{
559 4a5fcab7 Anthony Liguori
    QObject *token, *ret;
560 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = parser_context_save(ctxt);
561 4a5fcab7 Anthony Liguori
562 65c0f1e9 Michael Roth
    token = parser_context_pop_token(ctxt);
563 11e8a46c Anthony Liguori
    if (token == NULL) {
564 11e8a46c Anthony Liguori
        goto out;
565 11e8a46c Anthony Liguori
    }
566 4a5fcab7 Anthony Liguori
567 4a5fcab7 Anthony Liguori
    if (token_get_type(token) != JSON_KEYWORD) {
568 4a5fcab7 Anthony Liguori
        goto out;
569 4a5fcab7 Anthony Liguori
    }
570 4a5fcab7 Anthony Liguori
571 4a5fcab7 Anthony Liguori
    if (token_is_keyword(token, "true")) {
572 4a5fcab7 Anthony Liguori
        ret = QOBJECT(qbool_from_int(true));
573 4a5fcab7 Anthony Liguori
    } else if (token_is_keyword(token, "false")) {
574 4a5fcab7 Anthony Liguori
        ret = QOBJECT(qbool_from_int(false));
575 4a5fcab7 Anthony Liguori
    } else {
576 4a5fcab7 Anthony Liguori
        parse_error(ctxt, token, "invalid keyword `%s'", token_get_value(token));
577 4a5fcab7 Anthony Liguori
        goto out;
578 4a5fcab7 Anthony Liguori
    }
579 4a5fcab7 Anthony Liguori
580 4a5fcab7 Anthony Liguori
    return ret;
581 4a5fcab7 Anthony Liguori
582 4a5fcab7 Anthony Liguori
out: 
583 65c0f1e9 Michael Roth
    parser_context_restore(ctxt, saved_ctxt);
584 4a5fcab7 Anthony Liguori
585 4a5fcab7 Anthony Liguori
    return NULL;
586 4a5fcab7 Anthony Liguori
}
587 4a5fcab7 Anthony Liguori
588 65c0f1e9 Michael Roth
static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
589 4a5fcab7 Anthony Liguori
{
590 4a5fcab7 Anthony Liguori
    QObject *token = NULL, *obj;
591 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = parser_context_save(ctxt);
592 4a5fcab7 Anthony Liguori
593 4a5fcab7 Anthony Liguori
    if (ap == NULL) {
594 4a5fcab7 Anthony Liguori
        goto out;
595 4a5fcab7 Anthony Liguori
    }
596 4a5fcab7 Anthony Liguori
597 65c0f1e9 Michael Roth
    token = parser_context_pop_token(ctxt);
598 11e8a46c Anthony Liguori
    if (token == NULL) {
599 11e8a46c Anthony Liguori
        goto out;
600 11e8a46c Anthony Liguori
    }
601 4a5fcab7 Anthony Liguori
602 4a5fcab7 Anthony Liguori
    if (token_is_escape(token, "%p")) {
603 4a5fcab7 Anthony Liguori
        obj = va_arg(*ap, QObject *);
604 4a5fcab7 Anthony Liguori
    } else if (token_is_escape(token, "%i")) {
605 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qbool_from_int(va_arg(*ap, int)));
606 4a5fcab7 Anthony Liguori
    } else if (token_is_escape(token, "%d")) {
607 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
608 4a5fcab7 Anthony Liguori
    } else if (token_is_escape(token, "%ld")) {
609 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
610 2c0d4b36 Roy Tam
    } else if (token_is_escape(token, "%lld") ||
611 2c0d4b36 Roy Tam
               token_is_escape(token, "%I64d")) {
612 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qint_from_int(va_arg(*ap, long long)));
613 4a5fcab7 Anthony Liguori
    } else if (token_is_escape(token, "%s")) {
614 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
615 4a5fcab7 Anthony Liguori
    } else if (token_is_escape(token, "%f")) {
616 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qfloat_from_double(va_arg(*ap, double)));
617 4a5fcab7 Anthony Liguori
    } else {
618 4a5fcab7 Anthony Liguori
        goto out;
619 4a5fcab7 Anthony Liguori
    }
620 4a5fcab7 Anthony Liguori
621 4a5fcab7 Anthony Liguori
    return obj;
622 4a5fcab7 Anthony Liguori
623 4a5fcab7 Anthony Liguori
out:
624 65c0f1e9 Michael Roth
    parser_context_restore(ctxt, saved_ctxt);
625 4a5fcab7 Anthony Liguori
626 4a5fcab7 Anthony Liguori
    return NULL;
627 4a5fcab7 Anthony Liguori
}
628 4a5fcab7 Anthony Liguori
629 65c0f1e9 Michael Roth
static QObject *parse_literal(JSONParserContext *ctxt)
630 4a5fcab7 Anthony Liguori
{
631 4a5fcab7 Anthony Liguori
    QObject *token, *obj;
632 65c0f1e9 Michael Roth
    JSONParserContext saved_ctxt = parser_context_save(ctxt);
633 4a5fcab7 Anthony Liguori
634 65c0f1e9 Michael Roth
    token = parser_context_pop_token(ctxt);
635 11e8a46c Anthony Liguori
    if (token == NULL) {
636 11e8a46c Anthony Liguori
        goto out;
637 11e8a46c Anthony Liguori
    }
638 11e8a46c Anthony Liguori
639 4a5fcab7 Anthony Liguori
    switch (token_get_type(token)) {
640 4a5fcab7 Anthony Liguori
    case JSON_STRING:
641 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qstring_from_escaped_str(ctxt, token));
642 4a5fcab7 Anthony Liguori
        break;
643 4a5fcab7 Anthony Liguori
    case JSON_INTEGER:
644 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qint_from_int(strtoll(token_get_value(token), NULL, 10)));
645 4a5fcab7 Anthony Liguori
        break;
646 4a5fcab7 Anthony Liguori
    case JSON_FLOAT:
647 4a5fcab7 Anthony Liguori
        /* FIXME dependent on locale */
648 4a5fcab7 Anthony Liguori
        obj = QOBJECT(qfloat_from_double(strtod(token_get_value(token), NULL)));
649 4a5fcab7 Anthony Liguori
        break;
650 4a5fcab7 Anthony Liguori
    default:
651 4a5fcab7 Anthony Liguori
        goto out;
652 4a5fcab7 Anthony Liguori
    }
653 4a5fcab7 Anthony Liguori
654 4a5fcab7 Anthony Liguori
    return obj;
655 4a5fcab7 Anthony Liguori
656 4a5fcab7 Anthony Liguori
out:
657 65c0f1e9 Michael Roth
    parser_context_restore(ctxt, saved_ctxt);
658 4a5fcab7 Anthony Liguori
659 4a5fcab7 Anthony Liguori
    return NULL;
660 4a5fcab7 Anthony Liguori
}
661 4a5fcab7 Anthony Liguori
662 65c0f1e9 Michael Roth
static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
663 4a5fcab7 Anthony Liguori
{
664 4a5fcab7 Anthony Liguori
    QObject *obj;
665 4a5fcab7 Anthony Liguori
666 65c0f1e9 Michael Roth
    obj = parse_object(ctxt, ap);
667 4a5fcab7 Anthony Liguori
    if (obj == NULL) {
668 65c0f1e9 Michael Roth
        obj = parse_array(ctxt, ap);
669 4a5fcab7 Anthony Liguori
    }
670 4a5fcab7 Anthony Liguori
    if (obj == NULL) {
671 65c0f1e9 Michael Roth
        obj = parse_escape(ctxt, ap);
672 4a5fcab7 Anthony Liguori
    }
673 4a5fcab7 Anthony Liguori
    if (obj == NULL) {
674 65c0f1e9 Michael Roth
        obj = parse_keyword(ctxt);
675 4a5fcab7 Anthony Liguori
    } 
676 4a5fcab7 Anthony Liguori
    if (obj == NULL) {
677 65c0f1e9 Michael Roth
        obj = parse_literal(ctxt);
678 4a5fcab7 Anthony Liguori
    }
679 4a5fcab7 Anthony Liguori
680 4a5fcab7 Anthony Liguori
    return obj;
681 4a5fcab7 Anthony Liguori
}
682 4a5fcab7 Anthony Liguori
683 4a5fcab7 Anthony Liguori
QObject *json_parser_parse(QList *tokens, va_list *ap)
684 4a5fcab7 Anthony Liguori
{
685 ef749d07 Anthony Liguori
    return json_parser_parse_err(tokens, ap, NULL);
686 ef749d07 Anthony Liguori
}
687 ef749d07 Anthony Liguori
688 ef749d07 Anthony Liguori
QObject *json_parser_parse_err(QList *tokens, va_list *ap, Error **errp)
689 ef749d07 Anthony Liguori
{
690 65c0f1e9 Michael Roth
    JSONParserContext *ctxt = parser_context_new(tokens);
691 4a5fcab7 Anthony Liguori
    QObject *result;
692 4a5fcab7 Anthony Liguori
693 65c0f1e9 Michael Roth
    if (!ctxt) {
694 c1990ebf Michael Roth
        return NULL;
695 c1990ebf Michael Roth
    }
696 4a5fcab7 Anthony Liguori
697 65c0f1e9 Michael Roth
    result = parse_value(ctxt, ap);
698 65c0f1e9 Michael Roth
699 65c0f1e9 Michael Roth
    error_propagate(errp, ctxt->err);
700 4a5fcab7 Anthony Liguori
701 65c0f1e9 Michael Roth
    parser_context_free(ctxt);
702 ef749d07 Anthony Liguori
703 4a5fcab7 Anthony Liguori
    return result;
704 4a5fcab7 Anthony Liguori
}