-- | Token definitions.
module Data.Aeson.Decoding.Tokens (
    -- * Types
    Tokens (..),
    Lit (..),
    Number (..),
    TkArray (..),
    TkRecord (..),
) where

import           Data.Aeson.Key            (Key)
import           Data.Bifoldable           (Bifoldable (..))
import           Data.Bifunctor            (Bifunctor (..))
import           Data.Bitraversable        (Bitraversable (..), bifoldMapDefault, bimapDefault)
import           Data.Scientific           (Scientific)
import           Data.Text                 (Text)

-- | A well-formed JSON token stream.
--
-- /Note/: 'Lit' exists to make 'Tokens' have only 6 constructors.
-- This may or may not have impact on performance.
--
-- @since 2.1.2.0
--
data Tokens k e
    = TkLit !Lit k
    | TkText !Text k
    | TkNumber !Number k
    | TkArrayOpen (TkArray k e)
    | TkRecordOpen (TkRecord k e)
    | TkErr e
  deriving (Tokens k e -> Tokens k e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k e. (Eq k, Eq e) => Tokens k e -> Tokens k e -> Bool
/= :: Tokens k e -> Tokens k e -> Bool
$c/= :: forall k e. (Eq k, Eq e) => Tokens k e -> Tokens k e -> Bool
== :: Tokens k e -> Tokens k e -> Bool
$c== :: forall k e. (Eq k, Eq e) => Tokens k e -> Tokens k e -> Bool
Eq, Int -> Tokens k e -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k e. (Show k, Show e) => Int -> Tokens k e -> ShowS
forall k e. (Show k, Show e) => [Tokens k e] -> ShowS
forall k e. (Show k, Show e) => Tokens k e -> String
showList :: [Tokens k e] -> ShowS
$cshowList :: forall k e. (Show k, Show e) => [Tokens k e] -> ShowS
show :: Tokens k e -> String
$cshow :: forall k e. (Show k, Show e) => Tokens k e -> String
showsPrec :: Int -> Tokens k e -> ShowS
$cshowsPrec :: forall k e. (Show k, Show e) => Int -> Tokens k e -> ShowS
Show)

-- | Literals. @null@, @true@, @false@.
data Lit = LitNull | LitTrue | LitFalse
  deriving (Lit -> Lit -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Lit -> Lit -> Bool
$c/= :: Lit -> Lit -> Bool
== :: Lit -> Lit -> Bool
$c== :: Lit -> Lit -> Bool
Eq, Int -> Lit -> ShowS
[Lit] -> ShowS
Lit -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Lit] -> ShowS
$cshowList :: [Lit] -> ShowS
show :: Lit -> String
$cshow :: Lit -> String
showsPrec :: Int -> Lit -> ShowS
$cshowsPrec :: Int -> Lit -> ShowS
Show)

-- | Numbers
--
-- We preserve whether the number was integral, decimal or in scientific form.
data Number
    = NumInteger !Integer  -- ^ e.g. @123@
    | NumDecimal !Scientific  -- ^ e.g. @123.456@
    | NumScientific !Scientific -- ^ e.g. @123e456@, @123e-456@ or @123.456E-967@
  deriving (Number -> Number -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Number -> Number -> Bool
$c/= :: Number -> Number -> Bool
== :: Number -> Number -> Bool
$c== :: Number -> Number -> Bool
Eq, Int -> Number -> ShowS
[Number] -> ShowS
Number -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Number] -> ShowS
$cshowList :: [Number] -> ShowS
show :: Number -> String
$cshow :: Number -> String
showsPrec :: Int -> Number -> ShowS
$cshowsPrec :: Int -> Number -> ShowS
Show)

