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
This commit is contained in:
parent
fa5b38532d
commit
05c8e0e30a
1 changed files with 30 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
||||||
{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
|
{-# OPTIONS_GHC -W #-}
|
||||||
module Parse.Literal (literal) where
|
module Parse.Literal (literal) where
|
||||||
|
|
||||||
import Control.Applicative ((<$>))
|
import Control.Applicative ((<$>))
|
||||||
|
@ -7,16 +7,34 @@ import Parse.Helpers
|
||||||
import SourceSyntax.Literal
|
import SourceSyntax.Literal
|
||||||
|
|
||||||
literal :: IParser Literal
|
literal :: IParser Literal
|
||||||
literal = num <|> (Str <$> str) <|> (Chr <$> chr)
|
literal = num <|> (Str <$> str) <|> (Chr <$> chr) <?> "literal"
|
||||||
|
|
||||||
num :: IParser Literal
|
num :: IParser Literal
|
||||||
num = fmap toLit (preNum <?> "number")
|
num = toLit <$> (number <?> "number")
|
||||||
where toLit n | '.' `elem` n = FloatNum (read n)
|
where
|
||||||
| otherwise = IntNum (read n)
|
toLit n
|
||||||
preNum = concat <$> sequence [ option "" minus, many1 digit, option "" postNum ]
|
| any (`elem` ".eE") n = FloatNum (read n)
|
||||||
postNum = do try $ lookAhead (string "." >> digit)
|
| otherwise = IntNum (read n)
|
||||||
string "."
|
|
||||||
('.':) <$> many1 digit
|
number = concat <$> sequence
|
||||||
minus = try $ do string "-"
|
[ option "" minus
|
||||||
lookAhead digit
|
, many1 digit
|
||||||
return "-"
|
, 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)
|
||||||
|
|
Loading…
Reference in a new issue