Class: TreeHaver::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/tree_haver/node.rb

Overview

Note:

This is the key to tree_haver’s “write once, run anywhere” promise

Unified Node wrapper providing a consistent API across all backends

This class wraps backend-specific node objects (TreeSitter::Node, TreeStump::Node, etc.) and provides a unified interface so code works identically regardless of which backend is being used.

The wrapper automatically maps backend differences:

  • TreeStump uses node.kind → mapped to node.type

  • TreeStump uses node.is_named? → mapped to node.named?

  • All backends return consistent Point objects from position methods

Examples:

Basic node traversal

tree = parser.parse(source)
root = tree.root_node

puts root.type        # => "document"
puts root.start_byte  # => 0
puts root.text        # => full source text

root.children.each do |child|
  puts "#{child.type} at line #{child.start_point.row + 1}"
end

Position information

node = tree.root_node.children.first

# Point objects work as both objects and hashes
point = node.start_point
point.row              # => 0 (method access)
point[:row]            # => 0 (hash access)
point.column           # => 0

# Byte offsets
node.start_byte        # => 0
node.end_byte          # => 23

Error detection

if node.has_error?
  puts "Parse error in subtree"
end

if node.missing?
  puts "This node was inserted by error recovery"
end

Accessing backend-specific features

# Via passthrough (method_missing delegates to inner_node)
node.grammar_name  # TreeStump-specific, automatically delegated

# Or explicitly via inner_node
node.inner_node.grammar_name  # Same result

# Check if backend supports a feature
if node.inner_node.respond_to?(:some_feature)
  node.some_feature
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, source: nil) ⇒ Node

Returns a new instance of Node.

Parameters:

  • node (Object)

    Backend-specific node object

  • source (String) (defaults to: nil)

    Source text for text extraction



94
95
96
97
# File 'lib/tree_haver/node.rb', line 94

def initialize(node, source: nil)
  @inner_node = 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

Note:

This maintains backward compatibility with code written for specific backends while providing the benefits of the unified API

Delegate unknown methods to the underlying backend-specific node

This provides passthrough access for advanced usage when you need backend-specific features not exposed by TreeHaver’s unified API.

The delegation is automatic and transparent - you can call backend-specific methods directly on the TreeHaver::Node and they’ll be forwarded to the underlying node implementation.

Examples:

Using TreeStump-specific methods

# These methods don't exist in the unified API but are in TreeStump
node.grammar_name      # => "toml" (delegated to inner_node)
node.grammar_id        # => Integer (delegated to inner_node)
node.kind_id           # => Integer (delegated to inner_node)

Safe usage with respond_to? check

if node.respond_to?(:grammar_name)
  puts "Using #{node.grammar_name} grammar"
end

Equivalent explicit access

node.grammar_name              # Via passthrough (method_missing)
node.inner_node.grammar_name   # Explicit access (same result)

Parameters:

  • method_name (Symbol)

    method to call

  • args (Array)

    arguments to pass

  • block (Proc)

    block to pass

Returns:

  • (Object)

    result from the underlying node



568
569
570
571
572
573
574
# File 'lib/tree_haver/node.rb', line 568

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 Attribute Details

#inner_nodeObject (readonly)

The wrapped backend-specific node object

This provides direct access to the underlying backend node for advanced usage when you need backend-specific features not exposed by the unified API.

Examples:

Accessing backend-specific methods

# TreeStump-specific: grammar information
if node.inner_node.respond_to?(:grammar_name)
  puts node.inner_node.grammar_name  # => "toml"
  puts node.inner_node.grammar_id    # => Integer
end

# Check backend type
case node.inner_node.class.name
when /TreeStump/
  # TreeStump-specific code
when /TreeSitter/
  # ruby_tree_sitter-specific code
end

Returns:

  • (Object)

    The underlying node (TreeSitter::Node, TreeStump::Node, etc.)



86
87
88
# File 'lib/tree_haver/node.rb', line 86

def inner_node
  @inner_node
end

#sourceString (readonly)

The source text for text extraction

Returns:

  • (String)


90
91
92
# File 'lib/tree_haver/node.rb', line 90

def source
  @source
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

This allows nodes to be sorted by position and used in sorted collections. The Comparable module provides <, <=, ==, >=, >, and between? based on this.

Parameters:

  • other (Node)

    node to compare with

Returns:

  • (Integer, nil)

    -1, 0, 1, or nil if not comparable



483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/tree_haver/node.rb', line 483

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

  # Compare by position first (start_byte, then end_byte)
  cmp = start_byte <=> other.start_byte
  return cmp if cmp.nonzero?

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

  # For nodes at the same position with same span, compare by type
  type <=> other.type
