Ruby: ast.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

ast.c
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
19 struct ASTNodeData {
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,
48 RUBY_TYPED_FREE_IMMEDIATELY,
49};
50
51 struct ASTLocationData {
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,
73 RUBY_TYPED_FREE_IMMEDIATELY,
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 VALUE
408node_children(VALUE ast_value, const NODE *node)
409{
410 char name[sizeof("$") + DECIMAL_SIZE_OF(long)];
411
412 enum node_type type = nd_type(node);
413 switch (type) {
414 case NODE_BLOCK:
415 return dump_block(ast_value, RNODE_BLOCK(node));
416 case NODE_IF:
417 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);
418 case NODE_UNLESS:
419 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);
420 case NODE_CASE:
421 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE(node)->nd_head, RNODE_CASE(node)->nd_body);
422 case NODE_CASE2:
423 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE2(node)->nd_head, RNODE_CASE2(node)->nd_body);
424 case NODE_CASE3:
425 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE3(node)->nd_head, RNODE_CASE3(node)->nd_body);
426 case NODE_WHEN:
427 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);
428 case NODE_IN:
429 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);
430 case NODE_WHILE:
431 case NODE_UNTIL:
432 return rb_ary_push(rb_ary_new_from_node_args(ast_value, 2, RNODE_WHILE(node)->nd_cond, RNODE_WHILE(node)->nd_body),
433 RBOOL(RNODE_WHILE(node)->nd_state));
434 case NODE_ITER:
435 case NODE_FOR:
436 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ITER(node)->nd_iter, RNODE_ITER(node)->nd_body);
437 case NODE_FOR_MASGN:
438 return rb_ary_new_from_node_args(ast_value, 1, RNODE_FOR_MASGN(node)->nd_var);
439 case NODE_BREAK:
440 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BREAK(node)->nd_stts);
441 case NODE_NEXT:
442 return rb_ary_new_from_node_args(ast_value, 1, RNODE_NEXT(node)->nd_stts);
443 case NODE_RETURN:
444 return rb_ary_new_from_node_args(ast_value, 1, RNODE_RETURN(node)->nd_stts);
445 case NODE_REDO:
446 return rb_ary_new_from_node_args(ast_value, 0);
447 case NODE_RETRY:
448 return rb_ary_new_from_node_args(ast_value, 0);
449 case NODE_BEGIN:
450 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BEGIN(node)->nd_body);
451 case NODE_RESCUE:
452 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);
453 case NODE_RESBODY:
454 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);
455 case NODE_ENSURE:
456 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
457 case NODE_AND:
458 case NODE_OR:
459 {
460 VALUE ary = rb_ary_new();
461
462 while (1) {
463 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_1st));
464 if (!RNODE_AND(node)->nd_2nd || !nd_type_p(RNODE_AND(node)->nd_2nd, type))
465 break;
466 node = RNODE_AND(node)->nd_2nd;
467 }
468 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_2nd));
469 return ary;
470 }
471 case NODE_MASGN:
472 if (NODE_NAMED_REST_P(RNODE_MASGN(node)->nd_args)) {
473 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);
474 }
475 else {
476 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_value),
477 NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_head),
478 no_name_rest());
479 }
480 case NODE_LASGN:
481 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node)->nd_value)) {
482 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
483 }
484 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_LASGN(node)->nd_value));
485 case NODE_DASGN:
486 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node)->nd_value)) {
487 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
488 }
489 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_DASGN(node)->nd_value));
490 case NODE_IASGN:
491 return rb_ary_new_from_args(2, var_name(RNODE_IASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_IASGN(node)->nd_value));
492 case NODE_CVASGN:
493 return rb_ary_new_from_args(2, var_name(RNODE_CVASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CVASGN(node)->nd_value));
494 case NODE_GASGN:
495 return rb_ary_new_from_args(2, var_name(RNODE_GASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_GASGN(node)->nd_value));
496 case NODE_CDECL:
497 if (RNODE_CDECL(node)->nd_vid) {
498 return rb_ary_new_from_args(2, ID2SYM(RNODE_CDECL(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
499 }
500 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_else), ID2SYM(RNODE_COLON2(RNODE_CDECL(node)->nd_else)->nd_mid), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
501 case NODE_OP_ASGN1:
502 return rb_ary_new_from_args(4, NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_recv),
503 ID2SYM(RNODE_OP_ASGN1(node)->nd_mid),
504 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_index),
505 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_rvalue));
506 case NODE_OP_ASGN2:
507 return rb_ary_new_from_args(5, NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_recv),
508 RBOOL(RNODE_OP_ASGN2(node)->nd_aid),
509 ID2SYM(RNODE_OP_ASGN2(node)->nd_vid),
510 ID2SYM(RNODE_OP_ASGN2(node)->nd_mid),
511 NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_value));
512 case NODE_OP_ASGN_AND:
513 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_head), ID2SYM(idANDOP),
514 NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_value));
515 case NODE_OP_ASGN_OR:
516 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_head), ID2SYM(idOROP),
517 NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_value));
518 case NODE_OP_CDECL:
519 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_head),
520 ID2SYM(RNODE_OP_CDECL(node)->nd_aid),
521 NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_value));
522 case NODE_CALL:
523 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CALL(node)->nd_recv),
524 ID2SYM(RNODE_CALL(node)->nd_mid),
525 NEW_CHILD(ast_value, RNODE_CALL(node)->nd_args));
526 case NODE_OPCALL:
527 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_recv),
528 ID2SYM(RNODE_OPCALL(node)->nd_mid),
529 NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_args));
530 case NODE_QCALL:
531 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_recv),
532 ID2SYM(RNODE_QCALL(node)->nd_mid),
533 NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_args));
534 case NODE_FCALL:
535 return rb_ary_new_from_args(2, ID2SYM(RNODE_FCALL(node)->nd_mid),
536 NEW_CHILD(ast_value, RNODE_FCALL(node)->nd_args));
537 case NODE_VCALL:
538 return rb_ary_new_from_args(1, ID2SYM(RNODE_VCALL(node)->nd_mid));
539 case NODE_SUPER:
540 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SUPER(node)->nd_args);
541 case NODE_ZSUPER:
542 return rb_ary_new_from_node_args(ast_value, 0);
543 case NODE_LIST:
544 return dump_array(ast_value, RNODE_LIST(node));
545 case NODE_ZLIST:
546 return rb_ary_new_from_node_args(ast_value, 0);
547 case NODE_HASH:
548 return rb_ary_new_from_node_args(ast_value, 1, RNODE_HASH(node)->nd_head);
549 case NODE_YIELD:
550 return rb_ary_new_from_node_args(ast_value, 1, RNODE_YIELD(node)->nd_head);
551 case NODE_LVAR:
552 return rb_ary_new_from_args(1, var_name(RNODE_LVAR(node)->nd_vid));
553 case NODE_DVAR:
554 return rb_ary_new_from_args(1, var_name(RNODE_DVAR(node)->nd_vid));
555 case NODE_IVAR:
556 return rb_ary_new_from_args(1, ID2SYM(RNODE_IVAR(node)->nd_vid));
557 case NODE_CONST:
558 return rb_ary_new_from_args(1, ID2SYM(RNODE_CONST(node)->nd_vid));
559 case NODE_CVAR:
560 return rb_ary_new_from_args(1, ID2SYM(RNODE_CVAR(node)->nd_vid));
561 case NODE_GVAR:
562 return rb_ary_new_from_args(1, ID2SYM(RNODE_GVAR(node)->nd_vid));
563 case NODE_NTH_REF:
564 snprintf(name, sizeof(name), "$%ld", RNODE_NTH_REF(node)->nd_nth);
565 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
566 case NODE_BACK_REF:
567 name[0] = '$';
568 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
569 name[2] = '\0';
570 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
571 case NODE_MATCH:
572 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
573 case NODE_MATCH2:
574 if (RNODE_MATCH2(node)->nd_args) {
575 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);
576 }
577 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value);
578 case NODE_MATCH3:
579 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
580 case NODE_STR:
581 case NODE_XSTR:
582 return rb_ary_new_from_args(1, rb_node_str_string_val(node));
583 case NODE_INTEGER:
584 return rb_ary_new_from_args(1, rb_node_integer_literal_val(node));
585 case NODE_FLOAT:
586 return rb_ary_new_from_args(1, rb_node_float_literal_val(node));
587 case NODE_RATIONAL:
588 return rb_ary_new_from_args(1, rb_node_rational_literal_val(node));
589 case NODE_IMAGINARY:
590 return rb_ary_new_from_args(1, rb_node_imaginary_literal_val(node));
591 case NODE_REGX:
592 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
593 case NODE_ONCE:
594 return rb_ary_new_from_node_args(ast_value, 1, RNODE_ONCE(node)->nd_body);
595 case NODE_DSTR:
596 case NODE_DXSTR:
597 case NODE_DREGX:
598 case NODE_DSYM:
599 {
600 struct RNode_LIST *n = RNODE_DSTR(node)->nd_next;
601 VALUE head = Qnil, next = Qnil;
602 if (n) {
603 head = NEW_CHILD(ast_value, n->nd_head);
604 next = NEW_CHILD(ast_value, n->nd_next);
605 }
606 return rb_ary_new_from_args(3, rb_node_dstr_string_val(node), head, next);
607 }
608 case NODE_SYM:
609 return rb_ary_new_from_args(1, rb_node_sym_string_val(node));
610 case NODE_EVSTR:
611 return rb_ary_new_from_node_args(ast_value, 1, RNODE_EVSTR(node)->nd_body);
612 case NODE_ARGSCAT:
613 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSCAT(node)->nd_head, RNODE_ARGSCAT(node)->nd_body);
614 case NODE_ARGSPUSH:
615 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSPUSH(node)->nd_head, RNODE_ARGSPUSH(node)->nd_body);
616 case NODE_SPLAT:
617 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SPLAT(node)->nd_head);
618 case NODE_BLOCK_PASS:
619 return rb_ary_new_from_node_args(ast_value, 2, RNODE_BLOCK_PASS(node)->nd_head, RNODE_BLOCK_PASS(node)->nd_body);
620 case NODE_DEFN:
621 return rb_ary_new_from_args(2, ID2SYM(RNODE_DEFN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFN(node)->nd_defn));
622 case NODE_DEFS:
623 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));
624 case NODE_ALIAS:
625 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ALIAS(node)->nd_1st, RNODE_ALIAS(node)->nd_2nd);
626 case NODE_VALIAS:
627 return rb_ary_new_from_args(2, ID2SYM(RNODE_VALIAS(node)->nd_alias), ID2SYM(RNODE_VALIAS(node)->nd_orig));
628 case NODE_UNDEF:
629 return rb_ary_new_from_args(1, dump_parser_array(ast_value, RNODE_UNDEF(node)->nd_undefs));
630 case NODE_CLASS:
631 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);
632 case NODE_MODULE:
633 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MODULE(node)->nd_cpath, RNODE_MODULE(node)->nd_body);
634 case NODE_SCLASS:
635 return rb_ary_new_from_node_args(ast_value, 2, RNODE_SCLASS(node)->nd_recv, RNODE_SCLASS(node)->nd_body);
636 case NODE_COLON2:
637 return rb_ary_new_from_args(2, NEW_CHILD(ast_value, RNODE_COLON2(node)->nd_head), ID2SYM(RNODE_COLON2(node)->nd_mid));
638 case NODE_COLON3:
639 return rb_ary_new_from_args(1, ID2SYM(RNODE_COLON3(node)->nd_mid));
640 case NODE_DOT2:
641 case NODE_DOT3:
642 case NODE_FLIP2:
643 case NODE_FLIP3:
644 return rb_ary_new_from_node_args(ast_value, 2, RNODE_DOT2(node)->nd_beg, RNODE_DOT2(node)->nd_end);
645 case NODE_SELF:
646 return rb_ary_new_from_node_args(ast_value, 0);
647 case NODE_NIL:
648 return rb_ary_new_from_node_args(ast_value, 0);
649 case NODE_TRUE:
650 return rb_ary_new_from_node_args(ast_value, 0);
651 case NODE_FALSE:
652 return rb_ary_new_from_node_args(ast_value, 0);
653 case NODE_ERRINFO:
654 return rb_ary_new_from_node_args(ast_value, 0);
655 case NODE_DEFINED:
656 return rb_ary_new_from_node_args(ast_value, 1, RNODE_DEFINED(node)->nd_head);
657 case NODE_POSTEXE:
658 return rb_ary_new_from_node_args(ast_value, 1, RNODE_POSTEXE(node)->nd_body);
659 case NODE_ATTRASGN:
660 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));
661 case NODE_LAMBDA:
662 return rb_ary_new_from_node_args(ast_value, 1, RNODE_LAMBDA(node)->nd_body);
663 case NODE_OPT_ARG:
664 return rb_ary_new_from_node_args(ast_value, 2, RNODE_OPT_ARG(node)->nd_body, RNODE_OPT_ARG(node)->nd_next);
665 case NODE_KW_ARG:
666 return rb_ary_new_from_node_args(ast_value, 2, RNODE_KW_ARG(node)->nd_body, RNODE_KW_ARG(node)->nd_next);
667 case NODE_POSTARG:
668 if (NODE_NAMED_REST_P(RNODE_POSTARG(node)->nd_1st)) {
669 return rb_ary_new_from_node_args(ast_value, 2, RNODE_POSTARG(node)->nd_1st, RNODE_POSTARG(node)->nd_2nd);
670 }
671 return rb_ary_new_from_args(2, no_name_rest(),
672 NEW_CHILD(ast_value, RNODE_POSTARG(node)->nd_2nd));
673 case NODE_ARGS:
674 {
675 struct rb_args_info *ainfo = &RNODE_ARGS(node)->nd_ainfo;
676 return rb_ary_new_from_args(10,
677 INT2NUM(ainfo->pre_args_num),
678 NEW_CHILD(ast_value, ainfo->pre_init),
679 NEW_CHILD(ast_value, (NODE *)ainfo->opt_args),
680 var_name(ainfo->first_post_arg),
681 INT2NUM(ainfo->post_args_num),
682 NEW_CHILD(ast_value, ainfo->post_init),
683 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
684 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
685 : var_name(ainfo->rest_arg)),
686 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, (NODE *)ainfo->kw_args)),
687 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, ainfo->kw_rest_arg)),
688 var_name(ainfo->block_arg));
689 }
690 case NODE_SCOPE:
691 {
692 rb_ast_id_table_t *tbl = RNODE_SCOPE(node)->nd_tbl;
693 int i, size = tbl ? tbl->size : 0;
694 VALUE locals = rb_ary_new_capa(size);
695 for (i = 0; i < size; i++) {
696 rb_ary_push(locals, var_name(tbl->ids[i]));
697 }
698 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));
699 }
700 case NODE_ARYPTN:
701 {
702 VALUE rest = rest_arg(ast_value, RNODE_ARYPTN(node)->rest_arg);
703 return rb_ary_new_from_args(4,
704 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->nd_pconst),
705 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->pre_args),
706 rest,
707 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->post_args));
708 }
709 case NODE_FNDPTN:
710 {
711 VALUE pre_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->pre_rest_arg);
712 VALUE post_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->post_rest_arg);
713 return rb_ary_new_from_args(4,
714 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->nd_pconst),
715 pre_rest,
716 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->args),
717 post_rest);
718 }
719 case NODE_HSHPTN:
720 {
721 VALUE kwrest = RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
722 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwrestarg);
723
724 return rb_ary_new_from_args(3,
725 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pconst),
726 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwargs),
727 kwrest);
728 }
729 case NODE_LINE:
730 return rb_ary_new_from_args(1, rb_node_line_lineno_val(node));
731 case NODE_FILE:
732 return rb_ary_new_from_args(1, rb_node_file_path_val(node));
733 case NODE_ENCODING:
734 return rb_ary_new_from_args(1, rb_node_encoding_val(node));
735 case NODE_ERROR:
736 return rb_ary_new_from_node_args(ast_value, 0);
737 case NODE_ARGS_AUX:
738 case NODE_LAST:
739 break;
740 }
741
742 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
743}
744
745static VALUE
746ast_node_children(rb_execution_context_t *ec, VALUE self)
747{
748 struct ASTNodeData *data;
749 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
750
751 return node_children(data->ast_value, data->node);
752}
753
754static int
755null_loc_p(rb_code_location_t *loc)
756{
757 return (loc->beg_pos.lineno == 0 && loc->beg_pos.column == -1 && loc->end_pos.lineno == 0 && loc->end_pos.column == -1);
758}
759
760static VALUE
761location_new(rb_code_location_t *loc)
762{
763 VALUE obj;
764 struct ASTLocationData *data;
765
766 if (null_loc_p(loc)) return Qnil;
767
768 obj = TypedData_Make_Struct(rb_cLocation, struct ASTLocationData, &rb_location_type, data);
769 data->first_lineno = loc->beg_pos.lineno;
770 data->first_column = loc->beg_pos.column;
771 data->last_lineno = loc->end_pos.lineno;
772 data->last_column = loc->end_pos.column;
773
774 return obj;
775}
776
777static VALUE
778node_locations(VALUE ast_value, const NODE *node)
779{
780 enum node_type type = nd_type(node);
781 switch (type) {
782 case NODE_ALIAS:
783 return rb_ary_new_from_args(2,
784 location_new(nd_code_loc(node)),
785 location_new(&RNODE_ALIAS(node)->keyword_loc));
786 case NODE_AND:
787 return rb_ary_new_from_args(2,
788 location_new(nd_code_loc(node)),
789 location_new(&RNODE_AND(node)->operator_loc));
790 case NODE_BLOCK_PASS:
791 return rb_ary_new_from_args(2,
792 location_new(nd_code_loc(node)),
793 location_new(&RNODE_BLOCK_PASS(node)->operator_loc));
794 case NODE_BREAK:
795 return rb_ary_new_from_args(2,
796 location_new(nd_code_loc(node)),
797 location_new(&RNODE_BREAK(node)->keyword_loc));
798 case NODE_CASE:
799 return rb_ary_new_from_args(3,
800 location_new(nd_code_loc(node)),
801 location_new(&RNODE_CASE(node)->case_keyword_loc),
802 location_new(&RNODE_CASE(node)->end_keyword_loc));
803 case NODE_CASE2:
804 return rb_ary_new_from_args(3,
805 location_new(nd_code_loc(node)),
806 location_new(&RNODE_CASE2(node)->case_keyword_loc),
807 location_new(&RNODE_CASE2(node)->end_keyword_loc));
808 case NODE_CASE3:
809 return rb_ary_new_from_args(3,
810 location_new(nd_code_loc(node)),
811 location_new(&RNODE_CASE3(node)->case_keyword_loc),
812 location_new(&RNODE_CASE3(node)->end_keyword_loc));
813 case NODE_CLASS:
814 return rb_ary_new_from_args(4,
815 location_new(nd_code_loc(node)),
816 location_new(&RNODE_CLASS(node)->class_keyword_loc),
817 location_new(&RNODE_CLASS(node)->inheritance_operator_loc),
818 location_new(&RNODE_CLASS(node)->end_keyword_loc));
819 case NODE_COLON2:
820 return rb_ary_new_from_args(3,
821 location_new(nd_code_loc(node)),
822 location_new(&RNODE_COLON2(node)->delimiter_loc),
823 location_new(&RNODE_COLON2(node)->name_loc));
824 case NODE_COLON3:
825 return rb_ary_new_from_args(3,
826 location_new(nd_code_loc(node)),
827 location_new(&RNODE_COLON3(node)->delimiter_loc),
828 location_new(&RNODE_COLON3(node)->name_loc));
829 case NODE_DEFINED:
830 return rb_ary_new_from_args(2,
831 location_new(nd_code_loc(node)),
832 location_new(&RNODE_DEFINED(node)->keyword_loc));
833 case NODE_DOT2:
834 return rb_ary_new_from_args(2,
835 location_new(nd_code_loc(node)),
836 location_new(&RNODE_DOT2(node)->operator_loc));
837 case NODE_DOT3:
838 return rb_ary_new_from_args(2,
839 location_new(nd_code_loc(node)),
840 location_new(&RNODE_DOT3(node)->operator_loc));
841 case NODE_EVSTR:
842 return rb_ary_new_from_args(3,
843 location_new(nd_code_loc(node)),
844 location_new(&RNODE_EVSTR(node)->opening_loc),
845 location_new(&RNODE_EVSTR(node)->closing_loc));
846 case NODE_FLIP2:
847 return rb_ary_new_from_args(2,
848 location_new(nd_code_loc(node)),
849 location_new(&RNODE_FLIP2(node)->operator_loc));
850 case NODE_FLIP3:
851 return rb_ary_new_from_args(2,
852 location_new(nd_code_loc(node)),
853 location_new(&RNODE_FLIP3(node)->operator_loc));
854 case NODE_FOR:
855 return rb_ary_new_from_args(5,
856 location_new(nd_code_loc(node)),
857 location_new(&RNODE_FOR(node)->for_keyword_loc),
858 location_new(&RNODE_FOR(node)->in_keyword_loc),
859 location_new(&RNODE_FOR(node)->do_keyword_loc),
860 location_new(&RNODE_FOR(node)->end_keyword_loc));
861 case NODE_LAMBDA:
862 return rb_ary_new_from_args(4,
863 location_new(nd_code_loc(node)),
864 location_new(&RNODE_LAMBDA(node)->operator_loc),
865 location_new(&RNODE_LAMBDA(node)->opening_loc),
866 location_new(&RNODE_LAMBDA(node)->closing_loc));
867 case NODE_IF:
868 return rb_ary_new_from_args(4,
869 location_new(nd_code_loc(node)),
870 location_new(&RNODE_IF(node)->if_keyword_loc),
871 location_new(&RNODE_IF(node)->then_keyword_loc),
872 location_new(&RNODE_IF(node)->end_keyword_loc));
873 case NODE_IN:
874 return rb_ary_new_from_args(4,
875 location_new(nd_code_loc(node)),
876 location_new(&RNODE_IN(node)->in_keyword_loc),
877 location_new(&RNODE_IN(node)->then_keyword_loc),
878 location_new(&RNODE_IN(node)->operator_loc));
879 case NODE_MODULE:
880 return rb_ary_new_from_args(3,
881 location_new(nd_code_loc(node)),
882 location_new(&RNODE_MODULE(node)->module_keyword_loc),
883 location_new(&RNODE_MODULE(node)->end_keyword_loc));
884 case NODE_NEXT:
885 return rb_ary_new_from_args(2,
886 location_new(nd_code_loc(node)),
887 location_new(&RNODE_NEXT(node)->keyword_loc));
888 case NODE_OR:
889 return rb_ary_new_from_args(2,
890 location_new(nd_code_loc(node)),
891 location_new(&RNODE_OR(node)->operator_loc));
892 case NODE_OP_ASGN1:
893 return rb_ary_new_from_args(5,
894 location_new(nd_code_loc(node)),
895 location_new(&RNODE_OP_ASGN1(node)->call_operator_loc),
896 location_new(&RNODE_OP_ASGN1(node)->opening_loc),
897 location_new(&RNODE_OP_ASGN1(node)->closing_loc),
898 location_new(&RNODE_OP_ASGN1(node)->binary_operator_loc));
899 case NODE_OP_ASGN2:
900 return rb_ary_new_from_args(4,
901 location_new(nd_code_loc(node)),
902 location_new(&RNODE_OP_ASGN2(node)->call_operator_loc),
903 location_new(&RNODE_OP_ASGN2(node)->message_loc),
904 location_new(&RNODE_OP_ASGN2(node)->binary_operator_loc));
905 case NODE_POSTEXE:
906 return rb_ary_new_from_args(4,
907 location_new(nd_code_loc(node)),
908 location_new(&RNODE_POSTEXE(node)->keyword_loc),
909 location_new(&RNODE_POSTEXE(node)->opening_loc),
910 location_new(&RNODE_POSTEXE(node)->closing_loc));
911 case NODE_REDO:
912 return rb_ary_new_from_args(2,
913 location_new(nd_code_loc(node)),
914 location_new(&RNODE_REDO(node)->keyword_loc));
915 case NODE_REGX:
916 return rb_ary_new_from_args(4,
917 location_new(nd_code_loc(node)),
918 location_new(&RNODE_REGX(node)->opening_loc),
919 location_new(&RNODE_REGX(node)->content_loc),
920 location_new(&RNODE_REGX(node)->closing_loc));
921 case NODE_RETURN:
922 return rb_ary_new_from_args(2,
923 location_new(nd_code_loc(node)),
924 location_new(&RNODE_RETURN(node)->keyword_loc));
925
926 case NODE_SCLASS:
927 return rb_ary_new_from_args(4,
928 location_new(nd_code_loc(node)),
929 location_new(&RNODE_SCLASS(node)->class_keyword_loc),
930 location_new(&RNODE_SCLASS(node)->operator_loc),
931 location_new(&RNODE_SCLASS(node)->end_keyword_loc));
932
933 case NODE_SPLAT:
934 return rb_ary_new_from_args(2,
935 location_new(nd_code_loc(node)),
936 location_new(&RNODE_SPLAT(node)->operator_loc));
937 case NODE_SUPER:
938 return rb_ary_new_from_args(4,
939 location_new(nd_code_loc(node)),
940 location_new(&RNODE_SUPER(node)->keyword_loc),
941 location_new(&RNODE_SUPER(node)->lparen_loc),
942 location_new(&RNODE_SUPER(node)->rparen_loc));
943 case NODE_UNDEF:
944 return rb_ary_new_from_args(2,
945 location_new(nd_code_loc(node)),
946 location_new(&RNODE_UNDEF(node)->keyword_loc));
947 case NODE_UNLESS:
948 return rb_ary_new_from_args(4,
949 location_new(nd_code_loc(node)),
950 location_new(&RNODE_UNLESS(node)->keyword_loc),
951 location_new(&RNODE_UNLESS(node)->then_keyword_loc),
952 location_new(&RNODE_UNLESS(node)->end_keyword_loc));
953 case NODE_VALIAS:
954 return rb_ary_new_from_args(2,
955 location_new(nd_code_loc(node)),
956 location_new(&RNODE_VALIAS(node)->keyword_loc));
957 case NODE_WHEN:
958 return rb_ary_new_from_args(3,
959 location_new(nd_code_loc(node)),
960 location_new(&RNODE_WHEN(node)->keyword_loc),
961 location_new(&RNODE_WHEN(node)->then_keyword_loc));
962 case NODE_WHILE:
963 return rb_ary_new_from_args(3,
964 location_new(nd_code_loc(node)),
965 location_new(&RNODE_WHILE(node)->keyword_loc),
966 location_new(&RNODE_WHILE(node)->closing_loc));
967 case NODE_UNTIL:
968 return rb_ary_new_from_args(3,
969 location_new(nd_code_loc(node)),
970 location_new(&RNODE_UNTIL(node)->keyword_loc),
971 location_new(&RNODE_UNTIL(node)->closing_loc));
972 case NODE_YIELD:
973 return rb_ary_new_from_args(4,
974 location_new(nd_code_loc(node)),
975 location_new(&RNODE_YIELD(node)->keyword_loc),
976 location_new(&RNODE_YIELD(node)->lparen_loc),
977 location_new(&RNODE_YIELD(node)->rparen_loc));
978 case NODE_ARGS_AUX:
979 case NODE_LAST:
980 break;
981 default:
982 return rb_ary_new_from_args(1, location_new(nd_code_loc(node)));
983 }
984
985 rb_bug("node_locations: unknown node: %s", ruby_node_name(type));
986}
987
988static VALUE
989ast_node_locations(rb_execution_context_t *ec, VALUE self)
990{
991 struct ASTNodeData *data;
992 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
993
994 return node_locations(data->ast_value, data->node);
995}
996
997static VALUE
998ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
999{
1000 struct ASTNodeData *data;
1001 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1002
1003 return INT2NUM(nd_first_lineno(data->node));
1004}
1005
1006static VALUE
1007ast_node_first_column(rb_execution_context_t *ec, VALUE self)
1008{
1009 struct ASTNodeData *data;
1010 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1011
1012 return INT2NUM(nd_first_column(data->node));
1013}
1014
1015static VALUE
1016ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
1017{
1018 struct ASTNodeData *data;
1019 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1020
1021 return INT2NUM(nd_last_lineno(data->node));
1022}
1023
1024static VALUE
1025ast_node_last_column(rb_execution_context_t *ec, VALUE self)
1026{
1027 struct ASTNodeData *data;
1028 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1029
1030 return INT2NUM(nd_last_column(data->node));
1031}
1032
1033static VALUE
1034ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
1035{
1036 long i;
1037 struct ASTNodeData *data;
1038 rb_ast_t *ast;
1039 rb_parser_ary_t *parser_tokens;
1040 rb_parser_ast_token_t *parser_token;
1041 VALUE str, loc, token, all_tokens;
1042
1043 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1044 ast = rb_ruby_ast_data_get(data->ast_value);
1045
1046 parser_tokens = ast->node_buffer->tokens;
1047 if (parser_tokens == NULL) {
1048 return Qnil;
1049 }
1050
1051 all_tokens = rb_ary_new2(parser_tokens->len);
1052 for (i = 0; i < parser_tokens->len; i++) {
1053 parser_token = parser_tokens->data[i];
1054 str = rb_str_new(parser_token->str->ptr, parser_token->str->len);
1055 loc = rb_ary_new_from_args(4,
1056 INT2FIX(parser_token->loc.beg_pos.lineno),
1057 INT2FIX(parser_token->loc.beg_pos.column),
1058 INT2FIX(parser_token->loc.end_pos.lineno),
1059 INT2FIX(parser_token->loc.end_pos.column)
1060 );
1061 token = rb_ary_new_from_args(4, INT2FIX(parser_token->id), ID2SYM(rb_intern(parser_token->type_name)), str, loc);
1062 rb_ary_push(all_tokens, token);
1063 }
1064 rb_ary_freeze(all_tokens);
1065
1066 return all_tokens;
1067}
1068
1069static VALUE
1070ast_node_inspect(rb_execution_context_t *ec, VALUE self)
1071{
1072 VALUE str;
1073 VALUE cname;
1074 struct ASTNodeData *data;
1075 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1076
1077 cname = rb_class_path(rb_obj_class(self));
1078 str = rb_str_new2("#<");
1079
1080 rb_str_append(str, cname);
1081 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
1082 node_type_to_str(data->node),
1083 nd_first_lineno(data->node), nd_first_column(data->node),
1084 nd_last_lineno(data->node), nd_last_column(data->node));
1085
1086 return str;
1087}
1088
1089static VALUE
1090ast_node_script_lines(rb_execution_context_t *ec, VALUE self)
1091{
1092 struct ASTNodeData *data;
1093 rb_ast_t *ast;
1094 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1095 ast = rb_ruby_ast_data_get(data->ast_value);
1096 rb_parser_ary_t *ret = ast->body.script_lines;
1097 return rb_parser_build_script_lines_from(ret);
1098}
1099
1100static VALUE
1101ast_location_first_lineno(rb_execution_context_t *ec, VALUE self)
1102{
1103 struct ASTLocationData *data;
1104 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1105
1106 return INT2NUM(data->first_lineno);
1107}
1108
1109static VALUE
1110ast_location_first_column(rb_execution_context_t *ec, VALUE self)
1111{
1112 struct ASTLocationData *data;
1113 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1114
1115 return INT2NUM(data->first_column);
1116}
1117
1118static VALUE
1119ast_location_last_lineno(rb_execution_context_t *ec, VALUE self)
1120{
1121 struct ASTLocationData *data;
1122 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1123
1124 return INT2NUM(data->last_lineno);
1125}
1126
1127static VALUE
1128ast_location_last_column(rb_execution_context_t *ec, VALUE self)
1129{
1130 struct ASTLocationData *data;
1131 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1132
1133 return INT2NUM(data->last_column);
1134}
1135
1136static VALUE
1137ast_location_inspect(rb_execution_context_t *ec, VALUE self)
1138{
1139 VALUE str;
1140 VALUE cname;
1141 struct ASTLocationData *data;
1142 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1143
1144 cname = rb_class_path(rb_obj_class(self));
1145 str = rb_str_new2("#<");
1146
1147 rb_str_append(str, cname);
1148 rb_str_catf(str, ":@%d:%d-%d:%d>",
1149 data->first_lineno, data->first_column,
1150 data->last_lineno, data->last_column);
1151
1152 return str;
1153}
1154
1155#include "ast.rbinc"
1156
1157void
1158Init_ast(void)
1159{
1160 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
1161 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
1162 rb_cLocation = rb_define_class_under(rb_mAST, "Location", rb_cObject);
1163 rb_undef_alloc_func(rb_cNode);
1164 rb_undef_alloc_func(rb_cLocation);
1165}
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1509
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition class.c:1614
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
INT2NUM
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:653
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1429
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:264
encoding.h
Encoding relates APIs.
rb_utf8_encoding
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
Definition encoding.c:1535
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
rb_check_array_type
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
rb_ary_new
VALUE rb_ary_new(void)
Allocates a new, empty array.
rb_ary_new_capa
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
rb_ary_freeze
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
rb_file_open_str
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:7291
rb_io_close
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition io.c:5773
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:120
rb_str_append
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:3799
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
rb_strlen_lit
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1693
rb_str_new_cstr
#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
rb_class_path
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1664
util.h
DECIMAL_SIZE_OF
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:80
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:649
rb_data_type_t
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
TypedData_Make_Struct
#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:508
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
ASTLocationData
Definition ast.c:51
ASTNodeData
Definition ast.c:19
RNode_BLOCK
Definition rubyparser.h:255
RNode_LIST
Definition rubyparser.h:578
rb_args_info
Definition rubyparser.h:768
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40