on master help sign in
New combinators!
commit49f4ec91cd12dc4343355f9292b9aeca39347183 authorPaweł Nowak <pawel834@gmail.com> dateSun 23 Nov 2014, 18:17 (12 years ago) parent58e1ce6
2 files changed+26−12
Data/Syntax/Combinator.hs+24−10 syntax.cabal+2−2
Data/Syntax/Combinator.hs+24−10
@@ -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
syntax.cabal+2−2
@@ -1,5 +1,5 @@
11name: syntax
2−version: 0.2.0.0
2+version: 0.2.1.0
33synopsis: Abstract syntax descriptions for parsing and pretty-printing.
44description: Abstract syntax descriptions for parsing and pretty-printing.
55 Write a single syntax description, get both a parser and a pretty-printer.
@@ -27,5 +27,5 @@ library
2727 exposed-modules: Data.Syntax
2828 Data.Syntax.Char
2929 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
3131 default-language: Haskell2010