-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONUtil.vdmsl
More file actions
389 lines (346 loc) · 11.3 KB
/
JSONUtil.vdmsl
File metadata and controls
389 lines (346 loc) · 11.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
module JSONUtil
/***
JSONUtil
Author: Tomohiro Oda
Version: 0.02
License: the MIT License
Caution: JSONUtil does not distinguish "" and []
Copyright (c) 2013 Tomohiro Oda
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***/
imports from VDMUtil
functions
seq_of_char2val[@type]:seq1 of char -> bool * [@type];
val2seq_of_char[@T] : @T +> seq of char;
exports
types JSON; VALUE; STRING; ARRAY; OBJECT; NUMBER; BOOL;
functions
parseJSON : JSON -> bool * VALUE;
strictParseJSON : JSON -> bool * VALUE;
printJSON : VALUE -> JSON;
definitions
types
JSON = seq of char;
STRING = seq of char;
ARRAY = seq of VALUE;
OBJECT = map STRING to VALUE;
NUMBER = real;
BOOL = bool;
VALUE = [ STRING | ARRAY | OBJECT | NUMBER | BOOL ];
values
DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
NULL = "null";
TRUE = "true";
FALSE = "false";
functions
parseJSON : JSON -> bool * VALUE
parseJSON(json) == cases parseValue(json):
mk_(value, -) -> mk_(true, value),
others -> mk_(false, nil)
end
post let mk_(success, value) = RESULT in not success => value = nil;
strictParseJSON : JSON -> bool * VALUE
strictParseJSON(json) == cases parseValue(json):
mk_(value, rest) -> if trimBlanks(rest) = [] then mk_(true, value) else mk_(false, nil),
others -> mk_(false, nil)
end
post let mk_(success, value) = RESULT in not success => value = nil;
printJSON : VALUE -> JSON
printJSON(value) == printValue(value);
/***
printer for each type
***/
printValue : VALUE -> JSON
printValue(value) ==
if value = true then
TRUE
elseif value = false then
FALSE
elseif value = nil then
NULL
elseif is_(value, NUMBER) then
printNumber(value)
elseif is_(value, STRING) then
printString(value)
elseif is_(value, ARRAY) then
printArray(value)
elseif is_(value, OBJECT) then
printObject(value)
else
undefined;
printNumber : NUMBER -> JSON
printNumber(number) == VDMUtil`val2seq_of_char[real](number);
printString : STRING -> JSON
printString(string) == if string = [] then ['\x22', '\x22'] else VDMUtil`val2seq_of_char[seq of char](string);
printBool : BOOL -> JSON
printBool(boolean) == if boolean then TRUE else FALSE;
printArray : ARRAY -> JSON
printArray(array) == cases array:
[head] ^ tail -> "[" ^ printValue(head) ^ printArray0(tail) ^ "]",
others -> "[]"
end;
printArray0 : ARRAY -> JSON
printArray0(array) == cases array:
[head] ^ tail -> ", " ^ printValue(head) ^ printArray0(tail),
others -> []
end;
printObject : OBJECT -> JSON
printObject(object) == printObject0( {mk_(key, object(key)) | key in set dom object});
printObject0 : set of (STRING * VALUE) -> JSON
printObject0(object) == cases object:
{mk_(key, value)} union tail -> "{" ^ printString(key) ^ ":"^ printValue(value)^printObject1(tail) ^ "}",
others -> "{}"
end;
printObject1 : set of (STRING * VALUE) -> JSON
printObject1(object) == cases object:
{mk_(key, value)} union tail -> ", " ^ printString(key)^":"^printValue(value) ^ printObject1(tail),
others -> []
end;
/***
parser for each type
***/
parseValue : JSON -> [VALUE * JSON]
parseValue(json) == cases trimBlanks(json):
[] -> mk_(nil, json),
json0 ->
if hd json0 in set DIGITS or hd json0 in set {'-', '+'} then
parseNumber(json0)
elseif hd json0 = '\x22' then
parseString(json0)
elseif hd json0 = '{' then
parseObject(json0)
elseif hd json0 = '[' then
parseArray(json0)
elseif headmatch(json0, TRUE) then
mk_(true, [json0(i) | i in set {5,...,len json0}])
elseif headmatch(json0, FALSE) then
mk_(false, [json0(i) | i in set {6,...,len json0}])
elseif headmatch(json0, NULL) then
mk_(nil, [json0(i) | i in set {5,...,len json0}])
else nil
end
measure measureJSON;
parseObject : JSON -> [OBJECT * JSON]
parseObject(json) ==
if json = [] or hd json <> '{' then
nil
else
let rest = trimBlanks(tl json) in
if rest <> [] and hd rest = '}' then
mk_({ |-> }, tl rest)
else
parseObject0(rest)
measure measureJSON;
parseObject0 : JSON -> [OBJECT * JSON]
parseObject0(json) == cases parseString(json):
nil -> nil,
mk_(key, r) -> let json1 = trimBlanks(r) in
if json1 = [] or hd json1 <> ':' then
nil
else
cases parseValue(tl json1):
nil -> nil,
mk_(value, json2) -> let json3 = trimBlanks(json2) in
if json3 = [] then
nil
elseif hd json3 = '}' then
mk_({key |-> value}, tl json3)
elseif hd json3 = ',' then
cases parseObject0(trimBlanks(tl json3)):
nil -> nil,
mk_(object, rest1) -> mk_(object++{key |-> value}, rest1)
end
else
nil
end
end
measure measureJSON;
parseArray : JSON -> [ARRAY * JSON]
parseArray(json) ==
if json = [] or hd json <> '[' then
nil
else
let rest = trimBlanks(tl json) in
if rest <> [] and hd rest = ']' then
mk_([], tl rest)
else
parseArray0(tl json)
measure measureJSON;
parseArray0 : JSON -> [ARRAY * JSON]
parseArray0(json) == cases parseValue(json):
nil -> nil,
mk_(value, r) -> let rest = trimBlanks(r) in
if rest = [] then
nil
elseif hd rest = ']' then
mk_([value], tl rest)
elseif hd rest = ',' then
cases parseArray0(tl rest):
nil -> nil,
mk_(array1, rest1) -> mk_([value]^array1, rest1)
end
else
nil
end
measure measureJSON;
parseString : JSON -> [STRING * JSON]
parseString(json) ==
if json = [] or hd json <> '\x22' then
nil
else
parseString0(tl json);
parseString0 : JSON -> [STRING * JSON]
parseString0(json) == cases json:
[] -> nil,
['\x22'] ^ rest -> mk_([], rest),
['\\', '\x22'] ^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\x22'] ^ s, r)
end,
['\\', '\\'] ^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\\']^s, r)
end,
['\\', '/']^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['/']^s, r)
end,
['\\', 'b']^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\x08']^s, r)
end,
['\\', 'f']^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\f']^s, r)
end,
['\\', 'n']^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\n']^s, r)
end,
['\\', 'r']^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\r']^s, r)
end,
['\\', 't']^rest ->cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\t']^s, r)
end,
['\\', 'f']^rest ->cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_(['\f']^s, r)
end,
['\\', 'u']^[u1]^[u2]^[u3]^[u4]^rest ->
cases string2char(['\'', '\\', 'u', u1, u2, u3, u4, '\'']):
nil -> nil,
c -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_([c]^s, r)
end
end,
['\\']^- -> nil,
[c]^rest -> cases parseString0(rest):
nil -> nil,
mk_(s, r) -> mk_([c]^s, r)
end
end
measure measureJSON;
parseNumber : JSON -> [NUMBER * JSON]
parseNumber(json) == cases parseReal(json):
nil -> nil,
mk_(string, rest) -> cases string2real(string):
nil -> nil,
c -> mk_(c, rest)
end
end;
parseReal: JSON -> [JSON * JSON]
parseReal(json) == cases parseInt(json):
nil -> nil,
mk_(i, rest) ->
if rest = [] then
mk_(i, rest)
elseif hd rest = '.' then
cases parseDecimal(tl rest):
nil -> nil,
mk_(d, rest2) -> mk_(i^"."^d, rest2)
end
elseif hd rest in set {'e', 'E'} then
cases parseInt(tl rest):
nil -> nil,
mk_(e, rest2) -> mk_(i^[hd rest]^e, rest2)
end
else
mk_(i, rest)
end;
parseDecimal : JSON -> [JSON * JSON]
parseDecimal(json) == cases parseNat(json):
nil -> nil,
mk_(n, rest) ->
if rest = [] then
mk_(n, rest)
elseif hd rest in set {'e', 'E'} then
cases parseInt(tl rest):
nil -> nil,
mk_(e, rest2) -> mk_(n^[hd rest]^e, rest2)
end
else
mk_(n, rest)
end;
parseInt : JSON -> [JSON * JSON]
parseInt(json) ==
if json = [] then
nil
elseif hd json in set {'-', '+'} then
cases parseNat(tl json):
nil -> nil,
mk_(i, rest) -> mk_([hd json]^i, rest)
end
else parseNat(json);
parseNat: JSON -> [JSON * JSON]
parseNat(json) == cases json:
[] -> nil,
['0']^rest -> mk_("0", rest),
others -> if hd json in set DIGITS then parseNat0(json) else nil
end;
parseNat0 : JSON -> JSON * JSON
parseNat0(json) == cases json:
[] -> mk_([], json),
[c]^rest -> if c in set DIGITS then let mk_(n, r) = parseNat0(rest) in mk_([c]^n, r) else mk_([], json)
end
measure measureJSON;
/***
utilities
***/
headmatch : JSON * JSON -> bool
headmatch(json, pattern) == len json >= len pattern and [json(i) | i in set inds pattern] = pattern;
trimBlanks : JSON -> JSON
trimBlanks(json) == if json = [] or hd json not in set {' ', '\t', '\n', '\r'} then json else trimBlanks(tl json)
measure measureJSON;
string2char : seq of char -> [char]
string2char(string) == let mk_(-,c)=VDMUtil`seq_of_char2val[char](string) in c;
string2real : seq of char -> [real]
string2real(string) == let mk_(-,c)=VDMUtil`seq_of_char2val[real](string) in c;
measureJSON : JSON -> nat
measureJSON(json) == len json;
end JSONUtil
module VDMUtil
exports all
definitions
functions
seq_of_char2val[@type]:seq1 of char -> bool * [@type]
seq_of_char2val(string) == is not yet specified;
val2seq_of_char[@type] : @type +> seq of char
val2seq_of_char(x) == is not yet specified;
end VDMUtil