class Sith::MacroExpander

Public Class Methods

new(macros) click to toggle source
# File lib/sith/macro_expander.rb, line 6
def initialize(macros)
  @macros = macros
end

Public Instance Methods

expand(source) click to toggle source
# File lib/sith/macro_expander.rb, line 10
def expand(source)
  ast = Parser::CurrentRuby.parse(source)
  expand_node ast
end
expand_node(node) click to toggle source
# File lib/sith/macro_expander.rb, line 19
def expand_node(node)
  return node unless node.is_a?(Parser::AST::Node)

  if node.type == :send && @macros.key?(node.children[1])
    node = @macros[node.children[1]].expand_macro(node.children[2..-1])
  end
  children = node.children.map(&method(:expand_node))
  Parser::AST::Node.new node.type, children
end
expand_to_source(source) click to toggle source
# File lib/sith/macro_expander.rb, line 15
def expand_to_source(source)
  Unparser.unparse(expand(source))
end