forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation_plural_evaluator.cpp
466 lines (442 loc) · 16.5 KB
/
translation_plural_evaluator.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#if defined(LOCALIZE)
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include "enum_conversions.h"
#include "string_formatter.h"
#include "translation_plural_evaluator.h"
#include "try_parse_integer.h"
using LexicalError = TranslationPluralRulesEvaluator::LexicalError;
using ParseError = TranslationPluralRulesEvaluator::ParseError;
using EvaluationError = TranslationPluralRulesEvaluator::EvaluationError;
using ExprTokenType = TranslationPluralRulesEvaluator::ExprTokenType;
using Token = TranslationPluralRulesEvaluator::ExprToken;
using ExprNode = TranslationPluralRulesEvaluator::ExprNode;
// Workaround to GCC 5.5 not being able to hash an enum class of primitive underlying type
namespace std
{
template<> struct hash<ExprTokenType> {
std::size_t operator()( const ExprTokenType &token ) const {
return static_cast<std::size_t>( token );
}
};
} // namespace std
std::string TranslationPluralRulesEvaluator::ExprTokenTypeToString( ExprTokenType token )
{
static const std::unordered_map<ExprTokenType, const char *> ExprTokenTypeToStringMap{
{ ExprTokenType::Variable, "n" },
{ ExprTokenType::Plus, "+" },
{ ExprTokenType::Minus, "-" },
{ ExprTokenType::Multiplication, "*" },
{ ExprTokenType::Division, "/" },
{ ExprTokenType::Remainder, "%" },
{ ExprTokenType::LessThan, "<" },
{ ExprTokenType::Equal, "==" },
{ ExprTokenType::GreaterThan, ">" },
{ ExprTokenType::LessOrEqual, "<=" },
{ ExprTokenType::GreaterOrEqual, ">=" },
{ ExprTokenType::NotEqual, "!=" },
{ ExprTokenType::Negation, "!" },
{ ExprTokenType::And, "&&" },
{ ExprTokenType::Or, "||" },
{ ExprTokenType::LeftBracket, "("},
{ ExprTokenType::RightBracket, ")" },
{ ExprTokenType::Condition, "?" },
{ ExprTokenType::Branch, ":" },
{ ExprTokenType::Termination, ";" }
};
auto it = ExprTokenTypeToStringMap.find( token );
if( it == ExprTokenTypeToStringMap.end() ) {
return "[?]";
}
return it->second;
}
bool TranslationPluralRulesEvaluator::IsOperandToken( ExprTokenType token )
{
static const std::unordered_set<ExprTokenType> OperandTokens = {
ExprTokenType::Variable,
ExprTokenType::Constant
};
return OperandTokens.count( token ) == 1;
}
bool TranslationPluralRulesEvaluator::IsUnaryToken( ExprTokenType token )
{
static const std::unordered_set<ExprTokenType> UnaryTokens = {
ExprTokenType::Negation
};
return UnaryTokens.count( token );
}
bool TranslationPluralRulesEvaluator::IsBinaryToken( ExprTokenType token )
{
static const std::unordered_set<ExprTokenType> BinaryTokens = {
ExprTokenType::Plus,
ExprTokenType::Minus,
ExprTokenType::Multiplication,
ExprTokenType::Division,
ExprTokenType::Remainder,
ExprTokenType::LessThan,
ExprTokenType::Equal,
ExprTokenType::GreaterThan,
ExprTokenType::LessOrEqual,
ExprTokenType::NotEqual,
ExprTokenType::GreaterOrEqual,
ExprTokenType::And,
ExprTokenType::Or
};
return BinaryTokens.count( token );
}
bool TranslationPluralRulesEvaluator::IsTernaryToken( ExprTokenType token )
{
static const std::unordered_set<ExprTokenType> TernaryTokens = {
ExprTokenType::Condition,
ExprTokenType::Branch
};
return TernaryTokens.count( token );
}
int TranslationPluralRulesEvaluator::OperatorPrecedence( ExprTokenType token )
{
static const std::unordered_map<ExprTokenType, int> OperatorPrecedenceMap = {
{ ExprTokenType::Plus, 5 },
{ ExprTokenType::Minus, 5 },
{ ExprTokenType::Multiplication, 6 },
{ ExprTokenType::Division, 6 },
{ ExprTokenType::Remainder, 6 },
{ ExprTokenType::LessThan, 4 },
{ ExprTokenType::Equal, 3 },
{ ExprTokenType::GreaterThan, 4 },
{ ExprTokenType::LessOrEqual, 4 },
{ ExprTokenType::GreaterOrEqual, 4 },
{ ExprTokenType::NotEqual, 3 },
{ ExprTokenType::Negation, 7 },
{ ExprTokenType::And, 2 },
{ ExprTokenType::Or, 1 },
{ ExprTokenType::Condition, 0 },
{ ExprTokenType::Branch, 0 }
};
return OperatorPrecedenceMap.at( token );
}
std::string TranslationPluralRulesEvaluator::ExprToken::ToString() const
{
if( type == ExprTokenType::Constant ) {
return std::to_string( value );
} else {
return ExprTokenTypeToString( type );
}
}
Token TranslationPluralRulesEvaluator::GetNextToken( const char *&p )
{
while( *p != '\0' && *p == ' ' ) {
++p;
}
if( *p == '\0' || *p == ';' ) {
return ExprToken( ExprTokenType::Termination, 0 );
} else if( *p == 'n' ) {
++p;
return ExprToken( ExprTokenType::Variable, 0 );
} else if( *p == '+' ) {
++p;
return ExprToken( ExprTokenType::Plus, 0 );
} else if( *p == '-' ) {
++p;
return ExprToken( ExprTokenType::Minus, 0 );
} else if( *p == '*' ) {
++p;
return ExprToken( ExprTokenType::Multiplication, 0 );
} else if( *p == '/' ) {
++p;
return ExprToken( ExprTokenType::Division, 0 );
} else if( *p == '%' ) {
++p;
return ExprToken( ExprTokenType::Remainder, 0 );
} else if( *p == '<' && *( p + 1 ) == '=' ) {
p += 2;
return ExprToken( ExprTokenType::LessOrEqual, 0 );
} else if( *p == '=' && *( p + 1 ) == '=' ) {
p += 2;
return ExprToken( ExprTokenType::Equal, 0 );
} else if( *p == '>' && *( p + 1 ) == '=' ) {
p += 2;
return ExprToken( ExprTokenType::GreaterOrEqual, 0 );
} else if( *p == '!' && *( p + 1 ) == '=' ) {
p += 2;
return ExprToken( ExprTokenType::NotEqual, 0 );
} else if( *p == '<' ) {
++p;
return ExprToken( ExprTokenType::LessThan, 0 );
} else if( *p == '>' ) {
++p;
return ExprToken( ExprTokenType::GreaterThan, 0 );
} else if( *p == '!' ) {
++p;
return ExprToken( ExprTokenType::Negation, 0 );
} else if( *p == '&' && *( p + 1 ) == '&' ) {
p += 2;
return ExprToken( ExprTokenType::And, 0 );
} else if( *p == '|' && *( p + 1 ) == '|' ) {
p += 2;
return ExprToken( ExprTokenType::Or, 0 );
} else if( *p == '(' ) {
++p;
return ExprToken( ExprTokenType::LeftBracket, 0 );
} else if( *p == ')' ) {
++p;
return ExprToken( ExprTokenType::RightBracket, 0 );
} else if( *p == '?' ) {
++p;
return ExprToken( ExprTokenType::Condition, 0 );
} else if( *p == ':' ) {
++p;
return ExprToken( ExprTokenType::Branch, 0 );
} else if( std::isdigit( *p ) ) {
int value = 0;
while( std::isdigit( *p ) ) {
value = value * 10 + static_cast<int>( *p - '0' );
++p;
}
return ExprToken( ExprTokenType::Constant, value );
} else {
throw LexicalError( string_format( "Unexpected character %c in expression", *p ) );
}
}
std::vector<Token> TranslationPluralRulesEvaluator::Lexer( const std::string &expr )
{
std::vector<ExprToken> tokens;
const char *p = &expr[0];
ExprToken token;
while( ( token = GetNextToken( p ) ).type != ExprTokenType::Termination ) {
tokens.emplace_back( token );
}
return tokens;
}
void TranslationPluralRulesEvaluator::ExprNode::AddChild( ExprNode *child )
{
if( n_children == 3 ) {
throw ParseError( "Children nodes exceeded maximum of 3." );
}
children[n_children++] = child;
}
int TranslationPluralRulesEvaluator::ExprNode::Evaluate( std::size_t n )
{
if( n_children == 0 ) {
switch( token.type ) {
case ExprTokenType::Constant:
return token.value;
case ExprTokenType::Variable:
return n;
default:
throw EvaluationError( string_format( "Unexpected token %s at a node with 0 child.",
ExprTokenTypeToString( token.type ) ) );
}
} else if( n_children == 1 ) {
switch( token.type ) {
case ExprTokenType::Negation:
return children[0]->Evaluate( n ) == 0;
default:
throw EvaluationError( string_format( "Unexpected token %s at a node with 1 child.",
ExprTokenTypeToString( token.type ) ) );
}
} else if( n_children == 2 ) {
const int a = children[0]->Evaluate( n );
const int b = children[1]->Evaluate( n );
switch( token.type ) {
case ExprTokenType::Plus:
return a + b;
case ExprTokenType::Minus:
return a - b;
case ExprTokenType::Multiplication:
return a * b;
case ExprTokenType::Division:
if( b == 0 ) {
throw EvaluationError( "Division by zero" );
}
return a / b;
case ExprTokenType::Remainder:
if( b == 0 ) {
throw EvaluationError( "Division by zero" );
}
return a % b;
case ExprTokenType::LessThan:
return a < b;
case ExprTokenType::Equal:
return a == b;
case ExprTokenType::GreaterThan:
return a > b;
case ExprTokenType::NotEqual:
return a != b;
case ExprTokenType::LessOrEqual:
return a <= b;
case ExprTokenType::GreaterOrEqual:
return a >= b;
case ExprTokenType::Or:
return a || b;
case ExprTokenType::And:
return a && b;
default:
throw EvaluationError( string_format( "Unexpected token %s at a node with 2 children.",
ExprTokenTypeToString( token.type ) ) );
}
} else if( n_children == 3 ) {
switch( token.type ) {
case ExprTokenType::Condition:
return children[0]->Evaluate( n ) ? children[1]->Evaluate( n ) : children[2]->Evaluate( n );
default:
throw EvaluationError( string_format( "Unexpected token %s at a node with 3 children.",
ExprTokenTypeToString( token.type ) ) );
}
} else {
throw EvaluationError( string_format( "Unexpected %zu children in a node.", n_children ) );
}
}
void TranslationPluralRulesEvaluator::UnwindStacks( std::stack<ExprNode *> &operands,
std::stack<ExprNode *> &operators,
int precedence )
{
while( not operators.empty() &&
OperatorPrecedence( operators.top()->token.type ) >= precedence &&
( OperatorPrecedence( operators.top()->token.type ) > precedence ||
not IsTernaryToken( operators.top()->token.type ) ) ) {
if( IsUnaryToken( operators.top()->token.type ) ) {
if( operands.empty() ) {
throw ParseError( "Insufficient operands" );
}
ExprNode *a = operands.top();
operands.pop();
ExprNode *op = operators.top();
operators.pop();
op->AddChild( a );
operands.push( op );
} else if( IsBinaryToken( operators.top()->token.type ) ) {
if( operands.size() < 2 ) {
throw ParseError( "Insufficient operands" );
}
ExprNode *b = operands.top();
operands.pop();
ExprNode *a = operands.top();
operands.pop();
ExprNode *op = operators.top();
operators.pop();
op->AddChild( a );
op->AddChild( b );
operands.push( op );
} else if( IsTernaryToken( operators.top()->token.type ) ) {
if( operands.size() < 3 || operands.size() < 2 ) {
break;
}
ExprNode *c = operands.top();
operands.pop();
ExprNode *b = operands.top();
operands.pop();
ExprNode *a = operands.top();
operands.pop();
ExprNode *op2 = operators.top();
operators.pop();
ExprNode *op1 = operators.top();
operators.pop();
op1->AddChild( a );
op1->AddChild( b );
op1->AddChild( c );
operands.push( op1 );
delete op2;
}
}
}
ExprNode *TranslationPluralRulesEvaluator::ConstructExprTreeRecursive(
std::vector<ExprToken>::const_iterator &it,
const std::vector<ExprToken>::const_iterator &end )
{
std::stack<ExprNode *> operands;
std::stack<ExprNode *> operators;
while( it != end && it->type != ExprTokenType::Termination ) {
if( it->type == ExprTokenType::LeftBracket ) {
operands.push( ConstructExprTreeRecursive( ++it, end ) );
} else if( it->type == ExprTokenType::RightBracket ) {
++it;
break;
} else {
ExprNode *node = new ExprNode;
node->token = *it;
if( IsOperandToken( it->type ) ) {
operands.push( node );
} else {
UnwindStacks( operands, operators, OperatorPrecedence( it->type ) );
operators.push( node );
}
++it;
}
}
UnwindStacks( operands, operators );
if( operands.size() == 1 && operators.empty() ) {
return operands.top();
} else if( operators.size() == 1 && operands.empty() ) {
return operators.top();
}
throw ParseError( "Stacks not unwinded during expression tree construction." );
}
void TranslationPluralRulesEvaluator::ConstructExprTree( const std::vector<ExprToken> &tokens )
{
auto it = tokens.begin();
root = ConstructExprTreeRecursive( it, tokens.end() );
}
void TranslationPluralRulesEvaluator::DisposeExprNodeRecursive( ExprNode *node )
{
for( std::size_t i = 0; i < node->n_children; i++ ) {
DisposeExprNodeRecursive( node->children[i] );
delete node->children[i];
node->children[i] = nullptr;
}
}
void TranslationPluralRulesEvaluator::DisposeExprTree()
{
if( root != nullptr ) {
DisposeExprNodeRecursive( root );
delete root;
root = nullptr;
}
}
std::size_t TranslationPluralRulesEvaluator::Evaluate( const std::size_t n ) const
{
std::size_t result = root->Evaluate( n );
if( result >= n_plurals ) {
throw EvaluationError( "Plural expression exceeded number of plural forms." );
}
return result;
}
void TranslationPluralRulesEvaluator::ParsePluralRules( const std::string &plural_forms )
{
const std::string nplurals_header = "nplurals=";
std::size_t nplurals_header_pos = plural_forms.find( nplurals_header );
if( nplurals_header_pos == std::string::npos ) {
throw HeaderError( string_format( "Expect \"%s\" in %s", nplurals_header, plural_forms ) );
}
nplurals_header_pos += nplurals_header.length();
std::size_t first_semicolon = plural_forms.find( ';', nplurals_header_pos );
if( first_semicolon == std::string::npos ) {
throw HeaderError( string_format( "Expect semicolon in %s", plural_forms ) );
}
ret_val<int> n_plurals_optional = try_parse_integer<int>( plural_forms.substr(
nplurals_header_pos, first_semicolon - nplurals_header_pos ), false );
if( !n_plurals_optional.success() || n_plurals_optional.value() < 1 ) {
throw HeaderError( string_format( "Invalid number of plural forms in %s", plural_forms ) );
}
n_plurals = static_cast<std::size_t>( n_plurals_optional.value() );
const std::string plural_header = "plural=";
std::size_t plural_header_pos = plural_forms.find( plural_header );
if( plural_header_pos == std::string::npos ) {
throw HeaderError( string_format( "Expect \"%s\" in %s", plural_header, plural_forms ) );
}
plural_header_pos += plural_header.length();
std::size_t second_semicolon = plural_forms.find( ';', plural_header_pos );
const std::string expr = plural_forms.substr( plural_header_pos,
second_semicolon - plural_header_pos );
const std::vector<ExprToken> tokens = Lexer( expr );
ConstructExprTree( tokens );
}
TranslationPluralRulesEvaluator::TranslationPluralRulesEvaluator( const std::string &plural_forms )
{
ParsePluralRules( plural_forms );
}
TranslationPluralRulesEvaluator::~TranslationPluralRulesEvaluator()
{
DisposeExprTree();
}
#endif // defined(LOCALIZE)