X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Text%2FRead.hs;h=5ab8877efc080a44a955175fb1f678e5f79a149d;hb=7dfe4a22aa6a2c598b1496c661c7d532aaafa94f;hp=5009efbfb8aeb35ad5a530c7c6043ec584c1eff0;hpb=df5b214b2e4f8c0d337371c16ebf5a86f97e7d6b;p=ghc-base.git diff --git a/Text/Read.hs b/Text/Read.hs index 5009efb..5ab8877 100644 --- a/Text/Read.hs +++ b/Text/Read.hs @@ -1,4 +1,4 @@ -{-# OPTIONS_GHC -fno-implicit-prelude #-} +{-# OPTIONS_GHC -XNoImplicitPrelude #-} ----------------------------------------------------------------------------- -- | -- Module : Text.Read @@ -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 +