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
84
# File 'lib/tree_haver/backends/mri.rb', line 59

def available?
  return @loaded if @load_attempted

  @load_attempted = true
  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
    else
      # simplecov:disable only runs on non-MRI engines (JRuby, TruffleRuby)
      @loaded = false
      # simplecov:enable
    end
  rescue LoadError
    # simplecov:disable only runs when ruby_tree_sitter gem is not installed
    @loaded = false
    # simplecov:enable
  rescue StandardError
    # simplecov:disable defensive code - StandardError during require is extremely rare
    @loaded = false
    # simplecov:enable
  end
  @loaded
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

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

Returns:

  • (Hash{Symbol => Object})

    capability map



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

def capabilities
  return {} unless available?

  {
    backend: :mri,
    query: true,
    bytes_field: true,
    incremental: true,
    comment_support: :nodes_only
  }
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)



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

def reset!
  @load_attempted = false
  @loaded = false
end