free_mutant/lib/mutant/expression/namespace.rb

96 lines
2.1 KiB
Ruby
Raw Normal View History

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