Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

takeP fails with "unexpected end of input" for negative number of tokens #497

Closed
olafklinke opened this issue Nov 8, 2022 · 0 comments · Fixed by #498
Closed

takeP fails with "unexpected end of input" for negative number of tokens #497

olafklinke opened this issue Nov 8, 2022 · 0 comments · Fixed by #498
Labels

Comments

@olafklinke
Copy link
Contributor

parseTest (takeP Nothing (-1) :: Parsec Void String String) "foo"
expected behavior: return the empty string, similar to Prelude.take
actual behavior: fails with a message unexpected end of input.

While a negative number of tokens is clearly a non-sensical argument to that function, I'd expect either a more informative parse error along the lines of "can not take negative number of tokens" or a behavior analogous to Prelude.take where take (-1) = take 0.

The implementation of takeP for ParsecT explicitly uses EndOfInput, so this seems to be intended. Why? The Haddocks say:

It's guaranteed that if the parser succeeds, the requested number of tokens will be returned.

This is logically equivalent to:

If the requested number of tokens can not be returned, then the parser fails.

So yes, the parser honors the spec, but in a way inconsistent with its name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants