I am currently trying to write a simple parser in Parsec but keep running into problems regarding whitespace: As a minimal example, I have a parser that will parse two letters, either two lowercase or one uppercase and one lowercase. I would do this as
testP :: Parser String
testP = do
lookAhead lower
a1 <- lower
a2 <- lower
return [a1,a2]
<|> do
a1 <- upper
a2 <- lower
return [a1,a2]
This works as expected with strings like "as" or "Bs". Now I want to handle possible whitespace at the beginning of my input string. If I do
testP :: Parser String
testP = do
spaces
lookAhead lower
a1 <- lower
a2 <- lower
return [a1,a2]
<|> do
a1 <- upper
a2 <- lower
return [a1,a2]
I would expect the program to be able to parse both " as" and " Bs" now, but for the second string I get an error "expecting space or lowercase letter" instead.
Okay, I thought the spaces would be parsed regardless of which option is taken, but apparently not so let's put another spaces at the start of the second option, like this:
testP :: Parser String
testP = do
spaces
lookAhead lower
a1 <- lower
a2 <- lower
return [a1,a2]
<|> do
spaces
a1 <- upper
a2 <- lower
return [a1,a2]
This still gives me the same error when I try to parse " Bs", though. How am I misunderstanding whitespace handling here and how could I do this correctly?