Module: TreeHaver::Backends::Rust

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

Overview

Rust backend using the tree_stump gem

This backend wraps the tree_stump gem, which provides Ruby bindings to tree-sitter written in Rust. It offers native performance with Rust’s safety guarantees and includes precompiled binaries for common platforms.

tree_stump supports incremental parsing and the Query API, making it suitable for editor/IDE use cases where performance is critical.

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 ::TreeStump::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

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

  • TruffleRuby: ✗ magnus/rb-sys incompatible with TruffleRuby’s C API emulation

Defined Under Namespace

Classes: Language, Parser

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tree_haver/backends/rust.rb', line 52

def available?
  return @loaded if @load_attempted # rubocop:disable ThreadSafety/ClassInstanceVariable
  @load_attempted = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  begin
    # tree_stump uses magnus which requires MRI's C API
    # It doesn't work on JRuby or TruffleRuby
    if RUBY_ENGINE == "ruby"
      require "tree_stump"
      @loaded = true # rubocop:disable ThreadSafety/ClassInstanceVariable
    else
      @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
    end
  rescue LoadError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  rescue StandardError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  end
  @loaded # rubocop:disable ThreadSafety/ClassInstanceVariable
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

TreeHaver::Backends::Rust.capabilities
# => { backend: :rust, query: true, bytes_field: true, incremental: false }

Returns:

  • (Hash{Symbol => Object})

    capability map



87
88
89
90
91
92
93
94
95
# File 'lib/tree_haver/backends/rust.rb', line 87

def capabilities
  return {} unless available?
  {
    backend: :rust,
    query: true,
    bytes_field: true,
    incremental: false,  # TreeStump doesn't currently expose incremental parsing to Ruby
  }
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)



76
77
78
79
# File 'lib/tree_haver/backends/rust.rb', line 76

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