{-|
Module      : Data.Profunctor.Grammar
Description : grammar distributors
Copyright   : (C) 2026 - Eitan Chatav
License     : BSD-style (see the file LICENSE)
Maintainer  : Eitan Chatav <eitan.chatav@gmail.com>
Stability   : provisional
Portability : non-portable
-}

module Data.Profunctor.Grammar
  ( -- * Printor
    module Data.Profunctor.Grammar.Printor
    -- * Parsor
  , Parsor (..)
  , unparseP
  , parseP
    -- * Grammor
  , Grammor (..)
  ) where

import Control.Applicative
import Control.Arrow
import Control.Category
import Control.Lens
import Control.Lens.Extras
import Control.Lens.Grammar.BackusNaur
import Control.Lens.Grammar.Machine
import Control.Lens.Grammar.Symbol
import Control.Lens.Grammar.Token
import Control.Monad
import Data.Coerce
import Data.Monoid
import Data.Monoid.Disjunctive
import Data.Profunctor
import Data.Profunctor.Distributor
import Data.Profunctor.Monoidal.Cocartesian
import Data.Profunctor.Filtrator
import Data.Profunctor.Monoidal
import Data.Profunctor.Monadic (MonadicTrans (..))
import Prelude hiding (id, (.))
import GHC.Exts
import Witherable (Filterable (..))
import Data.Profunctor.Grammar.Printor

-- | t'Parsor' is a simple invertible parser `Profunctor`.
newtype Parsor s f a b = Parsor {forall s (f :: * -> *) a b.
Parsor s f a b -> Maybe a -> s -> f (b, s)
runParsor :: Maybe a -> s -> f (b,s)}

-- | Run the parser on an input string,
-- `uncons`ing tokens from the beginning of the string,
-- from left to right, returning a value and the remaining string.
parseP :: Parsor s f a b -> s -> f (b,s)
parseP :: forall s (f :: * -> *) a b. Parsor s f a b -> s -> f (b, s)
parseP (Parsor Maybe a -> s -> f (b, s)
f) = Maybe a -> s -> f (b, s)
f Maybe a
forall a. Maybe a
Nothing

-- | Run the parser in reverse on a value and an input string;
-- `snoc`ing tokens at the end of the string, from left to right,
-- and returning the new string.
unparseP :: Functor f => Parsor s f a b -> a -> s -> f s
unparseP :: forall (f :: * -> *) s a b.
Functor f =>
Parsor s f a b -> a -> s -> f s
unparseP (Parsor Maybe a -> s -> f (b, s)
f) a
a = ((b, s) -> s) -> f (b, s) -> f s
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (b, s) -> s
forall a b. (a, b) -> b
snd (f (b, s) -> f s) -> (s -> f (b, s)) -> s -> f s
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Maybe a -> s -> f (b, s)
f (a -> Maybe a
forall a. a -> Maybe a
Just a
a)

-- | t'Grammor' is a constant `Profunctor`.
newtype Grammor k a b = Grammor {forall k a b. Grammor k a b -> k
runGrammor :: k}

