Class: ASTTransform::Transformer

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

Instance Method Summary collapse

Constructor Details

#initialize(*transformations, emitter: LineAlignedEmitter.new) ⇒ Transformer

Constructs a new Transformer instance.

Parameters:



12
13
14
15
16
# File 'lib/ast_transform/transformer.rb', line 12

def initialize(*transformations, emitter: LineAlignedEmitter.new)
  @transformations = transformations
  @emitter = emitter
  @source_parser = SourceParser.new
end

Instance Method Details

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

Builds the AST for the given source.

Parameters:

  • source (String)

    The input source code.

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

    The file_path. This is important for source mapping in backtraces.

Returns:

  • (Parser::AST::Node)

    The AST.



24
25
26
# File 'lib/ast_transform/transformer.rb', line 24

def build_ast(source, file_path: "tmp")
  @source_parser.parse(source, file_path: file_path)
end

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

Builds the AST for the given file_path.

Parameters:

  • file_path (String)

    The input file path.

Returns:

  • (Parser::AST::Node)

    The AST.



33
34
35
# File 'lib/ast_transform/transformer.rb', line 33

def build_ast_from_file(file_path)
  @source_parser.parse_file(file_path)
end

#transform(source) ⇒ String

Transforms the given source.

Parameters:

  • source (String)

    The input source code to be transformed.

Returns:

  • (String)

    The transformed code, line-aligned (see #transform_file_source).



42
43
44
45
46
# File 'lib/ast_transform/transformer.rb', line 42

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.

Parameters:

  • ast (Parser::AST::Node)

    The input AST to be transformed.

Returns:

  • (Parser::AST::Node)

    The transformed AST.



83
84
85
86
87
# File 'lib/ast_transform/transformer.rb', line 83

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.

Parameters:

  • file_path (String)

    The input file to be transformed. Statement placement (and therefore

  • transformed_file_path (String)

    The file path to the transformed file.

Returns:

  • (String)

    The transformed code.



55
56
57
58
# File 'lib/ast_transform/transformer.rb', line 55

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.

Parameters:

  • source (String)

    The input source code to be transformed.

  • file_path (String)

    The file path for the input source. Statement placement (and

  • transformed_file_path (String)

    The file path the transformed file will be written to.

Returns:

  • (String)

    The transformed code, line-aligned: every statement carrying a source



70
71
72
73
74
75
76
# File 'lib/ast_transform/transformer.rb', line 70

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