dentaku 3.3.0 → 3.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -10
  3. data/.travis.yml +3 -6
  4. data/CHANGELOG.md +38 -1
  5. data/README.md +2 -2
  6. data/dentaku.gemspec +0 -2
  7. data/lib/dentaku.rb +14 -6
  8. data/lib/dentaku/ast.rb +5 -0
  9. data/lib/dentaku/ast/access.rb +15 -1
  10. data/lib/dentaku/ast/arithmetic.rb +29 -6
  11. data/lib/dentaku/ast/array.rb +15 -1
  12. data/lib/dentaku/ast/case.rb +13 -3
  13. data/lib/dentaku/ast/case/case_conditional.rb +13 -2
  14. data/lib/dentaku/ast/case/case_else.rb +12 -4
  15. data/lib/dentaku/ast/case/case_switch_variable.rb +8 -0
  16. data/lib/dentaku/ast/case/case_then.rb +12 -4
  17. data/lib/dentaku/ast/case/case_when.rb +12 -4
  18. data/lib/dentaku/ast/function.rb +11 -2
  19. data/lib/dentaku/ast/function_registry.rb +21 -0
  20. data/lib/dentaku/ast/functions/all.rb +36 -0
  21. data/lib/dentaku/ast/functions/any.rb +36 -0
  22. data/lib/dentaku/ast/functions/avg.rb +2 -2
  23. data/lib/dentaku/ast/functions/count.rb +8 -0
  24. data/lib/dentaku/ast/functions/duration.rb +51 -0
  25. data/lib/dentaku/ast/functions/if.rb +15 -2
  26. data/lib/dentaku/ast/functions/map.rb +36 -0
  27. data/lib/dentaku/ast/functions/mul.rb +3 -2
  28. data/lib/dentaku/ast/functions/pluck.rb +29 -0
  29. data/lib/dentaku/ast/functions/round.rb +1 -1
  30. data/lib/dentaku/ast/functions/rounddown.rb +1 -1
  31. data/lib/dentaku/ast/functions/roundup.rb +1 -1
  32. data/lib/dentaku/ast/functions/ruby_math.rb +47 -3
  33. data/lib/dentaku/ast/functions/string_functions.rb +68 -4
  34. data/lib/dentaku/ast/functions/sum.rb +3 -2
  35. data/lib/dentaku/ast/grouping.rb +3 -1
  36. data/lib/dentaku/ast/identifier.rb +5 -1
  37. data/lib/dentaku/ast/negation.rb +3 -1
  38. data/lib/dentaku/ast/node.rb +4 -0
  39. data/lib/dentaku/ast/operation.rb +8 -0
  40. data/lib/dentaku/bulk_expression_solver.rb +34 -25
  41. data/lib/dentaku/calculator.rb +19 -6
  42. data/lib/dentaku/date_arithmetic.rb +45 -0
  43. data/lib/dentaku/exceptions.rb +4 -4
  44. data/lib/dentaku/flat_hash.rb +9 -2
  45. data/lib/dentaku/parser.rb +31 -14
  46. data/lib/dentaku/token_matcher.rb +1 -1
  47. data/lib/dentaku/token_scanner.rb +1 -1
  48. data/lib/dentaku/tokenizer.rb +7 -2
  49. data/lib/dentaku/version.rb +1 -1
  50. data/spec/ast/addition_spec.rb +7 -1
  51. data/spec/ast/and_function_spec.rb +6 -6
  52. data/spec/ast/and_spec.rb +1 -1
  53. data/spec/ast/arithmetic_spec.rb +57 -29
  54. data/spec/ast/avg_spec.rb +9 -5
  55. data/spec/ast/count_spec.rb +7 -7
  56. data/spec/ast/division_spec.rb +7 -1
  57. data/spec/ast/function_spec.rb +9 -9
  58. data/spec/ast/max_spec.rb +3 -3
  59. data/spec/ast/min_spec.rb +3 -3
  60. data/spec/ast/mul_spec.rb +10 -6
  61. data/spec/ast/negation_spec.rb +48 -0
  62. data/spec/ast/node_spec.rb +11 -8
  63. data/spec/ast/numeric_spec.rb +1 -1
  64. data/spec/ast/or_spec.rb +6 -6
  65. data/spec/ast/round_spec.rb +14 -4
  66. data/spec/ast/rounddown_spec.rb +14 -4
  67. data/spec/ast/roundup_spec.rb +14 -4
  68. data/spec/ast/string_functions_spec.rb +35 -0
  69. data/spec/ast/sum_spec.rb +10 -6
  70. data/spec/ast/switch_spec.rb +5 -5
  71. data/spec/bulk_expression_solver_spec.rb +18 -1
  72. data/spec/calculator_spec.rb +173 -28
  73. data/spec/dentaku_spec.rb +18 -5
  74. data/spec/external_function_spec.rb +29 -5
  75. data/spec/parser_spec.rb +85 -123
  76. data/spec/spec_helper.rb +6 -4
  77. data/spec/token_matcher_spec.rb +8 -8
  78. data/spec/token_scanner_spec.rb +4 -4
  79. data/spec/tokenizer_spec.rb +32 -13
  80. metadata +11 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7a7a3fa41f933a1f88a76e736c32e71dcf87f391a7a4ba09d37b2abad8269b2
