interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / json / json.om

800 lines · 12545 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Bounded JSON syntax, decoded tokens, exact integer conversion and output.
3const jsonbad ulong 65537
4
5fn jsonbyte o edit uchar 65536 p ulong b uchar to ulong
6if ge p 65536
7ret jsonbad
8end
9put o p b
10ret add p 1
11end
12
13fn jsonraw o edit uchar 65536 p ulong s view uchar 65536 n ulong to ulong
14if or gt p 65536 gt n 65536
15ret jsonbad
16end
17if gt n sub ulong 65536 p
18ret jsonbad
19end
20for i ulong n
21put o add p i get s i
22end
23ret add p n
24end
25
26fn jsonhex c ulong to ulong
27if and ge c 48 le c 57
28ret sub c 48
29end
30if and ge c 65 le c 70
31ret sub c 55
32end
33if and ge c 97 le c 102
34ret sub c 87
35end
36ret 16
37end
38
39fn jsonh4 s view uchar 65536 p ulong n ulong to ulong
40if gt add p 4 n
41ret 65536
42end
43v = ulong 0
44for i ulong 4
45c = jsonhex ulong get s add p i
46if eq c 16
47ret 65536
48end
49v = add mul v 16 c
50end
51ret v
52end
53
54note Decode one UTF8 scalar. Bad sequences consume one byte, matching Go.
55fn jsonutf s view uchar 65536 p ulong n ulong r edit ulong 2 to void
56put r 0 65533
57put r 1 1
58c = ulong get s p
59if lt c 128
60put r 0 c
61ret
62end
63w = ulong 0
64v = ulong 0
65if and ge c 194 le c 223
66w = 2
67v = and c 31
68end
69if and ge c 224 le c 239
70w = 3
71v = and c 15
72end
73if and ge c 240 le c 244
74w = 4
75v = and c 7
76end
77if or eq w 0 gt add p w n
78ret
79end
80i = ulong 1
81while lt i w bound 4
82c = ulong get s add p i
83if or lt c 128 gt c 191
84ret
85end
86v = add mul v 64 and c 63
87i = add i 1
88end
89if and eq w 3 lt v 2048
90ret
91end
92if and eq w 4 lt v 65536
93ret
94end
95if or gt v 1114111 and ge v 55296 le v 57343
96ret
97end
98put r 0 v
99put r 1 w
100end
101
102fn jsonrune o edit uchar 65536 p ulong v ulong to ulong
103if lt v 128
104ret jsonbyte o p uchar v
105end
106if lt v 2048
107p = jsonbyte o p uchar add ulong 192 shr v 6
108else
109if lt v 65536
110p = jsonbyte o p uchar add ulong 224 shr v 12
111else
112p = jsonbyte o p uchar add ulong 240 shr v 18
113p = jsonbyte o p uchar add ulong 128 and shr v 12 63
114end
115p = jsonbyte o p uchar add ulong 128 and shr v 6 63
116end
117ret jsonbyte o p uchar add ulong 128 and v 63
118end
119
120note State: input length, cursor, decoded length, token count, parent, depth.
121fn jsonesc s view uchar 65536 st edit ulong 8 to ulong
122p = get st 1
123n = get st 0
124if ge p n
125ret 1114112
126end
127c = ulong get s p
128put st 1 add p 1
129if or eq c 34 or eq c 47 eq c 92
130ret c
131end
132if eq c 98
133ret 8
134end
135if eq c 102
136ret 12
137end
138if eq c 110
139ret 10
140end
141if eq c 114
142ret 13
143end
144if eq c 116
145ret 9
146end
147if ne c 117
148ret 1114112
149end
150p = add p 1
151c = jsonh4 s p n
152if eq c 65536
153ret 1114112
154end
155p = add p 4
156put st 1 p
157if and ge c 55296 le c 56319
158if le add p 6 n
159if and eq get s p 92 eq get s add p 1 117
160v = jsonh4 s add p 2 n
161if and ge v 56320 le v 57343
162put st 1 add p 6
163ret add ulong 65536 add mul sub c 55296 1024 sub v 56320
164end
165end
166end
167end
168if and ge c 55296 le c 57343
169ret 65533
170end
171ret c
172end
173
174fn jsonstring s view uchar 65536 st edit ulong 8 d edit uchar 65536 to int
175r = new ulong 2 0
176put st 1 add get st 1 1
177while lt get st 1 get st 0 bound 65536
178p = get st 1
179c = ulong get s p
180put st 1 add p 1
181if eq c 34
182ret 0
183end
184if lt c 32
185ret 1
186end
187if eq c 92
188c = jsonesc s st
189if gt c 1114111
190ret 1
191end
192else
193if ge c 128
194jsonutf s p get st 0 r
195c = get r 0
196put st 1 add p get r 1
197end
198end
199p = jsonrune d get st 2 c
200if eq p jsonbad
201ret 2
202end
203put st 2 p
204end
205ret 1
206end
207
208fn jsondigit c uchar to bool
209ret and ge c 48 le c 57
210end
211
212fn jsonnumber s view uchar 65536 st edit ulong 8 d edit uchar 65536 to int
213p = get st 1
214beg = p
215n = get st 0
216if eq get s p 45
217p = add p 1
218end
219if ge p n
220ret 1
221end
222if eq get s p 48
223p = add p 1
224else
225if not jsondigit get s p
226ret 1
227end
228while lt p n bound 65536
229if not jsondigit get s p
230break
231end
232p = add p 1
233end
234end
235if lt p n
236if eq get s p 46
237p = add p 1
238begd = p
239while lt p n bound 65536
240if not jsondigit get s p
241break
242end
243p = add p 1
244end
245if eq p begd
246ret 1
247end
248end
249end
250if lt p n
251if or eq get s p 101 eq get s p 69
252p = add p 1
253if lt p n
254if or eq get s p 43 eq get s p 45
255p = add p 1
256end
257end
258begd = p
259while lt p n bound 65536
260if not jsondigit get s p
261break
262end
263p = add p 1
264end
265if eq p begd
266ret 1
267end
268end
269end
270cnt = sub p beg
271if gt cnt sub ulong 65536 get st 2
272ret 2
273end
274for i ulong cnt
275put d add get st 2 i get s add beg i
276end
277put st 2 add get st 2 cnt
278put st 1 p
279ret 0
280end
281
282fn jsonoctet v ulong i ulong to uchar
283ret uchar and shr v mul i 8 255
284end
285
286fn jsonliteral s view uchar 65536 st edit ulong 8 kind ulong to int
287p = get st 1
288n = ulong 4
289v = ulong 1702195828
290if eq kind 6
291n = 5
292v = 435728179558
293end
294if eq kind 7
295v = 1819047278
296end
297if gt add p n get st 0
298ret 1
299end
300for i ulong n
301if ne get s add p i jsonoctet v i
302ret 1
303end
304end
305put st 1 add p n
306ret 0
307end
308
309fn jsonequal d view uchar 65536 a ulong b ulong n ulong to bool
310for i ulong n
311if ne get d add a i get d add b i
312ret false
313end
314end
315ret true
316end
317
318fn jsonkeyeq t view ulong 32768 d view uchar 65536 a ulong b ulong to bool
319if ne get t add a 2 get t add b 2
320ret false
321end
322ret jsonequal d get t add a 1 get t add b 1 get t add b 2
323end
324
325fn jsondupe t view ulong 32768 d view uchar 65536 id ulong to bool
326b = mul id 8
327par = get t add b 4
328i = add par 1
329while lt i id bound 4096
330a = mul i 8
331if eq get t add a 6 1
332if jsonkeyeq t d a b
333ret true
334end
335end
336i = get t add a 3
337end
338ret false
339end
340
341fn jsonfinish t edit ulong 32768 st view ulong 8 b ulong to void
342put t add b 2 sub get st 2 get t add b 1
343end
344
345fn jsonvalue s view uchar 65536 st edit ulong 8 t edit ulong 32768 d edit uchar 65536 key ulong to int
346if ge get st 3 4095
347ret 3
348end
349id = add get st 3 1
350put st 3 id
351b = mul id 8
352for i ulong 8
353put t add b i 0
354end
355put t b 4
356put t add b 1 get st 2
357put t add b 3 add id 1
358put t add b 4 get st 4
359put t add b 6 key
360c = get s get st 1
361if or eq c 123 eq c 91
362if ge get st 5 32
363ret 4
364end
365put t b 1
366if eq c 91
367put t b 2
368end
369put st 4 id
370put st 5 add get st 5 1
371put st 1 add get st 1 1
372ret 0
373end
374e = int 0
375if eq c 34
376put t b 3
377e = jsonstring s st d
378else
379if or eq c 116 or eq c 102 eq c 110
380put t b 5
381if eq c 102
382put t b 6
383end
384if eq c 110
385put t b 7
386end
387e = jsonliteral s st get t b
388else
389e = jsonnumber s st d
390end
391end
392if ne e 0
393ret e
394end
395jsonfinish t st b
396if eq key 1
397if jsondupe t d id
398ret 5
399end
400end
401ret 0
402end
403
404fn jsonspace c uchar to bool
405ret or eq c 32 or eq c 9 or eq c 10 eq c 13
406end
407
408note Container phase 0 initial, 1 separator, 2 after comma, 3 colon, 4 object value.
409fn jsonstep s view uchar 65536 st edit ulong 8 t edit ulong 32768 d edit uchar 65536 to int
410par = get st 4
411b = mul par 8
412phase = get t add b 7
413c = get s get st 1
414obj = eq get t b 1
415if or eq phase 0 eq phase 1
416if or and obj eq c 125 and not obj eq c 93
417put t add b 3 add get st 3 1
418put st 4 get t add b 4
419put st 5 sub get st 5 1
420put st 1 add get st 1 1
421ret 0
422end
423end
424if eq phase 1
425if ne c 44
426ret 1
427end
428put t add b 7 2
429put st 1 add get st 1 1
430ret 0
431end
432if eq phase 3
433if ne c 58
434ret 1
435end
436put t add b 7 4
437put st 1 add get st 1 1
438ret 0
439end
440key = ulong 0
441put t add b 7 1
442if and obj ne phase 4
443if ne c 34
444ret 1
445end
446if ge get t add b 5 512
447ret 6
448end
449key = 1
450put t add b 7 3
451end
452put t add b 5 add get t add b 5 1
453ret jsonvalue s st t d key
454end
455
456fn jsonparse s view uchar 65536 n ulong t edit ulong 32768 d edit uchar 65536 to int
457put t 0 0
458if or eq n 0 gt n 65536
459ret 2
460end
461st = new ulong 8 0
462put st 0 n
463e = int 0
464while lt get st 1 n bound 65536
465if jsonspace get s get st 1
466put st 1 add get st 1 1
467else
468if eq get st 4 0
469if ne get st 3 0
470ret 1
471end
472e = jsonvalue s st t d 0
473else
474e = jsonstep s st t d
475end
476if ne e 0
477ret e
478end
479end
480end
481if or ne get st 4 0 eq get st 3 0
482ret 1
483end
484put t 0 get st 3
485ret 0
486end
487
488fn jsonuint d view uchar 65536 off ulong n ulong r edit ulong 1 to int
489put r 0 0
490if or gt off 65536 or eq n 0 gt n 20
491ret 1
492end
493if gt n sub ulong 65536 off
494ret 1
495end
496if and gt n 1 eq get d off 48
497ret 1
498end
499v = ulong 0
500for i ulong n
501c = ulong get d add off i
502if or lt c 48 gt c 57
503ret 1
504end
505c = sub c 48
506if gt v div sub ulong 18446744073709551615 c 10
507ret 1
508end
509v = add mul v 10 c
510end
511put r 0 v
512ret 0
513end
514
515fn jsonint d view uchar 65536 off ulong n ulong r edit long 1 to int
516put r 0 0
517if or ge off 65536 eq n 0
518ret 1
519end
520negv = eq get d off 45
521if negv
522off = add off 1
523n = sub n 1
524end
525v = new ulong 1 0
526if ne jsonuint d off n v 0
527ret 1
528end
529x = get v 0
530if negv
531if gt x 9223372036854775808
532ret 1
533end
534if eq x 9223372036854775808
535put r 0 -9223372036854775808
536ret 0
537end
538put r 0 neg long x
539else
540if gt x 9223372036854775807
541ret 1
542end
543put r 0 long x
544end
545ret 0
546end
547
548fn jsoncopy t view ulong 32768 d view uchar 65536 id ulong o edit uchar 65536 to ulong
549if or eq id 0 gt id get t 0
550ret jsonbad
551end
552b = mul id 8
553n = get t add b 2
554for i ulong n
555put o i get d add get t add b 1 i
556end
557ret n
558end
559
560fn jsonname d view uchar 65536 off ulong key view uchar 65536 n ulong to bool
561for j ulong n
562if ne get key j get d add off j
563ret false
564end
565end
566ret true
567end
568
569fn jsonfind t view ulong 32768 d view uchar 65536 obj ulong key view uchar 65536 n ulong to ulong
570if or eq obj 0 gt obj get t 0
571ret 0
572end
573if or ne get t mul obj 8 1 gt n 65536
574ret 0
575end
576i = add obj 1
577while lt i get t add mul obj 8 3 bound 4096
578b = mul i 8
579if eq n get t add b 2
580if jsonname d get t add b 1 key n
581ret add i 1
582end
583end
584i = get t add mul add i 1 8 3
585end
586ret 0
587end
588
589fn jsonhc v ulong to uchar
590if lt v 10
591ret uchar add v 48
592end
593ret uchar add v 87
594end
595
596fn jsonuesc o edit uchar 65536 p ulong v ulong to ulong
597p = jsonbyte o p 92
598p = jsonbyte o p 117
599p = jsonbyte o p jsonhc and shr v 12 15
600p = jsonbyte o p jsonhc and shr v 8 15
601p = jsonbyte o p jsonhc and shr v 4 15
602ret jsonbyte o p jsonhc and v 15
603end
604
605fn jsonneeds v ulong to bool
606ret or lt v 32 or eq v 60 or eq v 62 or eq v 38 or eq v 8232 eq v 8233
607end
608
609fn jsonquote o edit uchar 65536 p ulong s view uchar 65536 off ulong n ulong to ulong
610if or gt n 65536 gt off 65536
611ret jsonbad
612end
613if gt n sub ulong 65536 off
614ret jsonbad
615end
616n = add off n
617p = jsonbyte o p 34
618i = off
619r = new ulong 2 0
620while lt i n bound 65536
621jsonutf s i n r
622v = get r 0
623i = add i get r 1
624if or eq v 34 eq v 92
625p = jsonbyte o p 92
626p = jsonbyte o p uchar v
627else
628if jsonneeds v
629c = ulong 0
630if eq v 8
631c = 98
632end
633if eq v 9
634c = 116
635end
636if eq v 10
637c = 110
638end
639if eq v 12
640c = 102
641end
642if eq v 13
643c = 114
644end
645if ne c 0
646p = jsonbyte o p 92
647p = jsonbyte o p uchar c
648else
649p = jsonuesc o p v
650end
651else
652if and eq v 65533 eq get r 1 1
653p = jsonuesc o p v
654else
655p = jsonrune o p v
656end
657end
658end
659if eq p jsonbad
660ret jsonbad
661end
662end
663ret jsonbyte o p 34
664end
665
666fn jsonstr o edit uchar 65536 p ulong s view uchar 65536 n ulong to ulong
667ret jsonquote o p s 0 n
668end
669
670fn jsonu64 o edit uchar 65536 p ulong v ulong to ulong
671buf = new uchar 20 0
672n = ulong 0
673while ne v 0 bound 20
674put buf n uchar add rem v 10 48
675n = add n 1
676v = div v 10
677end
678if eq n 0
679ret jsonbyte o p 48
680end
681while gt n 0 bound 20
682n = sub n 1
683p = jsonbyte o p get buf n
684end
685ret p
686end
687
688fn jsoni64 o edit uchar 65536 p ulong v long to ulong
689if ge v 0
690ret jsonu64 o p ulong v
691end
692p = jsonbyte o p 45
693if eq v -9223372036854775808
694ret jsonu64 o p 9223372036854775808
695end
696ret jsonu64 o p ulong neg v
697end
698
699fn jsonspan o edit uchar 65536 p ulong s view uchar 65536 off ulong n ulong to ulong
700if or gt off 65536 gt n 65536
701ret jsonbad
702end
703if gt n sub ulong 65536 off
704ret jsonbad
705end
706if gt p 65536
707ret jsonbad
708end
709if gt n sub ulong 65536 p
710ret jsonbad
711end
712for i ulong n
713put o add p i get s add off i
714end
715ret add p n
716end
717
718fn jsonword o edit uchar 65536 p ulong v ulong n ulong to ulong
719for i ulong n
720p = jsonbyte o p uchar and shr v mul i 8 255
721end
722ret p
723end
724
725fn jsonatom t view ulong 32768 d view uchar 65536 id ulong o edit uchar 65536 p ulong to ulong
726b = mul id 8
727k = get t b
728if eq k 1
729ret jsonbyte o p 123
730end
731if eq k 2
732ret jsonbyte o p 91
733end
734if eq k 3
735ret jsonquote o p d get t add b 1 get t add b 2
736end
737if eq k 4
738ret jsonspan o p d get t add b 1 get t add b 2
739end
740if eq k 5
741ret jsonword o p 1702195828 4
742end
743if eq k 6
744ret jsonword o p 435728179558 5
745end
746ret jsonword o p 1819047278 4
747end
748
749fn jsoncomma t view ulong 32768 b ulong par ulong to bool
750ret or eq get t mul par 8 2 eq get t add b 6 1
751end
752
753note Token table must be from a successful parse and unmodified.
754fn jsonencode t view ulong 32768 d view uchar 65536 o edit uchar 65536 to ulong
755if or eq get t 0 0 gt get t 0 4095
756ret jsonbad
757end
758p = ulong 0
759cur = ulong 0
760id = ulong 1
761while le id get t 0 bound 4095
762b = mul id 8
763par = get t add b 4
764while ne cur par bound 32
765c = uchar 125
766if eq get t mul cur 8 2
767c = 93
768end
769p = jsonbyte o p c
770cur = get t add mul cur 8 4
771end
772if ne par 0
773if gt id add par 1
774if jsoncomma t b par
775p = jsonbyte o p 44
776end
777end
778end
779p = jsonatom t d id o p
780if eq get t add b 6 1
781p = jsonbyte o p 58
782end
783if le get t b 2
784cur = id
785end
786if eq p jsonbad
787ret jsonbad
788end
789id = add id 1
790end
791while ne cur 0 bound 32
792c = uchar 125
793if eq get t mul cur 8 2
794c = 93
795end
796p = jsonbyte o p c
797cur = get t add mul cur 8 4
798end
799ret p
800end