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

Inherits:
Object
  • Object
show all
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.

Parameters:

  • ts_node_value (Native::TSNode)

    the TSNode struct (by value)



579
580
581
582
# File 'lib/tree_haver/backends/ffi.rb', line 579

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

#child(index) ⇒ Node?

Get a child by index

Parameters:

  • index (Integer)

    child index

Returns:

  • (Node, nil)

    child node or nil if index out of bounds



602
603
604
605
606
# File 'lib/tree_haver/backends/ffi.rb', line 602

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

Returns:

  • (Integer)

    child count



594
595
596
# File 'lib/tree_haver/backends/ffi.rb', line 594

def child_count
  Native.ts_node_child_count(@val)
end

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

Iterate over child nodes

Yield Parameters:

  • child (Node)

    each child node

Returns:

  • (Enumerator, nil)

    an enumerator if no block given, nil otherwise



652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/tree_haver/backends/ffi.rb', line 652

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

Returns:

  • (Integer)


618
619
620
# File 'lib/tree_haver/backends/ffi.rb', line 618

def end_byte
  Native.ts_node_end_byte(@val)
end

#end_pointObject

Get end point

Returns:

  • (Object)

    with row and column



634
635
636
637
638
# File 'lib/tree_haver/backends/ffi.rb', line 634

def end_point
  # FFI backend would need to implement ts_node_end_point
  # For now, return a simple struct
  Struct.new(:row, :column).new(0, Native.ts_node_end_byte(@val))
end

#has_error?Boolean

Check if node has error

Returns:

  • (Boolean)


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

def has_error?
  # Would need ts_node_has_error implementation
  false
end

#start_byteInteger

Get start byte offset

Returns:

  • (Integer)


611
612
613
# File 'lib/tree_haver/backends/ffi.rb', line 611

def start_byte
  Native.ts_node_start_byte(@val)
end

#start_pointObject

Get start point

Returns:

  • (Object)

    with row and column



625
626
627
628
629
# File 'lib/tree_haver/backends/ffi.rb', line 625

def start_point
  # FFI backend would need to implement ts_node_start_point
  # For now, return a simple struct
  Struct.new(:row, :column).new(0, Native.ts_node_start_byte(@val))
end

#typeString

Get the type name of this node

Returns:

  • (String)

    the node type (e.g., “document”, “table”, “pair”)



587
588
589
# File 'lib/tree_haver/backends/ffi.rb', line 587

def type
  Native.ts_node_type(@val)
end