packages feed

chatty-0.8.0.1: Text/Chatty/Scanner.hs

{-# LANGUAGE FlexibleInstances, FunctionalDependencies, ConstraintKinds, KindSignatures #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014,2021,2026 Enum 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 Control.Applicative
import Control.Monad
import Control.Monad.State
import Control.Monad.Identity
import Control.Monad.Trans.Class
import Control.Monad.Trans.Resource
import System.IO
import Data.Kind

-- | 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 = return Nothing
  -- | Input available yet?
  mready :: m Bool

-- ChScanner instance for: IO
instance ChScanner IO where
  mscan1 = getChar
  mscanL = getContents
  mscannable = fmap not isEOF
  mscanh = return $ Just stdin
  mready = hReady stdin

-- ChScanner instance for: StateT String
instance Monad m => ChScanner (StateT String m) where
  mscan1 = do
    c <- gets head
    modify tail
    return c
  mscanL = do
    s <- get
    put []
    return s
  mscannable = gets (not.null)
  mready = return 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 { runHereStringT :: String -> m (a,String) }

instance Monad m => Monad (HereStringT m) where
  (HereString h) >>= f = HereString $ \s -> do (a,s') <- h s; runHereStringT (f a) s'

instance MonadTrans HereStringT where
  lift m = HereString $ \s -> do a <- m; return (a,s)

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

instance Monad m => Applicative (HereStringT m) where
  (<*>) = ap
  pure a = HereString $ \s -> return (a,s)

instance Monad m => ChScanner (HereStringT m) where
  mscan1 = HereString $ \(s:ss) -> return (s,ss)
  mscanL = HereString $ \s -> return (s,[])
  mscannable = HereString $ \s -> return (not $ null s,s)
  mready = return True

instance MonadIO m => MonadIO (HereStringT m) where
  liftIO = lift . liftIO

instance MonadResource m => MonadResource (HereStringT m) where
  liftResourceT = lift . liftResourceT

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

instance Monad m => Monad (QuietT m) where
  (Quiet q) >>= f = Quiet $ do q' <- q; runQuietT (f q')

instance MonadTrans QuietT where
  lift = Quiet

instance Monad m => ChScanner (QuietT m) where
  mscan1 = return undefined
  mscanL = return []
  mscannable = return False
  mready = return False

instance Functor m => Functor (QuietT m) where
  fmap f (Quiet a) = Quiet $ fmap f a

instance Monad m => Applicative (QuietT m) where
  (<*>) = ap
  pure = Quiet . return

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

instance Monad m => Monad (InRedirT m) where
  (InRedir r) >>= f = InRedir $ \h -> do a <- r h; runInRedirT (f a) h

instance MonadTrans InRedirT where
  lift m = InRedir $ \_ -> m

instance MonadIO m => MonadIO (InRedirT m) where
  liftIO = lift . liftIO

instance MonadIO m => ChScanner (InRedirT m) where
  mscan1 = InRedir $ \h -> liftIO (hGetChar h)
  mscanL = InRedir $ \h -> liftIO (hGetContents h)
  mscannable = InRedir $ \h -> liftIO (hIsEOF h)
  mscanh = InRedir $ \h -> return (Just h)
  mready = InRedir $ \h -> liftIO (hReady h)

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

instance Monad m => Applicative (InRedirT m) where
  (<*>) = ap
  pure a = InRedir $ \h -> return a

instance MonadResource m => MonadResource (InRedirT m) where
  liftResourceT = lift . liftResourceT

-- | Run InRedir with handle
runInRedir :: InRedir a -> Handle -> IO a
runInRedir m h = runInRedirT m h

-- | Run InRedirT with a filename
runInRedirFT :: (Functor m,MonadResource m) => InRedirT m a -> FilePath -> m a
runInRedirFT m fp = do
  (key, h) <- allocate (liftIO $ openFile fp ReadMode) (liftIO . hClose)
  a <- runInRedirT m h
  a `seq` release key
  return a

-- | Line-scanning alternative to mscan1/L
mscanLn :: ChScanner m => m String
mscanLn = do
  h <- mscan1
  if h == '\n' then return ""
               else do
                 hs <- mscanLn
                 return (h:hs)

-- | Scan a fixed number of chars
mscanN :: ChScanner m => Int -> m String
mscanN n
  | n > 0 = do
    b <- mscannable
    if not b then return []
             else do
               h <- mscan1
               hs <- mscanN (n-1)
               return (h:hs)
  | otherwise = return []

-- Shell-like syntax
-- | Redirection source that does not provide any output
data EmptyI = EmptyI
-- | Class for all primitive redirection sources.
class RedirectionSource t (c :: (* -> *) -> Constraint) mt a r | t -> mt c, t a -> r where
  -- | Redirection
  (.<.) :: (c m,ChScanner (mt m)) => mt m a -> t -> m r
instance RedirectionSource EmptyI Monad QuietT a a where
  m .<. _ = runQuietT m
instance RedirectionSource FilePath MonadResource InRedirT a a where
  m .<. fp = runInRedirFT m fp
instance RedirectionSource Handle MonadIO InRedirT a a where
  m .<. fp = runInRedirT m 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
  m .<<. str = fmap fst $ runHereStringT m str