Class: Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/pertinent_parser/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, transform = nil, children = [], parent = nil) ⇒ Rule

Returns a new instance of Rule.



5
6
7
8
9
10
# 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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



2
3
4
# File 'lib/pertinent_parser/rule.rb', line 2

def children
  @children
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/pertinent_parser/rule.rb', line 2

def name
  @name
end

#parentObject

Returns the value of attribute parent.



2
3
4
# File 'lib/pertinent_parser/rule.rb', line 2

def parent
  @parent
end

#rangeObject

Returns the value of attribute range.



4
5
6
# File 'lib/pertinent_parser/rule.rb', line 4

def range
  @range
end

#transformObject

Returns the value of attribute transform.



3
4
5
# File 'lib/pertinent_parser/rule.rb', line 3

def transform
  @transform
end

Instance Method Details

#+(text) ⇒ Object



27
28
29
30
# File 'lib/pertinent_parser/rule.rb', line 27

def +(text)
  add(text.rule)
  return text
end

#<=>(r) ⇒ Object



11
12
13
# File 'lib/pertinent_parser/rule.rb', line 11

def <=>(r)
  range.first <=> r.range.first
end

#add(new_rule) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



22
23
24
25
26
# 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) ⇒ Object



14
15
16
17
18
19
20
21
# 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