dentaku 3.2.0 → 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +5 -10
  3. data/.travis.yml +4 -6
  4. data/CHANGELOG.md +86 -2
  5. data/README.md +7 -6
  6. data/dentaku.gemspec +1 -1
  7. data/lib/dentaku/ast/access.rb +21 -1
  8. data/lib/dentaku/ast/arithmetic.rb +51 -15
  9. data/lib/dentaku/ast/array.rb +41 -0
  10. data/lib/dentaku/ast/bitwise.rb +30 -5
  11. data/lib/dentaku/ast/case/case_conditional.rb +17 -2
  12. data/lib/dentaku/ast/case/case_else.rb +17 -3
  13. data/lib/dentaku/ast/case/case_switch_variable.rb +14 -0
  14. data/lib/dentaku/ast/case/case_then.rb +17 -3
  15. data/lib/dentaku/ast/case/case_when.rb +21 -3
  16. data/lib/dentaku/ast/case.rb +19 -3
  17. data/lib/dentaku/ast/comparators.rb +38 -28
  18. data/lib/dentaku/ast/function.rb +11 -3
  19. data/lib/dentaku/ast/function_registry.rb +21 -0
  20. data/lib/dentaku/ast/functions/all.rb +23 -0
  21. data/lib/dentaku/ast/functions/and.rb +2 -2
  22. data/lib/dentaku/ast/functions/any.rb +23 -0
  23. data/lib/dentaku/ast/functions/avg.rb +2 -2
  24. data/lib/dentaku/ast/functions/count.rb +8 -0
  25. data/lib/dentaku/ast/functions/duration.rb +51 -0
  26. data/lib/dentaku/ast/functions/enum.rb +37 -0
  27. data/lib/dentaku/ast/functions/filter.rb +23 -0
  28. data/lib/dentaku/ast/functions/if.rb +19 -2
  29. data/lib/dentaku/ast/functions/map.rb +23 -0
  30. data/lib/dentaku/ast/functions/or.rb +4 -4
  31. data/lib/dentaku/ast/functions/pluck.rb +30 -0
  32. data/lib/dentaku/ast/functions/round.rb +1 -1
  33. data/lib/dentaku/ast/functions/rounddown.rb +1 -1
  34. data/lib/dentaku/ast/functions/roundup.rb +1 -1
  35. data/lib/dentaku/ast/functions/ruby_math.rb +50 -3
  36. data/lib/dentaku/ast/functions/string_functions.rb +105 -12
  37. data/lib/dentaku/ast/functions/xor.rb +44 -0
  38. data/lib/dentaku/ast/grouping.rb +3 -1
  39. data/lib/dentaku/ast/identifier.rb +16 -4
  40. data/lib/dentaku/ast/literal.rb +10 -0
  41. data/lib/dentaku/ast/negation.rb +7 -1
  42. data/lib/dentaku/ast/nil.rb +4 -0
  43. data/lib/dentaku/ast/node.rb +8 -0
  44. data/lib/dentaku/ast/operation.rb +17 -0
  45. data/lib/dentaku/ast/string.rb +7 -0
  46. data/lib/dentaku/ast.rb +8 -0
  47. data/lib/dentaku/bulk_expression_solver.rb +38 -27
  48. data/lib/dentaku/calculator.rb +21 -8
  49. data/lib/dentaku/date_arithmetic.rb +45 -0
  50. data/lib/dentaku/exceptions.rb +11 -8
  51. data/lib/dentaku/flat_hash.rb +9 -2
  52. data/lib/dentaku/parser.rb +57 -16
  53. data/lib/dentaku/print_visitor.rb +101 -0
  54. data/lib/dentaku/token_matcher.rb +1 -1
  55. data/lib/dentaku/token_scanner.rb +9 -3
  56. data/lib/dentaku/tokenizer.rb +7 -2
  57. data/lib/dentaku/version.rb +1 -1
  58. data/lib/dentaku/visitor/infix.rb +82 -0
  59. data/lib/dentaku.rb +20 -7
  60. data/spec/ast/addition_spec.rb +7 -1
  61. data/spec/ast/all_spec.rb +25 -0
  62. data/spec/ast/and_function_spec.rb +6 -6
  63. data/spec/ast/and_spec.rb +1 -1
  64. data/spec/ast/any_spec.rb +23 -0
  65. data/spec/ast/arithmetic_spec.rb +64 -29
  66. data/spec/ast/avg_spec.rb +9 -5
  67. data/spec/ast/comparator_spec.rb +31 -1
  68. data/spec/ast/count_spec.rb +7 -7
  69. data/spec/ast/division_spec.rb +7 -1
  70. data/spec/ast/filter_spec.rb +25 -0
  71. data/spec/ast/function_spec.rb +20 -15
  72. data/spec/ast/map_spec.rb +27 -0
  73. data/spec/ast/max_spec.rb +16 -3
  74. data/spec/ast/min_spec.rb +16 -3
  75. data/spec/ast/mul_spec.rb +11 -6
  76. data/spec/ast/negation_spec.rb +48 -0
  77. data/spec/ast/node_spec.rb +11 -8
  78. data/spec/ast/numeric_spec.rb +1 -1
  79. data/spec/ast/or_spec.rb +7 -7
  80. data/spec/ast/pluck_spec.rb +32 -0
  81. data/spec/ast/round_spec.rb +14 -4
  82. data/spec/ast/rounddown_spec.rb +14 -4
  83. data/spec/ast/roundup_spec.rb +14 -4
  84. data/spec/ast/string_functions_spec.rb +73 -0
  85. data/spec/ast/sum_spec.rb +11 -6
  86. data/spec/ast/switch_spec.rb +5 -5
  87. data/spec/ast/xor_spec.rb +35 -0
  88. data/spec/bulk_expression_solver_spec.rb +37 -1
  89. data/spec/calculator_spec.rb +341 -32
  90. data/spec/dentaku_spec.rb +19 -6
  91. data/spec/external_function_spec.rb +32 -6
  92. data/spec/parser_spec.rb +100 -123
  93. data/spec/print_visitor_spec.rb +66 -0
  94. data/spec/spec_helper.rb +6 -4
  95. data/spec/token_matcher_spec.rb +8 -8
  96. data/spec/token_scanner_spec.rb +4 -4
  97. data/spec/tokenizer_spec.rb +56 -13
  98. data/spec/visitor/infix_spec.rb +31 -0
  99. data/spec/visitor_spec.rb +138 -0
  100. metadata +52 -7
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+ require 'set'
3
+
4
+ class TestVisitor
5
+ attr_reader :visited
6
+
7
+ def initialize(node)
8
+ @visited = Set.new
9
+ node.accept(self)
10
+ end
11
+
12
+ def mark_visited(node)
13
+ @visited.add(node.class.to_s.split("::").last.to_sym)
14
+ end
15
+
16
+ def visit_operation(node)
17
+ mark_visited(node)
18
+
19
+ node.left.accept(self) if node.left
20
+ node.right.accept(self) if node.right
21
+ end
22
+
23
+ def visit_function(node)
24
+ mark_visited(node)
25
+ node.args.each { |a| a.accept(self) }
26
+ end
27
+
28
+ def visit_array(node)
29
+ mark_visited(node)
30
+ end
31
+
32
+ def visit_case(node)
33
+ mark_visited(node)
34
+ node.switch.accept(self)
35
+ node.conditions.each { |c| c.accept(self) }
36
+ node.else && node.else.accept(self)
37
+ end
38
+
39
+ def visit_switch(node)
40
+ mark_visited(node)
41
+ node.node.accept(self)
42
+ end
43
+
44
+ def visit_case_conditional(node)
45
+ mark_visited(node)
46
+ node.when.accept(self)
47
+ node.then.accept(self)
48
+ end
49
+
50
+ def visit_when(node)
51
+ mark_visited(node)
52
+ node.node.accept(self)
53
+ end
54
+
55
+ def visit_then(node)
56
+ mark_visited(node)
57
+ node.node.accept(self)
58
+ end
59
+
60
+ def visit_else(node)
61
+ mark_visited(node)
62
+ node.node.accept(self)
63
+ end
64
+
65
+ def visit_negation(node)
66
+ mark_visited(node)
67
+ node.node.accept(self)
68
+ end
69
+
70
+ def visit_access(node)
71
+ mark_visited(node)
72
+ node.structure.accept(self)
73
+ node.index.accept(self)
74
+ end
75
+
76
+ def visit_literal(node)
77
+ mark_visited(node)
78
+ end
79
+
80
+ def visit_identifier(node)
81
+ mark_visited(node)
82
+ end
83
+
84
+ def visit_nil(node)
85
+ mark_visited(node)
86
+ end
87
+ end
88
+
89
+ describe TestVisitor do
90
+ def generic_subclasses
91
+ [
92
+ :Arithmetic,
93
+ :Bitwise,
94
+ :Combinator,
95
+ :Comparator,
96
+ :Function,
97
+ :FunctionRegistry,
98
+ :Grouping,
99
+ :Literal,
100
+ :Node,
101
+ :Operation,
102
+ :StringFunctions,
103
+ :RubyMath,
104
+ :Enum,
105
+ ]
106
+ end
107
+
108
+ it 'visits all concrete AST node types' do
109
+ @visited = Set.new
110
+
111
+ visit_nodes('(1 + 7) * (8 ^ 2) / - 3.0 - apples')
112
+ visit_nodes('1 < 2 and 3 <= 4 or 5 > 6 AND 7 >= 8 OR 9 != 10 and true')
113
+ visit_nodes('IF(a[0] = NULL, "five", \'seven\')')
114
+ visit_nodes('case (a % 5) when 0 then a else b end')
115
+ visit_nodes('0xCAFE & (0xDECAF << 3) | (0xBEEF >> 5)')
116
+ visit_nodes('2017-12-24 23:59:59')
117
+ visit_nodes('ALL({1, 2, 3}, "val", val % 2 == 0)')
118
+ visit_nodes('ANY(vals, val, val > 1)')
119
+ visit_nodes('COUNT({1, 2, 3})')
120
+ visit_nodes('PLUCK(users, age)')
121
+ visit_nodes('XOR(false, false)')
122
+ visit_nodes('duration(1, day)')
123
+ visit_nodes('MAP(vals, val, val + 1)')
124
+ visit_nodes('FILTER(vals, val, val > 1)')
125
+
126
+ @expected = Set.new(Dentaku::AST::constants - generic_subclasses)
127
+ expect(@visited.sort).to eq(@expected.sort)
128
+ end
129
+
130
+ private
131
+
132
+ def visit_nodes(string)
133
+ tokens = Dentaku::Tokenizer.new.tokenize(string)
134
+ node = Dentaku::Parser.new(tokens).parse
135
+ visitor = TestVisitor.new(node)
136
+ @visited += visitor.visited
137
+ end
138
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dentaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Solomon White
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-14 00:00:00.000000000 Z
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: codecov
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -143,6 +157,7 @@ files:
143
157
  - lib/dentaku/ast.rb
