Commit f9946a75 authored by sof's avatar sof

[project @ 1997-03-20 22:13:04 by sof]

readRational handles integral nos
parent f4bc936b
......@@ -292,10 +292,12 @@ readRational :: ReadS Rational -- NB: doesn't handle leading "-"
readRational r
= [ ( (n%1)*10^^(k-d), t ) | (n,d,s) <- readFix r,
(k,t) <- readExp s]
(k,t) <- readExp s] ++
[(0/0, t) | ("NaN", t) <- lex r] ++
[(1/0, t) | ("Infinity", t) <- lex r]
where readFix r = [(read (ds++ds'), length ds', t)
| (ds,'.':s) <- lexDigits r,
(ds',t) <- lexDigits s ]
| (ds,s) <- lexDigits r,
(ds',t) <- lexDotDigits s ]
readExp (e:s) | e `elem` "eE" = readExp' s
readExp s = [(0,s)]
......@@ -304,6 +306,11 @@ readRational r
readExp' ('+':s) = readDec s
readExp' s = readDec s
lexDotDigits ('.':s) = lex0Digits s
lexDotDigits s = [("",s)]
{- ToDo: remove completely
readRational__ :: String -> Rational -- we export this one (non-std)
-- NB: *does* handle a leading "-"
readRational__ top_s
......@@ -316,7 +323,7 @@ readRational__ top_s
[x] -> x
[] -> error ("readRational__: no parse:" ++ top_s)
_ -> error ("readRational__: ambiguous parse:" ++ top_s)
-}
-- The number of decimal digits m below is chosen to guarantee
-- read (show x) == x. See
-- Matula, D. W. A formalization of floating-point numeric base
......@@ -367,8 +374,7 @@ lex (c:s) | isSingle c = [([c],s)]
isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
isIdChar c = isAlphanum c || c `elem` "_'"
lexFracExp ('.':c:cs) | isDigit c
= [('.':ds++e,u) | (ds,t) <- lexDigits (c:cs),
lexFracExp ('.':cs) = [('.':ds++e,u) | (ds,t) <- lex0Digits cs,
(e,u) <- lexExp t]
lexFracExp s = [("",s)]
......@@ -381,6 +387,10 @@ lex (c:s) | isSingle c = [([c],s)]
lexDigits :: ReadS String
lexDigits = nonnull isDigit
-- 0 or more digits
lex0Digits :: ReadS String
lex0Digits s = [span isDigit s]
nonnull :: (Char -> Bool) -> ReadS String
nonnull p s = [(cs,t) | (cs@(_:_),t) <- [span p s]]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment