{-# LANGUAGE BangPatterns  #-}
{-# LANGUAGE DeriveGeneric #-}

module HaskellWorks.Data.Json.Standard.Cursor.Internal.Blank
  ( blankJson
  ) where

import Data.ByteString                                       (ByteString)
import Data.Word
import Data.Word8
import GHC.Generics
import HaskellWorks.Data.Json.Standard.Cursor.Internal.Word8
import Prelude                                               as P

import qualified Data.ByteString as BS

data BlankState
  = Escaped
  | InJson
  | InString
  | InNumber
  | InIdent
  deriving (forall x. BlankState -> Rep BlankState x)
-> (forall x. Rep BlankState x -> BlankState) -> Generic BlankState
forall x. Rep BlankState x -> BlankState
forall x. BlankState -> Rep BlankState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BlankState x -> BlankState
$cfrom :: forall x. BlankState -> Rep BlankState x
Generic

blankJson :: [BS.ByteString] -> [BS.ByteString]
blankJson :: [ByteString] -> [ByteString]
blankJson = BlankState -> [ByteString] -> [ByteString]
blankJson' BlankState
InJson

blankJson' :: BlankState -> [BS.ByteString] -> [BS.ByteString]
blankJson' :: BlankState -> [ByteString] -> [ByteString]
blankJson' BlankState
lastState [ByteString]
as = case [ByteString]
as of
  (ByteString
bs:[ByteString]
bss) ->
      let (!ByteString
cs, Just (!BlankState
nextState, ByteString
_)) = Int
-> ((BlankState, ByteString)
    -> Maybe (Word8, (BlankState, ByteString)))
-> (BlankState, ByteString)
-> (ByteString, Maybe (BlankState, ByteString))
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
BS.unfoldrN (ByteString -> Int
BS.length ByteString
bs) (BlankState, ByteString) -> Maybe (Word8, (BlankState, ByteString))
blankByteString (BlankState
lastState, ByteString
bs) in
      ByteString
csByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:BlankState -> [ByteString] -> [ByteString]
blankJson' BlankState
nextState [ByteString]
bss
  [] -> []
  where
    blankByteString :: (BlankState, ByteString) -> Maybe (Word8, (BlankState, ByteString))
    blankByteString :: (BlankState, ByteString) -> Maybe (Word8, (BlankState, ByteString))
blankByteString (BlankState
InJson, ByteString
bs) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
      Just (!Word8
c, !ByteString
cs) | Word8 -> Bool
isLeadingDigit Word8
c -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_1          , (BlankState
InNumber , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_quotedbl   -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_parenleft  , (BlankState
InString , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8 -> Bool
isAlphabetic Word8
c   -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
c           , (BlankState
InIdent  , ByteString
cs))
      Just (!Word8
c, !ByteString
cs)                    -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
c           , (BlankState
InJson   , ByteString
cs))
      Maybe (Word8, ByteString)
Nothing                           -> Maybe (Word8, (BlankState, ByteString))
forall a. Maybe a
Nothing
    blankByteString (BlankState
InString, ByteString
bs) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
      Just (!Word8
c, !ByteString
cs) | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_backslash -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_space      , (BlankState
Escaped  , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_quotedbl  -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_parenright , (BlankState
InJson   , ByteString
cs))
      Just (Word8
_ , !ByteString
cs)                   -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_space      , (BlankState
InString , ByteString
cs))
      Maybe (Word8, ByteString)
Nothing                          -> Maybe (Word8, (BlankState, ByteString))
forall a. Maybe a
Nothing
    blankByteString (BlankState
Escaped, ByteString
bs) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
      Just (Word8
_, !ByteString
cs) -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_space, (BlankState
InString, ByteString
cs))
      Maybe (Word8, ByteString)
Nothing       -> Maybe (Word8, (BlankState, ByteString))
forall a. Maybe a
Nothing
    blankByteString (BlankState
InNumber, ByteString
bs) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
      Just (!Word8
c, !ByteString
cs) | Word8 -> Bool
isTrailingDigit Word8
c -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_0          , (BlankState
InNumber , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_quotedbl    -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_parenleft  , (BlankState
InString , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8 -> Bool
isAlphabetic Word8
c    -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
c           , (BlankState
InIdent  , ByteString
cs))
      Just (!Word8
c, !ByteString
cs)                     -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
c           , (BlankState
InJson   , ByteString
cs))
      Maybe (Word8, ByteString)
Nothing                            -> Maybe (Word8, (BlankState, ByteString))
forall a. Maybe a
Nothing
    blankByteString (BlankState
InIdent, ByteString
bs) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
      Just (!Word8
c, !ByteString
cs) | Word8 -> Bool
isAlphabetic Word8
c   -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_underscore , (BlankState
InIdent  , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8 -> Bool
isLeadingDigit Word8
c -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_1          , (BlankState
InNumber , ByteString
cs))
      Just (!Word8
c, !ByteString
cs) | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_quotedbl   -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
_parenleft  , (BlankState
InString , ByteString
cs))
      Just (!Word8
c, !ByteString
cs)                    -> (Word8, (BlankState, ByteString))
-> Maybe (Word8, (BlankState, ByteString))
forall a. a -> Maybe a
Just (Word8
c           , (BlankState
InJson   , ByteString
cs))
      Maybe (Word8, ByteString)
Nothing                           -> Maybe (Word8, (BlankState, ByteString))
forall a. Maybe a
Nothing