-- Parsor instances
deriving stock instance Functor f => Functor (Parsor s f a)
instance Functor f => Profunctor (Parsor s f) where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Parsor s f b c -> Parsor s f a d
dimap a -> b
f c -> d
g = (Maybe a -> s -> f (d, s)) -> Parsor s f a d
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> f (d, s)) -> Parsor s f a d)
-> (Parsor s f b c -> Maybe a -> s -> f (d, s))
-> Parsor s f b c
-> Parsor s f a d
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Maybe a -> Maybe b)
-> ((s -> f (c, s)) -> s -> f (d, s))
-> (Maybe b -> s -> f (c, s))
-> Maybe a
-> s
-> f (d, s)
forall a b c d. (a -> b) -> (c -> d) -> (b -> c) -> a -> d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ((a -> b) -> Maybe a -> Maybe b
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) ((f (c, s) -> f (d, s)) -> (s -> f (c, s)) -> s -> f (d, s)
forall a b. (a -> b) -> (s -> a) -> s -> b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((c, s) -> (d, s)) -> f (c, s) -> f (d, s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((c -> d) -> (c, s) -> (d, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' c -> d
g))) ((Maybe b -> s -> f (c, s)) -> Maybe a -> s -> f (d, s))
-> (Parsor s f b c -> Maybe b -> s -> f (c, s))
-> Parsor s f b c
-> Maybe a
-> s
-> f (d, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsor s f b c -> Maybe b -> s -> f (c, s)
forall s (f :: * -> *) a b.
Parsor s f a b -> Maybe a -> s -> f (b, s)
runParsor
instance Monad m => Applicative (Parsor s m a) where
  pure :: forall a. a -> Parsor s m a a
pure a
b = (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor (\Maybe a
_ s
s -> (a, s) -> m (a, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
b,s
s))
  Parsor Maybe a -> s -> m (a -> b, s)
f <*> :: forall a b.
Parsor s m a (a -> b) -> Parsor s m a a -> Parsor s m a b
<*> Parsor Maybe a -> s -> m (a, s)
x = (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (b, s)) -> Parsor s m a b)
-> (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> do
    (g, s') <- Maybe a -> s -> m (a -> b, s)
f Maybe a
ma s
s
    (a, s'') <- x ma s'
    return (g a, s'')
instance (Alternative m, Monad m) => Strong (Parsor s m) where
  first' :: forall a b c. Parsor s m a b -> Parsor s m (a, c) (b, c)
first' Parsor s m a b
p = Parsor s m a b
p Parsor s m a b -> Parsor s m c c -> Parsor s m (a, c) (b, c)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< Parsor s m c c
forall a. Parsor s m a a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  second' :: forall a b c. Parsor s m a b -> Parsor s m (c, a) (c, b)
second' Parsor s m a b
p = Parsor s m c c
forall a. Parsor s m a a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id Parsor s m c c -> Parsor s m a b -> Parsor s m (c, a) (c, b)
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
>*< Parsor s m a b
p
instance Monad m => Monad (Parsor s m a) where
  return :: forall a. a -> Parsor s m a a
return = a -> Parsor s m a a
forall a. a -> Parsor s m a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Parsor Maybe a -> s -> m (a, s)
p >>= :: forall a b.
Parsor s m a a -> (a -> Parsor s m a b) -> Parsor s m a b
>>= a -> Parsor s m a b
f = (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (b, s)) -> Parsor s m a b)
-> (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> do
    (a, s') <- Maybe a -> s -> m (a, s)
p Maybe a
ma s
s
    runParsor (f a) ma s'
instance MonadicTrans (Parsor s) where
  liftP :: forall (m :: * -> *) b a. Monad m => m b -> Parsor s m a b
liftP m b
mb = (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor (\Maybe a
_ s
s -> (, s
s) (b -> (b, s)) -> m b -> m (b, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
mb)
instance (Alternative m, Monad m) => Alternative (Parsor s m a) where
  empty :: forall a. Parsor s m a a
empty = (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor (\Maybe a
_ s
_ -> m (a, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty)
  Parsor Maybe a -> s -> m (a, s)
p <|> :: forall a. Parsor s m a a -> Parsor s m a a -> Parsor s m a a
<|> Parsor Maybe a -> s -> m (a, s)
q = (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (a, s)) -> Parsor s m a a)
-> (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> Maybe a -> s -> m (a, s)
p Maybe a
ma s
s m (a, s) -> m (a, s) -> m (a, s)
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe a -> s -> m (a, s)
q Maybe a
ma s
s
instance (Alternative m, Monad m) => MonadPlus (Parsor s m a)
instance Filterable f => Filterable (Parsor s f a) where
  mapMaybe :: forall a b. (a -> Maybe b) -> Parsor s f a a -> Parsor s f a b
mapMaybe a -> Maybe b
f (Parsor Maybe a -> s -> f (a, s)
p) = (Maybe a -> s -> f (b, s)) -> Parsor s f a b
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> f (b, s)) -> Parsor s f a b)
-> (Maybe a -> s -> f (b, s)) -> Parsor s f a b
forall a b. (a -> b) -> a -> b
$ \Maybe a
fa s
s ->
    ((a, s) -> Maybe (b, s)) -> f (a, s) -> f (b, s)
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (\(a
a,s
t) -> (b -> (b, s)) -> Maybe b -> Maybe (b, s)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,s
t) (a -> Maybe b
f a
a)) (Maybe a -> s -> f (a, s)
p Maybe a
fa s
s)
instance Filterable f => Cochoice (Parsor s f) where
  unleft :: forall a d b.
Parsor s f (Either a d) (Either b d) -> Parsor s f a b
unleft = (Parsor s f a b, Parsor s f d d) -> Parsor s f a b
forall a b. (a, b) -> a
fst ((Parsor s f a b, Parsor s f d d) -> Parsor s f a b)
-> (Parsor s f (Either a d) (Either b d)
    -> (Parsor s f a b, Parsor s f d d))
-> Parsor s f (Either a d) (Either b d)
-> Parsor s f a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsor s f (Either a d) (Either b d)
-> (Parsor s f a b, Parsor s f d d)
forall a c b d.
Parsor s f (Either a c) (Either b d)
-> (Parsor s f a b, Parsor s f c d)
forall (p :: * -> * -> *) a c b d.
Filtrator p =>
p (Either a c) (Either b d) -> (p a b, p c d)
filtrate
  unright :: forall d a b.
Parsor s f (Either d a) (Either d b) -> Parsor s f a b
unright = (Parsor s f d d, Parsor s f a b) -> Parsor s f a b
forall a b. (a, b) -> b
snd ((Parsor s f d d, Parsor s f a b) -> Parsor s f a b)
-> (Parsor s f (Either d a) (Either d b)
    -> (Parsor s f d d, Parsor s f a b))
-> Parsor s f (Either d a) (Either d b)
-> Parsor s f a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsor s f (Either d a) (Either d b)
-> (Parsor s f d d, Parsor s f a b)
forall a c b d.
Parsor s f (Either a c) (Either b d)
-> (Parsor s f a b, Parsor s f c d)
forall (p :: * -> * -> *) a c b d.
Filtrator p =>
p (Either a c) (Either b d) -> (p a b, p c d)
filtrate
instance Filterable f => Filtrator (Parsor s f) where
  filtrate :: forall a c b d.
Parsor s f (Either a c) (Either b d)
-> (Parsor s f a b, Parsor s f c d)
filtrate (Parsor Maybe (Either a c) -> s -> f (Either b d, s)
p) =
    ( (Maybe a -> s -> f (b, s)) -> Parsor s f a b
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> f (b, s)) -> Parsor s f a b)
-> (Maybe a -> s -> f (b, s)) -> Parsor s f a b
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> ((Either b d, s) -> Maybe (b, s)) -> f (Either b d, s) -> f (b, s)
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe
        (\case{(Left b
b,s
t) -> (b, s) -> Maybe (b, s)
forall a. a -> Maybe a
Just (b
b,s
t); (Either b d, s)
_ -> Maybe (b, s)
forall a. Maybe a
Nothing})
        (Maybe (Either a c) -> s -> f (Either b d, s)
p ((a -> Either a c) -> Maybe a -> Maybe (Either a c)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either a c
forall a b. a -> Either a b
Left Maybe a
ma) s
s)
    , (Maybe c -> s -> f (d, s)) -> Parsor s f c d
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe c -> s -> f (d, s)) -> Parsor s f c d)
-> (Maybe c -> s -> f (d, s)) -> Parsor s f c d
forall a b. (a -> b) -> a -> b
$ \Maybe c
ma s
s -> ((Either b d, s) -> Maybe (d, s)) -> f (Either b d, s) -> f (d, s)
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe
        (\case{(Right d
b,s
t) -> (d, s) -> Maybe (d, s)
forall a. a -> Maybe a
Just (d
b,s
t); (Either b d, s)
_ -> Maybe (d, s)
forall a. Maybe a
Nothing})
        (Maybe (Either a c) -> s -> f (Either b d, s)
p ((c -> Either a c) -> Maybe c -> Maybe (Either a c)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> Either a c
forall a b. b -> Either a b
Right Maybe c
ma) s
s)
    )
instance (Alternative m, Monad m) => CocartesianMonoidal (Parsor s m)
{- | `manyP` & `optionalP` parse & print their elements in turn,
rather than through isomorphisms & sums, parsing more before fewer.
-}
instance (Alternative m, Monad m) => Distributor (Parsor s m) where
  manyP :: forall a b. Parsor s m a b -> Parsor s m [a] [b]
manyP (Parsor Maybe a -> s -> m (b, s)
p) = (Maybe [a] -> s -> m ([b], s)) -> Parsor s m [a] [b]
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe [a] -> s -> m ([b], s)) -> Parsor s m [a] [b])
-> (Maybe [a] -> s -> m ([b], s)) -> Parsor s m [a] [b]
forall a b. (a -> b) -> a -> b
$ \Maybe [a]
ma s
s -> case Maybe [a]
ma of
    Maybe [a]
