Rename AST module to AbstractSyntaxTree

Follow the same naming convention of `InstructionSequence` class.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65641 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
yui-knk 2018-11-09 01:37:41 +00:00
parent f67c0e5671
commit 1d7d08262e
3 changed files with 37 additions and 37 deletions

8
NEWS
View File

@ -226,17 +226,17 @@ sufficient information, see the ChangeLog file or Redmine
* Range#step now returns an instance of Enumerator::ArithmeticSequence * Range#step now returns an instance of Enumerator::ArithmeticSequence
class rather than one of Enumerator class. class rather than one of Enumerator class.
[RubyVM::AST] [RubyVM::AbstractSyntaxTree]
[New methods] [New methods]
* RubyVM::AST.parse parses a given string and returns AST * RubyVM::AbstractSyntaxTree.parse parses a given string and returns AST
nodes. [experimental] nodes. [experimental]
* RubyVM::AST.parse_file parses a given file and returns AST * RubyVM::AbstractSyntaxTree.parse_file parses a given file and returns AST
nodes. [experimental] nodes. [experimental]
* RubyVM::AST.of returns AST nodes of the given proc or method. * RubyVM::AbstractSyntaxTree.of returns AST nodes of the given proc or method.
[experimental] [experimental]
[String] [String]

24
ast.c
View File

@ -54,15 +54,15 @@ ast_new_internal(rb_ast_t *ast, NODE *node)
/* /*
* call-seq: * call-seq:
* RubyVM::AST.parse(string) -> RubyVM::AST::Node * RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node
* *
* Parses the given string into an abstract syntax tree, * Parses the given string into an abstract syntax tree,
* returning the root node of that tree. * returning the root node of that tree.
* *
* SyntaxError is raised if the given string is invalid syntax. * SyntaxError is raised if the given string is invalid syntax.
* *
* RubyVM::AST.parse("x = 1 + 2") * RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
* # => #<RubyVM::AST::Node(NODE_SCOPE(0) 1:0, 1:9): > * # => #<RubyVM::AbstractSyntaxTree::Node(NODE_SCOPE(0) 1:0, 1:9): >
*/ */
static VALUE static VALUE
rb_ast_s_parse(VALUE module, VALUE str) rb_ast_s_parse(VALUE module, VALUE str)
@ -88,7 +88,7 @@ rb_ast_s_parse(VALUE module, VALUE str)
/* /*
* call-seq: * call-seq:
* RubyVM::AST.parse_file(pathname) -> RubyVM::AST::Node * RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node
* *
* Reads the file from <code>pathname</code>, then parses it like ::parse, * Reads the file from <code>pathname</code>, then parses it like ::parse,
* returning the root node of the abstract syntax tree. * returning the root node of the abstract syntax tree.
@ -96,8 +96,8 @@ rb_ast_s_parse(VALUE module, VALUE str)
* SyntaxError is raised if <code>pathname</code>'s contents are not * SyntaxError is raised if <code>pathname</code>'s contents are not
* valid Ruby syntax. * valid Ruby syntax.
* *
* RubyVM::AST.parse_file("my-app/app.rb") * RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb")
* # => #<RubyVM::AST::Node(NODE_SCOPE(0) 1:0, 31:3): > * # => #<RubyVM::AbstractSyntaxTree::Node(NODE_SCOPE(0) 1:0, 31:3): >
*/ */
static VALUE static VALUE
rb_ast_s_parse_file(VALUE module, VALUE path) rb_ast_s_parse_file(VALUE module, VALUE path)
@ -200,7 +200,7 @@ node_type_to_str(NODE *node)
* *
* Returns the type of this node as a string. * Returns the type of this node as a string.
* *
* root = RubyVM::AST.parse("x = 1 + 2") * root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
* root.type # => "NODE_SCOPE" * root.type # => "NODE_SCOPE"
* call = root.children[2] * call = root.children[2]
* call.type # => "NODE_OPCALL" * call.type # => "NODE_OPCALL"
@ -659,14 +659,14 @@ void
Init_ast(void) Init_ast(void)
{ {
/* /*
* AST provides methods to parse Ruby code into * AbstractSyntaxTree provides methods to parse Ruby code into
* abstract syntax trees. The nodes in the tree * abstract syntax trees. The nodes in the tree
* are instances of RubyVM::AST::Node. * are instances of RubyVM::AbstractSyntaxTree::Node.
*/ */
rb_mAST = rb_define_module_under(rb_cRubyVM, "AST"); rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
/* /*
* RubyVM::AST::Node instances are created by parse methods in * RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in
* RubyVM::AST. * RubyVM::AbstractSyntaxTree.
*/ */
rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject); rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);

View File

