module Text.Read.HT where

{-| Parse a string containing an infix operator. -}
{-# INLINE readsInfixPrec #-}
readsInfixPrec :: (Read a, Read b) =>
   String -> Int -> Int -> (a -> b -> c) -> ReadS c
readsInfixPrec :: forall a b c.
(Read a, Read b) =>
String -> Int -> Int -> (a -> b -> c) -> ReadS c
readsInfixPrec String
opStr Int
opPrec Int
prec a -> b -> c
cons =
   forall a. Bool -> ReadS a -> ReadS a
readParen
     (Int
prec forall a. Ord a => a -> a -> Bool
>= Int
opPrec)
     ((\String
s -> [(forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> c
cons, String
s)]) forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
.>
      forall a. Read a => Int -> ReadS a
readsPrec Int
opPrec forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
.>
      (forall a. (a -> Bool) -> [a] -> [a]
filter ((String
opStrforall a. Eq a => a -> a -> Bool
==)forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [(String, String)]
lex) forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
.>
      forall a. Read a => Int -> ReadS a
readsPrec Int
opPrec)

{-| Compose two parsers sequentially. -}
infixl 9 .>
(.>) :: ReadS (b -> c) -> ReadS b -> ReadS c
.> :: forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
(.>) ReadS (b -> c)
ra ReadS b
rb =
   forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(b -> c
f,String
rest) -> forall a b. (a -> b) -> [a] -> [b]
map (\(b
b, String
rest') -> (b -> c
f b
b, String
rest')) (ReadS b
rb String
rest)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadS (b -> c)
ra


readMany :: (Read a) => String -> [a]
readMany :: forall a. Read a => String -> [a]
readMany String
x =
   let contReadList :: [(a, String)] -> [a]
contReadList []     = []
       contReadList ((a, String)
y:[]) = forall a b. (a, b) -> a
fst (a, String)
y forall a. a -> [a] -> [a]
: forall a. Read a => String -> [a]
readMany (forall a b. (a, b) -> b
snd (a, String)
y)
       contReadList [(a, String)]
_      = forall a. HasCallStack => String -> a
error String
"readMany: ambiguous parses"
   in  forall {a}. Read a => [(a, String)] -> [a]
contReadList (forall a. Read a => ReadS a
reads String
x)

maybeRead :: Read a => String -> Maybe a
maybeRead :: forall a. Read a => String -> Maybe a
maybeRead String
str =
   case forall a. Read a => ReadS a
reads String
str of
      [(a
x,String
"")] -> forall a. a -> Maybe a
Just a
x
      [(a, String)]
_ -> forall a. Maybe a
Nothing