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.
92 93 94 95 96 |
# File 'lib/tree_haver/backends/rust.rb', line 92 def initialize(name, path: nil) @name = name @backend = :rust @path = path end |
Instance Attribute Details
#backend ⇒ Symbol (readonly)
The backend this language is for
83 84 85 |
# File 'lib/tree_haver/backends/rust.rb', line 83 def backend @backend end |
#name ⇒ String (readonly)
The registered language name
79 80 81 |
# File 'lib/tree_haver/backends/rust.rb', line 79 def name @name end |
#path ⇒ String? (readonly)
The path this language was loaded from (if known)
87 88 89 |
# File 'lib/tree_haver/backends/rust.rb', line 87 def path @path end |
Class Method Details
.from_library(path, symbol: nil, name: nil) ⇒ Object Also known as: from_path
rubocop:disable Lint/UnusedMethodArgument
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tree_haver/backends/rust.rb', line 131 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>) lang_name = name || File.basename(path, ".*").sub(/^libtree-sitter-/, "") ::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.
105 106 107 108 109 110 |
# File 'lib/tree_haver/backends/rust.rb', line 105 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)
114 115 116 |
# File 'lib/tree_haver/backends/rust.rb', line 114 def hash [@backend, @name].hash end |