end

#==(other) ⇒ Boolean Also known as: eql?

Check equality based on inner_node identity

Two nodes are equal if they wrap the same backend node object. This is separate from the <=> comparison which orders by position. Nodes at the same position but wrapping different backend nodes are equal according to <=> (positional equality) but not equal according to == (identity equality).

Note: We override Comparable’s default == behavior to check inner_node identity rather than just relying on <=> returning 0, because we want identity-based equality for testing and collection membership, not position-based equality.

Parameters:

  • other (Object)

    object to compare with

Returns:

  • (Boolean)

    true if both nodes wrap the same inner_node



510
511
512
513
# File 'lib/tree_haver/node.rb', line 510

def ==(other)
  return false unless other.is_a?(Node)
  @inner_node == other.inner_node
end

#child(index) ⇒ Node?

Get a child by index

Parameters:

  • index (Integer)

    Child index

Returns:

  • (Node, nil)

    Wrapped child node, or nil if index out of bounds



308
309
310
311
312
313
314
315
# File 'lib/tree_haver/node.rb', line 308

def child(index)
  child_node = @inner_node.child(index)
  return if child_node.nil?
  Node.new(child_node, source: @source)
rescue IndexError
  # Some backends (e.g., MRI w/ ruby_tree_sitter) raise IndexError for out of bounds
  nil
end

#child_by_field_name(name) ⇒ Node? Also known as: field

Get a child by field name

Parameters:

  • name (String, Symbol)

    Field name

Returns:

  • (Node, nil)

    The child node for that field



415
416
417
418
419
420
421
422
423
424
# File 'lib/tree_haver/node.rb', line 415

def child_by_field_name(name)
  if @inner_node.respond_to?(:child_by_field_name)
    child_node = @inner_node.child_by_field_name(name.to_s)
    return if child_node.nil?
    Node.new(child_node, source: @source)
  else
    # Not all backends support field names
    nil
  end
end

#child_countInteger

Get the number of children

Returns:

  • (Integer)


300
301
302
# File 'lib/tree_haver/node.rb', line 300

def child_count
  @inner_node.child_count
end

#childrenArray<Node>

Get all children as wrapped nodes

Returns:

  • (Array<Node>)

    Array of wrapped child nodes



391
392
393
# File 'lib/tree_haver/node.rb', line 391

def children
  (0...child_count).map { |i| child(i) }.compact
end

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

Iterate over children

Yields:

  • (Node)

    Each child node

Returns:

  • (Enumerator, nil)


406
407
408
409
# File 'lib/tree_haver/node.rb', line 406

def each(&block)
  return to_enum(__method__) unless block_given?
  children.each(&block)
end

#end_byteInteger

Get the node’s end byte offset

Returns:

  • (Integer)


125
126
127
# File 'lib/tree_haver/node.rb', line 125

def end_byte
  @inner_node.end_byte
end

#end_lineInteger

Get the 1-based line number where this node ends

Convenience method that converts 0-based row to 1-based line number.

Returns:

  • (Integer)

    1-based line number



194
195
196
# File 'lib/tree_haver/node.rb', line 194

def end_line
  end_point.row + 1
end

#end_pointPoint

Get the node’s end position (row, column)

Returns:

  • (Point)

    with row and column accessors (also works as Hash)



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/tree_haver/node.rb', line 157

def end_point
  if @inner_node.respond_to?(:end_point)
    point = @inner_node.end_point
    # Handle both Point objects and hashes
    if point.is_a?(Hash)
      Point.new(point[:row], point[:column])
    else
      Point.new(point.row, point.column)
    end
  elsif @inner_node.respond_to?(:end_position)
    point = @inner_node.end_position
    # Handle both Point objects and hashes
    if point.is_a?(Hash)
      Point.new(point[:row], point[:column])
    else
      Point.new(point.row, point.column)
    end
  else
    raise TreeHaver::Error, "Backend node does not support end_point/end_position"
  end
end

#first_childNode?

Get the first child node

Convenience method for iteration patterns that expect first_child.

Returns:

  • (Node, nil)

    First child node or nil if no children



221
222
223
# File 'lib/tree_haver/node.rb', line 221

def first_child
  child(0)
end

#has_error?Boolean

Check if the node has an error

Returns:

  • (Boolean)


255
256
257
# File 'lib/tree_haver/node.rb', line 255

def has_error?
  @inner_node.has_error?
end

#hashInteger

Generate hash value for this node

