1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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'
[#<RubyVM::AbstractSyntaxTree::Node:VCALL@1:7-1:8>, #<RubyVM::AbstractSyntaxTree::Node:ARRAY@1:0-1:1>, :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, #<RubyVM::AbstractSyntaxTree::Node:ARRAY@1:6-1:7>]
```

(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
This commit is contained in:
yui-knk 2019-04-20 03:25:36 +00:00
parent 91557d0c52
commit b8b503d657

11
ast.c
View file

@ -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;