{-# Language GADTs #-}
-- | A context-free parser that can handle ambiguous left-recursive grammars and carry a monadic computation with each
-- parsing result.
module Text.Grampa.ContextFree.SortedMemoizing.Transformer.LeftRecursive (
   ParserT, SeparatedParser(..), AmbiguityDecidable,
   lift, liftPositive, tbind, tmap,
   autochain, parseSeparated, separated)
where

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

-- | Parser transformer for left-recursive grammars on top of 'Transformer.ParserT'.
type ParserT m = Fixed (Transformer.ParserT m)

-- | Lift a parse-free computation into the parser.
lift :: (Applicative m, Ord s) => m a -> ParserT m g s a
lift :: forall (m :: * -> *) s a (g :: (* -> *) -> *).
(Applicative m, Ord s) =>
m a -> ParserT m g s a
lift = forall (p :: ((* -> *) -> *) -> * -> * -> *) (g :: (* -> *) -> *) s
       a.
Alternative (p g s) =>
p g s a -> Fixed p g s a
liftPure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a (g :: (* -> *) -> *).
Ord 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 :: forall (m :: * -> *) b (g :: (* -> *) -> *) s a.
(Monad m, AmbiguityDecidable b) =>
ParserT m g s a -> (a -> m b) -> ParserT m g s b
tbind ParserT m g s a
p a -> m b
f = 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 (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 :: forall b (m :: * -> *) a (g :: (* -> *) -> *) s.
AmbiguityDecidable b =>
(m a -> m b) -> ParserT m g s a -> ParserT m g s b
tmap = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b (g :: (* -> *) -> *) s.
(m a -> m b) -> ParserT m g s a -> ParserT m g s b
Transformer.tmap