[Haskell-cafe] Is there any way to use somethig like global backtrack with parsec

Erik Rantapaa erantapaa at gmail.com
Tue May 31 01:52:50 UTC 2016


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?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160530/f755291b/attachment.html>


More information about the Haskell-Cafe mailing list