fast_gettext 4.2.0 → 4.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2add114a98a74b14ab732ccee6fbe25e0298c6d766f166ae7656304ff1bb29d7
4
- data.tar.gz: aaee771bc595d542e3954da371d3cb661a2b6d7f4b38ed1d5b71ab9909fda919
3
+ metadata.gz: 64684fc2728680b23966f9d04881ecb94fb8cc55bcbeb8957e8f2801ecfd9e83
4
+ data.tar.gz: 912394d101ac7f933f1b68f508a666f3fa0ac91c1dada28602c82c4eb81ff3bb
5
5
  SHA512:
6
- metadata.gz: 5df7240d2fa1f5e42e8e552358dd3ba2e564edb837e22d9e64380d3c08404b1d1c4d5cd93d3c4da2546934d430474746cf475393936b7500a727e52ff625fe48
7
- data.tar.gz: 58c342df8b8fba16c25003d5b8126984556acf8fd9c0bab9ae8393fa1d7e111c5b5bd3360fd562342da0985c81da3965a8998af79a5c670e55b577e4a7eb268b
6
+ metadata.gz: fb36b5a79ca99b573cf98705689b718654f5950dd6ecdebfdbfd549056153bf40e91e1f3a8b43ec4904d86dd394abf07a136c96a8e6ea52430b2f958e69f28e8
7
+ data.tar.gz: 7c655075c247157540a8c35d51ba17e547e622dccb3cef2b761c438d9d34889fd3de3e85f21cb7df3fd33266418979c7f227c3c9d8773156d1bcd779e661ff1f
data/CHANGELOG CHANGED
@@ -1,3 +1,4 @@
1
+ 4.3.0 -- stop using eval for pluralisation rule
1
2
  4.2.0 -- cache pluralisation rule
2
3
  4.1.0 -- drop EOL rubies + add racc dependency
3
4
  4.0.0 -- Remove String#% hack that would make it not blow up when using invalid keys, can temporarily restore with `FastGettext.allow_invalid_keys!`
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'fast_gettext/vendor/mofile'
4
+ require 'fast_gettext/plural_expression'
4
5
  module FastGettext
5
6
  # Responsibility:
6
7
  # - abstract mo files for Mo Repository
@@ -55,7 +56,7 @@ module FastGettext
55
56
  # additionally parsing fails when directly accessing po files, so this line was taken from gettext/mofile
56
57
  (data[''] || '').split("\n").each do |line|
57
58
  if /^Plural-Forms:\s*nplurals\s*=\s*(\d*);\s*plural\s*=\s*([^;]*)\n?/ =~ line
58
- return eval("->(n) { #{$2} }", nil, __FILE__, __LINE__) # rubocop:disable Security/Eval
59
+ return PluralExpression.compile($2)
59
60
  end
60
61
  end
