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.



287
288
289
290
291
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
# File 'lib/redparse/compile.rb', line 287

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.



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

def action
  @action
end

#drsObject (readonly)

Returns the value of attribute drs.



321
322
323
# File 'lib/redparse/compile.rb', line 321

def drs
  @drs
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#patternsObject (readonly)

Returns the value of attribute patterns.



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

def patterns
  @patterns
end

#priorityObject (readonly)

Returns the value of attribute priority.



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

def priority
  @priority
end

Instance Method Details

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



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

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

#at(n) ⇒ Object



332
333
334
335
336
# File 'lib/redparse/compile.rb', line 332

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

#final_promised_patternObject



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/redparse/compile.rb', line 365

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



388
389
390
391
# File 'lib/redparse/compile.rb', line 388

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

#hashObject



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

def hash; priority end

#lookback?Boolean

Returns:

  • (Boolean)


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

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

#looping?(n) ⇒ Boolean

Returns:

  • (Boolean)


341
342
343
344
345
346
347
348
349
# File 'lib/redparse/compile.rb', line 341

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)


337
338
339
340
# File 'lib/redparse/compile.rb', line 337

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

#reduces_toObject



351
352
353
354
355
356
357
358
# File 'lib/redparse/compile.rb', line 351

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)


360
361
362
363
# File 'lib/redparse/compile.rb', line 360

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