Class: TreeHaver::Backends::Rust::Language
- Inherits:
-
Object
- Object
- TreeHaver::Backends::Rust::Language
- Includes:
- Comparable
- Defined in:
- lib/tree_haver/backends/rust.rb
Overview
Wrapper for tree_stump Language
Provides TreeHaver-compatible interface to tree_stump’s language loading. tree_stump uses a registration-based API where languages are registered by name, then referenced by that name when setting parser language.
Instance Attribute Summary collapse
-
#backend ⇒ Symbol
readonly
The backend this language is for.
-
#name ⇒ String
readonly
The registered language name.
-
#path ⇒ String?
readonly
The path this language was loaded from (if known).
Class Method Summary collapse
-
.from_library(path, symbol: nil, name: nil) ⇒ Object
(also: from_path)
rubocop:disable Lint/UnusedMethodArgument.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compare languages for equality.
-
#hash ⇒ Integer
Hash value for this language (for use in Sets/Hashes).
-
#initialize(name, path: nil) ⇒ Language
constructor
private
A new instance of Language.
Constructor Details
#initialize(name, path: nil) ⇒ Language
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.
Returns a new instance of Language.
121 122 123 124 125 |
# File 'lib/tree_haver/backends/rust.rb', line 121 def initialize(name, path: nil) @name = name @backend = :rust @path = path end |
Instance Attribute Details
#backend ⇒ Symbol (readonly)
The backend this language is for
112 113 114 |
# File 'lib/tree_haver/backends/rust.rb', line 112 def backend @backend end |
#name ⇒ String (readonly)
The registered language name
108 109 110 |
# File 'lib/tree_haver/backends/rust.rb', line 108 def name @name end |
#path ⇒ String? (readonly)
The path this language was loaded from (if known)
116 117 118 |
# File 'lib/tree_haver/backends/rust.rb', line 116 def path @path end |
Class Method Details
.from_library(path, symbol: nil, name: nil) ⇒ Object Also known as: from_path
rubocop:disable Lint/UnusedMethodArgument
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/tree_haver/backends/rust.rb', line 160 def from_library(path, symbol: nil, name: nil) # rubocop:disable Lint/UnusedMethodArgument raise TreeHaver::NotAvailable, "tree_stump not available" unless Rust.available? # Validate the path exists before calling register_lang to provide a clear error raise TreeHaver::NotAvailable, "Language library not found: #{path}" unless File.exist?(path) # tree_stump uses TreeStump.register_lang(name, path) to register languages # The name is used to derive the symbol automatically (tree_sitter_<name>) # Use shared utility for consistent path parsing across backends lang_name = name || LibraryPathUtils.derive_language_name_from_path(path) ::TreeStump.register_lang(lang_name, path) new(lang_name, path: path) rescue RuntimeError => e raise TreeHaver::NotAvailable, "Failed to load language from #{path}: #{e.message}" end |
Instance Method Details
#<=>(other) ⇒ Integer?
Compare languages for equality
Rust languages are equal if they have the same backend and name. Name uniquely identifies a registered language in TreeStump.
134 135 136 137 138 139 |
# File 'lib/tree_haver/backends/rust.rb', line 134 def <=>(other) return unless other.is_a?(Language) return unless other.backend == @backend @name <=> other.name end |
#hash ⇒ Integer
Hash value for this language (for use in Sets/Hashes)
143 144 145 |
# File 'lib/tree_haver/backends/rust.rb', line 143 def hash [@backend, @name].hash end |