{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies, Safe #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides a typeclass for all monads that may scan text.
module Text.Chatty.Scanner where

import Text.Chatty.Finalizer
import Control.Applicative
import Control.Monad
import Control.Monad.State
import Control.Monad.Identity
import Control.Monad.Trans.Class
import System.IO

-- | A typeclass for all monads that may read input.
class Monad m => ChScanner m where
  -- | Read one single character
  mscan1 :: m Char
  -- | Lazily read all the input.
  mscanL :: m String
  -- | Input readable? (not EOF)
  mscannable :: m Bool
  -- | Return FD handle, if available
  mscanh :: m (Maybe Handle)
  mscanh = Maybe Handle -> m (Maybe Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Handle
forall a. Maybe a
Nothing
  -- | Input available yet?
  mready :: m Bool

-- ChScanner instance for: IO
instance ChScanner IO where
  mscan1 :: IO Char
mscan1 = IO Char
getChar
  mscanL :: IO String
mscanL = IO String
getContents
  mscannable :: IO Bool
mscannable = (Bool -> Bool) -> IO Bool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not IO Bool
isEOF
  mscanh :: IO (Maybe Handle)
mscanh = Maybe Handle -> IO (Maybe Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Handle -> IO (Maybe Handle))
-> Maybe Handle -> IO (Maybe Handle)
forall a b. (a -> b) -> a -> b
$ Handle -> Maybe Handle
forall a. a -> Maybe a
Just Handle
stdin
  mready :: IO Bool
mready = Handle -> IO Bool
hReady Handle
stdin

-- ChScanner instance for: StateT String
instance Monad m => ChScanner (StateT String m) where
  mscan1 :: StateT String m Char
mscan1 = do
    Char
c <- (String -> Char) -> StateT String m Char
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets String -> Char
forall a. [a] -> a
head
    (String -> String) -> StateT String m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify String -> String
forall a. [a] -> [a]
tail
    Char -> StateT String m Char
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c
  mscanL :: StateT String m String
mscanL = do
    String
s <- StateT String m String
forall s (m :: * -> *). MonadState s m => m s
get
    String -> StateT String m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put []
    String -> StateT String m String
forall (m :: * -> *) a. Monad m => a -> m a
return String
s
  mscannable :: StateT String m Bool
mscannable = (String -> Bool) -> StateT String m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Bool -> Bool
not(Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)
  mready :: StateT String m Bool
mready = Bool -> StateT String m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

-- Definition of HereStringT + instances
-- | HereStringT holds a given string and uses it as input for the function
-- (much like here-strings in the shell)
newtype HereStringT m a = HereString { HereStringT m a -> String -> m (a, String)
runHereStringT :: String -> m (a,String) }

instance Monad m => Monad (HereStringT m) where
  return :: a -> HereStringT m a
return a
a = (String -> m (a, String)) -> HereStringT m a
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (a, String)) -> HereStringT m a)
-> (String -> m (a, String)) -> HereStringT m a
forall a b. (a -> b) -> a -> b
$ \String
s -> (a, String) -> m (a, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,String
s)
  (HereString String -> m (a, String)
h) >>= :: HereStringT m a -> (a -> HereStringT m b) -> HereStringT m b
>>= a -> HereStringT m b
f = (String -> m (b, String)) -> HereStringT m b
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (b, String)) -> HereStringT m b)
-> (String -> m (b, String)) -> HereStringT m b
forall a b. (a -> b) -> a -> b
$ \String
s -> do (a
a,String
s') <- String -> m (a, String)
h String
s; HereStringT m b -> String -> m (b, String)
forall (m :: * -> *) a. HereStringT m a -> String -> m (a, String)
runHereStringT (a -> HereStringT m b
f a
a) String
s'

instance MonadTrans HereStringT where
  lift :: m a -> HereStringT m a
lift m a
m = (String -> m (a, String)) -> HereStringT m a
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (a, String)) -> HereStringT m a)
-> (String -> m (a, String)) -> HereStringT m a
forall a b. (a -> b) -> a -> b
$ \String
s -> do a
a <- m a
m; (a, String) -> m (a, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,String
s)

