{-# LANGUAGE OverloadedStrings #-}

module Data.JsonStream.TokenParser (
    Element(..)
  , TokenResult(..)
) where

import qualified Data.Aeson            as AE
import qualified Data.ByteString.Char8 as BS
import           Foreign.C.Types

data Element = 
    ArrayBegin
  | ArrayEnd !BS.ByteString -- Rest of the source string for correct (ParseDone <rest>)
  | ObjectBegin
  | ObjectEnd !BS.ByteString -- Rest of the source string for correct (ParseDone <rest>)
  | StringContent !BS.ByteString
  | StringRaw !BS.ByteString !Bool !BS.ByteString -- Allow raw strings to go into parser as bytestring/ isAscii
  | StringEnd !BS.ByteString
  | JValue !AE.Value
  | JInteger !CLong
  deriving (Int -> Element -> ShowS
[Element] -> ShowS
Element -> String
(Int -> Element -> ShowS)
-> (Element -> String) -> ([Element] -> ShowS) -> Show Element
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Element -> ShowS
showsPrec :: Int -> Element -> ShowS
$cshow :: Element -> String
show :: Element -> String
$cshowList :: [Element] -> ShowS
showList :: [Element] -> ShowS
Show, Element -> Element -> Bool
(Element -> Element -> Bool)
-> (Element -> Element -> Bool) -> Eq Element
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Element -> Element -> Bool
== :: Element -> Element -> Bool
$c/= :: Element -> Element -> Bool
/= :: Element -> Element -> Bool
Eq)

-- | Public interface for parsing JSON tokens.
data TokenResult =  TokMoreData (BS.ByteString -> TokenResult)
                  | PartialResult Element TokenResult
                  -- ^ found element, continuation, actual parsing view - so that we can report the unparsed
                  -- data when the parsing finishes.
                  | TokFailed

-- For debugging purposes
instance Show TokenResult where
  show :: TokenResult -> String
show (TokMoreData ByteString -> TokenResult
_) = String
"TokMoreData"
  show TokenResult
TokFailed = String
"TokFailed"
  show (PartialResult Element
el TokenResult
next) = String
"(PartialResult (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Element -> String
forall a. Show a => a -> String
show Element
el String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
") " String -> ShowS
forall a. [a] -> [a] -> [a]
++ TokenResult -> String
forall a. Show a => a -> String
show TokenResult
next String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"