diff options
author | Ian Lynagh <igloo@earth.li> | 2008-08-06 12:05:04 +0000 |
---|---|---|
committer | Ian Lynagh <igloo@earth.li> | 2008-08-06 12:05:04 +0000 |
commit | bc3938e68058b971df2554a11a41d8a81a6f939e (patch) | |
tree | 61f2a896b775609c7c18a05d23d6a3c9acc1c657 /libraries/base/Text | |
parent | 13fb5c0c68f66beb7e1f256849e006e7d2a56b34 (diff) | |
download | haskell-bc3938e68058b971df2554a11a41d8a81a6f939e.tar.gz |
Move some bits around to stop Data.Either being in the base import knot
Diffstat (limited to 'libraries/base/Text')
-rw-r--r-- | libraries/base/Text/Read.hs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/libraries/base/Text/Read.hs b/libraries/base/Text/Read.hs index 172a4c2ce3..5ab8877efc 100644 --- a/libraries/base/Text/Read.hs +++ b/libraries/base/Text/Read.hs @@ -45,7 +45,10 @@ module Text.Read ( ) where #ifdef __GLASGOW_HASKELL__ +import GHC.Base import GHC.Read +import Data.Either +import Text.ParserCombinators.ReadP as P #endif #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__) import Text.ParserCombinators.ReadPrec @@ -68,3 +71,30 @@ parens p = optional L.Punc ")" <- lexP return x #endif + +#ifdef __GLASGOW_HASKELL__ +------------------------------------------------------------------------ +-- utility functions + +-- | equivalent to 'readsPrec' with a precedence of 0. +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 + +-- | The 'read' function reads input from a string, which must be +-- completely consumed by the input process. +read :: Read a => String -> a +read s = either error id (readEither s) +#endif + |