instance Monad m => Functor (HereStringT m) where
  fmap :: (a -> b) -> HereStringT m a -> HereStringT m b
fmap a -> b
f HereStringT m a
a = (String -> m (b, String)) -> HereStringT m b
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (b, String)) -> HereStringT m b)
-> (String -> m (b, String)) -> HereStringT m b
forall a b. (a -> b) -> a -> b
$ \String
s -> do (a
a',String
s') <- HereStringT m a -> String -> m (a, String)
forall (m :: * -> *) a. HereStringT m a -> String -> m (a, String)
runHereStringT HereStringT m a
a String
s; (b, String) -> m (b, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
a',String
s')

instance Monad m => Applicative (HereStringT m) where
  <*> :: HereStringT m (a -> b) -> HereStringT m a -> HereStringT m b
(<*>) = HereStringT m (a -> b) -> HereStringT m a -> HereStringT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> HereStringT m a
pure = a -> HereStringT m a
forall (m :: * -> *) a. Monad m => a -> m a
return

instance Monad m => ChScanner (HereStringT m) where
  mscan1 :: HereStringT m Char
mscan1 = (String -> m (Char, String)) -> HereStringT m Char
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (Char, String)) -> HereStringT m Char)
-> (String -> m (Char, String)) -> HereStringT m Char
forall a b. (a -> b) -> a -> b
$ \(Char
s:String
ss) -> (Char, String) -> m (Char, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
s,String
ss)
  mscanL :: HereStringT m String
mscanL = (String -> m (String, String)) -> HereStringT m String
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (String, String)) -> HereStringT m String)
-> (String -> m (String, String)) -> HereStringT m String
forall a b. (a -> b) -> a -> b
$ \String
s -> (String, String) -> m (String, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (String
s,[])
  mscannable :: HereStringT m Bool
mscannable = (String -> m (Bool, String)) -> HereStringT m Bool
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m (Bool, String)) -> HereStringT m Bool)
-> (String -> m (Bool, String)) -> HereStringT m Bool
forall a b. (a -> b) -> a -> b
$ \String
s -> (Bool, String) -> m (Bool, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s,String
s)
  mready :: HereStringT m Bool
mready = Bool -> HereStringT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

instance MonadIO m => MonadIO (HereStringT m) where
  liftIO :: IO a -> HereStringT m a
liftIO = m a -> HereStringT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> HereStringT m a)
-> (IO a -> m a) -> IO a -> HereStringT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance ChFinalizer m => ChFinalizer (HereStringT m) where
  mqfh :: Handle -> HereStringT m ()
mqfh = m () -> HereStringT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HereStringT m ())
-> (Handle -> m ()) -> Handle -> HereStringT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ()
forall (m :: * -> *). ChFinalizer m => Handle -> m ()
mqfh
  mfin :: HereStringT m ()
mfin = m () -> HereStringT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall (m :: * -> *). ChFinalizer m => m ()
mfin

-- Definition of QuietT + instances
-- | QuietT does not convey any input (much like </dev/null in the shell)
newtype QuietT m a = Quiet { QuietT m a -> m a
runQuietT :: m a }

instance Monad m => Monad (QuietT m) where
  return :: a -> QuietT m a
