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