Ruby
2.1.3p242(2014-09-19revision47630)
Main Page
Modules
Data Structures
Files
File List
Globals
node.c
Go to the documentation of this file.
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
65
static
void
66
add_indent
(
VALUE
buf
,
VALUE
indent)
67
{
68
AR
(indent);
69
}
70
71
static
void
72
add_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
88
static
void
89
dump_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
if
(node->nd_value == (
NODE
*)-1) {
304
F_MSG
(
nd_value
,
"rvalue"
,
"(required keyword argument)"
);
305
}
306
else
{
307
F_NODE
(
nd_value
,
"rvalue"
);
308
}
309
break
;
310
311
case
NODE_GASGN
:
312
ANN
(
"global variable assignment"
);
313
ANN
(
"format: [nd_entry](gvar) = [nd_value]"
);
314
ANN
(
"example: $x = foo"
);
315
F_GENTRY
(
nd_entry
,
"global variable"
);
316
LAST_NODE
;
317
F_NODE
(
nd_value
,
"rvalue"
);
318
break
;
319
320
case
NODE_CDECL
:
321
ANN
(
"constant declaration"
);
322
ANN
(
"format: [nd_else]::[nd_vid](constant) = [nd_value]"
);
323
ANN
(
"example: X = foo"
);
324
if
(node->nd_vid) {
325
F_ID
(
nd_vid
,
"variable"
);
326
F_MSG
(
nd_else
,
"extension"
,
"not used"
);
327
}
328
else
{
329
F_MSG
(
nd_vid
,
"variable"
,
"0 (see extension field)"
);
330
F_NODE
(
nd_else
,
"extension"
);
331
}
332
LAST_NODE
;
333
F_NODE
(
nd_value
,
"rvalue"
);
334
break
;
335
336
case
NODE_OP_ASGN1
:
337
ANN
(
"array assignment with operator"
);
338
ANN
(
"format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]"
);
339
ANN
(
"example: ary[1] += foo"
);
340
F_NODE
(
nd_recv
,
"receiver"
);
341
F_ID
(
nd_vid
,
"operator"
);
342
F_NODE
(
nd_args
->nd_body,
"index"
);
343
LAST_NODE
;
344
F_NODE
(
nd_args
->nd_head,
"rvalue"
);
345
break
;
346
347
case
NODE_OP_ASGN2
:
348
ANN
(
"attr assignment with operator"
);
349
ANN
(
"format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]"
);
350
ANN
(
" where [attr] reader: [nd_next->nd_vid]"
);
351
ANN
(
" [attr] writer: [nd_next->nd_aid]"
);
352
ANN
(
"example: struct.field += foo"
);
353
F_NODE
(
nd_recv
,
"receiver"
);
354
F_ID
(
nd_next
->nd_vid,
"reader"
);
355
F_ID
(
nd_next
->nd_aid,
"writer"
);
356
F_CUSTOM1
(
nd_next
->nd_mid,
"operator"
, {
357
switch
(node->nd_next->nd_mid) {
358
case
0:
A
(
"0 (||)"
);
break
;
359
case
1:
A
(
"1 (&&)"
);
break
;
360
default
:
A_ID
(node->nd_next->nd_mid);
361
}
362
});
363
LAST_NODE
;
364
F_NODE
(nd_value,
"rvalue"
);
365
break
;
366
367
case
NODE_OP_ASGN_AND
:
368
ANN
(
"assignment with && operator"
);
369
ANN
(
"format: [nd_head] &&= [nd_value]"
);
370
ANN
(
"example: foo &&= bar"
);
371
goto
asgn_andor;
372
case
NODE_OP_ASGN_OR
:
373
ANN
(
"assignment with || operator"
);
374
ANN
(
"format: [nd_head] ||= [nd_value]"
);
375
ANN
(
"example: foo ||= bar"
);
376
asgn_andor:
377
F_NODE
(nd_head,
"variable"
);
378
LAST_NODE
;
379
F_NODE
(nd_value,
"rvalue"
);
380
break
;
381
382
case
NODE_CALL
:
383
ANN
(
"method invocation"
);
384
ANN
(
"format: [nd_recv].[nd_mid]([nd_args])"
);
385
ANN
(
"example: obj.foo(1)"
);
386
F_ID
(nd_mid,
"method id"
);
387
F_NODE
(nd_recv,
"receiver"
);
388
LAST_NODE
;
389
F_NODE
(
nd_args
,
"arguments"
);
390
break
;
391
392
case
NODE_FCALL
:
393
ANN
(
"function call"
);
394
ANN
(
"format: [nd_mid]([nd_args])"
);
395
ANN
(
"example: foo(1)"
);
396
F_ID
(nd_mid,
"method id"
);
397
LAST_NODE
;
398
F_NODE
(
nd_args
,
"arguments"
);
399
break
;
400
401
case
NODE_VCALL
:
402
ANN
(
"function call with no argument"
);
403
ANN
(
"format: [nd_mid]"
);
404
ANN
(
"example: foo"
);
405
F_ID
(nd_mid,
"method id"
);
406
break
;
407
408
case
NODE_SUPER
:
409
ANN
(
"super invocation"
);
410
ANN
(
"format: super [nd_args]"
);
411
ANN
(
"example: super 1"
);
412
LAST_NODE
;
413
F_NODE
(
nd_args
,
"arguments"
);
414
break
;
415
416
case
NODE_ZSUPER
:
417
ANN
(
"super invocation with no argument"
);
418
ANN
(
"format: super"
);
419
ANN
(
"example: super"
);
420
break
;
421
422
case
NODE_ARRAY
:
423
ANN
(
"array constructor"
);
424
ANN
(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])"
);
425
ANN
(
"example: [1, 2, 3]"
);
426
goto
ary;
427
case
NODE_VALUES
:
428
ANN
(
"return arguments"
);
429
ANN
(
"format: [ [nd_head], [nd_next].. ] (length: [nd_alen])"
);
430
ANN
(
"example: return 1, 2, 3"
);
431
ary:
432
F_LONG
(nd_alen,
"length"
);
433
F_NODE
(nd_head,
"element"
);
434
LAST_NODE
;
435
F_NODE
(nd_next,
"next element"
);
436
break
;
437
438
case
NODE_ZARRAY
:
439
ANN
(
"empty array constructor"
);
440
ANN
(
"format: []"
);
441
ANN
(
"example: []"
);
442
break
;
443
444
case
NODE_HASH
:
445
ANN
(
"hash constructor"
);
446
ANN
(
"format: { [nd_head] }"
);
447
ANN
(
"example: { 1 => 2, 3 => 4 }"
);
448
LAST_NODE
;
449
F_NODE
(nd_head,
"contents"
);
450
break
;
451
452
case
NODE_YIELD
:
453
ANN
(
"yield invocation"
);
454
ANN
(
"format: yield [nd_head]"
);
455
ANN
(
"example: yield 1"
);
456
LAST_NODE
;
457
F_NODE
(nd_head,
"arguments"
);
458
break
;
459
460
case
NODE_LVAR
:
461
ANN
(
"local variable reference"
);
462
ANN
(
"format: [nd_vid](lvar)"
);
463
ANN
(
"example: x"
);
464
goto
var;
465
case
NODE_DVAR
:
466
ANN
(
"dynamic variable reference"
);
467
ANN
(
"format: [nd_vid](dvar)"
);
468
ANN
(
"example: 1.times { x = 1; x }"
);
469
goto
var;
470
case
NODE_IVAR
:
471
ANN
(
"instance variable reference"
);
472
ANN
(
"format: [nd_vid](ivar)"
);
473
ANN
(
"example: @x"
);
474
goto
var;
475
case
NODE_CONST
:
476
ANN
(
"constant reference"
);
477
ANN
(
"format: [nd_vid](constant)"
);
478
ANN
(
"example: X"
);
479
goto
var;
480
case
NODE_CVAR
:
481
ANN
(
"class variable reference"
);
482
ANN
(
"format: [nd_vid](cvar)"
);
483
ANN
(
"example: @@x"
);
484
var:
485
F_ID
(nd_vid,
"local variable"
);
486
break
;
487
488
case
NODE_GVAR
:
489
ANN
(
"global variable reference"
);
490
ANN
(
"format: [nd_entry](gvar)"
);
491
ANN
(
"example: $x"
);
492
F_GENTRY
(nd_entry,
"global variable"
);
493
break
;
494
495
case
NODE_NTH_REF
:
496
ANN
(
"nth special variable reference"
);
497
ANN
(
"format: $[nd_nth]"
);
498
ANN
(
"example: $1, $2, .."
);
499
F_CUSTOM1
(nd_nth,
"variable"
, {
A
(
"$"
);
A_LONG
(node->nd_nth); });
500
break
;
501
502
case
NODE_BACK_REF
:
503
ANN
(
"back special variable reference"
);
504
ANN
(
"format: $[nd_nth]"
);
505
ANN
(
"example: $&, $`, $', $+"
);
506
F_CUSTOM1
(nd_nth,
"variable"
, {
507
char
name
[3];
508
name[0] =
'$'
;
509
name[1] = (char)node->nd_nth;
510
name[2] =
'\0'
;
511
A
(name);
512
});
513
break
;
514
515
case
NODE_MATCH
:
516
ANN
(
"match expression (against $_ implicitly)"
);
517
ANN
(
"format: [nd_lit] (in condition)"
);
518
ANN
(
"example: if /foo/; foo; end"
);
519
F_LIT
(nd_lit,
"regexp"
);
520
break
;
521
522
case
NODE_MATCH2
:
523
ANN
(
"match expression (regexp first)"
);
524
ANN
(
"format: [nd_recv] =~ [nd_value]"
);
525
ANN
(
"example: /foo/ =~ 'foo'"
);
526
F_NODE
(nd_recv,
"regexp (receiver)"
);
527
LAST_NODE
;
528
F_NODE
(nd_value,
"string (argument)"
);
529
break
;
530
531
case
NODE_MATCH3
:
532
ANN
(
"match expression (regexp second)"
);
533
ANN
(
"format: [nd_recv] =~ [nd_value]"
);
534
ANN
(
"example: 'foo' =~ /foo/"
);
535
F_NODE
(nd_recv,
"string (receiver)"
);
536
LAST_NODE
;
537
F_NODE
(nd_value,
"regexp (argument)"
);
538
break
;
539
540
case
NODE_LIT
:
541
ANN
(
"literal"
);
542
ANN
(
"format: [nd_lit]"
);
543
ANN
(
"example: 1, /foo/"
);
544
goto
lit;
545
case
NODE_STR
:
546
ANN
(
"string literal"
);
547
ANN
(
"format: [nd_lit]"
);
548
ANN
(
"example: 'foo'"
);
549
goto
lit;
550
case
NODE_XSTR
:
551
ANN
(
"xstring literal"
);
552
ANN
(
"format: [nd_lit]"
);
553
ANN
(
"example: `foo`"
);
554
lit:
555
F_LIT
(nd_lit,
"literal"
);
556
break
;
557
558
case
NODE_DSTR
:
559
ANN
(
"string literal with interpolation"
);
560
ANN
(
"format: [nd_lit]"
);
561
ANN
(
"example: \"foo#{ bar }baz\""
);
562
goto
dlit;
563
case
NODE_DXSTR
:
564
ANN
(
"xstring literal with interpolation"
);
565
ANN
(
"format: [nd_lit]"
);
566
ANN
(
"example: `foo#{ bar }baz`"
);
567
goto
dlit;
568
case
NODE_DREGX
:
569
ANN
(
"regexp literal with interpolation"
);
570
ANN
(
"format: [nd_lit]"
);
571
ANN
(
"example: /foo#{ bar }baz/"
);
572
goto
dlit;
573
case
NODE_DREGX_ONCE
:
574
ANN
(
"regexp literal with interpolation and once flag"
);
575
ANN
(
"format: [nd_lit]"
);
576
ANN
(
"example: /foo#{ bar }baz/o"
);
577
goto
dlit;
578
case
NODE_DSYM
:
579
ANN
(
"symbol literal with interpolation"
);
580
ANN
(
"format: [nd_lit]"
);
581
ANN
(
"example: :\"foo#{ bar }baz\""
);
582
dlit:
583
F_LIT
(nd_lit,
"literal"
);
584
F_NODE
(
nd_next
->nd_head,
"preceding string"
);
585
LAST_NODE
;
586
F_NODE
(
nd_next
->nd_next,
"interpolation"
);
587
break
;
588
589
case
NODE_EVSTR
:
590
ANN
(
"interpolation expression"
);
591
ANN
(
"format: \"..#{ [nd_lit] }..\""
);
592
ANN
(
"example: \"foo#{ bar }baz\""
);
593
LAST_NODE
;
594
F_NODE
(nd_body,
"body"
);
595
break
;
596
597
case
NODE_ARGSCAT
:
598
ANN
(
"splat argument following arguments"
);
599
ANN
(
"format: ..(*[nd_head], [nd_body..])"
);
600
ANN
(
"example: foo(*ary, post_arg1, post_arg2)"
);
601
F_NODE
(nd_head,
"preceding array"
);
602
LAST_NODE
;
603
F_NODE
(nd_body,
"following array"
);
604
break
;
605
606
case
NODE_ARGSPUSH
:
607
ANN
(
"splat argument following one argument"
);
608
ANN
(
"format: ..(*[nd_head], [nd_body])"
);
609
ANN
(
"example: foo(*ary, post_arg)"
);
610
F_NODE
(nd_head,
"preceding array"
);
611
LAST_NODE
;
612
F_NODE
(nd_body,
"following element"
);
613
break
;
614
615
case
NODE_SPLAT
:
616
ANN
(
"splat argument"
);
617
ANN
(
"format: *[nd_head]"
);
618
ANN
(
"example: foo(*ary)"
);
619
LAST_NODE
;
620
F_NODE
(nd_head,
"splat'ed array"
);
621
break
;
622
623
case
NODE_BLOCK_PASS
:
624
ANN
(
"arguments with block argument"
);
625
ANN
(
"format: ..([nd_head], &[nd_body])"
);
626
ANN
(
"example: foo(x, &blk)"
);
627
F_NODE
(nd_head,
"other arguments"
);
628
LAST_NODE
;
629
F_NODE
(nd_body,
"block argument"
);
630
break
;
631
632
case
NODE_DEFN
:
633
ANN
(
"method definition"
);
634
ANN
(
"format: def [nd_mid] [nd_defn]; end"
);
635
ANN
(
"example; def foo; bar; end"
);
636
F_ID
(nd_mid,
"method name"
);
637
LAST_NODE
;
638
F_NODE
(nd_defn,
"method definition"
);
639
break
;
640
641
case
NODE_DEFS
:
642
ANN
(
"singleton method definition"
);
643
ANN
(
"format: def [nd_recv].[nd_mid] [nd_defn]; end"
);
644
ANN
(
"example; def obj.foo; bar; end"
);
645
F_NODE
(nd_recv,
"receiver"
);
646
F_ID
(nd_mid,
"method name"
);
647
LAST_NODE
;
648
F_NODE
(nd_defn,
"method definition"
);
649
break
;
650
651
case
NODE_ALIAS
:
652
ANN
(
"method alias statement"
);
653
ANN
(
"format: alias [u1.node] [u2.node]"
);
654
ANN
(
"example: alias bar foo"
);
655
F_NODE
(u1.node,
"new name"
);
656
LAST_NODE
;
657
F_NODE
(u2.node,
"old name"
);
658
break
;
659
660
case
NODE_VALIAS
:
661
ANN
(
"global variable alias statement"
);
662
ANN
(
"format: alias [u1.id](gvar) [u2.id](gvar)"
);
663
ANN
(
"example: alias $y $x"
);
664
F_ID
(u1.id,
"new name"
);
665
F_ID
(u2.id,
"old name"
);
666
break
;
667
668
case
NODE_UNDEF
:
669
ANN
(
"method alias statement"
);
670
ANN
(
"format: undef [u2.node]"
);
671
ANN
(
"example: undef foo"
);
672
LAST_NODE
;
673
F_NODE
(u2.node,
"old name"
);
674
break
;
675
676
case
NODE_CLASS
:
677
ANN
(
"class definition"
);
678
ANN
(
"format: class [nd_cpath] < [nd_super]; [nd_body]; end"
);
679
ANN
(
"example: class C2 < C; ..; end"
);
680
F_NODE
(nd_cpath,
"class path"
);
681
F_NODE
(nd_super,
"superclass"
);
682
LAST_NODE
;
683
F_NODE
(nd_body,
"class definition"
);
684
break
;
685
686
case
NODE_MODULE
:
687
ANN
(
"module definition"
);
688
ANN
(
"format: module [nd_cpath]; [nd_body]; end"
);
689
ANN
(
"example: module M; ..; end"
);
690
F_NODE
(nd_cpath,
"module path"
);
691
LAST_NODE
;
692
F_NODE
(nd_body,
"module definition"
);
693
break
;
694
695
case
NODE_SCLASS
:
696
ANN
(
"singleton class definition"
);
697
ANN
(
"format: class << [nd_recv]; [nd_body]; end"
);
698
ANN
(
"example: class << obj; ..; end"
);
699
F_NODE
(nd_recv,
"receiver"
);
700
LAST_NODE
;
701
F_NODE
(nd_body,
"singleton class definition"
);
702
break
;
703
704
case
NODE_COLON2
:
705
ANN
(
"scoped constant reference"
);
706
ANN
(
"format: [nd_head]::[nd_mid]"
);
707
ANN
(
"example: M::C"
);
708
F_ID
(nd_mid,
"constant name"
);
709
LAST_NODE
;
710
F_NODE
(nd_head,
"receiver"
);
711
break
;
712
713
case
NODE_COLON3
:
714
ANN
(
"top-level constant reference"
);
715
ANN
(
"format: ::[nd_mid]"
);
716
ANN
(
"example: ::Object"
);
717
F_ID
(nd_mid,
"constant name"
);
718
break
;
719
720
case
NODE_DOT2
:
721
ANN
(
"range constructor (incl.)"
);
722
ANN
(
"format: [nd_beg]..[nd_end]"
);
723
ANN
(
"example: 1..5"
);
724
goto
dot;
725
case
NODE_DOT3
:
726
ANN
(
"range constructor (excl.)"
);
727
ANN
(
"format: [nd_beg]...[nd_end]"
);
728
ANN
(
"example: 1...5"
);
729
goto
dot;
730
case
NODE_FLIP2
:
731
ANN
(
"flip-flop condition (incl.)"
);
732
ANN
(
"format: [nd_beg]..[nd_end]"
);
733
ANN
(
"example: if (x==1)..(x==5); foo; end"
);
734
goto
dot;
735
case
NODE_FLIP3
:
736
ANN
(
"flip-flop condition (excl.)"
);
737
ANN
(
"format: [nd_beg]...[nd_end]"
);
738
ANN
(
"example: if (x==1)...(x==5); foo; end"
);
739
dot:
740
F_NODE
(nd_beg,
"begin"
);
741
LAST_NODE
;
742
F_NODE
(nd_end,
"end"
);
743
break
;
744
745
case
NODE_SELF
:
746
ANN
(
"self"
);
747
ANN
(
"format: self"
);
748
ANN
(
"example: self"
);
749
break
;
750
751
case
NODE_NIL
:
752
ANN
(
"nil"
);
753
ANN
(
"format: nil"
);
754
ANN
(
"example: nil"
);
755
break
;
756
757
case
NODE_TRUE
:
758
ANN
(
"true"
);
759
ANN
(
"format: true"
);
760
ANN
(
"example: true"
);
761
break
;
762
763
case
NODE_FALSE
:
764
ANN
(
"false"
);
765
ANN
(
"format: false"
);
766
ANN
(
"example: false"
);
767
break
;
768
769
case
NODE_ERRINFO
:
770
ANN
(
"virtual reference to $!"
);
771
ANN
(
"format: rescue => id"
);
772
ANN
(
"example: rescue => id"
);
773
break
;
774
775
case
NODE_DEFINED
:
776
ANN
(
"defined? expression"
);
777
ANN
(
"format: defined?([nd_head])"
);
778
ANN
(
"example: defined?(foo)"
);
779
F_NODE
(nd_head,
"expr"
);
780
break
;
781
782
case
NODE_POSTEXE
:
783
ANN
(
"post-execution"
);
784
ANN
(
"format: END { [nd_body] }"
);
785
ANN
(
"example: END { foo }"
);
786
LAST_NODE
;
787
F_NODE
(nd_body,
"END clause"
);
788
break
;
789
790
case
NODE_ATTRASGN
:
791
ANN
(
"attr assignment"
);
792
ANN
(
"format: [nd_recv].[nd_mid] = [nd_args]"
);
793
ANN
(
"example: struct.field = foo"
);
794
if
(node->nd_recv == (
NODE
*) 1) {
795
F_MSG
(nd_recv,
"receiver"
,
"1 (self)"
);
796
}
797
else
{
798
F_NODE
(nd_recv,
"receiver"
);
799
}
800
F_ID
(nd_mid,
"method name"
);
801
LAST_NODE
;
802
F_NODE
(
nd_args
,
"arguments"
);
803
break
;
804
805
case
NODE_PRELUDE
:
806
ANN
(
"pre-execution"
);
807
ANN
(
"format: BEGIN { [nd_head] }; [nd_body]"
);
808
ANN
(
"example: bar; BEGIN { foo }"
);
809
F_NODE
(nd_head,
"prelude"
);
810
LAST_NODE
;
811
F_NODE
(nd_body,
"body"
);
812
break
;
813
814
case
NODE_LAMBDA
:
815
ANN
(
"lambda expression"
);
816
ANN
(
"format: -> [nd_body]"
);
817
ANN
(
"example: -> { foo }"
);
818
LAST_NODE
;
819
F_NODE
(nd_body,
"lambda clause"
);
820
break
;
821
822
case
NODE_OPT_ARG
:
823
ANN
(
"optional arguments"
);
824
ANN
(
"format: def method_name([nd_body=some], [nd_next..])"
);
825
ANN
(
"example: def foo(a, b=1, c); end"
);
826
F_NODE
(nd_body,
"body"
);
827
LAST_NODE
;
828
F_NODE
(nd_next,
"next"
);
829
break
;
830
831
case
NODE_KW_ARG
:
832
ANN
(
"keyword arguments"
);
833
ANN
(
"format: def method_name([nd_body=some], [nd_next..])"
);
834
ANN
(
"example: def foo(a:1, b:2); end"
);
835
F_NODE
(nd_body,
"body"
);
836
LAST_NODE
;
837
F_NODE
(nd_next,
"next"
);
838
break
;
839
840
case
NODE_POSTARG
:
841
ANN
(
"post arguments"
);
842
ANN
(
"format: *[nd_1st], [nd_2nd..] = .."
);
843
ANN
(
"example: a, *rest, z = foo"
);
844
if
((
VALUE
)node->nd_1st != (
VALUE
)-1) {
845
F_NODE
(nd_1st,
"rest argument"
);
846
}
847
else
{
848
F_MSG
(nd_1st,
"rest argument"
,
"-1 (rest argument without name)"
);
849
}
850
LAST_NODE
;
851
F_NODE
(nd_2nd,
"post arguments"
);
852
break
;
853
854
case
NODE_ARGS
:
855
ANN
(
"method parameters"
);
856
ANN
(
"format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])"
);
857
ANN
(
"example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end"
);
858
F_INT
(
nd_ainfo
->pre_args_num,
"count of mandatory (pre-)arguments"
);
859
F_NODE
(
nd_ainfo
->pre_init,
"initialization of (pre-)arguments"
);
860
F_INT
(
nd_ainfo
->post_args_num,
"count of mandatory post-arguments"
);
861
F_NODE
(
nd_ainfo
->post_init,
"initialization of post-arguments"
);
862
F_ID
(
nd_ainfo
->first_post_arg,
"first post argument"
);
863
F_ID
(
nd_ainfo
->rest_arg,
"rest argument"
);
864
F_ID
(
nd_ainfo
->block_arg,
"block argument"
);
865
F_NODE
(
nd_ainfo
->opt_args,
"optional arguments"
);
866
LAST_NODE
;
867
F_NODE
(
nd_ainfo
->kw_args,
"keyword arguments"
);
868
F_NODE
(
nd_ainfo
->kw_rest_arg,
"keyword rest argument"
);
869
break
;
870
871
case
NODE_SCOPE
:
872
ANN
(
"new scope"
);
873
ANN
(
"format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body"
);
874
F_CUSTOM1
(nd_tbl,
"local table"
, {
875
ID
*tbl = node->nd_tbl;
876
int
i;
877
int
size
= tbl ? (int)*tbl++ : 0;
878
if
(size == 0)
A
(
"(empty)"
);
879
for
(i = 0; i <
size
; i++) {
880
A_ID
(tbl[i]);
if
(i < size - 1)
A
(
","
);
881
}
882
});
883
F_NODE
(
nd_args
,
"arguments"
);
884
LAST_NODE
;
885
F_NODE
(nd_body,
"body"
);
886
break
;
887
888
default
:
889
rb_bug
(
"dump_node: unknown node: %s"
,
ruby_node_name
(
nd_type
(node)));
890
}
891
}
892
893
VALUE
894
rb_parser_dump_tree
(
NODE
*node,
int
comment)
895
{
896
VALUE
buf
=
rb_str_new_cstr
(
897
"###########################################################\n"
898
"## Do NOT use this node dump for any purpose other than ##\n"
899
"## debug and research. Compatibility is not guaranteed. ##\n"
900
"###########################################################\n\n"
901
);
902
dump_node
(buf,
rb_str_new_cstr
(
"# "
), comment, node);
903
return
buf
;
904
}
905
Generated on Mon Sep 22 2014 11:05:35 for Ruby by
1.8.2