return = m a -> QuietT m a
forall (m :: * -> *) a. m a -> QuietT m a
Quiet (m a -> QuietT m a) -> (a -> m a) -> a -> QuietT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
  (Quiet m a
q) >>= :: QuietT m a -> (a -> QuietT m b) -> QuietT m b
>>= a -> QuietT m b
f = m b -> QuietT m b
forall (m :: * -> *) a. m a -> QuietT m a
Quiet (m b -> QuietT m b) -> m b -> QuietT m b
forall a b. (a -> b) -> a -> b
$ do a
q' <- m a
q; QuietT m b -> m b
forall (m :: * -> *) a. QuietT m a -> m a
runQuietT (a -> QuietT m b
f a
q')

instance MonadTrans QuietT where
  lift :: m a -> QuietT m a
lift = m a -> QuietT m a
forall (m :: * -> *) a. m a -> QuietT m a
Quiet

instance Monad m => ChScanner (QuietT m) where
  mscan1 :: QuietT m Char
mscan1 = Char -> QuietT m Char
forall (m :: * -> *) a. Monad m => a -> m a
return Char
forall a. HasCallStack => a
undefined
  mscanL :: QuietT m String
mscanL = String -> QuietT m String
forall (m :: * -> *) a. Monad m => a -> m a
return []
  mscannable :: QuietT m Bool
mscannable = Bool -> QuietT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
  mready :: QuietT m Bool
mready = Bool -> QuietT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

instance Functor m => Functor (QuietT m) where
  fmap :: (a -> b) -> QuietT m a -> QuietT m b
fmap a -> b
f (Quiet m a
a) = m b -> QuietT m b
forall (m :: * -> *) a. m a -> QuietT m a
Quiet (m b -> QuietT m b) -> m b -> QuietT m b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f m a
a

instance (Functor m, Monad m) => Applicative (QuietT m) where
  <*> :: QuietT m (a -> b) -> QuietT m a -> QuietT m b
(<*>) = QuietT m (a -> b) -> QuietT m a -> QuietT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> QuietT m a
pure = a -> QuietT m a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- Definition of InRedirT + instances
-- | InRedirT redirects all input to a given handle (much like <filename in the shell)
newtype InRedirT m a = InRedir { InRedirT m a -> Handle -> m a
runInRedirT :: Handle -> m a }
-- | InRedirT on an IO monad
type InRedir = InRedirT (HandleCloserT IO)

instance Monad m => Monad (InRedirT m) where
  return :: a -> InRedirT m a
return a
a = (Handle -> m a) -> InRedirT m a
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m a) -> InRedirT m a)
-> (Handle -> m a) -> InRedirT m a
forall a b. (a -> b) -> a -> b
$ \Handle
h -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
  (InRedir Handle -> m a
r) >>= :: InRedirT m a -> (a -> InRedirT m b) -> InRedirT m b
>>= a -> InRedirT m b
f = (Handle -> m b) -> InRedirT m b
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m b) -> InRedirT m b)
-> (Handle -> m b) -> InRedirT m b
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do a
a <- Handle -> m a
r Handle
h; InRedirT m b -> Handle -> m b
forall (m :: * -> *) a. InRedirT m a -> Handle -> m a
runInRedirT (a -> InRedirT m b
f a
a) Handle
h

instance MonadTrans InRedirT where
  lift :: m a -> InRedirT m a
lift m a
m = (Handle -> m a) -> InRedirT m a
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m a) -> InRedirT m a)
-> (Handle -> m a) -> InRedirT m a
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do a
a <- m a
m; a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a

instance MonadIO m => MonadIO (InRedirT m) where
  liftIO :: IO a -> InRedirT m a
liftIO = m a -> InRedirT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> InRedirT m a) -> (IO a -> m a) -> IO a -> InRedirT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance MonadIO m => ChScanner (InRedirT m) where
  mscan1 :: InRedirT m Char
mscan1 = (Handle -> m Char) -> InRedirT m Char
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m Char) -> InRedirT m Char)
-> (Handle -> m Char) -> InRedirT m Char
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do Char
c <- IO Char -> m Char
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Char -> m Char) -> IO Char -> m Char
forall a b. (a -> b) -> a -> b
$ Handle -> IO Char
hGetChar Handle
h; Char -> m Char
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c
  mscanL :: InRedirT m String
mscanL = (Handle -> m String) -> InRedirT m String
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m String) -> InRedirT m String)
-> (Handle -> m String) -> InRedirT m String
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do String
s <- IO String -> m String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> m String) -> IO String -> m String
forall a b. (a -> b) -> a -> b
$ Handle -> IO String
hGetContents Handle
h; String -> m String
forall (m :: * -> *) a. Monad m => a -> m a
return String
s
  mscannable :: InRedirT m Bool
mscannable = (Handle -> m Bool) -> InRedirT m Bool
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m Bool) -> InRedirT m Bool)
-> (Handle -> m Bool) -> InRedirT m Bool
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do Bool
b <- IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsEOF Handle
h; Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
b
  mscanh :: InRedirT m (Maybe Handle)
