{-# LANGUAGE 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 nondeterministic 'MonadParser' instance.
module Text.Chatty.Parser.Carrier where

import Control.Applicative
import Control.Monad
import Data.List
import Data.Monoid
import qualified Data.Foldable as F
import Text.Chatty.Parser
import Text.Chatty.Scanner

data CarrierT m a = Carry { carry :: String -> m (a,String) }

instance (MonadPlus m,F.Foldable m) => Monad (CarrierT m) where
  return a = Carry $ \s -> return (a,s)
  m >>= f = Carry $ \s -> msum [carry (f a) cs' | (a,cs') <- F.foldr (:) [] $ carry m s]
  fail _ = Carry $ const mzero

instance (MonadPlus m,F.Foldable m) => Functor (CarrierT m) where
  fmap = liftM

instance (MonadPlus m,F.Foldable m) => Applicative (CarrierT m) where
  pure = return
  f <*> a = f `ap` a

instance (MonadPlus m,F.Foldable m) => Alternative (CarrierT m) where
  empty = pabort
  a <|> b = a ??? b

instance (MonadPlus m,F.Foldable m) => MonadPlus (CarrierT m) where
  mzero = empty
  a `mplus` b = a <|> b

instance (MonadPlus m,F.Foldable m) => Monoid (CarrierT m a) where
  mempty = empty
  a `mappend` b = a <|> b

instance (MonadPlus m,F.Foldable m) => ChScanner (CarrierT m) where
  mscan1 = Carry $ \cx -> case cx of
                               c:cs -> return (c,cs)
                               [] -> mzero
  mscanL = Carry $ \cs -> return (cs,[])
  mscannable = Carry $ \cs -> return (not (null cs), cs)
  mready = mscannable

instance (MonadPlus m,F.Foldable m) => ChParser (CarrierT m) where
  pabort = Carry $ \s -> mzero
  p ??? q = Carry $ \cs -> carry p cs `mplus` carry q cs
  p ?? q = Carry $ \cs -> msum $ liftM return $ nub $ F.foldr (:) [] (carry p cs `mplus` carry q cs)
  ptry p = Carry $ \cs -> msum $ liftM return
                          $ map (\((a,s):as) -> (msum $ liftM return (a:map fst as), s))
                          $ groupBy (\a b -> snd a == snd b)
                          $ sortBy (\a b -> snd a `compare` snd b)
                          $ F.foldr (:) []
                          $ carry p cs

-- | Run a 'CarrierT' with its full input string.
runCarrierT :: (MonadPlus m,F.Foldable m) => String -> CarrierT m a -> m a
runCarrierT s c = liftM fst $ mfilter (\x -> null (snd x)) $ carry c s

-- | Embed the 'CarrierT' into a running 'MonadScanner' instance.
embedCarrierT :: (MonadPlus n,F.Foldable n,ChScanner m) => CarrierT n a -> m (n a)
embedCarrierT f = do
  s <- mscanL
  return $ runCarrierT s f