{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE DeriveFunctor #-}
module Text.ParserCombinators.ReadP
(
ReadP,
get,
look,
(+++),
(<++),
gather,
pfail,
eof,
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 GHC.Unicode ( isSpace )
import GHC.List ( replicate, null )
import GHC.Base hiding ( many )
import Control.Monad.Fail
infixr 5 +++, <++
type ReadS a = String -> [(a,String)]
data P a
= Get (Char -> P a)
| Look (String -> P a)
| Fail
| Result a (P a)
| Final [(a,String)]
deriving Functor
instance Applicative P where
pure :: a -> P a
pure x :: a
x = a -> P a -> P a
forall a. a -> P a -> P a
Result a
x P a
forall a. P a
Fail
<*> :: P (a -> b) -> P a -> P b
(<*>) = P (a -> b) -> P a -> P b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance MonadPlus P
instance Monad P where
(Get f :: Char -> P a
f) >>= :: P a -> (a -> P b) -> P b
>>= k :: a -> P b
k = (Char -> P b) -> P b
forall a. (Char -> P a) -> P a
Get (\c :: Char
c -> Char -> P a
f Char
c P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> P b
k)
(Look f :: String -> P a
f) >>= k :: a -> P b
k = (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\s :: String
s -> String -> P a
f String
s P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> P b
k)
Fail >>= _ = P b
forall a. P a
Fail
(Result x :: a
x p :: P a
p) >>= k :: a -> P b
k = a -> P b
k a
x P b -> P b -> P b
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (P a
p P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> P b
k)
(Final r :: [(a, String)]
r) >>= k :: a -> P b
k = [(b, String)] -> P b
forall a. [(a, String)] -> P a
final [(b, String)
ys' | (x :: a
x,s :: String
s) <- [(a, String)]
r, (b, String)
ys' <- P b -> ReadS b
forall a. P a -> ReadS a
run (a -> P b
k a
x) String
s]
instance MonadFail P where
fail :: String -> P a
fail _ = P a
forall a. P a
Fail
instance Alternative P where
empty :: P a
empty = P a
forall a. P a
Fail
Get f1 :: Char -> P a
f1 <|> :: P a -> P a -> P a
<|> Get f2 :: Char -> P a
f2 = (Char -> P a) -> P a
forall a. (Char -> P a) -> P a
Get (\c :: Char
c -> Char -> P a
f1 Char
c P a -> P a -> P a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Char -> P a
f2 Char
c)
Result x :: a
x p :: P a
p <|> q :: P a
q = a -> P a -> P a
forall a. a -> P a -> P a
Result a
x (P a
p P a -> P a -> P a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> P a
q)
p :: P a
p <|> Result x :: a
x q :: P a
q = a -> P a -> P a
forall a. a -> P a -> P a
Result a
x (P a
p P a -> P a -> P a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> P a
q)
Fail <|> p :: P a
p = P a
p
p :: P a
p <|> Fail = P a
p
Final r :: [(a, String)]
r <|> Final t :: [(a, String)]
t = [(a, String)] -> P a
forall a. [(a, String)] -> P a
Final ([(a, String)]
r [(a, String)] -> [(a, String)] -> [(a, String)]
forall a. [a] -> [a] -> [a]
++ [(a, String)]
t)
Final r :: [(a, String)]
r <|> Look f :: String -> P a
f = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> [(a, String)] -> P a
forall a. [(a, String)] -> P a
Final ([(a, String)]
r [(a, String)] -> [(a, String)] -> [(a, String)]
forall a. [a] -> [a] -> [a]
++ P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s))
Final r :: [(a, String)]
r <|> p :: P a
p = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> [(a, String)] -> P a
forall a. [(a, String)] -> P a
Final ([(a, String)]
r [(a, String)] -> [(a, String)] -> [(a, String)]
forall a. [a] -> [a] -> [a]
++ P a -> ReadS a
forall a. P a -> ReadS a
run P a
p String
s))
Look f :: String -> P a
f <|> Final r :: [(a, String)]
r = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> [(a, String)] -> P a
forall a. [(a, String)] -> P a
Final (P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s [(a, String)] -> [(a, String)] -> [(a, String)]
forall a. [a] -> [a] -> [a]
++ [(a, String)]
r))
p :: P a
p <|> Final r :: [(a, String)]
r = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> [(a, String)] -> P a
forall a. [(a, String)] -> P a
Final (P a -> ReadS a
forall a. P a -> ReadS a
run P a
p String
s [(a, String)] -> [(a, String)] -> [(a, String)]
forall a. [a] -> [a] -> [a]
++ [(a, String)]
r))
Look f :: String -> P a
f <|> Look g :: String -> P a
g = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> String -> P a
f String
s P a -> P a -> P a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> P a
g String
s)
Look f :: String -> P a
f <|> p :: P a
p = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> String -> P a
f String
s P a -> P a -> P a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> P a
p)
p :: P a
p <|> Look f :: String -> P a
f = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\s :: String
s -> P a
p P a -> P a -> P a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> P a
f String
s)
newtype ReadP a = R (forall b . (a -> P b) -> P b)
instance Functor ReadP where
fmap :: (a -> b) -> ReadP a -> ReadP b
fmap h :: a -> b
h (R f :: forall b. (a -> P b) -> P b
f) = (forall b. (b -> P b) -> P b) -> ReadP b
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\k :: b -> P b
k -> (a -> P b) -> P b
forall b. (a -> P b) -> P b
f (b -> P b
k (b -> P b) -> (a -> b) -> a -> P b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
h))
instance Applicative ReadP where
pure :: a -> ReadP a
pure x :: a
x = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\k :: a -> P b
k -> a -> P b
k a
x)
<*> :: ReadP (a -> b) -> ReadP a -> ReadP b
(<*>) = ReadP (a -> b) -> ReadP a -> ReadP b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad ReadP where
R m :: forall b. (a -> P b) -> P b
m >>= :: ReadP a -> (a -> ReadP b) -> ReadP b
>>= f :: a -> ReadP b
f = (forall b. (b -> P b) -> P b) -> ReadP b
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\k :: b -> P b
k -> (a -> P b) -> P b
forall b. (a -> P b) -> P b
m (\a :: a
a -> let R m' :: forall b. (b -> P b) -> P b
m' = a -> ReadP b
f a
a in (b -> P b) -> P b
forall b. (b -> P b) -> P b
m' b -> P b
k))
instance MonadFail ReadP where
fail :: String -> ReadP a
fail _ = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\_ -> P b
forall a. P a
Fail)
instance Alternative ReadP where
empty :: ReadP a
empty = ReadP a
forall a. ReadP a
pfail
<|> :: ReadP a -> ReadP a -> ReadP a
(<|>) = ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
(+++)
instance MonadPlus ReadP
final :: [(a,String)] -> P a
final :: [(a, String)] -> P a
final [] = P a
forall a. P a
Fail
final r :: [(a, String)]
r = [(a, String)] -> P a
forall a. [(a, String)] -> P a
Final [(a, String)]
r
run :: P a -> ReadS a
run :: P a -> ReadS a
run (Get f :: Char -> P a
f) (c :: Char
c:s :: String
s) = P a -> ReadS a
forall a. P a -> ReadS a
run (Char -> P a
f Char
c) String
s
run (Look f :: String -> P a
f) s :: String
s = P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s
run (Result x :: a
x p :: P a
p) s :: String
s = (a
x,String
s) (a, String) -> [(a, String)] -> [(a, String)]
forall a. a -> [a] -> [a]
: P a -> ReadS a
forall a. P a -> ReadS a
run P a
p String
s
run (Final r :: [(a, String)]
r) _ = [(a, String)]
r
run _ _ = []
get :: ReadP Char
get :: ReadP Char
get = (forall a. (Char -> P a) -> P a) -> ReadP Char
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R forall a. (Char -> P a) -> P a
Get
look :: ReadP String
look :: ReadP String
look = (forall a. (String -> P a) -> P a) -> ReadP String
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R forall a. (String -> P a) -> P a
Look
pfail :: ReadP a
pfail :: ReadP a
pfail = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\_ -> P b
forall a. P a
Fail)
(+++) :: ReadP a -> ReadP a -> ReadP a
R f1 :: forall b. (a -> P b) -> P b
f1 +++ :: ReadP a -> ReadP a -> ReadP a
+++ R f2 :: forall b. (a -> P b) -> P b
f2 = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\k :: a -> P b
k -> (a -> P b) -> P b
forall b. (a -> P b) -> P b
f1 a -> P b
k P b -> P b -> P b
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a -> P b) -> P b
forall b. (a -> P b) -> P b
f2 a -> P b
k)
(<++) :: ReadP a -> ReadP a -> ReadP a
R f0 :: forall b. (a -> P b) -> P b
f0 <++ :: ReadP a -> ReadP a -> ReadP a
<++ q :: ReadP a
q =
do String
s <- ReadP String
look
P a -> String -> Int# -> ReadP a
probe ((a -> P a) -> P a
forall b. (a -> P b) -> P b
f0 a -> P a
forall (m :: * -> *) a. Monad m => a -> m a
return) String
s 0#
where
probe :: P a -> String -> Int# -> ReadP a
probe (Get f :: Char -> P a
f) (c :: Char
c:s :: String
s) n :: Int#
n = P a -> String -> Int# -> ReadP a
probe (Char -> P a
f Char
c) String
s (Int#
nInt# -> Int# -> Int#
+#1#)
probe (Look f :: String -> P a
f) s :: String
s n :: Int#
n = P a -> String -> Int# -> ReadP a
probe (String -> P a
f String
s) String
s Int#
n
probe p :: P a
p@(Result _ _) _ n :: Int#
n = Int# -> ReadP ()
discard Int#
n ReadP () -> ReadP a -> ReadP a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (P a
p P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=)
probe (Final r :: [(a, String)]
r) _ _ = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R ([(a, String)] -> P a
forall a. [(a, String)] -> P a
Final [(a, String)]
r P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=)
probe _ _ _ = ReadP a
q
discard :: Int# -> ReadP ()
discard 0# = () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
discard n :: Int#
n = ReadP Char
get ReadP Char -> ReadP () -> ReadP ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int# -> ReadP ()
discard (Int#
nInt# -> Int# -> Int#
-#1#)
gather :: ReadP a -> ReadP (String, a)
gather :: ReadP a -> ReadP (String, a)
gather (R m :: forall b. (a -> P b) -> P b
m)
= (forall b. ((String, a) -> P b) -> P b) -> ReadP (String, a)
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\k :: (String, a) -> P b
k -> (String -> String) -> P (String -> P b) -> P b
forall b. (String -> String) -> P (String -> P b) -> P b
gath String -> String
forall a. a -> a
id ((a -> P (String -> P b)) -> P (String -> P b)
forall b. (a -> P b) -> P b
m (\a :: a
a -> (String -> P b) -> P (String -> P b)
forall (m :: * -> *) a. Monad m => a -> m a
return (\s :: String
s -> (String, a) -> P b
k (String
s,a
a)))))
where
gath :: (String -> String) -> P (String -> P b) -> P b
gath :: (String -> String) -> P (String -> P b) -> P b
gath l :: String -> String
l (Get f :: Char -> P (String -> P b)
f) = (Char -> P b) -> P b
forall a. (Char -> P a) -> P a
Get (\c :: Char
c -> (String -> String) -> P (String -> P b) -> P b
forall b. (String -> String) -> P (String -> P b) -> P b
gath (String -> String
l(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Char
cChar -> String -> String
forall a. a -> [a] -> [a]
:)) (Char -> P (String -> P b)
f Char
c))
gath _ Fail = P b
forall a. P a
Fail
gath l :: String -> String
l (Look f :: String -> P (String -> P b)
f) = (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\s :: String
s -> (String -> String) -> P (String -> P b) -> P b
forall b. (String -> String) -> P (String -> P b) -> P b
gath String -> String
l (String -> P (String -> P b)
f String
s))
gath l :: String -> String
l (Result k :: String -> P b
k p :: P (String -> P b)
p) = String -> P b
k (String -> String
l []) P b -> P b -> P b
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (String -> String) -> P (String -> P b) -> P b
forall b. (String -> String) -> P (String -> P b) -> P b
gath String -> String
l P (String -> P b)
p
gath _ (Final _) = String -> P b
forall a. String -> a
errorWithoutStackTrace "do not use readS_to_P in gather!"
satisfy :: (Char -> Bool) -> ReadP Char
satisfy :: (Char -> Bool) -> ReadP Char
satisfy p :: Char -> Bool
p = do Char
c <- ReadP Char
get; if Char -> Bool
p Char
c then Char -> ReadP Char
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c else ReadP Char
forall a. ReadP a
pfail
char :: Char -> ReadP Char
char :: Char -> ReadP Char
char c :: Char
c = (Char -> Bool) -> ReadP Char
satisfy (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==)
eof :: ReadP ()
eof :: ReadP ()
eof = do { String
s <- ReadP String
look
; if String -> Bool
forall a. [a] -> Bool
null String
s then () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else ReadP ()
forall a. ReadP a
pfail }
string :: String -> ReadP String
string :: String -> ReadP String
string this :: String
this = do String
s <- ReadP String
look; String -> String -> ReadP String
forall a. Eq a => [a] -> [a] -> ReadP String
scan String
this String
s
where
scan :: [a] -> [a] -> ReadP String
scan [] _ = do String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return String
this
scan (x :: a
x:xs :: [a]
xs) (y :: a
y:ys :: [a]
ys) | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = do Char
_ <- ReadP Char
get; [a] -> [a] -> ReadP String
scan [a]
xs [a]
ys
scan _ _ = do ReadP String
forall a. ReadP a
pfail
munch :: (Char -> Bool) -> ReadP String
munch :: (Char -> Bool) -> ReadP String
munch p :: Char -> Bool
p =
do String
s <- ReadP String
look
String -> ReadP String
scan String
s
where
scan :: String -> ReadP String
scan (c :: Char
c:cs :: String
cs) | Char -> Bool
p Char
c = do Char
_ <- ReadP Char
get; String
s <- String -> ReadP String
scan String
cs; String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
cChar -> String -> String
forall a. a -> [a] -> [a]
:String
s)
scan _ = do String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return ""
munch1 :: (Char -> Bool) -> ReadP String
munch1 :: (Char -> Bool) -> ReadP String
munch1 p :: Char -> Bool
p =
do Char
c <- ReadP Char
get
if Char -> Bool
p Char
c then do String
s <- (Char -> Bool) -> ReadP String
munch Char -> Bool
p; String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
cChar -> String -> String
forall a. a -> [a] -> [a]
:String
s)
else ReadP String
forall a. ReadP a
pfail
choice :: [ReadP a] -> ReadP a
choice :: [ReadP a] -> ReadP a
choice [] = ReadP a
forall a. ReadP a
pfail
choice [p :: ReadP a
p] = ReadP a
p
choice (p :: ReadP a
p:ps :: [ReadP a]
ps) = ReadP a
p ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
+++ [ReadP a] -> ReadP a
forall a. [ReadP a] -> ReadP a
choice [ReadP a]
ps
skipSpaces :: ReadP ()
skipSpaces :: ReadP ()
skipSpaces =
do String
s <- ReadP String
look
String -> ReadP ()
skip String
s
where
skip :: String -> ReadP ()
skip (c :: Char
c:s :: String
s) | Char -> Bool
isSpace Char
c = do Char
_ <- ReadP Char
get; String -> ReadP ()
skip String
s
skip _ = do () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
count :: Int -> ReadP a -> ReadP [a]
count :: Int -> ReadP a -> ReadP [a]
count n :: Int
n p :: ReadP a
p = [ReadP a] -> ReadP [a]
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence (Int -> ReadP a -> [ReadP a]
forall a. Int -> a -> [a]
replicate Int
n ReadP a
p)
between :: ReadP open -> ReadP close -> ReadP a -> ReadP a
between :: ReadP open -> ReadP close -> ReadP a -> ReadP a
between open :: ReadP open
open close :: ReadP close
close p :: ReadP a
p = do open
_ <- ReadP open
open
a
x <- ReadP a
p
close
_ <- ReadP close
close
a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
option :: a -> ReadP a -> ReadP a
option :: a -> ReadP a -> ReadP a
option x :: a
x p :: ReadP a
p = ReadP a
p ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
+++ a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
optional :: ReadP a -> ReadP ()
optional :: ReadP a -> ReadP ()
optional p :: ReadP a
p = (ReadP a
p ReadP a -> ReadP () -> ReadP ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) ReadP () -> ReadP () -> ReadP ()
forall a. ReadP a -> ReadP a -> ReadP a
+++ () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
many :: ReadP a -> ReadP [a]
many :: ReadP a -> ReadP [a]
many p :: ReadP a
p = [a] -> ReadP [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [] ReadP [a] -> ReadP [a] -> ReadP [a]
forall a. ReadP a -> ReadP a -> ReadP a
+++ ReadP a -> ReadP [a]
forall a. ReadP a -> ReadP [a]
many1 ReadP a
p
many1 :: ReadP a -> ReadP [a]
many1 :: ReadP a -> ReadP [a]
many1 p :: ReadP a
p = (a -> [a] -> [a]) -> ReadP a -> ReadP [a] -> ReadP [a]
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (:) ReadP a
p (ReadP a -> ReadP [a]
forall a. ReadP a -> ReadP [a]
many ReadP a
p)
skipMany :: ReadP a -> ReadP ()
skipMany :: ReadP a -> ReadP ()
skipMany p :: ReadP a
p = ReadP a -> ReadP [a]
forall a. ReadP a -> ReadP [a]
many ReadP a
p ReadP [a] -> ReadP () -> ReadP ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
skipMany1 :: ReadP a -> ReadP ()
skipMany1 :: ReadP a -> ReadP ()
skipMany1 p :: ReadP a
p = ReadP a
p ReadP a -> ReadP () -> ReadP ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ReadP a -> ReadP ()
forall a. ReadP a -> ReadP ()
skipMany ReadP a
p
sepBy :: ReadP a -> ReadP sep -> ReadP [a]
sepBy :: ReadP a -> ReadP sep -> ReadP [a]
sepBy p :: ReadP a
p sep :: ReadP sep
sep = ReadP a -> ReadP sep -> ReadP [a]
forall a sep. ReadP a -> ReadP sep -> ReadP [a]
sepBy1 ReadP a
p ReadP sep
sep ReadP [a] -> ReadP [a] -> ReadP [a]
forall a. ReadP a -> ReadP a -> ReadP a
+++ [a] -> ReadP [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
sepBy1 :: ReadP a -> ReadP sep -> ReadP [a]
sepBy1 :: ReadP a -> ReadP sep -> ReadP [a]
sepBy1 p :: ReadP a
p sep :: ReadP sep
sep = (a -> [a] -> [a]) -> ReadP a -> ReadP [a] -> ReadP [a]
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (:) ReadP a
p (ReadP a -> ReadP [a]
forall a. ReadP a -> ReadP [a]
many (ReadP sep
sep ReadP sep -> ReadP a -> ReadP a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ReadP a
p))
endBy :: ReadP a -> ReadP sep -> ReadP [a]
endBy :: ReadP a -> ReadP sep -> ReadP [a]
endBy p :: ReadP a
p sep :: ReadP sep
sep = ReadP a -> ReadP [a]
forall a. ReadP a -> ReadP [a]
many (do a
x <- ReadP a
p ; sep
_ <- ReadP sep
sep ; a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)
endBy1 :: ReadP a -> ReadP sep -> ReadP [a]
endBy1 :: ReadP a -> ReadP sep -> ReadP [a]
endBy1 p :: ReadP a
p sep :: ReadP sep
sep = ReadP a -> ReadP [a]
forall a. ReadP a -> ReadP [a]
many1 (do a
x <- ReadP a
p ; sep
_ <- ReadP sep
sep ; a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)
chainr :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
chainr :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
chainr p :: ReadP a
p op :: ReadP (a -> a -> a)
op x :: a
x = ReadP a -> ReadP (a -> a -> a) -> ReadP a
forall a. ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainr1 ReadP a
p ReadP (a -> a -> a)
op ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
+++ a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
chainl :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
chainl :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
chainl p :: ReadP a
p op :: ReadP (a -> a -> a)
op x :: a
x = ReadP a -> ReadP (a -> a -> a) -> ReadP a
forall a. ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainl1 ReadP a
p ReadP (a -> a -> a)
op ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
+++ a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
chainr1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainr1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainr1 p :: ReadP a
p op :: ReadP (a -> a -> a)
op = ReadP a
scan
where scan :: ReadP a
scan = ReadP a
p ReadP a -> (a -> ReadP a) -> ReadP a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ReadP a
rest
rest :: a -> ReadP a
rest x :: a
x = do a -> a -> a
f <- ReadP (a -> a -> a)
op
a
y <- ReadP a
scan
a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y)
ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
+++ a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
chainl1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainl1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainl1 p :: ReadP a
p op :: ReadP (a -> a -> a)
op = ReadP a
p ReadP a -> (a -> ReadP a) -> ReadP a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ReadP a
rest
where rest :: a -> ReadP a
rest x :: a
x = do a -> a -> a
f <- ReadP (a -> a -> a)
op
a
y <- ReadP a
p
a -> ReadP a
rest (a -> a -> a
f a
x a
y)
ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
+++ a -> ReadP a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
manyTill :: ReadP a -> ReadP end -> ReadP [a]
manyTill :: ReadP a -> ReadP end -> ReadP [a]
manyTill p :: ReadP a
p end :: ReadP end
end = ReadP [a]
scan
where scan :: ReadP [a]
scan = (ReadP end
end ReadP end -> ReadP [a] -> ReadP [a]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [a] -> ReadP [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []) ReadP [a] -> ReadP [a] -> ReadP [a]
forall a. ReadP a -> ReadP a -> ReadP a
<++ ((a -> [a] -> [a]) -> ReadP a -> ReadP [a] -> ReadP [a]
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (:) ReadP a
p ReadP [a]
scan)
readP_to_S :: ReadP a -> ReadS a
readP_to_S :: ReadP a -> ReadS a
readP_to_S (R f :: forall b. (a -> P b) -> P b
f) = P a -> ReadS a
forall a. P a -> ReadS a
run ((a -> P a) -> P a
forall b. (a -> P b) -> P b
f a -> P a
forall (m :: * -> *) a. Monad m => a -> m a
return)
readS_to_P :: ReadS a -> ReadP a
readS_to_P :: ReadS a -> ReadP a
readS_to_P r :: ReadS a
r =
(forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\k :: a -> P b
k -> (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\s :: String
s -> [(b, String)] -> P b
forall a. [(a, String)] -> P a
final [(b, String)
bs'' | (a :: a
a,s' :: String
s') <- ReadS a
r String
s, (b, String)
bs'' <- P b -> ReadS b
forall a. P a -> ReadS a
run (a -> P b
k a
a) String
s']))