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, column: %d)"term, ruby_node_name(nd_type(node)), nd_line(node), nd_column(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)");
138 static const char default_indent[] =
"| ";
144 const char *next_indent = default_indent;
148 node = node->nd_next;
160 const char *next_indent = default_indent;
173 ANN(
"statement sequence");
174 ANN(
"format: [nd_head]; ...; [nd_next]");
175 ANN(
"example: foo; bar");
180 comment ?
"statement #" :
"", ++i);
183 dump_node(
buf,
indent, comment, node->nd_head);
185 }
while (node->nd_next &&
187 (node = node->nd_next, 1));
196 ANN(
"format: if [nd_cond] then [nd_body] else [nd_else] end");
197 ANN(
"example: if x == 1 then foo else bar end");
205 ANN(
"unless statement");
206 ANN(
"format: unless [nd_cond] then [nd_body] else [nd_else] end");
207 ANN(
"example: unless x == 1 then foo else bar end");
215 ANN(
"case statement");
216 ANN(
"format: case [nd_head]; [nd_body]; end");
217 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
225 ANN(
"format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
226 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
234 ANN(
"wrapper for -n option");
235 ANN(
"format: ruby -ne '[nd_body]' (nd_cond is `gets')");
236 ANN(
"example: ruby -ne 'p $_'");
239 ANN(
"while statement");
240 ANN(
"format: while [nd_cond]; [nd_body]; end");
241 ANN(
"example: while x == 1; foo; end");
244 ANN(
"until statement");
245 ANN(
"format: until [nd_cond]; [nd_body]; end");
246 ANN(
"example: until x == 1; foo; end");
249 A_INT((
int)node->nd_state);
250 A((node->nd_state == 1) ?
" (while-end)" :
" (begin-end-while)");
258 ANN(
"method call with block");
259 ANN(
"format: [nd_iter] { [nd_body] }");
260 ANN(
"example: 3.times { foo }");
263 ANN(
"for statement");
264 ANN(
"format: for * in [nd_iter] do [nd_body] end");
265 ANN(
"example: for i in 1..3 do foo end");
273 ANN(
"for statement");
274 ANN(
"format: break [nd_stts]");
275 ANN(
"example: break 1");
278 ANN(
"next statement");
279 ANN(
"format: next [nd_stts]");
280 ANN(
"example: next 1");
283 ANN(
"return statement");
284 ANN(
"format: return [nd_stts]");
285 ANN(
"example: return 1");
292 ANN(
"redo statement");
294 ANN(
"example: redo");
298 ANN(
"retry statement");
299 ANN(
"format: retry");
300 ANN(
"example: retry");
304 ANN(
"begin statement");
305 ANN(
"format: begin; [nd_body]; end");
306 ANN(
"example: begin; 1; end");
312 ANN(
"rescue clause");
313 ANN(
"format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
314 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
322 ANN(
"rescue clause (cont'd)");
323 ANN(
"format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
324 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
332 ANN(
"ensure clause");
333 ANN(
"format: begin; [nd_head]; ensure; [nd_ensr]; end");
334 ANN(
"example: begin; foo; ensure; bar; end");
342 ANN(
"format: [nd_1st] && [nd_2nd]");
343 ANN(
"example: foo && bar");
347 ANN(
"format: [nd_1st] || [nd_2nd]");
348 ANN(
"example: foo || bar");
352 if (!node->nd_2nd ||
nd_type(node->nd_2nd) != (
int)type)
361 ANN(
"multiple assignment");
362 ANN(
"format: [nd_head], [nd_args] = [nd_value]");
363 ANN(
"example: a, b = foo");
371 F_MSG(
nd_args,
"splatn",
"-1 (rest argument without name)");
376 ANN(
"local variable assignment");
377 ANN(
"format: [nd_vid](lvar) = [nd_value]");
378 ANN(
"example: x = foo");
381 ANN(
"dynamic variable assignment (out of current scope)");
382 ANN(
"format: [nd_vid](dvar) = [nd_value]");
383 ANN(
"example: x = nil; 1.times { x = foo }");
386 ANN(
"dynamic variable assignment (in current scope)");
387 ANN(
"format: [nd_vid](current dvar) = [nd_value]");
388 ANN(
"example: 1.times { x = foo }");
391 ANN(
"instance variable assignment");
392 ANN(
"format: [nd_vid](ivar) = [nd_value]");
393 ANN(
"example: @x = foo");
396 ANN(
"class variable assignment");
397 ANN(
"format: [nd_vid](cvar) = [nd_value]");
398 ANN(
"example: @@x = foo");
402 if (node->nd_value == (
NODE *)-1) {
411 ANN(
"global variable assignment");
412 ANN(
"format: [nd_entry](gvar) = [nd_value]");
413 ANN(
"example: $x = foo");
420 ANN(
"constant declaration");
421 ANN(
"format: [nd_else]::[nd_vid](constant) = [nd_value]");
422 ANN(
"example: X = foo");
428 F_MSG(
nd_vid,
"constant",
"0 (see extension field)");
436 ANN(
"array assignment with operator");
437 ANN(
"format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
438 ANN(
"example: ary[1] += foo");
441 switch (node->nd_mid) {
442 case 0:
A(
"0 (||)");
break;
443 case 1:
A(
"1 (&&)");
break;
444 default:
A_ID(node->nd_mid);
453 ANN(
"attr assignment with operator");
454 ANN(
"format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
455 ANN(
" where [attr]: [nd_next->nd_vid]");
456 ANN(
"example: struct.field += foo");
459 if (node->nd_next->nd_aid)
A(
"? ");
460 A_ID(node->nd_next->nd_vid);
463 switch (node->nd_next->nd_mid) {
464 case 0:
A(
"0 (||)");
break;
465 case 1:
A(
"1 (&&)");
break;
466 default:
A_ID(node->nd_next->nd_mid);
474 ANN(
"assignment with && operator");
475 ANN(
"format: [nd_head] &&= [nd_value]");
476 ANN(
"example: foo &&= bar");
479 ANN(
"assignment with || operator");
480 ANN(
"format: [nd_head] ||= [nd_value]");
481 ANN(
"example: foo ||= bar");
489 ANN(
"constant declaration with operator");
490 ANN(
"format: [nd_head](constant) [nd_aid]= [nd_value]");
491 ANN(
"example: A::B ||= 1");
494 switch (node->nd_aid) {
495 case 0:
A(
"0 (||)");
break;
496 case 1:
A(
"1 (&&)");
break;
497 default:
A_ID(node->nd_mid);
506 ANN(
"method invocation");
507 ANN(
"format: [nd_recv].[nd_mid]([nd_args])");
508 ANN(
"example: obj.foo(1)");
516 ANN(
"function call");
517 ANN(
"format: [nd_mid]([nd_args])");
518 ANN(
"example: foo(1)");
525 ANN(
"function call with no argument");
526 ANN(
"format: [nd_mid]");
532 ANN(
"safe method invocation");
533 ANN(
"format: [nd_recv]&.[nd_mid]([nd_args])");
534 ANN(
"example: obj&.foo(1)");
542 ANN(
"super invocation");
543 ANN(
"format: super [nd_args]");
544 ANN(
"example: super 1");
550 ANN(
"super invocation with no argument");
551 ANN(
"format: super");
552 ANN(
"example: super");
556 ANN(
"array constructor");
557 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
558 ANN(
"example: [1, 2, 3]");
561 ANN(
"return arguments");
562 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
563 ANN(
"example: return 1, 2, 3");
569 ANN(
"empty array constructor");
575 if (!node->nd_alen) {
576 ANN(
"keyword arguments");
577 ANN(
"format: nd_head");
578 ANN(
"example: a: 1, b: 2");
581 ANN(
"hash constructor");
582 ANN(
"format: { [nd_head] }");
583 ANN(
"example: { 1 => 2, 3 => 4 }");
590 ANN(
"yield invocation");
591 ANN(
"format: yield [nd_head]");
592 ANN(
"example: yield 1");
598 ANN(
"local variable reference");
599 ANN(
"format: [nd_vid](lvar)");
604 ANN(
"dynamic variable reference");
605 ANN(
"format: [nd_vid](dvar)");
606 ANN(
"example: 1.times { x = 1; x }");
610 ANN(
"instance variable reference");
611 ANN(
"format: [nd_vid](ivar)");
616 ANN(
"constant reference");
617 ANN(
"format: [nd_vid](constant)");
622 ANN(
"class variable reference");
623 ANN(
"format: [nd_vid](cvar)");
629 ANN(
"global variable reference");
630 ANN(
"format: [nd_entry](gvar)");
636 ANN(
"nth special variable reference");
637 ANN(
"format: $[nd_nth]");
638 ANN(
"example: $1, $2, ..");
643 ANN(
"back special variable reference");
644 ANN(
"format: $[nd_nth]");
645 ANN(
"example: $&, $`, $', $+");
649 name[1] = (char)node->nd_nth;
656 ANN(
"match expression (against $_ implicitly)");
657 ANN(
"format: [nd_lit] (in condition)");
658 ANN(
"example: if /foo/; foo; end");
663 ANN(
"match expression (regexp first)");
664 ANN(
"format: [nd_recv] =~ [nd_value]");
665 ANN(
"example: /foo/ =~ 'foo'");
676 ANN(
"match expression (regexp second)");
677 ANN(
"format: [nd_recv] =~ [nd_value]");
678 ANN(
"example: 'foo' =~ /foo/");
686 ANN(
"format: [nd_lit]");
687 ANN(
"example: 1, /foo/");
690 ANN(
"string literal");
691 ANN(
"format: [nd_lit]");
692 ANN(
"example: 'foo'");
695 ANN(
"xstring literal");
696 ANN(
"format: [nd_lit]");
697 ANN(
"example: `foo`");
703 ANN(
"string literal with interpolation");
704 ANN(
"format: [nd_lit]");
705 ANN(
"example: \"foo#{ bar }baz\"");
708 ANN(
"xstring literal with interpolation");
709 ANN(
"format: [nd_lit]");
710 ANN(
"example: `foo#{ bar }baz`");
713 ANN(
"regexp literal with interpolation");
714 ANN(
"format: [nd_lit]");
715 ANN(
"example: /foo#{ bar }baz/");
718 ANN(
"regexp literal with interpolation and once flag");
719 ANN(
"format: [nd_lit]");
720 ANN(
"example: /foo#{ bar }baz/o");
723 ANN(
"symbol literal with interpolation");
724 ANN(
"format: [nd_lit]");
725 ANN(
"example: :\"foo#{ bar }baz\"");
734 ANN(
"interpolation expression");
735 ANN(
"format: \"..#{ [nd_lit] }..\"");
736 ANN(
"example: \"foo#{ bar }baz\"");
742 ANN(
"splat argument following arguments");
743 ANN(
"format: ..(*[nd_head], [nd_body..])");
744 ANN(
"example: foo(*ary, post_arg1, post_arg2)");
751 ANN(
"splat argument following one argument");
752 ANN(
"format: ..(*[nd_head], [nd_body])");
753 ANN(
"example: foo(*ary, post_arg)");
760 ANN(
"splat argument");
761 ANN(
"format: *[nd_head]");
762 ANN(
"example: foo(*ary)");
768 ANN(
"arguments with block argument");
769 ANN(
"format: ..([nd_head], &[nd_body])");
770 ANN(
"example: foo(x, &blk)");
777 ANN(
"method definition");
778 ANN(
"format: def [nd_mid] [nd_defn]; end");
779 ANN(
"example; def foo; bar; end");
786 ANN(
"singleton method definition");
787 ANN(
"format: def [nd_recv].[nd_mid] [nd_defn]; end");
788 ANN(
"example; def obj.foo; bar; end");
796 ANN(
"method alias statement");
797 ANN(
"format: alias [u1.node] [u2.node]");
798 ANN(
"example: alias bar foo");
799 F_NODE(u1.node,
"new name");
801 F_NODE(u2.node,
"old name");
805 ANN(
"global variable alias statement");
806 ANN(
"format: alias [u1.id](gvar) [u2.id](gvar)");
807 ANN(
"example: alias $y $x");
808 F_ID(u1.id,
"new name");
809 F_ID(u2.id,
"old name");
813 ANN(
"method alias statement");
814 ANN(
"format: undef [u2.node]");
815 ANN(
"example: undef foo");
817 F_NODE(u2.node,
"old name");
821 ANN(
"class definition");
822 ANN(
"format: class [nd_cpath] < [nd_super]; [nd_body]; end");
823 ANN(
"example: class C2 < C; ..; end");
831 ANN(
"module definition");
832 ANN(
"format: module [nd_cpath]; [nd_body]; end");
833 ANN(
"example: module M; ..; end");
840 ANN(
"singleton class definition");
841 ANN(
"format: class << [nd_recv]; [nd_body]; end");
842 ANN(
"example: class << obj; ..; end");
849 ANN(
"scoped constant reference");
850 ANN(
"format: [nd_head]::[nd_mid]");
851 ANN(
"example: M::C");
858 ANN(
"top-level constant reference");
859 ANN(
"format: ::[nd_mid]");
860 ANN(
"example: ::Object");
865 ANN(
"range constructor (incl.)");
866 ANN(
"format: [nd_beg]..[nd_end]");
867 ANN(
"example: 1..5");
870 ANN(
"range constructor (excl.)");
871 ANN(
"format: [nd_beg]...[nd_end]");
872 ANN(
"example: 1...5");
875 ANN(
"flip-flop condition (incl.)");
876 ANN(
"format: [nd_beg]..[nd_end]");
877 ANN(
"example: if (x==1)..(x==5); foo; end");
880 ANN(
"flip-flop condition (excl.)");
881 ANN(
"format: [nd_beg]...[nd_end]");
882 ANN(
"example: if (x==1)...(x==5); foo; end");
892 ANN(
"example: self");
904 ANN(
"example: true");
909 ANN(
"format: false");
910 ANN(
"example: false");
914 ANN(
"virtual reference to $!");
915 ANN(
"format: rescue => id");
916 ANN(
"example: rescue => id");
920 ANN(
"defined? expression");
921 ANN(
"format: defined?([nd_head])");
922 ANN(
"example: defined?(foo)");
927 ANN(
"post-execution");
928 ANN(
"format: END { [nd_body] }");
929 ANN(
"example: END { foo }");
935 ANN(
"attr assignment");
936 ANN(
"format: [nd_recv].[nd_mid] = [nd_args]");
937 ANN(
"example: struct.field = foo");
938 if (node->nd_recv == (
NODE *) 1) {
950 ANN(
"pre-execution");
951 ANN(
"format: BEGIN { [nd_head] }; [nd_body]");
952 ANN(
"example: bar; BEGIN { foo }");
953 #define nd_compile_option u3.value 957 if (node->nd_compile_option) {
964 ANN(
"lambda expression");
965 ANN(
"format: -> [nd_body]");
966 ANN(
"example: -> { foo }");
972 ANN(
"optional arguments");
973 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
974 ANN(
"example: def foo(a, b=1, c); end");
981 ANN(
"keyword arguments");
982 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
983 ANN(
"example: def foo(a:1, b:2); end");
990 ANN(
"post arguments");
991 ANN(
"format: *[nd_1st], [nd_2nd..] = ..");
992 ANN(
"example: a, *rest, z = foo");
997 F_MSG(
nd_1st,
"rest argument",
"-1 (rest argument without name)");
1004 ANN(
"method parameters");
1005 ANN(
"format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
1006 ANN(
"example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
1007 F_INT(
nd_ainfo->pre_args_num,
"count of mandatory (pre-)arguments");
1009 F_INT(
nd_ainfo->post_args_num,
"count of mandatory post-arguments");
1022 ANN(
"format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
1024 ID *tbl = node->nd_tbl;
1026 int size = tbl ? (int)*tbl++ : 0;
1027 if (size == 0)
A(
"(empty)");
1028 for (i = 0; i <
size; i++) {
1029 A_ID(tbl[i]);
if (i < size - 1)
A(
",");
1052 "###########################################################\n" 1053 "## Do NOT use this node dump for any purpose other than ##\n" 1054 "## debug and research. Compatibility is not guaranteed. ##\n" 1055 "###########################################################\n\n" 1085 size += (
RNODE(obj)->nd_tbl[0]+1) *
sizeof(*
RNODE(obj)->nd_tbl);
1089 if (
RNODE(obj)->nd_ainfo) {
1090 size +=
sizeof(*
RNODE(obj)->nd_ainfo);
1128 return RNODE(obj)->u3.value;
1161 return RNODE(obj)->u2.value;
1176 return RNODE(obj)->u1.value;
1182 return RNODE(obj)->u2.value;
1195 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)
#define D_NODE_HEADER(node)
void rb_gc_free_node(VALUE obj)
#define F_CUSTOM1(name, ann)
void rb_gc_mark(VALUE ptr)
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
#define RB_TYPE_P(obj, type)
#define F_GENTRY(name, ann)
#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 *)
size_t rb_node_memsize(VALUE obj)
#define nd_compile_option
#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