Text/Read.hs (original) (raw)

module Text.Read (

Read(..),
ReadS,

reads,
read,
readParen,
lex,

#if defined(GLASGOW_HASKELL) || defined(HUGS)

module Text.ParserCombinators.ReadPrec, L.Lexeme(..), lexP,
parens,
#endif #ifdef GLASGOW_HASKELL readListDefault,
readListPrecDefault, readEither,
readMaybe
#endif

) where

#ifdef GLASGOW_HASKELL import GHC.Base import GHC.Read import Data.Either import Data.Maybe import Text.ParserCombinators.ReadP as P #endif #if defined(GLASGOW_HASKELL) || defined(HUGS) import Text.ParserCombinators.ReadPrec import qualified Text.Read.Lex as L #endif

#ifdef HUGS

lexP :: ReadPrec L.Lexeme lexP = lift L.lex

parens :: ReadPrec a -> ReadPrec a parens p = optional where optional = p +++ mandatory mandatory = do L.Punc "(" <- lexP x <- reset optional L.Punc ")" <- lexP return x #endif

#ifdef GLASGOW_HASKELL

reads :: Read a => ReadS a reads = readsPrec minPrec

readEither :: Read a => String -> Either String a readEither s = case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of [x] -> Right x [] -> Left "Prelude.read: no parse" _ -> Left "Prelude.read: ambiguous parse" where read' = do x <- readPrec lift P.skipSpaces return x

readMaybe :: Read a => String -> Maybe a readMaybe s = case readEither s of Left _ -> Nothing Right a -> Just a

read :: Read a => String -> a read s = either error id (readEither s) #endif