class Prism::Node
This represents a node in the tree. It is the parent class of all of the various node types.
This represents a node in the tree. It is the parent class of all of the various node types.
Attributes
A unique identifier for this node. This is used in a very specific use case where you want to keep around a reference to a node without having to keep around the syntax tree in memory. This unique identifier will be consistent across multiple parses of the same source code.
A unique identifier for this node. This is used in a very specific use case where you want to keep around a reference to a node without having to keep around the syntax tree in memory. This unique identifier will be consistent across multiple parses of the same source code.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 332 def self.fields # This method should only be called on subclasses of Node, not Node # itself. raise NoMethodError, "undefined method `fields' for #{inspect}" if self == Node Reflection.fields_for(self) end
Returns a list of the fields that exist for this node class. Fields describe the structure of the node. This kind of reflection is useful for things like recursively visiting each node and field in the tree.
Returns a list of the fields that exist for this node class. Fields describe the structure of the node. This kind of reflection is useful for things like recursively visiting each node and field in the tree.
: () -> Array
Public Instance Methods
() { (node) → bool } → node?
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 295 def breadth_first_search(&blk) queue = [self] #: Array[node] while (node = queue.shift) return node if yield node queue.concat(node.compact_child_nodes) end nil end
Returns the first node that matches the given block when visited in a breadth-first search. This is useful for finding a node that matches a particular condition.
node.breadth_first_search { |node| node.node_id == node_id }
Returns the first node that matches the given block when visited in a breadth-first search. This is useful for finding a node that matches a particular condition.
node.breadth_first_search { |node| node.node_id == node_id }
: () { (node) -> bool } -> node?
() { (node) → bool } → Array[node]
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 314 def breadth_first_search_all(&blk) queue = [self] #: Array[Prism::node] results = [] #: Array[Prism::node] while (node = queue.shift) results << node if yield node queue.concat(node.compact_child_nodes) end results end
Returns all of the nodes that match the given block when visited in a breadth-first search. This is useful for finding all nodes that match a particular condition.
node.breadth_first_search_all { |node| node.is_a?(Prism::CallNode) }
Returns all of the nodes that match the given block when visited in a breadth-first search. This is useful for finding all nodes that match a particular condition.
node.breadth_first_search_all { |node| node.is_a?(Prism::CallNode) }
: () { (node) -> bool } -> Array
() → Location
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 58 def location location = @location return location if location.is_a?(Location) @location = Location.new(source, location >> 32, location & 0xFFFFFFFF) end
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 231 def newline? flags.anybits?(NodeFlags::NEWLINE) end
Returns true if the node has the newline flag set.
Returns true if the node has the newline flag set.
: () -> bool
(_Repository repository) → Relocation::Entry
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 50 def save(repository) repository.enter(node_id, :itself) end
Save this node using a saved source so that it can be retrieved later.
Save this node using a saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
(_Repository repository) → Relocation::Entry
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 67 def save_location(repository) repository.enter(node_id, :location) end
Save the location using a saved source so that it can be retrieved later.
Save the location using a saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
An alias for source_lines, used to mimic the API from RubyVM::AbstractSyntaxTree to make it easier to migrate.
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 208 def slice location.slice end
Slice the location of the node from the source.
Slice the location of the node from the source.
: () -> String
() → String
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 217 def slice_lines location.slice_lines end
Slice the location of the node from the source, starting at the beginning of the line that the location starts on, ending at the end of the line that the location ends on.
Slice the location of the node from the source, starting at the beginning of the line that the location starts on, ending at the end of the line that the location ends on.
: () -> String
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 197 def source_lines location.source_lines end
Returns all of the lines of the source code associated with this node.
Returns all of the lines of the source code associated with this node.
: () -> Array
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 238 def static_literal? flags.anybits?(NodeFlags::STATIC_LITERAL) end
Returns true if the node has the static literal flag set.
Returns true if the node has the static literal flag set.
: () -> bool
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 256 def to_dot # @type self: node DotVisitor.new.tap { |visitor| accept(visitor) }.to_dot end
Convert this node into a graphviz dot graph string.
Convert this node into a graphviz dot graph string.
: () -> String
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 269 def tunnel(line, column) queue = [self] #: Array[node] result = [] #: Array[node] offset = source.byte_offset(line, column) while (node = queue.shift) result << node node.each_child_node do |child_node| if child_node.start_offset <= offset && offset < child_node.end_offset queue << child_node break end end end result end
Returns a list of nodes that are descendants of this node that contain the given line and column. This is useful for locating a node that is selected based on the line and column of the source code.
Important to note is that the column given to this method should be in bytes, as opposed to characters or code units.
Returns a list of nodes that are descendants of this node that contain the given line and column. This is useful for locating a node that is selected based on the line and column of the source code.
Important to note is that the column given to this method should be in bytes, as opposed to characters or code units.
Location Delegators
These methods provide convenient access to the underlying Location object.
Public Instance Methods
(_CodeUnitsCache cache) → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 180 def cached_end_code_units_column(cache) location.cached_end_code_units_column(cache) end
Delegates to cached_end_code_units_column of the associated location object.
Delegates to cached_end_code_units_column of the associated location object.
: (_CodeUnitsCache cache) -> Integer
(_CodeUnitsCache cache) → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 134 def cached_end_code_units_offset(cache) location.cached_end_code_units_offset(cache) end
Delegates to cached_end_code_units_offset of the associated location object.
Delegates to cached_end_code_units_offset of the associated location object.
: (_CodeUnitsCache cache) -> Integer
(_CodeUnitsCache cache) → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 172 def cached_start_code_units_column(cache) location.cached_start_code_units_column(cache) end
Delegates to cached_start_code_units_column of the associated location object.
Delegates to cached_start_code_units_column of the associated location object.
: (_CodeUnitsCache cache) -> Integer
(_CodeUnitsCache cache) → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 126 def cached_start_code_units_offset(cache) location.cached_start_code_units_offset(cache) end
Delegates to cached_start_code_units_offset of the associated location object.
Delegates to cached_start_code_units_offset of the associated location object.
: (_CodeUnitsCache cache) -> Integer
() → Array[Comment]
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 188 def comments source.comments_for(node_id) end
The comments attached to this node, ordered by start offset. Comments are only attached when the source is parsed with the attach_comments option.
The comments attached to this node, ordered by start offset. Comments are only attached when the source is parsed with the attach_comments option.
: () -> Array
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 164 def end_character_column location.end_character_column end
Delegates to end_character_column of the associated location object.
Delegates to end_character_column of the associated location object.
: () -> Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 118 def end_character_offset location.end_character_offset end
Delegates to end_character_offset of the associated location object.
Delegates to end_character_offset of the associated location object.
: () -> Integer
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 148 def end_column location.end_column end
Delegates to end_column of the associated location object.
Delegates to end_column of the associated location object.
: () -> Integer
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 86 def end_line location.end_line end
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 101 def end_offset location = @location location.is_a?(Location) ? location.end_offset : ((location >> 32) + (location & 0xFFFFFFFF)) end
Delegates to end_offset of the associated location object.
Delegates to end_offset of the associated location object.
: () -> Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 156 def start_character_column location.start_character_column end
Delegates to start_character_column of the associated location object.
Delegates to start_character_column of the associated location object.
: () -> Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 110 def start_character_offset location.start_character_offset end
Delegates to start_character_offset of the associated location object.
Delegates to start_character_offset of the associated location object.
: () -> Integer
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 141 def start_column location.start_column end
Delegates to start_column of the associated location object.
Delegates to start_column of the associated location object.
: () -> Integer
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 79 def start_line location.start_line end
Delegates to start_line of the associated location object.
Delegates to start_line of the associated location object.
: () -> Integer
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 93 def start_offset location = @location location.is_a?(Location) ? location.start_offset : location >> 32 end
Delegates to start_offset of the associated location object.
Delegates to start_offset of the associated location object.
: () -> Integer
Node Interface
These methods are effectively abstract methods that are implemented by the various subclasses of Node.
Attributes
An bitset of flags for this node. There are certain flags that are common for all nodes, and then some nodes have specific flags. :stopdoc:
Source
A pointer to the source that this node was created from. :stopdoc:
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 412 def self.type raise NoMethodError, "undefined method `type' for #{inspect}" end
Similar to type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
Similar to type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
: () -> Symbol
Public Instance Methods
(_Visitor visitor) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 349 def accept(visitor) raise NoMethodError, "undefined method `accept' for #{inspect}" end
Accepts a visitor and calls back into the specialized visit function.
Accepts a visitor and calls back into the specialized visit function.
: (_Visitor visitor) -> untyped
() → Array[node?]
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 357 def child_nodes raise NoMethodError, "undefined method `child_nodes' for #{inspect}" end
Returns an array of child nodes, including nils in the place of optional nodes that were not present.
Returns an array of child nodes, including nils in the place of optional nodes that were not present.
: () -> Array
() → Array[node]
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 377 def compact_child_nodes raise NoMethodError, "undefined method `compact_child_nodes' for #{inspect}" end
Returns an array of child nodes, excluding any nils in the place of optional nodes that were not present.
Returns an array of child nodes, excluding any nils in the place of optional nodes that were not present.
: () -> Array
(*String replacements) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/node_ext.rbs, line 10
def deprecated: (*String replacements) -> void
: (*String replacements) -> void
() → Enumerator[node, void]
() { (node) → void } → void
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 369 def each_child_node(&blk) raise NoMethodError, "undefined method `each_child_node' for #{inspect}" end
With a block given, yields each child node. Without a block, returns an enumerator that contains each child node. Excludes any nils in the place of optional nodes that were not present.
With a block given, yields each child node. Without a block, returns an enumerator that contains each child node. Excludes any nils in the place of optional nodes that were not present.
: () -> Enumerator[node, void] : () { (node) -> void } -> void
() → String
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 384 def inspect raise NoMethodError, "undefined method `inspect' for #{inspect}" end
Returns a string representation of the node.
Returns a string representation of the node.
: () -> String
(Array[bool] lines) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/parse_result/newlines.rbs, line 111
def newline_flag!: (Array[bool] lines) -> void
: (Array lines) -> void
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/parse_result/newlines.rbs, line 108
def newline_flag?: () -> bool
: () -> bool
(PP q) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/node.rbs, line 171
def pretty_print: (PP q) -> void
Similar to inspect, but respects the current level of indentation given by the pretty print object.
: (PP q) -> void
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/node.rb, line 402 def type raise NoMethodError, "undefined method `type' for #{inspect}" end
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling [cls1, cls2].include?(node.class) or putting the node into a case statement and doing case node; when cls1; when cls2; end. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if youโre on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling [cls1, cls2].include?(node.class) or putting the node into a case statement and doing case node; when cls1; when cls2; end. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if youโre on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
: () -> Symbol