ast.rb: reduce nesting

This commit is contained in:
Nobuyoshi Nakada 2021-06-06 23:02:06 +09:00
parent c9af563e10
commit cf92b3cc97
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6

233
ast.rb
View File

@ -1,146 +1,143 @@
# for ast.c # for ast.c
class RubyVM # AbstractSyntaxTree provides methods to parse Ruby code into
# abstract syntax trees. The nodes in the tree
# are instances of RubyVM::AbstractSyntaxTree::Node.
#
# This module is MRI specific as it exposes implementation details
# of the MRI abstract syntax tree.
#
# This module is experimental and its API is not stable, therefore it might
# change without notice. As examples, the order of children nodes is not
# guaranteed, the number of children nodes might change, there is no way to
# access children nodes by name, etc.
#
# If you are looking for a stable API or an API working under multiple Ruby
# implementations, consider using the _parser_ gem or Ripper. If you would
# like to make RubyVM::AbstractSyntaxTree stable, please join the discussion
# at https://bugs.ruby-lang.org/issues/14844.
#
module RubyVM::AbstractSyntaxTree
# AbstractSyntaxTree provides methods to parse Ruby code into # call-seq:
# abstract syntax trees. The nodes in the tree # RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node
# are instances of RubyVM::AbstractSyntaxTree::Node.
# #
# This module is MRI specific as it exposes implementation details # Parses the given _string_ into an abstract syntax tree,
# of the MRI abstract syntax tree. # returning the root node of that tree.
# #
# This module is experimental and its API is not stable, therefore it might # SyntaxError is raised if the given _string_ is invalid syntax.
# change without notice. As examples, the order of children nodes is not
# guaranteed, the number of children nodes might change, there is no way to
# access children nodes by name, etc.
# #
# If you are looking for a stable API or an API working under multiple Ruby # RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
# implementations, consider using the _parser_ gem or Ripper. If you would # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>
# like to make RubyVM::AbstractSyntaxTree stable, please join the discussion def self.parse string
# at https://bugs.ruby-lang.org/issues/14844. Primitive.ast_s_parse string
end
# call-seq:
# RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node
# #
module AbstractSyntaxTree # Reads the file from _pathname_, then parses it like ::parse,
# returning the root node of the abstract syntax tree.
#
# SyntaxError is raised if _pathname_'s contents are not
# valid Ruby syntax.
#
# RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb")
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3>
def self.parse_file pathname
Primitive.ast_s_parse_file pathname
end
# call-seq:
# RubyVM::AbstractSyntaxTree.of(proc) -> RubyVM::AbstractSyntaxTree::Node
# RubyVM::AbstractSyntaxTree.of(method) -> RubyVM::AbstractSyntaxTree::Node
#
# Returns AST nodes of the given _proc_ or _method_.
#
# RubyVM::AbstractSyntaxTree.of(proc {1 + 2})
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42>
#
# def hello
# puts "hello, world"
# end
#
# RubyVM::AbstractSyntaxTree.of(method(:hello))
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3>
def self.of body
Primitive.ast_s_of body
end
# RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in
# RubyVM::AbstractSyntaxTree.
#
# This class is MRI specific.
#
class Node
# call-seq: # call-seq:
# RubyVM::AbstractSyntaxTree.parse(string) -> RubyVM::AbstractSyntaxTree::Node # node.type -> symbol
# #
# Parses the given _string_ into an abstract syntax tree, # Returns the type of this node as a symbol.
# returning the root node of that tree.
# #
# SyntaxError is raised if the given _string_ is invalid syntax. # root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
# # root.type # => :SCOPE
# RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") # lasgn = root.children[2]
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9> # lasgn.type # => :LASGN
def self.parse string # call = lasgn.children[1]
Primitive.ast_s_parse string # call.type # => :OPCALL
def type
Primitive.ast_node_type
end end
# call-seq: # call-seq:
# RubyVM::AbstractSyntaxTree.parse_file(pathname) -> RubyVM::AbstractSyntaxTree::Node # node.first_lineno -> integer
# #
# Reads the file from _pathname_, then parses it like ::parse, # The line number in the source code where this AST's text began.
# returning the root node of the abstract syntax tree. def first_lineno
# Primitive.ast_node_first_lineno
# SyntaxError is raised if _pathname_'s contents are not
# valid Ruby syntax.
#
# RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb")
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3>
def self.parse_file pathname
Primitive.ast_s_parse_file pathname
end end
# call-seq: # call-seq:
# RubyVM::AbstractSyntaxTree.of(proc) -> RubyVM::AbstractSyntaxTree::Node # node.first_column -> integer
# RubyVM::AbstractSyntaxTree.of(method) -> RubyVM::AbstractSyntaxTree::Node
# #
# Returns AST nodes of the given _proc_ or _method_. # The column number in the source code where this AST's text began.
# def first_column
# RubyVM::AbstractSyntaxTree.of(proc {1 + 2}) Primitive.ast_node_first_column
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42>
#
# def hello
# puts "hello, world"
# end
#
# RubyVM::AbstractSyntaxTree.of(method(:hello))
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3>
def self.of body
Primitive.ast_s_of body
end end
# RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in # call-seq:
# RubyVM::AbstractSyntaxTree. # node.last_lineno -> integer
# #
# This class is MRI specific. # The line number in the source code where this AST's text ended.
def last_lineno
Primitive.ast_node_last_lineno
end
# call-seq:
# node.last_column -> integer
# #
class Node # The column number in the source code where this AST's text ended.
def last_column
Primitive.ast_node_last_column
end
# call-seq: # call-seq:
# node.type -> symbol # node.children -> array
# #
# Returns the type of this node as a symbol. # Returns AST nodes under this one. Each kind of node
# # has different children, depending on what kind of node it is.
# root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") #
# root.type # => :SCOPE # The returned array may contain other nodes or <code>nil</code>.
# lasgn = root.children[2] def children
# lasgn.type # => :LASGN Primitive.ast_node_children
# call = lasgn.children[1] end
# call.type # => :OPCALL
def type
Primitive.ast_node_type
end
# call-seq: # call-seq:
# node.first_lineno -> integer # node.inspect -> string
# #
# The line number in the source code where this AST's text began. # Returns debugging information about this node as a string.
def first_lineno def inspect
Primitive.ast_node_first_lineno Primitive.ast_node_inspect
end
# call-seq:
# node.first_column -> integer
#
# The column number in the source code where this AST's text began.
def first_column
Primitive.ast_node_first_column
end
# call-seq:
# node.last_lineno -> integer
#
# The line number in the source code where this AST's text ended.
def last_lineno
Primitive.ast_node_last_lineno
end
# call-seq:
# node.last_column -> integer
#
# The column number in the source code where this AST's text ended.
def last_column
Primitive.ast_node_last_column
end
# call-seq:
# node.children -> array
#
# Returns AST nodes under this one. Each kind of node
# has different children, depending on what kind of node it is.
#
# The returned array may contain other nodes or <code>nil</code>.
def children
Primitive.ast_node_children
end
# call-seq:
# node.inspect -> string
#
# Returns debugging information about this node as a string.
def inspect
Primitive.ast_node_inspect
end
end end
end end
end end