3#include "internal/ruby_parser.h"
4#include "internal/symbol.h"
5#include "internal/warnings.h"
17static VALUE rb_cLocation;
25node_gc_mark(
void *ptr)
28 rb_gc_mark(data->ast_value);
32node_memsize(
const void *ptr)
35 rb_ast_t *ast = rb_ruby_ast_data_get(data->ast_value);
37 return sizeof(
struct ASTNodeData) + rb_ast_memsize(ast);
44 RUBY_TYPED_FREE_IMMEDIATELY,
55location_gc_mark(
void *ptr)
60location_memsize(
const void *ptr)
69 RUBY_TYPED_FREE_IMMEDIATELY,
81 data->ast_value = ast_value;
86ast_new_internal(
VALUE ast_value,
const NODE *node)
90 obj = rb_ast_node_alloc(rb_cNode);
91 setup_node(obj, ast_value, node);
102 return rb_parser_set_context(rb_parser_new(), NULL, 0);
106ast_parse_done(
VALUE ast_value)
108 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
110 if (!ast->body.root) {
112 rb_exc_raise(GET_EC()->errinfo);
115 return ast_new_internal(ast_value, (
NODE *)ast->body.root);
119setup_vparser(
VALUE keep_script_lines,
VALUE error_tolerant,
VALUE keep_tokens)
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);
131 return rb_ast_parse_str(str, keep_script_lines, error_tolerant, keep_tokens);
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);
147 return rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
159 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
160 ast_value = rb_parser_compile_file_path(vparser,
Qnil, f, 1);
162 return ast_parse_done(ast_value);
166rb_ast_parse_array(
VALUE array,
VALUE keep_script_lines,
VALUE error_tolerant,
VALUE keep_tokens)
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);
179node_find(
VALUE self,
const int node_id)
186 if (nd_node_id(data->node) == node_id)
return self;
188 ary = node_children(data->ast_value, data->node);
194 VALUE result = node_find(child, node_id);
195 if (
RTEST(result))
return result;
202extern VALUE rb_e_script;
209 if (!rb_frame_info_p(location)) {
210 rb_raise(
rb_eTypeError,
"Thread::Backtrace::Location object expected");
213 node_id = rb_get_node_id_from_frame_info(location);
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);
236 iseq = vm_proc_iseq(body);
238 if (!rb_obj_is_iseq((
VALUE)iseq))
return Qnil;
241 iseq = rb_method_iseq(body);
244 node_id = ISEQ_BODY(iseq)->location.node_id;
252 if (ISEQ_BODY(iseq)->prism) {
256 lines = ISEQ_BODY(iseq)->variable.script_lines;
258 VALUE path = rb_iseq_path(iseq);
259 int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path),
"-e", 2) == 0;
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");
266 node = rb_ast_parse_array(lines, keep_script_lines, error_tolerant, keep_tokens);
269 node = rb_ast_parse_str(rb_e_script, keep_script_lines, error_tolerant, keep_tokens);
272 node = rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
275 return node_find(node, node_id);
279rb_ast_node_alloc(
VALUE klass)
288node_type_to_str(
const NODE *node)
290 return (ruby_node_name(nd_type(node)) +
rb_strlen_lit(
"NODE_"));
299 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
308 return INT2FIX(nd_node_id(data->node));
311#define NEW_CHILD(ast_value, node) (node ? ast_new_internal(ast_value, node) : Qnil)
314rb_ary_new_from_node_args(
VALUE ast_value,
long n, ...)
323 for (i=0; i<n; i++) {
325 node = va_arg(ar,
NODE *);
326 rb_ary_push(ary, NEW_CHILD(ast_value, node));
335 VALUE ary = rb_ary_new();
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));
342 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
351 VALUE ary = rb_ary_new();
352 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
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));
358 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
368 if (p_ary->data_type != PARSER_ARY_DATA_NODE) {
369 rb_bug(
"unexpected rb_parser_ary_data_type: %d", p_ary->data_type);
374 for (
long i = 0; i < p_ary->len; i++) {
375 rb_ary_push(ary, NEW_CHILD(ast_value, p_ary->data[i]));
384 if (!
id)
return Qnil;
385 if (!rb_id2str(
id))
return Qnil;
393 CONST_ID(rest,
"NODE_SPECIAL_NO_NAME_REST");
398rest_arg(
VALUE ast_value,
const NODE *rest_arg)
400 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast_value, rest_arg) : no_name_rest();
404node_children(
VALUE ast_value,
const NODE *node)
408 enum node_type
type = nd_type(node);
411 return dump_block(ast_value, RNODE_BLOCK(node));
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);
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);
417 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE(node)->nd_head, RNODE_CASE(node)->nd_body);
419 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE2(node)->nd_head, RNODE_CASE2(node)->nd_body);
421 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE3(node)->nd_head, RNODE_CASE3(node)->nd_body);
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);
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);
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));
432 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ITER(node)->nd_iter, RNODE_ITER(node)->nd_body);
434 return rb_ary_new_from_node_args(ast_value, 1, RNODE_FOR_MASGN(node)->nd_var);
436 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BREAK(node)->nd_stts);
438 return rb_ary_new_from_node_args(ast_value, 1, RNODE_NEXT(node)->nd_stts);
440 return rb_ary_new_from_node_args(ast_value, 1, RNODE_RETURN(node)->nd_stts);
442 return rb_ary_new_from_node_args(ast_value, 0);
444 return rb_ary_new_from_node_args(ast_value, 0);
446 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BEGIN(node)->nd_body);
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);
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);
452 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
456 VALUE ary = rb_ary_new();
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))
462 node = RNODE_AND(node)->nd_2nd;
464 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_2nd));
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);
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),
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")));
480 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_LASGN(node)->nd_value));
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")));
485 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_DASGN(node)->nd_value));
487 return rb_ary_new_from_args(2, var_name(RNODE_IASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_IASGN(node)->nd_value));
489 return rb_ary_new_from_args(2, var_name(RNODE_CVASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CVASGN(node)->nd_value));
491 return rb_ary_new_from_args(2, var_name(RNODE_GASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_GASGN(node)->nd_value));
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));
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));
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));
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));
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));
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));
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));
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));
531 return rb_ary_new_from_args(2,
ID2SYM(RNODE_FCALL(node)->nd_mid),
532 NEW_CHILD(ast_value, RNODE_FCALL(node)->nd_args));
534 return rb_ary_new_from_args(1,
ID2SYM(RNODE_VCALL(node)->nd_mid));
536 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SUPER(node)->nd_args);
538 return rb_ary_new_from_node_args(ast_value, 0);
540 return dump_array(ast_value, RNODE_LIST(node));
542 return rb_ary_new_from_node_args(ast_value, 0);
544 return rb_ary_new_from_node_args(ast_value, 1, RNODE_HASH(node)->nd_head);
546 return rb_ary_new_from_node_args(ast_value, 1, RNODE_YIELD(node)->nd_head);
548 return rb_ary_new_from_args(1, var_name(RNODE_LVAR(node)->nd_vid));
550 return rb_ary_new_from_args(1, var_name(RNODE_DVAR(node)->nd_vid));
552 return rb_ary_new_from_args(1,
ID2SYM(RNODE_IVAR(node)->nd_vid));
554 return rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
556 return rb_ary_new_from_args(1,
ID2SYM(RNODE_CVAR(node)->nd_vid));
558 return rb_ary_new_from_args(1,
ID2SYM(RNODE_GVAR(node)->nd_vid));
560 snprintf(name,
sizeof(name),
"$%ld", RNODE_NTH_REF(node)->nd_nth);
561 return rb_ary_new_from_args(1,
ID2SYM(rb_intern(name)));
564 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
566 return rb_ary_new_from_args(1,
ID2SYM(rb_intern(name)));
568 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
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);
573 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value);
575 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
578 return rb_ary_new_from_args(1, rb_node_str_string_val(node));
580 return rb_ary_new_from_args(1, rb_node_integer_literal_val(node));
582 return rb_ary_new_from_args(1, rb_node_float_literal_val(node));
584 return rb_ary_new_from_args(1, rb_node_rational_literal_val(node));
586 return rb_ary_new_from_args(1, rb_node_imaginary_literal_val(node));
588 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
590 return rb_ary_new_from_node_args(ast_value, 1, RNODE_ONCE(node)->nd_body);
596 struct RNode_LIST *n = RNODE_DSTR(node)->nd_next;
599 head = NEW_CHILD(ast_value, n->nd_head);
600 next = NEW_CHILD(ast_value, n->nd_next);
602 return rb_ary_new_from_args(3, rb_node_dstr_string_val(node), head, next);
605 return rb_ary_new_from_args(1, rb_node_sym_string_val(node));
607 return rb_ary_new_from_node_args(ast_value, 1, RNODE_EVSTR(node)->nd_body);
609 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSCAT(node)->nd_head, RNODE_ARGSCAT(node)->nd_body);
611 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSPUSH(node)->nd_head, RNODE_ARGSPUSH(node)->nd_body);
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);
617 return rb_ary_new_from_args(2,
ID2SYM(RNODE_DEFN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFN(node)->nd_defn));
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));
621 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ALIAS(node)->nd_1st, RNODE_ALIAS(node)->nd_2nd);
623 return rb_ary_new_from_args(2,
ID2SYM(RNODE_VALIAS(node)->nd_alias),
ID2SYM(RNODE_VALIAS(node)->nd_orig));
625 return rb_ary_new_from_args(1, dump_parser_array(ast_value, RNODE_UNDEF(node)->nd_undefs));
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);
629 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MODULE(node)->nd_cpath, RNODE_MODULE(node)->nd_body);
631 return rb_ary_new_from_node_args(ast_value, 2, RNODE_SCLASS(node)->nd_recv, RNODE_SCLASS(node)->nd_body);
633 return rb_ary_new_from_args(2, NEW_CHILD(ast_value, RNODE_COLON2(node)->nd_head),
ID2SYM(RNODE_COLON2(node)->nd_mid));
635 return rb_ary_new_from_args(1,
ID2SYM(RNODE_COLON3(node)->nd_mid));
640 return rb_ary_new_from_node_args(ast_value, 2, RNODE_DOT2(node)->nd_beg, RNODE_DOT2(node)->nd_end);
642 return rb_ary_new_from_node_args(ast_value, 0);
644 return rb_ary_new_from_node_args(ast_value, 0);
646 return rb_ary_new_from_node_args(ast_value, 0);
648 return rb_ary_new_from_node_args(ast_value, 0);
650 return rb_ary_new_from_node_args(ast_value, 0);
652 return rb_ary_new_from_node_args(ast_value, 1, RNODE_DEFINED(node)->nd_head);
654 return rb_ary_new_from_node_args(ast_value, 1, RNODE_POSTEXE(node)->nd_body);
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));
658 return rb_ary_new_from_node_args(ast_value, 1, RNODE_LAMBDA(node)->nd_body);
660 return rb_ary_new_from_node_args(ast_value, 2, RNODE_OPT_ARG(node)->nd_body, RNODE_OPT_ARG(node)->nd_next);
662 return rb_ary_new_from_node_args(ast_value, 2, RNODE_KW_ARG(node)->nd_body, RNODE_KW_ARG(node)->nd_next);
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);
667 return rb_ary_new_from_args(2, no_name_rest(),
668 NEW_CHILD(ast_value, RNODE_POSTARG(node)->nd_2nd));
671 struct rb_args_info *ainfo = &RNODE_ARGS(node)->nd_ainfo;
672 return rb_ary_new_from_args(10,
674 NEW_CHILD(ast_value, ainfo->pre_init),
675 NEW_CHILD(ast_value, (
NODE *)ainfo->opt_args),
676 var_name(ainfo->first_post_arg),
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));
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]));
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));
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),
703 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->post_args));
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),
712 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->args),
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);
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),
726 return rb_ary_new_from_args(1, rb_node_line_lineno_val(node));
728 return rb_ary_new_from_args(1, rb_node_file_path_val(node));
730 return rb_ary_new_from_args(1, rb_node_encoding_val(node));
732 return rb_ary_new_from_node_args(ast_value, 0);
738 rb_bug(
"node_children: unknown node: %s", ruby_node_name(
type));
747 return node_children(data->ast_value, data->node);
753 return (loc->beg_pos.lineno == 0 && loc->beg_pos.column == -1 && loc->end_pos.lineno == 0 && loc->end_pos.column == -1);
762 if (null_loc_p(loc))
return Qnil;
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;
774node_locations(
VALUE ast_value,
const NODE *node)
776 enum node_type
type = nd_type(node);
779 return rb_ary_new_from_args(2,
780 location_new(nd_code_loc(node)),
781 location_new(&RNODE_ALIAS(node)->keyword_loc));
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));
792 return rb_ary_new_from_args(2,
793 location_new(nd_code_loc(node)),
794 location_new(&RNODE_BREAK(node)->keyword_loc));
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));
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));
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));
811 return rb_ary_new_from_args(2,
812 location_new(nd_code_loc(node)),
813 location_new(&RNODE_DOT2(node)->operator_loc));
815 return rb_ary_new_from_args(2,
816 location_new(nd_code_loc(node)),
817 location_new(&RNODE_DOT3(node)->operator_loc));
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));
824 return rb_ary_new_from_args(2,
825 location_new(nd_code_loc(node)),
826 location_new(&RNODE_FLIP2(node)->operator_loc));
828 return rb_ary_new_from_args(2,
829 location_new(nd_code_loc(node)),
830 location_new(&RNODE_FLIP3(node)->operator_loc));
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));
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));
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));
851 return rb_ary_new_from_args(2,
852 location_new(nd_code_loc(node)),
853 location_new(&RNODE_NEXT(node)->keyword_loc));
855 return rb_ary_new_from_args(2,
856 location_new(nd_code_loc(node)),
857 location_new(&RNODE_OR(node)->operator_loc));
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));
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));
872 return rb_ary_new_from_args(2,
873 location_new(nd_code_loc(node)),
874 location_new(&RNODE_REDO(node)->keyword_loc));
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));
882 return rb_ary_new_from_args(2,
883 location_new(nd_code_loc(node)),
884 location_new(&RNODE_RETURN(node)->keyword_loc));
886 return rb_ary_new_from_args(2,
887 location_new(nd_code_loc(node)),
888 location_new(&RNODE_SPLAT(node)->operator_loc));
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));
896 return rb_ary_new_from_args(2,
897 location_new(nd_code_loc(node)),
898 location_new(&RNODE_UNDEF(node)->keyword_loc));
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));
906 return rb_ary_new_from_args(2,
907 location_new(nd_code_loc(node)),
908 location_new(&RNODE_VALIAS(node)->keyword_loc));
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));
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));
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));
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));
934 return rb_ary_new_from_args(1, location_new(nd_code_loc(node)));
937 rb_bug(
"node_locations: unknown node: %s", ruby_node_name(
type));
946 return node_locations(data->ast_value, data->node);
955 return INT2NUM(nd_first_lineno(data->node));
964 return INT2NUM(nd_first_column(data->node));
973 return INT2NUM(nd_last_lineno(data->node));
982 return INT2NUM(nd_last_column(data->node));
993 VALUE str, loc, token, all_tokens;
996 ast = rb_ruby_ast_data_get(data->ast_value);
998 parser_tokens = ast->node_buffer->tokens;
999 if (parser_tokens == NULL) {
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)
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);
1016 rb_ary_freeze(all_tokens);
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));
1047 ast = rb_ruby_ast_data_get(data->ast_value);
1049 return rb_parser_build_script_lines_from(ret);
1058 return INT2NUM(data->first_lineno);
1067 return INT2NUM(data->first_column);
1076 return INT2NUM(data->last_lineno);
1085 return INT2NUM(data->last_column);
1100 rb_str_catf(str,
":@%d:%d-%d:%d>",
1101 data->first_lineno, data->first_column,
1102 data->last_lineno, data->last_column);
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define CLASS_OF
Old name of rb_class_of.
#define INT2NUM
Old name of RB_INT2NUM.
#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.
#define rb_ary_new2
Old name of rb_ary_new_capa.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
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.
VALUE rb_io_close(VALUE io)
Closes the IO.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_strlen_lit(str)
Length of a string literal.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
VALUE type(ANYARGS)
ANYARGS-ed function type.
Functions related to nodes in the AST.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#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...
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.