elm/compiler/Parse/Literal.hs

33 lines
1,007 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 qualified Parse.Pattern as Pattern
import qualified Parse.Type as Type
import Parse.Binop
import SourceSyntax.Pattern hiding (tuple,list)
import SourceSyntax.Literal
literal = num <|> str <|> chr
num :: IParser Literal
num = fmap toLit (preNum <?> "number")
where toLit n | '.' `elem` n = FloatNum (read n)
| otherwise = IntNum (read n)
preNum = (++) <$> many1 digit <*> option "" postNum
postNum = do try $ lookAhead (string "." >> digit)
string "."
('.':) <$> many1 digit
str :: IParser Literal
str = liftM Str . expecting "string" . betwixt '"' '"' . many $
backslashed <|> satisfy (/='"')
chr :: IParser Literal
chr = Chr <$> betwixt '\'' '\'' (backslashed <|> satisfy (/='\''))
<?> "character"