module Data.Geo.Jord.Parser
( digits
, double
, integer
, natural
, number
) where
import Control.Applicative ((<|>))
import Data.Char (isDigit)
import Text.ParserCombinators.ReadP (ReadP, char, count, munch1, option, satisfy)
digits :: Int -> ReadP Int
digits n = fmap read (count n digit)
double :: ReadP Double
double = do
s <- option 1.0 (fmap (\_ -> -1.0) (char '-'))
i <- natural
f <- char '.' >> munch1 isDigit
return (s * (read (show i ++ "." ++ f) :: Double))
integer :: ReadP Int
integer = do
s <- option 1 (fmap (\_ -> -1) (char '-'))
p <- natural
return (s * p)
natural :: ReadP Int
natural = fmap read (munch1 isDigit)
number :: ReadP Double
number = double <|> fmap fromIntegral integer
digit :: ReadP Char
digit = satisfy isDigit