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");
303 if (node->nd_value == (
NODE *)-1) {
312 ANN(
"global variable assignment");
313 ANN(
"format: [nd_entry](gvar) = [nd_value]");
314 ANN(
"example: $x = foo");
321 ANN(
"constant declaration");
322 ANN(
"format: [nd_else]::[nd_vid](constant) = [nd_value]");
323 ANN(
"example: X = foo");
329 F_MSG(
nd_vid,
"variable",
"0 (see extension field)");
337 ANN(
"array assignment with operator");
338 ANN(
"format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
339 ANN(
"example: ary[1] += foo");
348 ANN(
"attr assignment with operator");
349 ANN(
"format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
350 ANN(
" where [attr] reader: [nd_next->nd_vid]");
351 ANN(
" [attr] writer: [nd_next->nd_aid]");
352 ANN(
"example: struct.field += foo");
357 switch (node->nd_next->nd_mid) {
358 case 0: A(
"0 (||)"); break;
359 case 1: A(
"1 (&&)"); break;
360 default: A_ID(node->nd_next->nd_mid);
368 ANN(
"assignment with && operator");
369 ANN(
"format: [nd_head] &&= [nd_value]");
370 ANN(
"example: foo &&= bar");
373 ANN(
"assignment with || operator");
374 ANN(
"format: [nd_head] ||= [nd_value]");
375 ANN(
"example: foo ||= bar");
383 ANN(
"method invocation");
384 ANN(
"format: [nd_recv].[nd_mid]([nd_args])");
385 ANN(
"example: obj.foo(1)");
393 ANN(
"function call");
394 ANN(
"format: [nd_mid]([nd_args])");
395 ANN(
"example: foo(1)");
402 ANN(
"function call with no argument");
403 ANN(
"format: [nd_mid]");
409 ANN(
"super invocation");
410 ANN(
"format: super [nd_args]");
411 ANN(
"example: super 1");
417 ANN(
"super invocation with no argument");
418 ANN(
"format: super");
419 ANN(
"example: super");
423 ANN(
"array constructor");
424 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
425 ANN(
"example: [1, 2, 3]");
428 ANN(
"return arguments");
429 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
430 ANN(
"example: return 1, 2, 3");
439 ANN(
"empty array constructor");
445 ANN(
"hash constructor");
446 ANN(
"format: { [nd_head] }");
447 ANN(
"example: { 1 => 2, 3 => 4 }");
453 ANN(
"yield invocation");
454 ANN(
"format: yield [nd_head]");
455 ANN(
"example: yield 1");
461 ANN(
"local variable reference");
462 ANN(
"format: [nd_vid](lvar)");
466 ANN(
"dynamic variable reference");
467 ANN(
"format: [nd_vid](dvar)");
468 ANN(
"example: 1.times { x = 1; x }");
471 ANN(
"instance variable reference");
472 ANN(
"format: [nd_vid](ivar)");
476 ANN(
"constant reference");
477 ANN(
"format: [nd_vid](constant)");
481 ANN(
"class variable reference");
482 ANN(
"format: [nd_vid](cvar)");
489 ANN(
"global variable reference");
490 ANN(
"format: [nd_entry](gvar)");
496 ANN(
"nth special variable reference");
497 ANN(
"format: $[nd_nth]");
498 ANN(
"example: $1, $2, ..");
503 ANN(
"back special variable reference");
504 ANN(
"format: $[nd_nth]");
505 ANN(
"example: $&, $`, $', $+");
509 name[1] = (char)node->nd_nth;
516 ANN(
"match expression (against $_ implicitly)");
517 ANN(
"format: [nd_lit] (in condition)");
518 ANN(
"example: if /foo/; foo; end");
523 ANN(
"match expression (regexp first)");
524 ANN(
"format: [nd_recv] =~ [nd_value]");
525 ANN(
"example: /foo/ =~ 'foo'");
532 ANN(
"match expression (regexp second)");
533 ANN(
"format: [nd_recv] =~ [nd_value]");
534 ANN(
"example: 'foo' =~ /foo/");
542 ANN(
"format: [nd_lit]");
543 ANN(
"example: 1, /foo/");
546 ANN(
"string literal");
547 ANN(
"format: [nd_lit]");
548 ANN(
"example: 'foo'");
551 ANN(
"xstring literal");
552 ANN(
"format: [nd_lit]");
553 ANN(
"example: `foo`");
559 ANN(
"string literal with interpolation");
560 ANN(
"format: [nd_lit]");
561 ANN(
"example: \"foo#{ bar }baz\"");
564 ANN(
"xstring literal with interpolation");
565 ANN(
"format: [nd_lit]");
566 ANN(
"example: `foo#{ bar }baz`");
569 ANN(
"regexp literal with interpolation");
570 ANN(
"format: [nd_lit]");
571 ANN(
"example: /foo#{ bar }baz/");
574 ANN(
"regexp literal with interpolation and once flag");
575 ANN(
"format: [nd_lit]");
576 ANN(
"example: /foo#{ bar }baz/o");
579 ANN(
"symbol literal with interpolation");
580 ANN(
"format: [nd_lit]");
581 ANN(
"example: :\"foo#{ bar }baz\"");
590 ANN(
"interpolation expression");
591 ANN(
"format: \"..#{ [nd_lit] }..\"");
592 ANN(
"example: \"foo#{ bar }baz\"");
598 ANN(
"splat argument following arguments");
599 ANN(
"format: ..(*[nd_head], [nd_body..])");
600 ANN(
"example: foo(*ary, post_arg1, post_arg2)");
607 ANN(
"splat argument following one argument");
608 ANN(
"format: ..(*[nd_head], [nd_body])");
609 ANN(
"example: foo(*ary, post_arg)");
616 ANN(
"splat argument");
617 ANN(
"format: *[nd_head]");
618 ANN(
"example: foo(*ary)");
624 ANN(
"arguments with block argument");
625 ANN(
"format: ..([nd_head], &[nd_body])");
626 ANN(
"example: foo(x, &blk)");
633 ANN(
"method definition");
634 ANN(
"format: def [nd_mid] [nd_defn]; end");
635 ANN(
"example; def foo; bar; end");
642 ANN(
"singleton method definition");
643 ANN(
"format: def [nd_recv].[nd_mid] [nd_defn]; end");
644 ANN(
"example; def obj.foo; bar; end");
652 ANN(
"method alias statement");
653 ANN(
"format: alias [u1.node] [u2.node]");
654 ANN(
"example: alias bar foo");
655 F_NODE(u1.node,
"new name");
657 F_NODE(u2.node,
"old name");
661 ANN(
"global variable alias statement");
662 ANN(
"format: alias [u1.id](gvar) [u2.id](gvar)");
663 ANN(
"example: alias $y $x");
664 F_ID(u1.id,
"new name");
665 F_ID(u2.id,
"old name");
669 ANN(
"method alias statement");
670 ANN(
"format: undef [u2.node]");
671 ANN(
"example: undef foo");
673 F_NODE(u2.node,
"old name");
677 ANN(
"class definition");
678 ANN(
"format: class [nd_cpath] < [nd_super]; [nd_body]; end");
679 ANN(
"example: class C2 < C; ..; end");
687 ANN(
"module definition");
688 ANN(
"format: module [nd_cpath]; [nd_body]; end");
689 ANN(
"example: module M; ..; end");
696 ANN(
"singleton class definition");
697 ANN(
"format: class << [nd_recv]; [nd_body]; end");
698 ANN(
"example: class << obj; ..; end");
705 ANN(
"scoped constant reference");
706 ANN(
"format: [nd_head]::[nd_mid]");
707 ANN(
"example: M::C");
714 ANN(
"top-level constant reference");
715 ANN(
"format: ::[nd_mid]");
716 ANN(
"example: ::Object");
721 ANN(
"range constructor (incl.)");
722 ANN(
"format: [nd_beg]..[nd_end]");
723 ANN(
"example: 1..5");
726 ANN(
"range constructor (excl.)");
727 ANN(
"format: [nd_beg]...[nd_end]");
728 ANN(
"example: 1...5");
731 ANN(
"flip-flop condition (incl.)");
732 ANN(
"format: [nd_beg]..[nd_end]");
733 ANN(
"example: if (x==1)..(x==5); foo; end");
736 ANN(
"flip-flop condition (excl.)");
737 ANN(
"format: [nd_beg]...[nd_end]");
738 ANN(
"example: if (x==1)...(x==5); foo; end");
748 ANN(
"example: self");
760 ANN(
"example: true");
765 ANN(
"format: false");
766 ANN(
"example: false");
770 ANN(
"virtual reference to $!");
771 ANN(
"format: rescue => id");
772 ANN(
"example: rescue => id");
776 ANN(
"defined? expression");
777 ANN(
"format: defined?([nd_head])");
778 ANN(
"example: defined?(foo)");
783 ANN(
"post-execution");
784 ANN(
"format: END { [nd_body] }");
785 ANN(
"example: END { foo }");
791 ANN(
"attr assignment");
792 ANN(
"format: [nd_recv].[nd_mid] = [nd_args]");
793 ANN(
"example: struct.field = foo");
794 if (node->nd_recv == (
NODE *) 1) {
806 ANN(
"pre-execution");
807 ANN(
"format: BEGIN { [nd_head] }; [nd_body]");
808 ANN(
"example: bar; BEGIN { foo }");
815 ANN(
"lambda expression");
816 ANN(
"format: -> [nd_body]");
817 ANN(
"example: -> { foo }");
823 ANN(
"optional arguments");
824 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
825 ANN(
"example: def foo(a, b=1, c); end");
832 ANN(
"keyword arguments");
833 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
834 ANN(
"example: def foo(a:1, b:2); end");
841 ANN(
"post arguments");
842 ANN(
"format: *[nd_1st], [nd_2nd..] = ..");
843 ANN(
"example: a, *rest, z = foo");
848 F_MSG(
nd_1st,
"rest argument",
"-1 (rest argument without name)");
855 ANN(
"method parameters");
856 ANN(
"format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
857 ANN(
"example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
858 F_INT(
nd_ainfo->pre_args_num,
"count of mandatory (pre-)arguments");
860 F_INT(
nd_ainfo->post_args_num,
"count of mandatory post-arguments");
873 ANN(
"format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
875 ID *tbl = node->nd_tbl;
877 int size = tbl ? (int)*tbl++ : 0;
878 if (
size == 0)
A(
"(empty)");
879 for (i = 0; i <
size; i++) {
897 "###########################################################\n" 898 "## Do NOT use this node dump for any purpose other than ##\n" 899 "## debug and research. Compatibility is not guaranteed. ##\n" 900 "###########################################################\n\n"
VALUE rb_parser_dump_tree(NODE *node, int comment)
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 *)
const char * ruby_node_name(int node)