module Text.ParserCombinators.Poly.Text
(
Parser(P)
, Result(..)
, runParser
, next
, eof
, satisfy
, onFail
, manySatisfy
, many1Satisfy
, reparse
, 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
newtype Parser a = P (Text -> Result Text a)
runParser :: Parser a -> Text -> (Either String a, Text)
runParser (P p) = resultToEither . p
instance Functor Parser where
fmap f (P p) = P (fmap f . p)
instance Monad Parser where
return x = P (\ts-> Success ts x)
fail e = P (\ts-> Failure ts e)
(P f) >>= g = P (continue . f)
where
continue (Success ts x) = let (P g') = g x in g' ts
continue (Committed (Committed r)) = continue (Committed r)
continue (Committed r) = Committed (continue r)
continue (Failure ts e) = Failure ts e
instance Commitment Parser where
commit (P p) = P (Committed . p)
(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 _ _) -> r
r@(Committed _) -> r )
showErr (name,err) = name++":\n"++indent 2 err
instance Applicative Parser where
pure f = return f
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 where
empty = fail "no parse"
p <|> q = p `onFail` q
instance PolyParse Parser
next :: Parser Char
next = P (\bs-> case T.uncons bs of
Nothing -> Failure bs "Ran out of input (EOF)"
Just (c, bs') -> Success bs' c )
eof :: Parser ()
eof = P (\bs -> if T.null bs
then Success bs ()
else Failure bs "Expected end of input (EOF)" )
satisfy :: (Char -> Bool) -> Parser Char
satisfy f = do { x <- next
; if f x then return x else fail "Parse.satisfy: failed"
}
onFail :: Parser a -> Parser a -> Parser a
(P p) `onFail` (P q) = P (\ts-> continue ts $ p ts)
where continue ts (Failure _ _) = q ts
continue _ r = r
manySatisfy :: (Char->Bool) -> Parser Text
manySatisfy f = P (\bs-> let (pre,suf) = T.span f bs in Success suf pre)
many1Satisfy :: (Char->Bool) -> Parser Text
many1Satisfy f = do x <- manySatisfy f
if T.null x then fail "Parse.many1Satisfy: failed"
else return x
reparse :: Text -> Parser ()
reparse ts = P (\inp-> Success (ts `T.append` inp) ())