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
|
|
|
|
|
|
|
|
# Abstract base class for match expression
|
|
|
|
class Expression
|
2015-06-21 14:44:33 +00:00
|
|
|
include AbstractType, Adamantium::Flat
|
2014-05-31 01:02:15 +00:00
|
|
|
|
2015-06-21 14:44:33 +00: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-31 01:02:15 +00:00
|
|
|
|
2015-06-21 14:44:33 +00:00
|
|
|
private_constant(*constants(false))
|
2014-05-31 01:02:15 +00:00
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Syntax of expression
|
2014-12-22 17:54:18 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
2015-06-21 14:44:33 +00:00
|
|
|
abstract_method :syntax
|
2014-05-31 01:02:15 +00:00
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Match length with other expression
|
2014-05-31 01:02:15 +00:00
|
|
|
#
|
2014-06-28 20:47:46 +00:00
|
|
|
# @param [Expression] other
|
2014-05-31 01:02:15 +00:00
|
|
|
#
|
2017-07-06 09:45:17 +02:00
|
|
|
# @return [Integer]
|
2014-06-28 20:47:46 +00:00
|
|
|
def match_length(other)
|
|
|
|
if eql?(other)
|
2014-05-31 01:02:15 +00:00
|
|
|
syntax.length
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-28 20:47:46 +00:00
|
|
|
# Test if expression is prefix
|
|
|
|
#
|
|
|
|
# @param [Expression] other
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def prefix?(other)
|
|
|
|
!match_length(other).zero?
|
|
|
|
end
|
|
|
|
|
2015-06-21 14:44:33 +00:00
|
|
|
# Try to parse input into expression of receiver class
|
2014-05-31 01:02:15 +00:00
|
|
|
#
|
2014-06-28 20:47:46 +00:00
|
|
|
# @param [String] input
|
2014-05-31 01:02:15 +00:00
|
|
|
#
|
|
|
|
# @return [Expression]
|
2015-06-21 14:44:33 +00:00
|
|
|
# when successful
|
2014-05-31 01:02:15 +00:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2014-06-29 23:16:34 +00:00
|
|
|
def self.try_parse(input)
|
2015-06-21 14:44:33 +00:00
|
|
|
match = self::REGEXP.match(input)
|
|
|
|
return unless match
|
|
|
|
names = anima.attribute_names
|
|
|
|
new(Hash[names.zip(names.map(&match.method(:[])))])
|
2014-05-31 01:02:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end # Expression
|
|
|
|
end # Mutant
|