Fix #35, fix parsing of floating point values
authorHarry Garrood <harry@garrood.me>
Fri, 13 May 2016 00:26:44 +0000 (01:26 +0100)
committerHarry Garrood <harry@garrood.me>
Fri, 13 May 2016 00:26:44 +0000 (01:26 +0100)
Text/Parsec/Token.hs

index 6656cb7..93946f0 100644 (file)
@@ -530,31 +530,31 @@ makeTokenParser languageDef
                         }
 
     fractExponent n = do{ fract <- fraction
-                        ; expo  <- option 1.0 exponent'
-                        ; return ((fromInteger n + fract)*expo)
+                        ; expo  <- option "" exponent'
+                        ; readDouble (show n ++ fract ++ expo)
                         }
                     <|>
                       do{ expo <- exponent'
-                        ; return ((fromInteger n)*expo)
+                        ; readDouble (show n ++ expo)
                         }
+                      where
+                        readDouble s =
+                          case reads s of
+                            [(x, "")] -> return x
+                            _         -> parserZero
 
     fraction        = do{ char '.'
                         ; digits <- many1 digit <?> "fraction"
-                        ; return (foldr op 0.0 digits)
+                        ; return ('.' : digits)
                         }
                       <?> "fraction"
-                    where
-                      op d f    = (f + fromIntegral (digitToInt d))/10.0
 
     exponent'       = do{ oneOf "eE"
-                        ; f <- sign
+                        ; sign' <- fmap (:[]) (oneOf "+-") <|> return ""
                         ; e <- decimal <?> "exponent"
-                        ; return (power (f e))
+                        ; return ('e' : sign' ++ show e)
                         }
                       <?> "exponent"
-                    where
-                       power e  | e < 0      = 1.0/power(-e)
-                                | otherwise  = fromInteger (10^e)
 
 
     -- integers and naturals