forked from dominictarr/JSON.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.sh
93 lines (83 loc) · 1.8 KB
/
parse.sh
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
throw () {
echo $* >&2
exit 1
}
tokenize () {
egrep -ao '[]|[{}]|:|,|("((\\")|[^"])*")|:|(\-?[0-9]*\.?([0-9]*)?(e?\-?([0-9]*))?)|null|true|false' --color=never
}
parse_array () {
local index=0
local ary=''
read token
while true;
do
key=$index
case "$token" in
']') break ;;
*)
parse_value "$1" "$index"
let index=$index+1
ary="$ary""$value"
read token
case "$token" in
']') break ;;
',') ary="$ary", ;;
*)
if [ "_$token" = _ ]; then token=EOF; fi
throw "EXPECTED ] or , GOT $token"
;;
esac
read token
;;
esac
done
value=`printf '[%s]' $ary`
}
parse_object () {
local go=true
local obj=''
local EXPECT_COMMA=0
local EXPECT_COLON=0
read token
while [ "$go" = true ];
do
case "$token" in
'}') break ;;
*)
key=$token
read colon
if [ "$colon" != ':' ]; then throw "EXPECTED COLON, GOT $colon"; fi
if [ "_$key" = _ ]; then throw "NULL KEY"; fi
read token
parse_value "$1" "$key"
obj="$obj$key:$value"
read token
case "$token" in
'}') break;;
,) obj="$obj,"; read token ;;
*)
if [ "_$token" = _ ]; then token=EOF; fi
throw "EXPECTED , or }, but got $token"
;;
esac
;;
esac
done
value=`printf '{%s}' "$obj"`
}
parse_value () {
local jpath
if [ "x$1" = "x" ]; then jpath="$2"; else jpath="$1,$2"; fi
case "$token" in
{) parse_object "$jpath" ;;
[) parse_array "$jpath" ;;
','|'}'|']') throw "EXPECTED value, GOT $token" ;;
*) value=$token
;;
esac
printf "[%s]\t%s\n" "$jpath" "$value"
}
parse () {
read token
parse_value
}