1 /**********************************************************************
3 node_dump.c - dump ruby node tree
6 created at: 09/12/06 21:23:44 JST
8 Copyright (C) 2009 Yusuke Endoh
10 **********************************************************************/
13 #include "internal/hash.h"
14 #include "internal/variable.h"
15 #include "ruby/ruby.h"
18 #define A(str) rb_str_cat2(buf, (str))
19 #define AR(str) rb_str_concat(buf, (str))
21 #define A_INDENT add_indent(buf, indent)
22 #define D_INDENT rb_str_cat2(indent, next_indent)
23 #define D_DEDENT rb_str_resize(indent, RSTRING_LEN(indent) - 4)
24 #define A_ID(id) add_id(buf, (id))
25 #define A_INT(val) rb_str_catf(buf, "%d", (val))
26 #define A_LONG(val) rb_str_catf(buf, "%ld", (val))
27 #define A_LIT(lit) AR(rb_dump_literal(lit))
28 #define A_NODE_HEADER(node, term) \
29 rb_str_catf(buf, "@ %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))%s"term, \
30 ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node), \
31 nd_first_lineno(node), nd_first_column(node), \
32 nd_last_lineno(node), nd_last_column(node), \
33 (node->flags & NODE_FL_NEWLINE ? "*" : ""))
34 #define A_FIELD_HEADER(len, name, term) \
35 rb_str_catf(buf, "+- %.*s:"term, (len), (name))
36 #define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term))
38 #define D_NULL_NODE (A_INDENT, A("(null node)\n"))
39 #define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n"))
41 #define COMPOUND_FIELD(len, name) \
42 FIELD_BLOCK((D_FIELD_HEADER((len), (name), "\n"), D_INDENT), D_DEDENT)
44 #define COMPOUND_FIELD1(name, ann) \
45 COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \
46 FIELD_NAME_DESC(name, ann))
48 #define FIELD_NAME_DESC(name, ann) name " (" ann ")"
49 #define FIELD_NAME_LEN(name, ann) (int)( \
51 rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
53 #define SIMPLE_FIELD(len, name) \
54 FIELD_BLOCK(D_FIELD_HEADER((len), (name), " "), A("\n"))
56 #define FIELD_BLOCK(init, reset) \
57 for (init, field_flag = 1; \
58 field_flag; /* should be optimized away */ \
59 reset, field_flag = 0)
61 #define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
62 #define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
63 #define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
64 #define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
65 #define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
66 #define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)
67 #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
69 #define F_NODE(name, ann) \
70 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, node->name);}
74 A_INDENT; A("| # " ann "\n"); \
77 #define LAST_NODE (next_indent = " ")
80 rb_dump_literal(VALUE lit
)
82 if (!RB_SPECIAL_CONST_P(lit
)) {
84 switch (RB_BUILTIN_TYPE(lit
)) {
85 case T_CLASS
: case T_MODULE
: case T_ICLASS
:
86 str
= rb_class_path(lit
);
87 if (FL_TEST(lit
, FL_SINGLETON
)) {
88 str
= rb_sprintf("<%"PRIsVALUE
">", str
);
95 return rb_inspect(lit
);
99 add_indent(VALUE buf
, VALUE indent
)
105 add_id(VALUE buf
, ID id
)
111 VALUE str
= rb_id2str(id
);
116 rb_str_catf(buf
, "(internal variable: 0x%"PRIsVALUE
")", id
);
121 struct add_option_arg
{
126 static void dump_node(VALUE
, VALUE
, int, const NODE
*);
127 static const char default_indent
[] = "| ";
130 dump_array(VALUE buf
, VALUE indent
, int comment
, const NODE
*node
)
133 const char *next_indent
= default_indent
;
134 F_LONG(nd_alen
, "length");
135 F_NODE(nd_head
, "element");
136 while (node
->nd_next
&& nd_type_p(node
->nd_next
, NODE_LIST
)) {
137 node
= node
->nd_next
;
138 F_NODE(nd_head
, "element");
141 F_NODE(nd_next
, "next element");
145 dump_node(VALUE buf
, VALUE indent
, int comment
, const NODE
* node
)
149 const char *next_indent
= default_indent
;
159 type
= nd_type(node
);
162 ANN("statement sequence");
163 ANN("format: [nd_head]; ...; [nd_next]");
164 ANN("example: foo; bar");
168 rb_str_catf(buf
, "+- nd_head (%s%d):\n",
169 comment
? "statement #" : "", ++i
);
170 if (!node
->nd_next
) LAST_NODE
;
172 dump_node(buf
, indent
, comment
, node
->nd_head
);
174 } while (node
->nd_next
&&
175 nd_type_p(node
->nd_next
, NODE_BLOCK
) &&
176 (node
= node
->nd_next
, 1));
179 F_NODE(nd_next
, "next block");
185 ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
186 ANN("example: if x == 1 then foo else bar end");
187 F_NODE(nd_cond
, "condition expr");
188 F_NODE(nd_body
, "then clause");
190 F_NODE(nd_else
, "else clause");
194 ANN("unless statement");
195 ANN("format: unless [nd_cond] then [nd_body] else [nd_else] end");
196 ANN("example: unless x == 1 then foo else bar end");
197 F_NODE(nd_cond
, "condition expr");
198 F_NODE(nd_body
, "then clause");
200 F_NODE(nd_else
, "else clause");
204 ANN("case statement");
205 ANN("format: case [nd_head]; [nd_body]; end");
206 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
207 F_NODE(nd_head
, "case expr");
209 F_NODE(nd_body
, "when clauses");
212 ANN("case statement with no head");
213 ANN("format: case; [nd_body]; end");
214 ANN("example: case; when 1; foo; when 2; bar; else baz; end");
215 F_NODE(nd_head
, "case expr");
217 F_NODE(nd_body
, "when clauses");
220 ANN("case statement (pattern matching)");
221 ANN("format: case [nd_head]; [nd_body]; end");
222 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
223 F_NODE(nd_head
, "case expr");
225 F_NODE(nd_body
, "in clauses");
230 ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
231 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
232 F_NODE(nd_head
, "when value");
233 F_NODE(nd_body
, "when body");
235 F_NODE(nd_next
, "next when clause");
240 ANN("format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
241 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
242 F_NODE(nd_head
, "in pattern");
243 F_NODE(nd_body
, "in body");
245 F_NODE(nd_next
, "next in clause");
249 ANN("while statement");
250 ANN("format: while [nd_cond]; [nd_body]; end");
251 ANN("example: while x == 1; foo; end");
254 ANN("until statement");
255 ANN("format: until [nd_cond]; [nd_body]; end");
256 ANN("example: until x == 1; foo; end");
258 F_CUSTOM1(nd_state
, "begin-end-while?") {
259 A_INT((int)node
->nd_state
);
260 A((node
->nd_state
== 1) ? " (while-end)" : " (begin-end-while)");
262 F_NODE(nd_cond
, "condition");
264 F_NODE(nd_body
, "body");
268 ANN("method call with block");
269 ANN("format: [nd_iter] { [nd_body] }");
270 ANN("example: 3.times { foo }");
273 ANN("for statement");
274 ANN("format: for * in [nd_iter] do [nd_body] end");
275 ANN("example: for i in 1..3 do foo end");
277 F_NODE(nd_iter
, "iteration receiver");
279 F_NODE(nd_body
, "body");
283 ANN("vars of for statement with masgn");
284 ANN("format: for [nd_var] in ... do ... end");
285 ANN("example: for x, y in 1..3 do foo end");
287 F_NODE(nd_var
, "var");
291 ANN("break statement");
292 ANN("format: break [nd_stts]");
293 ANN("example: break 1");
296 ANN("next statement");
297 ANN("format: next [nd_stts]");
298 ANN("example: next 1");
301 ANN("return statement");
302 ANN("format: return [nd_stts]");
303 ANN("example: return 1");
306 F_NODE(nd_stts
, "value");
310 ANN("redo statement");
312 ANN("example: redo");
316 ANN("retry statement");
317 ANN("format: retry");
318 ANN("example: retry");
322 ANN("begin statement");
323 ANN("format: begin; [nd_body]; end");
324 ANN("example: begin; 1; end");
326 F_NODE(nd_body
, "body");
330 ANN("rescue clause");
331 ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
332 ANN("example: begin; foo; rescue; bar; else; baz; end");
333 F_NODE(nd_head
, "body");
334 F_NODE(nd_resq
, "rescue clause list");
336 F_NODE(nd_else
, "rescue else clause");
340 ANN("rescue clause (cont'd)");
341 ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
342 ANN("example: begin; foo; rescue; bar; else; baz; end");
343 F_NODE(nd_args
, "rescue exceptions");
344 F_NODE(nd_body
, "rescue clause");
346 F_NODE(nd_head
, "next rescue clause");
350 ANN("ensure clause");
351 ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
352 ANN("example: begin; foo; ensure; bar; end");
353 F_NODE(nd_head
, "body");
355 F_NODE(nd_ensr
, "ensure clause");
360 ANN("format: [nd_1st] && [nd_2nd]");
361 ANN("example: foo && bar");
365 ANN("format: [nd_1st] || [nd_2nd]");
366 ANN("example: foo || bar");
369 F_NODE(nd_1st
, "left expr");
370 if (!node
->nd_2nd
|| !nd_type_p(node
->nd_2nd
, type
))
375 F_NODE(nd_2nd
, "right expr");
379 ANN("multiple assignment");
380 ANN("format: [nd_head], [nd_args] = [nd_value]");
381 ANN("example: a, b = foo");
382 F_NODE(nd_value
, "rhsn");
383 F_NODE(nd_head
, "lhsn");
384 if (NODE_NAMED_REST_P(node
->nd_args
)) {
386 F_NODE(nd_args
, "splatn");
389 F_MSG(nd_args
, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
394 ANN("local variable assignment");
395 ANN("format: [nd_vid](lvar) = [nd_value]");
396 ANN("example: x = foo");
397 F_ID(nd_vid
, "local variable");
398 if (NODE_REQUIRED_KEYWORD_P(node
)) {
399 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
403 F_NODE(nd_value
, "rvalue");
407 ANN("dynamic variable assignment");
408 ANN("format: [nd_vid](dvar) = [nd_value]");
409 ANN("example: x = nil; 1.times { x = foo }");
410 ANN("example: 1.times { x = foo }");
411 F_ID(nd_vid
, "local variable");
412 if (NODE_REQUIRED_KEYWORD_P(node
)) {
413 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
417 F_NODE(nd_value
, "rvalue");
421 ANN("instance variable assignment");
422 ANN("format: [nd_vid](ivar) = [nd_value]");
423 ANN("example: @x = foo");
424 F_ID(nd_vid
, "instance variable");
426 F_NODE(nd_value
, "rvalue");
429 ANN("class variable assignment");
430 ANN("format: [nd_vid](cvar) = [nd_value]");
431 ANN("example: @@x = foo");
432 F_ID(nd_vid
, "class variable");
434 F_NODE(nd_value
, "rvalue");
437 ANN("global variable assignment");
438 ANN("format: [nd_vid](gvar) = [nd_value]");
439 ANN("example: $x = foo");
440 F_ID(nd_vid
, "global variable");
442 F_NODE(nd_value
, "rvalue");
446 ANN("constant declaration");
447 ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
448 ANN("example: X = foo");
450 F_ID(nd_vid
, "constant");
451 F_MSG(nd_else
, "extension", "not used");
454 F_MSG(nd_vid
, "constant", "0 (see extension field)");
455 F_NODE(nd_else
, "extension");
458 F_NODE(nd_value
, "rvalue");
462 ANN("array assignment with operator");
463 ANN("format: [nd_recv] [ [nd_args->nd_head] ] [nd_mid]= [nd_args->nd_body]");
464 ANN("example: ary[1] += foo");
465 F_NODE(nd_recv
, "receiver");
466 F_ID(nd_mid
, "operator");
467 F_NODE(nd_args
->nd_head
, "index");
469 F_NODE(nd_args
->nd_body
, "rvalue");
473 ANN("attr assignment with operator");
474 ANN("format: [nd_recv].[attr] [nd_next->nd_mid]= [nd_value]");
475 ANN(" where [attr]: [nd_next->nd_vid]");
476 ANN("example: struct.field += foo");
477 F_NODE(nd_recv
, "receiver");
478 F_CUSTOM1(nd_next
->nd_vid
, "attr") {
479 if (node
->nd_next
->nd_aid
) A("? ");
480 A_ID(node
->nd_next
->nd_vid
);
482 F_ID(nd_next
->nd_mid
, "operator");
484 F_NODE(nd_value
, "rvalue");
487 case NODE_OP_ASGN_AND
:
488 ANN("assignment with && operator");
489 ANN("format: [nd_head] &&= [nd_value]");
490 ANN("example: foo &&= bar");
492 case NODE_OP_ASGN_OR
:
493 ANN("assignment with || operator");
494 ANN("format: [nd_head] ||= [nd_value]");
495 ANN("example: foo ||= bar");
497 F_NODE(nd_head
, "variable");
499 F_NODE(nd_value
, "rvalue");
503 ANN("constant declaration with operator");
504 ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
505 ANN("example: A::B ||= 1");
506 F_NODE(nd_head
, "constant");
507 F_ID(nd_aid
, "operator");
509 F_NODE(nd_value
, "rvalue");
513 ANN("method invocation");
514 ANN("format: [nd_recv].[nd_mid]([nd_args])");
515 ANN("example: obj.foo(1)");
516 F_ID(nd_mid
, "method id");
517 F_NODE(nd_recv
, "receiver");
519 F_NODE(nd_args
, "arguments");
523 ANN("method invocation");
524 ANN("format: [nd_recv] [nd_mid] [nd_args]");
525 ANN("example: foo + bar");
526 F_ID(nd_mid
, "method id");
527 F_NODE(nd_recv
, "receiver");
529 F_NODE(nd_args
, "arguments");
533 ANN("function call");
534 ANN("format: [nd_mid]([nd_args])");
535 ANN("example: foo(1)");
536 F_ID(nd_mid
, "method id");
538 F_NODE(nd_args
, "arguments");
542 ANN("function call with no argument");
543 ANN("format: [nd_mid]");
545 F_ID(nd_mid
, "method id");
549 ANN("safe method invocation");
550 ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
551 ANN("example: obj&.foo(1)");
552 F_ID(nd_mid
, "method id");
553 F_NODE(nd_recv
, "receiver");
555 F_NODE(nd_args
, "arguments");
559 ANN("super invocation");
560 ANN("format: super [nd_args]");
561 ANN("example: super 1");
563 F_NODE(nd_args
, "arguments");
567 ANN("super invocation with no argument");
568 ANN("format: super");
569 ANN("example: super");
573 ANN("list constructor");
574 ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
575 ANN("example: [1, 2, 3]");
578 ANN("return arguments");
579 ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
580 ANN("example: return 1, 2, 3");
582 dump_array(buf
, indent
, comment
, node
);
586 ANN("empty list constructor");
592 if (!node
->nd_brace
) {
593 ANN("keyword arguments");
594 ANN("format: [nd_head]");
595 ANN("example: a: 1, b: 2");
598 ANN("hash constructor");
599 ANN("format: { [nd_head] }");
600 ANN("example: { 1 => 2, 3 => 4 }");
602 F_CUSTOM1(nd_brace
, "keyword arguments or hash literal") {
603 switch (node
->nd_brace
) {
604 case 0: A("0 (keyword argument)"); break;
605 case 1: A("1 (hash literal)"); break;
609 F_NODE(nd_head
, "contents");
613 ANN("yield invocation");
614 ANN("format: yield [nd_head]");
615 ANN("example: yield 1");
617 F_NODE(nd_head
, "arguments");
621 ANN("local variable reference");
622 ANN("format: [nd_vid](lvar)");
624 F_ID(nd_vid
, "local variable");
627 ANN("dynamic variable reference");
628 ANN("format: [nd_vid](dvar)");
629 ANN("example: 1.times { x = 1; x }");
630 F_ID(nd_vid
, "local variable");
633 ANN("instance variable reference");
634 ANN("format: [nd_vid](ivar)");
636 F_ID(nd_vid
, "instance variable");
639 ANN("constant reference");
640 ANN("format: [nd_vid](constant)");
642 F_ID(nd_vid
, "constant");
645 ANN("class variable reference");
646 ANN("format: [nd_vid](cvar)");
648 F_ID(nd_vid
, "class variable");
652 ANN("global variable reference");
653 ANN("format: [nd_vid](gvar)");
655 F_ID(nd_vid
, "global variable");
659 ANN("nth special variable reference");
660 ANN("format: $[nd_nth]");
661 ANN("example: $1, $2, ..");
662 F_CUSTOM1(nd_nth
, "variable") { A("$"); A_LONG(node
->nd_nth
); }
666 ANN("back special variable reference");
667 ANN("format: $[nd_nth]");
668 ANN("example: $&, $`, $', $+");
669 F_CUSTOM1(nd_nth
, "variable") {
671 name
[1] = (char)node
->nd_nth
;
677 ANN("match expression (against $_ implicitly)");
678 ANN("format: [nd_lit] (in condition)");
679 ANN("example: if /foo/; foo; end");
680 F_LIT(nd_lit
, "regexp");
684 ANN("match expression (regexp first)");
685 ANN("format: [nd_recv] =~ [nd_value]");
686 ANN("example: /foo/ =~ 'foo'");
687 F_NODE(nd_recv
, "regexp (receiver)");
688 if (!node
->nd_args
) LAST_NODE
;
689 F_NODE(nd_value
, "string (argument)");
692 F_NODE(nd_args
, "named captures");
697 ANN("match expression (regexp second)");
698 ANN("format: [nd_recv] =~ [nd_value]");
699 ANN("example: 'foo' =~ /foo/");
700 F_NODE(nd_recv
, "string (receiver)");
702 F_NODE(nd_value
, "regexp (argument)");
707 ANN("format: [nd_lit]");
708 ANN("example: 1, /foo/");
711 ANN("string literal");
712 ANN("format: [nd_lit]");
713 ANN("example: 'foo'");
716 ANN("xstring literal");
717 ANN("format: [nd_lit]");
718 ANN("example: `foo`");
720 F_LIT(nd_lit
, "literal");
724 ANN("once evaluation");
725 ANN("format: [nd_body]");
726 ANN("example: /foo#{ bar }baz/o");
728 F_NODE(nd_body
, "body");
731 ANN("string literal with interpolation");
732 ANN("format: [nd_lit]");
733 ANN("example: \"foo#{ bar }baz\"");
736 ANN("xstring literal with interpolation");
737 ANN("format: [nd_lit]");
738 ANN("example: `foo#{ bar }baz`");
741 ANN("regexp literal with interpolation");
742 ANN("format: [nd_lit]");
743 ANN("example: /foo#{ bar }baz/");
746 ANN("symbol literal with interpolation");
747 ANN("format: [nd_lit]");
748 ANN("example: :\"foo#{ bar }baz\"");
750 F_LIT(nd_lit
, "preceding string");
751 if (!node
->nd_next
) return;
752 F_NODE(nd_next
->nd_head
, "interpolation");
754 F_NODE(nd_next
->nd_next
, "tailing strings");
758 ANN("interpolation expression");
759 ANN("format: \"..#{ [nd_body] }..\"");
760 ANN("example: \"foo#{ bar }baz\"");
762 F_NODE(nd_body
, "body");
766 ANN("splat argument following arguments");
767 ANN("format: ..(*[nd_head], [nd_body..])");
768 ANN("example: foo(*ary, post_arg1, post_arg2)");
769 F_NODE(nd_head
, "preceding array");
771 F_NODE(nd_body
, "following array");
775 ANN("splat argument following one argument");
776 ANN("format: ..(*[nd_head], [nd_body])");
777 ANN("example: foo(*ary, post_arg)");
778 F_NODE(nd_head
, "preceding array");
780 F_NODE(nd_body
, "following element");
784 ANN("splat argument");
785 ANN("format: *[nd_head]");
786 ANN("example: foo(*ary)");
788 F_NODE(nd_head
, "splat'ed array");
791 case NODE_BLOCK_PASS
:
792 ANN("arguments with block argument");
793 ANN("format: ..([nd_head], &[nd_body])");
794 ANN("example: foo(x, &blk)");
795 F_NODE(nd_head
, "other arguments");
797 F_NODE(nd_body
, "block argument");
801 ANN("method definition");
802 ANN("format: def [nd_mid] [nd_defn]; end");
803 ANN("example: def foo; bar; end");
804 F_ID(nd_mid
, "method name");
806 F_NODE(nd_defn
, "method definition");
810 ANN("singleton method definition");
811 ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
812 ANN("example: def obj.foo; bar; end");
813 F_NODE(nd_recv
, "receiver");
814 F_ID(nd_mid
, "method name");
816 F_NODE(nd_defn
, "method definition");
820 ANN("method alias statement");
821 ANN("format: alias [nd_1st] [nd_2nd]");
822 ANN("example: alias bar foo");
823 F_NODE(nd_1st
, "new name");
825 F_NODE(nd_2nd
, "old name");
829 ANN("global variable alias statement");
830 ANN("format: alias [nd_alias](gvar) [nd_orig](gvar)");
831 ANN("example: alias $y $x");
832 F_ID(nd_alias
, "new name");
833 F_ID(nd_orig
, "old name");
837 ANN("method undef statement");
838 ANN("format: undef [nd_undef]");
839 ANN("example: undef foo");
841 F_NODE(nd_undef
, "old name");
845 ANN("class definition");
846 ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
847 ANN("example: class C2 < C; ..; end");
848 F_NODE(nd_cpath
, "class path");
849 F_NODE(nd_super
, "superclass");
851 F_NODE(nd_body
, "class definition");
855 ANN("module definition");
856 ANN("format: module [nd_cpath]; [nd_body]; end");
857 ANN("example: module M; ..; end");
858 F_NODE(nd_cpath
, "module path");
860 F_NODE(nd_body
, "module definition");
864 ANN("singleton class definition");
865 ANN("format: class << [nd_recv]; [nd_body]; end");
866 ANN("example: class << obj; ..; end");
867 F_NODE(nd_recv
, "receiver");
869 F_NODE(nd_body
, "singleton class definition");
873 ANN("scoped constant reference");
874 ANN("format: [nd_head]::[nd_mid]");
875 ANN("example: M::C");
876 F_ID(nd_mid
, "constant name");
878 F_NODE(nd_head
, "receiver");
882 ANN("top-level constant reference");
883 ANN("format: ::[nd_mid]");
884 ANN("example: ::Object");
885 F_ID(nd_mid
, "constant name");
889 ANN("range constructor (incl.)");
890 ANN("format: [nd_beg]..[nd_end]");
891 ANN("example: 1..5");
894 ANN("range constructor (excl.)");
895 ANN("format: [nd_beg]...[nd_end]");
896 ANN("example: 1...5");
899 ANN("flip-flop condition (incl.)");
900 ANN("format: [nd_beg]..[nd_end]");
901 ANN("example: if (x==1)..(x==5); foo; end");
904 ANN("flip-flop condition (excl.)");
905 ANN("format: [nd_beg]...[nd_end]");
906 ANN("example: if (x==1)...(x==5); foo; end");
908 F_NODE(nd_beg
, "begin");
910 F_NODE(nd_end
, "end");
916 ANN("example: self");
928 ANN("example: true");
933 ANN("format: false");
934 ANN("example: false");
938 ANN("virtual reference to $!");
939 ANN("format: rescue => id");
940 ANN("example: rescue => id");
944 ANN("defined? expression");
945 ANN("format: defined?([nd_head])");
946 ANN("example: defined?(foo)");
947 F_NODE(nd_head
, "expr");
951 ANN("post-execution");
952 ANN("format: END { [nd_body] }");
953 ANN("example: END { foo }");
955 F_NODE(nd_body
, "END clause");
959 ANN("attr assignment");
960 ANN("format: [nd_recv].[nd_mid] = [nd_args]");
961 ANN("example: struct.field = foo");
962 F_NODE(nd_recv
, "receiver");
963 F_ID(nd_mid
, "method name");
965 F_NODE(nd_args
, "arguments");
969 ANN("lambda expression");
970 ANN("format: -> [nd_body]");
971 ANN("example: -> { foo }");
973 F_NODE(nd_body
, "lambda clause");
977 ANN("optional arguments");
978 ANN("format: def method_name([nd_body=some], [nd_next..])");
979 ANN("example: def foo(a, b=1, c); end");
980 F_NODE(nd_body
, "body");
982 F_NODE(nd_next
, "next");
986 ANN("keyword arguments");
987 ANN("format: def method_name([nd_body=some], [nd_next..])");
988 ANN("example: def foo(a:1, b:2); end");
989 F_NODE(nd_body
, "body");
991 F_NODE(nd_next
, "next");
995 ANN("post arguments");
996 ANN("format: *[nd_1st], [nd_2nd..] = ..");
997 ANN("example: a, *rest, z = foo");
998 if (NODE_NAMED_REST_P(node
->nd_1st
)) {
999 F_NODE(nd_1st
, "rest argument");
1002 F_MSG(nd_1st
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1005 F_NODE(nd_2nd
, "post arguments");
1009 ANN("method parameters");
1010 ANN("format: def method_name(.., [nd_ainfo->nd_optargs], *[nd_ainfo->rest_arg], [nd_ainfo->first_post_arg], .., [nd_ainfo->kw_args], **[nd_ainfo->kw_rest_arg], &[nd_ainfo->block_arg])");
1011 ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
1012 F_INT(nd_ainfo
->pre_args_num
, "count of mandatory (pre-)arguments");
1013 F_NODE(nd_ainfo
->pre_init
, "initialization of (pre-)arguments");
1014 F_INT(nd_ainfo
->post_args_num
, "count of mandatory post-arguments");
1015 F_NODE(nd_ainfo
->post_init
, "initialization of post-arguments");
1016 F_ID(nd_ainfo
->first_post_arg
, "first post argument");
1017 F_CUSTOM1(nd_ainfo
->rest_arg
, "rest argument") {
1018 if (node
->nd_ainfo
->rest_arg
== NODE_SPECIAL_EXCESSIVE_COMMA
) {
1019 A("1 (excessed comma)");
1022 A_ID(node
->nd_ainfo
->rest_arg
);
1025 F_ID(nd_ainfo
->block_arg
, "block argument");
1026 F_NODE(nd_ainfo
->opt_args
, "optional arguments");
1027 F_NODE(nd_ainfo
->kw_args
, "keyword arguments");
1029 F_NODE(nd_ainfo
->kw_rest_arg
, "keyword rest argument");
1034 ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
1035 F_CUSTOM1(nd_tbl
, "local table") {
1036 rb_ast_id_table_t
*tbl
= node
->nd_tbl
;
1038 int size
= tbl
? tbl
->size
: 0;
1039 if (size
== 0) A("(empty)");
1040 for (i
= 0; i
< size
; i
++) {
1041 A_ID(tbl
->ids
[i
]); if (i
< size
- 1) A(",");
1044 F_NODE(nd_args
, "arguments");
1046 F_NODE(nd_body
, "body");
1050 ANN("array pattern");
1051 ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
1052 F_NODE(nd_pconst
, "constant");
1053 F_NODE(nd_apinfo
->pre_args
, "pre arguments");
1054 if (NODE_NAMED_REST_P(node
->nd_apinfo
->rest_arg
)) {
1055 F_NODE(nd_apinfo
->rest_arg
, "rest argument");
1058 F_MSG(nd_apinfo
->rest_arg
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1061 F_NODE(nd_apinfo
->post_args
, "post arguments");
1065 ANN("find pattern");
1066 ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
1067 F_NODE(nd_pconst
, "constant");
1068 if (NODE_NAMED_REST_P(node
->nd_fpinfo
->pre_rest_arg
)) {
1069 F_NODE(nd_fpinfo
->pre_rest_arg
, "pre rest argument");
1072 F_MSG(nd_fpinfo
->pre_rest_arg
, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1074 F_NODE(nd_fpinfo
->args
, "arguments");
1077 if (NODE_NAMED_REST_P(node
->nd_fpinfo
->post_rest_arg
)) {
1078 F_NODE(nd_fpinfo
->post_rest_arg
, "post rest argument");
1081 F_MSG(nd_fpinfo
->post_rest_arg
, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1086 ANN("hash pattern");
1087 ANN("format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
1088 F_NODE(nd_pconst
, "constant");
1089 F_NODE(nd_pkwargs
, "keyword arguments");
1091 if (node
->nd_pkwrestarg
== NODE_SPECIAL_NO_REST_KEYWORD
) {
1092 F_MSG(nd_pkwrestarg
, "keyword rest argument", "NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
1095 F_NODE(nd_pkwrestarg
, "keyword rest argument");
1099 ANN("Broken input recovered by Error Tolerant mode");
1107 rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node
)));
1111 rb_parser_dump_tree(const NODE
*node
, int comment
)
1113 VALUE buf
= rb_str_new_cstr(
1114 "###########################################################\n"
1115 "## Do NOT use this node dump for any purpose other than ##\n"
1116 "## debug and research. Compatibility is not guaranteed. ##\n"
1117 "###########################################################\n\n"
1119 dump_node(buf
, rb_str_new_cstr("# "), comment
, node
);