module Distribution.Compat.ReadP
(
ReadP,
get,
look,
(+++),
(<++),
gather,
pfail,
satisfy,
char,
string,
munch,
munch1,
skipSpaces,
choice,
count,
between,
option,
optional,
many,
many1,
skipMany,
skipMany1,
sepBy,
sepBy1,
endBy,
endBy1,
chainr,
chainl,
chainl1,
chainr1,
manyTill,
ReadS,
readP_to_S,
readS_to_P
)
where
import Control.Monad( MonadPlus(..), liftM2 )
import Data.Char (isSpace)
infixr 5 +++, <++
data P s a
= Get (s -> P s a)
| Look ([s] -> P s a)
| Fail
| Result a (P s a)
| Final [(a,[s])]
instance D:Monad ::
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> (forall a. String -> m a)
-> T:Monad mMonad (P s) where
return x = Result :: a -> P s a -> P s aResult x :: ax Fail :: P s aFail
(Get f) >>= k = Get :: (s -> P s a) -> P s aGet (\c -> f :: [s] -> P s af c :: sc (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= k :: a -> P s bk)
(Look f) >>= k = Look :: ([s] -> P s a) -> P s aLook (\s -> f :: [s] -> P s af s :: [s]s (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= k :: a -> P s bk)
Fail >>= _ = Fail :: P s aFail
(Result x p) >>= k = k :: a -> P s bk x :: ax mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` (p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= k :: a -> P s bk)
(Final r) >>= k = final :: [(a, [s])] -> P s afinal [ys' :: (b, [s])ys' | (x,s) <- r :: [(a, [s])]r, ys' <- run :: P c a -> [c] -> [(a, [c])]run (k :: a -> P s bk x :: ax) s :: [s]s]
fail _ = Fail :: P s aFail
instance D:MonadPlus ::
Monad m =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> T:MonadPlus mMonadPlus (P s) where
mzero = Fail :: P s aFail
Get f1 `mplus` Get f2 = Get :: (s -> P s a) -> P s aGet (\c -> f1 :: s -> P s af1 c :: sc mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` f2 :: s -> P s af2 c :: sc)
Result x p `mplus` q = Result :: a -> P s a -> P s aResult x :: ax (p :: P s ap mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` q :: P s aq)
p `mplus` Result x q = Result :: a -> P s a -> P s aResult x :: ax (p :: P s ap mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` q :: P s aq)
Fail `mplus` p = p :: P s ap
p `mplus` Fail = p :: P s ap
Final r `mplus` Final t = Final :: [(a, [s])] -> P s aFinal (r :: [(a, [s])]r (++) :: [a] -> [a] -> [a]++ t :: [(a, [s])]t)
Final r `mplus` Look f = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (r :: [(a, [s])]r (++) :: [a] -> [a] -> [a]++ run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af s :: [s]s) s :: [s]s))
Final r `mplus` p = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (r :: [(a, [s])]r (++) :: [a] -> [a] -> [a]++ run :: P c a -> [c] -> [(a, [c])]run p :: P s ap s :: [s]s))
Look f `mplus` Final r = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af s :: [s]s) s :: [s]s (++) :: [a] -> [a] -> [a]++ r :: [(a, [s])]r))
p `mplus` Final r = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (run :: P c a -> [c] -> [(a, [c])]run p :: P s ap s :: [s]s (++) :: [a] -> [a] -> [a]++ r :: [(a, [s])]r))
Look f `mplus` Look g = Look :: ([s] -> P s a) -> P s aLook (\s -> f :: [s] -> P s af s :: [s]s mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` g :: [s] -> P s ag s :: [s]s)
Look f `mplus` p = Look :: ([s] -> P s a) -> P s aLook (\s -> f :: [s] -> P s af s :: [s]s mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` p :: P s ap)
p `mplus` Look f = Look :: ([s] -> P s a) -> P s aLook (\s -> p :: P s ap mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` f :: [s] -> P s af s :: [s]s)
newtype Parser r s a = R ((a -> P s r) -> P s r)
type ReadP r a = Parser r Char a
instance D:Functor ::
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a)
-> T:Functor fFunctor (Parser r s) where
fmap h (R f) = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> f :: [s] -> P s af (k :: a -> P s bk (.) :: (b -> c) -> (a -> b) -> a -> c. h :: a -> bh))
instance D:Monad ::
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> (forall a. String -> m a)
-> T:Monad mMonad (Parser r s) where
return x = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> k :: a -> P s bk x :: ax)
fail _ = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\_ -> Fail :: P s aFail)
R m >>= f = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> m :: (a -> P s r) -> P s rm (\a -> let R m' = f :: [s] -> P s af a :: aa in m' :: (b -> P s r) -> P s rm' k :: a -> P s bk))
final :: [(a,[s])] -> P s a
final [] = Fail :: P s aFail
final r = Final :: [(a, [s])] -> P s aFinal r :: [(a, [s])]r
run :: P c a -> ([c] -> [(a, [c])])
run (Get f) (c:s) = run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af c :: sc) s :: [s]s
run (Look f) s = run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af s :: [s]s) s :: [s]s
run (Result x p) s = (x :: ax,s :: [s]s) (:) :: a -> [a] -> [a]: run :: P c a -> [c] -> [(a, [c])]run p :: P s ap s :: [s]s
run (Final r) _ = r :: [(a, [s])]r
run _ _ = [] :: [a][]
get :: ReadP r Char
get = R :: ((a -> P s r) -> P s r) -> Parser r s aR Get :: (s -> P s a) -> P s aGet
look :: ReadP r String
look = R :: ((a -> P s r) -> P s r) -> Parser r s aR Look :: ([s] -> P s a) -> P s aLook
pfail :: ReadP r a
pfail = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\_ -> Fail :: P s aFail)
(+++) :: ReadP r a -> ReadP r a -> ReadP r a
R f1 +++ R f2 = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> f1 :: s -> P s af1 k :: a -> P s bk mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` f2 :: s -> P s af2 k :: a -> P s bk)
(<++) :: ReadP a a -> ReadP r a -> ReadP r a
R f <++ q =
do s <- look :: ReadP r Stringlook
probe :: P Char a -> [Char] -> Int -> Parser r Char aprobe (f :: [s] -> P s af return :: Monad m => forall a. a -> m areturn) s :: [s]s 0
where
probe (Get f') (c:s) n = probe :: P Char a -> [Char] -> Int -> Parser r Char aprobe (f' :: [Char] -> P Char af' c :: sc) s :: [s]s (n :: Intn(+) :: Num a => a -> a -> a+1 :: Int)
probe (Look f') s n = probe :: P Char a -> [Char] -> Int -> Parser r Char aprobe (f' :: [Char] -> P Char af' s :: [s]s) s :: [s]s n :: Intn
probe p@(Result _ _) _ n = discard :: Int -> Parser r Char ()discard n :: Intn (>>) :: Monad m => forall a b. m a -> m b -> m b>> R :: ((a -> P s r) -> P s r) -> Parser r s aR (p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>=)
probe (Final r) _ _ = R :: ((a -> P s r) -> P s r) -> Parser r s aR (Final :: [(a, [s])] -> P s aFinal r :: [(a, [s])]r (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>=)
probe _ _ _ = q :: P s aq
discard 0 = return :: Monad m => forall a. a -> m areturn ()
discard n = get :: ReadP r Charget (>>) :: Monad m => forall a b. m a -> m b -> m b>> discard :: Int -> Parser r Char ()discard (n :: Intn(-) :: Num a => a -> a -> a1 :: Int)
gather :: ReadP (String -> P Char r) a -> ReadP r (String, a)
gather (R m) =
R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath id :: a -> aid (m :: (a -> P s r) -> P s rm (\a -> return :: Monad m => forall a. a -> m areturn (\s -> k :: a -> P s bk (s :: [s]s,a :: aa)))))
where
gath l (Get f) = Get :: (s -> P s a) -> P s aGet (\c -> gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath (l :: [t] -> cl(.) :: (b -> c) -> (a -> b) -> a -> c.(c :: sc(:) :: a -> [a] -> [a]:)) (f :: [s] -> P s af c :: sc))
gath _ Fail = Fail :: P s aFail
gath l (Look f) = Look :: ([s] -> P s a) -> P s aLook (\s -> gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath l :: [t] -> cl (f :: [s] -> P s af s :: [s]s))
gath l (Result k p) = k :: a -> P s bk (l :: [t] -> cl [] :: [a][]) mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath l :: [t] -> cl p :: P s ap
gath _ (Final _) = error :: [Char] -> aerror "do not use readS_to_P in gather!"
satisfy :: (Char -> Bool) -> ReadP r Char
satisfy p = do c <- get :: ReadP r Charget; if p :: P s ap c :: sc then return :: Monad m => forall a. a -> m areturn c :: sc else pfail :: ReadP r apfail
char :: Char -> ReadP r Char
char c = satisfy :: (Char -> Bool) -> ReadP r Charsatisfy (c :: sc (==) :: Eq a => a -> a -> Bool==)
string :: String -> ReadP r String
string this = do s <- look :: ReadP r Stringlook; scan :: [Char] -> Parser r Char [Char]scan this :: Stringthis s :: [s]s
where
scan [] _ = do return :: Monad m => forall a. a -> m areturn this :: Stringthis
scan (x:xs) (y:ys) | x :: ax (==) :: Eq a => a -> a -> Bool== y :: ay = do get :: ReadP r Charget (>>) :: Monad m => forall a b. m a -> m b -> m b>> scan :: [Char] -> Parser r Char [Char]scan xs :: [a]xs ys :: [a]ys
scan _ _ = do pfail :: ReadP r apfail
munch :: (Char -> Bool) -> ReadP r String
munch p =
do s <- look :: ReadP r Stringlook
scan :: [Char] -> Parser r Char [Char]scan s :: [s]s
where
scan (c:cs) | p :: P s ap c :: sc = do _ <- get :: ReadP r Charget; s <- scan :: [Char] -> Parser r Char [Char]scan cs :: [Char]cs; return :: Monad m => forall a. a -> m areturn (c :: sc(:) :: a -> [a] -> [a]:s :: [s]s)
scan _ = do return :: Monad m => forall a. a -> m areturn ""
munch1 :: (Char -> Bool) -> ReadP r String
munch1 p =
do c <- get :: ReadP r Charget
if p :: P s ap c :: sc then do s <- munch :: (Char -> Bool) -> ReadP r Stringmunch p :: P s ap; return :: Monad m => forall a. a -> m areturn (c :: sc(:) :: a -> [a] -> [a]:s :: [s]s)
else pfail :: ReadP r apfail
choice :: [ReadP r a] -> ReadP r a
choice [] = pfail :: ReadP r apfail
choice [p] = p :: P s ap
choice (p:ps) = p :: P s ap (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ choice :: [ReadP r a] -> ReadP r achoice ps :: [ReadP r a]ps
skipSpaces :: ReadP r ()
skipSpaces =
do s <- look :: ReadP r Stringlook
skip :: [Char] -> Parser r Char ()skip s :: [s]s
where
skip (c:s) | isSpace :: Char -> BoolisSpace c :: sc = do _ <- get :: ReadP r Charget; skip :: [Char] -> Parser r Char ()skip s :: [s]s
skip _ = do return :: Monad m => forall a. a -> m areturn ()
count :: Int -> ReadP r a -> ReadP r [a]
count n p = sequence :: Monad m => [m a] -> m [a]sequence (replicate :: Int -> a -> [a]replicate n :: Intn p :: P s ap)
between :: ReadP r open -> ReadP r close -> ReadP r a -> ReadP r a
between open close p = do _ <- open :: ReadP r openopen
x <- p :: P s ap
_ <- close :: ReadP r closeclose
return :: Monad m => forall a. a -> m areturn x :: ax
option :: a -> ReadP r a -> ReadP r a
option x p = p :: P s ap (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax
optional :: ReadP r a -> ReadP r ()
optional p = (p :: P s ap (>>) :: Monad m => forall a b. m a -> m b -> m b>> return :: Monad m => forall a. a -> m areturn ()) (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn ()
many :: ReadP r a -> ReadP r [a]
many p = return :: Monad m => forall a. a -> m areturn [] :: [a][] (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ many1 :: ReadP r a -> ReadP r [a]many1 p :: P s ap
many1 :: ReadP r a -> ReadP r [a]
many1 p = liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2 (:) :: a -> [a] -> [a](:) p :: P s ap (many :: ReadP r a -> ReadP r [a]many p :: P s ap)
skipMany :: ReadP r a -> ReadP r ()
skipMany p = many :: ReadP r a -> ReadP r [a]many p :: P s ap (>>) :: Monad m => forall a b. m a -> m b -> m b>> return :: Monad m => forall a. a -> m areturn ()
skipMany1 :: ReadP r a -> ReadP r ()
skipMany1 p = p :: P s ap (>>) :: Monad m => forall a b. m a -> m b -> m b>> skipMany :: ReadP r a -> ReadP r ()skipMany p :: P s ap
sepBy :: ReadP r a -> ReadP r sep -> ReadP r [a]
sepBy p sep = sepBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]sepBy1 p :: P s ap sep :: ReadP r sepsep (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn [] :: [a][]
sepBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]
sepBy1 p sep = liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2 (:) :: a -> [a] -> [a](:) p :: P s ap (many :: ReadP r a -> ReadP r [a]many (sep :: ReadP r sepsep (>>) :: Monad m => forall a b. m a -> m b -> m b>> p :: P s ap))
endBy :: ReadP r a -> ReadP r sep -> ReadP r [a]
endBy p sep = many :: ReadP r a -> ReadP r [a]many (do x <- p :: P s ap ; _ <- sep :: ReadP r sepsep ; return :: Monad m => forall a. a -> m areturn x :: ax)
endBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]
endBy1 p sep = many1 :: ReadP r a -> ReadP r [a]many1 (do x <- p :: P s ap ; _ <- sep :: ReadP r sepsep ; return :: Monad m => forall a. a -> m areturn x :: ax)
chainr :: ReadP r a -> ReadP r (a -> a -> a) -> a -> ReadP r a
chainr p op x = chainr1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r achainr1 p :: P s ap op :: ReadP r (a -> a -> a)op (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax
chainl :: ReadP r a -> ReadP r (a -> a -> a) -> a -> ReadP r a
chainl p op x = chainl1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r achainl1 p :: P s ap op :: ReadP r (a -> a -> a)op (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax
chainr1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r a
chainr1 p op = scan :: [Char] -> Parser r Char [Char]scan
where scan = p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= rest :: a -> ReadP r arest
rest x = do f <- op :: ReadP r (a -> a -> a)op
y <- scan :: [Char] -> Parser r Char [Char]scan
return :: Monad m => forall a. a -> m areturn (f :: [s] -> P s af x :: ax y :: ay)
(+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax
chainl1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r a
chainl1 p op = p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= rest :: a -> ReadP r arest
where rest x = do f <- op :: ReadP r (a -> a -> a)op
y <- p :: P s ap
rest :: a -> ReadP r arest (f :: [s] -> P s af x :: ax y :: ay)
(+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax
manyTill :: ReadP r a -> ReadP [a] end -> ReadP r [a]
manyTill p end = scan :: [Char] -> Parser r Char [Char]scan
where scan = (end :: ReadP [a] endend (>>) :: Monad m => forall a b. m a -> m b -> m b>> return :: Monad m => forall a. a -> m areturn [] :: [a][]) (<++) :: ReadP a a -> ReadP r a -> ReadP r a<++ (liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2 (:) :: a -> [a] -> [a](:) p :: P s ap scan :: [Char] -> Parser r Char [Char]scan)
readP_to_S :: ReadP a a -> ReadS a
readP_to_S (R f) = run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af return :: Monad m => forall a. a -> m areturn)
readS_to_P :: ReadS a -> ReadP r a
readS_to_P r =
R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> Look :: ([s] -> P s a) -> P s aLook (\s -> final :: [(a, [s])] -> P s afinal [bs'' :: (r, [Char])bs'' | (a,s') <- r :: [(a, [s])]r s :: [s]s, bs'' <- run :: P c a -> [c] -> [(a, [c])]run (k :: a -> P s bk a :: aa) s' :: Strings']))