Rewrite some lexer logic to match parse.y