Method: YAJI::Parser#parse

Defined in:
ext/yaji/parser_ext.c

#parse(*args) ⇒ Object



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
# File 'ext/yaji/parser_ext.c', line 275

static VALUE rb_yaji_parser_parse(int argc, VALUE* argv, VALUE self)
{
  yajl_status rc;
  yaji_parser* p = (yaji_parser*) DATA_PTR(self);
  int i;

  if (NIL_P(p->input)) {
    rb_raise(rb_eArgError, "input object required to use #parse method");
  }
  rb_scan_args(argc, argv, "00&", &p->parser_cb);
  RETURN_ENUMERATOR(self, argc, argv);

  p->chunk = Qnil;

  if (rb_respond_to(p->input, id_perform)) {
    rb_funcall(p->input, id_perform, 0);
  } else {
    p->chunk = rb_str_new(NULL, 0);
    while (rb_funcall(p->input, id_read, 2, p->rbufsize, p->chunk) != Qnil) {
      rb_yaji_parser_parse_chunk(p->chunk, self);
    }
  }

  p->events = rb_ary_new();
  rc = yajl_parse_complete(p->handle);

  if (rc == yajl_status_error ||
      (rc == yajl_status_insufficient_data && p->chunk != Qnil &&
       RSTRING_LEN(rb_funcall(p->chunk, id_strip, 0)) != 0)) {
    RERAISE_PARSER_ERROR(p);
  }
  for (i=0; i<RARRAY_LEN(p->events); i++) {
    rb_funcall(p->parser_cb, id_call, 1, RARRAY_PTR(p->events)[i]);
  }

  return Qnil;
}