Class: Henitai::SourceParser

Inherits:
Object
  • Object
show all
Defined in:
lib/henitai/source_parser.rb

Overview

Parses Ruby source into parser-compatible AST nodes using Prism.

The parser translation layer keeps the ‘Parser::AST::Node` shape that the mutation pipeline and Unparser already expect, while delegating syntax support to Prism.

Constant Summary collapse

DEFAULT_PATH =
"(string)"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_cache!Object

Clears the parse cache. Intended for test isolation.



30
31
32
# File 'lib/henitai/source_parser.rb', line 30

def self.clear_cache!
  @cache.clear
end

.parse(source, path: DEFAULT_PATH) ⇒ Object



17
18
19
# File 'lib/henitai/source_parser.rb', line 17

def self.parse(source, path: DEFAULT_PATH)
  new.parse(source, path:)
end

.parse_file(path) ⇒ Object

Returns the parsed AST for path, re-using a cached result when the file’s mtime has not changed. This avoids parsing the same file twice across pipeline phases (e.g. SubjectResolver then MutantGenerator).



24
25
26
27
# File 'lib/henitai/source_parser.rb', line 24

def self.parse_file(path)
  key = [path, File.mtime(path)]
  @cache[key] ||= new.parse_file(path)
end

Instance Method Details

#parse(source, path: DEFAULT_PATH) ⇒ Object



34
35
36
# File 'lib/henitai/source_parser.rb', line 34

def parse(source, path: DEFAULT_PATH)
  Prism::Translation::ParserCurrent.new.parse(source_buffer(source, path))
end

#parse_file(path) ⇒ Object



38
39
40
41
42
# File 'lib/henitai/source_parser.rb', line 38

def parse_file(path)
  # Ruby's file encoding rules apply here. Projects that use explicit source
  # encoding comments can be handled by a future encoding-aware option.
  parse(File.read(path), path:)
end