interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / json.om

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