Class: Rookout::Processor::Paths::Canopy::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/rookout/processor/paths/canopy/actions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ Actions

Returns a new instance of Actions.



11
12
13
14
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 11

def initialize namespace
  @namespace = namespace
  @operations = []
end

Instance Attribute Details

#operationsObject (readonly)

Returns the value of attribute operations.



16
17
18
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 16

def operations
  @operations
end

Instance Method Details

#make_and_execute_namespace_operation(input, start, finish, elements) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 51

def make_and_execute_namespace_operation input, start, finish, elements
  # Element 1 will not be null
  # Element 2 is a list of another elements (can be empty)
  # element1.(element2.element3.element4)
  # For further explanation, check ArithmeticPath.peg
  @operations = []
  @operations.push elements[1]

  elements[2].elements.each { |e| @operations.push e }

  @operations.each do |op|
    return op if op.is_a? ToolExceptionMarker
  end

  result = elements[1].read @namespace, false
  elements[2].elements.each do |e|
    result = e.read result, false
  end

  NamespaceResult.new result, input[start...finish]
rescue StandardError => e
  ToolExceptionMarker.new e
end

#make_apostrophe_string(_input, _start, _finish, elements) ⇒ Object



135
136
137
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 135

def make_apostrophe_string _input, _start, _finish, elements
  Text.new elements[2].text
end

#make_attribute(input, start, finish, _elements) ⇒ Object



47
48
49
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 47

def make_attribute input, start, finish, _elements
  AttributeOperation.new input[start...finish]
end

#make_attribute_operation(input, start, finish, _elements) ⇒ Object



43
44
45
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 43

def make_attribute_operation input, start, finish, _elements
  AttributeOperation.new input[start + 1...finish]
end

#make_bool(input, start, finish, _elements) ⇒ Object



173
174
175
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 173

def make_bool input, start, finish, _elements
  Bool.new input[start...finish].delete " "
end

#make_char(input, start, finish, _elements) ⇒ Object



169
170
171
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 169

def make_char input, start, finish, _elements
  Char.new input[start...finish].delete " "
end

#make_comp_exp(_input, _start, _finish, elements) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 84

def make_comp_exp _input, _start, _finish, elements
  # We can assume the following: atom ( opt_ atom )*
  # the first (which must be) will be simple atom
  # the second and so forth will always be pair <Opt, Atom>
  # Its important to remember that this execution will handle the inner brackets if exist
  # In order to handle priority (which i could not figure out if available with canopy library):
  # 1. lets make the tree flat
  # 2. handle priority ourselves - (atom opt1 atom) will be handled before (atom opt2 atom)
  #   and will return TreeNode with result
  # For further explanation, check ArithmeticPath.peg

  # handle case the size is 1
  return elements[0] if elements[1].elements.empty? || elements[0].is_a?(ToolExceptionMarker)

  flat_elements = [elements[0]]
  elements[1].elements.each do |it|
    return it.elements[1] if it.elements[1].is_a? ToolExceptionMarker

    flat_elements.push it.elements[0]
    flat_elements.push it.elements[1]
  end

  until flat_elements.length == 1
    restart_scan = false

    ALL_LEVELS.each_with_index do |_, level|
      flat_elements.each_with_index do |e, index|
        if e.is_a?(Opt) && e.level == level
          result = e.execute_operation flat_elements[index - 1], flat_elements[index + 1]
          flat_elements = flat_elements[0...index - 1] + [result] + flat_elements[index + 2..]
          restart_scan = true
          break
        end

        break if restart_scan
      end

      break if restart_scan
    end
  end

  flat_elements[0]
end

#make_comp_exp_ex(_input, _start, _finish, elements) ⇒ Object



75
76
77
78
79
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 75

def make_comp_exp_ex _input, _start, _finish, elements
  # Element 2 is the actual expression
  # For further explanation, check ArithmeticPath.peg
  elements[2]
end

#make_float(input, start, finish, _elements) ⇒ Object



161
162
163
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 161

def make_float input, start, finish, _elements
  FloatNumber.new input[start...finish].delete " "
end

#make_function_operation(_input, _start, _finish, elements) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 22

def make_function_operation _input, _start, _finish, elements
  name = elements[0].text + elements[1].text
  return elements[3] if elements[3].is_a? Exceptions::ToolException

  args = elements[3].text
  FunctionOperation.new name, args
end

#make_function_operation_access(_input, _start, _finish, elements) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 30

def make_function_operation_access _input, _start, _finish, elements
  # To build the function name, we will merge the unicode_set and all the unicode_set_with_numbers
  # To build the args we will simply read the atom at index 4
  #   which can be result of another operation; thus checking for exception
  # For further explanation, check ArithmeticPath.peg

  return elements[4] if elements[4].is_a? ToolExceptionMarker

  function_name = elements[1].text + elements[2].text
  args = elements[4].text
  FunctionOperation.new function_name, args
end

#make_list(input, start, finish, elements) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 143

def make_list input, start, finish, elements
  list = []
  items_to_scan = [elements[3]]
  until items_to_scan.empty?
    item = items_to_scan.pop

    if item.is_a? Marker
      list.push item
    else
      item.elements.each do |e|
        items_to_scan.push e
      end
    end
  end

  List.new list, input[start...finish]
end

#make_lookup_operation(input, start, finish, _elements) ⇒ Object



18
19
20
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 18

def make_lookup_operation input, start, finish, _elements
  LookupOperation.new input[start + 1...finish - 1]
end

#make_null(_input, _start, _finish, _elements) ⇒ Object



177
178
179
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 177

def make_null _input, _start, _finish, _elements
  Nil.new
end

#make_number(input, start, finish, _elements) ⇒ Object



165
166
167
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 165

def make_number input, start, finish, _elements
  Number.new input[start...finish].delete " "
end

#make_opt(_input, _start, _finish, elements) ⇒ Object

rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/AbcSize



131
132
133
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 131

def make_opt _input, _start, _finish, elements
  Opt.new elements[1].text
end

#make_string(_input, _start, _finish, elements) ⇒ Object



139
140
141
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 139

def make_string _input, _start, _finish, elements
  Text.new elements[2].text
end

#make_undefined(_input, _start, _finish, _elements) ⇒ Object



181
182
183
# File 'lib/rookout/processor/paths/canopy/actions.rb', line 181

def make_undefined _input, _start, _finish, _elements
  Nil.new
end