144
158
  - lib/dentaku/ast/access.rb
145
159
  - lib/dentaku/ast/arithmetic.rb
160
+ - lib/dentaku/ast/array.rb
146
161
  - lib/dentaku/ast/bitwise.rb
147
162
  - lib/dentaku/ast/case.rb
148
163
  - lib/dentaku/ast/case/case_conditional.rb
@@ -155,15 +170,22 @@ files:
155
170
  - lib/dentaku/ast/datetime.rb
156
171
  - lib/dentaku/ast/function.rb
157
172
  - lib/dentaku/ast/function_registry.rb
173
+ - lib/dentaku/ast/functions/all.rb
158
174
  - lib/dentaku/ast/functions/and.rb
175
+ - lib/dentaku/ast/functions/any.rb
159
176
  - lib/dentaku/ast/functions/avg.rb
160
177
  - lib/dentaku/ast/functions/count.rb
178
+ - lib/dentaku/ast/functions/duration.rb
179
+ - lib/dentaku/ast/functions/enum.rb
180
+ - lib/dentaku/ast/functions/filter.rb
161
181
  - lib/dentaku/ast/functions/if.rb
182
+ - lib/dentaku/ast/functions/map.rb
162
183
  - lib/dentaku/ast/functions/max.rb
163
184
  - lib/dentaku/ast/functions/min.rb
