Class: TreeHaver::Backends::Parslet::Node Private

Inherits:
TreeHaver::Base::Node show all
Defined in:
lib/tree_haver/backends/parslet.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.

Parslet node wrapper

Wraps Parslet parse results (Hash/Array/Slice) to provide tree-sitter-compatible node API.

Parslet produces different result types:

  • Hash: Named captures like => value, :value => …
  • Array: Repeated captures like […, …]
  • Parslet::Slice: Terminal string values with position info
  • String: Plain strings (less common)

This wrapper normalizes these into a tree-sitter-like node structure.

Inherits from Base::Node to get shared methods like #first_child, #last_child,
#to_s, #inspect, #==, #<=>, #source_position, #start_line, #end_line, etc.

Instance Attribute Summary collapse

Attributes inherited from TreeHaver::Base::Node

#inner_node, #lines, #source

Instance Method Summary collapse

Methods inherited from TreeHaver::Base::Node

#<=>, #==, #child_by_field_name, #each, #end_line, #first_child, #has_error?, #inspect, #last_child, #missing?, #next_sibling, #parent, #prev_sibling, #source_position, #start_line, #to_s

Constructor Details

#initialize(value, source, type: nil, key: nil) ⇒ 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.



333
334
335
336
337
338
# File 'lib/tree_haver/backends/parslet.rb', line 333

def initialize(value, source, type: nil, key: nil)
  @value = value
  @node_type = type || infer_type(key)
  @key = key
  super(value, source: source)
end

Instance Attribute Details

#node_typeObject (readonly)

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.



331
332
333
# File 'lib/tree_haver/backends/parslet.rb', line 331

def node_type
  @node_type
end

#valueObject (readonly)

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.



331
332
333
# File 'lib/tree_haver/backends/parslet.rb', line 331

def value
  @value
end

Instance Method Details

#child(index) ⇒ 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.

Override child to handle negative indices properly

Parameters:

  • index (Integer)

    child index

Returns:

  • (Node, nil)

    child node or nil



438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/tree_haver/backends/parslet.rb', line 438

def child(index)
  return if index.negative?

  case @value
  when Hash
    keys = @value.keys
    return if index >= keys.size
    key = keys[index]
    Node.new(@value[key], @source, key: key)
  when Array
    return if index >= @value.size
    Node.new(@value[index], @source, type: "element")
  end
end

#child_countInteger

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.

Override child_count for efficiency (avoid building full children array)

Returns:

  • (Integer)

    child count



455
456
457
458
459
460
461
462
463
464
# File 'lib/tree_haver/backends/parslet.rb', line 455

def child_count
  case @value
  when Hash
    @value.keys.size
  when Array
    @value.size
  else
    0
  end
end

#childrenArray<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 children

Returns:

  • (Array<Node>)

    child nodes



395
396
397
398
399
400
401
402
403
404
# File 'lib/tree_haver/backends/parslet.rb', line 395

def children
  case @value
  when Hash
    @value.map { |k, v| Node.new(v, @source, key: k) }
  when Array
    @value.map.with_index { |v, i| Node.new(v, @source, type: "element_#{i}") }
  else
    []
  end
end

#end_byteInteger

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 byte offset where this node ends.

Returns:

  • (Integer)

    byte offset where this node ends



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/tree_haver/backends/parslet.rb', line 375

def end_byte
  case @value
  when ::Parslet::Slice
    @value.offset + @value.size
  when Hash
    # Find last slice in hash values
    last_slice = find_last_slice(@value)
    last_slice ? (last_slice.offset + last_slice.size) : @source.length
  when Array
    # Find last slice in array
    last_slice = find_last_slice(@value)
    last_slice ? (last_slice.offset + last_slice.size) : @source.length
  else
    @source.length
  end
end

#end_pointHash{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.

Override end_point to calculate from source

Returns:

  • (Hash{Symbol => Integer})

    0, column: 0



416
417
418
# File 'lib/tree_haver/backends/parslet.rb', line 416

def end_point
  calculate_point(end_byte)
end

#named?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 is named

Hash keys in Parslet results are “named” in tree-sitter terminology.

Returns:

  • (Boolean)

    true if this node has a key



471
472
473
# File 'lib/tree_haver/backends/parslet.rb', line 471

def named?
  !@key.nil? || @value.is_a?(Hash)
end

#start_byteInteger

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 position information from Parslet::Slice if available

Returns:

  • (Integer)

    byte offset where this node starts



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/tree_haver/backends/parslet.rb', line 357

def start_byte
  case @value
  when ::Parslet::Slice
    @value.offset
  when Hash
    # Find first slice in hash values
    first_slice = find_first_slice(@value)
    first_slice&.offset || 0
  when Array
    # Find first slice in array
    first_slice = find_first_slice(@value)
    first_slice&.offset || 0
  else
    0
  end
end

#start_pointHash{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.

Override start_point to calculate from source

Returns:

  • (Hash{Symbol => Integer})

    0, column: 0



410
411
412
# File 'lib/tree_haver/backends/parslet.rb', line 410

def start_point
  calculate_point(start_byte)
end

#structural?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 represents a structural element vs a terminal/token

Returns:

  • (Boolean)

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



478
479
480
# File 'lib/tree_haver/backends/parslet.rb', line 478

def structural?
  @value.is_a?(Hash) || @value.is_a?(Array)
end

#textString

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.

Override text to handle Parslet-specific value types

Returns:

  • (String)

    matched text



422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/tree_haver/backends/parslet.rb', line 422

def text
  case @value
  when ::Parslet::Slice
    @value.to_s
  when String
    @value
  when Hash, Array
    @source[start_byte...end_byte] || ""
  else
    @value.to_s
  end
end

#typeString

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

For Parslet results:

  • Hash keys become node types for their values
  • Arrays become “sequence” type
  • Slices use their parent’s key as type

Returns:

  • (String)

    the node type



350
351
352
# File 'lib/tree_haver/backends/parslet.rb', line 350

def type
  @node_type
end