-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.zsh
471 lines (441 loc) · 8.55 KB
/
json.zsh
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/bin/zsh
#
# Pure ZSH json parser
#
source binutils.zsh
source zss.zsh
_json_buffer=''
_json_fd=0
function isspace()
{
if [ "$1" = " " ] || [ "$1" = "" ]; then
echo -n "t"
return 0;
fi
return 1
}
function isnumber()
{
case "$1" in
"0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9")
echo -n "t"
return 0
;;
esac
return 1
}
function isalnum()
{
case "$1" in
"0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"A"|"B"|"C"|"D"|"E"|"F"|"G"|"H"|"I"|"J"|"K"|"L"|"M"|"N"|"O"|"P"|"Q"|"R"|"S"|"T"|"U"|"V"|"W"|"X"|"Y"|"Z"|"a"|"b"|"c"|"d"|"e"|"f"|"g"|"h"|"i"|"j"|"k"|"l"|"m"|"n"|"o"|"p"|"q"|"r"|"s"|"t"|"u"|"v"|"w"|"x"|"y"|"z")
echo -n "t"
return 0
;;
esac
return 1
}
function isxdigit()
{
case "$1" in
"0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"A"|"B"|"C"|"D"|"E"|"F"|"a"|"b"|"c"|"d"|"e"|"f")
echo -n "t"
return 0
;;
esac
return 1
}
function _json_skip()
{
while [ -n "$(isspace $(_json_peek_any_char))" ]; do
_json_lex_any_char > /dev/null
done
}
function _json_peek_any_char()
{
local _json_index
_json_index=$(zss_read $_json_fd index)
if [ $#_json_buffer -lt $_json_index ]; then
echo -n "EOF"
zss_close $_json_fd
return 1
fi
echo -n "${_json_buffer[$_json_index]}"
}
function _json_lex_any_char()
{
local _json_index
_json_index=$(zss_read $_json_fd index)
c=$(_json_peek_any_char)
if [ "$c" != "EOF" ]; then
_json_index=$(($_json_index + 1))
zss_write $_json_fd index $_json_index
fi
echo -n $c
}
function _json_lex_one_char()
{
local i
for ((i=1; i<=$#1; i+=1)); do
if [ -n "$(_json_lex_char ${1[$i]})" ]; then
echo -n ${1[$i]}
return 0
fi
done
return 1
}
function _json_peek_one_char()
{
local i
for ((i=1; i<=$#1; i+=1)); do
_json_peek_char ${1[$i]}
if [ $? -eq 0 ]; then
echo -n ${1[$i]}
return 0
fi
done
return 1
}
function _json_peek_char()
{
local c
c=$(_json_peek_any_char)
if [ "$c" = "$1" ]; then
echo -n "$1"
return 0
fi
return 1
}
function _json_lex_char()
{
if [ -n "$(_json_peek_char $1)" ]; then
_json_lex_any_char
return 0
fi
return 1
}
function _json_lex_hex_digit()
{
local __c
__c=$(_json_peek_any_char)
if [ -z "$(isxdigit $__c)" ]; then
return 1
fi
_json_lex_any_char > /dev/null
echo -n $__c
return 0
}
function _json_parse_pair()
{
_json_skip
local str
if [ -n "$(_json_peek_char '"')" ]; then
str=$(_json_parse_string)
else
str=$(_json_parse_symbol)
fi
_json_skip
if [ -z "$(_json_lex_char ':')" ]; then
if [ -n "$(_json_peek_char ',')" ]; then
echo "Error: Object value not found" > /dev/stderr
echo -n "('$str' '')"
return 0
else
echo "Error: Can't find ':' between object and value" > /dev/stderr
fi
fi
local obj
obj=$(_shell_escape "$(_json_parse_value)")
echo -n " '$str' $obj"
}
function _json_parse_char()
{
_json_peek_char '"'
if [ $? -eq 0 ]; then
echo "Error: Character contains \"" > /dev/stderr
fi
if [ -z "$(_json_lex_char '\')" ]; then
_json_lex_any_char
return 0
fi
local c
c="$(_json_lex_any_char)"
case $c in
"EOF")
echo "Error: Could not lex" > /dev/stderr
;;
'"')
echo -n '"'
;;
'\\')
echo -n '\\'
;;
'/')
echo -n '/'
;;
'b')
echo -n -e '\\b'
;;
'f')
echo -n -e '\\f'
;;
'n')
echo -n -e '\\n'
;;
'r')
echo -n -e '\\r\'
;;
't')
echo -n -e '\\t'
;;
'u')
local a b d
a="$(_json_lex_hex_digit)"
b="$(_json_lex_hex_digit)"
c="$(_json_lex_hex_digit)"
d="$(_json_lex_hex_digit)"
if [ -n "$a" ] &&
[ -n "$b" ] &&
[ -n "$c" ] &&
[ -n "$d" ]; then
echo -n -e \\u$a$b$c$d
else
echo "Error: \\uXXXX format must 4 hex digits" > /dev/stderr
echo -n "."
fi
;;
*)
echo "Error: Invalid escape sequence" > /dev/stderr
echo -n "."
;;
esac
}
function _json_parse_symbol()
{
local result
result=""
isalnum "$(_json_peek_any_char)" > /dev/null
while [ $? -eq 0 ]; do
result="$result$(_json_lex_any_char)"
isalnum "$(_json_peek_any_char)" > /dev/null
done
echo -n "$result"
}
function _json_parse_string()
{
if [ -z "$(_json_lex_char '"')" ]; then
echo "Error: String must starting with '\"'" > /dev/stderr
fi
while [ true ]; do
if [ "$(_json_peek_any_char)" = "EOF" ]; then
echo "Error: can't find trailing '\"'" > /dev/stderr
return 1
fi
if [ -n "$(_json_lex_char '"')" ]; then
return 0
fi
_json_parse_char
done
}
function _json_parse_digits()
{
local result c
result=""
c="$(_json_lex_one_char "0123456789")"
while [ -n "$c" ]; do
echo -n $c
c="$(_json_lex_one_char "0123456789")"
done
}
function _json_parse_number()
{
local sign p intpart fracpart exppart
sign=$(_json_lex_one_char '-+')
p=$(_json_lex_one_char "0123456789")
if [ -z "$p" ]; then
echo "Error: Can't find number value" > /dev/stderr
echo -n "0.0"
return 0
fi
intpart=$p
if [ "$p" = "0" ]; then
if [ -n "$(_json_lex_one_char 'xX')" ]; then
echo "Error: JSON is not supported Hex Value" > /dev/stderr
elif [ -n "$(_json_peek_one_char '0123456789')" ]; then
echo "Error: JSON is not supported Octet Value" > /dev/stderr
fi
fi
intpart="$intpart$(_json_parse_digits)"
fracpart=""
if [ -n "$(_json_lex_char '.')" ]; then
fracpart=".$(_json_parse_digits)"
if [ $#fracpart -eq 1 ]; then
echo "Error: Required 1 more digits after period" > /dev/stderr
fracpart=".0"
fi
fi
exppart=""
if [ -n "$(_json_lex_one_char 'eE')" ]; then
exppart="e"
if [ -n "$(_json_lex_one_char '-')" ]; then
exppart="e-"
fi
local digits
digits=$(_json_parse_digits)
if [ -z "$digits" ]; then
echo "Error: Required 1 more digits after E sign" > /dev/stderr
digits="0"
fi
exppart="$exppart$digits"
fi
echo -n $(($sign$intpart$fracpart$exppart))
}
function _json_parse_object()
{
if [ -z "$(_json_lex_char '{')" ]; then
echo "Error: object must started with '{'" > /dev/stderr
fi
_json_skip
if [ -n "$(_json_lex_char '}')" ]; then
echo -n "()"
return 0;
fi
echo -n "("
_json_parse_pair
while [ true ]; do
_json_skip
if [ "$(_json_peek_any_char)" = "EOF" ]; then
echo "Error: ']' is not found" > /dev/stderr
echo -n ")"
return 1
fi
if [ -n "$(_json_lex_char '}')" ]; then
echo -n ")"
return 0
fi
if [ -n "$(_json_lex_char ',')" ]; then
_json_skip
if [ -n "$(_json_lex_char '}')" ]; then
echo "Error: JSON is not accepable trailing ','" > /dev/stderr
echo -n ")"
return 1
fi
else
echo "Error: JSON requires ',' between object values" > /dev/stderr
fi
_json_parse_pair
done
}
function _json_parse_array()
{
if [ -z "$(_json_lex_char '[')" ]; then
echo "Error: array must started with '['" > /dev/stderr
fi
_json_skip
if [ -n "$(_json_lex_char ']')" ]; then
echo -n "()"
return 0;
fi
echo -n "("
echo -n "$(_shell_escape "$(_json_parse_value)")"
while [ true ]; do
_json_skip
if [ "$(_json_peek_any_char)" = "EOF" ]; then
echo "Error: ']' is not found" > /dev/stderr
echo -n ")"
return 1
fi
if [ -n "$(_json_lex_char ']')" ]; then
echo -n ")"
return 0
fi
if [ -n "$(_json_lex_char ',')" ]; then
_json_skip
if [ -n "$(_json_lex_char ']')" ]; then
echo "Error: JSON is not accepable trailing ','" > /dev/stderr
echo -n ")"
return 1
fi
else
echo "Error: JSON requires ',' between array values" > /dev/stderr
fi
echo -n "$(_shell_escape "$(_json_parse_value)")"
done
}
function _json_parse_value()
{
local tmp
_json_skip
if [ -n "" ]; then
elif [ -n "$(_json_peek_char '"')" ]; then
echo -n "'$(_json_parse_string)'"
return 0
elif [ -n "$(_json_peek_one_char '0123456789')" ]; then
_json_parse_number
return 0
elif [ -n "$(_json_peek_char '{')" ]; then
_json_parse_object
return 0
elif [ -n "$(_json_peek_char '[')" ]; then
_json_parse_array
return 0
fi
local symbol
symbol=$(_json_parse_symbol)
if [ -z "$symbol" ]; then
echo "Error: can't lex value" > /dev/stderr
_json_lex_any_char > /dev/null
return 1
fi
if [ "$symbol" = "true" ]; then
echo -n "true"
return 0
fi
if [ "$symbol" = "false" ]; then
echo -n "false"
return 0
fi
if [ "$symbol" = "null" ]; then
echo -n "null"
return 0
fi
echo $symbol
return 1
}
function _shell_escape()
{
_bin_hexdump "$1"
}
function _json_get()
{
local obj json
json=$1
shift 1
if [ -n "$(isnumber "$1")" ]; then
typeset -a obj
else
typeset -A obj
fi
eval obj=$json
while [ -n "$1" ]; do
json=$(_bin_hexrestore "$(echo -n "$obj[$1]")")
shift 1
if [ -z "$1" ]; then
echo $json
return 0
fi
if [ -n "$(isnumber "$1")" ]; then
typeset -a obj
else
typeset -A obj
fi
eval obj=$json
done
echo $obj
}
function _json_init()
{
_json_buffer=$1
zss_open
_json_fd=$?
zss_write $_json_fd index 1
}