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);}
97 A_INDENT; A("| # " ann "\n"); \
100 #define LAST_NODE (next_indent = " ")
103 rb_dump_literal(VALUE lit
)
105 if (!RB_SPECIAL_CONST_P(lit
)) {
107 switch (RB_BUILTIN_TYPE(lit
)) {
108 case T_CLASS
: case T_MODULE
: case T_ICLASS
:
109 str
= rb_class_path(lit
);
110 if (RCLASS_SINGLETON_P(lit
)) {
111 str
= rb_sprintf("<%"PRIsVALUE
">", str
);
118 return rb_inspect(lit
);
122 add_indent(VALUE buf
, VALUE indent
)
128 add_id(VALUE buf
, ID id
)
134 VALUE str
= rb_id2str(id
);
139 rb_str_catf(buf
, "(internal variable: 0x%"PRIsVALUE
")", id
);
144 struct add_option_arg
{
149 static void dump_node(VALUE
, VALUE
, int, const NODE
*);
150 static const char default_indent
[] = "| ";
153 dump_array(VALUE buf
, VALUE indent
, int comment
, const NODE
*node
)
156 const char *next_indent
= default_indent
;
157 F_LONG(as
.nd_alen
, RNODE_LIST
, "length");
158 F_NODE(nd_head
, RNODE_LIST
, "element");
159 while (RNODE_LIST(node
)->nd_next
&& nd_type_p(RNODE_LIST(node
)->nd_next
, NODE_LIST
)) {
160 node
= RNODE_LIST(node
)->nd_next
;
161 F_NODE(nd_head
, RNODE_LIST
, "element");
164 F_NODE(nd_next
, RNODE_LIST
, "next element");
168 dump_node(VALUE buf
, VALUE indent
, int comment
, const NODE
* node
)
172 const char *next_indent
= default_indent
;
182 type
= nd_type(node
);
185 ANN("statement sequence");
186 ANN("format: [nd_head]; ...; [nd_next]");
187 ANN("example: foo; bar");
191 rb_str_catf(buf
, "+- nd_head (%s%d):\n",
192 comment
? "statement #" : "", ++i
);
193 if (!RNODE_BLOCK(node
)->nd_next
) LAST_NODE
;
195 dump_node(buf
, indent
, comment
, RNODE_BLOCK(node
)->nd_head
);
197 } while (RNODE_BLOCK(node
)->nd_next
&&
198 nd_type_p(RNODE_BLOCK(node
)->nd_next
, NODE_BLOCK
) &&
199 (node
= RNODE_BLOCK(node
)->nd_next
, 1));
200 if (RNODE_BLOCK(node
)->nd_next
) {
202 F_NODE(nd_next
, RNODE_BLOCK
, "next block");
208 ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
209 ANN("example: if x == 1 then foo else bar end");
210 F_NODE(nd_cond
, RNODE_IF
, "condition expr");
211 F_NODE(nd_body
, RNODE_IF
, "then clause");
213 F_NODE(nd_else
, RNODE_IF
, "else clause");
217 ANN("unless statement");
218 ANN("format: unless [nd_cond] then [nd_body] else [nd_else] end");
219 ANN("example: unless x == 1 then foo else bar end");
220 F_NODE(nd_cond
, RNODE_UNLESS
, "condition expr");
221 F_NODE(nd_body
, RNODE_UNLESS
, "then clause");
223 F_NODE(nd_else
, RNODE_UNLESS
, "else clause");
227 ANN("case statement");
228 ANN("format: case [nd_head]; [nd_body]; end");
229 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
230 F_NODE(nd_head
, RNODE_CASE
, "case expr");
232 F_NODE(nd_body
, RNODE_CASE
, "when clauses");
235 ANN("case statement with no head");
236 ANN("format: case; [nd_body]; end");
237 ANN("example: case; when 1; foo; when 2; bar; else baz; end");
238 F_NODE(nd_head
, RNODE_CASE2
, "case expr");
240 F_NODE(nd_body
, RNODE_CASE2
, "when clauses");
243 ANN("case statement (pattern matching)");
244 ANN("format: case [nd_head]; [nd_body]; end");
245 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
246 F_NODE(nd_head
, RNODE_CASE3
, "case expr");
248 F_NODE(nd_body
, RNODE_CASE3
, "in clauses");
253 ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
254 ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
255 F_NODE(nd_head
, RNODE_WHEN
, "when value");
256 F_NODE(nd_body
, RNODE_WHEN
, "when body");
258 F_NODE(nd_next
, RNODE_WHEN
, "next when clause");
263 ANN("format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
264 ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
265 F_NODE(nd_head
, RNODE_IN
, "in pattern");
266 F_NODE(nd_body
, RNODE_IN
, "in body");
268 F_NODE(nd_next
, RNODE_IN
, "next in clause");
272 ANN("while statement");
273 ANN("format: while [nd_cond]; [nd_body]; end");
274 ANN("example: while x == 1; foo; end");
277 ANN("until statement");
278 ANN("format: until [nd_cond]; [nd_body]; end");
279 ANN("example: until x == 1; foo; end");
281 F_CUSTOM1(nd_state
, "begin-end-while?") {
282 A_INT((int)RNODE_WHILE(node
)->nd_state
);
283 A((RNODE_WHILE(node
)->nd_state
== 1) ? " (while-end)" : " (begin-end-while)");
285 F_NODE(nd_cond
, RNODE_WHILE
, "condition");
287 F_NODE(nd_body
, RNODE_WHILE
, "body");
291 ANN("method call with block");
292 ANN("format: [nd_iter] { [nd_body] }");
293 ANN("example: 3.times { foo }");
296 ANN("for statement");
297 ANN("format: for * in [nd_iter] do [nd_body] end");
298 ANN("example: for i in 1..3 do foo end");
300 F_NODE(nd_iter
, RNODE_ITER
, "iteration receiver");
302 F_NODE(nd_body
, RNODE_ITER
, "body");
306 ANN("vars of for statement with masgn");
307 ANN("format: for [nd_var] in ... do ... end");
308 ANN("example: for x, y in 1..3 do foo end");
310 F_NODE(nd_var
, RNODE_FOR_MASGN
, "var");
314 ANN("break statement");
315 ANN("format: break [nd_stts]");
316 ANN("example: break 1");
318 F_NODE(nd_stts
, RNODE_BREAK
, "value");
321 ANN("next statement");
322 ANN("format: next [nd_stts]");
323 ANN("example: next 1");
325 F_NODE(nd_stts
, RNODE_NEXT
, "value");
328 ANN("return statement");
329 ANN("format: return [nd_stts]");
330 ANN("example: return 1");
332 F_NODE(nd_stts
, RNODE_RETURN
, "value");
336 ANN("redo statement");
338 ANN("example: redo");
342 ANN("retry statement");
343 ANN("format: retry");
344 ANN("example: retry");
348 ANN("begin statement");
349 ANN("format: begin; [nd_body]; end");
350 ANN("example: begin; 1; end");
352 F_NODE(nd_body
, RNODE_BEGIN
, "body");
356 ANN("rescue clause");
357 ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
358 ANN("example: begin; foo; rescue; bar; else; baz; end");
359 F_NODE(nd_head
, RNODE_RESCUE
, "body");
360 F_NODE(nd_resq
, RNODE_RESCUE
, "rescue clause list");
362 F_NODE(nd_else
, RNODE_RESCUE
, "rescue else clause");
366 ANN("rescue clause (cont'd)");
367 ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
368 ANN("example: begin; foo; rescue; bar; else; baz; end");
369 F_NODE(nd_args
, RNODE_RESBODY
, "rescue exceptions");
370 F_NODE(nd_body
, RNODE_RESBODY
, "rescue clause");
372 F_NODE(nd_next
, RNODE_RESBODY
, "next rescue clause");
376 ANN("ensure clause");
377 ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
378 ANN("example: begin; foo; ensure; bar; end");
379 F_NODE(nd_head
, RNODE_ENSURE
, "body");
381 F_NODE(nd_ensr
, RNODE_ENSURE
, "ensure clause");
386 ANN("format: [nd_1st] && [nd_2nd]");
387 ANN("example: foo && bar");
391 ANN("format: [nd_1st] || [nd_2nd]");
392 ANN("example: foo || bar");
395 F_NODE(nd_1st
, RNODE_AND
, "left expr");
396 if (!RNODE_AND(node
)->nd_2nd
|| !nd_type_p(RNODE_AND(node
)->nd_2nd
, type
))
398 node
= RNODE_AND(node
)->nd_2nd
;
401 F_NODE(nd_2nd
, RNODE_AND
, "right expr");
405 ANN("multiple assignment");
406 ANN("format: [nd_head], [nd_args] = [nd_value]");
407 ANN("example: a, b = foo");
408 F_NODE(nd_value
, RNODE_MASGN
, "rhsn");
409 F_NODE(nd_head
, RNODE_MASGN
, "lhsn");
410 if (NODE_NAMED_REST_P(RNODE_MASGN(node
)->nd_args
)) {
412 F_NODE(nd_args
, RNODE_MASGN
, "splatn");
415 F_MSG(nd_args
, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
420 ANN("local variable assignment");
421 ANN("format: [nd_vid](lvar) = [nd_value]");
422 ANN("example: x = foo");
423 F_ID(nd_vid
, RNODE_LASGN
, "local variable");
424 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node
)->nd_value
)) {
425 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
429 F_NODE(nd_value
, RNODE_LASGN
, "rvalue");
433 ANN("dynamic variable assignment");
434 ANN("format: [nd_vid](dvar) = [nd_value]");
435 ANN("example: x = nil; 1.times { x = foo }");
436 ANN("example: 1.times { x = foo }");
437 F_ID(nd_vid
, RNODE_DASGN
, "local variable");
438 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node
)->nd_value
)) {
439 F_MSG(nd_value
, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
443 F_NODE(nd_value
, RNODE_DASGN
, "rvalue");
447 ANN("instance variable assignment");
448 ANN("format: [nd_vid](ivar) = [nd_value]");
449 ANN("example: @x = foo");
450 F_ID(nd_vid
, RNODE_IASGN
, "instance variable");
452 F_NODE(nd_value
, RNODE_IASGN
, "rvalue");
455 ANN("class variable assignment");
456 ANN("format: [nd_vid](cvar) = [nd_value]");
457 ANN("example: @@x = foo");
458 F_ID(nd_vid
, RNODE_CVASGN
, "class variable");
460 F_NODE(nd_value
, RNODE_CVASGN
, "rvalue");
463 ANN("global variable assignment");
464 ANN("format: [nd_vid](gvar) = [nd_value]");
465 ANN("example: $x = foo");
466 F_ID(nd_vid
, RNODE_GASGN
, "global variable");
468 F_NODE(nd_value
, RNODE_GASGN
, "rvalue");
472 ANN("constant declaration");
473 ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
474 ANN("example: X = foo");
475 if (RNODE_CDECL(node
)->nd_vid
) {
476 F_ID(nd_vid
, RNODE_CDECL
, "constant");
477 F_MSG(nd_else
, "extension", "not used");
480 F_MSG(nd_vid
, "constant", "0 (see extension field)");
481 F_NODE(nd_else
, RNODE_CDECL
, "extension");
483 F_SHAREABILITY(shareability
, RNODE_CDECL
, "shareability");
485 F_NODE(nd_value
, RNODE_CDECL
, "rvalue");
489 ANN("array assignment with operator");
490 ANN("format: [nd_recv] [ [nd_index] ] [nd_mid]= [nd_rvalue]");
491 ANN("example: ary[1] += foo");
492 F_NODE(nd_recv
, RNODE_OP_ASGN1
, "receiver");
493 F_ID(nd_mid
, RNODE_OP_ASGN1
, "operator");
494 F_NODE(nd_index
, RNODE_OP_ASGN1
, "index");
496 F_NODE(nd_rvalue
, RNODE_OP_ASGN1
, "rvalue");
500 ANN("attr assignment with operator");
501 ANN("format: [nd_recv].[nd_vid] [nd_mid]= [nd_value]");
502 ANN("example: struct.field += foo");
503 F_NODE(nd_recv
, RNODE_OP_ASGN2
, "receiver");
504 F_CUSTOM1(nd_vid
, "attr") {
505 if (RNODE_OP_ASGN2(node
)->nd_aid
) A("? ");
506 A_ID(RNODE_OP_ASGN2(node
)->nd_vid
);
508 F_ID(nd_mid
, RNODE_OP_ASGN2
, "operator");
510 F_NODE(nd_value
, RNODE_OP_ASGN2
, "rvalue");
513 case NODE_OP_ASGN_AND
:
514 ANN("assignment with && operator");
515 ANN("format: [nd_head] &&= [nd_value]");
516 ANN("example: foo &&= bar");
518 case NODE_OP_ASGN_OR
:
519 ANN("assignment with || operator");
520 ANN("format: [nd_head] ||= [nd_value]");
521 ANN("example: foo ||= bar");
523 F_NODE(nd_head
, RNODE_OP_ASGN_AND
, "variable");
525 F_NODE(nd_value
, RNODE_OP_ASGN_AND
, "rvalue");
529 ANN("constant declaration with operator");
530 ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
531 ANN("example: A::B ||= 1");
532 F_NODE(nd_head
, RNODE_OP_CDECL
, "constant");
533 F_ID(nd_aid
, RNODE_OP_CDECL
, "operator");
534 F_SHAREABILITY(shareability
, RNODE_OP_CDECL
, "shareability");
536 F_NODE(nd_value
, RNODE_OP_CDECL
, "rvalue");
540 ANN("method invocation");
541 ANN("format: [nd_recv].[nd_mid]([nd_args])");
542 ANN("example: obj.foo(1)");
543 F_ID(nd_mid
, RNODE_CALL
, "method id");
544 F_NODE(nd_recv
, RNODE_CALL
, "receiver");
546 F_NODE(nd_args
, RNODE_CALL
, "arguments");
550 ANN("method invocation");
551 ANN("format: [nd_recv] [nd_mid] [nd_args]");
552 ANN("example: foo + bar");
553 F_ID(nd_mid
, RNODE_OPCALL
, "method id");
554 F_NODE(nd_recv
, RNODE_OPCALL
, "receiver");
556 F_NODE(nd_args
, RNODE_OPCALL
, "arguments");
560 ANN("function call");
561 ANN("format: [nd_mid]([nd_args])");
562 ANN("example: foo(1)");
563 F_ID(nd_mid
, RNODE_FCALL
, "method id");
565 F_NODE(nd_args
, RNODE_FCALL
, "arguments");
569 ANN("function call with no argument");
570 ANN("format: [nd_mid]");
572 F_ID(nd_mid
, RNODE_VCALL
, "method id");
576 ANN("safe method invocation");
577 ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
578 ANN("example: obj&.foo(1)");
579 F_ID(nd_mid
, RNODE_QCALL
, "method id");
580 F_NODE(nd_recv
, RNODE_QCALL
, "receiver");
582 F_NODE(nd_args
, RNODE_QCALL
, "arguments");
586 ANN("super invocation");
587 ANN("format: super [nd_args]");
588 ANN("example: super 1");
590 F_NODE(nd_args
, RNODE_SUPER
, "arguments");
594 ANN("super invocation with no argument");
595 ANN("format: super");
596 ANN("example: super");
600 ANN("list constructor");
601 ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
602 ANN("example: [1, 2, 3]");
603 dump_array(buf
, indent
, comment
, node
);
607 ANN("empty list constructor");
613 if (!RNODE_HASH(node
)->nd_brace
) {
614 ANN("keyword arguments");
615 ANN("format: [nd_head]");
616 ANN("example: a: 1, b: 2");
619 ANN("hash constructor");
620 ANN("format: { [nd_head] }");
621 ANN("example: { 1 => 2, 3 => 4 }");
623 F_CUSTOM1(nd_brace
, "keyword arguments or hash literal") {
624 switch (RNODE_HASH(node
)->nd_brace
) {
625 case 0: A("0 (keyword argument)"); break;
626 case 1: A("1 (hash literal)"); break;
630 F_NODE(nd_head
, RNODE_HASH
, "contents");
634 ANN("yield invocation");
635 ANN("format: yield [nd_head]");
636 ANN("example: yield 1");
638 F_NODE(nd_head
, RNODE_YIELD
, "arguments");
642 ANN("local variable reference");
643 ANN("format: [nd_vid](lvar)");
645 F_ID(nd_vid
, RNODE_LVAR
, "local variable");
648 ANN("dynamic variable reference");
649 ANN("format: [nd_vid](dvar)");
650 ANN("example: 1.times { x = 1; x }");
651 F_ID(nd_vid
, RNODE_DVAR
, "local variable");
654 ANN("instance variable reference");
655 ANN("format: [nd_vid](ivar)");
657 F_ID(nd_vid
, RNODE_IVAR
, "instance variable");
660 ANN("constant reference");
661 ANN("format: [nd_vid](constant)");
663 F_ID(nd_vid
, RNODE_CONST
, "constant");
666 ANN("class variable reference");
667 ANN("format: [nd_vid](cvar)");
669 F_ID(nd_vid
, RNODE_CVAR
, "class variable");
673 ANN("global variable reference");
674 ANN("format: [nd_vid](gvar)");
676 F_ID(nd_vid
, RNODE_GVAR
, "global variable");
680 ANN("nth special variable reference");
681 ANN("format: $[nd_nth]");
682 ANN("example: $1, $2, ..");
683 F_CUSTOM1(nd_nth
, "variable") { A("$"); A_LONG(RNODE_NTH_REF(node
)->nd_nth
); }
687 ANN("back special variable reference");
688 ANN("format: $[nd_nth]");
689 ANN("example: $&, $`, $', $+");
690 F_CUSTOM1(nd_nth
, "variable") {
692 name
[1] = (char)RNODE_BACK_REF(node
)->nd_nth
;
698 ANN("match expression (against $_ implicitly)");
699 ANN("format: [nd_lit] (in condition)");
700 ANN("example: if /foo/; foo; end");
702 F_VALUE(string
, rb_node_regx_string_val(node
), "string");
706 ANN("match expression (regexp first)");
707 ANN("format: [nd_recv] =~ [nd_value]");
708 ANN("example: /foo/ =~ 'foo'");
709 F_NODE(nd_recv
, RNODE_MATCH2
, "regexp (receiver)");
710 if (!RNODE_MATCH2(node
)->nd_args
) LAST_NODE
;
711 F_NODE(nd_value
, RNODE_MATCH2
, "string (argument)");
712 if (RNODE_MATCH2(node
)->nd_args
) {
714 F_NODE(nd_args
, RNODE_MATCH2
, "named captures");
719 ANN("match expression (regexp second)");
720 ANN("format: [nd_recv] =~ [nd_value]");
721 ANN("example: 'foo' =~ /foo/");
722 F_NODE(nd_recv
, RNODE_MATCH3
, "string (receiver)");
724 F_NODE(nd_value
, RNODE_MATCH3
, "regexp (argument)");
728 ANN("string literal");
729 ANN("format: [nd_lit]");
730 ANN("example: 'foo'");
733 ANN("xstring literal");
734 ANN("format: [nd_lit]");
735 ANN("example: `foo`");
737 F_VALUE(string
, rb_node_str_string_val(node
), "literal");
741 ANN("integer literal");
742 ANN("format: [val]");
744 F_VALUE(val
, rb_node_integer_literal_val(node
), "val");
748 ANN("float literal");
749 ANN("format: [val]");
751 F_VALUE(val
, rb_node_float_literal_val(node
), "val");
755 ANN("rational number literal");
756 ANN("format: [val]");
758 F_VALUE(val
, rb_node_rational_literal_val(node
), "val");
762 ANN("complex number literal");
763 ANN("format: [val]");
765 F_VALUE(val
, rb_node_imaginary_literal_val(node
), "val");
769 ANN("regexp literal");
770 ANN("format: [string]");
771 ANN("example: /foo/");
773 F_VALUE(string
, rb_node_regx_string_val(node
), "string");
777 ANN("once evaluation");
778 ANN("format: [nd_body]");
779 ANN("example: /foo#{ bar }baz/o");
781 F_NODE(nd_body
, RNODE_ONCE
, "body");
785 ANN("string literal with interpolation");
786 ANN("format: [nd_lit]");
787 ANN("example: \"foo#{ bar }baz\"");
790 ANN("xstring literal with interpolation");
791 ANN("format: [nd_lit]");
792 ANN("example: `foo#{ bar }baz`");
795 ANN("regexp literal with interpolation");
796 ANN("format: [nd_lit]");
797 ANN("example: /foo#{ bar }baz/");
800 ANN("symbol literal with interpolation");
801 ANN("format: [nd_lit]");
802 ANN("example: :\"foo#{ bar }baz\"");
804 F_VALUE(string
, rb_node_dstr_string_val(node
), "preceding string");
805 if (!RNODE_DSTR(node
)->nd_next
) return;
806 F_NODE(nd_next
->nd_head
, RNODE_DSTR
, "interpolation");
808 F_NODE(nd_next
->nd_next
, RNODE_DSTR
, "tailing strings");
812 ANN("symbol literal");
813 ANN("format: [string]");
814 ANN("example: :foo");
815 F_VALUE(string
, rb_node_sym_string_val(node
), "string");
819 ANN("interpolation expression");
820 ANN("format: \"..#{ [nd_body] }..\"");
821 ANN("example: \"foo#{ bar }baz\"");
823 F_NODE(nd_body
, RNODE_EVSTR
, "body");
827 ANN("splat argument following arguments");
828 ANN("format: ..(*[nd_head], [nd_body..])");
829 ANN("example: foo(*ary, post_arg1, post_arg2)");
830 F_NODE(nd_head
, RNODE_ARGSCAT
, "preceding array");
832 F_NODE(nd_body
, RNODE_ARGSCAT
, "following array");
836 ANN("splat argument following one argument");
837 ANN("format: ..(*[nd_head], [nd_body])");
838 ANN("example: foo(*ary, post_arg)");
839 F_NODE(nd_head
, RNODE_ARGSPUSH
, "preceding array");
841 F_NODE(nd_body
, RNODE_ARGSPUSH
, "following element");
845 ANN("splat argument");
846 ANN("format: *[nd_head]");
847 ANN("example: foo(*ary)");
849 F_NODE(nd_head
, RNODE_SPLAT
, "splat'ed array");
852 case NODE_BLOCK_PASS
:
853 ANN("arguments with block argument");
854 ANN("format: ..([nd_head], &[nd_body])");
855 ANN("example: foo(x, &blk)");
856 F_NODE(nd_head
, RNODE_BLOCK_PASS
, "other arguments");
858 F_NODE(nd_body
, RNODE_BLOCK_PASS
, "block argument");
862 ANN("method definition");
863 ANN("format: def [nd_mid] [nd_defn]; end");
864 ANN("example: def foo; bar; end");
865 F_ID(nd_mid
, RNODE_DEFN
, "method name");
867 F_NODE(nd_defn
, RNODE_DEFN
, "method definition");
871 ANN("singleton method definition");
872 ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
873 ANN("example: def obj.foo; bar; end");
874 F_NODE(nd_recv
, RNODE_DEFS
, "receiver");
875 F_ID(nd_mid
, RNODE_DEFS
, "method name");
877 F_NODE(nd_defn
, RNODE_DEFS
, "method definition");
881 ANN("method alias statement");
882 ANN("format: alias [nd_1st] [nd_2nd]");
883 ANN("example: alias bar foo");
884 F_NODE(nd_1st
, RNODE_ALIAS
, "new name");
886 F_NODE(nd_2nd
, RNODE_ALIAS
, "old name");
890 ANN("global variable alias statement");
891 ANN("format: alias [nd_alias](gvar) [nd_orig](gvar)");
892 ANN("example: alias $y $x");
893 F_ID(nd_alias
, RNODE_VALIAS
, "new name");
894 F_ID(nd_orig
, RNODE_VALIAS
, "old name");
898 ANN("method undef statement");
899 ANN("format: undef [nd_undef]");
900 ANN("example: undef foo");
902 F_NODE(nd_undef
, RNODE_UNDEF
, "old name");
906 ANN("class definition");
907 ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
908 ANN("example: class C2 < C; ..; end");
909 F_NODE(nd_cpath
, RNODE_CLASS
, "class path");
910 F_NODE(nd_super
, RNODE_CLASS
, "superclass");
912 F_NODE(nd_body
, RNODE_CLASS
, "class definition");
916 ANN("module definition");
917 ANN("format: module [nd_cpath]; [nd_body]; end");
918 ANN("example: module M; ..; end");
919 F_NODE(nd_cpath
, RNODE_MODULE
, "module path");
921 F_NODE(nd_body
, RNODE_MODULE
, "module definition");
925 ANN("singleton class definition");
926 ANN("format: class << [nd_recv]; [nd_body]; end");
927 ANN("example: class << obj; ..; end");
928 F_NODE(nd_recv
, RNODE_SCLASS
, "receiver");
930 F_NODE(nd_body
, RNODE_SCLASS
, "singleton class definition");
934 ANN("scoped constant reference");
935 ANN("format: [nd_head]::[nd_mid]");
936 ANN("example: M::C");
937 F_ID(nd_mid
, RNODE_COLON2
, "constant name");
939 F_NODE(nd_head
, RNODE_COLON2
, "receiver");
943 ANN("top-level constant reference");
944 ANN("format: ::[nd_mid]");
945 ANN("example: ::Object");
946 F_ID(nd_mid
, RNODE_COLON3
, "constant name");
950 ANN("range constructor (incl.)");
951 ANN("format: [nd_beg]..[nd_end]");
952 ANN("example: 1..5");
955 ANN("range constructor (excl.)");
956 ANN("format: [nd_beg]...[nd_end]");
957 ANN("example: 1...5");
960 ANN("flip-flop condition (incl.)");
961 ANN("format: [nd_beg]..[nd_end]");
962 ANN("example: if (x==1)..(x==5); foo; end");
965 ANN("flip-flop condition (excl.)");
966 ANN("format: [nd_beg]...[nd_end]");
967 ANN("example: if (x==1)...(x==5); foo; end");
969 F_NODE(nd_beg
, RNODE_DOT2
, "begin");
971 F_NODE(nd_end
, RNODE_DOT2
, "end");
977 ANN("example: self");
978 F_CUSTOM1(nd_state
, "nd_state") {
979 A_INT((int)RNODE_SELF(node
)->nd_state
);
992 ANN("example: true");
997 ANN("format: false");
998 ANN("example: false");
1002 ANN("virtual reference to $!");
1003 ANN("format: rescue => id");
1004 ANN("example: rescue => id");
1008 ANN("defined? expression");
1009 ANN("format: defined?([nd_head])");
1010 ANN("example: defined?(foo)");
1011 F_NODE(nd_head
, RNODE_DEFINED
, "expr");
1015 ANN("post-execution");
1016 ANN("format: END { [nd_body] }");
1017 ANN("example: END { foo }");
1019 F_NODE(nd_body
, RNODE_POSTEXE
, "END clause");
1023 ANN("attr assignment");
1024 ANN("format: [nd_recv].[nd_mid] = [nd_args]");
1025 ANN("example: struct.field = foo");
1026 F_NODE(nd_recv
, RNODE_ATTRASGN
, "receiver");
1027 F_ID(nd_mid
, RNODE_ATTRASGN
, "method name");
1029 F_NODE(nd_args
, RNODE_ATTRASGN
, "arguments");
1033 ANN("lambda expression");
1034 ANN("format: -> [nd_body]");
1035 ANN("example: -> { foo }");
1037 F_NODE(nd_body
, RNODE_LAMBDA
, "lambda clause");
1041 ANN("optional arguments");
1042 ANN("format: def method_name([nd_body=some], [nd_next..])");
1043 ANN("example: def foo(a, b=1, c); end");
1044 F_NODE(nd_body
, RNODE_OPT_ARG
, "body");
1046 F_NODE(nd_next
, RNODE_OPT_ARG
, "next");
1050 ANN("keyword arguments");
1051 ANN("format: def method_name([nd_body=some], [nd_next..])");
1052 ANN("example: def foo(a:1, b:2); end");
1053 F_NODE(nd_body
, RNODE_KW_ARG
, "body");
1055 F_NODE(nd_next
, RNODE_KW_ARG
, "next");
1059 ANN("post arguments");
1060 ANN("format: *[nd_1st], [nd_2nd..] = ..");
1061 ANN("example: a, *rest, z = foo");
1062 if (NODE_NAMED_REST_P(RNODE_POSTARG(node
)->nd_1st
)) {
1063 F_NODE(nd_1st
, RNODE_POSTARG
, "rest argument");
1066 F_MSG(nd_1st
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1069 F_NODE(nd_2nd
, RNODE_POSTARG
, "post arguments");
1073 ANN("method parameters");
1074 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])");
1075 ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
1076 F_INT(nd_ainfo
.pre_args_num
, RNODE_ARGS
, "count of mandatory (pre-)arguments");
1077 F_NODE(nd_ainfo
.pre_init
, RNODE_ARGS
, "initialization of (pre-)arguments");
1078 F_INT(nd_ainfo
.post_args_num
, RNODE_ARGS
, "count of mandatory post-arguments");
1079 F_NODE(nd_ainfo
.post_init
, RNODE_ARGS
, "initialization of post-arguments");
1080 F_ID(nd_ainfo
.first_post_arg
, RNODE_ARGS
, "first post argument");
1081 F_CUSTOM1(nd_ainfo
.rest_arg
, "rest argument") {
1082 if (RNODE_ARGS(node
)->nd_ainfo
.rest_arg
== NODE_SPECIAL_EXCESSIVE_COMMA
) {
1083 A("1 (excessed comma)");
1086 A_ID(RNODE_ARGS(node
)->nd_ainfo
.rest_arg
);
1089 F_ID(nd_ainfo
.block_arg
, RNODE_ARGS
, "block argument");
1090 F_NODE(nd_ainfo
.opt_args
, RNODE_ARGS
, "optional arguments");
1091 F_NODE(nd_ainfo
.kw_args
, RNODE_ARGS
, "keyword arguments");
1093 F_NODE(nd_ainfo
.kw_rest_arg
, RNODE_ARGS
, "keyword rest argument");
1098 ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
1099 F_CUSTOM1(nd_tbl
, "local table") {
1100 rb_ast_id_table_t
*tbl
= RNODE_SCOPE(node
)->nd_tbl
;
1102 int size
= tbl
? tbl
->size
: 0;
1103 if (size
== 0) A("(empty)");
1104 for (i
= 0; i
< size
; i
++) {
1105 A_ID(tbl
->ids
[i
]); if (i
< size
- 1) A(",");
1108 F_NODE(nd_args
, RNODE_SCOPE
, "arguments");
1110 F_NODE(nd_body
, RNODE_SCOPE
, "body");
1114 ANN("array pattern");
1115 ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
1116 F_NODE(nd_pconst
, RNODE_ARYPTN
, "constant");
1117 F_NODE(pre_args
, RNODE_ARYPTN
, "pre arguments");
1118 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node
)->rest_arg
)) {
1119 F_NODE(rest_arg
, RNODE_ARYPTN
, "rest argument");
1122 F_MSG(rest_arg
, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1125 F_NODE(post_args
, RNODE_ARYPTN
, "post arguments");
1129 ANN("find pattern");
1130 ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
1131 F_NODE(nd_pconst
, RNODE_FNDPTN
, "constant");
1132 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node
)->pre_rest_arg
)) {
1133 F_NODE(pre_rest_arg
, RNODE_FNDPTN
, "pre rest argument");
1136 F_MSG(pre_rest_arg
, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1138 F_NODE(args
, RNODE_FNDPTN
, "arguments");
1141 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node
)->post_rest_arg
)) {
1142 F_NODE(post_rest_arg
, RNODE_FNDPTN
, "post rest argument");
1145 F_MSG(post_rest_arg
, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1150 ANN("hash pattern");
1151 ANN("format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
1152 F_NODE(nd_pconst
, RNODE_HSHPTN
, "constant");
1153 F_NODE(nd_pkwargs
, RNODE_HSHPTN
, "keyword arguments");
1155 if (RNODE_HSHPTN(node
)->nd_pkwrestarg
== NODE_SPECIAL_NO_REST_KEYWORD
) {
1156 F_MSG(nd_pkwrestarg
, "keyword rest argument", "NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
1159 F_NODE(nd_pkwrestarg
, RNODE_HSHPTN
, "keyword rest argument");
1165 ANN("format: [lineno]");
1166 ANN("example: __LINE__");
1171 ANN("format: [path]");
1172 ANN("example: __FILE__");
1173 F_VALUE(path
, rb_node_file_path_val(node
), "path");
1178 ANN("format: [enc]");
1179 ANN("example: __ENCODING__");
1180 F_VALUE(enc
, rb_node_encoding_val(node
), "enc");
1184 ANN("Broken input recovered by Error Tolerant mode");
1192 rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node
)));
1196 rb_parser_dump_tree(const NODE
*node
, int comment
)
1198 VALUE buf
= rb_str_new_cstr(
1199 "###########################################################\n"
1200 "## Do NOT use this node dump for any purpose other than ##\n"
1201 "## debug and research. Compatibility is not guaranteed. ##\n"
1202 "###########################################################\n\n"
1204 dump_node(buf
, rb_str_new_cstr("# "), comment
, node
);