@@ -1,7 +1,7 @@
2Module : Data.Syntax.Combinator
2Module : Data.Syntax.Combinator
3Description : Combinators that work with any sequence type.
3Description : 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
5License : MIT
5License : MIT
7Maintainer : Paweł Nowak <pawel834@gmail.com>
7Maintainer : Paweł Nowak <pawel834@gmail.com>
@@ -15,15 +15,6 @@ import Control.Lens
15import Control.Lens.SemiIso
15import Control.Lens.SemiIso
16import Data.SemiIsoFunctor
16import Data.SemiIsoFunctor
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
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)
27-- | One or zero occurences of @f@.
18-- | One or zero occurences of @f@.
28optional :: SemiIsoAlternative f => f a -> f (Maybe a)
19optional :: SemiIsoAlternative f => f a -> f (Maybe a)
29optional f = _Just /$/ f /|/ sipure _Nothing
20optional f = _Just /$/ f /|/ sipure _Nothing
@@ -36,3 +27,26 @@ opt f = f /|/ sipure id
36opt_ :: SemiIsoAlternative f => f () -> f ()
27opt_ :: SemiIsoAlternative f => f () -> f ()
37opt_ f = semiIso (const (Left "opt_")) Right /$/ f
28opt_ f = semiIso (const (Left "opt_")) Right /$/ f
38 /|/ sipure id
29 /|/ sipure id
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
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
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)
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")
50+-- | Combine two alternatives.
51+eitherOf :: SemiIsoAlternative f => f a -> f b -> f (Either a b)
52+eitherOf a b = _Left /$/ a /|/ _Right /$/ b