Class: TreeHaver::Backends::MRI::Parser
- Inherits:
-
Object
- Object
- TreeHaver::Backends::MRI::Parser
- Defined in:
- lib/tree_haver/backends/mri.rb
Overview
Wrapper for ruby_tree_sitter Parser
This is a thin pass-through to ::TreeSitter::Parser from ruby_tree_sitter.
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
Create a new parser instance.
-
#language=(lang) ⇒ ::TreeSitter::Language
Set the language for this parser.
-
#parse(source) ⇒ ::TreeSitter::Tree
Parse source code.
-
#parse_string(old_tree, source) ⇒ ::TreeSitter::Tree
Parse source code with optional incremental parsing.
Constructor Details
#initialize ⇒ Parser
Create a new parser instance
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/tree_haver/backends/mri.rb', line 190 def initialize raise TreeHaver::NotAvailable, "ruby_tree_sitter not available" unless MRI.available? @parser = ::TreeSitter::Parser.new rescue NameError => e # TreeSitter constant doesn't exist - backend not loaded raise TreeHaver::NotAvailable, "ruby_tree_sitter not available: #{e.}" rescue Exception => e # rubocop:disable Lint/RescueException # TreeSitter errors inherit from Exception (not StandardError) in ruby_tree_sitter v2+ # We rescue Exception and check the class name dynamically to avoid NameError # at parse time when TreeSitter constant isn't loaded yet if defined?(TreeSitter::TreeSitterError) && e.is_a?(TreeSitter::TreeSitterError) raise TreeHaver::NotAvailable, "Could not create parser: #{e.}" else raise # Re-raise if it's not a TreeSitter error end end |
Instance Method Details
#language=(lang) ⇒ ::TreeSitter::Language
Set the language for this parser
Note: TreeHaver::Parser unwraps language objects before calling this method. This backend receives raw ::TreeSitter::Language objects, never wrapped ones.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/tree_haver/backends/mri.rb', line 215 def language=(lang) # lang is already unwrapped by TreeHaver::Parser, use directly @parser.language = lang # Verify it was set raise TreeHaver::NotAvailable, "Language not set correctly" if @parser.language.nil? # Return the language object lang rescue Exception => e # rubocop:disable Lint/RescueException # TreeSitter errors inherit from Exception (not StandardError) in ruby_tree_sitter v2+ # We rescue Exception and check the class name dynamically to avoid NameError # at parse time when TreeSitter constant isn't loaded yet if defined?(TreeSitter::TreeSitterError) && e.is_a?(TreeSitter::TreeSitterError) raise TreeHaver::NotAvailable, "Could not set language: #{e.}" else raise # Re-raise if it's not a TreeSitter error end end |
#parse(source) ⇒ ::TreeSitter::Tree
Parse source code
ruby_tree_sitter provides parse_string for string input
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/tree_haver/backends/mri.rb', line 241 def parse(source) # ruby_tree_sitter's parse_string(old_tree, string) method # Pass nil for old_tree (initial parse) # Return raw tree - TreeHaver::Parser will wrap it tree = @parser.parse_string(nil, source) raise TreeHaver::NotAvailable, "Parse returned nil - is language set?" if tree.nil? tree rescue Exception => e # rubocop:disable Lint/RescueException # TreeSitter errors inherit from Exception (not StandardError) in ruby_tree_sitter v2+ # We rescue Exception and check the class name dynamically to avoid NameError # at parse time when TreeSitter constant isn't loaded yet if defined?(TreeSitter::TreeSitterError) && e.is_a?(TreeSitter::TreeSitterError) raise TreeHaver::NotAvailable, "Could not parse source: #{e.}" else raise # Re-raise if it's not a TreeSitter error end end |
#parse_string(old_tree, source) ⇒ ::TreeSitter::Tree
Parse source code with optional incremental parsing
Note: old_tree should already be unwrapped by TreeHaver::Parser before reaching this method. The backend receives the raw inner tree (::TreeSitter::Tree or nil), not a wrapped TreeHaver::Tree.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/tree_haver/backends/mri.rb', line 268 def parse_string(old_tree, source) # old_tree is already unwrapped by TreeHaver::Parser, pass it directly # Return raw tree - TreeHaver::Parser will wrap it @parser.parse_string(old_tree, source) rescue Exception => e # rubocop:disable Lint/RescueException # TreeSitter errors inherit from Exception (not StandardError) in ruby_tree_sitter v2+ # We rescue Exception and check the class name dynamically to avoid NameError # at parse time when TreeSitter constant isn't loaded yet if defined?(TreeSitter::TreeSitterError) && e.is_a?(TreeSitter::TreeSitterError) raise TreeHaver::NotAvailable, "Could not parse source: #{e.}" else raise # Re-raise if it's not a TreeSitter error end end |