[project @ 2005-02-07 09:56:42 by ross]
[haskell-directory.git] / Text / Read / Lex.hs
index dd26cb1..740e27f 100644 (file)
@@ -1,4 +1,4 @@
-{-# OPTIONS -fno-implicit-prelude #-}
+{-# OPTIONS_GHC -fno-implicit-prelude #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  Text.Read.Lex
@@ -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,27 +31,40 @@ module Text.Read.Lex
 
 import Text.ParserCombinators.ReadP
 
+#ifdef __GLASGOW_HASKELL__
 import GHC.Base
 import GHC.Num( Num(..), Integer )
-import GHC.Show( Show(.. ), isSpace, isAlpha, isAlphaNum )
+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.List
 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 Control.Monad
 
 -- -----------------------------------------------------------------------------
 -- Lexing types
 
+-- ^ Haskell lexemes.
 data Lexeme
-  = Char   Char                -- Quotes removed, 
-  | String String      --      escapes interpreted
-  | Punc   String      -- Punctuation, eg "(", "::"
-  | Ident  String      -- Haskell identifiers, e.g. foo, baz
-  | Symbol String      -- Haskell symbols, e.g. >>, %
-  | Int Integer
-  | Rat Rational
+  = Char   Char                -- ^ Character literal
+  | String String      -- ^ String literal, with escapes interpreted
+  | Punc   String      -- ^ Punctuation or reserved symbol, e.g. @(@, @::@
+  | Ident  String      -- ^ Haskell identifier, e.g. @foo@, @Baz@
+  | Symbol String      -- ^ Haskell symbol, e.g. @>>@, @:%@
+  | Int Integer                -- ^ Integer literal
+  | Rat Rational       -- ^ Floating point literal
   | EOF
  deriving (Eq, Show)
 
@@ -127,6 +140,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
 
@@ -170,7 +189,7 @@ lexCharE =
          _    -> pfail
   
   lexNumeric =
-    do base <- lexBaseChar
+    do base <- lexBaseChar <++ return 10
        n    <- lexInteger base
        guard (n <= toInteger (ord maxBound))
        return (chr (fromInteger n))
@@ -301,12 +320,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