Module: WhyClasses::Parser

Defined in:
lib/why_classes/parser.rb

Overview

Wraps Prism's official parser-compatible frontend so that detection and rewriting share a single coordinate system. Prism is the actual parsing engine under the hood; the parser gem supplies the AST node interface, source buffer, ranges and TreeRewriter.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.parse(source, path = "(source)") ⇒ Object

Parses source and returns a Result. Parse errors are non-fatal: on failure ast is nil and error carries the exception message, so a single unparseable file never aborts a whole run.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/why_classes/parser.rb', line 20

def parse(source, path = "(source)")
  buffer = ::Parser::Source::Buffer.new(path, source: source)
  parser = ::Prism::Translation::Parser.new
  parser.diagnostics.all_errors_are_fatal = false
  parser.diagnostics.ignore_warnings      = true

  ast, comments = parser.parse_with_comments(buffer)
  Result.new(ast: ast, comments: comments, buffer: buffer, error: nil)
rescue ::Parser::SyntaxError, EncodingError => e
  Result.new(ast: nil, comments: [], buffer: buffer, error: e.message)
end