Nothing -> s -> m ([b], s)
parsing s
s
    Just [a]
as -> [a] -> s -> m ([b], s)
printing [a]
as s
s
    where
      parsing :: s -> m ([b], s)
parsing s
s =
        (do
          (b, s') <- Maybe a -> s -> m (b, s)
p Maybe a
forall a. Maybe a
Nothing s
s
          (bs, s'') <- parsing s'
          pure (b : bs, s''))
        m ([b], s) -> m ([b], s) -> m ([b], s)
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([b], s) -> m ([b], s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], s
s)
      printing :: [a] -> s -> m ([b], s)
printing [] s
s = ([b], s) -> m ([b], s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], s
s)
      printing (a
a : [a]
as) s
s = do
        (b, s') <- Maybe a -> s -> m (b, s)
p (a -> Maybe a
forall a. a -> Maybe a
Just a
a) s
s
        (bs, s'') <- printing as s'
        pure (b : bs, s'')
  optionalP :: forall a b. Parsor s m a b -> Parsor s m (Maybe a) (Maybe b)
optionalP (Parsor Maybe a -> s -> m (b, s)
p) = (Maybe (Maybe a) -> s -> m (Maybe b, s))
-> Parsor s m (Maybe a) (Maybe b)
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe (Maybe a) -> s -> m (Maybe b, s))
 -> Parsor s m (Maybe a) (Maybe b))
-> (Maybe (Maybe a) -> s -> m (Maybe b, s))
-> Parsor s m (Maybe a) (Maybe b)
forall a b. (a -> b) -> a -> b
$ \Maybe (Maybe a)
ma s
s -> case Maybe (Maybe a)
ma of
    Maybe (Maybe a)
Nothing -> ((b, s) -> (Maybe b, s)) -> m (b, s) -> m (Maybe b, s)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> Maybe b) -> (b, s) -> (Maybe b, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' b -> Maybe b
forall a. a -> Maybe a
Just) (Maybe a -> s -> m (b, s)
p Maybe a
forall a. Maybe a
Nothing s
s) m (Maybe b, s) -> m (Maybe b, s) -> m (Maybe b, s)
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Maybe b, s) -> m (Maybe b, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe b
forall a. Maybe a
Nothing, s
s)
    Just Maybe a
Nothing -> (Maybe b, s) -> m (Maybe b, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe b
forall a. Maybe a
Nothing, s
s)
    Just (Just a
a) -> ((b, s) -> (Maybe b, s)) -> m (b, s) -> m (Maybe b, s)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> Maybe b) -> (b, s) -> (Maybe b, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' b -> Maybe b
forall a. a -> Maybe a
Just) (Maybe a -> s -> m (b, s)
p (a -> Maybe a
forall a. a -> Maybe a
Just a
a) s
s)
instance (Alternative m, Monad m) => Choice (Parsor s m) where
  left' :: forall a b c.
