2014-05-30 21:02:15 -04:00
|
|
|
module Mutant
|
|
|
|
|
|
|
|
# Abstract base class for match expression
|
|
|
|
class Expression
|
2015-06-21 10:44:33 -04:00
|
|
|
include AbstractType, Adamantium::Flat
|
2014-05-30 21:02:15 -04:00
|
|
|
|
2015-06-21 10:44:33 -04:00
|
|
|
fragment = /[A-Za-z][A-Za-z\d_]*/.freeze
|
|
|
|
SCOPE_NAME_PATTERN = /(?<scope_name>#{fragment}(?:#{SCOPE_OPERATOR}#{fragment})*)/.freeze
|
|
|
|
SCOPE_SYMBOL_PATTERN = '(?<scope_symbol>[.#])'.freeze
|
2014-05-30 21:02:15 -04:00
|
|
|
|
2015-06-21 10:44:33 -04:00
|
|
|
private_constant(*constants(false))
|
2014-05-30 21:02:15 -04:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Syntax of expression
|
2014-12-22 12:54:18 -05:00
|
|
|
#
|
|
|
|
# @return [String]
|
2015-06-21 10:44:33 -04:00
|
|
|
abstract_method :syntax
|
2014-05-30 21:02:15 -04:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Match length with other expression
|
2014-05-30 21:02:15 -04:00
|
|
|
#
|
2014-06-28 16:47:46 -04:00
|
|
|
# @param [Expression] other
|
2014-05-30 21:02:15 -04:00
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
2014-06-28 16:47:46 -04:00
|
|
|
def match_length(other)
|
|
|
|
if eql?(other)
|
2014-05-30 21:02:15 -04:00
|
|
|
syntax.length
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-28 16:47:46 -04:00
|
|
|
# Test if expression is prefix
|
|
|
|
#
|
|
|
|
# @param [Expression] other
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def prefix?(other)
|
|
|
|
!match_length(other).zero?
|
|
|
|
end
|
|
|
|
|
2015-06-21 10:44:33 -04:00
|
|
|
# Try to parse input into expression of receiver class
|
2014-05-30 21:02:15 -04:00
|
|
|
#
|
2014-06-28 16:47:46 -04:00
|
|
|
# @param [String] input
|
2014-05-30 21:02:15 -04:00
|
|
|
#
|
|
|
|
# @return [Expression]
|
2015-06-21 10:44:33 -04:00
|
|
|
# when successful
|
2014-05-30 21:02:15 -04:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2014-06-29 19:16:34 -04:00
|
|
|
def self.try_parse(input)
|
2015-06-21 10:44:33 -04:00
|
|
|
match = self::REGEXP.match(input)
|
|
|
|
return unless match
|
|
|
|
names = anima.attribute_names
|
|
|
|
new(Hash[names.zip(names.map(&match.method(:[])))])
|
2014-05-30 21:02:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # Expression
|
|
|
|
end # Mutant
|