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))
31 rb_str_catf(buf, "(%d,%d)-(%d,%d)", \
32 loc.beg_pos.lineno, loc.beg_pos.column, \
33 loc.end_pos.lineno, loc.end_pos.column)
34 #define A_NODE_HEADER(node, term) \
35 rb_str_catf(buf, "@ %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))%s"term, \
36 ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node), \
37 nd_first_lineno(node), nd_first_column(node), \
38 nd_last_lineno(node), nd_last_column(node), \
39 (nd_fl_newline(node) ? "*" : ""))
40 #define A_FIELD_HEADER(len, name, term) \
41 rb_str_catf(buf, "+- %.*s:"term, (len), (name))
42 #define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term))
44 #define D_NULL_NODE (A_INDENT, A("(null node)\n"))
45 #define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n"))
47 #define COMPOUND_FIELD(len, name) \
48 FIELD_BLOCK((D_FIELD_HEADER((len), (name), "\n"), D_INDENT), D_DEDENT)
50 #define COMPOUND_FIELD1(name, ann) \
51 COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \
52 FIELD_NAME_DESC(name, ann))
54 #define FIELD_NAME_DESC(name, ann) name " (" ann ")"
55 #define FIELD_NAME_LEN(name, ann) (int)( \
57 rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
59 #define SIMPLE_FIELD(len, name) \
60 FIELD_BLOCK(D_FIELD_HEADER((len), (name), " "), A("\n"))
62 #define FIELD_BLOCK(init, reset) \
63 for (init, field_flag = 1; \
64 field_flag; /* should be optimized away */ \
65 reset, field_flag = 0)
67 #define A_SHAREABILITY(shareability) \
68 switch (shareability) { \
69 case rb_parser_shareable_none: \
70 rb_str_cat_cstr(buf, "none"); \
72 case rb_parser_shareable_literal: \
73 rb_str_cat_cstr(buf, "literal"); \
75 case rb_parser_shareable_copy: \
76 rb_str_cat_cstr(buf, "experimental_copy"); \
78 case rb_parser_shareable_everything: \
79 rb_str_cat_cstr(buf, "experimental_everything"); \
83 #define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
84 #define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
85 #define F_ID(name, type, ann) SIMPLE_FIELD1(#name, ann) A_ID(type(node)->name)
86 #define F_INT(name, type, ann) SIMPLE_FIELD1(#name, ann) A_INT(type(node)->name)
87 #define F_LONG(name, type, ann) SIMPLE_FIELD1(#name, ann) A_LONG(type(node)->name)
88 #define F_LIT(name, type, ann) SIMPLE_FIELD1(#name, ann) A_LIT(type(node)->name)
89 #define F_VALUE(name, val, ann) SIMPLE_FIELD1(#name, ann) A_LIT(val)
90 #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
91 #define F_LOC(name, type) SIMPLE_FIELD1(#name, "") A_LOC(type(node)->name)
92 #define F_SHAREABILITY(name, type, ann) SIMPLE_FIELD1(#name, ann) A_SHAREABILITY(type(node)->name)
94 #define F_NODE(name, type, ann) \
95 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, RNODE(type(node)->name));}
97 #define F_NODE2(name, n, ann) \
98 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, n);}
100 #define F_ARRAY(name, type, ann) \
101 COMPOUND_FIELD1(#name, ann) {dump_parser_array(buf, indent, comment, type(node)->name);}
105 A_INDENT; A("| # " ann "\n"); \
108 #define LAST_NODE (next_indent = " ")
111 rb_dump_literal(VALUE lit
)
113 if (!RB_SPECIAL_CONST_P(lit
)) {
115 switch (RB_BUILTIN_TYPE(lit
)) {
116 case T_CLASS
: case T_MODULE
: case T_ICLASS
:
117 str
= rb_class_path(lit
);
118 if (RCLASS_SINGLETON_P(lit
)) {
119 str
= rb_sprintf("<%"PRIsVALUE
">", str
);
126 return rb_inspect(lit
);
130 add_indent(VALUE buf
, VALUE indent
)
136 add_id(VALUE buf
, ID id
)
142 VALUE str
= rb_id2str(id
);
147 rb_str_catf(buf
, "(internal variable: 0x%"PRIsVALUE
")", id
);
152 struct add_option_arg
{
157 static void dump_node(VALUE
, VALUE
, int, const NODE
*);
158 static const char default_indent
[] = "| ";
161 dump_array(VALUE buf
, VALUE indent
, int comment
, const NODE
*node
)
164 const char *next_indent
= default_indent
;
165 F_LONG(as
.nd_alen
, RNODE_LIST
, "length");
166 F_NODE(nd_head
, RNODE_LIST
, "element");
167 while (RNODE_LIST(node
)->nd_next
&& nd_type_p(RNODE_LIST(node
)->nd_next
, NODE_LIST
)) {
168 node
= RNODE_LIST(node
)->nd_next
;
169 F_NODE(nd_head
, RNODE_LIST
, "element");
172 F_NODE(nd_next
, RNODE_LIST
, "next element");
176 dump_parser_array(VALUE buf
, VALUE indent
, int comment
, const rb_parser_ary_t
*ary
)
179 const char *next_indent
= default_indent
;
181 if (ary
->data_type
!= PARSER_ARY_DATA_NODE
) {
182 rb_bug("unexpected rb_parser_ary_data_type: %d", ary
->data_type
);
185 F_CUSTOM1(length
, "length") { A_LONG(ary
->len
); }
186 for (long i
= 0; i
< ary
->len
; i
++) {
187 if (i
== ary
->len
- 1) LAST_NODE
;
189 rb_str_catf(buf
, "+- element (%s%ld):\n",
190 comment
? "statement #" : "", i
);
192 dump_node(buf
, indent
, comment
, ary
->data
[i
]);
198 dump_node(VALUE buf
, VALUE indent
, int comment
, const NODE
* node
)
202 const char *next_indent
= default_indent
;
212 type
= nd_type(node
);
215 ANN("statement sequence");
216 ANN("format: [nd_head]; ...; [nd_next]");
217 ANN("example: foo; bar");
221 rb_str_catf(buf
, "+- nd_head (%s%d):\n",
222 comment
? "statement #" : "", ++i
);
223 if (!RNODE_BLOCK(node
)->nd_next
) LAST_NODE
;
225 dump_node(buf
, indent
, comment
, RNODE_BLOCK(node
)->nd_head
);
227 } while (RNODE_BLOCK(node
)->nd_next
&&
228 nd_type_p(RNODE_BLOCK(node
)->nd_next
, NODE_BLOCK
) &&
229 (node
= RNODE_BLOCK(node
)->nd_next
, 1));
230 if (RNODE_BLOCK(node
)->nd_next
) {
232 F_NODE(nd_next
, RNODE_BLOCK
, "next block");
238 ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
239 ANN("example: if x == 1 then foo else bar end");
240 F_NODE(nd_cond
, RNODE_IF
, "condition expr");
241 F_NODE(nd_body
, RNODE_IF
, "then clause");
242 F_NODE(nd_else
, RNODE_IF
, "else clause");
243 F_LOC(if_keyword_loc
, RNODE_IF
);
244 F_LOC(then_keyword_loc
, RNODE_IF
);
246 F_LOC(end_keyword_loc
, RNODE_IF
);
250 ANN("unless statement");
251 ANN("format: unless [nd_cond] then [nd_body] else [nd_else] end");
252 ANN("example: unless x == 1 then foo else bar end");
253 F_NODE(nd_cond
, RNODE_UNLESS
, "condition expr");
254 F_NODE(nd_body
, RNODE_UNLESS
, "then clause");
255 F_NODE(nd_else
, RNODE_UNLESS
, "else clause");
256 F_LOC(keyword_loc
, RNODE_UNLESS
);
257 F_LOC(then_keyword_loc
, RNODE_UNLESS
);
259 F_LOC(end_keyword_loc
, RNODE_UNLESS
);
263 ANN("case statement");
264 ANN("format: case [nd_head]; [nd_body]; end");
265 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
266 F_NODE(nd_head
, RNODE_CASE
, "case expr");
267 F_NODE(nd_body
, RNODE_CASE
, "when clauses");
268 F_LOC(case_keyword_loc
, RNODE_CASE
);
270 F_LOC(end_keyword_loc
, RNODE_CASE
);
273 ANN("case statement with no head");
274 ANN("format: case; [nd_body]; end");
275 ANN("example: case; when 1; foo; when 2; bar; else baz; end");
276 F_NODE(nd_head
, RNODE_CASE2
, "case expr");
277 F_NODE(nd_body
, RNODE_CASE2
, "when clauses");
278 F_LOC(case_keyword_loc
, RNODE_CASE2
);
280 F_LOC(end_keyword_loc
, RNODE_CASE2
);
283 ANN("case statement (pattern matching)");
284 ANN("format: case [nd_head]; [nd_body]; end");
285 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
286 F_NODE(nd_head
, RNODE_CASE3
, "case expr");
287 F_NODE(nd_body
, RNODE_CASE3
, "in clauses");
288 F_LOC(case_keyword_loc
, RNODE_CASE3
);
290 F_LOC(end_keyword_loc
, RNODE_CASE3
);
295 ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
296 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
297 F_NODE(nd_head
, RNODE_WHEN
, "when value");
298 F_NODE(nd_body
, RNODE_WHEN
, "when body");
300 F_NODE(nd_next
, RNODE_WHEN
, "next when clause");
301 F_LOC(keyword_loc
, RNODE_WHEN
);
303 F_LOC(then_keyword_loc
, RNODE_WHEN
);
308 ANN("format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
309 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
310 F_NODE(nd_head
, RNODE_IN
, "in pattern");
311 F_NODE(nd_body
, RNODE_IN
, "in body");
313 F_NODE(nd_next
, RNODE_IN
, "next in clause");
317 ANN("while statement");
318 ANN("format: while [nd_cond]; [nd_body]; end");
319 ANN("example: while x == 1; foo; end");
322 ANN("until statement");
323 ANN("format: until [nd_cond]; [nd_body]; end");
324 ANN("example: until x == 1; foo; end");
326 F_CUSTOM1(nd_state
, "begin-end-while?") {
327 A_INT((int)RNODE_WHILE(node
)->nd_state
);
328 A((RNODE_WHILE(node
)->nd_state
== 1) ? " (while-end)" : " (begin-end-while)");
330 F_NODE(nd_cond
, RNODE_WHILE
, "condition");
331 F_NODE(nd_body
, RNODE_WHILE
, "body");
332 F_LOC(keyword_loc
, RNODE_WHILE
);
334 F_LOC(closing_loc
, RNODE_WHILE
);
338 ANN("method call with block");
339 ANN("format: [nd_iter] { [nd_body] }");
340 ANN("example: 3.times { foo }");
341 F_NODE(nd_iter
, RNODE_ITER
, "iteration receiver");
343 F_NODE(nd_body
, RNODE_ITER
, "body");
347 ANN("for statement");
348 ANN("format: for * in [nd_iter] do [nd_body] end");
349 ANN("example: for i in 1..3 do foo end");
350 F_NODE(nd_iter
, RNODE_FOR
, "iteration receiver");
351 F_NODE(nd_body
, RNODE_FOR
, "body");
352 F_LOC(for_keyword_loc
, RNODE_FOR
);
353 F_LOC(in_keyword_loc
, RNODE_FOR
);
354 F_LOC(do_keyword_loc
, RNODE_FOR
);
356 F_LOC(end_keyword_loc
, RNODE_FOR
);
360 ANN("vars of for statement with masgn");
361 ANN("format: for [nd_var] in ... do ... end");
362 ANN("example: for x, y in 1..3 do foo end");
364 F_NODE(nd_var
, RNODE_FOR_MASGN
, "var");
368 ANN("break statement");
369 ANN("format: break [nd_stts]");
370 ANN("example: break 1");
371 F_NODE(nd_stts
, RNODE_BREAK
, "value");
373 F_LOC(keyword_loc
, RNODE_BREAK
);
376 ANN("next statement");
377 ANN("format: next [nd_stts]");
378 ANN("example: next 1");
379 F_NODE(nd_stts
, RNODE_NEXT
, "value");
381 F_LOC(keyword_loc
, RNODE_NEXT
);
384 ANN("return statement");
385 ANN("format: return [nd_stts]");
386 ANN("example: return 1");
387 F_NODE(nd_stts
, RNODE_RETURN
, "value");
389 F_LOC(keyword_loc
, RNODE_RETURN
);
393 ANN("redo statement");
395 ANN("example: redo");
396 F_LOC(keyword_loc
, RNODE_REDO
);
400 ANN("retry statement");
401 ANN("format: retry");
402 ANN("example: retry");
406 ANN("begin statement");
407 ANN("format: begin; [nd_body]; end");
408 ANN("example: begin; 1; end");
410 F_NODE(nd_body
, RNODE_BEGIN
, "body");
414 ANN("rescue clause");
415 ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
416 ANN("example: begin; foo; rescue; bar; else; baz; end");
417 F_NODE(nd_head
, RNODE_RESCUE
, "body");
418 F_NODE(nd_resq
, RNODE_RESCUE
, "rescue clause list");
420 F_NODE(nd_else
, RNODE_RESCUE
, "rescue else clause");
424 ANN("rescue clause (cont'd)");
425 ANN("format: rescue [nd_args] (=> [nd_exc_var]); [nd_body]; (rescue) [nd_next]");
426 ANN("example: begin; foo; rescue; bar; else; baz; end");
427 F_NODE(nd_args
, RNODE_RESBODY
, "rescue exceptions");
428 F_NODE(nd_exc_var
, RNODE_RESBODY
, "exception variable");
429 F_NODE(nd_body
, RNODE_RESBODY
, "rescue clause");
431 F_NODE(nd_next
, RNODE_RESBODY
, "next rescue clause");
435 ANN("ensure clause");
436 ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
437 ANN("example: begin; foo; ensure; bar; end");
438 F_NODE(nd_head
, RNODE_ENSURE
, "body");
440 F_NODE(nd_ensr
, RNODE_ENSURE
, "ensure clause");
445 ANN("format: [nd_1st] && [nd_2nd]");
446 ANN("example: foo && bar");
450 ANN("format: [nd_1st] || [nd_2nd]");
451 ANN("example: foo || bar");
454 F_NODE(nd_1st
, RNODE_AND
, "left expr");
455 if (!RNODE_AND(node
)->nd_2nd
|| !nd_type_p(RNODE_AND(node
)->nd_2nd
, type
))
457 node
= RNODE_AND(node
)->nd_2nd
;
459 F_NODE(nd_2nd
, RNODE_AND
, "right expr");
461 F_LOC(operator_loc
, RNODE_AND
);
465 ANN("multiple assignment");
466 ANN("format: [nd_head], [nd_args] = [nd_value]");
467 ANN("example: a, b = foo");
468 F_NODE(nd_value
, RNODE_MASGN
, "rhsn");
469 F_NODE(nd_head
, RNODE_MASGN
, "lhsn");
470 if (NODE_NAMED_REST_P(RNODE_MASGN(node
)->nd_args
)) {
472 F_NODE(nd_args
, RNODE_MASGN
, "splatn");
475 F_MSG(nd_args
, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
480 ANN("local variable assignment");
481 ANN("format: [nd_vid](lvar) = [nd_value]");
482 ANN("example: x = foo");
483 F_ID(nd_vid
, RNODE_LASGN
, "local variable");
484 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node
)->nd_value
)) {
485 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
489 F_NODE(nd_value
, RNODE_LASGN
, "rvalue");
493 ANN("dynamic variable assignment");
494 ANN("format: [nd_vid](dvar) = [nd_value]");
495 ANN("example: x = nil; 1.times { x = foo }");
496 ANN("example: 1.times { x = foo }");
497 F_ID(nd_vid
, RNODE_DASGN
, "local variable");
498 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node
)->nd_value
)) {
499 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
503 F_NODE(nd_value
, RNODE_DASGN
, "rvalue");
507 ANN("instance variable assignment");
508 ANN("format: [nd_vid](ivar) = [nd_value]");
509 ANN("example: @x = foo");
510 F_ID(nd_vid
, RNODE_IASGN
, "instance variable");
512 F_NODE(nd_value
, RNODE_IASGN
, "rvalue");
515 ANN("class variable assignment");
516 ANN("format: [nd_vid](cvar) = [nd_value]");
517 ANN("example: @@x = foo");
518 F_ID(nd_vid
, RNODE_CVASGN
, "class variable");
520 F_NODE(nd_value
, RNODE_CVASGN
, "rvalue");
523 ANN("global variable assignment");
524 ANN("format: [nd_vid](gvar) = [nd_value]");
525 ANN("example: $x = foo");
526 F_ID(nd_vid
, RNODE_GASGN
, "global variable");
528 F_NODE(nd_value
, RNODE_GASGN
, "rvalue");
532 ANN("constant declaration");
533 ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
534 ANN("example: X = foo");
535 if (RNODE_CDECL(node
)->nd_vid
) {
536 F_ID(nd_vid
, RNODE_CDECL
, "constant");
537 F_MSG(nd_else
, "extension", "not used");
540 F_MSG(nd_vid
, "constant", "0 (see extension field)");
541 F_NODE(nd_else
, RNODE_CDECL
, "extension");
543 F_SHAREABILITY(shareability
, RNODE_CDECL
, "shareability");
545 F_NODE(nd_value
, RNODE_CDECL
, "rvalue");
549 ANN("array assignment with operator");
550 ANN("format: [nd_recv] [ [nd_index] ] [nd_mid]= [nd_rvalue]");
551 ANN("example: ary[1] += foo");
552 F_NODE(nd_recv
, RNODE_OP_ASGN1
, "receiver");
553 F_ID(nd_mid
, RNODE_OP_ASGN1
, "operator");
554 F_NODE(nd_index
, RNODE_OP_ASGN1
, "index");
555 F_NODE(nd_rvalue
, RNODE_OP_ASGN1
, "rvalue");
556 F_LOC(call_operator_loc
, RNODE_OP_ASGN1
);
557 F_LOC(opening_loc
, RNODE_OP_ASGN1
);
558 F_LOC(closing_loc
, RNODE_OP_ASGN1
);
560 F_LOC(binary_operator_loc
, RNODE_OP_ASGN1
);
564 ANN("attr assignment with operator");
565 ANN("format: [nd_recv].[nd_vid] [nd_mid]= [nd_value]");
566 ANN("example: struct.field += foo");
567 F_NODE(nd_recv
, RNODE_OP_ASGN2
, "receiver");
568 F_CUSTOM1(nd_vid
, "attr") {
569 if (RNODE_OP_ASGN2(node
)->nd_aid
) A("? ");
570 A_ID(RNODE_OP_ASGN2(node
)->nd_vid
);
572 F_ID(nd_mid
, RNODE_OP_ASGN2
, "operator");
573 F_NODE(nd_value
, RNODE_OP_ASGN2
, "rvalue");
574 F_LOC(call_operator_loc
, RNODE_OP_ASGN2
);
575 F_LOC(message_loc
, RNODE_OP_ASGN2
);
577 F_LOC(binary_operator_loc
, RNODE_OP_ASGN2
);
580 case NODE_OP_ASGN_AND
:
581 ANN("assignment with && operator");
582 ANN("format: [nd_head] &&= [nd_value]");
583 ANN("example: foo &&= bar");
585 case NODE_OP_ASGN_OR
:
586 ANN("assignment with || operator");
587 ANN("format: [nd_head] ||= [nd_value]");
588 ANN("example: foo ||= bar");
590 F_NODE(nd_head
, RNODE_OP_ASGN_AND
, "variable");
592 F_NODE(nd_value
, RNODE_OP_ASGN_AND
, "rvalue");
596 ANN("constant declaration with operator");
597 ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
598 ANN("example: A::B ||= 1");
599 F_NODE(nd_head
, RNODE_OP_CDECL
, "constant");
600 F_ID(nd_aid
, RNODE_OP_CDECL
, "operator");
601 F_SHAREABILITY(shareability
, RNODE_OP_CDECL
, "shareability");
603 F_NODE(nd_value
, RNODE_OP_CDECL
, "rvalue");
607 ANN("method invocation");
608 ANN("format: [nd_recv].[nd_mid]([nd_args])");
609 ANN("example: obj.foo(1)");
610 F_ID(nd_mid
, RNODE_CALL
, "method id");
611 F_NODE(nd_recv
, RNODE_CALL
, "receiver");
613 F_NODE(nd_args
, RNODE_CALL
, "arguments");
617 ANN("method invocation");
618 ANN("format: [nd_recv] [nd_mid] [nd_args]");
619 ANN("example: foo + bar");
620 F_ID(nd_mid
, RNODE_OPCALL
, "method id");
621 F_NODE(nd_recv
, RNODE_OPCALL
, "receiver");
623 F_NODE(nd_args
, RNODE_OPCALL
, "arguments");
627 ANN("function call");
628 ANN("format: [nd_mid]([nd_args])");
629 ANN("example: foo(1)");
630 F_ID(nd_mid
, RNODE_FCALL
, "method id");
632 F_NODE(nd_args
, RNODE_FCALL
, "arguments");
636 ANN("function call with no argument");
637 ANN("format: [nd_mid]");
639 F_ID(nd_mid
, RNODE_VCALL
, "method id");
643 ANN("safe method invocation");
644 ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
645 ANN("example: obj&.foo(1)");
646 F_ID(nd_mid
, RNODE_QCALL
, "method id");
647 F_NODE(nd_recv
, RNODE_QCALL
, "receiver");
649 F_NODE(nd_args
, RNODE_QCALL
, "arguments");
653 ANN("super invocation");
654 ANN("format: super [nd_args]");
655 ANN("example: super 1");
656 F_NODE(nd_args
, RNODE_SUPER
, "arguments");
657 F_LOC(keyword_loc
, RNODE_SUPER
);
658 F_LOC(lparen_loc
, RNODE_SUPER
);
660 F_LOC(rparen_loc
, RNODE_SUPER
);
664 ANN("super invocation with no argument");
665 ANN("format: super");
666 ANN("example: super");
670 ANN("list constructor");
671 ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
672 ANN("example: [1, 2, 3]");
673 dump_array(buf
, indent
, comment
, node
);
677 ANN("empty list constructor");
683 if (!RNODE_HASH(node
)->nd_brace
) {
684 ANN("keyword arguments");
685 ANN("format: [nd_head]");
686 ANN("example: a: 1, b: 2");
689 ANN("hash constructor");
690 ANN("format: { [nd_head] }");
691 ANN("example: { 1 => 2, 3 => 4 }");
693 F_CUSTOM1(nd_brace
, "keyword arguments or hash literal") {
694 switch (RNODE_HASH(node
)->nd_brace
) {
695 case 0: A("0 (keyword argument)"); break;
696 case 1: A("1 (hash literal)"); break;
700 F_NODE(nd_head
, RNODE_HASH
, "contents");
704 ANN("yield invocation");
705 ANN("format: yield [nd_head]");
706 ANN("example: yield 1");
707 F_NODE(nd_head
, RNODE_YIELD
, "arguments");
708 F_LOC(keyword_loc
, RNODE_YIELD
);
709 F_LOC(lparen_loc
, RNODE_YIELD
);
711 F_LOC(rparen_loc
, RNODE_YIELD
);
715 ANN("local variable reference");
716 ANN("format: [nd_vid](lvar)");
718 F_ID(nd_vid
, RNODE_LVAR
, "local variable");
721 ANN("dynamic variable reference");
722 ANN("format: [nd_vid](dvar)");
723 ANN("example: 1.times { x = 1; x }");
724 F_ID(nd_vid
, RNODE_DVAR
, "local variable");
727 ANN("instance variable reference");
728 ANN("format: [nd_vid](ivar)");
730 F_ID(nd_vid
, RNODE_IVAR
, "instance variable");
733 ANN("constant reference");
734 ANN("format: [nd_vid](constant)");
736 F_ID(nd_vid
, RNODE_CONST
, "constant");
739 ANN("class variable reference");
740 ANN("format: [nd_vid](cvar)");
742 F_ID(nd_vid
, RNODE_CVAR
, "class variable");
746 ANN("global variable reference");
747 ANN("format: [nd_vid](gvar)");
749 F_ID(nd_vid
, RNODE_GVAR
, "global variable");
753 ANN("nth special variable reference");
754 ANN("format: $[nd_nth]");
755 ANN("example: $1, $2, ..");
756 F_CUSTOM1(nd_nth
, "variable") { A("$"); A_LONG(RNODE_NTH_REF(node
)->nd_nth
); }
760 ANN("back special variable reference");
761 ANN("format: $[nd_nth]");
762 ANN("example: $&, $`, $', $+");
763 F_CUSTOM1(nd_nth
, "variable") {
765 name
[1] = (char)RNODE_BACK_REF(node
)->nd_nth
;
771 ANN("match expression (against $_ implicitly)");
772 ANN("format: [nd_lit] (in condition)");
773 ANN("example: if /foo/; foo; end");
775 F_VALUE(string
, rb_node_regx_string_val(node
), "string");
779 ANN("match expression (regexp first)");
780 ANN("format: [nd_recv] =~ [nd_value]");
781 ANN("example: /foo/ =~ 'foo'");
782 F_NODE(nd_recv
, RNODE_MATCH2
, "regexp (receiver)");
783 if (!RNODE_MATCH2(node
)->nd_args
) LAST_NODE
;
784 F_NODE(nd_value
, RNODE_MATCH2
, "string (argument)");
785 if (RNODE_MATCH2(node
)->nd_args
) {
787 F_NODE(nd_args
, RNODE_MATCH2
, "named captures");
792 ANN("match expression (regexp second)");
793 ANN("format: [nd_recv] =~ [nd_value]");
794 ANN("example: 'foo' =~ /foo/");
795 F_NODE(nd_recv
, RNODE_MATCH3
, "string (receiver)");
797 F_NODE(nd_value
, RNODE_MATCH3
, "regexp (argument)");
801 ANN("string literal");
802 ANN("format: [nd_lit]");
803 ANN("example: 'foo'");
806 ANN("xstring literal");
807 ANN("format: [nd_lit]");
808 ANN("example: `foo`");
810 F_VALUE(string
, rb_node_str_string_val(node
), "literal");
814 ANN("integer literal");
815 ANN("format: [val]");
817 F_VALUE(val
, rb_node_integer_literal_val(node
), "val");
821 ANN("float literal");
822 ANN("format: [val]");
824 F_VALUE(val
, rb_node_float_literal_val(node
), "val");
828 ANN("rational number literal");
829 ANN("format: [val]");
831 F_VALUE(val
, rb_node_rational_literal_val(node
), "val");
835 ANN("complex number literal");
836 ANN("format: [val]");
838 F_VALUE(val
, rb_node_imaginary_literal_val(node
), "val");
842 ANN("regexp literal");
843 ANN("format: [string]");
844 ANN("example: /foo/");
845 F_VALUE(string
, rb_node_regx_string_val(node
), "string");
846 F_LOC(opening_loc
, RNODE_REGX
);
847 F_LOC(content_loc
, RNODE_REGX
);
849 F_LOC(closing_loc
, RNODE_REGX
);
853 ANN("once evaluation");
854 ANN("format: [nd_body]");
855 ANN("example: /foo#{ bar }baz/o");
857 F_NODE(nd_body
, RNODE_ONCE
, "body");
861 ANN("string literal with interpolation");
862 ANN("format: [nd_lit]");
863 ANN("example: \"foo#{ bar }baz\"");
866 ANN("xstring literal with interpolation");
867 ANN("format: [nd_lit]");
868 ANN("example: `foo#{ bar }baz`");
871 ANN("regexp literal with interpolation");
872 ANN("format: [nd_lit]");
873 ANN("example: /foo#{ bar }baz/");
876 ANN("symbol literal with interpolation");
877 ANN("format: [nd_lit]");
878 ANN("example: :\"foo#{ bar }baz\"");
880 F_VALUE(string
, rb_node_dstr_string_val(node
), "preceding string");
881 if (!RNODE_DSTR(node
)->nd_next
) return;
882 F_NODE(nd_next
->nd_head
, RNODE_DSTR
, "interpolation");
884 F_NODE(nd_next
->nd_next
, RNODE_DSTR
, "tailing strings");
888 ANN("symbol literal");
889 ANN("format: [string]");
890 ANN("example: :foo");
891 F_VALUE(string
, rb_node_sym_string_val(node
), "string");
895 ANN("interpolation expression");
896 ANN("format: \"..#{ [nd_body] }..\"");
897 ANN("example: \"foo#{ bar }baz\"");
898 F_NODE(nd_body
, RNODE_EVSTR
, "body");
899 F_LOC(opening_loc
, RNODE_EVSTR
);
901 F_LOC(closing_loc
, RNODE_EVSTR
);
905 ANN("splat argument following arguments");
906 ANN("format: ..(*[nd_head], [nd_body..])");
907 ANN("example: foo(*ary, post_arg1, post_arg2)");
908 F_NODE(nd_head
, RNODE_ARGSCAT
, "preceding array");
910 F_NODE(nd_body
, RNODE_ARGSCAT
, "following array");
914 ANN("splat argument following one argument");
915 ANN("format: ..(*[nd_head], [nd_body])");
916 ANN("example: foo(*ary, post_arg)");
917 F_NODE(nd_head
, RNODE_ARGSPUSH
, "preceding array");
919 F_NODE(nd_body
, RNODE_ARGSPUSH
, "following element");
923 ANN("splat argument");
924 ANN("format: *[nd_head]");
925 ANN("example: foo(*ary)");
926 F_NODE(nd_head
, RNODE_SPLAT
, "splat'ed array");
928 F_LOC(operator_loc
, RNODE_SPLAT
);
931 case NODE_BLOCK_PASS
:
932 ANN("arguments with block argument");
933 ANN("format: ..([nd_head], &[nd_body])");
934 ANN("example: foo(x, &blk)");
935 F_CUSTOM1(forwarding
, "arguments forwarding or not") {
936 switch (RNODE_BLOCK_PASS(node
)->forwarding
) {
937 case 0: A("0 (no forwarding)"); break;
938 case 1: A("1 (forwarding)"); break;
941 F_NODE(nd_head
, RNODE_BLOCK_PASS
, "other arguments");
942 F_NODE(nd_body
, RNODE_BLOCK_PASS
, "block argument");
944 F_LOC(operator_loc
, RNODE_BLOCK_PASS
);
948 ANN("method definition");
949 ANN("format: def [nd_mid] [nd_defn]; end");
950 ANN("example: def foo; bar; end");
951 F_ID(nd_mid
, RNODE_DEFN
, "method name");
953 F_NODE(nd_defn
, RNODE_DEFN
, "method definition");
957 ANN("singleton method definition");
958 ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
959 ANN("example: def obj.foo; bar; end");
960 F_NODE(nd_recv
, RNODE_DEFS
, "receiver");
961 F_ID(nd_mid
, RNODE_DEFS
, "method name");
963 F_NODE(nd_defn
, RNODE_DEFS
, "method definition");
967 ANN("method alias statement");
968 ANN("format: alias [nd_1st] [nd_2nd]");
969 ANN("example: alias bar foo");
970 F_NODE(nd_1st
, RNODE_ALIAS
, "new name");
971 F_NODE(nd_2nd
, RNODE_ALIAS
, "old name");
973 F_LOC(keyword_loc
, RNODE_ALIAS
);
977 ANN("global variable alias statement");
978 ANN("format: alias [nd_alias](gvar) [nd_orig](gvar)");
979 ANN("example: alias $y $x");
980 F_ID(nd_alias
, RNODE_VALIAS
, "new name");
981 F_ID(nd_orig
, RNODE_VALIAS
, "old name");
982 F_LOC(keyword_loc
, RNODE_VALIAS
);
986 ANN("method undef statement");
987 ANN("format: undef [nd_undefs]");
988 ANN("example: undef foo");
990 F_ARRAY(nd_undefs
, RNODE_UNDEF
, "nd_undefs");
991 F_LOC(keyword_loc
, RNODE_UNDEF
);
995 ANN("class definition");
996 ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
997 ANN("example: class C2 < C; ..; end");
998 F_NODE(nd_cpath
, RNODE_CLASS
, "class path");
999 F_NODE(nd_super
, RNODE_CLASS
, "superclass");
1000 F_NODE(nd_body
, RNODE_CLASS
, "class definition");
1001 F_LOC(class_keyword_loc
, RNODE_CLASS
);
1002 F_LOC(inheritance_operator_loc
, RNODE_CLASS
);
1004 F_LOC(end_keyword_loc
, RNODE_CLASS
);
1008 ANN("module definition");
1009 ANN("format: module [nd_cpath]; [nd_body]; end");
1010 ANN("example: module M; ..; end");
1011 F_NODE(nd_cpath
, RNODE_MODULE
, "module path");
1013 F_NODE(nd_body
, RNODE_MODULE
, "module definition");
1017 ANN("singleton class definition");
1018 ANN("format: class << [nd_recv]; [nd_body]; end");
1019 ANN("example: class << obj; ..; end");
1020 F_NODE(nd_recv
, RNODE_SCLASS
, "receiver");
1022 F_NODE(nd_body
, RNODE_SCLASS
, "singleton class definition");
1026 ANN("scoped constant reference");
1027 ANN("format: [nd_head]::[nd_mid]");
1028 ANN("example: M::C");
1029 F_ID(nd_mid
, RNODE_COLON2
, "constant name");
1031 F_NODE(nd_head
, RNODE_COLON2
, "receiver");
1035 ANN("top-level constant reference");
1036 ANN("format: ::[nd_mid]");
1037 ANN("example: ::Object");
1038 F_ID(nd_mid
, RNODE_COLON3
, "constant name");
1042 ANN("range constructor (incl.)");
1043 ANN("format: [nd_beg]..[nd_end]");
1044 ANN("example: 1..5");
1047 ANN("range constructor (excl.)");
1048 ANN("format: [nd_beg]...[nd_end]");
1049 ANN("example: 1...5");
1052 ANN("flip-flop condition (incl.)");
1053 ANN("format: [nd_beg]..[nd_end]");
1054 ANN("example: if (x==1)..(x==5); foo; end");
1057 ANN("flip-flop condition (excl.)");
1058 ANN("format: [nd_beg]...[nd_end]");
1059 ANN("example: if (x==1)...(x==5); foo; end");
1061 F_NODE(nd_beg
, RNODE_DOT2
, "begin");
1062 F_NODE(nd_end
, RNODE_DOT2
, "end");
1064 F_LOC(operator_loc
, RNODE_DOT2
);
1069 ANN("format: self");
1070 ANN("example: self");
1071 F_CUSTOM1(nd_state
, "nd_state") {
1072 A_INT((int)RNODE_SELF(node
)->nd_state
);
1079 ANN("example: nil");
1084 ANN("format: true");
1085 ANN("example: true");
1090 ANN("format: false");
1091 ANN("example: false");
1095 ANN("virtual reference to $!");
1096 ANN("format: rescue => id");
1097 ANN("example: rescue => id");
1101 ANN("defined? expression");
1102 ANN("format: defined?([nd_head])");
1103 ANN("example: defined?(foo)");
1105 F_NODE(nd_head
, RNODE_DEFINED
, "expr");
1109 ANN("post-execution");
1110 ANN("format: END { [nd_body] }");
1111 ANN("example: END { foo }");
1112 F_NODE(nd_body
, RNODE_POSTEXE
, "END clause");
1113 F_LOC(keyword_loc
, RNODE_POSTEXE
);
1114 F_LOC(opening_loc
, RNODE_POSTEXE
);
1116 F_LOC(closing_loc
, RNODE_POSTEXE
);
1120 ANN("attr assignment");
1121 ANN("format: [nd_recv].[nd_mid] = [nd_args]");
1122 ANN("example: struct.field = foo");
1123 F_NODE(nd_recv
, RNODE_ATTRASGN
, "receiver");
1124 F_ID(nd_mid
, RNODE_ATTRASGN
, "method name");
1126 F_NODE(nd_args
, RNODE_ATTRASGN
, "arguments");
1130 ANN("lambda expression");
1131 ANN("format: -> [nd_body]");
1132 ANN("example: -> { foo }");
1133 F_NODE(nd_body
, RNODE_LAMBDA
, "lambda clause");
1134 F_LOC(operator_loc
, RNODE_LAMBDA
);
1135 F_LOC(opening_loc
, RNODE_LAMBDA
);
1137 F_LOC(closing_loc
, RNODE_LAMBDA
);
1141 ANN("optional arguments");
1142 ANN("format: def method_name([nd_body=some], [nd_next..])");
1143 ANN("example: def foo(a, b=1, c); end");
1144 F_NODE(nd_body
, RNODE_OPT_ARG
, "body");
1146 F_NODE(nd_next
, RNODE_OPT_ARG
, "next");
1150 ANN("keyword arguments");
1151 ANN("format: def method_name([nd_body=some], [nd_next..])");
1152 ANN("example: def foo(a:1, b:2); end");
1153 F_NODE(nd_body
, RNODE_KW_ARG
, "body");
1155 F_NODE(nd_next
, RNODE_KW_ARG
, "next");
1159 ANN("post arguments");
1160 ANN("format: *[nd_1st], [nd_2nd..] = ..");
1161 ANN("example: a, *rest, z = foo");
1162 if (NODE_NAMED_REST_P(RNODE_POSTARG(node
)->nd_1st
)) {
1163 F_NODE(nd_1st
, RNODE_POSTARG
, "rest argument");
1166 F_MSG(nd_1st
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1169 F_NODE(nd_2nd
, RNODE_POSTARG
, "post arguments");
1173 ANN("method parameters");
1174 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])");
1175 ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
1176 F_CUSTOM1(nd_ainfo
.forwarding
, "arguments forwarding or not") {
1177 switch (RNODE_ARGS(node
)->nd_ainfo
.forwarding
) {
1178 case 0: A("0 (no forwarding)"); break;
1179 case 1: A("1 (forwarding)"); break;
1182 F_INT(nd_ainfo
.pre_args_num
, RNODE_ARGS
, "count of mandatory (pre-)arguments");
1183 F_NODE(nd_ainfo
.pre_init
, RNODE_ARGS
, "initialization of (pre-)arguments");
1184 F_INT(nd_ainfo
.post_args_num
, RNODE_ARGS
, "count of mandatory post-arguments");
1185 F_NODE(nd_ainfo
.post_init
, RNODE_ARGS
, "initialization of post-arguments");
1186 F_ID(nd_ainfo
.first_post_arg
, RNODE_ARGS
, "first post argument");
1187 F_CUSTOM1(nd_ainfo
.rest_arg
, "rest argument") {
1188 if (RNODE_ARGS(node
)->nd_ainfo
.rest_arg
== NODE_SPECIAL_EXCESSIVE_COMMA
) {
1189 A("1 (excessed comma)");
1192 A_ID(RNODE_ARGS(node
)->nd_ainfo
.rest_arg
);
1195 F_ID(nd_ainfo
.block_arg
, RNODE_ARGS
, "block argument");
1196 F_NODE(nd_ainfo
.opt_args
, RNODE_ARGS
, "optional arguments");
1197 F_NODE(nd_ainfo
.kw_args
, RNODE_ARGS
, "keyword arguments");
1199 F_NODE(nd_ainfo
.kw_rest_arg
, RNODE_ARGS
, "keyword rest argument");
1204 ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
1205 F_CUSTOM1(nd_tbl
, "local table") {
1206 rb_ast_id_table_t
*tbl
= RNODE_SCOPE(node
)->nd_tbl
;
1208 int size
= tbl
? tbl
->size
: 0;
1209 if (size
== 0) A("(empty)");
1210 for (i
= 0; i
< size
; i
++) {
1211 A_ID(tbl
->ids
[i
]); if (i
< size
- 1) A(",");
1214 F_NODE(nd_args
, RNODE_SCOPE
, "arguments");
1216 F_NODE(nd_body
, RNODE_SCOPE
, "body");
1220 ANN("array pattern");
1221 ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
1222 F_NODE(nd_pconst
, RNODE_ARYPTN
, "constant");
1223 F_NODE(pre_args
, RNODE_ARYPTN
, "pre arguments");
1224 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node
)->rest_arg
)) {
1225 F_NODE(rest_arg
, RNODE_ARYPTN
, "rest argument");
1228 F_MSG(rest_arg
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1231 F_NODE(post_args
, RNODE_ARYPTN
, "post arguments");
1235 ANN("find pattern");
1236 ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
1237 F_NODE(nd_pconst
, RNODE_FNDPTN
, "constant");
1238 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node
)->pre_rest_arg
)) {
1239 F_NODE(pre_rest_arg
, RNODE_FNDPTN
, "pre rest argument");
1242 F_MSG(pre_rest_arg
, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1244 F_NODE(args
, RNODE_FNDPTN
, "arguments");
1247 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node
)->post_rest_arg
)) {
1248 F_NODE(post_rest_arg
, RNODE_FNDPTN
, "post rest argument");
1251 F_MSG(post_rest_arg
, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1256 ANN("hash pattern");
1257 ANN("format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
1258 F_NODE(nd_pconst
, RNODE_HSHPTN
, "constant");
1259 F_NODE(nd_pkwargs
, RNODE_HSHPTN
, "keyword arguments");
1261 if (RNODE_HSHPTN(node
)->nd_pkwrestarg
== NODE_SPECIAL_NO_REST_KEYWORD
) {
1262 F_MSG(nd_pkwrestarg
, "keyword rest argument", "NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
1265 F_NODE(nd_pkwrestarg
, RNODE_HSHPTN
, "keyword rest argument");
1271 ANN("format: [lineno]");
1272 ANN("example: __LINE__");
1277 ANN("format: [path]");
1278 ANN("example: __FILE__");
1279 F_VALUE(path
, rb_node_file_path_val(node
), "path");
1284 ANN("format: [enc]");
1285 ANN("example: __ENCODING__");
1286 F_VALUE(enc
, rb_node_encoding_val(node
), "enc");
1290 ANN("Broken input recovered by Error Tolerant mode");
1298 rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node
)));
1302 rb_parser_dump_tree(const NODE
*node
, int comment
)
1304 VALUE buf
= rb_str_new_cstr(
1305 "###########################################################\n"
1306 "## Do NOT use this node dump for any purpose other than ##\n"
1307 "## debug and research. Compatibility is not guaranteed. ##\n"
1308 "###########################################################\n\n"
1310 dump_node(buf
, rb_str_new_cstr("# "), comment
, node
);