Parsor s m a b -> Parsor s m (Either a c) (Either b c)
left' = Either (Parsor s m a b) (Parsor s m c c)
-> Parsor s m (Either a c) (Either b c)
forall a b c d.
Either (Parsor s m a b) (Parsor s m c d)
-> Parsor s m (Either a c) (Either b d)
forall (p :: * -> * -> *) a b c d.
CocartesianChoice p =>
Either (p a b) (p c d) -> p (Either a c) (Either b d)
alternate (Either (Parsor s m a b) (Parsor s m c c)
 -> Parsor s m (Either a c) (Either b c))
-> (Parsor s m a b -> Either (Parsor s m a b) (Parsor s m c c))
-> Parsor s m a b
-> Parsor s m (Either a c) (Either b c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsor s m a b -> Either (Parsor s m a b) (Parsor s m c c)
forall a b. a -> Either a b
Left
  right' :: forall a b c.
Parsor s m a b -> Parsor s m (Either c a) (Either c b)
right' = Either (Parsor s m c c) (Parsor s m a b)
-> Parsor s m (Either c a) (Either c b)
forall a b c d.
Either (Parsor s m a b) (Parsor s m c d)
-> Parsor s m (Either a c) (Either b d)
forall (p :: * -> * -> *) a b c d.
CocartesianChoice p =>
Either (p a b) (p c d) -> p (Either a c) (Either b d)
alternate (Either (Parsor s m c c) (Parsor s m a b)
 -> Parsor s m (Either c a) (Either c b))
-> (Parsor s m a b -> Either (Parsor s m c c) (Parsor s m a b))
-> Parsor s m a b
-> Parsor s m (Either c a) (Either c b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsor s m a b -> Either (Parsor s m c c) (Parsor s m a b)
forall a b. b -> Either a b
Right
instance (Alternative m, Monad m) => DisjunctiveMonoid (Parsor s m a b)
instance (Alternative m, Monad m) => CocartesianChoice (Parsor s m) where
  alternate :: forall a b c d.
Either (Parsor s m a b) (Parsor s m c d)
-> Parsor s m (Either a c) (Either b d)
alternate = \case
    Left (Parsor Maybe a -> s -> m (b, s)
p) -> (Maybe (Either a c) -> s -> m (Either b d, s))
-> Parsor s m (Either a c) (Either b d)
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe (Either a c) -> s -> m (Either b d, s))
 -> Parsor s m (Either a c) (Either b d))
-> (Maybe (Either a c) -> s -> m (Either b d, s))
-> Parsor s m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ \Maybe (Either a c)
ma s
s -> case Maybe (Either a c)
ma of
      Maybe (Either a c)
Nothing -> ((b, s) -> (Either b d, s)) -> m (b, s) -> m (Either b d, s)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> Either b d) -> (b, s) -> (Either b d, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' b -> Either b d
forall a b. a -> Either a b
Left) (Maybe a -> s -> m (b, s)
p Maybe a
forall a. Maybe a
Nothing s
s)
      Just (Left a
a) -> ((b, s) -> (Either b d, s)) -> m (b, s) -> m (Either b d, s)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> Either b d) -> (b, s) -> (Either b d, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' b -> Either b d
forall a b. a -> Either a b
Left) (Maybe a -> s -> m (b, s)
p (a -> Maybe a
forall a. a -> Maybe a
Just a
a) s
s)
      Just (Right c
_) -> m (Either b d, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty
    Right (Parsor Maybe c -> s -> m (d, s)
p) -> (Maybe (Either a c) -> s -> m (Either b d, s))
-> Parsor s m (Either a c) (Either b d)
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe (Either a c) -> s -> m (Either b d, s))
 -> Parsor s m (Either a c) (Either b d))
-> (Maybe (Either a c) -> s -> m (Either b d, s))
-> Parsor s m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ \Maybe (Either a c)
ma s
s -> case Maybe (Either a c)
ma of
      Maybe (Either a c)
