-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsonreader.functions
More file actions
673 lines (521 loc) · 13.3 KB
/
jsonreader.functions
File metadata and controls
673 lines (521 loc) · 13.3 KB
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# This file is part of shellfire jsonreader. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/jsonreader/master/COPYRIGHT. No part of shellfire jsonreader, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire jsonreader. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/jsonreader/master/COPYRIGHT.
core_usesIn unicode utf8 utf16
jsonreader_path_index=[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
core_dependency_declares jsonreader_path_index
jsonreader_eventMatches()
{
local pathGlob="$1"
if ! core_variable_matches "$jsonreader_path" $pathGlob; then
return 1
elif [ $# -eq 1 ]; then
return 0
fi
local variant="$2"
if [ "$variant" != "$eventVariant" ]; then
return 1
fi
return 0
}
core_usesIn core file
jsonreader_parse()
{
local jsonFilePath="$1"
local _jsonreader_eventCallback="$2"
if core_variable_isUnset _jsonreader_SP; then
_jsonreader_initialise
fi
local eventKind='root'
local eventIndex=0
local eventKey=''
local buffer=''
local character=''
local pushBackForNumberParsing=''
local eofIsAllowedInParsingNumberBecauseThisIsRootValue=yes
local jsonreader_path=''
local TMP_FILE
core_temporaryFiles_newFileToRemoveOnExit
local singleCharactersFile="$TMP_FILE"
core_file_characterByCharacterCreate "$jsonFilePath" "$singleCharactersFile"
{
_jsonreader_skipWhitespace
_jsonreader_parseValue
_jsonreader_skipFinalWhitespace
} <"$singleCharactersFile"
rm -f "$singleCharactersFile"
}
_jsonreader_raiseEvent()
{
# eventKind
# root / array / object
# eventIndex (number)
# eventKey (if set)
# start / end / boolean / string / number
local eventVariant="$1"
# null / true / false / number / string / '' / count
local eventValue="$2"
$_jsonreader_eventCallback
}
_jsonreader_parseValue()
{
case "$character" in
'n')
_jsonreader_parseNull
;;
't')
_jsonreader_parseTrue
;;
'f')
_jsonreader_parseFalse
;;
'"')
_jsonreader_parseString true
;;
'-'|'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9')
_jsonreader_parseNumber
;;
'[')
eofIsAllowedInParsingNumberBecauseThisIsRootValue=no
_jsonreader_parseArray
;;
'{')
eofIsAllowedInParsingNumberBecauseThisIsRootValue=no
_jsonreader_parseObject
;;
*)
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect '$character'"
;;
esac
}
_jsonreader_parseArray()
{
local originalPath="$jsonreader_path"
local jsonreader_path="${originalPath}:"
local parentEventKind="$eventKind"
local eventKind=array
_jsonreader_raiseEvent start "$parentEventKind"
local oldEventIndex=$eventIndex
local oldEventKey="$eventKey"
local eventIndex=0
local eventKey=''
while true
do
_jsonreader_skipWhitespace
if [ "$character" = ']' ]; then
jsonreader_path="${originalPath}:"
local eventCount=$((eventIndex+1))
eventIndex=$oldEventIndex
eventKey="$oldEventKey"
_jsonreader_raiseEvent end "$parentEventKind"
return 0
fi
if [ $eventIndex -ne 0 ]; then
if [ "$character" != ',' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect '$character' instead of comma in array"
fi
_jsonreader_skipWhitespace
fi
jsonreader_path="${originalPath}:$(printf '%08d' "$eventIndex")"
_jsonreader_parseValue
eventIndex=$((eventIndex+1))
done
}
_jsonreader_parseObject()
{
local originalPath="$jsonreader_path"
local jsonreader_path="${originalPath}/"
local parentEventKind="$eventKind"
local eventKind=object
_jsonreader_raiseEvent start "$parentEventKind"
local oldEventIndex=$eventIndex
local oldEventKey="$eventKey"
local eventIndex=0
local eventKey=''
while true
do
_jsonreader_skipWhitespace
if [ "$character" = '}' ]; then
jsonreader_path="${originalPath}/"
local eventCount=$((eventIndex+1))
eventIndex=$oldEventIndex
eventKey="$oldEventKey"
_jsonreader_raiseEvent end "$parentEventKind"
return 0
fi
if [ $eventIndex -ne 0 ]; then
if [ "$character" != ',' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect '$character' instead of comma in object"
fi
_jsonreader_skipWhitespace
fi
if [ "$character" != '"' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect '$character' to start object key"
fi
_jsonreader_parseString false
eventKey="$buffer"
jsonreader_path="${originalPath}/"
_jsonreader_skipWhitespace
if [ "$character" != ':' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect '$character'after object key"
fi
_jsonreader_skipWhitespace
jsonreader_path="${originalPath}/${eventKey}"
_jsonreader_parseValue
eventIndex=$((eventIndex+1))
done
}
_jsonreader_parseNull()
{
buffer="$character"
{
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
} || _jsonreader_errorEndOfFile
if [ "$buffer" != 'null' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected 'null', not '$buffer'"
fi
_jsonreader_raiseEvent boolean null
buffer=''
}
_jsonreader_parseTrue()
{
buffer="$character"
{
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
} || _jsonreader_errorEndOfFile
if [ "$buffer" != 'true' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected 'true', not '$buffer'"
fi
_jsonreader_raiseEvent boolean true
buffer=''
}
_jsonreader_parseFalse()
{
buffer="$character"
{
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
_jsonreader_nextToBuffer
} || _jsonreader_errorEndOfFile
if [ "$buffer" != 'false' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected 'false', not '$buffer'"
fi
_jsonreader_raiseEvent boolean false
buffer=''
}
_jsonreader_parseNumber()
{
# We can't know we're "done" with a number until we drop off the end
# For object, array, we don't
# For string, true, false, null, we don't
# We can legitimately have EOF if this at the root level, thanks to RFC 7159
local number="$character"
case "$character" in
'-')
_jsonreader_next || _jsonreader_errorEndOfFile
number="${number}${character}"
;;
esac
if [ "$character" = '0' ]; then
_jsonreader_next || _jsonreader_errorEndOfFileIfNotParsingRootNumber
case "$character" in
'.')
_jsonreader_parseNumber_fraction
return 0
;;
'e'|'E')
_jsonreader_parseNumber_exponent
return 0
;;
*)
pushBackForNumberParsing="$character"
# can be '-0'
_jsonreader_raiseEvent number "$number"
return 0
;;
esac
fi
while true
do
_jsonreader_next || _jsonreader_errorEndOfFileIfNotParsingRootNumber
case "$character" in
'.')
_jsonreader_parseNumber_fraction
return 0
;;
'e'|'E')
_jsonreader_parseNumber_exponent
return 0
;;
[0-9])
number="${number}${character}"
;;
*)
pushBackForNumberParsing="$character"
_jsonreader_raiseEvent number "$number"
return 0
;;
esac
done
}
_jsonreader_parseString()
{
local raiseEvent="$1"
buffer=''
while _jsonreader_next
do
case "$character" in
'"')
if $raiseEvent; then
_jsonreader_raiseEvent string "$buffer"
fi
return 0
;;
'\')
_jsonreader_parseStringEscape
;;
*)
buffer="${buffer}${character}"
;;
esac
done
_jsonreader_errorEndOfFile
}
_jsonreader_initialise()
{
_jsonreader_SP=' '
_jsonreader_HT="$(printf '\t')"
_jsonreader_CR="$(printf '\r')"
_jsonreader_LF='
'
}
_jsonreader_errorEndOfFile()
{
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect EOF"
}
_jsonreader_skipWhitespace()
{
while _jsonreader_next
do
case "$character" in
"$_jsonreader_SP"|"$_jsonreader_HT"|"$_jsonreader_CR"|"$_jsonreader_LF")
continue
;;
*)
return 0
;;
esac
done
_jsonreader_errorEndOfFile
}
_jsonreader_skipFinalWhitespace()
{
while _jsonreader_next
do
case "$character" in
"$_jsonreader_SP"|"$_jsonreader_HT"|"$_jsonreader_CR"|"$_jsonreader_LF")
continue
;;
*)
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing did not expect non-whitespace '$character' after root value"
;;
esac
done
}
_jsonreader_parseStringEscape()
{
_jsonreader_next || _jsonreader_errorEndOfFile
case "$character" in
'"'|'\'|'/')
buffer="${buffer}${character}"
return 0
;;
'n')
buffer="${buffer}
"
return 0
;;
'b'|'f'|'r'|'t')
buffer="${buffer}$(printf "\\${character}")"
return 0
;;
'u')
:
;;
*)
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected an escape, not character '$character'"
;;
esac
# Unicode escape, high surrogate
local unicodeValue=0
_jsonreader_parseStringEscape_readUnicodeSequence
if _jsonreader_parseStringEscape_isNotHighSurrogate $unicodeValue; then
buffer="${buffer}$(utf8_encodeCodePoint "$unicodeValue")"
return 0
fi
# Unicode escape, low surrogate
local firstUnicodeValue="$unicodeValue"
{
_jsonreader_next
if [ "$character" != '\' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected the '\' start of a low surrogate \u escape, not '$character'"
fi
_jsonreader_next
if [ "$character" != 'u' ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected the 'u' of a '\i' start of a low surrogate \u escape, not '$character'"
fi
} || _jsonreader_errorEndOfFile
unicodeValue=0
_jsonreader_parseStringEscape_readUnicodeSequence
_jsonreader_parseStringEscape_isLowSurrogateOnly
local codePoint=$(unicode_utf16_surrogatePairToCodePoint "$firstUnicodeValue" "$unicodeValue")
buffer="${buffer}$(utf8_encodeCodePoint "$codePoint")"
}
_jsonreader_parseStringEscape_readUnicodeSequence()
{
local unicodeEscape='0x'
local count=0
while [ $count -lt 4 ]
do
_jsonreader_next || _jsonreader_errorEndOfFile
case "$character" in
[0-9]|[A-F]|[a-f])
unicodeSequence="${unicodeSequence}${character}"
;;
*)
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected an Unicode character in a string \u escape, not '$character'"
;;
esac
done
unicodeEscape="${unicodeEscape}${character}"
unicodeValue=$((unicodeEscape+0))
}
# aka leading surrogate
_jsonreader_parseStringEscape_isHighSurrogate()
{
if [ $1 -lt 55296 ]; then
return 0
fi
if [ $1 -gt 57343 ]; then
return 0
fi
if [ $1 -gt 56319 ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected a high surrogate, not a low surrogate"
fi
return 1
}
_jsonreader_parseStringEscape_isLowSurrogateOnly()
{
if [ $1 -lt 55296 ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected a low surrogate"
fi
if [ $1 -gt 57343 ]; then
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected a low surrogate"
fi
if [ $1 -gt 56319 ]; then
return 0
fi
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected a low surrogate"
}
_jsonreader_parseNumber_fraction()
{
number="${number}."
_jsonreader_next || _jsonreader_errorEndOfFile
case "$character" in
[0-9])
number="${number}${character}"
;;
*)
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected a digit after fraction (.) in number, not '$character'"
;;
esac
while true
do
_jsonreader_next || _jsonreader_errorEndOfFileIfNotParsingRootNumber
case "$character" in
[0-9])
number="${number}${character}"
;;
'e'|'E')
_jsonreader_parseNumber_exponent
;;
*)
pushBackForNumberParsing="$character"
_jsonreader_raiseEvent number "$number"
return 0
;;
esac
done
}
_jsonreader_parseNumber_exponent()
{
number="${number}E"
_jsonreader_next || _jsonreader_errorEndOfFile
case "$character" in
'+'|'-')
number="${number}${character}"
_jsonreader_next || _jsonreader_errorEndOfFile
;;
*)
number="${number}+"
;;
esac
case "$character" in
[0-9])
number="${number}${character}"
;;
*)
core_exitError $core_commandLine_exitCode_DATAERR "JSON parsing expected a digit after exponent in number, not '$character'"
;;
esac
while true
do
_jsonreader_next || _jsonreader_errorEndOfFileIfNotParsingRootNumber
case "$character" in
[0-9])
number="${number}${character}"
;;
*)
pushBackForNumberParsing="$character"
_jsonreader_raiseEvent number "$number"
return 0
;;
esac
done
}
_jsonreader_next()
{
if [ -n "$pushBackForNumberParsing" ]; then
character="$pushBackForNumberParsing"
pushBackForNumberParsing=''
return
fi
set +e
IFS='' read -r character
local exitCode=$?
set -e
if [ $exitCode -ne 0 ]; then
return 1
fi
if [ -z "$character" ]; then
character='
'
fi
}
_jsonreader_errorEndOfFileIfNotParsingRootNumber()
{
if core_variable_isTrue "$eofIsAllowedInParsingNumberBecauseThisIsRootValue"; then
_jsonreader_raiseEvent number "$number"
else
_jsonreader_errorEndOfFile
fi
}
_jsonreader_nextToBuffer()
{
_jsonreader_next
buffer="${buffer}${character}"
}