{-# OPTIONS_GHC -fno-warn-orphans #-}

-- |
-- Module      :  Data.Attoparsec.ByteString.Char8
-- Copyright   :  Bryan O'Sullivan 2007-2015
-- License     :  BSD3
--
-- Maintainer  :  bos@serpentine.com
-- Stability   :  experimental
-- Portability :  unknown
--
-- Simple, efficient, character-oriented combinator parsing for
-- 'B.ByteString' strings, loosely based on the Parsec library.

module Data.Attoparsec.ByteString.Char8
    (
    -- * Character encodings
    -- $encodings

    -- * Parser types
      Parser
    , DirParser
    , BackParser
    , MonoidalParser
    , A.Result
    , A.IResult(..)
    , I.compareResults

    -- * Running parsers
    , A.parse
    , A.feed
    , A.parseOnly
    , A.parseBackOnly
    , A.parseWith
    , A.parseTest

    -- ** Result conversion
    , A.maybeResult
    , A.eitherResult

    -- * Parsing individual characters
    , char
    , char8
    , anyChar
    , notChar
    , satisfy

    -- ** Lookahead
    , peekChar
    , peekChar'

    -- ** Special character parsers
    , digit
    , letter_iso8859_15
    , letter_ascii
    , space

    -- ** Fast predicates
    , isDigit
    , isDigit_w8
    , isAlpha_iso8859_15
    , isAlpha_ascii
    , isSpace
    , isSpace_w8

    -- *** Character classes
    , inClass
    , notInClass

    -- * Efficient string handling
    , I.string
    , I.stringCI
    , skipSpace
    , skipWhile
    , I.take
    , scan
    , takeWhile
    , takeWhile1
    , takeTill

    -- ** String combinators
    -- $specalt
    -- , (.*>)
    -- , (<*.)

    -- ** Consume all remaining input
    , I.takeByteString
    , I.takeLazyByteString

    -- * Text parsing
    , I.endOfLine
    , isEndOfLine
    , isHorizontalSpace

    -- * Numeric parsers
    , decimal
    , hexadecimal
    , signed
    , double
    , double'
    -- , Number(..)
    -- , number
    , rational
    , scientific
    , scientific'

    -- * Combinators
    , try
    , (<?>)
    , choice
    , count
    , option
    , many'
    , many1
    , many1'
    , manyTill
    , manyTill'
    , sepBy
    , sepBy'
    , sepBy1
    , sepBy1'
    , skipMany
    , skipMany1
    , eitherP
    , I.match
    -- * State observation and manipulation functions
    , I.endOfInput
    , I.atEnd
    ) where

import Control.Applicative (Alternative ((<|>)))
import Control.Monad (void, when)
import Debug.TraceEmbrace ( tw )
import Data.Attoparsec.ByteString.FastSet (charClass, memberChar)
import Data.Attoparsec.ByteString.Internal (DirParser, Parser, BackParser, DirectedTuple (..))
import Data.Attoparsec.Combinator
    ( many1', many', many1, try, (<?>), choice, option, sepBy, sepBy', sepBy1,
      sepBy1', manyTill, manyTill', skipMany, skipMany1, count, eitherP )
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

type Directed d = (I.Directed d, I.BsParserCon d, I.DirChunk d B.ByteString)

instance (Directed d, a ~ B.ByteString) => IsString (DirParser d a) where
    fromString :: String -> DirParser d a
fromString = ByteString -> DirParser d a
ByteString -> DirParser d ByteString
forall (d :: Dir).
BsParserCon d =>
ByteString -> DirParser d ByteString
I.string (ByteString -> DirParser d a)
-> (String -> ByteString) -> String -> DirParser d a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
B.pack

-- $encodings
--
-- This module is intended for parsing text that is
-- represented using an 8-bit character set, e.g. ASCII or
-- ISO-8859-15.  It /does not/ make any attempt to deal with character
-- encodings, multibyte characters, or wide characters.  In
-- particular, all attempts to use characters above code point U+00FF
-- will give wrong answers.
--
-- Code points below U+0100 are simply translated to and from their
-- numeric values, so e.g. the code point U+00A4 becomes the byte
-- @0xA4@ (which is the Euro symbol in ISO-8859-15, but the generic
-- currency sign in ISO-8859-1).  Haskell 'Char' values above U+00FF
-- are truncated, so e.g. U+1D6B7 is truncated to the byte @0xB7@.

-- | Consume input as long as the predicate returns 'True', and return
-- the consumed input.
--
-- This parser requires the predicate to succeed on at least one byte
-- of input: it will fail if the predicate never returns 'True' or if
-- there is no input left.
takeWhile1 :: Directed d => (Char -> Bool) -> DirParser d B.ByteString
takeWhile1 :: forall (d :: Dir).
Directed d =>
(Char -> Bool) -> DirParser d ByteString
takeWhile1 Char -> Bool
p = (Word8 -> Bool) -> DirParser d ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d 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 #-}

-- | The parser @satisfy p@ succeeds for any byte for which the
-- predicate @p@ returns 'True'. Returns the byte that is actually
-- parsed.
--
-- >digit = satisfy isDigit
-- >    where isDigit c = c >= '0' && c <= '9'
satisfy :: Directed d => (Char -> Bool) -> DirParser d Char
satisfy :: forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy = (Word8 -> Char) -> (Char -> Bool) -> DirParser d Char
forall a (d :: Dir).
(Show a, BsParserCon d) =>
(Word8 -> a) -> (a -> Bool) -> DirParser d a
I.satisfyWith Word8 -> Char
w2c
{-# INLINE satisfy #-}

-- | Match a letter, in the ISO-8859-15 encoding.
letter_iso8859_15 :: Directed d => DirParser d Char
letter_iso8859_15 :: forall (d :: Dir). Directed d => DirParser d Char
letter_iso8859_15 = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy Char -> Bool
isAlpha_iso8859_15 DirParser d Char -> String -> DirParser d Char
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> String
"letter_iso8859_15"
{-# INLINE letter_iso8859_15 #-}

-- | Match a letter, in the ASCII encoding.
letter_ascii :: Directed d => DirParser d Char
letter_ascii :: forall (d :: Dir). Directed d => DirParser d Char
letter_ascii = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy Char -> Bool
isAlpha_ascii DirParser d Char -> String -> DirParser d Char
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> String
"letter_ascii"
{-# INLINE letter_ascii #-}

-- | A fast alphabetic predicate for the ISO-8859-15 encoding
--
-- /Note/: For all character encodings other than ISO-8859-15, and
-- almost all Unicode code points above U+00A3, this predicate gives
-- /wrong answers/.
isAlpha_iso8859_15 :: Char -> Bool
isAlpha_iso8859_15 :: Char -> Bool
isAlpha_iso8859_15 Char
c = (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
'z') 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
'Z') Bool -> Bool -> Bool
||
                       (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'\166' Bool -> Bool -> Bool
&& Char -> Bool
moby Char
c)
  where moby :: Char -> Bool
moby = String -> Char -> Bool
notInClass String
"\167\169\171-\179\182\183\185\187\191\215\247"
        {-# NOINLINE moby #-}
{-# INLINE isAlpha_iso8859_15 #-}

-- | A fast alphabetic predicate for the ASCII encoding
--
-- /Note/: For all character encodings other than ASCII, and
-- almost all Unicode code points above U+007F, this predicate gives
-- /wrong answers/.
isAlpha_ascii :: Char -> Bool
isAlpha_ascii :: Char -> Bool
isAlpha_ascii Char
c = (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
'z') 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
'Z')
{-# INLINE isAlpha_ascii #-}

-- | Parse a single digit.
digit :: Directed d => DirParser d Char
digit :: forall (d :: Dir). Directed d => DirParser d Char
digit = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy Char -> Bool
isDigit DirParser d Char -> String -> DirParser d Char
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> String
"digit"
{-# INLINE digit #-}

-- | A fast digit predicate.
isDigit :: Char -> Bool
isDigit :: Char -> Bool
isDigit 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'
{-# INLINE isDigit #-}

-- | A fast digit predicate.
isDigit_w8 :: Word8 -> Bool
isDigit_w8 :: Word8 -> Bool
isDigit_w8 Word8
w = Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Word8
48 Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
9
{-# INLINE isDigit_w8 #-}

-- | Match any character.
anyChar :: Directed d => DirParser d Char
anyChar :: forall (d :: Dir). Directed d => DirParser d Char
anyChar = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy ((Char -> Bool) -> DirParser d Char)
-> (Char -> Bool) -> DirParser d Char
forall a b. (a -> b) -> a -> b
$ Bool -> Char -> Bool
forall a b. a -> b -> a
const Bool
True
{-# INLINE anyChar #-}

-- | Match any character, to perform lookahead. Returns 'Nothing' if
-- end of input has been reached. Does not consume any input.
--
-- /Note/: Because this parser does not fail, do not use it with
-- combinators such as 'many', because such parsers loop until a
-- failure occurs.  Careless use will thus result in an infinite loop.
peekChar :: Directed d => DirParser d (Maybe Char)
peekChar :: forall (d :: Dir). Directed d => DirParser d (Maybe Char)
peekChar = ((Word8 -> Char) -> Maybe Word8 -> Maybe Char
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word8 -> Char
w2c) (Maybe Word8 -> Maybe Char)
-> DirParser d ByteString (Maybe Word8)
-> DirParser d ByteString (Maybe Char)
forall a b.
(a -> b) -> DirParser d ByteString a -> DirParser d ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` DirParser d ByteString (Maybe Word8)
forall (d :: Dir). BsParserCon d => DirParser d (Maybe Word8)
I.peekWord8
{-# INLINE peekChar #-}

-- | Match any character, to perform lookahead.  Does not consume any
-- input, but will fail if end of input has been reached.
peekChar' :: Directed d => DirParser d Char
peekChar' :: forall (d :: Dir). Directed d => DirParser d Char
peekChar' = Word8 -> Char
w2c (Word8 -> Char)
-> DirParser d ByteString Word8 -> DirParser d ByteString Char
forall a b.
(a -> b) -> DirParser d ByteString a -> DirParser d ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` DirParser d ByteString Word8
forall (d :: Dir). BsParserCon d => DirParser d Word8
I.peekWord8'
{-# INLINE peekChar' #-}

-- | Fast predicate for matching ASCII space characters.
--
-- /Note/: This predicate only gives correct answers for the ASCII
-- encoding.  For instance, it does not recognise U+00A0 (non-breaking
-- space) as a space character, even though it is a valid ISO-8859-15
-- byte. For a Unicode-aware and only slightly slower predicate,
-- use 'Data.Char.isSpace'
isSpace :: Char -> Bool
isSpace :: Char -> Bool
isSpace Char
c = (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') Bool -> Bool -> Bool
|| (Char
'\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
<= Char
'\r')
{-# INLINE isSpace #-}

-- | Fast 'Word8' predicate for matching ASCII space characters.
isSpace_w8 :: Word8 -> Bool
isSpace_w8 :: Word8 -> Bool
isSpace_w8 Word8
w = Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
32 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Word8
9 Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
4
{-# INLINE isSpace_w8 #-}

-- | Parse a space character.
--
-- /Note/: This parser only gives correct answers for the ASCII
-- encoding.  For instance, it does not recognise U+00A0 (non-breaking
-- space) as a space character, even though it is a valid ISO-8859-15
-- byte.
space :: Directed d => DirParser d Char
space :: forall (d :: Dir). Directed d => DirParser d Char
space = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy Char -> Bool
isSpace DirParser d Char -> String -> DirParser d Char
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> String
"space"
{-# INLINE space #-}

-- | Match a specific character.
char :: Directed d => Char -> DirParser d Char
char :: forall (d :: Dir). Directed d => Char -> DirParser d Char
char Char
c = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c) DirParser d Char -> String -> DirParser d Char
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> [Char
c]
{-# INLINE char #-}

-- | Match a specific character, but return its 'Word8' value.
char8 :: Directed d => Char -> DirParser d Word8
char8 :: forall (d :: Dir). Directed d => Char -> DirParser d Word8
char8 Char
c = (Word8 -> Bool) -> DirParser d Word8
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d Word8
I.satisfy (\Word8
b -> $(tw "b is /b c") (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Word8
b Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
c) DirParser d Word8 -> String -> DirParser d Word8
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> [Char
c]
{-# INLINE char8 #-}

-- | Match any character except the given one.
notChar :: Directed d => Char -> DirParser d Char
notChar :: forall (d :: Dir). Directed d => Char -> DirParser d Char
notChar Char
c = (Char -> Bool) -> DirParser d Char
forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d Char
satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
c) DirParser d Char -> String -> DirParser d Char
forall (d :: Dir) i a. DirParser d i a -> String -> DirParser d i a
<?> String
"not " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Char
c]
{-# INLINE notChar #-}

-- | Match any character in a set.
--
-- >vowel = inClass "aeiou"
--
-- Range notation is supported.
--
-- >halfAlphabet = inClass "a-nA-N"
--
-- To add a literal \'-\' to a set, place it at the beginning or end
-- of the string.
inClass :: String -> Char -> Bool
inClass :: String -> Char -> Bool
inClass String
s = (Char -> FastSet -> Bool
`memberChar` FastSet
mySet)
    where mySet :: FastSet
mySet = String -> FastSet
charClass String
s
{-# INLINE inClass #-}

-- | Match any character not in a set.
notInClass :: String -> Char -> Bool
notInClass :: String -> Char -> Bool
notInClass 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 #-}

-- | Consume input as long as the predicate returns 'True', and return
-- the consumed input.
--
-- This parser does not fail.  It will return an empty string if the
-- predicate returns 'False' on the first byte of input.
--
-- /Note/: Because this parser does not fail, do not use it with
-- combinators such as 'many', because such parsers loop until a
-- failure occurs.  Careless use will thus result in an infinite loop.
takeWhile :: Directed d => (Char -> Bool) -> DirParser d B.ByteString
takeWhile :: forall (d :: Dir).
Directed d =>
(Char -> Bool) -> DirParser d ByteString
takeWhile Char -> Bool
p = (Word8 -> Bool) -> DirParser d ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d 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 #-}

-- | A stateful scanner.  The predicate consumes and transforms a
-- state argument, and each transformed state is passed to successive
-- invocations of the predicate on each byte of the input until one
-- returns 'Nothing' or the input ends.
--
-- This parser does not fail.  It will return an empty string if the
-- predicate returns 'Nothing' on the first byte of input.
--
-- /Note/: Because this parser does not fail, do not use it with
-- combinators such as 'many', because such parsers loop until a
-- failure occurs.  Careless use will thus result in an infinite loop.
scan :: (Show s, Directed d) => s -> (s -> Char -> Maybe s) -> DirParser d B.ByteString
scan :: forall s (d :: Dir).
(Show s, Directed d) =>
s -> (s -> Char -> Maybe s) -> DirParser d ByteString
scan s
s0 s -> Char -> Maybe s
p = s -> (s -> Word8 -> Maybe s) -> DirParser d ByteString
forall s (d :: Dir).
(Show s, BsParserCon d) =>
s -> (s -> Word8 -> Maybe s) -> DirParser d ByteString
I.scan s
s0 (\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 #-}

-- | Consume input as long as the predicate returns 'False'
-- (i.e. until it returns 'True'), and return the consumed input.
--
-- This parser does not fail.  It will return an empty string if the
-- predicate returns 'True' on the first byte of input.
--
-- /Note/: Because this parser does not fail, do not use it with
-- combinators such as 'many', because such parsers loop until a
-- failure occurs.  Careless use will thus result in an infinite loop.
takeTill :: Directed d => (Char -> Bool) -> DirParser d B.ByteString
takeTill :: forall (d :: Dir).
Directed d =>
(Char -> Bool) -> DirParser d ByteString
takeTill Char -> Bool
p = (Word8 -> Bool) -> DirParser d ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d 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 #-}

-- | Skip past input for as long as the predicate returns 'True'.
skipWhile :: Directed d => (Char -> Bool) -> DirParser d ()
skipWhile :: forall (d :: Dir). Directed d => (Char -> Bool) -> DirParser d ()
skipWhile Char -> Bool
p = (Word8 -> Bool) -> DirParser d ()
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d ()
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 #-}

-- | Skip over white space.
skipSpace :: Directed d => DirParser d ()
skipSpace :: forall (d :: Dir). Directed d => DirParser d ()
skipSpace = (Word8 -> Bool) -> DirParser d ()
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d ()
I.skipWhile Word8 -> Bool
isSpace_w8
{-# INLINE skipSpace #-}

-- $specalt
--
-- If you enable the @OverloadedStrings@ language extension, you can
-- use the '*>' and '<*' combinators to simplify the common task of
-- matching a statically known string, then immediately parsing
-- something else.
--
-- Instead of writing something like this:
--
-- @
--'I.string' \"foo\" '*>' wibble
-- @
--
-- Using @OverloadedStrings@, you can omit the explicit use of
-- 'I.string', and write a more compact version:
--
-- @
-- \"foo\" '*>' wibble
-- @
--
-- (Note: the '.*>' and '<*.' combinators that were originally
-- provided for this purpose are obsolete and unnecessary, and will be
-- removed in the next major version.)

-- | A predicate that matches either a carriage return @\'\\r\'@ or
-- newline @\'\\n\'@ character.
isEndOfLine :: Word8 -> Bool
isEndOfLine :: Word8 -> Bool
isEndOfLine Word8
w = Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
13 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
10
{-# INLINE isEndOfLine #-}

-- | A predicate that matches either a space @\' \'@ or horizontal tab
-- @\'\\t\'@ character.
isHorizontalSpace :: Word8 -> Bool
isHorizontalSpace :: Word8 -> Bool
isHorizontalSpace Word8
w = Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
32 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
9
{-# INLINE isHorizontalSpace #-}

-- | Parse and decode an unsigned hexadecimal number.  The hex digits
-- @\'a\'@ through @\'f\'@ may be upper or lower case.
--
-- This parser does not accept a leading @\"0x\"@ string.
hexadecimal :: (Directed d, Integral a, Bits a) => DirParser d a
hexadecimal :: forall (d :: Dir) a.
(Directed d, Integral a, Bits a) =>
DirParser d 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 a
0 (ByteString -> a)
-> DirParser d ByteString ByteString -> DirParser d ByteString a
forall a b.
(a -> b) -> DirParser d ByteString a -> DirParser d ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Word8 -> Bool) -> DirParser d ByteString ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d ByteString
I.takeWhile1 Word8 -> Bool
forall {a}. (Ord a, Num a) => a -> Bool
isHexDigit
  where
    isHexDigit :: a -> Bool
isHexDigit a
w = (a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
48 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
57) Bool -> Bool -> Bool
||
                   (a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
97 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
102) Bool -> Bool -> Bool
||
                   (a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
65 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
70)
    step :: a -> a -> a
step a
a a
w | a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
48 Bool -> Bool -> Bool
&& a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
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
.|. a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- a
48)
             | a
w a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
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
.|. a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => a -> a -> a
- a
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
.|. a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
w a -> a -> a
forall a. Num a => 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 #-}

{-# SPECIALISE hexadecimal :: BackParser Int #-}
{-# SPECIALISE hexadecimal :: BackParser Int8 #-}
{-# SPECIALISE hexadecimal :: BackParser Int16 #-}
{-# SPECIALISE hexadecimal :: BackParser Int32 #-}
{-# SPECIALISE hexadecimal :: BackParser Int64 #-}
{-# SPECIALISE hexadecimal :: BackParser Integer #-}
{-# SPECIALISE hexadecimal :: BackParser Word #-}
{-# SPECIALISE hexadecimal :: BackParser Word8 #-}
{-# SPECIALISE hexadecimal :: BackParser Word16 #-}
{-# SPECIALISE hexadecimal :: BackParser Word32 #-}
{-# SPECIALISE hexadecimal :: BackParser Word64 #-}

-- | Parse and decode an unsigned decimal number.
decimal :: (Directed d, Integral a) => DirParser d a
decimal :: forall (d :: Dir) a. (Directed d, Integral a) => DirParser d 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 a
0 (ByteString -> a)
-> DirParser d ByteString ByteString -> DirParser d ByteString a
forall a b.
(a -> b) -> DirParser d ByteString a -> DirParser d ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Word8 -> Bool) -> DirParser d ByteString ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d ByteString
I.takeWhile1 (\Word8
c -> $(tw "isDigit/c") (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Word8 -> Bool
isDigit_w8 Word8
c)
  where step :: a -> a -> a
step a
a a
w = a
a a -> a -> a
forall a. Num a => 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
- 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 #-}

{-# SPECIALISE decimal :: BackParser Int #-}
{-# SPECIALISE decimal :: BackParser Int8 #-}
{-# SPECIALISE decimal :: BackParser Int16 #-}
{-# SPECIALISE decimal :: BackParser Int32 #-}
{-# SPECIALISE decimal :: BackParser Int64 #-}
{-# SPECIALISE decimal :: BackParser Integer #-}
{-# SPECIALISE decimal :: BackParser Word #-}
{-# SPECIALISE decimal :: BackParser Word8 #-}
{-# SPECIALISE decimal :: BackParser Word16 #-}
{-# SPECIALISE decimal :: BackParser Word32 #-}
{-# SPECIALISE decimal :: BackParser Word64 #-}

-- | Parse a number with an optional leading @\'+\'@ or @\'-\'@ sign
-- character.
signed :: (Directed d, I.DirectedTuple d, Num a) => DirParser d a -> DirParser d 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 #-}

{-# SPECIALISE signed :: BackParser Int -> BackParser Int #-}
{-# SPECIALISE signed :: BackParser Int8 -> BackParser Int8 #-}
{-# SPECIALISE signed :: BackParser Int16 -> BackParser Int16 #-}
{-# SPECIALISE signed :: BackParser Int32 -> BackParser Int32 #-}
{-# SPECIALISE signed :: BackParser Int64 -> BackParser Int64 #-}
{-# SPECIALISE signed :: BackParser Integer -> BackParser Integer #-}
signed :: forall (d :: Dir) a.
(Directed d, DirectedTuple d, Num a) =>
DirParser d a -> DirParser d a
signed DirParser d a
p = (a -> a
forall a. Num a => a -> a
negate (a -> a) -> ((Word8, a) -> a) -> (Word8, a) -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8, a) -> a
forall a b. (a, b) -> b
snd ((Word8, a) -> a)
-> DirParser d ByteString (Word8, a) -> DirParser d a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> DirParser d Word8
forall (d :: Dir). Directed d => Char -> DirParser d Word8
char8 Char
'-' DirParser d Word8
-> DirParser d a -> DirParser d ByteString (Word8, a)
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< DirParser d a
p))
       DirParser d a -> DirParser d a -> DirParser d a
forall a.
DirParser d ByteString a
-> DirParser d ByteString a -> DirParser d ByteString a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Word8, a) -> a
forall a b. (a, b) -> b
snd ((Word8, a) -> a)
-> DirParser d ByteString (Word8, a) -> DirParser d a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> DirParser d Word8
forall (d :: Dir). Directed d => Char -> DirParser d Word8
char8 Char
'+' DirParser d Word8
-> DirParser d a -> DirParser d ByteString (Word8, a)
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< DirParser d a
p))
       DirParser d a -> DirParser d a -> DirParser d a
forall a.
DirParser d ByteString a
-> DirParser d ByteString a -> DirParser d ByteString a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> DirParser d a
p

type MonoidalParser d = (I.DirChunk d B.ByteString, DirectedTuple d, Directed d, Alternative (DirParser d))

-- | Parse a rational number.
--
-- The syntax accepted by this parser is the same as for 'double'.
--
-- /Note/: this parser is not safe for use with inputs from untrusted
-- sources.  An input with a suitably large exponent such as
-- @"1e1000000000"@ will cause a huge 'Integer' to be allocated,
-- resulting in what is effectively a denial-of-service attack.
--
-- In most cases, it is better to use 'double' or 'scientific'
-- instead.
rational :: (Fractional a, MonoidalParser d) => DirParser d a
{-# SPECIALIZE rational :: Parser Double #-}
{-# SPECIALIZE rational :: Parser Float #-}
{-# SPECIALIZE rational :: Parser Rational #-}
{-# SPECIALIZE rational :: Parser Scientific #-}

{-# SPECIALIZE rational :: BackParser Double #-}
{-# SPECIALIZE rational :: BackParser Float #-}
{-# SPECIALIZE rational :: BackParser Rational #-}
{-# SPECIALIZE rational :: BackParser Scientific #-}
rational :: forall a (d :: Dir).
(Fractional a, MonoidalParser d) =>
DirParser d a
rational = (Scientific -> a) -> DirParser d a
forall (d :: Dir) a.
MonoidalParser d =>
(Scientific -> a) -> DirParser d a
scientifically Scientific -> a
forall a b. (Real a, Fractional b) => a -> b
realToFrac

-- | Parse a 'Double'.
--
-- This parser accepts an optional leading sign character, followed by
-- at most one decimal digit.  The syntax is similar to that accepted by
-- the 'read' function, with the exception that a trailing @\'.\'@ is
 -- consumed.
--
-- === Examples
--
-- These examples use this helper:
--
-- @
-- r :: 'Parser' a -> 'Data.ByteString.ByteString' -> 'Data.Attoparsec.ByteString.Result' a
-- r p s = 'feed' ('Data.Attoparsec.parse' p s) 'mempty'
-- @
--
-- Examples with behaviour identical to 'read', if you feed an empty
-- continuation to the first result:
--
-- > double "3"     == Done "" 3.0
-- > double "3.1"   == Done "" 3.1
-- > double "3e4"   == Done "" 30000.0
-- > double "3.1e4" == Done "" 31000.0
-- > double "3e"    == Done "e" 3.0
--
-- Examples with behaviour identical to 'read':
--
-- > double ".3"    == Fail ".3" _ _
-- > double "e3"    == Fail "e3" _ _
--
-- Example of difference from 'read':
--
-- > double "3.foo" == Done "foo" 3.0
--
-- This function does not accept string representations of \"NaN\" or
-- \"Infinity\".
double :: MonoidalParser d => DirParser d Double
double :: forall (d :: Dir). MonoidalParser d => DirParser d Double
double = (Scientific -> Double) -> DirParser d Double
forall (d :: Dir) a.
MonoidalParser d =>
(Scientific -> a) -> DirParser d a
scientifically Scientific -> Double
forall a. RealFloat a => Scientific -> a
Sci.toRealFloat
{-# INLINE double #-}

-- | Original attoparsec implementation
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
{-# INLINE double' #-}

-- | Parse a scientific number.
--
-- The syntax accepted by this parser is the same as for 'double'.
scientific :: MonoidalParser d => DirParser d Scientific
scientific :: forall (d :: Dir). MonoidalParser d => DirParser d Scientific
scientific = (Scientific -> Scientific) -> DirParser d Scientific
forall (d :: Dir) a.
MonoidalParser d =>
(Scientific -> a) -> DirParser d a
scientifically Scientific -> Scientific
forall a. a -> a
id

-- | Original attoparsec implementation
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

-- A strict pair
data SP = SP !Integer {-# UNPACK #-}!Int

{-# INLINE scientifically' #-}
scientifically' :: (Scientific -> a) -> Parser a
scientifically' :: forall a. (Scientific -> a) -> Parser a
scientifically' Scientific -> a
h = do
  let minus :: Word8
minus = Word8
45
      plus :: Word8
plus  = Word8
43
  sign <- DirParser 'Forward Word8
forall (d :: Dir). BsParserCon d => DirParser d Word8
I.peekWord8'
  let !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
  when (sign == plus || sign == minus) $
    void $ I.anyWord8

  n <- decimal

  let f 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
w = a
a a -> a -> a
forall a. Num a => 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
- a
48)

  dotty <- I.peekWord8
  -- '.' -> ascii 46
  SP c e <- case dotty of
              Just Word8
46 -> DirParser 'Forward Word8
forall (d :: Dir). BsParserCon d => DirParser d Word8
I.anyWord8 DirParser 'Forward Word8
-> DirParser 'Forward ByteString SP
-> DirParser 'Forward ByteString SP
forall a b.
DirParser 'Forward ByteString a
-> DirParser 'Forward ByteString b
-> DirParser 'Forward ByteString b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (ByteString -> SP
f (ByteString -> SP)
-> DirParser 'Forward ByteString ByteString
-> DirParser 'Forward ByteString SP
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word8 -> Bool) -> DirParser 'Forward ByteString ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d ByteString
I.takeWhile Word8 -> Bool
isDigit_w8)
              Maybe Word8
_       -> SP -> DirParser 'Forward ByteString SP
forall a. a -> DirParser 'Forward ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Int -> SP
SP Integer
n Int
0)

  let !signedCoeff | Bool
positive  =  Integer
c
                   | Bool
otherwise = -Integer
c

  let littleE = Word8
101
      bigE    = Word8
69
  (I.satisfy (\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) *>
      fmap (h . Sci.scientific signedCoeff . (e +)) (signed decimal)) <|>
    return (h $ Sci.scientific signedCoeff    e)

{-# INLINE scientifically #-}
scientifically :: MonoidalParser d => (Scientific -> a) -> DirParser d a
scientifically :: forall (d :: Dir) a.
MonoidalParser d =>
(Scientific -> a) -> DirParser d a
scientifically Scientific -> a
h =
  DirParser d (Integer, (Integer, Maybe (ByteString, Maybe Int)))
num DirParser d (Integer, (Integer, Maybe (ByteString, Maybe Int)))
-> ((Integer, (Integer, Maybe (ByteString, Maybe Int)))
    -> DirParser d ByteString a)
-> DirParser d ByteString a
forall a b.
DirParser d ByteString a
-> (a -> DirParser d ByteString b) -> DirParser d ByteString b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  (Integer
s, (Integer
i, Maybe (ByteString, Maybe Int)
mCoefE)) ->
      case Maybe (ByteString, Maybe Int)
mCoefE of
        Maybe (ByteString, Maybe Int)
Nothing -> a -> DirParser d ByteString a
forall a. a -> DirParser d ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Scientific -> a
h (Scientific -> a) -> Scientific -> a
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Scientific
Sci.scientific (Integer
sInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
i) Int
0) -- no dot
        Just (ByteString
fraqDigits, Maybe Int
mE) ->
          case Maybe Int
mE of
            Maybe Int
Nothing ->  -- no exp
              a -> DirParser d ByteString a
forall a. a -> DirParser d ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Scientific -> a
h (Scientific -> a) -> Scientific -> a
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Scientific
Sci.scientific (Integer
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer -> ByteString -> Integer
forall {a}. Num a => a -> ByteString -> a
cDot Integer
i ByteString
fraqDigits)) (ByteString -> Int
eDot ByteString
fraqDigits))
            Just Int
e ->
              a -> DirParser d ByteString a
forall a. a -> DirParser d ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Scientific -> a
h (Scientific -> a) -> Scientific -> a
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Scientific
Sci.scientific (Integer
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer -> ByteString -> Integer
forall {a}. Num a => a -> ByteString -> a
cDot Integer
i ByteString
fraqDigits)) (Int
e Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
eDot ByteString
fraqDigits))
  where
    cDot :: a -> ByteString -> a
cDot a
n ByteString
fracDigits = (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 a
n ByteString
fracDigits
    eDot :: ByteString -> Int
eDot 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
a a -> a -> a
forall a. Num a => 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
- a
48)
    signInt :: DirParser d ByteString Integer
signInt = Integer
-> DirParser d ByteString Integer -> DirParser d ByteString Integer
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
option Integer
1 ((Char -> DirParser d Char
forall (d :: Dir). Directed d => Char -> DirParser d Char
char Char
'-' DirParser d Char
-> DirParser d ByteString Integer -> DirParser d ByteString Integer
forall a b.
DirParser d ByteString a
-> DirParser d ByteString b -> DirParser d ByteString b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Integer -> DirParser d ByteString Integer
forall a. a -> DirParser d ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (-Integer
1) DirParser d ByteString Integer
-> DirParser d ByteString Integer -> DirParser d ByteString Integer
forall a.
DirParser d ByteString a
-> DirParser d ByteString a -> DirParser d ByteString a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> DirParser d Char
forall (d :: Dir). Directed d => Char -> DirParser d Char
char Char
'+' DirParser d Char
-> DirParser d ByteString Integer -> DirParser d ByteString Integer
forall a b.
DirParser d ByteString a
-> DirParser d ByteString b -> DirParser d ByteString b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Integer -> DirParser d ByteString Integer
forall a. a -> DirParser d ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
1)))
    matchE :: DirParser d ByteString Int
matchE = (((), (Integer, Integer)) -> (Integer, Integer))
-> DirParser d ByteString ((), (Integer, Integer))
-> DirParser d ByteString (Integer, Integer)
forall a b.
(a -> b) -> DirParser d ByteString a -> DirParser d ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((), (Integer, Integer)) -> (Integer, Integer)
forall a b. (a, b) -> b
snd (DirParser d Char -> DirParser d ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Char -> DirParser d Char
forall (d :: Dir). Directed d => Char -> DirParser d Char
char Char
'e' DirParser d Char -> DirParser d Char -> DirParser d Char
forall a.
DirParser d ByteString a
-> DirParser d ByteString a -> DirParser d ByteString a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Char -> DirParser d Char
forall (d :: Dir). Directed d => Char -> DirParser d Char
char Char
'E') DirParser d ByteString ()
-> DirParser d ByteString (Integer, Integer)
-> DirParser d ByteString ((), (Integer, Integer))
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< (DirParser d ByteString Integer
signInt DirParser d ByteString Integer
-> DirParser d ByteString Integer
-> DirParser d ByteString (Integer, Integer)
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< DirParser d ByteString Integer
forall (d :: Dir) a. (Directed d, Integral a) => DirParser d a
decimal)) DirParser d ByteString (Integer, Integer)
-> ((Integer, Integer) -> DirParser d ByteString Int)
-> DirParser d ByteString Int
forall a b.
DirParser d ByteString a
-> (a -> DirParser d ByteString b) -> DirParser d ByteString b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      (Integer
s, Integer
d) -> Int -> DirParser d ByteString Int
forall a. a -> DirParser d ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> DirParser d ByteString Int)
-> (Integer -> Int) -> Integer -> DirParser d ByteString Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> DirParser d ByteString Int)
-> Integer -> DirParser d ByteString Int
forall a b. (a -> b) -> a -> b
$ Integer
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*  Integer
d
    eP :: DirParser d ByteString (Maybe Int)
eP = Maybe Int
-> DirParser d ByteString (Maybe Int)
-> DirParser d ByteString (Maybe Int)
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
option Maybe Int
forall a. Maybe a
Nothing (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int)
-> DirParser d ByteString Int -> DirParser d ByteString (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DirParser d ByteString Int
matchE)
    num :: DirParser d (Integer, (Integer, Maybe (ByteString, Maybe Int)))
num = DirParser d ByteString Integer
signInt DirParser d ByteString Integer
-> DirParser d (Integer, Maybe (ByteString, Maybe Int))
-> DirParser d (Integer, (Integer, Maybe (ByteString, Maybe Int)))
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< (DirParser d ByteString Integer
forall (d :: Dir) a. (Directed d, Integral a) => DirParser d a
decimal DirParser d ByteString Integer
-> DirParser d (Maybe (ByteString, Maybe Int))
-> DirParser d (Integer, Maybe (ByteString, Maybe Int))
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< Maybe (ByteString, Maybe Int)
-> DirParser d (Maybe (ByteString, Maybe Int))
-> DirParser d (Maybe (ByteString, Maybe Int))
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
option Maybe (ByteString, Maybe Int)
forall a. Maybe a
Nothing
                       ((ByteString, Maybe Int) -> Maybe (ByteString, Maybe Int)
forall a. a -> Maybe a
Just ((ByteString, Maybe Int) -> Maybe (ByteString, Maybe Int))
-> DirParser d ByteString (ByteString, Maybe Int)
-> DirParser d (Maybe (ByteString, Maybe Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (((Char, (ByteString, Maybe Int)) -> (ByteString, Maybe Int))
-> DirParser d ByteString (Char, (ByteString, Maybe Int))
-> DirParser d ByteString (ByteString, Maybe Int)
forall a b.
(a -> b) -> DirParser d ByteString a -> DirParser d ByteString b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Char, (ByteString, Maybe Int)) -> (ByteString, Maybe Int)
forall a b. (a, b) -> b
snd (Char -> DirParser d Char
forall (d :: Dir). Directed d => Char -> DirParser d Char
char Char
'.' DirParser d Char
-> DirParser d ByteString (ByteString, Maybe Int)
-> DirParser d ByteString (Char, (ByteString, Maybe Int))
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< (DirParser d ByteString
frac DirParser d ByteString
-> DirParser d ByteString (Maybe Int)
-> DirParser d ByteString (ByteString, Maybe Int)
forall a b. DirParser d a -> DirParser d b -> DirParser d (a, b)
forall (d :: Dir) a b.
DirectedTuple d =>
DirParser d a -> DirParser d b -> DirParser d (a, b)
>*< DirParser d ByteString (Maybe Int)
eP)))))
      where
        frac :: DirParser d ByteString
frac = (Word8 -> Bool) -> DirParser d ByteString
forall (d :: Dir).
BsParserCon d =>
(Word8 -> Bool) -> DirParser d ByteString
I.takeWhile Word8 -> Bool
isDigit_w8