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