[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / node_dump.c
blob37abea8441b4ea9b496a467b254e137b6bbe49ca
1 /**********************************************************************
3 node_dump.c - dump ruby node tree
5 $Author: mame $
6 created at: 09/12/06 21:23:44 JST
8 Copyright (C) 2009 Yusuke Endoh
10 **********************************************************************/
12 #include "internal.h"
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"
18 #include "vm_core.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)( \
52 comment ? \
53 rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
54 rb_strlen_lit(name))
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"); \
67 break; \
68 case rb_parser_shareable_literal: \
69 rb_str_cat_cstr(buf, "literal"); \
70 break; \
71 case rb_parser_shareable_copy: \
72 rb_str_cat_cstr(buf, "experimental_copy"); \
73 break; \
74 case rb_parser_shareable_everything: \
75 rb_str_cat_cstr(buf, "experimental_everything"); \
76 break; \
79 #define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
80 #define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
81 #define F_ID(name, type, ann) SIMPLE_FIELD1(#name, ann) A_ID(type(node)->name)
82 #define F_INT(name, type, ann) SIMPLE_FIELD1(#name, ann) A_INT(type(node)->name)
83 #define F_LONG(name, type, ann) SIMPLE_FIELD1(#name, ann) A_LONG(type(node)->name)
84 #define F_LIT(name, type, ann) SIMPLE_FIELD1(#name, ann) A_LIT(type(node)->name)
85 #define F_VALUE(name, val, ann) SIMPLE_FIELD1(#name, ann) A_LIT(val)
86 #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
87 #define F_SHAREABILITY(name, type, ann) SIMPLE_FIELD1(#name, ann) A_SHAREABILITY(type(node)->name)
89 #define F_NODE(name, type, ann) \
90 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, RNODE(type(node)->name));}
92 #define F_NODE2(name, n, ann) \
93 COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, n);}
95 #define ANN(ann) \
96 if (comment) { \
97 A_INDENT; A("| # " ann "\n"); \
100 #define LAST_NODE (next_indent = " ")
102 VALUE
103 rb_dump_literal(VALUE lit)
105 if (!RB_SPECIAL_CONST_P(lit)) {
106 VALUE str;
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);
113 return str;
114 default:
115 break;
118 return rb_inspect(lit);
121 static void
122 add_indent(VALUE buf, VALUE indent)
124 AR(indent);
127 static void
128 add_id(VALUE buf, ID id)
130 if (id == 0) {
131 A("(null)");
133 else {
134 VALUE str = rb_id2str(id);
135 if (str) {
136 A(":"); AR(str);
138 else {
139 rb_str_catf(buf, "(internal variable: 0x%"PRIsVALUE")", id);
144 struct add_option_arg {
145 VALUE buf, indent;
146 st_index_t count;
149 static void dump_node(VALUE, VALUE, int, const NODE *);
150 static const char default_indent[] = "| ";
152 static void
153 dump_array(VALUE buf, VALUE indent, int comment, const NODE *node)
155 int field_flag;
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");
163 LAST_NODE;
164 F_NODE(nd_next, RNODE_LIST, "next element");
167 static void
168 dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
170 int field_flag;
171 int i;
172 const char *next_indent = default_indent;
173 enum node_type type;
175 if (!node) {
176 D_NULL_NODE;
177 return;
180 D_NODE_HEADER(node);
182 type = nd_type(node);
183 switch (type) {
184 case NODE_BLOCK:
185 ANN("statement sequence");
186 ANN("format: [nd_head]; ...; [nd_next]");
187 ANN("example: foo; bar");
188 i = 0;
189 do {
190 A_INDENT;
191 rb_str_catf(buf, "+- nd_head (%s%d):\n",
192 comment ? "statement #" : "", ++i);
193 if (!RNODE_BLOCK(node)->nd_next) LAST_NODE;
194 D_INDENT;
195 dump_node(buf, indent, comment, RNODE_BLOCK(node)->nd_head);
196 D_DEDENT;
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) {
201 LAST_NODE;
202 F_NODE(nd_next, RNODE_BLOCK, "next block");
204 return;
206 case NODE_IF:
207 ANN("if statement");
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");
212 LAST_NODE;
213 F_NODE(nd_else, RNODE_IF, "else clause");
214 return;
216 case NODE_UNLESS:
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");
222 LAST_NODE;
223 F_NODE(nd_else, RNODE_UNLESS, "else clause");
224 return;
226 case NODE_CASE:
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");
231 LAST_NODE;
232 F_NODE(nd_body, RNODE_CASE, "when clauses");
233 return;
234 case NODE_CASE2:
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");
239 LAST_NODE;
240 F_NODE(nd_body, RNODE_CASE2, "when clauses");
241 return;
242 case NODE_CASE3:
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");
247 LAST_NODE;
248 F_NODE(nd_body, RNODE_CASE3, "in clauses");
249 return;
251 case NODE_WHEN:
252 ANN("when clause");
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");
257 LAST_NODE;
258 F_NODE(nd_next, RNODE_WHEN, "next when clause");
259 return;
261 case NODE_IN:
262 ANN("in 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");
267 LAST_NODE;
268 F_NODE(nd_next, RNODE_IN, "next in clause");
269 return;
271 case NODE_WHILE:
272 ANN("while statement");
273 ANN("format: while [nd_cond]; [nd_body]; end");
274 ANN("example: while x == 1; foo; end");
275 goto loop;
276 case NODE_UNTIL:
277 ANN("until statement");
278 ANN("format: until [nd_cond]; [nd_body]; end");
279 ANN("example: until x == 1; foo; end");
280 loop:
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");
286 LAST_NODE;
287 F_NODE(nd_body, RNODE_WHILE, "body");
288 return;
290 case NODE_ITER:
291 ANN("method call with block");
292 ANN("format: [nd_iter] { [nd_body] }");
293 ANN("example: 3.times { foo }");
294 goto iter;
295 case NODE_FOR:
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");
299 iter:
300 F_NODE(nd_iter, RNODE_ITER, "iteration receiver");
301 LAST_NODE;
302 F_NODE(nd_body, RNODE_ITER, "body");
303 return;
305 case NODE_FOR_MASGN:
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");
309 LAST_NODE;
310 F_NODE(nd_var, RNODE_FOR_MASGN, "var");
311 return;
313 case NODE_BREAK:
314 ANN("break statement");
315 ANN("format: break [nd_stts]");
316 ANN("example: break 1");
317 LAST_NODE;
318 F_NODE(nd_stts, RNODE_BREAK, "value");
319 return;
320 case NODE_NEXT:
321 ANN("next statement");
322 ANN("format: next [nd_stts]");
323 ANN("example: next 1");
324 LAST_NODE;
325 F_NODE(nd_stts, RNODE_NEXT, "value");
326 return;
327 case NODE_RETURN:
328 ANN("return statement");
329 ANN("format: return [nd_stts]");
330 ANN("example: return 1");
331 LAST_NODE;
332 F_NODE(nd_stts, RNODE_RETURN, "value");
333 return;
335 case NODE_REDO:
336 ANN("redo statement");
337 ANN("format: redo");
338 ANN("example: redo");
339 return;
341 case NODE_RETRY:
342 ANN("retry statement");
343 ANN("format: retry");
344 ANN("example: retry");
345 return;
347 case NODE_BEGIN:
348 ANN("begin statement");
349 ANN("format: begin; [nd_body]; end");
350 ANN("example: begin; 1; end");
351 LAST_NODE;
352 F_NODE(nd_body, RNODE_BEGIN, "body");
353 return;
355 case NODE_RESCUE:
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");
361 LAST_NODE;
362 F_NODE(nd_else, RNODE_RESCUE, "rescue else clause");
363 return;
365 case NODE_RESBODY:
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");
371 LAST_NODE;
372 F_NODE(nd_next, RNODE_RESBODY, "next rescue clause");
373 return;
375 case NODE_ENSURE:
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");
380 LAST_NODE;
381 F_NODE(nd_ensr, RNODE_ENSURE, "ensure clause");
382 return;
384 case NODE_AND:
385 ANN("&& operator");
386 ANN("format: [nd_1st] && [nd_2nd]");
387 ANN("example: foo && bar");
388 goto andor;
389 case NODE_OR:
390 ANN("|| operator");
391 ANN("format: [nd_1st] || [nd_2nd]");
392 ANN("example: foo || bar");
393 andor:
394 while (1) {
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))
397 break;
398 node = RNODE_AND(node)->nd_2nd;
400 LAST_NODE;
401 F_NODE(nd_2nd, RNODE_AND, "right expr");
402 return;
404 case NODE_MASGN:
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)) {
411 LAST_NODE;
412 F_NODE(nd_args, RNODE_MASGN, "splatn");
414 else {
415 F_MSG(nd_args, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
417 return;
419 case NODE_LASGN:
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)");
427 else {
428 LAST_NODE;
429 F_NODE(nd_value, RNODE_LASGN, "rvalue");
431 return;
432 case NODE_DASGN:
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)");
441 else {
442 LAST_NODE;
443 F_NODE(nd_value, RNODE_DASGN, "rvalue");
445 return;
446 case NODE_IASGN:
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");
451 LAST_NODE;
452 F_NODE(nd_value, RNODE_IASGN, "rvalue");
453 return;
454 case NODE_CVASGN:
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");
459 LAST_NODE;
460 F_NODE(nd_value, RNODE_CVASGN, "rvalue");
461 return;
462 case NODE_GASGN:
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");
467 LAST_NODE;
468 F_NODE(nd_value, RNODE_GASGN, "rvalue");
469 return;
471 case NODE_CDECL:
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");
479 else {
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");
484 LAST_NODE;
485 F_NODE(nd_value, RNODE_CDECL, "rvalue");
486 return;
488 case NODE_OP_ASGN1:
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");
495 LAST_NODE;
496 F_NODE(nd_rvalue, RNODE_OP_ASGN1, "rvalue");
497 return;
499 case NODE_OP_ASGN2:
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");
509 LAST_NODE;
510 F_NODE(nd_value, RNODE_OP_ASGN2, "rvalue");
511 return;
513 case NODE_OP_ASGN_AND:
514 ANN("assignment with && operator");
515 ANN("format: [nd_head] &&= [nd_value]");
516 ANN("example: foo &&= bar");
517 goto asgn_andor;
518 case NODE_OP_ASGN_OR:
519 ANN("assignment with || operator");
520 ANN("format: [nd_head] ||= [nd_value]");
521 ANN("example: foo ||= bar");
522 asgn_andor:
523 F_NODE(nd_head, RNODE_OP_ASGN_AND, "variable");
524 LAST_NODE;
525 F_NODE(nd_value, RNODE_OP_ASGN_AND, "rvalue");
526 return;
528 case NODE_OP_CDECL:
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");
535 LAST_NODE;
536 F_NODE(nd_value, RNODE_OP_CDECL, "rvalue");
537 return;
539 case NODE_CALL:
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");
545 LAST_NODE;
546 F_NODE(nd_args, RNODE_CALL, "arguments");
547 return;
549 case NODE_OPCALL:
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");
555 LAST_NODE;
556 F_NODE(nd_args, RNODE_OPCALL, "arguments");
557 return;
559 case NODE_FCALL:
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");
564 LAST_NODE;
565 F_NODE(nd_args, RNODE_FCALL, "arguments");
566 return;
568 case NODE_VCALL:
569 ANN("function call with no argument");
570 ANN("format: [nd_mid]");
571 ANN("example: foo");
572 F_ID(nd_mid, RNODE_VCALL, "method id");
573 return;
575 case NODE_QCALL:
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");
581 LAST_NODE;
582 F_NODE(nd_args, RNODE_QCALL, "arguments");
583 return;
585 case NODE_SUPER:
586 ANN("super invocation");
587 ANN("format: super [nd_args]");
588 ANN("example: super 1");
589 LAST_NODE;
590 F_NODE(nd_args, RNODE_SUPER, "arguments");
591 return;
593 case NODE_ZSUPER:
594 ANN("super invocation with no argument");
595 ANN("format: super");
596 ANN("example: super");
597 return;
599 case NODE_LIST:
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);
604 return;
606 case NODE_ZLIST:
607 ANN("empty list constructor");
608 ANN("format: []");
609 ANN("example: []");
610 return;
612 case NODE_HASH:
613 if (!RNODE_HASH(node)->nd_brace) {
614 ANN("keyword arguments");
615 ANN("format: [nd_head]");
616 ANN("example: a: 1, b: 2");
618 else {
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;
629 LAST_NODE;
630 F_NODE(nd_head, RNODE_HASH, "contents");
631 return;
633 case NODE_YIELD:
634 ANN("yield invocation");
635 ANN("format: yield [nd_head]");
636 ANN("example: yield 1");
637 LAST_NODE;
638 F_NODE(nd_head, RNODE_YIELD, "arguments");
639 return;
641 case NODE_LVAR:
642 ANN("local variable reference");
643 ANN("format: [nd_vid](lvar)");
644 ANN("example: x");
645 F_ID(nd_vid, RNODE_LVAR, "local variable");
646 return;
647 case NODE_DVAR:
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");
652 return;
653 case NODE_IVAR:
654 ANN("instance variable reference");
655 ANN("format: [nd_vid](ivar)");
656 ANN("example: @x");
657 F_ID(nd_vid, RNODE_IVAR, "instance variable");
658 return;
659 case NODE_CONST:
660 ANN("constant reference");
661 ANN("format: [nd_vid](constant)");
662 ANN("example: X");
663 F_ID(nd_vid, RNODE_CONST, "constant");
664 return;
665 case NODE_CVAR:
666 ANN("class variable reference");
667 ANN("format: [nd_vid](cvar)");
668 ANN("example: @@x");
669 F_ID(nd_vid, RNODE_CVAR, "class variable");
670 return;
672 case NODE_GVAR:
673 ANN("global variable reference");
674 ANN("format: [nd_vid](gvar)");
675 ANN("example: $x");
676 F_ID(nd_vid, RNODE_GVAR, "global variable");
677 return;
679 case NODE_NTH_REF:
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); }
684 return;
686 case NODE_BACK_REF:
687 ANN("back special variable reference");
688 ANN("format: $[nd_nth]");
689 ANN("example: $&, $`, $', $+");
690 F_CUSTOM1(nd_nth, "variable") {
691 char name[3] = "$ ";
692 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
693 A(name);
695 return;
697 case NODE_MATCH:
698 ANN("match expression (against $_ implicitly)");
699 ANN("format: [nd_lit] (in condition)");
700 ANN("example: if /foo/; foo; end");
701 LAST_NODE;
702 F_VALUE(string, rb_node_regx_string_val(node), "string");
703 return;
705 case NODE_MATCH2:
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) {
713 LAST_NODE;
714 F_NODE(nd_args, RNODE_MATCH2, "named captures");
716 return;
718 case NODE_MATCH3:
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)");
723 LAST_NODE;
724 F_NODE(nd_value, RNODE_MATCH3, "regexp (argument)");
725 return;
727 case NODE_STR:
728 ANN("string literal");
729 ANN("format: [nd_lit]");
730 ANN("example: 'foo'");
731 goto str;
732 case NODE_XSTR:
733 ANN("xstring literal");
734 ANN("format: [nd_lit]");
735 ANN("example: `foo`");
736 str:
737 F_VALUE(string, rb_node_str_string_val(node), "literal");
738 return;
740 case NODE_INTEGER:
741 ANN("integer literal");
742 ANN("format: [val]");
743 ANN("example: 1");
744 F_VALUE(val, rb_node_integer_literal_val(node), "val");
745 return;
747 case NODE_FLOAT:
748 ANN("float literal");
749 ANN("format: [val]");
750 ANN("example: 1.2");
751 F_VALUE(val, rb_node_float_literal_val(node), "val");
752 return;
754 case NODE_RATIONAL:
755 ANN("rational number literal");
756 ANN("format: [val]");
757 ANN("example: 1r");
758 F_VALUE(val, rb_node_rational_literal_val(node), "val");
759 return;
761 case NODE_IMAGINARY:
762 ANN("complex number literal");
763 ANN("format: [val]");
764 ANN("example: 1i");
765 F_VALUE(val, rb_node_imaginary_literal_val(node), "val");
766 return;
768 case NODE_REGX:
769 ANN("regexp literal");
770 ANN("format: [string]");
771 ANN("example: /foo/");
772 LAST_NODE;
773 F_VALUE(string, rb_node_regx_string_val(node), "string");
774 return;
776 case NODE_ONCE:
777 ANN("once evaluation");
778 ANN("format: [nd_body]");
779 ANN("example: /foo#{ bar }baz/o");
780 LAST_NODE;
781 F_NODE(nd_body, RNODE_ONCE, "body");
782 return;
784 case NODE_DSTR:
785 ANN("string literal with interpolation");
786 ANN("format: [nd_lit]");
787 ANN("example: \"foo#{ bar }baz\"");
788 goto dlit;
789 case NODE_DXSTR:
790 ANN("xstring literal with interpolation");
791 ANN("format: [nd_lit]");
792 ANN("example: `foo#{ bar }baz`");
793 goto dlit;
794 case NODE_DREGX:
795 ANN("regexp literal with interpolation");
796 ANN("format: [nd_lit]");
797 ANN("example: /foo#{ bar }baz/");
798 goto dlit;
799 case NODE_DSYM:
800 ANN("symbol literal with interpolation");
801 ANN("format: [nd_lit]");
802 ANN("example: :\"foo#{ bar }baz\"");
803 dlit:
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");
807 LAST_NODE;
808 F_NODE(nd_next->nd_next, RNODE_DSTR, "tailing strings");
809 return;
811 case NODE_SYM:
812 ANN("symbol literal");
813 ANN("format: [string]");
814 ANN("example: :foo");
815 F_VALUE(string, rb_node_sym_string_val(node), "string");
816 return;
818 case NODE_EVSTR:
819 ANN("interpolation expression");
820 ANN("format: \"..#{ [nd_body] }..\"");
821 ANN("example: \"foo#{ bar }baz\"");
822 LAST_NODE;
823 F_NODE(nd_body, RNODE_EVSTR, "body");
824 return;
826 case NODE_ARGSCAT:
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");
831 LAST_NODE;
832 F_NODE(nd_body, RNODE_ARGSCAT, "following array");
833 return;
835 case NODE_ARGSPUSH:
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");
840 LAST_NODE;
841 F_NODE(nd_body, RNODE_ARGSPUSH, "following element");
842 return;
844 case NODE_SPLAT:
845 ANN("splat argument");
846 ANN("format: *[nd_head]");
847 ANN("example: foo(*ary)");
848 LAST_NODE;
849 F_NODE(nd_head, RNODE_SPLAT, "splat'ed array");
850 return;
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");
857 LAST_NODE;
858 F_NODE(nd_body, RNODE_BLOCK_PASS, "block argument");
859 return;
861 case NODE_DEFN:
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");
866 LAST_NODE;
867 F_NODE(nd_defn, RNODE_DEFN, "method definition");
868 return;
870 case NODE_DEFS:
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");
876 LAST_NODE;
877 F_NODE(nd_defn, RNODE_DEFS, "method definition");
878 return;
880 case NODE_ALIAS:
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");
885 LAST_NODE;
886 F_NODE(nd_2nd, RNODE_ALIAS, "old name");
887 return;
889 case NODE_VALIAS:
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");
895 return;
897 case NODE_UNDEF:
898 ANN("method undef statement");
899 ANN("format: undef [nd_undef]");
900 ANN("example: undef foo");
901 LAST_NODE;
902 F_NODE(nd_undef, RNODE_UNDEF, "old name");
903 return;
905 case NODE_CLASS:
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");
911 LAST_NODE;
912 F_NODE(nd_body, RNODE_CLASS, "class definition");
913 return;
915 case NODE_MODULE:
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");
920 LAST_NODE;
921 F_NODE(nd_body, RNODE_MODULE, "module definition");
922 return;
924 case NODE_SCLASS:
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");
929 LAST_NODE;
930 F_NODE(nd_body, RNODE_SCLASS, "singleton class definition");
931 return;
933 case NODE_COLON2:
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");
938 LAST_NODE;
939 F_NODE(nd_head, RNODE_COLON2, "receiver");
940 return;
942 case NODE_COLON3:
943 ANN("top-level constant reference");
944 ANN("format: ::[nd_mid]");
945 ANN("example: ::Object");
946 F_ID(nd_mid, RNODE_COLON3, "constant name");
947 return;
949 case NODE_DOT2:
950 ANN("range constructor (incl.)");
951 ANN("format: [nd_beg]..[nd_end]");
952 ANN("example: 1..5");
953 goto dot;
954 case NODE_DOT3:
955 ANN("range constructor (excl.)");
956 ANN("format: [nd_beg]...[nd_end]");
957 ANN("example: 1...5");
958 goto dot;
959 case NODE_FLIP2:
960 ANN("flip-flop condition (incl.)");
961 ANN("format: [nd_beg]..[nd_end]");
962 ANN("example: if (x==1)..(x==5); foo; end");
963 goto dot;
964 case NODE_FLIP3:
965 ANN("flip-flop condition (excl.)");
966 ANN("format: [nd_beg]...[nd_end]");
967 ANN("example: if (x==1)...(x==5); foo; end");
968 dot:
969 F_NODE(nd_beg, RNODE_DOT2, "begin");
970 LAST_NODE;
971 F_NODE(nd_end, RNODE_DOT2, "end");
972 return;
974 case NODE_SELF:
975 ANN("self");
976 ANN("format: self");
977 ANN("example: self");
978 F_CUSTOM1(nd_state, "nd_state") {
979 A_INT((int)RNODE_SELF(node)->nd_state);
981 return;
983 case NODE_NIL:
984 ANN("nil");
985 ANN("format: nil");
986 ANN("example: nil");
987 return;
989 case NODE_TRUE:
990 ANN("true");
991 ANN("format: true");
992 ANN("example: true");
993 return;
995 case NODE_FALSE:
996 ANN("false");
997 ANN("format: false");
998 ANN("example: false");
999 return;
1001 case NODE_ERRINFO:
1002 ANN("virtual reference to $!");
1003 ANN("format: rescue => id");
1004 ANN("example: rescue => id");
1005 return;
1007 case NODE_DEFINED:
1008 ANN("defined? expression");
1009 ANN("format: defined?([nd_head])");
1010 ANN("example: defined?(foo)");
1011 F_NODE(nd_head, RNODE_DEFINED, "expr");
1012 return;
1014 case NODE_POSTEXE:
1015 ANN("post-execution");
1016 ANN("format: END { [nd_body] }");
1017 ANN("example: END { foo }");
1018 LAST_NODE;
1019 F_NODE(nd_body, RNODE_POSTEXE, "END clause");
1020 return;
1022 case NODE_ATTRASGN:
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");
1028 LAST_NODE;
1029 F_NODE(nd_args, RNODE_ATTRASGN, "arguments");
1030 return;
1032 case NODE_LAMBDA:
1033 ANN("lambda expression");
1034 ANN("format: -> [nd_body]");
1035 ANN("example: -> { foo }");
1036 LAST_NODE;
1037 F_NODE(nd_body, RNODE_LAMBDA, "lambda clause");
1038 return;
1040 case NODE_OPT_ARG:
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");
1045 LAST_NODE;
1046 F_NODE(nd_next, RNODE_OPT_ARG, "next");
1047 return;
1049 case NODE_KW_ARG:
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");
1054 LAST_NODE;
1055 F_NODE(nd_next, RNODE_KW_ARG, "next");
1056 return;
1058 case NODE_POSTARG:
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");
1065 else {
1066 F_MSG(nd_1st, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1068 LAST_NODE;
1069 F_NODE(nd_2nd, RNODE_POSTARG, "post arguments");
1070 return;
1072 case NODE_ARGS:
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)");
1085 else {
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");
1092 LAST_NODE;
1093 F_NODE(nd_ainfo.kw_rest_arg, RNODE_ARGS, "keyword rest argument");
1094 return;
1096 case NODE_SCOPE:
1097 ANN("new scope");
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;
1101 int i;
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");
1109 LAST_NODE;
1110 F_NODE(nd_body, RNODE_SCOPE, "body");
1111 return;
1113 case NODE_ARYPTN:
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");
1121 else {
1122 F_MSG(rest_arg, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1124 LAST_NODE;
1125 F_NODE(post_args, RNODE_ARYPTN, "post arguments");
1126 return;
1128 case NODE_FNDPTN:
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");
1135 else {
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");
1140 LAST_NODE;
1141 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
1142 F_NODE(post_rest_arg, RNODE_FNDPTN, "post rest argument");
1144 else {
1145 F_MSG(post_rest_arg, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
1147 return;
1149 case NODE_HSHPTN:
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");
1154 LAST_NODE;
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)");
1158 else {
1159 F_NODE(nd_pkwrestarg, RNODE_HSHPTN, "keyword rest argument");
1161 return;
1163 case NODE_LINE:
1164 ANN("line");
1165 ANN("format: [lineno]");
1166 ANN("example: __LINE__");
1167 return;
1169 case NODE_FILE:
1170 ANN("line");
1171 ANN("format: [path]");
1172 ANN("example: __FILE__");
1173 F_VALUE(path, rb_node_file_path_val(node), "path");
1174 return;
1176 case NODE_ENCODING:
1177 ANN("encoding");
1178 ANN("format: [enc]");
1179 ANN("example: __ENCODING__");
1180 F_VALUE(enc, rb_node_encoding_val(node), "enc");
1181 return;
1183 case NODE_ERROR:
1184 ANN("Broken input recovered by Error Tolerant mode");
1185 return;
1187 case NODE_ARGS_AUX:
1188 case NODE_LAST:
1189 break;
1192 rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
1195 VALUE
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);
1205 return buf;