{-# LANGUAGE NoImplicitPrelude #-}

-- | More readable combinators for writing parsers.

module Data.Attoparsec.Combinators
  ( alternating
  , appending
  , concating
  , pured
  ) where

import           Stack.Prelude

-- | Concatenate two parsers.

appending ::
     (Applicative f, Semigroup a)
  => f a
  -> f a
  -> f a
appending :: forall (f :: * -> *) a.
(Applicative f, Semigroup a) =>
f a -> f a -> f a
appending f a
a f a
b = a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) (a -> a -> a) -> f a -> f (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a f (a -> a) -> f a -> f a
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
b

-- | Alternative parsers.

alternating :: Alternative f => f a -> f a -> f a
alternating :: forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
alternating f a
a f a
b = f a
a f a -> f a -> f a
forall a. f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f a
b

-- | Pure something.

pured :: (Applicative g, Applicative f) => g a -> g (f a)
pured :: forall (g :: * -> *) (f :: * -> *) a.
(Applicative g, Applicative f) =>
g a -> g (f a)
pured = (a -> f a) -> g a -> g (f a)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Concating the result of an action.

concating :: (Monoid m, Applicative f) => f [m] -> f m
concating :: forall m (f :: * -> *). (Monoid m, Applicative f) => f [m] -> f m
concating = ([m] -> m) -> f [m] -> f m
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [m] -> m
forall a. Monoid a => [a] -> a
mconcat