I need to parse, using Megaparsec a data structure like
data Foo
= Simple String
| Dotted Foo String
where I can have alphanumeric strings separated by dots.
For example abc
should be parsed to Simple "abc"
and abc.def
to Dotted (Simple "abc") "def"
.
My parser at the moment is like
fooParser :: Parser Foo
fooParser
= Simple <$> alphaNum
<|> do
foo <- fooParser
constant "."
end <- alphaNum
pure $ Dotted foo end
This works fine for Simple
, but it does not parse any Dotted
, because the first option always succeeds parsing the first piece of the string.
Which is the best option to fix my parser?