class RubyVM::AbstractSyntaxTree::Node

RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in RubyVM::AbstractSyntaxTree.

This class is MRI specific.

Public Instance Methods

all_tokens → array click to toggle source

Returns all tokens for the input script regardless the receiver node. Returns nil if keep_tokens is not enabled when parse method is called.

root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true)
root.all_tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
root.children[-1].all_tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
# File ast.rb, line 206
def all_tokens
  Primitive.ast_node_all_tokens
end
children → array click to toggle source

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 nil.

# File ast.rb, line 217
def children
  Primitive.ast_node_children
end
first_column → integer click to toggle source

The column number in the source code where this AST’s text began.

# File ast.rb, line 149
def first_column
  Primitive.ast_node_first_column
end
first_lineno → integer click to toggle source

The line number in the source code where this AST’s text began.

# File ast.rb, line 141
def first_lineno
  Primitive.ast_node_first_lineno
end
inspect → string click to toggle source

Returns debugging information about this node as a string.

# File ast.rb, line 225
def inspect
  Primitive.ast_node_inspect
end
last_column → integer click to toggle source

The column number in the source code where this AST’s text ended.

# File ast.rb, line 165
def last_column
  Primitive.ast_node_last_column
end
last_lineno → integer click to toggle source

The line number in the source code where this AST’s text ended.

# File ast.rb, line 157
def last_lineno
  Primitive.ast_node_last_lineno
end
node_id → integer click to toggle source

Returns an internal node_id number. Note that this is an API for ruby internal use, debugging, and research. Do not use this for any other purpose. The compatibility is not guaranteed.

# File ast.rb, line 236
def node_id
  Primitive.ast_node_node_id
end
pretty_print(q) click to toggle source
# File lib/pp.rb, line 588
def pretty_print(q)
  q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") {
    case type
    when :SCOPE
      pretty_print_children(q, %w"tbl args body")
    when :ARGS
      pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block])
    when :DEFN
      pretty_print_children(q, %w[mid body])
    when :ARYPTN
      pretty_print_children(q, %w[const pre rest post])
    when :HSHPTN
      pretty_print_children(q, %w[const kw kwrest])
    else
      pretty_print_children(q)
    end
  }
end
pretty_print_children(q, names = []) click to toggle source
# File lib/pp.rb, line 575
def pretty_print_children(q, names = [])
  children.zip(names) do |c, n|
    if n
      q.breakable
      q.text "#{n}:"
    end
    q.group(2) do
      q.breakable
      q.pp c
    end
  end
end
script_lines → array click to toggle source

Returns the original source code as an array of lines.

Note that this is an API for ruby internal use, debugging, and research. Do not use this for any other purpose. The compatibility is not guaranteed.

# File ast.rb, line 248
def script_lines
  Primitive.ast_node_script_lines
end
source → string click to toggle source

Returns the code fragment that corresponds to this AST.

Note that this is an API for ruby internal use, debugging, and research. Do not use this for any other purpose. The compatibility is not guaranteed.

Also note that this API may return an incomplete code fragment that does not parse; for example, a here document following an expression may be dropped.

# File ast.rb, line 264
def source
  lines = script_lines
  if lines
    lines = lines[first_lineno - 1 .. last_lineno - 1]
    lines[-1] = lines[-1][0...last_column]
    lines[0] = lines[0][first_column..-1]
    lines.join
  else
    nil
  end
end
tokens → array click to toggle source

Returns tokens corresponding to the location of the node. Returns nil if keep_tokens is not enabled when parse method is called.

root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true)
root.tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
root.tokens.map{_1[2]}.join # => "x = 1 + 2"

Token is an array of:

# File ast.rb, line 185
def tokens
  return nil unless all_tokens

  all_tokens.each_with_object([]) do |token, a|
    loc = token.last
    if ([first_lineno, first_column] <=> [loc[0], loc[1]]) <= 0 &&
       ([last_lineno, last_column]   <=> [loc[2], loc[3]]) >= 0
       a << token
    end
  end
end
type → symbol click to toggle source

Returns the type of this node as a symbol.

root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
root.type # => :SCOPE
lasgn = root.children[2]
lasgn.type # => :LASGN
call = lasgn.children[1]
call.type # => :OPCALL
# File ast.rb, line 133
def type
  Primitive.ast_node_type
end