yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qjson-Convert-to-parser-to-recursive-descent.patch

34b321
From 2975abb487a54b49246646db9aa40ee6d1beaa97 Mon Sep 17 00:00:00 2001
34b321
From: Markus Armbruster <armbru@redhat.com>
34b321
Date: Wed, 27 Jul 2016 07:35:08 +0200
34b321
Subject: [PATCH 10/16] qjson: Convert to parser to recursive descent
34b321
34b321
RH-Author: Markus Armbruster <armbru@redhat.com>
34b321
Message-id: <1469604913-12442-12-git-send-email-armbru@redhat.com>
34b321
Patchwork-id: 71474
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 10/15] qjson: Convert to parser to recursive descent
34b321
Bugzilla: 1276036
34b321
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
RH-Acked-by: John Snow <jsnow@redhat.com>
34b321
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
34b321
34b321
We backtrack in parse_value(), even though JSON is LL(1) and thus can
34b321
be parsed by straightforward recursive descent.  Do exactly that.
34b321
34b321
Based on an almost-correct patch from Paolo Bonzini.
34b321
34b321
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
34b321
Signed-off-by: Markus Armbruster <armbru@redhat.com>
34b321
Message-Id: <1448486613-17634-10-git-send-email-armbru@redhat.com>
34b321
Reviewed-by: Eric Blake <eblake@redhat.com>
34b321
(cherry picked from commit d538b25543f4db026bb435066e2403a542522c40)
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
34b321
Conflicts:
34b321
	qobject/json-parser.c
34b321
34b321
Straighforward conflicts because lacking commit fc48ffc "qobject: Use
34b321
'bool' for qbool", we still use qbool_from_int(), and we lack commit
34b321
e549e71 "json-parser: Accept 'null' in QMP".
34b321
34b321
Signed-off-by: Markus Armbruster <armbru@redhat.com>
34b321
---
34b321
 qobject/json-parser.c | 163 ++++++++++++++------------------------------------
34b321
 1 file changed, 46 insertions(+), 117 deletions(-)
34b321
34b321
diff --git a/qobject/json-parser.c b/qobject/json-parser.c
34b321
index 79f4173..b242fba 100644
34b321
--- a/qobject/json-parser.c
34b321
+++ b/qobject/json-parser.c
34b321
@@ -266,23 +266,6 @@ static QObject *parser_context_peek_token(JSONParserContext *ctxt)
34b321
     return token;
34b321
 }
34b321
 
34b321
-static JSONParserContext parser_context_save(JSONParserContext *ctxt)
34b321
-{
34b321
-    JSONParserContext saved_ctxt = {0};
34b321
-    saved_ctxt.tokens.pos = ctxt->tokens.pos;
34b321
-    saved_ctxt.tokens.count = ctxt->tokens.count;
34b321
-    saved_ctxt.tokens.buf = ctxt->tokens.buf;
34b321
-    return saved_ctxt;
34b321
-}
34b321
-
34b321
-static void parser_context_restore(JSONParserContext *ctxt,
34b321
-                                   JSONParserContext saved_ctxt)
34b321
-{
34b321
-    ctxt->tokens.pos = saved_ctxt.tokens.pos;
34b321
-    ctxt->tokens.count = saved_ctxt.tokens.count;
34b321
-    ctxt->tokens.buf = saved_ctxt.tokens.buf;
34b321
-}
34b321
-
34b321
 static void tokens_append_from_iter(QObject *obj, void *opaque)
