Class: ASTTransform::SourceParser

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

Overview

Owns source → AST parsing: Prism's C parser through its whitequark translation layer, so every consumer gets the node vocabulary Parser::AST::Processor understands. Transformer parses through this seam; analysis-only consumers that never emit instantiate it directly and keep the instance for as many parses as they need.

Instance Method Summary collapse

Constructor Details

#initializeSourceParser

Constructs a new SourceParser instance.

KwargsBuilder is a required implementation detail, not an injection seam: the framework's emission depends on the kwargs/hash distinction it preserves, so every parse goes through it.



16
17
18
# File 'lib/ast_transform/source_parser.rb', line 16

def initialize
  @builder = KwargsBuilder.new
end

Instance Method Details

#parse(source, file_path: "tmp") ⇒ Parser::AST::Node

Parses the given source.

in backtraces.

Parameters:

  • source (String)

    The input source code.

  • file_path (String) (defaults to: "tmp")

    The file path recorded on source locations. This is important for source mapping

Returns:

  • (Parser::AST::Node)

    The AST.



27
28
29
30
31
32
# File 'lib/ast_transform/source_parser.rb', line 27

def parse(source, file_path: "tmp")
  # A fresh parser per parse: parser instances accumulate per-run state (lexer position, diagnostics), and
  # constructing one is trivial next to the parse itself.
  parser = Prism::Translation::Parser.new(@builder)
  parser.parse(create_buffer(source, file_path, parser.default_encoding))
end

#parse_file(file_path) ⇒ Parser::AST::Node

Parses the source in the given file_path.

Parameters:

  • file_path (String)

    The input file path.

Returns:

  • (Parser::AST::Node)

    The AST.



39
40
41
# File 'lib/ast_transform/source_parser.rb', line 39

def parse_file(file_path)
  parse(File.read(file_path), file_path: file_path)
end