Skip to content
  • Markus Armbruster's avatar
    c0ee3afa
    json: Make lexer's "character consumed" logic less confusing · c0ee3afa
    Markus Armbruster authored
    
    
    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: default avatarMarkus Armbruster <armbru@redhat.com>
    Reviewed-by: default avatarEric Blake <eblake@redhat.com>
    Message-Id: <20180831075841.13363-4-armbru@redhat.com>
    c0ee3afa
    json: Make lexer's "character consumed" logic less confusing
    Markus Armbruster authored
    
    
    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: default avatarMarkus Armbruster <armbru@redhat.com>
    Reviewed-by: default avatarEric Blake <eblake@redhat.com>
    Message-Id: <20180831075841.13363-4-armbru@redhat.com>
Loading