34b321
 {
34b321
     JSONParserContext *ctxt = opaque;
34b321
@@ -334,7 +317,6 @@ static void parser_context_free(JSONParserContext *ctxt)
34b321
 static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
34b321
 {
34b321
     QObject *key = NULL, *token = NULL, *value, *peek;
34b321
-    JSONParserContext saved_ctxt = parser_context_save(ctxt);
34b321
 
34b321
     peek = parser_context_peek_token(ctxt);
34b321
     if (peek == NULL) {
34b321
@@ -372,7 +354,6 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
34b321
     return 0;
34b321
 
34b321
 out:
34b321
-    parser_context_restore(ctxt, saved_ctxt);
34b321
     qobject_decref(key);
34b321
 
34b321
     return -1;
34b321
@@ -382,16 +363,9 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
34b321
 {
34b321
     QDict *dict = NULL;
34b321
     QObject *token, *peek;
34b321
-    JSONParserContext saved_ctxt = parser_context_save(ctxt);
34b321
 
34b321
     token = parser_context_pop_token(ctxt);
34b321
-    if (token == NULL) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (token_get_type(token) != JSON_LCURLY) {
34b321
-        goto out;
34b321
-    }
34b321
+    assert(token && token_get_type(token) == JSON_LCURLY);
34b321
 
34b321
     dict = qdict_new();
34b321
 
34b321
@@ -435,7 +409,6 @@ static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
34b321
     return QOBJECT(dict);
34b321
 
34b321
 out:
34b321
-    parser_context_restore(ctxt, saved_ctxt);
34b321
     QDECREF(dict);
34b321
     return NULL;
34b321
 }
34b321
@@ -444,16 +417,9 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
34b321
 {
34b321
     QList *list = NULL;
34b321
     QObject *token, *peek;
34b321
-    JSONParserContext saved_ctxt = parser_context_save(ctxt);
34b321
 
34b321
     token = parser_context_pop_token(ctxt);
34b321
-    if (token == NULL) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (token_get_type(token) != JSON_LSQUARE) {
34b321
-        goto out;
34b321
-    }
34b321
+    assert(token && token_get_type(token) == JSON_LSQUARE);
34b321
 
34b321
     list = qlist_new();
34b321
 
34b321
@@ -507,107 +473,70 @@ static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
34b321
     return QOBJECT(list);
34b321
 
34b321
 out:
34b321
-    parser_context_restore(ctxt, saved_ctxt);
34b321
     QDECREF(list);
34b321
     return NULL;
34b321
 }
34b321
 
34b321
 static QObject *parse_keyword(JSONParserContext *ctxt)
34b321
 {
34b321
-    QObject *token, *ret;
34b321
-    JSONParserContext saved_ctxt = parser_context_save(ctxt);
34b321
+    QObject *token;
34b321
     const char *val;
34b321
 
34b321
     token = parser_context_pop_token(ctxt);
34b321
-    if (token == NULL) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (token_get_type(token) != JSON_KEYWORD) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
+    assert(token && token_get_type(token) == JSON_KEYWORD);
34b321
     val = token_get_value(token);
34b321
 
34b321
     if (!strcmp(val, "true")) {
34b321
-        ret = QOBJECT(qbool_from_int(true));
34b321
+        return QOBJECT(qbool_from_int(true));
34b321
     } else if (!strcmp(val, "false")) {
34b321
-        ret = QOBJECT(qbool_from_int(false));
34b321
-    } else {
34b321
-        parse_error(ctxt, token, "invalid keyword '%s'", val);
34b321
-        goto out;
34b321
+        return QOBJECT(qbool_from_int(false));
34b321
     }
34b321
-
34b321
-    return ret;
34b321
-
34b321
-out: 
34b321
-    parser_context_restore(ctxt, saved_ctxt);
34b321
-
34b321
+    parse_error(ctxt, token, "invalid keyword '%s'", val);
34b321
     return NULL;
34b321
 }
34b321
 
34b321
 static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
34b321
 {
34b321
-    QObject *token = NULL, *obj;
34b321
-    JSONParserContext saved_ctxt = parser_context_save(ctxt);
34b321
+    QObject *token;
34b321
     const char *val;
34b321
 
34b321
     if (ap == NULL) {
34b321
-        goto out;
34b321
+        return NULL;
34b321
     }
34b321
 
34b321
     token = parser_context_pop_token(ctxt);
34b321
-    if (token == NULL) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
-    if (token_get_type(token) != JSON_ESCAPE) {
34b321
-        goto out;
34b321
-    }
34b321
-
34b321
+    assert(token && token_get_type(token) == JSON_ESCAPE);
34b321
     val = token_get_value(token);
34b321
 
34b321
     if (!strcmp(val, "%p")) {
34b321
-        obj = va_arg(*ap, QObject *);
34b321
+        return va_arg(*ap, QObject *);
34b321
     } else if (!strcmp(val, "%i")) {
34b321
-        obj = QOBJECT(qbool_from_int(va_arg(*ap, int)));
34b321
+        return QOBJECT(qbool_from_int(va_arg(*ap, int)));
34b321
     } else if (!strcmp(val, "%d")) {
34b321
-        obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
34b321
+        return QOBJECT(qint_from_int(va_arg(*ap, int)));
34b321
     } else if (!strcmp(val, "%ld")) {
34b321
-        obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
34b321
+        return QOBJECT(qint_from_int(va_arg(*ap, long)));
34b321
     } else if (!strcmp(val, "%lld") ||
34b321
                !strcmp(val, "%I64d")) {
34b321
-        obj = QOBJECT(qint_from_int(va_arg(*ap, long long)));
34b321
+        return QOBJECT(qint_from_int(va_arg(*ap, long long)));
34b321
     } else if (!strcmp(val, "%s")) {
34b321
-        obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
34b321
+        return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
34b321
     } else if (!strcmp(val, "%f")) {
34b321
-        obj = QOBJECT(qfloat_from_double(va_arg(*ap, double)));
34b321
-    } else {
34b321
-        goto out;
34b321
+        return QOBJECT(qfloat_from_double(va_arg(*ap, double)));
34b321
     }
34b321
-
34b321
-    return obj;
34b321
-
34b321
-out:
34b321
-    parser_context_restore(ctxt, saved_ctxt);
34b321
-
34b321
     return NULL;
34b321
 }
34b321
 
34b321
 static QObject *parse_literal(JSONParserContext *ctxt)
34b321
 {
34b321
-    QObject *token, *obj;
34b321
-    JSONParserContext saved_ctxt = parser_context_save(ctxt);
34b321
+    QObject *token;
34b321
 
34b321
     token = parser_context_pop_token(ctxt);
34b321
-    if (token == NULL) {
34b321
-        goto out;
34b321
-    }
34b321
+    assert(token);
34b321
 
34b321
     switch (token_get_type(token)) {
34b321
     case JSON_STRING:
34b321
-        obj = QOBJECT(qstring_from_escaped_str(ctxt, token));
34b321
-        break;
34b321
+        return QOBJECT(qstring_from_escaped_str(ctxt, token));
34b321
     case JSON_INTEGER: {
34b321
         /* A possibility exists that this is a whole-valued float where the
34b321
          * fractional part was left out due to being 0 (.0). It's not a big
34b321
@@ -626,46 +555,46 @@ static QObject *parse_literal(JSONParserContext *ctxt)
34b321
         errno = 0; /* strtoll doesn't set errno on success */
34b321
         value = strtoll(token_get_value(token), NULL, 10);
34b321
         if (errno != ERANGE) {
34b321
-            obj = QOBJECT(qint_from_int(value));
34b321
-            break;
34b321
+            return QOBJECT(qint_from_int(value));
34b321
         }
34b321
         /* fall through to JSON_FLOAT */
34b321
     }
34b321
     case JSON_FLOAT:
34b321
         /* FIXME dependent on locale */
34b321
-        obj = QOBJECT(qfloat_from_double(strtod(token_get_value(token), NULL)));
34b321
-        break;
34b321
+        return QOBJECT(qfloat_from_double(strtod(token_get_value(token),
34b321
+                                                 NULL)));
34b321
     default:
34b321
-        goto out;
34b321
+        abort();
34b321
     }
34b321
-
34b321
-    return obj;
34b321
-
34b321
-out:
34b321
-    parser_context_restore(ctxt, saved_ctxt);
34b321
-
34b321
-    return NULL;
34b321
 }
34b321
 
34b321
 static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
34b321
 {
34b321
-    QObject *obj;
34b321
+    QObject *token;
34b321
 
34b321
-    obj = parse_object(ctxt, ap);
34b321
-    if (obj == NULL) {
34b321
-        obj = parse_array(ctxt, ap);
34b321
-    }
34b321
-    if (obj == NULL) {
34b321
-        obj = parse_escape(ctxt, ap);
34b321
-    }
34b321
-    if (obj == NULL) {
34b321
-        obj = parse_keyword(ctxt);
34b321
-    } 
34b321
-    if (obj == NULL) {
34b321
-        obj = parse_literal(ctxt);
34b321
+    token = parser_context_peek_token(ctxt);
34b321
+    if (token == NULL) {
34b321
+        parse_error(ctxt, NULL, "premature EOI");
34b321
+        return NULL;
34b321
     }
34b321
 
34b321
-    return obj;
34b321
+    switch (token_get_type(token)) {
34b321
+    case JSON_LCURLY:
34b321
+        return parse_object(ctxt, ap);
34b321
+    case JSON_LSQUARE:
34b321
+        return parse_array(ctxt, ap);
34b321
+    case JSON_ESCAPE:
34b321
+        return parse_escape(ctxt, ap);
34b321
+    case JSON_INTEGER:
34b321
+    case JSON_FLOAT:
34b321
+    case JSON_STRING:
34b321
+        return parse_literal(ctxt);
34b321
+    case JSON_KEYWORD:
34b321
+        return parse_keyword(ctxt);
34b321
+    default:
34b321
+        parse_error(ctxt, token, "expecting value");
34b321
+        return NULL;
34b321
+    }
34b321
 }
34b321
 
34b321
 QObject *json_parser_parse(QList *tokens, va_list *ap)
34b321
-- 
34b321
1.8.3.1
34b321