Nothing -> ((d, s) -> (Either b d, s)) -> m (d, s) -> m (Either b d, s)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((d -> Either b d) -> (d, s) -> (Either b d, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' d -> Either b d
forall a b. b -> Either a b
Right) (Maybe c -> s -> m (d, s)
p Maybe c
forall a. Maybe a
Nothing s
s)
      Just (Right c
a) -> ((d, s) -> (Either b d, s)) -> m (d, s) -> m (Either b d, s)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((d -> Either b d) -> (d, s) -> (Either b d, s)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first' d -> Either b d
forall a b. b -> Either a b
Right) (Maybe c -> s -> m (d, s)
p (c -> Maybe c
forall a. a -> Maybe a
Just c
a) s
s)
      Just (Left a
_) -> m (Either b d, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty
instance (Alternative m, Monad m) => Alternator (Parsor s m) where
  optionP :: forall a b. APrism a b () () -> Parsor s m a b -> Parsor s m a b
optionP APrism a b () ()
def Parsor s m a b
p = (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (b, s)) -> Parsor s m a b)
-> (Maybe a -> s -> m (b, s)) -> Parsor s m a b
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> case Maybe a
ma of
    Maybe a
Nothing -> Parsor s m a b -> Maybe a -> s -> m (b, s)
forall s (f :: * -> *) a b.
Parsor s f a b -> Maybe a -> s -> f (b, s)
runParsor (Parsor s m a b
p Parsor s m a b -> Parsor s m a b -> Parsor s m a b
forall a. Parsor s m a a -> Parsor s m a a -> Parsor s m a a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> APrism a b () () -> Parsor s m a b
forall (p :: * -> * -> *) a b.
(Monoidal p, Choice p) =>
APrism a b () () -> p a b
pureP APrism a b () ()
def) Maybe a
ma s
s
    Just a
_ -> Parsor s m a b -> Maybe a -> s -> m (b, s)
forall s (f :: * -> *) a b.
Parsor s f a b -> Maybe a -> s -> f (b, s)
runParsor (APrism a b () () -> Parsor s m a b
forall (p :: * -> * -> *) a b.
(Monoidal p, Choice p) =>
APrism a b () () -> p a b
pureP APrism a b () ()
def Parsor s m a b -> Parsor s m a b -> Parsor s m a b
forall a. Parsor s m a a -> Parsor s m a a -> Parsor s m a a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parsor s m a b
p) Maybe a
ma s
s
instance (Alternative m, Monad m) => Category (Parsor s m) where
  id :: forall a. Parsor s m a a
id = (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (a, s)) -> Parsor s m a a)
-> (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> case Maybe a
ma of
    Maybe a
Nothing -> m (a, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty
    Just a
a  -> (a, s) -> m (a, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a,s
s)
  Parsor Maybe b -> s -> m (c, s)
q . :: forall b c a. Parsor s m b c -> Parsor s m a b -> Parsor s m a c
. Parsor Maybe a -> s -> m (b, s)
p = (Maybe a -> s -> m (c, s)) -> Parsor s m a c
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (c, s)) -> Parsor s m a c)
-> (Maybe a -> s -> m (c, s)) -> Parsor s m a c
forall a b. (a -> b) -> a -> b
$ \Maybe a
ma s
s -> case Maybe a
ma of
    Maybe a
Nothing -> m (c, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty
    Just a
a -> do
      (b, t) <- Maybe a -> s -> m (b, s)
p (a -> Maybe a
forall a. a -> Maybe a
Just a
a) s
s
      q (Just b) t
instance (Alternative m, Monad m) => Arrow (Parsor s m) where
  arr :: forall b c. (b -> c) -> Parsor s m b c
arr b -> c
f = (Maybe b -> s -> m (c, s)) -> Parsor s m b c
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe b -> s -> m (c, s)) -> Parsor s m b c)
-> (Maybe b -> s -> m (c, s)) -> Parsor s m b c
forall a b. (a -> b) -> a -> b
$ \Maybe b
ma s
s -> case Maybe b
ma of
    Maybe b