164
185
  - lib/dentaku/ast/functions/mul.rb
165
186
  - lib/dentaku/ast/functions/not.rb
166
187
  - lib/dentaku/ast/functions/or.rb
188
+ - lib/dentaku/ast/functions/pluck.rb
167
189
  - lib/dentaku/ast/functions/round.rb
168
190
  - lib/dentaku/ast/functions/rounddown.rb
169
191
  - lib/dentaku/ast/functions/roundup.rb
@@ -171,6 +193,7 @@ files:
171
193
  - lib/dentaku/ast/functions/string_functions.rb
172
194
  - lib/dentaku/ast/functions/sum.rb
173
195
  - lib/dentaku/ast/functions/switch.rb
196
+ - lib/dentaku/ast/functions/xor.rb
174
197
  - lib/dentaku/ast/grouping.rb
175
198
  - lib/dentaku/ast/identifier.rb
176
199
  - lib/dentaku/ast/literal.rb
@@ -183,10 +206,12 @@ files:
183
206
  - lib/dentaku/ast/string.rb
184
207
  - lib/dentaku/bulk_expression_solver.rb
185
208
  - lib/dentaku/calculator.rb
209
+ - lib/dentaku/date_arithmetic.rb
186
210
  - lib/dentaku/dependency_resolver.rb
