New combinators!

This commit is contained in:
Paweł Nowak 2014-11-23 18:17:04 +01:00
parent 58e1ce6dd0
commit 49f4ec91cd
2 changed files with 26 additions and 12 deletions

View File

@ -1,7 +1,7 @@
{- |
Module : Data.Syntax.Combinator
Description : Combinators that work with any sequence type.
Copyright : (c) Paweł Nowak
Copyright : (c) Daan Leijen 1999-2001, Bryan O'Sullivan 2007-2014, Paweł Nowak 2014
License : MIT
Maintainer : Paweł Nowak <pawel834@gmail.com>
@ -15,15 +15,6 @@ import Control.Lens
import Control.Lens.SemiIso
import Data.SemiIsoFunctor
-- | Zero or more occurences of @v@ separated by @s@.
sepBy :: SemiIsoAlternative f => f a -> f () -> f [a]
sepBy v s = sepBy1 v s
/|/ sipure _Empty
-- | One or more occurences of @v@ separated by @s@.
sepBy1 :: SemiIsoAlternative f => f a -> f () -> f [a]
sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /|/ sipure _Empty)
-- | One or zero occurences of @f@.
optional :: SemiIsoAlternative f => f a -> f (Maybe a)
optional f = _Just /$/ f /|/ sipure _Nothing
@ -36,3 +27,26 @@ opt f = f /|/ sipure id
opt_ :: SemiIsoAlternative f => f () -> f ()
opt_ f = semiIso (const (Left "opt_")) Right /$/ f
/|/ sipure id
-- | @manyTill p end@ applies action p zero or more times until action
-- end succeeds, and returns the list of values returned by p.
manyTill :: SemiIsoAlternative f => f a -> f () -> f [a]
manyTill p end = _Empty /$/ end
/|/ _Cons /$/ p /*/ manyTill p end
-- | Zero or more occurences of @v@ separated by @s@.
sepBy :: SemiIsoAlternative f => f a -> f () -> f [a]
sepBy v s = sepBy1 v s /|/ sipure _Empty
-- | One or more occurences of @v@ separated by @s@.
sepBy1 :: SemiIsoAlternative f => f a -> f () -> f [a]
sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /|/ sipure _Empty)
-- | Tries to apply the actions in the list in order, until one of
-- them succeeds. Returns the value of the succeeding action.
choice :: SemiIsoAlternative f => [f a] -> f a
choice = foldr (/|/) (sifail "choice: all alternatives failed")
-- | Combine two alternatives.
eitherOf :: SemiIsoAlternative f => f a -> f b -> f (Either a b)
eitherOf a b = _Left /$/ a /|/ _Right /$/ b

View File

@ -1,5 +1,5 @@
name: syntax
version: 0.2.0.0
version: 0.2.1.0
synopsis: Abstract syntax descriptions for parsing and pretty-printing.
description: Abstract syntax descriptions for parsing and pretty-printing.
Write a single syntax description, get both a parser and a pretty-printer.
@ -27,5 +27,5 @@ library
exposed-modules: Data.Syntax
Data.Syntax.Char
Data.Syntax.Combinator
build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 0.4, scientific >= 0.3
build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 0.4.1, scientific >= 0.3
default-language: Haskell2010