Stability | experimental |
---|---|
Safe Haskell | None |
Language | Haskell98 |
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
parsers here can be safely used with ReadP
as they do
not contain any occurences of gather
.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
accepts. If Unicode is supported, one can also include:readNumber
U+23E8
(DECIMAL EXPONENT SYMBOL)U+2044
(FRACTION SLASH) andU+2215
(DIVISION SLASH)U+2212
(MINUS SIGN)
- readNumber :: Fractional a => String -> Maybe a
- readSciRational :: String -> Maybe SciRational
- readSign :: Num a => Char -> Maybe a
- readNumberP :: Fractional a => ReadP a
- readSciRationalP :: ReadP SciRational
- readScientificP :: Fractional a => ReadP a
- readDecimalP :: Fractional a => ReadP a
- readIntegerP :: Num a => ReadP a
- readUnsignedP :: Num a => ReadP a
- readHexP :: Num a => ReadP a
- readDecP :: Num a => ReadP a
- readOctP :: Num a => ReadP a
- readBinP :: Num a => ReadP a
- isBinDigit :: Char -> Bool
- isDecimalExponentSymbol :: Char -> Bool
- isFractionSlash :: Char -> Bool
Simple parsers
readNumber :: Fractional a => String -> Maybe a Source
Read a number (see
).readNumberP
readSciRational :: String -> Maybe SciRational 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
ReadP
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 )?
readSciRationalP :: ReadP SciRational Source
Read a number (see
).readNumberP
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]+
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.