class Regextest::Front::ManageParentheses

A class for managing parentheses

Public Class Methods

new() click to toggle source
# File lib/regextest/front/manage-parentheses.rb, line 7
def initialize()
  @paren_hash = {}
  @paren_array = []
end

Public Instance Methods

add(paren) click to toggle source

register a parenthesis

# File lib/regextest/front/manage-parentheses.rb, line 13
def add(paren)
  # register capturable parentheses
  if(paren.prefix.length == 0 ||    # capture without prefix or
     (paren.prefix[-1] != ':' &&    # other than (?: or (?i: or (?imx), etc.
      !paren.prefix.match(/^([imx]*(?:\-[imx]+)?)$/) &&
      !paren.prefix.match(/^[\=\!\>]|\<[\=\!]/)
     )
    ) 
    @paren_array.push paren
  end
  
  # if name (ie. (?<foo>... ), register the name
  if(paren.name)
    @paren_hash[paren.name] = paren
  end
  paren
end
get_paren(get_id, offset = nil) click to toggle source

search target parenthesis

# File lib/regextest/front/manage-parentheses.rb, line 43
def get_paren(get_id, offset = nil)
  if !offset
    if(Integer === get_id)
      @paren_hash["$$_#{get_id}"]
    else
      @paren_hash[get_id]
    end
  else
    # puts "offset = #{offset}, id = #{get_id}"
    target_id = @paren_array.size + 1
    @paren_array.each_with_index do | paren, i |
      # puts paren.offset
      if paren.offset > offset
        target_id = i + 1  # paren is started from 1
        break
      end
    end
    relative_offset = get_id.to_i
    if relative_offset < 0
      target_id += get_id.to_i
    else
      target_id += get_id.to_i - 1
    end
    @paren_hash["$$_#{target_id}"]
  end
end
sort() click to toggle source

sort of parentheses (since number of parenthesis not analyze order but offset order)

# File lib/regextest/front/manage-parentheses.rb, line 32
def sort
  # pp @paren_array.map{|paren| paren.offset}
  @paren_array.sort{|x, y| x.offset <=> y.offset}.each_with_index do | paren, i |
    # puts "$$_#{i+1}  offset:#{paren.offset}"
    refer_name = "$$_#{i+1}"
    @paren_hash[refer_name] = paren    # parenthesis number from 1
    paren.set_refer_name(refer_name)
  end
end