Class: Interscript::Node::Rule::Sub

Inherits:
Interscript::Node::Rule show all
Defined in:
lib/interscript/node/rule/sub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, before: nil, not_before: nil, after: nil, not_after: nil, priority: nil, reverse_run: nil) ⇒ Sub

Returns a new instance of Sub.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/interscript/node/rule/sub.rb', line 8

def initialize (from, to,
                before: nil, not_before: nil,
                after: nil, not_after: nil,
                priority: nil, reverse_run: nil)
  self.from = Interscript::Node::Item.try_convert from
  if to == :upcase
    self.to = :upcase
  elsif to == :downcase
    self.to = :downcase
  else
    self.to = Interscript::Node::Item.try_convert to
  end

  self.priority = priority

  #raise TypeError, "Can't supply both before and not_before" if before && not_before
  #raise TypeError, "Can't supply both after and not_after" if after && not_after

  self.reverse_run = reverse_run

  self.before = Interscript::Node::Item.try_convert(before) if before
  self.after = Interscript::Node::Item.try_convert(after) if after
  self.not_before = Interscript::Node::Item.try_convert(not_before) if not_before
  self.not_after = Interscript::Node::Item.try_convert(not_after) if not_after
end

Instance Attribute Details

#afterObject

Returns the value of attribute after.



3
4
5
# File 'lib/interscript/node/rule/sub.rb', line 3

def after
  @after
end

#beforeObject

Returns the value of attribute before.



3
4
5
# File 'lib/interscript/node/rule/sub.rb', line 3

def before
  @before
end

#fromObject

Returns the value of attribute from.



2
3
4
# File 'lib/interscript/node/rule/sub.rb', line 2

def from
  @from
end

#not_afterObject

Returns the value of attribute not_after.



3
4
5
# File 'lib/interscript/node/rule/sub.rb', line 3

def not_after
  @not_after
end

#not_beforeObject

Returns the value of attribute not_before.



3
4
5
# File 'lib/interscript/node/rule/sub.rb', line 3

def not_before
  @not_before
end

#priorityObject

Returns the value of attribute priority.



6
7
8
# File 'lib/interscript/node/rule/sub.rb', line 6

def priority
  @priority
end

#reverse_afterObject

Returns the value of attribute reverse_after.



4
5
6
# File 'lib/interscript/node/rule/sub.rb', line 4

def reverse_after
  @reverse_after
end

#reverse_beforeObject

Returns the value of attribute reverse_before.



4
5
6
# File 'lib/interscript/node/rule/sub.rb', line 4

def reverse_before
  @reverse_before
end

#reverse_not_afterObject

Returns the value of attribute reverse_not_after.



4
5
6
# File 'lib/interscript/node/rule/sub.rb', line 4

def reverse_not_after
  @reverse_not_after
end

#reverse_not_beforeObject

Returns the value of attribute reverse_not_before.



4
5
6
# File 'lib/interscript/node/rule/sub.rb', line 4

def reverse_not_before
  @reverse_not_before
end

#reverse_runObject

Returns the value of attribute reverse_run.



5
6
7
# File 'lib/interscript/node/rule/sub.rb', line 5

def reverse_run
  @reverse_run
end

#toObject

Returns the value of attribute to.



2
3
4
# File 'lib/interscript/node/rule/sub.rb', line 2

def to
  @to
end

Instance Method Details

#==(other) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/interscript/node/rule/sub.rb', line 199

def ==(other)
  super &&
  self.from == other.from &&
  self.to == other.to &&
  self.before == other.before &&
  self.after == other.after &&
  self.not_before == other.not_before &&
  self.not_after == other.not_after &&
  self.priority == other.priority
end

#has_assertions?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/interscript/node/rule/sub.rb', line 99

def has_assertions?
  !!(before || not_before || not_after || after)
end

#inspectObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/interscript/node/rule/sub.rb', line 210