Nothing -> m (c, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty
    Just b
a  -> (c, s) -> m (c, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> c
f b
a, s
s)
  *** :: forall b c b' c'.
Parsor s m b c -> Parsor s m b' c' -> Parsor s m (b, b') (c, c')
(***) = Parsor s m b c -> Parsor s m b' c' -> Parsor s m (b, b') (c, c')
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
(>*<)
  first :: forall b c d. Parsor s m b c -> Parsor s m (b, d) (c, d)
first = Parsor s m b c -> Parsor s m (b, d) (c, d)
forall b c d. Parsor s m b c -> Parsor s m (b, d) (c, d)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first'
  second :: forall b c d. Parsor s m b c -> Parsor s m (d, b) (d, c)
second = Parsor s m b c -> Parsor s m (d, b) (d, c)
forall b c d. Parsor s m b c -> Parsor s m (d, b) (d, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (c, a) (c, b)
second'
instance (Alternative m, Monad m) => ArrowZero (Parsor s m) where
  zeroArrow :: forall b c. Parsor s m b c
zeroArrow = Parsor s m b c
forall a. Parsor s m b a
forall (f :: * -> *) a. Alternative f => f a
empty
instance (Alternative m, Monad m) => ArrowPlus (Parsor s m) where
  <+> :: forall b c. Parsor s m b c -> Parsor s m b c -> Parsor s m b c
(<+>) = Parsor s m b c -> Parsor s m b c -> Parsor s m b c
forall a. Parsor s m b a -> Parsor s m b a -> Parsor s m b a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
instance (Alternative m, Monad m) => ArrowChoice (Parsor s m) where
  +++ :: forall b c b' c'.
Parsor s m b c
-> Parsor s m b' c' -> Parsor s m (Either b b') (Either c c')
(+++) = Parsor s m b c
-> Parsor s m b' c' -> Parsor s m (Either b b') (Either c c')
forall b c b' c'.
Parsor s m b c
-> Parsor s m b' c' -> Parsor s m (Either b b') (Either c c')
forall (p :: * -> * -> *) a b c d.
CocartesianMonoidal p =>
p a b -> p c d -> p (Either a c) (Either b d)
(>+<)
  left :: forall b c d.
Parsor s m b c -> Parsor s m (Either b d) (Either c d)
left = Parsor s m b c -> Parsor s m (Either b d) (Either c d)
forall b c d.
Parsor s m b c -> Parsor s m (Either b d) (Either c d)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either a c) (Either b c)
left'
  right :: forall b c d.
Parsor s m b c -> Parsor s m (Either d b) (Either d c)
right = Parsor s m b c -> Parsor s m (Either d b) (Either d c)
forall b c d.
Parsor s m b c -> Parsor s m (Either d b) (Either d c)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right'
instance
  ( Categorized a, a ~ Item s, IsList s
  , Cons s s a a, Snoc s s a a
  , Filterable m, Alternative m, Monad m
  ) => Tokenized a (Parsor s m a a) where
    anyToken :: Parsor s m a a
anyToken = (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall s (f :: * -> *) a b.
(Maybe a -> s -> f (b, s)) -> Parsor s f a b
Parsor ((Maybe a -> s -> m (a, s)) -> Parsor s m a a)
-> (Maybe a -> s -> m (a, s)) -> Parsor s m a a
forall a b. (a -> b) -> a -> b
$ (s -> m (a, s)) -> (a -> s -> m (a, s)) -> Maybe a -> s -> m (a, s)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
      (m (a, s) -> ((a, s) -> m (a, s)) -> Maybe (a, s) -> m (a, s)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m (a, s)
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty (a, s) -> m (a, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, s) -> m (a, s)) -> (s -> Maybe (a, s)) -> s -> m (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> Maybe (a, s)
forall s a. Cons s s a a => s -> Maybe (a, s)
uncons)
      (\a
a -> (a, s) -> m (a, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, s) -> m (a, s)) -> (s -> (a, s)) -> s -> m (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a
a,) (s -> (a, s)) -> (s -> s) -> s -> (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (s -> a -> s) -> a -> s -> s
forall a b c. (a -> b -> c) -> b -> a -> c
flip s -> a -> s
forall s a. Snoc s s a a => s -> a -> s
snoc a
a)
instance
  ( Categorized a, a ~ Item s, IsList s
  , Cons s s a a, Snoc s s a a
  , Filterable m, Alternative m, Monad m
  ) => TokenAlgebra a (Parsor s m a a)
instance
  ( Categorized a, a ~ Item s, IsList s
  , Cons s s a a, Snoc s s a a
  , Filterable m, Alternative m, Monad m
  ) => TerminalSymbol a (Parsor s m () ()) where
instance
  ( Char ~ Item s, IsList s
  , Cons s s Char Char, Snoc s s Char Char
  , Filterable m, Alternative m, Monad m
  ) => IsString (Parsor s m () ()) where
  fromString :: String -> Parsor s m () ()
fromString = String -> Parsor s m () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal
instance
  ( Char ~ Item s, IsList s
  , Cons s s Char Char, Snoc s s Char Char, AsEmpty s
  , Filterable m, Alternative m, Monad m
  ) => IsString (Parsor s m s s) where
  fromString :: String -> Parsor s m s s
fromString = String -> Parsor s m s s
forall (f :: * -> *) a (p :: * -> * -> *) s.
(Foldable f, Tokenized a (p a a), Monoidal p, Choice p, AsEmpty s,
 Cons s s a a) =>
f a -> p s s
tokens
instance Ruled (Parsor s m a b)
instance BackusNaurForm (Parsor s m a b)
instance Tried (Parsor s m a b)
instance (Alternative m, Monad m) => MonadFail (Parsor s m a) where
  fail :: forall a. String -> Parsor s m a a
fail String
_ = Parsor s m a a
forall a. Parsor s m a a
forall (f :: * -> *) a. Alternative f => f a
empty
instance AsEmpty s => Matching s (Parsor s [] a b) where
  s
word =~ :: s -> Parsor s [] a b -> Bool
=~ Parsor s [] a b
p = case
    [ () | (b
_, s
remaining) <- Parsor s [] a b -> Maybe a -> s -> [(b, s)]
forall s (f :: * -> *) a b.
Parsor s f a b -> Maybe a -> s -> f (b, s)
runParsor Parsor s [] a b
p Maybe a
forall a. Maybe a
Nothing s
word
    , APrism s s () () -> s -> Bool
forall s t a b. APrism s t a b -> s -> Bool
is APrism s s () ()
forall a. AsEmpty a => Prism' a ()
Prism' s ()
_Empty s
remaining
    ] of [] -> Bool
False; ()
_:[()]
_ -> Bool
True

-- Grammor instances
instance Functor (Grammor k a) where fmap :: forall a b. (a -> b) -> Grammor k a a -> Grammor k a b
fmap a -> b
_ = Grammor k a a -> Grammor k a b
forall a b. Coercible a b => a -> b
coerce
instance Contravariant (Grammor k a) where contramap :: forall a' a. (a' -> a) -> Grammor k a a -> Grammor k a a'
contramap a' -> a
_ = Grammor k a a -> Grammor k a a'
forall a b. Coercible a b => a -> b
coerce
instance Profunctor (Grammor k) where dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Grammor k b c -> Grammor k a d
dimap a -> b
_ c -> d
_ = Grammor k b c -> Grammor k a d
forall a b. Coercible a b => a -> b
coerce
instance Bifunctor (Grammor k) where bimap :: forall a b c d.
(a -> b) -> (c -> d) -> Grammor k a c -> Grammor k b d
bimap a -> b
_ c -> d
_ = Grammor k a c -> Grammor k b d
forall a b. Coercible a b => a -> b
coerce
instance Choice (Grammor k) where
  left' :: forall a b c. Grammor k a b -> Grammor k (Either a c) (Either b c)
left' = Grammor k a b -> Grammor k (Either a c) (Either b c)
forall a b. Coercible a b => a -> b
coerce
  right' :: forall a b c. Grammor k a b -> Grammor k (Either c a) (Either c b)
right' = Grammor k a b -> Grammor k (Either c a) (Either c b)
forall a b. Coercible a b => a -> b
coerce
instance Monoid k => Applicative (Grammor k a) where
  pure :: forall a. a -> Grammor k a a
pure a
_ = k -> Grammor k a a
forall k a b. k -> Grammor k a b
Grammor k
forall a. Monoid a => a
mempty
  Grammor k
rex1 <*> :: forall a b. Grammor k a (a -> b) -> Grammor k a a -> Grammor k a b
<*> Grammor k
rex2 = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k
rex1 k -> k -> k
forall a. Semigroup a => a -> a -> a
<> k
rex2)
instance DisjunctiveMonoid k => DisjunctiveMonoid (Grammor k a b) where
  falseP :: Grammor k a b
