class Prism::Node
This represents a node in the tree. It is the parent class of all of the various node types.
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.
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 pointer to the source that this node was created from.
Public Class Methods
Source
# File lib/prism/node.rb, line 245defself.fields# This method should only be called on subclasses of Node, not Node# itself.raiseNoMethodError,"undefined method `fields' for #{inspect}"ifself==NodeReflection.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 nodeand field in the tree.
Public Instance Methods
Source
# File lib/prism/node.rb, line 231defbreadth_first_search(&block)queue = [self]#: Array[Prism::node]while (node =queue.shift)returnnodeifyieldnodequeue.concat(node.compact_child_nodes)endnilend
Returns the first node that matches the given block when visited in a depth-first search. This is useful for finding a node that matches a particular condition.
node.breadth_first_search {|node|node.node_id==node_id }
Source
# File lib/prism/node.rb, line 118defcached_end_code_units_column(cache)location.cached_end_code_units_column(cache)end
Delegates to thecached_end_code_units_column of the associated location object.
Source
# File lib/prism/node.rb, line 86defcached_end_code_units_offset(cache)location.cached_end_code_units_offset(cache)end
Delegates to thecached_end_code_units_offset of the associated location object.
Source
# File lib/prism/node.rb, line 112defcached_start_code_units_column(cache)location.cached_start_code_units_column(cache)end
Delegates to thecached_start_code_units_column of the associated location object.
Source
# File lib/prism/node.rb, line 80defcached_start_code_units_offset(cache)location.cached_start_code_units_offset(cache)end
Delegates to thecached_start_code_units_offset of the associated location object.
Source
# File lib/prism/node.rb, line 133defcommentslocation.commentsend
Delegates to the comments of the associated location object.
Source
# File lib/prism/node.rb, line 106defend_character_columnlocation.end_character_columnend
Delegates to theend_character_column of the associated location object.
Source
# File lib/prism/node.rb, line 74defend_character_offsetlocation.end_character_offsetend
Delegates to theend_character_offset of the associated location object.
Source
# File lib/prism/node.rb, line 96defend_columnlocation.end_columnend
Delegates to theend_column of the associated location object.
Source
# File lib/prism/node.rb, line 50defend_linelocation.end_lineend
Delegates to theend_line of the associated location object.
Source
# File lib/prism/node.rb, line 63defend_offsetlocation =@locationlocation.is_a?(Location)?location.end_offset: ((location>>32)+ (location&0xFFFFFFFF))end
The end offset of the node in the source. This method is effectively a delegate method to the location object.
Source
# File lib/prism/node.rb, line 123defleading_commentslocation.leading_commentsend
Delegates to theleading_comments of the associated location object.
Source
# File lib/prism/node.rb, line 33deflocationlocation =@locationreturnlocationiflocation.is_a?(Location)@location =Location.new(source,location>>32,location&0xFFFFFFFF)end
ALocation instance that represents the location of this node in the source.
Source
# File lib/prism/node.rb, line 164defnewline?flags.anybits?(NodeFlags::NEWLINE)end
Returns true if the node has the newline flag set.
Source
# File lib/prism/node.rb, line 175defpretty_print(q)q.seplist(inspect.chomp.each_line,-> {q.breakable })do|line|q.text(line.chomp)endq.current_group.breakend
Similar to inspect, but respects the current level of indentation given by the pretty print object.
Source
# File lib/prism/node.rb, line 27defsave(repository)repository.enter(node_id,:itself)end
Save this node using a saved source so that it can be retrieved later.
Source
# File lib/prism/node.rb, line 40defsave_location(repository)repository.enter(node_id,:location)end
Save the location using a saved source so that it can be retrieved later.
An alias forsource_lines, used to mimic the API fromRubyVM::AbstractSyntaxTree to make it easier to migrate.
Source
# File lib/prism/node.rb, line 147defslicelocation.sliceend
Slice the location of the node from the source.
Source
# File lib/prism/node.rb, line 154defslice_lineslocation.slice_linesend
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.
Source
# File lib/prism/node.rb, line 138defsource_lineslocation.source_linesend
Returns all of the lines of the source code associated with this node.
Source
# File lib/prism/node.rb, line 101defstart_character_columnlocation.start_character_columnend
Delegates to thestart_character_column of the associated location object.
Source
# File lib/prism/node.rb, line 69defstart_character_offsetlocation.start_character_offsetend
Delegates to thestart_character_offset of the associated location object.
Source
# File lib/prism/node.rb, line 91defstart_columnlocation.start_columnend
Delegates to thestart_column of the associated location object.
Source
# File lib/prism/node.rb, line 45defstart_linelocation.start_lineend
Delegates to thestart_line of the associated location object.
Source
# File lib/prism/node.rb, line 56defstart_offsetlocation =@locationlocation.is_a?(Location)?location.start_offset:location>>32end
The start offset of the node in the source. This method is effectively a delegate method to the location object.
Source
# File lib/prism/node.rb, line 169defstatic_literal?flags.anybits?(NodeFlags::STATIC_LITERAL)end
Returns true if the node has the static literal flag set.
Source
# File lib/prism/node.rb, line 183defto_dot# @type self: nodeDotVisitor.new.tap {|visitor|accept(visitor) }.to_dotend
Convert this node into a graphviz dot graph string.
Source
# File lib/prism/node.rb, line 128deftrailing_commentslocation.trailing_commentsend
Delegates to thetrailing_comments of the associated location object.
Source
# File lib/prism/node.rb, line 194deftunnel(line,column)queue = [self]#: Array[Prism::node]result = []#: Array[Prism::node]while (node =queue.shift)result<<nodenode.compact_child_nodes.eachdo|child_node|child_location =child_node.locationstart_line =child_location.start_lineend_line =child_location.end_lineifstart_line==end_lineifline==start_line&&column>=child_location.start_column&&column<child_location.end_columnqueue<<child_nodebreakendelsif (line==start_line&&column>=child_location.start_column)|| (line==end_line&&column<child_location.end_column)queue<<child_nodebreakelsifline>start_line&&line<end_linequeue<<child_nodebreakendendendresultend
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.
Node interface
Public Class Methods
Source
# File lib/prism/node.rb, line 310defself.typeraiseNoMethodError,"undefined method `type' for #{inspect}"end
Similar totype, 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 liketype, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
Public Instance Methods
Source
# File lib/prism/node.rb, line 261defaccept(visitor)raiseNoMethodError,"undefined method `accept' for #{inspect}"end
Accepts a visitor and calls back into the specialized visit function.
Source
# File lib/prism/node.rb, line 267defchild_nodesraiseNoMethodError,"undefined method `child_nodes' for #{inspect}"end
Returns an array of child nodes, includingnils in the place of optional nodes that were not present.
Source
# File lib/prism/node.rb, line 281defcomment_targetsraiseNoMethodError,"undefined method `comment_targets' for #{inspect}"end
Returns an array of child nodes and locations that could potentially have comments attached to them.
Source
# File lib/prism/node.rb, line 275defcompact_child_nodesraiseNoMethodError,"undefined method `compact_child_nodes' for #{inspect}"end
Returns an array of child nodes, excluding anynils in the place of optional nodes that were not present.
Source
# File lib/prism/node.rb, line 286definspectraiseNoMethodError,"undefined method `inspect' for #{inspect}"end
Returns a string representation of the node.
Source
# File lib/prism/node.rb, line 302deftyperaiseNoMethodError,"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 doingcase 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 calltype, 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.