lib/regextest/back/main.rb in regextest-0.1.4 vs lib/regextest/back/main.rb in regextest-0.1.5

- old
+ new

@@ -7,13 +7,14 @@ require 'regextest/back/result' # Main class of back-end. Construct candidates array and narrow down class Regextest::Back::Main include Regextest::Common - def initialize(json_obj, max_nest) + def initialize(json_obj, max_nest, retry_count = 0) @json_obj = json_obj @max_nest = max_nest + @retry_count = retry_count @parens_hash = {} # hash to keep string generated by parentheses @nest = 0 # current nest of back-reference @quit_mode = false # flag for preventing from increase of nest # if true, \g<foo> is restrained if possible end @@ -226,16 +227,23 @@ end # repeat def generate_candidates_repeat(param) target = param[:json] + max_repeat = target["max_repeat"] + min_repeat = target["min_repeat"] + + if @retry_count > 0 + @retry_count.times{ max_repeat = (max_repeat + 1)/2 } + end + if(@quit_mode) - repeat = target["min_repeat"] - elsif(target["max_repeat"] > target["min_repeat"]) - repeat = target["min_repeat"]+TstRand(target["max_repeat"]-target["min_repeat"]+1) + repeat = min_repeat + elsif(max_repeat > min_repeat) + repeat = min_repeat + TstRand(max_repeat - min_repeat + 1) else - repeat = target["min_repeat"] + repeat = min_repeat end result = [] if target["repeat_option"].index("reluctant") result.push Regextest::Back::Element.new({cmd: :CMD_ANC_RELUCTANT_BEGIN, id: target["id"]}) end @@ -271,10 +279,16 @@ # back_refer def generate_candidates_back_refer(param) target = param[:json] if @parens_hash[target["refer_name"]][:generated] - relative_num = (target["relative_num"]=="")?(-1):(@nest + target["relative_num"].to_i) + relative_num = -1 # default value + if target["relative_num"] != "" + work = @nest + target["relative_num"].to_i + return nil if(work < 0 || !@parens_hash[target["refer_name"]][:generated][work]) + relative_num = work + end + # puts "relative: #{relative_num}, nest=#{@nest}, :#{target}" result = @parens_hash[target["refer_name"]][:generated][relative_num] else result = nil end result