3#include "internal/parse.h"
4#include "internal/symbol.h"
5#include "internal/warnings.h"
24node_gc_mark(
void *ptr)
27 rb_gc_mark((
VALUE)data->ast);
31node_memsize(
const void *ptr)
34 return rb_ast_memsize(data->ast);
41 RUBY_TYPED_FREE_IMMEDIATELY,
61 obj = rb_ast_node_alloc(rb_cNode);
62 setup_node(obj, ast, node);
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, error_tolerant, keep_tokens);
99 VALUE vparser = ast_parse_new();
100 if (
RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
101 if (
RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
102 if (
RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
103 ast = rb_parser_compile_string_path(vparser,
Qnil, str, 1);
104 return ast_parse_done(ast);
110 return rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
122 rb_funcall(f, rb_intern(
"set_encoding"), 2, rb_enc_from_encoding(enc),
rb_str_new_cstr(
"-"));
123 VALUE vparser = ast_parse_new();
124 if (
RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
125 if (
RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
126 if (
RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
127 ast = rb_parser_compile_file_path(vparser,
Qnil, f, 1);
129 return ast_parse_done(ast);
133lex_array(
VALUE array,
int index)
135 VALUE str = rb_ary_entry(array, index);
138 if (!rb_enc_asciicompat(rb_enc_get(str))) {
146rb_ast_parse_array(
VALUE array,
VALUE keep_script_lines,
VALUE error_tolerant,
VALUE keep_tokens)
150 array = rb_check_array_type(array);
151 VALUE vparser = ast_parse_new();
152 if (
RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
153 if (
RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
154 if (
RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
155 ast = rb_parser_compile_generic(vparser, lex_array,
Qnil, array, 1);
156 return ast_parse_done(ast);
162node_find(
VALUE self,
const int node_id)
169 if (nd_node_id(data->node) == node_id)
return self;
171 ary = node_children(data->ast, data->node);
177 VALUE result = node_find(child, node_id);
178 if (
RTEST(result))
return result;
185extern VALUE rb_e_script;
188script_lines(
VALUE path)
192 CONST_ID(script_lines,
"SCRIPT_LINES__");
196 lines = rb_hash_lookup(hash, path);
206 if (!rb_frame_info_p(location)) {
210 node_id = rb_get_node_id_from_frame_info(location);
225 if (rb_frame_info_p(body)) {
226 iseq = rb_get_iseq_from_frame_info(body);
227 node_id = rb_get_node_id_from_frame_info(body);
233 iseq = vm_proc_iseq(body);
235 if (!rb_obj_is_iseq((
VALUE)iseq))
return Qnil;
238 iseq = rb_method_iseq(body);
241 node_id = ISEQ_BODY(iseq)->location.node_id;
248 lines = ISEQ_BODY(iseq)->variable.script_lines;
250 VALUE path = rb_iseq_path(iseq);
251 int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path),
"-e", 2) == 0;
253 if (
NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
257 if (!
NIL_P(lines) || !
NIL_P(lines = script_lines(path))) {
258 node = rb_ast_parse_array(lines, keep_script_lines, error_tolerant, keep_tokens);
261 node = rb_ast_parse_str(rb_e_script, keep_script_lines, error_tolerant, keep_tokens);
264 node = rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
267 return node_find(node, node_id);
271rb_ast_node_alloc(
VALUE klass)
280node_type_to_str(
const NODE *node)
282 return (ruby_node_name(nd_type(node)) +
rb_strlen_lit(
"NODE_"));
291 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
300 return INT2FIX(nd_node_id(data->node));
303#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
306rb_ary_new_from_node_args(
rb_ast_t *ast,
long n, ...)
315 for (i=0; i<n; i++) {
317 node = va_arg(ar,
NODE *);
318 rb_ary_push(ary, NEW_CHILD(ast, node));
327 VALUE ary = rb_ary_new();
329 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
330 }
while (node->nd_next &&
331 nd_type_p(node->nd_next, NODE_BLOCK) &&
332 (node = node->nd_next, 1));
334 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
343 VALUE ary = rb_ary_new();
344 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
346 while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
347 node = node->nd_next;
348 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
350 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
358 if (!
id)
return Qnil;
359 if (!rb_id2str(
id))
return Qnil;
367 CONST_ID(rest,
"NODE_SPECIAL_NO_NAME_REST");
374 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast, rest_arg) : no_name_rest();
382 enum node_type
type = nd_type(node);
385 return dump_block(ast, node);
387 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
389 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
391 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
393 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
395 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
397 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
399 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
402 return rb_ary_push(rb_ary_new_from_node_args(ast, 2, node->nd_cond, node->nd_body),
403 RBOOL(node->nd_state));
406 return rb_ary_new_from_node_args(ast, 2, node->nd_iter, node->nd_body);
408 return rb_ary_new_from_node_args(ast, 1, node->nd_var);
412 return rb_ary_new_from_node_args(ast, 1, node->nd_stts);
414 return rb_ary_new_from_node_args(ast, 0);
416 return rb_ary_new_from_node_args(ast, 0);
418 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
420 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_resq, node->nd_else);
422 return rb_ary_new_from_node_args(ast, 3, node->nd_args, node->nd_body, node->nd_head);
424 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_ensr);
428 VALUE ary = rb_ary_new();
431 rb_ary_push(ary, NEW_CHILD(ast, node->nd_1st));
432 if (!node->nd_2nd || !nd_type_p(node->nd_2nd,
type))
436 rb_ary_push(ary, NEW_CHILD(ast, node->nd_2nd));
440 if (NODE_NAMED_REST_P(node->nd_args)) {
441 return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args);
444 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_value),
445 NEW_CHILD(ast, node->nd_head),
453 if (NODE_REQUIRED_KEYWORD_P(node)) {
454 return rb_ary_new_from_args(2, var_name(node->nd_vid),
ID2SYM(rb_intern(
"NODE_SPECIAL_REQUIRED_KEYWORD")));
456 return rb_ary_new_from_args(2, var_name(node->nd_vid), NEW_CHILD(ast, node->nd_value));
459 return rb_ary_new_from_args(2,
ID2SYM(node->nd_vid), NEW_CHILD(ast, node->nd_value));
461 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_else),
ID2SYM(node->nd_else->nd_mid), NEW_CHILD(ast, node->nd_value));
463 return rb_ary_new_from_args(4, NEW_CHILD(ast, node->nd_recv),
465 NEW_CHILD(ast, node->nd_args->nd_head),
466 NEW_CHILD(ast, node->nd_args->nd_body));
468 return rb_ary_new_from_args(5, NEW_CHILD(ast, node->nd_recv),
469 RBOOL(node->nd_next->nd_aid),
470 ID2SYM(node->nd_next->nd_vid),
471 ID2SYM(node->nd_next->nd_mid),
472 NEW_CHILD(ast, node->nd_value));
473 case NODE_OP_ASGN_AND:
474 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
ID2SYM(idANDOP),
475 NEW_CHILD(ast, node->nd_value));
476 case NODE_OP_ASGN_OR:
477 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
ID2SYM(idOROP),
478 NEW_CHILD(ast, node->nd_value));
480 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
482 NEW_CHILD(ast, node->nd_value));
486 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
488 NEW_CHILD(ast, node->nd_args));
490 return rb_ary_new_from_args(2,
ID2SYM(node->nd_mid),
491 NEW_CHILD(ast, node->nd_args));
493 return rb_ary_new_from_args(1,
ID2SYM(node->nd_mid));
495 return rb_ary_new_from_node_args(ast, 1, node->nd_args);
497 return rb_ary_new_from_node_args(ast, 0);
500 return dump_array(ast, node);
502 return rb_ary_new_from_node_args(ast, 0);
504 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
506 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
509 return rb_ary_new_from_args(1, var_name(node->nd_vid));
514 return rb_ary_new_from_args(1,
ID2SYM(node->nd_vid));
516 snprintf(name,
sizeof(name),
"$%ld", node->nd_nth);
517 return rb_ary_new_from_args(1,
ID2SYM(rb_intern(name)));
520 name[1] = (char)node->nd_nth;
522 return rb_ary_new_from_args(1,
ID2SYM(rb_intern(name)));
525 return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
527 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
529 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
534 return rb_ary_new_from_args(1, node->nd_lit);
536 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
542 NODE *n = node->nd_next;
545 head = NEW_CHILD(ast, n->nd_head);
546 next = NEW_CHILD(ast, n->nd_next);
548 return rb_ary_new_from_args(3, node->nd_lit, head, next);
551 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
553 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
555 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
557 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
558 case NODE_BLOCK_PASS:
559 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
561 return rb_ary_new_from_args(2,
ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
563 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
565 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
567 return rb_ary_new_from_args(2,
ID2SYM(node->nd_alias),
ID2SYM(node->nd_orig));
569 return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
571 return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
573 return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
575 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
577 return rb_ary_new_from_args(2, NEW_CHILD(ast, node->nd_head),
ID2SYM(node->nd_mid));
579 return rb_ary_new_from_args(1,
ID2SYM(node->nd_mid));
584 return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
586 return rb_ary_new_from_node_args(ast, 0);
588 return rb_ary_new_from_node_args(ast, 0);
590 return rb_ary_new_from_node_args(ast, 0);
592 return rb_ary_new_from_node_args(ast, 0);
594 return rb_ary_new_from_node_args(ast, 0);
596 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
598 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
600 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_args));
602 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
604 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
606 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
608 if (NODE_NAMED_REST_P(node->nd_1st)) {
609 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
611 return rb_ary_new_from_args(2, no_name_rest(),
612 NEW_CHILD(ast, node->nd_2nd));
616 return rb_ary_new_from_args(10,
618 NEW_CHILD(ast, ainfo->pre_init),
619 NEW_CHILD(ast, ainfo->opt_args),
620 var_name(ainfo->first_post_arg),
622 NEW_CHILD(ast, ainfo->post_init),
623 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
624 ?
ID2SYM(rb_intern(
"NODE_SPECIAL_EXCESSIVE_COMMA"))
625 : var_name(ainfo->rest_arg)),
626 (ainfo->no_kwarg ?
Qfalse : NEW_CHILD(ast, ainfo->kw_args)),
627 (ainfo->no_kwarg ?
Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
628 var_name(ainfo->block_arg));
633 int i, size = tbl ? tbl->size : 0;
634 VALUE locals = rb_ary_new_capa(size);
635 for (i = 0; i < size; i++) {
636 rb_ary_push(locals, var_name(tbl->ids[i]));
638 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
643 VALUE rest = rest_arg(ast, apinfo->rest_arg);
644 return rb_ary_new_from_args(4,
645 NEW_CHILD(ast, node->nd_pconst),
646 NEW_CHILD(ast, apinfo->pre_args),
648 NEW_CHILD(ast, apinfo->post_args));
653 VALUE pre_rest = rest_arg(ast, fpinfo->pre_rest_arg);
654 VALUE post_rest = rest_arg(ast, fpinfo->post_rest_arg);
655 return rb_ary_new_from_args(4,
656 NEW_CHILD(ast, node->nd_pconst),
658 NEW_CHILD(ast, fpinfo->args),
663 VALUE kwrest = node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ?
ID2SYM(rb_intern(
"NODE_SPECIAL_NO_REST_KEYWORD")) :
664 NEW_CHILD(ast, node->nd_pkwrestarg);
666 return rb_ary_new_from_args(3,
667 NEW_CHILD(ast, node->nd_pconst),
668 NEW_CHILD(ast, node->nd_pkwargs),
672 return rb_ary_new_from_node_args(ast, 0);
678 rb_bug(
"node_children: unknown node: %s", ruby_node_name(
type));
687 return node_children(data->ast, data->node);
696 return INT2NUM(nd_first_lineno(data->node));
705 return INT2NUM(nd_first_column(data->node));
714 return INT2NUM(nd_last_lineno(data->node));
723 return INT2NUM(nd_last_column(data->node));
732 return rb_ast_tokens(data->ast);
748 node_type_to_str(data->node),
749 nd_first_lineno(data->node), nd_first_column(data->node),
750 nd_last_lineno(data->node), nd_last_column(data->node));
760 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.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
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_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_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.
#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.
#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.
uintptr_t VALUE
Type that represents a Ruby object.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.