@@ -1,7 +1,7 @@
11{- |
22Module : Data.Syntax.Combinator
33Description : Combinators that work with any sequence type.
4−Copyright : (c) Paweł Nowak
4+Copyright : (c) Daan Leijen 1999-2001, Bryan O'Sullivan 2007-2014, Paweł Nowak 2014
55License : MIT
66
77Maintainer : Paweł Nowak <pawel834@gmail.com>
@@ -15,15 +15,6 @@ import Control.Lens
1515import Control.Lens.SemiIso
1616import Data.SemiIsoFunctor
1717
18−-- | Zero or more occurences of @v@ separated by @s@.
19−sepBy :: SemiIsoAlternative f => f a -> f () -> f [a]
20−sepBy v s = sepBy1 v s
21− /|/ sipure _Empty
22−
23−-- | One or more occurences of @v@ separated by @s@.
24−sepBy1 :: SemiIsoAlternative f => f a -> f () -> f [a]
25−sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /|/ sipure _Empty)
26−
2718-- | One or zero occurences of @f@.
2819optional :: SemiIsoAlternative f => f a -> f (Maybe a)
2920optional f = _Just /$/ f /|/ sipure _Nothing
@@ -36,3 +27,26 @@ opt f = f /|/ sipure id
3627opt_ :: SemiIsoAlternative f => f () -> f ()
3728opt_ f = semiIso (const (Left "opt_")) Right /$/ f
3829 /|/ sipure id
30+
31+-- | @manyTill p end@ applies action p zero or more times until action
32+-- end succeeds, and returns the list of values returned by p.
33+manyTill :: SemiIsoAlternative f => f a -> f () -> f [a]
34+manyTill p end = _Empty /$/ end
35+ /|/ _Cons /$/ p /*/ manyTill p end
36+
37+-- | Zero or more occurences of @v@ separated by @s@.
38+sepBy :: SemiIsoAlternative f => f a -> f () -> f [a]
39+sepBy v s = sepBy1 v s /|/ sipure _Empty
40+
41+-- | One or more occurences of @v@ separated by @s@.
42+sepBy1 :: SemiIsoAlternative f => f a -> f () -> f [a]
43+sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /|/ sipure _Empty)
44+
45+-- | Tries to apply the actions in the list in order, until one of
46+-- them succeeds. Returns the value of the succeeding action.
47+choice :: SemiIsoAlternative f => [f a] -> f a
48+choice = foldr (/|/) (sifail "choice: all alternatives failed")
49+
50+-- | Combine two alternatives.
51+eitherOf :: SemiIsoAlternative f => f a -> f b -> f (Either a b)
52+eitherOf a b = _Left /$/ a /|/ _Right /$/ b