falseP = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor k
forall a. DisjunctiveMonoid a => a
falseP
  Grammor k
rex1 >|< :: Grammor k a b -> Grammor k a b -> Grammor k a b
>|< Grammor k
rex2 = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k
rex1 k -> k -> k
forall a. DisjunctiveMonoid a => a -> a -> a
>|< k
rex2)
instance KleeneStarAlgebra k => Alternative (Grammor k a) where
  empty :: forall a. Grammor k a a
empty = Grammor k a a
forall a. DisjunctiveMonoid a => a
falseP
  <|> :: forall a. Grammor k a a -> Grammor k a a -> Grammor k a a
(<|>) = Grammor k a a -> Grammor k a a -> Grammor k a a
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<)
  many :: forall a. Grammor k a a -> Grammor k a [a]
many = (a -> [a]) -> Grammor k [a] [a] -> Grammor k a [a]
forall a b c. (a -> b) -> Grammor k b c -> Grammor k a c
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> [a]
forall a. a -> [a]
repeat (Grammor k [a] [a] -> Grammor k a [a])
-> (Grammor k a a -> Grammor k [a] [a])
-> Grammor k a a
-> Grammor k a [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammor k a a -> Grammor k [a] [a]
forall a b. Grammor k a b -> Grammor k [a] [b]
forall (p :: * -> * -> *) a b. Distributor p => p a b -> p [a] [b]
manyP
  some :: forall a. Grammor k a a -> Grammor k a [a]
some = (a -> [a]) -> Grammor k [a] [a] -> Grammor k a [a]
forall a b c. (a -> b) -> Grammor k b c -> Grammor k a c
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> [a]
forall a. a -> [a]
repeat (Grammor k [a] [a] -> Grammor k a [a])
-> (Grammor k a a -> Grammor k [a] [a])
-> Grammor k a a
-> Grammor k a [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammor k a a -> Grammor k [a] [a]
forall a b. Grammor k a b -> Grammor k [a] [b]
forall (p :: * -> * -> *) a b. Alternator p => p a b -> p [a] [b]
someP
instance KleeneStarAlgebra k => CocartesianMonoidal (Grammor k)
instance KleeneStarAlgebra k => CocartesianChoice (Grammor k) where
  alternate :: forall a b c d.
Either (Grammor k a b) (Grammor k c d)
-> Grammor k (Either a c) (Either b d)
alternate = (Grammor k a b -> Grammor k (Either a c) (Either b d))
-> (Grammor k c d -> Grammor k (Either a c) (Either b d))
-> Either (Grammor k a b) (Grammor k c d)
-> Grammor k (Either a c) (Either b d)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Grammor k a b -> Grammor k (Either a c) (Either b d)
forall a b. Coercible a b => a -> b
coerce Grammor k c d -> Grammor k (Either a c) (Either b d)
forall a b. Coercible a b => a -> b
coerce
instance KleeneStarAlgebra k => Distributor (Grammor k) where
  manyP :: forall a b. Grammor k a b -> Grammor k [a] [b]
manyP (Grammor k
rex) = k -> Grammor k [a] [b]
forall k a b. k -> Grammor k a b
Grammor (k -> k
forall k. KleeneStarAlgebra k => k -> k
starK k
rex)
  optionalP :: forall a b. Grammor k a b -> Grammor k (Maybe a) (Maybe b)
optionalP (Grammor k
rex) = k -> Grammor k (Maybe a) (Maybe b)
forall k a b. k -> Grammor k a b
Grammor (k -> k
forall k. KleeneStarAlgebra k => k -> k
optK k
rex)
instance KleeneStarAlgebra k => Alternator (Grammor k) where
  someP :: forall a b. Grammor k a b -> Grammor k [a] [b]
someP (Grammor k
rex) = k -> Grammor k [a] [b]
forall k a b. k -> Grammor k a b
Grammor (k -> k
forall k. KleeneStarAlgebra k => k -> k
plusK k
rex)
  optionP :: forall a b. APrism a b () () -> Grammor k a b -> Grammor k a b
optionP APrism a b () ()
_ (Grammor k
rex) = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> k
forall k. KleeneStarAlgebra k => k -> k
optK k
rex)
  manyTillP :: forall a b. Grammor k a b -> Grammor k [a] [b] -> Grammor k [a] [b]
manyTillP (Grammor k
rex) (Grammor k
rest) = k -> Grammor k [a] [b]
forall k a b. k -> Grammor k a b
Grammor (k -> k
forall k. KleeneStarAlgebra k => k -> k
starK k
rex k -> k -> k
forall a. Semigroup a => a -> a -> a
<> k
rest)
  quantP :: forall a b. Int -> Maybe Int -> Grammor k a b -> Grammor k [a] [b]
quantP Int
lo Maybe Int
hi (Grammor k
rex) = k -> Grammor k [a] [b]
forall k a b. k -> Grammor k a b
Grammor (Int -> Maybe Int -> k -> k
forall k. KleeneStarAlgebra k => Int -> Maybe Int -> k -> k
quantK Int
lo Maybe Int
hi k
rex)
instance Tokenized token k => Tokenized token (Grammor k a b) where
  anyToken :: Grammor k a b
anyToken = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor k
forall token p. Tokenized token p => p
anyToken
  token :: token -> Grammor k a b
token = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b) -> (token -> k) -> token -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. token -> k
forall token p. Tokenized token p => token -> p
token
  oneOf :: forall (f :: * -> *). Foldable f => f token -> Grammor k a b
