Class: Prism::Node
- Inherits:
-
Object
- Object
- Prism::Node
- Defined in:
- lib/prism/node.rb,
lib/prism/node_ext.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/node_ext.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/extension.c
Overview
This represents a node in the tree. It is the parent class of all of the various node types.
Instance Attribute Summary collapse
-
#flags ⇒ Integer
readonly
An bitset of flags for this node.
-
#node_id ⇒ Integer
readonly
A unique identifier for this node.
-
#source ⇒ Source
readonly
A pointer to the source that this node was created from.
Class Method Summary collapse
-
.fields ⇒ Array[Reflection::Field]
Returns a list of the fields that exist for this node class.
-
.type ⇒ Symbol
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.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
Accepts a visitor and calls back into the specialized visit function.
-
#breadth_first_search {|arg0| ... } ⇒ node?
(also: #find)
Returns the first node that matches the given block when visited in a breadth-first search.
-
#breadth_first_search_all {|arg0| ... } ⇒ Array[node]
(also: #find_all)
Returns all of the nodes that match the given block when visited in a breadth-first search.
-
#cached_end_code_units_column(cache) ⇒ Integer
Delegates to
cached_end_code_units_columnof the associated location object. -
#cached_end_code_units_offset(cache) ⇒ Integer
Delegates to
cached_end_code_units_offsetof the associated location object. -
#cached_start_code_units_column(cache) ⇒ Integer
Delegates to
cached_start_code_units_columnof the associated location object. -
#cached_start_code_units_offset(cache) ⇒ Integer
Delegates to
cached_start_code_units_offsetof the associated location object. -
#child_nodes ⇒ Array[node?]
(also: #deconstruct)
Returns an array of child nodes, including
nils in the place of optional nodes that were not present. -
#comments ⇒ Array[Comment]
The comments attached to this node, ordered by start offset.
-
#compact_child_nodes ⇒ Array[node]
Returns an array of child nodes, excluding any
nils in the place of optional nodes that were not present. -
#deprecated(*replacements) ⇒ void
: (*String replacements) -> void.
-
#each_child_node {|arg0| ... } ⇒ Object
With a block given, yields each child node.
-
#end_character_column ⇒ Integer
Delegates to
end_character_columnof the associated location object. -
#end_character_offset ⇒ Integer
Delegates to
end_character_offsetof the associated location object. -
#end_column ⇒ Integer
Delegates to
end_columnof the associated location object. -
#end_line ⇒ Integer
Delegates to
end_lineof the associated location object. -
#end_offset ⇒ Integer
Delegates to
end_offsetof the associated location object. -
#inspect ⇒ String
Returns a string representation of the node.
-
#location ⇒ Location
A Location instance that represents the location of this node in the source.
-
#newline? ⇒ Boolean
Returns true if the node has the newline flag set.
-
#newline_flag!(lines) ⇒ void
: (Array lines) -> void.
-
#newline_flag? ⇒ Boolean
: () -> bool.
-
#pretty_print(q) ⇒ void
Similar to inspect, but respects the current level of indentation given by the pretty print object.
-
#save(repository) ⇒ Relocation::Entry
Save this node using a saved source so that it can be retrieved later.
-
#save_location(repository) ⇒ Relocation::Entry
Save the location using a saved source so that it can be retrieved later.
-
#slice ⇒ String
Slice the location of the node from the source.
-
#slice_lines ⇒ String
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_lines ⇒ Array[String]
(also: #script_lines)
Returns all of the lines of the source code associated with this node.
-
#start_character_column ⇒ Integer
Delegates to
start_character_columnof the associated location object. -
#start_character_offset ⇒ Integer
Delegates to
start_character_offsetof the associated location object. -
#start_column ⇒ Integer
Delegates to
start_columnof the associated location object. -
#start_line ⇒ Integer
Delegates to
start_lineof the associated location object. -
#start_offset ⇒ Integer
Delegates to
start_offsetof the associated location object. -
#static_literal? ⇒ Boolean
Returns true if the node has the static literal flag set.
-
#to_dot ⇒ String
Convert this node into a graphviz dot graph string.
-
#tunnel(line, column) ⇒ Array[node]
Returns a list of nodes that are descendants of this node that contain the given line and column.
-
#type ⇒ Symbol
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Instance Attribute Details
#flags ⇒ Integer (readonly)
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:
224 225 226 |
# File 'lib/prism/node.rb', line 224 def flags @flags end |
#node_id ⇒ Integer (readonly)
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.
41 42 43 |
# File 'lib/prism/node.rb', line 41 def node_id @node_id end |
#source ⇒ Source (readonly)
A pointer to the source that this node was created from. :stopdoc:
33 34 35 |
# File 'lib/prism/node.rb', line 33 def source @source end |
Class Method Details
.fields ⇒ Array[Reflection::Field]
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
332 333 334 335 336 337 338 |
# File '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 |
.type ⇒ Symbol
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
412 413 414 |
# File 'lib/prism/node.rb', line 412 def self.type raise NoMethodError, "undefined method `type' for #{inspect}" end |
Instance Method Details
#accept(visitor) ⇒ Object
Accepts a visitor and calls back into the specialized visit function.
: (_Visitor visitor) -> untyped
349 350 351 |
# File 'lib/prism/node.rb', line 349 def accept(visitor) raise NoMethodError, "undefined method `accept' for #{inspect}" end |
#breadth_first_search {|arg0| ... } ⇒ node? Also known as: find
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?
295 296 297 298 299 300 301 302 303 304 |
# File '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 |
#breadth_first_search_all {|arg0| ... } ⇒ Array[node] Also known as: find_all
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
314 315 316 317 318 319 320 321 322 323 324 |
# File '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 |
#cached_end_code_units_column(cache) ⇒ Integer
Delegates to cached_end_code_units_column
of the associated location object.
: (_CodeUnitsCache cache) -> Integer
180 181 182 |
# File 'lib/prism/node.rb', line 180 def cached_end_code_units_column(cache) location.cached_end_code_units_column(cache) end |
#cached_end_code_units_offset(cache) ⇒ Integer
Delegates to cached_end_code_units_offset
of the associated location object.
: (_CodeUnitsCache cache) -> Integer
134 135 136 |
# File 'lib/prism/node.rb', line 134 def cached_end_code_units_offset(cache) location.cached_end_code_units_offset(cache) end |
#cached_start_code_units_column(cache) ⇒ Integer
Delegates to cached_start_code_units_column
of the associated location object.
: (_CodeUnitsCache cache) -> Integer
172 173 174 |
# File 'lib/prism/node.rb', line 172 def cached_start_code_units_column(cache) location.cached_start_code_units_column(cache) end |
#cached_start_code_units_offset(cache) ⇒ Integer
Delegates to cached_start_code_units_offset
of the associated location object.
: (_CodeUnitsCache cache) -> Integer
126 127 128 |
# File 'lib/prism/node.rb', line 126 def cached_start_code_units_offset(cache) location.cached_start_code_units_offset(cache) end |
#child_nodes ⇒ Array[node?] Also known as: deconstruct
Returns an array of child nodes, including nils in the place of optional
nodes that were not present.
: () -> Array
357 358 359 |
# File 'lib/prism/node.rb', line 357 def child_nodes raise NoMethodError, "undefined method `child_nodes' for #{inspect}" end |
#comments ⇒ Array[Comment]
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
188 189 190 |
# File 'lib/prism/node.rb', line 188 def comments source.comments_for(node_id) end |
#compact_child_nodes ⇒ Array[node]
Returns an array of child nodes, excluding any nils in the place of
optional nodes that were not present.
: () -> Array
377 378 379 |
# File 'lib/prism/node.rb', line 377 def compact_child_nodes raise NoMethodError, "undefined method `compact_child_nodes' for #{inspect}" end |
#deprecated(*replacements) ⇒ void
This method returns an undefined value.
: (*String replacements) -> void
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/prism/node_ext.rb', line 13 def deprecated(*replacements) # :nodoc: location = caller_locations(1, 1)&.[](0)&.label suggest = replacements.map { |replacement| "#{self.class}##{replacement}" } warn(<<~MSG, uplevel: 1, category: :deprecated) [deprecation]: #{self.class}##{location} is deprecated and will be \ removed in the next major version. Use #{suggest.join("/")} instead. #{(caller(1, 3) || []).join("\n")} MSG end |
#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void
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
369 370 371 |
# File 'lib/prism/node.rb', line 369 def each_child_node(&blk) raise NoMethodError, "undefined method `each_child_node' for #{inspect}" end |
#end_character_column ⇒ Integer
Delegates to end_character_column
of the associated location object.
: () -> Integer
164 165 166 |
# File 'lib/prism/node.rb', line 164 def end_character_column location.end_character_column end |
#end_character_offset ⇒ Integer
Delegates to end_character_offset
of the associated location object.
: () -> Integer
118 119 120 |
# File 'lib/prism/node.rb', line 118 def end_character_offset location.end_character_offset end |
#end_column ⇒ Integer
Delegates to end_column of the associated location object.
: () -> Integer
148 149 150 |
# File 'lib/prism/node.rb', line 148 def end_column location.end_column end |
#end_line ⇒ Integer
Delegates to end_line of the associated location object.
: () -> Integer
86 87 88 |
# File 'lib/prism/node.rb', line 86 def end_line location.end_line end |
#end_offset ⇒ Integer
Delegates to end_offset of the associated location object.
: () -> Integer
101 102 103 104 |
# File 'lib/prism/node.rb', line 101 def end_offset location = @location location.is_a?(Location) ? location.end_offset : ((location >> 32) + (location & 0xFFFFFFFF)) end |
#inspect ⇒ String
Returns a string representation of the node.
: () -> String
384 385 386 |
# File 'lib/prism/node.rb', line 384 def inspect raise NoMethodError, "undefined method `inspect' for #{inspect}" end |
#location ⇒ Location
A Location instance that represents the location of this node in the source.
: () -> Location
58 59 60 61 62 |
# File '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 |
#newline? ⇒ Boolean
Returns true if the node has the newline flag set.
: () -> bool
231 232 233 |
# File 'lib/prism/node.rb', line 231 def newline? flags.anybits?(NodeFlags::NEWLINE) end |
#newline_flag!(lines) ⇒ void
This method returns an undefined value.
: (Array lines) -> void
218 219 220 221 222 223 224 |
# File 'lib/prism/parse_result/newlines.rb', line 218 def newline_flag!(lines) # :nodoc: line = location.start_line unless lines[line] lines[line] = true @newline_flag = true end end |
#newline_flag? ⇒ Boolean
: () -> bool
213 214 215 |
# File 'lib/prism/parse_result/newlines.rb', line 213 def newline_flag? # :nodoc: !!defined?(@newline_flag) end |
#pretty_print(q) ⇒ void
This method returns an undefined value.
Similar to inspect, but respects the current level of indentation given by the pretty print object.
: (PP q) -> void
246 247 248 249 250 251 |
# File 'lib/prism/node.rb', line 246 def pretty_print(q) # :nodoc: q.seplist(inspect.chomp.each_line, -> { q.breakable }) do |line| q.text(line.chomp) end q.current_group.break end |
#save(repository) ⇒ Relocation::Entry
Save this node using a saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
50 51 52 |
# File 'lib/prism/node.rb', line 50 def save(repository) repository.enter(node_id, :itself) end |
#save_location(repository) ⇒ Relocation::Entry
Save the location using a saved source so that it can be retrieved later.
: (_Repository repository) -> Relocation::Entry
67 68 69 |
# File 'lib/prism/node.rb', line 67 def save_location(repository) repository.enter(node_id, :location) end |
#slice ⇒ String
Slice the location of the node from the source.
: () -> String
208 209 210 |
# File 'lib/prism/node.rb', line 208 def slice location.slice end |
#slice_lines ⇒ String
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
217 218 219 |
# File 'lib/prism/node.rb', line 217 def slice_lines location.slice_lines end |
#source_lines ⇒ Array[String] Also known as: script_lines
Returns all of the lines of the source code associated with this node.
: () -> Array
197 198 199 |
# File 'lib/prism/node.rb', line 197 def source_lines location.source_lines end |
#start_character_column ⇒ Integer
Delegates to start_character_column
of the associated location object.
: () -> Integer
156 157 158 |
# File 'lib/prism/node.rb', line 156 def start_character_column location.start_character_column end |
#start_character_offset ⇒ Integer
Delegates to start_character_offset
of the associated location object.
: () -> Integer
110 111 112 |
# File 'lib/prism/node.rb', line 110 def start_character_offset location.start_character_offset end |
#start_column ⇒ Integer
Delegates to start_column of the associated location object.
: () -> Integer
141 142 143 |
# File 'lib/prism/node.rb', line 141 def start_column location.start_column end |
#start_line ⇒ Integer
Delegates to start_line of the associated location object.
: () -> Integer
79 80 81 |
# File 'lib/prism/node.rb', line 79 def start_line location.start_line end |
#start_offset ⇒ Integer
Delegates to start_offset of the associated location object.
: () -> Integer
93 94 95 96 |
# File 'lib/prism/node.rb', line 93 def start_offset location = @location location.is_a?(Location) ? location.start_offset : location >> 32 end |
#static_literal? ⇒ Boolean
Returns true if the node has the static literal flag set.
: () -> bool
238 239 240 |
# File 'lib/prism/node.rb', line 238 def static_literal? flags.anybits?(NodeFlags::STATIC_LITERAL) end |
#to_dot ⇒ String
Convert this node into a graphviz dot graph string.
: () -> String
256 257 258 259 |
# File 'lib/prism/node.rb', line 256 def to_dot # @type self: node DotVisitor.new.tap { |visitor| accept(visitor) }.to_dot end |
#tunnel(line, column) ⇒ Array[node]
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.
: (Integer line, Integer column) -> Array
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File '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 |
#type ⇒ Symbol
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
402 403 404 |
# File 'lib/prism/node.rb', line 402 def type raise NoMethodError, "undefined method `type' for #{inspect}" end |