interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / json / json.om

1254 lines · 22922 bytes Raw Blame History
1note seed/json/json.om: strict RFC 8259 JSON reader for seeded Omega (omega-mv #788). Contract: README.md.
2note Composition: an OM-2 unit built by the caller, `{ printf 'om2\n'; cat seed/json/json.om [seed/json/jsonw.om
3note [seed/json/jsonf.om]] app.om; }`. The unit has no globals, no texts and no host words: the caller owns
4note every array. Names: functions js_*, constants js*; locals are short.
5note Tape: a caller `uint` array. Cells 0..15 are the header (jsh* below); token t occupies the four cells from
6note 16 + 4t: [0] type | number flags << 4 | child count << 8, [1] first byte offset, [2] byte span,
7note [3] match index for a container begin or end, decoded byte length for a key or string, 0 otherwise.
8note Every value is checked; nothing is rounded: a number token is its source lexeme.
9
10const jsnone ulong 4294967295
11const jsbads ulong 4294967294
12const jsth ulong 16
13const jsts ulong 4
14const jstmax ulong 16777215
15const jsmaxn ulong 4294967295
16const jsdcap ulong 65535
17const jsmagic ulong 788
18
19note token types
20const jsobj ulong 1
21const jsobje ulong 2
22const jsarr ulong 3
23const jsarre ulong 4
24const jskey ulong 5
25const jsstr ulong 6
26const jsnum ulong 7
27const jstrue ulong 8
28const jsfalse ulong 9
29const jsnull ulong 10
30
31note number flags (token cell 0, bits 4..6)
32const jsfneg ulong 1
33const jsffrac ulong 2
34const jsfexp ulong 4
35
36note reader refusal codes; js_parse returns minus the code
37const jseeof ulong 1
38const jsesyn ulong 2
39const jsetrl ulong 3
40const jsedep ulong 4
41const jsesiz ulong 5
42const jseutf ulong 6
43const jsesur ulong 7
44const jsectl ulong 8
45const jseesc ulong 9
46const jsenum ulong 10
47const jsetok ulong 11
48const jselit ulong 12
49const jsearg ulong 13
50
51note header cells
52const jshok ulong 0
53const jshn ulong 1
54const jshpos ulong 2
55const jshnt ulong 3
56const jshcur ulong 4
57const jshdep ulong 5
58const jsherr ulong 6
59const jshepos ulong 7
60const jshdlim ulong 8
61const jshmaxd ulong 9
62const jshdtot ulong 10
63const jshdmax ulong 11
64const jshcap ulong 12
65const jshsl ulong 13
66const jshslim ulong 14
67const jshmagc ulong 15
68
69note ======== header and token cells ========
70
71fn js_hg tape edit uint * i ulong to ulong
72ret ulong get tape i
73end
74
75fn js_hs tape edit uint * i ulong v ulong to void
76put tape i uint v
77end
78
79fn js_fail tape edit uint * code ulong q ulong to void
80if eq get tape jsherr 0
81put tape jsherr uint code
82put tape jshepos uint q
83end
84end
85
86fn js_okst tape edit uint * st ulong to ulong
87if ne get tape jsherr 0
88ret 3
89end
90ret st
91end
92
93note js_cntinc: a key counts as an object member, any value directly inside an array as an element
94fn js_cntinc tape edit uint * ty ulong to void
95cur = js_hg tape jshcur
96if eq cur 0
97ret
98end
99if or eq ty jsobje eq ty jsarre
100ret
101end
102b = add jsth mul sub cur 1 jsts
103ct = and ulong get tape b 15
104if or eq ty jskey eq ct jsarr
105put tape b add get tape b 256
106end
107end
108
109fn js_tok tape edit uint * ty ulong q ulong w ulong to ulong
110t = js_hg tape jshnt
111if ge t js_hg tape jshcap
112js_fail tape jsetok q
113ret jsnone
114end
115b = add jsth mul t jsts
116put tape b uint ty
117put tape add b 1 uint q
118put tape add b 2 uint w
119put tape add b 3 0
120js_hs tape jshnt add t 1
121js_cntinc tape ty
122ret t
123end
124
125note ======== limits and entry ========
126
127note js_init: size limit 1..4294967295 bytes (the tape stores offsets in 32-bit cells: a field width, not a
128note choice), depth limit 1..65535; the tape must hold the header and one token.
129fn js_init tape edit uint * slim ulong dlim ulong to long
130if lt len tape add jsth jsts
131ret neg long jsearg
132end
133if or eq slim 0 gt slim jsmaxn
134ret neg long jsearg
135end
136if or eq dlim 0 gt dlim jsdcap
137ret neg long jsearg
138end
139for i ulong jsth
140put tape i 0
141end
142c = div sub len tape jsth jsts
143if gt c jstmax
144c = jstmax
145end
146js_hs tape jshslim slim
147js_hs tape jshdlim dlim
148js_hs tape jshcap c
149js_hs tape jshmagc jsmagic
150ret 0
151end
152
153fn js_reset tape edit uint * n ulong to void
154js_hs tape jshok 0
155js_hs tape jshn 0
156js_hs tape jshpos 0
157js_hs tape jshnt 0
158js_hs tape jshcur 0
159js_hs tape jshdep 0
160js_hs tape jsherr 0
161js_hs tape jshepos 0
162js_hs tape jshmaxd 0
163js_hs tape jshdtot 0
164js_hs tape jshdmax 0
165js_hs tape jshsl 0
166if gt n js_hg tape jshslim
167js_fail tape jsesiz js_hg tape jshslim
168ret
169end
170js_hs tape jshn n
171end
172
173fn js_result tape edit uint * to long
174e = js_hg tape jsherr
175if ne e 0
176js_hs tape jshnt 0
177js_hs tape jshok 0
178ret neg long e
179end
180js_hs tape jshok 1
181ret long js_hg tape jshnt
182end
183
184note js_parse: n bytes of src. Returns the token count, or minus a refusal code (tape then holds no tokens).
185note Loop bound: every step consumes at least one distinct input byte (a token's first byte, a closing byte
186note or a comma) or ends the parse, so steps <= n + 1 <= jsmaxn + 1.
187fn js_parse src view uchar * tape edit uint * n ulong to long
188if lt len tape add jsth jsts
189ret neg long jsearg
190end
191if ne js_hg tape jshmagc jsmagic
192ret neg long jsearg
193end
194js_reset tape n
195if ne js_hg tape jsherr 0
196ret js_result tape
197end
198if gt n len src
199js_fail tape jsearg 0
200ret js_result tape
201end
202st = ulong 0
203while lt st 3 bound 9223372036854775807
204st = js_step src tape st
205end
206ret js_result tape
207end
208
209note states: 0 value expected, 1 after a value, 2 member key expected, 3 finished or refused
210fn js_step src view uchar * tape edit uint * st ulong to ulong
211js_ws src tape
212if eq st 1
213ret js_after src tape
214end
215p = js_hg tape jshpos
216if ge p js_hg tape jshn
217js_fail tape jseeof p
218ret 3
219end
220if eq st 2
221ret js_keyst src tape
222end
223ret js_value src tape
224end
225
226fn js_ws src view uchar * tape edit uint * to void
227p = js_hg tape jshpos
228n = js_hg tape jshn
229while lt p n bound 9223372036854775807
230c = get src p
231if and ne c 32 and ne c 9 and ne c 10 ne c 13
232break
233end
234p = add p 1
235end
236js_hs tape jshpos p
237end
238
239fn js_at src view uchar * tape edit uint * q ulong c uchar to bool
240if ge q js_hg tape jshn
241ret false
242end
243ret eq get src q c
244end
245
246fn js_value src view uchar * tape edit uint * to ulong
247p = js_hg tape jshpos
248c = get src p
249if eq c 123
250ret js_open src tape jsobj
251end
252if eq c 91
253ret js_open src tape jsarr
254end
255if eq c 34
256js_string src tape jsstr
257ret js_okst tape 1
258end
259if or eq c 45 and ge c 48 le c 57
260js_number src tape
261ret js_okst tape 1
262end
263if eq c 116
264js_lit src tape 0
265ret js_okst tape 1
266end
267if eq c 102
268js_lit src tape 1
269ret js_okst tape 1
270end
271if eq c 110
272js_lit src tape 2
273ret js_okst tape 1
274end
275js_fail tape jsesyn p
276ret 3
277end
278
279fn js_after src view uchar * tape edit uint * to ulong
280d = js_hg tape jshdep
281p = js_hg tape jshpos
282n = js_hg tape jshn
283if eq d 0
284if lt p n
285js_fail tape jsetrl p
286end
287ret 3
288end
289if ge p n
290js_fail tape jseeof p
291ret 3
292end
293c = get src p
294b = add jsth mul sub js_hg tape jshcur 1 jsts
295bt = and ulong get tape b 15
296if eq c 44
297js_hs tape jshpos add p 1
298if eq bt jsobj
299ret 2
300end
301ret 0
302end
303if and eq bt jsobj eq c 125
304js_close tape jsobje
305ret js_okst tape 1
306end
307if and eq bt jsarr eq c 93
308js_close tape jsarre
309ret js_okst tape 1
310end
311js_fail tape jsesyn p
312ret 3
313end
314
315fn js_keyst src view uchar * tape edit uint * to ulong
316p = js_hg tape jshpos
317if ne get src p 34
318js_fail tape jsesyn p
319ret 3
320end
321js_string src tape jskey
322if ne get tape jsherr 0
323ret 3
324end
325js_ws src tape
326q = js_hg tape jshpos
327if ge q js_hg tape jshn
328js_fail tape jseeof q
329ret 3
330end
331if ne get src q 58
332js_fail tape jsesyn q
333ret 3
334end
335js_hs tape jshpos add q 1
336ret 0
337end
338
339note ======== containers ========
340
341note js_open: cell 3 of an open container keeps the enclosing open container (+1) until it closes.
342fn js_open src view uchar * tape edit uint * ty ulong to ulong
343p = js_hg tape jshpos
344d = js_hg tape jshdep
345if ge d js_hg tape jshdlim
346js_fail tape jsedep p
347ret 3
348end
349t = js_tok tape ty p 1
350if eq t jsnone
351ret 3
352end
353put tape add add jsth mul t jsts 3 uint js_hg tape jshcur
354js_hs tape jshcur add t 1
355d = add d 1
356js_hs tape jshdep d
357if gt d js_hg tape jshmaxd
358js_hs tape jshmaxd d
359end
360js_hs tape jshpos add p 1
361js_ws src tape
362e = uchar 93
363if eq ty jsobj
364e = uchar 125
365end
366q = js_hg tape jshpos
367if js_at src tape q e
368js_close tape add ty 1
369ret js_okst tape 1
370end
371if eq ty jsobj
372ret 2
373end
374ret 0
375end
376
377fn js_close tape edit uint * ety ulong to void
378p = js_hg tape jshpos
379cur = js_hg tape jshcur
380b = add jsth mul sub cur 1 jsts
381js_hs tape jshcur ulong get tape add b 3
382js_hs tape jshdep sub js_hg tape jshdep 1
383e = js_tok tape ety p 1
384if eq e jsnone
385ret
386end
387put tape add b 3 uint e
388put tape add add jsth mul e jsts 3 uint sub cur 1
389put tape add b 2 uint add sub p ulong get tape add b 1 1
390js_hs tape jshpos add p 1
391end
392
393note ======== literals and numbers ========
394
395note js_litb: byte i of literal k (0 true, 1 false, 2 null)
396fn js_litb k ulong i ulong to uchar
397if eq k 0
398if eq i 0
399ret 116
400end
401if eq i 1
402ret 114
403end
404if eq i 2
405ret 117
406end
407ret 101
408end
409if eq k 1
410if eq i 0
411ret 102
412end
413if eq i 1
414ret 97
415end
416if eq i 2
417ret 108
418end
419if eq i 3
420ret 115
421end
422ret 101
423end
424if eq i 0
425ret 110
426end
427if eq i 1
428ret 117
429end
430ret 108
431end
432
433fn js_litn k ulong to ulong
434if eq k 1
435ret 5
436end
437ret 4
438end
439
440fn js_lit src view uchar * tape edit uint * k ulong to void
441p = js_hg tape jshpos
442n = js_hg tape jshn
443m = js_litn k
444for i ulong m
445q = add p i
446if ge q n
447js_fail tape jseeof n
448ret
449end
450if ne get src q js_litb k i
451js_fail tape jselit q
452ret
453end
454end
455t = js_tok tape add k 8 p m
456js_hs tape jshpos add p m
457end
458
459fn js_isdig src view uchar * tape edit uint * q ulong to bool
460if ge q js_hg tape jshn
461ret false
462end
463c = get src q
464ret and ge c 48 le c 57
465end
466
467fn js_digits src view uchar * tape edit uint * q ulong to ulong
468p = q
469while js_isdig src tape p bound 9223372036854775807
470p = add p 1
471end
472ret p
473end
474
475note js_number: RFC 8259 section 6 grammar; a leading zero followed by a digit is refused as a number
476fn js_number src view uchar * tape edit uint * to void
477s = js_hg tape jshpos
478p = s
479f = ulong 0
480if js_at src tape p 45
481f = jsfneg
482p = add p 1
483end
484if not js_isdig src tape p
485js_fail tape jsenum p
486ret
487end
488if js_at src tape p 48
489p = add p 1
490if js_isdig src tape p
491js_fail tape jsenum p
492ret
493end
494else
495p = js_digits src tape p
496end
497if js_at src tape p 46
498p = add p 1
499if not js_isdig src tape p
500js_fail tape jsenum p
501ret
502end
503p = js_digits src tape p
504f = or f jsffrac
505end
506if or js_at src tape p 101 js_at src tape p 69
507p = add p 1
508if or js_at src tape p 43 js_at src tape p 45
509p = add p 1
510end
511if not js_isdig src tape p
512js_fail tape jsenum p
513ret
514end
515p = js_digits src tape p
516f = or f jsfexp
517end
518t = js_tok tape jsnum s sub p s
519if ne t jsnone
520put tape add jsth mul t jsts uint or jsnum shl f 4
521end
522js_hs tape jshpos p
523end
524
525note ======== strings ========
526
527note js_u8st: RFC 3629 section 4 well-formed UTF-8 as a state machine. State 0 expects a lead byte;
528note 1, 2, 3 expect that many continuation bytes 80..BF; 4 after E0 expects A0..BF; 5 after ED expects
529note 80..9F; 6 after F0 expects 90..BF; 7 after F4 expects 80..8F. 9 is refusal (overlong, surrogate,
530note above U+10FFFF, stray continuation, C0, C1, F5..FF).
531fn js_u8st s ulong c uchar to ulong
532if eq s 0
533if lt c 128
534ret 0
535end
536if lt c 194
537ret 9
538end
539if lt c 224
540ret 1
541end
542if eq c 224
543ret 4
544end
545if eq c 237
546ret 5
547end
548if lt c 240
549ret 2
550end
551if eq c 240
552ret 6
553end
554if lt c 244
555ret 3
556end
557if eq c 244
558ret 7
559end
560ret 9
561end
562lo = uchar 128
563hi = uchar 191
564if eq s 4
565lo = uchar 160
566end
567if eq s 5
568hi = uchar 159
569end
570if eq s 6
571lo = uchar 144
572end
573if eq s 7
574hi = uchar 143
575end
576if or lt c lo gt c hi
577ret 9
578end
579if eq s 1
580ret 0
581end
582if or eq s 2 or eq s 4 eq s 5
583ret 1
584end
585ret 2
586end
587
588fn js_hexv c uchar to ulong
589if and ge c 48 le c 57
590ret ulong sub c 48
591end
592if and ge c 97 le c 102
593ret ulong sub c 87
594end
595if and ge c 65 le c 70
596ret ulong sub c 55
597end
598ret 16
599end
600
601fn js_cplen v ulong to ulong
602if lt v 128
603ret 1
604end
605if lt v 2048
606ret 2
607end
608if lt v 65536
609ret 3
610end
611ret 4
612end
613
614fn js_esc1 c uchar to bool
615if or eq c 34 eq c 92
616ret true
617end
618if or eq c 47 eq c 98
619ret true
620end
621if or eq c 102 eq c 110
622ret true
623end
624ret or eq c 114 eq c 116
625end
626
627fn js_dadd tape edit uint * k ulong to void
628js_hs tape jshsl add js_hg tape jshsl k
629end
630
631fn js_hex4 src view uchar * tape edit uint * q ulong to ulong
632n = js_hg tape jshn
633v = ulong 0
634for i ulong 4
635r = add q i
636if ge r n
637js_fail tape jseeof n
638ret jsnone
639end
640h = js_hexv get src r
641if eq h 16
642js_fail tape jseesc r
643ret jsnone
644end
645v = or shl v 4 h
646end
647ret v
648end
649
650note js_uesc: p is the backslash of \uXXXX. A high surrogate must be followed at once by \u and a low
651note surrogate; a lone low surrogate, or a high one without its low half, is refused (RFC 8259 section 8.2).
652note js_uesc/js_escape return the next source position, or 0 on refusal (valid next positions exceed 1).
653fn js_uesc src view uchar * tape edit uint * p ulong to ulong
654h = js_hex4 src tape add p 2
655if eq h jsnone
656ret 0
657end
658q = add p 6
659if and ge h 56320 le h 57343
660js_fail tape jsesur p
661ret 0
662end
663if or lt h 55296 gt h 56319
664js_dadd tape js_cplen h
665ret q
666end
667if not and js_at src tape q 92 js_at src tape add q 1 117
668if ge q js_hg tape jshn
669js_fail tape jseeof q
670ret 0
671end
672js_fail tape jsesur p
673ret 0
674end
675l = js_hex4 src tape add q 2
676if eq l jsnone
677ret 0
678end
679if or lt l 56320 gt l 57343
680js_fail tape jsesur q
681ret 0
682end
683js_dadd tape 4
684ret add q 6
685end
686
687fn js_escape src view uchar * tape edit uint * p ulong to ulong
688q = add p 1
689if ge q js_hg tape jshn
690js_fail tape jseeof q
691ret 0
692end
693c = get src q
694if eq c 117
695ret js_uesc src tape p
696end
697if not js_esc1 c
698js_fail tape jseesc q
699ret 0
700end
701js_dadd tape 1
702ret add q 1
703end
704
705fn js_strtok tape edit uint * ty ulong s ulong e ulong to void
706t = js_tok tape ty s sub e s
707js_hs tape jshpos e
708if eq t jsnone
709ret
710end
711l = js_hg tape jshsl
712put tape add add jsth mul t jsts 3 uint l
713js_hs tape jshdtot add js_hg tape jshdtot l
714if gt l js_hg tape jshdmax
715js_hs tape jshdmax l
716end
717end
718
719note js_string: validates one string and records its decoded length; bytes are decoded later by js_sget.
720note Each pass advances p by at least one byte, so passes <= n <= jsmaxn.
721fn js_string src view uchar * tape edit uint * ty ulong to void
722s = js_hg tape jshpos
723n = js_hg tape jshn
724js_hs tape jshsl 0
725p = add s 1
726st = ulong 0
727while lt p n bound 9223372036854775807
728c = get src p
729if or ne st 0 ge c 128
730st = js_u8st st c
731if eq st 9
732js_fail tape jseutf p
733ret
734end
735js_dadd tape 1
736p = add p 1
737continue
738end
739if eq c 34
740js_strtok tape ty s add p 1
741ret
742end
743if lt c 32
744js_fail tape jsectl p
745ret
746end
747if eq c 92
748p = js_escape src tape p
749if eq p 0
750ret
751end
752continue
753end
754js_dadd tape 1
755p = add p 1
756end
757js_fail tape jseeof n
758end
759
760note ======== public tape accessors (read only) ========
761
762fn js_ok tape view uint * to bool
763if lt len tape add jsth jsts
764ret false
765end
766ret eq get tape jshok 1
767end
768
769fn js_ntok tape view uint * to ulong
770if not js_ok tape
771ret 0
772end
773ret ulong get tape jshnt
774end
775
776fn js_err tape view uint * to ulong
777if lt len tape jsth
778ret jsearg
779end
780ret ulong get tape jsherr
781end
782
783fn js_epos tape view uint * to ulong
784if lt len tape jsth
785ret 0
786end
787ret ulong get tape jshepos
788end
789
790fn js_maxdep tape view uint * to ulong
791if not js_ok tape
792ret 0
793end
794ret ulong get tape jshmaxd
795end
796
797note js_dtotal: decoded bytes of every key and string; js_dmax: the longest one (sizes for js_sget buffers)
798fn js_dtotal tape view uint * to ulong
799if not js_ok tape
800ret 0
801end
802ret ulong get tape jshdtot
803end
804
805fn js_dmax tape view uint * to ulong
806if not js_ok tape
807ret 0
808end
809ret ulong get tape jshdmax
810end
811
812fn js_cell tape view uint * t ulong i ulong to ulong
813if not js_ok tape
814ret 0
815end
816if ge t ulong get tape jshnt
817ret 0
818end
819ret ulong get tape add add jsth mul t jsts i
820end
821
822fn js_type tape view uint * t ulong to ulong
823ret and js_cell tape t 0 15
824end
825
826fn js_nflags tape view uint * t ulong to ulong
827ret and shr js_cell tape t 0 4 7
828end
829
830note js_nkids: members of an object, elements of an array
831fn js_nkids tape view uint * t ulong to ulong
832ty = js_type tape t
833if or eq ty jsobj eq ty jsarr
834ret shr js_cell tape t 0 8
835end
836ret 0
837end
838
839fn js_off tape view uint * t ulong to ulong
840ret js_cell tape t 1
841end
842
843fn js_span tape view uint * t ulong to ulong
844ret js_cell tape t 2
845end
846
847fn js_match tape view uint * t ulong to ulong
848ty = js_type tape t
849if and ge ty jsobj le ty jsarre
850ret js_cell tape t 3
851end
852ret jsnone
853end
854
855fn js_slen tape view uint * t ulong to ulong
856ty = js_type tape t
857if or eq ty jskey eq ty jsstr
858ret js_cell tape t 3
859end
860ret 0
861end
862
863note js_last: the last token of the value starting at t (its end token for a container)
864fn js_last tape view uint * t ulong to ulong
865ty = js_type tape t
866if or eq ty jsobj eq ty jsarr
867ret js_cell tape t 3
868end
869ret t
870end
871
872note js_first: first member key of an object or first element of an array, jsnone when empty or not one
873fn js_first tape view uint * t ulong to ulong
874if eq js_nkids tape t 0
875ret jsnone
876end
877ret add t 1
878end
879
880note js_enext: the element after array element e; js_mnext: the key after member key k; jsnone at the end
881fn js_enext tape view uint * e ulong to ulong
882n = add js_last tape e 1
883ty = js_type tape n
884if or eq ty 0 or eq ty jsobje eq ty jsarre
885ret jsnone
886end
887ret n
888end
889
890fn js_mnext tape view uint * k ulong to ulong
891if ne js_type tape k jskey
892ret jsnone
893end
894n = add js_last tape add k 1 1
895if ne js_type tape n jskey
896ret jsnone
897end
898ret n
899end
900
901note ======== strings from the source (off is a key or string token's offset) ========
902
903fn js_slim src view uchar * off ulong to ulong
904n = len src
905m = add off add jsmaxn 2
906if gt n m
907ret m
908end
909ret n
910end
911
912fn js_atv src view uchar * q ulong c uchar to bool
913if ge q len src
914ret false
915end
916ret eq get src q c
917end
918
919fn js_hex4s src view uchar * q ulong to ulong
920if gt add q 4 len src
921ret jsnone
922end
923v = ulong 0
924for i ulong 4
925h = js_hexv get src add q i
926if eq h 16
927ret jsnone
928end
929v = or shl v 4 h
930end
931ret v
932end
933
934fn js_esv c uchar to ulong
935if or eq c 34 or eq c 92 eq c 47
936ret ulong c
937end
938if eq c 98
939ret 8
940end
941if eq c 102
942ret 12
943end
944if eq c 110
945ret 10
946end
947if eq c 114
948ret 13
949end
950if eq c 116
951ret 9
952end
953ret jsnone
954end
955
956note js_ecp: the code point of the escape whose backslash is at p; jsnone for a malformed escape,
957note jsbads for a surrogate that is not one well-formed pair
958fn js_ecp src view uchar * p ulong to ulong
959q = add p 1
960if ge q len src
961ret jsnone
962end
963c = get src q
964if ne c 117
965ret js_esv c
966end
967h = js_hex4s src add p 2
968if eq h jsnone
969ret jsnone
970end
971if and ge h 56320 le h 57343
972ret jsbads
973end
974if or lt h 55296 gt h 56319
975ret h
976end
977r = add p 6
978if not js_atv src r 92
979ret jsbads
980end
981if not js_atv src add r 1 117
982ret jsbads
983end
984l = js_hex4s src add r 2
985if eq l jsnone
986ret jsnone
987end
988if or lt l 56320 gt l 57343
989ret jsbads
990end
991ret add 65536 add shl sub h 55296 10 sub l 56320
992end
993
994note js_elen: source bytes of the escape at p whose code point is cp
995fn js_elen src view uchar * p ulong cp ulong to ulong
996if ne get src add p 1 117
997ret 2
998end
999if ge cp 65536
1000ret 12
1001end
1002ret 6
1003end
1004
1005note js_putcp: UTF-8 encoding of cp at w; returns the next w, or jsnone when it does not fit
1006fn js_putcp ob edit uchar * w ulong cp ulong to ulong
1007k = js_cplen cp
1008if gt add w k len ob
1009ret jsnone
1010end
1011if eq k 1
1012put ob w uchar cp
1013ret add w 1
1014end
1015if eq k 2
1016put ob w uchar or 192 shr cp 6
1017put ob add w 1 uchar or 128 and cp 63
1018ret add w 2
1019end
1020if eq k 3
1021put ob w uchar or 224 shr cp 12
1022put ob add w 1 uchar or 128 and shr cp 6 63
1023put ob add w 2 uchar or 128 and cp 63
1024ret add w 3
1025end
1026put ob w uchar or 240 shr cp 18
1027put ob add w 1 uchar or 128 and shr cp 12 63
1028put ob add w 2 uchar or 128 and shr cp 6 63
1029put ob add w 3 uchar or 128 and cp 63
1030ret add w 4
1031end
1032
1033note js_sgesc: decode the escape at src[p] into ob at w; the next w, or minus a code
1034fn js_sgesc src view uchar * p ulong ob edit uchar * w ulong to long
1035cp = js_ecp src p
1036if eq cp jsnone
1037ret neg long jseesc
1038end
1039if eq cp jsbads
1040ret neg long jsesur
1041end
1042r = js_putcp ob w cp
1043if eq r jsnone
1044ret neg long jsearg
1045end
1046ret long r
1047end
1048
1049note js_sget: decode the string whose opening quote is src[off] into ob[0..); returns the byte count or minus
1050note a code (jsearg: ob too small or off not a quote). It re-checks UTF-8, controls, escapes and surrogates.
1051fn js_sget src view uchar * off ulong ob edit uchar * to long
1052if ge off js_slim src off
1053ret neg long jsearg
1054end
1055if ne get src off 34
1056ret neg long jsearg
1057end
1058p = add off 1
1059w = ulong 0
1060st = ulong 0
1061while lt p js_slim src off bound 9223372036854775807
1062c = get src p
1063if and eq st 0 eq c 34
1064ret long w
1065end
1066if and eq st 0 eq c 92
1067r = js_sgesc src p ob w
1068if lt r 0
1069ret r
1070end
1071w = ulong r
1072p = add p js_elen src p js_ecp src p
1073continue
1074end
1075if and eq st 0 lt c 32
1076ret neg long jsectl
1077end
1078st = js_u8st st c
1079if eq st 9
1080ret neg long jseutf
1081end
1082if ge w len ob
1083ret neg long jsearg
1084end
1085put ob w c
1086w = add w 1
1087p = add p 1
1088end
1089ret neg long jseeof
1090end
1091
1092note js_cpeq: whether key[k ..] starts with the UTF-8 encoding of cp; the next k, or jsnone
1093fn js_cpeq key view uchar * k ulong cp ulong to ulong
1094tmp = new uchar 4 0
1095m = js_putcp tmp 0 cp
1096if gt m sub len key k
1097ret jsnone
1098end
1099for i ulong m
1100if ne get key add k i get tmp i
1101ret jsnone
1102end
1103end
1104ret add k m
1105end
1106
1107note js_streq: whether the decoded string at src[off] equals the bytes of key exactly
1108fn js_streq src view uchar * off ulong key view uchar * to bool
1109if ge off js_slim src off
1110ret false
1111end
1112if ne get src off 34
1113ret false
1114end
1115p = add off 1
1116k = ulong 0
1117while lt p js_slim src off bound 9223372036854775807
1118c = get src p
1119if eq c 34
1120ret eq k len key
1121end
1122if ne c 92
1123if ge k len key
1124ret false
1125end
1126if ne get key k c
1127ret false
1128end
1129k = add k 1
1130p = add p 1
1131continue
1132end
1133cp = js_ecp src p
1134if ge cp jsbads
1135ret false
1136end
1137k = js_cpeq key k cp
1138if eq k jsnone
1139ret false
1140end
1141p = add p js_elen src p cp
1142end
1143ret false
1144end
1145
1146note ======== integers from a number lexeme at src[off] (exact; no rounding) ========
1147
1148note js_ovf10: whether m * 10 + d exceeds 18446744073709551615
1149fn js_ovf10 m ulong d ulong to bool
1150if gt m 1844674407370955161
1151ret true
1152end
1153if lt m 1844674407370955161
1154ret false
1155end
1156ret gt d 5
1157end
1158
1159note js_ilex: 0 not an integer lexeme (fraction or exponent present, or no digit), 1 integer whose
1160note magnitude fits ulong, 2 integer whose magnitude exceeds 18446744073709551615
1161fn js_ilex src view uchar * off ulong to ulong
1162n = js_slim src off
1163p = off
1164if js_atv src p 45
1165p = add p 1
1166end
1167m = ulong 0
1168ovf = false
1169k = ulong 0
1170while lt p n bound 9223372036854775807
1171c = get src p
1172if or lt c 48 gt c 57
1173break
1174end
1175d = ulong sub c 48
1176if js_ovf10 m d
1177ovf = true
1178else
1179m = add mul m 10 d
1180end
1181k = add k 1
1182p = add p 1
1183end
1184if eq k 0
1185ret 0
1186end
1187if lt p n
1188c = get src p
1189if or eq c 46 or eq c 101 eq c 69
1190ret 0
1191end
1192end
1193if ovf
1194ret 2
1195end
1196ret 1
1197end
1198
1199fn js_imag src view uchar * off ulong to ulong
1200n = js_slim src off
1201p = off
1202if js_atv src p 45
1203p = add p 1
1204end
1205m = ulong 0
1206while lt p n bound 25
1207c = get src p
1208if or lt c 48 gt c 57
1209break
1210end
1211m = add mul m 10 ulong sub c 48
1212p = add p 1
1213end
1214ret m
1215end
1216
1217fn js_isu src view uchar * off ulong to bool
1218if ne js_ilex src off 1
1219ret false
1220end
1221ret not js_atv src off 45
1222end
1223
1224fn js_tou src view uchar * off ulong to ulong
1225if not js_isu src off
1226ret 0
1227end
1228ret js_imag src off
1229end
1230
1231fn js_isl src view uchar * off ulong to bool
1232if ne js_ilex src off 1
1233ret false
1234end
1235m = js_imag src off
1236if js_atv src off 45
1237ret le m 9223372036854775808
1238end
1239ret le m 9223372036854775807
1240end
1241
1242fn js_tol src view uchar * off ulong to long
1243if not js_isl src off
1244ret 0
1245end
1246m = js_imag src off
1247if not js_atv src off 45
1248ret long m
1249end
1250if eq m 9223372036854775808
1251ret long -9223372036854775808
1252end
1253ret neg long m
1254end