Project

General

Profile

Feature #14833 ยป ast_node_pp.patch

pocke (Masataka Kuwabara), 06/07/2018 02:44 PM

View differences:

ast.c
465 465
}
466 466

  
467 467
static VALUE
468
rb_ast_node_inspect(VALUE self)
468
ast_node_inspect_head(VALUE self)
469 469
{
470 470
    VALUE str;
471 471
    VALUE cname;
......
478 478
    rb_str_append(str, cname);
479 479
    rb_str_cat2(str, "(");
480 480
    rb_str_catf(str, "%s(%d) %d:%d, %d:%d", node_type_to_str(data->node), nd_type(data->node), nd_first_lineno(data->node), nd_first_column(data->node), nd_last_lineno(data->node), nd_last_column(data->node));
481

  
482
    return str;
483
}
484

  
485
static VALUE
486
rb_ast_node_inspect(VALUE self)
487
{
488
    VALUE str;
489
    str = ast_node_inspect_head(self);
481 490
    rb_str_cat2(str, "): >");
482 491

  
483 492
    return str;
484 493
}
485 494

  
495

  
496
static VALUE
497
ast_node_pretty_prrint_children(VALUE _, VALUE ary)
498
{
499
    VALUE self, pp;
500
    VALUE children;
501
    VALUE child;
502
    long i;
503

  
504
    self = RARRAY_AREF(ary, 0);
505
    pp = RARRAY_AREF(ary, 1);
506
    children = rb_ast_node_children(self);
507

  
508
    rb_funcall(pp, rb_intern("breakable"), 0, 0);
509
    for (i = 0; i < RARRAY_LEN(children); i++) {
510
        if (i != 0) rb_funcall(pp, rb_intern("comma_breakable"), 0);
511
        child = RARRAY_AREF(children, i);
512
        rb_funcall(pp, rb_intern("pp"), 1, child);
513
    }
514
    return Qnil;
515
}
516

  
517
static VALUE
518
rb_ast_node_pretty_print(VALUE self, VALUE pp)
519
{
520
    VALUE str;
521
    VALUE group_argv[1];
522
    group_argv[0] = INT2NUM(2);
523

  
524
    str = ast_node_inspect_head(self);
525

  
526
    rb_funcall(pp, rb_intern("text"), 1, str);
527

  
528
    rb_block_call(pp, rb_intern("group"), 1, group_argv, ast_node_pretty_prrint_children, rb_ary_new3(2, self, pp));
529

  
530
    rb_funcall(pp, rb_intern("text"), 1, rb_str_new2("): >"));
531
    return str;
532
}
533

  
486 534
void
487 535
Init_ast(void)
488 536
{
......
500 548
    rb_define_method(rb_cNode, "last_column", rb_ast_node_last_column, 0);
501 549
    rb_define_method(rb_cNode, "children", rb_ast_node_children, 0);
502 550
    rb_define_method(rb_cNode, "inspect", rb_ast_node_inspect, 0);
551
    rb_define_method(rb_cNode, "pretty_print", rb_ast_node_pretty_print, 1);
503 552
}