Class: AtCoderFriends::Parser::InputFormatMatcher

Inherits:
Object
  • Object
show all
Includes:
InputFormatUtils
Defined in:
lib/at_coder_friends/parser/input_format.rb

Overview

holds regular expressions and matches it with input format string

Constant Summary

Constants included from InputFormatConstants

AtCoderFriends::Parser::InputFormatConstants::ADD_TAG, AtCoderFriends::Parser::InputFormatConstants::DELIMS, AtCoderFriends::Parser::InputFormatConstants::DIMENSION_TBL, AtCoderFriends::Parser::InputFormatConstants::RE_0, AtCoderFriends::Parser::InputFormatConstants::RE_00, AtCoderFriends::Parser::InputFormatConstants::RE_99, AtCoderFriends::Parser::InputFormatConstants::RE_BLOCK, AtCoderFriends::Parser::InputFormatConstants::RE_ITEM, AtCoderFriends::Parser::InputFormatConstants::RE_IX, AtCoderFriends::Parser::InputFormatConstants::RE_IX_0, AtCoderFriends::Parser::InputFormatConstants::RE_IX_00, AtCoderFriends::Parser::InputFormatConstants::RE_IX_99, AtCoderFriends::Parser::InputFormatConstants::RE_SINGLE, AtCoderFriends::Parser::InputFormatConstants::RE_SZ, AtCoderFriends::Parser::InputFormatConstants::RE_SZ2_0, AtCoderFriends::Parser::InputFormatConstants::RE_SZ2_REF, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_0, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_00, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_99, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_REF, AtCoderFriends::Parser::InputFormatConstants::SECTIONS, AtCoderFriends::Parser::InputFormatConstants::TO_SUFFIX, AtCoderFriends::Parser::InputFormatConstants::TO_SUFFIX_STR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InputFormatUtils

#extract_delim, #normalize_fmt, #normalize_name, #normalize_names, #normalize_size, #size_array, #split_size

Constructor Details

#initialize(container: nil, item: nil, pat: nil, gen_names: nil, gen_pat2: nil) ⇒ InputFormatMatcher

Returns a new instance of InputFormatMatcher.



158
159
160
161
162
163
164
165
166
167
# File 'lib/at_coder_friends/parser/input_format.rb', line 158

def initialize(
  container: nil, item: nil,
  pat: nil, gen_names: nil, gen_pat2: nil
)
  @container = container
  @item = item
  @pat = pat
  @gen_names = gen_names
  @gen_pat2 = gen_pat2
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def container
  @container
end

#delimObject (readonly)

Returns the value of attribute delim.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def delim
  @delim
end

#gen_namesObject (readonly)

Returns the value of attribute gen_names.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def gen_names
  @gen_names
end

#gen_pat2Object (readonly)

Returns the value of attribute gen_pat2.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def gen_pat2
  @gen_pat2
end

#itemObject (readonly)

Returns the value of attribute item.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def item
  @item
end

#ix0Object (readonly)

Returns the value of attribute ix0.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def ix0
  @ix0
end

#namesObject (readonly)

Returns the value of attribute names.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def names
  @names
end

#patObject (readonly)

Returns the value of attribute pat.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def pat
  @pat
end

#pat2Object (readonly)

Returns the value of attribute pat2.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def pat2
  @pat2
end

#sizeObject (readonly)

Returns the value of attribute size.



155
156
157
# File 'lib/at_coder_friends/parser/input_format.rb', line 155

def size
  @size
end

Instance Method Details

#match(str) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/at_coder_friends/parser/input_format.rb', line 169

def match(str)
  str, dlm = extract_delim(str)
  return false unless (m1 = pat.match(str))

  @names = gen_names.call(m1)
  @pat2 = gen_pat2&.call(names)
  @size = (m1.names.include?('sz') && m1['sz']) || ''
  @ix0 = (m1.names.include?('ix0') && m1['ix0']) || size
  @delim = dlm
  true
end

#match2(str) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/at_coder_friends/parser/input_format.rb', line 181

def match2(str)
  return false unless pat2

  str, _dlm = extract_delim(str)
  return true if /\A\.+\z/ =~ str
  return false unless (m2 = pat2.match(str))

  m2.names.include?('sz') && @size = m2['sz']
  true
end

#to_inpdefObject



192
193
194
195
196
197
198
199
# File 'lib/at_coder_friends/parser/input_format.rb', line 192

def to_inpdef
  Problem::InputFormat.new(
    container: container, item: item,
    names: normalize_names(names),
    size: normalize_size(container, size, ix0),
    delim: delim
  )
end