module RubyVM::AbstractSyntaxTree

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 bugs.ruby-lang.org/issues/14844.

Public Class Methods

RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(backtrace_location) → integer click to toggle source

Returns the node id for the given backtrace location.

begin
  raise
rescue =>  e
  loc = e.backtrace_locations.first
  RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(loc)
end # => 0
# File ast.rb, line 81
def self.node_id_for_backtrace_location backtrace_location
  Primitive.node_id_for_backtrace_location backtrace_location
end
RubyVM::AbstractSyntaxTree.of(proc) → RubyVM::AbstractSyntaxTree::Node click to toggle source
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>
# File ast.rb, line 66
def self.of body, keep_script_lines: false, error_tolerant: false, keep_tokens: false
  Primitive.ast_s_of body, keep_script_lines, error_tolerant, keep_tokens
end
RubyVM::AbstractSyntaxTree.parse(string) → RubyVM::AbstractSyntaxTree::Node click to toggle source

Parses the given string into an abstract syntax tree, returning the root node of that tree.

SyntaxError is raised if the given string is invalid syntax.

RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
# => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>
# File ast.rb, line 32
def self.parse string, keep_script_lines: false, error_tolerant: false, keep_tokens: false
  Primitive.ast_s_parse string, keep_script_lines, error_tolerant, keep_tokens
end
RubyVM::AbstractSyntaxTree.parse_file(pathname) → RubyVM::AbstractSyntaxTree::Node click to toggle source

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>
# File ast.rb, line 47
def self.parse_file pathname, keep_script_lines: false, error_tolerant: false, keep_tokens: false
  Primitive.ast_s_parse_file pathname, keep_script_lines, error_tolerant, keep_tokens
end