module Text.ParserCombinators.Poly.Text
  ( -- * The Parser datatype
    Parser(P)
  , Result(..)
  , runParser
    -- ** Basic parsers
  , next
  , eof
  , satisfy
  , onFail
    -- ** Derived parsers (but implemented more efficiently)
  , manySatisfy
  , many1Satisfy
    -- ** Re-parsing
  , reparse
    -- * Re-export all more general combinators
  , module Text.ParserCombinators.Poly.Base
  , module Control.Applicative
  ) where


import Text.ParserCombinators.Poly.Base
import Text.ParserCombinators.Poly.Result
import qualified Data.Text.Lazy as T
import Data.Text.Lazy (Text)
import Control.Applicative
import qualified Control.Monad.Fail as Fail

-- | This @Parser@ datatype is a specialised parsing monad with error
--   reporting.  Whereas the standard version can be used for arbitrary
--   token types, this version is specialised to Text input only.
newtype Parser a = P (Text -> Result Text a)

-- | Apply a parser to an input token sequence.
runParser :: Parser a -> Text -> (Either String a, Text)
runParser :: forall a. Parser a -> Text -> (Either String a, Text)
runParser (P Text -> Result Text a
p) = forall z a. Result z a -> (Either String a, z)
resultToEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Result Text a
p

instance Functor Parser where
    fmap :: forall a b. (a -> b) -> Parser a -> Parser b
fmap a -> b
f (P Text -> Result Text a
p) = forall a. (Text -> Result Text a) -> Parser a
P (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Result Text a
p)

instance Monad Parser where
    return :: forall a. a -> Parser a
return       = forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (P Text -> Result Text a
f) >>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
>>= a -> Parser b
g  = forall a. (Text -> Result Text a) -> Parser a
P (Result Text a -> Result Text b
continue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Result Text a
f)
      where
        continue :: Result Text a -> Result Text b
continue (Success Text
ts a
x)             = let (P Text -> Result Text b
g') = a -> Parser b
g a
x in Text -> Result Text b
g' Text
ts
        continue (Committed Result Text a
r)              = forall z a. Result z a -> Result z a
Committed (Result Text a -> Result Text b
continue Result Text a
r)
        continue (Failure Text
ts String
e)             = forall z a. z -> String -> Result z a
Failure Text
ts String
e

#if !MIN_VERSION_base(4,13,0)
    fail         = Fail.fail
#endif

instance Fail.MonadFail Parser where
    fail :: forall a. String -> Parser a
fail String
e       = forall a. (Text -> Result Text a) -> Parser a
P (\Text
ts-> forall z a. z -> String -> Result z a
Failure Text
ts String
e)

instance Commitment Parser where
    commit :: forall a. Parser a -> Parser a
commit (P Text -> Result Text a
p)         = forall a. (Text -> Result Text a) -> Parser a
P (forall z a. Result z a -> Result z a
Committed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall z a. Result z a -> Result z a
squash forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Result Text a
p)
      where
        squash :: Result z a -> Result z a
squash (Committed Result z a
r) = Result z a -> Result z a
squash Result z a
r
        squash Result z a
r             = Result z a
r
    (P Text -> Result Text a
p) adjustErr :: forall a. Parser a -> (String -> String) -> Parser a
`adjustErr` String -> String
f  = forall a. (Text -> Result Text a) -> Parser a
P (forall z a. Result z a -> Result z a
adjust forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Result Text a
p)
      where
        adjust :: Result z a -> Result z a
adjust (Failure z
z String
e) = forall z a. z -> String -> Result z a
Failure z
z (String -> String
f String
e)
        adjust (Committed Result z a
r) = forall z a. Result z a -> Result z a
Committed (Result z a -> Result z a
adjust Result z a
r)
        adjust  Result z a
good         = Result z a
good

    oneOf' :: forall a. [(String, Parser a)] -> Parser a
oneOf' = forall {a}. [(String, String)] -> [(String, Parser a)] -> Parser a
accum []
      where accum :: [(String, String)] -> [(String, Parser a)] -> Parser a
accum [(String, String)]
errs [] =
                forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"failed to parse any of the possible choices:\n"
                            forall a. [a] -> [a] -> [a]
++Int -> String -> String
indent Int
2 (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (String, String) -> String
showErr (forall a. [a] -> [a]
reverse [(String, String)]
errs)))
            accum [(String, String)]
errs ((String
e,P Text -> Result Text a
p):[(String, Parser a)]
ps) =
                forall a. (Text -> Result Text a) -> Parser a
