Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Parsec stop parsing with multiple parser?

I'm trying to write a simple parser in Haskell using Parsec but my input "Hello World" is never correctly parsed.

My code looks like this:

parser = p1 <|> p2

p1 = string "Hello"
p2 = string "Hello World"

If I run it I get the error unexpected whitespace

like image 815
Milzo Avatar asked Oct 26 '25 14:10

Milzo


1 Answers

p1 already consumes the tokens "Hello" and therefore p2 instantly fails because the next token is whitespace.

You could use something like try to reset the consumed tokens.

parser = try p1 <|> p2

p1 = string "Hello"
p2 = string "Hello World"
like image 158
Anton Kesy Avatar answered Oct 29 '25 05:10

Anton Kesy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!