mscanh = (Handle -> m (Maybe Handle)) -> InRedirT m (Maybe Handle)
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m (Maybe Handle)) -> InRedirT m (Maybe Handle))
-> (Handle -> m (Maybe Handle)) -> InRedirT m (Maybe Handle)
forall a b. (a -> b) -> a -> b
$ \Handle
h -> Maybe Handle -> m (Maybe Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle -> Maybe Handle
forall a. a -> Maybe a
Just Handle
h)
  mready :: InRedirT m Bool
mready = (Handle -> m Bool) -> InRedirT m Bool
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m Bool) -> InRedirT m Bool)
-> (Handle -> m Bool) -> InRedirT m Bool
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do Bool
r <- IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hReady Handle
h; Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
r

instance Monad m => Functor (InRedirT m) where
  fmap :: (a -> b) -> InRedirT m a -> InRedirT m b
fmap a -> b
f InRedirT m a
a = (Handle -> m b) -> InRedirT m b
forall (m :: * -> *) a. (Handle -> m a) -> InRedirT m a
InRedir ((Handle -> m b) -> InRedirT m b)
-> (Handle -> m b) -> InRedirT m b
forall a b. (a -> b) -> a -> b
$ \Handle
h -> do a
a' <- InRedirT m a -> Handle -> m a
forall (m :: * -> *) a. InRedirT m a -> Handle -> m a
runInRedirT InRedirT m a
a Handle
h; b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
a')

instance Monad m => Applicative (InRedirT m) where
  <*> :: InRedirT m (a -> b) -> InRedirT m a -> InRedirT m b
(<*>) = InRedirT m (a -> b) -> InRedirT m a -> InRedirT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> InRedirT m a
pure = a -> InRedirT m a
forall (m :: * -> *) a. Monad m => a -> m a
return

instance ChFinalizer m => ChFinalizer (InRedirT m) where
  mqfh :: Handle -> InRedirT m ()
mqfh = m () -> InRedirT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> InRedirT m ())
-> (Handle -> m ()) -> Handle -> InRedirT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ()
forall (m :: * -> *). ChFinalizer m => Handle -> m ()
mqfh
  mfin :: InRedirT m ()
mfin = m () -> InRedirT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall (m :: * -> *). ChFinalizer m => m ()
mfin

-- | Run InRedir with handle
runInRedir :: InRedir a -> Handle -> IO a
runInRedir :: InRedir a -> Handle -> IO a
runInRedir InRedir a
m Handle
h = HandleCloserT IO a -> IO a
forall (m :: * -> *) a.
(MonadIO m, Functor m) =>
HandleCloserT m a -> m a
withLazyIO (HandleCloserT IO a -> IO a) -> HandleCloserT IO a -> IO a
forall a b. (a -> b) -> a -> b
$ InRedir a -> Handle -> HandleCloserT IO a
forall (m :: * -> *) a. InRedirT m a -> Handle -> m a
runInRedirT InRedir a
m Handle
h

-- | Run InRedirT with a filename
runInRedirFT :: (Functor m,MonadIO m,ChFinalizer m) => InRedirT m a -> FilePath -> m a
runInRedirFT :: InRedirT m a -> String -> m a
runInRedirFT InRedirT m a
m String
fp = do
  Handle
h <- IO Handle -> m Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Handle -> m Handle) -> IO Handle -> m Handle
forall a b. (a -> b) -> a -> b
$ String -> IOMode -> IO Handle
openFile String
fp IOMode
ReadMode
  a
a <- InRedirT m a -> Handle -> m a
forall (m :: * -> *) a. InRedirT m a -> Handle -> m a
runInRedirT InRedirT m a
m Handle
h
  Handle -> m ()
forall (m :: * -> *). ChFinalizer m => Handle -> m ()
mqfh Handle
h
  --liftIO $ hClose h
  a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a

