[project @ 2004-12-14 12:44:52 by simonmar]
[ghc-base.git] / Text / Read / Lex.hs
index 2706177..dda2859 100644 (file)
@@ -7,7 +7,7 @@
 -- 
 -- Maintainer  :  libraries@haskell.org
 -- Stability   :  provisional
--- Portability :  portable
+-- Portability :  non-portable (uses Text.ParserCombinators.ReadP)
 --
 -- The cut-down Haskell lexer, used by Text.Read
 --
@@ -31,26 +31,31 @@ module Text.Read.Lex
 
 import Text.ParserCombinators.ReadP
 
+#ifdef __GLASGOW_HASKELL__
 import GHC.Base
 import GHC.Num( Num(..), Integer )
-import GHC.Show( Show(.. ), showChar, showString,
-                isSpace, isAlpha, isAlphaNum,
-                isOctDigit, isHexDigit, toUpper )
-import GHC.Real( Ratio(..), Integral, Rational, (%), fromIntegral, fromRational, 
+import GHC.Show( Show(..) )
+#ifndef __HADDOCK__
+import {-# SOURCE #-} GHC.Unicode ( isSpace, isAlpha, isAlphaNum )
+#endif
+import GHC.Real( Ratio(..), Integral, Rational, (%), fromIntegral, 
                 toInteger, (^), (^^), infinity, notANumber )
-import GHC.Float( Float, Double )
 import GHC.List
-import GHC.Show( ShowS, shows )
-import GHC.Enum( minBound, maxBound )
+import GHC.Enum( maxBound )
+#else
+import Prelude hiding ( lex )
+import Data.Char( chr, ord, isSpace, isAlpha, isAlphaNum )
+import Data.Ratio( Ratio, (%) )
+#endif
+#ifdef __HUGS__
+import Hugs.Prelude( Ratio(..) )
+#endif
 import Data.Maybe
-import Data.Either
 import Control.Monad
 
 -- -----------------------------------------------------------------------------
 -- Lexing types
 
-type LexP = ReadP Lexeme
-
 data Lexeme
   = Char   Char                -- Quotes removed, 
   | String String      --      escapes interpreted
@@ -134,6 +139,12 @@ lexId = lex_nan <++ lex_id
     isIdsChar c = isAlpha c || c == '_'
     isIdfChar c = isAlphaNum c || c `elem` "_'"
 
+#ifndef __GLASGOW_HASKELL__
+infinity, notANumber :: Rational
+infinity   = 1 :% 0
+notANumber = 0 :% 0
+#endif
+
 -- ---------------------------------------------------------------------------
 -- Lexing character literals
 
@@ -177,7 +188,7 @@ lexCharE =
          _    -> pfail
   
   lexNumeric =
-    do base <- lexBaseChar
+    do base <- lexBaseChar <++ return 10
        n    <- lexInteger base
        guard (n <= toInteger (ord maxBound))
        return (chr (fromInteger n))
@@ -294,10 +305,6 @@ lexString =
 type Base   = Int
 type Digits = [Int]
 
-showDigit :: Int -> ShowS
-showDigit n | n <= 9    = shows n
-            | otherwise = showChar (chr (n + ord 'A' - 10))
-
 lexNumber :: ReadP Lexeme
 lexNumber 
   = lexHexOct  <++     -- First try for hex or octal 0x, 0o etc
@@ -312,12 +319,9 @@ lexHexOct
        return (Int (val (fromIntegral base) 0 digits))
 
 lexBaseChar :: ReadP Int
--- Lex a single character indicating the base, 
--- or return 10 if there isn't one
-lexBaseChar = lex_base <++ return 10
-   where
-      lex_base = do { c <- get;
-                     case c of
+-- Lex a single character indicating the base; fail if not there
+lexBaseChar = do { c <- get;
+                  case c of
                        'o' -> return 8
                        'O' -> return 8
                        'x' -> return 16