dentaku 4.0.2 → 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 +87 -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 +105 -4
- data/lib/dentaku/ast/functions/duration.rb +3 -3
- data/lib/dentaku/ast/functions/rounddown.rb +8 -2
- data/lib/dentaku/ast/functions/roundup.rb +8 -2
- data/lib/dentaku/ast/functions/xor.rb +6 -6
- 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 +48 -7
- 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_parser.rb +7 -6
- data/lib/dentaku/parser.rb +15 -2
- data/lib/dentaku/print_visitor.rb +38 -2
- data/lib/dentaku/token_scanner.rb +37 -19
- data/lib/dentaku/tokenizer.rb +6 -2
- 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/ast/xor_spec.rb +16 -3
- data/spec/builtin_functions_spec.rb +26 -0
- data/spec/bulk_expression_solver_spec.rb +88 -0
- data/spec/calculator_spec.rb +181 -1
- data/spec/case_sensitivity_spec.rb +54 -0
- 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 +9 -3
- data/lib/dentaku/token_matchers.rb +0 -29
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'dentaku'
|
|
3
|
+
require 'dentaku/calculator'
|
|
4
|
+
|
|
5
|
+
describe Dentaku::Calculator do
|
|
6
|
+
let(:case_sensitive) { described_class.new(case_sensitive: true) }
|
|
7
|
+
let(:case_insensitive) { described_class.new }
|
|
8
|
+
|
|
9
|
+
it 'honours its own setting regardless of what other instances do' do
|
|
10
|
+
expect(case_sensitive.evaluate('Kiwi + 5', Kiwi: -2, kiwi: 2)).to eq(3)
|
|
11
|
+
expect(case_insensitive.evaluate('Kiwi + 5', Kiwi: -2, kiwi: 2)).to eq(7)
|
|
12
|
+
|
|
13
|
+
# same two settings, created and evaluated in the opposite order
|
|
14
|
+
other_sensitive = described_class.new(case_sensitive: true)
|
|
15
|
+
other_insensitive = described_class.new
|
|
16
|
+
expect(other_insensitive.evaluate('Kiwi + 5', Kiwi: -2, kiwi: 2)).to eq(7)
|
|
17
|
+
expect(other_sensitive.evaluate('Kiwi + 5', Kiwi: -2, kiwi: 2)).to eq(3)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Identifier casing used to be applied twice: once by the identifier scanner,
|
|
21
|
+
# reading a class-level flag on TokenScanner that every calculator in the
|
|
22
|
+
# process shared, and again by AST::Identifier using the calculator's own
|
|
23
|
+
# flag. The shared flag meant one instance could overwrite another's mode
|
|
24
|
+
# while the first was still tokenizing -- in practice, whenever two threads
|
|
25
|
+
# evaluated at the same time, identifiers silently folded to the other
|
|
26
|
+
# instance's casing.
|
|
27
|
+
#
|
|
28
|
+
# Reproduce that interleaving deterministically: once this calculator has
|
|
29
|
+
# fetched its scanners (which is when the mode used to be recorded), let a
|
|
30
|
+
# second, case-insensitive calculator tokenize before the first one scans its
|
|
31
|
+
# identifiers. The first calculator must be unaffected.
|
|
32
|
+
it 'is unaffected by another instance tokenizing while it scans' do
|
|
33
|
+
interloped = false
|
|
34
|
+
allow(Dentaku::TokenScanner).to receive(:scanners).and_wrap_original do |original, *args|
|
|
35
|
+
original.call(*args).tap do
|
|
36
|
+
next if interloped
|
|
37
|
+
|
|
38
|
+
interloped = true
|
|
39
|
+
case_insensitive.evaluate('Zz', Zz: 1)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
expect(case_sensitive.evaluate('Kiwi', Kiwi: -2, kiwi: 2)).to eq(-2)
|
|
44
|
+
expect(case_insensitive.evaluate('Kiwi', Kiwi: -2, kiwi: 2)).to eq(2)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# casing is applied in exactly one place, AST::Identifier, which is handed
|
|
48
|
+
# the mode of the calculator that owns the parse -- so the token stream keeps
|
|
49
|
+
# the source casing of every identifier
|
|
50
|
+
it 'leaves identifier casing to the parser' do
|
|
51
|
+
tokens = Dentaku::Tokenizer.new.tokenize('Kiwi + 5')
|
|
52
|
+
expect(tokens.first.value).to eq('Kiwi')
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -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
|
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.0
|
|
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,13 +176,16 @@ 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
|
|
182
|
+
- spec/case_sensitivity_spec.rb
|
|
178
183
|
- spec/dentaku_spec.rb
|
|
179
184
|
- spec/dependency_resolver_spec.rb
|
|
180
185
|
- spec/exceptions_spec.rb
|
|
181
186
|
- spec/external_function_spec.rb
|
|
182
187
|
- spec/gemspec_spec.rb
|
|
188
|
+
- spec/humanize_visitor_spec.rb
|
|
183
189
|
- spec/identifiers_spec.rb
|
|
184
190
|
- spec/numeric_parser_spec.rb
|
|
185
191
|
- spec/parser_spec.rb
|
|
@@ -215,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
215
221
|
- !ruby/object:Gem::Version
|
|
216
222
|
version: '0'
|
|
217
223
|
requirements: []
|
|
218
|
-
rubygems_version:
|
|
224
|
+
rubygems_version: 4.0.20
|
|
219
225
|
specification_version: 4
|
|
220
226
|
summary: A formula language parser and evaluator
|
|
221
227
|
test_files: []
|
|
@@ -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
|