{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UnboxedTuples #-}
module Codec.CBOR.Pretty
( prettyHexEnc
) where
#include "cbor.h"
import Data.Word
import qualified Data.ByteString as S
import qualified Data.Text as T
import Codec.CBOR.ByteArray.Sliced
import Codec.CBOR.Encoding
import Codec.CBOR.Write
import qualified Control.Monad.Fail as Fail
import Control.Monad (replicateM_)
import GHC.Int (Int64)
import Numeric
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
newtype PP a = PP (Tokens -> Int -> ShowS -> Either String (Tokens,Int,ShowS,a))
prettyHexEnc :: Encoding -> String
prettyHexEnc :: Encoding -> String
prettyHexEnc Encoding
e = case PP () -> Encoding -> Either String (Tokens, Int, ShowS, ())
forall a. PP a -> Encoding -> Either String (Tokens, Int, ShowS, a)
runPP PP ()
pprint Encoding
e of
Left String
s -> String
s
Right (Tokens
TkEnd,Int
_,ShowS
ss,()
_) -> ShowS
ss String
""
Right (Tokens
toks,Int
_,ShowS
ss,()
_) -> ShowS
ss ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String
"\nprettyEnc: Not all input was consumed (this is probably a problem with the pretty printing code). Tokens left: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Tokens -> String
forall a. Show a => a -> String
show Tokens
toks
runPP :: PP a -> Encoding -> Either String (Tokens, Int, ShowS, a)
runPP :: forall a. PP a -> Encoding -> Either String (Tokens, Int, ShowS, a)
runPP (PP Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a)
f) (Encoding Tokens -> Tokens
enc) = Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a)
f (Tokens -> Tokens
enc Tokens
TkEnd) Int
0 ShowS
forall a. a -> a
id
deriving instance Functor PP
instance Applicative PP where
pure :: forall a. a -> PP a
pure a
a = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP (\Tokens
toks Int
ind ShowS
ss -> (Tokens, Int, ShowS, a) -> Either String (Tokens, Int, ShowS, a)
forall a b. b -> Either a b
Right (Tokens
toks, Int
ind, ShowS
ss, a
a))
(PP Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, a -> b)
f) <*> :: forall a b. PP (a -> b) -> PP a -> PP b
<*> (PP Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a)
x) = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, b))
-> PP b
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP ((Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, b))
-> PP b)
-> (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, b))
-> PP b
forall a b. (a -> b) -> a -> b
$ \Tokens
toks Int
ind ShowS
ss -> case Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, a -> b)
f Tokens
toks Int
ind ShowS
ss of
Left String
s -> String -> Either String (Tokens, Int, ShowS, b)
forall a b. a -> Either a b
Left String
s
Right (Tokens
toks', Int
ind',ShowS
ss',a -> b
f') -> case Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a)
x Tokens
toks' Int
ind' ShowS
ss' of
Left String
s -> String -> Either String (Tokens, Int, ShowS, b)
forall a b. a -> Either a b
Left String
s
Right (Tokens
toks'', Int
ind'', ShowS
ss'', a
x') -> (Tokens, Int, ShowS, b) -> Either String (Tokens, Int, ShowS, b)
forall a b. b -> Either a b
Right (Tokens
toks'', Int
ind'', ShowS
ss'', a -> b
f' a
x')
instance Monad PP where
(PP Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a)
f) >>= :: forall a b. PP a -> (a -> PP b) -> PP b
>>= a -> PP b
g = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, b))
-> PP b
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP ((Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, b))
-> PP b)
-> (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, b))
-> PP b
forall a b. (a -> b) -> a -> b
$ \Tokens
toks Int
ind ShowS
ss -> case Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a)
f Tokens
toks Int
ind ShowS
ss of
Left String
s -> String -> Either String (Tokens, Int, ShowS, b)
forall a b. a -> Either a b
Left String
s
Right (Tokens
toks', Int
ind', ShowS
ss', a
x) -> let PP Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, b)
g' = a -> PP b
g a
x
in Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, b)
g' Tokens
toks' Int
ind' ShowS
ss'
return :: forall a. a -> PP a
return = a -> PP a
forall a. a -> PP a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
#if !MIN_VERSION_base(4,13,0)
fail = Fail.fail
#endif
instance Fail.MonadFail PP where
fail :: forall a. String -> PP a
fail String
s = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP ((Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a)
-> (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
forall a b. (a -> b) -> a -> b
$ \Tokens
_ Int
_ ShowS
_ -> String -> Either String (Tokens, Int, ShowS, a)
forall a b. a -> Either a b
Left String
s
indent :: PP ()
indent :: PP ()
indent = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, ()))
-> PP ()
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP (\Tokens
toks Int
ind ShowS
ss -> (Tokens, Int, ShowS, ()) -> Either String (Tokens, Int, ShowS, ())
forall a b. b -> Either a b
Right (Tokens
toks,Int
ind,ShowS
ss ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
ind Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++),()))
nl :: PP ()
nl :: PP ()
nl = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, ()))
-> PP ()
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP (\Tokens
toks Int
ind ShowS
ss -> (Tokens, Int, ShowS, ()) -> Either String (Tokens, Int, ShowS, ())
forall a b. b -> Either a b
Right (Tokens
toks,Int
ind,ShowS
ss ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'\n'Char -> ShowS
forall a. a -> [a] -> [a]
:), ()))
inc :: Int -> PP ()
inc :: Int -> PP ()
inc Int
i = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, ()))
-> PP ()
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP (\Tokens
toks Int
ind ShowS
ss -> (Tokens, Int, ShowS, ()) -> Either String (Tokens, Int, ShowS, ())
forall a b. b -> Either a b
Right (Tokens
toks,Int
indInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
i,ShowS
ss,()))
dec :: Int -> PP ()
dec :: Int -> PP ()
dec Int
i = Int -> PP ()
inc (-Int
i)
getTerm :: PP Tokens
getTerm :: PP Tokens
getTerm = (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, Tokens))
-> PP Tokens
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP ((Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, Tokens))
-> PP Tokens)
-> (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, Tokens))
-> PP Tokens
forall a b. (a -> b) -> a -> b
$ \Tokens
toks Int
ind ShowS
ss ->
case Tokens -> Maybe (Tokens, Tokens)
unconsToken Tokens
toks of
Just (Tokens
tk,Tokens
rest) -> (Tokens, Int, ShowS, Tokens)
-> Either String (Tokens, Int, ShowS, Tokens)
forall a b. b -> Either a b
Right (Tokens
rest,Int
ind,ShowS
ss,Tokens
tk)
Maybe (Tokens, Tokens)
Nothing -> String -> Either String (Tokens, Int, ShowS, Tokens)
forall a b. a -> Either a b
Left String
"getTok: Unexpected end of input"
peekTerm :: PP Tokens
peekTerm :: PP Tokens
peekTerm = (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, Tokens))
-> PP Tokens
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP ((Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, Tokens))
-> PP Tokens)
-> (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, Tokens))
-> PP Tokens
forall a b. (a -> b) -> a -> b
$ \Tokens
toks Int
ind ShowS
ss ->
case Tokens -> Maybe (Tokens, Tokens)
unconsToken Tokens
toks of
Just (Tokens
tk,Tokens
_) -> (Tokens, Int, ShowS, Tokens)
-> Either String (Tokens, Int, ShowS, Tokens)
forall a b. b -> Either a b
Right (Tokens
toks,Int
ind,ShowS
ss,Tokens
tk)
Maybe (Tokens, Tokens)
Nothing -> String -> Either String (Tokens, Int, ShowS, Tokens)
forall a b. a -> Either a b
Left String
"peekTerm: Unexpected end of input"
appShowS :: ShowS -> PP ()
appShowS :: ShowS -> PP ()
appShowS ShowS
s = (Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, ()))
-> PP ()
forall a.
(Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, a))
-> PP a
PP ((Tokens -> Int -> ShowS -> Either String (Tokens, Int, ShowS, ()))
-> PP ())
-> (Tokens
-> Int -> ShowS -> Either String (Tokens, Int, ShowS, ()))
-> PP ()
forall a b. (a -> b) -> a -> b
$ \Tokens
toks Int
ind ShowS
ss -> (Tokens, Int, ShowS, ()) -> Either String (Tokens, Int, ShowS, ())
forall a b. b -> Either a b
Right (Tokens
toks,Int
ind,ShowS
ss ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
s,())
str :: String -> PP ()
str :: String -> PP ()
str = ShowS -> PP ()
appShowS (ShowS -> PP ()) -> (String -> ShowS) -> String -> PP ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString
shown :: Show a => a -> PP ()
shown :: forall a. Show a => a -> PP ()
shown = ShowS -> PP ()
appShowS (ShowS -> PP ()) -> (a -> ShowS) -> a -> PP ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows
parens :: PP a -> PP a
parens :: forall a. PP a -> PP a
parens PP a
pp = String -> PP ()
str String
"(" PP () -> PP a -> PP a
forall a b. PP a -> PP b -> PP b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> PP a
pp PP a -> PP () -> PP a
forall a b. PP a -> PP b -> PP a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* String -> PP ()
str String
")"
indef :: PP () -> PP ()
indef :: PP () -> PP ()
indef PP ()
pp = do
Tokens
tk <- PP Tokens
peekTerm
case Tokens
tk of
TkBreak Tokens
TkEnd -> Int -> PP ()
dec Int
3 PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP ()
pprint
Tokens
_ -> PP ()
pp PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
indef PP ()
pp
pprint :: PP ()
pprint :: PP ()
pprint = do
PP ()
nl
Tokens
term <- PP Tokens
getTerm
Tokens -> PP ()
hexRep Tokens
term
String -> PP ()
str String
" "
case Tokens
term of
TkInt Int
i Tokens
TkEnd -> Int -> PP ()
ppTkInt Int
i
TkInt Int
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkInt64 Int64
i Tokens
TkEnd -> Int64 -> PP ()
ppTkInt64 Int64
i
TkInt64 Int64
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkInteger Integer
i Tokens
TkEnd -> Integer -> PP ()
ppTkInteger Integer
i
TkInteger Integer
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkWord64 Word64
w Tokens
TkEnd -> Word64 -> PP ()
ppTkWord64 Word64
w
TkWord64 Word64
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkWord Word
w Tokens
TkEnd -> Word -> PP ()
ppTkWord Word
w
TkWord Word
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkBytes ByteString
bs Tokens
TkEnd -> ByteString -> PP ()
ppTkBytes ByteString
bs
TkBytes ByteString
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkBytesBegin Tokens
TkEnd -> PP ()
ppTkBytesBegin
TkBytesBegin Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkByteArray SlicedByteArray
ba Tokens
TkEnd -> SlicedByteArray -> PP ()
ppTkByteArray SlicedByteArray
ba
TkByteArray SlicedByteArray
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkUtf8ByteArray SlicedByteArray
ba Tokens
TkEnd -> SlicedByteArray -> PP ()
ppTkUtf8ByteArray SlicedByteArray
ba
TkUtf8ByteArray SlicedByteArray
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkString Text
t Tokens
TkEnd -> Text -> PP ()
ppTkString Text
t
TkString Text
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkStringBegin Tokens
TkEnd -> PP ()
ppTkStringBegin
TkStringBegin Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkListLen Word
w Tokens
TkEnd -> Word -> PP ()
ppTkListLen Word
w
TkListLen Word
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkListBegin Tokens
TkEnd -> PP ()
ppTkListBegin
TkListBegin Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkMapLen Word
w Tokens
TkEnd -> Word -> PP ()
ppTkMapLen Word
w
TkMapLen Word
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkMapBegin Tokens
TkEnd -> PP ()
ppTkMapBegin
TkMapBegin Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkBreak Tokens
TkEnd -> PP ()
ppTkBreak
TkBreak Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkTag Word
w Tokens
TkEnd -> Word -> PP ()
ppTkTag Word
w
TkTag Word
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkTag64 Word64
w Tokens
TkEnd -> Word64 -> PP ()
ppTkTag64 Word64
w
TkTag64 Word64
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkBool Bool
b Tokens
TkEnd -> Bool -> PP ()
ppTkBool Bool
b
TkBool Bool
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkNull Tokens
TkEnd -> PP ()
ppTkNull
TkNull Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkUndef Tokens
TkEnd -> PP ()
ppTkUndef
TkUndef Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkSimple Word8
w Tokens
TkEnd -> Word8 -> PP ()
ppTkSimple Word8
w
TkSimple Word8
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkFloat16 Float
f Tokens
TkEnd -> Float -> PP ()
ppTkFloat16 Float
f
TkFloat16 Float
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkFloat32 Float
f Tokens
TkEnd -> Float -> PP ()
ppTkFloat32 Float
f
TkFloat32 Float
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkFloat64 Double
f Tokens
TkEnd -> Double -> PP ()
ppTkFloat64 Double
f
TkFloat64 Double
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
TkEncoded ByteString
_ Tokens
TkEnd -> PP ()
ppTkEncoded
TkEncoded ByteString
_ Tokens
_ -> Tokens -> PP ()
forall {m :: * -> *} {a} {a}. (MonadFail m, Show a) => a -> m a
termFailure Tokens
term
Tokens
TkEnd -> String -> PP ()
str String
"# End of input"
where
termFailure :: a -> m a
termFailure a
t = String -> m a
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m a) -> String -> m a
forall a b. (a -> b) -> a -> b
$ [String] -> String
unwords [String
"pprint: Unexpected token:", a -> String
forall a. Show a => a -> String
show a
t]
ppTkInt :: Int -> PP ()
ppTkInt :: Int -> PP ()
ppTkInt Int
i = String -> PP ()
str String
"# int" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Int -> PP ()
forall a. Show a => a -> PP ()
shown Int
i)
ppTkInt64 :: Int64 -> PP ()
ppTkInt64 :: Int64 -> PP ()
ppTkInt64 Int64
i = String -> PP ()
str String
"# int" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Int64 -> PP ()
forall a. Show a => a -> PP ()
shown Int64
i)
ppTkInteger :: Integer -> PP ()
ppTkInteger :: Integer -> PP ()
ppTkInteger Integer
i = String -> PP ()
str String
"# integer" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Integer -> PP ()
forall a. Show a => a -> PP ()
shown Integer
i)
ppTkWord64 :: Word64 -> PP ()
ppTkWord64 :: Word64 -> PP ()
ppTkWord64 Word64
w = String -> PP ()
str String
"# word" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Word64 -> PP ()
forall a. Show a => a -> PP ()
shown Word64
w)
ppTkWord :: Word -> PP ()
ppTkWord :: Word -> PP ()
ppTkWord Word
w = String -> PP ()
str String
"# word" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Word -> PP ()
forall a. Show a => a -> PP ()
shown Word
w)
ppTkByteArray :: SlicedByteArray -> PP ()
ppTkByteArray :: SlicedByteArray -> PP ()
ppTkByteArray SlicedByteArray
bs = String -> PP ()
str String
"# bytes" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Int -> PP ()
forall a. Show a => a -> PP ()
shown (Int -> PP ()) -> Int -> PP ()
forall a b. (a -> b) -> a -> b
$ SlicedByteArray -> Int
sizeofSlicedByteArray SlicedByteArray
bs)
ppTkUtf8ByteArray :: SlicedByteArray -> PP ()
ppTkUtf8ByteArray :: SlicedByteArray -> PP ()
ppTkUtf8ByteArray SlicedByteArray
bs = String -> PP ()
str String
"# text" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Int -> PP ()
forall a. Show a => a -> PP ()
shown (Int -> PP ()) -> Int -> PP ()
forall a b. (a -> b) -> a -> b
$ SlicedByteArray -> Int
sizeofSlicedByteArray SlicedByteArray
bs)
ppTkBytes :: S.ByteString -> PP ()
ppTkBytes :: ByteString -> PP ()
ppTkBytes ByteString
bs = String -> PP ()
str String
"# bytes" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Int -> PP ()
forall a. Show a => a -> PP ()
shown (ByteString -> Int
S.length ByteString
bs))
ppTkBytesBegin :: PP ()
ppTkBytesBegin :: PP ()
ppTkBytesBegin = String -> PP ()
str String
"# bytes(*)" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> PP ()
inc Int
3 PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
indef PP ()
pprint
ppTkString :: T.Text -> PP ()
ppTkString :: Text -> PP ()
ppTkString Text
t = String -> PP ()
str String
"# text" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Text -> PP ()
forall a. Show a => a -> PP ()
shown Text
t)
ppTkStringBegin:: PP ()
ppTkStringBegin :: PP ()
ppTkStringBegin = String -> PP ()
str String
"# text(*)" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> PP ()
inc Int
3 PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
indef PP ()
pprint
ppTkEncoded :: PP ()
ppTkEncoded :: PP ()
ppTkEncoded = String -> PP ()
str String
"# pre-encoded CBOR term"
ppTkListLen :: Word -> PP ()
ppTkListLen :: Word -> PP ()
ppTkListLen Word
n = do
String -> PP ()
str String
"# list"
PP () -> PP ()
forall a. PP a -> PP a
parens (Word -> PP ()
forall a. Show a => a -> PP ()
shown Word
n)
Int -> PP ()
inc Int
3
Int -> PP () -> PP ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ (Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
n) PP ()
pprint
Int -> PP ()
dec Int
3
ppTkListBegin :: PP ()
ppTkListBegin :: PP ()
ppTkListBegin = String -> PP ()
str String
"# list(*)" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> PP ()
inc Int
3 PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
indef PP ()
pprint
ppMapPairs :: PP ()
ppMapPairs :: PP ()
ppMapPairs = do
PP ()
nl
Int -> PP ()
inc Int
3
PP ()
indent
String -> PP ()
str String
" # key"
PP ()
pprint
Int -> PP ()
dec Int
3
PP ()
nl
Int -> PP ()
inc Int
3
PP ()
indent
String -> PP ()
str String
" # value"
PP ()
pprint
Int -> PP ()
dec Int
3
ppTkMapLen :: Word -> PP ()
ppTkMapLen :: Word -> PP ()
ppTkMapLen Word
w = do
String -> PP ()
str String
"# map"
PP () -> PP ()
forall a. PP a -> PP a
parens (Word -> PP ()
forall a. Show a => a -> PP ()
shown Word
w)
Int -> PP () -> PP ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ (Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
w) PP ()
ppMapPairs
ppTkMapBegin :: PP ()
ppTkMapBegin :: PP ()
ppTkMapBegin = String -> PP ()
str String
"# map(*)" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> PP ()
inc Int
3 PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
indef PP ()
ppMapPairs
ppTkBreak :: PP ()
ppTkBreak :: PP ()
ppTkBreak = String -> PP ()
str String
"# break"
ppTkTag :: Word -> PP ()
ppTkTag :: Word -> PP ()
ppTkTag Word
w = do
String -> PP ()
str String
"# tag"
PP () -> PP ()
forall a. PP a -> PP a
parens (Word -> PP ()
forall a. Show a => a -> PP ()
shown Word
w)
Int -> PP ()
inc Int
3
PP ()
pprint
Int -> PP ()
dec Int
3
ppTkTag64 :: Word64 -> PP ()
ppTkTag64 :: Word64 -> PP ()
ppTkTag64 Word64
w = do
String -> PP ()
str String
"# tag"
PP () -> PP ()
forall a. PP a -> PP a
parens (Word64 -> PP ()
forall a. Show a => a -> PP ()
shown Word64
w)
Int -> PP ()
inc Int
3
PP ()
pprint
Int -> PP ()
dec Int
3
ppTkBool :: Bool -> PP ()
ppTkBool :: Bool -> PP ()
ppTkBool Bool
True = String -> PP ()
str String
"# bool" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (String -> PP ()
str String
"true")
ppTkBool Bool
False = String -> PP ()
str String
"# bool" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (String -> PP ()
str String
"false")
ppTkNull :: PP ()
ppTkNull :: PP ()
ppTkNull = String -> PP ()
str String
"# null"
ppTkUndef :: PP ()
ppTkUndef :: PP ()
ppTkUndef = String -> PP ()
str String
"# undefined"
ppTkSimple :: Word8 -> PP ()
ppTkSimple :: Word8 -> PP ()
ppTkSimple Word8
w = String -> PP ()
str String
"# simple" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Word8 -> PP ()
forall a. Show a => a -> PP ()
shown Word8
w)
ppTkFloat16 :: Float -> PP ()
ppTkFloat16 :: Float -> PP ()
ppTkFloat16 Float
f = String -> PP ()
str String
"# float16" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Float -> PP ()
forall a. Show a => a -> PP ()
shown Float
f)
ppTkFloat32 :: Float -> PP ()
ppTkFloat32 :: Float -> PP ()
ppTkFloat32 Float
f = String -> PP ()
str String
"# float32" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Float -> PP ()
forall a. Show a => a -> PP ()
shown Float
f)
ppTkFloat64 :: Double -> PP ()
ppTkFloat64 :: Double -> PP ()
ppTkFloat64 Double
f = String -> PP ()
str String
"# float64" PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP () -> PP ()
forall a. PP a -> PP a
parens (Double -> PP ()
forall a. Show a => a -> PP ()
shown Double
f)
unconsToken :: Tokens -> Maybe (Tokens, Tokens)
unconsToken :: Tokens -> Maybe (Tokens, Tokens)
unconsToken Tokens
TkEnd = Maybe (Tokens, Tokens)
forall a. Maybe a
Nothing
unconsToken (TkWord Word
w Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word -> Tokens -> Tokens
TkWord Word
w Tokens
TkEnd,Tokens
tks)
unconsToken (TkWord64 Word64
w Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word64 -> Tokens -> Tokens
TkWord64 Word64
w Tokens
TkEnd,Tokens
tks)
unconsToken (TkInt Int
i Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Int -> Tokens -> Tokens
TkInt Int
i Tokens
TkEnd,Tokens
tks)
unconsToken (TkInt64 Int64
i Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Int64 -> Tokens -> Tokens
TkInt64 Int64
i Tokens
TkEnd,Tokens
tks)
unconsToken (TkBytes ByteString
bs Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (ByteString -> Tokens -> Tokens
TkBytes ByteString
bs Tokens
TkEnd,Tokens
tks)
unconsToken (TkBytesBegin Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkBytesBegin Tokens
TkEnd,Tokens
tks)
unconsToken (TkByteArray SlicedByteArray
a Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (SlicedByteArray -> Tokens -> Tokens
TkByteArray SlicedByteArray
a Tokens
TkEnd,Tokens
tks)
unconsToken (TkString Text
t Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Text -> Tokens -> Tokens
TkString Text
t Tokens
TkEnd,Tokens
tks)
unconsToken (TkStringBegin Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkStringBegin Tokens
TkEnd,Tokens
tks)
unconsToken (TkUtf8ByteArray SlicedByteArray
a Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (SlicedByteArray -> Tokens -> Tokens
TkUtf8ByteArray SlicedByteArray
a Tokens
TkEnd,Tokens
tks)
unconsToken (TkListLen Word
len Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word -> Tokens -> Tokens
TkListLen Word
len Tokens
TkEnd,Tokens
tks)
unconsToken (TkListBegin Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkListBegin Tokens
TkEnd,Tokens
tks)
unconsToken (TkMapLen Word
len Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word -> Tokens -> Tokens
TkMapLen Word
len Tokens
TkEnd,Tokens
tks)
unconsToken (TkMapBegin Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkMapBegin Tokens
TkEnd,Tokens
tks)
unconsToken (TkTag Word
w Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word -> Tokens -> Tokens
TkTag Word
w Tokens
TkEnd,Tokens
tks)
unconsToken (TkTag64 Word64
w64 Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word64 -> Tokens -> Tokens
TkTag64 Word64
w64 Tokens
TkEnd,Tokens
tks)
unconsToken (TkInteger Integer
i Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Integer -> Tokens -> Tokens
TkInteger Integer
i Tokens
TkEnd,Tokens
tks)
unconsToken (TkNull Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkNull Tokens
TkEnd,Tokens
tks)
unconsToken (TkUndef Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkUndef Tokens
TkEnd,Tokens
tks)
unconsToken (TkBool Bool
b Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Bool -> Tokens -> Tokens
TkBool Bool
b Tokens
TkEnd,Tokens
tks)
unconsToken (TkSimple Word8
w8 Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Word8 -> Tokens -> Tokens
TkSimple Word8
w8 Tokens
TkEnd,Tokens
tks)
unconsToken (TkFloat16 Float
f16 Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Float -> Tokens -> Tokens
TkFloat16 Float
f16 Tokens
TkEnd,Tokens
tks)
unconsToken (TkFloat32 Float
f32 Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Float -> Tokens -> Tokens
TkFloat32 Float
f32 Tokens
TkEnd,Tokens
tks)
unconsToken (TkFloat64 Double
f64 Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Double -> Tokens -> Tokens
TkFloat64 Double
f64 Tokens
TkEnd,Tokens
tks)
unconsToken (TkEncoded ByteString
bs Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (ByteString -> Tokens -> Tokens
TkEncoded ByteString
bs Tokens
TkEnd,Tokens
tks)
unconsToken (TkBreak Tokens
tks) = (Tokens, Tokens) -> Maybe (Tokens, Tokens)
forall a. a -> Maybe a
Just (Tokens -> Tokens
TkBreak Tokens
TkEnd,Tokens
tks)
hexRep :: Tokens -> PP ()
hexRep :: Tokens -> PP ()
hexRep Tokens
tk = ByteString -> PP ()
go (ByteString -> PP ())
-> ((Tokens -> Tokens) -> ByteString)
-> (Tokens -> Tokens)
-> PP ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> ByteString
toStrictByteString (Encoding -> ByteString)
-> ((Tokens -> Tokens) -> Encoding)
-> (Tokens -> Tokens)
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> PP ()) -> (Tokens -> Tokens) -> PP ()
forall a b. (a -> b) -> a -> b
$ Tokens -> Tokens -> Tokens
forall a b. a -> b -> a
const Tokens
tk where
go :: ByteString -> PP ()
go ByteString
bs | ByteString -> Int
S.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
16 = case Int -> ByteString -> (ByteString, ByteString)
S.splitAt Int
16 ByteString
bs of
(ByteString
h,ByteString
t) -> PP ()
indent PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ShowS -> PP ()
appShowS (ByteString -> ShowS
hexBS ByteString
h) PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PP ()
nl PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> PP ()
go ByteString
t
| Bool
otherwise = PP ()
indent PP () -> PP () -> PP ()
forall a b. PP a -> PP b -> PP b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ShowS -> PP ()
appShowS (ByteString -> ShowS
hexBS ByteString
bs)
hexBS :: S.ByteString -> ShowS
hexBS :: ByteString -> ShowS
hexBS = (ShowS -> ShowS -> ShowS) -> ShowS -> [ShowS] -> ShowS
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) ShowS
forall a. a -> a
id ([ShowS] -> ShowS)
-> (ByteString -> [ShowS]) -> ByteString -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> ShowS) -> [Word8] -> [ShowS]
forall a b. (a -> b) -> [a] -> [b]
map (\Word8
n -> ((if Word8
n Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
16 then (Char
'0'Char -> ShowS
forall a. a -> [a] -> [a]
:) else ShowS
forall a. a -> a
id) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ShowS
forall a. Integral a => a -> ShowS
showHex Word8
n ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:))) ([Word8] -> [ShowS])
-> (ByteString -> [Word8]) -> ByteString -> [ShowS]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
S.unpack