
If don't have to use Parsec, you can use a parser library like ReadP / ReadS. For example: import Text.ParserCombinators.ReadP abc = (choice [ string "a", string "ab" ] ) >> char 'c' >> eof run p s = case (readP_to_S p) s of [] -> Left "no parse" ((a,_):_) -> Right a test1 = run abc "ac" test2 = run abc "abc" test3 = run abc "ab" -- should fail On Monday, May 30, 2016 at 1:15:25 PM UTC-5, Petr Sokolov wrote:
For example this parser doesn't parse "abc"
test :: Parser String test = ((try $ string "a") <|> (string "ab")) *> (string "c")
I can rewrite it so it would parse "abc", but is there any general approach to force parser to return and try other branches?
(try a <|> b) *> c
Something like
(tryOtherBranchesIfFail (try a <|> b)) *> c
So if it cannot parse "abc" with (try a *> c) it could return and try (b *> c) Or it isn't possible?