Class: TreeHaver::Backends::FFI::Node
- Inherits:
-
Object
- Object
- TreeHaver::Backends::FFI::Node
- 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
-
#child(index) ⇒ Node?
Get a child by index.
-
#child_count ⇒ Integer
Get the number of children.
-
#each {|child| ... } ⇒ Enumerator?
Iterate over child nodes.
-
#end_byte ⇒ Integer
Get end byte offset.
-
#end_point ⇒ Object
Get end point.
-
#has_error? ⇒ Boolean
Check if node has error.
-
#initialize(ts_node_value) ⇒ Node
constructor
private
A new instance of Node.
-
#start_byte ⇒ Integer
Get start byte offset.
-
#start_point ⇒ Object
Get start point.
-
#type ⇒ String
Get the type name of this node.
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.
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
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_count ⇒ Integer
Get the number of children
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
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_byte ⇒ Integer
Get end byte offset
618 619 620 |
# File 'lib/tree_haver/backends/ffi.rb', line 618 def end_byte Native.ts_node_end_byte(@val) end |
#end_point ⇒ Object
Get end point
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
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_byte ⇒ Integer
Get start byte offset
611 612 613 |
# File 'lib/tree_haver/backends/ffi.rb', line 611 def start_byte Native.ts_node_start_byte(@val) end |
#start_point ⇒ Object
Get start point
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 |
#type ⇒ String
Get the type name of this node
587 588 589 |
# File 'lib/tree_haver/backends/ffi.rb', line 587 def type Native.ts_node_type(@val) end |