Class: RedParse::Rule

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

Overview

original user rules, slightly chewed on

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rawrule, priority) ⇒ Rule

Returns a new instance of Rule.



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
407
408
409
410
411
# File 'lib/redparse/compile.rb', line 379

def initialize(rawrule,priority)
  @priority=priority
  @action=rawrule.right
  @patterns=rawrule.left.subregs.dup
  #remove lookback decoration if any, just note that lb was present
  if Reg::LookBack===@patterns[0] 
    @lookback=true
    @patterns[0]=@patterns[0].subregs[0]
  end

  case @patterns[-1]
  #Symbol is pointless here, methinks.
  when Proc,Symbol;    #do nothing
  when Reg::LookAhead; @patterns[-1]=@patterns[-1].subregs[0]
  else                 @patterns.push Object  #add la if none was present
  end

  #search for looping matchers with minimum >0 and replace them
  #with a number of scalars (== the minimum) followed by a loop with 0 min.
  #search for bare strings or regexps and replace with KW(   ) wrapper
  @patterns.each_with_index{|p,i|
    case p
    when String,Regexp; @patterns[i]=RedParse.KW(p)
    when Reg::Repeat
      if p.itemrange.first>0
        @patterns[i,1]=
          *[p.subregs[0]]*p.itemrange.first<< #minimum # as scalars
          p.subregs[0].reg.* #0-based looper
      end
    end
  }
  @drs=[]
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



421
422
423
# File 'lib/redparse/compile.rb', line 421

def action
  @action
end

#drsObject (readonly)

Returns the value of attribute drs.



413
414
415
# File 'lib/redparse/compile.rb', line 413

def drs
  @drs
end

#nameObject

Returns the value of attribute name.



422
423
424
# File 'lib/redparse/compile.rb', line 422

def name
  @name
end

#patternsObject (readonly)

Returns the value of attribute patterns.



421
422
423
# File 'lib/redparse/compile.rb', line 421

def patterns
  @patterns
end

#priorityObject (readonly)

Returns the value of attribute priority.



421
422
423
# File 'lib/redparse/compile.rb', line 421

def priority
  @priority
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



416
# File 'lib/redparse/compile.rb', line 416

def == other; Rule===other and priority==other.priority end

#at(n) ⇒ Object



424
425
426
427
428
# File 'lib/redparse/compile.rb', line 424

def at(n)
  result=patterns[n]
  result=result.subregs[0] if Reg::Repeat===result
  result
end

#final_promised_patternObject



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/redparse/compile.rb', line 457

def final_promised_pattern
  case @action
  when DeleteMonkey #delete_monkey
    vector_indexes=(@action.first_changed_index..-1).select{|i| Reg::Repeat===@patterns[i] }
    fail unless vector_indexes.empty?
    result=@patterns.dup
    result.delete_at @action.first_changed_index
  when StackMonkey #stack_monkey
    result=@patterns.dup
    result[@action.first_changed_index..-1]=[@action.hint]
  when Class
    result= [@action,@patterns.last]
    result.unshift @patterns.first if lookback?
  when :accept, :error, :shift
    result=@patterns.dup
  else 
    pp @action
    fail
  end
  result[-1]=result[-1].la unless result.empty?
  result
end

#final_promised_ruleObject



480
481
482
483
# File 'lib/redparse/compile.rb', line 480

def final_promised_rule
  @final_promised_rule ||=
    Rule.new(-final_promised_pattern>>nil,-priority)
end

#hashObject



415
# File 'lib/redparse/compile.rb', line 415

def hash; priority end

#lookback?Boolean

Returns:

  • (Boolean)


419
# File 'lib/redparse/compile.rb', line 419

def lookback?; @lookback if defined? @lookback end

#looping?(n) ⇒ Boolean

Returns:

  • (Boolean)


433
434
435
436
437
438
439
440
441
# File 'lib/redparse/compile.rb', line 433

def looping? n
  p=patterns[n]
  return false unless Reg::Repeat===p 
  return false if p.itemrange.last==1
  fail unless p.itemrange.last.infinite?
  return true
rescue Exception
  return false
end

#optional?(n) ⇒ Boolean

Returns:

  • (Boolean)


429
430
431
432
# File 'lib/redparse/compile.rb', line 429

def optional? n
  p=patterns[n]
  return Reg::Repeat===p && p.itemrange.first.zero?
end

#reduces_toObject



443
444
445
446
447
448
449
450
# File 'lib/redparse/compile.rb', line 443

def reduces_to
  case @action
  when Class; @action
  when StackMonkey; @action.exemplars
  when :error,:shift,:accept; nil
  else fail "#@action unexpected in reduces_to"
  end
end

#unruly?Boolean

Returns:

  • (Boolean)


452
453
454
455
# File 'lib/redparse/compile.rb', line 452

def unruly?
  return if action==:accept
  action.class!=Class || lookback?
end