module Streamly.Internal.Unicode.Char.Parser where
import Control.Applicative (Alternative(..))
import Control.Monad.Catch (MonadCatch)
import Data.Bits (Bits, (.|.), shiftL)
import Data.Char (ord)
import Streamly.Internal.Data.Parser (Parser)
import qualified Data.Char as Char
import qualified Streamly.Internal.Data.Parser as Parser
import qualified Streamly.Internal.Data.Fold as Fold
#define CHAR_PARSER_SIG(NAME) NAME :: MonadCatch m => Parser m Char Char
#define CHAR_PARSER(NAME, PREDICATE) NAME = Parser.satisfy Char.PREDICATE
CHAR_PARSER_SIG(space)
CHAR_PARSER(space,isSpace)
CHAR_PARSER_SIG(lower)
CHAR_PARSER(lower,isLower)
CHAR_PARSER_SIG(upper)
CHAR_PARSER(upper,isUpper)
CHAR_PARSER_SIG(alpha)
CHAR_PARSER(alpha,isAlpha)
CHAR_PARSER_SIG(alphaNum)
CHAR_PARSER(alphaNum,isAlphaNum)
CHAR_PARSER_SIG(print)
CHAR_PARSER(print,isPrint)
CHAR_PARSER_SIG(digit)
CHAR_PARSER(digit,isDigit)
CHAR_PARSER_SIG(octDigit)
CHAR_PARSER(octDigit,isOctDigit)
CHAR_PARSER_SIG(hexDigit)
CHAR_PARSER(hexDigit,isHexDigit)
CHAR_PARSER_SIG(letter)
CHAR_PARSER(letter,isLetter)
CHAR_PARSER_SIG(mark)
CHAR_PARSER(mark,isMark)
CHAR_PARSER_SIG(number)
CHAR_PARSER(number,isNumber)
CHAR_PARSER_SIG(punctuation)
punctuation :: Parser m Char Char
CHAR_PARSER(punctuation,isPunctuation)
CHAR_PARSER_SIG(symbol)
CHAR_PARSER(symbol,isSymbol)
CHAR_PARSER_SIG(separator)
CHAR_PARSER(separator,isSeparator)
CHAR_PARSER_SIG(ascii)
CHAR_PARSER(ascii,isAscii)
CHAR_PARSER_SIG(latin1)
CHAR_PARSER(latin1,isLatin1)
CHAR_PARSER_SIG(asciiUpper)
CHAR_PARSER(asciiUpper,isAsciiUpper)
CHAR_PARSER_SIG(asciiLower)
CHAR_PARSER(asciiLower,isAsciiLower)
{-# INLINE char #-}
char :: MonadCatch m => Char -> Parser m Char Char
char :: Char -> Parser m Char Char
char Char
c = (Char -> Bool) -> Parser m Char Char
forall (m :: * -> *) a. MonadCatch m => (a -> Bool) -> Parser m a a
Parser.satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c)
{-# INLINE decimal #-}
decimal :: (MonadCatch m, Integral a) => Parser m Char a
decimal :: Parser m Char a
decimal = (Char -> Bool) -> Fold m Char a -> Parser m Char a
forall (m :: * -> *) a b.
MonadCatch m =>
(a -> Bool) -> Fold m a b -> Parser m a b
Parser.takeWhile1 Char -> Bool
Char.isDigit ((a -> Char -> a) -> a -> Fold m Char a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
Fold.foldl' a -> Char -> a
forall a. Num a => a -> Char -> a
step a
0)
where
step :: a -> Char -> a
step a
a Char
c = a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
10 a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
48)
{-# INLINE hexadecimal #-}
hexadecimal :: (MonadCatch m, Integral a, Bits a) => Parser m Char a
hexadecimal :: Parser m Char a
hexadecimal = (Char -> Bool) -> Fold m Char a -> Parser m Char a
forall (m :: * -> *) a b.
MonadCatch m =>
(a -> Bool) -> Fold m a b -> Parser m a b
Parser.takeWhile1 Char -> Bool
isHexDigit ((a -> Char -> a) -> a -> Fold m Char a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Fold m a b
Fold.foldl' a -> Char -> a
forall a. (Bits a, Num a) => a -> Char -> a
step a
0)
where
isHexDigit :: Char -> Bool
isHexDigit Char
c =
(Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'9')
Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'f')
Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'F')
step :: a -> Char -> a
step a
a Char
c
| Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
48 Bool -> Bool -> Bool
&& Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
57 =
(a
a a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` Int
4) a -> a -> a
forall a. Bits a => a -> a -> a
.|. Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
48)
| Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
97 =
(a
a a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` Int
4) a -> a -> a
forall a. Bits a => a -> a -> a
.|. Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
87)
| Bool
otherwise =
(a
a a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` Int
4) a -> a -> a
forall a. Bits a => a -> a -> a
.|. Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
55)
where
w :: Int
w = Char -> Int
ord Char
c
{-# INLINE signed #-}
signed :: (Num a, MonadCatch m) => Parser m Char a -> Parser m Char a
signed :: Parser m Char a -> Parser m Char a
signed Parser m Char a
p = (a -> a
forall a. Num a => a -> a
negate (a -> a) -> Parser m Char a -> Parser m Char a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser m Char Char
forall (m :: * -> *). MonadCatch m => Char -> Parser m Char Char
char Char
'-' Parser m Char Char -> Parser m Char a -> Parser m Char a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser m Char a
p)) Parser m Char a -> Parser m Char a -> Parser m Char a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> Parser m Char Char
forall (m :: * -> *). MonadCatch m => Char -> Parser m Char Char
char Char
'+' Parser m Char Char -> Parser m Char a -> Parser m Char a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser m Char a
p) Parser m Char a -> Parser m Char a -> Parser m Char a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser m Char a
p
double :: Parser m Char Double
double :: Parser m Char Double
double = Parser m Char Double
forall a. HasCallStack => a
undefined