class Metamorpher::Rewriter::Rule
Public Instance Methods
apply(ast, &block)
click to toggle source
# File lib/metamorpher/rewriter/rule.rb, line 10 def apply(ast, &block) rewrite_all(ast, matches_for(ast).take(1), &block) end
reduce(ast, &block)
click to toggle source
# File lib/metamorpher/rewriter/rule.rb, line 14 def reduce(ast, &block) rewrite_all(ast, matches_for(ast), &block) end
Private Instance Methods
matches_for(ast)
click to toggle source
# File lib/metamorpher/rewriter/rule.rb, line 38 def matches_for(ast) traverser.traverse(ast) .lazy # only compute the next match when needed .map { |current| pattern.match(current) } .select(&:matches?) end
rewrite(ast, match, &block)
click to toggle source
# File lib/metamorpher/rewriter/rule.rb, line 24 def rewrite(ast, match, &block) original = match.root substitution = substitution_with_special_values(match) rewritten = replacement.substitute(substitution) block.call(original, rewritten) if block ast.replace(original.path, rewritten) end
rewrite_all(ast, matches, &block)
click to toggle source
# File lib/metamorpher/rewriter/rule.rb, line 20 def rewrite_all(ast, matches, &block) matches.reduce(ast) { |a, e| rewrite(a, e, &block) } end
substitution_with_special_values(match)
click to toggle source
# File lib/metamorpher/rewriter/rule.rb, line 32 def substitution_with_special_values(match) match.substitution.dup.tap do |substitution| substitution[:&] = match.root.dup # add the "whole match" special variable (&) end end