-- | Array tokens.
data TkArray k e
    = TkItem (Tokens (TkArray k e) e)
    | TkArrayEnd k
    | TkArrayErr e
  deriving (TkArray k e -> TkArray k e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k e. (Eq k, Eq e) => TkArray k e -> TkArray k e -> Bool
/= :: TkArray k e -> TkArray k e -> Bool
$c/= :: forall k e. (Eq k, Eq e) => TkArray k e -> TkArray k e -> Bool
== :: TkArray k e -> TkArray k e -> Bool
$c== :: forall k e. (Eq k, Eq e) => TkArray k e -> TkArray k e -> Bool
Eq, Int -> TkArray k e -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k e. (Show k, Show e) => Int -> TkArray k e -> ShowS
forall k e. (Show k, Show e) => [TkArray k e] -> ShowS
forall k e. (Show k, Show e) => TkArray k e -> String
showList :: [TkArray k e] -> ShowS
$cshowList :: forall k e. (Show k, Show e) => [TkArray k e] -> ShowS
show :: TkArray k e -> String
$cshow :: forall k e. (Show k, Show e) => TkArray k e -> String
showsPrec :: Int -> TkArray k e -> ShowS
$cshowsPrec :: forall k e. (Show k, Show e) => Int -> TkArray k e -> ShowS
Show)

-- | Record tokens.
data TkRecord k e
    = TkPair !Key (Tokens (TkRecord k e) e)
    | TkRecordEnd k
    | TkRecordErr e
  deriving (TkRecord k e -> TkRecord k e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k e. (Eq k, Eq e) => TkRecord k e -> TkRecord k e -> Bool
/= :: TkRecord k e -> TkRecord k e -> Bool
$c/= :: forall k e. (Eq k, Eq e) => TkRecord k e -> TkRecord k e -> Bool
== :: TkRecord k e -> TkRecord k e -> Bool
$c== :: forall k e. (Eq k, Eq e) => TkRecord k e -> TkRecord k e -> Bool
Eq, Int -> TkRecord k e -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k e. (Show k, Show e) => Int -> TkRecord k e -> ShowS
forall k e. (Show k, Show e) => [TkRecord k e] -> ShowS
forall k e. (Show k, Show e) => TkRecord k e -> String
showList :: [TkRecord k e] -> ShowS
$cshowList :: forall k e. (Show k, Show e) => [TkRecord k e] -> ShowS
show :: TkRecord k e -> String
$cshow :: forall k e. (Show k, Show e) => TkRecord k e -> String
showsPrec :: Int -> TkRecord k e -> ShowS
$cshowsPrec :: forall k e. (Show k, Show e) => Int -> TkRecord k e -> ShowS
Show)

instance Functor (Tokens k) where fmap :: forall a b. (a -> b) -> Tokens k a -> Tokens k b
fmap = forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second
instance Functor (TkArray k) where fmap :: forall a b. (a -> b) -> TkArray k a -> TkArray k b
fmap = forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second
instance Functor (TkRecord k) where fmap :: forall a b. (a -> b) -> TkRecord k a -> TkRecord k b
fmap = forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second

instance Foldable (Tokens k) where foldMap :: forall m a. Monoid m => (a -> m) -> Tokens k a -> m
foldMap = forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (forall a b. a -> b -> a
const forall a. Monoid a => a
mempty)
instance Foldable (TkArray k) where foldMap :: forall m a. Monoid m => (a -> m) -> TkArray k a -> m
foldMap = forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (forall a b. a -> b -> a
const forall a. Monoid a => a
mempty)
instance Foldable (TkRecord k) where foldMap :: forall m a. Monoid m => (a -> m) -> TkRecord k a -> m
foldMap = forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (forall a b. a -> b -> a
const forall a. Monoid a => a
mempty)

instance Traversable (Tokens k) where traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Tokens k a -> f (Tokens k b)
traverse = forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse forall (f :: * -> *) a. Applicative f => a -> f a
pure
instance Traversable (TkArray k) where traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TkArray k a -> f (TkArray k b)
traverse = forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse forall (f :: * -> *) a. Applicative f => a -> f a
pure
instance Traversable (TkRecord k) where traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TkRecord k a -> f (TkRecord k b)
traverse = forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse forall (f :: * -> *) a. Applicative f => a -> f a
pure

