Class: TreeHaver::Backends::Prism::Language
- Inherits:
-
Object
- Object
- TreeHaver::Backends::Prism::Language
- Includes:
- Comparable
- Defined in:
- lib/tree_haver/backends/prism.rb
Overview
Prism language wrapper
Unlike tree-sitter which supports many languages via grammar files, Prism only parses Ruby. This class exists for API compatibility with other tree_haver backends.
Instance Attribute Summary collapse
-
#backend ⇒ Symbol
readonly
The backend this language is for.
-
#name ⇒ Symbol
(also: #language_name)
readonly
The language name (always :ruby for Prism).
-
#options ⇒ Hash
readonly
Prism parsing options.
Class Method Summary collapse
-
.from_library(_path = nil, symbol: nil, name: nil) ⇒ Language
(also: from_path)
Load language from library path (API compatibility).
-
.ruby(options = {}) ⇒ Language
Create a Ruby language instance (convenience method).
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compare languages for equality.
-
#hash ⇒ Integer
Hash value for this language (for use in Sets/Hashes).
-
#initialize(name = :ruby, options: {}) ⇒ Language
constructor
A new instance of Language.
Constructor Details
#initialize(name = :ruby, options: {}) ⇒ Language
Returns a new instance of Language.
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/tree_haver/backends/prism.rb', line 108 def initialize(name = :ruby, options: {}) @name = name.to_sym @backend = :prism = unless @name == :ruby raise TreeHaver::NotAvailable, "Prism only supports Ruby parsing. " \ "Got language: #{name.inspect}" end end |
Instance Attribute Details
#backend ⇒ Symbol (readonly)
The backend this language is for
100 101 102 |
# File 'lib/tree_haver/backends/prism.rb', line 100 def backend @backend end |
#name ⇒ Symbol (readonly) Also known as: language_name
The language name (always :ruby for Prism)
95 96 97 |
# File 'lib/tree_haver/backends/prism.rb', line 95 def name @name end |
#options ⇒ Hash (readonly)
Prism parsing options
104 105 106 |
# File 'lib/tree_haver/backends/prism.rb', line 104 def end |
Class Method Details
.from_library(_path = nil, symbol: nil, name: nil) ⇒ Language Also known as: from_path
Load language from library path (API compatibility)
Prism only supports Ruby, so path and symbol parameters are ignored. This method exists for API consistency with tree-sitter backends, allowing ‘TreeHaver.parser_for(:ruby)` to work regardless of backend.
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/tree_haver/backends/prism.rb', line 168 def from_library(_path = nil, symbol: nil, name: nil) # Derive language name from symbol if provided lang_name = name || symbol&.to_s&.sub(/^tree_sitter_/, "")&.to_sym || :ruby unless lang_name == :ruby raise TreeHaver::NotAvailable, "Prism backend only supports Ruby, not #{lang_name}. " \ "Use a tree-sitter backend for #{lang_name} support." end ruby end |
.ruby(options = {}) ⇒ Language
Create a Ruby language instance (convenience method)
153 154 155 |
# File 'lib/tree_haver/backends/prism.rb', line 153 def ruby( = {}) new(:ruby, options: ) end |
Instance Method Details
#<=>(other) ⇒ Integer?
Compare languages for equality
Prism languages are equal if they have the same backend and options.
126 127 128 129 130 131 |
# File 'lib/tree_haver/backends/prism.rb', line 126 def <=>(other) return unless other.is_a?(Language) return unless other.backend == @backend .to_a.sort <=> other..to_a.sort end |
#hash ⇒ Integer
Hash value for this language (for use in Sets/Hashes)
135 136 137 |
# File 'lib/tree_haver/backends/prism.rb', line 135 def hash [@backend, @name, .to_a.sort].hash end |