2018-01-15 18:43:17 -05:00
|
|
|
#include "ruby.h"
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "vm_core.h"
|
|
|
|
|
|
|
|
static VALUE rb_mAST;
|
|
|
|
static VALUE rb_cNode;
|
|
|
|
|
|
|
|
struct ASTNodeData {
|
|
|
|
rb_ast_t *ast;
|
|
|
|
NODE *node;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
node_gc_mark(void *ptr)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data = (struct ASTNodeData *)ptr;
|
|
|
|
rb_gc_mark((VALUE)data->ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t rb_node_type = {
|
|
|
|
"AST/node",
|
|
|
|
{node_gc_mark, RUBY_TYPED_DEFAULT_FREE, 0,},
|
|
|
|
0, 0,
|
|
|
|
RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE rb_ast_node_alloc(VALUE klass);
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_node(VALUE obj, rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(obj, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
data->ast = ast;
|
|
|
|
data->node = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ast_new_internal(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
obj = rb_ast_node_alloc(rb_cNode);
|
|
|
|
setup_node(obj, ast, node);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_s_parse(VALUE module, VALUE str)
|
|
|
|
{
|
|
|
|
VALUE obj;
|
|
|
|
rb_ast_t *ast = 0;
|
|
|
|
|
|
|
|
const VALUE parser = rb_parser_new();
|
|
|
|
|
|
|
|
str = rb_check_string_type(str);
|
2018-05-31 01:49:56 -04:00
|
|
|
rb_parser_set_context(parser, NULL, 1);
|
2018-01-15 18:43:17 -05:00
|
|
|
ast = rb_parser_compile_string_path(parser, rb_str_new_cstr("no file name"), str, 1);
|
|
|
|
|
|
|
|
if (!ast->body.root) return Qnil;
|
|
|
|
|
|
|
|
obj = ast_new_internal(ast, (NODE *)ast->body.root);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_s_parse_file(VALUE module, VALUE path)
|
|
|
|
{
|
|
|
|
VALUE obj, f;
|
|
|
|
rb_ast_t *ast = 0;
|
|
|
|
rb_encoding *enc = rb_utf8_encoding();
|
|
|
|
|
|
|
|
const VALUE parser = rb_parser_new();
|
|
|
|
|
|
|
|
FilePathValue(path);
|
|
|
|
f = rb_file_open_str(path, "r");
|
|
|
|
rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
|
2018-05-31 01:49:56 -04:00
|
|
|
rb_parser_set_context(parser, NULL, 1);
|
2018-01-15 18:43:17 -05:00
|
|
|
ast = rb_parser_compile_file_path(parser, path, f, 1);
|
|
|
|
|
|
|
|
rb_io_close(f);
|
|
|
|
|
|
|
|
if (!ast->body.root) return Qnil;
|
|
|
|
|
|
|
|
obj = ast_new_internal(ast, (NODE *)ast->body.root);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
node_type_to_str(NODE *node)
|
|
|
|
{
|
|
|
|
return ruby_node_name(nd_type(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_type(VALUE self)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return rb_str_new_cstr(node_type_to_str(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ary_new_from_node_args(rb_ast_t *ast, long n, ...)
|
|
|
|
{
|
|
|
|
va_list ar;
|
|
|
|
VALUE ary;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(n);
|
|
|
|
|
|
|
|
va_start(ar, n);
|
|
|
|
for (i=0; i<n; i++) {
|
2018-06-06 02:14:21 -04:00
|
|
|
NODE *node;
|
|
|
|
node = va_arg(ar, NODE *);
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node));
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
|
|
|
va_end(ar);
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_block(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
do {
|
2018-06-06 02:14:21 -04:00
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
|
2018-01-15 18:43:17 -05:00
|
|
|
} while (node->nd_next &&
|
2018-06-06 02:14:21 -04:00
|
|
|
nd_type(node->nd_next) == NODE_BLOCK &&
|
|
|
|
(node = node->nd_next, 1));
|
2018-01-15 18:43:17 -05:00
|
|
|
if (node->nd_next) {
|
2018-06-06 02:14:21 -04:00
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_array(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
|
|
|
|
|
|
|
|
while (node->nd_next && nd_type(node->nd_next) == NODE_ARRAY) {
|
2018-06-06 02:14:21 -04:00
|
|
|
node = node->nd_next;
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
node_children(rb_ast_t *ast, NODE *node)
|
|
|
|
{
|
2018-05-01 02:55:43 -04:00
|
|
|
enum node_type type = nd_type(node);
|
|
|
|
switch (type) {
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_BLOCK:
|
2018-06-06 02:14:21 -04:00
|
|
|
return dump_block(ast, node);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_IF:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_UNLESS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CASE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CASE2:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_WHEN:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_WHILE:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto loop;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_UNTIL:
|
|
|
|
loop:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_cond, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ITER:
|
|
|
|
case NODE_FOR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_iter, node->nd_body);
|
2018-01-16 02:24:53 -05:00
|
|
|
case NODE_FOR_MASGN:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_var);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_BREAK:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto jump;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_NEXT:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto jump;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_RETURN:
|
|
|
|
jump:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_stts);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_REDO:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_RETRY:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_BEGIN:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_RESCUE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_resq, node->nd_else);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_RESBODY:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_args, node->nd_body, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ENSURE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_ensr);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_AND:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto andor;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OR:
|
|
|
|
andor:
|
2018-06-06 02:14:21 -04:00
|
|
|
{
|
|
|
|
VALUE ary = rb_ary_new();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_1st));
|
|
|
|
if (!node->nd_2nd || nd_type(node->nd_2nd) != (int)type)
|
|
|
|
break;
|
|
|
|
node = node->nd_2nd;
|
|
|
|
}
|
|
|
|
rb_ary_push(ary, NEW_CHILD(ast, node->nd_2nd));
|
|
|
|
return ary;
|
|
|
|
}
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_MASGN:
|
2018-03-20 09:30:57 -04:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_args)) {
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args);
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_value, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_LASGN:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto asgn;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DASGN:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto asgn;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DASGN_CURR:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto asgn;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_IASGN:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto asgn;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CVASGN:
|
|
|
|
asgn:
|
2018-03-20 09:30:57 -04:00
|
|
|
if (NODE_REQUIRED_KEYWORD_P(node)) {
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_GASGN:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CDECL:
|
|
|
|
if (node->nd_vid) {
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_else, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OP_ASGN1:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_args->nd_head, node->nd_args->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OP_ASGN2:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OP_ASGN_AND:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto asgn_andor;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OP_ASGN_OR:
|
|
|
|
asgn_andor:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OP_CDECL:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CALL:
|
|
|
|
case NODE_OPCALL:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_args);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_FCALL:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_args);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_VCALL:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_QCALL:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_args);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_SUPER:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_args);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ZSUPER:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ARRAY:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto ary;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_VALUES:
|
|
|
|
ary:
|
2018-06-06 02:14:21 -04:00
|
|
|
return dump_array(ast, node);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ZARRAY:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_HASH:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_YIELD:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_LVAR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DVAR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_IVAR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CONST:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CVAR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_GVAR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_NTH_REF:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_BACK_REF:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_MATCH:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_MATCH2:
|
2018-06-06 02:14:21 -04:00
|
|
|
if (node->nd_args) {
|
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
|
|
|
|
}
|
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_MATCH3:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_LIT:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto lit;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_STR:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto lit;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_XSTR:
|
|
|
|
lit:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ONCE:
|
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
|
|
|
case NODE_DSTR:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto dlit;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DXSTR:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto dlit;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DREGX:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto dlit;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DSYM:
|
|
|
|
dlit:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_next->nd_head, node->nd_next->nd_next);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_EVSTR:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ARGSCAT:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ARGSPUSH:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_SPLAT:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_BLOCK_PASS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DEFN:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_defn);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DEFS:
|
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_defn);
|
|
|
|
case NODE_ALIAS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_VALIAS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_UNDEF:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_CLASS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_MODULE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_SCLASS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_COLON2:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_COLON3:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DOT2:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto dot;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DOT3:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto dot;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_FLIP2:
|
2018-06-06 02:14:21 -04:00
|
|
|
goto dot;
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_FLIP3:
|
|
|
|
dot:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_SELF:
|
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
|
|
|
case NODE_NIL:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_TRUE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_FALSE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ERRINFO:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 0);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_DEFINED:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_head);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_POSTEXE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ATTRASGN:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_args);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_LAMBDA:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_OPT_ARG:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_KW_ARG:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_POSTARG:
|
2018-06-06 02:14:21 -04:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_1st)) {
|
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
|
|
|
|
}
|
|
|
|
return rb_ary_new_from_node_args(ast, 1, node->nd_2nd);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ARGS:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 5, node->nd_ainfo->pre_init, node->nd_ainfo->post_init, node->nd_ainfo->opt_args, node->nd_ainfo->kw_args, node->nd_ainfo->kw_rest_arg);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_SCOPE:
|
2018-06-06 02:14:21 -04:00
|
|
|
return rb_ary_new_from_node_args(ast, 2, node->nd_args, node->nd_body);
|
2018-01-15 18:43:17 -05:00
|
|
|
case NODE_ARGS_AUX:
|
|
|
|
case NODE_LAST:
|
2018-06-06 02:14:21 -04:00
|
|
|
break;
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
|
|
|
|
2018-05-01 02:55:43 -04:00
|
|
|
rb_bug("node_children: unknown node: %s", ruby_node_name(type));
|
2018-01-15 18:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_children(VALUE self)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return node_children(data->ast, data->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_first_lineno(VALUE self)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_first_lineno(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_first_column(VALUE self)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_first_column(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_last_lineno(VALUE self)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_last_lineno(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_last_column(VALUE self)
|
|
|
|
{
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
return INT2NUM(nd_last_column(data->node));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_ast_node_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
VALUE cname;
|
|
|
|
struct ASTNodeData *data;
|
|
|
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
|
|
|
|
|
|
|
cname = rb_class_path(rb_obj_class(self));
|
|
|
|
str = rb_str_new2("#<");
|
|
|
|
|
|
|
|
rb_str_append(str, cname);
|
|
|
|
rb_str_cat2(str, "(");
|
|
|
|
rb_str_catf(str, "%s(%d) %d:%d, %d:%d", node_type_to_str(data->node), nd_type(data->node), nd_first_lineno(data->node), nd_first_column(data->node), nd_last_lineno(data->node), nd_last_column(data->node));
|
|
|
|
rb_str_cat2(str, "): >");
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_ast(void)
|
|
|
|
{
|
2018-05-31 02:13:06 -04:00
|
|
|
rb_mAST = rb_define_module_under(rb_cRubyVM, "AST");
|
2018-01-15 18:43:17 -05:00
|
|
|
rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
|
|
|
|
|
|
|
|
rb_define_alloc_func(rb_cNode, rb_ast_node_alloc);
|
2018-05-31 02:24:19 -04:00
|
|
|
rb_undef_method(CLASS_OF(rb_cNode), "new");
|
2018-01-15 18:43:17 -05:00
|
|
|
rb_define_singleton_method(rb_mAST, "parse", rb_ast_s_parse, 1);
|
|
|
|
rb_define_singleton_method(rb_mAST, "parse_file", rb_ast_s_parse_file, 1);
|
|
|
|
rb_define_method(rb_cNode, "type", rb_ast_node_type, 0);
|
|
|
|
rb_define_method(rb_cNode, "first_lineno", rb_ast_node_first_lineno, 0);
|
|
|
|
rb_define_method(rb_cNode, "first_column", rb_ast_node_first_column, 0);
|
|
|
|
rb_define_method(rb_cNode, "last_lineno", rb_ast_node_last_lineno, 0);
|
|
|
|
rb_define_method(rb_cNode, "last_column", rb_ast_node_last_column, 0);
|
|
|
|
rb_define_method(rb_cNode, "children", rb_ast_node_children, 0);
|
|
|
|
rb_define_method(rb_cNode, "inspect", rb_ast_node_inspect, 0);
|
|
|
|
}
|