on master help sign in
New combinators!
49f4ec9Paweł Nowak
Data/Syntax/Combinator.hs+24−10
@@ -1,7 +1,7 @@
1{- |
1{- |
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
6
6
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
17
17
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−
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
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
syntax.cabal+2−2
@@ -1,5 +1,5 @@
1name: syntax
1name: syntax
2−version: 0.2.0.0
2+version: 0.2.1.0
3synopsis: Abstract syntax descriptions for parsing and pretty-printing.
3synopsis: Abstract syntax descriptions for parsing and pretty-printing.
4description: Abstract syntax descriptions for parsing and pretty-printing.
4description: Abstract syntax descriptions for parsing and pretty-printing.
5 Write a single syntax description, get both a parser and a pretty-printer.
5 Write a single syntax description, get both a parser and a pretty-printer.
@@ -27,5 +27,5 @@ library
27 exposed-modules: Data.Syntax
27 exposed-modules: Data.Syntax
28 Data.Syntax.Char
28 Data.Syntax.Char
29 Data.Syntax.Combinator
29 Data.Syntax.Combinator
30− build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 0.4, scientific >= 0.3
30+ build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 0.4.1, scientific >= 0.3
31 default-language: Haskell2010
31 default-language: Haskell2010