interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / research / lib / jflat.om
| 1 | om2 |
| 2 | note jflat: JSON on stdin, one line per value on stdout: PATH, a tab, VALUE, a newline. |
| 3 | note PATH is "/" followed by the object keys and array indices from the root, joined by "/" |
| 4 | note (`/errors/7/meaning`, `/words/fopen/types/0`); the root value alone has the path "/". |
| 5 | note VALUE is a string with its JSON escapes decoded (\uXXXX and surrogate pairs become UTF-8), |
| 6 | note then newline, tab and backslash written as \n, \t and \\ so that every value stays on one |
| 7 | note line; numbers, true, false and null are copied as written; an empty object or array is |
| 8 | note written as {} or []; nonempty objects/arrays start with {/[ markers at their own paths. In a key, "/" and "\" are written \/ and \\, and newline and tab \n and \t, |
| 9 | note so a path splits only on its separators and never holds a tab. Lines come in document order, so the |
| 10 | note output is a deterministic function of the input bytes. Nesting is limited by the 8 MiB stack: a |
| 11 | note document nested deeper than it can hold stops with trap 6 (exit 106). |
| 12 | note Exit 0 on success; 2 when the input is not one JSON value (stderr names the byte offset), |
| 13 | note when it is 16777216 bytes or longer, or when a write fails; on an error, output not yet written |
| 14 | note is dropped. The input refusal is the size of the largest global array at the pinned omc (omega-mv |
| 15 | note has no run-time memory growth yet); `guides check` alarms when data/reference.tsv passes half of it. |
| 16 | |
| 17 | global src uchar 16777216 |
| 18 | global obf uchar 1048576 |
| 19 | global pth uchar 1048576 |
| 20 | global nb uchar 32 |
| 21 | global st ulong 8 |
| 22 | |
| 23 | note st cells: input position, input length, path length, output length |
| 24 | const c_p ulong 0 |
| 25 | const c_n ulong 1 |
| 26 | const c_pl ulong 2 |
| 27 | const c_ol ulong 3 |
| 28 | |
| 29 | text m_big "jflat: input is 16777216 bytes or longer\n" |
| 30 | text m_bad "jflat: malformed JSON at byte " |
| 31 | text m_deep "jflat: a path is 1048576 bytes or longer\n" |
| 32 | text m_tail ": expected one JSON value\n" |
| 33 | text k_true "true" |
| 34 | text k_false "false" |
| 35 | text k_null "null" |
| 36 | |
| 37 | fn ow2 t view uchar * to void |
| 38 | n = len t |
| 39 | p = ulong 0 |
| 40 | while lt p n bound 1048576 |
| 41 | k = write 2 t p sub n p |
| 42 | if le k 0 |
| 43 | exit 2 |
| 44 | end |
| 45 | p = add p ulong k |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | fn flush to void |
| 50 | n = get st c_ol |
| 51 | p = ulong 0 |
| 52 | while lt p n bound 1048576 |
| 53 | k = write 1 obf p sub n p |
| 54 | if le k 0 |
| 55 | exit 2 |
| 56 | end |
| 57 | p = add p ulong k |
| 58 | end |
| 59 | put st c_ol 0 |
| 60 | end |
| 61 | |
| 62 | fn ob c uchar to void |
| 63 | n = get st c_ol |
| 64 | if eq n 1048576 |
| 65 | flush |
| 66 | n = ulong 0 |
| 67 | end |
| 68 | put obf n c |
| 69 | put st c_ol add n 1 |
| 70 | end |
| 71 | |
| 72 | note bad: report the position and stop with exit 2 |
| 73 | fn bad to void |
| 74 | ow2 m_bad |
| 75 | v = get st c_p |
| 76 | i = ulong 32 |
| 77 | while true bound 21 |
| 78 | i = sub i 1 |
| 79 | put nb i uchar add rem v 10 48 |
| 80 | v = div v 10 |
| 81 | if eq v 0 |
| 82 | break |
| 83 | end |
| 84 | end |
| 85 | p = i |
| 86 | while lt p 32 bound 32 |
| 87 | k = write 2 nb p sub 32 p |
| 88 | if le k 0 |
| 89 | exit 2 |
| 90 | end |
| 91 | p = add p ulong k |
| 92 | end |
| 93 | ow2 m_tail |
| 94 | exit 2 |
| 95 | end |
| 96 | |
| 97 | fn padd c uchar to void |
| 98 | n = get st c_pl |
| 99 | if eq n 1048576 |
| 100 | ow2 m_deep |
| 101 | exit 2 |
| 102 | end |
| 103 | put pth n c |
| 104 | put st c_pl add n 1 |
| 105 | end |
| 106 | |
| 107 | note pnum: v in decimal onto the path |
| 108 | fn pnum v ulong to void |
| 109 | i = ulong 32 |
| 110 | n = v |
| 111 | while true bound 21 |
| 112 | i = sub i 1 |
| 113 | put nb i uchar add rem n 10 48 |
| 114 | n = div n 10 |
| 115 | if eq n 0 |
| 116 | break |
| 117 | end |
| 118 | end |
| 119 | while lt i 32 bound 32 |
| 120 | padd get nb i |
| 121 | i = add i 1 |
| 122 | end |
| 123 | end |
| 124 | |
| 125 | note opath: the current path and a tab |
| 126 | fn opath to void |
| 127 | n = get st c_pl |
| 128 | if eq n 0 |
| 129 | ob uchar 47 |
| 130 | end |
| 131 | i = ulong 0 |
| 132 | while lt i n bound 1048576 |
| 133 | ob get pth i |
| 134 | i = add i 1 |
| 135 | end |
| 136 | ob uchar 9 |
| 137 | end |
| 138 | |
| 139 | fn peek to uchar |
| 140 | p = get st c_p |
| 141 | if lt p get st c_n |
| 142 | ret get src p |
| 143 | end |
| 144 | ret uchar 0 |
| 145 | end |
| 146 | |
| 147 | fn step to void |
| 148 | put st c_p add get st c_p 1 |
| 149 | end |
| 150 | |
| 151 | fn isws c uchar to bool |
| 152 | if or eq c 32 eq c 10 |
| 153 | ret true |
| 154 | end |
| 155 | ret or eq c 13 eq c 9 |
| 156 | end |
| 157 | |
| 158 | fn skipws to void |
| 159 | p = get st c_p |
| 160 | n = get st c_n |
| 161 | while lt p n bound 16777216 |
| 162 | if not isws get src p |
| 163 | break |
| 164 | end |
| 165 | p = add p 1 |
| 166 | end |
| 167 | put st c_p p |
| 168 | end |
| 169 | |
| 170 | note sput: one decoded byte of a string, into the path (key) or the output (value) |
| 171 | fn sput c uchar k bool to void |
| 172 | if k |
| 173 | if or eq c 47 eq c 92 |
| 174 | padd uchar 92 |
| 175 | end |
| 176 | if eq c 10 |
| 177 | padd uchar 92 |
| 178 | padd uchar 110 |
| 179 | ret |
| 180 | end |
| 181 | if eq c 9 |
| 182 | padd uchar 92 |
| 183 | padd uchar 116 |
| 184 | ret |
| 185 | end |
| 186 | padd c |
| 187 | ret |
| 188 | end |
| 189 | if eq c 10 |
| 190 | ob uchar 92 |
| 191 | ob uchar 110 |
| 192 | ret |
| 193 | end |
| 194 | if eq c 9 |
| 195 | ob uchar 92 |
| 196 | ob uchar 116 |
| 197 | ret |
| 198 | end |
| 199 | if eq c 92 |
| 200 | ob uchar 92 |
| 201 | end |
| 202 | ob c |
| 203 | end |
| 204 | |
| 205 | note hexv: the value of one hex digit, or 16 when c is not one |
| 206 | fn hexv c uchar to ulong |
| 207 | if and ge c 48 le c 57 |
| 208 | ret ulong sub c 48 |
| 209 | end |
| 210 | if and ge c 97 le c 102 |
| 211 | ret ulong sub c 87 |
| 212 | end |
| 213 | if and ge c 65 le c 70 |
| 214 | ret ulong sub c 55 |
| 215 | end |
| 216 | ret ulong 16 |
| 217 | end |
| 218 | |
| 219 | note hex4: the 4 hex digits at the position; the position moves past them |
| 220 | fn hex4 to ulong |
| 221 | p = get st c_p |
| 222 | if gt add p 4 get st c_n |
| 223 | bad |
| 224 | end |
| 225 | v = ulong 0 |
| 226 | for i ulong 4 |
| 227 | d = hexv get src add p i |
| 228 | if eq d 16 |
| 229 | bad |
| 230 | end |
| 231 | v = add mul v 16 d |
| 232 | end |
| 233 | put st c_p add p 4 |
| 234 | ret v |
| 235 | end |
| 236 | |
| 237 | note utf8: code point v as UTF-8 |
| 238 | fn utf8 v ulong k bool to void |
| 239 | if lt v 128 |
| 240 | sput uchar v k |
| 241 | ret |
| 242 | end |
| 243 | if lt v 2048 |
| 244 | sput uchar or 192 shr v 6 k |
| 245 | sput uchar or 128 and v 63 k |
| 246 | ret |
| 247 | end |
| 248 | if lt v 65536 |
| 249 | sput uchar or 224 shr v 12 k |
| 250 | sput uchar or 128 and shr v 6 63 k |
| 251 | sput uchar or 128 and v 63 k |
| 252 | ret |
| 253 | end |
| 254 | sput uchar or 240 shr v 18 k |
| 255 | sput uchar or 128 and shr v 12 63 k |
| 256 | sput uchar or 128 and shr v 6 63 k |
| 257 | sput uchar or 128 and v 63 k |
| 258 | end |
| 259 | |
| 260 | note uesc: after `\u`; a high surrogate must be followed by `\u` and a low one |
| 261 | fn uesc k bool to void |
| 262 | v = hex4 |
| 263 | if and ge v 56320 le v 57343 |
| 264 | bad |
| 265 | end |
| 266 | if and ge v 55296 le v 56319 |
| 267 | if ne peek 92 |
| 268 | bad |
| 269 | end |
| 270 | step |
| 271 | if ne peek 117 |
| 272 | bad |
| 273 | end |
| 274 | step |
| 275 | w = hex4 |
| 276 | if or lt w 56320 gt w 57343 |
| 277 | bad |
| 278 | end |
| 279 | v = add 65536 add shl sub v 55296 10 sub w 56320 |
| 280 | end |
| 281 | utf8 v k |
| 282 | end |
| 283 | |
| 284 | note esc: after a backslash |
| 285 | fn esc k bool to void |
| 286 | e = peek |
| 287 | step |
| 288 | if or eq e 34 or eq e 92 eq e 47 |
| 289 | sput e k |
| 290 | ret |
| 291 | end |
| 292 | if eq e 98 |
| 293 | sput uchar 8 k |
| 294 | ret |
| 295 | end |
| 296 | if eq e 102 |
| 297 | sput uchar 12 k |
| 298 | ret |
| 299 | end |
| 300 | if eq e 110 |
| 301 | sput uchar 10 k |
| 302 | ret |
| 303 | end |
| 304 | if eq e 114 |
| 305 | sput uchar 13 k |
| 306 | ret |
| 307 | end |
| 308 | if eq e 116 |
| 309 | sput uchar 9 k |
| 310 | ret |
| 311 | end |
| 312 | if eq e 117 |
| 313 | uesc k |
| 314 | ret |
| 315 | end |
| 316 | put st c_p sub get st c_p 1 |
| 317 | bad |
| 318 | end |
| 319 | |
| 320 | note str: the string at the position (its opening quote), decoded into the path or the output |
| 321 | fn str k bool to void |
| 322 | step |
| 323 | while true bound 16777216 |
| 324 | if ge get st c_p get st c_n |
| 325 | bad |
| 326 | end |
| 327 | c = peek |
| 328 | step |
| 329 | if eq c 34 |
| 330 | ret |
| 331 | end |
| 332 | if lt c 32 |
| 333 | put st c_p sub get st c_p 1 |
| 334 | bad |
| 335 | end |
| 336 | if eq c 92 |
| 337 | esc k |
| 338 | else |
| 339 | sput c k |
| 340 | end |
| 341 | end |
| 342 | end |
| 343 | |
| 344 | fn same s ulong e ulong t view uchar * to bool |
| 345 | if ne sub e s len t |
| 346 | ret false |
| 347 | end |
| 348 | for i ulong len t bound 8 |
| 349 | if ne get src add s i get t i |
| 350 | ret false |
| 351 | end |
| 352 | end |
| 353 | ret true |
| 354 | end |
| 355 | |
| 356 | fn isnum c uchar to bool |
| 357 | if and ge c 48 le c 57 |
| 358 | ret true |
| 359 | end |
| 360 | if or eq c 45 eq c 43 |
| 361 | ret true |
| 362 | end |
| 363 | ret or eq c 46 or eq c 101 eq c 69 |
| 364 | end |
| 365 | |
| 366 | note JSON numbers: optional minus, integer without leading zero, fraction and exponent. |
| 367 | fn jdig c uchar to bool |
| 368 | ret and ge c 48 le c 57 |
| 369 | end |
| 370 | fn jnumok s ulong e ulong to bool |
| 371 | i = s |
| 372 | if ge i e |
| 373 | ret false |
| 374 | end |
| 375 | if eq get src i 45 |
| 376 | i = add i 1 |
| 377 | end |
| 378 | if ge i e |
| 379 | ret false |
| 380 | end |
| 381 | if eq get src i 48 |
| 382 | i = add i 1 |
| 383 | if lt i e |
| 384 | if jdig get src i |
| 385 | ret false |
| 386 | end |
| 387 | end |
| 388 | else |
| 389 | c = get src i |
| 390 | if not and ge c 49 le c 57 |
| 391 | ret false |
| 392 | end |
| 393 | while lt i e bound 16777216 |
| 394 | if not jdig get src i |
| 395 | break |
| 396 | end |
| 397 | i = add i 1 |
| 398 | end |
| 399 | end |
| 400 | if lt i e |
| 401 | if eq get src i 46 |
| 402 | i = add i 1 |
| 403 | a = i |
| 404 | while lt i e bound 16777216 |
| 405 | if not jdig get src i |
| 406 | break |
| 407 | end |
| 408 | i = add i 1 |
| 409 | end |
| 410 | if eq i a |
| 411 | ret false |
| 412 | end |
| 413 | end |
| 414 | end |
| 415 | if lt i e |
| 416 | c = get src i |
| 417 | if or eq c 101 eq c 69 |
| 418 | i = add i 1 |
| 419 | if lt i e |
| 420 | c = get src i |
| 421 | if or eq c 43 eq c 45 |
| 422 | i = add i 1 |
| 423 | end |
| 424 | end |
| 425 | a = i |
| 426 | while lt i e bound 16777216 |
| 427 | if not jdig get src i |
| 428 | break |
| 429 | end |
| 430 | i = add i 1 |
| 431 | end |
| 432 | if eq i a |
| 433 | ret false |
| 434 | end |
| 435 | end |
| 436 | end |
| 437 | ret eq i e |
| 438 | end |
| 439 | note lit: a number, true, false or null, copied as written |
| 440 | fn lit to void |
| 441 | s = get st c_p |
| 442 | p = s |
| 443 | n = get st c_n |
| 444 | while lt p n bound 16777216 |
| 445 | c = get src p |
| 446 | if or isws c or eq c 44 or eq c 125 eq c 93 |
| 447 | break |
| 448 | end |
| 449 | p = add p 1 |
| 450 | end |
| 451 | if eq p s |
| 452 | bad |
| 453 | end |
| 454 | c0 = get src s |
| 455 | if or eq c0 116 or eq c0 102 eq c0 110 |
| 456 | if not or same s p k_true or same s p k_false same s p k_null |
| 457 | bad |
| 458 | end |
| 459 | else |
| 460 | if not jnumok s p |
| 461 | bad |
| 462 | end |
| 463 | end |
| 464 | opath |
| 465 | i = s |
| 466 | while lt i p bound 16777216 |
| 467 | ob get src i |
| 468 | i = add i 1 |
| 469 | end |
| 470 | ob uchar 10 |
| 471 | put st c_p p |
| 472 | end |
| 473 | |
| 474 | note empty: an empty object or array, written as its two brackets |
| 475 | fn empty a uchar b uchar to void |
| 476 | opath |
| 477 | ob a |
| 478 | ob b |
| 479 | ob uchar 10 |
| 480 | end |
| 481 | |
| 482 | fn obj to void |
| 483 | step |
| 484 | skipws |
| 485 | if eq peek 125 |
| 486 | step |
| 487 | empty uchar 123 uchar 125 |
| 488 | ret |
| 489 | end |
| 490 | note Nonempty object marker preserves duplicate container occurrences for security consumers. |
| 491 | opath |
| 492 | ob uchar 123 |
| 493 | ob uchar 10 |
| 494 | base = get st c_pl |
| 495 | while true bound 16777216 |
| 496 | skipws |
| 497 | if ne peek 34 |
| 498 | bad |
| 499 | end |
| 500 | padd uchar 47 |
| 501 | str true |
| 502 | skipws |
| 503 | if ne peek 58 |
| 504 | bad |
| 505 | end |
| 506 | step |
| 507 | val |
| 508 | put st c_pl base |
| 509 | skipws |
| 510 | c = peek |
| 511 | if eq c 125 |
| 512 | step |
| 513 | ret |
| 514 | end |
| 515 | if ne c 44 |
| 516 | bad |
| 517 | end |
| 518 | step |
| 519 | end |
| 520 | end |
| 521 | |
| 522 | fn arr to void |
| 523 | step |
| 524 | skipws |
| 525 | if eq peek 93 |
| 526 | step |
| 527 | empty uchar 91 uchar 93 |
| 528 | ret |
| 529 | end |
| 530 | note Preserve nonempty array occurrences as well as their indexed children. |
| 531 | opath |
| 532 | ob uchar 91 |
| 533 | ob uchar 10 |
| 534 | base = get st c_pl |
| 535 | i = ulong 0 |
| 536 | while true bound 16777216 |
| 537 | padd uchar 47 |
| 538 | pnum i |
| 539 | val |
| 540 | put st c_pl base |
| 541 | skipws |
| 542 | c = peek |
| 543 | if eq c 93 |
| 544 | step |
| 545 | ret |
| 546 | end |
| 547 | if ne c 44 |
| 548 | bad |
| 549 | end |
| 550 | step |
| 551 | i = add i 1 |
| 552 | end |
| 553 | end |
| 554 | |
| 555 | fn val to void |
| 556 | skipws |
| 557 | if ge get st c_p get st c_n |
| 558 | bad |
| 559 | end |
| 560 | c = peek |
| 561 | if eq c 123 |
| 562 | obj |
| 563 | ret |
| 564 | end |
| 565 | if eq c 91 |
| 566 | arr |
| 567 | ret |
| 568 | end |
| 569 | if eq c 34 |
| 570 | opath |
| 571 | str false |
| 572 | ob uchar 10 |
| 573 | ret |
| 574 | end |
| 575 | lit |
| 576 | end |
| 577 | |
| 578 | fn main to int |
| 579 | n = ulong 0 |
| 580 | while true bound 16777217 |
| 581 | if eq n 16777216 |
| 582 | ow2 m_big |
| 583 | ret 2 |
| 584 | end |
| 585 | k = read 0 src n sub 16777216 n |
| 586 | if lt k 0 |
| 587 | ow2 m_big |
| 588 | ret 2 |
| 589 | end |
| 590 | if eq k 0 |
| 591 | break |
| 592 | end |
| 593 | n = add n ulong k |
| 594 | end |
| 595 | put st c_n n |
| 596 | put st c_p 0 |
| 597 | put st c_pl 0 |
| 598 | put st c_ol 0 |
| 599 | val |
| 600 | skipws |
| 601 | if ne get st c_p n |
| 602 | bad |
| 603 | end |
| 604 | flush |
| 605 | ret 0 |
| 606 | end |