2014-05-30 21:02:15 -04:00
|
|
|
module Mutant
|
|
|
|
class Expression
|
|
|
|
# Abstract base class for expressions matching namespaces
|
|
|
|
class Namespace < self
|
2015-06-21 10:44:33 -04:00
|
|
|
include AbstractType, Anima.new(:scope_name)
|
|
|
|
private(*anima.attribute_names)
|
2014-09-16 20:47:09 -04:00
|
|
|
|
2014-05-30 21:02:15 -04:00
|
|
|
# Recursive namespace expression
|
|
|
|
class Recursive < self
|
2015-06-21 10:44:33 -04:00
|
|
|
REGEXP = /\A#{SCOPE_NAME_PATTERN}?\*\z/.freeze
|
2014-05-30 21:02:15 -04:00
|
|
|
|
|
|
|
# Initialize object
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def initialize(*)
|
|
|
|
super
|
2014-06-29 17:42:18 -04:00
|
|
|
@recursion_pattern = Regexp.union(
|
2015-06-21 10:44:33 -04:00
|
|
|
/\A#{scope_name}\z/,
|
|
|
|
/\A#{scope_name}::/,
|
|
|
|
/\A#{scope_name}[.#]/
|
2014-06-29 17:42:18 -04:00
|
|
|
)
|
2014-05-30 21:02:15 -04:00
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Syntax for expression
|
2015-06-21 10:44:33 -04:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def syntax
|
|
|
|
"#{scope_name}*"
|
|
|
|
end
|
|
|
|
memoize :syntax
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Matcher for expression
|
2014-06-29 18:33:40 -04:00
|
|
|
#
|
2014-12-22 11:18:26 -05:00
|
|
|
# @param [Env::Bootstrap] env
|
2014-06-29 18:33:40 -04:00
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
2014-12-22 11:18:26 -05:00
|
|
|
def matcher(env)
|
|
|
|
Matcher::Namespace.new(env, self)
|
2014-06-29 18:33:40 -04:00
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Length of match with other expression
|
2014-05-30 21:02:15 -04:00
|
|
|
#
|
|
|
|
# @param [Expression] expression
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def match_length(expression)
|
|
|
|
if @recursion_pattern =~ expression.syntax
|
2015-06-21 10:44:33 -04:00
|
|
|
scope_name.length
|
2014-05-30 21:02:15 -04:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Recursive
|
|
|
|
|
|
|
|
# Exact namespace expression
|
|
|
|
class Exact < self
|
|
|
|
|
|
|
|
MATCHER = Matcher::Scope
|
2015-06-21 10:44:33 -04:00
|
|
|
private_constant(*constants(false))
|
|
|
|
|
|
|
|
REGEXP = /\A#{SCOPE_NAME_PATTERN}\z/.freeze
|
2014-05-30 21:02:15 -04:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Matcher matcher on expression
|
2014-06-29 18:33:40 -04:00
|
|
|
#
|
2014-12-22 11:18:26 -05:00
|
|
|
# @param [Env::Bootstrap] env
|
2014-06-29 18:33:40 -04:00
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
2014-12-22 11:18:26 -05:00
|
|
|
def matcher(env)
|
2015-06-21 10:44:33 -04:00
|
|
|
Matcher::Scope.new(env, Object.const_get(scope_name), self)
|
2014-06-29 18:33:40 -04:00
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Syntax for expression
|
2015-06-21 10:44:33 -04:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
alias_method :syntax, :scope_name
|
|
|
|
public :syntax
|
|
|
|
|
2014-06-29 18:33:40 -04:00
|
|
|
end # Exact
|
2014-05-30 21:02:15 -04:00
|
|
|
end # Namespace
|
|
|
|
end # Namespace
|
|
|
|
end # Mutant
|