{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Circuit.Parser.ProcessCompiler
(
compileProcess,
compileProcessWithInput,
)
where
import Circuit.Parser (Uncons (..))
import Circuit.Parser.Syntax (ParserSyntax, derive, nullableValue)
import Circuit.Process (Process (..))
import Data.Proxy (Proxy (..))
import Data.These (These (..))
import Prelude
dropOne :: forall f s. (Uncons f s) => Proxy s -> f -> f
dropOne :: forall f s. Uncons f s => Proxy s -> f -> f
dropOne Proxy s
_ f
xs = case forall f s. Uncons f s => f -> These s f
uncons @f @s f
xs of
These s
_ f
rest -> f
rest
This s
_ -> forall f s. Uncons f s => f
nil @f @s
That f
_ -> f
xs
compileProcess ::
(Eq s, Uncons f s) =>
ParserSyntax f s a ->
Process s (Maybe a)
compileProcess :: forall s f a.
(Eq s, Uncons f s) =>
ParserSyntax f s a -> Process s (Maybe a)
compileProcess ParserSyntax f s a
p0 = (s -> ParserSyntax f s a)
-> (ParserSyntax f s a -> s -> ParserSyntax f s a)
-> (ParserSyntax f s a -> Maybe a)
-> Process s (Maybe a)
forall s a b. (a -> s) -> (s -> a -> s) -> (s -> b) -> Process a b
Process s -> ParserSyntax f s a
inject ParserSyntax f s a -> s -> ParserSyntax f s a
forall {s} {f} {a}.
(Eq s, Uncons f s) =>
ParserSyntax f s a -> s -> ParserSyntax f s a
step ParserSyntax f s a -> Maybe a
forall {a}. ParserSyntax f s a -> Maybe a
extract
where
inject :: s -> ParserSyntax f s a
inject s
c = s -> ParserSyntax f s a -> ParserSyntax f s a
forall s f a.
(Eq s, Uncons f s) =>
s -> ParserSyntax f s a -> ParserSyntax f s a
derive s
c ParserSyntax f s a
p0
step :: ParserSyntax f s a -> s -> ParserSyntax f s a
step ParserSyntax f s a
p s
c = s -> ParserSyntax f s a -> ParserSyntax f s a
forall s f a.
(Eq s, Uncons f s) =>
s -> ParserSyntax f s a -> ParserSyntax f s a
derive s
c ParserSyntax f s a
p
extract :: ParserSyntax f s a -> Maybe a
extract = ParserSyntax f s a -> Maybe a
forall f s a. Uncons f s => ParserSyntax f s a -> Maybe a
nullableValue
compileProcessWithInput ::
forall f s a.
(Eq s, Uncons f s) =>
f ->
ParserSyntax f s a ->
Process s (Maybe (a, f))
compileProcessWithInput :: forall f s a.
(Eq s, Uncons f s) =>
f -> ParserSyntax f s a -> Process s (Maybe (a, f))
compileProcessWithInput f
input ParserSyntax f s a
p0 = (s -> (ParserSyntax f s a, f))
-> ((ParserSyntax f s a, f) -> s -> (ParserSyntax f s a, f))
-> ((ParserSyntax f s a, f) -> Maybe (a, f))
-> Process s (Maybe (a, f))
forall s a b. (a -> s) -> (s -> a -> s) -> (s -> b) -> Process a b
Process s -> (ParserSyntax f s a, f)
inject (ParserSyntax f s a, f) -> s -> (ParserSyntax f s a, f)
step (ParserSyntax f s a, f) -> Maybe (a, f)
forall {f} {s} {a} {t}.
Uncons f s =>
(ParserSyntax f s a, t) -> Maybe (a, t)
extract
where
inject :: s -> (ParserSyntax f s a, f)
inject s
c = (s -> ParserSyntax f s a -> ParserSyntax f s a
forall s f a.
(Eq s, Uncons f s) =>
s -> ParserSyntax f s a -> ParserSyntax f s a
derive s
c ParserSyntax f s a
p0, Proxy s -> f -> f
forall f s. Uncons f s => Proxy s -> f -> f
dropOne (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @s) f
input)
step :: (ParserSyntax f s a, f) -> s -> (ParserSyntax f s a, f)
step (ParserSyntax f s a
p, f
rest) s
c = (s -> ParserSyntax f s a -> ParserSyntax f s a
forall s f a.
(Eq s, Uncons f s) =>
s -> ParserSyntax f s a -> ParserSyntax f s a
derive s
c ParserSyntax f s a
p, Proxy s -> f -> f
forall f s. Uncons f s => Proxy s -> f -> f
dropOne (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @s) f
rest)
extract :: (ParserSyntax f s a, t) -> Maybe (a, t)
extract (ParserSyntax f s a
p, t
rest) = (,t
rest) (a -> (a, t)) -> Maybe a -> Maybe (a, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserSyntax f s a -> Maybe a
forall f s a. Uncons f s => ParserSyntax f s a -> Maybe a
nullableValue ParserSyntax f s a
p