| Copyright | (c) C. Maeder 2011-2014 |
|---|---|
| License | BSD |
| Maintainer | chr.maeder@web.de |
| Stability | provisional |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Text.ParserCombinators.Parsec.Numeric
Contents
Description
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 fromInteger id) as is done for the parsers fractional2 and
floating2.
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.
A couple of parsers have been added that take a Bool argument, where False
does not require any digit following the decimal dot. The parsers
fractional3 and floating3 allow even to start a number with the decimal
dot. Also parsers hexFract, binFract, hexFloat and binFloat for
hexadecimal or binary fractions and floats have been added.
Note that most 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 wish to check for the end of a string using
eof, i.e. "liftM2 const nat eof".
The returned values may be inaccurate. Int may overflow. Fractional numbers
should be accurate as only one division is performed. Floating point numbers
with decimal exponents may be inaccurate due to using **. Rational numbers
are needed for correct conversions, but large positive or negative exponents
may be a problem and the class RealFloat is needed to check for minimal and
maximal exponents.
- floating :: (Floating f, Stream s m Char) => ParsecT s u m f
- floating2 :: (Floating f, Stream s m Char) => Bool -> ParsecT s u m f
- floating3 :: (Floating f, Stream s m Char) => Bool -> ParsecT s u m f
- decimalFloat :: (Integral i, Floating f, Stream s m Char) => ParsecT s u m (Either i f)
- decFloat :: (Integral i, Floating f, Stream s m Char) => Bool -> ParsecT s u m (Either i f)
- hexFloat :: (Integral i, Floating f, Stream s m Char) => Bool -> ParsecT s u m (Either i f)
- binFloat :: (Integral i, Floating f, Stream s m Char) => Bool -> ParsecT s u m (Either i f)
- natFloat :: (Integral i, Floating f, Stream s m Char) => ParsecT s u m (Either i f)
- zeroNumFloat :: (Integral i, Floating f, Stream s m Char) => ParsecT s u m (Either i f)
- fractExponent :: (Floating f, Stream s m Char) => Integer -> ParsecT s u m f
- hexFractExp :: (Floating f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f
- binFractExp :: (Floating f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f
- fractExp :: (Floating f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f
- genFractExp :: (Floating f, Stream s m Char) => Integer -> ParsecT s u m f -> ParsecT s u m (f -> f) -> ParsecT s u m f
- genFractAndExp :: (Floating f, Stream s m Char) => f -> ParsecT s u m f -> ParsecT s u m (f -> f) -> ParsecT s u m f
- exponentFactor :: (Floating f, Stream s m Char) => ParsecT s u m (f -> f)
- hexExponentFactor :: (Floating f, Stream s m Char) => ParsecT s u m (f -> f)
- extExponentFactor :: (Floating f, Stream s m Char) => Int -> ParsecT s u m (f -> f)
- exponentValue :: Floating f => Int -> Integer -> f
- fractional :: (Fractional f, Stream s m Char) => ParsecT s u m f
- fractional2 :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f
- fractional3 :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f
- decFract :: (Integral i, Fractional f, Stream s m Char) => Bool -> ParsecT s u m (Either i f)
- hexFract :: (Integral i, Fractional f, Stream s m Char) => Bool -> ParsecT s u m (Either i f)
- binFract :: (Integral i, Fractional f, Stream s m Char) => Bool -> ParsecT s u m (Either i f)
- decimalFract :: (Integral i, Fractional f, Stream s m Char) => ParsecT s u m (Either i f)
- natFract :: (Integral i, Fractional f, Stream s m Char) => ParsecT s u m (Either i f)
- zeroNumFract :: (Integral i, Fractional f, Stream s m Char) => ParsecT s u m (Either i f)
- fractFract :: (Fractional f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f
- genFractFract :: (Fractional f, Stream s m Char) => Integer -> ParsecT s u m f -> ParsecT s u m f
- fraction :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f
- hexFraction :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f
- binFraction :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f
- baseFraction :: (Fractional f, Stream s m Char) => Bool -> Int -> ParsecT s u m Char -> ParsecT s u m f
- fractionValue :: Fractional f => Int -> String -> f
- int :: (Integral i, Stream s m Char) => ParsecT s u m i
- sign :: (Num a, Stream s m Char) => ParsecT s u m (a -> a)
- decimal :: (Integral i, Stream s m Char) => ParsecT s u m i
- binDigit :: Stream s m Char => ParsecT s u m Char
- binary :: (Integral i, Stream s m Char) => ParsecT s u m i
- nat :: (Integral i, Stream s m Char) => ParsecT s u m i
- zeroNumber :: (Integral i, Stream s m Char) => ParsecT s u m i
- hexOrOct :: (Integral i, Stream s m Char) => ParsecT s u m i
- hexadecimal :: (Integral i, Stream s m Char) => ParsecT s u m i
- hexnum :: (Integral i, Stream s m Char) => ParsecT s u m i
- octal :: (Integral i, Stream s m Char) => ParsecT s u m i
- number :: (Integral i, Stream s m Char) => Int -> ParsecT s u m Char -> ParsecT s u m i
- numberValue :: Integral i => Int -> String -> i
floats
floating :: (Floating f, Stream s m Char) => ParsecT s u m f Source #
parse a decimal unsigned floating point number containing a dot, e or E
floating2 :: (Floating f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a floating point number possibly containing a decimal dot, e or E
floating3 :: (Floating f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a floating point number possibly starting with a decimal dot.
Note, that a single decimal point or a number starting with .E is illegal.
decimalFloat :: (Integral i, Floating f, Stream s m Char) => ParsecT s u m (Either i f) Source #
same as floating but returns a non-negative integral wrapped by Left if
a fractional part and exponent is missing
decFloat :: (Integral i, Floating f, Stream s m Char) => Bool -> ParsecT s u m (Either i f) Source #
same as floating but returns a non-negative integral wrapped by Left if
a fractional part and exponent is missing
hexFloat :: (Integral i, Floating f, Stream s m Char) => Bool -> ParsecT s u m (Either i f) Source #
parse a hexadecimal floating point number
binFloat :: (Integral i, Floating f, Stream s m Char) => Bool -> ParsecT s u m (Either i f) Source #
parse a binary floating point number
natFloat :: (Integral i, Floating f, Stream s m Char) => ParsecT s u m (Either i f) Source #
parse hexadecimal, octal or decimal integrals or floating
float parts
zeroNumFloat :: (Integral i, Floating f, Stream s m Char) => ParsecT s u m (Either i f) Source #
parse any hexadecimal, octal, decimal or floating point number following a zero
fractExponent :: (Floating f, Stream s m Char) => Integer -> ParsecT s u m f Source #
parse a floating point number given the number before a dot, e or E
hexFractExp :: (Floating f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f Source #
parse a hex floating point number given the number before a dot, p or P
binFractExp :: (Floating f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f Source #
parse a binary floating point number given the number before a dot, p or P
fractExp :: (Floating f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f Source #
parse a floating point number given the number before a dot, e or E
genFractExp :: (Floating f, Stream s m Char) => Integer -> ParsecT s u m f -> ParsecT s u m (f -> f) -> ParsecT s u m f Source #
parse a floating point number given the number before the fraction and exponent
genFractAndExp :: (Floating f, Stream s m Char) => f -> ParsecT s u m f -> ParsecT s u m (f -> f) -> ParsecT s u m f Source #
parse a floating point number given the number before the fraction and exponent that must follow the fraction
exponentFactor :: (Floating f, Stream s m Char) => ParsecT s u m (f -> f) Source #
parse a floating point exponent starting with e or E
hexExponentFactor :: (Floating f, Stream s m Char) => ParsecT s u m (f -> f) Source #
parse a hexadecimal floating point starting with p (IEEE 754)
extExponentFactor :: (Floating f, Stream s m Char) => Int -> ParsecT s u m (f -> f) Source #
parse a signed decimal and compute the exponent factor given a base. For hexadecimal exponential notation (IEEE 754) the base is 2 and the leading character a p.
exponentValue :: Floating f => Int -> Integer -> f Source #
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, Stream s m Char) => ParsecT s u m f Source #
parse a fractional number containing a decimal dot
fractional2 :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a fractional number possibly containing a decimal dot
fractional3 :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a fractional number possibly starting with a decimal dot
decFract :: (Integral i, Fractional f, Stream s m Char) => Bool -> ParsecT s u m (Either i f) Source #
a decimal fractional
hexFract :: (Integral i, Fractional f, Stream s m Char) => Bool -> ParsecT s u m (Either i f) Source #
a hexadecimal fractional
binFract :: (Integral i, Fractional f, Stream s m Char) => Bool -> ParsecT s u m (Either i f) Source #
a binary fractional
decimalFract :: (Integral i, Fractional f, Stream s m Char) => ParsecT s u m (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, Stream s m Char) => ParsecT s u m (Either i f) Source #
parse hexadecimal, octal or decimal integrals or fractional
zeroNumFract :: (Integral i, Fractional f, Stream s m Char) => ParsecT s u m (Either i f) Source #
parse any hexadecimal, octal, decimal or fractional number following a zero
fractional parts
fractFract :: (Fractional f, Stream s m Char) => Integer -> Bool -> ParsecT s u m f Source #
parse a fractional number given the number before the dot
genFractFract :: (Fractional f, Stream s m Char) => Integer -> ParsecT s u m f -> ParsecT s u m f Source #
combine the given number before the dot with a parser for the fractional part
fraction :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a dot followed by decimal digits as fractional part
hexFraction :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a dot followed by hexadecimal digits as fractional part
binFraction :: (Fractional f, Stream s m Char) => Bool -> ParsecT s u m f Source #
parse a dot followed by binary digits as fractional part
baseFraction :: (Fractional f, Stream s m Char) => Bool -> Int -> ParsecT s u m Char -> ParsecT s u m f Source #
parse a dot followed by base dependent digits as fractional part
fractionValue :: Fractional f => Int -> String -> f Source #
compute the fraction given by a sequence of digits following the dot. Only one division is performed and trailing zeros are ignored.
integers and naturals
decimal :: (Integral i, Stream s m Char) => ParsecT s u m i Source #
parse plain non-negative decimal numbers given by a non-empty sequence of digits
nat :: (Integral i, Stream s m Char) => ParsecT s u m i Source #
parse non-negative hexadecimal, octal or decimal numbers nat :: Integral i => ParsecT s u m i
natural parts
zeroNumber :: (Integral i, Stream s m Char) => ParsecT s u m i Source #
parse a nat syntactically starting with a zero
zeroNumber :: Integral i => ParsecT s u m i
hexadecimal :: (Integral i, Stream s m Char) => ParsecT s u m i Source #
parse a hexadecimal number preceded by an x or X character
octal :: (Integral i, Stream s m Char) => ParsecT s u m i Source #
parse an octal number preceded by an o or O character