Class: List::Matcher::Special

Inherits:
Object
  • Object
show all
Defined in:
lib/list_matcher.rb

Constant Summary collapse

NULL =
Regexp.new '(?!)'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, specials, list) ⇒ Special



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/list_matcher.rb', line 334

def initialize( engine, specials, list )
  @engine = engine
  @list = list
  max = 0
  list.each do |w|
    w.chars.each{ |c| i = c.ord; max = i if i > max }
  end
  @specials = [].tap do |ar|
    specials.sort do |a, b|
      a = a.first
      b = b.first
      s1 = a.is_a?(String) || a.is_a?(Symbol)
      s2 = b.is_a?(String) || b.is_a?(Symbol)
      if s1 && s2
        b.to_s <=> a.to_s
      elsif s1
        -1
      elsif s2
        1
      else
        s = a.to_s.length - b.to_s.length
        s == 0 ? a.to_s <=> b.to_s : s
      end
    end.each do |var, opts|
      c = ( max += 1 ).chr
      sp = if opts.is_a? Hash
        pat = opts.delete :pattern
        raise Error, "symbol #{var} requires a pattern" unless pat || var.is_a?(Regexp)
        pat ||= var.to_s
        SpecialPattern.new engine, c, var, pat, **opts
      elsif opts.is_a? String
        SpecialPattern.new engine, c, var, opts
      elsif var.is_a?(Regexp) && opts.nil?
        SpecialPattern.new engine, c, var, nil
      else
        raise Error, "symbol #{var} requires a pattern"
      end
      ar << sp
    end
  end
  if engine.bound
    if engine.left_bound
      c = ( max += 1 ).chr
      @left = SpecialPattern.new engine, c, c, engine.left_bound
      @specials << @left
    end
    if engine.right_bound
      c = ( max += 1 ).chr
      @right = SpecialPattern.new engine, c, c, engine.right_bound
      @specials << @right
    end
  end
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



329
330
331
# File 'lib/list_matcher.rb', line 329

def engine
  @engine
end

#leftObject

Returns the value of attribute left.



330
331
332
# File 'lib/list_matcher.rb', line 330

def left
  @left
end

#listObject

Returns the value of attribute list.



330
331
332
# File 'lib/list_matcher.rb', line 330

def list
  @list
end

#rightObject

Returns the value of attribute right.



330
331
332
# File 'lib/list_matcher.rb', line 330

def right
  @right
end

#specialsObject

Returns the value of attribute specials.



330
331
332
# File 'lib/list_matcher.rb', line 330

def specials
  @specials
end

Instance Method Details

#normalizeObject

reduce the list to a version ready for pattern generation



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/list_matcher.rb', line 408

def normalize
  rx = if specials.empty?
    NULL
  else
    Regexp.new '(' + specials.map(&:var).map(&:to_s).join('|') + ')'
  end
  l = r = false
  list = self.list.uniq.map do |w|
    parts = w.split rx
    e = parts.size - 1
    (0..e).map do |i|
      p = parts[i]
      if rx === p
        p = specials.detect{ |sp| sp.var === p }
        special_map[p.char] = p
        if engine.bound
          if i == 0 && engine.left_bound && p.left
            p = "#{left}#{p}" if t
            l = true
          end
          if i == e && engine.right_bound && p.right
            p = "#{p}#{right}"
            r = true
          end
        end
      else
        p = p.downcase if engine.case_insensitive
        if engine.bound
          if i == 0 && engine.left_bound && engine.word_test === p[0]
            p = "#{left}#{p}"
            l = true
          end
          if i == e && engine.right_bound && engine.word_test === p[-1]
            p = "#{p}#{right}"
            r = true
          end
        end
      end
      p
    end.join
  end.uniq.sort
  special_map[left.char] = left if l
  special_map[right.char] = right if r
  list
end

#special_mapObject



399
400
401
# File 'lib/list_matcher.rb', line 399

def special_map
  @special_map ||= {}
end

#symbols(s) ⇒ Object



403
404
405
# File 'lib/list_matcher.rb', line 403

def symbols(s)
  special_map[s]
end

#verifyObject

confirm that all special patterns are legitimate regexen



389
390
391
392
393
394
395
396
397
# File 'lib/list_matcher.rb', line 389

def verify
  specials.each do |s|
    begin
      Regexp.new s.pat
    rescue
      raise Error, "the symbol #{s.symbol} has an ill-formed pattern: #{s.pat}"
    end
  end
end