Class: Rubyzen::Parsers::ASTParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyzen/parsers/a_s_t_parser.rb

Overview

Singleton parser that converts Ruby source files into Rubyzen declarations using RuboCop’s AST processing. Results are cached via Cache::ParseCache.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeASTParser

Returns a new instance of ASTParser.



15
16
17
# File 'lib/rubyzen/parsers/a_s_t_parser.rb', line 15

def initialize
  @cache = Rubyzen::Cache::ParseCache.new
end

Class Method Details

.instanceASTParser

Returns the singleton instance of the parser.

Returns:



11
12
13
# File 'lib/rubyzen/parsers/a_s_t_parser.rb', line 11

def self.instance
  @instance ||= new
end

Instance Method Details

#parse_file(file_path) ⇒ Declarations::FileDeclaration?

Parses a Ruby source file and returns its declaration, using the cache.

Parameters:

  • file_path (String)

    absolute path to the Ruby file

Returns:



23
24
25
26
27
28
29
30
# File 'lib/rubyzen/parsers/a_s_t_parser.rb', line 23

def parse_file(file_path)
  @cache.fetch_or_parse(file_path) do
    source = File.read(file_path)
    processed_source = RuboCop::AST::ProcessedSource.new(source, RUBY_VERSION.to_f, file_path)
    next nil unless processed_source.ast
    Rubyzen::Declarations::FileDeclaration.new(file_path, processed_source.ast)
  end
end