Module: TreeHaver::Backends::MRI

Defined in:
lib/tree_haver/backends/mri.rb

Overview

MRI backend using the ruby_tree_sitter gem

This backend wraps the ruby_tree_sitter gem, which is a native C extension for MRI Ruby. It provides the most feature-complete tree-sitter integration on MRI, including support for the Query API.

Tree/Node Architecture

This backend (like all tree-sitter backends: MRI, Rust, FFI, Java) does NOT define its own Tree or Node classes. Instead:

  • Parser#parse returns raw ::TreeSitter::Tree objects

  • These are wrapped by TreeHaver::Tree (inherits from Base::Tree)

  • ‘TreeHaver::Tree#root_node` wraps raw nodes in TreeHaver::Node

This differs from pure-Ruby backends (Citrus, Prism, Psych) which define their own Backend::X::Tree and Backend::X::Node classes.

Platform Compatibility

  • MRI Ruby: ✓ Full support (fastest tree-sitter backend on MRI)

  • JRuby: ✗ Cannot load native C extensions (runs on JVM)

  • TruffleRuby: ✗ C extension not compatible with TruffleRuby

Defined Under Namespace

Classes: Language, Parser

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tree_haver/backends/mri.rb', line 59

def available?
  return @loaded if @load_attempted # rubocop:disable ThreadSafety/ClassInstanceVariable
  @load_attempted = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  begin
    # ruby_tree_sitter is a C extension that only works on MRI
    # It doesn't work on JRuby or TruffleRuby
    if RUBY_ENGINE == "ruby"
      require "tree_sitter"
      @loaded = true # rubocop:disable ThreadSafety/ClassInstanceVariable
    else
      # :nocov: only runs on non-MRI engines (JRuby, TruffleRuby)
      @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
      # :nocov:
    end
  rescue LoadError
    # :nocov: only runs when ruby_tree_sitter gem is not installed
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
    # :nocov:
  rescue StandardError
    # :nocov: defensive code - StandardError during require is extremely rare
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
    # :nocov:
  end
  @loaded # rubocop:disable ThreadSafety/ClassInstanceVariable
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

TreeHaver::Backends::MRI.capabilities
# => { backend: :mri, query: true, bytes_field: true, incremental: true }

Returns:

  • (Hash{Symbol => Object})

    capability map



100
101
102
103
104
105
106
107
108
# File 'lib/tree_haver/backends/mri.rb', line 100

def capabilities
  return {} unless available?
  {
    backend: :mri,
    query: true,
    bytes_field: true,
    incremental: true,
  }
end

.reset!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.

Reset the load state (primarily for testing)



89
90
91
92
# File 'lib/tree_haver/backends/mri.rb', line 89

def reset!
  @load_attempted = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
end