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:



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.

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.



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.

Parameters:

  • file_path (String)

    The input file path.

Returns:

  • (Parser::AST::Node)

    The AST.



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.

Parameters:

  • source (String)

    The input source code to be transformed.

Returns:

  • (String)

    The transformed code, line-aligned (see #transform_file_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.

Parameters:

  • ast (Parser::AST::Node)

    The input AST to be transformed.

Returns:

  • (Parser::AST::Node)

    The transformed 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.

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.



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.

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



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