Class: ASTTransform::SourceParser
- Inherits:
-
Object
- Object
- ASTTransform::SourceParser
- 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
-
#initialize ⇒ SourceParser
constructor
Constructs a new SourceParser instance.
-
#parse(source, file_path: "tmp") ⇒ Parser::AST::Node
Parses the given
source. -
#parse_file(file_path) ⇒ Parser::AST::Node
Parses the source in the given
file_path.
Constructor Details
#initialize ⇒ SourceParser
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.
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.
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 |