P (\Text
ts-> case Text -> Result Text a
p Text
ts of
                           Failure Text
_ String
err ->
                                       let (P Text -> Result Text a
p') = [(String, String)] -> [(String, Parser a)] -> Parser a
accum ((String
e,String
err)forall a. a -> [a] -> [a]
:[(String, String)]
errs) [(String, Parser a)]
ps
                                       in Text -> Result Text a
p' Text
ts
                           r :: Result Text a
r@(Success Text
_ a
_)    -> Result Text a
r
                           r :: Result Text a
r@(Committed Result Text a
_)    -> Result Text a
r )
            showErr :: (String, String) -> String
showErr (String
name,String
err) = String
nameforall a. [a] -> [a] -> [a]
++String
":\n"forall a. [a] -> [a] -> [a]
++Int -> String -> String
indent Int
2 String
err

instance Applicative Parser where
    pure :: forall a. a -> Parser a
pure a
x    = forall a. (Text -> Result Text a) -> Parser a
P (\Text
ts-> forall z a. z -> a -> Result z a
Success Text
ts a
x)
    Parser (a -> b)
pf <*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser a
px = do { a -> b
f <- Parser (a -> b)
pf; a
x <- Parser a
px; forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
x) }
#if defined(GLASGOW_HASKELL) && GLASGOW_HASKELL > 610
    p  <*  q  = p `discard` q
#endif

instance Alternative Parser where
    empty :: forall a. Parser a
empty     = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no parse"
    Parser a
p <|> :: forall a. Parser a -> Parser a -> Parser a
<|> Parser a
q   = Parser a
p forall a. Parser a -> Parser a -> Parser a
`onFail` Parser a
q

instance PolyParse Parser

------------------------------------------------------------------------

-- | Simply return the next token in the input tokenstream.
next :: Parser Char
next :: Parser Char
next = forall a. (Text -> Result Text a) -> Parser a
P (\Text
bs-> case Text -> Maybe (Char, Text)
T.uncons Text
bs of
                Maybe (Char, Text)
Nothing       -> forall z a. z -> String -> Result z a
Failure Text
bs String
"Ran out of input (EOF)"
                Just (Char
c, Text
bs') -> forall z a. z -> a -> Result z a
Success Text
bs' Char
c )

-- | Succeed if the end of file/input has been reached, fail otherwise.
eof :: Parser ()
eof :: Parser ()
eof = forall a. (Text -> Result Text a) -> Parser a
P (\Text
bs -> if Text -> Bool
T.null Text
bs
                then forall z a. z -> a -> Result z a
Success Text
bs ()
                else forall z a. z -> String -> Result z a
Failure Text
bs String
"Expected end of input (EOF)" )

-- | Return the next token if it satisfies the given predicate.
satisfy :: (Char -> Bool) -> Parser Char
satisfy :: (Char -> Bool) -> Parser Char
satisfy Char -> Bool
f = do { Char
x <- Parser Char
next
               ; if Char -> Bool
f Char
x then forall (m :: * -> *) a. Monad m => a -> m a
return Char
x else forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Parse.satisfy: failed"
               }

-- | @p `onFail` q@ means parse p, unless p fails, in which case
--   parse q instead.
--   Can be chained together to give multiple attempts to parse something.
--   (Note that q could itself be a failing parser, e.g. to change the error
--   message from that defined in p to something different.)
--   However, a severe failure in p cannot be ignored.
onFail :: Parser a -> Parser a -> Parser a
(P Text -> Result Text a
p) onFail :: forall a. Parser a -> Parser a -> Parser a
`onFail` (P Text -> Result Text a
q) = forall a. (Text -> Result Text a) -> Parser a
P (\Text
ts-> Text -> Result Text a -> Result Text a
continue Text
ts forall a b. (a -> b) -> a -> b
$ Text -> Result Text a
p Text
ts)
  where continue :: Text -> Result Text a -> Result Text a
continue Text
ts (Failure Text
_ String
_) = Text -> Result Text a
q Text
ts
    --  continue _  (Committed r) = r	-- no, remain Committed
        continue Text
_  Result Text a
r             = Result Text a
r

------------------------------------------------------------------------

-- | @manySatisfy p@ is a more efficient fused version of @many (satisfy p)@
manySatisfy :: (Char->Bool) -> Parser Text
manySatisfy :: (Char -> Bool) -> Parser Text
manySatisfy Char -> Bool
f = forall a. (Text -> Result Text a) -> Parser a
P (\Text
bs-> let (Text
pre,Text
suf) = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
f Text
bs in forall z a. z -> a -> Result z a
Success Text
suf Text
pre)

-- | @many1Satisfy p@ is a more efficient fused version of @many1 (satisfy p)@
many1Satisfy :: (Char->Bool) -> Parser Text
many1Satisfy :: (Char -> Bool) -> Parser Text
many1Satisfy Char -> Bool
f = do Text
x <- (Char -> Bool) -> Parser Text
manySatisfy Char -> Bool
f
                    if Text -> Bool
T.null Text
x then forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Parse.many1Satisfy: failed"
                                else forall (m :: * -> *) a. Monad m => a -> m a
return Text
x

------------------------------------------------------------------------

-- | Push some tokens back onto the front of the input stream and reparse.
--   This is useful e.g. for recursively expanding macros.  When the
--   user-parser recognises a macro use, it can lookup the macro
--   expansion from the parse state, lex it, and then stuff the
--   lexed expansion back down into the parser.
reparse    :: Text -> Parser ()
reparse :: Text -> Parser ()
reparse Text
ts  = forall a. (Text -> Result Text a) -> Parser a
P (\Text
inp-> forall z a. z -> a -> Result z a
Success (Text
ts Text -> Text -> Text
`T.append` Text
inp) ())

------------------------------------------------------------------------