Class: Regextest::Front::Parenthesis::Paren

Inherits:
Object
  • Object
show all
Includes:
Common, Empty
Defined in:
lib/regextest/front/parenthesis.rb

Constant Summary collapse

@@id =

a class variable for generating unique name of element

0

Constants included from Common

Common::TstConstDebug, Common::TstConstRecursionMax, Common::TstConstRepeatMax, Common::TstConstRetryMax, Common::TstConstRetryMaxSecond, Common::TstConstTimeout, Common::TstConstUnicodeCharSet, Common::TstFixnumMax

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#TstLog, #TstMdPrint, #TstRand, #TstShuffle, #is_random?, #reset_random_called

Constructor Details

#initialize(paren_start, element = nil, paren_end = nil) ⇒ Paren

Constructor



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/regextest/front/parenthesis.rb', line 17

def initialize(paren_start, element = nil, paren_end = nil)
  @options = @@parse_options
  @paren_type = paren_start[0]
  @offset = paren_start[1]
  if paren_end
    @length = (paren_end[1] - paren_start[1]) + paren_end[2]
  else
    @length = paren_start[2]
  end
  
  # delete head '(', '?', and tail ")"
  @prefix = @paren_type.sub(/^\(\??/, "")
  if @prefix.index("(") != 0
    @prefix.sub!(/\)$/, "")
  end
  
  @name = get_name(@prefix)
  @condition = nil  # set at generating json
  @refer_name = nil
  if element
    TstLog("Parenthesis: name:#{@name}, offset:#{@offset}, element:#{element}")
    @element = element
    @type_name = "LEX_PAREN"
  else
    TstLog("Parenthesis: name:#{@name}, offset:#{@offset}, element: \"\"")
    @element = TEmpty.new
    @type_name = "LEX_OPTION_PAREN"    # (?x-i) etc.
  end
  @generated_string = []
  @nest = 0
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



49
50
51
# File 'lib/regextest/front/parenthesis.rb', line 49

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/regextest/front/parenthesis.rb', line 49

def name
  @name
end

#offsetObject (readonly)

Returns the value of attribute offset.



49
50
51
# File 'lib/regextest/front/parenthesis.rb', line 49

def offset
  @offset
end

#prefixObject (readonly)

Returns the value of attribute prefix.



49
50
51
# File 'lib/regextest/front/parenthesis.rb', line 49

def prefix
  @prefix
end

#refer_nameObject (readonly)

Returns the value of attribute refer_name.



49
50
51
# File 'lib/regextest/front/parenthesis.rb', line 49

def refer_name
  @refer_name
end

Instance Method Details

#get_condition(prefix) ⇒ Object

get condition of parenthesis



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/regextest/front/parenthesis.rb', line 61

def get_condition(prefix)
  # puts "prefix: #{prefix}"
  if(md = prefix.match(/^\((\d+)\)$/))
    condition_name = @options[:parens].get_paren(md[1].to_i)
    if !condition_name
      raise "condition number #{prefix} is invalid"
    end
  elsif(md = prefix.match(/(?u:^\(<(\w+)>\)|\('(\w+)'\)$)/))
    match_string = md[1] || md[2]
    condition_name = @options[:parens].get_paren(match_string)
    if !condition_name
      raise "condition name (#{match_string}) is not found"
    end
  else
    condition_name = nil
  end
  
  # check number of elements
  if(condition_name)
    if(Regextest::Front::Selectable::Selectable === @element)
      if(@element.candidates.size > 2)
        raise "invalid condition. 1 or 2 selectable elements"
      end
    end
  end
  
  condition_name
end

#get_name(prefix) ⇒ Object

get name of parenthesis (if any)



52
53
54
55
56
57
58
# File 'lib/regextest/front/parenthesis.rb', line 52

def get_name(prefix)
  if(md = prefix.match(/(?u:^[<'](\w+)[>']$)/))
    md[1]
  else
    nil
  end
end

#get_value(relative_num = 0) ⇒ Object

get generated string



96
97
98
99
100
101
102
103
104
# File 'lib/regextest/front/parenthesis.rb', line 96

def get_value(relative_num = 0)
  # print "gen: "; pp @generated_string
  if(@generated_string.size > 0)
    @generated_string[-1]
  else
    warn "Error: refer uninitialized parenthesis"
    nil
  end
end

#jsonObject

transform to json format



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/regextest/front/parenthesis.rb', line 132

def json
  @@id += 1
  @condition = get_condition(@prefix)
  condition_name = @condition.refer_name if @condition
  "{\"type\": \"#{@type_name}\"," +
  " \"name\": \"#{@name}\"," +
  " \"offset\": \"#{@offset}\"," +
  " \"length\": \"#{@length}\"," +
  " \"prefix\": \"#{@prefix}\"," +
  " \"refer_name\": \"#{@refer_name}\"," +
  " \"condition_name\": \"#{condition_name}\"," +
  " \"id\": \"p#{@@id}\", " +
  " \"value\": #{@element.json}" +
  "}"
end

#set_options(options) ⇒ Object

set options



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/regextest/front/parenthesis.rb', line 107

def set_options(options)
  reg_options = options[:reg_options]
  TstLog("Parenthesis set_options before: #{reg_options.inspect}, prefix: #{@prefix}");
  if md = @prefix.match(/^([imxdau]*(?:\-[imx]*)?)(:)?$/)
    if md[2]
      # deep copy if (?imx: ) pattern
      cur_options = reg_options.dup
    else
      # replace option if (?imx) pattern
      cur_options = reg_options
    end
    cur_options.modify(md[1])
    TstLog("Parenthesis set_options after: #{cur_options.inspect}, new_regopt: #{md[1]}");
  else
    cur_options = reg_options
  end
  
  new_options = options.dup
  new_options[:reg_options] = cur_options

  @element.set_options(new_options)
  self
end

#set_refer_name(name) ⇒ Object

set unique name for back reference



91
92
93
# File 'lib/regextest/front/parenthesis.rb', line 91

def set_refer_name(name)
  @refer_name = name
end