module Text.ParserCombinators.Poly.Lex
  ( 
    LexReturn(..)
  , Parser(P)
  , Result(..)
  , runParser
    
  , next
  , eof
  , satisfy
  , onFail
    
  , reparse
    
  , module Text.ParserCombinators.Poly.Base
  , module Control.Applicative
  ) where
import Text.ParserCombinators.Poly.Base
import Text.ParserCombinators.Poly.Result
import Control.Applicative
import qualified Control.Monad.Fail as Fail
data LexReturn t = LexReturn t String (String->LexReturn t)
                 | LexFinish
newtype Parser t a = P (LexReturn t -> Result (LexReturn t) a)
runParser :: Parser t a -> LexReturn t -> (Either String a, String)
runParser (P p) = (\ (a,b)->(a,stripLex b)) . resultToEither . p
  where stripLex  LexFinish        = ""
        stripLex (LexReturn _ s _) = s
instance Functor (Parser t) where
    fmap f (P p) = P (fmap f . p)
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 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 . unlines . map 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 _ _)    -> r
                           r@(Committed _)    -> r )
            showErr (name,err) = name ++ "\n" ++ indent 2 err
infixl 6 `onFail`       
(P p) `onFail` (P q) = P (\ts-> continue ts $ p ts)
      where
        continue ts (Failure _ _) = q ts
    
        continue _  r             = r
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 Alternative (Parser t) where
    empty     = fail "no parse"
    p <|> q   = p `onFail` q
instance PolyParse (Parser t)
next :: Parser t t
next = P (\ts-> case ts of
                  LexFinish       -> Failure ts "Ran out of input (EOF)"
                  LexReturn t s k -> Success (k s) t)
eof :: Parser t ()
eof = P (\ts -> case ts of
                  LexFinish       -> Success ts ()
                  LexReturn _ _ _ -> Failure ts "Expected end of input (EOF)" )
satisfy :: (t -> Bool) -> Parser t t
satisfy f = do { x <- next
               ; if f x then return x else fail "Parse.satisfy: failed"
               }
reparse    :: [t] -> Parser t ()
reparse ts  = P (\inp-> Success (ts `prefix` inp) ())
  where
    (t:ts) `prefix` k = LexReturn t "" (const (ts `prefix` k))
    []     `prefix` k = k