HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleparsec-numbers-0.1.0Haskell98

Text.ParserCombinators.Parsec.Number

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.

  • 48 values

floats

8 declarations
valuefloating3 :: Floating f => Bool -> CharParser st f
#

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.

float parts

valueextExponentFactor :: Floating f => Int -> CharParser st (f -> f)
#

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.

valueexponentValue :: Floating f => Int -> Integer -> f
#

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)

9 declarations

fractional parts

valuefractionValue :: Fractional f => Int -> String -> f
#

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

6 declarations
valueint :: Integral i => CharParser st i
#

parse an optional sign immediately followed by a nat. Note, that in Daan Leijen's code the sign was wrapped as lexeme in order to skip comments and spaces in between.

valuedecimal :: Integral i => CharParser st i
#

parse plain non-negative decimal numbers given by a non-empty sequence of digits

natural parts