lib/regexp-examples/groups.rb in regexp-examples-0.0.0 vs lib/regexp-examples/groups.rb in regexp-examples-0.0.1

- old
+ new

@@ -16,72 +16,91 @@ @chars = @chars[1..-1] else @negative = false end - # TODO: Can I make this more legible? - # Ranges a-b + init_backslash_chars + init_ranges + end + + def init_ranges # save first and last "-" if present first = nil last = nil first = @chars.shift if @chars.first == "-" last = @chars.pop if @chars.last == "-" + # Replace all instances of e.g. ["a" "-" "z"] with ["a", "b", ..., "z"] while i = @chars.index("-") @chars[i-1..i+1] = (@chars[i-1]..@chars[i+1]).to_a end # restore them back @chars.unshift(first) if first @chars.push(last) if last end + + def init_backslash_chars + @chars.each_with_index do |char, i| + if char == "\\" + if BackslashCharMap.keys.include?(@chars[i+1]) + @chars[i..i+1] = BackslashCharMap[@chars[i+1]] + elsif @chars[i+1] == "\\" + @chars.delete_at(i+1) + else + @chars.delete_at(i) + end + end + end + end + def result if @negative - CHARS - @chars + CharSets::Any - @chars else @chars end end end class DotGroup def result - CHARS + CharSets::Any end end class MultiGroup - attr_reader :group_num - def initialize(groups, group_num) + attr_reader :group_id + def initialize(groups, group_id) @groups = groups - @group_num = group_num + @group_id = group_id end # Generates the result of each contained group # and adds the filled group of each result to # itself def result - strings = @groups.map {|x| x.result} - result = RegexpExamples::permutations_of_strings(strings) - result.each {|x| BackReferenceTracker.add_filled_group(@group_num, x)} - result + strings = @groups.map {|repeater| repeater.result} + subgroups = @groups.select{ |repeater| repeater.group.respond_to? :group_id}.map{|repeater| repeater.group} + RegexpExamples::permutations_of_strings(strings).map do |result| + group_id ? CaptureGroupResult.new(group_id, subgroups, result) : result + end end end class MultiGroupEnd def result [''] end end class OrGroup - def initialize(repeaters) - @repeaters = repeaters + def initialize(left_repeaters, right_repeaters) + @repeaters = left_repeaters.concat(right_repeaters) end def result - repeaters_results = @repeaters.map do |repeater| - repeater.result + @repeaters.map do |repeater| + RegexpExamples::permutations_of_strings(repeater.result) end - RegexpExamples::permutations_of_strings(repeaters_results) end end class BackReferenceGroup attr_reader :num