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
71
# File 'lib/tree_haver/backends/rust.rb', line 52

def available?
  return @loaded if @load_attempted

  @load_attempted = true
  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
    else
      @loaded = false
    end
  rescue LoadError
    @loaded = false
  rescue StandardError
    @loaded = false
  end
  @loaded
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

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

Returns:

  • (Hash{Symbol => Object})

    capability map



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

def capabilities
  return {} unless available?

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



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

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