module Graphics.Vty.Input.Classify.Parse
( Parser
, runParser
, failParse
, readInt
, readChar
, expectChar
)
where
import Graphics.Vty.Input.Events
import Graphics.Vty.Input.Classify.Types
import Control.Monad.Trans.Maybe
import Control.Monad.State
type Parser a = MaybeT (State String) a
runParser :: String -> Parser Event -> KClass
runParser :: String -> Parser Event -> KClass
runParser String
s Parser Event
parser =
case State String (Maybe Event) -> String -> (Maybe Event, String)
forall s a. State s a -> s -> (a, s)
runState (Parser Event -> State String (Maybe Event)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT Parser Event
parser) String
s of
(Maybe Event
Nothing, String
_) -> KClass
Invalid
(Just Event
e, String
remaining) -> Event -> String -> KClass
Valid Event
e String
remaining
failParse :: Parser a
failParse :: Parser a
failParse = String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"invalid parse"
readInt :: Parser Int
readInt :: Parser Int
readInt = do
String
s <- MaybeT (State String) String
forall s (m :: * -> *). MonadState s m => m s
get
case (ReadS Int
forall a. Read a => ReadS a
reads :: ReadS Int) String
s of
[(Int
i, String
rest)] -> String -> MaybeT (State String) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put String
rest MaybeT (State String) () -> Parser Int -> Parser Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Parser Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
[(Int, String)]
_ -> Parser Int
forall a. Parser a
failParse
readChar :: Parser Char
readChar :: Parser Char
readChar = do
String
s <- MaybeT (State String) String
forall s (m :: * -> *). MonadState s m => m s
get
case String
s of
Char
c:String
rest -> String -> MaybeT (State String) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put String
rest MaybeT (State String) () -> Parser Char -> Parser Char
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Parser Char
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c
String
_ -> Parser Char
forall a. Parser a
failParse
expectChar :: Char -> Parser ()
expectChar :: Char -> MaybeT (State String) ()
expectChar Char
c = do
Char
c' <- Parser Char
readChar
if Char
c' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c then () -> MaybeT (State String) ()
forall (m :: * -> *) a. Monad m => a -> m a
return () else MaybeT (State String) ()
forall a. Parser a
failParse