descriptive-0.9.4: Self-describing consumers/parsers; forms, cmd-line args, JSON, etc.

Safe Haskell | Safe
Language | Haskell98

Descriptive

Contents

- Consuming and describing
- Lower-level runners
- Types
- Combinators

Description

Descriptive parsers.

Synopsis

- consume :: Consumer s d Identity a -> s -> Result (Description d) a
- describe :: Consumer s d Identity a -> s -> Description d
- runConsumer :: Monad m => Consumer s d m a -> StateT s m (Result (Description d) a)
- runDescription :: Monad m => Consumer s d m a -> StateT s m (Description d)
- data Description a
  - = Unit !a
  - | Bounded !Integer !Bound !(Description a)
  - | And !(Description a) !(Description a)
  - | Or !(Description a) !(Description a)
  - | Sequence ![Description a]
  - | Wrap a !(Description a)
  - | None
- data Bound
  - = NaturalBound !Integer
  - | UnlimitedBound
- data Consumer s d m a = Consumer {
  - consumerDesc :: StateT s m (Description d)
  - consumerParse :: StateT s m (Result (Description d) a)
  }
- data Result e a
  - = Failed e
  - | Succeeded a
  - | Continued e
- consumer :: StateT s m (Description d) -> StateT s m (Result (Description d) a) -> Consumer s d m a
- wrap :: (StateT t m (Description d) -> StateT s m (Description d)) -> (StateT t m (Description d) -> StateT t m (Result (Description d) a) -> StateT s m (Result (Description d) b)) -> Consumer t d m a -> Consumer s d m b

# Consuming and describing

consume #

Arguments

:: Consumer s d Identity a | The consumer to run.
-> s | Initial state.
-> Result (Description d) a |

Run a consumer.

describe #

Arguments

:: Consumer s d Identity a | The consumer to run.
-> s | Initial state. Can be "empty" if you don't use it for generating descriptions.
-> Description d | A description and resultant state.

Describe a consumer.

# Lower-level runners

runConsumer #

Arguments

:: Monad m |
=> Consumer s d m a | The consumer to run.
-> StateT s m (Result (Description d) a) |

Run a consumer.

runDescription #

Arguments

:: Monad m |
=> Consumer s d m a | The consumer to run.
-> StateT s m (Description d) | A description and resultant state.

Describe a consumer.

# Types

data Description a #

Description of a consumable thing.

Constructors

Unit !a |
Bounded !Integer !Bound !(Description a) |
And !(Description a) !(Description a) |
Or !(Description a) !(Description a) |
Sequence ![Description a] |
Wrap a !(Description a) |
None |

Instances

Functor Description # |
Methods fmap :: (a -> b) -> Description a -> Description b # (<$) :: a -> Description b -> Description a #
Eq a => Eq (Description a) # |
Methods (==) :: Description a -> Description a -> Bool # (/=) :: Description a -> Description a -> Bool #
Show a => Show (Description a) # |
Methods showsPrec :: Int -> Description a -> ShowS # show :: Description a -> String # showList :: [Description a] -> ShowS #
Monoid (Description d) # |
Methods mempty :: Description d # mappend :: Description d -> Description d -> Description d # mconcat :: [Description d] -> Description d #
Monoid a => Monoid (Result (Description d) a) # |
Methods mempty :: Result (Description d) a # mappend :: Result (Description d) a -> Result (Description d) a -> Result (Description d) a # mconcat :: [Result (Description d) a] -> Result (Description d) a #

data Bound #

The bounds of a many-consumable thing.

Constructors

NaturalBound !Integer |
UnlimitedBound |

Instances

Eq Bound # |
Methods (==) :: Bound -> Bound -> Bool # (/=) :: Bound -> Bound -> Bool #
Show Bound # |
Methods showsPrec :: Int -> Bound -> ShowS # show :: Bound -> String # showList :: [Bound] -> ShowS #

data Consumer s d m a #

A consumer.

Constructors

Consumer |
Fields consumerDesc :: StateT s m (Description d) consumerParse :: StateT s m (Result (Description d) a)

Instances

Monad m => Functor (Consumer s d m) # |
Methods fmap :: (a -> b) -> Consumer s d m a -> Consumer s d m b # (<$) :: a -> Consumer s d m b -> Consumer s d m a #
Monad m => Applicative (Consumer s d m) # |
Methods pure :: a -> Consumer s d m a # (<*>) :: Consumer s d m (a -> b) -> Consumer s d m a -> Consumer s d m b # (*>) :: Consumer s d m a -> Consumer s d m b -> Consumer s d m b # (<*) :: Consumer s d m a -> Consumer s d m b -> Consumer s d m a #
Monad m => Alternative (Consumer s d m) # |
Methods empty :: Consumer s d m a # (<|>) :: Consumer s d m a -> Consumer s d m a -> Consumer s d m a # some :: Consumer s d m a -> Consumer s d m [a] # many :: Consumer s d m a -> Consumer s d m [a] #
(Monoid a, Monad m) => Monoid (Consumer s d m a) # |
Methods mempty :: Consumer s d m a # mappend :: Consumer s d m a -> Consumer s d m a -> Consumer s d m a # mconcat :: [Consumer s d m a] -> Consumer s d m a #

data Result e a #

Some result.

Constructors

Failed e | The whole process failed.
Succeeded a | The whole process succeeded.
Continued e | There were errors but we continued to collect all the errors.

Instances

Bifunctor Result # |
Methods bimap :: (a -> b) -> (c -> d) -> Result a c -> Result b d # first :: (a -> b) -> Result a c -> Result b c # second :: (b -> c) -> Result a b -> Result a c #
(Eq a, Eq e) => Eq (Result e a) # |
Methods (==) :: Result e a -> Result e a -> Bool # (/=) :: Result e a -> Result e a -> Bool #
(Ord a, Ord e) => Ord (Result e a) # |
Methods compare :: Result e a -> Result e a -> Ordering # (<) :: Result e a -> Result e a -> Bool # (<=) :: Result e a -> Result e a -> Bool # (>) :: Result e a -> Result e a -> Bool # (>=) :: Result e a -> Result e a -> Bool # max :: Result e a -> Result e a -> Result e a # min :: Result e a -> Result e a -> Result e a #
(Show a, Show e) => Show (Result e a) # |
Methods showsPrec :: Int -> Result e a -> ShowS # show :: Result e a -> String # showList :: [Result e a] -> ShowS #
Monoid a => Monoid (Result (Description d) a) # |
Methods mempty :: Result (Description d) a # mappend :: Result (Description d) a -> Result (Description d) a -> Result (Description d) a # mconcat :: [Result (Description d) a] -> Result (Description d) a #

# Combinators

consumer #

Arguments

:: StateT s m (Description d) | Produce description based on the state.
-> StateT s m (Result (Description d) a) | Parse the state and maybe transform it if desired.
-> Consumer s d m a |

Make a self-describing consumer.

wrap #

Arguments

:: (StateT t m (Description d) -> StateT s m (Description d)) | Transform the description.
-> (StateT t m (Description d) -> StateT t m (Result (Description d) a) -> StateT s m (Result (Description d) b)) | Transform the parser. Can re-run the parser as many times as desired.
-> Consumer t d m a |
-> Consumer s d m b |

Wrap a consumer with another consumer. The type looks more intimidating than it actually is. The source code is trivial. It simply allows for a way to transform the type of the state.