Class: TreeHaver::Backends::Java::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_haver/backends/java.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.

Java backend parser wrapper (raw backend parser)

This is a **raw backend parser** that wraps a jtreesitter Parser object via JRuby’s Java interop. It is NOT intended for direct use by application code.

Users should use TreeHaver::Parser which wraps this class and provides:

  • Automatic backend selection

  • Language wrapper unwrapping

  • Tree wrapping with source storage

  • Unified API across all backends

Instance Method Summary collapse

Constructor Details

#initializeParser

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.

Create a new parser instance

Raises:



495
496
497
498
# File 'lib/tree_haver/backends/java.rb', line 495

def initialize
  raise TreeHaver::NotAvailable, "Java backend not available" unless Java.available?
  @parser = Java.java_classes[:Parser].new
end

Instance Method Details

#language=(lang) ⇒ void

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.

This method returns an undefined value.

Set the language for this parser

Note: TreeHaver::Parser unwraps language objects before calling this method. This backend receives the Language wrapper’s inner impl (java Language object).

Parameters:

  • lang (Object)

    the Java language object (already unwrapped)



507
508
509
510
# File 'lib/tree_haver/backends/java.rb', line 507

def language=(lang)
  # lang is already unwrapped by TreeHaver::Parser
  @parser.language = lang
end

#parse(source) ⇒ Tree

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.

Parse source code

Parameters:

  • source (String)

    the source code to parse

Returns:

  • (Tree)

    raw backend tree (wrapping happens in TreeHaver::Parser)

Raises:



516
517
518
519
520
521
522
# File 'lib/tree_haver/backends/java.rb', line 516

def parse(source)
  java_result = @parser.parse(source)
  # jtreesitter 0.26.0 returns Optional<Tree>
  java_tree = unwrap_optional(java_result)
  raise TreeHaver::Error, "Parser returned no tree" unless java_tree
  Tree.new(java_tree)
end

#parse_string(old_tree, source) ⇒ Tree

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.

Parse source code with optional incremental parsing

Note: old_tree is already unwrapped by TreeHaver::Parser before reaching this method. The backend receives the raw Tree wrapper’s impl, not a TreeHaver::Tree.

When old_tree is provided and has been edited, tree-sitter will reuse unchanged nodes for better performance.

Parameters:

  • old_tree (Tree, nil)

    previous backend tree for incremental parsing (already unwrapped)

  • source (String)

    the source code to parse

Returns:

  • (Tree)

    raw backend tree (wrapping happens in TreeHaver::Parser)

Raises:

See Also:



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/tree_haver/backends/java.rb', line 536

def parse_string(old_tree, source)
  # old_tree is already unwrapped to Tree wrapper's impl by TreeHaver::Parser
  if old_tree
    # Get the actual Java Tree object
    java_old_tree = if old_tree.is_a?(Tree)
      old_tree.impl
    else
      unwrap_optional(old_tree)
    end

    java_result = if java_old_tree
      # jtreesitter 0.26.0 API: parse(String source, Tree oldTree)
      @parser.parse(source, java_old_tree)
    else
      @parser.parse(source)
    end
  else
    java_result = @parser.parse(source)
  end
  # jtreesitter 0.26.0 returns Optional<Tree>
  java_tree = unwrap_optional(java_result)
  raise TreeHaver::Error, "Parser returned no tree" unless java_tree
  Tree.new(java_tree)
end