From 05c8e0e30aad6e28482bce9f712e6b6c7b3c9207 Mon Sep 17 00:00:00 2001 From: Evan Czaplicki Date: Wed, 19 Feb 2014 08:57:08 -0500 Subject: [PATCH] Add support for exponent notation for floating point numbers Brought to my attention by this thread: https://groups.google.com/forum/#!topic/elm-discuss/jQXmaqLHcIA --- compiler/Parse/Literal.hs | 42 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/compiler/Parse/Literal.hs b/compiler/Parse/Literal.hs index 93ef428..9232fb5 100644 --- a/compiler/Parse/Literal.hs +++ b/compiler/Parse/Literal.hs @@ -1,4 +1,4 @@ -{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-} +{-# OPTIONS_GHC -W #-} module Parse.Literal (literal) where import Control.Applicative ((<$>)) @@ -7,16 +7,34 @@ import Parse.Helpers import SourceSyntax.Literal literal :: IParser Literal -literal = num <|> (Str <$> str) <|> (Chr <$> chr) +literal = num <|> (Str <$> str) <|> (Chr <$> chr) "literal" num :: IParser Literal -num = fmap toLit (preNum "number") - where toLit n | '.' `elem` n = FloatNum (read n) - | otherwise = IntNum (read n) - preNum = concat <$> sequence [ option "" minus, many1 digit, option "" postNum ] - postNum = do try $ lookAhead (string "." >> digit) - string "." - ('.':) <$> many1 digit - minus = try $ do string "-" - lookAhead digit - return "-" \ No newline at end of file +num = toLit <$> (number "number") + where + toLit n + | any (`elem` ".eE") n = FloatNum (read n) + | otherwise = IntNum (read n) + + number = concat <$> sequence + [ option "" minus + , many1 digit + , option "" decimals + , option "" exponent ] + + minus = try $ do + string "-" + lookAhead digit + return "-" + + decimals = do + try $ lookAhead (string "." >> digit) + string "." + n <- many1 digit + return ('.' : n) + + exponent = do + string "e" <|> string "E" + op <- option "" (string "+" <|> string "-") + n <- many1 digit + return ('e' : op ++ n)