187
211
  - lib/dentaku/exceptions.rb
188
212
  - lib/dentaku/flat_hash.rb
189
213
  - lib/dentaku/parser.rb
214
+ - lib/dentaku/print_visitor.rb
190
215
  - lib/dentaku/string_casing.rb
191
216
  - lib/dentaku/token.rb
192
217
  - lib/dentaku/token_matcher.rb
@@ -194,28 +219,36 @@ files:
194
219
  - lib/dentaku/token_scanner.rb
195
220
  - lib/dentaku/tokenizer.rb
196
221
  - lib/dentaku/version.rb
222
+ - lib/dentaku/visitor/infix.rb
197
223
  - spec/ast/addition_spec.rb
224
+ - spec/ast/all_spec.rb
198
225
  - spec/ast/and_function_spec.rb
199
226
  - spec/ast/and_spec.rb
227
+ - spec/ast/any_spec.rb
200
228
  - spec/ast/arithmetic_spec.rb
201
229
  - spec/ast/avg_spec.rb
202
230
  - spec/ast/case_spec.rb
203
231
  - spec/ast/comparator_spec.rb
204
232
  - spec/ast/count_spec.rb
205
233
  - spec/ast/division_spec.rb
234
+ - spec/ast/filter_spec.rb
206
235
  - spec/ast/function_spec.rb
236
+ - spec/ast/map_spec.rb
207
237
  - spec/ast/max_spec.rb
208
238
  - spec/ast/min_spec.rb
209
239
  - spec/ast/mul_spec.rb
240
+ - spec/ast/negation_spec.rb
210
241
  - spec/ast/node_spec.rb
211
242
  - spec/ast/numeric_spec.rb
212
243
  - spec/ast/or_spec.rb
244
+ - spec/ast/pluck_spec.rb
213
245
  - spec/ast/round_spec.rb
214
246
  - spec/ast/rounddown_spec.rb
215
247
  - spec/ast/roundup_spec.rb
