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

<||> question #269

Open
jakebennert opened this issue Nov 13, 2019 · 2 comments
Open

<||> question #269

jakebennert opened this issue Nov 13, 2019 · 2 comments

Comments

@jakebennert
Copy link

Is choice a comparable option instead of <||>?
https://hackage.haskell.org/package/parsec-3.1.14.0/docs/Text-Parsec-Combinator.html

@ChrisEPhifer
Copy link

I don't believe so; our implementation of <||> uses try on the first parser, which says "if you fail, restore the input state to what it was before you consumed any input". From what I can tell, choice does not restore the input state after failure.

Here's the implementation of choice:

choice :: (Stream s m t) => [ParsecT s u m a] -> ParsecT s u m a
choice ps           = foldr (<|>) mzero ps

You could certainly roll your own version of choice that uses <||> instead of <|>, though.

@ChrisEPhifer
Copy link

Thinking about this a bit more, this will actually only effect you if any of the strings you're attempting to parse with the parsers given to choice consume inputs that share a prefix... I think if all the options are common-prefix-free, you should be totally fine to use choice instead of chained uses of <||>.

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

No branches or pull requests

2 participants