4
- data.tar.gz: 3045bd6715d486669c2fe015165a2bef31928fccdf4e6a07f59a67257b9f4867
3
+ metadata.gz: 1eb86933b899627333d93e993cd2d093a11f4ca54b2e0f6668c3580a8f133975
4
+ data.tar.gz: 004bd53fa5c3c6815bafdbf98788de96757b688a6ba217b7f8e96be31a4f7d5d
5
5
  SHA512:
6
- metadata.gz: 2f8b4a5d727ec64a0308a57570fc51ff09864174fd0669956acb6b26b0d6b5694d6a0ec563eb107b577db79dd37ddd383fc36ec47bd17ebb11008981e2d5c729
7
- data.tar.gz: 1cb16ed5d1bbad1c896d3907761206178093531ef02fe27eabaa19e1b5187f66f1d9d6586d410107133c08c520596dd7b9a6d5b47f23decb47e80ed2cbdd0e28
6
+ metadata.gz: 32f0e44638fc3738a3df5ba6a04abdb4ee66177049242260e26fea1ad6844a66a35e65ca5a27448f118838e6ccecd133f46650e9fbff5d878bbc14b1e7d952e8
7
+ data.tar.gz: 8081bcf51e30daa33dbaba3f8796951151c1bfd2e3975f63e5291aac7e04e09f2fc2f00aa956cfdad99caf1b8a8fb4f04b2c0789d8909b0982372e75dc7f7fc0
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.4
2
+ TargetRubyVersion: 2.6
3
3
  # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
4
4
  # to ignore them, so only the ones explicitly set in this file are enabled.
5
5
  DisabledByDefault: true
@@ -8,11 +8,6 @@ AllCops:
8
8
  Style/AndOr:
9
9
  Enabled: true
10
10
 
11
- # Do not use braces for hash literals when they are the last argument of a
12
- # method call.
13
- Style/BracesAroundHashParameters:
14
- Enabled: true
15
-
16
11
  # Align `when` with `case`.
17
12
  Layout/CaseIndentation:
18
13
  Enabled: true
@@ -93,11 +88,11 @@ Style/StringLiterals:
93
88
  EnforcedStyle: double_quotes
94
89
 
95
90
  # Detect hard tabs, no hard tabs.
96
- Layout/Tab:
91
+ Layout/IndentationStyle:
97
92
  Enabled: true
98
93
 
99
94
  # Blank lines should not have any spaces.
100
- Layout/TrailingBlankLines:
95
+ Layout/TrailingEmptyLines:
101
96
  Enabled: true
