Skip to content

Commit

Permalink
json: Eliminate lexer state IN_WHITESPACE, pseudo-token JSON_SKIP
Browse files Browse the repository at this point in the history
The lexer ignores whitespace like this:

         on whitespace      on non-ws   spontaneously
    IN_START --> IN_WHITESPACE --> JSON_SKIP --> IN_START
                    ^    |
                     \__/  on whitespace

This accumulates a whitespace token in state IN_WHITESPACE, only to
throw it away on the transition via JSON_SKIP to the start state.
Wasteful.  Go from IN_START to IN_START on whitespace directly,
dropping the whitespace character.

Signed-off-by: Markus Armbruster <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
Markus Armbruster committed Sep 24, 2018
1 parent 2ce4ee6 commit 1e960b4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
22 changes: 5 additions & 17 deletions qobject/json-lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ enum json_lexer_state {
IN_SIGN,
IN_KEYWORD,
IN_INTERP,
IN_WHITESPACE,
IN_START,
IN_START_INTERP, /* must be IN_START + 1 */
};
Expand Down Expand Up @@ -228,15 +227,6 @@ static const uint8_t json_lexer[][256] = {
['a' ... 'z'] = IN_KEYWORD,
},

/* whitespace */
[IN_WHITESPACE] = {
TERMINAL(JSON_SKIP),
[' '] = IN_WHITESPACE,
['\t'] = IN_WHITESPACE,
['\r'] = IN_WHITESPACE,
['\n'] = IN_WHITESPACE,
},

/* interpolation */
[IN_INTERP] = {
TERMINAL(JSON_INTERP),
Expand All @@ -263,10 +253,10 @@ static const uint8_t json_lexer[][256] = {
[','] = JSON_COMMA,
[':'] = JSON_COLON,
['a' ... 'z'] = IN_KEYWORD,
[' '] = IN_WHITESPACE,
['\t'] = IN_WHITESPACE,
['\r'] = IN_WHITESPACE,
['\n'] = IN_WHITESPACE,
[' '] = IN_START,
['\t'] = IN_START,
['\r'] = IN_START,
['\n'] = IN_START,
},
[IN_START_INTERP]['%'] = IN_INTERP,
};
Expand Down Expand Up @@ -323,10 +313,8 @@ static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
json_message_process_token(lexer, lexer->token, new_state,
lexer->x, lexer->y);
/* fall through */
case JSON_SKIP:
g_string_truncate(lexer->token, 0);
/* fall through */
case IN_START:
g_string_truncate(lexer->token, 0);
new_state = lexer->start_state;
break;
case JSON_ERROR:
Expand Down
1 change: 0 additions & 1 deletion qobject/json-parser-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ typedef enum json_token_type {
JSON_KEYWORD,
JSON_STRING,
JSON_INTERP,
JSON_SKIP,
JSON_END_OF_INPUT,
JSON_MAX = JSON_END_OF_INPUT
} JSONTokenType;
Expand Down

0 comments on commit 1e960b4

Please sign in to comment.