1
|
open Yojson.Safe
|
2
|
open Yojson.Safe.Util
|
3
|
|
4
|
let rec assoc_map_except_str l f str =
|
5
|
match l with
|
6
|
| (s,x)::y ->
|
7
|
if (String.equal s str) then
|
8
|
assoc_map_except_str y f str
|
9
|
else
|
10
|
(s,f str x)::assoc_map_except_str y f str
|
11
|
| [] -> []
|
12
|
|
13
|
let rec map_2_args f l arg1 =
|
14
|
match l with
|
15
|
| hd::tl -> (f arg1 hd)::(map_2_args f tl arg1)
|
16
|
| [] -> []
|
17
|
|
18
|
(*
|
19
|
Remove `Assoc nodes with tag 'str' in json j
|
20
|
*)
|
21
|
let rec prune_str str json =
|
22
|
match json with
|
23
|
| `Assoc ((t,hd)::tl) ->
|
24
|
if (String.equal str t) then
|
25
|
`Assoc (assoc_map_except_str tl prune_str str)
|
26
|
else
|
27
|
`Assoc ((t, prune_str str hd)::(assoc_map_except_str tl prune_str str))
|
28
|
| `List (hd::tl) -> `List ((prune_str str hd)::(map_2_args prune_str tl str))
|
29
|
| `String (s) -> if (String.equal str s) then `String ("") else `String (s)
|
30
|
| x -> x
|
31
|
|
32
|
(*******************)
|
33
|
|
34
|
let rec name_pair_list_to_string l =
|
35
|
match l with
|
36
|
| (t, `String(x))::tl ->
|
37
|
if (String.equal t "name") then
|
38
|
(x::name_pair_list_to_string tl)
|
39
|
else
|
40
|
(name_pair_list_to_string tl)
|
41
|
| _ -> []
|
42
|
|
43
|
let rec assoc_filter_string l str =
|
44
|
match l with
|
45
|
| `Assoc (x) -> name_pair_list_to_string x
|
46
|
| _ -> []
|
47
|
|
48
|
(********************)
|
49
|
|
50
|
let rec pairlist_remove str l f =
|
51
|
match l with
|
52
|
| (t,j)::tl ->
|
53
|
if (String.equal t str) then
|
54
|
(f j)::(pairlist_remove str tl f)
|
55
|
else
|
56
|
`Assoc ((t, f j)::[])::(pairlist_remove str tl f)
|
57
|
| [] -> []
|
58
|
|
59
|
(******************)
|
60
|
let rec assoc_elem_fst pair_list =
|
61
|
match pair_list with
|
62
|
| (t,j)::tl -> t::(assoc_elem_fst tl)
|
63
|
| [] -> []
|
64
|
|
65
|
let rec assoc_elem_snd pair_list =
|
66
|
match pair_list with
|
67
|
| (t,j)::tl -> j::(assoc_elem_snd tl)
|
68
|
| [] -> []
|
69
|
|
70
|
let rec assoc_elem_filter pair_list str =
|
71
|
match pair_list with
|
72
|
| (t,j)::tl -> if (String.equal t str) then
|
73
|
(t,j)::(assoc_elem_filter tl str)
|
74
|
else assoc_elem_filter tl str
|
75
|
| [] -> []
|
76
|
|
77
|
let rec assoc_elem_filternot pair_list str =
|
78
|
match pair_list with
|
79
|
| (t,j)::tl -> if (not (String.equal t str)) then
|
80
|
(t,j)::(assoc_elem_filternot tl str)
|
81
|
else assoc_elem_filternot tl str
|
82
|
| [] -> []
|
83
|
|
84
|
let rec assoc_elem_filter_snd pair_list str =
|
85
|
match pair_list with
|
86
|
| (t,j)::tl -> if (String.equal t str) then
|
87
|
j::(assoc_elem_filter_snd tl str)
|
88
|
else assoc_elem_filter_snd tl str
|
89
|
| [] -> []
|
90
|
|
91
|
let rec assoc_elem_filternot_snd pair_list str =
|
92
|
match pair_list with
|
93
|
| (t,j)::tl -> if (not (String.equal t str)) then
|
94
|
j::(assoc_elem_filter_snd tl str)
|
95
|
else assoc_elem_filter_snd tl str
|
96
|
| [] -> []
|
97
|
|
98
|
let rec pairlist_snd_as_list pair_list str =
|
99
|
match pair_list with
|
100
|
| (t,j)::tl -> if (String.equal t str) then
|
101
|
(t,`List (j::[]))::(pairlist_snd_as_list tl str)
|
102
|
else (t,j)::(pairlist_snd_as_list tl str)
|
103
|
| [] -> []
|
104
|
|
105
|
let all_members str json =
|
106
|
match json with
|
107
|
| `Assoc (l) -> assoc_elem_filter_snd l str
|
108
|
| _ -> []
|
109
|
|
110
|
let retain_other_members str json =
|
111
|
match json with
|
112
|
| `Assoc (l) -> `Assoc (assoc_elem_filter l str)
|
113
|
| _ -> `Null
|
114
|
|
115
|
(*
|
116
|
DESIGN_UNIT as lists
|
117
|
*)
|
118
|
let vhdl_json_designunits_content_as_list json =
|
119
|
let designunits_contents = json |> member "DESIGN_FILE" |> all_members "DESIGN_UNIT" in
|
120
|
`List designunits_contents
|
121
|
|
122
|
let vhdl_json_designfile_content_excluding json str =
|
123
|
json |> member "DESIGN_FILE" |> retain_other_members "DESIGN_UNIT"
|
124
|
|
125
|
let vhdl_json_list_designunits json =
|
126
|
let designunits_list = vhdl_json_designunits_content_as_list json in
|
127
|
`Assoc (("DESIGN_FILE", (`Assoc (("DESIGN_UNIT", designunits_list)::[])))::[])
|
128
|
|
129
|
let rec pairlist_contains_str str l =
|
130
|
match l with
|
131
|
| (t,j)::tl -> if (String.equal t str) then true else pairlist_contains_str str tl
|
132
|
| [] -> false
|
133
|
|
134
|
(*
|
135
|
ITEM element content as list
|
136
|
*)
|
137
|
let assoc_elem_as_list str json =
|
138
|
match json with
|
139
|
| `Assoc (l) -> `Assoc (pairlist_snd_as_list l str)
|
140
|
| x -> x
|
141
|
|
142
|
let rec map_list map_f l f =
|
143
|
match l with
|
144
|
| hd::tl -> (map_f (f hd) f)::(map_list map_f tl f)
|
145
|
| [] -> []
|
146
|
|
147
|
let rec map_pairlist map_f l f =
|
148
|
match l with
|
149
|
| (t,j)::tl -> (t, map_f (f j) f)::(map_pairlist map_f tl f)
|
150
|
| [] -> []
|
151
|
|
152
|
let rec map_snd f l =
|
153
|
match l with
|
154
|
| (t,j)::tl -> (t,f j)::(map_snd f tl)
|
155
|
| [] -> []
|
156
|
|
157
|
let rec map_all json f =
|
158
|
match json with
|
159
|
| `Assoc ((t,j)::tl) ->
|
160
|
`Assoc ((t,(map_all (f j) f))::(map_pairlist map_all tl f))
|
161
|
| `List (hd::tl) ->
|
162
|
`List ((map_all (f hd) f)::(map_list map_all tl f))
|
163
|
| x -> x
|
164
|
|
165
|
let numeric_literal_simpl json =
|
166
|
match json with
|
167
|
| `Assoc (("NUMERIC_LITERAL", `Assoc (("TOKEN", `Assoc (("text", `String(x))::[]))::[]))::[]) -> `String (x)
|
168
|
| x -> x
|
169
|
|
170
|
let flatten_numeric_literal json =
|
171
|
map_all json (numeric_literal_simpl)
|
172
|
|
173
|
let to_list_str str json =
|
174
|
map_all json (assoc_elem_as_list str)
|
175
|
|
176
|
let rec to_list_content_str str json =
|
177
|
match json with
|
178
|
| `Assoc (l) -> if (pairlist_contains_str str l) then
|
179
|
`Assoc (
|
180
|
(str, to_list_content_str str (`List (assoc_elem_filter_snd l str)))
|
181
|
::(assoc_elem_filternot (map_snd (to_list_content_str str) l) str)
|
182
|
)
|
183
|
else
|
184
|
`Assoc (map_snd (to_list_content_str str) l)
|
185
|
| `List (hd::tl) -> `List ((to_list_content_str str hd)::(List.map (to_list_content_str str) tl))
|
186
|
| x -> x
|
187
|
|
188
|
let rec prune_null_assoc json =
|
189
|
match json with
|
190
|
| `Assoc ((t, `Assoc([]))::tl) -> prune_null_assoc (`Assoc tl)
|
191
|
| `Assoc ((t, `Null)::tl) -> prune_null_assoc (`Assoc tl)
|
192
|
| `Assoc ((t, j)::tl) -> `Assoc ((t, (prune_null_assoc j))::(map_snd prune_null_assoc tl))
|
193
|
| `List (`Null::[]) -> `Null
|
194
|
| `List (l) -> `List (List.map prune_null_assoc l)
|
195
|
| x -> x
|
196
|
|
197
|
(*
|
198
|
Value printers
|
199
|
*)
|
200
|
let rec print_depth json depth indent =
|
201
|
if (depth > 0) then
|
202
|
match json with
|
203
|
| `Assoc ((t,j)::tl) ->
|
204
|
(indent^t)::(List.append (print_depth j (depth-1) (indent^" "))
|
205
|
(print_depth (`Assoc (tl)) depth indent))
|
206
|
| `List (hd::tl) ->
|
207
|
List.append (print_depth hd depth indent)
|
208
|
(print_depth (`List (tl)) depth indent)
|
209
|
| `String (s) -> (indent^s)::[]
|
210
|
| _ -> []
|
211
|
else
|
212
|
[]
|
213
|
|
214
|
let rec flatten_ivd json =
|
215
|
match json with
|
216
|
| `Assoc ((t, `List (l))::[]) -> if (String.equal t "INTERFACE_VARIABLE_DECLARATION") then
|
217
|
`List (List.map flatten_ivd l) else `Assoc ((t, flatten_ivd (`List(l)))::[])
|
218
|
| `Assoc (l) -> `Assoc (map_snd flatten_ivd l)
|
219
|
| `List (hd::tl) -> `List((flatten_ivd hd)::(List.map flatten_ivd tl))
|
220
|
| x -> x
|
221
|
|
222
|
(*
|
223
|
let do_stuff json =
|
224
|
match json with
|
225
|
| `Assoc ((t,j)::tl) ->
|
226
|
| `List (hd::tl) ->
|
227
|
| `String (s) ->
|
228
|
| _ -> x
|
229
|
*)
|