{-# 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 (NonEmpty (a,String))
deriving (forall a b. (a -> b) -> P a -> P b)
-> (forall a b. a -> P b -> P a) -> Functor P
forall a b. a -> P b -> P a
forall a b. (a -> b) -> P a -> P b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> P b -> P a
$c<$ :: forall a b. a -> P b -> P a
fmap :: forall a b. (a -> b) -> P a -> P b
$cfmap :: forall a b. (a -> b) -> P a -> P b
Functor
instance Applicative P where
pure :: forall a. a -> P a
pure a
x = a -> P a -> P a
forall a. a -> P a -> P a
Result a
x P a
forall a. P a
Fail
<*> :: forall a b. 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 Char -> P a
f) >>= :: forall a b. P a -> (a -> P b) -> P b
>>= a -> P b
k = (Char -> P b) -> P b
forall a. (Char -> P a) -> P a
Get (\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 String -> P a
f) >>= a -> P b
k = (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\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)
P a
Fail >>= a -> P b
_ = P b
forall a. P a
Fail
(Result a
x P a
p) >>= 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 ((a, String)
r:|[(a, String)]
rs)) >>= a -> P b
k = [(b, String)] -> P b
forall a. [(a, String)] -> P a
final [(b, String)
ys' | (a
x,String
s) <- ((a, String)
r(a, String) -> [(a, String)] -> [(a, String)]
forall a. a -> [a] -> [a]
:[(a, String)]
rs), (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 :: forall a. String -> P a
fail String
_ = P a
forall a. P a
Fail
instance Alternative P where
empty :: forall a. P a
empty = P a
forall a. P a
Fail
Get Char -> P a
f1 <|> :: forall a. P a -> P a -> P a
<|> Get Char -> P a
f2 = (Char -> P a) -> P a
forall a. (Char -> P a) -> P a
Get (\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 a
x P a
p <|> 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 a
p <|> Result a
x 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 a
Fail <|> P a
p = P a
p
P a
p <|> P a
Fail = P a
p
Final NonEmpty (a, String)
r <|> Final NonEmpty (a, String)
t = NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final (NonEmpty (a, String)
r NonEmpty (a, String)
-> NonEmpty (a, String) -> NonEmpty (a, String)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, String)
t)
Final ((a, String)
r:|[(a, String)]
rs) <|> Look String -> P a
f = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final ((a, String)
r(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|([(a, String)]
rs [(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 ((a, String)
r:|[(a, String)]
rs) <|> P a
p = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final ((a, String)
r(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|([(a, String)]
rs [(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 String -> P a
f <|> Final NonEmpty (a, String)
r = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final (case P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s of
[] -> NonEmpty (a, String)
r
((a, String)
x:[(a, String)]
xs) -> ((a, String)
x(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|[(a, String)]
xs) NonEmpty (a, String)
-> NonEmpty (a, String) -> NonEmpty (a, String)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, String)
r))
P a
p <|> Final NonEmpty (a, String)
r = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final (case P a -> ReadS a
forall a. P a -> ReadS a
run P a
p String
s of
[] -> NonEmpty (a, String)
r
((a, String)
x:[(a, String)]
xs) -> ((a, String)
x(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|[(a, String)]
xs) NonEmpty (a, String)
-> NonEmpty (a, String) -> NonEmpty (a, String)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, String)
r))
Look String -> P a
f <|> Look String -> P a
g = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\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 String -> P a
f <|> P a
p = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\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 a
p <|> Look String -> P a
f = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\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 :: forall a b. (a -> b) -> ReadP a -> ReadP b
fmap a -> b
h (R 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 (\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 :: forall a. a -> ReadP a
pure a
x = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\a -> P b
k -> a -> P b
k a
x)
<*> :: forall a b. 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 forall b. (a -> P b) -> P b
m >>= :: forall a b. ReadP a -> (a -> ReadP b) -> ReadP b
>>= 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 (\b -> P b
k -> (a -> P b) -> P b
forall b. (a -> P b) -> P b
m (\a
a -> let R 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 :: forall a. String -> ReadP a
fail String
_ = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\a -> P b
_ -> P b
forall a. P a
Fail)
instance Alternative ReadP where
empty :: forall a. ReadP a
empty = ReadP a
forall a. ReadP a
pfail
<|> :: forall a. 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 :: forall a. [(a, String)] -> P a
final [] = P a
forall a. P a
Fail
final ((a, String)
r:[(a, String)]
rs) = NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final ((a, String)
r(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|[(a, String)]
rs)
run :: P a -> ReadS a
run :: forall a. P a -> ReadS a
run (Get Char -> P a
f) (Char
c:String
s) = P a -> ReadS a
forall a. P a -> ReadS a
run (Char -> P a
f Char
c) String
s
run (Look String -> P a
f) String
s = P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s
run (Result a
x P a
p) 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 ((a, String)
r:|[(a, String)]
rs)) String
_ = ((a, String)
r(a, String) -> [(a, String)] -> [(a, String)]
forall a. a -> [a] -> [a]
:[(a, String)]
rs)
run P a
_ String
_ = []
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 :: forall a. ReadP a
pfail = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\a -> P b
_ -> P b
forall a. P a
Fail)
(+++) :: ReadP a -> ReadP a -> ReadP a
R forall b. (a -> P b) -> P b
f1 +++ :: forall a. ReadP a -> ReadP a -> ReadP a
+++ R 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 (\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 forall b. (a -> P b) -> P b
f0 <++ :: forall a. ReadP a -> ReadP a -> ReadP a
<++ 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 Int#
0#
where
probe :: P a -> String -> Int# -> ReadP a
probe (Get Char -> P a
f) (Char
c:String
s) Int#
n = P a -> String -> Int# -> ReadP a
probe (Char -> P a
f Char
c) String
s (Int#
nInt# -> Int# -> Int#
+#Int#
1#)
probe (Look String -> P a
f) String
s 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 a
_ P a
_) String
_ 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 NonEmpty (a, String)
r) String
_ Int#
_ = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final NonEmpty (a, String)
r P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=)
probe P a
_ String
_ Int#
_ = ReadP a
q
discard :: Int# -> ReadP ()
discard Int#
0# = () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
discard 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#
-#Int#
1#)
gather :: ReadP a -> ReadP (String, a)
gather :: forall a. ReadP a -> ReadP (String, a)
gather (R 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 (\(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 -> (String -> P b) -> P (String -> P b)
forall (m :: * -> *) a. Monad m => a -> m a
return (\String
s -> (String, a) -> P b
k (String
s,a
a)))))
where
gath :: (String -> String) -> P (String -> P b) -> P b
gath :: forall b. (String -> String) -> P (String -> P b) -> P b
gath String -> String
l (Get Char -> P (String -> P b)
f) = (Char -> P b) -> P b
forall a. (Char -> P a) -> P a
Get (\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 String -> String
_ P (String -> P b)
Fail = P b
forall a. P a
Fail
gath String -> String
l (Look String -> P (String -> P b)
f) = (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\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 String -> String
l (Result String -> P b
k 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 String -> String
_ (Final NonEmpty (String -> P b, String)
_) = String -> P b
forall a. String -> a
errorWithoutStackTrace String
"do not use readS_to_P in gather!"
satisfy :: (Char -> Bool) -> ReadP Char
satisfy :: (Char -> Bool) -> ReadP Char
satisfy 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 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 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 [] [a]
_ = String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return String
this
scan (a
x:[a]
xs) (a
y:[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 [a]
_ [a]
_ = ReadP String
forall a. ReadP a
pfail
munch :: (Char -> Bool) -> ReadP String
munch :: (Char -> Bool) -> ReadP String
munch Char -> Bool
p =
do String
s <- ReadP String
look
String -> ReadP String
scan String
s
where
scan :: String -> ReadP String
scan (Char
c: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 String
_ = String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return String
""
munch1 :: (Char -> Bool) -> ReadP String
munch1 :: (Char -> Bool) -> ReadP String
munch1 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 :: forall a. [ReadP a] -> ReadP a
choice [] = ReadP a
forall a. ReadP a
pfail
choice [ReadP a
p] = ReadP a
p
choice (ReadP a
p:[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 (Char
c:String
s) | Char -> Bool
isSpace Char
c = do Char
_ <- ReadP Char
get; String -> ReadP ()
skip String
s
skip String
_ = () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
count :: Int -> ReadP a -> ReadP [a]
count :: forall a. Int -> ReadP a -> ReadP [a]
count Int
n 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 :: forall open close a.
ReadP open -> ReadP close -> ReadP a -> ReadP a
between ReadP open
open ReadP close
close 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 :: forall a. a -> ReadP a -> ReadP a
option a
x 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 :: forall a. ReadP a -> ReadP ()
optional 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 :: forall a. ReadP a -> ReadP [a]
many 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 :: forall a. ReadP a -> ReadP [a]
many1 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 :: forall a. ReadP a -> ReadP ()
skipMany 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 :: forall a. ReadP a -> ReadP ()
skipMany1 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 :: forall a sep. ReadP a -> ReadP sep -> ReadP [a]
sepBy ReadP a
p 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 :: forall a sep. ReadP a -> ReadP sep -> ReadP [a]
sepBy1 ReadP a
p 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 :: forall a sep. ReadP a -> ReadP sep -> ReadP [a]
endBy ReadP a
p 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 :: forall a sep. ReadP a -> ReadP sep -> ReadP [a]
endBy1 ReadP a
p 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 :: forall a. ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
chainr ReadP a
p ReadP (a -> a -> a)
op 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 :: forall a. ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
chainl ReadP a
p ReadP (a -> a -> a)
op 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 :: forall a. ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainr1 ReadP a
p 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 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 :: forall a. ReadP a -> ReadP (a -> a -> a) -> ReadP a
chainl1 ReadP a
p 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 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 :: forall a sep. ReadP a -> ReadP sep -> ReadP [a]
manyTill ReadP a
p 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 :: forall a. ReadP a -> ReadS a
readP_to_S (R 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 :: forall a. ReadS a -> ReadP a
readS_to_P ReadS a
r =
(forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\a -> P b
k -> (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\String
s -> [(b, String)] -> P b
forall a. [(a, String)] -> P a
final [(b, String)
bs'' | (a
a,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']))