Search code examples
haskellparsec

Parsec optionMaybe always consuming input


I'm trying to debug why despite combining optionMaybe with try, parsec is consuming the input. Basically in the below code, why is the second run not returning Right "abc" (which is what I expect)?

Given

myParser = do
    optionMaybe $ try (between (char '/') (char '/') $ many1 (noneOf "/")
    many1 (noneOf "/")

I get

> parse myParser "" "/abc/def"
Right "def"

> parse myParser "" "/abc"
Left (line 1, column 5):
unexpected end of input
expecting "/"

Solution

  • The whole of between (char '/') (char '/') $ many1 (noneOf "/") fails due to the missing closing '/' in "/abc" , and so optionMaybe $ try $ between ... doesn't consume anything (and results in Nothing). You're left with all of "/abc" of the input, which of course doesn't match the remaining parser many1 (noneOf "/").