Class: TreeHaver::Backends::Prism::Node Private
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- TreeHaver::Backends::Prism::Node
- Defined in:
- lib/tree_haver/backends/prism.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Prism node wrapper
Wraps Prism::Node objects to provide tree-sitter-compatible node API.
Prism nodes provide:
- type: class name without "Node" suffix (e.g., ProgramNode → "program")
- location: ::Prism::Location with start/end offsets and line/column
- child_nodes: array of child nodes
- Various node-specific accessors
Instance Attribute Summary
Attributes inherited from TreeHaver::Base::Node
Instance Method Summary collapse
-
#child_by_field_name(name) ⇒ Node?
(also: #field)
private
Get a child by field name (Prism node accessor).
-
#children ⇒ Array<Node>
private
Get all child nodes.
-
#end_byte ⇒ Integer
private
Get byte offset where the node ends.
-
#end_point ⇒ Hash{Symbol => Integer}
private
Get the end position as row/column (0-based).
-
#has_error? ⇒ Boolean
private
Check if this node has errors.
-
#initialize(node, source) ⇒ Node
constructor
private
A new instance of Node.
-
#kind ⇒ String
private
Alias for type (API compatibility).
-
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
private
Delegate unknown methods to the underlying Prism node.
-
#missing? ⇒ Boolean
private
Check if this node is a "missing" node (error recovery).
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
private
Check if node responds to a method (includes delegation to inner_node).
-
#start_byte ⇒ Integer
private
Get byte offset where the node starts.
-
#start_point ⇒ Hash{Symbol => Integer}
private
Get the start position as row/column (0-based).
-
#text ⇒ String
(also: #slice)
private
Get the text content of this node.
-
#to_s ⇒ String
private
String representation.
-
#type ⇒ String
private
Get node type from Prism class name.
Methods inherited from TreeHaver::Base::Node
#<=>, #==, #child, #child_count, #each, #end_line, #first_child, #inspect, #last_child, #named?, #native_type, #next_sibling, #parent, #prev_sibling, #source_position, #start_line
Constructor Details
#initialize(node, source) ⇒ Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Node.
388 389 390 |
# File 'lib/tree_haver/backends/prism.rb', line 388 def initialize(node, source) super(node, source: source) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delegate unknown methods to the underlying Prism node
This provides passthrough access for Prism-specific node methods
like receiver, message, arguments, etc.
553 554 555 556 557 558 559 |
# File 'lib/tree_haver/backends/prism.rb', line 553 def method_missing(method_name, *args, **kwargs, &block) if inner_node.respond_to?(method_name) inner_node.public_send(method_name, *args, **kwargs, &block) else super end end |
Instance Method Details
#child_by_field_name(name) ⇒ Node? Also known as: field
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get a child by field name (Prism node accessor)
Prism nodes have specific accessors for their children.
512 513 514 515 516 517 518 519 520 521 |
# File 'lib/tree_haver/backends/prism.rb', line 512 def child_by_field_name(name) return if inner_node.nil? return unless inner_node.respond_to?(name) result = inner_node.public_send(name) return if result.nil? # Wrap if it's a node result.is_a?(::Prism::Node) ? Node.new(result, source) : nil end |
#children ⇒ Array<Node>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get all child nodes
459 460 461 462 463 464 |
# File 'lib/tree_haver/backends/prism.rb', line 459 def children return [] if inner_node.nil? return [] unless inner_node.respond_to?(:child_nodes) inner_node.child_nodes.compact.map { |n| Node.new(n, source) } end |
#end_byte ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get byte offset where the node ends
425 426 427 428 429 430 |
# File 'lib/tree_haver/backends/prism.rb', line 425 def end_byte return 0 if inner_node.nil? || !inner_node.respond_to?(:location) loc = inner_node.location loc&.end_offset || 0 end |
#end_point ⇒ Hash{Symbol => Integer}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the end position as row/column (0-based)
447 448 449 450 451 452 453 454 |
# File 'lib/tree_haver/backends/prism.rb', line 447 def end_point return { row: 0, column: 0 } if inner_node.nil? || !inner_node.respond_to?(:location) loc = inner_node.location return { row: 0, column: 0 } unless loc { row: (loc.end_line - 1), column: loc.end_column } end |
#has_error? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this node has errors
485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/tree_haver/backends/prism.rb', line 485 def has_error? return false if inner_node.nil? # Check if this is an error node type return true if type.include?('missing') || type.include?('error') # Check children recursively (Prism error nodes are usually children) return false unless inner_node.respond_to?(:child_nodes) inner_node.child_nodes.compact.any? { |n| n.class.name.to_s.include?('Missing') } end |
#kind ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Alias for type (API compatibility)
408 409 410 |
# File 'lib/tree_haver/backends/prism.rb', line 408 def kind type end |
#missing? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if this node is a "missing" node (error recovery)
500 501 502 503 504 |
# File 'lib/tree_haver/backends/prism.rb', line 500 def missing? return false if inner_node.nil? type.include?('missing') end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if node responds to a method (includes delegation to inner_node)
537 538 539 540 541 |
# File 'lib/tree_haver/backends/prism.rb', line 537 def respond_to_missing?(method_name, include_private = false) return false if inner_node.nil? inner_node.respond_to?(method_name, include_private) || super end |
#start_byte ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get byte offset where the node starts
415 416 417 418 419 420 |
# File 'lib/tree_haver/backends/prism.rb', line 415 def start_byte return 0 if inner_node.nil? || !inner_node.respond_to?(:location) loc = inner_node.location loc&.start_offset || 0 end |
#start_point ⇒ Hash{Symbol => Integer}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the start position as row/column (0-based)
435 436 437 438 439 440 441 442 |
# File 'lib/tree_haver/backends/prism.rb', line 435 def start_point return { row: 0, column: 0 } if inner_node.nil? || !inner_node.respond_to?(:location) loc = inner_node.location return { row: 0, column: 0 } unless loc { row: (loc.start_line - 1), column: loc.start_column } end |
#text ⇒ String Also known as: slice
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the text content of this node
469 470 471 472 473 474 475 476 477 |
# File 'lib/tree_haver/backends/prism.rb', line 469 def text return '' if inner_node.nil? if inner_node.respond_to?(:slice) inner_node.slice else super end end |
#to_s ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
String representation
528 529 530 |
# File 'lib/tree_haver/backends/prism.rb', line 528 def to_s text end |
#type ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get node type from Prism class name
Converts PrismClassName to tree-sitter-style type string. Example: CallNode → "call_node", ProgramNode → "program_node"
398 399 400 401 402 403 404 |
# File 'lib/tree_haver/backends/prism.rb', line 398 def type return 'nil' if inner_node.nil? # Convert class name to snake_case type class_name = inner_node.class.name.split('::').last class_name.gsub(/([A-Z])/, '_\1').downcase.sub(/^_/, '') end |