Class: TheFox::Range::Lexer::Scope

Inherits:
Base
  • Object
show all
Defined in:
lib/thefox-ext/range/lexer/scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_child, #chain, #children, #dup, generate_nonce, #has_children, #has_parent_item, #instance_id, #instance_id=, #next_item, #next_item=, #nonce, #nonce=, #org_prev_item, #org_prev_item=, #parent_item, #parent_item=, #prev_item, #prev_item=, #symbole

Constructor Details

#initialize(items = nil, parent_scope = nil, level = 0) ⇒ Scope

Returns a new instance of Scope.



6
7
8
9
10
11
12
13
14
15
# File 'lib/thefox-ext/range/lexer/scope.rb', line 6

def initialize(items = nil, parent_scope = nil, level = 0)
  super()

  @item_collection = Collection.new(items)
  @parent_scope = parent_scope
  @level = level
  @ref_item = nil

  # puts '%s-> Scope(#%s).initialize(p=%s lev=%d)'.colorize(:red) % [' ' * (@level * 2), @instance_id, @parent_scope.inspect, @level]
end

Class Method Details

.keep_instance_id_on_dupObject



259
260
261
# File 'lib/thefox-ext/range/lexer/scope.rb', line 259

def keep_instance_id_on_dup()
  true
end

.keep_nonce_on_dupObject



256
257
258
# File 'lib/thefox-ext/range/lexer/scope.rb', line 256

def keep_nonce_on_dup()
  true
end

Instance Method Details

#currObject



40
41
42
# File 'lib/thefox-ext/range/lexer/scope.rb', line 40

def curr()
  @item_collection.curr
end

#inspectObject



21
22
23
# File 'lib/thefox-ext/range/lexer/scope.rb', line 21

def inspect()
  'Scope(#%s len=%d)' % [@instance_id, @item_collection.length]
end

#itemsObject



36
37
38
# File 'lib/thefox-ext/range/lexer/scope.rb', line 36

def items()
  @item_collection.items
end

#nameObject

:nocov:



18
19
20
# File 'lib/thefox-ext/range/lexer/scope.rb', line 18

def name()
  'Scope(#%s)' % [@instance_id]
end

#parent_scopeObject



48
49
50
# File 'lib/thefox-ext/range/lexer/scope.rb', line 48

def parent_scope()
  @parent_scope
end

#parent_scope=(parent_scope) ⇒ Object



51
52
53
# File 'lib/thefox-ext/range/lexer/scope.rb', line 51

def parent_scope=(parent_scope)
  @parent_scope = parent_scope
end

#prevObject



44
45
46
# File 'lib/thefox-ext/range/lexer/scope.rb', line 44

def prev()
  @item_collection.prev
end

#push(item) ⇒ Object

:nocov:



26
27
28
29
30
31
32
33
34
# File 'lib/thefox-ext/range/lexer/scope.rb', line 26

def push(item)
  # puts '%s-> %s.push(%s) -> PS=%s PRI=%s'.colorize(:green) % [' ' * (@level * 2),
  #   inspect,
  #   item.inspect,
  #   @parent_scope.inspect,
  #   @parent_scope.ref_item.inspect,
  # ]
  @item_collection.push(item)
end

#ref_itemObject



55
56
57
# File 'lib/thefox-ext/range/lexer/scope.rb', line 55

def ref_item()
  @ref_item
end

#ref_item=(ref_item) ⇒ Object



58
59
60
61
62
# File 'lib/thefox-ext/range/lexer/scope.rb', line 58

def ref_item=(ref_item)
  # puts '%s--> Set Ref Item: %s'.colorize(:green) % [' ' * (@level * 2), inspect]
  ref_item.is_ref_item = true
  @ref_item = ref_item
end

#resolveObject



64
65
66
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
98
99
100
101
102
103
104
105
106
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/thefox-ext/range/lexer/scope.rb', line 64