-- | Run InRedir with a filename
runInRedirF :: InRedir a -> FilePath -> IO a
runInRedirF :: InRedir a -> String -> IO a
runInRedirF InRedir a
m String
fp = HandleCloserT IO a -> IO a
forall (m :: * -> *) a.
(MonadIO m, Functor m) =>
HandleCloserT m a -> m a
withLazyIO (HandleCloserT IO a -> IO a) -> HandleCloserT IO a -> IO a
forall a b. (a -> b) -> a -> b
$ InRedir a -> String -> HandleCloserT IO a
forall (m :: * -> *) a.
(Functor m, MonadIO m, ChFinalizer m) =>
InRedirT m a -> String -> m a
runInRedirFT InRedir a
m String
fp

-- | Line-scanning alternative to mscan1/L
mscanLn :: ChScanner m => m String
mscanLn :: m String
mscanLn = do
  Char
h <- m Char
forall (m :: * -> *). ChScanner m => m Char
mscan1
  if Char
h Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n' then String -> m String
forall (m :: * -> *) a. Monad m => a -> m a
return String
""
               else do
                 String
hs <- m String
forall (m :: * -> *). ChScanner m => m String
mscanLn
                 String -> m String
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
hChar -> String -> String
forall a. a -> [a] -> [a]
:String
hs)

-- | Scan a fixed number of chars
mscanN :: ChScanner m => Int -> m String
mscanN :: Int -> m String
mscanN Int
n
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = do
    Bool
b <- m Bool
forall (m :: * -> *). ChScanner m => m Bool
mscannable
    if Bool -> Bool
not Bool
b then String -> m String
forall (m :: * -> *) a. Monad m => a -> m a
return []
             else do
               Char
h <- m Char
forall (m :: * -> *). ChScanner m => m Char
mscan1
               String
hs <- Int -> m String
forall (m :: * -> *). ChScanner m => Int -> m String
mscanN (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
               String -> m String
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
hChar -> String -> String
forall a. a -> [a] -> [a]
:String
hs)
  | Bool
otherwise = String -> m String
forall (m :: * -> *) a. Monad m => a -> m a
return []

-- Shell-like syntax
-- | Redirection source that does not provide any output
data EmptyI = EmptyI
-- | Class for all primitive redirection sources.
class RedirectionSource t mt a r | t -> mt, t a -> r where
  -- | Redirection
  (.<.) :: (ChFinalizer m,Functor m,MonadIO m,ChScanner (mt m)) => mt m a -> t -> m r
instance RedirectionSource EmptyI QuietT a a where
  QuietT m a
m .<. :: QuietT m a -> EmptyI -> m a
.<. EmptyI
_ = QuietT m a -> m a
forall (m :: * -> *) a. QuietT m a -> m a
runQuietT QuietT m a
m
instance RedirectionSource FilePath InRedirT a a where
  InRedirT m a
m .<. :: InRedirT m a -> String -> m a
.<. String
fp = InRedirT m a -> String -> m a
forall (m :: * -> *) a.
(Functor m, MonadIO m, ChFinalizer m) =>
InRedirT m a -> String -> m a
runInRedirFT InRedirT m a
m String
fp
instance RedirectionSource Handle InRedirT a a where
  InRedirT m a
m .<. :: InRedirT m a -> Handle -> m a
.<. Handle
fp = InRedirT m a -> Handle -> m a
forall (m :: * -> *) a. InRedirT m a -> Handle -> m a
runInRedirT InRedirT m a
m Handle
fp
-- | Class for all Here-Documents
class RedirectionHeredoc t mt a r | t -> mt, t a -> r where
  -- | Redirection
  (.<<.) :: (Functor m,ChScanner (mt m)) => mt m a -> t -> m r
instance RedirectionHeredoc String HereStringT a a where
  HereStringT m a
m .<<. :: HereStringT m a -> String -> m a
.<<. String
str = ((a, String) -> a) -> m (a, String) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, String) -> a
forall a b. (a, b) -> a
fst (m (a, String) -> m a) -> m (a, String) -> m a
forall a b. (a -> b) -> a -> b
$ HereStringT m a -> String -> m (a, String)
forall (m :: * -> *) a. HereStringT m a -> String -> m (a, String)
runHereStringT HereStringT m a
m String
str