15 #define A(str) rb_str_cat2(buf, (str)) 16 #define AR(str) rb_str_concat(buf, (str)) 18 #define A_INDENT add_indent(buf, indent) 19 #define D_INDENT rb_str_cat2(indent, next_indent) 20 #define D_DEDENT rb_str_resize(indent, RSTRING_LEN(indent) - 4) 21 #define A_ID(id) add_id(buf, (id)) 22 #define A_INT(val) rb_str_catf(buf, "%d", (val)) 23 #define A_LONG(val) rb_str_catf(buf, "%ld", (val)) 24 #define A_LIT(lit) AR(rb_inspect(lit)) 25 #define A_NODE_HEADER(node, term) \ 26 rb_str_catf(buf, "@ %s (line: %d)"term, ruby_node_name(nd_type(node)), nd_line(node)) 27 #define A_FIELD_HEADER(len, name, term) \ 28 rb_str_catf(buf, "+- %.*s:"term, (len), (name)) 29 #define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term)) 31 #define D_NULL_NODE (A_INDENT, A("(null node)\n")) 32 #define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n")) 34 #define COMPOUND_FIELD(len, name, block) \ 36 D_FIELD_HEADER((len), (name), "\n"); \ 42 #define COMPOUND_FIELD1(name, ann, block) \ 43 COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \ 44 FIELD_NAME_DESC(name, ann), \ 47 #define FIELD_NAME_DESC(name, ann) name " (" ann ")" 48 #define FIELD_NAME_LEN(name, ann) (int)( \ 50 rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \ 52 #define SIMPLE_FIELD(len, name) \ 53 for (D_FIELD_HEADER((len), (name), " "), field_flag = 1; \ 55 A("\n"), field_flag = 0) 57 #define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann)) 58 #define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann) 59 #define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name) 60 #define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID((node->name)->id) 61 #define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name) 62 #define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name) 63 #define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name) 64 #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc) 66 #define F_NODE(name, ann) \ 67 COMPOUND_FIELD1(#name, ann, dump_node(buf, indent, comment, node->name)) 68 #define F_OPTION(name, ann) \ 69 COMPOUND_FIELD1(#name, ann, dump_option(buf, indent, node->name)) 73 A_INDENT; A("| # " ann "\n"); \ 76 #define LAST_NODE (next_indent = " ") 96 A(
"(internal variable)");
148 node = node->nd_next;
171 ANN(
"statement sequence");
172 ANN(
"format: [nd_head]; ...; [nd_next]");
173 ANN(
"example: foo; bar");
178 comment ?
"statement #" :
"", ++i);
181 dump_node(buf, indent, comment, node->nd_head);
183 }
while (node->nd_next &&
185 (node = node->nd_next, 1));
186 if (!node->nd_next)
break;
193 ANN(
"format: if [nd_cond] then [nd_body] else [nd_else] end");
194 ANN(
"example: if x == 1 then foo else bar end");
202 ANN(
"case statement");
203 ANN(
"format: case [nd_head]; [nd_body]; end");
204 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
212 ANN(
"format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
213 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
221 ANN(
"wrapper for -n option");
222 ANN(
"format: ruby -ne '[nd_body]' (nd_cond is `gets')");
223 ANN(
"example: ruby -ne 'p $_'");
226 ANN(
"while statement");
227 ANN(
"format: while [nd_cond]; [nd_body]; end");
228 ANN(
"example: while x == 1; foo; end");
231 ANN(
"until statement");
232 ANN(
"format: until [nd_cond]; [nd_body]; end");
233 ANN(
"example: until x == 1; foo; end");
236 A_INT((
int)node->nd_state);
237 A((node->nd_state == 1) ?
" (while-end)" :
" (begin-end-while)");
245 ANN(
"method call with block");
246 ANN(
"format: [nd_iter] { [nd_body] }");
247 ANN(
"example: 3.times { foo }");
250 ANN(
"for statement");
251 ANN(
"format: for * in [nd_iter] do [nd_body] end");
252 ANN(
"example: for i in 1..3 do foo end");
260 ANN(
"for statement");
261 ANN(
"format: break [nd_stts]");
262 ANN(
"example: break 1");
265 ANN(
"next statement");
266 ANN(
"format: next [nd_stts]");
267 ANN(
"example: next 1");
270 ANN(
"return statement");
271 ANN(
"format: return [nd_stts]");
272 ANN(
"example: return 1");
279 ANN(
"redo statement");
281 ANN(
"example: redo");
285 ANN(
"retry statement");
286 ANN(
"format: retry");
287 ANN(
"example: retry");
291 ANN(
"begin statement");
292 ANN(
"format: begin; [nd_body]; end");
293 ANN(
"example: begin; 1; end");
299 ANN(
"rescue clause");
300 ANN(
"format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
301 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
309 ANN(
"rescue clause (cont'd)");
310 ANN(
"format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
311 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
319 ANN(
"ensure clause");
320 ANN(
"format: begin; [nd_head]; ensure; [nd_ensr]; end");
321 ANN(
"example: begin; foo; ensure; bar; end");
329 ANN(
"format: [nd_1st] && [nd_2nd]");
330 ANN(
"example: foo && bar");
334 ANN(
"format: [nd_1st] || [nd_2nd]");
335 ANN(
"example: foo && bar");
343 ANN(
"multiple assignment");
344 ANN(
"format: [nd_head], [nd_args] = [nd_value]");
345 ANN(
"example: a, b = foo");
353 F_MSG(
nd_args,
"splatn",
"-1 (rest argument without name)");
358 ANN(
"local variable assignment");
359 ANN(
"format: [nd_vid](lvar) = [nd_value]");
360 ANN(
"example: x = foo");
363 ANN(
"dynamic variable assignment (out of current scope)");
364 ANN(
"format: [nd_vid](dvar) = [nd_value]");
365 ANN(
"example: x = nil; 1.times { x = foo }");
368 ANN(
"dynamic variable assignment (in current scope)");
369 ANN(
"format: [nd_vid](current dvar) = [nd_value]");
370 ANN(
"example: 1.times { x = foo }");
373 ANN(
"instance variable assignment");
374 ANN(
"format: [nd_vid](ivar) = [nd_value]");
375 ANN(
"example: @x = foo");
378 ANN(
"class variable assignment");
379 ANN(
"format: [nd_vid](cvar) = [nd_value]");
380 ANN(
"example: @@x = foo");
384 if (node->nd_value == (
NODE *)-1) {
393 ANN(
"global variable assignment");
394 ANN(
"format: [nd_entry](gvar) = [nd_value]");
395 ANN(
"example: $x = foo");
402 ANN(
"constant declaration");
403 ANN(
"format: [nd_else]::[nd_vid](constant) = [nd_value]");
404 ANN(
"example: X = foo");
410 F_MSG(
nd_vid,
"variable",
"0 (see extension field)");
418 ANN(
"array assignment with operator");
419 ANN(
"format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
420 ANN(
"example: ary[1] += foo");
429 ANN(
"attr assignment with operator");
430 ANN(
"format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
431 ANN(
" where [attr]: [nd_next->nd_vid]");
432 ANN(
"example: struct.field += foo");
435 if (node->nd_next->nd_aid)
A(
"? ");
436 A_ID(node->nd_next->nd_vid);
439 switch (node->nd_next->nd_mid) {
440 case 0:
A(
"0 (||)");
break;
441 case 1:
A(
"1 (&&)");
break;
442 default:
A_ID(node->nd_next->nd_mid);
450 ANN(
"assignment with && operator");
451 ANN(
"format: [nd_head] &&= [nd_value]");
452 ANN(
"example: foo &&= bar");
455 ANN(
"assignment with || operator");
456 ANN(
"format: [nd_head] ||= [nd_value]");
457 ANN(
"example: foo ||= bar");
465 ANN(
"method invocation");
466 ANN(
"format: [nd_recv].[nd_mid]([nd_args])");
467 ANN(
"example: obj.foo(1)");
475 ANN(
"function call");
476 ANN(
"format: [nd_mid]([nd_args])");
477 ANN(
"example: foo(1)");
484 ANN(
"function call with no argument");
485 ANN(
"format: [nd_mid]");
491 ANN(
"safe method invocation");
492 ANN(
"format: [nd_recv]&.[nd_mid]([nd_args])");
493 ANN(
"example: obj&.foo(1)");
501 ANN(
"super invocation");
502 ANN(
"format: super [nd_args]");
503 ANN(
"example: super 1");
509 ANN(
"super invocation with no argument");
510 ANN(
"format: super");
511 ANN(
"example: super");
515 ANN(
"array constructor");
516 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
517 ANN(
"example: [1, 2, 3]");
520 ANN(
"return arguments");
521 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
522 ANN(
"example: return 1, 2, 3");
528 ANN(
"empty array constructor");
534 ANN(
"hash constructor");
535 ANN(
"format: { [nd_head] }");
536 ANN(
"example: { 1 => 2, 3 => 4 }");
542 ANN(
"yield invocation");
543 ANN(
"format: yield [nd_head]");
544 ANN(
"example: yield 1");
550 ANN(
"local variable reference");
551 ANN(
"format: [nd_vid](lvar)");
555 ANN(
"dynamic variable reference");
556 ANN(
"format: [nd_vid](dvar)");
557 ANN(
"example: 1.times { x = 1; x }");
560 ANN(
"instance variable reference");
561 ANN(
"format: [nd_vid](ivar)");
565 ANN(
"constant reference");
566 ANN(
"format: [nd_vid](constant)");
570 ANN(
"class variable reference");
571 ANN(
"format: [nd_vid](cvar)");
578 ANN(
"global variable reference");
579 ANN(
"format: [nd_entry](gvar)");
585 ANN(
"nth special variable reference");
586 ANN(
"format: $[nd_nth]");
587 ANN(
"example: $1, $2, ..");
592 ANN(
"back special variable reference");
593 ANN(
"format: $[nd_nth]");
594 ANN(
"example: $&, $`, $', $+");
598 name[1] = (char)node->nd_nth;
605 ANN(
"match expression (against $_ implicitly)");
606 ANN(
"format: [nd_lit] (in condition)");
607 ANN(
"example: if /foo/; foo; end");
612 ANN(
"match expression (regexp first)");
613 ANN(
"format: [nd_recv] =~ [nd_value]");
614 ANN(
"example: /foo/ =~ 'foo'");
625 ANN(
"match expression (regexp second)");
626 ANN(
"format: [nd_recv] =~ [nd_value]");
627 ANN(
"example: 'foo' =~ /foo/");
635 ANN(
"format: [nd_lit]");
636 ANN(
"example: 1, /foo/");
639 ANN(
"string literal");
640 ANN(
"format: [nd_lit]");
641 ANN(
"example: 'foo'");
644 ANN(
"xstring literal");
645 ANN(
"format: [nd_lit]");
646 ANN(
"example: `foo`");
652 ANN(
"string literal with interpolation");
653 ANN(
"format: [nd_lit]");
654 ANN(
"example: \"foo#{ bar }baz\"");
657 ANN(
"xstring literal with interpolation");
658 ANN(
"format: [nd_lit]");
659 ANN(
"example: `foo#{ bar }baz`");
662 ANN(
"regexp literal with interpolation");
663 ANN(
"format: [nd_lit]");
664 ANN(
"example: /foo#{ bar }baz/");
667 ANN(
"regexp literal with interpolation and once flag");
668 ANN(
"format: [nd_lit]");
669 ANN(
"example: /foo#{ bar }baz/o");
672 ANN(
"symbol literal with interpolation");
673 ANN(
"format: [nd_lit]");
674 ANN(
"example: :\"foo#{ bar }baz\"");
683 ANN(
"interpolation expression");
684 ANN(
"format: \"..#{ [nd_lit] }..\"");
685 ANN(
"example: \"foo#{ bar }baz\"");
691 ANN(
"splat argument following arguments");
692 ANN(
"format: ..(*[nd_head], [nd_body..])");
693 ANN(
"example: foo(*ary, post_arg1, post_arg2)");
700 ANN(
"splat argument following one argument");
701 ANN(
"format: ..(*[nd_head], [nd_body])");
702 ANN(
"example: foo(*ary, post_arg)");
709 ANN(
"splat argument");
710 ANN(
"format: *[nd_head]");
711 ANN(
"example: foo(*ary)");
717 ANN(
"arguments with block argument");
718 ANN(
"format: ..([nd_head], &[nd_body])");
719 ANN(
"example: foo(x, &blk)");
726 ANN(
"method definition");
727 ANN(
"format: def [nd_mid] [nd_defn]; end");
728 ANN(
"example; def foo; bar; end");
735 ANN(
"singleton method definition");
736 ANN(
"format: def [nd_recv].[nd_mid] [nd_defn]; end");
737 ANN(
"example; def obj.foo; bar; end");
745 ANN(
"method alias statement");
746 ANN(
"format: alias [u1.node] [u2.node]");
747 ANN(
"example: alias bar foo");
748 F_NODE(u1.node,
"new name");
750 F_NODE(u2.node,
"old name");
754 ANN(
"global variable alias statement");
755 ANN(
"format: alias [u1.id](gvar) [u2.id](gvar)");
756 ANN(
"example: alias $y $x");
757 F_ID(u1.id,
"new name");
758 F_ID(u2.id,
"old name");
762 ANN(
"method alias statement");
763 ANN(
"format: undef [u2.node]");
764 ANN(
"example: undef foo");
766 F_NODE(u2.node,
"old name");
770 ANN(
"class definition");
771 ANN(
"format: class [nd_cpath] < [nd_super]; [nd_body]; end");
772 ANN(
"example: class C2 < C; ..; end");
780 ANN(
"module definition");
781 ANN(
"format: module [nd_cpath]; [nd_body]; end");
782 ANN(
"example: module M; ..; end");
789 ANN(
"singleton class definition");
790 ANN(
"format: class << [nd_recv]; [nd_body]; end");
791 ANN(
"example: class << obj; ..; end");
798 ANN(
"scoped constant reference");
799 ANN(
"format: [nd_head]::[nd_mid]");
800 ANN(
"example: M::C");
807 ANN(
"top-level constant reference");
808 ANN(
"format: ::[nd_mid]");
809 ANN(
"example: ::Object");
814 ANN(
"range constructor (incl.)");
815 ANN(
"format: [nd_beg]..[nd_end]");
816 ANN(
"example: 1..5");
819 ANN(
"range constructor (excl.)");
820 ANN(
"format: [nd_beg]...[nd_end]");
821 ANN(
"example: 1...5");
824 ANN(
"flip-flop condition (incl.)");
825 ANN(
"format: [nd_beg]..[nd_end]");
826 ANN(
"example: if (x==1)..(x==5); foo; end");
829 ANN(
"flip-flop condition (excl.)");
830 ANN(
"format: [nd_beg]...[nd_end]");
831 ANN(
"example: if (x==1)...(x==5); foo; end");
841 ANN(
"example: self");
853 ANN(
"example: true");
858 ANN(
"format: false");
859 ANN(
"example: false");
863 ANN(
"virtual reference to $!");
864 ANN(
"format: rescue => id");
865 ANN(
"example: rescue => id");
869 ANN(
"defined? expression");
870 ANN(
"format: defined?([nd_head])");
871 ANN(
"example: defined?(foo)");
876 ANN(
"post-execution");
877 ANN(
"format: END { [nd_body] }");
878 ANN(
"example: END { foo }");
884 ANN(
"attr assignment");
885 ANN(
"format: [nd_recv].[nd_mid] = [nd_args]");
886 ANN(
"example: struct.field = foo");
887 if (node->nd_recv == (
NODE *) 1) {
899 ANN(
"pre-execution");
900 ANN(
"format: BEGIN { [nd_head] }; [nd_body]");
901 ANN(
"example: bar; BEGIN { foo }");
902 #define nd_compile_option u3.value 906 if (node->nd_compile_option) {
913 ANN(
"lambda expression");
914 ANN(
"format: -> [nd_body]");
915 ANN(
"example: -> { foo }");
921 ANN(
"optional arguments");
922 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
923 ANN(
"example: def foo(a, b=1, c); end");
930 ANN(
"keyword arguments");
931 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
932 ANN(
"example: def foo(a:1, b:2); end");
939 ANN(
"post arguments");
940 ANN(
"format: *[nd_1st], [nd_2nd..] = ..");
941 ANN(
"example: a, *rest, z = foo");
946 F_MSG(
nd_1st,
"rest argument",
"-1 (rest argument without name)");
953 ANN(
"method parameters");
954 ANN(
"format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
955 ANN(
"example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
956 F_INT(
nd_ainfo->pre_args_num,
"count of mandatory (pre-)arguments");
958 F_INT(
nd_ainfo->post_args_num,
"count of mandatory post-arguments");
971 ANN(
"format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
973 ID *tbl = node->nd_tbl;
975 int size = tbl ? (int)*tbl++ : 0;
976 if (size == 0)
A(
"(empty)");
977 for (i = 0; i <
size; i++) {
978 A_ID(tbl[i]);
if (i < size - 1)
A(
",");
995 "###########################################################\n" 996 "## Do NOT use this node dump for any purpose other than ##\n" 997 "## debug and research. Compatibility is not guaranteed. ##\n" 998 "###########################################################\n\n" 1031 size += (
RNODE(obj)->nd_tbl[0]+1) *
sizeof(*
RNODE(obj)->nd_tbl);
1035 if (
RNODE(obj)->nd_ainfo) {
1036 size +=
sizeof(*
RNODE(obj)->nd_ainfo);
1078 return RNODE(obj)->u3.value;
1111 return RNODE(obj)->u2.value;
1126 return RNODE(obj)->u1.value;
1132 return RNODE(obj)->u2.value;
1145 return RNODE(obj)->u2.value;
VALUE rb_parser_dump_tree(NODE *node, int comment)
void rb_bug(const char *fmt,...)
#define F_NODE(name, ann)
static void dump_option(VALUE buf, VALUE indent, VALUE opt)
#define D_NODE_HEADER(node)
static void add_id(VALUE buf, ID id)
void rb_gc_free_node(VALUE obj)
#define F_CUSTOM1(name, ann)
static void dump_node(VALUE, VALUE, int, NODE *)
void rb_gc_mark(VALUE ptr)
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
#define RB_TYPE_P(obj, type)
#define F_GENTRY(name, ann)
static int add_option_i(VALUE key, VALUE val, VALUE args)
static void add_indent(VALUE buf, VALUE indent)
#define F_LONG(name, ann)
VALUE rb_gc_mark_node(NODE *obj)
unsigned char buf[MIME_BUF_SIZE]
#define F_MSG(name, ann, desc)
VALUE rb_str_new_cstr(const char *)
static const char default_indent[]
size_t rb_node_memsize(VALUE obj)
#define nd_compile_option
static void dump_array(VALUE buf, VALUE indent, int comment, NODE *node)
#define F_OPTION(name, ann)
const char * ruby_node_name(int node)
VALUE rb_str_catf(VALUE str, const char *format,...)
void rb_gc_mark_maybe(VALUE obj)
struct rb_args_info * args