Rewrite hardcoded lex_state checks in lexer to use helpers