2012-07-23 16:54:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2012-07-26 13:25:23 -04:00
|
|
|
# Matcher to find AST for method
|
2012-08-09 17:07:22 -04:00
|
|
|
class Method < self
|
2012-10-15 08:36:15 -04:00
|
|
|
include Adamantium, Equalizer.new(:identification)
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
# Parse a method string into filter
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
#
|
|
|
|
# @return [Matcher::Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.parse(input)
|
2012-07-23 16:57:29 -04:00
|
|
|
Classifier.run(input)
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Enumerate matches
|
|
|
|
#
|
|
|
|
# @return [Enumerable]
|
|
|
|
# returns enumerable when no block given
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# returns self when block given
|
|
|
|
#
|
|
|
|
# @api private
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
def each(&block)
|
2012-07-29 16:44:53 -04:00
|
|
|
return to_enum unless block_given?
|
2012-08-01 12:34:03 -04:00
|
|
|
subject.tap do |subject|
|
|
|
|
yield subject if subject
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return scope
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
2012-08-15 22:10:54 -04:00
|
|
|
# @return [Class|Module]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-10-26 05:24:29 -04:00
|
|
|
attr_reader :scope
|
2012-07-23 16:54:35 -04:00
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return context
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2012-08-15 22:10:54 -04:00
|
|
|
# @return [Context::Scope]
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-10-26 05:24:29 -04:00
|
|
|
attr_reader :context
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return method name
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
2012-08-14 16:45:34 -04:00
|
|
|
# @return [String]
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-10-26 05:24:29 -04:00
|
|
|
attr_reader :method_name
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Initialize method filter
|
|
|
|
#
|
|
|
|
# @param [Class|Module] scope
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize(scope, method_name)
|
|
|
|
@scope, @method_name = scope, method_name.to_sym
|
|
|
|
@context = Context::Scope.build(scope, source_path)
|
|
|
|
end
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return method
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2012-07-23 16:54:35 -04:00
|
|
|
# @return [UnboundMethod]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-02 19:28:15 -04:00
|
|
|
abstract_method :method
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return full ast
|
|
|
|
#
|
|
|
|
# @return [Rubinius::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def ast
|
2012-08-14 06:26:56 -04:00
|
|
|
File.read(source_path).to_ast
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
2012-08-14 06:26:56 -04:00
|
|
|
# Return path to source
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-14 06:26:56 -04:00
|
|
|
def source_path
|
2012-07-23 16:54:35 -04:00
|
|
|
source_location.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source file line
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-14 16:45:34 -04:00
|
|
|
def source_line
|
2012-07-23 16:54:35 -04:00
|
|
|
source_location.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source location
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_location
|
|
|
|
method.source_location
|
|
|
|
end
|
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return matched node
|
|
|
|
#
|
|
|
|
# @return [Rubinis::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-02 19:28:15 -04:00
|
|
|
abstract_method :matched_node
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-01 12:34:03 -04:00
|
|
|
# Return subject
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
2012-08-01 12:34:03 -04:00
|
|
|
# @return [Subject]
|
|
|
|
# returns subject if there is a matched node
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-01 12:34:03 -04:00
|
|
|
def subject
|
2012-07-26 13:25:23 -04:00
|
|
|
node = matched_node
|
2012-08-15 22:10:54 -04:00
|
|
|
return unless node
|
2012-08-20 11:53:41 -04:00
|
|
|
Subject.new(self, context, node)
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
2012-08-01 12:34:03 -04:00
|
|
|
memoize :subject
|
2012-08-16 13:10:24 -04:00
|
|
|
|
|
|
|
# Return matched node
|
|
|
|
#
|
|
|
|
# @return [Rubinus::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def matched_node
|
|
|
|
last_match = nil
|
|
|
|
ast.walk do |predicate, node|
|
|
|
|
last_match = node if match?(node)
|
|
|
|
predicate
|
|
|
|
end
|
|
|
|
last_match
|
|
|
|
end
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|