1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
-- |
-- Collection of commonly used regular expressions.
module Text.Regex.Applicative.Common (
-- * Digits
digit
, hexDigit
-- * Numbers
, signed
, decimal
, hexadecimal
) where
import Data.Char
import Data.List (foldl')
import Text.Regex.Applicative
-- | Decimal digit, i.e. @\'0\'@..@\'9\'@
digit :: Num a => RE Char a
digit = fromIntegral . digitToInt <$> psym isDigit
-- | Hexadecimal digit
-- i.e. @\'0\'@..@\'9\'@, @\'a\'@..@\'f\'@, @\'A\'@..@\'F\'@.
hexDigit :: Num a => RE Char a
hexDigit = fromIntegral . digitToInt <$> psym isHexDigit
-- | Add optional sign
signed :: Num a => RE Char a -> RE Char a
signed p = sign <*> p
where
sign = id <$ sym '+'
<|> negate <$ sym '-'
<|> pure id
-- | Parse decimal number without sign.
decimal :: Num a => RE Char a
decimal = foldl' (\d i -> d*10 + i) 0 <$> some digit
-- | Parse decimal number without sign.
hexadecimal :: Num a => RE Char a
hexadecimal = foldl' (\d i -> d*16 + i) 0 <$> some hexDigit
|