class Vernacular::AST::ASTModifier

Represents a modification that will be performed against the AST between the time that the source code is read and the time that it is compiled down to YARV instruction sequences.

Constants

BuilderExtension
ParserExtension

Attributes

builder_extensions[R]
parser_extensions[R]
rewriter_block[R]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/vernacular/ast/modifier.rb, line 27
def initialize
  @builder_extensions = []
  @parser_extensions = []
  yield self if block_given?
end

Public Instance Methods

build_rewriter(&block) click to toggle source
# File lib/vernacular/ast/modifier.rb, line 33
def build_rewriter(&block)
  @rewriter_block = block
end
components() click to toggle source
# File lib/vernacular/ast/modifier.rb, line 58
def components
  (builder_extensions + parser_extensions).flat_map(&:components) +
    rewriter_block.source_location
end
extend_builder(method, &block) click to toggle source
# File lib/vernacular/ast/modifier.rb, line 37
def extend_builder(method, &block)
  builder_extensions << BuilderExtension.new(method, block)
end
extend_parser(symbol, pattern, code) click to toggle source
# File lib/vernacular/ast/modifier.rb, line 41
def extend_parser(symbol, pattern, code)
  parser_extensions << ParserExtension.new(symbol, pattern, code)
end
modify(source) click to toggle source
# File lib/vernacular/ast/modifier.rb, line 45
def modify(source)
  raise 'You must first configure a rewriter!' unless rewriter_block

  rewriter = Class.new(Parser::TreeRewriter, &rewriter_block).new
  rewriter.instance_variable_set(:@parser, ASTParser.parser)

  buffer = Parser::Source::Buffer.new('<dynamic>')
  buffer.source = source

  ast = ASTParser.parse(source)
  rewriter.rewrite(buffer, ast)
end