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 A_ID(id) add_id(buf, (id))
20 #define A_INT(val) rb_str_catf(buf, "%d", (val));
21 #define A_LONG(val) rb_str_catf(buf, "%ld", (val));
22 #define A_LIT(lit) AR(rb_inspect(lit))
23 #define A_NODE_HEADER(node) \
24 rb_str_catf(buf, "@ %s (line: %d)", ruby_node_name(nd_type(node)), nd_line(node))
25 #define A_FIELD_HEADER(name) \
26 rb_str_catf(buf, "+- %s:", (name))
28 #define D_NULL_NODE A_INDENT; A("(null node)"); A("\n");
29 #define D_NODE_HEADER(node) A_INDENT; A_NODE_HEADER(node); A("\n");
31 #define COMPOUND_FIELD(name, name2, block) \
33 A_INDENT; A_FIELD_HEADER(comment ? (name2) : (name)); A("\n"); \
34 rb_str_cat2(indent, next_indent); \
36 rb_str_resize(indent, RSTRING_LEN(indent) - 4); \
39 #define SIMPLE_FIELD(name, name2, block) \
41 A_INDENT; A_FIELD_HEADER(comment ? (name2) : (name)); A(" "); block; A("\n"); \
44 #define F_CUSTOM1(name, ann, block) SIMPLE_FIELD(#name, #name " (" ann ")", block)
45 #define F_ID(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_ID(node->name))
46 #define F_GENTRY(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_ID((node->name)->id))
47 #define F_INT(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_INT(node->name))
48 #define F_LONG(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_LONG(node->name))
49 #define F_LIT(name, ann) SIMPLE_FIELD(#name, #name " (" ann ")", A_LIT(node->name))
50 #define F_MSG(name, ann, desc) SIMPLE_FIELD(#name, #name " (" ann ")", A(desc))
52 #define F_CUSTOM2(name, ann, block) \
53 COMPOUND_FIELD(#name, #name " (" ann ")", block)
55 #define F_NODE(name, ann) \
56 COMPOUND_FIELD(#name, #name " (" ann ")", dump_node(buf, indent, comment, node->name))
60 A_INDENT; A("| # "); A(ann); A("\n"); \
63 #define LAST_NODE (next_indent = " ")
83 A(
"(internal variable)");
91 const char *next_indent =
"| ";
102 ANN(
"statement sequence");
103 ANN(
"format: [nd_head]; [nd_next]");
104 ANN(
"example: foo; bar");
112 ANN(
"format: if [nd_cond] then [nd_body] else [nd_else] end");
113 ANN(
"example: if x == 1 then foo else bar end");
121 ANN(
"case statement");
122 ANN(
"format: case [nd_head]; [nd_body]; end");
123 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
131 ANN(
"format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
132 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
140 ANN(
"wrapper for -n option");
141 ANN(
"format: ruby -ne '[nd_body]' (nd_cond is `gets')");
142 ANN(
"example: ruby -ne 'p $_'");
145 ANN(
"while statement");
146 ANN(
"format: while [nd_cond]; [nd_body]; end");
147 ANN(
"example: while x == 1; foo; end");
150 ANN(
"until statement");
151 ANN(
"format: until [nd_cond]; [nd_body]; end");
152 ANN(
"example: until x == 1; foo; end");
155 A_INT((
int)node->nd_state);
156 A((node->nd_state == 1) ?
" (while-end)" :
" (begin-end-while)");
164 ANN(
"method call with block");
165 ANN(
"format: [nd_iter] { [nd_body] }");
166 ANN(
"example: 3.times { foo }");
169 ANN(
"for statement");
170 ANN(
"format: for * in [nd_iter] do [nd_body] end");
171 ANN(
"example: for i in 1..3 do foo end");
179 ANN(
"for statement");
180 ANN(
"format: break [nd_stts]");
181 ANN(
"example: break 1");
184 ANN(
"next statement");
185 ANN(
"format: next [nd_stts]");
186 ANN(
"example: next 1");
189 ANN(
"return statement");
190 ANN(
"format: return [nd_stts]");
191 ANN(
"example: return 1");
198 ANN(
"redo statement");
200 ANN(
"example: redo");
204 ANN(
"retry statement");
205 ANN(
"format: retry");
206 ANN(
"example: retry");
210 ANN(
"begin statement");
211 ANN(
"format: begin; [nd_body]; end");
212 ANN(
"example: begin; 1; end");
218 ANN(
"rescue clause");
219 ANN(
"format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
220 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
228 ANN(
"rescue clause (cont'd)");
229 ANN(
"format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
230 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
238 ANN(
"ensure clause");
239 ANN(
"format: begin; [nd_head]; ensure; [nd_ensr]; end");
240 ANN(
"example: begin; foo; ensure; bar; end");
248 ANN(
"format: [nd_1st] && [nd_2nd]");
249 ANN(
"example: foo && bar");
253 ANN(
"format: [nd_1st] || [nd_2nd]");
254 ANN(
"example: foo && bar");
262 ANN(
"multiple assignment");
263 ANN(
"format: [nd_head], [nd_args] = [nd_value]");
264 ANN(
"example: a, b = foo");
272 F_MSG(
nd_args,
"splatn",
"-1 (rest argument without name)");
277 ANN(
"local variable assignment");
278 ANN(
"format: [nd_vid](lvar) = [nd_value]");
279 ANN(
"example: x = foo");
282 ANN(
"dynamic variable assignment (out of current scope)");
283 ANN(
"format: [nd_vid](dvar) = [nd_value]");
284 ANN(
"example: x = nil; 1.times { x = foo }");
287 ANN(
"dynamic variable assignment (in current scope)");
288 ANN(
"format: [nd_vid](current dvar) = [nd_value]");
289 ANN(
"example: 1.times { x = foo }");
292 ANN(
"instance variable assignment");
293 ANN(
"format: [nd_vid](ivar) = [nd_value]");
294 ANN(
"example: @x = foo");
297 ANN(
"class variable assignment");
298 ANN(
"format: [nd_vid](cvar) = [nd_value]");
299 ANN(
"example: @@x = foo");
307 ANN(
"global variable assignment");
308 ANN(
"format: [nd_entry](gvar) = [nd_value]");
309 ANN(
"example: $x = foo");
316 ANN(
"constant declaration");
317 ANN(
"format: [nd_else]::[nd_vid](constant) = [nd_value]");
318 ANN(
"example: X = foo");
324 F_MSG(
nd_vid,
"variable",
"0 (see extension field)");
332 ANN(
"array assignment with operator");
333 ANN(
"format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
334 ANN(
"example: ary[1] += foo");
343 ANN(
"attr assignment with operator");
344 ANN(
"format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
345 ANN(
" where [attr] reader: [nd_next->nd_vid]");
346 ANN(
" [attr] writer: [nd_next->nd_aid]");
347 ANN(
"example: struct.field += foo");
352 switch (node->nd_next->nd_mid) {
353 case 0:
A(
"0 (||)");
break;
354 case 1:
A(
"1 (&&)");
break;
355 default:
A_ID(node->nd_next->nd_mid);
363 ANN(
"assignment with && operator");
364 ANN(
"format: [nd_head] &&= [nd_value]");
365 ANN(
"example: foo &&= bar");
368 ANN(
"assignment with || operator");
369 ANN(
"format: [nd_head] ||= [nd_value]");
370 ANN(
"example: foo ||= bar");
378 ANN(
"method invocation");
379 ANN(
"format: [nd_recv].[nd_mid]([nd_args])");
380 ANN(
"example: obj.foo(1)");
388 ANN(
"function call");
389 ANN(
"format: [nd_mid]([nd_args])");
390 ANN(
"example: foo(1)");
397 ANN(
"function call with no argument");
398 ANN(
"format: [nd_mid]");
404 ANN(
"super invocation");
405 ANN(
"format: super [nd_args]");
406 ANN(
"example: super 1");
412 ANN(
"super invocation with no argument");
413 ANN(
"format: super");
414 ANN(
"example: super");
418 ANN(
"array constructor");
419 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
420 ANN(
"example: [1, 2, 3]");
423 ANN(
"return arguments");
424 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
425 ANN(
"example: return 1, 2, 3");
434 ANN(
"empty array constructor");
440 ANN(
"hash constructor");
441 ANN(
"format: { [nd_head] }");
442 ANN(
"example: { 1 => 2, 3 => 4 }");
448 ANN(
"yield invocation");
449 ANN(
"format: yield [nd_head]");
450 ANN(
"example: yield 1");
456 ANN(
"local variable reference");
457 ANN(
"format: [nd_vid](lvar)");
461 ANN(
"dynamic variable reference");
462 ANN(
"format: [nd_vid](dvar)");
463 ANN(
"example: 1.times { x = 1; x }");
466 ANN(
"instance variable reference");
467 ANN(
"format: [nd_vid](ivar)");
471 ANN(
"constant reference");
472 ANN(
"format: [nd_vid](constant)");
476 ANN(
"class variable reference");
477 ANN(
"format: [nd_vid](cvar)");
484 ANN(
"global variable reference");
485 ANN(
"format: [nd_entry](gvar)");
491 ANN(
"nth special variable reference");
492 ANN(
"format: $[nd_nth]");
493 ANN(
"example: $1, $2, ..");
498 ANN(
"back special variable reference");
499 ANN(
"format: $[nd_nth]");
500 ANN(
"example: $&, $`, $', $+");
504 name[1] = (char)node->nd_nth;
511 ANN(
"match expression (against $_ implicitly)");
512 ANN(
"format: [nd_lit] (in condition)");
513 ANN(
"example: if /foo/; foo; end");
518 ANN(
"match expression (regexp first)");
519 ANN(
"format: [nd_recv] =~ [nd_value]");
520 ANN(
"example: /foo/ =~ 'foo'");
527 ANN(
"match expression (regexp second)");
528 ANN(
"format: [nd_recv] =~ [nd_value]");
529 ANN(
"example: 'foo' =~ /foo/");
537 ANN(
"format: [nd_lit]");
538 ANN(
"example: 1, /foo/");
541 ANN(
"string literal");
542 ANN(
"format: [nd_lit]");
543 ANN(
"example: 'foo'");
546 ANN(
"xstring literal");
547 ANN(
"format: [nd_lit]");
548 ANN(
"example: `foo`");
554 ANN(
"string literal with interpolation");
555 ANN(
"format: [nd_lit]");
556 ANN(
"example: \"foo#{ bar }baz\"");
559 ANN(
"xstring literal with interpolation");
560 ANN(
"format: [nd_lit]");
561 ANN(
"example: `foo#{ bar }baz`");
564 ANN(
"regexp literal with interpolation");
565 ANN(
"format: [nd_lit]");
566 ANN(
"example: /foo#{ bar }baz/");
569 ANN(
"regexp literal with interpolation and once flag");
570 ANN(
"format: [nd_lit]");
571 ANN(
"example: /foo#{ bar }baz/o");
574 ANN(
"symbol literal with interpolation");
575 ANN(
"format: [nd_lit]");
576 ANN(
"example: :\"foo#{ bar }baz\"");
585 ANN(
"interpolation expression");
586 ANN(
"format: \"..#{ [nd_lit] }..\"");
587 ANN(
"example: \"foo#{ bar }baz\"");
593 ANN(
"splat argument following arguments");
594 ANN(
"format: ..(*[nd_head], [nd_body..])");
595 ANN(
"example: foo(*ary, post_arg1, post_arg2)");
602 ANN(
"splat argument following one argument");
603 ANN(
"format: ..(*[nd_head], [nd_body])");
604 ANN(
"example: foo(*ary, post_arg)");
611 ANN(
"splat argument");
612 ANN(
"format: *[nd_head]");
613 ANN(
"example: foo(*ary)");
619 ANN(
"arguments with block argument");
620 ANN(
"format: ..([nd_head], &[nd_body])");
621 ANN(
"example: foo(x, &blk)");
628 ANN(
"method definition");
629 ANN(
"format: def [nd_mid] [nd_defn]; end");
630 ANN(
"example; def foo; bar; end");
637 ANN(
"singleton method definition");
638 ANN(
"format: def [nd_recv].[nd_mid] [nd_defn]; end");
639 ANN(
"example; def obj.foo; bar; end");
647 ANN(
"method alias statement");
648 ANN(
"format: alias [u1.node] [u2.node]");
649 ANN(
"example: alias bar foo");
650 F_NODE(u1.node,
"new name");
652 F_NODE(u2.node,
"old name");
656 ANN(
"global variable alias statement");
657 ANN(
"format: alias [u1.id](gvar) [u2.id](gvar)");
658 ANN(
"example: alias $y $x");
659 F_ID(u1.id,
"new name");
660 F_ID(u2.id,
"old name");
664 ANN(
"method alias statement");
665 ANN(
"format: undef [u2.node]");
666 ANN(
"example: undef foo");
668 F_NODE(u2.node,
"old name");
672 ANN(
"class definition");
673 ANN(
"format: class [nd_cpath] < [nd_super]; [nd_body]; end");
674 ANN(
"example: class C2 < C; ..; end");
682 ANN(
"module definition");
683 ANN(
"format: module [nd_cpath]; [nd_body]; end");
684 ANN(
"example: module M; ..; end");
691 ANN(
"singleton class definition");
692 ANN(
"format: class << [nd_recv]; [nd_body]; end");
693 ANN(
"example: class << obj; ..; end");
700 ANN(
"scoped constant reference");
701 ANN(
"format: [nd_head]::[nd_mid]");
702 ANN(
"example: M::C");
709 ANN(
"top-level constant reference");
710 ANN(
"format: ::[nd_mid]");
711 ANN(
"example: ::Object");
716 ANN(
"range constructor (incl.)");
717 ANN(
"format: [nd_beg]..[nd_end]");
718 ANN(
"example: 1..5");
721 ANN(
"range constructor (excl.)");
722 ANN(
"format: [nd_beg]...[nd_end]");
723 ANN(
"example: 1...5");
726 ANN(
"flip-flop condition (incl.)");
727 ANN(
"format: [nd_beg]..[nd_end]");
728 ANN(
"example: if (x==1)..(x==5); foo; end");
731 ANN(
"flip-flop condition (excl.)");
732 ANN(
"format: [nd_beg]...[nd_end]");
733 ANN(
"example: if (x==1)...(x==5); foo; end");
743 ANN(
"example: self");
755 ANN(
"example: true");
760 ANN(
"format: false");
761 ANN(
"example: false");
765 ANN(
"virtual reference to $!");
766 ANN(
"format: rescue => id");
767 ANN(
"example: rescue => id");
771 ANN(
"defined? expression");
772 ANN(
"format: defined?([nd_head])");
773 ANN(
"example: defined?(foo)");
778 ANN(
"post-execution");
779 ANN(
"format: END { [nd_body] }");
780 ANN(
"example: END { foo }");
786 ANN(
"attr assignment");
787 ANN(
"format: [nd_recv].[nd_mid] = [nd_args]");
788 ANN(
"example: struct.field = foo");
789 if (node->nd_recv == (
NODE *) 1) {
801 ANN(
"pre-execution");
802 ANN(
"format: BEGIN { [nd_head] }; [nd_body]");
803 ANN(
"example: bar; BEGIN { foo }");
810 ANN(
"lambda expression");
811 ANN(
"format: -> [nd_body]");
812 ANN(
"example: -> { foo }");
818 ANN(
"optional arguments");
819 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
820 ANN(
"example: def foo(a, b=1, c); end");
827 ANN(
"post arguments");
828 ANN(
"format: *[nd_1st], [nd_2nd..] = ..");
829 ANN(
"example: a, *rest, z = foo");
834 F_MSG(
nd_1st,
"rest argument",
"-1 (rest argument without name)");
841 ANN(
"method parameters (cont'd)");
843 if (node->nd_rest == 1)
A(
"nil (with last comma)");
844 else A_ID(node->nd_rest);
849 node = node->nd_next;
856 ANN(
"method parameters (cont'd)");
861 node = node->nd_next;
868 ANN(
"method parameters (cont'd)");
879 ANN(
"method parameters");
880 ANN(
"format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
881 ANN(
"example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
890 ANN(
"format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
892 ID *
tbl = node->nd_tbl;
894 int size = tbl ? (
int)*tbl++ : 0;
895 if (size == 0)
A(
"(empty)");
896 for (i = 0; i <
size; i++) {
897 A_ID(tbl[i]);
if (i < size - 1)
A(
",");
914 "###########################################################\n"
915 "## Do NOT use this node dump for any purpose other than ##\n"
916 "## debug and research. Compatibility is not guaranteed. ##\n"
917 "###########################################################\n\n"
void rb_bug(const char *fmt,...)
#define F_NODE(name, ann)
#define D_NODE_HEADER(node)
static void add_id(VALUE buf, ID id)
#define F_GENTRY(name, ann)
static void add_indent(VALUE buf, VALUE indent)
static void dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
#define F_LONG(name, ann)
unsigned char buf[MIME_BUF_SIZE]
#define F_CUSTOM1(name, ann, block)
#define F_MSG(name, ann, desc)
VALUE rb_str_new_cstr(const char *)
VALUE rb_parser_dump_tree(NODE *node, int comment)
const char * ruby_node_name(int node)
#define F_CUSTOM2(name, ann, block)