2009-11-12 00:39:14 +08:00
|
|
|
/*
|
2018-08-24 00:40:20 +08:00
|
|
|
* JSON Parser
|
2009-11-12 00:39:14 +08: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-08-24 00:40:20 +08:00
|
|
|
#ifndef JSON_PARSER_INT_H
|
|
|
|
#define JSON_PARSER_INT_H
|
|
|
|
|
|
|
|
#include "qapi/qmp/json-parser.h"
|
2009-11-12 00:39:14 +08:00
|
|
|
|
|
|
|
typedef enum json_token_type {
|
2018-08-31 15:58:40 +08:00
|
|
|
JSON_ERROR = 0, /* must be zero, see json_lexer[] */
|
|
|
|
/* Gap for lexer states */
|
|
|
|
JSON_LCURLY = 100,
|
|
|
|
JSON_MIN = JSON_LCURLY,
|
2015-11-26 05:23:26 +08:00
|
|
|
JSON_RCURLY,
|
|
|
|
JSON_LSQUARE,
|
|
|
|
JSON_RSQUARE,
|
|
|
|
JSON_COLON,
|
|
|
|
JSON_COMMA,
|
2009-11-12 00:39:14 +08:00
|
|
|
JSON_INTEGER,
|
|
|
|
JSON_FLOAT,
|
|
|
|
JSON_KEYWORD,
|
|
|
|
JSON_STRING,
|
2018-08-24 00:40:04 +08:00
|
|
|
JSON_INTERP,
|
2018-08-24 00:40:12 +08:00
|
|
|
JSON_END_OF_INPUT,
|
json: Make lexer's "character consumed" logic less confusing
The lexer uses macro TERMINAL_NEEDED_LOOKAHEAD() to decide whether a
state transition consumes the input character. It returns true when
the state transition is defined with the TERMINAL() macro. To detect
that, it checks whether input '\0' would have resulted in the same
state transition, and the new state is not IN_ERROR.
Why does that even work? For all states, the new state on input '\0'
is either IN_ERROR or defined with TERMINAL(). If the state
transition equals the one we'd get for input '\0', it goes to IN_ERROR
or to the argument of TERMINAL(). We never use TERMINAL(IN_ERROR),
because it makes no sense. Thus, if it doesn't go to IN_ERROR, it
must be defined with TERMINAL().
Since this isn't quite confusing enough, we negate the result to get
@char_consumed, and ignore it when @flush is true.
Instead of deriving the lookahead bit from the state transition, make
it explicit. This is easier to understand, and a bit more flexible,
too.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180831075841.13363-4-armbru@redhat.com>
2018-08-31 15:58:38 +08:00
|
|
|
JSON_MAX = JSON_END_OF_INPUT
|
2009-11-12 00:39:14 +08:00
|
|
|
} JSONTokenType;
|
|
|
|
|
2018-08-24 00:40:20 +08:00
|
|
|
typedef struct JSONToken JSONToken;
|
2009-11-12 00:39:14 +08:00
|
|
|
|
2018-08-24 00:40:20 +08:00
|
|
|
/* json-lexer.c */
|
2018-08-24 00:40:05 +08:00
|
|
|
void json_lexer_init(JSONLexer *lexer, bool enable_interpolation);
|
2018-08-24 00:39:58 +08:00
|
|
|
void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size);
|
|
|
|
void json_lexer_flush(JSONLexer *lexer);
|
2009-11-12 00:39:14 +08:00
|
|
|
void json_lexer_destroy(JSONLexer *lexer);
|
|
|
|
|
2018-08-24 00:40:20 +08:00
|
|
|
/* json-streamer.c */
|
|
|
|
void json_message_process_token(JSONLexer *lexer, GString *input,
|
|
|
|
JSONTokenType type, int x, int y);
|
|
|
|
|
|
|
|
/* json-parser.c */
|
|
|
|
JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr);
|
|
|
|
QObject *json_parser_parse(GQueue *tokens, va_list *ap, Error **errp);
|
|
|
|
|
2009-11-12 00:39:14 +08:00
|
|
|
#endif
|