instance Bifunctor Tokens where bimap :: forall a b c d. (a -> b) -> (c -> d) -> Tokens a c -> Tokens b d
bimap = forall (t :: * -> * -> *) a b c d.
Bitraversable t =>
(a -> b) -> (c -> d) -> t a c -> t b d
bimapDefault
instance Bifunctor TkArray where bimap :: forall a b c d. (a -> b) -> (c -> d) -> TkArray a c -> TkArray b d
bimap = forall (t :: * -> * -> *) a b c d.
Bitraversable t =>
(a -> b) -> (c -> d) -> t a c -> t b d
bimapDefault
instance Bifunctor TkRecord where bimap :: forall a b c d.
(a -> b) -> (c -> d) -> TkRecord a c -> TkRecord b d
bimap = forall (t :: * -> * -> *) a b c d.
Bitraversable t =>
(a -> b) -> (c -> d) -> t a c -> t b d
bimapDefault

instance Bifoldable Tokens where bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> Tokens a b -> m
bifoldMap = forall (t :: * -> * -> *) m a b.
(Bitraversable t, Monoid m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault
instance Bifoldable TkArray where bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> TkArray a b -> m
bifoldMap = forall (t :: * -> * -> *) m a b.
(Bitraversable t, Monoid m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault
instance Bifoldable TkRecord where bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> TkRecord a b -> m
bifoldMap = forall (t :: * -> * -> *) m a b.
(Bitraversable t, Monoid m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault

instance Bitraversable Tokens where
    bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> Tokens a b -> f (Tokens c d)
bitraverse a -> f c
f b -> f d
_ (TkLit Lit
l a
k)       = forall k e. Lit -> k -> Tokens k e
TkLit Lit
l forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
k
    bitraverse a -> f c
f b -> f d
_ (TkText Text
t a
k)      = forall k e. Text -> k -> Tokens k e
TkText Text
t forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
k
    bitraverse a -> f c
f b -> f d
_ (TkNumber Number
n a
k)    = forall k e. Number -> k -> Tokens k e
TkNumber Number
n forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
k
    bitraverse a -> f c
f b -> f d
g (TkArrayOpen TkArray a b
ts)  = forall k e. TkArray k e -> Tokens k e
TkArrayOpen forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g TkArray a b
ts
    bitraverse a -> f c
f b -> f d
g (TkRecordOpen TkRecord a b
ts) = forall k e. TkRecord k e -> Tokens k e
TkRecordOpen forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g TkRecord a b
ts
    bitraverse a -> f c
_ b -> f d
g (TkErr b
e)         = forall k e. e -> Tokens k e
TkErr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f d
g b
e

instance Bitraversable TkArray where
    bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> TkArray a b -> f (TkArray c d)
bitraverse a -> f c
f b -> f d
g (TkItem Tokens (TkArray a b) b
ts)    = forall k e. Tokens (TkArray k e) e -> TkArray k e
TkItem forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse (forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g) b -> f d
g Tokens (TkArray a b) b
ts
    bitraverse a -> f c
f b -> f d
_ (TkArrayEnd a
k) = forall k e. k -> TkArray k e
TkArrayEnd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
k
    bitraverse a -> f c
_ b -> f d
g (TkArrayErr b
e) = forall k e. e -> TkArray k e
TkArrayErr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f d
g b
e

instance Bitraversable TkRecord where
    bitraverse :: forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> TkRecord a b -> f (TkRecord c d)
bitraverse a -> f c
f b -> f d
g (TkPair Key
k Tokens (TkRecord a b) b
ts)   = forall k e. Key -> Tokens (TkRecord k e) e -> TkRecord k e
TkPair Key
k forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse (forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> f c
f b -> f d
g) b -> f d
g Tokens (TkRecord a b) b
ts
    bitraverse a -> f c
f b -> f d
_ (TkRecordEnd a
k) = forall k e. k -> TkRecord k e
TkRecordEnd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f c
f a
k
    bitraverse a -> f c
_ b -> f d
g (TkRecordErr b
e) = forall k e. e -> TkRecord k e
TkRecordErr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> f d
g b
e