Method: #stringify

Defined in:
lib/interscript/utils/regexp_converter.rb

#stringify(node) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/interscript/utils/regexp_converter.rb', line 163

def stringify(node)
  tokens = node.flatten
  subs = {
    characterset_start: 'any(',
    characterset_stop: ')',
    array_start: '[',
    array_stop: ']',
    capture_start: 'capture(',
    capture_stop: ')',
    zero_or_one_start: 'maybe(',
    zero_or_one_stop: ')',
    zero_or_more_start: 'maybe_some(',
    zero_or_more_stop: ')',
    one_or_more_start: 'some(',
    one_or_more_stop: ')',
    alternation_start: 'any([',
    alternation_stop: '])',
    alternative_start: '',
    alternative_stop: '',
    boundary: 'boundary',
    non_word_boundary: 'non_word_boundary',
    space: 'space',
    line_start: 'line_start',
    line_end: 'line_end',
    any_character: 'any_character',
    range_start: 'any(',
    range_mid: '..',
    range_stop: ')',
    backref_num_start: 'ref(',
    backref_num_stop: ')'
  }

  str = []
  tokens.each_with_index do |token, idx|
    prev = tokens[idx - 1] if idx > 0
    left_side = %i[characterset_stop capture_stop
           zero_or_one_stop zero_or_more_stop one_or_more_stop
           boundary non_word_boundary
           line_start any_character range_stop space
                                           backref_num_stop]
    right_side = %i[characterset_start capture_start
            zero_or_one_start zero_or_more_start one_or_more_start
            boundary non_word_boundary
            line_end any_character range_start space
                                       backref_num_start]
    #if prev==:range_stop and token==:range_start
    #  str << ' :adding_ranges '
    #end
    if (prev.instance_of?(String) && right_side.include?(token)) or
      (left_side.include?(prev) && token.instance_of?(String)) or
      (left_side.include?(prev) && right_side.include?(token))
      str << ' + '
    end
    str << ', ' if prev == :alternative_stop and token == :alternative_start
    # str << '[' if prev == :characterset_start and token == :range_start
    # str << ']' if prev == :range_stop and token ==:characterset_stop
    if subs.include? token
      str << subs[token]
    elsif token.instance_of?(String)
      if prev.instance_of?(String)
        str[-1] = "#{str[-1][0..-2]}#{token}\""
      else
        str << "\"#{token}\""
      end
    else
      str << " #{token.inspect} "
    end
    # puts [idx, token].inspect
    # puts str.inspect
  end
  str.join.gsub('\\\\u', '\\u')
end