OSDN Git Service

json: Make lexer's "character consumed" logic less confusing
authorMarkus Armbruster <armbru@redhat.com>
Fri, 31 Aug 2018 07:58:38 +0000 (09:58 +0200)
committerMarkus Armbruster <armbru@redhat.com>
Mon, 24 Sep 2018 16:06:09 +0000 (18:06 +0200)
commitc0ee3afa7fa2547b5766dd25e52ced292c204d4e
treeed7c00a7a0a246f83c2242878bd5883def2e4e47
parent852dfa76b85c5d23541377809aa4bcfb4fc037db
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>
qobject/json-lexer.c
qobject/json-parser-int.h