@ -3,7 +3,7 @@ require 'test/unit'
require 'tempfile' require 'tempfile'
class RubyVM class RubyVM
module AST module AbstractSyntaxTree
class Node class Node
class CodePosition class CodePosition
include Comparable include Comparable
@ -64,14 +64,14 @@ class TestAst < Test::Unit::TestCase
def ast def ast
return @ast if defined?(@ast) return @ast if defined?(@ast)
@ast = RubyVM::AST.parse_file(@path) @ast = RubyVM::AbstractSyntaxTree.parse_file(@path)
end end
private private
def validate_range0(node) def validate_range0(node)
beg_pos, end_pos = node.beg_pos, node.end_pos beg_pos, end_pos = node.beg_pos, node.end_pos
children = node.children.grep(RubyVM::AST::Node) children = node.children.grep(RubyVM::AbstractSyntaxTree::Node)
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
@ -99,7 +99,7 @@ class TestAst < Test::Unit::TestCase
def validate_not_cared0(node) def validate_not_cared0(node)
beg_pos, end_pos = node.beg_pos, node.end_pos beg_pos, end_pos = node.beg_pos, node.end_pos
children = node.children.grep(RubyVM::AST::Node) children = node.children.grep(RubyVM::AbstractSyntaxTree::Node)
@errors << { type: :first_lineno, node: node } if beg_pos.lineno == 0 @errors << { type: :first_lineno, node: node } if beg_pos.lineno == 0
@errors << { type: :first_column, node: node } if beg_pos.column == -1 @errors << { type: :first_column, node: node } if beg_pos.column == -1
@ -131,24 +131,24 @@ class TestAst < Test::Unit::TestCase
end end
def test_allocate def test_allocate
assert_raise(TypeError) {RubyVM::AST::Node.allocate} assert_raise(TypeError) {RubyVM::AbstractSyntaxTree::Node.allocate}
end end
def test_column_with_long_heredoc_identifier def test_column_with_long_heredoc_identifier
term = "A"*257 term = "A"*257
ast = RubyVM::AST.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("NODE_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::AST.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("NODE_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::AST.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("NODE_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)
@ -156,7 +156,7 @@ class TestAst < Test::Unit::TestCase
def test_parse_raises_syntax_error def test_parse_raises_syntax_error
assert_raise_with_message(SyntaxError, /keyword_end/) do assert_raise_with_message(SyntaxError, /keyword_end/) do
RubyVM::AST.parse("end") RubyVM::AbstractSyntaxTree.parse("end")
end end
end end
@ -165,7 +165,7 @@ class TestAst < Test::Unit::TestCase
f.puts "end" f.puts "end"
f.close f.close
assert_raise_with_message(SyntaxError, /keyword_end/) do assert_raise_with_message(SyntaxError, /keyword_end/) do
RubyVM::AST.parse_file(f.path) RubyVM::AbstractSyntaxTree.parse_file(f.path)
end end
end end
end end
@ -174,23 +174,23 @@ class TestAst < Test::Unit::TestCase
proc = Proc.new { 1 + 2 } proc = Proc.new { 1 + 2 }
method = self.method(__method__) method = self.method(__method__)
node_proc = RubyVM::AST.of(proc) node_proc = RubyVM::AbstractSyntaxTree.of(proc)
node_method = RubyVM::AST.of(method) node_method = RubyVM::AbstractSyntaxTree.of(method)
assert_instance_of(RubyVM::AST::Node, node_proc) assert_instance_of(RubyVM::AbstractSyntaxTree::Node, node_proc)
assert_instance_of(RubyVM::AST::Node, node_method) assert_instance_of(RubyVM::AbstractSyntaxTree::Node, node_method)
assert_raise(TypeError) { RubyVM::AST.of("1 + 2") } assert_raise(TypeError) { RubyVM::AbstractSyntaxTree.of("1 + 2") }
end end
def test_scope_local_variables def test_scope_local_variables
node = RubyVM::AST.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("NODE_LASGN", body.type)
end end
def test_call def test_call
node = RubyVM::AST.parse("nil.foo") node = RubyVM::AbstractSyntaxTree.parse("nil.foo")
_, _, body = *node.children _, _, body = *node.children
assert_equal("NODE_CALL", body.type) assert_equal("NODE_CALL", body.type)
recv, mid, args = body.children recv, mid, args = body.children
@ -200,7 +200,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_fcall def test_fcall
node = RubyVM::AST.parse("foo()") node = RubyVM::AbstractSyntaxTree.parse("foo()")
_, _, body = *node.children _, _, body = *node.children
assert_equal("NODE_FCALL", body.type) assert_equal("NODE_FCALL", body.type)
mid, args = body.children mid, args = body.children
@ -209,7 +209,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_vcall def test_vcall
node = RubyVM::AST.parse("foo") node = RubyVM::AbstractSyntaxTree.parse("foo")
_, _, body = *node.children _, _, body = *node.children
assert_equal("NODE_VCALL", body.type) assert_equal("NODE_VCALL", body.type)
mid, args = body.children mid, args = body.children
@ -218,7 +218,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_defn def test_defn
node = RubyVM::AST.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("NODE_DEFN", body.type)
mid, defn = body.children mid, defn = body.children
@ -227,7 +227,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_defs def test_defs
node = RubyVM::AST.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("NODE_DEFS", body.type)
recv, mid, defn = body.children recv, mid, defn = body.children