oneOf = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b) -> (f token -> k) -> f token -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f token -> k
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f token -> k
oneOf
  notOneOf :: forall (f :: * -> *). Foldable f => f token -> Grammor k a b
notOneOf = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b) -> (f token -> k) -> f token -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f token -> k
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f token -> k
notOneOf
  asIn :: Categorize token -> Grammor k a b
asIn = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b)
-> (Categorize token -> k) -> Categorize token -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Categorize token -> k
forall token p. Tokenized token p => Categorize token -> p
asIn
  notAsIn :: Categorize token -> Grammor k a b
notAsIn = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b)
-> (Categorize token -> k) -> Categorize token -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Categorize token -> k
forall token p. Tokenized token p => Categorize token -> p
notAsIn
instance TokenAlgebra a k => TokenAlgebra a (Grammor k a b) where
  tokenClass :: TokenClass a -> Grammor k a b
tokenClass = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b)
-> (TokenClass a -> k) -> TokenClass a -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TokenClass a -> k
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass
instance TerminalSymbol token k
  => TerminalSymbol token (Grammor k a b) where
  terminal :: [token] -> Grammor k a b
terminal = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b) -> ([token] -> k) -> [token] -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [token] -> k
forall token s. TerminalSymbol token s => [token] -> s
terminal
instance TerminalSymbol Char k => IsString (Grammor k () ()) where
  fromString :: String -> Grammor k () ()
fromString = String -> Grammor k () ()
forall token s. TerminalSymbol token s => [token] -> s
terminal
instance Ruled k => Ruled (Grammor k a b) where
  rule :: String -> Grammor k a b -> Grammor k a b
rule String
name = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b)
-> (Grammor k a b -> k) -> Grammor k a b -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> k -> k
forall p. Ruled p => String -> p -> p
rule String
name (k -> k) -> (Grammor k a b -> k) -> Grammor k a b -> k
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammor k a b -> k
forall k a b. Grammor k a b -> k
runGrammor
instance BackusNaurForm k => BackusNaurForm (Grammor k a b) where
  ruleRec :: String -> (Grammor k a b -> Grammor k a b) -> Grammor k a b
ruleRec String
name = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b)
-> ((Grammor k a b -> Grammor k a b) -> k)
-> (Grammor k a b -> Grammor k a b)
-> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> (k -> k) -> k
forall bnf. BackusNaurForm bnf => String -> (bnf -> bnf) -> bnf
ruleRec String
name ((k -> k) -> k)
-> ((Grammor k a b -> Grammor k a b) -> k -> k)
-> (Grammor k a b -> Grammor k a b)
-> k
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (k -> Grammor k a b)
-> (Grammor k a b -> k)
-> (Grammor k a b -> Grammor k a b)
-> k
-> k
forall a b c d. (a -> b) -> (c -> d) -> (b -> c) -> a -> d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor Grammor k a b -> k
forall k a b. Grammor k a b -> k
runGrammor
instance Tried k => Tried (Grammor k a b) where
  try :: Grammor k a b -> Grammor k a b
try = k -> Grammor k a b
forall k a b. k -> Grammor k a b
Grammor (k -> Grammor k a b)
-> (Grammor k a b -> k) -> Grammor k a b -> Grammor k a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. k -> k
forall p. Tried p => p -> p
try (k -> k) -> (Grammor k a b -> k) -> Grammor k a b -> k
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Grammor k a b -> k
forall k a b. Grammor k a b -> k
runGrammor
instance Matching s k => Matching s (Grammor k a b) where
  s
word =~ :: s -> Grammor k a b -> Bool
=~ Grammor k a b
pattern = s
word s -> k -> Bool
forall word pattern.
Matching word pattern =>
word -> pattern -> Bool
=~ Grammor k a b -> k
forall k a b. Grammor k a b -> k
runGrammor Grammor k a b
pattern