module Data.Geo.Jord.Parse
    ( digits
    , double
    , integer
    , natural
    , number
    ) where
import Control.Applicative
import Data.Char
import Text.ParserCombinators.ReadP
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 '.' >> natural
    return (s * (read (show i ++ "." ++ show 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