Reputation: 43
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
Upvotes: 4
Views: 115
Reputation: 832
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"
Upvotes: 7