-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjson.m
596 lines (487 loc) · 16.2 KB
/
json.m
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
% Bower - a frontend for the Notmuch email system
% Copyright (C) 2011 Peter Wang
:- module json.
:- interface.
:- import_module bool.
:- import_module integer.
:- import_module list.
:- import_module map.
:- import_module parsing_utils.
%-----------------------------------------------------------------------------%
:- type json
---> null
; bool(bool)
; int(int)
; integer(integer) % only if too big for int()
; float(float)
; string(esc_string)
; list(list(json)) % array
; map(map(string, json)). % object
:- type esc_string.
:- pred parse_json(string::in, parse_result(json)::out) is cc_multi.
:- func unescape(esc_string) = string.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module float.
:- import_module int.
:- import_module maybe.
:- import_module string.
:- import_module unit.
:- import_module string_util.
:- type esc_string
---> esc_string(string).
% RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
%-----------------------------------------------------------------------------%
parse_json(Input, ParseResult) :-
parsing_utils.parse(Input, skip_ws, parse_json_eof, ParseResult).
:- pred parse_json_eof(src::in, json::out, ps::in, ps::out) is semidet.
parse_json_eof(Src, Value, !PS) :-
skip_ws(Src, _, !PS),
value(Src, Value, !PS),
eof(Src, _, !PS).
:- pred skip_ws(src::in, unit::out, ps::in, ps::out) is semidet.
skip_ws(Src, unit, !PS) :-
(
next_char_np(Src, C, !PS),
ws(C)
->
skip_ws(Src, _, !PS)
;
true
).
:- pred ws(char::in) is semidet.
ws(' ').
ws('\t').
ws('\n').
ws('\r').
:- pred next_char_np(src::in, char::out, ps::in, ps::out) is semidet.
next_char_np(Src, C, !PS) :-
% All calls to next_char_np can be changed to next_char_no_progress
% when compatibility with Mercury 11.07 is dropped.
parsing_utils.next_char(Src, C, !PS).
:- pred value(src::in, json::out, ps::in, ps::out) is semidet.
value(Src, Value, !PS) :-
next_char_np(Src, C, !.PS, _PS),
(
C = ('n'),
keyword(id_chars, "null", Src, _, !PS),
Value = null
;
C = ('f'),
keyword(id_chars, "false", Src, _, !PS),
Value = bool(no)
;
C = ('t'),
keyword(id_chars, "true", Src, _, !PS),
Value = bool(yes)
;
C = ('['),
array(Src, List, !PS),
Value = list(List)
;
C = ('{'),
object(Src, Map, !PS),
Value = map(Map)
;
( C = ('-')
; C = ('0') ; C = ('1') ; C = ('2') ; C = ('3') ; C = ('4')
; C = ('5') ; C = ('6') ; C = ('7') ; C = ('8') ; C = ('9')
),
number(Src, Number, !PS),
Value = Number
;
C = ('"'),
string(Src, String, !PS),
Value = string(String)
).
:- func id_chars = string.
id_chars = "abcdefghijklmnopqrstuvwxyz".
%-----------------------------------------------------------------------------%
:- pred array(src::in, list(json)::out, ps::in, ps::out) is semidet.
array(Src, List, !PS) :-
brackets("[", "]", comma_separated_list(value), Src, List, !PS).
%-----------------------------------------------------------------------------%
:- pred object(src::in, map(string, json)::out, ps::in, ps::out)
is semidet.
object(Src, Map, !PS) :-
punct("{", Src, _, !PS),
( object_member(Src, Name, Value, !PS) ->
Map0 = map.singleton(Name, Value),
object_members(Src, Map0, Map, !PS)
;
Map = map.init
),
punct("}", Src, _, !PS).
:- pred object_members(src::in, map(string, json)::in, map(string, json)::out,
ps::in, ps::out) is semidet.
object_members(Src, !Map, !PS) :-
( punct(",", Src, _, !PS) ->
PS0 = !.PS,
object_member(Src, Name, Value, !PS),
( map.insert(Name, Value, !Map) ->
object_members(Src, !Map, !PS)
;
fail_with_message("duplicate field name", Src, !:Map, PS0, !:PS)
)
;
true
).
:- pred object_member(src::in, string::out, json::out, ps::in, ps::out)
is semidet.
object_member(Src, Name, Value, !PS) :-
string(Src, EscName, !PS),
Name = unescape(EscName),
punct(":", Src, _, !PS),
value(Src, Value, !PS).
%-----------------------------------------------------------------------------%
:- pred number(src::in, json::out, ps::in, ps::out) is semidet.
number(Src, Number, !PS) :-
current_offset(Src, Start, !PS),
optional(minus, Src, _, !PS),
int(Src, _, !PS),
( frac(Src, _, !PS) ->
HasFrac = yes
;
HasFrac = no
),
( exp(Src, _, !PS) ->
HasExp = yes
;
HasExp = no
),
current_offset(Src, End, !PS),
input_substring(Src, Start, End, NumberString),
( if HasFrac = yes ; HasExp = yes then
string.to_float(NumberString, Float),
not is_nan_or_inf(Float),
Number = float(Float)
else if string.to_int(NumberString, Int) then
Number = int(Int)
else if integer_from_string(NumberString, Integer) then
Number = integer(Integer)
else
fail
),
skip_ws(Src, unit, !PS).
:- pred 'DIGIT'(src::in, char::out, ps::in, ps::out) is semidet.
'DIGIT'(Src, C, !PS) :-
next_char_np(Src, C, !PS), % for better error messages
char.is_digit(C).
:- pred digits0(src::in, unit::out, ps::in, ps::out) is det.
digits0(Src, unit, !PS) :-
( 'DIGIT'(Src, _, !PS) ->
digits0(Src, _, !PS)
;
true
).
:- pred digits1(src::in, unit::out, ps::in, ps::out) is semidet.
digits1(Src, unit, !PS) :-
'DIGIT'(Src, _, !PS),
digits0(Src, _, !PS).
:- pred int(src::in, unit::out, ps::in, ps::out) is semidet.
int(Src, unit, !PS) :-
'DIGIT'(Src, C, !PS),
( C = ('0') ->
true
;
digits0(Src, unit, !PS)
).
:- pred frac(src::in, unit::out, ps::in, ps::out) is semidet.
frac(Src, unit, !PS) :-
next_char(Src, '.', !PS), % progress
digits1(Src, _, !PS).
:- pred exp(src::in, unit::out, ps::in, ps::out) is semidet.
exp(Src, unit, !PS) :-
e(Src, _, !PS),
optional(minus_or_plus, Src, _, !PS),
digits1(Src, _, !PS).
:- pred e(src::in, unit::out, ps::in, ps::out) is semidet.
e(Src, unit, !PS) :-
next_char(Src, E, !PS),
( E = ('e')
; E = ('E')
).
:- pred minus(src::in, unit::out, ps::in, ps::out) is semidet.
minus(Src, unit, !PS) :-
next_char(Src, '-', !PS).
:- pred minus_or_plus(src::in, unit::out, ps::in, ps::out) is semidet.
minus_or_plus(Src, unit, !PS) :-
next_char(Src, C, !PS),
( C = ('-')
; C = ('+')
).
:- pred integer_from_string(string::in, integer::out) is semidet.
integer_from_string(Str, Integer) :-
% Mercury 14.01 only has integer.from_string/1
% Mercury ROTD 2020-08-18+ only has integer.from_string/2
% Maintain compatibility with both older and newer compilers
% for now by using integer.det_from_string/1.
promise_equivalent_solutions [Res] (
( try []
I = integer.det_from_string(Str)
then
Res = yes(I)
catch_any _Exception ->
Res = no
)
),
Res = yes(Integer).
%-----------------------------------------------------------------------------%
:- pred string(src::in, esc_string::out, ps::in, ps::out) is semidet.
string(Src, esc_string(EscString), !PS) :-
next_char(Src, '"', !PS),
current_offset(Src, Start, !PS),
string_tail(Src, End, !PS),
input_substring(Src, Start, End, EscString),
skip_ws(Src, _, !PS).
:- pred string_tail(src::in, int::out, ps::in, ps::out) is semidet.
string_tail(Src, End, !PS) :-
PS0 = !.PS,
next_char(Src, C, !PS),
( C = ('"') ->
current_offset(Src, End, PS0, _)
; C = ('\\') ->
escape_sequence(Src, _, PS0, !PS),
string_tail(Src, End, !PS)
; unescaped_char(C) ->
string_tail(Src, End, !PS)
;
fail_with_message("invalid character", Src, End, PS0, !:PS)
).
:- pred unescaped_char(char::in) is semidet.
unescaped_char(C) :-
char.to_int(C, I),
% 0x22 (") excluded earlier.
% 0x5C (\) excluded earlier.
0x20 =< I, I =< 0x10ffff.
:- pred escape_sequence(src::in, char::out, ps::in, ps::in, ps::out)
is semidet.
escape_sequence(Src, Char, PS0, !PS) :-
next_char(Src, C1, !PS),
( simple_escape(C1) ->
Char = C1
; C1 = ('u') ->
unicode_escape_sequence(Src, Char, PS0, !PS)
;
fail_with_message("invalid escape sequence", Src, Char, PS0, !:PS)
).
:- pred unicode_escape_sequence(src::in, char::out, ps::in, ps::in, ps::out)
is semidet.
unicode_escape_sequence(Src, UnicodeScalarValue, PS0, !PS) :-
hex_codepoint(Src, CodePoint1, !PS),
( is_surrogate_lead(CodePoint1) ->
PS1 = !.PS,
(
next_char(Src, '\\', !PS),
next_char(Src, 'u', !PS),
hex_codepoint(Src, CodePoint2, !PS),
is_surrogate_trail(CodePoint2)
->
surrogate_pair(CodePoint1, CodePoint2, UnicodeScalarValue)
;
fail_with_message("expected trail surrogate code point",
Src, UnicodeScalarValue, PS1, !:PS)
)
; is_surrogate_trail(CodePoint1) ->
fail_with_message("unexpected trail surrogate code point",
Src, UnicodeScalarValue, PS0, !:PS)
; CodePoint1 = 0 ->
fail_with_message("null character not allowed",
Src, UnicodeScalarValue, PS0, !:PS)
;
% This happens to allow Unicode Noncharacters.
char.from_int(CodePoint1, UnicodeScalarValue)
).
:- pred hex_codepoint(src::in, int::out, ps::in, ps::out) is semidet.
hex_codepoint(Src, Int, !PS) :-
parse_hex_digit(Src, U1, !PS),
parse_hex_digit(Src, U2, !PS),
parse_hex_digit(Src, U3, !PS),
parse_hex_digit(Src, U4, !PS),
Int = (U1 << 12) \/ (U2 << 8) \/ (U3 << 4) \/ U4.
:- pred parse_hex_digit(src::in, int::out, ps::in, ps::out) is semidet.
parse_hex_digit(Src, Int, !PS) :-
next_char(Src, C, !PS),
char.is_hex_digit(C, Int).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
unescape(esc_string(ES)) = S :-
( contains_backslash(ES) ->
Anchor0 = 0,
Pos0 = 0,
unescape_loop(ES, Anchor0, Pos0, _Pos, empty, RevPieces),
string_from_rev_pieces(RevPieces, S)
;
S = ES
).
:- pred contains_backslash(string::in) is semidet.
:- pragma foreign_proc("C",
contains_backslash(Str::in),
[will_not_call_mercury, promise_pure, thread_safe],
"
SUCCESS_INDICATOR = (strchr(Str, '\\\\') != NULL);
").
:- pred unescape_loop(string::in, int::in, int::in, int::out,
pieces::in, pieces::out) is det.
unescape_loop(Src, AnchorPos, !Pos, !RevPieces) :-
Pos0 = !.Pos,
next_char(Src, C0, !Pos),
( C0 = char.det_from_int(0):char ->
% End of string.
add_substring_piece(Src, AnchorPos, !.Pos, !RevPieces)
; C0 = ('\\') ->
add_substring_piece(Src, AnchorPos, Pos0, !RevPieces),
unescape_sequence(Src, MaybeValid, !Pos, !RevPieces),
(
MaybeValid = yes(SeqString),
!:RevPieces = literal(SeqString, !.RevPieces)
;
MaybeValid = no,
add_replacement_char(!RevPieces)
),
NextAnchor = !.Pos,
unescape_loop(Src, NextAnchor, !Pos, !RevPieces)
;
unescape_loop(Src, AnchorPos, !Pos, !RevPieces)
).
:- pred next_char(string::in, char::out, int::in, int::out) is det.
next_char(S, C, I0, I) :-
( string.unsafe_index_next(S, I0, I1, C1) ->
C = C1,
I = I1
;
C = char.det_from_int(0),
I = I0
).
:- pred add_substring_piece(string::in, int::in, int::in,
pieces::in, pieces::out) is det.
add_substring_piece(Src, AnchorPos, Pos, RevPieces0, RevPieces) :-
( AnchorPos < Pos ->
RevPieces = substring(Src, AnchorPos, Pos, RevPieces0)
;
RevPieces = RevPieces0
).
:- pred add_replacement_char(pieces::in, pieces::out) is det.
add_replacement_char(RevPieces0, RevPieces) :-
RevPieces = literal("\ufffd", RevPieces0).
:- pred unescape_sequence(string::in, maybe(string)::out, int::in, int::out,
pieces::in, pieces::out) is det.
unescape_sequence(Src, MaybeValid, !Pos, !RevPieces) :-
next_char(Src, Char, !Pos),
( simple_escape(Char, RealChar) ->
MaybeValid = yes(RealChar)
; Char = 'u' ->
unescape_unicode_sequence(Src, MaybeValid, !Pos, !RevPieces)
;
MaybeValid = no
).
:- pred unescape_unicode_sequence(string::in, maybe(string)::out,
int::in, int::out, pieces::in, pieces::out) is det.
unescape_unicode_sequence(Src, MaybeValid, !Pos, !RevPieces) :-
maybe_hex_codepoint(Src, MaybeCodePoint1, !Pos),
(
MaybeCodePoint1 = yes(CodePoint1),
( is_surrogate_lead(CodePoint1) ->
trail_surrogate_sequence(Src, MaybeTrail, !Pos),
(
MaybeTrail = yes(CodePoint2),
( surrogate_pair(CodePoint1, CodePoint2, UnicodeScalarValue) ->
UnicharString = string.from_char(UnicodeScalarValue),
MaybeValid = yes(UnicharString)
;
MaybeValid = no
)
;
MaybeTrail = no,
MaybeValid = no
)
;
% This happens to allow Unicode Noncharacters.
% Reject NUL.
CodePoint1 \= 0,
char.from_int(CodePoint1, Unichar)
->
UnicharString = string.from_char(Unichar),
MaybeValid = yes(UnicharString)
;
MaybeValid = no
)
;
MaybeCodePoint1 = no,
MaybeValid = no
).
:- pred maybe_hex_codepoint(string::in, maybe(int)::out, int::in, int::out)
is det.
maybe_hex_codepoint(Src, MaybeCodePoint, !Pos) :-
% We want to advance Pos as far as possible in presence of errors.
( hex_digit(Src, U1, !Pos) ->
( hex_digit(Src, U2, !Pos) ->
( hex_digit(Src, U3, !Pos) ->
( hex_digit(Src, U4, !Pos) ->
Int = (U1 << 12) \/ (U2 << 8) \/ (U3 << 4) \/ U4,
MaybeCodePoint = yes(Int)
;
MaybeCodePoint = no
)
;
MaybeCodePoint = no
)
;
MaybeCodePoint = no
)
;
MaybeCodePoint = no
).
:- pred hex_digit(string::in, int::out, int::in, int::out) is semidet.
hex_digit(Src, Int, !Pos) :-
next_char(Src, C, !Pos),
char.is_hex_digit(C, Int).
:- pred trail_surrogate_sequence(string::in, maybe(int)::out,
int::in, int::out) is det.
trail_surrogate_sequence(Src, MaybeTrail, !Pos) :-
(
next_char(Src, '\\', !Pos),
next_char(Src, 'u', !Pos)
->
maybe_hex_codepoint(Src, MaybeCodePoint, !Pos),
(
MaybeCodePoint = yes(CodePoint),
is_surrogate_trail(CodePoint)
->
MaybeTrail = MaybeCodePoint
;
MaybeTrail = no
)
;
MaybeTrail = no
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- pred simple_escape(char::in) is semidet.
simple_escape(C) :-
simple_escape(C, _).
:- pred simple_escape(char::in, string::out) is semidet.
simple_escape('"', "\"").
simple_escape('\\', "\\").
simple_escape('/', "/").
simple_escape('b', "\b").
simple_escape('f', "\f").
simple_escape('n', "\n").
simple_escape('r', "\r").
simple_escape('t', "\t").
:- pred is_surrogate_lead(int::in) is semidet.
is_surrogate_lead(I) :-
0xd800 =< I, I =< 0xdbff.
:- pred is_surrogate_trail(int::in) is semidet.
is_surrogate_trail(I) :-
0xdc00 =< I, I =< 0xdfff.
:- pred surrogate_pair(int::in, int::in, char::out) is semidet.
surrogate_pair(Lead, Trail, UnicodeScalarValue) :-
Hi = Lead - 0xd800,
Lo = Trail - 0xdc00,
Int = 0x10000 + ((Hi << 10) \/ Lo),
char.from_int(Int, UnicodeScalarValue).
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sts=4 sw=4 et