[project @ 2001-02-22 16:10:12 by rrt]
[ghc-hetmet.git] / ghc / lib / std / PrelRead.lhs
index 6c8da89..084a22f 100644 (file)
@@ -1,5 +1,7 @@
+% ------------------------------------------------------------------------------
+% $Id: PrelRead.lhs,v 1.17 2001/02/22 13:17:59 simonpj Exp $
 %
-% (c) The AQUA Project, Glasgow University, 1994-1998
+% (c) The University of Glasgow, 1994-2000
 %
 
 \section[PrelRead]{Module @PrelRead@}
@@ -12,15 +14,14 @@ Instances of the Read class.
 module PrelRead where
 
 import PrelErr         ( error )
-import PrelEnum                ( Enum(..) )
+import PrelEnum                ( Enum(..), maxBound )
 import PrelNum
-import PrelNumExtra
+import PrelReal
+import PrelFloat
 import PrelList
-import PrelTup
 import PrelMaybe
 import PrelShow                -- isAlpha etc
 import PrelBase
-import Monad
 
 -- needed for readIO and instance Read Buffermode
 import PrelIOBase ( IO, userError, BufferMode(..) )
@@ -58,6 +59,22 @@ class  Read a  where
     readList   = readList__ reads
 \end{code}
 
+In this module we treat [(a,String)] as a monad in MonadPlus
+But MonadPlus isn't defined yet, so we simply give local
+declarations for mzero and guard suitable for this particular
+type.  It would also be reasonably to move MonadPlus to PrelBase
+along with Monad and Functor, but that seems overkill for one 
+example
+
+\begin{code}
+mzero :: [a]
+mzero = []
+
+guard :: Bool -> [()]
+guard True  = [()]
+guard False = []
+\end{code}
+
 %*********************************************************
 %*                                                     *
 \subsection{Utility functions}
@@ -174,6 +191,8 @@ lex (c:s) | isSingle c = return ([c],s)
                (nam,t) <- return (span isIdChar s)
                return (c:nam, t)
           | isDigit c  = do
+{- Removed, 13/03/2000 by SDM.
+   Doesn't work, and not required by Haskell report.
                 let
                  (pred, s', isDec) =
                    case s of
@@ -182,9 +201,9 @@ lex (c:s) | isSingle c = return ([c],s)
                      ('x':rs) -> (isHexDigit, rs, False)
                      ('X':rs) -> (isHexDigit, rs, False)
                      _        -> (isDigit, s, True)
-
-                (ds,s)  <- return (span pred s')
-                (fe,t)  <- lexFracExp isDec s
+-}
+                (ds,s)  <- return (span isDigit s)
+                (fe,t)  <- lexFracExp s
                 return (c:ds++fe,t)
           | otherwise  = mzero    -- bad character
              where
@@ -192,11 +211,11 @@ lex (c:s) | isSingle c = return ([c],s)
               isSym c    =  c `elem` "!@#$%&*+./<=>?\\^|:-~"
               isIdChar c =  isAlphaNum c || c `elem` "_'"
 
-              lexFracExp True ('.':cs)   = do
+              lexFracExp ('.':c:cs) | isDigit c = do
                        (ds,t) <- lex0Digits cs
                        (e,u)  <- lexExp t
-                       return ('.':ds++e,u)
-              lexFracExp _ s        = return ("",s)
+                       return ('.':c:ds++e,u)
+              lexFracExp s        = return ("",s)
 
               lexExp (e:s) | e `elem` "eE" = 
                  (do
@@ -253,7 +272,7 @@ lexLitChar ('\\':s)     =  do
         fromAsciiLab (x:y:ls)   | isUpper y &&
                                   [x,y]   `elem` asciiEscTab = return ([x,y], ls)
         fromAsciiLab _                                       = mzero
-                                  
+
         asciiEscTab = "DEL" : asciiTab
 
         {-
@@ -265,8 +284,7 @@ lexLitChar ('\\':s)     =  do
         -}
         checkSize base f str = do
           (num, res) <- f str
-             -- Note: this is assumes that a Char is 8 bits long.
-          if (toAnInt base num) > 255 then 
+          if toAnInteger base num > toInteger (ord maxBound) then 
              mzero
            else
              case base of
@@ -274,7 +292,7 @@ lexLitChar ('\\':s)     =  do
                 16 -> return ('x':num, res)
                 _  -> return (num, res)
 
-       toAnInt base xs = foldl (\ acc n -> acc*base + n) 0 (map digitToInt xs)
+       toAnInteger base = foldl (\ acc n -> acc*base + toInteger (digitToInt n)) 0
 
 
 lexLitChar (c:s)        =  return ([c],s)
@@ -521,7 +539,7 @@ readHex = readInt 16 isHexDigit hex
 readInt :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
 readInt radix isDig digToInt s = do
     (ds,r) <- nonnull isDig s
-    return (foldl1 (\n d -> n * radix + d) (map (fromInt . digToInt) ds), r)
+    return (foldl1 (\n d -> n * radix + d) (map (fromInteger . int2Integer . digToInt) ds), r)
 
 {-# SPECIALISE readSigned ::
                ReadS Int     -> ReadS Int,