module Text.ParserCombinators.Poly.Parser
  ( 
    Parser(P)   
  , Result(..)  
    
  , next        
  , eof         
  , satisfy     
  , satisfyMsg  
  , onFail      
    
  , reparse     
  ) where
import Text.ParserCombinators.Poly.Base
import Text.ParserCombinators.Poly.Result
import Control.Applicative
import qualified Control.Monad.Fail as Fail
newtype Parser t a = P ([t] -> Result [t] a)
instance Functor (Parser t) where
    fmap f (P p) = P (fmap f . p)
instance Applicative (Parser t) where
    pure x    = P (\ts-> Success ts x)
    pf <*> px = do { f <- pf; x <- px; return (f x) }
#if defined(GLASGOW_HASKELL) && GLASGOW_HASKELL > 610
    p  <*  q  = p `discard` q
#endif
instance Monad (Parser t) where
    return       = pure
    fail         = Fail.fail
    (P f) >>= g  = P (continue . f)
      where
        continue (Success ts x)             = let (P g') = g x in g' ts
        continue (Committed r)              = Committed (continue r)
        continue (Failure ts e)             = Failure ts e
instance Fail.MonadFail (Parser t) where
    fail e       = P (\ts-> Failure ts e)
instance Alternative (Parser t) where
    empty     = fail "no parse"
    p <|> q   = p `onFail` q
instance PolyParse (Parser t)
instance Commitment (Parser t) where
    commit (P p)         = P (Committed . squash . p)
      where
        squash (Committed r) = squash r
        squash r             = r
    (P p) `adjustErr` f  = P (adjust . p)
      where
        adjust (Failure z e) = Failure z (f e)
        adjust (Committed r) = Committed (adjust r)
        adjust  good         = good
    oneOf' = accum []
      where accum errs [] =
                fail ("failed to parse any of the possible choices:\n"
                            ++indent 2 (concatMap showErr (reverse errs)))
            accum errs ((e,P p):ps) =
                P (\ts-> case p ts of
                           Failure _ err ->
                                       let (P p) = accum ((e,err):errs) ps
                                       in p ts
                           r@(Success z a)    -> r
                           r@(Committed _)    -> r )
            showErr (name,err) = name++":\n"++indent 2 err
infixl 6 `onFail`       
onFail :: Parser t a -> Parser t a -> Parser t a
(P p) `onFail` (P q) = P (\ts-> continue ts $ p ts)
  where
    continue ts (Failure z e) = q ts
    continue _  r             = r
next :: Parser t t
next = P (\ts-> case ts of
                  []      -> Failure [] "Ran out of input (EOF)"
                  (t:ts') -> Success ts' t )
eof  :: Parser t ()
eof  = P (\ts-> case ts of
                  []      -> Success [] ()
                  (t:ts') -> Failure ts "Expected end of input (EOF)" )
satisfy :: (t->Bool) -> Parser t t
satisfy pred = do { x <- next
                  ; if pred x then return x else fail "Parse.satisfy: failed"
                  }
satisfyMsg :: Show t => (t->Bool) -> String -> Parser t t
satisfyMsg pred s
             = do { x <- next
                  ; if pred x then return x
                              else fail $ "Parse.satisfy ("++s++") ("
                                                           ++show x++"): failed"
                  }
reparse    :: [t] -> Parser t ()
reparse ts  = P (\inp-> Success (ts++inp) ())