{-# LANGUAGE FlexibleInstances, 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 buffered scanners as well as a buffering monad transformer.
module Text.Chatty.Scanner.Buffered where

import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.State
import Control.Monad.Trans.Class
import Text.Chatty.Scanner

-- | Typeclass for all buffered 'ChScanner's.
class ChScanner m => ChBufferedScanner m where
  -- | Scan the next character without removing it.
  mpeek1 :: m Char
  -- | Prepend the given character to the scannable input.
  mprepend :: String -> m ()

-- | Typeclass for all 'BufferedScanner's with support for pushing and popping.
class ChBufferedScanner m => ChStackBufferedScanner m where
  -- | Push the current input state to the stack.
  mpush :: m ()
  -- | Pop the previous input state from the stack.
  mpop :: m ()

instance Monad m => ChBufferedScanner (StateT String m) where
  mpeek1 :: StateT String m Char
mpeek1 = (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
  mprepend :: String -> StateT String m ()
mprepend String
s = (String -> String) -> StateT String m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (String
sString -> String -> String
forall a. [a] -> [a] -> [a]
++)
  
instance Monad m => ChBufferedScanner (HereStringT m) where
  mpeek1 :: HereStringT m Char
mpeek1 = (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
$ \String
ss -> (Char, String) -> m (Char, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Char
forall a. [a] -> a
head String
ss, String
ss)
  mprepend :: String -> HereStringT m ()
mprepend String
s = (String -> m ((), String)) -> HereStringT m ()
forall (m :: * -> *) a.
(String -> m (a, String)) -> HereStringT m a
HereString ((String -> m ((), String)) -> HereStringT m ())
-> (String -> m ((), String)) -> HereStringT m ()
forall a b. (a -> b) -> a -> b
$ \String
ss -> ((), String) -> m ((), String)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), String
sString -> String -> String
forall a. [a] -> [a] -> [a]
++String
ss)

-- | A buffering 'MonadScanner' transformer that lets you use 'mpeek1' and 'mprepend' everywhere.
newtype ScannerBufferT m a = ScannerBuffer { ScannerBufferT m a -> [String] -> m (a, [String])
runScannerBufferT :: [String] -> m (a,[String]) }

instance Monad m => Monad (ScannerBufferT m) where
  return :: a -> ScannerBufferT m a
return a
a = ([String] -> m (a, [String])) -> ScannerBufferT m a
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (a, [String])) -> ScannerBufferT m a)
-> ([String] -> m (a, [String])) -> ScannerBufferT 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)
  (ScannerBuffer [String] -> m (a, [String])
c) >>= :: ScannerBufferT m a
-> (a -> ScannerBufferT m b) -> ScannerBufferT m b
>>= a -> ScannerBufferT m b
f = ([String] -> m (b, [String])) -> ScannerBufferT m b
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (b, [String])) -> ScannerBufferT m b)
-> ([String] -> m (b, [String])) -> ScannerBufferT m b
forall a b. (a -> b) -> a -> b
$ \[String]
s -> do (a
a,[String]
s') <- [String] -> m (a, [String])
c [String]
s; ScannerBufferT m b -> [String] -> m (b, [String])
forall (m :: * -> *) a.
ScannerBufferT m a -> [String] -> m (a, [String])
runScannerBufferT (a -> ScannerBufferT m b
f a
a) [String]
s'

instance MonadTrans ScannerBufferT where
  lift :: m a -> ScannerBufferT m a
lift m a
m = ([String] -> m (a, [String])) -> ScannerBufferT m a
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (a, [String])) -> ScannerBufferT m a)
-> ([String] -> m (a, [String])) -> ScannerBufferT 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 (ScannerBufferT m) where
  fmap :: (a -> b) -> ScannerBufferT m a -> ScannerBufferT m b
fmap = (a -> b) -> ScannerBufferT m a -> ScannerBufferT m b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM

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

instance ChScanner m => ChScanner (ScannerBufferT m) where
  mscan1 :: ScannerBufferT m Char
mscan1 = ([String] -> m (Char, [String])) -> ScannerBufferT m Char
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (Char, [String])) -> ScannerBufferT m Char)
-> ([String] -> m (Char, [String])) -> ScannerBufferT m Char
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> (if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ss then do Char
s <- m Char
forall (m :: * -> *). ChScanner m => m Char
mscan1; (Char, [String]) -> m (Char, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
s,[]String -> [String] -> [String]
forall a. a -> [a] -> [a]
:(String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Char
sChar -> String -> String
forall a. a -> [a] -> [a]
:) [String]
sx) else (Char, [String]) -> m (Char, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Char
forall a. [a] -> a
head String
ss,String -> String
forall a. [a] -> [a]
tail String
ssString -> [String] -> [String]
forall a. a -> [a] -> [a]
:(String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Char
forall a. [a] -> a
head String
ssChar -> String -> String
forall a. a -> [a] -> [a]
:) [String]
sx))
  mscanL :: ScannerBufferT m String
