class RedParse::Rule
Attributes
action[R]
drs[R]
name[RW]
patterns[R]
priority[R]
Public Class Methods
new(rawrule,priority)
click to toggle source
# 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
Public Instance Methods
==(other;)
click to toggle source
# File lib/redparse/compile.rb, line 324 def == other; Rule===other and priority==other.priority end
Also aliased as: eql?
at(n)
click to toggle source
# 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_pattern()
click to toggle source
# 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_rule()
click to toggle source
# File lib/redparse/compile.rb, line 388 def final_promised_rule @final_promised_rule ||= Rule.new(-final_promised_pattern>>nil,-priority) end
hash()
click to toggle source
# File lib/redparse/compile.rb, line 323 def hash; priority end
lookback?()
click to toggle source
# File lib/redparse/compile.rb, line 327 def lookback?; @lookback if defined? @lookback end
looping?(n)
click to toggle source
# 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)
click to toggle source
# File lib/redparse/compile.rb, line 337 def optional? n p=patterns[n] return Reg::Repeat===p && p.itemrange.first.zero? end
reduces_to()
click to toggle source
# 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?()
click to toggle source
# File lib/redparse/compile.rb, line 360 def unruly? return if action==:accept action.class!=Class || lookback? end