# hx/path - path selectors # # Copyright (c) 2009-2010 MenTaLguY # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. module Hx module Path module Selector def prefix ; "" ; end def suffix ; "" ; end def regexp ; nil ; end def remaining_suffix(prefix_consumed) ; suffix ; end def accept?(path) raise NotImplementedError, "#{self.class}#accept? not implemented" end def |(other) Disjunction.new(self, other) end def &(other) Conjunction.new(self, other) end def ~() Negation.new(self) end end class All include Selector REGEXP = Regexp.new("^.*$") def regexp ; REGEXP ; end def accept?(path) ; true ; end end ALL = All.new class Literal include Selector attr_reader :value alias prefix value def initialize(value) @value = value @regexp = nil end def remaining_suffix(prefix_consumed) @value[prefix_consumed..-1] end def regexp @regexp ||= Regexp.new("^#{Regexp.quote(@value)}$") end def accept?(path) @value == path end end def self.parse_pattern(pattern_string) tokens = [] pattern_string.scan(/(\*\*?|[^*]+)/) do |token,| tokens << case token when "**"; :doublestar when "*"; :star else; token end end prefix = tokens.first if tokens.size == 1 and String === prefix Literal.new(prefix) else prefix = "" unless String === prefix suffix = tokens.last suffix = "" unless String === suffix regexp = Regexp.new("^#{tokens.map { |token| case token when :doublestar; '.*' when :star; '[^/]*' else; Regexp.quote(token) end }}$") Pattern.new(regexp, prefix, suffix) end end def self.literal(literal_string) Literal.new(literal_string) end class Pattern include Selector attr_reader :prefix attr_reader :suffix attr_reader :regexp def initialize(regexp, prefix, suffix) @regexp = regexp @prefix = prefix @suffix = suffix end def accept?(path) !!(@regexp.match(path)) end end module Connective include Selector attr_reader :prefix attr_reader :suffix def initialize(*selectors) @selectors = selectors prefixes = selectors.map { |s| s.prefix } prefix_length = (prefixes.first || "").length prefixes.each_cons(2) do |a, b| prefix_length.downto(0) do |length| prefix_length = length break if a[0...length] == b[0...length] end end @prefix = prefixes.first[0...prefix_length] suffixes = selectors.map { |s| s.remaining_suffix(prefix_length) } suffix_length = (suffixes.first || "").length suffixes.each_cons(2) do |a, b| suffix_length.downto(0) do |length| suffix_length = length break if a[-length..-1] == b[-length..-1] end end @suffix = suffixes.first[-suffix_length..-1] end end class Conjunction include Connective def accept?(path) @selectors.all? { |s| s.accept? path } end end class Disjunction include Connective attr_reader :regexp def initialize(*selectors) super regexps = @selectors.map { |s| s.regexp } if regexps.all? @regexp = Regexp.union(*regexps) else @regexp = nil end end def accept?(path) if @regexp !!@regexp.match(path) else @selectors.any? { |s| s.accept? path } end end end class Negation include Selector def initialize(selector) @selector = selector end def accept?(path) not @selector.accept?(path) end end end end