102
97
 
103
98
  # No trailing whitespace.
@@ -105,12 +100,12 @@ Layout/TrailingWhitespace:
105
100
  Enabled: true
106
101
 
107
102
  # Use quotes for string literals when they are enough.
108
- Style/UnneededPercentQ:
103
+ Style/RedundantPercentQ:
109
104
  Enabled: true
110
105
 
111
106
  # Align `end` with the matching keyword or starting expression except for
112
107
  # assignments, where it should be aligned with the LHS.
113
- Lint/EndAlignment:
108
+ Layout/EndAlignment:
114
109
  Enabled: true
115
110
  EnforcedStyleAlignWith: variable
116
111
 
@@ -1,12 +1,9 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  rvm:
4
- - 2.0.0-p648
5
- - 2.1.10
6
- - 2.2.9
7
- - 2.3.6
8
- - 2.4.3
9
- - 2.5.0
4
+ - 2.5.7
5
+ - 2.6.5
6
+ - 2.7.0
10
7
  before_install:
11
8
  - gem update bundler
12
9
  - gem update --system
@@ -1,5 +1,38 @@
1
1
  # Change Log
2
2
 
3
+ ## [v3.4.0] 2020-12-07
4
+ - allow access to intermediate values of flattened hashes
5
+ - catch invalid array syntax in the parse phase
6
+ - drop support for Ruby < 2.5, add support for Ruby 2.7
7
+ - add support for subtracting date literals
8
+ - improve error handling
9
+ - improve math function implementation
10
+ - add caching for calculated variable values
11
+ - allow custom unbound variable handling block at Dentaku module level
12
+ - add enum functions `ANY`, `ALL`, `MAP` and `PLUCK`
13
+ - allow self-referential formulas in bulk expression solver
14
+ - misc internal fixes and enhancements
15
+ ## [v3.3.4] 2019-11-21
16
+ - bugfix release
17
+
18
+ ## [v3.3.3] 2019-11-20
19
+ - date / duration addition and subtraction
20
+ - validate arity for custom functions with variable arity
21
+ - make AST serializable with Marshal.dump
22
+ - performance optimization for arithmetic node validation
23
+ - support lazy evaluation for expensive values
24
+ - short-circuit IF function
25
+ - better error when empty string is used in arithmetic operation
26
+
27
+ ## [v3.3.2] 2019-06-10
28
+ - add ability to pre-load AST cache
29
+ - fix negation node bug
30
+
31
+ ## [v3.3.1] 2019-03-26
32
+ - better errors for parse failures and exceptions in internal functions
33
+ - fix Ruby 2.6.0 deprecation warnings
34
+ - fix issue with functions in nested case statements
35
+
3
36
  ## [v3.3.0] 2018-12-04
4
37
  - add array literal syntax
5
38
  - return correct type from string function AST nodes
@@ -166,7 +199,11 @@
166
199
  ## [v0.1.0] 2012-01-20
167
200
  - initial release
168
201
 
169
- [HEAD]: https://github.com/rubysolo/dentaku/compare/v3.3.0...HEAD
202
+ [v3.4.0]: https://github.com/rubysolo/dentaku/compare/v3.3.4...v3.4.0
203
+ [v3.3.4]: https://github.com/rubysolo/dentaku/compare/v3.3.3...v3.3.4
204
+ [v3.3.3]: https://github.com/rubysolo/dentaku/compare/v3.3.2...v3.3.3
205
+ [v3.3.2]: https://github.com/rubysolo/dentaku/compare/v3.3.1...v3.3.2
206
+ [v3.3.1]: https://github.com/rubysolo/dentaku/compare/v3.3.0...v3.3.1
170
207
  [v3.3.0]: https://github.com/rubysolo/dentaku/compare/v3.2.1...v3.3.0
171
208
  [v3.2.1]: https://github.com/rubysolo/dentaku/compare/v3.2.0...v3.2.1
