dentaku 4.1.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +1 -1
- data/.github/workflows/rubocop.yml +1 -1
- data/CHANGELOG.md +59 -0
- data/README.md +176 -11
- data/lib/dentaku/ast/access.rb +14 -11
- data/lib/dentaku/ast/array.rb +2 -0
- data/lib/dentaku/ast/combinators.rb +83 -0
- data/lib/dentaku/ast/functions/rounddown.rb +8 -2
- data/lib/dentaku/ast/functions/roundup.rb +8 -2
- data/lib/dentaku/ast/member_access.rb +56 -0
- data/lib/dentaku/ast/negation.rb +4 -0
- data/lib/dentaku/ast/operation.rb +6 -0
- data/lib/dentaku/ast.rb +2 -0
- data/lib/dentaku/bulk_expression_solver.rb +40 -2
- data/lib/dentaku/calculator.rb +26 -3
- data/lib/dentaku/dependency_resolver.rb +12 -0
- data/lib/dentaku/humanize_visitor.rb +276 -0
- data/lib/dentaku/parser.rb +15 -2
- data/lib/dentaku/print_visitor.rb +38 -2
- data/lib/dentaku/token_scanner.rb +36 -12
- data/lib/dentaku/tokenizer.rb +6 -1
- data/lib/dentaku/version.rb +1 -1
- data/lib/dentaku/visitor/infix.rb +4 -0
- data/mise.toml +2 -0
- data/spec/ast/member_access_spec.rb +124 -0
- data/spec/builtin_functions_spec.rb +26 -0
- data/spec/bulk_expression_solver_spec.rb +71 -5
- data/spec/calculator_spec.rb +140 -1
- data/spec/humanize_visitor_spec.rb +217 -0
- data/spec/parser_spec.rb +8 -0
- data/spec/print_visitor_spec.rb +66 -0
- data/spec/tokenizer_spec.rb +19 -0
- data/spec/visitor_spec.rb +7 -0
- metadata +7 -2
- data/lib/dentaku/token_matchers.rb +0 -29
data/spec/print_visitor_spec.rb
CHANGED
|
@@ -44,6 +44,64 @@ describe Dentaku::PrintVisitor do
|
|
|
44
44
|
expect(repr).to eq('IF(a[0] = NULL, "five", "seven")')
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
+
it 'handles member access' do
|
|
48
|
+
expect(roundtrip('a[0].b.c + 1')).to eq('a[0].b.c + 1')
|
|
49
|
+
expect(roundtrip('FILTER(xs, x, x.y > 1)[0].y')).to eq('FILTER(xs, x, x.y > 1)[0].y')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'groups anything but accesses and function calls before member access' do
|
|
53
|
+
[
|
|
54
|
+
'(a + b).c',
|
|
55
|
+
'(user).Name',
|
|
56
|
+
'("abc").x',
|
|
57
|
+
'(1).x',
|
|
58
|
+
'(CASE 1 WHEN 1 THEN users[0] END).name',
|
|
59
|
+
'IF(a, b, c).d',
|
|
60
|
+
'a[0].b.c'
|
|
61
|
+
].each do |formula|
|
|
62
|
+
expect(roundtrip(formula)).to eq(formula)
|
|
63
|
+
expect(roundtrip(roundtrip(formula))).to eq(formula)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'groups anything but variables, arrays, accesses and function calls before an index' do
|
|
68
|
+
[
|
|
69
|
+
'(a + b)[0]',
|
|
70
|
+
'(-(a))[0]',
|
|
71
|
+
'("abc")[0]',
|
|
72
|
+
'(CASE 1 WHEN 1 THEN a END)[0]',
|
|
73
|
+
'a[0]',
|
|
74
|
+
'a[0][1]',
|
|
75
|
+
'a[b[0]]',
|
|
76
|
+
'FILTER(a, x, x > 1)[0]',
|
|
77
|
+
'{10, 20, 30}[0]',
|
|
78
|
+
'users[0].name[0]'
|
|
79
|
+
].each do |formula|
|
|
80
|
+
expect(roundtrip(formula)).to eq(formula)
|
|
81
|
+
expect(roundtrip(roundtrip(formula))).to eq(formula)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it 'keeps the meaning of a grouped index' do
|
|
86
|
+
calculator = Dentaku::Calculator.new
|
|
87
|
+
data = { a: [5, 6], b: [1, 2] }
|
|
88
|
+
printed = described_class.new(calculator.ast('(a + b)[0]')).to_s
|
|
89
|
+
expect(calculator.evaluate!(printed, data)).to eq(calculator.evaluate!('(a + b)[0]', data))
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'prints array literals as they are written' do
|
|
93
|
+
['{1, 2}', '{}', '{a, b}', '{{1}, {2, 3}}', '{"x", 1 + 2}', 'SUM({1, 2, x})'].each do |formula|
|
|
94
|
+
expect(roundtrip(formula)).to eq(formula)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'keeps the lookup rules when grouping a variable' do
|
|
99
|
+
calculator = Dentaku::Calculator.new
|
|
100
|
+
data = { user: { 'Name' => 'y', 'name' => 'x' } }
|
|
101
|
+
printed = described_class.new(calculator.ast('(user).Name')).to_s
|
|
102
|
+
expect(calculator.evaluate!(printed, data)).to eq(calculator.evaluate!('(user).Name', data))
|
|
103
|
+
end
|
|
104
|
+
|
|
47
105
|
it 'handles a case statement' do
|
|
48
106
|
repr = roundtrip('case (a % 5) when 0 then a else b end')
|
|
49
107
|
expect(repr).to eq('CASE a % 5 WHEN 0 THEN a ELSE b END')
|
|
@@ -64,6 +122,14 @@ describe Dentaku::PrintVisitor do
|
|
|
64
122
|
expect(repr).to eq('2017-12-24 23:59:59')
|
|
65
123
|
end
|
|
66
124
|
|
|
125
|
+
it 'handles XOR and NOT operators' do
|
|
126
|
+
expect(roundtrip('a XOR b')).to eq('a xor b')
|
|
127
|
+
expect(roundtrip('!a AND NOT b')).to eq('not a and not b')
|
|
128
|
+
expect(roundtrip('!(a AND b)')).to eq('not (a and b)')
|
|
129
|
+
expect(roundtrip('NOT a = b')).to eq('not a = b')
|
|
130
|
+
expect(roundtrip('NOT NOT a')).to eq('not (not a)')
|
|
131
|
+
end
|
|
132
|
+
|
|
67
133
|
it 'handles a percentage in a formula' do
|
|
68
134
|
repr = roundtrip('((3*4%) * 0.001)')
|
|
69
135
|
expect(repr).to eq('3 * 4% * 0.001')
|
data/spec/tokenizer_spec.rb
CHANGED
|
@@ -224,6 +224,25 @@ describe Dentaku::Tokenizer do
|
|
|
224
224
|
expect(tokens.map(&:value)).to eq(['a', :lbracket, 1, :rbracket])
|
|
225
225
|
end
|
|
226
226
|
|
|
227
|
+
it 'tokenizes member access after brackets and parentheses' do
|
|
228
|
+
tokens = tokenizer.tokenize('a[0].b.c + (d).e')
|
|
229
|
+
expect(tokens.map(&:category)).to eq(
|
|
230
|
+
%i(identifier access numeric access member_access member_access operator grouping identifier grouping member_access)
|
|
231
|
+
)
|
|
232
|
+
expect(tokens.map(&:value)).to eq(['a', :lbracket, 0, :rbracket, 'b', 'c', :add, :open, 'd', :close, 'e'])
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
it 'rejects whitespace before member access' do
|
|
236
|
+
expect { tokenizer.tokenize('a[0] .b') }.to raise_error(Dentaku::TokenizerError, /parse error at: '.b'/)
|
|
237
|
+
expect(tokenizer.tokenize('a[0].b + c').map(&:category)).to eq(%i(identifier access numeric access member_access operator identifier))
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it 'keeps dots inside identifiers' do
|
|
241
|
+
tokens = tokenizer.tokenize('a.b.c')
|
|
242
|
+
expect(tokens.map(&:category)).to eq(%i(identifier))
|
|
243
|
+
expect(tokens.map(&:value)).to eq(['a.b.c'])
|
|
244
|
+
end
|
|
245
|
+
|
|
227
246
|
it 'detects unbalanced parentheses' do
|
|
228
247
|
expect { tokenizer.tokenize('(5+3') }.to raise_error(Dentaku::TokenizerError, /too many opening parentheses/)
|
|
229
248
|
expect { tokenizer.tokenize(')') }.to raise_error(Dentaku::TokenizerError, /too many closing parentheses/)
|
data/spec/visitor_spec.rb
CHANGED
|
@@ -73,6 +73,11 @@ class TestVisitor
|
|
|
73
73
|
node.index.accept(self)
|
|
74
74
|
end
|
|
75
75
|
|
|
76
|
+
def visit_member_access(node)
|
|
77
|
+
mark_visited(node)
|
|
78
|
+
node.structure.accept(self)
|
|
79
|
+
end
|
|
80
|
+
|
|
76
81
|
def visit_literal(node)
|
|
77
82
|
mark_visited(node)
|
|
78
83
|
end
|
|
@@ -111,6 +116,7 @@ describe TestVisitor do
|
|
|
111
116
|
visit_nodes('(1 + 7) * (8 ^ 2) / - 3.0 - apples * 5%')
|
|
112
117
|
visit_nodes('1 < 2 and 3 <= 4 or 5 > 6 AND 7 >= 8 OR 9 != 10 and true')
|
|
113
118
|
visit_nodes('IF(a[0] = NULL, "five", \'seven\')')
|
|
119
|
+
visit_nodes('a[0].b')
|
|
114
120
|
visit_nodes('case (a % 5) when 0 then a else b end')
|
|
115
121
|
visit_nodes('0xCAFE & (0xDECAF << 3) | (0xBEEF >> 5)')
|
|
116
122
|
visit_nodes('2017-12-24 23:59:59')
|
|
@@ -119,6 +125,7 @@ describe TestVisitor do
|
|
|
119
125
|
visit_nodes('COUNT({1, 2, 3})')
|
|
120
126
|
visit_nodes('PLUCK(users, age)')
|
|
121
127
|
visit_nodes('XOR(false, false)')
|
|
128
|
+
visit_nodes('!a XOR NOT b')
|
|
122
129
|
visit_nodes('duration(1, day)')
|
|
123
130
|
visit_nodes('MAP(vals, val, val + 1)')
|
|
124
131
|
visit_nodes('FILTER(vals, val, val > 1)')
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dentaku
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.
|
|
4
|
+
version: 4.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Solomon White
|
|
@@ -117,6 +117,7 @@ files:
|
|
|
117
117
|
- lib/dentaku/ast/identifier.rb
|
|
118
118
|
- lib/dentaku/ast/literal.rb
|
|
119
119
|
- lib/dentaku/ast/logical.rb
|
|
120
|
+
- lib/dentaku/ast/member_access.rb
|
|
120
121
|
- lib/dentaku/ast/negation.rb
|
|
121
122
|
- lib/dentaku/ast/nil.rb
|
|
122
123
|
- lib/dentaku/ast/node.rb
|
|
@@ -129,17 +130,18 @@ files:
|
|
|
129
130
|
- lib/dentaku/dependency_resolver.rb
|
|
130
131
|
- lib/dentaku/exceptions.rb
|
|
131
132
|
- lib/dentaku/flat_hash.rb
|
|
133
|
+
- lib/dentaku/humanize_visitor.rb
|
|
132
134
|
- lib/dentaku/numeric_parser.rb
|
|
133
135
|
- lib/dentaku/parser.rb
|
|
134
136
|
- lib/dentaku/print_visitor.rb
|
|
135
137
|
- lib/dentaku/string_casing.rb
|
|
136
138
|
- lib/dentaku/token.rb
|
|
137
139
|
- lib/dentaku/token_matcher.rb
|
|
138
|
-
- lib/dentaku/token_matchers.rb
|
|
139
140
|
- lib/dentaku/token_scanner.rb
|
|
140
141
|
- lib/dentaku/tokenizer.rb
|
|
141
142
|
- lib/dentaku/version.rb
|
|
142
143
|
- lib/dentaku/visitor/infix.rb
|
|
144
|
+
- mise.toml
|
|
143
145
|
- spec/ast/abs_spec.rb
|
|
144
146
|
- spec/ast/addition_spec.rb
|
|
145
147
|
- spec/ast/all_spec.rb
|
|
@@ -157,6 +159,7 @@ files:
|
|
|
157
159
|
- spec/ast/intercept_spec.rb
|
|
158
160
|
- spec/ast/map_spec.rb
|
|
159
161
|
- spec/ast/max_spec.rb
|
|
162
|
+
- spec/ast/member_access_spec.rb
|
|
160
163
|
- spec/ast/min_spec.rb
|
|
161
164
|
- spec/ast/mul_spec.rb
|
|
162
165
|
- spec/ast/negation_spec.rb
|
|
@@ -173,6 +176,7 @@ files:
|
|
|
173
176
|
- spec/ast/switch_spec.rb
|
|
174
177
|
- spec/ast/xor_spec.rb
|
|
175
178
|
- spec/benchmark.rb
|
|
179
|
+
- spec/builtin_functions_spec.rb
|
|
176
180
|
- spec/bulk_expression_solver_spec.rb
|
|
177
181
|
- spec/calculator_spec.rb
|
|
178
182
|
- spec/case_sensitivity_spec.rb
|
|
@@ -181,6 +185,7 @@ files:
|
|
|
181
185
|
- spec/exceptions_spec.rb
|
|
182
186
|
- spec/external_function_spec.rb
|
|
183
187
|
- spec/gemspec_spec.rb
|
|
188
|
+
- spec/humanize_visitor_spec.rb
|
|
184
189
|
- spec/identifiers_spec.rb
|
|
185
190
|
- spec/numeric_parser_spec.rb
|
|
186
191
|
- spec/parser_spec.rb
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
module Dentaku
|
|
2
|
-
module TokenMatchers
|
|
3
|
-
def self.token_matchers(*symbols)
|
|
4
|
-
symbols.map { |s| matcher(s) }
|
|
5
|
-
end
|
|
6
|
-
|
|
7
|
-
def self.function_token_matchers(function_name, *symbols)
|
|
8
|
-
token_matchers(:open, *symbols, :close).unshift(
|
|
9
|
-
TokenMatcher.function(function_name)
|
|
10
|
-
)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def self.matcher(symbol)
|
|
14
|
-
@matchers ||= [
|
|
15
|
-
:datetime, :numeric, :string, :addsub, :subtract, :muldiv, :pow, :mod,
|
|
16
|
-
:comparator, :comp_gt, :comp_lt, :open, :close, :comma,
|
|
17
|
-
:non_close_plus, :non_group, :non_group_star, :arguments,
|
|
18
|
-
:logical, :combinator, :if, :round, :roundup, :rounddown, :not,
|
|
19
|
-
:anchored_minus
|
|
20
|
-
].each_with_object({}) do |name, matchers|
|
|
21
|
-
matchers[name] = TokenMatcher.public_send(name)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
@matchers.fetch(symbol) do
|
|
25
|
-
raise "Unknown token symbol #{ symbol }"
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|