{-# LANGUAGE BangPatterns, CPP, FlexibleInstances, TypeFamilies,
TypeSynonymInstances, GADTs #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans -fno-warn-warnings-deprecations #-}
module Data.Attoparsec.ByteString.Char8
(
Parser
, A.Result
, A.IResult(..)
, I.compareResults
, A.parse
, A.feed
, A.parseOnly
, A.parseWith
, A.parseTest
, A.maybeResult
, A.eitherResult
, char
, char8
, anyChar
, notChar
, satisfy
, peekChar
, peekChar'
, digit
, letter_iso8859_15
, letter_ascii
, space
, isDigit
, isDigit_w8
, isAlpha_iso8859_15
, isAlpha_ascii
, isSpace
, isSpace_w8
, inClass
, notInClass
, I.string
, I.stringCI
, skipSpace
, skipWhile
, I.take
, scan
, takeWhile
, takeWhile1
, takeTill
, (.*>)
, (<*.)
, I.takeByteString
, I.takeLazyByteString
, I.endOfLine
, isEndOfLine
, isHorizontalSpace
, decimal
, hexadecimal
, signed
, double
, Number(..)
, number
, rational
, scientific
, try
, (<?>)
, choice
, count
, option
, many'
, many1
, many1'
, manyTill
, manyTill'
, sepBy
, sepBy'
, sepBy1
, sepBy1'
, skipMany
, skipMany1
, eitherP
, I.match
, I.endOfInput
, I.atEnd
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (pure, (*>), (<*), (<$>))
import Data.Word (Word)
#endif
import Control.Applicative ((<|>))
import Control.Monad (void, when)
import Data.Attoparsec.ByteString.FastSet (charClass, memberChar)
import Data.Attoparsec.ByteString.Internal (Parser)
import Data.Attoparsec.Combinator
import Data.Attoparsec.Number (Number(..))
import Data.Bits (Bits, (.|.), shiftL)
import Data.ByteString.Internal (c2w, w2c)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.String (IsString(..))
import Data.Scientific (Scientific)
import qualified Data.Scientific as Sci
import Data.Word (Word8, Word16, Word32, Word64)
import Prelude hiding (takeWhile)
import qualified Data.Attoparsec.ByteString as A
import qualified Data.Attoparsec.ByteString.Internal as I
import qualified Data.Attoparsec.Internal as I
import qualified Data.ByteString as B8
import qualified Data.ByteString.Char8 as B
instance (a ~ B.ByteString) => IsString (Parser a) where
fromString :: String -> Parser a
fromString = ByteString -> Parser ByteString
I.string (ByteString -> Parser ByteString)
-> (String -> ByteString) -> String -> Parser ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
B.pack
takeWhile1 :: (Char -> Bool) -> Parser B.ByteString
takeWhile1 :: (Char -> Bool) -> Parser ByteString
takeWhile1 p :: Char -> Bool
p = (Word8 -> Bool) -> Parser ByteString
I.takeWhile1 (Char -> Bool
p (Char -> Bool) -> (Word8 -> Char) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE takeWhile1 #-}
satisfy :: (Char -> Bool) -> Parser Char
satisfy :: (Char -> Bool) -> Parser Char
satisfy = (Word8 -> Char) -> (Char -> Bool) -> Parser Char
forall a. (Word8 -> a) -> (a -> Bool) -> Parser a
I.satisfyWith Word8 -> Char
w2c
{-# INLINE satisfy #-}
letter_iso8859_15 :: Parser Char
letter_iso8859_15 :: Parser Char
letter_iso8859_15 = (Char -> Bool) -> Parser Char
satisfy Char -> Bool
isAlpha_iso8859_15 Parser Char -> String -> Parser Char
forall i a. Parser i a -> String -> Parser i a
<?> "letter_iso8859_15"
{-# INLINE letter_iso8859_15 #-}
letter_ascii :: Parser Char
letter_ascii :: Parser Char
letter_ascii = (Char -> Bool) -> Parser Char
satisfy Char -> Bool
isAlpha_ascii Parser Char -> String -> Parser Char
forall i a. Parser i a -> String -> Parser i a
<?> "letter_ascii"
{-# INLINE letter_ascii #-}
isAlpha_iso8859_15 :: Char -> Bool
isAlpha_iso8859_15 :: Char -> Bool
isAlpha_iso8859_15 c :: Char
c = (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= 'a' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= 'z') Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= 'A' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= 'Z') Bool -> Bool -> Bool
||
(Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= '\166' Bool -> Bool -> Bool
&& Char -> Bool
moby Char
c)
where moby :: Char -> Bool
moby = String -> Char -> Bool
notInClass "\167\169\171-\179\182\183\185\187\191\215\247"
{-# NOINLINE moby #-}
{-# INLINE isAlpha_iso8859_15 #-}
isAlpha_ascii :: Char -> Bool
isAlpha_ascii :: Char -> Bool
isAlpha_ascii c :: Char
c = (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= 'a' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= 'z') Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= 'A' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= 'Z')
{-# INLINE isAlpha_ascii #-}
digit :: Parser Char
digit :: Parser Char
digit = (Char -> Bool) -> Parser Char
satisfy Char -> Bool
isDigit Parser Char -> String -> Parser Char
forall i a. Parser i a -> String -> Parser i a
<?> "digit"
{-# INLINE digit #-}
isDigit :: Char -> Bool
isDigit :: Char -> Bool
isDigit c :: Char
c = Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= '0' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= '9'
{-# INLINE isDigit #-}
isDigit_w8 :: Word8 -> Bool
isDigit_w8 :: Word8 -> Bool
isDigit_w8 w :: Word8
w = Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- 48 Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= 9
{-# INLINE isDigit_w8 #-}
anyChar :: Parser Char
anyChar :: Parser Char
anyChar = (Char -> Bool) -> Parser Char
satisfy ((Char -> Bool) -> Parser Char) -> (Char -> Bool) -> Parser Char
forall a b. (a -> b) -> a -> b
$ Bool -> Char -> Bool
forall a b. a -> b -> a
const Bool
True
{-# INLINE anyChar #-}
peekChar :: Parser (Maybe Char)
peekChar :: Parser (Maybe Char)
peekChar = ((Word8 -> Char) -> Maybe Word8 -> Maybe Char
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word8 -> Char
w2c) (Maybe Word8 -> Maybe Char)
-> Parser ByteString (Maybe Word8) -> Parser (Maybe Char)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Parser ByteString (Maybe Word8)
I.peekWord8
{-# INLINE peekChar #-}
peekChar' :: Parser Char
peekChar' :: Parser Char
peekChar' = Word8 -> Char
w2c (Word8 -> Char) -> Parser ByteString Word8 -> Parser Char
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Parser ByteString Word8
I.peekWord8'
{-# INLINE peekChar' #-}
isSpace :: Char -> Bool
isSpace :: Char -> Bool
isSpace c :: Char
c = (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== ' ') Bool -> Bool -> Bool
|| ('\t' Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= '\r')
{-# INLINE isSpace #-}
isSpace_w8 :: Word8 -> Bool
isSpace_w8 :: Word8 -> Bool
isSpace_w8 w :: Word8
w = Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 32 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- 9 Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= 4
{-# INLINE isSpace_w8 #-}
space :: Parser Char
space :: Parser Char
space = (Char -> Bool) -> Parser Char
satisfy Char -> Bool
isSpace Parser Char -> String -> Parser Char
forall i a. Parser i a -> String -> Parser i a
<?> "space"
{-# INLINE space #-}
char :: Char -> Parser Char
char :: Char -> Parser Char
char c :: Char
c = (Char -> Bool) -> Parser Char
satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c) Parser Char -> String -> Parser Char
forall i a. Parser i a -> String -> Parser i a
<?> [Char
c]
{-# INLINE char #-}
char8 :: Char -> Parser Word8
char8 :: Char -> Parser ByteString Word8
char8 c :: Char
c = (Word8 -> Bool) -> Parser ByteString Word8
I.satisfy (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
c) Parser ByteString Word8 -> String -> Parser ByteString Word8
forall i a. Parser i a -> String -> Parser i a
<?> [Char
c]
{-# INLINE char8 #-}
notChar :: Char -> Parser Char
notChar :: Char -> Parser Char
notChar c :: Char
c = (Char -> Bool) -> Parser Char
satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
c) Parser Char -> String -> Parser Char
forall i a. Parser i a -> String -> Parser i a
<?> "not " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Char
c]
{-# INLINE notChar #-}
inClass :: String -> Char -> Bool
inClass :: String -> Char -> Bool
inClass s :: String
s = (Char -> FastSet -> Bool
`memberChar` FastSet
mySet)
where mySet :: FastSet
mySet = String -> FastSet
charClass String
s
{-# INLINE inClass #-}
notInClass :: String -> Char -> Bool
notInClass :: String -> Char -> Bool
notInClass s :: String
s = Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Char -> Bool
inClass String
s
{-# INLINE notInClass #-}
takeWhile :: (Char -> Bool) -> Parser B.ByteString
takeWhile :: (Char -> Bool) -> Parser ByteString
takeWhile p :: Char -> Bool
p = (Word8 -> Bool) -> Parser ByteString
I.takeWhile (Char -> Bool
p (Char -> Bool) -> (Word8 -> Char) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE takeWhile #-}
scan :: s -> (s -> Char -> Maybe s) -> Parser B.ByteString
scan :: s -> (s -> Char -> Maybe s) -> Parser ByteString
scan s0 :: s
s0 p :: s -> Char -> Maybe s
p = s -> (s -> Word8 -> Maybe s) -> Parser ByteString
forall s. s -> (s -> Word8 -> Maybe s) -> Parser ByteString
I.scan s
s0 (\s :: s
s -> s -> Char -> Maybe s
p s
s (Char -> Maybe s) -> (Word8 -> Char) -> Word8 -> Maybe s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE scan #-}
takeTill :: (Char -> Bool) -> Parser B.ByteString
takeTill :: (Char -> Bool) -> Parser ByteString
takeTill p :: Char -> Bool
p = (Word8 -> Bool) -> Parser ByteString
I.takeTill (Char -> Bool
p (Char -> Bool) -> (Word8 -> Char) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE takeTill #-}
skipWhile :: (Char -> Bool) -> Parser ()
skipWhile :: (Char -> Bool) -> Parser ()
skipWhile p :: Char -> Bool
p = (Word8 -> Bool) -> Parser ()
I.skipWhile (Char -> Bool
p (Char -> Bool) -> (Word8 -> Char) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE skipWhile #-}
skipSpace :: Parser ()
skipSpace :: Parser ()
skipSpace = (Word8 -> Bool) -> Parser ()
I.skipWhile Word8 -> Bool
isSpace_w8
{-# INLINE skipSpace #-}
(.*>) :: B.ByteString -> Parser a -> Parser a
s :: ByteString
s .*> :: ByteString -> Parser a -> Parser a
.*> f :: Parser a
f = ByteString -> Parser ByteString
I.string ByteString
s Parser ByteString -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
f
{-# DEPRECATED (.*>) "This is no longer necessary, and will be removed. Use '*>' instead." #-}
(<*.) :: Parser a -> B.ByteString -> Parser a
f :: Parser a
f <*. :: Parser a -> ByteString -> Parser a
<*. s :: ByteString
s = Parser a
f Parser a -> Parser ByteString -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ByteString -> Parser ByteString
I.string ByteString
s
{-# DEPRECATED (<*.) "This is no longer necessary, and will be removed. Use '<*' instead." #-}
isEndOfLine :: Word8 -> Bool
isEndOfLine :: Word8 -> Bool
isEndOfLine w :: Word8
w = Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 13 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 10
{-# INLINE isEndOfLine #-}
isHorizontalSpace :: Word8 -> Bool
isHorizontalSpace :: Word8 -> Bool
isHorizontalSpace w :: Word8
w = Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 32 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 9
{-# INLINE isHorizontalSpace #-}
hexadecimal :: (Integral a, Bits a) => Parser a
hexadecimal :: Parser a
hexadecimal = (a -> Word8 -> a) -> a -> ByteString -> a
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B8.foldl' a -> Word8 -> a
forall a a. (Bits a, Integral a, Num a) => a -> a -> a
step 0 (ByteString -> a) -> Parser ByteString -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Word8 -> Bool) -> Parser ByteString
I.takeWhile1 Word8 -> Bool
forall a. (Ord a, Num a) => a -> Bool
isHexDigit
where
isHexDigit :: a -> Bool
isHexDigit w :: a
w = (a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 48 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= 57) Bool -> Bool -> Bool
||
(a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 97 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= 102) Bool -> Bool -> Bool
||
(a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 65 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= 70)
step :: a -> a -> a
step a :: a
a w :: a
w | a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 48 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= 57 = (a
a a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` 4) a -> a -> a
forall a. Bits a => a -> a -> a
.|. a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- 48)
| a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 97 = (a
a a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` 4) a -> a -> a
forall a. Bits a => a -> a -> a
.|. a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- 87)
| Bool
otherwise = (a
a a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` 4) a -> a -> a
forall a. Bits a => a -> a -> a
.|. a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- 55)
{-# SPECIALISE hexadecimal :: Parser Int #-}
{-# SPECIALISE hexadecimal :: Parser Int8 #-}
{-# SPECIALISE hexadecimal :: Parser Int16 #-}
{-# SPECIALISE hexadecimal :: Parser Int32 #-}
{-# SPECIALISE hexadecimal :: Parser Int64 #-}
{-# SPECIALISE hexadecimal :: Parser Integer #-}
{-# SPECIALISE hexadecimal :: Parser Word #-}
{-# SPECIALISE hexadecimal :: Parser Word8 #-}
{-# SPECIALISE hexadecimal :: Parser Word16 #-}
{-# SPECIALISE hexadecimal :: Parser Word32 #-}
{-# SPECIALISE hexadecimal :: Parser Word64 #-}
decimal :: Integral a => Parser a
decimal :: Parser a
decimal = (a -> Word8 -> a) -> a -> ByteString -> a
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B8.foldl' a -> Word8 -> a
forall a a. (Integral a, Num a) => a -> a -> a
step 0 (ByteString -> a) -> Parser ByteString -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Word8 -> Bool) -> Parser ByteString
I.takeWhile1 Word8 -> Bool
isDigit_w8
where step :: a -> a -> a
step a :: a
a w :: a
w = a
a a -> a -> a
forall a. Num a => a -> a -> a
* 10 a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- 48)
{-# SPECIALISE decimal :: Parser Int #-}
{-# SPECIALISE decimal :: Parser Int8 #-}
{-# SPECIALISE decimal :: Parser Int16 #-}
{-# SPECIALISE decimal :: Parser Int32 #-}
{-# SPECIALISE decimal :: Parser Int64 #-}
{-# SPECIALISE decimal :: Parser Integer #-}
{-# SPECIALISE decimal :: Parser Word #-}
{-# SPECIALISE decimal :: Parser Word8 #-}
{-# SPECIALISE decimal :: Parser Word16 #-}
{-# SPECIALISE decimal :: Parser Word32 #-}
{-# SPECIALISE decimal :: Parser Word64 #-}
signed :: Num a => Parser a -> Parser a
{-# SPECIALISE signed :: Parser Int -> Parser Int #-}
{-# SPECIALISE signed :: Parser Int8 -> Parser Int8 #-}
{-# SPECIALISE signed :: Parser Int16 -> Parser Int16 #-}
{-# SPECIALISE signed :: Parser Int32 -> Parser Int32 #-}
{-# SPECIALISE signed :: Parser Int64 -> Parser Int64 #-}
{-# SPECIALISE signed :: Parser Integer -> Parser Integer #-}
signed :: Parser a -> Parser a
signed p :: Parser a
p = (a -> a
forall a. Num a => a -> a
negate (a -> a) -> Parser a -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser ByteString Word8
char8 '-' Parser ByteString Word8 -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p))
Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> Parser ByteString Word8
char8 '+' Parser ByteString Word8 -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p)
Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser a
p
rational :: Fractional a => Parser a
{-# SPECIALIZE rational :: Parser Double #-}
{-# SPECIALIZE rational :: Parser Float #-}
{-# SPECIALIZE rational :: Parser Rational #-}
{-# SPECIALIZE rational :: Parser Scientific #-}
rational :: Parser a
rational = (Scientific -> a) -> Parser a
forall a. (Scientific -> a) -> Parser a
scientifically Scientific -> a
forall a b. (Real a, Fractional b) => a -> b
realToFrac
double :: Parser Double
double :: Parser Double
double = (Scientific -> Double) -> Parser Double
forall a. (Scientific -> a) -> Parser a
scientifically Scientific -> Double
forall a. RealFloat a => Scientific -> a
Sci.toRealFloat
number :: Parser Number
number :: Parser Number
number = (Scientific -> Number) -> Parser Number
forall a. (Scientific -> a) -> Parser a
scientifically ((Scientific -> Number) -> Parser Number)
-> (Scientific -> Number) -> Parser Number
forall a b. (a -> b) -> a -> b
$ \s :: Scientific
s ->
let e :: Int
e = Scientific -> Int
Sci.base10Exponent Scientific
s
c :: Integer
c = Scientific -> Integer
Sci.coefficient Scientific
s
in if Int
e Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0
then Integer -> Number
I (Integer
c Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* 10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
e)
else Double -> Number
D (Scientific -> Double
forall a. RealFloat a => Scientific -> a
Sci.toRealFloat Scientific
s)
{-# DEPRECATED number "Use 'scientific' instead." #-}
scientific :: Parser Scientific
scientific :: Parser Scientific
scientific = (Scientific -> Scientific) -> Parser Scientific
forall a. (Scientific -> a) -> Parser a
scientifically Scientific -> Scientific
forall a. a -> a
id
data SP = SP !Integer {-# UNPACK #-}!Int
{-# INLINE scientifically #-}
scientifically :: (Scientific -> a) -> Parser a
scientifically :: (Scientific -> a) -> Parser a
scientifically h :: Scientific -> a
h = do
let minus :: Word8
minus = 45
plus :: Word8
plus = 43
Word8
sign <- Parser ByteString Word8
I.peekWord8'
let !positive :: Bool
positive = Word8
sign Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
plus Bool -> Bool -> Bool
|| Word8
sign Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
minus
Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word8
sign Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
plus Bool -> Bool -> Bool
|| Word8
sign Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
minus) (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$
Parser ByteString Word8 -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Word8 -> Parser ())
-> Parser ByteString Word8 -> Parser ()
forall a b. (a -> b) -> a -> b
$ Parser ByteString Word8
I.anyWord8
Integer
n <- Parser Integer
forall a. Integral a => Parser a
decimal
let f :: ByteString -> SP
f fracDigits :: ByteString
fracDigits = Integer -> Int -> SP
SP ((Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B8.foldl' Integer -> Word8 -> Integer
forall a a. (Integral a, Num a) => a -> a -> a
step Integer
n ByteString
fracDigits)
(Int -> Int
forall a. Num a => a -> a
negate (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
B8.length ByteString
fracDigits)
step :: a -> a -> a
step a :: a
a w :: a
w = a
a a -> a -> a
forall a. Num a => a -> a -> a
* 10 a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- 48)
Maybe Word8
dotty <- Parser ByteString (Maybe Word8)
I.peekWord8
SP c :: Integer
c e :: Int
e <- case Maybe Word8
dotty of
Just 46 -> Parser ByteString Word8
I.anyWord8 Parser ByteString Word8
-> Parser ByteString SP -> Parser ByteString SP
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (ByteString -> SP
f (ByteString -> SP) -> Parser ByteString -> Parser ByteString SP
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word8 -> Bool) -> Parser ByteString
I.takeWhile Word8 -> Bool
isDigit_w8)
_ -> SP -> Parser ByteString SP
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Int -> SP
SP Integer
n 0)
let !signedCoeff :: Integer
signedCoeff | Bool
positive = Integer
c
| Bool
otherwise = -Integer
c
let littleE :: Word8
littleE = 101
bigE :: Word8
bigE = 69
((Word8 -> Bool) -> Parser ByteString Word8
I.satisfy (\ex :: Word8
ex -> Word8
ex Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
littleE Bool -> Bool -> Bool
|| Word8
ex Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
bigE) Parser ByteString Word8 -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
(Int -> a) -> Parser ByteString Int -> Parser a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Scientific -> a
h (Scientific -> a) -> (Int -> Scientific) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Int -> Scientific
Sci.scientific Integer
signedCoeff (Int -> Scientific) -> (Int -> Int) -> Int -> Scientific
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
+)) (Parser ByteString Int -> Parser ByteString Int
forall a. Num a => Parser a -> Parser a
signed Parser ByteString Int
forall a. Integral a => Parser a
decimal)) Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Scientific -> a
h (Scientific -> a) -> Scientific -> a
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Scientific
Sci.scientific Integer
signedCoeff Int
e)