Class: TreeHaver::Backends::FFI::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tree_haver/backends/ffi.rb

Overview

FFI-based tree-sitter node

Wraps a TSNode by-value struct. TSNode is passed by value in the tree-sitter C API, so we store the struct value directly.

Instance Method Summary collapse

Constructor Details

#initialize(ts_node_value) ⇒ 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.



613
614
615
616
# File 'lib/tree_haver/backends/ffi.rb', line 613

def initialize(ts_node_value)
  # Store by-value struct (FFI will copy); methods pass it back by value
  @val = ts_node_value
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compare nodes for ordering (used by Comparable module)

Nodes are ordered by their position in the source:

  1. First by start_byte (earlier nodes come first)

  2. Then by end_byte for tie-breaking (shorter spans come first)

  3. Then by type for deterministic ordering



708
709
710
711
712
713
714
715
716
717
718
# File 'lib/tree_haver/backends/ffi.rb', line 708

def <=>(other)
  return unless other.is_a?(Node)

  cmp = start_byte <=> other.start_byte
  return cmp if cmp.nonzero?

  cmp = end_byte <=> other.end_byte
  return cmp if cmp.nonzero?

  type <=> other.type
end

#child(index) ⇒ Node?

Get a child by index



636
637
638
639
640
# File 'lib/tree_haver/backends/ffi.rb', line 636

def child(index)
  return if index >= child_count || index < 0
  child_node = Native.ts_node_child(@val, index)
  Node.new(child_node)
end

#child_countInteger

Get the number of children



628
629
630
# File 'lib/tree_haver/backends/ffi.rb', line 628

def child_count
  Native.ts_node_child_count(@val)
end

#each {|child| ... } ⇒ Enumerator?

Iterate over child nodes

Yield Parameters:

  • child (Node)

    each child node



686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/tree_haver/backends/ffi.rb', line 686

def each
  return enum_for(:each) unless block_given?

  count = child_count
  i = 0
  while i < count
    child = Native.ts_node_child(@val, i)
    yield Node.new(child)
    i += 1
  end
  nil
end

#end_byteInteger

Get end byte offset



652
653
654
# File 'lib/tree_haver/backends/ffi.rb', line 652

def end_byte
  Native.ts_node_end_byte(@val)
end

#end_pointTreeHaver::Point

Get end point



668
669
670
671
672
# File 'lib/tree_haver/backends/ffi.rb', line 668

def end_point
  point = Native.ts_node_end_point(@val)
  # TSPoint is returned by value as an FFI::Struct with :row and :column fields
  TreeHaver::Point.new(point[:row], point[:column])
end

#has_error?Boolean

Check if node has error



677
678
679
680
# File 'lib/tree_haver/backends/ffi.rb', line 677

def has_error?
  # Would need ts_node_has_error implementation
  false
end

#start_byteInteger

Get start byte offset



645
646
647
# File 'lib/tree_haver/backends/ffi.rb', line 645

def start_byte
  Native.ts_node_start_byte(@val)
end

#start_pointTreeHaver::Point

Get start point



659
660
661
662
663
# File 'lib/tree_haver/backends/ffi.rb', line 659

def start_point
  point = Native.ts_node_start_point(@val)
  # TSPoint is returned by value as an FFI::Struct with :row and :column fields
  TreeHaver::Point.new(point[:row], point[:column])
end

#typeString

Get the type name of this node



621
622
623
# File 'lib/tree_haver/backends/ffi.rb', line 621

def type
  Native.ts_node_type(@val)
end