module Text.Read.HT where
{-# INLINE readsInfixPrec #-}
readsInfixPrec :: (Read a, Read b) =>
String -> Int -> Int -> (a -> b -> c) -> ReadS c
readsInfixPrec :: String -> Int -> Int -> (a -> b -> c) -> ReadS c
readsInfixPrec String
opStr Int
opPrec Int
prec a -> b -> c
cons =
Bool -> ReadS c -> ReadS c
forall a. Bool -> ReadS a -> ReadS a
readParen
(Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
opPrec)
((\String
s -> [((b -> c) -> String -> b -> c
forall a b. a -> b -> a
const ((b -> c) -> String -> b -> c)
-> (a -> b -> c) -> a -> String -> b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> c
cons, String
s)]) (String -> [(a -> String -> b -> c, String)])
-> ReadS a -> ReadS (String -> b -> c)
forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
.>
Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec Int
opPrec ReadS (String -> b -> c) -> ReadS String -> ReadS (b -> c)
forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
.>
(((String, String) -> Bool)
-> [(String, String)] -> [(String, String)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((String
opStrString -> String -> Bool
forall a. Eq a => a -> a -> Bool
==)(String -> Bool)
-> ((String, String) -> String) -> (String, String) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(String, String) -> String
forall a b. (a, b) -> a
fst) ([(String, String)] -> [(String, String)])
-> ReadS String -> ReadS String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadS String
lex) ReadS (b -> c) -> ReadS b -> ReadS c
forall b c. ReadS (b -> c) -> ReadS b -> ReadS c
.>
Int -> ReadS b
forall a. Read a => Int -> ReadS a
readsPrec Int
opPrec)
infixl 9 .>
(.>) :: ReadS (b -> c) -> ReadS b -> ReadS c
.> :: ReadS (b -> c) -> ReadS b -> ReadS c
(.>) ReadS (b -> c)
ra ReadS b
rb =
((b -> c, String) -> [(c, String)])
-> [(b -> c, String)] -> [(c, String)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(b -> c
f,String
rest) -> ((b, String) -> (c, String)) -> [(b, String)] -> [(c, String)]
forall a b. (a -> b) -> [a] -> [b]
map (\(b
b, String
rest') -> (b -> c
f b
b, String
rest')) (ReadS b
rb String
rest)) ([(b -> c, String)] -> [(c, String)]) -> ReadS (b -> c) -> ReadS c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadS (b -> c)
ra
readMany :: (Read a) => String -> [a]
readMany :: String -> [a]
readMany String
x =
let contReadList :: [(a, String)] -> [a]
contReadList [] = []
contReadList ((a, String)
y:[]) = (a, String) -> a
forall a b. (a, b) -> a
fst (a, String)
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: String -> [a]
forall a. Read a => String -> [a]
readMany ((a, String) -> String
forall a b. (a, b) -> b
snd (a, String)
y)
contReadList [(a, String)]
_ = String -> [a]
forall a. HasCallStack => String -> a
error String
"readMany: ambiguous parses"
in [(a, String)] -> [a]
forall a. Read a => [(a, String)] -> [a]
contReadList (ReadS a
forall a. Read a => ReadS a
reads String
x)
maybeRead :: Read a => String -> Maybe a
maybeRead :: String -> Maybe a
maybeRead String
str =
case ReadS a
forall a. Read a => ReadS a
reads String
str of
[(a
x,String
"")] -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
[(a, String)]
_ -> Maybe a
forall a. Maybe a
Nothing