Refine RubyVM::AbstractSyntaxTree::Node#type
* ast.c (rb_ast_node_type): simplified to return a Symbol without "NODE_" prefix. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66142 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8edd642381
commit
afa685398e
14
ast.c
14
ast.c
@ -262,21 +262,21 @@ rb_ast_node_alloc(VALUE klass)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const char*
|
static const char*
|
||||||
node_type_to_str(NODE *node)
|
node_type_to_str(const NODE *node)
|
||||||
{
|
{
|
||||||
return ruby_node_name(nd_type(node));
|
return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* node.type -> string
|
* node.type -> symbol
|
||||||
*
|
*
|
||||||
* Returns the type of this node as a string.
|
* Returns the type of this node as a symbol.
|
||||||
*
|
*
|
||||||
* root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
|
* root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
|
||||||
* root.type # => "NODE_SCOPE"
|
* root.type # => :SCOPE
|
||||||
* call = root.children[2]
|
* call = root.children[2]
|
||||||
* call.type # => "NODE_OPCALL"
|
* call.type # => :OPCALL
|
||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_ast_node_type(VALUE self)
|
rb_ast_node_type(VALUE self)
|
||||||
@ -284,7 +284,7 @@ rb_ast_node_type(VALUE self)
|
|||||||
struct ASTNodeData *data;
|
struct ASTNodeData *data;
|
||||||
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
|
||||||
|
|
||||||
return rb_fstring_cstr(node_type_to_str(data->node));
|
return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
|
||||||
}
|
}
|
||||||
|
|
||||||
#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
|
#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
|
||||||
|
@ -529,13 +529,13 @@ class RubyVM::AbstractSyntaxTree::Node
|
|||||||
end
|
end
|
||||||
|
|
||||||
def pretty_print(q)
|
def pretty_print(q)
|
||||||
q.group(1, "(#{type.sub(/\ANODE_/,'')}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") {
|
q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") {
|
||||||
case type
|
case type
|
||||||
when "NODE_SCOPE"
|
when :SCOPE
|
||||||
pretty_print_children(q, %w"tbl args body")
|
pretty_print_children(q, %w"tbl args body")
|
||||||
when "NODE_ARGS"
|
when :ARGS
|
||||||
pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block])
|
pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block])
|
||||||
when "NODE_DEFN"
|
when :DEFN
|
||||||
pretty_print_children(q, %w[mid body])
|
pretty_print_children(q, %w[mid body])
|
||||||
else
|
else
|
||||||
pretty_print_children(q)
|
pretty_print_children(q)
|
||||||
|
@ -76,7 +76,7 @@ class TestAst < Test::Unit::TestCase
|
|||||||
return true if children.empty?
|
return true if children.empty?
|
||||||
# These NODE_D* has NODE_ARRAY as nd_next->nd_next whose last locations
|
# These NODE_D* has NODE_ARRAY as nd_next->nd_next whose last locations
|
||||||
# we can not update when item is appended.
|
# we can not update when item is appended.
|
||||||
return true if ["NODE_DSTR", "NODE_DXSTR", "NODE_DREGX", "NODE_DSYM"].include? node.type
|
return true if [:DSTR, :DXSTR, :DREGX, :DSYM].include? node.type
|
||||||
|
|
||||||
min = children.map(&:beg_pos).min
|
min = children.map(&:beg_pos).min
|
||||||
max = children.map(&:end_pos).max
|
max = children.map(&:end_pos).max
|
||||||
@ -138,18 +138,18 @@ class TestAst < Test::Unit::TestCase
|
|||||||
term = "A"*257
|
term = "A"*257
|
||||||
ast = RubyVM::AbstractSyntaxTree.parse("<<-#{term}\n""ddddddd\n#{term}\n")
|
ast = RubyVM::AbstractSyntaxTree.parse("<<-#{term}\n""ddddddd\n#{term}\n")
|
||||||
node = ast.children[2]
|
node = ast.children[2]
|
||||||
assert_equal("NODE_STR", node.type)
|
assert_equal(:STR, node.type)
|
||||||
assert_equal(0, node.first_column)
|
assert_equal(0, node.first_column)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_column_of_heredoc
|
def test_column_of_heredoc
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("<<-SRC\nddddddd\nSRC\n").children[2]
|
node = RubyVM::AbstractSyntaxTree.parse("<<-SRC\nddddddd\nSRC\n").children[2]
|
||||||
assert_equal("NODE_STR", node.type)
|
assert_equal(:STR, node.type)
|
||||||
assert_equal(0, node.first_column)
|
assert_equal(0, node.first_column)
|
||||||
assert_equal(6, node.last_column)
|
assert_equal(6, node.last_column)
|
||||||
|
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("<<SRC\nddddddd\nSRC\n").children[2]
|
node = RubyVM::AbstractSyntaxTree.parse("<<SRC\nddddddd\nSRC\n").children[2]
|
||||||
assert_equal("NODE_STR", node.type)
|
assert_equal(:STR, node.type)
|
||||||
assert_equal(0, node.first_column)
|
assert_equal(0, node.first_column)
|
||||||
assert_equal(5, node.last_column)
|
assert_equal(5, node.last_column)
|
||||||
end
|
end
|
||||||
@ -200,15 +200,15 @@ class TestAst < Test::Unit::TestCase
|
|||||||
node = RubyVM::AbstractSyntaxTree.parse("x = 0")
|
node = RubyVM::AbstractSyntaxTree.parse("x = 0")
|
||||||
lv, _, body = *node.children
|
lv, _, body = *node.children
|
||||||
assert_equal([:x], lv)
|
assert_equal([:x], lv)
|
||||||
assert_equal("NODE_LASGN", body.type)
|
assert_equal(:LASGN, body.type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_call
|
def test_call
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("nil.foo")
|
node = RubyVM::AbstractSyntaxTree.parse("nil.foo")
|
||||||
_, _, body = *node.children
|
_, _, body = *node.children
|
||||||
assert_equal("NODE_CALL", body.type)
|
assert_equal(:CALL, body.type)
|
||||||
recv, mid, args = body.children
|
recv, mid, args = body.children
|
||||||
assert_equal("NODE_NIL", recv.type)
|
assert_equal(:NIL, recv.type)
|
||||||
assert_equal(:foo, mid)
|
assert_equal(:foo, mid)
|
||||||
assert_nil(args)
|
assert_nil(args)
|
||||||
end
|
end
|
||||||
@ -216,7 +216,7 @@ class TestAst < Test::Unit::TestCase
|
|||||||
def test_fcall
|
def test_fcall
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("foo()")
|
node = RubyVM::AbstractSyntaxTree.parse("foo()")
|
||||||
_, _, body = *node.children
|
_, _, body = *node.children
|
||||||
assert_equal("NODE_FCALL", body.type)
|
assert_equal(:FCALL, body.type)
|
||||||
mid, args = body.children
|
mid, args = body.children
|
||||||
assert_equal(:foo, mid)
|
assert_equal(:foo, mid)
|
||||||
assert_nil(args)
|
assert_nil(args)
|
||||||
@ -225,7 +225,7 @@ class TestAst < Test::Unit::TestCase
|
|||||||
def test_vcall
|
def test_vcall
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("foo")
|
node = RubyVM::AbstractSyntaxTree.parse("foo")
|
||||||
_, _, body = *node.children
|
_, _, body = *node.children
|
||||||
assert_equal("NODE_VCALL", body.type)
|
assert_equal(:VCALL, body.type)
|
||||||
mid, args = body.children
|
mid, args = body.children
|
||||||
assert_equal(:foo, mid)
|
assert_equal(:foo, mid)
|
||||||
assert_nil(args)
|
assert_nil(args)
|
||||||
@ -234,19 +234,19 @@ class TestAst < Test::Unit::TestCase
|
|||||||
def test_defn
|
def test_defn
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("def a; end")
|
node = RubyVM::AbstractSyntaxTree.parse("def a; end")
|
||||||
_, _, body = *node.children
|
_, _, body = *node.children
|
||||||
assert_equal("NODE_DEFN", body.type)
|
assert_equal(:DEFN, body.type)
|
||||||
mid, defn = body.children
|
mid, defn = body.children
|
||||||
assert_equal(:a, mid)
|
assert_equal(:a, mid)
|
||||||
assert_equal("NODE_SCOPE", defn.type)
|
assert_equal(:SCOPE, defn.type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_defs
|
def test_defs
|
||||||
node = RubyVM::AbstractSyntaxTree.parse("def a.b; end")
|
node = RubyVM::AbstractSyntaxTree.parse("def a.b; end")
|
||||||
_, _, body = *node.children
|
_, _, body = *node.children
|
||||||
assert_equal("NODE_DEFS", body.type)
|
assert_equal(:DEFS, body.type)
|
||||||
recv, mid, defn = body.children
|
recv, mid, defn = body.children
|
||||||
assert_equal("NODE_VCALL", recv.type)
|
assert_equal(:VCALL, recv.type)
|
||||||
assert_equal(:b, mid)
|
assert_equal(:b, mid)
|
||||||
assert_equal("NODE_SCOPE", defn.type)
|
assert_equal(:SCOPE, defn.type)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user