2009-11-11 17:39:14 +01:00
|
|
|
/*
|
2018-08-23 18:40:20 +02:00
|
|
|
* JSON Parser
|
2009-11-11 17:39:14 +01: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-23 18:40:20 +02:00
|
|
|
#ifndef JSON_PARSER_INT_H
|
|
|
|
#define JSON_PARSER_INT_H
|
|
|
|
|
|
|
|
#include "qapi/qmp/json-parser.h"
|
2009-11-11 17:39:14 +01:00
|
|
|
|
|
|
|
typedef enum json_token_type {
|
2018-08-31 09:58:40 +02:00
|
|
|
JSON_ERROR = 0, /* must be zero, see json_lexer[] */
|
|
|
|
/* Gap for lexer states */
|
|
|
|
JSON_LCURLY = 100,
|
|
|
|
JSON_MIN = JSON_LCURLY,
|
2015-11-25 22:23:26 +01:00
|
|
|
JSON_RCURLY,
|
|
|
|
JSON_LSQUARE,
|
|
|
|
JSON_RSQUARE,
|
|
|
|
JSON_COLON,
|
|
|
|
JSON_COMMA,
|
2009-11-11 17:39:14 +01:00
|
|
|
JSON_INTEGER,
|
|
|
|
JSON_FLOAT,
|
|
|
|
JSON_KEYWORD,
|
|
|
|
JSON_STRING,
|
2018-08-23 18:40:04 +02:00
|
|
|
JSON_INTERP,
|
2018-08-23 18:40:12 +02: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 09:58:38 +02:00
|
|
|
JSON_MAX = JSON_END_OF_INPUT
|
2009-11-11 17:39:14 +01:00
|
|
|
} JSONTokenType;
|
|
|
|
|
2018-08-23 18:40:20 +02:00
|
|
|
typedef struct JSONToken JSONToken;
|
2009-11-11 17:39:14 +01:00
|
|
|
|
2018-08-23 18:40:20 +02:00
|
|
|
/* json-lexer.c */
|
2018-08-23 18:40:05 +02:00
|
|
|
void json_lexer_init(JSONLexer *lexer, bool enable_interpolation);
|
2018-08-23 18:39:58 +02:00
|
|
|
void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size);
|
|
|
|
void json_lexer_flush(JSONLexer *lexer);
|
2009-11-11 17:39:14 +01:00
|
|
|
void json_lexer_destroy(JSONLexer *lexer);
|
|
|
|
|
2018-08-23 18:40:20 +02: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-11 17:39:14 +01:00
|
|
|
#endif
|