{-# 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 = gets head
  mprepend s = modify (s++)
  
instance Monad m => ChBufferedScanner (HereStringT m) where
  mpeek1 = HereString $ \ss -> return (head ss, ss)
  mprepend s = HereString $ \ss -> return ((), s++ss)

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

instance Monad m => Monad (ScannerBufferT m) where
  return a = ScannerBuffer $ \s -> return (a,s)
  (ScannerBuffer c) >>= f = ScannerBuffer $ \s -> do (a,s') <- c s; runScannerBufferT (f a) s'

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

instance Monad m => Functor (ScannerBufferT m) where
  fmap = liftM

instance Monad m => Applicative (ScannerBufferT m) where
  (<*>) = ap
  pure = return

instance ChScanner m => ChScanner (ScannerBufferT m) where
  mscan1 = ScannerBuffer $ \(ss:sx) -> (if null ss then do s <- mscan1; return (s,[]:map (s:) sx) else return (head ss,tail ss:map (head ss:) sx))
  mscanL = ScannerBuffer $ \(ss:sx) -> do l <- mscanL; return (ss++l, []:map (++l) sx)
  mscannable = ScannerBuffer $ \(ss:sx) -> (if null ss then do b <- mscannable; return (b,[]:sx) else return (True,ss:sx))
  mscanh = return Nothing
  mready = ScannerBuffer $ \(ss:sx) -> (if null ss then do b <- mready; return (b,[]:sx) else return (True,ss:sx))

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

instance ChScanner m => ChBufferedScanner (ScannerBufferT m) where
  mpeek1 = ScannerBuffer $ \(ss:sx) -> (if null ss then do s <- mscan1; return (s,[s]:sx) else return (head ss,ss:sx))
  mprepend s = ScannerBuffer $ \(ss:sx) -> return ((),(s++ss):sx)

instance ChScanner m => ChStackBufferedScanner (ScannerBufferT m) where
  mpush = ScannerBuffer $ \(ss:sx) -> return ((),ss:[]:sx)
  mpop = ScannerBuffer $ \(_:sx) -> return ((),sx)