{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Text.ParserCombinators.ReadPrec
(
ReadPrec,
Prec,
minPrec,
lift,
prec,
step,
reset,
get,
look,
(+++),
(<++),
pfail,
choice,
readPrec_to_P,
readP_to_Prec,
readPrec_to_S,
readS_to_Prec,
)
where
import Text.ParserCombinators.ReadP
( ReadP
, ReadS
, readP_to_S
, readS_to_P
)
import qualified Text.ParserCombinators.ReadP as ReadP
( get
, look
, (+++), (<++)
, pfail
)
import GHC.Num( Num(..) )
import GHC.Base
import Control.Monad.Fail
newtype ReadPrec a = P (Prec -> ReadP a)
instance Functor ReadPrec where
fmap :: forall a b. (a -> b) -> ReadPrec a -> ReadPrec b
fmap a -> b
h (P Prec -> ReadP a
f) = (Prec -> ReadP b) -> ReadPrec b
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> (a -> b) -> ReadP a -> ReadP b
forall a b. (a -> b) -> ReadP a -> ReadP b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
h (Prec -> ReadP a
f Prec
n))
instance Applicative ReadPrec where
pure :: forall a. a -> ReadPrec a
pure a
x = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> a -> ReadP a
forall a. a -> ReadP a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
<*> :: forall a b. ReadPrec (a -> b) -> ReadPrec a -> ReadPrec b
(<*>) = ReadPrec (a -> b) -> ReadPrec a -> ReadPrec b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
liftA2 :: forall a b c.
(a -> b -> c) -> ReadPrec a -> ReadPrec b -> ReadPrec c
liftA2 = (a -> b -> c) -> ReadPrec a -> ReadPrec b -> ReadPrec c
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2
instance Monad ReadPrec where
P Prec -> ReadP a
f >>= :: forall a b. ReadPrec a -> (a -> ReadPrec b) -> ReadPrec b
>>= a -> ReadPrec b
k = (Prec -> ReadP b) -> ReadPrec b
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> do a
a <- Prec -> ReadP a
f Prec
n; let P Prec -> ReadP b
f' = a -> ReadPrec b
k a
a in Prec -> ReadP b
f' Prec
n)
instance MonadFail ReadPrec where
fail :: forall a. String -> ReadPrec a
fail String
s = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> String -> ReadP a
forall a. String -> ReadP a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
s)
instance MonadPlus ReadPrec
instance Alternative ReadPrec where
empty :: forall a. ReadPrec a
empty = ReadPrec a
forall a. ReadPrec a
pfail
<|> :: forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
(<|>) = ReadPrec a -> ReadPrec a -> ReadPrec a
forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
(+++)
type Prec = Int
minPrec :: Prec
minPrec :: Prec
minPrec = Prec
0
lift :: ReadP a -> ReadPrec a
lift :: forall a. ReadP a -> ReadPrec a
lift ReadP a
m = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> ReadP a
m)
step :: ReadPrec a -> ReadPrec a
step :: forall a. ReadPrec a -> ReadPrec a
step (P Prec -> ReadP a
f) = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> Prec -> ReadP a
f (Prec
nPrec -> Prec -> Prec
forall a. Num a => a -> a -> a
+Prec
1))
reset :: ReadPrec a -> ReadPrec a
reset :: forall a. ReadPrec a -> ReadPrec a
reset (P Prec -> ReadP a
f) = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> Prec -> ReadP a
f Prec
minPrec)
prec :: Prec -> ReadPrec a -> ReadPrec a
prec :: forall a. Prec -> ReadPrec a -> ReadPrec a
prec Prec
n (P Prec -> ReadP a
f) = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
c -> if Prec
c Prec -> Prec -> Bool
forall a. Ord a => a -> a -> Bool
<= Prec
n then Prec -> ReadP a
f Prec
n else ReadP a
forall a. ReadP a
ReadP.pfail)
get :: ReadPrec Char
get :: ReadPrec Char
get = ReadP Char -> ReadPrec Char
forall a. ReadP a -> ReadPrec a
lift ReadP Char
ReadP.get
look :: ReadPrec String
look :: ReadPrec String
look = ReadP String -> ReadPrec String
forall a. ReadP a -> ReadPrec a
lift ReadP String
ReadP.look
(+++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
P Prec -> ReadP a
f1 +++ :: forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
+++ P Prec -> ReadP a
f2 = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> Prec -> ReadP a
f1 Prec
n ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ Prec -> ReadP a
f2 Prec
n)
(<++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
P Prec -> ReadP a
f1 <++ :: forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
<++ P Prec -> ReadP a
f2 = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> Prec -> ReadP a
f1 Prec
n ReadP a -> ReadP a -> ReadP a
forall a. ReadP a -> ReadP a -> ReadP a
ReadP.<++ Prec -> ReadP a
f2 Prec
n)
pfail :: ReadPrec a
pfail :: forall a. ReadPrec a
pfail = ReadP a -> ReadPrec a
forall a. ReadP a -> ReadPrec a
lift ReadP a
forall a. ReadP a
ReadP.pfail
choice :: [ReadPrec a] -> ReadPrec a
choice :: forall a. [ReadPrec a] -> ReadPrec a
choice [ReadPrec a]
ps = (ReadPrec a -> ReadPrec a -> ReadPrec a)
-> ReadPrec a -> [ReadPrec a] -> ReadPrec a
forall a b. (a -> b -> b) -> b -> [a] -> b
foldr ReadPrec a -> ReadPrec a -> ReadPrec a
forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
(+++) ReadPrec a
forall a. ReadPrec a
pfail [ReadPrec a]
ps
readPrec_to_P :: ReadPrec a -> (Int -> ReadP a)
readPrec_to_P :: forall a. ReadPrec a -> Prec -> ReadP a
readPrec_to_P (P Prec -> ReadP a
f) = Prec -> ReadP a
f
readP_to_Prec :: (Int -> ReadP a) -> ReadPrec a
readP_to_Prec :: forall a. (Prec -> ReadP a) -> ReadPrec a
readP_to_Prec Prec -> ReadP a
f = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P Prec -> ReadP a
f
readPrec_to_S :: ReadPrec a -> (Int -> ReadS a)
readPrec_to_S :: forall a. ReadPrec a -> Prec -> ReadS a
readPrec_to_S (P Prec -> ReadP a
f) Prec
n = ReadP a -> ReadS a
forall a. ReadP a -> ReadS a
readP_to_S (Prec -> ReadP a
f Prec
n)
readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
readS_to_Prec :: forall a. (Prec -> ReadS a) -> ReadPrec a
readS_to_Prec Prec -> ReadS a
f = (Prec -> ReadP a) -> ReadPrec a
forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> ReadS a -> ReadP a
forall a. ReadS a -> ReadP a
readS_to_P (Prec -> ReadS a
f Prec
n))