1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
// Copyright (c) 2021, Peter Ohler, All rights reserved.
#include "saj2.h"
#include "cache.h"
#include "mem.h"
#include "oj.h"
#include "parser.h"
static VALUE get_key(ojParser p) {
Saj d = (Saj)p->ctx;
const char *key = buf_str(&p->key);
size_t len = buf_len(&p->key);
volatile VALUE rkey;
if (d->cache_keys) {
rkey = cache_intern(d->str_cache, key, len);
} else {
rkey = rb_utf8_str_new(key, len);
}
return rkey;
}
static void push_key(Saj d, VALUE key) {
if (d->klen <= (size_t)(d->tail - d->keys)) {
size_t off = d->tail - d->keys;
d->klen += d->klen / 2;
OJ_R_REALLOC_N(d->keys, VALUE, d->klen);
d->tail = d->keys + off;
}
*d->tail = key;
d->tail++;
}
static void noop(ojParser p) {
}
static void open_object(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_hash_start_id, 1, Qnil);
}
static void open_object_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_hash_start_id, 3, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void open_object_key(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE key = get_key(p);
push_key(d, key);
rb_funcall(d->handler, oj_hash_start_id, 1, key);
}
static void open_object_loc_key(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE key = get_key(p);
push_key(d, key);
rb_funcall(d->handler, oj_hash_start_id, 3, key, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void open_array(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_array_start_id, 1, Qnil);
}
static void open_array_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_array_start_id, 3, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void open_array_key(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE key = get_key(p);
push_key(d, key);
rb_funcall(d->handler, oj_array_start_id, 1, key);
}
static void open_array_loc_key(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE key = get_key(p);
push_key(d, key);
rb_funcall(d->handler, oj_array_start_id, 3, key, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void close_object(ojParser p) {
Saj d = (Saj)p->ctx;
VALUE key = Qnil;
if (OBJECT_FUN == p->stack[p->depth]) {
d->tail--;
if (d->tail < d->keys) {
rb_raise(rb_eIndexError, "accessing key stack");
}
key = *d->tail;
}
rb_funcall(d->handler, oj_hash_end_id, 1, key);
}
static void close_object_loc(ojParser p) {
Saj d = (Saj)p->ctx;
VALUE key = Qnil;
if (OBJECT_FUN == p->stack[p->depth]) {
d->tail--;
if (d->tail < d->keys) {
rb_raise(rb_eIndexError, "accessing key stack");
}
key = *d->tail;
}
rb_funcall(d->handler, oj_hash_end_id, 3, key, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void close_array(ojParser p) {
Saj d = (Saj)p->ctx;
VALUE key = Qnil;
if (OBJECT_FUN == p->stack[p->depth]) {
d->tail--;
if (d->tail < d->keys) {
rb_raise(rb_eIndexError, "accessing key stack");
}
key = *d->tail;
}
rb_funcall(d->handler, oj_array_end_id, 1, key);
}
static void close_array_loc(ojParser p) {
Saj d = (Saj)p->ctx;
VALUE key = Qnil;
if (OBJECT_FUN == p->stack[p->depth]) {
d->tail--;
if (d->tail < d->keys) {
rb_raise(rb_eIndexError, "accessing key stack");
}
key = *d->tail;
}
rb_funcall(d->handler, oj_array_end_id, 3, key, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void add_null(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qnil, Qnil);
}
static void add_null_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 4, Qnil, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void add_null_key(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qnil, get_key(p));
}
static void add_null_key_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
Qnil,
get_key(p),
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_true(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qtrue, Qnil);
}
static void add_true_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 4, Qtrue, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void add_true_key(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qtrue, get_key(p));
}
static void add_true_key_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
Qtrue,
get_key(p),
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_false(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qfalse, Qnil);
}
static void add_false_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 4, Qfalse, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void add_false_key(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, Qfalse, get_key(p));
}
static void add_false_key_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
Qfalse,
get_key(p),
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_int(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, LONG2NUM(p->num.fixnum), Qnil);
}
static void add_int_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
LONG2NUM(p->num.fixnum),
Qnil,
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_int_key(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, LONG2NUM(p->num.fixnum), get_key(p));
}
static void add_int_key_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
LONG2NUM(p->num.fixnum),
get_key(p),
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_float(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, rb_float_new(p->num.dub), Qnil);
}
static void add_float_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
rb_float_new(p->num.dub),
Qnil,
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_float_key(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler, oj_add_value_id, 2, rb_float_new(p->num.dub), get_key(p));
}
static void add_float_key_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
rb_float_new(p->num.dub),
get_key(p),
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_big(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
2,
rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
Qnil);
}
static void add_big_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
Qnil,
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_big_key(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
2,
rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
get_key(p));
}
static void add_big_key_loc(ojParser p) {
rb_funcall(((Saj)p->ctx)->handler,
oj_add_value_id,
4,
rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))),
get_key(p),
LONG2FIX(p->line),
LONG2FIX(p->cur - p->col));
}
static void add_str(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE rstr;
const char *str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
if (d->cache_str < len) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
rb_funcall(d->handler, oj_add_value_id, 2, rstr, Qnil);
}
static void add_str_loc(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE rstr;
const char *str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
if (d->cache_str < len) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
rb_funcall(d->handler, oj_add_value_id, 4, rstr, Qnil, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void add_str_key(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE rstr;
const char *str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
if (d->cache_str < len) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
rb_funcall(d->handler, oj_add_value_id, 2, rstr, get_key(p));
}
static void add_str_key_loc(ojParser p) {
Saj d = (Saj)p->ctx;
volatile VALUE rstr;
const char *str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
if (d->cache_str < len) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
rb_funcall(d->handler, oj_add_value_id, 4, rstr, get_key(p), LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
}
static void reset(ojParser p) {
Funcs end = p->funcs + 3;
Funcs f;
for (f = p->funcs; f < end; f++) {
f->add_null = noop;
f->add_true = noop;
f->add_false = noop;
f->add_int = noop;
f->add_float = noop;
f->add_big = noop;
f->add_str = noop;
f->open_array = noop;
f->close_array = noop;
f->open_object = noop;
f->close_object = noop;
}
}
static VALUE option(ojParser p, const char *key, VALUE value) {
Saj d = (Saj)p->ctx;
if (0 == strcmp(key, "handler")) {
return d->handler;
}
if (0 == strcmp(key, "handler=")) {
d->tail = d->keys;
d->handler = value;
reset(p);
if (rb_respond_to(value, oj_hash_start_id)) {
if (1 == rb_obj_method_arity(value, oj_hash_start_id)) {
p->funcs[TOP_FUN].open_object = open_object;
p->funcs[ARRAY_FUN].open_object = open_object;
p->funcs[OBJECT_FUN].open_object = open_object_key;
} else {
p->funcs[TOP_FUN].open_object = open_object_loc;
p->funcs[ARRAY_FUN].open_object = open_object_loc;
p->funcs[OBJECT_FUN].open_object = open_object_loc_key;
}
}
if (rb_respond_to(value, oj_array_start_id)) {
if (1 == rb_obj_method_arity(value, oj_array_start_id)) {
p->funcs[TOP_FUN].open_array = open_array;
p->funcs[ARRAY_FUN].open_array = open_array;
p->funcs[OBJECT_FUN].open_array = open_array_key;
} else {
p->funcs[TOP_FUN].open_array = open_array_loc;
p->funcs[ARRAY_FUN].open_array = open_array_loc;
p->funcs[OBJECT_FUN].open_array = open_array_loc_key;
}
}
if (rb_respond_to(value, oj_hash_end_id)) {
if (1 == rb_obj_method_arity(value, oj_hash_end_id)) {
p->funcs[TOP_FUN].close_object = close_object;
p->funcs[ARRAY_FUN].close_object = close_object;
p->funcs[OBJECT_FUN].close_object = close_object;
} else {
p->funcs[TOP_FUN].close_object = close_object_loc;
p->funcs[ARRAY_FUN].close_object = close_object_loc;
p->funcs[OBJECT_FUN].close_object = close_object_loc;
}
}
if (rb_respond_to(value, oj_array_end_id)) {
if (1 == rb_obj_method_arity(value, oj_array_end_id)) {
p->funcs[TOP_FUN].close_array = close_array;
p->funcs[ARRAY_FUN].close_array = close_array;
p->funcs[OBJECT_FUN].close_array = close_array;
} else {
p->funcs[TOP_FUN].close_array = close_array_loc;
p->funcs[ARRAY_FUN].close_array = close_array_loc;
p->funcs[OBJECT_FUN].close_array = close_array_loc;
}
}
if (rb_respond_to(value, oj_add_value_id)) {
if (2 == rb_obj_method_arity(value, oj_add_value_id)) {
p->funcs[TOP_FUN].add_null = add_null;
p->funcs[ARRAY_FUN].add_null = add_null;
p->funcs[OBJECT_FUN].add_null = add_null_key;
p->funcs[TOP_FUN].add_true = add_true;
p->funcs[ARRAY_FUN].add_true = add_true;
p->funcs[OBJECT_FUN].add_true = add_true_key;
p->funcs[TOP_FUN].add_false = add_false;
p->funcs[ARRAY_FUN].add_false = add_false;
p->funcs[OBJECT_FUN].add_false = add_false_key;
p->funcs[TOP_FUN].add_int = add_int;
p->funcs[ARRAY_FUN].add_int = add_int;
p->funcs[OBJECT_FUN].add_int = add_int_key;
p->funcs[TOP_FUN].add_float = add_float;
p->funcs[ARRAY_FUN].add_float = add_float;
p->funcs[OBJECT_FUN].add_float = add_float_key;
p->funcs[TOP_FUN].add_big = add_big;
p->funcs[ARRAY_FUN].add_big = add_big;
p->funcs[OBJECT_FUN].add_big = add_big_key;
p->funcs[TOP_FUN].add_str = add_str;
p->funcs[ARRAY_FUN].add_str = add_str;
p->funcs[OBJECT_FUN].add_str = add_str_key;
} else {
p->funcs[TOP_FUN].add_null = add_null_loc;
p->funcs[ARRAY_FUN].add_null = add_null_loc;
p->funcs[OBJECT_FUN].add_null = add_null_key_loc;
p->funcs[TOP_FUN].add_true = add_true_loc;
p->funcs[ARRAY_FUN].add_true = add_true_loc;
p->funcs[OBJECT_FUN].add_true = add_true_key_loc;
p->funcs[TOP_FUN].add_false = add_false_loc;
p->funcs[ARRAY_FUN].add_false = add_false_loc;
p->funcs[OBJECT_FUN].add_false = add_false_key_loc;
p->funcs[TOP_FUN].add_int = add_int_loc;
p->funcs[ARRAY_FUN].add_int = add_int_loc;
p->funcs[OBJECT_FUN].add_int = add_int_key_loc;
p->funcs[TOP_FUN].add_float = add_float_loc;
p->funcs[ARRAY_FUN].add_float = add_float_loc;
p->funcs[OBJECT_FUN].add_float = add_float_key_loc;
p->funcs[TOP_FUN].add_big = add_big_loc;
p->funcs[ARRAY_FUN].add_big = add_big_loc;
p->funcs[OBJECT_FUN].add_big = add_big_key_loc;
p->funcs[TOP_FUN].add_str = add_str_loc;
p->funcs[ARRAY_FUN].add_str = add_str_loc;
p->funcs[OBJECT_FUN].add_str = add_str_key_loc;
}
}
return Qnil;
}
if (0 == strcmp(key, "cache_keys")) {
return d->cache_keys ? Qtrue : Qfalse;
}
if (0 == strcmp(key, "cache_keys=")) {
d->cache_keys = (Qtrue == value);
return d->cache_keys ? Qtrue : Qfalse;
}
if (0 == strcmp(key, "cache_strings")) {
return INT2NUM((int)d->cache_str);
}
if (0 == strcmp(key, "cache_strings=")) {
int limit = NUM2INT(value);
if (CACHE_MAX_KEY < limit) {
limit = CACHE_MAX_KEY;
} else if (limit < 0) {
limit = 0;
}
d->cache_str = limit;
return INT2NUM((int)d->cache_str);
}
rb_raise(rb_eArgError, "%s is not an option for the SAJ (Simple API for JSON) saj", key);
return Qnil; // Never reached due to the raise but required by the compiler.
}
static VALUE result(ojParser p) {
return Qnil;
}
static void start(ojParser p) {
Saj d = (Saj)p->ctx;
d->tail = d->keys;
}
static void dfree(ojParser p) {
Saj d = (Saj)p->ctx;
if (NULL != d->keys) {
OJ_R_FREE(d->keys);
}
cache_free(d->str_cache);
OJ_R_FREE(p->ctx);
}
static void mark(ojParser p) {
if (NULL == p || NULL == p->ctx) {
return;
}
Saj d = (Saj)p->ctx;
VALUE *kp;
cache_mark(d->str_cache);
if (Qnil != d->handler) {
rb_gc_mark(d->handler);
}
if (!d->cache_keys) {
for (kp = d->keys; kp < d->tail; kp++) {
rb_gc_mark(*kp);
}
}
}
static VALUE form_str(const char *str, size_t len) {
return rb_str_freeze(rb_utf8_str_new(str, len));
}
void oj_init_saj(ojParser p, Saj d) {
d->klen = 256;
d->keys = OJ_R_ALLOC_N(VALUE, d->klen);
d->tail = d->keys;
d->handler = Qnil;
d->str_cache = cache_create(0, form_str, true, false);
d->cache_str = 16;
d->cache_keys = true;
d->thread_safe = false;
p->ctx = (void *)d;
reset(p);
p->option = option;
p->result = result;
p->free = dfree;
p->mark = mark;
p->start = start;
}
void oj_set_parser_saj(ojParser p) {
Saj d = OJ_R_ALLOC(struct _saj);
oj_init_saj(p, d);
}
|