3#include "internal/parse.h"
4#include "internal/symbol.h"
5#include "internal/warnings.h"
24node_gc_mark(
void *ptr)
31node_memsize(
const void *ptr)
34 return rb_ast_memsize(data->ast);
41 RUBY_TYPED_FREE_IMMEDIATELY,
44static VALUE rb_ast_node_alloc(VALUE klass);
61 obj = rb_ast_node_alloc(rb_cNode);
62 setup_node(obj, ast, node);
67static VALUE rb_ast_parse_str(VALUE str, VALUE keep_script_lines);
68static VALUE rb_ast_parse_file(VALUE path, VALUE keep_script_lines);
73 return rb_parser_set_context(rb_parser_new(), NULL, 0);
79 if (!ast->body.root) {
84 return ast_new_internal(ast, (
NODE *)ast->body.root);
90 return rb_ast_parse_str(str, keep_script_lines);
94rb_ast_parse_str(VALUE str, VALUE keep_script_lines)
99 VALUE vparser = ast_parse_new();
100 if (
RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
101 ast = rb_parser_compile_string_path(vparser,
Qnil, str, 1);
102 return ast_parse_done(ast);
108 return rb_ast_parse_file(path, keep_script_lines);
112rb_ast_parse_file(VALUE path, VALUE keep_script_lines)
121 VALUE vparser = ast_parse_new();
122 if (
RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
123 ast = rb_parser_compile_file_path(vparser,
Qnil, f, 1);
125 return ast_parse_done(ast);
129lex_array(VALUE array,
int index)
135 rb_raise(rb_eArgError,
"invalid source encoding");
142rb_ast_parse_array(VALUE array, VALUE keep_script_lines)
147 VALUE vparser = ast_parse_new();
148 if (
RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
149 ast = rb_parser_compile_generic(vparser, lex_array,
Qnil, array, 1);
150 return ast_parse_done(ast);
156node_find(VALUE
self,
const int node_id)
163 if (nd_node_id(data->node) == node_id)
return self;
165 ary = node_children(data->ast, data->node);
171 VALUE result = node_find(child, node_id);
172 if (
RTEST(result))
return result;
179extern VALUE rb_e_script;
182script_lines(VALUE path)
186 CONST_ID(script_lines,
"SCRIPT_LINES__");
198 VALUE node, lines =
Qnil;
202 if (rb_frame_info_p(body)) {
203 iseq = rb_get_iseq_from_frame_info(body);
204 node_id = rb_get_node_id_from_frame_info(body);
210 iseq = vm_proc_iseq(body);
212 if (!rb_obj_is_iseq((VALUE)iseq))
return Qnil;
215 iseq = rb_method_iseq(body);
218 node_id = iseq->body->location.node_id;
225 lines = iseq->body->variable.script_lines;
227 VALUE path = rb_iseq_path(iseq);
230 if (
NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
231 rb_raise(rb_eArgError,
"cannot get AST for method defined in eval");
234 if (!
NIL_P(lines) || !
NIL_P(lines = script_lines(path))) {
235 node = rb_ast_parse_array(lines, keep_script_lines);
238 node = rb_ast_parse_str(rb_e_script, keep_script_lines);
241 node = rb_ast_parse_file(path, keep_script_lines);
244 return node_find(node, node_id);
248rb_ast_node_alloc(VALUE klass)
257node_type_to_str(
const NODE *node)
259 return (ruby_node_name(nd_type(node)) +
rb_strlen_lit(
"NODE_"));
268 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
277 return INT2FIX(nd_node_id(data->node));
280#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
283rb_ary_new_from_node_args(
rb_ast_t *ast,
long n, ...)
292 for (i=0; i<n; i++) {
294 node = va_arg(ar,
NODE *);
307 }
while (node->nd_next &&
308 nd_type_p(node->nd_next, NODE_BLOCK) &&
309 (node = node->nd_next, 1));
323 while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
324 node = node->nd_next;
335 if (!
id)
return Qnil;
344 CONST_ID(rest,
"NODE_SPECIAL_NO_NAME_REST");
351 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast, rest_arg) : no_name_rest();
359 enum node_type
type = nd_type(node);
362 return dump_block(ast, node);
364 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
366 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
368 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
370 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
372 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
374 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
376 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
379 return rb_ary_push(rb_ary_new_from_node_args(ast, 2, node->nd_cond, node->nd_body),
380 RBOOL(node->nd_state));
383 return rb_ary_new_from_node_args(ast, 2, node->nd_iter, node->nd_body);
385 return rb_ary_new_from_node_args(ast, 1, node->nd_var);
389 return rb_ary_new_from_node_args(ast, 1, node->nd_stts);
391 return rb_ary_new_from_node_args(ast, 0);
393 return rb_ary_new_from_node_args(ast, 0);
395 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
397 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_resq, node->nd_else);
399 return rb_ary_new_from_node_args(ast, 3, node->nd_args, node->nd_body, node->nd_head);
401 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_ensr);
409 if (!node->nd_2nd || !nd_type_p(node->nd_2nd,
type))
417 if (NODE_NAMED_REST_P(node->nd_args)) {
418 return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args);
422 NEW_CHILD(ast, node->nd_head),
430 if (NODE_REQUIRED_KEYWORD_P(node)) {
442 NEW_CHILD(ast, node->nd_args->nd_head),
443 NEW_CHILD(ast, node->nd_args->nd_body));
446 RBOOL(node->nd_next->nd_aid),
447 ID2SYM(node->nd_next->nd_vid),
448 ID2SYM(node->nd_next->nd_mid),
449 NEW_CHILD(ast, node->nd_value));
450 case NODE_OP_ASGN_AND:
452 NEW_CHILD(ast, node->nd_value));
453 case NODE_OP_ASGN_OR:
455 NEW_CHILD(ast, node->nd_value));
459 NEW_CHILD(ast, node->nd_value));
465 NEW_CHILD(ast, node->nd_args));
468 NEW_CHILD(ast, node->nd_args));
472 return rb_ary_new_from_node_args(ast, 1, node->nd_args);
474 return rb_ary_new_from_node_args(ast, 0);
477 return dump_array(ast, node);
479 return rb_ary_new_from_node_args(ast, 0);
481 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
483 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
493 snprintf(name,
sizeof(name),
"$%ld", node->nd_nth);
497 name[1] = (char)node->nd_nth;
502 return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
504 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
506 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
513 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
519 NODE *n = node->nd_next;
522 head = NEW_CHILD(ast, n->nd_head);
523 next = NEW_CHILD(ast, n->nd_next);
528 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
530 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
532 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
534 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
535 case NODE_BLOCK_PASS:
536 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
542 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
546 return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
548 return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
550 return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
552 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
561 return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
563 return rb_ary_new_from_node_args(ast, 0);
565 return rb_ary_new_from_node_args(ast, 0);
567 return rb_ary_new_from_node_args(ast, 0);
569 return rb_ary_new_from_node_args(ast, 0);
571 return rb_ary_new_from_node_args(ast, 0);
573 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
575 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
579 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
581 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
583 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
585 if (NODE_NAMED_REST_P(node->nd_1st)) {
586 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
589 NEW_CHILD(ast, node->nd_2nd));
595 NEW_CHILD(ast, ainfo->pre_init),
596 NEW_CHILD(ast, ainfo->opt_args),
597 var_name(ainfo->first_post_arg),
599 NEW_CHILD(ast, ainfo->post_init),
600 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
602 : var_name(ainfo->rest_arg)),
603 (ainfo->no_kwarg ?
Qfalse : NEW_CHILD(ast, ainfo->kw_args)),
604 (ainfo->no_kwarg ?
Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
605 var_name(ainfo->block_arg));
610 int i, size = tbl ? tbl->size : 0;
612 for (i = 0; i < size; i++) {
615 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
620 VALUE rest = rest_arg(ast, apinfo->rest_arg);
622 NEW_CHILD(ast, node->nd_pconst),
623 NEW_CHILD(ast, apinfo->pre_args),
625 NEW_CHILD(ast, apinfo->post_args));
630 VALUE pre_rest = rest_arg(ast, fpinfo->pre_rest_arg);
631 VALUE post_rest = rest_arg(ast, fpinfo->post_rest_arg);
633 NEW_CHILD(ast, node->nd_pconst),
635 NEW_CHILD(ast, fpinfo->args),
640 VALUE kwrest = node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ?
ID2SYM(
rb_intern(
"NODE_SPECIAL_NO_REST_KEYWORD")) :
641 NEW_CHILD(ast, node->nd_pkwrestarg);
644 NEW_CHILD(ast, node->nd_pconst),
645 NEW_CHILD(ast, node->nd_pkwargs),
653 rb_bug(
"node_children: unknown node: %s", ruby_node_name(
type));
662 return node_children(data->ast, data->node);
671 return INT2NUM(nd_first_lineno(data->node));
680 return INT2NUM(nd_first_column(data->node));
689 return INT2NUM(nd_last_lineno(data->node));
698 return INT2NUM(nd_last_column(data->node));
714 node_type_to_str(data->node),
715 nd_first_lineno(data->node), nd_first_column(data->node),
716 nd_last_lineno(data->node), nd_last_column(data->node));
726 VALUE ret = data->ast->body.script_lines;
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 T_HASH
Old name of RUBY_T_HASH.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#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.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
VALUE rb_enc_from_encoding(rb_encoding *enc)
Queries the Ruby-level counterpart instance of rb_cEncoding that corresponds to the passed encoding.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
void rb_gc_mark(VALUE obj)
Marks an object.
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
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_strlen_lit(str)
Length of a string literal.
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
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.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
#define DECIMAL_SIZE_OF_BITS(n)
an approximation of ceil(n * log10(2)), up to 65536 at least
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
VALUE type(ANYARGS)
ANYARGS-ed function type.
#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.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the 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 FilePathValue(v)
Ensures that the parameter object is a path.
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.