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?, #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_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.
336 337 338 |
# File 'lib/tree_haver/backends/parslet.rb', line 336 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.
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
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_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)
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 |
#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
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_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.
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_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
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.
478 479 480 |
# File 'lib/tree_haver/backends/parslet.rb', line 478 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
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_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
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
485 486 487 |
# File 'lib/tree_haver/backends/parslet.rb', line 485 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
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 |
#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
355 356 357 |
# File 'lib/tree_haver/backends/parslet.rb', line 355 def type @node_type end |