module Annotations.F.ParserCombinators (
mkBounded, unit, chainr, chainl
) where
import Annotations.Bounds
import Annotations.BoundsParser
import Annotations.F.Annotated
import qualified Text.Parsec as P
import Data.Function
mkBounded :: Monad m => Range -> AnnFix1 Bounds f -> P s m (AnnFix Bounds f)
mkBounded left x = do
right <- getPos
return (mkAnnFix (Bounds left right) x)
unit :: Monad m => P s m (AnnFix1 Bounds f) -> P s m (AnnFix Bounds f)
unit p = do
left <- getPos
x <- p
mkBounded left x
chainr :: Monad m => P s m (AnnFix Bounds f) ->
P s m (AnnFix Bounds f -> AnnFix Bounds f -> AnnFix1 Bounds f) -> P s m (AnnFix Bounds f)
chainr px pf = fix $ \loop -> do
left <- getPos
x <- px
P.option x $ do
f <- pf
y <- loop
mkBounded left (f x y)
chainl :: Monad m => P s m (AnnFix Bounds f) ->
P s m (AnnFix Bounds f -> AnnFix Bounds f -> AnnFix1 Bounds f) -> P s m (AnnFix Bounds f)
chainl px pf = do
left <- getPos
px >>= rest left
where
rest left = fix $ \loop x -> P.option x $ do
f <- pf
y <- px
mkBounded left (f x y) >>= loop