Text.Read.Lex.expect doesn't shortcut
Summary
Text.Read.Lex.expect
is defined thus:
expect :: Lexeme -> ReadP ()
expect lexeme = do { skipSpaces
; thing <- lexToken
; if thing == lexeme then return () else pfail }
Text.Read.Lex.lexToxen
, in turn, is defined
lexToken :: ReadP Lexeme
lexToken = lexEOF +++
lexLitChar +++
lexString +++
lexPunc +++
lexSymbol +++
lexId +++
lexNumber
So if I say expect EOF
, for example, and pass in "\"A really long string\""
, the parse won't fail until it has read through the entire string. That's pretty lousy. And there's no good excuse for it: I'm telling the lexer what I expect, so it has no good excuse for not using that information.
Expected behavior
I expect expect
to invoke a different lexer depending on the Lexeme
constructor it's passed. When practical, I expect the Lexeme
constructor argument to guide further lexing, to enable fast failure. This will require additional internal notes to make sure everything stays in sync.
Alternative: The expect
function can be documented as being slow, and avoided when possible.
Environment
- GHC version used: 9.6
Optional:
- Operating System:
- System Architecture: