Class: Sith::MacroExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/sith/macro_expander.rb

Instance Method Summary collapse

Constructor Details

#initialize(macros) ⇒ MacroExpander

Returns a new instance of MacroExpander.



6
7
8
# File 'lib/sith/macro_expander.rb', line 6

def initialize(macros)
  @macros = macros
end

Instance Method Details

#expand(source) ⇒ Object



10
11
12
13
# File 'lib/sith/macro_expander.rb', line 10

def expand(source)
  ast = Parser::CurrentRuby.parse(source)
  expand_node ast
end

#expand_node(node) ⇒ Object



19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



15
16
17
# File 'lib/sith/macro_expander.rb', line 15

def expand_to_source(source)
  Unparser.unparse(expand(source))
end