{-# language DeriveFunctor #-}
{-# language DeriveFoldable #-}
{-# language DerivingStrategies #-}

module Data.Bytes.Parser.Types
  ( Parser
  , Result(..)
  , Slice(..)
  ) where

import Data.Bytes.Parser.Internal (Parser(..))

-- | The result of running a parser.
data Result e a
  = Failure e
    -- ^ An error message indicating what went wrong.
  | Success {-# UNPACK #-} !(Slice a)
    -- ^ The parsed value and the number of bytes
    -- remaining in parsed slice.
  deriving stock (Result e a -> Result e a -> Bool
(Result e a -> Result e a -> Bool)
-> (Result e a -> Result e a -> Bool) -> Eq (Result e a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e a. (Eq e, Eq a) => Result e a -> Result e a -> Bool
/= :: Result e a -> Result e a -> Bool
$c/= :: forall e a. (Eq e, Eq a) => Result e a -> Result e a -> Bool
== :: Result e a -> Result e a -> Bool
$c== :: forall e a. (Eq e, Eq a) => Result e a -> Result e a -> Bool
Eq,Int -> Result e a -> ShowS
[Result e a] -> ShowS
Result e a -> String
(Int -> Result e a -> ShowS)
-> (Result e a -> String)
-> ([Result e a] -> ShowS)
-> Show (Result e a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show e, Show a) => Int -> Result e a -> ShowS
forall e a. (Show e, Show a) => [Result e a] -> ShowS
forall e a. (Show e, Show a) => Result e a -> String
showList :: [Result e a] -> ShowS
$cshowList :: forall e a. (Show e, Show a) => [Result e a] -> ShowS
show :: Result e a -> String
$cshow :: forall e a. (Show e, Show a) => Result e a -> String
showsPrec :: Int -> Result e a -> ShowS
$cshowsPrec :: forall e a. (Show e, Show a) => Int -> Result e a -> ShowS
Show,Result e a -> Bool
(a -> m) -> Result e a -> m
(a -> b -> b) -> b -> Result e a -> b
(forall m. Monoid m => Result e m -> m)
-> (forall m a. Monoid m => (a -> m) -> Result e a -> m)
-> (forall m a. Monoid m => (a -> m) -> Result e a -> m)
-> (forall a b. (a -> b -> b) -> b -> Result e a -> b)
-> (forall a b. (a -> b -> b) -> b -> Result e a -> b)
-> (forall b a. (b -> a -> b) -> b -> Result e a -> b)
-> (forall b a. (b -> a -> b) -> b -> Result e a -> b)
-> (forall a. (a -> a -> a) -> Result e a -> a)
-> (forall a. (a -> a -> a) -> Result e a -> a)
-> (forall a. Result e a -> [a])
-> (forall a. Result e a -> Bool)
-> (forall a. Result e a -> Int)
-> (forall a. Eq a => a -> Result e a -> Bool)
-> (forall a. Ord a => Result e a -> a)
-> (forall a. Ord a => Result e a -> a)
-> (forall a. Num a => Result e a -> a)
-> (forall a. Num a => Result e a -> a)
-> Foldable (Result e)
forall a. Eq a => a -> Result e a -> Bool
forall a. Num a => Result e a -> a
forall a. Ord a => Result e a -> a
forall m. Monoid m => Result e m -> m
forall a. Result e a -> Bool
forall a. Result e a -> Int
forall a. Result e a -> [a]
forall a. (a -> a -> a) -> Result e a -> a
forall e a. Eq a => a -> Result e a -> Bool
forall e a. Num a => Result e a -> a
forall e a. Ord a => Result e a -> a
forall m a. Monoid m => (a -> m) -> Result e a -> m
forall e m. Monoid m => Result e m -> m
forall e a. Result e a -> Bool
forall e a. Result e a -> Int
forall e a. Result e a -> [a]
forall b a. (b -> a -> b) -> b -> Result e a -> b
forall a b. (a -> b -> b) -> b -> Result e a -> b
forall e a. (a -> a -> a) -> Result e a -> a
forall e m a. Monoid m => (a -> m) -> Result e a -> m
forall e b a. (b -> a -> b) -> b -> Result e a -> b
forall e a b. (a -> b -> b) -> b -> Result e a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Result e a -> a
$cproduct :: forall e a. Num a => Result e a -> a
sum :: Result e a -> a
$csum :: forall e a. Num a => Result e a -> a
minimum :: Result e a -> a
$cminimum :: forall e a. Ord a => Result e a -> a
maximum :: Result e a -> a
$cmaximum :: forall e a. Ord a => Result e a -> a
elem :: a -> Result e a -> Bool
$celem :: forall e a. Eq a => a -> Result e a -> Bool
length :: Result e a -> Int
$clength :: forall e a. Result e a -> Int
null :: Result e a -> Bool
$cnull :: forall e a. Result e a -> Bool
toList :: Result e a -> [a]
$ctoList :: forall e a. Result e a -> [a]
foldl1 :: (a -> a -> a) -> Result e a -> a
$cfoldl1 :: forall e a. (a -> a -> a) -> Result e a -> a
foldr1 :: (a -> a -> a) -> Result e a -> a
$cfoldr1 :: forall e a. (a -> a -> a) -> Result e a -> a
foldl' :: (b -> a -> b) -> b -> Result e a -> b
$cfoldl' :: forall e b a. (b -> a -> b) -> b -> Result e a -> b
foldl :: (b -> a -> b) -> b -> Result e a -> b
$cfoldl :: forall e b a. (b -> a -> b) -> b -> Result e a -> b
foldr' :: (a -> b -> b) -> b -> Result e a -> b
$cfoldr' :: forall e a b. (a -> b -> b) -> b -> Result e a -> b
foldr :: (a -> b -> b) -> b -> Result e a -> b
$cfoldr :: forall e a b. (a -> b -> b) -> b -> Result e a -> b
foldMap' :: (a -> m) -> Result e a -> m
$cfoldMap' :: forall e m a. Monoid m => (a -> m) -> Result e a -> m
foldMap :: (a -> m) -> Result e a -> m
$cfoldMap :: forall e m a. Monoid m => (a -> m) -> Result e a -> m
fold :: Result e m -> m
$cfold :: forall e m. Monoid m => Result e m -> m
Foldable,a -> Result e b -> Result e a
(a -> b) -> Result e a -> Result e b
(forall a b. (a -> b) -> Result e a -> Result e b)
-> (forall a b. a -> Result e b -> Result e a)
-> Functor (Result e)
forall a b. a -> Result e b -> Result e a
forall a b. (a -> b) -> Result e a -> Result e b
forall e a b. a -> Result e b -> Result e a
forall e a b. (a -> b) -> Result e a -> Result e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Result e b -> Result e a
$c<$ :: forall e a b. a -> Result e b -> Result e a
fmap :: (a -> b) -> Result e a -> Result e b
$cfmap :: forall e a b. (a -> b) -> Result e a -> Result e b
Functor)

-- | Slicing metadata (an offset and a length) accompanied
-- by a value. This does not represent a slice into the
-- value. This type is intended to be used as the result
-- of an executed parser. In this context the slicing metadata
-- describe a slice into to the array (or byte array) that
-- from which the value was parsed.
--
-- It is often useful to check the @length@ when a parser
-- succeeds since a non-zero length indicates that there
-- was additional unconsumed input. The @offset@ is only
-- ever needed to construct a new slice (via @Bytes@ or
-- @SmallVector@) from the remaining input.
data Slice a = Slice
  { Slice a -> Int
offset :: {-# UNPACK #-} !Int
    -- ^ Offset into the array.
  , Slice a -> Int
length :: {-# UNPACK #-} !Int
    -- ^ Length of the slice.
  , Slice a -> a
value :: a
    -- ^ The structured data that was successfully parsed.
  } deriving stock (Slice a -> Slice a -> Bool
(Slice a -> Slice a -> Bool)
-> (Slice a -> Slice a -> Bool) -> Eq (Slice a)
forall a. Eq a => Slice a -> Slice a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Slice a -> Slice a -> Bool
$c/= :: forall a. Eq a => Slice a -> Slice a -> Bool
== :: Slice a -> Slice a -> Bool
$c== :: forall a. Eq a => Slice a -> Slice a -> Bool
Eq,Int -> Slice a -> ShowS
[Slice a] -> ShowS
Slice a -> String
(Int -> Slice a -> ShowS)
-> (Slice a -> String) -> ([Slice a] -> ShowS) -> Show (Slice a)
forall a. Show a => Int -> Slice a -> ShowS
forall a. Show a => [Slice a] -> ShowS
forall a. Show a => Slice a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Slice a] -> ShowS
$cshowList :: forall a. Show a => [Slice a] -> ShowS
show :: Slice a -> String
$cshow :: forall a. Show a => Slice a -> String
showsPrec :: Int -> Slice a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Slice a -> ShowS
Show,Slice a -> Bool
(a -> m) -> Slice a -> m
(a -> b -> b) -> b -> Slice a -> b
(forall m. Monoid m => Slice m -> m)
-> (forall m a. Monoid m => (a -> m) -> Slice a -> m)
-> (forall m a. Monoid m => (a -> m) -> Slice a -> m)
-> (forall a b. (a -> b -> b) -> b -> Slice a -> b)
-> (forall a b. (a -> b -> b) -> b -> Slice a -> b)
-> (forall b a. (b -> a -> b) -> b -> Slice a -> b)
-> (forall b a. (b -> a -> b) -> b -> Slice a -> b)
-> (forall a. (a -> a -> a) -> Slice a -> a)
-> (forall a. (a -> a -> a) -> Slice a -> a)
-> (forall a. Slice a -> [a])
-> (forall a. Slice a -> Bool)
-> (forall a. Slice a -> Int)
-> (forall a. Eq a => a -> Slice a -> Bool)
-> (forall a. Ord a => Slice a -> a)
-> (forall a. Ord a => Slice a -> a)
-> (forall a. Num a => Slice a -> a)
-> (forall a. Num a => Slice a -> a)
-> Foldable Slice
forall a. Eq a => a -> Slice a -> Bool
forall a. Num a => Slice a -> a
forall a. Ord a => Slice a -> a
forall m. Monoid m => Slice m -> m
forall a. Slice a -> Bool
forall a. Slice a -> Int
forall a. Slice a -> [a]
forall a. (a -> a -> a) -> Slice a -> a
forall m a. Monoid m => (a -> m) -> Slice a -> m
forall b a. (b -> a -> b) -> b -> Slice a -> b
forall a b. (a -> b -> b) -> b -> Slice a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Slice a -> a
$cproduct :: forall a. Num a => Slice a -> a
sum :: Slice a -> a
$csum :: forall a. Num a => Slice a -> a
minimum :: Slice a -> a
$cminimum :: forall a. Ord a => Slice a -> a
maximum :: Slice a -> a
$cmaximum :: forall a. Ord a => Slice a -> a
elem :: a -> Slice a -> Bool
$celem :: forall a. Eq a => a -> Slice a -> Bool
length :: Slice a -> Int
$clength :: forall a. Slice a -> Int
null :: Slice a -> Bool
$cnull :: forall a. Slice a -> Bool
toList :: Slice a -> [a]
$ctoList :: forall a. Slice a -> [a]
foldl1 :: (a -> a -> a) -> Slice a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Slice a -> a
foldr1 :: (a -> a -> a) -> Slice a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Slice a -> a
foldl' :: (b -> a -> b) -> b -> Slice a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Slice a -> b
foldl :: (b -> a -> b) -> b -> Slice a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Slice a -> b
foldr' :: (a -> b -> b) -> b -> Slice a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Slice a -> b
foldr :: (a -> b -> b) -> b -> Slice a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Slice a -> b
foldMap' :: (a -> m) -> Slice a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Slice a -> m
foldMap :: (a -> m) -> Slice a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Slice a -> m
fold :: Slice m -> m
$cfold :: forall m. Monoid m => Slice m -> m
Foldable,a -> Slice b -> Slice a
(a -> b) -> Slice a -> Slice b
(forall a b. (a -> b) -> Slice a -> Slice b)
-> (forall a b. a -> Slice b -> Slice a) -> Functor Slice
forall a b. a -> Slice b -> Slice a
forall a b. (a -> b) -> Slice a -> Slice b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Slice b -> Slice a
$c<$ :: forall a b. a -> Slice b -> Slice a
fmap :: (a -> b) -> Slice a -> Slice b
$cfmap :: forall a b. (a -> b) -> Slice a -> Slice b
Functor)