2009-11-11 19:39:23 +03:00
|
|
|
/*
|
2016-01-29 16:48:37 +03:00
|
|
|
* JSON Parser
|
2009-11-11 19:39:23 +03:00
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:01 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2009-11-11 19:39:23 +03:00
|
|
|
#include "qemu-common.h"
|
2016-06-09 19:48:32 +03:00
|
|
|
#include "qapi/qmp/types.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/json-parser.h"
|
|
|
|
#include "qapi/qmp/json-lexer.h"
|
2015-11-26 00:23:32 +03:00
|
|
|
#include "qapi/qmp/json-streamer.h"
|
2009-11-11 19:39:23 +03:00
|
|
|
|
|
|
|
typedef struct JSONParserContext
|
|
|
|
{
|
2011-06-01 21:14:50 +04:00
|
|
|
Error *err;
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *current;
|
2015-11-26 00:23:31 +03:00
|
|
|
GQueue *buf;
|
2009-11-11 19:39:23 +03:00
|
|
|
} JSONParserContext;
|
|
|
|
|
|
|
|
#define BUG_ON(cond) assert(!(cond))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* 0) make errors meaningful again
|
|
|
|
* 1) add geometry information to tokens
|
|
|
|
* 3) should we return a parsed size?
|
|
|
|
* 4) deal with premature EOI
|
|
|
|
*/
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
|
2009-11-11 19:39:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error handler
|
|
|
|
*/
|
2010-09-23 23:28:05 +04:00
|
|
|
static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token, const char *msg, ...)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2010-03-24 18:12:05 +03:00
|
|
|
va_list ap;
|
2011-06-01 21:14:50 +04:00
|
|
|
char message[1024];
|
2010-03-24 18:12:05 +03:00
|
|
|
va_start(ap, msg);
|
2011-06-01 21:14:50 +04:00
|
|
|
vsnprintf(message, sizeof(message), msg, ap);
|
2010-03-24 18:12:05 +03:00
|
|
|
va_end(ap);
|
2011-06-01 21:14:50 +04:00
|
|
|
if (ctxt->err) {
|
|
|
|
error_free(ctxt->err);
|
|
|
|
ctxt->err = NULL;
|
|
|
|
}
|
2014-03-22 03:42:26 +04:00
|
|
|
error_setg(&ctxt->err, "JSON parse error, %s", message);
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String helpers
|
|
|
|
*
|
|
|
|
* These helpers are used to unescape strings.
|
|
|
|
*/
|
|
|
|
static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
|
|
|
|
{
|
|
|
|
if (wchar <= 0x007F) {
|
|
|
|
BUG_ON(buffer_length < 2);
|
|
|
|
|
|
|
|
buffer[0] = wchar & 0x7F;
|
|
|
|
buffer[1] = 0;
|
|
|
|
} else if (wchar <= 0x07FF) {
|
|
|
|
BUG_ON(buffer_length < 3);
|
|
|
|
|
|
|
|
buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
|
|
|
|
buffer[1] = 0x80 | (wchar & 0x3F);
|
|
|
|
buffer[2] = 0;
|
|
|
|
} else {
|
|
|
|
BUG_ON(buffer_length < 4);
|
|
|
|
|
|
|
|
buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
|
|
|
|
buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
|
|
|
|
buffer[2] = 0x80 | (wchar & 0x3F);
|
|
|
|
buffer[3] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hex2decimal(char ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
return (ch - '0');
|
|
|
|
} else if (ch >= 'a' && ch <= 'f') {
|
|
|
|
return 10 + (ch - 'a');
|
|
|
|
} else if (ch >= 'A' && ch <= 'F') {
|
|
|
|
return 10 + (ch - 'A');
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse_string(): Parse a json string and return a QObject
|
|
|
|
*
|
|
|
|
* string
|
|
|
|
* ""
|
|
|
|
* " chars "
|
|
|
|
* chars
|
|
|
|
* char
|
|
|
|
* char chars
|
|
|
|
* char
|
|
|
|
* any-Unicode-character-
|
|
|
|
* except-"-or-\-or-
|
|
|
|
* control-character
|
|
|
|
* \"
|
|
|
|
* \\
|
|
|
|
* \/
|
|
|
|
* \b
|
|
|
|
* \f
|
|
|
|
* \n
|
|
|
|
* \r
|
|
|
|
* \t
|
|
|
|
* \u four-hex-digits
|
|
|
|
*/
|
2015-11-26 00:23:32 +03:00
|
|
|
static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
|
|
|
|
JSONToken *token)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
const char *ptr = token->str;
|
2009-11-11 19:39:23 +03:00
|
|
|
QString *str;
|
|
|
|
int double_quote = 1;
|
|
|
|
|
|
|
|
if (*ptr == '"') {
|
|
|
|
double_quote = 1;
|
|
|
|
} else {
|
|
|
|
double_quote = 0;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
str = qstring_new();
|
|
|
|
while (*ptr &&
|
|
|
|
((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
|
|
|
|
if (*ptr == '\\') {
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
switch (*ptr) {
|
|
|
|
case '"':
|
|
|
|
qstring_append(str, "\"");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
qstring_append(str, "'");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
qstring_append(str, "\\");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
qstring_append(str, "/");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
qstring_append(str, "\b");
|
|
|
|
ptr++;
|
|
|
|
break;
|
2010-05-20 00:06:15 +04:00
|
|
|
case 'f':
|
|
|
|
qstring_append(str, "\f");
|
|
|
|
ptr++;
|
|
|
|
break;
|
2009-11-11 19:39:23 +03:00
|
|
|
case 'n':
|
|
|
|
qstring_append(str, "\n");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
qstring_append(str, "\r");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
qstring_append(str, "\t");
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case 'u': {
|
|
|
|
uint16_t unicode_char = 0;
|
|
|
|
char utf8_char[4];
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (qemu_isxdigit(*ptr)) {
|
|
|
|
unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
|
|
|
|
} else {
|
|
|
|
parse_error(ctxt, token,
|
|
|
|
"invalid hex escape sequence in string");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
|
|
|
|
qstring_append(str, utf8_char);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
parse_error(ctxt, token, "invalid escape sequence in string");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char dummy[2];
|
|
|
|
|
|
|
|
dummy[0] = *ptr++;
|
|
|
|
dummy[1] = 0;
|
|
|
|
|
|
|
|
qstring_append(str, dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
|
|
|
out:
|
|
|
|
QDECREF(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
/* Note: the token object returned by parser_context_peek_token or
|
|
|
|
* parser_context_pop_token is deleted as soon as parser_context_pop_token
|
|
|
|
* is called again.
|
2015-11-26 00:23:31 +03:00
|
|
|
*/
|
2015-11-26 00:23:32 +03:00
|
|
|
static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
|
2012-08-15 22:45:43 +04:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
g_free(ctxt->current);
|
2015-11-26 00:23:31 +03:00
|
|
|
assert(!g_queue_is_empty(ctxt->buf));
|
|
|
|
ctxt->current = g_queue_pop_head(ctxt->buf);
|
|
|
|
return ctxt->current;
|
2012-08-15 22:45:43 +04:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
|
2012-08-15 22:45:43 +04:00
|
|
|
{
|
2015-11-26 00:23:31 +03:00
|
|
|
assert(!g_queue_is_empty(ctxt->buf));
|
|
|
|
return g_queue_peek_head(ctxt->buf);
|
2012-08-15 22:45:43 +04:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:31 +03:00
|
|
|
static JSONParserContext *parser_context_new(GQueue *tokens)
|
2012-08-15 22:45:43 +04:00
|
|
|
{
|
|
|
|
JSONParserContext *ctxt;
|
|
|
|
|
|
|
|
if (!tokens) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt = g_malloc0(sizeof(JSONParserContext));
|
2015-11-26 00:23:31 +03:00
|
|
|
ctxt->buf = tokens;
|
2012-08-15 22:45:43 +04:00
|
|
|
|
|
|
|
return ctxt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* to support error propagation, ctxt->err must be freed separately */
|
|
|
|
static void parser_context_free(JSONParserContext *ctxt)
|
|
|
|
{
|
|
|
|
if (ctxt) {
|
2015-11-26 00:23:31 +03:00
|
|
|
while (!g_queue_is_empty(ctxt->buf)) {
|
|
|
|
parser_context_pop_token(ctxt);
|
2012-08-15 22:45:43 +04:00
|
|
|
}
|
2015-11-26 00:23:32 +03:00
|
|
|
g_free(ctxt->current);
|
2015-11-26 00:23:31 +03:00
|
|
|
g_queue_free(ctxt->buf);
|
2012-08-15 22:45:43 +04:00
|
|
|
g_free(ctxt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-11 19:39:23 +03:00
|
|
|
/**
|
|
|
|
* Parsing rules
|
|
|
|
*/
|
2012-08-15 22:45:43 +04:00
|
|
|
static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
QObject *key = NULL, *value;
|
|
|
|
JSONToken *peek, *token;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
peek = parser_context_peek_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (peek == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
key = parse_value(ctxt, ap);
|
2010-02-24 18:17:58 +03:00
|
|
|
if (!key || qobject_type(key) != QTYPE_QSTRING) {
|
2009-11-11 19:39:23 +03:00
|
|
|
parse_error(ctxt, peek, "key is not a string in object");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
if (token->type != JSON_COLON) {
|
2009-11-11 19:39:23 +03:00
|
|
|
parse_error(ctxt, token, "missing : in object pair");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
value = parse_value(ctxt, ap);
|
2009-11-11 19:39:23 +03:00
|
|
|
if (value == NULL) {
|
|
|
|
parse_error(ctxt, token, "Missing value in dict");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
|
|
|
|
|
|
|
|
qobject_decref(key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
qobject_decref(key);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
|
|
|
QDict *dict = NULL;
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token, *peek;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-26 00:23:32 +03:00
|
|
|
assert(token && token->type == JSON_LCURLY);
|
2009-11-11 19:39:23 +03:00
|
|
|
|
|
|
|
dict = qdict_new();
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
peek = parser_context_peek_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (peek == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
if (peek->type != JSON_RCURLY) {
|
2012-08-15 22:45:43 +04:00
|
|
|
if (parse_pair(ctxt, dict, ap) == -1) {
|
2009-11-11 19:39:23 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
while (token->type != JSON_RCURLY) {
|
|
|
|
if (token->type != JSON_COMMA) {
|
2009-11-11 19:39:23 +03:00
|
|
|
parse_error(ctxt, token, "expected separator in dict");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
if (parse_pair(ctxt, dict, ap) == -1) {
|
2009-11-11 19:39:23 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 13:20:24 +04:00
|
|
|
(void)parser_context_pop_token(ctxt);
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return QOBJECT(dict);
|
|
|
|
|
|
|
|
out:
|
|
|
|
QDECREF(dict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
|
|
|
QList *list = NULL;
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token, *peek;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-26 00:23:32 +03:00
|
|
|
assert(token && token->type == JSON_LSQUARE);
|
2009-11-11 19:39:23 +03:00
|
|
|
|
|
|
|
list = qlist_new();
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
peek = parser_context_peek_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (peek == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
if (peek->type != JSON_RSQUARE) {
|
2009-11-11 19:39:23 +03:00
|
|
|
QObject *obj;
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
obj = parse_value(ctxt, ap);
|
2009-11-11 19:39:23 +03:00
|
|
|
if (obj == NULL) {
|
|
|
|
parse_error(ctxt, token, "expecting value");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qlist_append_obj(list, obj);
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
while (token->type != JSON_RSQUARE) {
|
|
|
|
if (token->type != JSON_COMMA) {
|
2009-11-11 19:39:23 +03:00
|
|
|
parse_error(ctxt, token, "expected separator in list");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
obj = parse_value(ctxt, ap);
|
2009-11-11 19:39:23 +03:00
|
|
|
if (obj == NULL) {
|
|
|
|
parse_error(ctxt, token, "expecting value");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qlist_append_obj(list, obj);
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 21:14:55 +04:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 13:20:24 +04:00
|
|
|
(void)parser_context_pop_token(ctxt);
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return QOBJECT(list);
|
|
|
|
|
|
|
|
out:
|
|
|
|
QDECREF(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_keyword(JSONParserContext *ctxt)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-26 00:23:32 +03:00
|
|
|
assert(token && token->type == JSON_KEYWORD);
|
2015-11-26 00:23:27 +03:00
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
if (!strcmp(token->str, "true")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qbool_from_bool(true));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "false")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qbool_from_bool(false));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "null")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return qnull();
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
2015-11-26 00:23:32 +03:00
|
|
|
parse_error(ctxt, token, "invalid keyword '%s'", token->str);
|
2009-11-11 19:39:23 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
|
|
|
if (ap == NULL) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return NULL;
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-26 00:23:32 +03:00
|
|
|
assert(token && token->type == JSON_ESCAPE);
|
2015-11-26 00:23:28 +03:00
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
if (!strcmp(token->str, "%p")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return va_arg(*ap, QObject *);
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "%i")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "%d")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qint_from_int(va_arg(*ap, int)));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "%ld")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qint_from_int(va_arg(*ap, long)));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "%lld") ||
|
|
|
|
!strcmp(token->str, "%I64d")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qint_from_int(va_arg(*ap, long long)));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "%s")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
|
2015-11-26 00:23:32 +03:00
|
|
|
} else if (!strcmp(token->str, "%f")) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qfloat_from_double(va_arg(*ap, double)));
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_literal(JSONParserContext *ctxt)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-26 00:23:30 +03:00
|
|
|
assert(token);
|
2011-06-01 21:14:55 +04:00
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
switch (token->type) {
|
2009-11-11 19:39:23 +03:00
|
|
|
case JSON_STRING:
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qstring_from_escaped_str(ctxt, token));
|
2013-05-11 02:46:05 +04:00
|
|
|
case JSON_INTEGER: {
|
|
|
|
/* A possibility exists that this is a whole-valued float where the
|
|
|
|
* fractional part was left out due to being 0 (.0). It's not a big
|
|
|
|
* deal to treat these as ints in the parser, so long as users of the
|
|
|
|
* resulting QObject know to expect a QInt in place of a QFloat in
|
|
|
|
* cases like these.
|
|
|
|
*
|
|
|
|
* However, in some cases these values will overflow/underflow a
|
|
|
|
* QInt/int64 container, thus we should assume these are to be handled
|
|
|
|
* as QFloats/doubles rather than silently changing their values.
|
|
|
|
*
|
|
|
|
* strtoll() indicates these instances by setting errno to ERANGE
|
|
|
|
*/
|
|
|
|
int64_t value;
|
|
|
|
|
|
|
|
errno = 0; /* strtoll doesn't set errno on success */
|
2015-11-26 00:23:32 +03:00
|
|
|
value = strtoll(token->str, NULL, 10);
|
2013-05-11 02:46:05 +04:00
|
|
|
if (errno != ERANGE) {
|
2015-11-26 00:23:30 +03:00
|
|
|
return QOBJECT(qint_from_int(value));
|
2013-05-11 02:46:05 +04:00
|
|
|
}
|
|
|
|
/* fall through to JSON_FLOAT */
|
|
|
|
}
|
2009-11-11 19:39:23 +03:00
|
|
|
case JSON_FLOAT:
|
2016-01-29 16:48:37 +03:00
|
|
|
/* FIXME dependent on locale; a pervasive issue in QEMU */
|
|
|
|
/* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
|
|
|
|
* but those might be useful extensions beyond JSON */
|
2015-11-26 00:23:32 +03:00
|
|
|
return QOBJECT(qfloat_from_double(strtod(token->str, NULL)));
|
2009-11-11 19:39:23 +03:00
|
|
|
default:
|
2015-11-26 00:23:30 +03:00
|
|
|
abort();
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2015-11-26 00:23:32 +03:00
|
|
|
JSONToken *token;
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2015-11-26 00:23:30 +03:00
|
|
|
token = parser_context_peek_token(ctxt);
|
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
return NULL;
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:32 +03:00
|
|
|
switch (token->type) {
|
2015-11-26 00:23:30 +03:00
|
|
|
case JSON_LCURLY:
|
|
|
|
return parse_object(ctxt, ap);
|
|
|
|
case JSON_LSQUARE:
|
|
|
|
return parse_array(ctxt, ap);
|
|
|
|
case JSON_ESCAPE:
|
|
|
|
return parse_escape(ctxt, ap);
|
|
|
|
case JSON_INTEGER:
|
|
|
|
case JSON_FLOAT:
|
|
|
|
case JSON_STRING:
|
|
|
|
return parse_literal(ctxt);
|
|
|
|
case JSON_KEYWORD:
|
|
|
|
return parse_keyword(ctxt);
|
|
|
|
default:
|
|
|
|
parse_error(ctxt, token, "expecting value");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-11 19:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:31 +03:00
|
|
|
QObject *json_parser_parse(GQueue *tokens, va_list *ap)
|
2011-06-01 21:14:50 +04:00
|
|
|
{
|
|
|
|
return json_parser_parse_err(tokens, ap, NULL);
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:23:31 +03:00
|
|
|
QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
|
2009-11-11 19:39:23 +03:00
|
|
|
{
|
2012-08-15 22:45:43 +04:00
|
|
|
JSONParserContext *ctxt = parser_context_new(tokens);
|
2009-11-11 19:39:23 +03:00
|
|
|
QObject *result;
|
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
if (!ctxt) {
|
2011-06-01 21:15:00 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
result = parse_value(ctxt, ap);
|
|
|
|
|
|
|
|
error_propagate(errp, ctxt->err);
|
2009-11-11 19:39:23 +03:00
|
|
|
|
2012-08-15 22:45:43 +04:00
|
|
|
parser_context_free(ctxt);
|
2011-06-01 21:14:50 +04:00
|
|
|
|
2009-11-11 19:39:23 +03:00
|
|
|
return result;
|
|
|
|
}
|