Skip to content

Commit

Permalink
Find and cache the parser location when installed as a Gem (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-e authored Jan 27, 2025
1 parent f93b556 commit 8cb52d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 24 additions & 5 deletions lib/reaper/ast_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ class AstParser

attr_reader :parser, :language

@parser_paths_cache = {}

class << self
def find_parser_path(language, platform, arch)
cache_key = "#{language}-#{platform}-#{arch}"
return @parser_paths_cache[cache_key] if @parser_paths_cache.key?(cache_key)

extension = platform == 'darwin' ? 'dylib' : 'so'
parser_file = "libtree-sitter-#{language}-#{platform}-#{arch}.#{extension}"

parser_paths = [
File.join(File.dirname(__FILE__), '..', '..', 'parsers', parser_file), # Relative to this file
File.join(Gem::Specification.find_by_name('emerge').gem_dir, 'parsers', parser_file) # Installed gem path
]

parser_path = parser_paths.find { |path| File.exist?(path) }
raise "No language grammar found for #{language}. Searched in: #{parser_paths.join(', ')}" unless parser_path

@parser_paths_cache[cache_key] = parser_path
parser_path
end
end

def initialize(language)
@parser = TreeSitter::Parser.new
@language = language
Expand All @@ -52,11 +75,7 @@ def initialize(language)
raise "Unsupported architecture: #{RUBY_PLATFORM}"
end

extension = platform == 'darwin' ? 'dylib' : 'so'
parser_file = "libtree-sitter-#{language}-#{platform}-#{arch}.#{extension}"
parser_path = File.join('parsers', parser_file)
raise "No language grammar found for #{language}" unless File.exist?(parser_path)

parser_path = self.class.find_parser_path(language, platform, arch)
@parser.language = TreeSitter::Language.load(language, parser_path)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EmergeCLI
VERSION = '0.6.1'.freeze
VERSION = '0.6.2'.freeze
end

0 comments on commit 8cb52d4

Please sign in to comment.