Class: List::Matcher::Special
- Inherits:
-
Object
- Object
- List::Matcher::Special
- Defined in:
- lib/list_matcher.rb
Constant Summary collapse
- NULL =
Regexp.new '(?!)'
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.
-
#specials ⇒ Object
Returns the value of attribute specials.
Instance Method Summary collapse
-
#initialize(engine, specials, list) ⇒ Special
constructor
A new instance of Special.
-
#normalize ⇒ Object
reduce the list to a version ready for pattern generation.
- #special_map ⇒ Object
- #symbols(s) ⇒ Object
-
#verify ⇒ Object
confirm that all special patterns are legitimate regexen.
Constructor Details
#initialize(engine, specials, list) ⇒ Special
Returns a new instance of Special.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/list_matcher.rb', line 292 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 "variable #{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 "variable #{var} requires a pattern" end ar << sp end end if engine.bound c = ( max += 1 ).chr @left = SpecialPattern.new engine, c, c, engine.left_bound @specials << @left c = ( max += 1 ).chr @right = SpecialPattern.new engine, c, c, engine.right_bound @specials << @right end end |
Instance Attribute Details
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
287 288 289 |
# File 'lib/list_matcher.rb', line 287 def engine @engine end |
#left ⇒ Object
Returns the value of attribute left.
288 289 290 |
# File 'lib/list_matcher.rb', line 288 def left @left end |
#list ⇒ Object
Returns the value of attribute list.
288 289 290 |
# File 'lib/list_matcher.rb', line 288 def list @list end |
#right ⇒ Object
Returns the value of attribute right.
288 289 290 |
# File 'lib/list_matcher.rb', line 288 def right @right end |
#specials ⇒ Object
Returns the value of attribute specials.
288 289 290 |
# File 'lib/list_matcher.rb', line 288 def specials @specials end |
Instance Method Details
#normalize ⇒ Object
reduce the list to a version ready for pattern generation
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/list_matcher.rb', line 362 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 && p.left p = "#{left}#{p}" if t l = true end if i == e && p.right p = "#{p}#{right}" r = true end end else p = p.downcase if engine.case_insensitive if engine.bound if i == 0 && engine.word_test === p[0] p = "#{left}#{p}" l = true end if i == e && 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_map ⇒ Object
353 354 355 |
# File 'lib/list_matcher.rb', line 353 def special_map @special_map ||= {} end |
#symbols(s) ⇒ Object
357 358 359 |
# File 'lib/list_matcher.rb', line 357 def symbols(s) special_map[s] end |
#verify ⇒ Object
confirm that all special patterns are legitimate regexen
343 344 345 346 347 348 349 350 351 |
# File 'lib/list_matcher.rb', line 343 def verify specials.each do |s| begin Regexp.new s.pat rescue raise SyntaxError.new "the symbol #{s.var} has an ill-formed pattern: #{s.pat}" end end end |