Ruby 4.1.0dev (2026-04-22 revision 42b3cdc51a30b4b155f80fb7808e2b0e634dddc1)
ast.c (42b3cdc51a30b4b155f80fb7808e2b0e634dddc1)
1/* indent-tabs-mode: nil */
2#include "internal.h"
3#include "internal/ruby_parser.h"
4#include "internal/symbol.h"
5#include "internal/warnings.h"
6#include "iseq.h"
7#include "node.h"
8#include "ruby.h"
9#include "ruby/encoding.h"
10#include "ruby/util.h"
11#include "vm_core.h"
12
13#include "builtin.h"
14
15static VALUE rb_mAST;
16static VALUE rb_cNode;
17static VALUE rb_cLocation;
18
20 VALUE ast_value;
21 const NODE *node;
22};
23
24static void
25node_gc_mark(void *ptr)
26{
27 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
28 rb_gc_mark(data->ast_value);
29}
30
31static size_t
32node_memsize(const void *ptr)
33{
34 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
35 size_t size = sizeof(struct ASTNodeData);
36 if (data->ast_value) {
37 rb_ast_t *ast = rb_ruby_ast_data_get(data->ast_value);
38 size += rb_ast_memsize(ast);
39 }
40
41 return size;
42}
43
44static const rb_data_type_t rb_node_type = {
45 "AST/node",
46 {node_gc_mark, RUBY_TYPED_DEFAULT_FREE, node_memsize,},
47 0, 0,
49};
50
52 int first_lineno;
53 int first_column;
54 int last_lineno;
55 int last_column;
56};
57
58static void
59location_gc_mark(void *ptr)
60{
61}
62
63static size_t
64location_memsize(const void *ptr)
65{
66 return sizeof(struct ASTLocationData);
67}
68
69static const rb_data_type_t rb_location_type = {
70 "AST/location",
71 {location_gc_mark, RUBY_TYPED_DEFAULT_FREE, location_memsize,},
72 0, 0,
74};
75
76
77static VALUE rb_ast_node_alloc(VALUE klass);
78
79static void
80setup_node(VALUE obj, VALUE ast_value, const NODE *node)
81{
82 struct ASTNodeData *data;
83
84 TypedData_Get_Struct(obj, struct ASTNodeData, &rb_node_type, data);
85 data->ast_value = ast_value;
86 data->node = node;
87}
88
89static VALUE
90ast_new_internal(VALUE ast_value, const NODE *node)
91{
92 VALUE obj;
93
94 obj = rb_ast_node_alloc(rb_cNode);
95 setup_node(obj, ast_value, node);
96
97 return obj;
98}
99
100static VALUE rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
101static VALUE rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
102
103static VALUE
104ast_parse_new(void)
105{
106 return rb_parser_set_context(rb_parser_new(), NULL, 0);
107}
108
109static VALUE
110ast_parse_done(VALUE ast_value)
111{
112 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
113
114 if (!ast->body.root) {
115 rb_ast_dispose(ast);
116 rb_exc_raise(GET_EC()->errinfo);
117 }
118
119 return ast_new_internal(ast_value, (NODE *)ast->body.root);
120}
121
122static VALUE
123setup_vparser(VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
124{
125 VALUE vparser = ast_parse_new();
126 if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser);
127 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
128 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
129 return vparser;
130}
131
132static VALUE
133ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
134{
135 return rb_ast_parse_str(str, keep_script_lines, error_tolerant, keep_tokens);
136}
137
138static VALUE
139rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
140{
141 VALUE ast_value = Qnil;
142 StringValue(str);
143 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
144 ast_value = rb_parser_compile_string_path(vparser, Qnil, str, 1);
145 return ast_parse_done(ast_value);
146}
147
148static VALUE
149ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
150{
151 return rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
152}
153
154static VALUE
155rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
156{
157 VALUE f;
158 VALUE ast_value = Qnil;
159 rb_encoding *enc = rb_utf8_encoding();
160
161 f = rb_file_open_str(path, "r");
162 rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
163 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
164 ast_value = rb_parser_compile_file_path(vparser, Qnil, f, 1);
165 rb_io_close(f);
166 return ast_parse_done(ast_value);
167}
168
169static VALUE
170rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
171{
172 VALUE ast_value = Qnil;
173
174 array = rb_check_array_type(array);
175 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
176 ast_value = rb_parser_compile_array(vparser, Qnil, array, 1);
177 return ast_parse_done(ast_value);
178}
179
180static VALUE node_children(VALUE, const NODE*);
181
182static VALUE
183node_find(VALUE self, const int node_id)
184{
185 VALUE ary;
186 long i;
187 struct ASTNodeData *data;
188 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
189
190 if (nd_node_id(data->node) == node_id) return self;
191
192 ary = node_children(data->ast_value, data->node);
193
194 for (i = 0; i < RARRAY_LEN(ary); i++) {
195 VALUE child = RARRAY_AREF(ary, i);
196
197 if (CLASS_OF(child) == rb_cNode) {
198 VALUE result = node_find(child, node_id);
199 if (RTEST(result)) return result;
200 }
201 }
202
203 return Qnil;
204}
205
206extern VALUE rb_e_script;
207
208static VALUE
209node_id_for_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
210{
211 int node_id;
212
213 if (!rb_frame_info_p(location)) {
214 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
215 }
216
217 node_id = rb_get_node_id_from_frame_info(location);
218 if (node_id == -1) {
219 return Qnil;
220 }
221
222 return INT2NUM(node_id);
223}
224
225static VALUE
226ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
227{
228 VALUE node, lines = Qnil;
229 const rb_iseq_t *iseq;
230 int node_id;
231
232 if (rb_frame_info_p(body)) {
233 iseq = rb_get_iseq_from_frame_info(body);
234 node_id = rb_get_node_id_from_frame_info(body);
235 }
236 else {
237 iseq = NULL;
238
239 if (rb_obj_is_proc(body)) {
240 iseq = vm_proc_iseq(body);
241
242 if (!rb_obj_is_iseq((VALUE)iseq)) return Qnil;
243 }
244 else {
245 iseq = rb_method_iseq(body);
246 }
247 if (iseq) {
248 node_id = ISEQ_BODY(iseq)->location.node_id;
249 }
250 }
251
252 if (!iseq) {
253 return Qnil;
254 }
255
256 if (ISEQ_BODY(iseq)->prism) {
257 rb_raise(rb_eRuntimeError, "cannot get AST for ISEQ compiled by prism");
258 }
259
260 lines = ISEQ_BODY(iseq)->variable.script_lines;
261
262 VALUE path = rb_iseq_path(iseq);
263 int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0;
264
265 if (NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
266 rb_raise(rb_eArgError, "cannot get AST for method defined in eval");
267 }
268
269 if (!NIL_P(lines)) {
270 node = rb_ast_parse_array(lines, keep_script_lines, error_tolerant, keep_tokens);
271 }
272 else if (e_option) {
273 node = rb_ast_parse_str(rb_e_script, keep_script_lines, error_tolerant, keep_tokens);
274 }
275 else {
276 node = rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
277 }
278
279 return node_find(node, node_id);
280}
281
282static VALUE
283rb_ast_node_alloc(VALUE klass)
284{
285 struct ASTNodeData *data;
286 VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
287
288 return obj;
289}
290
291static const char*
292node_type_to_str(const NODE *node)
293{
294 return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
295}
296
297static VALUE
298ast_node_type(rb_execution_context_t *ec, VALUE self)
299{
300 struct ASTNodeData *data;
301 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
302
303 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
304}
305
306static VALUE
307ast_node_node_id(rb_execution_context_t *ec, VALUE self)
308{
309 struct ASTNodeData *data;
310 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
311
312 return INT2FIX(nd_node_id(data->node));
313}
314
315#define NEW_CHILD(ast_value, node) (node ? ast_new_internal(ast_value, node) : Qnil)
316
317static VALUE
318rb_ary_new_from_node_args(VALUE ast_value, long n, ...)
319{
320 va_list ar;
321 VALUE ary;
322 long i;
323
324 ary = rb_ary_new2(n);
325
326 va_start(ar, n);
327 for (i=0; i<n; i++) {
328 NODE *node;
329 node = va_arg(ar, NODE *);
330 rb_ary_push(ary, NEW_CHILD(ast_value, node));
331 }
332 va_end(ar);
333 return ary;
334}
335
336static VALUE
337dump_block(VALUE ast_value, const struct RNode_BLOCK *node)
338{
339 VALUE ary = rb_ary_new();
340 do {
341 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
342 } while (node->nd_next &&
343 nd_type_p(node->nd_next, NODE_BLOCK) &&
344 (node = RNODE_BLOCK(node->nd_next), 1));
345 if (node->nd_next) {
346 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
347 }
348
349 return ary;
350}
351
352static VALUE
353dump_array(VALUE ast_value, const struct RNode_LIST *node)
354{
355 VALUE ary = rb_ary_new();
356 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
357
358 while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
359 node = RNODE_LIST(node->nd_next);
360 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
361 }
362 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
363
364 return ary;
365}
366
367static VALUE
368dump_parser_array(VALUE ast_value, rb_parser_ary_t *p_ary)
369{
370 VALUE ary;
371
372 if (p_ary->data_type != PARSER_ARY_DATA_NODE) {
373 rb_bug("unexpected rb_parser_ary_data_type: %d", p_ary->data_type);
374 }
375
376 ary = rb_ary_new();
377
378 for (long i = 0; i < p_ary->len; i++) {
379 rb_ary_push(ary, NEW_CHILD(ast_value, p_ary->data[i]));
380 }
381
382 return ary;
383}
384
385static VALUE
386var_name(ID id)
387{
388 if (!id) return Qnil;
389 if (!rb_id2str(id)) return Qnil;
390 return ID2SYM(id);
391}
392
393static VALUE
394no_name_rest(void)
395{
396 ID rest;
397 CONST_ID(rest, "NODE_SPECIAL_NO_NAME_REST");
398 return ID2SYM(rest);
399}
400
401static VALUE
402rest_arg(VALUE ast_value, const NODE *rest_arg)
403{
404 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast_value, rest_arg) : no_name_rest();
405}
406
407static ID
408node_colon_name(const NODE *node)
409{
410 switch (nd_type(node)) {
411 case NODE_COLON2:
412 return RNODE_COLON2(node)->nd_mid;
413 case NODE_COLON3:
414 return RNODE_COLON3(node)->nd_mid;
415 default:
416 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
417 }
418}
419
420static VALUE
421node_children(VALUE ast_value, const NODE *node)
422{
423 char name[sizeof("$") + DECIMAL_SIZE_OF(long)];
424
425 enum node_type type = nd_type(node);
426 switch (type) {
427 case NODE_BLOCK:
428 return dump_block(ast_value, RNODE_BLOCK(node));
429 case NODE_IF:
430 return rb_ary_new_from_node_args(ast_value, 3, RNODE_IF(node)->nd_cond, RNODE_IF(node)->nd_body, RNODE_IF(node)->nd_else);
431 case NODE_UNLESS:
432 return rb_ary_new_from_node_args(ast_value, 3, RNODE_UNLESS(node)->nd_cond, RNODE_UNLESS(node)->nd_body, RNODE_UNLESS(node)->nd_else);
433 case NODE_CASE:
434 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE(node)->nd_head, RNODE_CASE(node)->nd_body);
435 case NODE_CASE2:
436 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE2(node)->nd_head, RNODE_CASE2(node)->nd_body);
437 case NODE_CASE3:
438 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE3(node)->nd_head, RNODE_CASE3(node)->nd_body);
439 case NODE_WHEN:
440 return rb_ary_new_from_node_args(ast_value, 3, RNODE_WHEN(node)->nd_head, RNODE_WHEN(node)->nd_body, RNODE_WHEN(node)->nd_next);
441 case NODE_IN:
442 return rb_ary_new_from_node_args(ast_value, 3, RNODE_IN(node)->nd_head, RNODE_IN(node)->nd_body, RNODE_IN(node)->nd_next);
443 case NODE_WHILE:
444 case NODE_UNTIL:
445 return rb_ary_push(rb_ary_new_from_node_args(ast_value, 2, RNODE_WHILE(node)->nd_cond, RNODE_WHILE(node)->nd_body),
446 RBOOL(RNODE_WHILE(node)->nd_state));
447 case NODE_ITER:
448 case NODE_FOR:
449 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ITER(node)->nd_iter, RNODE_ITER(node)->nd_body);
450 case NODE_FOR_MASGN:
451 return rb_ary_new_from_node_args(ast_value, 1, RNODE_FOR_MASGN(node)->nd_var);
452 case NODE_BREAK:
453 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BREAK(node)->nd_stts);
454 case NODE_NEXT:
455 return rb_ary_new_from_node_args(ast_value, 1, RNODE_NEXT(node)->nd_stts);
456 case NODE_RETURN:
457 return rb_ary_new_from_node_args(ast_value, 1, RNODE_RETURN(node)->nd_stts);
458 case NODE_REDO:
459 return rb_ary_new_from_node_args(ast_value, 0);
460 case NODE_RETRY:
461 return rb_ary_new_from_node_args(ast_value, 0);
462 case NODE_BEGIN:
463 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BEGIN(node)->nd_body);
464 case NODE_RESCUE:
465 return rb_ary_new_from_node_args(ast_value, 3, RNODE_RESCUE(node)->nd_head, RNODE_RESCUE(node)->nd_resq, RNODE_RESCUE(node)->nd_else);
466 case NODE_RESBODY:
467 return rb_ary_new_from_node_args(ast_value, 4, RNODE_RESBODY(node)->nd_args, RNODE_RESBODY(node)->nd_exc_var, RNODE_RESBODY(node)->nd_body, RNODE_RESBODY(node)->nd_next);
468 case NODE_ENSURE:
469 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
470 case NODE_AND:
471 case NODE_OR:
472 {
473 VALUE ary = rb_ary_new();
474
475 while (1) {
476 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_1st));
477 if (!RNODE_AND(node)->nd_2nd || !nd_type_p(RNODE_AND(node)->nd_2nd, type))
478 break;
479 node = RNODE_AND(node)->nd_2nd;
480 }
481 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_2nd));
482 return ary;
483 }
484 case NODE_MASGN:
485 if (NODE_NAMED_REST_P(RNODE_MASGN(node)->nd_args)) {
486 return rb_ary_new_from_node_args(ast_value, 3, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head, RNODE_MASGN(node)->nd_args);
487 }
488 else {
489 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_value),
490 NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_head),
491 no_name_rest());
492 }
493 case NODE_LASGN:
494 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node)->nd_value)) {
495 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
496 }
497 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_LASGN(node)->nd_value));
498 case NODE_DASGN:
499 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node)->nd_value)) {
500 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
501 }
502 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_DASGN(node)->nd_value));
503 case NODE_IASGN:
504 return rb_ary_new_from_args(2, var_name(RNODE_IASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_IASGN(node)->nd_value));
505 case NODE_CVASGN:
506 return rb_ary_new_from_args(2, var_name(RNODE_CVASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CVASGN(node)->nd_value));
507 case NODE_GASGN:
508 return rb_ary_new_from_args(2, var_name(RNODE_GASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_GASGN(node)->nd_value));
509 case NODE_CDECL:
510 if (RNODE_CDECL(node)->nd_vid) {
511 return rb_ary_new_from_args(2, ID2SYM(RNODE_CDECL(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
512 }
513 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_else), ID2SYM(node_colon_name(RNODE_CDECL(node)->nd_else)), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
514 case NODE_OP_ASGN1:
515 return rb_ary_new_from_args(4, NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_recv),
516 ID2SYM(RNODE_OP_ASGN1(node)->nd_mid),
517 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_index),
518 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_rvalue));
519 case NODE_OP_ASGN2:
520 return rb_ary_new_from_args(5, NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_recv),
521 RBOOL(RNODE_OP_ASGN2(node)->nd_aid),
522 ID2SYM(RNODE_OP_ASGN2(node)->nd_vid),
523 ID2SYM(RNODE_OP_ASGN2(node)->nd_mid),
524 NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_value));
525 case NODE_OP_ASGN_AND:
526 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_head), ID2SYM(idANDOP),
527 NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_value));
528 case NODE_OP_ASGN_OR:
529 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_head), ID2SYM(idOROP),
530 NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_value));
531 case NODE_OP_CDECL:
532 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_head),
533 ID2SYM(RNODE_OP_CDECL(node)->nd_aid),
534 NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_value));
535 case NODE_CALL:
536 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CALL(node)->nd_recv),
537 ID2SYM(RNODE_CALL(node)->nd_mid),
538 NEW_CHILD(ast_value, RNODE_CALL(node)->nd_args));
539 case NODE_OPCALL:
540 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_recv),
541 ID2SYM(RNODE_OPCALL(node)->nd_mid),
542 NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_args));
543 case NODE_QCALL:
544 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_recv),
545 ID2SYM(RNODE_QCALL(node)->nd_mid),
546 NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_args));
547 case NODE_FCALL:
548 return rb_ary_new_from_args(2, ID2SYM(RNODE_FCALL(node)->nd_mid),
549 NEW_CHILD(ast_value, RNODE_FCALL(node)->nd_args));
550 case NODE_VCALL:
551 return rb_ary_new_from_args(1, ID2SYM(RNODE_VCALL(node)->nd_mid));
552 case NODE_SUPER:
553 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SUPER(node)->nd_args);
554 case NODE_ZSUPER:
555 return rb_ary_new_from_node_args(ast_value, 0);
556 case NODE_LIST:
557 return dump_array(ast_value, RNODE_LIST(node));
558 case NODE_ZLIST:
559 return rb_ary_new_from_node_args(ast_value, 0);
560 case NODE_HASH:
561 return rb_ary_new_from_node_args(ast_value, 1, RNODE_HASH(node)->nd_head);
562 case NODE_YIELD:
563 return rb_ary_new_from_node_args(ast_value, 1, RNODE_YIELD(node)->nd_head);
564 case NODE_LVAR:
565 return rb_ary_new_from_args(1, var_name(RNODE_LVAR(node)->nd_vid));
566 case NODE_DVAR:
567 return rb_ary_new_from_args(1, var_name(RNODE_DVAR(node)->nd_vid));
568 case NODE_IVAR:
569 return rb_ary_new_from_args(1, ID2SYM(RNODE_IVAR(node)->nd_vid));
570 case NODE_CONST:
571 return rb_ary_new_from_args(1, ID2SYM(RNODE_CONST(node)->nd_vid));
572 case NODE_CVAR:
573 return rb_ary_new_from_args(1, ID2SYM(RNODE_CVAR(node)->nd_vid));
574 case NODE_GVAR:
575 return rb_ary_new_from_args(1, ID2SYM(RNODE_GVAR(node)->nd_vid));
576 case NODE_NTH_REF:
577 snprintf(name, sizeof(name), "$%ld", RNODE_NTH_REF(node)->nd_nth);
578 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
579 case NODE_BACK_REF:
580 name[0] = '$';
581 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
582 name[2] = '\0';
583 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
584 case NODE_MATCH:
585 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
586 case NODE_MATCH2:
587 if (RNODE_MATCH2(node)->nd_args) {
588 return rb_ary_new_from_node_args(ast_value, 3, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value, RNODE_MATCH2(node)->nd_args);
589 }
590 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value);
591 case NODE_MATCH3:
592 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
593 case NODE_STR:
594 case NODE_XSTR:
595 return rb_ary_new_from_args(1, rb_node_str_string_val(node));
596 case NODE_INTEGER:
597 return rb_ary_new_from_args(1, rb_node_integer_literal_val(node));
598 case NODE_FLOAT:
599 return rb_ary_new_from_args(1, rb_node_float_literal_val(node));
600 case NODE_RATIONAL:
601 return rb_ary_new_from_args(1, rb_node_rational_literal_val(node));
602 case NODE_IMAGINARY:
603 return rb_ary_new_from_args(1, rb_node_imaginary_literal_val(node));
604 case NODE_REGX:
605 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
606 case NODE_ONCE:
607 return rb_ary_new_from_node_args(ast_value, 1, RNODE_ONCE(node)->nd_body);
608 case NODE_DSTR:
609 case NODE_DXSTR:
610 case NODE_DREGX:
611 case NODE_DSYM:
612 {
613 struct RNode_LIST *n = RNODE_DSTR(node)->nd_next;
614 VALUE head = Qnil, next = Qnil;
615 if (n) {
616 head = NEW_CHILD(ast_value, n->nd_head);
617 next = NEW_CHILD(ast_value, n->nd_next);
618 }
619 return rb_ary_new_from_args(3, rb_node_dstr_string_val(node), head, next);
620 }
621 case NODE_SYM:
622 return rb_ary_new_from_args(1, rb_node_sym_string_val(node));
623 case NODE_EVSTR:
624 return rb_ary_new_from_node_args(ast_value, 1, RNODE_EVSTR(node)->nd_body);
625 case NODE_ARGSCAT:
626 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSCAT(node)->nd_head, RNODE_ARGSCAT(node)->nd_body);
627 case NODE_ARGSPUSH:
628 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSPUSH(node)->nd_head, RNODE_ARGSPUSH(node)->nd_body);
629 case NODE_SPLAT:
630 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SPLAT(node)->nd_head);
631 case NODE_BLOCK_PASS:
632 return rb_ary_new_from_node_args(ast_value, 2, RNODE_BLOCK_PASS(node)->nd_head, RNODE_BLOCK_PASS(node)->nd_body);
633 case NODE_DEFN:
634 return rb_ary_new_from_args(2, ID2SYM(RNODE_DEFN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFN(node)->nd_defn));
635 case NODE_DEFS:
636 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_DEFS(node)->nd_recv), ID2SYM(RNODE_DEFS(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFS(node)->nd_defn));
637 case NODE_ALIAS:
638 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ALIAS(node)->nd_1st, RNODE_ALIAS(node)->nd_2nd);
639 case NODE_VALIAS:
640 return rb_ary_new_from_args(2, ID2SYM(RNODE_VALIAS(node)->nd_alias), ID2SYM(RNODE_VALIAS(node)->nd_orig));
641 case NODE_UNDEF:
642 return rb_ary_new_from_args(1, dump_parser_array(ast_value, RNODE_UNDEF(node)->nd_undefs));
643 case NODE_CLASS:
644 return rb_ary_new_from_node_args(ast_value, 3, RNODE_CLASS(node)->nd_cpath, RNODE_CLASS(node)->nd_super, RNODE_CLASS(node)->nd_body);
645 case NODE_MODULE:
646 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MODULE(node)->nd_cpath, RNODE_MODULE(node)->nd_body);
647 case NODE_SCLASS:
648 return rb_ary_new_from_node_args(ast_value, 2, RNODE_SCLASS(node)->nd_recv, RNODE_SCLASS(node)->nd_body);
649 case NODE_COLON2:
650 return rb_ary_new_from_args(2, NEW_CHILD(ast_value, RNODE_COLON2(node)->nd_head), ID2SYM(RNODE_COLON2(node)->nd_mid));
651 case NODE_COLON3:
652 return rb_ary_new_from_args(1, ID2SYM(RNODE_COLON3(node)->nd_mid));
653 case NODE_DOT2:
654 case NODE_DOT3:
655 case NODE_FLIP2:
656 case NODE_FLIP3:
657 return rb_ary_new_from_node_args(ast_value, 2, RNODE_DOT2(node)->nd_beg, RNODE_DOT2(node)->nd_end);
658 case NODE_SELF:
659 return rb_ary_new_from_node_args(ast_value, 0);
660 case NODE_NIL:
661 return rb_ary_new_from_node_args(ast_value, 0);
662 case NODE_TRUE:
663 return rb_ary_new_from_node_args(ast_value, 0);
664 case NODE_FALSE:
665 return rb_ary_new_from_node_args(ast_value, 0);
666 case NODE_ERRINFO:
667 return rb_ary_new_from_node_args(ast_value, 0);
668 case NODE_DEFINED:
669 return rb_ary_new_from_node_args(ast_value, 1, RNODE_DEFINED(node)->nd_head);
670 case NODE_POSTEXE:
671 return rb_ary_new_from_node_args(ast_value, 1, RNODE_POSTEXE(node)->nd_body);
672 case NODE_ATTRASGN:
673 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_ATTRASGN(node)->nd_recv), ID2SYM(RNODE_ATTRASGN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_ATTRASGN(node)->nd_args));
674 case NODE_LAMBDA:
675 return rb_ary_new_from_node_args(ast_value, 1, RNODE_LAMBDA(node)->nd_body);
676 case NODE_OPT_ARG:
677 return rb_ary_new_from_node_args(ast_value, 2, RNODE_OPT_ARG(node)->nd_body, RNODE_OPT_ARG(node)->nd_next);
678 case NODE_KW_ARG:
679 return rb_ary_new_from_node_args(ast_value, 2, RNODE_KW_ARG(node)->nd_body, RNODE_KW_ARG(node)->nd_next);
680 case NODE_POSTARG:
681 if (NODE_NAMED_REST_P(RNODE_POSTARG(node)->nd_1st)) {
682 return rb_ary_new_from_node_args(ast_value, 2, RNODE_POSTARG(node)->nd_1st, RNODE_POSTARG(node)->nd_2nd);
683 }
684 return rb_ary_new_from_args(2, no_name_rest(),
685 NEW_CHILD(ast_value, RNODE_POSTARG(node)->nd_2nd));
686 case NODE_ARGS:
687 {
688 struct rb_args_info *ainfo = &RNODE_ARGS(node)->nd_ainfo;
689 return rb_ary_new_from_args(10,
690 INT2NUM(ainfo->pre_args_num),
691 NEW_CHILD(ast_value, ainfo->pre_init),
692 NEW_CHILD(ast_value, (NODE *)ainfo->opt_args),
693 var_name(ainfo->first_post_arg),
694 INT2NUM(ainfo->post_args_num),
695 NEW_CHILD(ast_value, ainfo->post_init),
696 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
697 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
698 : var_name(ainfo->rest_arg)),
699 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, (NODE *)ainfo->kw_args)),
700 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, ainfo->kw_rest_arg)),
701 (ainfo->no_blockarg ? Qfalse : var_name(ainfo->block_arg)));
702 }
703 case NODE_SCOPE:
704 {
705 rb_ast_id_table_t *tbl = RNODE_SCOPE(node)->nd_tbl;
706 int i, size = tbl ? tbl->size : 0;
707 VALUE locals = rb_ary_new_capa(size);
708 for (i = 0; i < size; i++) {
709 rb_ary_push(locals, var_name(tbl->ids[i]));
710 }
711 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast_value, (NODE *)RNODE_SCOPE(node)->nd_args), NEW_CHILD(ast_value, RNODE_SCOPE(node)->nd_body));
712 }
713 case NODE_ARYPTN:
714 {
715 VALUE rest = rest_arg(ast_value, RNODE_ARYPTN(node)->rest_arg);
716 return rb_ary_new_from_args(4,
717 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->nd_pconst),
718 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->pre_args),
719 rest,
720 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->post_args));
721 }
722 case NODE_FNDPTN:
723 {
724 VALUE pre_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->pre_rest_arg);
725 VALUE post_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->post_rest_arg);
726 return rb_ary_new_from_args(4,
727 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->nd_pconst),
728 pre_rest,
729 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->args),
730 post_rest);
731 }
732 case NODE_HSHPTN:
733 {
734 VALUE kwrest = RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
735 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwrestarg);
736
737 return rb_ary_new_from_args(3,
738 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pconst),
739 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwargs),
740 kwrest);
741 }
742 case NODE_LINE:
743 return rb_ary_new_from_args(1, rb_node_line_lineno_val(node));
744 case NODE_FILE:
745 return rb_ary_new_from_args(1, rb_node_file_path_val(node));
746 case NODE_ENCODING:
747 return rb_ary_new_from_args(1, rb_node_encoding_val(node));
748 case NODE_ERROR:
749 return rb_ary_new_from_node_args(ast_value, 0);
750 case NODE_ARGS_AUX:
751 case NODE_LAST:
752 break;
753 }
754
755 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
756}
757
758static VALUE
759ast_node_children(rb_execution_context_t *ec, VALUE self)
760{
761 struct ASTNodeData *data;
762 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
763
764 return node_children(data->ast_value, data->node);
765}
766
767static int
768null_loc_p(rb_code_location_t *loc)
769{
770 return (loc->beg_pos.lineno == 0 && loc->beg_pos.column == -1 && loc->end_pos.lineno == 0 && loc->end_pos.column == -1);
771}
772
773static VALUE
774location_new(rb_code_location_t *loc)
775{
776 VALUE obj;
777 struct ASTLocationData *data;
778
779 if (null_loc_p(loc)) return Qnil;
780
781 obj = TypedData_Make_Struct(rb_cLocation, struct ASTLocationData, &rb_location_type, data);
782 data->first_lineno = loc->beg_pos.lineno;
783 data->first_column = loc->beg_pos.column;
784 data->last_lineno = loc->end_pos.lineno;
785 data->last_column = loc->end_pos.column;
786
787 return obj;
788}
789
790static VALUE
791node_locations(VALUE ast_value, const NODE *node)
792{
793 enum node_type type = nd_type(node);
794 switch (type) {
795 case NODE_ALIAS:
796 return rb_ary_new_from_args(2,
797 location_new(nd_code_loc(node)),
798 location_new(&RNODE_ALIAS(node)->keyword_loc));
799 case NODE_AND:
800 return rb_ary_new_from_args(2,
801 location_new(nd_code_loc(node)),
802 location_new(&RNODE_AND(node)->operator_loc));
803 case NODE_BLOCK_PASS:
804 return rb_ary_new_from_args(2,
805 location_new(nd_code_loc(node)),
806 location_new(&RNODE_BLOCK_PASS(node)->operator_loc));
807 case NODE_BREAK:
808 return rb_ary_new_from_args(2,
809 location_new(nd_code_loc(node)),
810 location_new(&RNODE_BREAK(node)->keyword_loc));
811 case NODE_CASE:
812 return rb_ary_new_from_args(3,
813 location_new(nd_code_loc(node)),
814 location_new(&RNODE_CASE(node)->case_keyword_loc),
815 location_new(&RNODE_CASE(node)->end_keyword_loc));
816 case NODE_CASE2:
817 return rb_ary_new_from_args(3,
818 location_new(nd_code_loc(node)),
819 location_new(&RNODE_CASE2(node)->case_keyword_loc),
820 location_new(&RNODE_CASE2(node)->end_keyword_loc));
821 case NODE_CASE3:
822 return rb_ary_new_from_args(3,
823 location_new(nd_code_loc(node)),
824 location_new(&RNODE_CASE3(node)->case_keyword_loc),
825 location_new(&RNODE_CASE3(node)->end_keyword_loc));
826 case NODE_CLASS:
827 return rb_ary_new_from_args(4,
828 location_new(nd_code_loc(node)),
829 location_new(&RNODE_CLASS(node)->class_keyword_loc),
830 location_new(&RNODE_CLASS(node)->inheritance_operator_loc),
831 location_new(&RNODE_CLASS(node)->end_keyword_loc));
832 case NODE_COLON2:
833 return rb_ary_new_from_args(3,
834 location_new(nd_code_loc(node)),
835 location_new(&RNODE_COLON2(node)->delimiter_loc),
836 location_new(&RNODE_COLON2(node)->name_loc));
837 case NODE_COLON3:
838 return rb_ary_new_from_args(3,
839 location_new(nd_code_loc(node)),
840 location_new(&RNODE_COLON3(node)->delimiter_loc),
841 location_new(&RNODE_COLON3(node)->name_loc));
842 case NODE_DEFINED:
843 return rb_ary_new_from_args(2,
844 location_new(nd_code_loc(node)),
845 location_new(&RNODE_DEFINED(node)->keyword_loc));
846 case NODE_DOT2:
847 return rb_ary_new_from_args(2,
848 location_new(nd_code_loc(node)),
849 location_new(&RNODE_DOT2(node)->operator_loc));
850 case NODE_DOT3:
851 return rb_ary_new_from_args(2,
852 location_new(nd_code_loc(node)),
853 location_new(&RNODE_DOT3(node)->operator_loc));
854 case NODE_EVSTR:
855 return rb_ary_new_from_args(3,
856 location_new(nd_code_loc(node)),
857 location_new(&RNODE_EVSTR(node)->opening_loc),
858 location_new(&RNODE_EVSTR(node)->closing_loc));
859 case NODE_FLIP2:
860 return rb_ary_new_from_args(2,
861 location_new(nd_code_loc(node)),
862 location_new(&RNODE_FLIP2(node)->operator_loc));
863 case NODE_FLIP3:
864 return rb_ary_new_from_args(2,
865 location_new(nd_code_loc(node)),
866 location_new(&RNODE_FLIP3(node)->operator_loc));
867 case NODE_FOR:
868 return rb_ary_new_from_args(5,
869 location_new(nd_code_loc(node)),
870 location_new(&RNODE_FOR(node)->for_keyword_loc),
871 location_new(&RNODE_FOR(node)->in_keyword_loc),
872 location_new(&RNODE_FOR(node)->do_keyword_loc),
873 location_new(&RNODE_FOR(node)->end_keyword_loc));
874 case NODE_LAMBDA:
875 return rb_ary_new_from_args(4,
876 location_new(nd_code_loc(node)),
877 location_new(&RNODE_LAMBDA(node)->operator_loc),
878 location_new(&RNODE_LAMBDA(node)->opening_loc),
879 location_new(&RNODE_LAMBDA(node)->closing_loc));
880 case NODE_IF:
881 return rb_ary_new_from_args(4,
882 location_new(nd_code_loc(node)),
883 location_new(&RNODE_IF(node)->if_keyword_loc),
884 location_new(&RNODE_IF(node)->then_keyword_loc),
885 location_new(&RNODE_IF(node)->end_keyword_loc));
886 case NODE_IN:
887 return rb_ary_new_from_args(4,
888 location_new(nd_code_loc(node)),
889 location_new(&RNODE_IN(node)->in_keyword_loc),
890 location_new(&RNODE_IN(node)->then_keyword_loc),
891 location_new(&RNODE_IN(node)->operator_loc));
892 case NODE_MODULE:
893 return rb_ary_new_from_args(3,
894 location_new(nd_code_loc(node)),
895 location_new(&RNODE_MODULE(node)->module_keyword_loc),
896 location_new(&RNODE_MODULE(node)->end_keyword_loc));
897 case NODE_NEXT:
898 return rb_ary_new_from_args(2,
899 location_new(nd_code_loc(node)),
900 location_new(&RNODE_NEXT(node)->keyword_loc));
901 case NODE_OR:
902 return rb_ary_new_from_args(2,
903 location_new(nd_code_loc(node)),
904 location_new(&RNODE_OR(node)->operator_loc));
905 case NODE_OP_ASGN1:
906 return rb_ary_new_from_args(5,
907 location_new(nd_code_loc(node)),
908 location_new(&RNODE_OP_ASGN1(node)->call_operator_loc),
909 location_new(&RNODE_OP_ASGN1(node)->opening_loc),
910 location_new(&RNODE_OP_ASGN1(node)->closing_loc),
911 location_new(&RNODE_OP_ASGN1(node)->binary_operator_loc));
912 case NODE_OP_ASGN2:
913 return rb_ary_new_from_args(4,
914 location_new(nd_code_loc(node)),
915 location_new(&RNODE_OP_ASGN2(node)->call_operator_loc),
916 location_new(&RNODE_OP_ASGN2(node)->message_loc),
917 location_new(&RNODE_OP_ASGN2(node)->binary_operator_loc));
918 case NODE_POSTEXE:
919 return rb_ary_new_from_args(4,
920 location_new(nd_code_loc(node)),
921 location_new(&RNODE_POSTEXE(node)->keyword_loc),
922 location_new(&RNODE_POSTEXE(node)->opening_loc),
923 location_new(&RNODE_POSTEXE(node)->closing_loc));
924 case NODE_REDO:
925 return rb_ary_new_from_args(2,
926 location_new(nd_code_loc(node)),
927 location_new(&RNODE_REDO(node)->keyword_loc));
928 case NODE_REGX:
929 return rb_ary_new_from_args(4,
930 location_new(nd_code_loc(node)),
931 location_new(&RNODE_REGX(node)->opening_loc),
932 location_new(&RNODE_REGX(node)->content_loc),
933 location_new(&RNODE_REGX(node)->closing_loc));
934 case NODE_RETURN:
935 return rb_ary_new_from_args(2,
936 location_new(nd_code_loc(node)),
937 location_new(&RNODE_RETURN(node)->keyword_loc));
938
939 case NODE_SCLASS:
940 return rb_ary_new_from_args(4,
941 location_new(nd_code_loc(node)),
942 location_new(&RNODE_SCLASS(node)->class_keyword_loc),
943 location_new(&RNODE_SCLASS(node)->operator_loc),
944 location_new(&RNODE_SCLASS(node)->end_keyword_loc));
945
946 case NODE_SPLAT:
947 return rb_ary_new_from_args(2,
948 location_new(nd_code_loc(node)),
949 location_new(&RNODE_SPLAT(node)->operator_loc));
950 case NODE_SUPER:
951 return rb_ary_new_from_args(4,
952 location_new(nd_code_loc(node)),
953 location_new(&RNODE_SUPER(node)->keyword_loc),
954 location_new(&RNODE_SUPER(node)->lparen_loc),
955 location_new(&RNODE_SUPER(node)->rparen_loc));
956 case NODE_UNDEF:
957 return rb_ary_new_from_args(2,
958 location_new(nd_code_loc(node)),
959 location_new(&RNODE_UNDEF(node)->keyword_loc));
960 case NODE_UNLESS:
961 return rb_ary_new_from_args(4,
962 location_new(nd_code_loc(node)),
963 location_new(&RNODE_UNLESS(node)->keyword_loc),
964 location_new(&RNODE_UNLESS(node)->then_keyword_loc),
965 location_new(&RNODE_UNLESS(node)->end_keyword_loc));
966 case NODE_VALIAS:
967 return rb_ary_new_from_args(2,
968 location_new(nd_code_loc(node)),
969 location_new(&RNODE_VALIAS(node)->keyword_loc));
970 case NODE_WHEN:
971 return rb_ary_new_from_args(3,
972 location_new(nd_code_loc(node)),
973 location_new(&RNODE_WHEN(node)->keyword_loc),
974 location_new(&RNODE_WHEN(node)->then_keyword_loc));
975 case NODE_WHILE:
976 return rb_ary_new_from_args(3,
977 location_new(nd_code_loc(node)),
978 location_new(&RNODE_WHILE(node)->keyword_loc),
979 location_new(&RNODE_WHILE(node)->closing_loc));
980 case NODE_UNTIL:
981 return rb_ary_new_from_args(3,
982 location_new(nd_code_loc(node)),
983 location_new(&RNODE_UNTIL(node)->keyword_loc),
984 location_new(&RNODE_UNTIL(node)->closing_loc));
985 case NODE_YIELD:
986 return rb_ary_new_from_args(4,
987 location_new(nd_code_loc(node)),
988 location_new(&RNODE_YIELD(node)->keyword_loc),
989 location_new(&RNODE_YIELD(node)->lparen_loc),
990 location_new(&RNODE_YIELD(node)->rparen_loc));
991 case NODE_ARGS_AUX:
992 case NODE_LAST:
993 break;
994 default:
995 return rb_ary_new_from_args(1, location_new(nd_code_loc(node)));
996 }
997
998 rb_bug("node_locations: unknown node: %s", ruby_node_name(type));
999}
1000
1001static VALUE
1002ast_node_locations(rb_execution_context_t *ec, VALUE self)
1003{
1004 struct ASTNodeData *data;
1005 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1006
1007 return node_locations(data->ast_value, data->node);
1008}
1009
1010static VALUE
1011ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
1012{
1013 struct ASTNodeData *data;
1014 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1015
1016 return INT2NUM(nd_first_lineno(data->node));
1017}
1018
1019static VALUE
1020ast_node_first_column(rb_execution_context_t *ec, VALUE self)
1021{
1022 struct ASTNodeData *data;
1023 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1024
1025 return INT2NUM(nd_first_column(data->node));
1026}
1027
1028static VALUE
1029ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
1030{
1031 struct ASTNodeData *data;
1032 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1033
1034 return INT2NUM(nd_last_lineno(data->node));
1035}
1036
1037static VALUE
1038ast_node_last_column(rb_execution_context_t *ec, VALUE self)
1039{
1040 struct ASTNodeData *data;
1041 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1042
1043 return INT2NUM(nd_last_column(data->node));
1044}
1045
1046static VALUE
1047ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
1048{
1049 long i;
1050 struct ASTNodeData *data;
1051 rb_ast_t *ast;
1052 rb_parser_ary_t *parser_tokens;
1053 rb_parser_ast_token_t *parser_token;
1054 VALUE str, loc, token, all_tokens;
1055
1056 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1057 ast = rb_ruby_ast_data_get(data->ast_value);
1058
1059 parser_tokens = ast->node_buffer->tokens;
1060 if (parser_tokens == NULL) {
1061 return Qnil;
1062 }
1063
1064 all_tokens = rb_ary_new2(parser_tokens->len);
1065 for (i = 0; i < parser_tokens->len; i++) {
1066 parser_token = parser_tokens->data[i];
1067 str = rb_str_new(parser_token->str->ptr, parser_token->str->len);
1068 loc = rb_ary_new_from_args(4,
1069 INT2FIX(parser_token->loc.beg_pos.lineno),
1070 INT2FIX(parser_token->loc.beg_pos.column),
1071 INT2FIX(parser_token->loc.end_pos.lineno),
1072 INT2FIX(parser_token->loc.end_pos.column)
1073 );
1074 token = rb_ary_new_from_args(4, INT2FIX(parser_token->id), ID2SYM(rb_intern(parser_token->type_name)), str, loc);
1075 rb_ary_push(all_tokens, token);
1076 }
1077 rb_ary_freeze(all_tokens);
1078
1079 return all_tokens;
1080}
1081
1082static VALUE
1083ast_node_inspect(rb_execution_context_t *ec, VALUE self)
1084{
1085 VALUE str;
1086 VALUE cname;
1087 struct ASTNodeData *data;
1088 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1089
1090 cname = rb_class_path(rb_obj_class(self));
1091 str = rb_str_new2("#<");
1092
1093 rb_str_append(str, cname);
1094 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
1095 node_type_to_str(data->node),
1096 nd_first_lineno(data->node), nd_first_column(data->node),
1097 nd_last_lineno(data->node), nd_last_column(data->node));
1098
1099 return str;
1100}
1101
1102static VALUE
1103ast_node_script_lines(rb_execution_context_t *ec, VALUE self)
1104{
1105 struct ASTNodeData *data;
1106 rb_ast_t *ast;
1107 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1108 ast = rb_ruby_ast_data_get(data->ast_value);
1109 rb_parser_ary_t *ret = ast->body.script_lines;
1110 return rb_parser_build_script_lines_from(ret);
1111}
1112
1113static VALUE
1114ast_location_first_lineno(rb_execution_context_t *ec, VALUE self)
1115{
1116 struct ASTLocationData *data;
1117 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1118
1119 return INT2NUM(data->first_lineno);
1120}
1121
1122static VALUE
1123ast_location_first_column(rb_execution_context_t *ec, VALUE self)
1124{
1125 struct ASTLocationData *data;
1126 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1127
1128 return INT2NUM(data->first_column);
1129}
1130
1131static VALUE
1132ast_location_last_lineno(rb_execution_context_t *ec, VALUE self)
1133{
1134 struct ASTLocationData *data;
1135 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1136
1137 return INT2NUM(data->last_lineno);
1138}
1139
1140static VALUE
1141ast_location_last_column(rb_execution_context_t *ec, VALUE self)
1142{
1143 struct ASTLocationData *data;
1144 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1145
1146 return INT2NUM(data->last_column);
1147}
1148
1149static VALUE
1150ast_location_inspect(rb_execution_context_t *ec, VALUE self)
1151{
1152 VALUE str;
1153 VALUE cname;
1154 struct ASTLocationData *data;
1155 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1156
1157 cname = rb_class_path(rb_obj_class(self));
1158 str = rb_str_new2("#<");
1159
1160 rb_str_append(str, cname);
1161 rb_str_catf(str, ":@%d:%d-%d:%d>",
1162 data->first_lineno, data->first_column,
1163 data->last_lineno, data->last_column);
1164
1165 return str;
1166}
1167
1168#include "ast.rbinc"
1169
1170void
1171Init_ast(void)
1172{
1173 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
1174 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
1175 rb_cLocation = rb_define_class_under(rb_mAST, "Location", rb_cObject);
1176 rb_undef_alloc_func(rb_cNode);
1177 rb_undef_alloc_func(rb_cLocation);
1178}
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1528
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1633
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:661
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1427
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1425
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:235
Encoding relates APIs.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1120
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
Definition io.c:7298
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition io.c:5780
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:122
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3843
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1693
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1731
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
VALUE type(ANYARGS)
ANYARGS-ed function type.
Functions related to nodes in the AST.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:81
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:122
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:769
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:578
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:229
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40