2014-05-30 21:02:15 -04:00
|
|
|
module Mutant
|
|
|
|
class Expression
|
|
|
|
# Abstract base class for expressions matching namespaces
|
|
|
|
class Namespace < self
|
|
|
|
include AbstractType
|
|
|
|
|
|
|
|
# Recursive namespace expression
|
|
|
|
class Recursive < self
|
|
|
|
|
2014-05-30 23:09:48 -04:00
|
|
|
register(/\A(?<namespace>#{SCOPE_PATTERN})?\*\z/)
|
2014-05-30 21:02:15 -04:00
|
|
|
|
|
|
|
# Initialize object
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def initialize(*)
|
|
|
|
super
|
2014-05-30 23:09:48 -04:00
|
|
|
namespace_src = Regexp.escape(namespace)
|
2014-06-29 17:42:18 -04:00
|
|
|
@recursion_pattern = Regexp.union(
|
|
|
|
/\A#{namespace_src}\z/,
|
|
|
|
/\A#{namespace_src}::/,
|
|
|
|
/\A#{namespace_src}[.#]/
|
|
|
|
)
|
2014-05-30 21:02:15 -04:00
|
|
|
end
|
|
|
|
|
2014-06-29 18:33:40 -04:00
|
|
|
# Return matcher
|
|
|
|
#
|
2014-07-05 22:44:55 -04:00
|
|
|
# @param [Env] env
|
2014-06-29 18:33:40 -04:00
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-05 22:44:55 -04:00
|
|
|
def matcher(env)
|
|
|
|
Matcher::Namespace.new(env, self)
|
2014-06-29 18:33:40 -04:00
|
|
|
end
|
|
|
|
|
2014-05-30 21:02:15 -04:00
|
|
|
# Return length of match
|
|
|
|
#
|
|
|
|
# @param [Expression] expression
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def match_length(expression)
|
|
|
|
if @recursion_pattern =~ expression.syntax
|
2014-05-30 23:09:48 -04:00
|
|
|
namespace.length
|
2014-05-30 21:02:15 -04:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-30 23:09:48 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Return matched namespace
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def namespace
|
2014-07-05 23:43:23 -04:00
|
|
|
match[__method__] || EMPTY_STRING
|
2014-05-30 23:09:48 -04:00
|
|
|
end
|
|
|
|
|
2014-05-30 21:02:15 -04:00
|
|
|
end # Recursive
|
|
|
|
|
|
|
|
# Exact namespace expression
|
|
|
|
class Exact < self
|
|
|
|
|
|
|
|
register(/\A(?<namespace>#{SCOPE_PATTERN})\z/)
|
|
|
|
|
|
|
|
MATCHER = Matcher::Scope
|
|
|
|
|
2014-06-29 18:33:40 -04:00
|
|
|
# Return matcher
|
|
|
|
#
|
2014-07-05 22:44:55 -04:00
|
|
|
# @param [Cache] env
|
2014-06-29 18:33:40 -04:00
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-05 22:44:55 -04:00
|
|
|
def matcher(env)
|
|
|
|
Matcher::Scope.new(env, Mutant.constant_lookup(namespace), self)
|
2014-06-29 18:33:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Return namespace
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def namespace
|
|
|
|
match[__method__].to_s
|
|
|
|
end
|
2014-05-30 21:02:15 -04:00
|
|
|
|
2014-06-29 18:33:40 -04:00
|
|
|
end # Exact
|
2014-05-30 21:02:15 -04:00
|
|
|
end # Namespace
|
|
|
|
end # Namespace
|
|
|
|
end # Mutant
|