diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2026-06-26 12:17:21 +0200 |
|---|---|---|
| committer | Markus Armbruster <armbru@redhat.com> | 2026-07-02 13:48:56 +0200 |
| commit | 3b2f714e4a17d42b75d92422b191ab095cbf7c6b (patch) | |
| tree | 89777c1ea813ab7ff3f278a238714cb496e9832c /include | |
| parent | a21232a2558e947e5f9d46de9b9590d8e006050b (diff) | |
| download | qemu-3b2f714e4a17d42b75d92422b191ab095cbf7c6b.tar.gz qemu-3b2f714e4a17d42b75d92422b191ab095cbf7c6b.zip | |
json-parser: replace with a push parser
In order to avoid stashing all the tokens corresponding to a JSON value,
embed the parsing stack and state machine in JSONParser. This is more
efficient and allows for more prompt error recovery; it also does not
make the code substantially larger than the current recursive descent
parser, though the state machine is probably a bit harder to follow.
The stack consists of QLists and QDicts corresponding to open
brackets and braces, plus optionally a QString with the current
key on top of each QDict.
After each value is parsed, it is added to the top array or dictionary
or, if the stack is empty, json_parser_feed returns the complete
QObject.
For now, json-streamer.c keeps tracking the tokens up until braces
and brackets are balanced, and then shoves the whole queue of tokens
into the push parser. The only logic change is that JSON_END_OF_INPUT
always triggers the emptying of the queue; the parser takes notice and
checks that there is nothing on the stack. Not using brace_count
and bracket_count for this is the first step towards improved separation
of concerns between json-parser.c and json-streamer.c.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-ID: <20260626101727.1727389-2-pbonzini@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Minor comment improvements]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Diffstat (limited to 'include')
| -rw-r--r-- | include/qobject/json-parser.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/include/qobject/json-parser.h b/include/qobject/json-parser.h index 7345a9bd5c..05346fa816 100644 --- a/include/qobject/json-parser.h +++ b/include/qobject/json-parser.h @@ -20,6 +20,12 @@ typedef struct JSONLexer { int x, y; } JSONLexer; +typedef struct JSONParserContext { + Error *err; + GQueue *stack; + va_list *ap; +} JSONParserContext; + typedef struct JSONMessageParser { void (*emit)(void *opaque, QObject *json, Error *err); void *opaque; |