def resolve()
  # puts '-> %s.resolve(%d)' % [self.inspect, @level]

  # puts '%s-> SL1: %s P=%s' % [' ' * (@level * 2), inspect, @parent_item.inspect]

  scopes = Collection.new([Scope.new(nil, self, @level + 1)])
  block_stack = Collection.new()
  parent_item = nil
  @item_collection.items.each do |item|
    # puts '%s-> SL1 Item: %s' % [' ' * (@level * 2), item.inspect]

    push_to_scope = false

    case item
    when Separator
      if block_stack.length == 0
        scopes.push(Scope.new(nil, self, @level + 1))
      else
        push_to_scope = true
      end
    when BlockDown
      if block_stack.length == 0
        # puts '%s--> BlockDown: Curr=%s'.colorize(:green) % [' ' * (@level * 2),
        #   scopes.curr.curr.inspect,
        # ]

        prev_item = scopes.curr.curr

        scopes.push(Scope.new(nil, self, @level + 1))
        # scopes.curr.ref_item = item.prev_item
        scopes.curr.ref_item = prev_item

        # puts '%s--> BlockDown: PrevItem=%s'.colorize(:green) % [' ' * (@level * 2),
        #   scopes.curr.ref_item.inspect,
        # ]
      else
        push_to_scope = true
      end

      # Block Stack
      block_stack.push(item)
    when BlockUp
      block_stack.pop

      prev_ref_item = scopes.curr.ref_item
      # puts '-> BlockUp: %s' % [prev_ref_item.inspect]

      # TODO: THIS!!!!
      scopes.push(Scope.new(nil, self, @level + 1))
      scopes.curr.ref_item = prev_ref_item
    else
      push_to_scope = true
    end # case item

    if push_to_scope
      # puts '%s--> Push Item: %s PS=%s PI=%s RI=%s,%s' % [' ' * (@level * 2),
      #   item.inspect,
      #   scopes.curr.parent_scope.inspect,
      #   scopes.curr.parent_item.inspect,
      #   @ref_item.inspect,
      #   scopes.curr.ref_item.inspect,
      # ]
      scopes.curr.push(item)
      scopes.curr.curr.parent_item = self
      # puts '%s--> Pushed Item: %s' % [' ' * (@level * 2), item.inspect]
    end
  end # @item_collection.items

  # puts
  # puts '%s-> Scopes: %s'.colorize(:blue) % [' ' * (@level * 2), scopes.inspect]
  # scopes.items.each do |scope|
  #   puts scope.inspect.colorize(:blue)
  #   puts scope.items.map{ |i| i.inspect }.to_s.colorize(:blue)
  # end

  if scopes.length > 1
    resolved = []
    scopes.items.each do |scope|
      resolved.push(scope.resolve)
    end
    resolved
  else
    # puts
    # puts '%s-> SL1 Items' % [' ' * (@level * 2)]
    # pp scopes.curr.items.map{ |item| item.inspect }

    # puts '%s-> SL2 ' % [' ' * (@level * 2)]
    item_collection1 = Collection.new
    scopes.curr.items.each do |item|
      # puts '%s-> SL2 Item: %s (P=%s)' % [
      #   ' ' * (@level * 2),
      #   item.inspect,
      #   item.parent_item.inspect,
      # ]

      case item
      when Number
        # puts '--> Its %s' % [item.inspect]
        if item.next_item.is_a?(Range) || item.prev_item.is_a?(Range)
          # Skip Range
          # puts '--> Skip Range'
        elsif item.prev_item.is_a?(Interval)
          # Skip Interval
          # puts '%s--> Skip Interval' % [' ' * (@level * 2)]
          # item.prev_item.next_item = nil
          # item.parent_item = item.prev_item
          # item.prev_item = nil
          # item.next_item = nil
        elsif item.next_item.is_a?(Operator)
          # Skip Operator
          # puts '--> Skip Operator'
        elsif item.has_children
          # Skip
          # puts '--> Skip Has Children'
        elsif item.is_ref_item
          # puts '--> Skip Ref Item'
        else
          # puts '--> Push'
          # puts '--> Push: %s' % [item.inspect]
          item_collection1.push(item)
        end
      when Range
        if item.prev_item.is_a?(Number) && item.next_item.is_a?(Number)
          # puts '--> Range normal'
          item_collection1.push(item)
          item_collection1.curr.left_item = item.prev_item
          item_collection1.curr.right_item = item.next_item

          # 1-10/3
          # item.prev_item                       Number
          # item                                 Range
          # item.next_item                       Number
          # item.next_item.next_item             Interval
          # item.next_item.next_item.next_item   Number

          interval_item = item.next_item.next_item
          if interval_item.is_a?(Interval)
            item_collection1.curr.interval = interval_item
            item_collection1.curr.interval.number = item.next_item.next_item.next_item
            item_collection1.curr.interval.number.parent_item = item_collection1.curr.interval
          end
        else
          raise 'Invalid Range: %s %s' % [
            item.prev_item.inspect,
            item.next_item.inspect,
          ]
        end
      when Interval
        # Skip
      when Operator
        if item_collection1.curr.is_a?(Range)
          # puts '%s-> Operator, Curr Range'.colorize(:cyan) % [' ' * (@level * 2)]

          if item_collection1.curr.right_item.is_a?(Number)
            item_collection1.curr.right_item.inc
          end
        elsif item.prev_item.is_a?(Number)
          # puts '%s-> Operator, Number'.colorize(:cyan) % [' ' * (@level * 2)]

          item_collection1.push(Range.new(item.symbole))
          item_collection1.curr.left_item = item.prev_item
          item_collection1.curr.right_item = item.prev_item
          item_collection1.curr.right_item.inc
        elsif item.prev_item.is_a?(Operator)
          # puts '%s-> Operator, Prev Operator'.colorize(:cyan) % [' ' * (@level * 2)]

          if item_collection1.curr.is_a?(Range)
            if item_collection1.curr.right_item.is_a?(Number)
              item_collection1.curr.right_item.inc
            end
          end
        # else
        #   puts '%s-> Operator ELSE: %s' % [' ' * (@level * 2), item.prev_item.inspect]
        end
      else
        item_collection1.push(item)
      end # case item
    end # scopes.curr.items.each

    # puts '%s-> L3 Items' % [' ' * (@level * 2)]
    # pp item_collection1.items.map{ |item| item.inspect }

    # puts '%s-> L4 [convert to int]'.colorize(:blue) % [' ' * (@level * 2)]
    items2 = []
    item_collection1.items.each do |item|
      items2.push(item.resolve)
    end
    items2
  end
end