elm/compiler/Parse/Literal.hs

27 lines
889 B
Haskell
Raw Normal View History

2013-06-14 02:15:40 +00:00
module Parse.Literal (literal) where
import Control.Applicative ((<$>), (<*>))
import Control.Monad
import Text.Parsec hiding (newline,spaces)
import Text.Parsec.Indent
import Parse.Helpers
import SourceSyntax.Literal
literal = num <|> (Str <$> str) <|> chr
2013-06-14 02:15:40 +00:00
num :: IParser Literal
num = fmap toLit (preNum <?> "number")
where toLit n | '.' `elem` n = FloatNum (read n)
| otherwise = IntNum (read n)
2013-10-14 20:59:37 +00:00
preNum = concat <$> sequence [ option "" minus, many1 digit, option "" postNum ]
2013-06-14 02:15:40 +00:00
postNum = do try $ lookAhead (string "." >> digit)
string "."
('.':) <$> many1 digit
2013-10-14 20:59:37 +00:00
minus = try $ do string "-"
lookAhead digit
return "-"
2013-06-14 02:15:40 +00:00
2013-08-07 16:12:53 +00:00
chr :: IParser Literal
chr = Chr <$> betwixt '\'' '\'' (backslashed <|> satisfy (/='\''))
<?> "character"