Class: RubyVM::AbstractSyntaxTree::Node
Overview
Node
instances are created by parse methods in ::RubyVM::AbstractSyntaxTree
.
This class is MRI specific.
Instance Method Summary
-
#all_tokens ⇒ Array
Returns all tokens for the input script regardless the receiver node.
-
#children ⇒ Array
Returns AST nodes under this one.
-
#first_column ⇒ Integer
The column number in the source code where this AST’s text began.
-
#first_lineno ⇒ Integer
The line number in the source code where this AST’s text began.
-
#inspect ⇒ String
Returns debugging information about this node as a string.
-
#last_column ⇒ Integer
The column number in the source code where this AST’s text ended.
-
#last_lineno ⇒ Integer
The line number in the source code where this AST’s text ended.
-
#locations ⇒ Array
Returns location objects associated with the AST node.
-
#node_id ⇒ Integer
Returns an internal node_id number.
-
#script_lines ⇒ Array
Returns the original source code as an array of lines.
-
#source ⇒ String
Returns the code fragment that corresponds to this AST.
-
#tokens ⇒ Array
Returns tokens corresponding to the location of the node.
-
#type ⇒ Symbol
Returns the type of this node as a symbol.
Instance Method Details
#all_tokens ⇒ Array
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 205
def all_tokens Primitive.ast_node_all_tokens end
#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 nil
.
# File 'ast.rb', line 216
def children Primitive.ast_node_children end
#first_column ⇒ Integer
The column number in the source code where this AST’s text began.
# File 'ast.rb', line 148
def first_column Primitive.ast_node_first_column end
#first_lineno ⇒ Integer
The line number in the source code where this AST’s text began.
# File 'ast.rb', line 140
def first_lineno Primitive.ast_node_first_lineno end
#inspect ⇒ String
Returns debugging information about this node as a string.
# File 'ast.rb', line 224
def inspect Primitive.ast_node_inspect end
#last_column ⇒ Integer
The column number in the source code where this AST’s text ended.
# File 'ast.rb', line 164
def last_column Primitive.ast_node_last_column end
#last_lineno ⇒ Integer
The line number in the source code where this AST’s text ended.
# File 'ast.rb', line 156
def last_lineno Primitive.ast_node_last_lineno end
#locations ⇒ Array
Returns location objects associated with the AST node. The returned array contains Location
.
# File 'ast.rb', line 280
def locations Primitive.ast_node_locations end
#node_id ⇒ Integer
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 235
def node_id Primitive.ast_node_node_id end
#script_lines ⇒ Array
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 247
def script_lines Primitive.ast_node_script_lines end
#source ⇒ String
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 263
def source lines = script_lines if lines lines = lines[first_lineno - 1 .. last_lineno - 1] lines[-1] = lines[-1].byteslice(0...last_column) lines[0] = lines[0].byteslice(first_column..-1) lines.join else nil end end
#tokens ⇒ Array
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:
-
id
-
token type
-
source code text
-
location [ first_lineno, first_column, last_lineno, last_column ]
# File 'ast.rb', line 184
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
# File 'ast.rb', line 132
def type Primitive.ast_node_type end