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