{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
module Parse.Literal (literal) where

import Control.Applicative ((<$>))
import Text.Parsec hiding (newline,spaces)
import Parse.Helpers
import SourceSyntax.Literal

literal :: IParser Literal
literal = num <|> (Str <$> str) <|> (Chr <$> chr)

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 "-"
{-# OPTIONS_GHC -W #-}
module Parse.Literal (literal) where

import Control.Applicative ((<$>))
import Text.Parsec hiding (newline,spaces)
import Parse.Helpers
import SourceSyntax.Literal

literal :: IParser Literal
literal = num <|> (Str <$> str) <|> (Chr <$> chr) <?> "literal"

num :: IParser Literal
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)

Add support for exponent notation for floating point numbers
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