Uses the hash of the inner_node to ensure nodes wrapping the same backend node have the same hash value.

Returns:

  • (Integer)

    hash value



524
525
526
# File 'lib/tree_haver/node.rb', line 524

def hash
  @inner_node.hash
end

#inspectString

String representation for debugging

Returns:

  • (String)


461
462
463
# File 'lib/tree_haver/node.rb', line 461

def inspect
  "#<#{self.class} type=#{type} bytes=#{start_byte}..#{end_byte}>"
end

#missing?Boolean

Check if the node is missing

Returns:

  • (Boolean)


261
262
263
264
# File 'lib/tree_haver/node.rb', line 261

def missing?
  return false unless @inner_node.respond_to?(:missing?)
  @inner_node.missing?
end

#named?Boolean

Check if the node is named

Returns:

  • (Boolean)


268
269
270
271
272
273
274
275
276
# File 'lib/tree_haver/node.rb', line 268

def named?
  if @inner_node.respond_to?(:named?)
    @inner_node.named?
  elsif @inner_node.respond_to?(:is_named?)
    @inner_node.is_named?
  else
    true # Default to true if not supported
  end
end

#named_child(index) ⇒ Node?

Get a named child by index

Returns the nth named child (skipping unnamed children). Uses backend’s native named_child if available, otherwise provides fallback.

Parameters:

  • index (Integer)

    Named child index (0-based)

Returns:

  • (Node, nil)

    Wrapped named child node, or nil if index out of bounds



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/tree_haver/node.rb', line 324

def named_child(index)
  # Try native implementation first
  if @inner_node.respond_to?(:named_child)
    child_node = @inner_node.named_child(index)
    return if child_node.nil?
    return Node.new(child_node, source: @source)
  end

  # Fallback: manually iterate through children and count named ones
  named_count = 0
  (0...child_count).each do |i|
    child_node = @inner_node.child(i)
    next if child_node.nil?

    # Check if this child is named
    is_named = if child_node.respond_to?(:named?)
      child_node.named?
    elsif child_node.respond_to?(:is_named?)
      child_node.is_named?
    else
      true  # Assume named if we can't determine
    end

    if is_named
      return Node.new(child_node, source: @source) if named_count == index
      named_count += 1
    end
  end

  nil  # Index out of bounds
end

#named_child_countInteger

Get the count of named children

Uses backend’s native named_child_count if available, otherwise provides fallback.

Returns:

  • (Integer)

    Number of named children



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/tree_haver/node.rb', line 361

def named_child_count
  # Try native implementation first
  if @inner_node.respond_to?(:named_child_count)
    return @inner_node.named_child_count
  end

  # Fallback: count named children manually
  count = 0
  (0...child_count).each do |i|
    child_node = @inner_node.child(i)
    next if child_node.nil?

    # Check if this child is named
    is_named = if child_node.respond_to?(:named?)
      child_node.named?
    elsif child_node.respond_to?(:is_named?)
      child_node.is_named?
    else
      true  # Assume named if we can't determine
    end

    count += 1 if is_named
  end

  count
end

#named_childrenArray<Node>

Get named children only

Returns:

  • (Array<Node>)

    Array of named child nodes



398
399
400
# File 'lib/tree_haver/node.rb', line 398

def named_children
  children.select(&:named?)
end

#next_siblingNode?

Get next sibling

Returns:



442
443
444
445
446
447
# File 'lib/tree_haver/node.rb', line 442

def next_sibling
  return unless @inner_node.respond_to?(:next_sibling)
  sibling = @inner_node.next_sibling
  return if sibling.nil?
  Node.new(sibling, source: @source)
end

#parentNode?

Get the parent node

Returns:

  • (Node, nil)

    The parent node



432
433
434
435
436
437
# File 'lib/tree_haver/node.rb', line 432

def parent
  return unless @inner_node.respond_to?(:parent)
  parent_node = @inner_node.parent
  return if parent_node.nil?
  Node.new(parent_node, source: @source)
end

#prev_siblingNode?

Get previous sibling

Returns:



452
453
454
455
456
457
# File 'lib/tree_haver/node.rb', line 452

def prev_sibling
  return unless @inner_node.respond_to?(:prev_sibling)
  sibling = @inner_node.prev_sibling
  return if sibling.nil?
  Node.new(sibling, source: @source)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Check if node responds to a method (includes delegation to inner_node)

Parameters:

  • method_name (Symbol)

    method to check

  • include_private (Boolean) (defaults to: false)

    include private methods

Returns:

  • (Boolean)


533
534
535
# File 'lib/tree_haver/node.rb', line 533