def inspect
  out = "sub "
  params = []
  params << @from.inspect
  if Symbol === @to
    params << @to.to_s
  else
    params << @to.inspect
  end
  params << "reverse_run: #{@reverse_run.inspect}" unless @reverse_run.nil?

  params << "before: #{@before.inspect}" if @before
  params << "after: #{@after.inspect}" if @after
  params << "not_before: #{@not_before.inspect}" if @not_before
  params << "not_after: #{@not_after.inspect}" if @not_after

  params << "priority: #{@priority.inspect}" if @priority
  out << params.join(", ")
end

#max_lengthObject



34
35
36
37
38
39
40
41
42
# File 'lib/interscript/node/rule/sub.rb', line 34

def max_length
  len = self.from.max_length
  len += self.before.max_length if self.before
  len += self.after.max_length if self.after
  len += self.not_before.max_length if self.not_before
  len += self.not_after.max_length if self.not_after
  len += self.priority if self.priority
  len
end

#reverseObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/interscript/node/rule/sub.rb', line 67

def reverse
  if to == :upcase
    xfrom = from.downcase
    xto = :downcase
  elsif to == :downcase
    xfrom = from.upcase
    xto = :upcase
  else
    xto, xfrom = reverse_transfer(from, to)
  end

  # A special case: sub "a", "" shouldn't be present in a reverse map
  rrun = self.reverse_run.nil? ? nil : !self.reverse_run
  if rrun.nil? && !has_assertions? &&
    (xfrom == "" ||
      (Interscript::Node::Item::String === xfrom && xfrom.data == '') ||
      (Interscript::Node::Item::Alias === xfrom && xfrom.name == :none)
    )

    rrun = true
  end

  Interscript::Node::Rule::Sub.new(xfrom, xto,
    before: before, after: after,
    not_before: not_before, not_after: not_after,

    reverse_run: rrun,

    priority: priority ? -priority : nil
  )
end

#reverse_transfer(from, to) ⇒ Object

Attempt to transfer some references to boundary/line_begin around. Those in general should go into before/after clauses, but for now let’s try to get the best compatibility possible. Also, CaptureGroup, CaptureRef need to be shifted around



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/interscript/node/rule/sub.rb', line 107

def reverse_transfer from, to
  # This part is about moving initial and final boundary like aliases
  case from
  when Interscript::Node::Item::Group
    first = from.children.first
    last = from.children.last

    if Interscript::Node::Item::Alias === first && first.boundary_like?
      out = Interscript::Node::Item::Group.new + first + to
      to = out.compact

      from = from.dup.tap do |i|
        i.children = i.children[1..-1]
      end.compact
    end

    if Interscript::Node::Item::Alias === last && last.boundary_like?
      out = Interscript::Node::Item::Group.new + to + last
      to = out.compact

      from = from.dup.tap do |i|
        i.children = i.children[0..-2]
      end.compact
    end
  when Interscript::Node::Item::Alias
    if from.boundary_like?
      to = if from.name.to_s.end_with? "_end"
        Interscript::Node::Item::Group.new + to + from
      else
        Interscript::Node::Item::Group.new + from + to
      end
      from = Interscript::Node::Item::Alias.new(:none)
    end
  end

  # This part is about moving backreferences
  state = {left:[], right:[]}

  from  = reverse_transfer_visit(from, :from, state)
  to    = reverse_transfer_visit(to,   :to,   state)

  [from, to]
end

#to_hashObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/interscript/node/rule/sub.rb', line 44

def to_hash
  puts self.from.inspect if $DEBUG
  puts params.inspect if $DEBUG
  hash = { :class => self.class.to_s,
    :from => self.from.to_hash,
    :to => Symbol === self.to ? self.to : self.to.to_hash,
    :reverse_run => self.reverse_run,
    :before => self.before&.to_hash,
    :not_before => self.not_before&.to_hash,
    :after => self.after&.to_hash,
    :not_after => self.not_after&.to_hash,
    :priority => self.priority
  }

  hash[:before] = self.before&.to_hash if self.before
  hash[:not_before] = self.not_before&.to_hash if self.not_before
  hash[:after] = self.after&.to_hash if self.after
  hash[:not_after] = self.not_after&.to_hash if self.not_after
  hash[:priority] = self.priority if self.priority

  hash
end