module Language.Lexer.Tlex.Data.InputString (
    InputStringContext (..),
    InputString (..),
    runInputString,
) where

import           Language.Lexer.Tlex.Prelude

import qualified Language.Lexer.Tlex.Runner  as Tlex


data InputStringContext e = InputStringContext
    { InputStringContext e -> [e]
inputStringCtxRest :: [e]
    , InputStringContext e -> Int
inputStringCtxPos  :: Int
    }
    deriving (InputStringContext e -> InputStringContext e -> Bool
(InputStringContext e -> InputStringContext e -> Bool)
-> (InputStringContext e -> InputStringContext e -> Bool)
-> Eq (InputStringContext e)
forall e.
Eq e =>
InputStringContext e -> InputStringContext e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: InputStringContext e -> InputStringContext e -> Bool
$c/= :: forall e.
Eq e =>
InputStringContext e -> InputStringContext e -> Bool
== :: InputStringContext e -> InputStringContext e -> Bool
$c== :: forall e.
Eq e =>
InputStringContext e -> InputStringContext e -> Bool
Eq, Int -> InputStringContext e -> ShowS
[InputStringContext e] -> ShowS
InputStringContext e -> String
(Int -> InputStringContext e -> ShowS)
-> (InputStringContext e -> String)
-> ([InputStringContext e] -> ShowS)
-> Show (InputStringContext e)
forall e. Show e => Int -> InputStringContext e -> ShowS
forall e. Show e => [InputStringContext e] -> ShowS
forall e. Show e => InputStringContext e -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [InputStringContext e] -> ShowS
$cshowList :: forall e. Show e => [InputStringContext e] -> ShowS
show :: InputStringContext e -> String
$cshow :: forall e. Show e => InputStringContext e -> String
showsPrec :: Int -> InputStringContext e -> ShowS
$cshowsPrec :: forall e. Show e => Int -> InputStringContext e -> ShowS
Show)

initialInputStringContext :: [e] -> InputStringContext e
initialInputStringContext :: [e] -> InputStringContext e
initialInputStringContext [e]
s = InputStringContext :: forall e. [e] -> Int -> InputStringContext e
InputStringContext
    { $sel:inputStringCtxRest:InputStringContext :: [e]
inputStringCtxRest = [e]
s
    , $sel:inputStringCtxPos:InputStringContext :: Int
inputStringCtxPos = Int
0
    }

newtype InputString e a = InputString
    { InputString e a -> State (InputStringContext e) a
unInputString :: State (InputStringContext e) a
    }
    deriving (
        a -> InputString e b -> InputString e a
(a -> b) -> InputString e a -> InputString e b
(forall a b. (a -> b) -> InputString e a -> InputString e b)
-> (forall a b. a -> InputString e b -> InputString e a)
-> Functor (InputString e)
forall a b. a -> InputString e b -> InputString e a
forall a b. (a -> b) -> InputString e a -> InputString e b
forall e a b. a -> InputString e b -> InputString e a
forall e a b. (a -> b) -> InputString e a -> InputString e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> InputString e b -> InputString e a
$c<$ :: forall e a b. a -> InputString e b -> InputString e a
fmap :: (a -> b) -> InputString e a -> InputString e b
$cfmap :: forall e a b. (a -> b) -> InputString e a -> InputString e b
Functor,
        Functor (InputString e)
a -> InputString e a
Functor (InputString e)
-> (forall a. a -> InputString e a)
-> (forall a b.
    InputString e (a -> b) -> InputString e a -> InputString e b)
-> (forall a b c.
    (a -> b -> c)
    -> InputString e a -> InputString e b -> InputString e c)
-> (forall a b.
    InputString e a -> InputString e b -> InputString e b)
-> (forall a b.
    InputString e a -> InputString e b -> InputString e a)
-> Applicative (InputString e)
InputString e a -> InputString e b -> InputString e b
InputString e a -> InputString e b -> InputString e a
InputString e (a -> b) -> InputString e a -> InputString e b
(a -> b -> c)
-> InputString e a -> InputString e b -> InputString e c
forall e. Functor (InputString e)
forall a. a -> InputString e a
forall e a. a -> InputString e a
forall a b. InputString e a -> InputString e b -> InputString e a
forall a b. InputString e a -> InputString e b -> InputString e b
forall a b.
InputString e (a -> b) -> InputString e a -> InputString e b
forall e a b. InputString e a -> InputString e b -> InputString e a
forall e a b. InputString e a -> InputString e b -> InputString e b
forall e a b.
InputString e (a -> b) -> InputString e a -> InputString e b
forall a b c.
(a -> b -> c)
-> InputString e a -> InputString e b -> InputString e c
forall e a b c.
(a -> b -> c)
-> InputString e a -> InputString e b -> InputString e c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: InputString e a -> InputString e b -> InputString e a
$c<* :: forall e a b. InputString e a -> InputString e b -> InputString e a
*> :: InputString e a -> InputString e b -> InputString e b
$c*> :: forall e a b. InputString e a -> InputString e b -> InputString e b
liftA2 :: (a -> b -> c)
-> InputString e a -> InputString e b -> InputString e c
$cliftA2 :: forall e a b c.
(a -> b -> c)
-> InputString e a -> InputString e b -> InputString e c
<*> :: InputString e (a -> b) -> InputString e a -> InputString e b
$c<*> :: forall e a b.
InputString e (a -> b) -> InputString e a -> InputString e b
pure :: a -> InputString e a
$cpure :: forall e a. a -> InputString e a
$cp1Applicative :: forall e. Functor (InputString e)
Applicative,
        Applicative (InputString e)
a -> InputString e a
Applicative (InputString e)
-> (forall a b.
    InputString e a -> (a -> InputString e b) -> InputString e b)
-> (forall a b.
    InputString e a -> InputString e b -> InputString e b)
-> (forall a. a -> InputString e a)
-> Monad (InputString e)
InputString e a -> (a -> InputString e b) -> InputString e b
InputString e a -> InputString e b -> InputString e b
forall e. Applicative (InputString e)
forall a. a -> InputString e a
forall e a. a -> InputString e a
forall a b. InputString e a -> InputString e b -> InputString e b
forall a b.
InputString e a -> (a -> InputString e b) -> InputString e b
forall e a b. InputString e a -> InputString e b -> InputString e b
forall e a b.
InputString e a -> (a -> InputString e b) -> InputString e b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> InputString e a
$creturn :: forall e a. a -> InputString e a
>> :: InputString e a -> InputString e b -> InputString e b
$c>> :: forall e a b. InputString e a -> InputString e b -> InputString e b
>>= :: InputString e a -> (a -> InputString e b) -> InputString e b
$c>>= :: forall e a b.
InputString e a -> (a -> InputString e b) -> InputString e b
$cp1Monad :: forall e. Applicative (InputString e)
Monad
    ) via State (InputStringContext e)

