From b8b503d657731fc0982c584f8b82f4976ea1283f Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 20 Apr 2019 03:25:36 +0000 Subject: [PATCH] Add symbol to the result of `RubyVM::AbstractSyntaxTree#children`. Add symbol to the result to make pattern match easily. For example: (1) NODE_MASGN * NODE_SPECIAL_NO_NAME_REST ``` $ ./miniruby -e 'p RubyVM::AbstractSyntaxTree.parse("a, * = b").children[-1].children' [#, #, :NODE_SPECIAL_NO_NAME_REST] ``` (2) NODE_POSTARG * NODE_SPECIAL_NO_NAME_REST ``` $ ./miniruby -e 'p RubyVM::AbstractSyntaxTree.parse("a, *, _ = b").children[-1].children[-1].children' [:NODE_SPECIAL_NO_NAME_REST, #] ``` (3) NODE_LASGN * NODE_SPECIAL_REQUIRED_KEYWORD ``` $ ./miniruby -e 'p RubyVM::AbstractSyntaxTree.parse("def a(k:) end").children[-1].children[-1].children[1].children[7].children[0].children' [:k, :NODE_SPECIAL_REQUIRED_KEYWORD] ``` git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67627 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ast.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ast.c b/ast.c index 9b57a09198..5945df95ce 100644 --- a/ast.c +++ b/ast.c @@ -419,7 +419,11 @@ node_children(rb_ast_t *ast, NODE *node) if (NODE_NAMED_REST_P(node->nd_args)) { return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args); } - return rb_ary_new_from_node_args(ast, 2, node->nd_value, node->nd_head); + else { + return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_value), + NEW_CHILD(ast, node->nd_head), + ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST"))); + } case NODE_LASGN: goto asgn; case NODE_DASGN: @@ -431,7 +435,7 @@ node_children(rb_ast_t *ast, NODE *node) case NODE_CVASGN: asgn: if (NODE_REQUIRED_KEYWORD_P(node)) { - return rb_ary_new_from_args(1, var_name(node->nd_vid)); + return rb_ary_new_from_args(2, var_name(node->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD"))); } return rb_ary_new_from_args(2, var_name(node->nd_vid), NEW_CHILD(ast, node->nd_value)); case NODE_GASGN: @@ -600,7 +604,8 @@ node_children(rb_ast_t *ast, NODE *node) 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); + return rb_ary_new_from_args(2, ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST")), + NEW_CHILD(ast, node->nd_2nd)); case NODE_ARGS: { struct rb_args_info *ainfo = node->nd_ainfo;