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/class.h"
14 #include "internal/hash.h"
15 #include "internal/ruby_parser.h"
16 #include "internal/variable.h"
17 #include "ruby/ruby.h"
20 #define A(str) rb_str_cat2(buf, (str))
21 #define AR(str) rb_str_concat(buf, (str))
23 #define A_INDENT add_indent(buf, indent)
24 #define D_INDENT rb_str_cat2(indent, next_indent)
25 #define D_DEDENT rb_str_resize(indent, RSTRING_LEN(indent) - 4)
26 #define A_ID(id) add_id(buf, (id))
27 #define A_INT(val) rb_str_catf(buf, "%d", (val))
28 #define A_LONG(val) rb_str_catf(buf, "%ld", (val))
29 #define A_LIT(lit) AR(rb_dump_literal(lit))
30 #define A_NODE_HEADER(node, term) \
31 rb_str_catf(buf, "@ %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))%s"term, \
32 ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node), \
33 nd_first_lineno(node), nd_first_column(node), \
34 nd_last_lineno(node), nd_last_column(node), \
35 (nd_fl_newline(node) ? "*" : ""))
36 #define A_FIELD_HEADER(len, name, term) \
37 rb_str_catf(buf, "+- %.*s:"term, (len), (name))
38 #define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term))
40 #define D_NULL_NODE (A_INDENT, A("(null node)\n"))
41 #define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n"))
43 #define COMPOUND_FIELD(len, name) \
44 FIELD_BLOCK((D_FIELD_HEADER((len), (name), "\n"), D_INDENT), D_DEDENT)
46 #define COMPOUND_FIELD1(name, ann) \
47 COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \
48 FIELD_NAME_DESC(name, ann))
50 #define FIELD_NAME_DESC(name, ann) name " (" ann ")"
51 #define FIELD_NAME_LEN(name, ann) (int)( \
53 rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
55 #define SIMPLE_FIELD(len, name) \
56 FIELD_BLOCK(D_FIELD_HEADER((len), (name), " "), A("\n"))
58 #define FIELD_BLOCK(init, reset) \
59 for (init, field_flag = 1; \
60 field_flag; /* should be optimized away */ \
61 reset, field_flag = 0)
63 #define A_SHAREABILITY(shareability) \
64 switch (shareability) { \
65 case rb_parser_shareable_none: \
66 rb_str_cat_cstr(buf, "none"); \
68 case rb_parser_shareable_literal: \
69 rb_str_cat_cstr(buf, "literal"); \
71 case rb_parser_shareable_copy: \
72 rb_str_cat_cstr(buf, "experimental_copy"); \
74 case rb_parser_shareable_everything: \
75 rb_str_cat_cstr(buf, "experimental_everything"); \
79 #define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
80 #define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
81 #define F_ID(name, type, ann) SIMPLE_FIELD1(#name, ann) A_ID(type(node)->name)
82 #define F_INT(name, type, ann) SIMPLE_FIELD1(#name, ann) A_INT(type(node)->name)
83 #define F_LONG(name, type, ann) SIMPLE_FIELD1(#name, ann) A_LONG(type(node)->name)
84 #define F_LIT(name, type, ann) SIMPLE_FIELD1(#name, ann) A_LIT(type(node)->name)
85 #define F_VALUE(name, val, ann) SIMPLE_FIELD1(#name, ann) A_LIT(val)
86 #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
87 #define F_SHAREABILITY(name, type, ann) SIMPLE_FIELD1(#name, ann) A_SHAREABILITY(type(node)->name)
89 #define F_NODE(name, type, ann) \
90 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, RNODE(type(node)->name));}
92 #define F_NODE2(name, n, ann) \
93 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, n);}
95 #define F_ARRAY(name, type, ann) \
96 COMPOUND_FIELD1(#name, ann) {dump_parser_array(buf, indent, comment, type(node)->name);}
100 A_INDENT; A("| # " ann "\n"); \
103 #define LAST_NODE (next_indent = " ")
106 rb_dump_literal(VALUE lit
)
108 if (!RB_SPECIAL_CONST_P(lit
)) {
110 switch (RB_BUILTIN_TYPE(lit
)) {
111 case T_CLASS
: case T_MODULE
: case T_ICLASS
:
112 str
= rb_class_path(lit
);
113 if (RCLASS_SINGLETON_P(lit
)) {
114 str
= rb_sprintf("<%"PRIsVALUE
">", str
);
121 return rb_inspect(lit
);
125 add_indent(VALUE buf
, VALUE indent
)
131 add_id(VALUE buf
, ID id
)
137 VALUE str
= rb_id2str(id
);
142 rb_str_catf(buf
, "(internal variable: 0x%"PRIsVALUE
")", id
);
147 struct add_option_arg
{
152 static void dump_node(VALUE
, VALUE
, int, const NODE
*);
153 static const char default_indent
[] = "| ";
156 dump_array(VALUE buf
, VALUE indent
, int comment
, const NODE
*node
)
159 const char *next_indent
= default_indent
;
160 F_LONG(as
.nd_alen
, RNODE_LIST
, "length");
161 F_NODE(nd_head
, RNODE_LIST
, "element");
162 while (RNODE_LIST(node
)->nd_next
&& nd_type_p(RNODE_LIST(node
)->nd_next
, NODE_LIST
)) {
163 node
= RNODE_LIST(node
)->nd_next
;
164 F_NODE(nd_head
, RNODE_LIST
, "element");
167 F_NODE(nd_next
, RNODE_LIST
, "next element");
171 dump_parser_array(VALUE buf
, VALUE indent
, int comment
, const rb_parser_ary_t
*ary
)
174 const char *next_indent
= default_indent
;
176 if (ary
->data_type
!= PARSER_ARY_DATA_NODE
) {
177 rb_bug("unexpected rb_parser_ary_data_type: %d", ary
->data_type
);
180 F_CUSTOM1(length
, "length") { A_LONG(ary
->len
); }
181 for (long i
= 0; i
< ary
->len
; i
++) {
182 if (i
== ary
->len
- 1) LAST_NODE
;
184 rb_str_catf(buf
, "+- element (%s%ld):\n",
185 comment
? "statement #" : "", i
);
187 dump_node(buf
, indent
, comment
, ary
->data
[i
]);
193 dump_node(VALUE buf
, VALUE indent
, int comment
, const NODE
* node
)
197 const char *next_indent
= default_indent
;
207 type
= nd_type(node
);
210 ANN("statement sequence");
211 ANN("format: [nd_head]; ...; [nd_next]");
212 ANN("example: foo; bar");
216 rb_str_catf(buf
, "+- nd_head (%s%d):\n",
217 comment
? "statement #" : "", ++i
);
218 if (!RNODE_BLOCK(node
)->nd_next
) LAST_NODE
;
220 dump_node(buf
, indent
, comment
, RNODE_BLOCK(node
)->nd_head
);
222 } while (RNODE_BLOCK(node
)->nd_next
&&
223 nd_type_p(RNODE_BLOCK(node
)->nd_next
, NODE_BLOCK
) &&
224 (node
= RNODE_BLOCK(node
)->nd_next
, 1));
225 if (RNODE_BLOCK(node
)->nd_next
) {
227 F_NODE(nd_next
, RNODE_BLOCK
, "next block");
233 ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
234 ANN("example: if x == 1 then foo else bar end");
235 F_NODE(nd_cond
, RNODE_IF
, "condition expr");
236 F_NODE(nd_body
, RNODE_IF
, "then clause");
238 F_NODE(nd_else
, RNODE_IF
, "else clause");
242 ANN("unless statement");
243 ANN("format: unless [nd_cond] then [nd_body] else [nd_else] end");
244 ANN("example: unless x == 1 then foo else bar end");
245 F_NODE(nd_cond
, RNODE_UNLESS
, "condition expr");
246 F_NODE(nd_body
, RNODE_UNLESS
, "then clause");
248 F_NODE(nd_else
, RNODE_UNLESS
, "else clause");
252 ANN("case statement");
253 ANN("format: case [nd_head]; [nd_body]; end");
254 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
255 F_NODE(nd_head
, RNODE_CASE
, "case expr");
257 F_NODE(nd_body
, RNODE_CASE
, "when clauses");
260 ANN("case statement with no head");
261 ANN("format: case; [nd_body]; end");
262 ANN("example: case; when 1; foo; when 2; bar; else baz; end");
263 F_NODE(nd_head
, RNODE_CASE2
, "case expr");
265 F_NODE(nd_body
, RNODE_CASE2
, "when clauses");
268 ANN("case statement (pattern matching)");
269 ANN("format: case [nd_head]; [nd_body]; end");
270 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
271 F_NODE(nd_head
, RNODE_CASE3
, "case expr");
273 F_NODE(nd_body
, RNODE_CASE3
, "in clauses");
278 ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
279 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
280 F_NODE(nd_head
, RNODE_WHEN
, "when value");
281 F_NODE(nd_body
, RNODE_WHEN
, "when body");
283 F_NODE(nd_next
, RNODE_WHEN
, "next when clause");
288 ANN("format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
289 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
290 F_NODE(nd_head
, RNODE_IN
, "in pattern");
291 F_NODE(nd_body
, RNODE_IN
, "in body");
293 F_NODE(nd_next
, RNODE_IN
, "next in clause");
297 ANN("while statement");
298 ANN("format: while [nd_cond]; [nd_body]; end");
299 ANN("example: while x == 1; foo; end");
302 ANN("until statement");
303 ANN("format: until [nd_cond]; [nd_body]; end");
304 ANN("example: until x == 1; foo; end");
306 F_CUSTOM1(nd_state
, "begin-end-while?") {
307 A_INT((int)RNODE_WHILE(node
)->nd_state
);
308 A((RNODE_WHILE(node
)->nd_state
== 1) ? " (while-end)" : " (begin-end-while)");
310 F_NODE(nd_cond
, RNODE_WHILE
, "condition");
312 F_NODE(nd_body
, RNODE_WHILE
, "body");
316 ANN("method call with block");
317 ANN("format: [nd_iter] { [nd_body] }");
318 ANN("example: 3.times { foo }");
321 ANN("for statement");
322 ANN("format: for * in [nd_iter] do [nd_body] end");
323 ANN("example: for i in 1..3 do foo end");
325 F_NODE(nd_iter
, RNODE_ITER
, "iteration receiver");
327 F_NODE(nd_body
, RNODE_ITER
, "body");
331 ANN("vars of for statement with masgn");
332 ANN("format: for [nd_var] in ... do ... end");
333 ANN("example: for x, y in 1..3 do foo end");
335 F_NODE(nd_var
, RNODE_FOR_MASGN
, "var");
339 ANN("break statement");
340 ANN("format: break [nd_stts]");
341 ANN("example: break 1");
343 F_NODE(nd_stts
, RNODE_BREAK
, "value");
346 ANN("next statement");
347 ANN("format: next [nd_stts]");
348 ANN("example: next 1");
350 F_NODE(nd_stts
, RNODE_NEXT
, "value");
353 ANN("return statement");
354 ANN("format: return [nd_stts]");
355 ANN("example: return 1");
357 F_NODE(nd_stts
, RNODE_RETURN
, "value");
361 ANN("redo statement");
363 ANN("example: redo");
367 ANN("retry statement");
368 ANN("format: retry");
369 ANN("example: retry");
373 ANN("begin statement");
374 ANN("format: begin; [nd_body]; end");
375 ANN("example: begin; 1; end");
377 F_NODE(nd_body
, RNODE_BEGIN
, "body");
381 ANN("rescue clause");
382 ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
383 ANN("example: begin; foo; rescue; bar; else; baz; end");
384 F_NODE(nd_head
, RNODE_RESCUE
, "body");
385 F_NODE(nd_resq
, RNODE_RESCUE
, "rescue clause list");
387 F_NODE(nd_else
, RNODE_RESCUE
, "rescue else clause");
391 ANN("rescue clause (cont'd)");
392 ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
393 ANN("example: begin; foo; rescue; bar; else; baz; end");
394 F_NODE(nd_args
, RNODE_RESBODY
, "rescue exceptions");
395 F_NODE(nd_body
, RNODE_RESBODY
, "rescue clause");
397 F_NODE(nd_next
, RNODE_RESBODY
, "next rescue clause");
401 ANN("ensure clause");
402 ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
403 ANN("example: begin; foo; ensure; bar; end");
404 F_NODE(nd_head
, RNODE_ENSURE
, "body");
406 F_NODE(nd_ensr
, RNODE_ENSURE
, "ensure clause");
411 ANN("format: [nd_1st] && [nd_2nd]");
412 ANN("example: foo && bar");
416 ANN("format: [nd_1st] || [nd_2nd]");
417 ANN("example: foo || bar");
420 F_NODE(nd_1st
, RNODE_AND
, "left expr");
421 if (!RNODE_AND(node
)->nd_2nd
|| !nd_type_p(RNODE_AND(node
)->nd_2nd
, type
))
423 node
= RNODE_AND(node
)->nd_2nd
;
426 F_NODE(nd_2nd
, RNODE_AND
, "right expr");
430 ANN("multiple assignment");
431 ANN("format: [nd_head], [nd_args] = [nd_value]");
432 ANN("example: a, b = foo");
433 F_NODE(nd_value
, RNODE_MASGN
, "rhsn");
434 F_NODE(nd_head
, RNODE_MASGN
, "lhsn");
435 if (NODE_NAMED_REST_P(RNODE_MASGN(node
)->nd_args
)) {
437 F_NODE(nd_args
, RNODE_MASGN
, "splatn");
440 F_MSG(nd_args
, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
445 ANN("local variable assignment");
446 ANN("format: [nd_vid](lvar) = [nd_value]");
447 ANN("example: x = foo");
448 F_ID(nd_vid
, RNODE_LASGN
, "local variable");
449 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node
)->nd_value
)) {
450 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
454 F_NODE(nd_value
, RNODE_LASGN
, "rvalue");
458 ANN("dynamic variable assignment");
459 ANN("format: [nd_vid](dvar) = [nd_value]");
460 ANN("example: x = nil; 1.times { x = foo }");
461 ANN("example: 1.times { x = foo }");
462 F_ID(nd_vid
, RNODE_DASGN
, "local variable");
463 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node
)->nd_value
)) {
464 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
468 F_NODE(nd_value
, RNODE_DASGN
, "rvalue");
472 ANN("instance variable assignment");
473 ANN("format: [nd_vid](ivar) = [nd_value]");
474 ANN("example: @x = foo");
475 F_ID(nd_vid
, RNODE_IASGN
, "instance variable");
477 F_NODE(nd_value
, RNODE_IASGN
, "rvalue");
480 ANN("class variable assignment");
481 ANN("format: [nd_vid](cvar) = [nd_value]");
482 ANN("example: @@x = foo");
483 F_ID(nd_vid
, RNODE_CVASGN
, "class variable");
485 F_NODE(nd_value
, RNODE_CVASGN
, "rvalue");
488 ANN("global variable assignment");
489 ANN("format: [nd_vid](gvar) = [nd_value]");
490 ANN("example: $x = foo");
491 F_ID(nd_vid
, RNODE_GASGN
, "global variable");
493 F_NODE(nd_value
, RNODE_GASGN
, "rvalue");
497 ANN("constant declaration");
498 ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
499 ANN("example: X = foo");
500 if (RNODE_CDECL(node
)->nd_vid
) {
501 F_ID(nd_vid
, RNODE_CDECL
, "constant");
502 F_MSG(nd_else
, "extension", "not used");
505 F_MSG(nd_vid
, "constant", "0 (see extension field)");
506 F_NODE(nd_else
, RNODE_CDECL
, "extension");
508 F_SHAREABILITY(shareability
, RNODE_CDECL
, "shareability");
510 F_NODE(nd_value
, RNODE_CDECL
, "rvalue");
514 ANN("array assignment with operator");
515 ANN("format: [nd_recv] [ [nd_index] ] [nd_mid]= [nd_rvalue]");
516 ANN("example: ary[1] += foo");
517 F_NODE(nd_recv
, RNODE_OP_ASGN1
, "receiver");
518 F_ID(nd_mid
, RNODE_OP_ASGN1
, "operator");
519 F_NODE(nd_index
, RNODE_OP_ASGN1
, "index");
521 F_NODE(nd_rvalue
, RNODE_OP_ASGN1
, "rvalue");
525 ANN("attr assignment with operator");
526 ANN("format: [nd_recv].[nd_vid] [nd_mid]= [nd_value]");
527 ANN("example: struct.field += foo");
528 F_NODE(nd_recv
, RNODE_OP_ASGN2
, "receiver");
529 F_CUSTOM1(nd_vid
, "attr") {
530 if (RNODE_OP_ASGN2(node
)->nd_aid
) A("? ");
531 A_ID(RNODE_OP_ASGN2(node
)->nd_vid
);
533 F_ID(nd_mid
, RNODE_OP_ASGN2
, "operator");
535 F_NODE(nd_value
, RNODE_OP_ASGN2
, "rvalue");
538 case NODE_OP_ASGN_AND
:
539 ANN("assignment with && operator");
540 ANN("format: [nd_head] &&= [nd_value]");
541 ANN("example: foo &&= bar");
543 case NODE_OP_ASGN_OR
:
544 ANN("assignment with || operator");
545 ANN("format: [nd_head] ||= [nd_value]");
546 ANN("example: foo ||= bar");
548 F_NODE(nd_head
, RNODE_OP_ASGN_AND
, "variable");
550 F_NODE(nd_value
, RNODE_OP_ASGN_AND
, "rvalue");
554 ANN("constant declaration with operator");
555 ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
556 ANN("example: A::B ||= 1");
557 F_NODE(nd_head
, RNODE_OP_CDECL
, "constant");
558 F_ID(nd_aid
, RNODE_OP_CDECL
, "operator");
559 F_SHAREABILITY(shareability
, RNODE_OP_CDECL
, "shareability");
561 F_NODE(nd_value
, RNODE_OP_CDECL
, "rvalue");
565 ANN("method invocation");
566 ANN("format: [nd_recv].[nd_mid]([nd_args])");
567 ANN("example: obj.foo(1)");
568 F_ID(nd_mid
, RNODE_CALL
, "method id");
569 F_NODE(nd_recv
, RNODE_CALL
, "receiver");
571 F_NODE(nd_args
, RNODE_CALL
, "arguments");
575 ANN("method invocation");
576 ANN("format: [nd_recv] [nd_mid] [nd_args]");
577 ANN("example: foo + bar");
578 F_ID(nd_mid
, RNODE_OPCALL
, "method id");
579 F_NODE(nd_recv
, RNODE_OPCALL
, "receiver");
581 F_NODE(nd_args
, RNODE_OPCALL
, "arguments");
585 ANN("function call");
586 ANN("format: [nd_mid]([nd_args])");
587 ANN("example: foo(1)");
588 F_ID(nd_mid
, RNODE_FCALL
, "method id");
590 F_NODE(nd_args
, RNODE_FCALL
, "arguments");
594 ANN("function call with no argument");
595 ANN("format: [nd_mid]");
597 F_ID(nd_mid
, RNODE_VCALL
, "method id");
601 ANN("safe method invocation");
602 ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
603 ANN("example: obj&.foo(1)");
604 F_ID(nd_mid
, RNODE_QCALL
, "method id");
605 F_NODE(nd_recv
, RNODE_QCALL
, "receiver");
607 F_NODE(nd_args
, RNODE_QCALL
, "arguments");
611 ANN("super invocation");
612 ANN("format: super [nd_args]");
613 ANN("example: super 1");
615 F_NODE(nd_args
, RNODE_SUPER
, "arguments");
619 ANN("super invocation with no argument");
620 ANN("format: super");
621 ANN("example: super");
625 ANN("list constructor");
626 ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
627 ANN("example: [1, 2, 3]");
628 dump_array(buf
, indent
, comment
, node
);
632 ANN("empty list constructor");
638 if (!RNODE_HASH(node
)->nd_brace
) {
639 ANN("keyword arguments");
640 ANN("format: [nd_head]");
641 ANN("example: a: 1, b: 2");
644 ANN("hash constructor");
645 ANN("format: { [nd_head] }");
646 ANN("example: { 1 => 2, 3 => 4 }");
648 F_CUSTOM1(nd_brace
, "keyword arguments or hash literal") {
649 switch (RNODE_HASH(node
)->nd_brace
) {
650 case 0: A("0 (keyword argument)"); break;
651 case 1: A("1 (hash literal)"); break;
655 F_NODE(nd_head
, RNODE_HASH
, "contents");
659 ANN("yield invocation");
660 ANN("format: yield [nd_head]");
661 ANN("example: yield 1");
663 F_NODE(nd_head
, RNODE_YIELD
, "arguments");
667 ANN("local variable reference");
668 ANN("format: [nd_vid](lvar)");
670 F_ID(nd_vid
, RNODE_LVAR
, "local variable");
673 ANN("dynamic variable reference");
674 ANN("format: [nd_vid](dvar)");
675 ANN("example: 1.times { x = 1; x }");
676 F_ID(nd_vid
, RNODE_DVAR
, "local variable");
679 ANN("instance variable reference");
680 ANN("format: [nd_vid](ivar)");
682 F_ID(nd_vid
, RNODE_IVAR
, "instance variable");
685 ANN("constant reference");
686 ANN("format: [nd_vid](constant)");
688 F_ID(nd_vid
, RNODE_CONST
, "constant");
691 ANN("class variable reference");
692 ANN("format: [nd_vid](cvar)");
694 F_ID(nd_vid
, RNODE_CVAR
, "class variable");
698 ANN("global variable reference");
699 ANN("format: [nd_vid](gvar)");
701 F_ID(nd_vid
, RNODE_GVAR
, "global variable");
705 ANN("nth special variable reference");
706 ANN("format: $[nd_nth]");
707 ANN("example: $1, $2, ..");
708 F_CUSTOM1(nd_nth
, "variable") { A("$"); A_LONG(RNODE_NTH_REF(node
)->nd_nth
); }
712 ANN("back special variable reference");
713 ANN("format: $[nd_nth]");
714 ANN("example: $&, $`, $', $+");
715 F_CUSTOM1(nd_nth
, "variable") {
717 name
[1] = (char)RNODE_BACK_REF(node
)->nd_nth
;
723 ANN("match expression (against $_ implicitly)");
724 ANN("format: [nd_lit] (in condition)");
725 ANN("example: if /foo/; foo; end");
727 F_VALUE(string
, rb_node_regx_string_val(node
), "string");
731 ANN("match expression (regexp first)");
732 ANN("format: [nd_recv] =~ [nd_value]");
733 ANN("example: /foo/ =~ 'foo'");
734 F_NODE(nd_recv
, RNODE_MATCH2
, "regexp (receiver)");
735 if (!RNODE_MATCH2(node
)->nd_args
) LAST_NODE
;
736 F_NODE(nd_value
, RNODE_MATCH2
, "string (argument)");
737 if (RNODE_MATCH2(node
)->nd_args
) {
739 F_NODE(nd_args
, RNODE_MATCH2
, "named captures");
744 ANN("match expression (regexp second)");
745 ANN("format: [nd_recv] =~ [nd_value]");
746 ANN("example: 'foo' =~ /foo/");
747 F_NODE(nd_recv
, RNODE_MATCH3
, "string (receiver)");
749 F_NODE(nd_value
, RNODE_MATCH3
, "regexp (argument)");
753 ANN("string literal");
754 ANN("format: [nd_lit]");
755 ANN("example: 'foo'");
758 ANN("xstring literal");
759 ANN("format: [nd_lit]");
760 ANN("example: `foo`");
762 F_VALUE(string
, rb_node_str_string_val(node
), "literal");
766 ANN("integer literal");
767 ANN("format: [val]");
769 F_VALUE(val
, rb_node_integer_literal_val(node
), "val");
773 ANN("float literal");
774 ANN("format: [val]");
776 F_VALUE(val
, rb_node_float_literal_val(node
), "val");
780 ANN("rational number literal");
781 ANN("format: [val]");
783 F_VALUE(val
, rb_node_rational_literal_val(node
), "val");
787 ANN("complex number literal");
788 ANN("format: [val]");
790 F_VALUE(val
, rb_node_imaginary_literal_val(node
), "val");
794 ANN("regexp literal");
795 ANN("format: [string]");
796 ANN("example: /foo/");
798 F_VALUE(string
, rb_node_regx_string_val(node
), "string");
802 ANN("once evaluation");
803 ANN("format: [nd_body]");
804 ANN("example: /foo#{ bar }baz/o");
806 F_NODE(nd_body
, RNODE_ONCE
, "body");
810 ANN("string literal with interpolation");
811 ANN("format: [nd_lit]");
812 ANN("example: \"foo#{ bar }baz\"");
815 ANN("xstring literal with interpolation");
816 ANN("format: [nd_lit]");
817 ANN("example: `foo#{ bar }baz`");
820 ANN("regexp literal with interpolation");
821 ANN("format: [nd_lit]");
822 ANN("example: /foo#{ bar }baz/");
825 ANN("symbol literal with interpolation");
826 ANN("format: [nd_lit]");
827 ANN("example: :\"foo#{ bar }baz\"");
829 F_VALUE(string
, rb_node_dstr_string_val(node
), "preceding string");
830 if (!RNODE_DSTR(node
)->nd_next
) return;
831 F_NODE(nd_next
->nd_head
, RNODE_DSTR
, "interpolation");
833 F_NODE(nd_next
->nd_next
, RNODE_DSTR
, "tailing strings");
837 ANN("symbol literal");
838 ANN("format: [string]");
839 ANN("example: :foo");
840 F_VALUE(string
, rb_node_sym_string_val(node
), "string");
844 ANN("interpolation expression");
845 ANN("format: \"..#{ [nd_body] }..\"");
846 ANN("example: \"foo#{ bar }baz\"");
848 F_NODE(nd_body
, RNODE_EVSTR
, "body");
852 ANN("splat argument following arguments");
853 ANN("format: ..(*[nd_head], [nd_body..])");
854 ANN("example: foo(*ary, post_arg1, post_arg2)");
855 F_NODE(nd_head
, RNODE_ARGSCAT
, "preceding array");
857 F_NODE(nd_body
, RNODE_ARGSCAT
, "following array");
861 ANN("splat argument following one argument");
862 ANN("format: ..(*[nd_head], [nd_body])");
863 ANN("example: foo(*ary, post_arg)");
864 F_NODE(nd_head
, RNODE_ARGSPUSH
, "preceding array");
866 F_NODE(nd_body
, RNODE_ARGSPUSH
, "following element");
870 ANN("splat argument");
871 ANN("format: *[nd_head]");
872 ANN("example: foo(*ary)");
874 F_NODE(nd_head
, RNODE_SPLAT
, "splat'ed array");
877 case NODE_BLOCK_PASS
:
878 ANN("arguments with block argument");
879 ANN("format: ..([nd_head], &[nd_body])");
880 ANN("example: foo(x, &blk)");
881 F_NODE(nd_head
, RNODE_BLOCK_PASS
, "other arguments");
883 F_NODE(nd_body
, RNODE_BLOCK_PASS
, "block argument");
887 ANN("method definition");
888 ANN("format: def [nd_mid] [nd_defn]; end");
889 ANN("example: def foo; bar; end");
890 F_ID(nd_mid
, RNODE_DEFN
, "method name");
892 F_NODE(nd_defn
, RNODE_DEFN
, "method definition");
896 ANN("singleton method definition");
897 ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
898 ANN("example: def obj.foo; bar; end");
899 F_NODE(nd_recv
, RNODE_DEFS
, "receiver");
900 F_ID(nd_mid
, RNODE_DEFS
, "method name");
902 F_NODE(nd_defn
, RNODE_DEFS
, "method definition");
906 ANN("method alias statement");
907 ANN("format: alias [nd_1st] [nd_2nd]");
908 ANN("example: alias bar foo");
909 F_NODE(nd_1st
, RNODE_ALIAS
, "new name");
911 F_NODE(nd_2nd
, RNODE_ALIAS
, "old name");
915 ANN("global variable alias statement");
916 ANN("format: alias [nd_alias](gvar) [nd_orig](gvar)");
917 ANN("example: alias $y $x");
918 F_ID(nd_alias
, RNODE_VALIAS
, "new name");
919 F_ID(nd_orig
, RNODE_VALIAS
, "old name");
923 ANN("method undef statement");
924 ANN("format: undef [nd_undefs]");
925 ANN("example: undef foo");
927 F_ARRAY(nd_undefs
, RNODE_UNDEF
, "nd_undefs");
931 ANN("class definition");
932 ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
933 ANN("example: class C2 < C; ..; end");
934 F_NODE(nd_cpath
, RNODE_CLASS
, "class path");
935 F_NODE(nd_super
, RNODE_CLASS
, "superclass");
937 F_NODE(nd_body
, RNODE_CLASS
, "class definition");
941 ANN("module definition");
942 ANN("format: module [nd_cpath]; [nd_body]; end");
943 ANN("example: module M; ..; end");
944 F_NODE(nd_cpath
, RNODE_MODULE
, "module path");
946 F_NODE(nd_body
, RNODE_MODULE
, "module definition");
950 ANN("singleton class definition");
951 ANN("format: class << [nd_recv]; [nd_body]; end");
952 ANN("example: class << obj; ..; end");
953 F_NODE(nd_recv
, RNODE_SCLASS
, "receiver");
955 F_NODE(nd_body
, RNODE_SCLASS
, "singleton class definition");
959 ANN("scoped constant reference");
960 ANN("format: [nd_head]::[nd_mid]");
961 ANN("example: M::C");
962 F_ID(nd_mid
, RNODE_COLON2
, "constant name");
964 F_NODE(nd_head
, RNODE_COLON2
, "receiver");
968 ANN("top-level constant reference");
969 ANN("format: ::[nd_mid]");
970 ANN("example: ::Object");
971 F_ID(nd_mid
, RNODE_COLON3
, "constant name");
975 ANN("range constructor (incl.)");
976 ANN("format: [nd_beg]..[nd_end]");
977 ANN("example: 1..5");
980 ANN("range constructor (excl.)");
981 ANN("format: [nd_beg]...[nd_end]");
982 ANN("example: 1...5");
985 ANN("flip-flop condition (incl.)");
986 ANN("format: [nd_beg]..[nd_end]");
987 ANN("example: if (x==1)..(x==5); foo; end");
990 ANN("flip-flop condition (excl.)");
991 ANN("format: [nd_beg]...[nd_end]");
992 ANN("example: if (x==1)...(x==5); foo; end");
994 F_NODE(nd_beg
, RNODE_DOT2
, "begin");
996 F_NODE(nd_end
, RNODE_DOT2
, "end");
1001 ANN("format: self");
1002 ANN("example: self");
1003 F_CUSTOM1(nd_state
, "nd_state") {
1004 A_INT((int)RNODE_SELF(node
)->nd_state
);
1011 ANN("example: nil");
1016 ANN("format: true");
1017 ANN("example: true");
1022 ANN("format: false");
1023 ANN("example: false");
1027 ANN("virtual reference to $!");
1028 ANN("format: rescue => id");
1029 ANN("example: rescue => id");
1033 ANN("defined? expression");
1034 ANN("format: defined?([nd_head])");
1035 ANN("example: defined?(foo)");
1036 F_NODE(nd_head
, RNODE_DEFINED
, "expr");
1040 ANN("post-execution");
1041 ANN("format: END { [nd_body] }");
1042 ANN("example: END { foo }");
1044 F_NODE(nd_body
, RNODE_POSTEXE
, "END clause");
1048 ANN("attr assignment");
1049 ANN("format: [nd_recv].[nd_mid] = [nd_args]");
1050 ANN("example: struct.field = foo");
1051 F_NODE(nd_recv
, RNODE_ATTRASGN
, "receiver");
1052 F_ID(nd_mid
, RNODE_ATTRASGN
, "method name");
1054 F_NODE(nd_args
, RNODE_ATTRASGN
, "arguments");
1058 ANN("lambda expression");
1059 ANN("format: -> [nd_body]");
1060 ANN("example: -> { foo }");
1062 F_NODE(nd_body
, RNODE_LAMBDA
, "lambda clause");
1066 ANN("optional arguments");
1067 ANN("format: def method_name([nd_body=some], [nd_next..])");
1068 ANN("example: def foo(a, b=1, c); end");
1069 F_NODE(nd_body
, RNODE_OPT_ARG
, "body");
1071 F_NODE(nd_next
, RNODE_OPT_ARG
, "next");
1075 ANN("keyword arguments");
1076 ANN("format: def method_name([nd_body=some], [nd_next..])");
1077 ANN("example: def foo(a:1, b:2); end");
1078 F_NODE(nd_body
, RNODE_KW_ARG
, "body");
1080 F_NODE(nd_next
, RNODE_KW_ARG
, "next");
1084 ANN("post arguments");
1085 ANN("format: *[nd_1st], [nd_2nd..] = ..");
1086 ANN("example: a, *rest, z = foo");
1087 if (NODE_NAMED_REST_P(RNODE_POSTARG(node
)->nd_1st
)) {
1088 F_NODE(nd_1st
, RNODE_POSTARG
, "rest argument");
1091 F_MSG(nd_1st
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1094 F_NODE(nd_2nd
, RNODE_POSTARG
, "post arguments");
1098 ANN("method parameters");
1099 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])");
1100 ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
1101 F_INT(nd_ainfo
.pre_args_num
, RNODE_ARGS
, "count of mandatory (pre-)arguments");
1102 F_NODE(nd_ainfo
.pre_init
, RNODE_ARGS
, "initialization of (pre-)arguments");
1103 F_INT(nd_ainfo
.post_args_num
, RNODE_ARGS
, "count of mandatory post-arguments");
1104 F_NODE(nd_ainfo
.post_init
, RNODE_ARGS
, "initialization of post-arguments");
1105 F_ID(nd_ainfo
.first_post_arg
, RNODE_ARGS
, "first post argument");
1106 F_CUSTOM1(nd_ainfo
.rest_arg
, "rest argument") {
1107 if (RNODE_ARGS(node
)->nd_ainfo
.rest_arg
== NODE_SPECIAL_EXCESSIVE_COMMA
) {
1108 A("1 (excessed comma)");
1111 A_ID(RNODE_ARGS(node
)->nd_ainfo
.rest_arg
);
1114 F_ID(nd_ainfo
.block_arg
, RNODE_ARGS
, "block argument");
1115 F_NODE(nd_ainfo
.opt_args
, RNODE_ARGS
, "optional arguments");
1116 F_NODE(nd_ainfo
.kw_args
, RNODE_ARGS
, "keyword arguments");
1118 F_NODE(nd_ainfo
.kw_rest_arg
, RNODE_ARGS
, "keyword rest argument");
1123 ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
1124 F_CUSTOM1(nd_tbl
, "local table") {
1125 rb_ast_id_table_t
*tbl
= RNODE_SCOPE(node
)->nd_tbl
;
1127 int size
= tbl
? tbl
->size
: 0;
1128 if (size
== 0) A("(empty)");
1129 for (i
= 0; i
< size
; i
++) {
1130 A_ID(tbl
->ids
[i
]); if (i
< size
- 1) A(",");
1133 F_NODE(nd_args
, RNODE_SCOPE
, "arguments");
1135 F_NODE(nd_body
, RNODE_SCOPE
, "body");
1139 ANN("array pattern");
1140 ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
1141 F_NODE(nd_pconst
, RNODE_ARYPTN
, "constant");
1142 F_NODE(pre_args
, RNODE_ARYPTN
, "pre arguments");
1143 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node
)->rest_arg
)) {
1144 F_NODE(rest_arg
, RNODE_ARYPTN
, "rest argument");
1147 F_MSG(rest_arg
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1150 F_NODE(post_args
, RNODE_ARYPTN
, "post arguments");
1154 ANN("find pattern");
1155 ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
1156 F_NODE(nd_pconst
, RNODE_FNDPTN
, "constant");
1157 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node
)->pre_rest_arg
)) {
1158 F_NODE(pre_rest_arg
, RNODE_FNDPTN
, "pre rest argument");
1161 F_MSG(pre_rest_arg
, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1163 F_NODE(args
, RNODE_FNDPTN
, "arguments");
1166 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node
)->post_rest_arg
)) {
1167 F_NODE(post_rest_arg
, RNODE_FNDPTN
, "post rest argument");
1170 F_MSG(post_rest_arg
, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1175 ANN("hash pattern");
1176 ANN("format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
1177 F_NODE(nd_pconst
, RNODE_HSHPTN
, "constant");
1178 F_NODE(nd_pkwargs
, RNODE_HSHPTN
, "keyword arguments");
1180 if (RNODE_HSHPTN(node
)->nd_pkwrestarg
== NODE_SPECIAL_NO_REST_KEYWORD
) {
1181 F_MSG(nd_pkwrestarg
, "keyword rest argument", "NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
1184 F_NODE(nd_pkwrestarg
, RNODE_HSHPTN
, "keyword rest argument");
1190 ANN("format: [lineno]");
1191 ANN("example: __LINE__");
1196 ANN("format: [path]");
1197 ANN("example: __FILE__");
1198 F_VALUE(path
, rb_node_file_path_val(node
), "path");
1203 ANN("format: [enc]");
1204 ANN("example: __ENCODING__");
1205 F_VALUE(enc
, rb_node_encoding_val(node
), "enc");
1209 ANN("Broken input recovered by Error Tolerant mode");
1217 rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node
)));
1221 rb_parser_dump_tree(const NODE
*node
, int comment
)
1223 VALUE buf
= rb_str_new_cstr(
1224 "###########################################################\n"
1225 "## Do NOT use this node dump for any purpose other than ##\n"
1226 "## debug and research. Compatibility is not guaranteed. ##\n"
1227 "###########################################################\n\n"
1229 dump_node(buf
, rb_str_new_cstr("# "), comment
, node
);