node.c

Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   node.c - ruby node tree
00004 
00005   $Author: mame $
00006   created at: 09/12/06 21:23:44 JST
00007 
00008   Copyright (C) 2009 Yusuke Endoh
00009 
00010 **********************************************************************/
00011 
00012 #include "ruby/ruby.h"
00013 #include "vm_core.h"
00014 
00015 #define A(str) rb_str_cat2(buf, (str))
00016 #define AR(str) rb_str_concat(buf, (str))
00017 
00018 #define A_INDENT add_indent(buf, indent)
00019 #define A_ID(id) add_id(buf, id)
00020 #define A_INT(val) rb_str_catf(buf, "%d", (val));
00021 #define A_LONG(val) rb_str_catf(buf, "%ld", (val));
00022 #define A_LIT(lit) AR(rb_inspect(lit))
00023 #define A_NODE_HEADER(node) \
00024     rb_str_catf(buf, "@ %s (line: %d)", ruby_node_name(nd_type(node)), nd_line(node))
00025 #define A_FIELD_HEADER(name) \
00026     rb_str_catf(buf, "+- %s:", name)
00027 
00028 #define D_NULL_NODE A_INDENT; A("(null node)"); A("\n");
00029 #define D_NODE_HEADER(node) A_INDENT; A_NODE_HEADER(node); A("\n");
00030 
00031 #define COMPOUND_FIELD(name, name2, block) \
00032     do { \
00033         A_INDENT; A_FIELD_HEADER(comment ? name2 : name); A("\n"); \
00034         rb_str_cat2(indent, next_indent); \
00035         block; \
00036         rb_str_resize(indent, RSTRING_LEN(indent) - 4); \
00037     } while (0)
00038 
00039 #define SIMPLE_FIELD(name, name2, block) \
00040     do { \
00041         A_INDENT; A_FIELD_HEADER(comment ? name2 : name); A(" "); block; A("\n"); \
00042     } while (0)
00043 
00044 #define F_CUSTOM1(name, ann, block) SIMPLE_FIELD(#name, #name " (" ann ")", block)
00045 #define F_ID(name, ann)             SIMPLE_FIELD(#name, #name " (" ann ")", A_ID(node->name))
00046 #define F_GENTRY(name, ann)         SIMPLE_FIELD(#name, #name " (" ann ")", A_ID((node->name)->id))
00047 #define F_INT(name, ann)            SIMPLE_FIELD(#name, #name " (" ann ")", A_INT(node->name))
00048 #define F_LONG(name, ann)           SIMPLE_FIELD(#name, #name " (" ann ")", A_LONG(node->name))
00049 #define F_LIT(name, ann)            SIMPLE_FIELD(#name, #name " (" ann ")", A_LIT(node->name))
00050 #define F_MSG(name, ann, desc)      SIMPLE_FIELD(#name, #name " (" ann ")", A(desc))
00051 
00052 #define F_CUSTOM2(name, ann, block) \
00053     COMPOUND_FIELD(#name, #name " (" ann ")", block)
00054 
00055 #define F_NODE(name, ann) \
00056     COMPOUND_FIELD(#name, #name " (" ann ")", dump_node(buf, indent, comment, node->name))
00057 
00058 #define ANN(ann) \
00059     if (comment) { \
00060         A_INDENT; A("| # "); A(ann); A("\n"); \
00061     }
00062 
00063 #define LAST_NODE (next_indent = "    ")
00064 
00065 static void
00066 add_indent(VALUE buf, VALUE indent)
00067 {
00068     AR(indent);
00069 }
00070 
00071 static void
00072 add_id(VALUE buf, ID id)
00073 {
00074     if (id == 0) {
00075         A("(null)");
00076     }
00077     else {
00078         VALUE str = rb_id2str(id);
00079         if (str) {
00080             A(":"); AR(rb_id2str(id));
00081         }
00082         else {
00083             A("(internal variable)");
00084         }
00085     }
00086 }
00087 
00088 static void
00089 dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
00090 {
00091     const char *next_indent = "|   ";
00092 
00093     if (!node) {
00094         D_NULL_NODE;
00095         return;
00096     }
00097 
00098     D_NODE_HEADER(node);
00099 
00100     switch (nd_type(node)) {
00101       case NODE_BLOCK:
00102         ANN("statement sequence");
00103         ANN("format: [nd_head]; [nd_next]");
00104         ANN("example: foo; bar");
00105         F_NODE(nd_head, "current statement");
00106         LAST_NODE;
00107         F_NODE(nd_next, "next block");
00108         break;
00109 
00110       case NODE_IF:
00111         ANN("if statement");
00112         ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
00113         ANN("example: if x == 1 then foo else bar end");
00114         F_NODE(nd_cond, "condition expr");
00115         F_NODE(nd_body, "then clause");
00116         LAST_NODE;
00117         F_NODE(nd_else, "else clause");
00118         break;
00119 
00120       case NODE_CASE:
00121         ANN("case statement");
00122         ANN("format: case [nd_head]; [nd_body]; end");
00123         ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
00124         F_NODE(nd_head, "case expr");
00125         LAST_NODE;
00126         F_NODE(nd_body, "when clauses");
00127         break;
00128 
00129       case NODE_WHEN:
00130         ANN("if statement");
00131         ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
00132         ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
00133         F_NODE(nd_head, "when value");
00134         F_NODE(nd_body, "when clause");
00135         LAST_NODE;
00136         F_NODE(nd_next, "next when clause");
00137         break;
00138 
00139       case NODE_OPT_N:
00140         ANN("wrapper for -n option");
00141         ANN("format: ruby -ne '[nd_body]' (nd_cond is `gets')");
00142         ANN("example: ruby -ne 'p $_'");
00143         goto loop;
00144       case NODE_WHILE:
00145         ANN("while statement");
00146         ANN("format: while [nd_cond]; [nd_body]; end");
00147         ANN("example: while x == 1; foo; end");
00148         goto loop;
00149       case NODE_UNTIL:
00150         ANN("until statement");
00151         ANN("format: until [nd_cond]; [nd_body]; end");
00152         ANN("example: until x == 1; foo; end");
00153       loop:
00154         F_CUSTOM1(nd_state, "begin-end-while?", {
00155            A_INT((int)node->nd_state);
00156            A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
00157         });
00158         F_NODE(nd_cond, "condition");
00159         LAST_NODE;
00160         F_NODE(nd_body, "body");
00161         break;
00162 
00163       case NODE_ITER:
00164         ANN("method call with block");
00165         ANN("format: [nd_iter] { [nd_body] }");
00166         ANN("example: 3.times { foo }");
00167         goto iter;
00168       case NODE_FOR:
00169         ANN("for statement");
00170         ANN("format: for * in [nd_iter] do [nd_body] end");
00171         ANN("example: for i in 1..3 do foo end");
00172         iter:
00173         F_NODE(nd_iter, "iteration receiver");
00174         LAST_NODE;
00175         F_NODE(nd_body, "body");
00176         break;
00177 
00178       case NODE_BREAK:
00179         ANN("for statement");
00180         ANN("format: break [nd_stts]");
00181         ANN("example: break 1");
00182         goto jump;
00183       case NODE_NEXT:
00184         ANN("next statement");
00185         ANN("format: next [nd_stts]");
00186         ANN("example: next 1");
00187         goto jump;
00188       case NODE_RETURN:
00189         ANN("return statement");
00190         ANN("format: return [nd_stts]");
00191         ANN("example: return 1");
00192         jump:
00193         LAST_NODE;
00194         F_NODE(nd_stts, "value");
00195         break;
00196 
00197       case NODE_REDO:
00198         ANN("redo statement");
00199         ANN("format: redo");
00200         ANN("example: redo");
00201         break;
00202 
00203       case NODE_RETRY:
00204         ANN("retry statement");
00205         ANN("format: retry");
00206         ANN("example: retry");
00207         break;
00208 
00209       case NODE_BEGIN:
00210         ANN("begin statement");
00211         ANN("format: begin; [nd_body]; end");
00212         ANN("example: begin; 1; end");
00213         LAST_NODE;
00214         F_NODE(nd_body, "body");
00215         break;
00216 
00217       case NODE_RESCUE:
00218         ANN("rescue clause");
00219         ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
00220         ANN("example: begin; foo; rescue; bar; else; baz; end");
00221         F_NODE(nd_head, "body");
00222         F_NODE(nd_resq, "rescue clause list");
00223         LAST_NODE;
00224         F_NODE(nd_else, "rescue else clause");
00225         break;
00226 
00227       case NODE_RESBODY:
00228         ANN("rescue clause (cont'd)");
00229         ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
00230         ANN("example: begin; foo; rescue; bar; else; baz; end");
00231         F_NODE(nd_args, "rescue exceptions");
00232         F_NODE(nd_body, "rescue clause");
00233         LAST_NODE;
00234         F_NODE(nd_head, "next rescue clause");
00235         break;
00236 
00237       case NODE_ENSURE:
00238         ANN("ensure clause");
00239         ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
00240         ANN("example: begin; foo; ensure; bar; end");
00241         F_NODE(nd_head, "body");
00242         LAST_NODE;
00243         F_NODE(nd_ensr, "ensure clause");
00244         break;
00245 
00246       case NODE_AND:
00247         ANN("&& operator");
00248         ANN("format: [nd_1st] && [nd_2nd]");
00249         ANN("example: foo && bar");
00250         goto andor;
00251       case NODE_OR:
00252         ANN("|| operator");
00253         ANN("format: [nd_1st] || [nd_2nd]");
00254         ANN("example: foo && bar");
00255         andor:
00256         F_NODE(nd_1st, "left expr");
00257         LAST_NODE;
00258         F_NODE(nd_2nd, "right expr");
00259         break;
00260 
00261       case NODE_MASGN:
00262         ANN("multiple assignment");
00263         ANN("format: [nd_head], [nd_args] = [nd_value]");
00264         ANN("example: a, b = foo");
00265         F_NODE(nd_value, "rhsn");
00266         F_NODE(nd_head, "lhsn");
00267         if ((VALUE)node->nd_args != (VALUE)-1) {
00268             LAST_NODE;
00269             F_NODE(nd_args, "splatn");
00270         }
00271         else {
00272             F_MSG(nd_args, "splatn", "-1 (rest argument without name)");
00273         }
00274         break;
00275 
00276       case NODE_LASGN:
00277         ANN("local variable assignment");
00278         ANN("format: [nd_vid](lvar) = [nd_value]");
00279         ANN("example: x = foo");
00280         goto asgn;
00281       case NODE_DASGN:
00282         ANN("dynamic variable assignment (out of current scope)");
00283         ANN("format: [nd_vid](dvar) = [nd_value]");
00284         ANN("example: x = nil; 1.times { x = foo }");
00285         goto asgn;
00286       case NODE_DASGN_CURR:
00287         ANN("dynamic variable assignment (in current scope)");
00288         ANN("format: [nd_vid](current dvar) = [nd_value]");
00289         ANN("example: 1.times { x = foo }");
00290         goto asgn;
00291       case NODE_IASGN:
00292         ANN("instance variable assignment");
00293         ANN("format: [nd_vid](ivar) = [nd_value]");
00294         ANN("example: @x = foo");
00295         goto asgn;
00296       case NODE_CVASGN:
00297         ANN("class variable assignment");
00298         ANN("format: [nd_vid](cvar) = [nd_value]");
00299         ANN("example: @@x = foo");
00300         asgn:
00301         F_ID(nd_vid, "variable");
00302         LAST_NODE;
00303         F_NODE(nd_value, "rvalue");
00304         break;
00305 
00306       case NODE_GASGN:
00307         ANN("global variable assignment");
00308         ANN("format: [nd_entry](gvar) = [nd_value]");
00309         ANN("example: $x = foo");
00310         F_GENTRY(nd_entry, "global variable");
00311         LAST_NODE;
00312         F_NODE(nd_value, "rvalue");
00313         break;
00314 
00315       case NODE_CDECL:
00316         ANN("constant declaration");
00317         ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
00318         ANN("example: X = foo");
00319         if (node->nd_vid) {
00320            F_ID(nd_vid, "variable");
00321            F_MSG(nd_else, "extension", "not used");
00322         }
00323         else {
00324            F_MSG(nd_vid, "variable", "0 (see extension field)");
00325            F_NODE(nd_else, "extension");
00326         }
00327         LAST_NODE;
00328         F_NODE(nd_value, "rvalue");
00329         break;
00330 
00331       case NODE_OP_ASGN1:
00332         ANN("array assignment with operator");
00333         ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
00334         ANN("example: ary[1] += foo");
00335         F_NODE(nd_recv, "receiver");
00336         F_ID(nd_vid, "operator");
00337         F_NODE(nd_args->nd_body, "index");
00338         LAST_NODE;
00339         F_NODE(nd_args->nd_head, "rvalue");
00340         break;
00341 
00342       case NODE_OP_ASGN2:
00343         ANN("attr assignment with operator");
00344         ANN("format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
00345         ANN("          where [attr] reader: [nd_next->nd_vid]");
00346         ANN("                [attr] writer: [nd_next->nd_aid]");
00347         ANN("example: struct.field += foo");
00348         F_NODE(nd_recv, "receiver");
00349         F_ID(nd_next->nd_vid, "reader");
00350         F_ID(nd_next->nd_aid, "writer");
00351         F_CUSTOM1(nd_next->nd_mid, "operator", {
00352            switch (node->nd_next->nd_mid) {
00353            case 0: A("0 (||)"); break;
00354            case 1: A("1 (&&)"); break;
00355            default: A_ID(node->nd_next->nd_mid);
00356            }
00357         });
00358         LAST_NODE;
00359         F_NODE(nd_value, "rvalue");
00360         break;
00361 
00362       case NODE_OP_ASGN_AND:
00363         ANN("assignment with && operator");
00364         ANN("format: [nd_head] &&= [nd_value]");
00365         ANN("example: foo &&= bar");
00366         goto asgn_andor;
00367       case NODE_OP_ASGN_OR:
00368         ANN("assignment with || operator");
00369         ANN("format: [nd_head] ||= [nd_value]");
00370         ANN("example: foo ||= bar");
00371         asgn_andor:
00372         F_NODE(nd_head, "variable");
00373         LAST_NODE;
00374         F_NODE(nd_value, "rvalue");
00375         break;
00376 
00377       case NODE_CALL:
00378         ANN("method invocation");
00379         ANN("format: [nd_recv].[nd_mid]([nd_args])");
00380         ANN("example: obj.foo(1)");
00381         F_ID(nd_mid, "method id");
00382         F_NODE(nd_recv, "receiver");
00383         LAST_NODE;
00384         F_NODE(nd_args, "arguments");
00385         break;
00386 
00387       case NODE_FCALL:
00388         ANN("function call");
00389         ANN("format: [nd_mid]([nd_args])");
00390         ANN("example: foo(1)");
00391         F_ID(nd_mid, "method id");
00392         LAST_NODE;
00393         F_NODE(nd_args, "arguments");
00394         break;
00395 
00396       case NODE_VCALL:
00397         ANN("function call with no argument");
00398         ANN("format: [nd_mid]");
00399         ANN("example: foo");
00400         F_ID(nd_mid, "method id");
00401         break;
00402 
00403       case NODE_SUPER:
00404         ANN("super invocation");
00405         ANN("format: super [nd_args]");
00406         ANN("example: super 1");
00407         LAST_NODE;
00408         F_NODE(nd_args, "arguments");
00409         break;
00410 
00411       case NODE_ZSUPER:
00412         ANN("super invocation with no argument");
00413         ANN("format: super");
00414         ANN("example: super");
00415         break;
00416 
00417       case NODE_ARRAY:
00418         ANN("array constructor");
00419         ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
00420         ANN("example: [1, 2, 3]");
00421         goto ary;
00422       case NODE_VALUES:
00423         ANN("return arguments");
00424         ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
00425         ANN("example: return 1, 2, 3");
00426         ary:
00427         F_LONG(nd_alen, "length");
00428         F_NODE(nd_head, "element");
00429         LAST_NODE;
00430         F_NODE(nd_next, "next element");
00431         break;
00432 
00433       case NODE_ZARRAY:
00434         ANN("empty array constructor");
00435         ANN("format: []");
00436         ANN("example: []");
00437         break;
00438 
00439       case NODE_HASH:
00440         ANN("hash constructor");
00441         ANN("format: { [nd_head] }");
00442         ANN("example: { 1 => 2, 3 => 4 }");
00443         LAST_NODE;
00444         F_NODE(nd_head, "contents");
00445         break;
00446 
00447       case NODE_YIELD:
00448         ANN("yield invocation");
00449         ANN("format: yield [nd_head]");
00450         ANN("example: yield 1");
00451         LAST_NODE;
00452         F_NODE(nd_head, "arguments");
00453         break;
00454 
00455       case NODE_LVAR:
00456         ANN("local variable reference");
00457         ANN("format: [nd_vid](lvar)");
00458         ANN("example: x");
00459         goto var;
00460       case NODE_DVAR:
00461         ANN("dynamic variable reference");
00462         ANN("format: [nd_vid](dvar)");
00463         ANN("example: 1.times { x = 1; x }");
00464         goto var;
00465       case NODE_IVAR:
00466         ANN("instance variable reference");
00467         ANN("format: [nd_vid](ivar)");
00468         ANN("example: @x");
00469         goto var;
00470       case NODE_CONST:
00471         ANN("constant reference");
00472         ANN("format: [nd_vid](constant)");
00473         ANN("example: X");
00474         goto var;
00475       case NODE_CVAR:
00476         ANN("class variable reference");
00477         ANN("format: [nd_vid](cvar)");
00478         ANN("example: @@x");
00479         var:
00480         F_ID(nd_vid, "local variable");
00481         break;
00482 
00483       case NODE_GVAR:
00484         ANN("global variable reference");
00485         ANN("format: [nd_entry](gvar)");
00486         ANN("example: $x");
00487         F_GENTRY(nd_entry, "global variable");
00488         break;
00489 
00490       case NODE_NTH_REF:
00491         ANN("nth special variable reference");
00492         ANN("format: $[nd_nth]");
00493         ANN("example: $1, $2, ..");
00494         F_CUSTOM1(nd_nth, "variable", { A("$"); A_LONG(node->nd_nth); });
00495         break;
00496 
00497       case NODE_BACK_REF:
00498         ANN("back special variable reference");
00499         ANN("format: $[nd_nth]");
00500         ANN("example: $&, $`, $', $+");
00501         F_CUSTOM1(nd_nth, "variable", {
00502            char name[3];
00503            name[0] = '$';
00504            name[1] = node->nd_nth;
00505            name[2] = '\0';
00506            A(name);
00507         });
00508         break;
00509 
00510       case NODE_MATCH:
00511         ANN("match expression (against $_ implicitly)");
00512         ANN("format: [nd_lit] (in condition)");
00513         ANN("example: if /foo/; foo; end");
00514         F_LIT(nd_lit, "regexp");
00515         break;
00516 
00517       case NODE_MATCH2:
00518         ANN("match expression (regexp first)");
00519         ANN("format: [nd_recv] =~ [nd_value]");
00520         ANN("example: /foo/ =~ 'foo'");
00521         F_NODE(nd_recv, "regexp (receiver)");
00522         LAST_NODE;
00523         F_NODE(nd_value, "string (argument)");
00524         break;
00525 
00526       case NODE_MATCH3:
00527         ANN("match expression (regexp second)");
00528         ANN("format: [nd_recv] =~ [nd_value]");
00529         ANN("example: 'foo' =~ /foo/");
00530         F_NODE(nd_recv, "string (receiver)");
00531         LAST_NODE;
00532         F_NODE(nd_value, "regexp (argument)");
00533         break;
00534 
00535       case NODE_LIT:
00536         ANN("literal");
00537         ANN("format: [nd_lit]");
00538         ANN("example: 1, /foo/");
00539         goto lit;
00540       case NODE_STR:
00541         ANN("string literal");
00542         ANN("format: [nd_lit]");
00543         ANN("example: 'foo'");
00544         goto lit;
00545       case NODE_XSTR:
00546         ANN("xstring literal");
00547         ANN("format: [nd_lit]");
00548         ANN("example: `foo`");
00549         lit:
00550         F_LIT(nd_lit, "literal");
00551         break;
00552 
00553       case NODE_DSTR:
00554         ANN("string literal with interpolation");
00555         ANN("format: [nd_lit]");
00556         ANN("example: \"foo#{ bar }baz\"");
00557         goto dlit;
00558       case NODE_DXSTR:
00559         ANN("xstring literal with interpolation");
00560         ANN("format: [nd_lit]");
00561         ANN("example: `foo#{ bar }baz`");
00562         goto dlit;
00563       case NODE_DREGX:
00564         ANN("regexp literal with interpolation");
00565         ANN("format: [nd_lit]");
00566         ANN("example: /foo#{ bar }baz/");
00567         goto dlit;
00568       case NODE_DREGX_ONCE:
00569         ANN("regexp literal with interpolation and once flag");
00570         ANN("format: [nd_lit]");
00571         ANN("example: /foo#{ bar }baz/o");
00572         goto dlit;
00573       case NODE_DSYM:
00574         ANN("symbol literal with interpolation");
00575         ANN("format: [nd_lit]");
00576         ANN("example: :\"foo#{ bar }baz\"");
00577         dlit:
00578         F_LIT(nd_lit, "literal");
00579         F_NODE(nd_next->nd_head, "preceding string");
00580         LAST_NODE;
00581         F_NODE(nd_next->nd_next, "interpolation");
00582         break;
00583 
00584       case NODE_EVSTR:
00585         ANN("interpolation expression");
00586         ANN("format: \"..#{ [nd_lit] }..\"");
00587         ANN("example: \"foo#{ bar }baz\"");
00588         LAST_NODE;
00589         F_NODE(nd_body, "body");
00590         break;
00591 
00592       case NODE_ARGSCAT:
00593         ANN("splat argument following arguments");
00594         ANN("format: ..(*[nd_head], [nd_body..])");
00595         ANN("example: foo(*ary, post_arg1, post_arg2)");
00596         F_NODE(nd_head, "preceding array");
00597         LAST_NODE;
00598         F_NODE(nd_body, "following array");
00599         break;
00600 
00601       case NODE_ARGSPUSH:
00602         ANN("splat argument following one argument");
00603         ANN("format: ..(*[nd_head], [nd_body])");
00604         ANN("example: foo(*ary, post_arg)");
00605         F_NODE(nd_head, "preceding array");
00606         LAST_NODE;
00607         F_NODE(nd_body, "following element");
00608         break;
00609 
00610       case NODE_SPLAT:
00611         ANN("splat argument");
00612         ANN("format: *[nd_head]");
00613         ANN("example: foo(*ary)");
00614         LAST_NODE;
00615         F_NODE(nd_head, "splat'ed array");
00616         break;
00617 
00618       case NODE_BLOCK_PASS:
00619         ANN("arguments with block argument");
00620         ANN("format: ..([nd_head], &[nd_body])");
00621         ANN("example: foo(x, &blk)");
00622         F_NODE(nd_head, "other arguments");
00623         LAST_NODE;
00624         F_NODE(nd_body, "block argument");
00625         break;
00626 
00627       case NODE_DEFN:
00628         ANN("method definition");
00629         ANN("format: def [nd_mid] [nd_defn]; end");
00630         ANN("example; def foo; bar; end");
00631         F_ID(nd_mid, "method name");
00632         LAST_NODE;
00633         F_NODE(nd_defn, "method definition");
00634         break;
00635 
00636       case NODE_DEFS:
00637         ANN("singleton method definition");
00638         ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
00639         ANN("example; def obj.foo; bar; end");
00640         F_NODE(nd_recv, "receiver");
00641         F_ID(nd_mid, "method name");
00642         LAST_NODE;
00643         F_NODE(nd_defn, "method definition");
00644         break;
00645 
00646       case NODE_ALIAS:
00647         ANN("method alias statement");
00648         ANN("format: alias [u1.node] [u2.node]");
00649         ANN("example: alias bar foo");
00650         F_NODE(u1.node, "new name");
00651         LAST_NODE;
00652         F_NODE(u2.node, "old name");
00653         break;
00654 
00655       case NODE_VALIAS:
00656         ANN("global variable alias statement");
00657         ANN("format: alias [u1.id](gvar) [u2.id](gvar)");
00658         ANN("example: alias $y $x");
00659         F_ID(u1.id, "new name");
00660         F_ID(u2.id, "old name");
00661         break;
00662 
00663       case NODE_UNDEF:
00664         ANN("method alias statement");
00665         ANN("format: undef [u2.node]");
00666         ANN("example: undef foo");
00667         LAST_NODE;
00668         F_NODE(u2.node, "old name");
00669         break;
00670 
00671       case NODE_CLASS:
00672         ANN("class definition");
00673         ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
00674         ANN("example: class C2 < C; ..; end");
00675         F_NODE(nd_cpath, "class path");
00676         F_NODE(nd_super, "superclass");
00677         LAST_NODE;
00678         F_NODE(nd_body, "class definition");
00679         break;
00680 
00681       case NODE_MODULE:
00682         ANN("module definition");
00683         ANN("format: module [nd_cpath]; [nd_body]; end");
00684         ANN("example: module M; ..; end");
00685         F_NODE(nd_cpath, "module path");
00686         LAST_NODE;
00687         F_NODE(nd_body, "module definition");
00688         break;
00689 
00690       case NODE_SCLASS:
00691         ANN("singleton class definition");
00692         ANN("format: class << [nd_recv]; [nd_body]; end");
00693         ANN("example: class << obj; ..; end");
00694         F_NODE(nd_recv, "receiver");
00695         LAST_NODE;
00696         F_NODE(nd_body, "singleton class definition");
00697         break;
00698 
00699       case NODE_COLON2:
00700         ANN("scoped constant reference");
00701         ANN("format: [nd_head]::[nd_mid]");
00702         ANN("example: M::C");
00703         F_ID(nd_mid, "constant name");
00704         LAST_NODE;
00705         F_NODE(nd_head, "receiver");
00706         break;
00707 
00708       case NODE_COLON3:
00709         ANN("top-level constant reference");
00710         ANN("format: ::[nd_mid]");
00711         ANN("example: ::Object");
00712         F_ID(nd_mid, "constant name");
00713         break;
00714 
00715       case NODE_DOT2:
00716         ANN("range constructor (incl.)");
00717         ANN("format: [nd_beg]..[nd_end]");
00718         ANN("example: 1..5");
00719         goto dot;
00720       case NODE_DOT3:
00721         ANN("range constructor (excl.)");
00722         ANN("format: [nd_beg]...[nd_end]");
00723         ANN("example: 1...5");
00724         goto dot;
00725       case NODE_FLIP2:
00726         ANN("flip-flop condition (incl.)");
00727         ANN("format: [nd_beg]..[nd_end]");
00728         ANN("example: if (x==1)..(x==5); foo; end");
00729         goto dot;
00730       case NODE_FLIP3:
00731         ANN("flip-flop condition (excl.)");
00732         ANN("format: [nd_beg]...[nd_end]");
00733         ANN("example: if (x==1)...(x==5); foo; end");
00734         dot:
00735         F_NODE(nd_beg, "begin");
00736         LAST_NODE;
00737         F_NODE(nd_end, "end");
00738         break;
00739 
00740       case NODE_SELF:
00741         ANN("self");
00742         ANN("format: self");
00743         ANN("example: self");
00744         break;
00745 
00746       case NODE_NIL:
00747         ANN("nil");
00748         ANN("format: nil");
00749         ANN("example: nil");
00750         break;
00751 
00752       case NODE_TRUE:
00753         ANN("true");
00754         ANN("format: true");
00755         ANN("example: true");
00756         break;
00757 
00758       case NODE_FALSE:
00759         ANN("false");
00760         ANN("format: false");
00761         ANN("example: false");
00762         break;
00763 
00764       case NODE_ERRINFO:
00765         ANN("virtual reference to $!");
00766         ANN("format: rescue => id");
00767         ANN("example: rescue => id");
00768         break;
00769 
00770       case NODE_DEFINED:
00771         ANN("defined? expression");
00772         ANN("format: defined?([nd_head])");
00773         ANN("example: defined?(foo)");
00774         F_NODE(nd_head, "expr");
00775         break;
00776 
00777       case NODE_POSTEXE:
00778         ANN("post-execution");
00779         ANN("format: END { [nd_body] }");
00780         ANN("example: END { foo }");
00781         LAST_NODE;
00782         F_NODE(nd_body, "END clause");
00783         break;
00784 
00785       case NODE_ATTRASGN:
00786         ANN("attr assignment");
00787         ANN("format: [nd_recv].[nd_mid] = [nd_args]");
00788         ANN("example: struct.field = foo");
00789         if (node->nd_recv == (NODE *) 1) {
00790             F_MSG(nd_recv, "receiver", "1 (self)");
00791         }
00792         else {
00793             F_NODE(nd_recv, "receiver");
00794         }
00795         F_ID(nd_mid, "method name");
00796         LAST_NODE;
00797         F_NODE(nd_args, "arguments");
00798         break;
00799 
00800       case NODE_PRELUDE:
00801         ANN("pre-execution");
00802         ANN("format: BEGIN { [nd_head] }; [nd_body]");
00803         ANN("example: bar; BEGIN { foo }");
00804         F_NODE(nd_head, "prelude");
00805         LAST_NODE;
00806         F_NODE(nd_body, "body");
00807         break;
00808 
00809       case NODE_LAMBDA:
00810         ANN("lambda expression");
00811         ANN("format: -> [nd_body]");
00812         ANN("example: -> { foo }");
00813         LAST_NODE;
00814         F_NODE(nd_body, "lambda clause");
00815         break;
00816 
00817       case NODE_OPT_ARG:
00818         ANN("optional arguments");
00819         ANN("format: def method_name([nd_body=some], [nd_next..])");
00820         ANN("example: def foo(a, b=1, c); end");
00821         F_NODE(nd_body, "body");
00822         LAST_NODE;
00823         F_NODE(nd_next, "next");
00824         break;
00825 
00826       case NODE_POSTARG:
00827         ANN("post arguments");
00828         ANN("format: *[nd_1st], [nd_2nd..] = ..");
00829         ANN("example: a, *rest, z = foo");
00830         if ((VALUE)node->nd_1st != (VALUE)-1) {
00831             F_NODE(nd_1st, "rest argument");
00832         }
00833         else {
00834             F_MSG(nd_1st, "rest argument", "-1 (rest argument without name)");
00835         }
00836         LAST_NODE;
00837         F_NODE(nd_2nd, "post arguments");
00838         break;
00839 
00840       case NODE_ARGS_AUX:
00841         ANN("method parameters (cont'd)");
00842         F_CUSTOM1(nd_rest, "rest argument", {
00843             if (node->nd_rest == 1) A("nil (with last comma)");
00844             else A_ID(node->nd_rest);
00845         });
00846         F_CUSTOM1(nd_body, "block argument", { A_ID((ID)node->nd_body); });
00847         LAST_NODE;
00848         F_CUSTOM2(nd_next, "aux info 2", {
00849             node = node->nd_next;
00850             next_indent = "|    ";
00851             if (!node) {
00852                 D_NULL_NODE;
00853             }
00854             else {
00855                 D_NODE_HEADER(node);
00856                 ANN("method parameters (cont'd)");
00857                 F_ID(nd_pid, "first post argument");
00858                 F_LONG(nd_plen, "post argument length");
00859                 LAST_NODE;
00860                 F_CUSTOM2(nd_next, "aux info 3", {
00861                     node = node->nd_next;
00862                     next_indent = "|   ";
00863                     if (!node) {
00864                         D_NULL_NODE;
00865                     }
00866                     else {
00867                         D_NODE_HEADER(node);
00868                         ANN("method parameters (cont'd)");
00869                         F_NODE(nd_1st, "init arguments (m)");
00870                         LAST_NODE;
00871                         F_NODE(nd_2nd, "init arguments (p)");
00872                     }
00873                 });
00874             }
00875         });
00876         break;
00877 
00878       case NODE_ARGS:
00879         ANN("method parameters");
00880         ANN("format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
00881         ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
00882         F_LONG(nd_frml, "argc");
00883         F_NODE(nd_next, "aux info 1");
00884         LAST_NODE;
00885         F_NODE(nd_opt, "optional arguments");
00886         break;
00887 
00888       case NODE_SCOPE:
00889         ANN("new scope");
00890         ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
00891         F_CUSTOM1(nd_tbl, "local table", {
00892            ID *tbl = node->nd_tbl;
00893            int i;
00894            int size = tbl ? (int)*tbl++ : 0;
00895            if (size == 0) A("(empty)");
00896            for (i = 0; i < size; i++) {
00897                 A_ID(tbl[i]); if (i < size - 1) A(",");
00898            }
00899         });
00900         F_NODE(nd_args, "arguments");
00901         LAST_NODE;
00902         F_NODE(nd_body, "body");
00903         break;
00904 
00905       default:
00906         rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
00907     }
00908 }
00909 
00910 VALUE
00911 rb_parser_dump_tree(NODE *node, int comment)
00912 {
00913     VALUE buf = rb_str_new_cstr(
00914         "###########################################################\n"
00915         "## Do NOT use this node dump for any purpose other than  ##\n"
00916         "## debug and research.  Compatibility is not guaranteed. ##\n"
00917         "###########################################################\n\n"
00918     );
00919     dump_node(buf, rb_str_new_cstr("# "), comment, node);
00920     return buf;
00921 }
00922 

Generated on Wed Aug 10 09:17:05 2011 for Ruby by  doxygen 1.4.7