Class: ASTTransform::Transformer
- Inherits:
-
Object
- Object
- ASTTransform::Transformer
- Defined in:
- lib/ast_transform/transformer.rb
Instance Method Summary collapse
-
#build_ast(source, file_path: "tmp") ⇒ Parser::AST::Node
Builds the AST for the given
source. -
#build_ast_from_file(file_path) ⇒ Parser::AST::Node
Builds the AST for the given
file_path. -
#initialize(*transformations, emitter: LineAlignedEmitter.new) ⇒ Transformer
constructor
Constructs a new Transformer instance.
-
#transform(source) ⇒ String
Transforms the given
source. -
#transform_ast(ast) ⇒ Parser::AST::Node
Transforms the given
ast. -
#transform_file(file_path, transformed_file_path) ⇒ String
Transforms the give
file_path. -
#transform_file_source(source, file_path, _transformed_file_path) ⇒ String
Transforms the given
sourceinfile_path.
Constructor Details
#initialize(*transformations, emitter: LineAlignedEmitter.new) ⇒ Transformer
Constructs a new Transformer instance.
15 16 17 18 |
# File 'lib/ast_transform/transformer.rb', line 15 def initialize(*transformations, emitter: LineAlignedEmitter.new) @transformations = transformations @emitter = emitter end |
Instance Method Details
#build_ast(source, file_path: "tmp") ⇒ Parser::AST::Node
Builds the AST for the given source.
26 27 28 29 |
# File 'lib/ast_transform/transformer.rb', line 26 def build_ast(source, file_path: "tmp") buffer = create_buffer(source, file_path) parser.parse(buffer) end |
#build_ast_from_file(file_path) ⇒ Parser::AST::Node
Builds the AST for the given file_path.
36 37 38 39 |
# File 'lib/ast_transform/transformer.rb', line 36 def build_ast_from_file(file_path) source = File.read(file_path) build_ast(source, file_path: file_path) end |
#transform(source) ⇒ String
Transforms the given source.
46 47 48 49 50 |
# File 'lib/ast_transform/transformer.rb', line 46 def transform(source) ast = build_ast(source) transformed_ast = transform_ast(ast) @emitter.emit(transformed_ast, 'tmp') end |
#transform_ast(ast) ⇒ Parser::AST::Node
Transforms the given ast.
87 88 89 90 91 |
# File 'lib/ast_transform/transformer.rb', line 87 def transform_ast(ast) @transformations.inject(ast) do |ast, transformation| transformation.run(ast) end end |
#transform_file(file_path, transformed_file_path) ⇒ String
Transforms the give file_path.
backtrace and breakpoint line numbers) is derived from this file's source locations.
59 60 61 62 |
# File 'lib/ast_transform/transformer.rb', line 59 def transform_file(file_path, transformed_file_path) source = File.read(file_path) transform_file_source(source, file_path, transformed_file_path) end |
#transform_file_source(source, file_path, _transformed_file_path) ⇒ String
Transforms the given source in file_path.
therefore backtrace and breakpoint line numbers) is derived from the source locations parsed under this path. location is emitted at its original source line.
74 75 76 77 78 79 80 |
# File 'lib/ast_transform/transformer.rb', line 74 def transform_file_source(source, file_path, _transformed_file_path) source_ast = build_ast(source, file_path: file_path) # At this point, the transformed_ast contains source locations for the original +source+. transformed_ast = transform_ast(source_ast) @emitter.emit(transformed_ast, file_path) end |