Op 31 mei 2016, om 3:52  heeft Erik Rantapaa <erantapaa@gmail.com> het volgende geschreven:

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?

If you use uu-parsinglib all your problems will be gone. No need to hasskel around with “try” constructs. All alternatives are tried “in parallel”.

 Doaitse



(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?

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe