2009-12-09 17:05:59 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
node.c - ruby node tree
|
|
|
|
|
|
|
|
$Author: mame $
|
|
|
|
created at: 09/12/06 21:23:44 JST
|
|
|
|
|
|
|
|
Copyright (C) 2009 Yusuke Endoh
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/hash.h"
|
|
|
|
#include "internal/variable.h"
|
2009-12-09 17:05:59 +03:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "vm_core.h"
|
|
|
|
|
2019-04-23 01:24:52 +03:00
|
|
|
#define NODE_BUF_DEFAULT_LEN 16
|
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
#define A(str) rb_str_cat2(buf, (str))
|
|
|
|
#define AR(str) rb_str_concat(buf, (str))
|
|
|
|
|
|
|
|
#define A_INDENT add_indent(buf, indent)
|
2016-10-05 09:43:34 +03:00
|
|
|
#define D_INDENT rb_str_cat2(indent, next_indent)
|
|
|
|
#define D_DEDENT rb_str_resize(indent, RSTRING_LEN(indent) - 4)
|
2010-12-20 17:13:12 +03:00
|
|
|
#define A_ID(id) add_id(buf, (id))
|
2012-11-21 16:42:11 +04:00
|
|
|
#define A_INT(val) rb_str_catf(buf, "%d", (val))
|
|
|
|
#define A_LONG(val) rb_str_catf(buf, "%ld", (val))
|
2020-10-20 13:32:10 +03:00
|
|
|
#define A_LIT(lit) AR(rb_dump_literal(lit))
|
2015-10-08 04:44:51 +03:00
|
|
|
#define A_NODE_HEADER(node, term) \
|
2021-07-12 06:10:16 +03:00
|
|
|
rb_str_catf(buf, "@ %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))%s"term, \
|
|
|
|
ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node), \
|
2018-01-08 09:55:12 +03:00
|
|
|
nd_first_lineno(node), nd_first_column(node), \
|
|
|
|
nd_last_lineno(node), nd_last_column(node), \
|
|
|
|
(node->flags & NODE_FL_NEWLINE ? "*" : ""))
|
2015-10-08 04:45:41 +03:00
|
|
|
#define A_FIELD_HEADER(len, name, term) \
|
|
|
|
rb_str_catf(buf, "+- %.*s:"term, (len), (name))
|
|
|
|
#define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term))
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2015-10-08 04:44:51 +03:00
|
|
|
#define D_NULL_NODE (A_INDENT, A("(null node)\n"))
|
|
|
|
#define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n"))
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2017-12-05 11:50:14 +03:00
|
|
|
#define COMPOUND_FIELD(len, name) \
|
|
|
|
FIELD_BLOCK((D_FIELD_HEADER((len), (name), "\n"), D_INDENT), D_DEDENT)
|
|
|
|
|
|
|
|
#define COMPOUND_FIELD1(name, ann) \
|
2016-03-10 10:27:40 +03:00
|
|
|
COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \
|
2017-12-05 11:50:14 +03:00
|
|
|
FIELD_NAME_DESC(name, ann))
|
2016-03-10 09:19:55 +03:00
|
|
|
|
2015-10-08 04:45:41 +03:00
|
|
|
#define FIELD_NAME_DESC(name, ann) name " (" ann ")"
|
|
|
|
#define FIELD_NAME_LEN(name, ann) (int)( \
|
|
|
|
comment ? \
|
|
|
|
rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
|
|
|
|
rb_strlen_lit(name))
|
|
|
|
#define SIMPLE_FIELD(len, name) \
|
2017-12-05 11:50:14 +03:00
|
|
|
FIELD_BLOCK(D_FIELD_HEADER((len), (name), " "), A("\n"))
|
|
|
|
|
|
|
|
#define FIELD_BLOCK(init, reset) \
|
|
|
|
for (init, field_flag = 1; \
|
2015-10-08 04:45:00 +03:00
|
|
|
field_flag; /* should be optimized away */ \
|
2017-12-05 11:50:14 +03:00
|
|
|
reset, field_flag = 0)
|
2015-10-08 04:45:00 +03:00
|
|
|
|
2015-10-08 04:45:41 +03:00
|
|
|
#define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
|
2015-10-08 04:45:00 +03:00
|
|
|
#define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
|
|
|
|
#define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
|
2020-07-03 10:56:44 +03:00
|
|
|
#define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
|
2015-10-08 04:45:00 +03:00
|
|
|
#define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
|
|
|
|
#define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
|
|
|
|
#define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)
|
|
|
|
#define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
#define F_NODE(name, ann) \
|
2017-12-05 11:50:14 +03:00
|
|
|
COMPOUND_FIELD1(#name, ann) {dump_node(buf, indent, comment, node->name);}
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
#define ANN(ann) \
|
|
|
|
if (comment) { \
|
2015-10-08 04:45:41 +03:00
|
|
|
A_INDENT; A("| # " ann "\n"); \
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LAST_NODE (next_indent = " ")
|
|
|
|
|
2020-10-20 13:32:10 +03:00
|
|
|
VALUE
|
|
|
|
rb_dump_literal(VALUE lit)
|
|
|
|
{
|
|
|
|
if (!RB_SPECIAL_CONST_P(lit)) {
|
|
|
|
VALUE str;
|
|
|
|
switch (RB_BUILTIN_TYPE(lit)) {
|
|
|
|
case T_CLASS: case T_MODULE: case T_ICLASS:
|
|
|
|
str = rb_class_path(lit);
|
|
|
|
if (FL_TEST(lit, FL_SINGLETON)) {
|
|
|
|
str = rb_sprintf("<%"PRIsVALUE">", str);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rb_inspect(lit);
|
|
|
|
}
|
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
static void
|
|
|
|
add_indent(VALUE buf, VALUE indent)
|
|
|
|
{
|
|
|
|
AR(indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_id(VALUE buf, ID id)
|
|
|
|
{
|
|
|
|
if (id == 0) {
|
|
|
|
A("(null)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE str = rb_id2str(id);
|
|
|
|
if (str) {
|
2011-01-31 17:29:49 +03:00
|
|
|
A(":"); AR(str);
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-06-04 05:40:21 +03:00
|
|
|
rb_str_catf(buf, "(internal variable: 0x%"PRIsVALUE")", id);
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:19:55 +03:00
|
|
|
struct add_option_arg {
|
|
|
|
VALUE buf, indent;
|
|
|
|
st_index_t count;
|
|
|
|
};
|
|
|
|
|
2018-01-05 10:47:51 +03:00
|
|
|
static void dump_node(VALUE, VALUE, int, const NODE *);
|
2016-08-15 14:31:36 +03:00
|
|
|
static const char default_indent[] = "| ";
|
|
|
|
|
|
|
|
static void
|
2018-01-05 10:47:51 +03:00
|
|
|
dump_array(VALUE buf, VALUE indent, int comment, const NODE *node)
|
2016-08-15 14:31:36 +03:00
|
|
|
{
|
|
|
|
int field_flag;
|
|
|
|
const char *next_indent = default_indent;
|
|
|
|
F_LONG(nd_alen, "length");
|
|
|
|
F_NODE(nd_head, "element");
|
2021-12-03 18:01:24 +03:00
|
|
|
while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
|
2016-08-15 14:31:36 +03:00
|
|
|
node = node->nd_next;
|
|
|
|
F_NODE(nd_head, "element");
|
|
|
|
}
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_next, "next element");
|
|
|
|
}
|
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
static void
|
2018-01-05 10:47:51 +03:00
|
|
|
dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
|
2009-12-09 17:05:59 +03:00
|
|
|
{
|
2015-10-08 04:45:00 +03:00
|
|
|
int field_flag;
|
2016-10-05 09:43:34 +03:00
|
|
|
int i;
|
2016-08-15 14:31:36 +03:00
|
|
|
const char *next_indent = default_indent;
|
2017-02-08 04:18:56 +03:00
|
|
|
enum node_type type;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
D_NULL_NODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
D_NODE_HEADER(node);
|
|
|
|
|
2017-02-08 04:18:56 +03:00
|
|
|
type = nd_type(node);
|
|
|
|
switch (type) {
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_BLOCK:
|
|
|
|
ANN("statement sequence");
|
2016-10-05 09:43:34 +03:00
|
|
|
ANN("format: [nd_head]; ...; [nd_next]");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("example: foo; bar");
|
2016-10-05 09:43:34 +03:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
A_INDENT;
|
|
|
|
rb_str_catf(buf, "+- nd_head (%s%d):\n",
|
|
|
|
comment ? "statement #" : "", ++i);
|
|
|
|
if (!node->nd_next) LAST_NODE;
|
|
|
|
D_INDENT;
|
|
|
|
dump_node(buf, indent, comment, node->nd_head);
|
|
|
|
D_DEDENT;
|
|
|
|
} while (node->nd_next &&
|
2021-12-03 18:01:24 +03:00
|
|
|
nd_type_p(node->nd_next, NODE_BLOCK) &&
|
2016-10-05 09:43:34 +03:00
|
|
|
(node = node->nd_next, 1));
|
2017-10-17 15:30:12 +03:00
|
|
|
if (node->nd_next) {
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_next, "next block");
|
|
|
|
}
|
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_IF:
|
|
|
|
ANN("if statement");
|
|
|
|
ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
|
|
|
|
ANN("example: if x == 1 then foo else bar end");
|
|
|
|
F_NODE(nd_cond, "condition expr");
|
|
|
|
F_NODE(nd_body, "then clause");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_else, "else clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2017-09-14 08:27:02 +03:00
|
|
|
case NODE_UNLESS:
|
|
|
|
ANN("unless statement");
|
|
|
|
ANN("format: unless [nd_cond] then [nd_body] else [nd_else] end");
|
|
|
|
ANN("example: unless x == 1 then foo else bar end");
|
|
|
|
F_NODE(nd_cond, "condition expr");
|
|
|
|
F_NODE(nd_body, "then clause");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_else, "else clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2017-09-14 08:27:02 +03:00
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_CASE:
|
|
|
|
ANN("case statement");
|
|
|
|
ANN("format: case [nd_head]; [nd_body]; end");
|
|
|
|
ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
|
|
|
|
F_NODE(nd_head, "case expr");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "when clauses");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2017-10-31 03:46:30 +03:00
|
|
|
case NODE_CASE2:
|
2017-12-15 08:55:09 +03:00
|
|
|
ANN("case statement with no head");
|
2017-10-31 03:46:30 +03:00
|
|
|
ANN("format: case; [nd_body]; end");
|
|
|
|
ANN("example: case; when 1; foo; when 2; bar; else baz; end");
|
|
|
|
F_NODE(nd_head, "case expr");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "when clauses");
|
|
|
|
return;
|
2019-04-17 09:48:03 +03:00
|
|
|
case NODE_CASE3:
|
|
|
|
ANN("case statement (pattern matching)");
|
|
|
|
ANN("format: case [nd_head]; [nd_body]; end");
|
|
|
|
ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
|
|
|
|
F_NODE(nd_head, "case expr");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "in clauses");
|
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_WHEN:
|
2017-12-15 08:55:08 +03:00
|
|
|
ANN("when clause");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
|
|
|
|
ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
|
|
|
|
F_NODE(nd_head, "when value");
|
2017-12-15 08:55:08 +03:00
|
|
|
F_NODE(nd_body, "when body");
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_next, "next when clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2019-04-17 09:48:03 +03:00
|
|
|
case NODE_IN:
|
|
|
|
ANN("in clause");
|
2019-04-17 10:15:08 +03:00
|
|
|
ANN("format: in [nd_head]; [nd_body]; (in or else) [nd_next]");
|
2019-04-17 09:48:03 +03:00
|
|
|
ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
|
2019-04-27 06:55:32 +03:00
|
|
|
F_NODE(nd_head, "in pattern");
|
2019-04-17 09:48:03 +03:00
|
|
|
F_NODE(nd_body, "in body");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_next, "next in clause");
|
|
|
|
return;
|
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_WHILE:
|
|
|
|
ANN("while statement");
|
|
|
|
ANN("format: while [nd_cond]; [nd_body]; end");
|
|
|
|
ANN("example: while x == 1; foo; end");
|
|
|
|
goto loop;
|
|
|
|
case NODE_UNTIL:
|
|
|
|
ANN("until statement");
|
|
|
|
ANN("format: until [nd_cond]; [nd_body]; end");
|
|
|
|
ANN("example: until x == 1; foo; end");
|
2009-12-09 18:46:45 +03:00
|
|
|
loop:
|
2015-10-08 04:45:00 +03:00
|
|
|
F_CUSTOM1(nd_state, "begin-end-while?") {
|
2015-10-08 04:44:37 +03:00
|
|
|
A_INT((int)node->nd_state);
|
|
|
|
A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
|
2015-10-08 04:45:00 +03:00
|
|
|
}
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_cond, "condition");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "body");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ITER:
|
|
|
|
ANN("method call with block");
|
|
|
|
ANN("format: [nd_iter] { [nd_body] }");
|
|
|
|
ANN("example: 3.times { foo }");
|
|
|
|
goto iter;
|
|
|
|
case NODE_FOR:
|
|
|
|
ANN("for statement");
|
|
|
|
ANN("format: for * in [nd_iter] do [nd_body] end");
|
|
|
|
ANN("example: for i in 1..3 do foo end");
|
2014-04-25 11:12:58 +04:00
|
|
|
iter:
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_iter, "iteration receiver");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "body");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2018-01-16 10:24:53 +03:00
|
|
|
case NODE_FOR_MASGN:
|
|
|
|
ANN("vars of for statement with masgn");
|
|
|
|
ANN("format: for [nd_var] in ... do ... end");
|
|
|
|
ANN("example: for x, y in 1..3 do foo end");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_var, "var");
|
|
|
|
return;
|
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_BREAK:
|
2017-12-15 08:55:08 +03:00
|
|
|
ANN("break statement");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("format: break [nd_stts]");
|
|
|
|
ANN("example: break 1");
|
|
|
|
goto jump;
|
|
|
|
case NODE_NEXT:
|
|
|
|
ANN("next statement");
|
|
|
|
ANN("format: next [nd_stts]");
|
|
|
|
ANN("example: next 1");
|
|
|
|
goto jump;
|
|
|
|
case NODE_RETURN:
|
|
|
|
ANN("return statement");
|
|
|
|
ANN("format: return [nd_stts]");
|
|
|
|
ANN("example: return 1");
|
2014-04-25 11:12:58 +04:00
|
|
|
jump:
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_stts, "value");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_REDO:
|
|
|
|
ANN("redo statement");
|
|
|
|
ANN("format: redo");
|
|
|
|
ANN("example: redo");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_RETRY:
|
|
|
|
ANN("retry statement");
|
|
|
|
ANN("format: retry");
|
|
|
|
ANN("example: retry");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_BEGIN:
|
|
|
|
ANN("begin statement");
|
|
|
|
ANN("format: begin; [nd_body]; end");
|
|
|
|
ANN("example: begin; 1; end");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "body");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_RESCUE:
|
|
|
|
ANN("rescue clause");
|
|
|
|
ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
|
|
|
|
ANN("example: begin; foo; rescue; bar; else; baz; end");
|
|
|
|
F_NODE(nd_head, "body");
|
|
|
|
F_NODE(nd_resq, "rescue clause list");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_else, "rescue else clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_RESBODY:
|
|
|
|
ANN("rescue clause (cont'd)");
|
|
|
|
ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
|
|
|
|
ANN("example: begin; foo; rescue; bar; else; baz; end");
|
|
|
|
F_NODE(nd_args, "rescue exceptions");
|
|
|
|
F_NODE(nd_body, "rescue clause");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_head, "next rescue clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ENSURE:
|
|
|
|
ANN("ensure clause");
|
|
|
|
ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
|
|
|
|
ANN("example: begin; foo; ensure; bar; end");
|
|
|
|
F_NODE(nd_head, "body");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_ensr, "ensure clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_AND:
|
|
|
|
ANN("&& operator");
|
|
|
|
ANN("format: [nd_1st] && [nd_2nd]");
|
|
|
|
ANN("example: foo && bar");
|
|
|
|
goto andor;
|
|
|
|
case NODE_OR:
|
|
|
|
ANN("|| operator");
|
|
|
|
ANN("format: [nd_1st] || [nd_2nd]");
|
2017-02-05 17:16:52 +03:00
|
|
|
ANN("example: foo || bar");
|
2014-04-25 11:12:58 +04:00
|
|
|
andor:
|
2017-02-08 04:18:56 +03:00
|
|
|
while (1) {
|
|
|
|
F_NODE(nd_1st, "left expr");
|
2021-12-03 18:01:24 +03:00
|
|
|
if (!node->nd_2nd || !nd_type_p(node->nd_2nd, type))
|
2017-02-08 04:18:56 +03:00
|
|
|
break;
|
|
|
|
node = node->nd_2nd;
|
|
|
|
}
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_2nd, "right expr");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_MASGN:
|
|
|
|
ANN("multiple assignment");
|
|
|
|
ANN("format: [nd_head], [nd_args] = [nd_value]");
|
|
|
|
ANN("example: a, b = foo");
|
|
|
|
F_NODE(nd_value, "rhsn");
|
|
|
|
F_NODE(nd_head, "lhsn");
|
2018-03-20 16:30:57 +03:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_args)) {
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "splatn");
|
|
|
|
}
|
|
|
|
else {
|
2017-11-04 17:24:16 +03:00
|
|
|
F_MSG(nd_args, "splatn", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_LASGN:
|
|
|
|
ANN("local variable assignment");
|
|
|
|
ANN("format: [nd_vid](lvar) = [nd_value]");
|
|
|
|
ANN("example: x = foo");
|
2017-12-15 08:55:11 +03:00
|
|
|
F_ID(nd_vid, "local variable");
|
2018-03-20 16:30:57 +03:00
|
|
|
if (NODE_REQUIRED_KEYWORD_P(node)) {
|
2017-12-15 08:55:11 +03:00
|
|
|
F_MSG(nd_value, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
|
|
|
}
|
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_DASGN:
|
2021-12-12 17:33:47 +03:00
|
|
|
ANN("dynamic variable assignment");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("format: [nd_vid](dvar) = [nd_value]");
|
|
|
|
ANN("example: x = nil; 1.times { x = foo }");
|
|
|
|
ANN("example: 1.times { x = foo }");
|
2017-12-15 08:55:11 +03:00
|
|
|
F_ID(nd_vid, "local variable");
|
2018-03-20 16:30:57 +03:00
|
|
|
if (NODE_REQUIRED_KEYWORD_P(node)) {
|
2018-01-07 15:55:10 +03:00
|
|
|
F_MSG(nd_value, "rvalue", "NODE_SPECIAL_REQUIRED_KEYWORD (required keyword argument)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
|
|
|
}
|
2017-12-15 08:55:11 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_IASGN:
|
|
|
|
ANN("instance variable assignment");
|
|
|
|
ANN("format: [nd_vid](ivar) = [nd_value]");
|
|
|
|
ANN("example: @x = foo");
|
2017-12-15 08:55:11 +03:00
|
|
|
F_ID(nd_vid, "instance variable");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_CVASGN:
|
|
|
|
ANN("class variable assignment");
|
|
|
|
ANN("format: [nd_vid](cvar) = [nd_value]");
|
|
|
|
ANN("example: @@x = foo");
|
2017-12-15 08:55:11 +03:00
|
|
|
F_ID(nd_vid, "class variable");
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
2017-12-15 08:55:11 +03:00
|
|
|
F_NODE(nd_value, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_GASGN:
|
|
|
|
ANN("global variable assignment");
|
|
|
|
ANN("format: [nd_entry](gvar) = [nd_value]");
|
|
|
|
ANN("example: $x = foo");
|
|
|
|
F_GENTRY(nd_entry, "global variable");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_CDECL:
|
|
|
|
ANN("constant declaration");
|
|
|
|
ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
|
|
|
|
ANN("example: X = foo");
|
|
|
|
if (node->nd_vid) {
|
2017-10-17 15:30:11 +03:00
|
|
|
F_ID(nd_vid, "constant");
|
|
|
|
F_MSG(nd_else, "extension", "not used");
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-10-17 15:30:11 +03:00
|
|
|
F_MSG(nd_vid, "constant", "0 (see extension field)");
|
|
|
|
F_NODE(nd_else, "extension");
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_OP_ASGN1:
|
|
|
|
ANN("array assignment with operator");
|
2018-06-08 02:18:16 +03:00
|
|
|
ANN("format: [nd_recv] [ [nd_args->nd_head] ] [nd_mid]= [nd_args->nd_body]");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("example: ary[1] += foo");
|
|
|
|
F_NODE(nd_recv, "receiver");
|
2018-01-09 04:49:40 +03:00
|
|
|
F_ID(nd_mid, "operator");
|
2017-02-26 12:33:17 +03:00
|
|
|
F_NODE(nd_args->nd_head, "index");
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
2017-02-26 12:33:17 +03:00
|
|
|
F_NODE(nd_args->nd_body, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_OP_ASGN2:
|
|
|
|
ANN("attr assignment with operator");
|
2018-06-08 02:18:16 +03:00
|
|
|
ANN("format: [nd_recv].[attr] [nd_next->nd_mid]= [nd_value]");
|
2015-10-12 06:39:23 +03:00
|
|
|
ANN(" where [attr]: [nd_next->nd_vid]");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("example: struct.field += foo");
|
|
|
|
F_NODE(nd_recv, "receiver");
|
2015-10-22 09:30:12 +03:00
|
|
|
F_CUSTOM1(nd_next->nd_vid, "attr") {
|
|
|
|
if (node->nd_next->nd_aid) A("? ");
|
|
|
|
A_ID(node->nd_next->nd_vid);
|
|
|
|
}
|
2018-01-09 04:49:40 +03:00
|
|
|
F_ID(nd_next->nd_mid, "operator");
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_OP_ASGN_AND:
|
|
|
|
ANN("assignment with && operator");
|
|
|
|
ANN("format: [nd_head] &&= [nd_value]");
|
|
|
|
ANN("example: foo &&= bar");
|
|
|
|
goto asgn_andor;
|
|
|
|
case NODE_OP_ASGN_OR:
|
|
|
|
ANN("assignment with || operator");
|
|
|
|
ANN("format: [nd_head] ||= [nd_value]");
|
|
|
|
ANN("example: foo ||= bar");
|
2014-04-25 11:12:58 +04:00
|
|
|
asgn_andor:
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_head, "variable");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2017-10-17 14:13:53 +03:00
|
|
|
case NODE_OP_CDECL:
|
|
|
|
ANN("constant declaration with operator");
|
|
|
|
ANN("format: [nd_head](constant) [nd_aid]= [nd_value]");
|
|
|
|
ANN("example: A::B ||= 1");
|
2017-10-17 14:38:16 +03:00
|
|
|
F_NODE(nd_head, "constant");
|
2018-01-09 04:49:40 +03:00
|
|
|
F_ID(nd_aid, "operator");
|
2017-10-17 14:13:53 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "rvalue");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2017-10-17 14:13:53 +03:00
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_CALL:
|
|
|
|
ANN("method invocation");
|
|
|
|
ANN("format: [nd_recv].[nd_mid]([nd_args])");
|
|
|
|
ANN("example: obj.foo(1)");
|
|
|
|
F_ID(nd_mid, "method id");
|
|
|
|
F_NODE(nd_recv, "receiver");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2018-05-10 12:49:55 +03:00
|
|
|
case NODE_OPCALL:
|
|
|
|
ANN("method invocation");
|
|
|
|
ANN("format: [nd_recv] [nd_mid] [nd_args]");
|
|
|
|
ANN("example: foo + bar");
|
|
|
|
F_ID(nd_mid, "method id");
|
|
|
|
F_NODE(nd_recv, "receiver");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "arguments");
|
|
|
|
return;
|
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_FCALL:
|
|
|
|
ANN("function call");
|
|
|
|
ANN("format: [nd_mid]([nd_args])");
|
|
|
|
ANN("example: foo(1)");
|
|
|
|
F_ID(nd_mid, "method id");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_VCALL:
|
|
|
|
ANN("function call with no argument");
|
|
|
|
ANN("format: [nd_mid]");
|
|
|
|
ANN("example: foo");
|
|
|
|
F_ID(nd_mid, "method id");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2015-12-05 10:58:11 +03:00
|
|
|
case NODE_QCALL:
|
|
|
|
ANN("safe method invocation");
|
|
|
|
ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
|
|
|
|
ANN("example: obj&.foo(1)");
|
|
|
|
F_ID(nd_mid, "method id");
|
|
|
|
F_NODE(nd_recv, "receiver");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2015-12-05 10:58:11 +03:00
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_SUPER:
|
|
|
|
ANN("super invocation");
|
|
|
|
ANN("format: super [nd_args]");
|
|
|
|
ANN("example: super 1");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ZSUPER:
|
|
|
|
ANN("super invocation with no argument");
|
|
|
|
ANN("format: super");
|
|
|
|
ANN("example: super");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2019-09-07 04:42:00 +03:00
|
|
|
case NODE_LIST:
|
|
|
|
ANN("list constructor");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
|
|
|
|
ANN("example: [1, 2, 3]");
|
|
|
|
goto ary;
|
|
|
|
case NODE_VALUES:
|
|
|
|
ANN("return arguments");
|
|
|
|
ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
|
|
|
|
ANN("example: return 1, 2, 3");
|
2014-04-25 11:12:58 +04:00
|
|
|
ary:
|
2016-08-15 14:31:36 +03:00
|
|
|
dump_array(buf, indent, comment, node);
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2019-09-07 04:42:00 +03:00
|
|
|
case NODE_ZLIST:
|
|
|
|
ANN("empty list constructor");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("format: []");
|
|
|
|
ANN("example: []");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_HASH:
|
2019-03-15 08:19:58 +03:00
|
|
|
if (!node->nd_brace) {
|
2017-08-11 04:27:24 +03:00
|
|
|
ANN("keyword arguments");
|
|
|
|
ANN("format: nd_head");
|
|
|
|
ANN("example: a: 1, b: 2");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ANN("hash constructor");
|
|
|
|
ANN("format: { [nd_head] }");
|
|
|
|
ANN("example: { 1 => 2, 3 => 4 }");
|
|
|
|
}
|
2019-03-15 08:19:58 +03:00
|
|
|
F_CUSTOM1(nd_brace, "keyword arguments or hash literal") {
|
|
|
|
switch (node->nd_brace) {
|
2017-11-09 04:50:58 +03:00
|
|
|
case 0: A("0 (keyword argument)"); break;
|
|
|
|
case 1: A("1 (hash literal)"); break;
|
|
|
|
}
|
|
|
|
}
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_head, "contents");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_YIELD:
|
|
|
|
ANN("yield invocation");
|
|
|
|
ANN("format: yield [nd_head]");
|
|
|
|
ANN("example: yield 1");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_head, "arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_LVAR:
|
|
|
|
ANN("local variable reference");
|
|
|
|
ANN("format: [nd_vid](lvar)");
|
|
|
|
ANN("example: x");
|
2017-10-17 14:38:16 +03:00
|
|
|
F_ID(nd_vid, "local variable");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_DVAR:
|
|
|
|
ANN("dynamic variable reference");
|
|
|
|
ANN("format: [nd_vid](dvar)");
|
|
|
|
ANN("example: 1.times { x = 1; x }");
|
2017-10-17 14:38:16 +03:00
|
|
|
F_ID(nd_vid, "local variable");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_IVAR:
|
|
|
|
ANN("instance variable reference");
|
|
|
|
ANN("format: [nd_vid](ivar)");
|
|
|
|
ANN("example: @x");
|
2017-10-17 14:38:16 +03:00
|
|
|
F_ID(nd_vid, "instance variable");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_CONST:
|
|
|
|
ANN("constant reference");
|
|
|
|
ANN("format: [nd_vid](constant)");
|
|
|
|
ANN("example: X");
|
2017-10-17 14:38:16 +03:00
|
|
|
F_ID(nd_vid, "constant");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_CVAR:
|
|
|
|
ANN("class variable reference");
|
|
|
|
ANN("format: [nd_vid](cvar)");
|
|
|
|
ANN("example: @@x");
|
2017-10-17 14:38:16 +03:00
|
|
|
F_ID(nd_vid, "class variable");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_GVAR:
|
|
|
|
ANN("global variable reference");
|
|
|
|
ANN("format: [nd_entry](gvar)");
|
|
|
|
ANN("example: $x");
|
|
|
|
F_GENTRY(nd_entry, "global variable");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_NTH_REF:
|
|
|
|
ANN("nth special variable reference");
|
|
|
|
ANN("format: $[nd_nth]");
|
|
|
|
ANN("example: $1, $2, ..");
|
2015-10-08 04:45:00 +03:00
|
|
|
F_CUSTOM1(nd_nth, "variable") { A("$"); A_LONG(node->nd_nth); }
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_BACK_REF:
|
|
|
|
ANN("back special variable reference");
|
|
|
|
ANN("format: $[nd_nth]");
|
|
|
|
ANN("example: $&, $`, $', $+");
|
2015-10-08 04:45:00 +03:00
|
|
|
F_CUSTOM1(nd_nth, "variable") {
|
2021-11-17 17:38:19 +03:00
|
|
|
char name[3] = "$ ";
|
2015-10-08 04:44:37 +03:00
|
|
|
name[1] = (char)node->nd_nth;
|
|
|
|
A(name);
|
2015-10-08 04:45:00 +03:00
|
|
|
}
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_MATCH:
|
|
|
|
ANN("match expression (against $_ implicitly)");
|
|
|
|
ANN("format: [nd_lit] (in condition)");
|
|
|
|
ANN("example: if /foo/; foo; end");
|
|
|
|
F_LIT(nd_lit, "regexp");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_MATCH2:
|
|
|
|
ANN("match expression (regexp first)");
|
|
|
|
ANN("format: [nd_recv] =~ [nd_value]");
|
|
|
|
ANN("example: /foo/ =~ 'foo'");
|
|
|
|
F_NODE(nd_recv, "regexp (receiver)");
|
2016-03-14 10:53:39 +03:00
|
|
|
if (!node->nd_args) LAST_NODE;
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_value, "string (argument)");
|
2016-03-14 10:53:39 +03:00
|
|
|
if (node->nd_args) {
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "named captures");
|
|
|
|
}
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_MATCH3:
|
|
|
|
ANN("match expression (regexp second)");
|
|
|
|
ANN("format: [nd_recv] =~ [nd_value]");
|
|
|
|
ANN("example: 'foo' =~ /foo/");
|
|
|
|
F_NODE(nd_recv, "string (receiver)");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_value, "regexp (argument)");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_LIT:
|
|
|
|
ANN("literal");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: 1, /foo/");
|
|
|
|
goto lit;
|
|
|
|
case NODE_STR:
|
|
|
|
ANN("string literal");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: 'foo'");
|
|
|
|
goto lit;
|
|
|
|
case NODE_XSTR:
|
|
|
|
ANN("xstring literal");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: `foo`");
|
2014-04-25 11:12:58 +04:00
|
|
|
lit:
|
2009-12-09 17:05:59 +03:00
|
|
|
F_LIT(nd_lit, "literal");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2018-01-04 15:47:22 +03:00
|
|
|
case NODE_ONCE:
|
|
|
|
ANN("once evaluation");
|
|
|
|
ANN("format: [nd_body]");
|
|
|
|
ANN("example: /foo#{ bar }baz/o");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "body");
|
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_DSTR:
|
|
|
|
ANN("string literal with interpolation");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: \"foo#{ bar }baz\"");
|
|
|
|
goto dlit;
|
|
|
|
case NODE_DXSTR:
|
|
|
|
ANN("xstring literal with interpolation");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: `foo#{ bar }baz`");
|
|
|
|
goto dlit;
|
|
|
|
case NODE_DREGX:
|
|
|
|
ANN("regexp literal with interpolation");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: /foo#{ bar }baz/");
|
|
|
|
goto dlit;
|
|
|
|
case NODE_DSYM:
|
|
|
|
ANN("symbol literal with interpolation");
|
|
|
|
ANN("format: [nd_lit]");
|
|
|
|
ANN("example: :\"foo#{ bar }baz\"");
|
2014-04-25 11:12:58 +04:00
|
|
|
dlit:
|
2015-10-09 09:44:22 +03:00
|
|
|
F_LIT(nd_lit, "preceding string");
|
2020-09-02 17:12:22 +03:00
|
|
|
if (!node->nd_next) return;
|
2015-10-09 09:44:22 +03:00
|
|
|
F_NODE(nd_next->nd_head, "interpolation");
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
2015-10-09 09:44:22 +03:00
|
|
|
F_NODE(nd_next->nd_next, "tailing strings");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_EVSTR:
|
|
|
|
ANN("interpolation expression");
|
|
|
|
ANN("format: \"..#{ [nd_lit] }..\"");
|
|
|
|
ANN("example: \"foo#{ bar }baz\"");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "body");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ARGSCAT:
|
|
|
|
ANN("splat argument following arguments");
|
|
|
|
ANN("format: ..(*[nd_head], [nd_body..])");
|
|
|
|
ANN("example: foo(*ary, post_arg1, post_arg2)");
|
|
|
|
F_NODE(nd_head, "preceding array");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "following array");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ARGSPUSH:
|
|
|
|
ANN("splat argument following one argument");
|
|
|
|
ANN("format: ..(*[nd_head], [nd_body])");
|
|
|
|
ANN("example: foo(*ary, post_arg)");
|
|
|
|
F_NODE(nd_head, "preceding array");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "following element");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_SPLAT:
|
|
|
|
ANN("splat argument");
|
|
|
|
ANN("format: *[nd_head]");
|
|
|
|
ANN("example: foo(*ary)");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_head, "splat'ed array");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_BLOCK_PASS:
|
|
|
|
ANN("arguments with block argument");
|
|
|
|
ANN("format: ..([nd_head], &[nd_body])");
|
|
|
|
ANN("example: foo(x, &blk)");
|
|
|
|
F_NODE(nd_head, "other arguments");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "block argument");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_DEFN:
|
|
|
|
ANN("method definition");
|
|
|
|
ANN("format: def [nd_mid] [nd_defn]; end");
|
2018-05-10 03:23:23 +03:00
|
|
|
ANN("example: def foo; bar; end");
|
2009-12-09 17:05:59 +03:00
|
|
|
F_ID(nd_mid, "method name");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_defn, "method definition");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_DEFS:
|
|
|
|
ANN("singleton method definition");
|
|
|
|
ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
|
2018-05-10 03:23:23 +03:00
|
|
|
ANN("example: def obj.foo; bar; end");
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_recv, "receiver");
|
|
|
|
F_ID(nd_mid, "method name");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_defn, "method definition");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ALIAS:
|
|
|
|
ANN("method alias statement");
|
2018-06-28 16:22:13 +03:00
|
|
|
ANN("format: alias [nd_1st] [nd_2nd]");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("example: alias bar foo");
|
2017-11-16 14:03:37 +03:00
|
|
|
F_NODE(nd_1st, "new name");
|
2009-12-09 17:05:59 +03:00
|
|
|
LAST_NODE;
|
2017-11-16 14:03:37 +03:00
|
|
|
F_NODE(nd_2nd, "old name");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_VALIAS:
|
|
|
|
ANN("global variable alias statement");
|
2018-06-28 16:22:13 +03:00
|
|
|
ANN("format: alias [nd_alias](gvar) [nd_orig](gvar)");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("example: alias $y $x");
|
2017-11-16 14:03:37 +03:00
|
|
|
F_ID(nd_alias, "new name");
|
|
|
|
F_ID(nd_orig, "old name");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_UNDEF:
|
2018-09-16 06:24:15 +03:00
|
|
|
ANN("method undef statement");
|
2018-06-28 16:22:13 +03:00
|
|
|
ANN("format: undef [nd_undef]");
|
2009-12-09 17:05:59 +03:00
|
|
|
ANN("example: undef foo");
|
|
|
|
LAST_NODE;
|
2017-11-16 14:03:37 +03:00
|
|
|
F_NODE(nd_undef, "old name");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_CLASS:
|
|
|
|
ANN("class definition");
|
|
|
|
ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
|
|
|
|
ANN("example: class C2 < C; ..; end");
|
|
|
|
F_NODE(nd_cpath, "class path");
|
|
|
|
F_NODE(nd_super, "superclass");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "class definition");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_MODULE:
|
|
|
|
ANN("module definition");
|
|
|
|
ANN("format: module [nd_cpath]; [nd_body]; end");
|
|
|
|
ANN("example: module M; ..; end");
|
|
|
|
F_NODE(nd_cpath, "module path");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "module definition");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_SCLASS:
|
|
|
|
ANN("singleton class definition");
|
|
|
|
ANN("format: class << [nd_recv]; [nd_body]; end");
|
|
|
|
ANN("example: class << obj; ..; end");
|
|
|
|
F_NODE(nd_recv, "receiver");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "singleton class definition");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_COLON2:
|
|
|
|
ANN("scoped constant reference");
|
|
|
|
ANN("format: [nd_head]::[nd_mid]");
|
|
|
|
ANN("example: M::C");
|
|
|
|
F_ID(nd_mid, "constant name");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_head, "receiver");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_COLON3:
|
|
|
|
ANN("top-level constant reference");
|
|
|
|
ANN("format: ::[nd_mid]");
|
|
|
|
ANN("example: ::Object");
|
|
|
|
F_ID(nd_mid, "constant name");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_DOT2:
|
|
|
|
ANN("range constructor (incl.)");
|
|
|
|
ANN("format: [nd_beg]..[nd_end]");
|
|
|
|
ANN("example: 1..5");
|
|
|
|
goto dot;
|
|
|
|
case NODE_DOT3:
|
|
|
|
ANN("range constructor (excl.)");
|
|
|
|
ANN("format: [nd_beg]...[nd_end]");
|
|
|
|
ANN("example: 1...5");
|
|
|
|
goto dot;
|
|
|
|
case NODE_FLIP2:
|
|
|
|
ANN("flip-flop condition (incl.)");
|
|
|
|
ANN("format: [nd_beg]..[nd_end]");
|
|
|
|
ANN("example: if (x==1)..(x==5); foo; end");
|
|
|
|
goto dot;
|
|
|
|
case NODE_FLIP3:
|
|
|
|
ANN("flip-flop condition (excl.)");
|
|
|
|
ANN("format: [nd_beg]...[nd_end]");
|
|
|
|
ANN("example: if (x==1)...(x==5); foo; end");
|
2014-04-25 11:12:58 +04:00
|
|
|
dot:
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_beg, "begin");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_end, "end");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_SELF:
|
|
|
|
ANN("self");
|
|
|
|
ANN("format: self");
|
|
|
|
ANN("example: self");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_NIL:
|
|
|
|
ANN("nil");
|
|
|
|
ANN("format: nil");
|
|
|
|
ANN("example: nil");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_TRUE:
|
|
|
|
ANN("true");
|
|
|
|
ANN("format: true");
|
|
|
|
ANN("example: true");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_FALSE:
|
|
|
|
ANN("false");
|
|
|
|
ANN("format: false");
|
|
|
|
ANN("example: false");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ERRINFO:
|
|
|
|
ANN("virtual reference to $!");
|
|
|
|
ANN("format: rescue => id");
|
|
|
|
ANN("example: rescue => id");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_DEFINED:
|
|
|
|
ANN("defined? expression");
|
|
|
|
ANN("format: defined?([nd_head])");
|
|
|
|
ANN("example: defined?(foo)");
|
|
|
|
F_NODE(nd_head, "expr");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_POSTEXE:
|
|
|
|
ANN("post-execution");
|
|
|
|
ANN("format: END { [nd_body] }");
|
|
|
|
ANN("example: END { foo }");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "END clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ATTRASGN:
|
|
|
|
ANN("attr assignment");
|
|
|
|
ANN("format: [nd_recv].[nd_mid] = [nd_args]");
|
|
|
|
ANN("example: struct.field = foo");
|
2017-10-24 08:54:30 +03:00
|
|
|
F_NODE(nd_recv, "receiver");
|
2009-12-09 17:05:59 +03:00
|
|
|
F_ID(nd_mid, "method name");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_args, "arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_LAMBDA:
|
|
|
|
ANN("lambda expression");
|
|
|
|
ANN("format: -> [nd_body]");
|
|
|
|
ANN("example: -> { foo }");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "lambda clause");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_OPT_ARG:
|
|
|
|
ANN("optional arguments");
|
|
|
|
ANN("format: def method_name([nd_body=some], [nd_next..])");
|
|
|
|
ANN("example: def foo(a, b=1, c); end");
|
|
|
|
F_NODE(nd_body, "body");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_next, "next");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2011-12-26 18:20:03 +04:00
|
|
|
case NODE_KW_ARG:
|
|
|
|
ANN("keyword arguments");
|
|
|
|
ANN("format: def method_name([nd_body=some], [nd_next..])");
|
|
|
|
ANN("example: def foo(a:1, b:2); end");
|
|
|
|
F_NODE(nd_body, "body");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_next, "next");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2011-12-26 18:20:03 +04:00
|
|
|
|
2009-12-09 17:05:59 +03:00
|
|
|
case NODE_POSTARG:
|
|
|
|
ANN("post arguments");
|
|
|
|
ANN("format: *[nd_1st], [nd_2nd..] = ..");
|
|
|
|
ANN("example: a, *rest, z = foo");
|
2018-03-20 16:30:57 +03:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_1st)) {
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_1st, "rest argument");
|
|
|
|
}
|
|
|
|
else {
|
2017-11-04 17:24:16 +03:00
|
|
|
F_MSG(nd_1st, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_2nd, "post arguments");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_ARGS:
|
|
|
|
ANN("method parameters");
|
2021-11-17 17:38:52 +03:00
|
|
|
ANN("format: def method_name(.., [nd_ainfo->nd_optargs], *[nd_ainfo->rest_arg], [nd_ainfo->first_post_arg], .., [nd_ainfo->kw_args], **[nd_ainfo->kw_rest_arg], &[nd_ainfo->block_arg])");
|
|
|
|
ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, kw: 1, **kwrest, &blk); end");
|
2011-12-27 16:17:36 +04:00
|
|
|
F_INT(nd_ainfo->pre_args_num, "count of mandatory (pre-)arguments");
|
2011-12-26 18:19:58 +04:00
|
|
|
F_NODE(nd_ainfo->pre_init, "initialization of (pre-)arguments");
|
2011-12-27 16:17:36 +04:00
|
|
|
F_INT(nd_ainfo->post_args_num, "count of mandatory post-arguments");
|
2011-12-26 18:19:58 +04:00
|
|
|
F_NODE(nd_ainfo->post_init, "initialization of post-arguments");
|
|
|
|
F_ID(nd_ainfo->first_post_arg, "first post argument");
|
2019-06-04 17:11:38 +03:00
|
|
|
F_CUSTOM1(nd_ainfo->rest_arg, "rest argument") {
|
2019-06-05 08:03:50 +03:00
|
|
|
if (node->nd_ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA) {
|
2019-06-04 17:17:38 +03:00
|
|
|
A("1 (excessed comma)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
A_ID(node->nd_ainfo->rest_arg);
|
|
|
|
}
|
|
|
|
}
|
2011-12-26 18:19:58 +04:00
|
|
|
F_ID(nd_ainfo->block_arg, "block argument");
|
|
|
|
F_NODE(nd_ainfo->opt_args, "optional arguments");
|
2011-12-26 18:20:03 +04:00
|
|
|
F_NODE(nd_ainfo->kw_args, "keyword arguments");
|
2017-02-27 16:52:08 +03:00
|
|
|
LAST_NODE;
|
2012-03-13 17:00:17 +04:00
|
|
|
F_NODE(nd_ainfo->kw_rest_arg, "keyword rest argument");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
|
|
|
case NODE_SCOPE:
|
|
|
|
ANN("new scope");
|
|
|
|
ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
|
2015-10-08 04:45:00 +03:00
|
|
|
F_CUSTOM1(nd_tbl, "local table") {
|
2021-11-17 21:40:49 +03:00
|
|
|
rb_ast_id_table_t *tbl = node->nd_tbl;
|
2015-10-08 04:44:37 +03:00
|
|
|
int i;
|
2021-11-17 21:40:49 +03:00
|
|
|
int size = tbl ? tbl->size : 0;
|
2015-10-08 04:44:37 +03:00
|
|
|
if (size == 0) A("(empty)");
|
|
|
|
for (i = 0; i < size; i++) {
|
2021-11-17 21:40:49 +03:00
|
|
|
A_ID(tbl->ids[i]); if (i < size - 1) A(",");
|
2015-10-08 04:44:37 +03:00
|
|
|
}
|
2015-10-08 04:45:00 +03:00
|
|
|
}
|
2009-12-09 17:05:59 +03:00
|
|
|
F_NODE(nd_args, "arguments");
|
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_body, "body");
|
2017-10-17 15:30:12 +03:00
|
|
|
return;
|
2019-04-17 09:48:03 +03:00
|
|
|
|
|
|
|
case NODE_ARYPTN:
|
|
|
|
ANN("array pattern");
|
|
|
|
ANN("format: [nd_pconst]([pre_args], ..., *[rest_arg], [post_args], ...)");
|
|
|
|
F_NODE(nd_pconst, "constant");
|
|
|
|
F_NODE(nd_apinfo->pre_args, "pre arguments");
|
2019-04-20 06:37:22 +03:00
|
|
|
if (NODE_NAMED_REST_P(node->nd_apinfo->rest_arg)) {
|
|
|
|
F_NODE(nd_apinfo->rest_arg, "rest argument");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
F_MSG(nd_apinfo->rest_arg, "rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
|
|
|
|
}
|
2019-04-17 09:48:03 +03:00
|
|
|
LAST_NODE;
|
|
|
|
F_NODE(nd_apinfo->post_args, "post arguments");
|
|
|
|
return;
|
|
|
|
|
2020-06-14 03:24:36 +03:00
|
|
|
case NODE_FNDPTN:
|
|
|
|
ANN("find pattern");
|
|
|
|
ANN("format: [nd_pconst](*[pre_rest_arg], args, ..., *[post_rest_arg])");
|
|
|
|
F_NODE(nd_pconst, "constant");
|
|
|
|
if (NODE_NAMED_REST_P(node->nd_fpinfo->pre_rest_arg)) {
|
|
|
|
F_NODE(nd_fpinfo->pre_rest_arg, "pre rest argument");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
F_MSG(nd_fpinfo->pre_rest_arg, "pre rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
|
|
|
|
}
|
|
|
|
F_NODE(nd_fpinfo->args, "arguments");
|
|
|
|
|
|
|
|
LAST_NODE;
|
|
|
|
if (NODE_NAMED_REST_P(node->nd_fpinfo->post_rest_arg)) {
|
|
|
|
F_NODE(nd_fpinfo->post_rest_arg, "post rest argument");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
F_MSG(nd_fpinfo->post_rest_arg, "post rest argument", "NODE_SPECIAL_NO_NAME_REST (rest argument without name)");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
2019-04-17 09:48:03 +03:00
|
|
|
case NODE_HSHPTN:
|
|
|
|
ANN("hash pattern");
|
|
|
|
ANN("format: [nd_pconst]([nd_pkwargs], ..., **[nd_pkwrestarg])");
|
|
|
|
F_NODE(nd_pconst, "constant");
|
|
|
|
F_NODE(nd_pkwargs, "keyword arguments");
|
|
|
|
LAST_NODE;
|
2019-09-01 10:39:34 +03:00
|
|
|
if (node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
|
|
|
|
F_MSG(nd_pkwrestarg, "keyword rest argument", "NODE_SPECIAL_NO_REST_KEYWORD (**nil)");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
F_NODE(nd_pkwrestarg, "keyword rest argument");
|
|
|
|
}
|
2019-04-17 09:48:03 +03:00
|
|
|
return;
|
2009-12-09 17:05:59 +03:00
|
|
|
|
2017-10-17 15:30:12 +03:00
|
|
|
case NODE_ARGS_AUX:
|
|
|
|
case NODE_LAST:
|
|
|
|
break;
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
2017-10-17 15:30:12 +03:00
|
|
|
|
|
|
|
rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
|
2009-12-09 17:05:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2018-01-05 10:47:51 +03:00
|
|
|
rb_parser_dump_tree(const NODE *node, int comment)
|
2009-12-09 17:05:59 +03:00
|
|
|
{
|
|
|
|
VALUE buf = rb_str_new_cstr(
|
|
|
|
"###########################################################\n"
|
|
|
|
"## Do NOT use this node dump for any purpose other than ##\n"
|
|
|
|
"## debug and research. Compatibility is not guaranteed. ##\n"
|
|
|
|
"###########################################################\n\n"
|
|
|
|
);
|
|
|
|
dump_node(buf, rb_str_new_cstr("# "), comment, node);
|
|
|
|
return buf;
|
|
|
|
}
|
2014-08-16 05:53:10 +04:00
|
|
|
|
2018-01-17 16:15:13 +03:00
|
|
|
/* Setup NODE structure.
|
2017-11-04 13:02:43 +03:00
|
|
|
* NODE is not an object managed by GC, but it imitates an object
|
|
|
|
* so that it can work with `RB_TYPE_P(obj, T_NODE)`.
|
|
|
|
* This dirty hack is needed because Ripper jumbles NODEs and other type
|
|
|
|
* objects.
|
|
|
|
*/
|
2017-10-24 09:16:31 +03:00
|
|
|
void
|
|
|
|
rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
|
|
|
|
{
|
|
|
|
n->flags = T_NODE;
|
2021-01-14 09:42:45 +03:00
|
|
|
nd_init_type(n, type);
|
2017-10-24 09:16:31 +03:00
|
|
|
n->u1.value = a0;
|
|
|
|
n->u2.value = a1;
|
|
|
|
n->u3.value = a2;
|
2018-01-09 11:45:35 +03:00
|
|
|
n->nd_loc.beg_pos.lineno = 0;
|
|
|
|
n->nd_loc.beg_pos.column = 0;
|
|
|
|
n->nd_loc.end_pos.lineno = 0;
|
|
|
|
n->nd_loc.end_pos.column = 0;
|
2022-07-29 21:12:39 +03:00
|
|
|
n->node_id = -1;
|
2017-10-24 09:16:31 +03:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
typedef struct node_buffer_elem_struct {
|
|
|
|
struct node_buffer_elem_struct *next;
|
2019-09-09 19:37:06 +03:00
|
|
|
long len;
|
2018-01-16 18:12:09 +03:00
|
|
|
NODE buf[FLEX_ARY_LEN];
|
2017-10-27 19:44:57 +03:00
|
|
|
} node_buffer_elem_t;
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
typedef struct {
|
2017-10-27 19:44:57 +03:00
|
|
|
long idx, len;
|
|
|
|
node_buffer_elem_t *head;
|
2018-01-16 18:12:09 +03:00
|
|
|
node_buffer_elem_t *last;
|
2019-09-09 19:37:06 +03:00
|
|
|
} node_buffer_list_t;
|
|
|
|
|
|
|
|
struct node_buffer_struct {
|
|
|
|
node_buffer_list_t unmarkable;
|
|
|
|
node_buffer_list_t markable;
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link *local_tables;
|
2019-11-05 03:04:58 +03:00
|
|
|
VALUE mark_hash;
|
2017-10-28 00:08:37 +03:00
|
|
|
};
|
2017-10-27 19:44:57 +03:00
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static void
|
|
|
|
init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
nb->idx = 0;
|
2019-04-23 01:24:52 +03:00
|
|
|
nb->len = NODE_BUF_DEFAULT_LEN;
|
2019-09-09 19:37:06 +03:00
|
|
|
nb->head = nb->last = head;
|
|
|
|
nb->head->len = nb->len;
|
2017-10-27 19:44:57 +03:00
|
|
|
nb->head->next = NULL;
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static node_buffer_t *
|
|
|
|
rb_node_buffer_new(void)
|
|
|
|
{
|
2019-10-07 10:56:08 +03:00
|
|
|
const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
|
|
|
|
const size_t alloc_size = sizeof(node_buffer_t) + (bucket_size * 2);
|
|
|
|
STATIC_ASSERT(
|
|
|
|
integer_overflow,
|
|
|
|
offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE)
|
|
|
|
> sizeof(node_buffer_t) + 2 * sizeof(node_buffer_elem_t));
|
|
|
|
node_buffer_t *nb = ruby_xmalloc(alloc_size);
|
2019-09-09 19:37:06 +03:00
|
|
|
init_node_buffer_list(&nb->unmarkable, (node_buffer_elem_t*)&nb[1]);
|
|
|
|
init_node_buffer_list(&nb->markable, (node_buffer_elem_t*)((size_t)nb->unmarkable.head + bucket_size));
|
2020-07-24 02:00:28 +03:00
|
|
|
nb->local_tables = 0;
|
2019-11-05 03:04:58 +03:00
|
|
|
nb->mark_hash = Qnil;
|
2017-10-27 19:44:57 +03:00
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
2017-11-16 11:19:46 +03:00
|
|
|
static void
|
2019-09-09 19:37:06 +03:00
|
|
|
node_buffer_list_free(node_buffer_list_t * nb)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
node_buffer_elem_t *nbe = nb->head;
|
|
|
|
|
2018-01-16 18:12:09 +03:00
|
|
|
while (nbe != nb->last) {
|
2017-10-27 19:44:57 +03:00
|
|
|
void *buf = nbe;
|
|
|
|
nbe = nbe->next;
|
|
|
|
xfree(buf);
|
|
|
|
}
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link {
|
|
|
|
struct rb_ast_local_table_link *next;
|
|
|
|
// struct rb_ast_id_table {
|
|
|
|
int size;
|
|
|
|
ID ids[FLEX_ARY_LEN];
|
|
|
|
// }
|
|
|
|
};
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static void
|
|
|
|
rb_node_buffer_free(node_buffer_t *nb)
|
|
|
|
{
|
|
|
|
node_buffer_list_free(&nb->unmarkable);
|
|
|
|
node_buffer_list_free(&nb->markable);
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link *local_table = nb->local_tables;
|
2020-07-24 02:00:28 +03:00
|
|
|
while (local_table) {
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link *next_table = local_table->next;
|
2020-07-24 02:00:28 +03:00
|
|
|
xfree(local_table);
|
|
|
|
local_table = next_table;
|
|
|
|
}
|
2017-10-27 19:44:57 +03:00
|
|
|
xfree(nb);
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static NODE *
|
|
|
|
ast_newnode_in_bucket(node_buffer_list_t *nb)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
if (nb->idx >= nb->len) {
|
|
|
|
long n = nb->len * 2;
|
|
|
|
node_buffer_elem_t *nbe;
|
2019-10-07 10:56:08 +03:00
|
|
|
nbe = rb_xmalloc_mul_add(n, sizeof(NODE), offsetof(node_buffer_elem_t, buf));
|
2019-09-09 19:37:06 +03:00
|
|
|
nbe->len = n;
|
2017-10-27 19:44:57 +03:00
|
|
|
nb->idx = 0;
|
|
|
|
nb->len = n;
|
|
|
|
nbe->next = nb->head;
|
|
|
|
nb->head = nbe;
|
|
|
|
}
|
|
|
|
return &nb->head->buf[nb->idx++];
|
|
|
|
}
|
|
|
|
|
2021-01-14 09:42:45 +03:00
|
|
|
RBIMPL_ATTR_PURE()
|
|
|
|
static bool
|
|
|
|
nodetype_markable_p(enum node_type type)
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
|
|
|
switch (type) {
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_MATCH:
|
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DSYM:
|
|
|
|
case NODE_ARGS:
|
|
|
|
case NODE_ARYPTN:
|
2020-06-14 03:24:36 +03:00
|
|
|
case NODE_FNDPTN:
|
2021-01-14 09:42:45 +03:00
|
|
|
return true;
|
2019-09-27 04:20:56 +03:00
|
|
|
default:
|
2021-01-14 09:42:45 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
rb_ast_newnode(rb_ast_t *ast, enum node_type type)
|
|
|
|
{
|
|
|
|
node_buffer_t *nb = ast->node_buffer;
|
|
|
|
node_buffer_list_t *bucket =
|
|
|
|
(nodetype_markable_p(type) ? &nb->markable : &nb->unmarkable);
|
|
|
|
return ast_newnode_in_bucket(bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ast_node_type_change(NODE *n, enum node_type type)
|
|
|
|
{
|
|
|
|
enum node_type old_type = nd_type(n);
|
|
|
|
if (nodetype_markable_p(old_type) != nodetype_markable_p(type)) {
|
|
|
|
rb_bug("node type changed: %s -> %s",
|
|
|
|
ruby_node_name(old_type), ruby_node_name(type));
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:40:49 +03:00
|
|
|
rb_ast_id_table_t *
|
|
|
|
rb_ast_new_local_table(rb_ast_t *ast, int size)
|
|
|
|
{
|
|
|
|
size_t alloc_size = sizeof(struct rb_ast_local_table_link) + size * sizeof(ID);
|
|
|
|
struct rb_ast_local_table_link *link = ruby_xmalloc(alloc_size);
|
|
|
|
link->next = ast->node_buffer->local_tables;
|
|
|
|
ast->node_buffer->local_tables = link;
|
|
|
|
link->size = size;
|
|
|
|
|
|
|
|
return (rb_ast_id_table_t *) &link->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_ast_id_table_t *
|
|
|
|
rb_ast_resize_latest_local_table(rb_ast_t *ast, int size)
|
2020-07-24 02:00:28 +03:00
|
|
|
{
|
2021-11-17 21:40:49 +03:00
|
|
|
struct rb_ast_local_table_link *link = ast->node_buffer->local_tables;
|
|
|
|
size_t alloc_size = sizeof(struct rb_ast_local_table_link) + size * sizeof(ID);
|
|
|
|
link = ruby_xrealloc(link, alloc_size);
|
|
|
|
ast->node_buffer->local_tables = link;
|
|
|
|
link->size = size;
|
|
|
|
|
|
|
|
return (rb_ast_id_table_t *) &link->size;
|
2020-07-24 02:00:28 +03:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_delete_node(rb_ast_t *ast, NODE *n)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
(void)ast;
|
|
|
|
(void)n;
|
|
|
|
/* should we implement freelist? */
|
|
|
|
}
|
|
|
|
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_t *
|
2017-10-27 19:44:57 +03:00
|
|
|
rb_ast_new(void)
|
|
|
|
{
|
2018-08-22 13:38:55 +03:00
|
|
|
node_buffer_t *nb = rb_node_buffer_new();
|
2021-04-27 09:31:19 +03:00
|
|
|
rb_ast_t *ast = (rb_ast_t *)rb_imemo_new(imemo_ast, 0, 0, 0, (VALUE)nb);
|
2018-08-22 13:38:55 +03:00
|
|
|
return ast;
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
typedef void node_itr_t(void *ctx, NODE * node);
|
|
|
|
|
|
|
|
static void
|
|
|
|
iterate_buffer_elements(node_buffer_elem_t *nbe, long len, node_itr_t *func, void *ctx)
|
|
|
|
{
|
|
|
|
long cursor;
|
|
|
|
for (cursor = 0; cursor < len; cursor++) {
|
|
|
|
func(ctx, &nbe->buf[cursor]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iterate_node_values(node_buffer_list_t *nb, node_itr_t * func, void *ctx)
|
|
|
|
{
|
|
|
|
node_buffer_elem_t *nbe = nb->head;
|
|
|
|
|
|
|
|
/* iterate over the head first because it's not full */
|
|
|
|
iterate_buffer_elements(nbe, nb->idx, func, ctx);
|
|
|
|
|
|
|
|
nbe = nbe->next;
|
|
|
|
while (nbe) {
|
|
|
|
iterate_buffer_elements(nbe, nbe->len, func, ctx);
|
|
|
|
nbe = nbe->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mark_ast_value(void *ctx, NODE * node)
|
|
|
|
{
|
|
|
|
switch (nd_type(node)) {
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_ARGS:
|
2019-09-09 19:37:06 +03:00
|
|
|
{
|
|
|
|
struct rb_args_info *args = node->nd_ainfo;
|
2019-09-10 01:46:07 +03:00
|
|
|
rb_gc_mark_movable(args->imemo);
|
2019-09-09 19:37:06 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_MATCH:
|
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DSYM:
|
2020-11-01 10:19:07 +03:00
|
|
|
rb_gc_mark_movable(node->nd_lit);
|
|
|
|
break;
|
2020-08-01 19:03:43 +03:00
|
|
|
case NODE_ARYPTN:
|
|
|
|
case NODE_FNDPTN:
|
2020-11-01 10:19:07 +03:00
|
|
|
rb_gc_mark_movable(node->nd_rval);
|
2019-09-27 04:20:56 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("unreachable node %s", ruby_node_name(nd_type(node)));
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:46:07 +03:00
|
|
|
static void
|
|
|
|
update_ast_value(void *ctx, NODE * node)
|
|
|
|
{
|
|
|
|
switch (nd_type(node)) {
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_ARGS:
|
2019-09-10 01:46:07 +03:00
|
|
|
{
|
|
|
|
struct rb_args_info *args = node->nd_ainfo;
|
|
|
|
args->imemo = rb_gc_location(args->imemo);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-28 22:56:36 +03:00
|
|
|
case NODE_MATCH:
|
2019-09-27 04:20:56 +03:00
|
|
|
case NODE_LIT:
|
|
|
|
case NODE_STR:
|
|
|
|
case NODE_XSTR:
|
|
|
|
case NODE_DSTR:
|
|
|
|
case NODE_DXSTR:
|
|
|
|
case NODE_DREGX:
|
|
|
|
case NODE_DSYM:
|
2020-11-01 10:19:07 +03:00
|
|
|
node->nd_lit = rb_gc_location(node->nd_lit);
|
|
|
|
break;
|
2020-08-01 19:03:43 +03:00
|
|
|
case NODE_ARYPTN:
|
|
|
|
case NODE_FNDPTN:
|
2020-11-01 10:19:07 +03:00
|
|
|
node->nd_rval = rb_gc_location(node->nd_rval);
|
2019-09-27 04:20:56 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("unreachable");
|
2019-09-10 01:46:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ast_update_references(rb_ast_t *ast)
|
|
|
|
{
|
|
|
|
if (ast->node_buffer) {
|
|
|
|
node_buffer_t *nb = ast->node_buffer;
|
|
|
|
|
|
|
|
iterate_node_values(&nb->markable, update_ast_value, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_mark(rb_ast_t *ast)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
2019-11-05 03:04:58 +03:00
|
|
|
if (ast->node_buffer) rb_gc_mark(ast->node_buffer->mark_hash);
|
2019-08-17 02:04:26 +03:00
|
|
|
if (ast->body.compile_option) rb_gc_mark(ast->body.compile_option);
|
2019-09-09 19:37:06 +03:00
|
|
|
if (ast->node_buffer) {
|
|
|
|
node_buffer_t *nb = ast->node_buffer;
|
|
|
|
|
|
|
|
iterate_node_values(&nb->markable, mark_ast_value, NULL);
|
|
|
|
}
|
2021-06-17 17:43:08 +03:00
|
|
|
if (ast->body.script_lines) rb_gc_mark(ast->body.script_lines);
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_free(rb_ast_t *ast)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
2017-11-10 16:46:38 +03:00
|
|
|
if (ast->node_buffer) {
|
|
|
|
rb_node_buffer_free(ast->node_buffer);
|
|
|
|
ast->node_buffer = 0;
|
|
|
|
}
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:37:06 +03:00
|
|
|
static size_t
|
|
|
|
buffer_list_size(node_buffer_list_t *nb)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
node_buffer_elem_t *nbe = nb->head;
|
|
|
|
while (nbe != nb->last) {
|
|
|
|
nbe = nbe->next;
|
|
|
|
size += offsetof(node_buffer_elem_t, buf) + nb->len * sizeof(NODE);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-04-23 01:24:52 +03:00
|
|
|
size_t
|
|
|
|
rb_ast_memsize(const rb_ast_t *ast)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
node_buffer_t *nb = ast->node_buffer;
|
|
|
|
|
|
|
|
if (nb) {
|
|
|
|
size += sizeof(node_buffer_t) + offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
|
2019-09-09 19:37:06 +03:00
|
|
|
size += buffer_list_size(&nb->unmarkable);
|
|
|
|
size += buffer_list_size(&nb->markable);
|
2019-04-23 01:24:52 +03:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:44:57 +03:00
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_dispose(rb_ast_t *ast)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
|
|
|
rb_ast_free(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-29 18:51:23 +03:00
|
|
|
rb_ast_add_mark_object(rb_ast_t *ast, VALUE obj)
|
2017-10-27 19:44:57 +03:00
|
|
|
{
|
2019-11-05 03:04:58 +03:00
|
|
|
if (NIL_P(ast->node_buffer->mark_hash)) {
|
|
|
|
RB_OBJ_WRITE(ast, &ast->node_buffer->mark_hash, rb_ident_hash_new());
|
2019-09-09 19:37:06 +03:00
|
|
|
}
|
2019-11-05 03:04:58 +03:00
|
|
|
rb_hash_aset(ast->node_buffer->mark_hash, obj, Qtrue);
|
2017-10-27 19:44:57 +03:00
|
|
|
}
|