Class: TreeHaver::Backends::Parslet::Node Private
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- TreeHaver::Backends::Parslet::Node
- 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
-
#node_type ⇒ Object
readonly
private
-
#value ⇒ Object
readonly
private
Attributes inherited from TreeHaver::Base::Node
Instance Method Summary collapse
-
#child(index) ⇒ Node?
private
Override child to handle negative indices properly.
-
#child_count ⇒ Integer
private
Override child_count for efficiency (avoid building full children array).
-
#children ⇒ Array<Node>
private
Get all children.
-
#end_byte ⇒ Integer
private
Byte offset where this node ends.
-
#end_point ⇒ Hash{Symbol => Integer}
private
Override end_point to calculate from source.
-
#initialize(value, source, type: nil, key: nil) ⇒ Node
constructor
private
A new instance of Node.
-
#named? ⇒ Boolean
private
Check if node is named.
-
#start_byte ⇒ Integer
private
Get position information from Parslet::Slice if available.
-
#start_point ⇒ Hash{Symbol => Integer}
private
Override start_point to calculate from source.
-
#structural? ⇒ Boolean
private
Check if this node represents a structural element vs a terminal/token.
-
#text ⇒ String
private
Override text to handle Parslet-specific value types.
-
#type ⇒ String
private
Get node type.
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_type ⇒ Object (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 |
#value ⇒ Object (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
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_count ⇒ 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 child_count for efficiency (avoid building full children array)
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 |
#children ⇒ Array<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
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_byte ⇒ 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.
Returns 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_point ⇒ Hash{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
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.
471 472 473 |
# File 'lib/tree_haver/backends/parslet.rb', line 471 def named? !@key.nil? || @value.is_a?(Hash) end |
#start_byte ⇒ 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.
Get position information from Parslet::Slice if available
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_point ⇒ Hash{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
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
478 479 480 |
# File 'lib/tree_haver/backends/parslet.rb', line 478 def structural? @value.is_a?(Hash) || @value.is_a?(Array) end |
#text ⇒ String
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
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 |
#type ⇒ String
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
350 351 352 |
# File 'lib/tree_haver/backends/parslet.rb', line 350 def type @node_type end |