mscanL = ([String] -> m (String, [String])) -> ScannerBufferT m String
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (String, [String])) -> ScannerBufferT m String)
-> ([String] -> m (String, [String])) -> ScannerBufferT m String
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> do String
l <- m String
forall (m :: * -> *). ChScanner m => m String
mscanL; (String, [String]) -> m (String, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (String
ssString -> String -> String
forall a. [a] -> [a] -> [a]
++String
l, []String -> [String] -> [String]
forall a. a -> [a] -> [a]
:(String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String -> String -> String
forall a. [a] -> [a] -> [a]
++String
l) [String]
sx)
  mscannable :: ScannerBufferT m Bool
mscannable = ([String] -> m (Bool, [String])) -> ScannerBufferT m Bool
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (Bool, [String])) -> ScannerBufferT m Bool)
-> ([String] -> m (Bool, [String])) -> ScannerBufferT m Bool
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> (if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ss then do Bool
b <- m Bool
forall (m :: * -> *). ChScanner m => m Bool
mscannable; (Bool, [String]) -> m (Bool, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
b,[]String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx) else (Bool, [String]) -> m (Bool, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True,String
ssString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx))
  mscanh :: ScannerBufferT m (Maybe Handle)
mscanh = Maybe Handle -> ScannerBufferT m (Maybe Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Handle
forall a. Maybe a
Nothing
  mready :: ScannerBufferT m Bool
mready = ([String] -> m (Bool, [String])) -> ScannerBufferT m Bool
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (Bool, [String])) -> ScannerBufferT m Bool)
-> ([String] -> m (Bool, [String])) -> ScannerBufferT m Bool
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> (if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ss then do Bool
b <- m Bool
forall (m :: * -> *). ChScanner m => m Bool
mready; (Bool, [String]) -> m (Bool, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
b,[]String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx) else (Bool, [String]) -> m (Bool, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True,String
ssString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx))

instance MonadIO m => MonadIO (ScannerBufferT m) where
  liftIO :: IO a -> ScannerBufferT m a
liftIO = m a -> ScannerBufferT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ScannerBufferT m a)
-> (IO a -> m a) -> IO a -> ScannerBufferT 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 ChScanner m => ChBufferedScanner (ScannerBufferT m) where
  mpeek1 :: ScannerBufferT m Char
mpeek1 = ([String] -> m (Char, [String])) -> ScannerBufferT m Char
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m (Char, [String])) -> ScannerBufferT m Char)
-> ([String] -> m (Char, [String])) -> ScannerBufferT m Char
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> (if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ss then do Char
s <- m Char
forall (m :: * -> *). ChScanner m => m Char
mscan1; (Char, [String]) -> m (Char, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
s,[Char
s]String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx) else (Char, [String]) -> m (Char, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Char
forall a. [a] -> a
head String
ss,String
ssString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx))
  mprepend :: String -> ScannerBufferT m ()
mprepend String
s = ([String] -> m ((), [String])) -> ScannerBufferT m ()
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m ((), [String])) -> ScannerBufferT m ())
-> ([String] -> m ((), [String])) -> ScannerBufferT m ()
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> ((), [String]) -> m ((), [String])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),(String
sString -> String -> String
forall a. [a] -> [a] -> [a]
++String
ss)String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx)

instance ChScanner m => ChStackBufferedScanner (ScannerBufferT m) where
  mpush :: ScannerBufferT m ()
mpush = ([String] -> m ((), [String])) -> ScannerBufferT m ()
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m ((), [String])) -> ScannerBufferT m ())
-> ([String] -> m ((), [String])) -> ScannerBufferT m ()
forall a b. (a -> b) -> a -> b
$ \(String
ss:[String]
sx) -> ((), [String]) -> m ((), [String])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),String
ssString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[]String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
sx)
  mpop :: ScannerBufferT m ()
mpop = ([String] -> m ((), [String])) -> ScannerBufferT m ()
forall (m :: * -> *) a.
([String] -> m (a, [String])) -> ScannerBufferT m a
ScannerBuffer (([String] -> m ((), [String])) -> ScannerBufferT m ())
-> ([String] -> m ((), [String])) -> ScannerBufferT m ()
forall a b. (a -> b) -> a -> b
$ \(String
_:[String]
sx) -> ((), [String]) -> m ((), [String])
forall (m :: * -> *) a. Monad m => a -> m a
return ((),[String]
sx)