runInputString :: InputString e a -> [e] -> (a, InputStringContext e)
runInputString :: InputString e a -> [e] -> (a, InputStringContext e)
runInputString (InputString State (InputStringContext e) a
runner) [e]
input =
    State (InputStringContext e) a
-> InputStringContext e -> (a, InputStringContext e)
forall s a. State s a -> s -> (a, s)
runState State (InputStringContext e) a
runner do [e] -> InputStringContext e
forall e. [e] -> InputStringContext e
initialInputStringContext [e]
input

instance Enum e => Tlex.TlexContext (InputStringContext e) e (InputString e) where
    tlexGetInputPart :: InputString e (Maybe e)
tlexGetInputPart = State (InputStringContext e) (Maybe e) -> InputString e (Maybe e)
forall e a. State (InputStringContext e) a -> InputString e a
InputString do
        InputStringContext e
inputCtx <- StateT (InputStringContext e) Identity (InputStringContext e)
forall (m :: * -> *) s. Monad m => StateT s m s
get
        case InputStringContext e -> [e]
forall e. InputStringContext e -> [e]
inputStringCtxRest InputStringContext e
inputCtx of
            []  -> Maybe e -> State (InputStringContext e) (Maybe e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe e
forall a. Maybe a
Nothing
            e
c:[e]
r -> do
                InputStringContext e -> StateT (InputStringContext e) Identity ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put do InputStringContext :: forall e. [e] -> Int -> InputStringContext e
InputStringContext
                        { $sel:inputStringCtxRest:InputStringContext :: [e]
inputStringCtxRest = [e]
r
                        , $sel:inputStringCtxPos:InputStringContext :: Int
inputStringCtxPos = Int -> Int
forall a. Enum a => a -> a
succ do InputStringContext e -> Int
forall e. InputStringContext e -> Int
inputStringCtxPos InputStringContext e
inputCtx
                        }
                Maybe e -> State (InputStringContext e) (Maybe e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure do e -> Maybe e
forall a. a -> Maybe a
Just e
c
    tlexGetMark :: InputString e (InputStringContext e)
tlexGetMark = StateT (InputStringContext e) Identity (InputStringContext e)
-> InputString e (InputStringContext e)
forall e a. State (InputStringContext e) a -> InputString e a
InputString StateT (InputStringContext e) Identity (InputStringContext e)
forall (m :: * -> *) s. Monad m => StateT s m s
get