172
209
  [v3.2.0]: https://github.com/rubysolo/dentaku/compare/v3.1.0...v3.2.0
data/README.md CHANGED
@@ -284,7 +284,7 @@ using Calculator#add_functions.
284
284
  FUNCTION ALIASES
285
285
  ----------------
286
286
 
287
- Every function can be aliased by synonyms. For example, it can be useful if
287
+ Every function can be aliased by synonyms. For example, it can be useful if
288
288
  your application is multilingual.
289
289
 
290
290
  ```ruby
@@ -321,7 +321,7 @@ LICENSE
321
321
 
322
322
  (The MIT License)
323
323
 
324
- Copyright © 2012-2018 Solomon White
324
+ Copyright © 2012-2019 Solomon White
325
325
 
326
326
  Permission is hereby granted, free of charge, to any person obtaining a copy of
327
327
  this software and associated documentation files (the ‘Software’), to deal in
@@ -14,8 +14,6 @@ Gem::Specification.new do |s|
14
14
  Dentaku is a parser and evaluator for mathematical formulas
15
15
  DESC
16
16
 
17
- s.rubyforge_project = "dentaku"
18
-
19
17
  s.add_development_dependency('codecov')
20
18
  s.add_development_dependency('pry')
21
19
  s.add_development_dependency('pry-byebug')
@@ -5,19 +5,21 @@ require "dentaku/version"
5
5
  module Dentaku
6
6
  @enable_ast_caching = false
7
7
  @enable_dependency_order_caching = false
8
+ @enable_identifier_caching = false
8
9
  @aliases = {}
9
10
 
10
- def self.evaluate(expression, data = {})
11
- calculator.evaluate(expression, data)
11
+ def self.evaluate(expression, data = {}, &block)
12
+ calculator.evaluate(expression, data, &block)
12
13
  end
13
14
 
14
- def self.evaluate!(expression, data = {})
15
- calculator.evaluate!(expression, data)
15
+ def self.evaluate!(expression, data = {}, &block)
16
+ calculator.evaluate!(expression, data, &block)
16
17
  end
17
18
 
18
19
  def self.enable_caching!
19
20
  enable_ast_cache!
20
21
  enable_dependency_order_cache!
22
+ enable_identifier_cache!
21
23
  end
22
24
 
23
25
  def self.enable_ast_cache!
@@ -36,6 +38,14 @@ module Dentaku
36
38
  @enable_dependency_order_caching
37
39
  end
38
40
 
41
+ def self.enable_identifier_cache!
42
+ @enable_identifier_caching = true
43
+ end
44
+
45
+ def self.cache_identifier?
46
+ @enable_identifier_caching
47
+ end
48
+
39
49
  def self.aliases
40
50
  @aliases
41
51
  end
@@ -44,8 +54,6 @@ module Dentaku
44
54
  @aliases = hash
45
55
  end
46
56
 
47
- private
48
-
49
57
  def self.calculator
50
58
  @calculator ||= Dentaku::Calculator.new
51
59
  end
@@ -15,14 +15,19 @@ require_relative './ast/array'
15
15
  require_relative './ast/grouping'
16
16
  require_relative './ast/case'
17
17
  require_relative './ast/function_registry'
18
+ require_relative './ast/functions/all'
18
19
  require_relative './ast/functions/and'
20
+ require_relative './ast/functions/any'
19
21
  require_relative './ast/functions/avg'
20
22
  require_relative './ast/functions/count'
23
+ require_relative './ast/functions/duration'
21
24
  require_relative './ast/functions/if'
25
+ require_relative './ast/functions/map'
22
26
  require_relative './ast/functions/max'
23
27
  require_relative './ast/functions/min'
24
28
  require_relative './ast/functions/not'
25
29
  require_relative './ast/functions/or'
30
+ require_relative './ast/functions/pluck'
26
31
  require_relative './ast/functions/round'
27
32
  require_relative './ast/functions/rounddown'
28
33
  require_relative './ast/functions/roundup'
@@ -1,10 +1,20 @@
1
+ require_relative "./node"
2
+
1
3
  module Dentaku
2
4
  module AST
3
- class Access
5
+ class Access < Node
4
6
  def self.arity
5
7
  2
6
8
  end
7
9
 
10
+ def self.min_param_count
11
+ arity
12
+ end
13
+
14
+ def self.max_param_count
15
+ arity
16
+ end
17
+
8
18
  def self.peek(*)
9
19
  end
10
20
 
@@ -22,6 +32,10 @@ module Dentaku
22
32
  def dependencies(context = {})
23
33
  @structure.dependencies(context) + @index.dependencies(context)
24
34
  end
35
+
36
+ def type
37
+ nil
38
+ end
25
39
  end
26
40
  end
27
41
  end
@@ -1,4 +1,5 @@
1
1
  require_relative './operation'
2
+ require_relative '../date_arithmetic'
2
3
  require 'bigdecimal'
3
4
  require 'bigdecimal/util'
4
5
 
@@ -12,6 +13,7 @@ module Dentaku
12
13
  raise NodeError.new(:numeric, left.type, :left),
13
14
  "#{self.class} requires numeric operands"
14
15
  end
16
+
15
17
  unless valid_right?
16
18
  raise NodeError.new(:numeric, right.type, :right),
17
19
  "#{self.class} requires numeric operands"
@@ -29,7 +31,12 @@ module Dentaku
29
31
  def value(context = {})
30
32
  l = cast(left.value(context))
31
33
  r = cast(right.value(context))
32
- l.public_send(operator, r)
34
+ begin
35
+ l.public_send(operator, r)
36
+ rescue ::TypeError => e
37
+ # Right cannot be converted to a suitable type for left. e.g. [] + 1
38
+ raise Dentaku::ArgumentError.for(:incompatible_type, value: r, for: l.class), e.message
39
+ end
33
40
  end
34
41
 
35
42
  private
@@ -40,7 +47,7 @@ module Dentaku
40
47
  end
41
48
 
42
49
  def numeric(val, prefer_integer)
43
- v = BigDecimal.new(val, Float::DIG + 1)
50
+ v = BigDecimal(val, Float::DIG + 1)
44
51
  v = v.to_i if prefer_integer && v.frac.zero?
45
52
  v
46
53
  rescue ::TypeError
@@ -50,15 +57,15 @@ module Dentaku
50
57
  end
51
58
 
52
59
  def valid_node?(node)
53
- node && (node.dependencies.any? || node.type == :numeric)
60
+ node && (node.type == :numeric || node.dependencies.any?)
54
61
  end
55
62
 
56
63
  def valid_left?
57
- valid_node?(left)
64
+ valid_node?(left) || left.type == :datetime
58
65
  end
59
66
 
60
67
  def valid_right?
61
- valid_node?(right)
68
+ valid_node?(right) || right.type == :duration || right.type == :datetime
62
69
  end
63
70
 
64
71
  def validate_value(val)
@@ -77,7 +84,7 @@ module Dentaku
77
84
  end
78
85
 
79
86
  def validate_format(string)
80
- unless string =~ /\A-?\d*(\.\d+)?\z/
87
+ unless string =~ /\A-?\d*(\.\d+)?\z/ && !string.empty?
81
88
  raise Dentaku::ArgumentError.for(:invalid_value, value: string, for: BigDecimal),
82
89
  "String input '#{string}' is not coercible to numeric"
83
90
  end
@@ -92,6 +99,14 @@ module Dentaku
92
99
  def self.precedence
93
100
  10
94
101
  end
102
+
103
+ def value(context = {})
104
+ if left.type == :datetime
105
+ Dentaku::DateArithmetic.new(left.value(context)).add(right.value(context))
106
+ else
107
+ super
108
+ end
109
+ end
95
110
  end
96
111
 
97
112
  class Subtraction < Arithmetic
@@ -102,6 +117,14 @@ module Dentaku
102
117
  def self.precedence
103
118
  10
104
119
  end
120
+
121
+ def value(context = {})
122
+ if left.type == :datetime
123
+ Dentaku::DateArithmetic.new(left.value(context)).sub(right.value(context))
124
+ else
125
+ super
126
+ end
127
+ end
105
128
  end
106
129
 
107
130
  class Multiplication < Arithmetic
@@ -1,9 +1,19 @@
1
+ require_relative "./node"
2
+
1
3
  module Dentaku
2
4
  module AST
3
- class Array
5
+ class Array < Node
4
6
  def self.arity
5
7
  end
6
8
 
9
+ def self.min_param_count
10
+ 0
11
+ end
12
+
13
+ def self.max_param_count
14
+ Float::INFINITY
15
+ end
16
+
7
17
  def self.peek(*)
8
18
  end
9
19
 
@@ -18,6 +28,10 @@ module Dentaku
18
28
  def dependencies(context = {})
19
29
  @elements.flat_map { |el| el.dependencies(context) }
20
30
  end
31
+
32
+ def type
33
+ nil
34
+ end
21
35
  end
22
36
  end
23
37
  end
@@ -3,24 +3,34 @@ require_relative './case/case_when'
3
3
  require_relative './case/case_then'
4
4
  require_relative './case/case_switch_variable'
5
5
  require_relative './case/case_else'
6
+ require 'dentaku/exceptions'
6
7
 
7
8
  module Dentaku
8
9
  module AST
9
10
  class Case < Node
11
+ def self.min_param_count
12
+ 2
13
+ end
14
+
15
+ def self.max_param_count
16
+ Float::INFINITY
17
+ end
18
+
10
19
  def initialize(*nodes)
11
20
  @switch = nodes.shift
12
21
 
13
22
  unless @switch.is_a?(AST::CaseSwitchVariable)
14
- raise 'Case missing switch variable'
23
+ raise ParseError.for(:node_invalid), 'Case missing switch variable'
15
24
  end
16
25
 
17
26
  @conditions = nodes
18
27
 
28
+ @else = nil
19
29
  @else = @conditions.pop if @conditions.last.is_a?(AST::CaseElse)
20
30
 
21
31
  @conditions.each do |condition|
22
32
  unless condition.is_a?(AST::CaseConditional)
23
- raise "#{condition} is not a CaseConditional"
33
+ raise ParseError.for(:node_invalid), "#{condition} is not a CaseConditional"
24
34
  end
25
35
  end
26
36
  end
@@ -36,7 +46,7 @@ module Dentaku
36
46
  if @else
37
47
  return @else.value(context)
38
48
  else
39
- raise "No block matched the switch value '#{switch_value}'"
49
+ raise ArgumentError.for(:invalid_value), "No block matched the switch value '#{switch_value}'"
40
50
  end
41
51
  end
42
52
 
@@ -1,17 +1,28 @@
1
+ require 'dentaku/exceptions'
2
+
1
3
  module Dentaku
2
4
  module AST
3
5
  class CaseConditional < Node
4
6
  attr_reader :when,
5
7
  :then
6
8
 
9
+ def self.min_param_count
10
+ 2
11
+ end
12
+
13
+ def self.max_param_count
14
+ 2
15
+ end
16
+
7
17
  def initialize(when_statement, then_statement)
8
18
  @when = when_statement
9
19
  unless @when.is_a?(AST::CaseWhen)
10
- raise 'Expected first argument to be a CaseWhen'
20
+ raise ParseError.for(:node_invalid), 'Expected first argument to be a CaseWhen'
11
21
  end
22
+
12
23
  @then = then_statement
13
24
  unless @then.is_a?(AST::CaseThen)
14
- raise 'Expected second argument to be a CaseThen'
25
+ raise ParseError.for(:node_invalid), 'Expected second argument to be a CaseThen'
15
26
  end
16
27
  end
17
28