13#include "internal/class.h"
14#include "internal/hash.h"
15#include "internal/ruby_parser.h"
16#include "internal/variable.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; \
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 = " ")
111rb_dump_literal(
VALUE lit)
118 if (RCLASS_SINGLETON_P(lit)) {
119 str = rb_sprintf(
"<%"PRIsVALUE
">", str);
142 VALUE str = rb_id2str(
id);
147 rb_str_catf(buf,
"(internal variable: 0x%"PRIsVALUE
")",
id);
157static void dump_node(
VALUE,
VALUE,
int,
const NODE *);
158static const char default_indent[] =
"| ";
161dump_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");
176dump_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]);
198dump_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");
243 F_NODE(nd_else, RNODE_IF,
"else clause");
247 ANN(
"unless statement");
248 ANN(
"format: unless [nd_cond] then [nd_body] else [nd_else] end");
249 ANN(
"example: unless x == 1 then foo else bar end");
250 F_NODE(nd_cond, RNODE_UNLESS,
"condition expr");
251 F_NODE(nd_body, RNODE_UNLESS,
"then clause");
252 F_NODE(nd_else, RNODE_UNLESS,
"else clause");
253 F_LOC(keyword_loc, RNODE_UNLESS);
254 F_LOC(then_keyword_loc, RNODE_UNLESS);
256 F_LOC(end_keyword_loc, RNODE_UNLESS);
260 ANN(
"case statement");
261 ANN(
"format: case [nd_head]; [nd_body]; end");
262 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
263 F_NODE(nd_head, RNODE_CASE,
"case expr");
264 F_NODE(nd_body, RNODE_CASE,
"when clauses");
265 F_LOC(case_keyword_loc, RNODE_CASE);
267 F_LOC(end_keyword_loc, RNODE_CASE);
270 ANN(
"case statement with no head");
271 ANN(
"format: case; [nd_body]; end");
272 ANN(
"example: case; when 1; foo; when 2; bar; else baz; end");
273 F_NODE(nd_head, RNODE_CASE2,
"case expr");
274 F_NODE(nd_body, RNODE_CASE2,
"when clauses");
275 F_LOC(case_keyword_loc, RNODE_CASE2);
277 F_LOC(end_keyword_loc, RNODE_CASE2);
280 ANN(
"case statement (pattern matching)");
281 ANN(
"format: case [nd_head]; [nd_body]; end");
282 ANN(
"example: case x; in 1; foo; in 2; bar; else baz; end");
283 F_NODE(nd_head, RNODE_CASE3,
"case expr");
284 F_NODE(nd_body, RNODE_CASE3,
"in clauses");
285 F_LOC(case_keyword_loc, RNODE_CASE3);
287 F_LOC(end_keyword_loc, RNODE_CASE3);
292 ANN(
"format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
293 ANN(
"example: case x; when 1; foo; when 2; bar; else baz; end");
294 F_NODE(nd_head, RNODE_WHEN,
"when value");
295 F_NODE(nd_body, RNODE_WHEN,
"when body");
297 F_NODE(nd_next, RNODE_WHEN,
"next when clause");
298 F_LOC(keyword_loc, RNODE_WHEN);
300 F_LOC(then_keyword_loc, RNODE_WHEN);
305 ANN(
"format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
306 ANN(
"example: case x; in 1; foo; in 2; bar; else baz; end");
307 F_NODE(nd_head, RNODE_IN,
"in pattern");
308 F_NODE(nd_body, RNODE_IN,
"in body");
310 F_NODE(nd_next, RNODE_IN,
"next in clause");
314 ANN(
"while statement");
315 ANN(
"format: while [nd_cond]; [nd_body]; end");
316 ANN(
"example: while x == 1; foo; end");
319 ANN(
"until statement");
320 ANN(
"format: until [nd_cond]; [nd_body]; end");
321 ANN(
"example: until x == 1; foo; end");
323 F_CUSTOM1(nd_state,
"begin-end-while?") {
324 A_INT((
int)RNODE_WHILE(node)->nd_state);
325 A((RNODE_WHILE(node)->nd_state == 1) ?
" (while-end)" :
" (begin-end-while)");
327 F_NODE(nd_cond, RNODE_WHILE,
"condition");
328 F_NODE(nd_body, RNODE_WHILE,
"body");
329 F_LOC(keyword_loc, RNODE_WHILE);
331 F_LOC(closing_loc, RNODE_WHILE);
335 ANN(
"method call with block");
336 ANN(
"format: [nd_iter] { [nd_body] }");
337 ANN(
"example: 3.times { foo }");
340 ANN(
"for statement");
341 ANN(
"format: for * in [nd_iter] do [nd_body] end");
342 ANN(
"example: for i in 1..3 do foo end");
344 F_NODE(nd_iter, RNODE_ITER,
"iteration receiver");
346 F_NODE(nd_body, RNODE_ITER,
"body");
350 ANN(
"vars of for statement with masgn");
351 ANN(
"format: for [nd_var] in ... do ... end");
352 ANN(
"example: for x, y in 1..3 do foo end");
354 F_NODE(nd_var, RNODE_FOR_MASGN,
"var");
358 ANN(
"break statement");
359 ANN(
"format: break [nd_stts]");
360 ANN(
"example: break 1");
361 F_NODE(nd_stts, RNODE_BREAK,
"value");
363 F_LOC(keyword_loc, RNODE_BREAK);
366 ANN(
"next statement");
367 ANN(
"format: next [nd_stts]");
368 ANN(
"example: next 1");
369 F_NODE(nd_stts, RNODE_NEXT,
"value");
371 F_LOC(keyword_loc, RNODE_NEXT);
374 ANN(
"return statement");
375 ANN(
"format: return [nd_stts]");
376 ANN(
"example: return 1");
377 F_NODE(nd_stts, RNODE_RETURN,
"value");
379 F_LOC(keyword_loc, RNODE_RETURN);
383 ANN(
"redo statement");
385 ANN(
"example: redo");
386 F_LOC(keyword_loc, RNODE_REDO);
390 ANN(
"retry statement");
391 ANN(
"format: retry");
392 ANN(
"example: retry");
396 ANN(
"begin statement");
397 ANN(
"format: begin; [nd_body]; end");
398 ANN(
"example: begin; 1; end");
400 F_NODE(nd_body, RNODE_BEGIN,
"body");
404 ANN(
"rescue clause");
405 ANN(
"format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
406 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
407 F_NODE(nd_head, RNODE_RESCUE,
"body");
408 F_NODE(nd_resq, RNODE_RESCUE,
"rescue clause list");
410 F_NODE(nd_else, RNODE_RESCUE,
"rescue else clause");
414 ANN(
"rescue clause (cont'd)");
415 ANN(
"format: rescue [nd_args] (=> [nd_exc_var]); [nd_body]; (rescue) [nd_next]");
416 ANN(
"example: begin; foo; rescue; bar; else; baz; end");
417 F_NODE(nd_args, RNODE_RESBODY,
"rescue exceptions");
418 F_NODE(nd_exc_var, RNODE_RESBODY,
"exception variable");
419 F_NODE(nd_body, RNODE_RESBODY,
"rescue clause");
421 F_NODE(nd_next, RNODE_RESBODY,
"next rescue clause");
425 ANN(
"ensure clause");
426 ANN(
"format: begin; [nd_head]; ensure; [nd_ensr]; end");
427 ANN(
"example: begin; foo; ensure; bar; end");
428 F_NODE(nd_head, RNODE_ENSURE,
"body");
430 F_NODE(nd_ensr, RNODE_ENSURE,
"ensure clause");
435 ANN(
"format: [nd_1st] && [nd_2nd]");
436 ANN(
"example: foo && bar");
440 ANN(
"format: [nd_1st] || [nd_2nd]");
441 ANN(
"example: foo || bar");
444 F_NODE(nd_1st, RNODE_AND,
"left expr");
445 if (!RNODE_AND(node)->nd_2nd || !nd_type_p(RNODE_AND(node)->nd_2nd,
type))
447 node = RNODE_AND(node)->nd_2nd;
449 F_NODE(nd_2nd, RNODE_AND,
"right expr");
451 F_LOC(operator_loc, RNODE_AND);
455 ANN(
"multiple assignment");
456 ANN(
"format: [nd_head], [nd_args] = [nd_value]");
457 ANN(
"example: a, b = foo");
458 F_NODE(nd_value, RNODE_MASGN,
"rhsn");
459 F_NODE(nd_head, RNODE_MASGN,
"lhsn");
460 if (NODE_NAMED_REST_P(RNODE_MASGN(node)->nd_args)) {
462 F_NODE(nd_args, RNODE_MASGN,
"splatn");
465 F_MSG(nd_args,
"splatn",
"NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
470 ANN(
"local variable assignment");
471 ANN(
"format: [nd_vid](lvar) = [nd_value]");
472 ANN(
"example: x = foo");
473 F_ID(nd_vid, RNODE_LASGN,
"local variable");
474 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node)->nd_value)) {
475 F_MSG(nd_value,
"rvalue",
"NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
479 F_NODE(nd_value, RNODE_LASGN,
"rvalue");
483 ANN(
"dynamic variable assignment");
484 ANN(
"format: [nd_vid](dvar) = [nd_value]");
485 ANN(
"example: x = nil; 1.times { x = foo }");
486 ANN(
"example: 1.times { x = foo }");
487 F_ID(nd_vid, RNODE_DASGN,
"local variable");
488 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node)->nd_value)) {
489 F_MSG(nd_value,
"rvalue",
"NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
493 F_NODE(nd_value, RNODE_DASGN,
"rvalue");
497 ANN(
"instance variable assignment");
498 ANN(
"format: [nd_vid](ivar) = [nd_value]");
499 ANN(
"example: @x = foo");
500 F_ID(nd_vid, RNODE_IASGN,
"instance variable");
502 F_NODE(nd_value, RNODE_IASGN,
"rvalue");
505 ANN(
"class variable assignment");
506 ANN(
"format: [nd_vid](cvar) = [nd_value]");
507 ANN(
"example: @@x = foo");
508 F_ID(nd_vid, RNODE_CVASGN,
"class variable");
510 F_NODE(nd_value, RNODE_CVASGN,
"rvalue");
513 ANN(
"global variable assignment");
514 ANN(
"format: [nd_vid](gvar) = [nd_value]");
515 ANN(
"example: $x = foo");
516 F_ID(nd_vid, RNODE_GASGN,
"global variable");
518 F_NODE(nd_value, RNODE_GASGN,
"rvalue");
522 ANN(
"constant declaration");
523 ANN(
"format: [nd_else]::[nd_vid](constant) = [nd_value]");
524 ANN(
"example: X = foo");
525 if (RNODE_CDECL(node)->nd_vid) {
526 F_ID(nd_vid, RNODE_CDECL,
"constant");
527 F_MSG(nd_else,
"extension",
"not used");
530 F_MSG(nd_vid,
"constant",
"0 (see extension field)");
531 F_NODE(nd_else, RNODE_CDECL,
"extension");
533 F_SHAREABILITY(shareability, RNODE_CDECL,
"shareability");
535 F_NODE(nd_value, RNODE_CDECL,
"rvalue");
539 ANN(
"array assignment with operator");
540 ANN(
"format: [nd_recv] [ [nd_index] ] [nd_mid]= [nd_rvalue]");
541 ANN(
"example: ary[1] += foo");
542 F_NODE(nd_recv, RNODE_OP_ASGN1,
"receiver");
543 F_ID(nd_mid, RNODE_OP_ASGN1,
"operator");
544 F_NODE(nd_index, RNODE_OP_ASGN1,
"index");
545 F_NODE(nd_rvalue, RNODE_OP_ASGN1,
"rvalue");
546 F_LOC(call_operator_loc, RNODE_OP_ASGN1);
547 F_LOC(opening_loc, RNODE_OP_ASGN1);
548 F_LOC(closing_loc, RNODE_OP_ASGN1);
550 F_LOC(binary_operator_loc, RNODE_OP_ASGN1);
554 ANN(
"attr assignment with operator");
555 ANN(
"format: [nd_recv].[nd_vid] [nd_mid]= [nd_value]");
556 ANN(
"example: struct.field += foo");
557 F_NODE(nd_recv, RNODE_OP_ASGN2,
"receiver");
558 F_CUSTOM1(nd_vid,
"attr") {
559 if (RNODE_OP_ASGN2(node)->nd_aid) A(
"? ");
560 A_ID(RNODE_OP_ASGN2(node)->nd_vid);
562 F_ID(nd_mid, RNODE_OP_ASGN2,
"operator");
563 F_NODE(nd_value, RNODE_OP_ASGN2,
"rvalue");
564 F_LOC(call_operator_loc, RNODE_OP_ASGN2);
565 F_LOC(message_loc, RNODE_OP_ASGN2);
567 F_LOC(binary_operator_loc, RNODE_OP_ASGN2);
570 case NODE_OP_ASGN_AND:
571 ANN(
"assignment with && operator");
572 ANN(
"format: [nd_head] &&= [nd_value]");
573 ANN(
"example: foo &&= bar");
575 case NODE_OP_ASGN_OR:
576 ANN(
"assignment with || operator");
577 ANN(
"format: [nd_head] ||= [nd_value]");
578 ANN(
"example: foo ||= bar");
580 F_NODE(nd_head, RNODE_OP_ASGN_AND,
"variable");
582 F_NODE(nd_value, RNODE_OP_ASGN_AND,
"rvalue");
586 ANN(
"constant declaration with operator");
587 ANN(
"format: [nd_head](constant) [nd_aid]= [nd_value]");
588 ANN(
"example: A::B ||= 1");
589 F_NODE(nd_head, RNODE_OP_CDECL,
"constant");
590 F_ID(nd_aid, RNODE_OP_CDECL,
"operator");
591 F_SHAREABILITY(shareability, RNODE_OP_CDECL,
"shareability");
593 F_NODE(nd_value, RNODE_OP_CDECL,
"rvalue");
597 ANN(
"method invocation");
598 ANN(
"format: [nd_recv].[nd_mid]([nd_args])");
599 ANN(
"example: obj.foo(1)");
600 F_ID(nd_mid, RNODE_CALL,
"method id");
601 F_NODE(nd_recv, RNODE_CALL,
"receiver");
603 F_NODE(nd_args, RNODE_CALL,
"arguments");
607 ANN(
"method invocation");
608 ANN(
"format: [nd_recv] [nd_mid] [nd_args]");
609 ANN(
"example: foo + bar");
610 F_ID(nd_mid, RNODE_OPCALL,
"method id");
611 F_NODE(nd_recv, RNODE_OPCALL,
"receiver");
613 F_NODE(nd_args, RNODE_OPCALL,
"arguments");
617 ANN(
"function call");
618 ANN(
"format: [nd_mid]([nd_args])");
619 ANN(
"example: foo(1)");
620 F_ID(nd_mid, RNODE_FCALL,
"method id");
622 F_NODE(nd_args, RNODE_FCALL,
"arguments");
626 ANN(
"function call with no argument");
627 ANN(
"format: [nd_mid]");
629 F_ID(nd_mid, RNODE_VCALL,
"method id");
633 ANN(
"safe method invocation");
634 ANN(
"format: [nd_recv]&.[nd_mid]([nd_args])");
635 ANN(
"example: obj&.foo(1)");
636 F_ID(nd_mid, RNODE_QCALL,
"method id");
637 F_NODE(nd_recv, RNODE_QCALL,
"receiver");
639 F_NODE(nd_args, RNODE_QCALL,
"arguments");
643 ANN(
"super invocation");
644 ANN(
"format: super [nd_args]");
645 ANN(
"example: super 1");
647 F_NODE(nd_args, RNODE_SUPER,
"arguments");
651 ANN(
"super invocation with no argument");
652 ANN(
"format: super");
653 ANN(
"example: super");
657 ANN(
"list constructor");
658 ANN(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
659 ANN(
"example: [1, 2, 3]");
660 dump_array(buf, indent, comment, node);
664 ANN(
"empty list constructor");
670 if (!RNODE_HASH(node)->nd_brace) {
671 ANN(
"keyword arguments");
672 ANN(
"format: [nd_head]");
673 ANN(
"example: a: 1, b: 2");
676 ANN(
"hash constructor");
677 ANN(
"format: { [nd_head] }");
678 ANN(
"example: { 1 => 2, 3 => 4 }");
680 F_CUSTOM1(nd_brace,
"keyword arguments or hash literal") {
681 switch (RNODE_HASH(node)->nd_brace) {
682 case 0: A(
"0 (keyword argument)");
break;
683 case 1: A(
"1 (hash literal)");
break;
687 F_NODE(nd_head, RNODE_HASH,
"contents");
691 ANN(
"yield invocation");
692 ANN(
"format: yield [nd_head]");
693 ANN(
"example: yield 1");
695 F_NODE(nd_head, RNODE_YIELD,
"arguments");
699 ANN(
"local variable reference");
700 ANN(
"format: [nd_vid](lvar)");
702 F_ID(nd_vid, RNODE_LVAR,
"local variable");
705 ANN(
"dynamic variable reference");
706 ANN(
"format: [nd_vid](dvar)");
707 ANN(
"example: 1.times { x = 1; x }");
708 F_ID(nd_vid, RNODE_DVAR,
"local variable");
711 ANN(
"instance variable reference");
712 ANN(
"format: [nd_vid](ivar)");
714 F_ID(nd_vid, RNODE_IVAR,
"instance variable");
717 ANN(
"constant reference");
718 ANN(
"format: [nd_vid](constant)");
720 F_ID(nd_vid, RNODE_CONST,
"constant");
723 ANN(
"class variable reference");
724 ANN(
"format: [nd_vid](cvar)");
726 F_ID(nd_vid, RNODE_CVAR,
"class variable");
730 ANN(
"global variable reference");
731 ANN(
"format: [nd_vid](gvar)");
733 F_ID(nd_vid, RNODE_GVAR,
"global variable");
737 ANN(
"nth special variable reference");
738 ANN(
"format: $[nd_nth]");
739 ANN(
"example: $1, $2, ..");
740 F_CUSTOM1(nd_nth,
"variable") { A(
"$"); A_LONG(RNODE_NTH_REF(node)->nd_nth); }
744 ANN(
"back special variable reference");
745 ANN(
"format: $[nd_nth]");
746 ANN(
"example: $&, $`, $', $+");
747 F_CUSTOM1(nd_nth,
"variable") {
749 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
755 ANN(
"match expression (against $_ implicitly)");
756 ANN(
"format: [nd_lit] (in condition)");
757 ANN(
"example: if /foo/; foo; end");
759 F_VALUE(
string, rb_node_regx_string_val(node),
"string");
763 ANN(
"match expression (regexp first)");
764 ANN(
"format: [nd_recv] =~ [nd_value]");
765 ANN(
"example: /foo/ =~ 'foo'");
766 F_NODE(nd_recv, RNODE_MATCH2,
"regexp (receiver)");
767 if (!RNODE_MATCH2(node)->nd_args) LAST_NODE;
768 F_NODE(nd_value, RNODE_MATCH2,
"string (argument)");
769 if (RNODE_MATCH2(node)->nd_args) {
771 F_NODE(nd_args, RNODE_MATCH2,
"named captures");
776 ANN(
"match expression (regexp second)");
777 ANN(
"format: [nd_recv] =~ [nd_value]");
778 ANN(
"example: 'foo' =~ /foo/");
779 F_NODE(nd_recv, RNODE_MATCH3,
"string (receiver)");
781 F_NODE(nd_value, RNODE_MATCH3,
"regexp (argument)");
785 ANN(
"string literal");
786 ANN(
"format: [nd_lit]");
787 ANN(
"example: 'foo'");
790 ANN(
"xstring literal");
791 ANN(
"format: [nd_lit]");
792 ANN(
"example: `foo`");
794 F_VALUE(
string, rb_node_str_string_val(node),
"literal");
798 ANN(
"integer literal");
799 ANN(
"format: [val]");
801 F_VALUE(val, rb_node_integer_literal_val(node),
"val");
805 ANN(
"float literal");
806 ANN(
"format: [val]");
808 F_VALUE(val, rb_node_float_literal_val(node),
"val");
812 ANN(
"rational number literal");
813 ANN(
"format: [val]");
815 F_VALUE(val, rb_node_rational_literal_val(node),
"val");
819 ANN(
"complex number literal");
820 ANN(
"format: [val]");
822 F_VALUE(val, rb_node_imaginary_literal_val(node),
"val");
826 ANN(
"regexp literal");
827 ANN(
"format: [string]");
828 ANN(
"example: /foo/");
830 F_VALUE(
string, rb_node_regx_string_val(node),
"string");
834 ANN(
"once evaluation");
835 ANN(
"format: [nd_body]");
836 ANN(
"example: /foo#{ bar }baz/o");
838 F_NODE(nd_body, RNODE_ONCE,
"body");
842 ANN(
"string literal with interpolation");
843 ANN(
"format: [nd_lit]");
844 ANN(
"example: \"foo#{ bar }baz\"");
847 ANN(
"xstring literal with interpolation");
848 ANN(
"format: [nd_lit]");
849 ANN(
"example: `foo#{ bar }baz`");
852 ANN(
"regexp literal with interpolation");
853 ANN(
"format: [nd_lit]");
854 ANN(
"example: /foo#{ bar }baz/");
857 ANN(
"symbol literal with interpolation");
858 ANN(
"format: [nd_lit]");
859 ANN(
"example: :\"foo#{ bar }baz\"");
861 F_VALUE(
string, rb_node_dstr_string_val(node),
"preceding string");
862 if (!RNODE_DSTR(node)->nd_next)
return;
863 F_NODE(nd_next->nd_head, RNODE_DSTR,
"interpolation");
865 F_NODE(nd_next->nd_next, RNODE_DSTR,
"tailing strings");
869 ANN(
"symbol literal");
870 ANN(
"format: [string]");
871 ANN(
"example: :foo");
872 F_VALUE(
string, rb_node_sym_string_val(node),
"string");
876 ANN(
"interpolation expression");
877 ANN(
"format: \"..#{ [nd_body] }..\"");
878 ANN(
"example: \"foo#{ bar }baz\"");
880 F_NODE(nd_body, RNODE_EVSTR,
"body");
884 ANN(
"splat argument following arguments");
885 ANN(
"format: ..(*[nd_head], [nd_body..])");
886 ANN(
"example: foo(*ary, post_arg1, post_arg2)");
887 F_NODE(nd_head, RNODE_ARGSCAT,
"preceding array");
889 F_NODE(nd_body, RNODE_ARGSCAT,
"following array");
893 ANN(
"splat argument following one argument");
894 ANN(
"format: ..(*[nd_head], [nd_body])");
895 ANN(
"example: foo(*ary, post_arg)");
896 F_NODE(nd_head, RNODE_ARGSPUSH,
"preceding array");
898 F_NODE(nd_body, RNODE_ARGSPUSH,
"following element");
902 ANN(
"splat argument");
903 ANN(
"format: *[nd_head]");
904 ANN(
"example: foo(*ary)");
905 F_NODE(nd_head, RNODE_SPLAT,
"splat'ed array");
907 F_LOC(operator_loc, RNODE_SPLAT);
910 case NODE_BLOCK_PASS:
911 ANN(
"arguments with block argument");
912 ANN(
"format: ..([nd_head], &[nd_body])");
913 ANN(
"example: foo(x, &blk)");
914 F_CUSTOM1(forwarding,
"arguments forwarding or not") {
915 switch (RNODE_BLOCK_PASS(node)->forwarding) {
916 case 0: A(
"0 (no forwarding)");
break;
917 case 1: A(
"1 (forwarding)");
break;
920 F_NODE(nd_head, RNODE_BLOCK_PASS,
"other arguments");
921 F_NODE(nd_body, RNODE_BLOCK_PASS,
"block argument");
923 F_LOC(operator_loc, RNODE_BLOCK_PASS);
927 ANN(
"method definition");
928 ANN(
"format: def [nd_mid] [nd_defn]; end");
929 ANN(
"example: def foo; bar; end");
930 F_ID(nd_mid, RNODE_DEFN,
"method name");
932 F_NODE(nd_defn, RNODE_DEFN,
"method definition");
936 ANN(
"singleton method definition");
937 ANN(
"format: def [nd_recv].[nd_mid] [nd_defn]; end");
938 ANN(
"example: def obj.foo; bar; end");
939 F_NODE(nd_recv, RNODE_DEFS,
"receiver");
940 F_ID(nd_mid, RNODE_DEFS,
"method name");
942 F_NODE(nd_defn, RNODE_DEFS,
"method definition");
946 ANN(
"method alias statement");
947 ANN(
"format: alias [nd_1st] [nd_2nd]");
948 ANN(
"example: alias bar foo");
949 F_NODE(nd_1st, RNODE_ALIAS,
"new name");
950 F_NODE(nd_2nd, RNODE_ALIAS,
"old name");
952 F_LOC(keyword_loc, RNODE_ALIAS);
956 ANN(
"global variable alias statement");
957 ANN(
"format: alias [nd_alias](gvar) [nd_orig](gvar)");
958 ANN(
"example: alias $y $x");
959 F_ID(nd_alias, RNODE_VALIAS,
"new name");
960 F_ID(nd_orig, RNODE_VALIAS,
"old name");
961 F_LOC(keyword_loc, RNODE_VALIAS);
965 ANN(
"method undef statement");
966 ANN(
"format: undef [nd_undefs]");
967 ANN(
"example: undef foo");
969 F_ARRAY(nd_undefs, RNODE_UNDEF,
"nd_undefs");
970 F_LOC(keyword_loc, RNODE_UNDEF);
974 ANN(
"class definition");
975 ANN(
"format: class [nd_cpath] < [nd_super]; [nd_body]; end");
976 ANN(
"example: class C2 < C; ..; end");
977 F_NODE(nd_cpath, RNODE_CLASS,
"class path");
978 F_NODE(nd_super, RNODE_CLASS,
"superclass");
980 F_NODE(nd_body, RNODE_CLASS,
"class definition");
984 ANN(
"module definition");
985 ANN(
"format: module [nd_cpath]; [nd_body]; end");
986 ANN(
"example: module M; ..; end");
987 F_NODE(nd_cpath, RNODE_MODULE,
"module path");
989 F_NODE(nd_body, RNODE_MODULE,
"module definition");
993 ANN(
"singleton class definition");
994 ANN(
"format: class << [nd_recv]; [nd_body]; end");
995 ANN(
"example: class << obj; ..; end");
996 F_NODE(nd_recv, RNODE_SCLASS,
"receiver");
998 F_NODE(nd_body, RNODE_SCLASS,
"singleton class definition");
1002 ANN(
"scoped constant reference");
1003 ANN(
"format: [nd_head]::[nd_mid]");
1004 ANN(
"example: M::C");
1005 F_ID(nd_mid, RNODE_COLON2,
"constant name");
1007 F_NODE(nd_head, RNODE_COLON2,
"receiver");
1011 ANN(
"top-level constant reference");
1012 ANN(
"format: ::[nd_mid]");
1013 ANN(
"example: ::Object");
1014 F_ID(nd_mid, RNODE_COLON3,
"constant name");
1018 ANN(
"range constructor (incl.)");
1019 ANN(
"format: [nd_beg]..[nd_end]");
1020 ANN(
"example: 1..5");
1023 ANN(
"range constructor (excl.)");
1024 ANN(
"format: [nd_beg]...[nd_end]");
1025 ANN(
"example: 1...5");
1028 ANN(
"flip-flop condition (incl.)");
1029 ANN(
"format: [nd_beg]..[nd_end]");
1030 ANN(
"example: if (x==1)..(x==5); foo; end");
1033 ANN(
"flip-flop condition (excl.)");
1034 ANN(
"format: [nd_beg]...[nd_end]");
1035 ANN(
"example: if (x==1)...(x==5); foo; end");
1037 F_NODE(nd_beg, RNODE_DOT2,
"begin");
1039 F_NODE(nd_end, RNODE_DOT2,
"end");
1044 ANN(
"format: self");
1045 ANN(
"example: self");
1046 F_CUSTOM1(nd_state,
"nd_state") {
1047 A_INT((
int)RNODE_SELF(node)->nd_state);
1054 ANN(
"example: nil");
1059 ANN(
"format: true");
1060 ANN(
"example: true");
1065 ANN(
"format: false");
1066 ANN(
"example: false");
1070 ANN(
"virtual reference to $!");
1071 ANN(
"format: rescue => id");
1072 ANN(
"example: rescue => id");
1076 ANN(
"defined? expression");
1077 ANN(
"format: defined?([nd_head])");
1078 ANN(
"example: defined?(foo)");
1080 F_NODE(nd_head, RNODE_DEFINED,
"expr");
1084 ANN(
"post-execution");
1085 ANN(
"format: END { [nd_body] }");
1086 ANN(
"example: END { foo }");
1088 F_NODE(nd_body, RNODE_POSTEXE,
"END clause");
1092 ANN(
"attr assignment");
1093 ANN(
"format: [nd_recv].[nd_mid] = [nd_args]");
1094 ANN(
"example: struct.field = foo");
1095 F_NODE(nd_recv, RNODE_ATTRASGN,
"receiver");
1096 F_ID(nd_mid, RNODE_ATTRASGN,
"method name");
1098 F_NODE(nd_args, RNODE_ATTRASGN,
"arguments");
1102 ANN(
"lambda expression");
1103 ANN(
"format: -> [nd_body]");
1104 ANN(
"example: -> { foo }");
1106 F_NODE(nd_body, RNODE_LAMBDA,
"lambda clause");
1110 ANN(
"optional arguments");
1111 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
1112 ANN(
"example: def foo(a, b=1, c); end");
1113 F_NODE(nd_body, RNODE_OPT_ARG,
"body");
1115 F_NODE(nd_next, RNODE_OPT_ARG,
"next");
1119 ANN(
"keyword arguments");
1120 ANN(
"format: def method_name([nd_body=some], [nd_next..])");
1121 ANN(
"example: def foo(a:1, b:2); end");
1122 F_NODE(nd_body, RNODE_KW_ARG,
"body");
1124 F_NODE(nd_next, RNODE_KW_ARG,
"next");
1128 ANN(
"post arguments");
1129 ANN(
"format: *[nd_1st], [nd_2nd..] = ..");
1130 ANN(
"example: a, *rest, z = foo");
1131 if (NODE_NAMED_REST_P(RNODE_POSTARG(node)->nd_1st)) {
1132 F_NODE(nd_1st, RNODE_POSTARG,
"rest argument");
1135 F_MSG(nd_1st,
"rest argument",
"NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1138 F_NODE(nd_2nd, RNODE_POSTARG,
"post arguments");
1142 ANN(
"method parameters");
1143 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])");
1144 ANN(
"example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
1145 F_CUSTOM1(nd_ainfo.forwarding,
"arguments forwarding or not") {
1146 switch (RNODE_ARGS(node)->nd_ainfo.forwarding) {
1147 case 0: A(
"0 (no forwarding)");
break;
1148 case 1: A(
"1 (forwarding)");
break;
1151 F_INT(nd_ainfo.pre_args_num, RNODE_ARGS,
"count of mandatory (pre-)arguments");
1152 F_NODE(nd_ainfo.pre_init, RNODE_ARGS,
"initialization of (pre-)arguments");
1153 F_INT(nd_ainfo.post_args_num, RNODE_ARGS,
"count of mandatory post-arguments");
1154 F_NODE(nd_ainfo.post_init, RNODE_ARGS,
"initialization of post-arguments");
1155 F_ID(nd_ainfo.first_post_arg, RNODE_ARGS,
"first post argument");
1156 F_CUSTOM1(nd_ainfo.rest_arg,
"rest argument") {
1157 if (RNODE_ARGS(node)->nd_ainfo.rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA) {
1158 A(
"1 (excessed comma)");
1161 A_ID(RNODE_ARGS(node)->nd_ainfo.rest_arg);
1164 F_ID(nd_ainfo.block_arg, RNODE_ARGS,
"block argument");
1165 F_NODE(nd_ainfo.opt_args, RNODE_ARGS,
"optional arguments");
1166 F_NODE(nd_ainfo.kw_args, RNODE_ARGS,
"keyword arguments");
1168 F_NODE(nd_ainfo.kw_rest_arg, RNODE_ARGS,
"keyword rest argument");
1173 ANN(
"format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
1174 F_CUSTOM1(nd_tbl,
"local table") {
1175 rb_ast_id_table_t *tbl = RNODE_SCOPE(node)->nd_tbl;
1177 int size = tbl ? tbl->size : 0;
1178 if (size == 0) A(
"(empty)");
1179 for (i = 0; i < size; i++) {
1180 A_ID(tbl->ids[i]);
if (i < size - 1) A(
",");
1183 F_NODE(nd_args, RNODE_SCOPE,
"arguments");
1185 F_NODE(nd_body, RNODE_SCOPE,
"body");
1189 ANN(
"array pattern");
1190 ANN(
"format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
1191 F_NODE(nd_pconst, RNODE_ARYPTN,
"constant");
1192 F_NODE(pre_args, RNODE_ARYPTN,
"pre arguments");
1193 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
1194 F_NODE(rest_arg, RNODE_ARYPTN,
"rest argument");
1197 F_MSG(rest_arg,
"rest argument",
"NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1200 F_NODE(post_args, RNODE_ARYPTN,
"post arguments");
1204 ANN(
"find pattern");
1205 ANN(
"format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
1206 F_NODE(nd_pconst, RNODE_FNDPTN,
"constant");
1207 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
1208 F_NODE(pre_rest_arg, RNODE_FNDPTN,
"pre rest argument");
1211 F_MSG(pre_rest_arg,
"pre rest argument",
"NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1213 F_NODE(args, RNODE_FNDPTN,
"arguments");
1216 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
1217 F_NODE(post_rest_arg, RNODE_FNDPTN,
"post rest argument");
1220 F_MSG(post_rest_arg,
"post rest argument",
"NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1225 ANN(
"hash pattern");
1226 ANN(
"format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
1227 F_NODE(nd_pconst, RNODE_HSHPTN,
"constant");
1228 F_NODE(nd_pkwargs, RNODE_HSHPTN,
"keyword arguments");
1230 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
1231 F_MSG(nd_pkwrestarg,
"keyword rest argument",
"NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
1234 F_NODE(nd_pkwrestarg, RNODE_HSHPTN,
"keyword rest argument");
1240 ANN(
"format: [lineno]");
1241 ANN(
"example: __LINE__");
1246 ANN(
"format: [path]");
1247 ANN(
"example: __FILE__");
1248 F_VALUE(path, rb_node_file_path_val(node),
"path");
1253 ANN(
"format: [enc]");
1254 ANN(
"example: __ENCODING__");
1255 F_VALUE(enc, rb_node_encoding_val(node),
"enc");
1259 ANN(
"Broken input recovered by Error Tolerant mode");
1267 rb_bug(
"dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
1271rb_parser_dump_tree(
const NODE *node,
int comment)
1274 "###########################################################\n"
1275 "## Do NOT use this node dump for any purpose other than ##\n"
1276 "## debug and research. Compatibility is not guaranteed. ##\n"
1277 "###########################################################\n\n"
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_CLASS
Old name of RUBY_T_CLASS.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
VALUE type(ANYARGS)
ANYARGS-ed function type.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.