sci-ratio-0.2.0.0: Rational numbers in scientific notation.

Stabilityexperimental
Safe HaskellNone

Data.SciRatio.Read

Contents

Description

The functions here parse numbers in a variety of formats. Examples:

>>> readSciRational "-0.0e+3"         -- result: Just ((0 % 1) .^ 0)
>>> readSciRational "0.25e+2"         -- result: Just ((25 % 1) .^ 0)
>>> readSciRational "-1.0e-1"         -- result: Just (((-1) % 1) .^ (-1))
>>> readSciRational "5.0e+20/6.e0"    -- result: Just ((25 % 3) .^ 19)
>>> readSciRational "0xfeedface"      -- result: Just ((4277009102 % 1) .^ 0)
>>> readSciRational "1e99999999"      -- result: Just ((1 % 1) .^ 99999999)

Synopsis

Simple parsers

readNumber :: Fractional a => String -> Maybe aSource

Read a number (see readNumberP).

readSign :: Num a => Char -> Maybe aSource

Interpret a sign:

 sign = [-+]

Note: U+2212 (MINUS SIGN) is also accepted.

ReadP parsers

readNumberP :: Fractional a => ReadP aSource

Read a rational number in scientific notation:

 number = [0] [bB] [0-1]+
        | [0] [oO] [0-7]+
        | [0] [xX] [0-9a-fA-F]+
        | scientific ( fraction_slash scientific )?

readScientificP :: Fractional a => ReadP aSource

Read a decimal fraction in scientific notation:

 scientific = decimal (decimal_exponent_symbol sign? dec)?

readDecimalP :: Fractional a => ReadP aSource

Read a decimal fraction:

 decimal = sign? ( [0-9]+ [.]? [0-9]*
                 | [.] [0-9]+ )

readIntegerP :: Num a => ReadP aSource

Read a signed integer in base 10.

 integer = sign? [0-9]+

readUnsignedP :: Num a => ReadP aSource

Read a unsigned number in either binary (0b), octal (0o), decimal, or hexadecimal (0x) format:

 unsigned = [0] [bB] [0-1]+
          | [0] [oO] [0-7]+
          | [0] [xX] [0-9a-fA-F]+
          | dec

readHexP :: Num a => ReadP aSource

Read an unsigned integer in hexadecimal notation:

 hex = [0-9A-Fa-f]+

readDecP :: Num a => ReadP aSource

Read an unsigned integer in base 10.

 dec = [0-9]+

Note: Although similar functions exist in Lex, the versions here do not require Eq.

readOctP :: Num a => ReadP aSource

Read an unsigned integer in octal notation:

 oct = [0-7]+

readBinP :: Num a => ReadP aSource

Read an unsigned integer in binary notation:

 bin = [01]+

Character predicates

isBinDigit :: Char -> BoolSource

Whether the character is a binary digit:

 bin_digit = [01]

isDecimalExponentSymbol :: Char -> BoolSource

Whether the character can be used as an exponent symbol in scientific notation:

 decimal_exponent_symbol = [eE]

Note: U+23E8 (DECIMAL EXPONENT SYMBOL) is also accepted.

isFractionSlash :: Char -> BoolSource

Whether the character is a fraction slash:

 fraction_slash = [/]

Note: U+002F (SOLIDUS), U+2044 (FRACTION SLASH), and U+2215 (DIVISION SLASH) are also accepted.