Code Snippet
src/Byte/Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
| --------------------------------------------------------------------------------
--
-- Byte.Parser, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Byte.Parser
( Byte, Bytes
, Output
( output
)
, Parser
( parse
)
, empty, many, some, (<|>)
, sepBy
, peekP
, failP, spanP
, getP
, byteP, bytesP, chunkP
, run
)
where
--------------------------------------------------------------------------------
import Control.Applicative
( Alternative
, empty
, many
, some
, (<|>)
)
import Data.Bits
( Bits
, shiftL
, shiftR
)
import Data.Char
( chr
)
import Data.Word
( Word8
)
--------------------------------------------------------------------------------
type Byte = Word8
type Bytes = [Byte]
type Index = Word
type Error = String
--------------------------------------------------------------------------------
newtype Output a = O
{ output :: (Either (Index, Error) (Index, a, Bytes))
}
newtype Parser a = P
{ parse :: Index -> Bytes -> Output a
}
--------------------------------------------------------------------------------
instance Show a => Show (Output a) where
show o =
case ppp "show" o of
Left e -> e
Right a -> show a
--------------------------------------------------------------------------------
instance Functor Output where
fmap _ (O (Left e)) = O $ Left e
fmap f (O (Right (i,a,bs))) = O $ Right $ (i, f a, bs)
instance Applicative Output where
pure a = O $ Right (0, a, [])
O (Left e) <*> _ = O $ Left e
O (Right (_,f,_)) <*> O r =
case r of
Left e -> O $ Left e
Right (i, a, bs) -> O $ Right (i, f a, bs)
instance Alternative Output where
empty = O $ Left (0, [])
O (Left _) <|> o = o
o <|> _ = o
--------------------------------------------------------------------------------
instance Functor Parser where
fmap f (P p) = P $ \ n bs ->
case p n bs of
O (Left (i, e)) -> O $ Left (i, e)
O (Right (i, a, xs)) -> O $ Right (i, f a, xs)
instance Applicative Parser where
pure a = P $ \ n bs -> O $ Right (n, a, bs)
(<*>) (P p1) (P p2) = P $ \ n bs ->
case p1 n bs of
O (Left (i, e)) -> O $ Left (i, e)
O (Right (i, f, xs)) ->
case p2 i xs of
O (Left (j, e)) -> O $ Left (j, e)
O (Right (j, a, ys)) -> O $ Right (j, f a, ys)
instance Monad Parser where
(>>=) (P p) f = P $ \ n bs ->
case p n bs of
O (Left (i, e)) -> O $ Left (i, e)
O (Right (i, a, xs)) -> parse (f a) i xs
instance Alternative Parser where
empty = P $ \ i __ -> O $ Left (i, [])
(<|>) (P p1) (P p2) = P $ \ n bs -> p1 n bs <|> p2 n bs
--------------------------------------------------------------------------------
sepBy
:: (Alternative f)
=> f a
-> f b
-> f [a]
sepBy p sep =
aux <|> pure []
where
aux = (:) <$> p <*> (many $ sep *> p)
--------------------------------------------------------------------------------
peekP
:: Int
-> Parser Bytes
peekP n =
P $ \ i bs -> O $ Right (i, take n bs, bs)
failP
:: Error
-> Parser a
failP msg =
P $ \ i bs -> O $ Left (i, err i msg bs)
spanP
:: (Byte -> Bool)
-> Parser Bytes
spanP f =
P $ \ i bs -> aux i bs
where
aux i bs =
case span f bs of
([],__) -> O $ Left (i, err i "spanP" bs)
(as,rs) -> O $ Right (i, as, rs)
getP
:: Parser Byte
getP =
P $ \ i bs -> aux i bs
where
aux i [ ] = O $ Left (i, err i "getP" [])
aux i (x:rs) = O $ Right (i+1, x, rs)
byteP
:: Byte
-> Parser Byte
byteP b =
P $ \ i bs -> aux i bs
where
aux i [ ] = O $ Left (i, err i "getP" [])
aux i bs@(x:rs) =
if b == x
then O $ Right (i+1, x, rs)
else O $ Left (i, err i "byteP" bs)
bytesP
:: Bytes
-> Parser Bytes
bytesP =
sequenceA . map byteP
chunkP
:: Integral a
=> a
-> Parser Bytes
chunkP n =
P $ \ i bs ->
case take m bs of
xs | length xs == m -> O $ Right (i+j, take m xs, drop m bs)
___________________ -> O $ Left (i, err i "chunkP" bs)
where
j = fromIntegral n
m = fromIntegral n
--------------------------------------------------------------------------------
run
:: Parser a
-> Bytes
-> Either Error a
run p =
ppp "run" . parse p 0
--------------------------------------------------------------------------------
-- HELPERS
ppp
:: String
-> Output a
-> Either Error a
ppp _ (O (Left (_,e))) = Left $ e
ppp _ (O (Right (_,a,[]))) = Right a
ppp m (O (Right (i,_,bs))) = Left $ err i ("ppp > " ++ m) bs
(.<.)
:: Bits a
=> a
-> Int
-> a
(.<.) x y = x `shiftL` y
(.>.)
:: Bits a
=> a
-> Int
-> a
(.>.) x y = x `shiftR` y
n2b
:: (Bits a, Integral a)
=> Int
-> (a -> a)
-> a
-> [Byte]
n2b b f =
aux []
where
aux [ ] 0 = [ fromIntegral $! f 0 ]
aux acc 0 = acc
aux acc n =
aux (r : acc) c
where
c = n .>. b
r = fromIntegral $ f (n - c * 1 .<. b)
b2h
:: Word8
-> [Char]
b2h =
pad True 2 '0' . map (chr . fromIntegral) . n2b 4 {- 2^4 = 016 -} aux
where
aux n
| n <= 0x09 = 48 + n
| otherwise = 55 + n
pad :: Bool -> Int -> Char -> String -> String
pad d n c x =
if l > n
then x
else
if d
then replicate (n-l) c ++ x
else x ++ replicate (n-l) c
where
l = length x
cof
:: Int
-> [a]
-> [[a]]
cof _ [] = [ ]
cof n xs = y : (cof n ys)
where
(y,ys) = splitAt n xs
err
:: Index
-> String
-> [Word8]
-> String
err i f bs =
"# Parser error" ++ "\n" ++
"* Function:.....: " ++ f ++ "\n" ++
"* Index.........: " ++ pad True 16 '0' (show i) ++ "\n" ++
"* Unparsed bytes: " ++ show m ++ "\n" ++ out bs
where
m = length bs
out xs = -- mimic `hexl-mode` in `emacs`
(++ "…") $
foldl (\a (x,y) -> a ++ (rp y) ++ " " ++ x ++ "\n") [] . zip zs . cof 40 $
foldl (\a x -> a ++ x ++ " ") [] . cof 04 $
concat $
map b2h $
ys
where
rp = pad False 40 ' '
ys = take 640 xs
zs = cof 016 $ map ec ys
ec =
\ b ->
if b < 032 || b == 127
then '.'
else chr $ fromIntegral b
|
src/Byte/Parser/JSON.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
| --------------------------------------------------------------------------------
--
-- Byte.Parser.JSON, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Byte.Parser.JSON
( jsonP
, stringP, numberP, objectP, arrayP, boolP, nullP
)
where
--------------------------------------------------------------------------------
import Data.Char
( chr
)
import Byte.Parser
( Byte
, Parser
, byteP
, bytesP
, chunkP
, failP
, getP
, many
, peekP
, sepBy
, spanP
, (<|>)
)
import qualified Data.JSON as JSON
--------------------------------------------------------------------------------
jsonP
:: Parser JSON.Value
jsonP =
stringP <|> numberP <|> objectP <|> arrayP <|> boolP <|> nullP
stringP
:: Parser JSON.Value
stringP =
JSON.String <$> charsP
numberP
:: Parser JSON.Value
numberP =
JSON.Number . toRational . s2d . map (chr . fromIntegral) <$> aux
where
aux = sci <|> dec <|> int
sci =
d <|> i
where
d = cmb <$> dec <*> (byteP cce <|> byteP lce) <*> num
i = cmb <$> int <*> (byteP cce <|> byteP lce) <*> num
dec =
n <|> d
where
n = (:) <$> byteP neg <*> d
d = cmb <$> num <*> byteP dot <*> num
int =
n <|> num
where
n = (:) <$> byteP neg <*> num
neg = 045
dot = 046
cce = 069
lce = 101
num = spanP (\b -> 47 < b && b < 58)
s2d = read :: String -> Double
cmb = \xs y zs -> xs ++ [y] ++ zs
objectP
:: Parser JSON.Value
objectP =
JSON.Object <$> aux
where
aux =
oc *> ws *> ps `sepBy` (ws *> cs <* ws) <* ws <* cc
where
ps =
(\k _ v -> (k,v))
<$> charsP
<*> (ws *> kv <* ws)
<*> jsonP
oc = byteP 123
cc = byteP 125
cs = byteP 044
kv = byteP 058
ws = many whitespaceP
arrayP
:: Parser JSON.Value
arrayP =
JSON.Array <$> aux
where
aux =
ob *> ws *> jsonP `sepBy` (ws *> cs <* ws) <* ws <* cb
where
ob = byteP 091
cb = byteP 093
cs = byteP 044
ws = many whitespaceP
boolP
:: Parser JSON.Value
boolP =
trueP <|> falseP
nullP
:: Parser JSON.Value
nullP =
aux <$> bytesP [110,117,108,108]
where
aux _ = JSON.Null
--------------------------------------------------------------------------------
-- HELPERS
trueP
:: Parser JSON.Value
trueP =
aux <$> bytesP [116,114,117,101]
where
aux _ = JSON.Boolean True
falseP
:: Parser JSON.Value
falseP =
aux <$> bytesP [102,97,108,115,101]
where
aux _ = JSON.Boolean False
whitespaceP
:: Parser Byte
whitespaceP =
ht <|> lf <|> cr <|> sb
where
ht = byteP 009
lf = byteP 010
cr = byteP 013
sb = byteP 032
charsP
:: Parser [Char]
charsP =
map (chr . fromIntegral)
<$> (byteP 034 *> aux <* byteP 034)
where
aux =
peekP n >>= \ bs ->
case bs of
[ ] -> pure []
-- Reverse slash: '\\'
[092] -> esc
(b:_) ->
if g b
then pure []
else f
where
n = 1
f = (:) <$> getP <*> aux
g =
\ b ->
-- All except
b < 032 || -- Control Codes
b == 127 || -- Delete: '\DEL'
b == 034 -- Quotation Mark
esc =
peekP n >>= \ bs ->
case bs of
[092 ] -> failP "charsP > invalid single reverse slash"
-- Escaped backspace: '\b'
[092, 098] -> f
-- Escaped horizontal tab: '\t'
[092, 116] -> f
-- Escaped line feed: '\n
[092, 110] -> f
-- Escaped form feed: '\f'
[092, 102] -> f
-- Escaped carriage return: '\r
[092, 114] -> f
-- Escaped double quotes: '\\':'"'
[092, 034] -> f
-- Escaped forward slash: '\\':'/'
[092, 047] -> f
-- Escaped reverse slash: '\\':'\\'
[092, 092] -> f
[092, 117] -> uni
__________ -> failP "charsP > invalid reverse slash sequence"
where
n = 2
f = (++) <$> chunkP n <*> aux
uni =
peekP n >>= \ bs ->
case bs of
-- Unicode: '\\':'u':'0':'0':'0':'0' - '\\':'u':'F':'F':'F':'F'
092:117:xs | length xs == 4 && all f xs -> g
_______________________________________ -> failP e
where
n = 6
f =
\ b ->
(047 < b && b < 058) || -- [ 0 .. 9 ]
(064 < b && b < 071) || -- ['A'..'F']
(092 < b && b < 103) -- ['a'..'f']
g = (++) <$> chunkP n <*> aux
e = "charsP > invalid hex unicode"
|
src/Data/JSON.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| --------------------------------------------------------------------------------
--
-- Data.JSON, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Data.JSON
( Value
( String
, Number
, Object
, Array
, Boolean
, Null
)
, DTO
( encode
, decode
)
)
where
--------------------------------------------------------------------------------
type Error = String
--------------------------------------------------------------------------------
data Value
= String String
| Number Rational
| Object [(String, Value)]
| Array [Value]
| Boolean Bool
| Null
deriving
( Read
, Show
)
--------------------------------------------------------------------------------
class DTO a where
encode :: a -> Value
decode :: Value -> Either Error a
|
exe/JsonByteStream.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| --------------------------------------------------------------------------------
--
-- JsonByteStream, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Main (main) where
--------------------------------------------------------------------------------
import qualified Data.ByteString.Lazy as LBS
import Text.Show.Pretty
( ppShow
)
import qualified Byte.Parser as Parser
import qualified Byte.Parser.JSON as JSON
--------------------------------------------------------------------------------
main :: IO ()
main =
LBS.getContents >>= putStrLn . aux . eof . LBS.unpack
where
eof [ ] = [ ]
eof [010] = [ ]
eof (b:bs) = b:eof bs
aux bs =
case Parser.run JSON.jsonP bs of
Right ast -> ppShow ast
Left err -> err
|
json-bytestream-parser.cabal
stack.yaml
build.bash
Code Output:
dat/error-message-mimic-emacs-hexl-mode.json
dat/json.org-example-00.json
dat/json.org-example-01.json
dat/json.org-example-02.json
dat/json.org-example-03.json
dat/json.org-example-04.json
References:
- JSON.org:
- Wikipedia (ASCII):
- Haskell Hackage:
- YouTube - Tsoding channel:
- Old (school) Reddit:
- JSONformatter.org:
- GitLab - SPISE MISU ApS: