dentaku 4.1.0 → 4.2.1
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 +75 -0
- data/README.md +176 -11
- data/lib/dentaku/ast/access.rb +14 -11
- data/lib/dentaku/ast/arithmetic.rb +17 -1
- data/lib/dentaku/ast/array.rb +2 -0
- data/lib/dentaku/ast/bitwise.rb +16 -1
- data/lib/dentaku/ast/combinators.rb +83 -0
- data/lib/dentaku/ast/functions/directed_rounding.rb +37 -0
- data/lib/dentaku/ast/functions/mul.rb +2 -1
- data/lib/dentaku/ast/functions/rounddown.rb +2 -4
- data/lib/dentaku/ast/functions/roundup.rb +2 -4
- 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/numeric_limits.rb +67 -0
- data/lib/dentaku/numeric_parser.rb +6 -3
- 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/arithmetic_spec.rb +1 -1
- 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 +175 -1
- data/spec/humanize_visitor_spec.rb +217 -0
- data/spec/numeric_limits_spec.rb +59 -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 +10 -2
- data/lib/dentaku/token_matchers.rb +0 -29
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'dentaku'
|
|
3
|
+
require 'dentaku/humanize_visitor'
|
|
4
|
+
|
|
5
|
+
describe Dentaku::HumanizeVisitor do
|
|
6
|
+
let(:calculator) { Dentaku::Calculator.new }
|
|
7
|
+
|
|
8
|
+
def humanize(expression, values = {})
|
|
9
|
+
described_class.new(calculator.ast(expression), values).to_s
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'verbalizes comparison operators' do
|
|
13
|
+
expect(humanize('a >= 5')).to eq('a is greater than or equal to 5')
|
|
14
|
+
expect(humanize('a <= 5')).to eq('a is less than or equal to 5')
|
|
15
|
+
expect(humanize('a > 5')).to eq('a is greater than 5')
|
|
16
|
+
expect(humanize('a < 5')).to eq('a is less than 5')
|
|
17
|
+
expect(humanize('a = 5')).to eq('a equals 5')
|
|
18
|
+
expect(humanize('a != 5')).to eq('a does not equal 5')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'verbalizes arithmetic operators' do
|
|
22
|
+
expect(humanize('1 + 2')).to eq('1 plus 2')
|
|
23
|
+
expect(humanize('3 - 1')).to eq('3 minus 1')
|
|
24
|
+
expect(humanize('2 * 3')).to eq('2 times 3')
|
|
25
|
+
expect(humanize('10 / 2')).to eq('10 divided by 2')
|
|
26
|
+
expect(humanize('2 ^ 3')).to eq('2 to the power of 3')
|
|
27
|
+
expect(humanize('7 % 3')).to eq('7 modulo 3')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'verbalizes bitwise operators' do
|
|
31
|
+
expect(humanize('1 & 2')).to eq('1 bitwise and 2')
|
|
32
|
+
expect(humanize('1 | 2')).to eq('1 bitwise or 2')
|
|
33
|
+
expect(humanize('1 << 2')).to eq('1 shifted left by 2')
|
|
34
|
+
expect(humanize('1 >> 2')).to eq('1 shifted right by 2')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'verbalizes logical combinators' do
|
|
38
|
+
expect(humanize('a >= 5 and a <= 10'))
|
|
39
|
+
.to eq('a is greater than or equal to 5 and a is less than or equal to 10')
|
|
40
|
+
expect(humanize('a < 0 or a > 100')).to eq('a is less than 0 or a is greater than 100')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'verbalizes XOR and NOT operators' do
|
|
44
|
+
expect(humanize('a XOR b')).to eq('a exclusive-or b')
|
|
45
|
+
expect(humanize('NOT a > 5')).to eq('not a is greater than 5')
|
|
46
|
+
expect(humanize('!(a OR b)')).to eq('not (a or b)')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'verbalizes negation' do
|
|
50
|
+
expect(humanize('-a')).to eq('negative a')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'verbalizes nil literal' do
|
|
54
|
+
expect(humanize('a = NULL')).to eq('a equals null')
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'substitutes identifiers with provided values' do
|
|
58
|
+
expect(humanize('days >= min and days <= max', min: 5, max: 20))
|
|
59
|
+
.to eq('days is greater than or equal to 5 and days is less than or equal to 20')
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'quotes string values in substitution' do
|
|
63
|
+
expect(humanize('name = expected', expected: 'Buk')).to eq('name equals "Buk"')
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'preserves identifiers with no substitution value' do
|
|
67
|
+
expect(humanize('a + b', a: 1)).to eq('1 plus b')
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'verbalizes IF' do
|
|
71
|
+
expect(humanize('IF(a > 0, "yes", "no")')).to eq('if a is greater than 0 then "yes" else "no"')
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it 'verbalizes AND function' do
|
|
75
|
+
expect(humanize('AND(a > 0, b > 0)')).to eq('a is greater than 0 and b is greater than 0')
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it 'verbalizes OR function' do
|
|
79
|
+
expect(humanize('OR(a > 0, b > 0)')).to eq('a is greater than 0 or b is greater than 0')
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it 'verbalizes XOR' do
|
|
83
|
+
expect(humanize('XOR(a, b)')).to eq('a exclusive-or b')
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'verbalizes NOT' do
|
|
87
|
+
expect(humanize('NOT(a)')).to eq('not a')
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it 'verbalizes SWITCH with default' do
|
|
91
|
+
expect(humanize('SWITCH(x, 1, "one", 2, "two", "other")'))
|
|
92
|
+
.to eq('x switch: when 1 use "one"; when 2 use "two"; otherwise "other"')
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'verbalizes SWITCH without default' do
|
|
96
|
+
expect(humanize('SWITCH(x, 1, "one", 2, "two")'))
|
|
97
|
+
.to eq('x switch: when 1 use "one"; when 2 use "two"')
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it 'verbalizes MIN / MAX / SUM / AVG / COUNT' do
|
|
101
|
+
expect(humanize('MIN(a, b, c)')).to eq('minimum of a, b, c')
|
|
102
|
+
expect(humanize('MAX(a, b)')).to eq('maximum of a, b')
|
|
103
|
+
expect(humanize('SUM(a, b, c)')).to eq('sum of a, b, c')
|
|
104
|
+
expect(humanize('AVG(a, b)')).to eq('average of a, b')
|
|
105
|
+
expect(humanize('COUNT(a, b, c)')).to eq('count of a, b, c')
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it 'verbalizes ABS' do
|
|
109
|
+
expect(humanize('ABS(x)')).to eq('absolute value of x')
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it 'verbalizes ROUND / ROUNDUP / ROUNDDOWN' do
|
|
113
|
+
expect(humanize('ROUND(x, 2)')).to eq('x rounded to 2 decimal places')
|
|
114
|
+
expect(humanize('ROUNDUP(x, 2)')).to eq('x rounded up to 2 decimal places')
|
|
115
|
+
expect(humanize('ROUNDDOWN(x, 2)')).to eq('x rounded down to 2 decimal places')
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it 'verbalizes INTERCEPT' do
|
|
119
|
+
expect(humanize('INTERCEPT(xs, ys)')).to eq('linear intercept of xs and ys')
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it 'verbalizes LEFT / RIGHT' do
|
|
123
|
+
expect(humanize('LEFT(s, 3)')).to eq('first 3 characters of s')
|
|
124
|
+
expect(humanize('RIGHT(s, 3)')).to eq('last 3 characters of s')
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it 'verbalizes MID' do
|
|
128
|
+
expect(humanize('MID(s, 2, 4)')).to eq('4 characters of s starting at position 2')
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it 'verbalizes LEN' do
|
|
132
|
+
expect(humanize('LEN(s)')).to eq('length of s')
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'verbalizes FIND' do
|
|
136
|
+
expect(humanize('FIND(needle, haystack)')).to eq('position of needle in haystack')
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it 'verbalizes SUBSTITUTE' do
|
|
140
|
+
expect(humanize('SUBSTITUTE(text, "foo", "bar")')).to eq('text with "foo" replaced by "bar"')
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it 'verbalizes CONCAT' do
|
|
144
|
+
expect(humanize('CONCAT(a, b, c)')).to eq('a, b and c joined')
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
it 'verbalizes CONTAINS' do
|
|
148
|
+
expect(humanize('CONTAINS(needle, haystack)')).to eq('haystack contains needle')
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it 'verbalizes CASE' do
|
|
152
|
+
expr = 'CASE x WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "other" END'
|
|
153
|
+
expect(humanize(expr)).to eq('x case: when 1 then "one" when 2 then "two"; otherwise "other"')
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it 'verbalizes MAP' do
|
|
157
|
+
expect(humanize('MAP(items, x, x * 2)')).to eq('for each x in items: x times 2')
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it 'verbalizes FILTER' do
|
|
161
|
+
expect(humanize('FILTER(items, x, x > 0)')).to eq('filter x in items where x is greater than 0')
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it 'verbalizes ALL' do
|
|
165
|
+
expect(humanize('ALL(items, x, x > 0)')).to eq('all x in items satisfy x is greater than 0')
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it 'verbalizes ANY' do
|
|
169
|
+
expect(humanize('ANY(items, x, x > 0)')).to eq('any x in items satisfies x is greater than 0')
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it 'verbalizes PLUCK' do
|
|
173
|
+
expect(humanize('PLUCK(items, name)')).to eq('values of name from items')
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it 'verbalizes unary Math functions' do
|
|
177
|
+
expect(humanize('SIN(x)')).to eq('sine of x')
|
|
178
|
+
expect(humanize('COS(x)')).to eq('cosine of x')
|
|
179
|
+
expect(humanize('TAN(x)')).to eq('tangent of x')
|
|
180
|
+
expect(humanize('SQRT(x)')).to eq('square root of x')
|
|
181
|
+
expect(humanize('LOG(x)')).to eq('logarithm of x')
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
it 'verbalizes LOG with base' do
|
|
185
|
+
expect(humanize('LOG(x, 10)')).to eq('logarithm of x in base 10')
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it 'verbalizes binary Math functions' do
|
|
189
|
+
expect(humanize('ATAN2(y, x)')).to eq('arctangent of y and x')
|
|
190
|
+
expect(humanize('HYPOT(a, b)')).to eq('hypotenuse of a and b')
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
it 'keeps member access in operands' do
|
|
194
|
+
expect(humanize('users[0].age >= 18')).to eq('users[0].age is greater than or equal to 18')
|
|
195
|
+
expect(humanize('(a + b).c = 1')).to eq('(a plus b).c equals 1')
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'groups function calls before member access' do
|
|
199
|
+
expect(humanize('IF(a, b, c).d = 1')).to eq('(if a then b else c).d equals 1')
|
|
200
|
+
expect(humanize('FILTER(users, u, u.age > 20).name'))
|
|
201
|
+
.to eq('(filter u in users where u.age is greater than 20).name')
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it 'groups function calls and operations before an index' do
|
|
205
|
+
expect(humanize('FILTER(users, u, u.age > 20)[0]'))
|
|
206
|
+
.to eq('(filter u in users where u.age is greater than 20)[0]')
|
|
207
|
+
expect(humanize('FILTER(users, u, u.age > 20)[0].name'))
|
|
208
|
+
.to eq('(filter u in users where u.age is greater than 20)[0].name')
|
|
209
|
+
expect(humanize('(a + b)[0] = 1')).to eq('(a plus b)[0] equals 1')
|
|
210
|
+
expect(humanize('users[0]')).to eq('users[0]')
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
it 'is exposed via Calculator#humanize' do
|
|
214
|
+
result = calculator.humanize('days >= min and days <= max', min: 5, max: 20)
|
|
215
|
+
expect(result).to eq('days is greater than or equal to 5 and days is less than or equal to 20')
|
|
216
|
+
end
|
|
217
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'dentaku'
|
|
3
|
+
|
|
4
|
+
# Ruby does bignum arithmetic in single C calls that Timeout can't interrupt,
|
|
5
|
+
# so one short formula could otherwise tie up a thread for hours.
|
|
6
|
+
describe 'numeric size limits' do
|
|
7
|
+
let(:calculator) { Dentaku::Calculator.new }
|
|
8
|
+
let(:max) { Dentaku::NumericLimits::MAX_DIGITS }
|
|
9
|
+
|
|
10
|
+
def rejects(formula, **context)
|
|
11
|
+
expect { calculator.evaluate!(formula, **context) }
|
|
12
|
+
.to raise_error(Dentaku::ArgumentError, /more than #{max} digits/)
|
|
13
|
+
expect(calculator.evaluate(formula, **context)).to be_nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'still computes large numbers within the limit' do
|
|
17
|
+
expect(calculator.evaluate!('2 ^ 33000')).to eq(2**33000)
|
|
18
|
+
expect(calculator.evaluate!('7 ^ -2')).to eq(Rational(1, 49))
|
|
19
|
+
expect(calculator.evaluate!('1 << 33000')).to eq(1 << 33000)
|
|
20
|
+
expect(calculator.evaluate!('ROUND(1e9000)')).to eq(10**9000)
|
|
21
|
+
expect(calculator.evaluate!('1.5 ^ 0.5')).to be_within(1e-12).of(Math.sqrt(1.5))
|
|
22
|
+
expect(calculator.evaluate!('0 ^ 100000000')).to eq(0)
|
|
23
|
+
expect(calculator.evaluate!('1 ^ 100000000')).to eq(1)
|
|
24
|
+
expect(calculator.evaluate!('-1 ^ 100000001')).to eq(-1)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'rejects a power whose result would be too long' do
|
|
28
|
+
rejects('2 ^ 40000')
|
|
29
|
+
rejects('7 ^ 100000000')
|
|
30
|
+
rejects('7 ^ -100000000')
|
|
31
|
+
rejects('7 ^ 1e8')
|
|
32
|
+
rejects('7.5 ^ 100000000')
|
|
33
|
+
rejects('0xffffffff ^ 10000000')
|
|
34
|
+
rejects('x ^ y', x: 7, y: 100_000_000)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'rejects a left shift whose result would be too long' do
|
|
38
|
+
rejects('1 << 40000')
|
|
39
|
+
rejects('1 << 10000000000')
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'rejects arithmetic results that grow past the limit' do
|
|
43
|
+
rejects('x * x', x: 10**6000)
|
|
44
|
+
rejects('MUL(7 ^ 9000, 7 ^ 9000)')
|
|
45
|
+
rejects('x | 1', x: 10**20000)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'rejects oversized numbers where they are used' do
|
|
49
|
+
rejects('ROUND(1e20000)')
|
|
50
|
+
rejects('ROUND(1e100000000, -2)')
|
|
51
|
+
rejects('ROUNDUP(1e100000000)')
|
|
52
|
+
rejects('ROUNDDOWN(1e100000000)')
|
|
53
|
+
rejects('ROUND(1.5, 1e100000000)')
|
|
54
|
+
rejects('LEFT("abc", 1e100000000)')
|
|
55
|
+
rejects('MID("abc", 1e100000000, 1)')
|
|
56
|
+
rejects('1e100000000 + 1')
|
|
57
|
+
rejects('x + 1', x: '1e100000000')
|
|
58
|
+
end
|
|
59
|
+
end
|
data/spec/parser_spec.rb
CHANGED
|
@@ -69,6 +69,11 @@ describe Dentaku::Parser do
|
|
|
69
69
|
expect(node.value("a" => [1, 2, 3])).to eq(2)
|
|
70
70
|
end
|
|
71
71
|
|
|
72
|
+
it 'evaluates access directly on array literal' do
|
|
73
|
+
expect(parse('{10, 20, 30}[0]').value).to eq(10)
|
|
74
|
+
expect(parse('{10, 20, 30}[-1]').value).to eq(30)
|
|
75
|
+
end
|
|
76
|
+
|
|
72
77
|
it 'evaluates boolean expressions' do
|
|
73
78
|
node = parse('true AND false')
|
|
74
79
|
expect(node.value).to eq(false)
|
|
@@ -107,6 +112,9 @@ describe Dentaku::Parser do
|
|
|
107
112
|
|
|
108
113
|
node = parse('{1, 2, 3} - {2,3}')
|
|
109
114
|
expect(node.value).to eq([1])
|
|
115
|
+
|
|
116
|
+
node = parse('{{1, 2}, {3, 4}}')
|
|
117
|
+
expect(node.value).to eq([[1, 2], [3, 4]])
|
|
110
118
|
end
|
|
111
119
|
|
|
112
120
|
context 'invalid expression' do
|
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.1
|
|
4
|
+
version: 4.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Solomon White
|
|
@@ -92,6 +92,7 @@ files:
|
|
|
92
92
|
- lib/dentaku/ast/functions/any.rb
|
|
93
93
|
- lib/dentaku/ast/functions/avg.rb
|
|
94
94
|
- lib/dentaku/ast/functions/count.rb
|
|
95
|
+
- lib/dentaku/ast/functions/directed_rounding.rb
|
|
95
96
|
- lib/dentaku/ast/functions/duration.rb
|
|
96
97
|
- lib/dentaku/ast/functions/enum.rb
|
|
97
98
|
- lib/dentaku/ast/functions/filter.rb
|
|
@@ -117,6 +118,7 @@ files:
|
|
|
117
118
|
- lib/dentaku/ast/identifier.rb
|
|
118
119
|
- lib/dentaku/ast/literal.rb
|
|
119
120
|
- lib/dentaku/ast/logical.rb
|
|
121
|
+
- lib/dentaku/ast/member_access.rb
|
|
120
122
|
- lib/dentaku/ast/negation.rb
|
|
121
123
|
- lib/dentaku/ast/nil.rb
|
|
122
124
|
- lib/dentaku/ast/node.rb
|
|
@@ -129,17 +131,19 @@ files:
|
|
|
129
131
|
- lib/dentaku/dependency_resolver.rb
|
|
130
132
|
- lib/dentaku/exceptions.rb
|
|
131
133
|
- lib/dentaku/flat_hash.rb
|
|
134
|
+
- lib/dentaku/humanize_visitor.rb
|
|
135
|
+
- lib/dentaku/numeric_limits.rb
|
|
132
136
|
- lib/dentaku/numeric_parser.rb
|
|
133
137
|
- lib/dentaku/parser.rb
|
|
134
138
|
- lib/dentaku/print_visitor.rb
|
|
135
139
|
- lib/dentaku/string_casing.rb
|
|
136
140
|
- lib/dentaku/token.rb
|
|
137
141
|
- lib/dentaku/token_matcher.rb
|
|
138
|
-
- lib/dentaku/token_matchers.rb
|
|
139
142
|
- lib/dentaku/token_scanner.rb
|
|
140
143
|
- lib/dentaku/tokenizer.rb
|
|
141
144
|
- lib/dentaku/version.rb
|
|
142
145
|
- lib/dentaku/visitor/infix.rb
|
|
146
|
+
- mise.toml
|
|
143
147
|
- spec/ast/abs_spec.rb
|
|
144
148
|
- spec/ast/addition_spec.rb
|
|
145
149
|
- spec/ast/all_spec.rb
|
|
@@ -157,6 +161,7 @@ files:
|
|
|
157
161
|
- spec/ast/intercept_spec.rb
|
|
158
162
|
- spec/ast/map_spec.rb
|
|
159
163
|
- spec/ast/max_spec.rb
|
|
164
|
+
- spec/ast/member_access_spec.rb
|
|
160
165
|
- spec/ast/min_spec.rb
|
|
161
166
|
- spec/ast/mul_spec.rb
|
|
162
167
|
- spec/ast/negation_spec.rb
|
|
@@ -173,6 +178,7 @@ files:
|
|
|
173
178
|
- spec/ast/switch_spec.rb
|
|
174
179
|
- spec/ast/xor_spec.rb
|
|
175
180
|
- spec/benchmark.rb
|
|
181
|
+
- spec/builtin_functions_spec.rb
|
|
176
182
|
- spec/bulk_expression_solver_spec.rb
|
|
177
183
|
- spec/calculator_spec.rb
|
|
178
184
|
- spec/case_sensitivity_spec.rb
|
|
@@ -181,7 +187,9 @@ files:
|
|
|
181
187
|
- spec/exceptions_spec.rb
|
|
182
188
|
- spec/external_function_spec.rb
|
|
183
189
|
- spec/gemspec_spec.rb
|
|
190
|
+
- spec/humanize_visitor_spec.rb
|
|
184
191
|
- spec/identifiers_spec.rb
|
|
192
|
+
- spec/numeric_limits_spec.rb
|
|
185
193
|
- spec/numeric_parser_spec.rb
|
|
186
194
|
- spec/parser_spec.rb
|
|
187
195
|
- spec/print_visitor_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
|