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?, #native_type, #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.



338
339
340
341
342
343
# File 'lib/tree_haver/backends/parslet.rb', line 338

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.



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

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.



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

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



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/tree_haver/backends/parslet.rb', line 443

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



462
463
464
465
466
467
468
469
470
471
# File 'lib/tree_haver/backends/parslet.rb', line 462

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



400
401
402
403
404
405
406
407
408
409
# File 'lib/tree_haver/backends/parslet.rb', line 400

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



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/tree_haver/backends/parslet.rb', line 380

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



421
422
423
# File 'lib/tree_haver/backends/parslet.rb', line 421

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



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

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



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/tree_haver/backends/parslet.rb', line 362

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



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

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



485
486
487
# File 'lib/tree_haver/backends/parslet.rb', line 485

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



427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/tree_haver/backends/parslet.rb', line 427

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



355
356
357
# File 'lib/tree_haver/backends/parslet.rb', line 355

def type
  @node_type
end