-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplural_rule.go
327 lines (268 loc) · 6.55 KB
/
plural_rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package pogo
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
var (
pluralAllRE = regexp.MustCompile(`^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=(.+);$`)
pluralRuleRE = regexp.MustCompile(`\s*([^?]+)\?\s*(\d+)\s*:(\s*(\d+)\s*)?`)
)
// PluralRules represent list of rules to evaluate which form should be used
type PluralRules []PluralRule
// Eval evaluate rules to find which form should be used
func (rules PluralRules) Eval(n int) int {
if len(rules) == 1 {
if rules[0].Check(n) {
return 1
}
return 0
}
for i := range rules {
if rules[i].Check(n) {
return i
}
}
return len(rules)
}
// Len is a number of rules
func (rules PluralRules) Len() int {
return len(rules) + 1
}
// String implements fmt.Stringer
func (rules PluralRules) String() string {
res := &strings.Builder{}
_, _ = fmt.Fprintf(res, "nplurals=%d; plural=", rules.Len())
if len(rules) == 1 {
_, _ = fmt.Fprintf(res, "%s;", rules[0])
} else {
for i := range rules {
_, _ = fmt.Fprintf(res, "%s ? %d : ", rules[i], i)
}
_, _ = fmt.Fprintf(res, "%d;", len(rules))
}
return res.String()
}
// ParsePluralRules from po format
func ParsePluralRules(source string) (PluralRules, error) {
sub := pluralAllRE.FindStringSubmatch(source)
if len(sub) != 3 {
return nil, errors.New("invalid source format")
}
n, _ := strconv.Atoi(sub[1])
switch n {
case 0:
return nil, errors.New("nplurals shouldn't be zero")
case 1:
k, err := strconv.Atoi(strings.TrimSpace(sub[2]))
if err != nil {
return nil, errors.WithStack(err)
}
if k != 0 {
return nil, errors.Errorf("unexpected choice %d, expected 0", k)
}
return nil, nil
case 2:
rule, err := ParsePluralRule(sub[2])
if err != nil {
return nil, err
}
return PluralRules{rule}, nil
default:
return parsePluralRules(sub[2], n)
}
}
func parsePluralRules(source string, n int) (PluralRules, error) {
subs := pluralRuleRE.FindAllStringSubmatch(source, -1)
if len(subs) != n-1 {
return nil, errors.New("rules count missmatch")
}
res := make(PluralRules, n-1)
for i, sub := range subs {
k, _ := strconv.Atoi(sub[2])
if k != i {
return nil, errors.Errorf("unexpected choice %d, expected %d", k, i)
}
if i == n-2 {
o, _ := strconv.Atoi(sub[4])
if o != n-1 {
return nil, errors.Errorf("unexpected choice %d, expected %d", o, n-1)
}
}
rule, err := ParsePluralRule(strings.TrimSpace(sub[1]))
if err != nil {
return nil, err
}
res[i] = rule
}
return res, nil
}
// PluralRule is a condition to choose variant
type PluralRule interface {
fmt.Stringer
Check(int) bool
}
// ParsePluralRule from source
func ParsePluralRule(source string) (PluralRule, error) {
return ruleBuilder{source}.Build()
}
type evaluer struct {
source string
Eval func(int) int
}
func (e evaluer) String() string {
return e.source
}
func number(n int) evaluer { return evaluer{strconv.Itoa(n), func(x int) int { return n }} }
func equiv() evaluer { return evaluer{"n", func(x int) int { return x }} }
func mod(a, b evaluer) evaluer {
return evaluer{
fmt.Sprintf("%s%%%s", a, b),
func(x int) int { return a.Eval(x) % b.Eval(x) },
}
}
type parenthes struct {
rule PluralRule
}
func (p parenthes) Check(n int) bool {
return p.rule.Check(n)
}
func (p parenthes) String() string {
return fmt.Sprintf("(%s)", p.rule)
}
type checker struct {
op string
a, b fmt.Stringer
check func(int) bool
}
func (cr checker) String() string {
return fmt.Sprintf("%s %s %s", cr.a, cr.op, cr.b)
}
func (cr checker) Check(n int) bool {
return cr.check(n)
}
func eql(a, b evaluer) PluralRule {
return checker{
"==", a, b,
func(n int) bool { return a.Eval(n) == b.Eval(n) },
}
}
func neq(a, b evaluer) PluralRule {
return checker{
"!=", a, b,
func(n int) bool { return a.Eval(n) != b.Eval(n) },
}
}
func gtr(a, b evaluer) PluralRule {
return checker{
">", a, b,
func(n int) bool { return a.Eval(n) > b.Eval(n) },
}
}
func geq(a, b evaluer) PluralRule {
return checker{
">=", a, b,
func(n int) bool { return a.Eval(n) >= b.Eval(n) },
}
}
func lss(a, b evaluer) PluralRule {
return checker{
"<", a, b,
func(n int) bool { return a.Eval(n) < b.Eval(n) },
}
}
func leq(a, b evaluer) PluralRule {
return checker{
"<=", a, b,
func(n int) bool { return a.Eval(n) <= b.Eval(n) },
}
}
func and(a, b PluralRule) PluralRule {
return checker{
"&&", a, b,
func(n int) bool { return a.Check(n) && b.Check(n) },
}
}
func or(a, b PluralRule) PluralRule {
return checker{
"||", a, b,
func(n int) bool { return a.Check(n) || b.Check(n) },
}
}
type ruleBuilder struct {
source string
}
func (cb ruleBuilder) Build() (PluralRule, error) {
expr, err := parser.ParseExpr(cb.source)
if err != nil {
return nil, errors.WithStack(err)
}
var ch PluralRule
err = recoverHandledError(func() {
ch = cb.processExpression(expr)
})
return ch, err
}
func (cb ruleBuilder) processExpression(expr ast.Expr) PluralRule {
switch te := expr.(type) {
case *ast.ParenExpr:
return parenthes{cb.processExpression(te.X)}
case *ast.BinaryExpr:
return cb.processComparsion(te)
}
cb.invalidExpr(expr)
return nil
}
func (cb ruleBuilder) processComparsion(expr *ast.BinaryExpr) PluralRule {
switch expr.Op {
case token.LAND:
return and(cb.processExpression(expr.X), cb.processExpression(expr.Y))
case token.LOR:
return or(cb.processExpression(expr.X), cb.processExpression(expr.Y))
case token.EQL:
return eql(cb.processArithmetic(expr.X), cb.processArithmetic(expr.Y))
case token.NEQ:
return neq(cb.processArithmetic(expr.X), cb.processArithmetic(expr.Y))
case token.GTR:
return gtr(cb.processArithmetic(expr.X), cb.processArithmetic(expr.Y))
case token.GEQ:
return geq(cb.processArithmetic(expr.X), cb.processArithmetic(expr.Y))
case token.LSS:
return lss(cb.processArithmetic(expr.X), cb.processArithmetic(expr.Y))
case token.LEQ:
return leq(cb.processArithmetic(expr.X), cb.processArithmetic(expr.Y))
}
cb.invalidExpr(expr)
return nil
}
func (cb ruleBuilder) processArithmetic(expr ast.Expr) evaluer {
switch te := expr.(type) {
case *ast.BasicLit:
x, err := strconv.Atoi(te.Value)
if err == nil {
return number(x)
}
case *ast.Ident:
if te.Name == "n" {
return equiv()
}
case *ast.BinaryExpr:
if te.Op == token.REM {
return mod(cb.processArithmetic(te.X), cb.processArithmetic(te.Y))
}
}
cb.invalidExpr(expr)
return evaluer{}
}
func (cb ruleBuilder) invalidExpr(expr ast.Expr) {
panic(errors.Errorf(
"%d:%d: invalid expression '%s'",
expr.Pos(), expr.End(),
cb.source[int(expr.Pos())-1:int(expr.End())-1],
))
}