Add tests from testsuite/tests/h98
[ghc-base.git] / GHC / Read.lhs
index b890b46..77daece 100644 (file)
@@ -1,6 +1,7 @@
 \begin{code}
-{-# OPTIONS_GHC -XNoImplicitPrelude #-}
+{-# LANGUAGE CPP, NoImplicitPrelude #-}
 {-# OPTIONS_HADDOCK hide #-}
+
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  GHC.Read
@@ -67,7 +68,7 @@ import {-# SOURCE #-} GHC.Unicode       ( isDigit )
 #endif
 import GHC.Num
 import GHC.Real
-import GHC.Float
+import GHC.Float ()
 import GHC.Show
 import GHC.Base
 import GHC.Arr
@@ -314,10 +315,15 @@ choose :: [(String, ReadPrec a)] -> ReadPrec a
 -- ^ Parse the specified lexeme and continue as specified.
 -- Esp useful for nullary constructors; e.g.
 --    @choose [(\"A\", return A), (\"B\", return B)]@
+-- We match both Ident and Symbol because the constructor
+-- might be an operator eg (:=:)
 choose sps = foldr ((+++) . try_one) pfail sps
            where
-             try_one (s,p) = do { L.Ident s' <- lexP ;
-                                  if s == s' then p else pfail }
+             try_one (s,p) = do { token <- lexP ;
+                                  case token of
+                                    L.Ident s'  | s==s' -> p
+                                    L.Symbol s' | s==s' -> p
+                                    _other              -> pfail }
 \end{code}
 
 
@@ -446,28 +452,28 @@ instance Read L.Lexeme where
 %*********************************************************
 
 \begin{code}
-readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
+readNumber :: Num a => (L.Lexeme -> ReadPrec a) -> ReadPrec a
 -- Read a signed number
 readNumber convert =
   parens
   ( do x <- lexP
        case x of
-         L.Symbol "-" -> do n <- readNumber convert
+         L.Symbol "-" -> do y <- lexP
+                            n <- convert y
                             return (negate n)
-       
-         _   -> case convert x of
-                   Just n  -> return n
-                   Nothing -> pfail
+
+         _   -> convert x
   )
 
-convertInt :: Num a => L.Lexeme -> Maybe a
-convertInt (L.Int i) = Just (fromInteger i)
-convertInt _         = Nothing
 
-convertFrac :: Fractional a => L.Lexeme -> Maybe a
-convertFrac (L.Int i) = Just (fromInteger i)
-convertFrac (L.Rat r) = Just (fromRational r)
-convertFrac _         = Nothing
+convertInt :: Num a => L.Lexeme -> ReadPrec a
+convertInt (L.Int i) = return (fromInteger i)
+convertInt _         = pfail
+
+convertFrac :: Fractional a => L.Lexeme -> ReadPrec a
+convertFrac (L.Int i) = return (fromInteger i)
+convertFrac (L.Rat r) = return (fromRational r)
+convertFrac _         = pfail
 
 instance Read Int where
   readPrec     = readNumber convertInt