Class: List::Matcher::Special
- Inherits:
-
Object
- Object
- List::Matcher::Special
- Defined in:
- lib/list_matcher.rb
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#left ⇒ Object
Returns the value of attribute left.
-
#list ⇒ Object
Returns the value of attribute list.
-
#right ⇒ Object
Returns the value of attribute right.
-
#special_map ⇒ Object
readonly
Returns the value of attribute special_map.
-
#specials ⇒ Object
Returns the value of attribute specials.
Instance Method Summary collapse
-
#[](s) ⇒ Object
maps a symbol character back to the symbol object.
-
#initialize(engine, specials, list) ⇒ Special
constructor
A new instance of Special.
-
#normalize ⇒ Object
reduce the list to a version ready for pattern generation.
-
#verify ⇒ Object
confirm that all special patterns are legitimate regexen.
Constructor Details
#initialize(engine, specials, list) ⇒ Special
Returns a new instance of Special.
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 387 388 389 |
# File 'lib/list_matcher.rb', line 336 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(engine.encoding) 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 SymbolPattern.new engine, c, var, pat, **opts elsif opts.is_a? String SymbolPattern.new engine, c, var, opts elsif var.is_a?(Regexp) && opts.nil? SymbolPattern.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(engine.encoding) @left = SymbolPattern.new engine, c, c, engine.left_bound @specials << @left end if engine.right_bound c = ( max += 1 ).chr(engine.encoding) @right = SymbolPattern.new engine, c, c, engine.right_bound @specials << @right end end @special_map = Hash[@specials.map{ |s| [ s.char, s ] }] end |
Instance Attribute Details
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
333 334 335 |
# File 'lib/list_matcher.rb', line 333 def engine @engine end |
#left ⇒ Object
Returns the value of attribute left.
334 335 336 |
# File 'lib/list_matcher.rb', line 334 def left @left end |
#list ⇒ Object
Returns the value of attribute list.
334 335 336 |
# File 'lib/list_matcher.rb', line 334 def list @list end |
#right ⇒ Object
Returns the value of attribute right.
334 335 336 |
# File 'lib/list_matcher.rb', line 334 def right @right end |
#special_map ⇒ Object (readonly)
Returns the value of attribute special_map.
333 334 335 |
# File 'lib/list_matcher.rb', line 333 def special_map @special_map end |
#specials ⇒ Object
Returns the value of attribute specials.
334 335 336 |
# File 'lib/list_matcher.rb', line 334 def specials @specials end |
Instance Method Details
#[](s) ⇒ Object
maps a symbol character back to the symbol object
403 404 405 |
# File 'lib/list_matcher.rb', line 403 def [](s) special_map[s] end |
#normalize ⇒ Object
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 |
# File 'lib/list_matcher.rb', line 408 def normalize rx = if specials.empty? /(?!)/ else Regexp.new '(' + specials.map(&:var).map(&:to_s).join('|') + ')' end list = self.list.uniq.map do |w| parts = w.split(rx).select{ |p| p.length > 0 } e = parts.size - 1 (0..e).map do |i| p = parts[i] if rx === p p = specials.detect{ |sp| sp.var === p } if engine.bound s = p if i == 0 && engine.left_bound && engine.word_test === p.left s = "#{left}#{s}" end if i == e && engine.right_bound && engine.word_test === p.right s = "#{s}#{right}" end p = s end else p = p.downcase if engine.case_insensitive if engine.bound s = p if i == 0 && engine.left_bound && engine.word_test === p[0] s = "#{left}#{s}" end if i == e && engine.right_bound && engine.word_test === p[-1] s = "#{s}#{right}" end p = s end end p end.join end.uniq.sort list end |
#verify ⇒ Object
confirm that all special patterns are legitimate regexen
392 393 394 395 396 397 398 399 400 |
# File 'lib/list_matcher.rb', line 392 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 |