61
62
  nil
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FastGettext
4
+ # The plural expression of a Plural-Forms header, or the pluralisation_rule
5
+ # of a YAML locale, is arithmetic over n. Parse it here instead of evaluating
6
+ # it: a catalogue is input, and a rule in it must never run as Ruby code.
7
+ #
8
+ # The grammar is the one GNU gettext defines for the expression: decimal
9
+ # integers, the variable n, parentheses, the ternary ?:, comparison, equality,
10
+ # boolean, additive and multiplicative operators, and !, unary - and unary +.
11
+ # Anything else raises InvalidExpression.
12
+ module PluralExpression
13
+ # A rule outside the grammar. It is a SyntaxError because the evaluation
14
+ # this replaces raised SyntaxError for a rule Ruby could not parse.
15
+ class InvalidExpression < SyntaxError; end
16
+
17
+ OPERATORS = [
18
+ "||", "&&", "==", "!=", "<=", ">=", "<", ">",
19
+ "+", "-", "*", "/", "%", "!", "?", ":", "(", ")"
20
+ ].freeze
21
+ PRECEDENCE = {
22
+ "||" => 1,
23
+ "&&" => 2,
24
+ "==" => 3, "!=" => 3,
25
+ "<" => 4, "<=" => 4, ">" => 4, ">=" => 4,
26
+ "+" => 5, "-" => 5,
27
+ "*" => 6, "/" => 6, "%" => 6
28
+ }.freeze
29
+ TOKEN = /\s+|#{Regexp.union(OPERATORS)}|\d+|\bn\b|./
30
+
31
+ module_function
32
+
33
+ # Compile a rule to a callable that selects a plural index for a count.
34
+ def compile(source)
35
+ expression = Parser.new(tokenize(source)).parse
36
+ ->(n) { evaluate(expression, n) }
37
+ end
38
+
39
+ def tokenize(source)
40
+ tokens = source.to_s.scan(TOKEN)
41
+ tokens.reject! { |token| token.match?(/\A\s+\z/) }
42
+ tokens.each do |token|
43
+ next if token == "n" || token.match?(/\A\d+\z/) || OPERATORS.include?(token)
44
+
45
+ raise InvalidExpression, "unexpected #{token.inspect} in plural expression"
46
+ end
47
+ tokens
48
+ end
49
+
50
+ def evaluate(expression, n)
51
+ case expression[0]
52
+ when :number
53
+ expression[1]
54
+ when :variable
55
+ n
56
+ when :not
57
+ !evaluate(expression[1], n)
58
+ when :negate
59
+ -evaluate(expression[1], n)
60
+ when :positive
61
+ evaluate(expression[1], n)
62
+ when :conditional
63
+ evaluate(expression[1], n) ? evaluate(expression[2], n) : evaluate(expression[3], n)
64
+ else
65
+ operator = expression[1]
66
+ left = evaluate(expression[2], n)
67
+ return left && evaluate(expression[3], n) if operator == "&&"
68
+ return left || evaluate(expression[3], n) if operator == "||"
69
+
70
+ right = evaluate(expression[3], n)
71
+ case operator
72
+ when "==" then left == right
73
+ when "!=" then left != right
74
+ when "<" then left < right
75
+ when "<=" then left <= right
76
+ when ">" then left > right
77
+ when ">=" then left >= right
78
+ else left.send(operator, right)
79
+ end
80
+ end
81
+ end
82
+
83
+ class Parser
84
+ def initialize(tokens)
85
+ @tokens = tokens
86
+ @position = 0
87
+ end
88
+
89
+ def parse
90
+ result = parse_expression
91
+ unless @position == @tokens.size
92
+ raise InvalidExpression, "unexpected #{peek.inspect} in plural expression"
93
+ end
94
+
95
+ result
96
+ end
97
+
98
+ def parse_expression
99
+ condition = binary(1)
100
+ return condition unless take("?")
101
+
102
+ positive = parse_expression
103
+ need(":")
104
+ [:conditional, condition, positive, parse_expression]
105
+ end
106
+
107
+ def binary(level)
108
+ return unary if level > 6
109
+
110
+ left = binary(level + 1)
111
+ while PRECEDENCE[peek] == level
112
+ operator = peek
113
+ @position += 1
114
+ left = [:binary, operator, left, binary(level + 1)]
115
+ end
116
+ left
117
+ end
118
+
119
+ def unary
120
+ return [:not, unary] if take("!")
121
+ return [:negate, unary] if take("-")
122
+ return [:positive, unary] if take("+")
123
+
124
+ atom
125
+ end
126
+
127
+ def atom
128
+ token = peek
129
+ @position += 1
130
+ case token
131
+ when /\A\d+\z/ then [:number, token.to_i]
132
+ when "n" then [:variable]
133
+ when "(" then parse_expression.tap { need(")") }
134
+ else raise InvalidExpression, "unexpected #{token.inspect} in plural expression"
135
+ end
136
+ end
137
+
138
+ def peek
139
+ @tokens[@position]
140
+ end
141
+
142
+ def take(token)
143
+ return false unless peek == token
144
+
145
+ @position += 1
146
+ true
147
+ end
148
+
149
+ def need(token)
150
+ return if take(token)
151
+
152
+ raise InvalidExpression, "expected #{token.inspect} in plural expression"
153
+ end
154
+ end
155
+ end
156
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'fast_gettext/translation_repository/base'
4
+ require 'fast_gettext/plural_expression'
4
5
  require 'yaml'
5
6
 
6
7
  module FastGettext
@@ -31,7 +32,7 @@ module FastGettext
31
32
  return unless rule = self['pluralisation_rule']
32
33
 
33
34
  (@pluralisation_rules ||= {})[rule] ||=
34
- eval("->(n) { #{rule} }", nil, __FILE__, __LINE__) # rubocop:disable Security/Eval -- TODO remove eval
35
+ PluralExpression.compile(rule)
35
36
  end
36
37
 
37
38
  def reload
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastGettext
4
- VERSION = Version = '4.2.0' # rubocop:disable Naming/ConstantName
4
+ VERSION = Version = '4.3.0' # rubocop:disable Naming/ConstantName
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_gettext
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -188,6 +188,7 @@ files:
188
188
  - lib/fast_gettext.rb
189
189
  - lib/fast_gettext/cache.rb
190
190
  - lib/fast_gettext/mo_file.rb
191
+ - lib/fast_gettext/plural_expression.rb
191
192
  - lib/fast_gettext/po_file.rb
192
193
  - lib/fast_gettext/storage.rb
193
194
  - lib/fast_gettext/translation.rb