Received: from eggs.gnu.org ([2001:4830:134:3::10]:49954) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1b7r-0006Du-Hl for qemu-devel@nongnu.org; Wed, 25 Nov 2015 09:32:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a1b7q-0001Xf-AO for qemu-devel@nongnu.org; Wed, 25 Nov 2015 09:32:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:57095) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1b7q-0001XV-0N for qemu-devel@nongnu.org; Wed, 25 Nov 2015 09:32:50 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id B98B4C0B7AAD for ; Wed, 25 Nov 2015 14:32:49 +0000 (UTC) From: Markus Armbruster References: <1448300659-23559-1-git-send-email-pbonzini@redhat.com> <1448300659-23559-3-git-send-email-pbonzini@redhat.com> Date: Wed, 25 Nov 2015 15:32:47 +0100 In-Reply-To: <1448300659-23559-3-git-send-email-pbonzini@redhat.com> (Paolo Bonzini's message of "Mon, 23 Nov 2015 18:44:17 +0100") Message-ID: <87k2p62lps.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v2 2/4] qjson: do not save/restore contexts List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: qemu-devel@nongnu.org Paolo Bonzini writes: > JSON is LL(1) and our parser indeed needs only 1 token lookahead. > Saving the parser context is mostly unnecessary; "Mit Kanonen auf Spatzen" (shooting cannons at sparrows). > we can replace it > with peeking at the next token, or remove it altogether when the > restore only happens on errors. The token list is destroyed anyway > on errors. > > The only interesting thing is that parse_keyword always eats > a TOKEN_KEYWORD, even if it is invalid, so it must come last in > parse_value (otherwise, NULL is returned, parse_literal is invoked > and it tries to peek beyond end of input). This is caught by > /errors/unterminated/literal, which actually checks for an unterminated > keyword. =E0=B2=A0_=E0=B2=A0 > > Signed-off-by: Paolo Bonzini > --- > qobject/json-parser.c | 59 ++++++++++++++++++---------------------------= ------ > 1 file changed, 21 insertions(+), 38 deletions(-) > > diff --git a/qobject/json-parser.c b/qobject/json-parser.c > index ac991ba..7a287ea 100644 > --- a/qobject/json-parser.c > +++ b/qobject/json-parser.c > @@ -296,23 +296,6 @@ static QObject *parser_context_peek_token(JSONParser= Context *ctxt) > return token; > } >=20=20 > -static JSONParserContext parser_context_save(JSONParserContext *ctxt) > -{ > - JSONParserContext saved_ctxt =3D {0}; > - saved_ctxt.tokens.pos =3D ctxt->tokens.pos; > - saved_ctxt.tokens.count =3D ctxt->tokens.count; > - saved_ctxt.tokens.buf =3D ctxt->tokens.buf; > - return saved_ctxt; > -} > - > -static void parser_context_restore(JSONParserContext *ctxt, > - JSONParserContext saved_ctxt) > -{ > - ctxt->tokens.pos =3D saved_ctxt.tokens.pos; > - ctxt->tokens.count =3D saved_ctxt.tokens.count; > - ctxt->tokens.buf =3D saved_ctxt.tokens.buf; > -} > - This saves and restores tokens, which is an array @buf of @count tokens with a cursor @pos. @buf and count only ever change in parser_context_new(). Saving and restoring them has always been pointless. What this actually does is saving and restoring the cursor. > static void tokens_append_from_iter(QObject *obj, void *opaque) > { > JSONParserContext *ctxt =3D opaque; > @@ -364,7 +347,6 @@ static void parser_context_free(JSONParserContext *ct= xt) > static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) > { > QObject *key =3D NULL, *token =3D NULL, *value, *peek; > - JSONParserContext saved_ctxt =3D parser_context_save(ctxt); >=20=20 > peek =3D parser_context_peek_token(ctxt); > if (peek =3D=3D NULL) { > @@ -402,7 +384,6 @@ static int parse_pair(JSONParserContext *ctxt, QDict = *dict, va_list *ap) > return 0; >=20=20 > out: > - parser_context_restore(ctxt, saved_ctxt); > qobject_decref(key); >=20=20 > return -1; Before your patch: on error, we rewind the cursor to where it was on entry. Useful in a backtracking parser, but this shouldn't be one. Now: we leave it wherever we error out. Fine, because the sole caller parse_object() won't actually use it on error. > @@ -412,9 +393,8 @@ static QObject *parse_object(JSONParserContext *ctxt,= va_list *ap) > { > QDict *dict =3D NULL; > QObject *token, *peek; > - JSONParserContext saved_ctxt =3D parser_context_save(ctxt); >=20=20 > - token =3D parser_context_pop_token(ctxt); > + token =3D parser_context_peek_token(ctxt); > if (token =3D=3D NULL) { > goto out; > } > @@ -425,6 +405,7 @@ static QObject *parse_object(JSONParserContext *ctxt,= va_list *ap) >=20=20 > dict =3D qdict_new(); >=20=20 > + parser_context_pop_token(ctxt); > peek =3D parser_context_peek_token(ctxt); > if (peek =3D=3D NULL) { > parse_error(ctxt, NULL, "premature EOI"); > @@ -465,7 +446,6 @@ static QObject *parse_object(JSONParserContext *ctxt,= va_list *ap) > return QOBJECT(dict); >=20=20 > out: > - parser_context_restore(ctxt, saved_ctxt); > QDECREF(dict); > return NULL; > } I'm not 100% when exactly parser_context_peek_token() returns null, but I think can see what your patch does anyway. Before: if we can parse an object, advance cursor behind the object and return it, else leave the cursor alone and return null. The sole caller parse_value() relies on this behavior to try alternatives until one succeeds. After: if we can parse an object, same as before, else advance the cursor to right before the offending token and return null. Consider QMP input { 1 [ 2 ] }. json_parser_parse_err(): parse_value() @ pos=3D0: triy parse_object() @ pos=3D0: consume '{' parse_pair() @ pos=3D1: parse_value() @ pos=3D1: try ..., consume 1, now pos=3D2 fail with "key is not a string in object" propagate failure try parse_array() @ pos=3D2: consume [ 2 ], return the QList, now pos=3D5 return the QList throw away the error (caller json_parser_parse() passed errp=3DNULL) return the QList Fortunately, that's not valid QMP, and we get {"error": {"class": "GenericError", "desc": "Expected 'object' in QMP i= nput"}} Shouldn't be hard to fix. we just have to complete the job of turning this thing into a recursive descent parser. I'll give it a shot. [...]