2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2013-01-21 14:08:30 -05:00
|
|
|
module Mutant
|
|
|
|
class CLI
|
|
|
|
# A classifier for input strings
|
2013-09-11 15:02:03 -04:00
|
|
|
class Classifier
|
2013-06-27 16:18:07 -04:00
|
|
|
include AbstractType, Adamantium::Flat, Concord.new(:cache, :match)
|
|
|
|
|
|
|
|
include Equalizer.new(:identifier)
|
2013-01-21 14:08:30 -05:00
|
|
|
|
2013-07-29 17:09:22 -04:00
|
|
|
SCOPE_NAME_PATTERN = /[A-Za-z][A-Za-z\d_]*/.freeze
|
|
|
|
SCOPE_OPERATOR = '::'.freeze
|
|
|
|
CBASE_PATTERN = /\A#{SCOPE_OPERATOR}/.freeze
|
|
|
|
|
|
|
|
METHOD_NAME_PATTERN = Regexp.union(
|
|
|
|
/[A-Za-z_][A-Za-z\d_]*[!?=]?/,
|
|
|
|
*OPERATOR_METHODS.map(&:to_s)
|
|
|
|
).freeze
|
|
|
|
|
|
|
|
SCOPE_PATTERN = /
|
|
|
|
(?:#{SCOPE_OPERATOR})?#{SCOPE_NAME_PATTERN}
|
|
|
|
(?:#{SCOPE_OPERATOR}#{SCOPE_NAME_PATTERN})*
|
|
|
|
/x.freeze
|
2013-01-21 14:08:30 -05:00
|
|
|
|
2013-09-11 16:04:08 -04:00
|
|
|
REGISTRY = {}
|
2013-04-20 20:36:21 -04:00
|
|
|
|
|
|
|
# Register classifier
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-09-11 16:04:08 -04:00
|
|
|
def self.register(regexp)
|
|
|
|
REGISTRY[regexp] = self
|
2013-04-20 20:36:21 -04:00
|
|
|
end
|
|
|
|
private_class_method :register
|
|
|
|
|
2013-04-17 23:31:21 -04:00
|
|
|
# Return constant
|
2013-01-21 14:08:30 -05:00
|
|
|
#
|
|
|
|
# @param [String] location
|
|
|
|
#
|
|
|
|
# @return [Class|Module]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.constant_lookup(location)
|
2013-07-28 13:00:13 -04:00
|
|
|
location
|
2013-07-29 17:09:22 -04:00
|
|
|
.sub(CBASE_PATTERN, EMPTY_STRING)
|
2013-07-28 13:00:13 -04:00
|
|
|
.split(SCOPE_OPERATOR)
|
|
|
|
.reduce(Object) do |parent, name|
|
2013-07-28 15:33:21 -04:00
|
|
|
parent.const_get(name, nil)
|
2013-08-05 01:46:49 -04:00
|
|
|
end
|
2013-01-21 14:08:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return matchers for input
|
|
|
|
#
|
2013-09-08 18:05:49 -04:00
|
|
|
# @param [Cache] cache
|
|
|
|
# @param [String] pattern
|
|
|
|
#
|
2013-09-13 16:28:04 -04:00
|
|
|
# @return [Matcher]
|
2013-01-21 14:08:30 -05:00
|
|
|
# if a classifier handles the input
|
|
|
|
#
|
2013-09-08 18:05:49 -04:00
|
|
|
# @raise [RuntimeError]
|
2013-01-21 14:08:30 -05:00
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-09-11 16:06:32 -04:00
|
|
|
def self.run(cache, pattern)
|
2013-09-11 16:04:08 -04:00
|
|
|
matches = find(pattern)
|
|
|
|
case matches.length
|
2013-09-08 18:05:49 -04:00
|
|
|
when 0
|
|
|
|
raise Error, "No matcher handles: #{pattern.inspect}"
|
|
|
|
when 1
|
2013-09-11 16:04:08 -04:00
|
|
|
klass, match = matches.first
|
2013-09-13 16:28:04 -04:00
|
|
|
klass.new(cache, match).matcher
|
2013-09-08 18:05:49 -04:00
|
|
|
else
|
|
|
|
raise Error, "More than one matcher found for: #{pattern.inspect}"
|
|
|
|
end
|
2013-01-21 14:08:30 -05:00
|
|
|
end
|
|
|
|
|
2013-09-11 16:04:08 -04:00
|
|
|
# Find classifiers
|
2013-06-27 16:18:07 -04:00
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
#
|
2013-01-21 14:08:30 -05:00
|
|
|
# @api private
|
|
|
|
#
|
2013-09-11 16:04:08 -04:00
|
|
|
def self.find(input)
|
|
|
|
REGISTRY.each_with_object([]) do |(regexp, klass), matches|
|
|
|
|
match = regexp.match(input)
|
|
|
|
if match
|
|
|
|
matches << [klass, match]
|
|
|
|
end
|
|
|
|
end
|
2013-01-21 14:08:30 -05:00
|
|
|
end
|
2013-09-11 16:04:08 -04:00
|
|
|
private_class_method :find
|
2013-01-21 14:08:30 -05:00
|
|
|
|
|
|
|
# Enumerate subjects
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# if block given
|
|
|
|
#
|
|
|
|
# @return [Enumerator<Subject>]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
return to_enum unless block_given?
|
|
|
|
matcher.each(&block)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-06-27 16:18:07 -04:00
|
|
|
# Return identifier
|
2013-01-21 14:08:30 -05:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-27 16:18:07 -04:00
|
|
|
def identifier
|
2013-01-21 14:08:30 -05:00
|
|
|
match.to_s
|
|
|
|
end
|
2013-06-27 16:18:07 -04:00
|
|
|
memoize :identifier
|
2013-01-21 14:08:30 -05:00
|
|
|
|
|
|
|
# Return matcher
|
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
abstract_method :matcher
|
2013-07-29 17:39:38 -04:00
|
|
|
private :matcher
|
2013-01-21 14:08:30 -05:00
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Classifier
|
|
|
|
end # CLI
|
|
|
|
end # Mutant
|