class Rule

Attributes

children[RW]
name[RW]
parent[RW]
range[RW]
transform[RW]

Public Class Methods

new(range, transform=nil, children=[], parent=nil) click to toggle source
# File lib/pertinent_parser/rule.rb, line 5
def initialize(range, transform=nil, children=[], parent=nil)
  @range = range.to_a
  @children = children
  @parent = parent
  @transform = transform
end

Public Instance Methods

+(text) click to toggle source
# File lib/pertinent_parser/rule.rb, line 27
def +(text)
  add(text.rule)
  return text
end
<=>(r) click to toggle source
# File lib/pertinent_parser/rule.rb, line 11
def <=>(r)
  range.first <=> r.range.first
end
add(new_rule) click to toggle source
# File lib/pertinent_parser/rule.rb, line 31
def add(new_rule)
  intersection = range & new_rule.range
  if intersection == new_rule.range
    contain = []
    input = new_rule
    @children.each do |child|
      result = child.add(input)
      case result
      when Rule        
        input = result
      when :inside     
        return :inside
      when :contain    
        contain << child
      when :outside
      end
    end
    @children -= contain
    contain.each do |child|
      input.add child
    end
    @children << input
    @children.sort!
    return :inside
  elsif intersection.empty?
    return :outside
  elsif intersection == range
    if @parent.nil?
      children = new_rule.children
      new_rule.children = [self]
      children.each do |child|
        new_rule.add child
      end
      return new_rule
    end
    return :contain
  else
    difference = new_rule.range - intersection
    transforms = new_rule.transform.split(difference.size)
    if intersection.first < difference.first
      inter_tran, diff_tran = transforms
    else
      diff_tran, inter_tran = transforms
    end
    self.add(Rule.new(intersection, inter_tran))
    return Rule.new(difference, diff_tran)
  end
end
apply(str) click to toggle source
# File lib/pertinent_parser/rule.rb, line 22
def apply(str)
  s = str.dup
  apply_recur(s)
  return s
end
apply_recur(s, offset=0) click to toggle source
# File lib/pertinent_parser/rule.rb, line 14
def apply_recur(s, offset=0)
  pre = offset
  @children.each do |child|
    offset += child.apply_recur(s, offset)
  end
  # This was an optimization gone wrong. Sorry. Applies the transformation to the portion of the text.
  return (s[@range.first+pre..@range.last+offset] = @transform.apply(s[@range.first+pre..@range.last+offset])).size - range.size
end