Module: TreeHaver::Backends::FFI
- Defined in:
- lib/tree_haver/backends/ffi.rb
Overview
Requires the ‘ffi` gem and libtree-sitter shared library to be installed
FFI-based backend for calling libtree-sitter directly
This backend uses Ruby FFI (JNR-FFI on JRuby) to call the native tree-sitter C library without requiring MRI C extensions. This makes it compatible with JRuby, TruffleRuby, and other Ruby implementations that support FFI.
The FFI backend currently supports:
-
Parsing source code
-
AST node traversal
-
Accessing node types and children
Not yet supported:
-
Query API (tree-sitter queries/patterns)
Defined Under Namespace
Modules: Native Classes: Language, Node, Parser, Tree
Class Method Summary collapse
-
.available? ⇒ Boolean
Check if the FFI backend is available.
-
.capabilities ⇒ Hash{Symbol => Object}
Get capabilities supported by this backend.
-
.ffi_gem_available? ⇒ Boolean
private
Check if the FFI gem can be loaded.
-
.reset! ⇒ void
private
Reset the load state (primarily for testing).
Class Method Details
.available? ⇒ Boolean
Check if the FFI backend is available
Returns true if:
-
The ‘ffi` gem is present
-
MRI backend (ruby_tree_sitter) has NOT been loaded
FFI and MRI backends conflict at the libtree-sitter level. Once MRI loads, using FFI will cause segfaults.
198 199 200 201 202 203 |
# File 'lib/tree_haver/backends/ffi.rb', line 198 def available? return false unless TreeHaver::Backends::FFI.ffi_gem_available? # Check if MRI backend has been loaded (which blocks FFI) !defined?(::TreeSitter::Parser) end |
.capabilities ⇒ Hash{Symbol => Object}
Get capabilities supported by this backend
223 224 225 226 227 228 229 230 231 |
# File 'lib/tree_haver/backends/ffi.rb', line 223 def capabilities return {} unless available? { backend: :ffi, parse: true, query: false, bytes_field: true, } end |
.ffi_gem_available? ⇒ Boolean
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.
Check if the FFI gem can be loaded
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tree_haver/backends/ffi.rb', line 31 def ffi_gem_available? return @ffi_gem_available if defined?(@ffi_gem_available) @ffi_gem_available = begin require "ffi" true rescue LoadError false end 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)
Note: FFI backend doesn’t maintain load state like other backends, but this method is provided for API consistency.
212 213 214 215 |
# File 'lib/tree_haver/backends/ffi.rb', line 212 def reset! # FFI backend uses constant-time availability check, no state to reset nil end |