def respond_to_missing?(method_name, include_private = false)
  @inner_node.respond_to?(method_name, include_private) || super
end

#source_positionHash{Symbol => Integer}

Get position information as a hash

Returns a hash with 1-based line numbers and 0-based columns. This format is compatible with *-merge gems’ FileAnalysisBase.

Examples:

node.source_position
# => { start_line: 1, end_line: 3, start_column: 0, end_column: 10 }

Returns:

  • (Hash{Symbol => Integer})

    Position hash



207
208
209
210
211
212
213
214
# File 'lib/tree_haver/node.rb', line 207

def source_position
  {
    start_line: start_line,
    end_line: end_line,
    start_column: start_point.column,
    end_column: end_point.column,
  }
end

#start_byteInteger

Get the node’s start byte offset

Returns:

  • (Integer)


119
120
121
# File 'lib/tree_haver/node.rb', line 119

def start_byte
  @inner_node.start_byte
end

#start_lineInteger

Get the 1-based line number where this node starts

Convenience method that converts 0-based row to 1-based line number. This is useful for error messages and matching with editor line numbers.

Returns:

  • (Integer)

    1-based line number



185
186
187
# File 'lib/tree_haver/node.rb', line 185

def start_line
  start_point.row + 1
end

#start_pointPoint

Get the node’s start position (row, column)

Returns:

  • (Point)

    with row and column accessors (also works as Hash)



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/tree_haver/node.rb', line 132

def start_point
  if @inner_node.respond_to?(:start_point)
    point = @inner_node.start_point
    # Handle both Point objects and hashes
    if point.is_a?(Hash)
      Point.new(point[:row], point[:column])
    else
      Point.new(point.row, point.column)
    end
  elsif @inner_node.respond_to?(:start_position)
    point = @inner_node.start_position
    # Handle both Point objects and hashes
    if point.is_a?(Hash)
      Point.new(point[:row], point[:column])
    else
      Point.new(point.row, point.column)
    end
  else
    raise TreeHaver::Error, "Backend node does not support start_point/start_position"
  end
end

#structural?Boolean

Check if the node is structural (non-terminal)

In tree-sitter, this is equivalent to being a “named” node. Named nodes represent actual syntactic constructs (e.g., table, keyvalue, string) while anonymous nodes are syntax/punctuation (e.g., [, =, whitespace).

For Citrus backends, this checks if the node is a non-terminal rule.

Returns:

  • (Boolean)

    true if this is a structural (non-terminal) node



287
288
289
290
291
292
293
294
295
296
# File 'lib/tree_haver/node.rb', line 287

def structural?
  # Delegate to inner_node if it has its own structural? method (e.g., Citrus)
  if @inner_node.respond_to?(:structural?)
    @inner_node.structural?
  else
    # For tree-sitter backends, named? is equivalent to structural?
    # Named nodes are syntactic constructs; anonymous nodes are punctuation
    named?
  end
end

#textString

Get the node’s text content

Returns:

  • (String)


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/tree_haver/node.rb', line 228

def text
  if @inner_node.respond_to?(:text)
    # Some backends (like TreeStump) require source as argument
    # Check arity to determine how to call
    arity = @inner_node.method(:text).arity
    if arity == 0 || arity == -1
      # No required arguments, or optional arguments only
      @inner_node.text
    elsif arity >= 1 && @source
      # Has required argument(s) - pass source
      @inner_node.text(@source)
    elsif @source
      # Fallback to byte extraction
      @source[start_byte...end_byte] || ""
    else
      raise TreeHaver::Error, "Cannot extract text: backend requires source but none provided"
    end
  elsif @source
    # Fallback: extract from source using byte positions
    @source[start_byte...end_byte] || ""
  else
    raise TreeHaver::Error, "Cannot extract text: node has no text method and no source provided"
  end
end

#to_sString

String representation

Returns:

  • (String)


467
468
469
# File 'lib/tree_haver/node.rb', line 467

def to_s
  text
end

#typeString

Get the node’s type/kind as a string

Maps backend-specific methods to a unified API:

  • ruby_tree_sitter: node.type

  • tree_stump: node.kind

  • FFI: node.type

Returns:

  • (String)

    The node type



107
108
109
110
111
112
113
114
115
# File 'lib/tree_haver/node.rb', line 107

def type
  if @inner_node.respond_to?(:type)
    @inner_node.type.to_s
  elsif @inner_node.respond_to?(:kind)
    @inner_node.kind.to_s
  else
    raise TreeHaver::Error, "Backend node does not support type/kind"
  end
end