1module Text.Read.HT where
2
3{-| Parse a string containing an infix operator. -}
4{-# INLINE readsInfixPrec #-}
5readsInfixPrec :: (Read a, Read b) =>
6   String -> Int -> Int -> (a -> b -> c) -> ReadS c
7readsInfixPrec opStr opPrec prec cons =
8   readParen
9     (prec >= opPrec)
10     ((\s -> [(const . cons, s)]) .>
11      readsPrec opPrec .>
12      (filter ((opStr==).fst) . lex) .>
13      readsPrec opPrec)
14
15{-| Compose two parsers sequentially. -}
16infixl 9 .>
17(.>) :: ReadS (b -> c) -> ReadS b -> ReadS c
18(.>) ra rb =
19   concatMap (\(f,rest) -> map (\(b, rest') -> (f b, rest')) (rb rest)) . ra
20
21
22readMany :: (Read a) => String -> [a]
23readMany x =
24   let contReadList []     = []
25       contReadList (y:[]) = fst y : readMany (snd y)
26       contReadList _      = error "readMany: ambiguous parses"
27   in  contReadList (reads x)
28
29maybeRead :: Read a => String -> Maybe a
30maybeRead str =
31   case reads str of
32      [(x,"")] -> Just x
33      _ -> Nothing
34