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

Stabilityexperimental
Safe HaskellNone
LanguageHaskell98

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)

All the ReadP parsers here can be safely used with gather as they do not contain any occurences of readS_to_P.

For writing lexers, a simple regular expression to detect numbers would be:

[-+]? [.]? [0-9] [-+/.0-9a-zA-Z]*

Note that this is more lenient than what the grammar of readNumber accepts. If Unicode is supported, one can also include:

  • U+23E8 (DECIMAL EXPONENT SYMBOL)
  • U+2044 (FRACTION SLASH) and U+2215 (DIVISION SLASH)
  • U+2212 (MINUS SIGN)

Synopsis

Simple parsers

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

Read a number (see readNumberP).

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

Interpret a sign:

sign = [-+]

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

ReadP parsers

readNumberP :: Fractional a => ReadP a Source

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 a Source

Read a decimal fraction in scientific notation:

scientific = decimal (decimal_exponent_symbol sign? dec)?

readDecimalP :: Fractional a => ReadP a Source

Read a decimal fraction:

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

readIntegerP :: Num a => ReadP a Source

Read a signed integer in base 10.

integer = sign? [0-9]+

readUnsignedP :: Num a => ReadP a Source

Read an 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 a Source

Read an unsigned integer in hexadecimal notation:

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

readDecP :: Num a => ReadP a Source

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 a Source

Read an unsigned integer in octal notation:

oct = [0-7]+

readBinP :: Num a => ReadP a Source

Read an unsigned integer in binary notation:

bin = [01]+

Character predicates

isBinDigit :: Char -> Bool Source

Whether the character is a binary digit:

bin_digit = [01]

isDecimalExponentSymbol :: Char -> Bool Source

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 -> Bool Source

Whether the character is a fraction slash:

fraction_slash = [/]

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