Portability | portable |
---|---|
Stability | provisional |
Maintainer | chr.maeder@web.de |
adjusted and portable number parsers stolen from Text.ParserCombinators.Parsec.Token
The basic top-level number parsers are decimal
, nat
, int
, fractional
,
decimalFract
, natFract
, floating
, decimalFloat
, natFloat
.
natFloat
parses numeric literals as defined for Haskell. All numbers are
unsigned, i.e. non-negative. Leading zeros are allowed. At least a single
digit is required. A decimal point must be preceded and followed by at least
one digit.
A result type (Either Integer Double)
can be converted to a final Double
using (either fromIntegral id)
.
The parser nat
, natFract
and natFloat
parse hexadecimal and octal
integrals (beginning with 0x
, 0X
, 0o
or 0O
) that are disallowed when
using decimal
, decimalFract
and decimalFloat
.
The parsers decimalFract
and natFract
only allow a decimal point, whereas
decimalFloat
and natFloat
also allow the exponent notation using e
or
E
.
The parser fractional
requires a decimal point between at least two
digits and floating
requires either a decimal point or the exponent
notation using e
or E
. (Both parsers do not return integral values and do
not support hexadecimal or octal values).
Signed numbers can be parsed using "ap
sign
" as is done
for the int
parser.
Note that all top-level parsers succeed on a string like "1.0e-100
", but
only the floating point parsers consume the whole string. The fractional
parsers stop before the exponent and the integral parsers before the decimal
point. You may which to check for the end of a string using
eof
, i.e. liftM2 const nat eof
.
- floating :: Floating f => CharParser st f
- decimalFloat :: (Integral i, Floating f) => CharParser st (Either i f)
- natFloat :: (Integral i, Floating f) => CharParser st (Either i f)
- zeroNumFloat :: (Integral i, Floating f) => CharParser st (Either i f)
- fractFloat :: (Integral i, Floating f) => i -> CharParser st (Either i f)
- fractExponent :: (Integral i, Floating f) => i -> CharParser st f
- exponentFactor :: Floating f => CharParser st (f -> f)
- exponentValue :: Floating f => Integer -> f
- fractional :: Fractional f => CharParser st f
- decimalFract :: (Integral i, Fractional f) => CharParser st (Either i f)
- natFract :: (Integral i, Fractional f) => CharParser st (Either i f)
- zeroNumFract :: (Integral i, Fractional f) => CharParser st (Either i f)
- fractFract :: (Integral i, Fractional f) => i -> CharParser st f
- fraction :: Fractional f => CharParser st f
- fractionValue :: Fractional f => String -> f
- int :: Integral i => CharParser st i
- sign :: Num a => CharParser st (a -> a)
- decimal :: Integral i => CharParser st i
- nat :: Integral i => CharParser st i
- zeroNumber :: Integral i => CharParser st i
- hexadecimal :: Integral i => CharParser st i
- octal :: Integral i => CharParser st i
- number :: Integral i => Int -> GenParser tok st Char -> GenParser tok st i
- numberValue :: Integral i => Int -> String -> i
floats
floating :: Floating f => CharParser st fSource
parse a decimal unsigned floating point number containing a dot, e or E
decimalFloat :: (Integral i, Floating f) => CharParser st (Either i f)Source
same as floating
but returns a non-negative integral wrapped by Left if
a fractional part and exponent is missing
natFloat :: (Integral i, Floating f) => CharParser st (Either i f)Source
parse hexadecimal, octal or decimal integrals or floating
float parts
zeroNumFloat :: (Integral i, Floating f) => CharParser st (Either i f)Source
parse any hexadecimal, octal, decimal or floating point number following a zero
fractFloat :: (Integral i, Floating f) => i -> CharParser st (Either i f)Source
same as fractExponent
, returns a f wrapped by Right
fractExponent :: (Integral i, Floating f) => i -> CharParser st fSource
parse a floating point number given the number before a dot, e or E
exponentFactor :: Floating f => CharParser st (f -> f)Source
parse a floating point exponent starting with e or E
exponentValue :: Floating f => Integer -> fSource
compute the factor given by the number following e or E. This
implementation uses **
rather than ^
for more efficiency for large
integers.
fractional numbers (with just a decimal point between digits)
fractional :: Fractional f => CharParser st fSource
parse a fractional number containing a decimal dot
decimalFract :: (Integral i, Fractional f) => CharParser st (Either i f)Source
same as fractional
but returns a non-negative integral wrapped by Left if
a fractional part is missing
natFract :: (Integral i, Fractional f) => CharParser st (Either i f)Source
parse hexadecimal, octal or decimal integrals or fractional
zeroNumFract :: (Integral i, Fractional f) => CharParser st (Either i f)Source
parse any hexadecimal, octal, decimal or fractional number following a zero
fractional parts
fractFract :: (Integral i, Fractional f) => i -> CharParser st fSource
parse a fractional number given the number before the dot
fraction :: Fractional f => CharParser st fSource
parse a dot followed by digits as fractional part
fractionValue :: Fractional f => String -> fSource
compute the fraction given by a sequence of digits following the dot
integers and naturals
int :: Integral i => CharParser st iSource
sign :: Num a => CharParser st (a -> a)Source
decimal :: Integral i => CharParser st iSource
parse plain non-negative decimal numbers given by a non-empty sequence of digits
nat :: Integral i => CharParser st iSource
parse non-negative hexadecimal, octal or decimal numbers
natural parts
zeroNumber :: Integral i => CharParser st iSource
parse a nat
syntactically starting with a zero
hexadecimal :: Integral i => CharParser st iSource
parse a hexadecimal number preceded by an x or X character
octal :: Integral i => CharParser st iSource
parse an octal number preceded by an o or O character
number :: Integral i => Int -> GenParser tok st Char -> GenParser tok st iSource
parse a non-negative number given a base and a parser for the digits
numberValue :: Integral i => Int -> String -> iSource
compute the value from a string of digits using a base