216
248
  - spec/ast/string_functions_spec.rb
217
249
  - spec/ast/sum_spec.rb
218
250
  - spec/ast/switch_spec.rb
251
+ - spec/ast/xor_spec.rb
219
252
  - spec/benchmark.rb
220
253
  - spec/bulk_expression_solver_spec.rb
221
254
  - spec/calculator_spec.rb
@@ -223,16 +256,19 @@ files:
223
256
  - spec/exceptions_spec.rb
224
257
  - spec/external_function_spec.rb
225
258
  - spec/parser_spec.rb
259
+ - spec/print_visitor_spec.rb
226
260
  - spec/spec_helper.rb
227
261
  - spec/token_matcher_spec.rb
228
262
  - spec/token_scanner_spec.rb
229
263
  - spec/token_spec.rb
230
264
  - spec/tokenizer_spec.rb
265
+ - spec/visitor/infix_spec.rb
266
+ - spec/visitor_spec.rb
231
267
  homepage: http://github.com/rubysolo/dentaku
232
268
  licenses:
233
269
  - MIT
234
270
  metadata: {}
235
- post_install_message:
271
+ post_install_message:
236
272
  rdoc_options: []
237
273
  require_paths:
238
274
  - lib
@@ -247,34 +283,40 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
283
  - !ruby/object:Gem::Version
248
284
  version: '0'
249
285
  requirements: []
250
- rubyforge_project: dentaku
251
- rubygems_version: 2.6.13
252
- signing_key:
286
+ rubygems_version: 3.3.7
287
+ signing_key:
253
288
  specification_version: 4
254
289
  summary: A formula language parser and evaluator
255
290
  test_files:
256
291
  - spec/ast/addition_spec.rb
292
+ - spec/ast/all_spec.rb
257
293
  - spec/ast/and_function_spec.rb
258
294
  - spec/ast/and_spec.rb
295
+ - spec/ast/any_spec.rb
259
296
  - spec/ast/arithmetic_spec.rb
260
297
  - spec/ast/avg_spec.rb
261
298
  - spec/ast/case_spec.rb
262
299
  - spec/ast/comparator_spec.rb
263
300
  - spec/ast/count_spec.rb
264
301
  - spec/ast/division_spec.rb
302
+ - spec/ast/filter_spec.rb
265
303
  - spec/ast/function_spec.rb
304
+ - spec/ast/map_spec.rb
266
305
  - spec/ast/max_spec.rb
267
306
  - spec/ast/min_spec.rb
268
307
  - spec/ast/mul_spec.rb
308
+ - spec/ast/negation_spec.rb
269
309
  - spec/ast/node_spec.rb
270
310
  - spec/ast/numeric_spec.rb
271
311
  - spec/ast/or_spec.rb
312
+ - spec/ast/pluck_spec.rb
272
313
  - spec/ast/round_spec.rb
273
314
  - spec/ast/rounddown_spec.rb
274
315
  - spec/ast/roundup_spec.rb
275
316
  - spec/ast/string_functions_spec.rb
276
317
  - spec/ast/sum_spec.rb
277
318
  - spec/ast/switch_spec.rb
319
+ - spec/ast/xor_spec.rb
278
320
  - spec/benchmark.rb
279
321
  - spec/bulk_expression_solver_spec.rb
280
322
  - spec/calculator_spec.rb
@@ -282,8 +324,11 @@ test_files:
282
324
  - spec/exceptions_spec.rb
283
325
  - spec/external_function_spec.rb
284
326
  - spec/parser_spec.rb
327
+ - spec/print_visitor_spec.rb
285
328
  - spec/spec_helper.rb
286
329
  - spec/token_matcher_spec.rb
287
330
  - spec/token_scanner_spec.rb
288
331
  - spec/token_spec.rb
289
332
  - spec/tokenizer_spec.rb
333
+ - spec/visitor/infix_spec.rb
334
+ - spec/visitor_spec.rb