{-# Language GADTs #-}
module Text.Grampa.ContextFree.LeftRecursive.Transformer (ParserT, SeparatedParser(..), AmbiguityDecidable,
                                                          lift, liftPositive, tbind, tmap,
                                                          parseSeparated, separated)
where

import Text.Grampa.ContextFree.LeftRecursive (Fixed, SeparatedParser(..),
                                              liftPositive, liftPure, mapPrimitive, parseSeparated, separated)
import qualified Text.Grampa.ContextFree.SortedMemoizing.Transformer as Transformer
import Text.Grampa.Internal (AmbiguityDecidable)

type ParserT m = Fixed (Transformer.ParserT m)

-- | Lift a parse-free computation into the parser.
lift :: Applicative m => m a -> ParserT m g s a
lift :: m a -> ParserT m g s a
lift = ParserT m g s a -> ParserT m g s a
forall (p :: ((* -> *) -> *) -> * -> * -> *) (g :: (* -> *) -> *) s
       a.
Alternative (p g s) =>
p g s a -> Fixed p g s a
liftPure (ParserT m g s a -> ParserT m g s a)
-> (m a -> ParserT m g s a) -> m a -> ParserT m g s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> ParserT m g s a
forall (m :: * -> *) a (g :: (* -> *) -> *) s.
m a -> ParserT m g s a
Transformer.lift

-- | Transform the computation carried by the parser using the monadic bind ('>>=').
tbind :: (Monad m, AmbiguityDecidable b) => ParserT m g s a -> (a -> m b) -> ParserT m g s b
tbind :: ParserT m g s a -> (a -> m b) -> ParserT m g s b
tbind ParserT m g s a
p a -> m b
f = (ParserT m g s a -> ParserT m g s b)
-> ParserT m g s a -> ParserT m g s b
forall (p :: ((* -> *) -> *) -> * -> * -> *) (g :: (* -> *) -> *) s
       a b.
AmbiguityDecidable b =>
(p g s a -> p g s b) -> Fixed p g s a -> Fixed p g s b
mapPrimitive (ParserT m g s a -> (a -> m b) -> ParserT m g s b
forall (m :: * -> *) (g :: (* -> *) -> *) s a b.
Monad m =>
ParserT m g s a -> (a -> m b) -> ParserT m g s b
`Transformer.tbind` a -> m b
f) ParserT m g s a
p

-- | Transform the computation carried by the parser.
tmap :: AmbiguityDecidable b => (m a -> m b) -> ParserT m g s a -> ParserT m g s b
tmap :: (m a -> m b) -> ParserT m g s a -> ParserT m g s b
tmap = (ParserT m g s a -> ParserT m g s b)
-> ParserT m g s a -> ParserT m g s b
forall (p :: ((* -> *) -> *) -> * -> * -> *) (g :: (* -> *) -> *) s
       a b.
AmbiguityDecidable b =>
(p g s a -> p g s b) -> Fixed p g s a -> Fixed p g s b
mapPrimitive ((ParserT m g s a -> ParserT m g s b)
 -> ParserT m g s a -> ParserT m g s b)
-> ((m a -> m b) -> ParserT m g s a -> ParserT m g s b)
-> (m a -> m b)
-> ParserT m g s a
-> ParserT m g s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m a -> m b) -> ParserT m g s a -> ParserT m g s b
forall (m :: * -> *) a b (g :: (* -> *) -> *) s.
(m a -> m b) -> ParserT m g s a -> ParserT m g s b
Transformer.tmap