free_mutant/lib/mutant/expression/namespace.rb

91 lines
1.8 KiB
Ruby
Raw Normal View History

module Mutant
class Expression
# Abstract base class for expressions matching namespaces
class Namespace < self
include AbstractType
2014-09-17 00:47:09 +00:00
private
# Return matched namespace
#
# @return [String]
#
# @api private
#
def namespace
match[__method__]
end
# Recursive namespace expression
class Recursive < self
2014-05-31 03:09:48 +00:00
register(/\A(?<namespace>#{SCOPE_PATTERN})?\*\z/)
# Initialize object
#
# @return [undefined]
#
# @api private
def initialize(*)
super
2014-06-29 21:42:18 +00:00
@recursion_pattern = Regexp.union(
/\A#{namespace}\z/,
/\A#{namespace}::/,
/\A#{namespace}[.#]/
2014-06-29 21:42:18 +00:00
)
end
# Return matcher
#
2014-12-22 16:18:26 +00:00
# @param [Env::Bootstrap] env
#
# @return [Matcher]
#
# @api private
#
2014-12-22 16:18:26 +00:00
def matcher(env)
Matcher::Namespace.new(env, self)
end
# Return length of match
#
# @param [Expression] expression
#
# @return [Fixnum]
#
# @api private
#
def match_length(expression)
if @recursion_pattern =~ expression.syntax
2014-05-31 03:09:48 +00:00
namespace.length
else
0
end
end
end # Recursive
# Exact namespace expression
class Exact < self
register(/\A(?<namespace>#{SCOPE_PATTERN})\z/)
MATCHER = Matcher::Scope
# Return matcher
#
2014-12-22 16:18:26 +00:00
# @param [Env::Bootstrap] env
#
# @return [Matcher]
#
# @api private
#
2014-12-22 16:18:26 +00:00
def matcher(env)
Matcher::Scope.new(env, Mutant.constant_lookup(namespace), self)
end
end # Exact
end # Namespace
end # Namespace
end # Mutant