1/**********************************************************************
2
3  node.c - ruby node tree
4
5  $Author: mame $
6  created at: 09/12/06 21:23:44 JST
7
8  Copyright (C) 2009 Yusuke Endoh
9
10**********************************************************************/
11
12#include "ruby/ruby.h"
13#include "vm_core.h"
14
15#define A(str) rb_str_cat2(buf, (str))
16#define AR(str) rb_str_concat(buf, (str))
17
18#define A_INDENT add_indent(buf, indent)
19#define A_ID(id) add_id(buf, (id))
20#define A_INT(val) rb_str_catf(buf, "%d", (val))
21#define A_LONG(val) rb_str_catf(buf, "%ld", (val))
22#define A_LIT(lit) AR(rb_inspect(lit))
23#define A_NODE_HEADER(node) \
24    rb_str_catf(buf, "@ %s (line: %d)", ruby_node_name(nd_type(node)), nd_line(node))
25#define A_FIELD_HEADER(name) \
26    rb_str_catf(buf, "+- %s:", (name))
27
28#define D_NULL_NODE A_INDENT; A("(null node)"); A("\n");
29#define D_NODE_HEADER(node) A_INDENT; A_NODE_HEADER(node); A("\n");
30
31#define COMPOUND_FIELD(name, name2, block) \
32    do { \
33	A_INDENT; A_FIELD_HEADER(comment ? (name2) : (name)); A("\n"); \
34	rb_str_cat2(indent, next_indent); \
35	block; \
36	rb_str_resize(indent, RSTRING_LEN(indent) - 4); \
37    } while (0)
38
39#define SIMPLE_FIELD(name, name2, block) \
40    do { \
41	A_INDENT; A_FIELD_HEADER(comment ? (name2) : (name)); A(" "); block; A("\n"); \
42    } while (0)
43
44#define F_CUSTOM1(name, ann, block) SIMPLE_FIELD(#name, #name " (" ann ")", block)
45#define F_ID(name, ann)		    SIMPLE_FIELD(#name, #name " (" ann ")", A_ID(node->name))
46#define F_GENTRY(name, ann)	    SIMPLE_FIELD(#name, #name " (" ann ")", A_ID((node->name)->id))
47#define F_INT(name, ann)	    SIMPLE_FIELD(#name, #name " (" ann ")", A_INT(node->name))
48#define F_LONG(name, ann)	    SIMPLE_FIELD(#name, #name " (" ann ")", A_LONG(node->name))
49#define F_LIT(name, ann)	    SIMPLE_FIELD(#name, #name " (" ann ")", A_LIT(node->name))
50#define F_MSG(name, ann, desc)	    SIMPLE_FIELD(#name, #name " (" ann ")", A(desc))
51
52#define F_CUSTOM2(name, ann, block) \
53    COMPOUND_FIELD(#name, #name " (" ann ")", block)
54
55#define F_NODE(name, ann) \
56    COMPOUND_FIELD(#name, #name " (" ann ")", dump_node(buf, indent, comment, node->name))
57
58#define ANN(ann) \
59    if (comment) { \
60	A_INDENT; A("| # "); A(ann); A("\n"); \
61    }
62
63#define LAST_NODE (next_indent = "    ")
64
65static void
66add_indent(VALUE buf, VALUE indent)
67{
68    AR(indent);
69}
70
71static void
72add_id(VALUE buf, ID id)
73{
74    if (id == 0) {
75	A("(null)");
76    }
77    else {
78	VALUE str = rb_id2str(id);
79	if (str) {
80	    A(":"); AR(str);
81	}
82	else {
83	    A("(internal variable)");
84	}
85    }
86}
87
88static void
89dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
90{
91    const char *next_indent = "|   ";
92
93    if (!node) {
94	D_NULL_NODE;
95	return;
96    }
97
98    D_NODE_HEADER(node);
99
100    switch (nd_type(node)) {
101      case NODE_BLOCK:
102	ANN("statement sequence");
103	ANN("format: [nd_head]; [nd_next]");
104	ANN("example: foo; bar");
105	F_NODE(nd_head, "current statement");
106	LAST_NODE;
107	F_NODE(nd_next, "next block");
108	break;
109
110      case NODE_IF:
111	ANN("if statement");
112	ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
113	ANN("example: if x == 1 then foo else bar end");
114	F_NODE(nd_cond, "condition expr");
115	F_NODE(nd_body, "then clause");
116	LAST_NODE;
117	F_NODE(nd_else, "else clause");
118	break;
119
120      case NODE_CASE:
121	ANN("case statement");
122	ANN("format: case [nd_head]; [nd_body]; end");
123	ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
124	F_NODE(nd_head, "case expr");
125	LAST_NODE;
126	F_NODE(nd_body, "when clauses");
127	break;
128
129      case NODE_WHEN:
130	ANN("if statement");
131	ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
132	ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
133	F_NODE(nd_head, "when value");
134	F_NODE(nd_body, "when clause");
135	LAST_NODE;
136	F_NODE(nd_next, "next when clause");
137	break;
138
139      case NODE_OPT_N:
140	ANN("wrapper for -n option");
141	ANN("format: ruby -ne '[nd_body]' (nd_cond is `gets')");
142	ANN("example: ruby -ne 'p $_'");
143	goto loop;
144      case NODE_WHILE:
145	ANN("while statement");
146	ANN("format: while [nd_cond]; [nd_body]; end");
147	ANN("example: while x == 1; foo; end");
148	goto loop;
149      case NODE_UNTIL:
150	ANN("until statement");
151	ANN("format: until [nd_cond]; [nd_body]; end");
152	ANN("example: until x == 1; foo; end");
153      loop:
154	F_CUSTOM1(nd_state, "begin-end-while?", {
155	   A_INT((int)node->nd_state);
156	   A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
157	});
158	F_NODE(nd_cond, "condition");
159	LAST_NODE;
160	F_NODE(nd_body, "body");
161	break;
162
163      case NODE_ITER:
164	ANN("method call with block");
165	ANN("format: [nd_iter] { [nd_body] }");
166	ANN("example: 3.times { foo }");
167	goto iter;
168      case NODE_FOR:
169	ANN("for statement");
170	ANN("format: for * in [nd_iter] do [nd_body] end");
171	ANN("example: for i in 1..3 do foo end");
172	iter:
173	F_NODE(nd_iter, "iteration receiver");
174	LAST_NODE;
175	F_NODE(nd_body, "body");
176	break;
177
178      case NODE_BREAK:
179	ANN("for statement");
180	ANN("format: break [nd_stts]");
181	ANN("example: break 1");
182	goto jump;
183      case NODE_NEXT:
184	ANN("next statement");
185	ANN("format: next [nd_stts]");
186	ANN("example: next 1");
187	goto jump;
188      case NODE_RETURN:
189	ANN("return statement");
190	ANN("format: return [nd_stts]");
191	ANN("example: return 1");
192        jump:
193	LAST_NODE;
194	F_NODE(nd_stts, "value");
195	break;
196
197      case NODE_REDO:
198	ANN("redo statement");
199	ANN("format: redo");
200	ANN("example: redo");
201	break;
202
203      case NODE_RETRY:
204	ANN("retry statement");
205	ANN("format: retry");
206	ANN("example: retry");
207	break;
208
209      case NODE_BEGIN:
210	ANN("begin statement");
211	ANN("format: begin; [nd_body]; end");
212	ANN("example: begin; 1; end");
213	LAST_NODE;
214	F_NODE(nd_body, "body");
215	break;
216
217      case NODE_RESCUE:
218	ANN("rescue clause");
219	ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
220	ANN("example: begin; foo; rescue; bar; else; baz; end");
221	F_NODE(nd_head, "body");
222	F_NODE(nd_resq, "rescue clause list");
223	LAST_NODE;
224	F_NODE(nd_else, "rescue else clause");
225	break;
226
227      case NODE_RESBODY:
228	ANN("rescue clause (cont'd)");
229	ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
230	ANN("example: begin; foo; rescue; bar; else; baz; end");
231	F_NODE(nd_args, "rescue exceptions");
232	F_NODE(nd_body, "rescue clause");
233	LAST_NODE;
234	F_NODE(nd_head, "next rescue clause");
235	break;
236
237      case NODE_ENSURE:
238	ANN("ensure clause");
239	ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
240	ANN("example: begin; foo; ensure; bar; end");
241	F_NODE(nd_head, "body");
242	LAST_NODE;
243	F_NODE(nd_ensr, "ensure clause");
244	break;
245
246      case NODE_AND:
247	ANN("&& operator");
248	ANN("format: [nd_1st] && [nd_2nd]");
249	ANN("example: foo && bar");
250	goto andor;
251      case NODE_OR:
252	ANN("|| operator");
253	ANN("format: [nd_1st] || [nd_2nd]");
254	ANN("example: foo && bar");
255	andor:
256	F_NODE(nd_1st, "left expr");
257	LAST_NODE;
258	F_NODE(nd_2nd, "right expr");
259	break;
260
261      case NODE_MASGN:
262	ANN("multiple assignment");
263	ANN("format: [nd_head], [nd_args] = [nd_value]");
264	ANN("example: a, b = foo");
265	F_NODE(nd_value, "rhsn");
266	F_NODE(nd_head, "lhsn");
267	if ((VALUE)node->nd_args != (VALUE)-1) {
268	    LAST_NODE;
269	    F_NODE(nd_args, "splatn");
270	}
271	else {
272	    F_MSG(nd_args, "splatn", "-1 (rest argument without name)");
273	}
274	break;
275
276      case NODE_LASGN:
277	ANN("local variable assignment");
278	ANN("format: [nd_vid](lvar) = [nd_value]");
279	ANN("example: x = foo");
280	goto asgn;
281      case NODE_DASGN:
282	ANN("dynamic variable assignment (out of current scope)");
283	ANN("format: [nd_vid](dvar) = [nd_value]");
284	ANN("example: x = nil; 1.times { x = foo }");
285	goto asgn;
286      case NODE_DASGN_CURR:
287	ANN("dynamic variable assignment (in current scope)");
288	ANN("format: [nd_vid](current dvar) = [nd_value]");
289	ANN("example: 1.times { x = foo }");
290	goto asgn;
291      case NODE_IASGN:
292	ANN("instance variable assignment");
293	ANN("format: [nd_vid](ivar) = [nd_value]");
294	ANN("example: @x = foo");
295	goto asgn;
296      case NODE_CVASGN:
297	ANN("class variable assignment");
298	ANN("format: [nd_vid](cvar) = [nd_value]");
299	ANN("example: @@x = foo");
300	asgn:
301	F_ID(nd_vid, "variable");
302	LAST_NODE;
303	F_NODE(nd_value, "rvalue");
304	break;
305
306      case NODE_GASGN:
307	ANN("global variable assignment");
308	ANN("format: [nd_entry](gvar) = [nd_value]");
309	ANN("example: $x = foo");
310	F_GENTRY(nd_entry, "global variable");
311	LAST_NODE;
312	F_NODE(nd_value, "rvalue");
313	break;
314
315      case NODE_CDECL:
316	ANN("constant declaration");
317	ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
318	ANN("example: X = foo");
319	if (node->nd_vid) {
320	   F_ID(nd_vid, "variable");
321	   F_MSG(nd_else, "extension", "not used");
322	}
323	else {
324	   F_MSG(nd_vid, "variable", "0 (see extension field)");
325	   F_NODE(nd_else, "extension");
326	}
327	LAST_NODE;
328	F_NODE(nd_value, "rvalue");
329	break;
330
331      case NODE_OP_ASGN1:
332	ANN("array assignment with operator");
333	ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
334	ANN("example: ary[1] += foo");
335	F_NODE(nd_recv, "receiver");
336	F_ID(nd_vid, "operator");
337	F_NODE(nd_args->nd_body, "index");
338	LAST_NODE;
339	F_NODE(nd_args->nd_head, "rvalue");
340	break;
341
342      case NODE_OP_ASGN2:
343	ANN("attr assignment with operator");
344	ANN("format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
345	ANN("          where [attr] reader: [nd_next->nd_vid]");
346	ANN("                [attr] writer: [nd_next->nd_aid]");
347	ANN("example: struct.field += foo");
348	F_NODE(nd_recv, "receiver");
349	F_ID(nd_next->nd_vid, "reader");
350	F_ID(nd_next->nd_aid, "writer");
351	F_CUSTOM1(nd_next->nd_mid, "operator", {
352	   switch (node->nd_next->nd_mid) {
353	   case 0: A("0 (||)"); break;
354	   case 1: A("1 (&&)"); break;
355	   default: A_ID(node->nd_next->nd_mid);
356	   }
357	});
358	LAST_NODE;
359	F_NODE(nd_value, "rvalue");
360	break;
361
362      case NODE_OP_ASGN_AND:
363	ANN("assignment with && operator");
364	ANN("format: [nd_head] &&= [nd_value]");
365	ANN("example: foo &&= bar");
366	goto asgn_andor;
367      case NODE_OP_ASGN_OR:
368	ANN("assignment with || operator");
369	ANN("format: [nd_head] ||= [nd_value]");
370	ANN("example: foo ||= bar");
371	asgn_andor:
372	F_NODE(nd_head, "variable");
373	LAST_NODE;
374	F_NODE(nd_value, "rvalue");
375	break;
376
377      case NODE_CALL:
378	ANN("method invocation");
379	ANN("format: [nd_recv].[nd_mid]([nd_args])");
380	ANN("example: obj.foo(1)");
381	F_ID(nd_mid, "method id");
382	F_NODE(nd_recv, "receiver");
383	LAST_NODE;
384	F_NODE(nd_args, "arguments");
385	break;
386
387      case NODE_FCALL:
388	ANN("function call");
389	ANN("format: [nd_mid]([nd_args])");
390	ANN("example: foo(1)");
391	F_ID(nd_mid, "method id");
392	LAST_NODE;
393	F_NODE(nd_args, "arguments");
394	break;
395
396      case NODE_VCALL:
397	ANN("function call with no argument");
398	ANN("format: [nd_mid]");
399	ANN("example: foo");
400	F_ID(nd_mid, "method id");
401	break;
402
403      case NODE_SUPER:
404	ANN("super invocation");
405	ANN("format: super [nd_args]");
406	ANN("example: super 1");
407	LAST_NODE;
408	F_NODE(nd_args, "arguments");
409	break;
410
411      case NODE_ZSUPER:
412	ANN("super invocation with no argument");
413	ANN("format: super");
414	ANN("example: super");
415	break;
416
417      case NODE_ARRAY:
418	ANN("array constructor");
419	ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
420	ANN("example: [1, 2, 3]");
421	goto ary;
422      case NODE_VALUES:
423	ANN("return arguments");
424	ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
425	ANN("example: return 1, 2, 3");
426	ary:
427	F_LONG(nd_alen, "length");
428	F_NODE(nd_head, "element");
429	LAST_NODE;
430	F_NODE(nd_next, "next element");
431	break;
432
433      case NODE_ZARRAY:
434	ANN("empty array constructor");
435	ANN("format: []");
436	ANN("example: []");
437	break;
438
439      case NODE_HASH:
440	ANN("hash constructor");
441	ANN("format: { [nd_head] }");
442	ANN("example: { 1 => 2, 3 => 4 }");
443	LAST_NODE;
444	F_NODE(nd_head, "contents");
445	break;
446
447      case NODE_YIELD:
448	ANN("yield invocation");
449	ANN("format: yield [nd_head]");
450	ANN("example: yield 1");
451	LAST_NODE;
452	F_NODE(nd_head, "arguments");
453	break;
454
455      case NODE_LVAR:
456	ANN("local variable reference");
457	ANN("format: [nd_vid](lvar)");
458	ANN("example: x");
459	goto var;
460      case NODE_DVAR:
461	ANN("dynamic variable reference");
462	ANN("format: [nd_vid](dvar)");
463	ANN("example: 1.times { x = 1; x }");
464	goto var;
465      case NODE_IVAR:
466	ANN("instance variable reference");
467	ANN("format: [nd_vid](ivar)");
468	ANN("example: @x");
469	goto var;
470      case NODE_CONST:
471	ANN("constant reference");
472	ANN("format: [nd_vid](constant)");
473	ANN("example: X");
474	goto var;
475      case NODE_CVAR:
476	ANN("class variable reference");
477	ANN("format: [nd_vid](cvar)");
478	ANN("example: @@x");
479        var:
480	F_ID(nd_vid, "local variable");
481	break;
482
483      case NODE_GVAR:
484	ANN("global variable reference");
485	ANN("format: [nd_entry](gvar)");
486	ANN("example: $x");
487	F_GENTRY(nd_entry, "global variable");
488	break;
489
490      case NODE_NTH_REF:
491	ANN("nth special variable reference");
492	ANN("format: $[nd_nth]");
493	ANN("example: $1, $2, ..");
494	F_CUSTOM1(nd_nth, "variable", { A("$"); A_LONG(node->nd_nth); });
495	break;
496
497      case NODE_BACK_REF:
498	ANN("back special variable reference");
499	ANN("format: $[nd_nth]");
500	ANN("example: $&, $`, $', $+");
501	F_CUSTOM1(nd_nth, "variable", {
502	   char name[3];
503	   name[0] = '$';
504	   name[1] = (char)node->nd_nth;
505	   name[2] = '\0';
506	   A(name);
507	});
508	break;
509
510      case NODE_MATCH:
511	ANN("match expression (against $_ implicitly)");
512        ANN("format: [nd_lit] (in condition)");
513	ANN("example: if /foo/; foo; end");
514	F_LIT(nd_lit, "regexp");
515	break;
516
517      case NODE_MATCH2:
518	ANN("match expression (regexp first)");
519        ANN("format: [nd_recv] =~ [nd_value]");
520	ANN("example: /foo/ =~ 'foo'");
521	F_NODE(nd_recv, "regexp (receiver)");
522	LAST_NODE;
523	F_NODE(nd_value, "string (argument)");
524	break;
525
526      case NODE_MATCH3:
527	ANN("match expression (regexp second)");
528        ANN("format: [nd_recv] =~ [nd_value]");
529	ANN("example: 'foo' =~ /foo/");
530	F_NODE(nd_recv, "string (receiver)");
531	LAST_NODE;
532	F_NODE(nd_value, "regexp (argument)");
533	break;
534
535      case NODE_LIT:
536	ANN("literal");
537	ANN("format: [nd_lit]");
538	ANN("example: 1, /foo/");
539	goto lit;
540      case NODE_STR:
541	ANN("string literal");
542	ANN("format: [nd_lit]");
543	ANN("example: 'foo'");
544	goto lit;
545      case NODE_XSTR:
546	ANN("xstring literal");
547	ANN("format: [nd_lit]");
548	ANN("example: `foo`");
549	lit:
550	F_LIT(nd_lit, "literal");
551	break;
552
553      case NODE_DSTR:
554	ANN("string literal with interpolation");
555	ANN("format: [nd_lit]");
556	ANN("example: \"foo#{ bar }baz\"");
557	goto dlit;
558      case NODE_DXSTR:
559	ANN("xstring literal with interpolation");
560	ANN("format: [nd_lit]");
561	ANN("example: `foo#{ bar }baz`");
562	goto dlit;
563      case NODE_DREGX:
564	ANN("regexp literal with interpolation");
565	ANN("format: [nd_lit]");
566	ANN("example: /foo#{ bar }baz/");
567	goto dlit;
568      case NODE_DREGX_ONCE:
569	ANN("regexp literal with interpolation and once flag");
570	ANN("format: [nd_lit]");
571	ANN("example: /foo#{ bar }baz/o");
572	goto dlit;
573      case NODE_DSYM:
574	ANN("symbol literal with interpolation");
575	ANN("format: [nd_lit]");
576	ANN("example: :\"foo#{ bar }baz\"");
577	dlit:
578	F_LIT(nd_lit, "literal");
579	F_NODE(nd_next->nd_head, "preceding string");
580	LAST_NODE;
581	F_NODE(nd_next->nd_next, "interpolation");
582	break;
583
584      case NODE_EVSTR:
585	ANN("interpolation expression");
586	ANN("format: \"..#{ [nd_lit] }..\"");
587	ANN("example: \"foo#{ bar }baz\"");
588	LAST_NODE;
589	F_NODE(nd_body, "body");
590	break;
591
592      case NODE_ARGSCAT:
593	ANN("splat argument following arguments");
594	ANN("format: ..(*[nd_head], [nd_body..])");
595	ANN("example: foo(*ary, post_arg1, post_arg2)");
596	F_NODE(nd_head, "preceding array");
597	LAST_NODE;
598	F_NODE(nd_body, "following array");
599	break;
600
601      case NODE_ARGSPUSH:
602	ANN("splat argument following one argument");
603	ANN("format: ..(*[nd_head], [nd_body])");
604	ANN("example: foo(*ary, post_arg)");
605	F_NODE(nd_head, "preceding array");
606	LAST_NODE;
607	F_NODE(nd_body, "following element");
608	break;
609
610      case NODE_SPLAT:
611	ANN("splat argument");
612	ANN("format: *[nd_head]");
613	ANN("example: foo(*ary)");
614	LAST_NODE;
615	F_NODE(nd_head, "splat'ed array");
616	break;
617
618      case NODE_BLOCK_PASS:
619	ANN("arguments with block argument");
620	ANN("format: ..([nd_head], &[nd_body])");
621	ANN("example: foo(x, &blk)");
622	F_NODE(nd_head, "other arguments");
623	LAST_NODE;
624	F_NODE(nd_body, "block argument");
625	break;
626
627      case NODE_DEFN:
628	ANN("method definition");
629	ANN("format: def [nd_mid] [nd_defn]; end");
630	ANN("example; def foo; bar; end");
631	F_ID(nd_mid, "method name");
632	LAST_NODE;
633	F_NODE(nd_defn, "method definition");
634	break;
635
636      case NODE_DEFS:
637	ANN("singleton method definition");
638	ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
639	ANN("example; def obj.foo; bar; end");
640	F_NODE(nd_recv, "receiver");
641	F_ID(nd_mid, "method name");
642	LAST_NODE;
643	F_NODE(nd_defn, "method definition");
644	break;
645
646      case NODE_ALIAS:
647	ANN("method alias statement");
648	ANN("format: alias [u1.node] [u2.node]");
649	ANN("example: alias bar foo");
650	F_NODE(u1.node, "new name");
651	LAST_NODE;
652	F_NODE(u2.node, "old name");
653	break;
654
655      case NODE_VALIAS:
656	ANN("global variable alias statement");
657	ANN("format: alias [u1.id](gvar) [u2.id](gvar)");
658	ANN("example: alias $y $x");
659	F_ID(u1.id, "new name");
660	F_ID(u2.id, "old name");
661	break;
662
663      case NODE_UNDEF:
664	ANN("method alias statement");
665	ANN("format: undef [u2.node]");
666	ANN("example: undef foo");
667	LAST_NODE;
668	F_NODE(u2.node, "old name");
669	break;
670
671      case NODE_CLASS:
672	ANN("class definition");
673	ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
674	ANN("example: class C2 < C; ..; end");
675	F_NODE(nd_cpath, "class path");
676	F_NODE(nd_super, "superclass");
677	LAST_NODE;
678	F_NODE(nd_body, "class definition");
679	break;
680
681      case NODE_MODULE:
682	ANN("module definition");
683	ANN("format: module [nd_cpath]; [nd_body]; end");
684	ANN("example: module M; ..; end");
685	F_NODE(nd_cpath, "module path");
686	LAST_NODE;
687	F_NODE(nd_body, "module definition");
688	break;
689
690      case NODE_SCLASS:
691	ANN("singleton class definition");
692	ANN("format: class << [nd_recv]; [nd_body]; end");
693	ANN("example: class << obj; ..; end");
694	F_NODE(nd_recv, "receiver");
695	LAST_NODE;
696	F_NODE(nd_body, "singleton class definition");
697	break;
698
699      case NODE_COLON2:
700	ANN("scoped constant reference");
701	ANN("format: [nd_head]::[nd_mid]");
702	ANN("example: M::C");
703	F_ID(nd_mid, "constant name");
704	LAST_NODE;
705	F_NODE(nd_head, "receiver");
706	break;
707
708      case NODE_COLON3:
709	ANN("top-level constant reference");
710	ANN("format: ::[nd_mid]");
711	ANN("example: ::Object");
712	F_ID(nd_mid, "constant name");
713	break;
714
715      case NODE_DOT2:
716	ANN("range constructor (incl.)");
717	ANN("format: [nd_beg]..[nd_end]");
718	ANN("example: 1..5");
719	goto dot;
720      case NODE_DOT3:
721	ANN("range constructor (excl.)");
722	ANN("format: [nd_beg]...[nd_end]");
723	ANN("example: 1...5");
724	goto dot;
725      case NODE_FLIP2:
726	ANN("flip-flop condition (incl.)");
727	ANN("format: [nd_beg]..[nd_end]");
728	ANN("example: if (x==1)..(x==5); foo; end");
729	goto dot;
730      case NODE_FLIP3:
731	ANN("flip-flop condition (excl.)");
732	ANN("format: [nd_beg]...[nd_end]");
733	ANN("example: if (x==1)...(x==5); foo; end");
734	dot:
735	F_NODE(nd_beg, "begin");
736	LAST_NODE;
737	F_NODE(nd_end, "end");
738	break;
739
740      case NODE_SELF:
741	ANN("self");
742	ANN("format: self");
743	ANN("example: self");
744	break;
745
746      case NODE_NIL:
747	ANN("nil");
748	ANN("format: nil");
749	ANN("example: nil");
750	break;
751
752      case NODE_TRUE:
753	ANN("true");
754	ANN("format: true");
755	ANN("example: true");
756	break;
757
758      case NODE_FALSE:
759	ANN("false");
760	ANN("format: false");
761	ANN("example: false");
762	break;
763
764      case NODE_ERRINFO:
765	ANN("virtual reference to $!");
766	ANN("format: rescue => id");
767	ANN("example: rescue => id");
768	break;
769
770      case NODE_DEFINED:
771	ANN("defined? expression");
772	ANN("format: defined?([nd_head])");
773	ANN("example: defined?(foo)");
774	F_NODE(nd_head, "expr");
775	break;
776
777      case NODE_POSTEXE:
778	ANN("post-execution");
779	ANN("format: END { [nd_body] }");
780	ANN("example: END { foo }");
781	LAST_NODE;
782	F_NODE(nd_body, "END clause");
783	break;
784
785      case NODE_ATTRASGN:
786	ANN("attr assignment");
787	ANN("format: [nd_recv].[nd_mid] = [nd_args]");
788	ANN("example: struct.field = foo");
789	if (node->nd_recv == (NODE *) 1) {
790	    F_MSG(nd_recv, "receiver", "1 (self)");
791	}
792	else {
793	    F_NODE(nd_recv, "receiver");
794	}
795	F_ID(nd_mid, "method name");
796	LAST_NODE;
797	F_NODE(nd_args, "arguments");
798	break;
799
800      case NODE_PRELUDE:
801	ANN("pre-execution");
802	ANN("format: BEGIN { [nd_head] }; [nd_body]");
803	ANN("example: bar; BEGIN { foo }");
804	F_NODE(nd_head, "prelude");
805	LAST_NODE;
806	F_NODE(nd_body, "body");
807	break;
808
809      case NODE_LAMBDA:
810	ANN("lambda expression");
811	ANN("format: -> [nd_body]");
812	ANN("example: -> { foo }");
813	LAST_NODE;
814	F_NODE(nd_body, "lambda clause");
815	break;
816
817      case NODE_OPT_ARG:
818	ANN("optional arguments");
819	ANN("format: def method_name([nd_body=some], [nd_next..])");
820	ANN("example: def foo(a, b=1, c); end");
821	F_NODE(nd_body, "body");
822	LAST_NODE;
823	F_NODE(nd_next, "next");
824	break;
825
826      case NODE_KW_ARG:
827	ANN("keyword arguments");
828	ANN("format: def method_name([nd_body=some], [nd_next..])");
829	ANN("example: def foo(a:1, b:2); end");
830	F_NODE(nd_body, "body");
831	LAST_NODE;
832	F_NODE(nd_next, "next");
833	break;
834
835      case NODE_POSTARG:
836	ANN("post arguments");
837	ANN("format: *[nd_1st], [nd_2nd..] = ..");
838	ANN("example: a, *rest, z = foo");
839	if ((VALUE)node->nd_1st != (VALUE)-1) {
840	    F_NODE(nd_1st, "rest argument");
841	}
842	else {
843	    F_MSG(nd_1st, "rest argument", "-1 (rest argument without name)");
844	}
845	LAST_NODE;
846	F_NODE(nd_2nd, "post arguments");
847	break;
848
849      case NODE_ARGS:
850	ANN("method parameters");
851	ANN("format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
852	ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
853	F_INT(nd_ainfo->pre_args_num, "count of mandatory (pre-)arguments");
854	F_NODE(nd_ainfo->pre_init, "initialization of (pre-)arguments");
855	F_INT(nd_ainfo->post_args_num, "count of mandatory post-arguments");
856	F_NODE(nd_ainfo->post_init, "initialization of post-arguments");
857	F_ID(nd_ainfo->first_post_arg, "first post argument");
858	F_ID(nd_ainfo->rest_arg, "rest argument");
859	F_ID(nd_ainfo->block_arg, "block argument");
860	F_NODE(nd_ainfo->opt_args, "optional arguments");
861	LAST_NODE;
862	F_NODE(nd_ainfo->kw_args, "keyword arguments");
863	F_NODE(nd_ainfo->kw_rest_arg, "keyword rest argument");
864	break;
865
866      case NODE_SCOPE:
867	ANN("new scope");
868	ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
869	F_CUSTOM1(nd_tbl, "local table", {
870	   ID *tbl = node->nd_tbl;
871	   int i;
872	   int size = tbl ? (int)*tbl++ : 0;
873	   if (size == 0) A("(empty)");
874	   for (i = 0; i < size; i++) {
875		A_ID(tbl[i]); if (i < size - 1) A(",");
876	   }
877	});
878	F_NODE(nd_args, "arguments");
879	LAST_NODE;
880	F_NODE(nd_body, "body");
881	break;
882
883      default:
884	rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
885    }
886}
887
888VALUE
889rb_parser_dump_tree(NODE *node, int comment)
890{
891    VALUE buf = rb_str_new_cstr(
892	"###########################################################\n"
893	"## Do NOT use this node dump for any purpose other than  ##\n"
894	"## debug and research.  Compatibility is not guaranteed. ##\n"
895	"###########################################################\n\n"
896    );
897    dump_node(buf, rb_str_new_cstr("# "), comment, node);
898    return buf;
899}
900