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 467 468 469 470 471 472 473 474 475
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#ifndef PARSE_PLURAL_H_180465845670839576
#define PARSE_PLURAL_H_180465845670839576
#include <zen/string_tools.h>
namespace plural
{
//expression interface
struct Expression { virtual ~Expression() {} };
template <class T>
struct Expr : public Expression
{
virtual T eval() const = 0;
};
class ParsingError {};
class PluralForm
{
public:
explicit PluralForm(const std::string& stream); //throw ParsingError
size_t getForm(int64_t n) const { n_ = std::abs(n) ; return static_cast<size_t>(expr_->eval()); }
private:
std::shared_ptr<Expr<int64_t>> expr_;
mutable int64_t n_ = 0;
};
//validate plural form
class InvalidPluralForm {};
class PluralFormInfo
{
public:
PluralFormInfo(const std::string& definition, int pluralCount); //throw InvalidPluralForm
size_t getCount() const { return forms_.size(); }
bool isSingleNumberForm(size_t index) const { return index < forms_.size() ? forms_[index].count == 1 : false; }
int getFirstNumber (size_t index) const { return index < forms_.size() ? forms_[index].firstNumber : -1; }
private:
struct FormInfo
{
int count = 0;
int firstNumber = 0; //which maps to the plural form index position
};
std::vector<FormInfo> forms_;
};
//--------------------------- implementation ---------------------------
/* https://www.gnu.org/software/hello/manual/gettext/Plural-forms.html
https://translate.sourceforge.net/wiki/l10n/pluralforms
Grammar for Plural forms parser
-------------------------------
expression:
conditional-expression
conditional-expression:
logical-or-expression
logical-or-expression ? expression : expression
logical-or-expression:
logical-and-expression
logical-or-expression || logical-and-expression
logical-and-expression:
equality-expression
logical-and-expression && equality-expression
equality-expression:
relational-expression
relational-expression == relational-expression
relational-expression != relational-expression
relational-expression:
multiplicative-expression
multiplicative-expression > multiplicative-expression
multiplicative-expression < multiplicative-expression
multiplicative-expression >= multiplicative-expression
multiplicative-expression <= multiplicative-expression
multiplicative-expression:
pm-expression
multiplicative-expression % pm-expression
pm-expression:
variable-number-n-expression
constant-number-expression
( expression )
.po format,e.g.: (n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2) */
namespace impl
{
template <class BinaryOp, class ParamType, class ResultType>
struct BinaryExp : public Expr<ResultType>
{
using ExpLhs = std::shared_ptr<Expr<ParamType>>;
using ExpRhs = std::shared_ptr<Expr<ParamType>>;
BinaryExp(const ExpLhs& lhs, const ExpRhs& rhs) : lhs_(lhs), rhs_(rhs) { assert(lhs && rhs); }
ResultType eval() const override { return BinaryOp()(lhs_->eval(), rhs_->eval()); }
private:
ExpLhs lhs_;
ExpRhs rhs_;
};
template <class BinaryOp, class ParamType> inline
std::shared_ptr<Expression> makeBiExp(const std::shared_ptr<Expression>& lhs, const std::shared_ptr<Expression>& rhs) //throw ParsingError
{
auto exLeft = std::dynamic_pointer_cast<Expr<ParamType>>(lhs);
auto exRight = std::dynamic_pointer_cast<Expr<ParamType>>(rhs);
if (!exLeft || !exRight)
throw ParsingError();
using ResultType = decltype(BinaryOp()(std::declval<ParamType>(), std::declval<ParamType>()));
return std::make_shared<BinaryExp<BinaryOp, ParamType, ResultType>>(exLeft, exRight);
}
template <class T>
struct ConditionalExp : public Expr<T>
{
ConditionalExp(const std::shared_ptr<Expr<bool>>& ifExp,
const std::shared_ptr<Expr<T>>& thenExp,
const std::shared_ptr<Expr<T>>& elseExp) : ifExp_(ifExp), thenExp_(thenExp), elseExp_(elseExp) { assert(ifExp && thenExp && elseExp); }
T eval() const override { return ifExp_->eval() ? thenExp_->eval() : elseExp_->eval(); }
private:
std::shared_ptr<Expr<bool>> ifExp_;
std::shared_ptr<Expr<T>> thenExp_;
std::shared_ptr<Expr<T>> elseExp_;
};
struct ConstNumberExp : public Expr<int64_t>
{
ConstNumberExp(int64_t n) : n_(n) {}
int64_t eval() const override { return n_; }
private:
int64_t n_;
};
struct VariableNumberNExp : public Expr<int64_t>
{
VariableNumberNExp(int64_t& n) : n_(n) {}
int64_t eval() const override { return n_; }
private:
int64_t& n_;
};
//-------------------------------------------------------------------------------
enum class TokenType
{
ternaryQuest,
ternaryColon,
logicOr,
logicAnd,
equal,
notEqual,
less,
lessEqual,
greater,
greaterEqual,
modulus,
variableN,
constNumber,
bracketLeft,
bracketRight,
end,
};
struct Token
{
Token(TokenType t) : type(t) {}
Token(int64_t num) : number(num) {}
TokenType type = TokenType::constNumber;
int64_t number = 0; //if type == TokenType::constNumber
};
class Scanner
{
public:
explicit Scanner(const std::string& stream) : stream_(stream), pos_(stream_.begin()) {}
Token getNextToken() //throw ParsingError
{
//skip whitespace
pos_ = std::find_if_not(pos_, stream_.end(), zen::isWhiteSpace<char>);
if (pos_ == stream_.end())
return TokenType::end;
for (const auto& [tokenString, tokenEnum] : tokens_)
if (startsWith(tokenString))
{
pos_ += tokenString.size();
return Token(tokenEnum);
}
auto digitEnd = std::find_if_not(pos_, stream_.end(), zen::isDigit<char>);
if (pos_ == digitEnd)
throw ParsingError(); //unknown token
auto number = zen::stringTo<int64_t>(std::string(pos_, digitEnd));
pos_ = digitEnd;
return number;
}
private:
bool startsWith(const std::string& prefix) const
{
return zen::startsWith(zen::makeStringView(pos_, stream_.end()), prefix);
}
using TokenList = std::vector<std::pair<std::string, TokenType>>;
const TokenList tokens_
{
{"?", TokenType::ternaryQuest},
{":", TokenType::ternaryColon},
{"||", TokenType::logicOr },
{"&&", TokenType::logicAnd },
{"==", TokenType::equal },
{"!=", TokenType::notEqual },
{"<=", TokenType::lessEqual },
{"<", TokenType::less },
{">=", TokenType::greaterEqual},
{">", TokenType::greater },
{"%", TokenType::modulus },
{"n", TokenType::variableN },
{"N", TokenType::variableN },
{"(", TokenType::bracketLeft },
{")", TokenType::bracketRight},
};
const std::string stream_;
std::string::const_iterator pos_;
};
//-------------------------------------------------------------------------------
class Parser
{
public:
Parser(const std::string& stream, int64_t& n) :
scn_(stream),
tk_(scn_.getNextToken()), //throw ParsingError
n_(n) {}
std::shared_ptr<Expr<int64_t>> parse() //throw ParsingError; return value always bound!
{
auto e = std::dynamic_pointer_cast<Expr<int64_t>>(parseExpression()); //throw ParsingError
if (!e)
throw ParsingError();
expectToken(TokenType::end); //throw ParsingError
return e;
}
private:
std::shared_ptr<Expression> parseExpression() { return parseConditional(); }//throw ParsingError
std::shared_ptr<Expression> parseConditional() //throw ParsingError
{
std::shared_ptr<Expression> e = parseLogicalOr();
if (token().type == TokenType::ternaryQuest)
{
nextToken(); //throw ParsingError
auto ifExp = std::dynamic_pointer_cast<Expr<bool>>(e);
auto thenExp = std::dynamic_pointer_cast<Expr<int64_t>>(parseExpression()); //associativity: <-
consumeToken(TokenType::ternaryColon); //throw ParsingError
auto elseExp = std::dynamic_pointer_cast<Expr<int64_t>>(parseExpression()); //
if (!ifExp || !thenExp || !elseExp)
throw ParsingError();
return std::make_shared<ConditionalExp<int64_t>>(ifExp, thenExp, elseExp);
}
return e;
}
std::shared_ptr<Expression> parseLogicalOr()
{
std::shared_ptr<Expression> e = parseLogicalAnd();
while (token().type == TokenType::logicOr) //associativity: ->
{
nextToken(); //throw ParsingError
std::shared_ptr<Expression> rhs = parseLogicalAnd();
e = makeBiExp<std::logical_or<>, bool>(e, rhs); //throw ParsingError
}
return e;
}
std::shared_ptr<Expression> parseLogicalAnd()
{
std::shared_ptr<Expression> e = parseEquality();
while (token().type == TokenType::logicAnd) //associativity: ->
{
nextToken(); //throw ParsingError
std::shared_ptr<Expression> rhs = parseEquality();
e = makeBiExp<std::logical_and<>, bool>(e, rhs); //throw ParsingError
}
return e;
}
std::shared_ptr<Expression> parseEquality()
{
std::shared_ptr<Expression> e = parseRelational();
TokenType t = token().type;
if (t == TokenType::equal || //associativity: n/a
t == TokenType::notEqual)
{
nextToken(); //throw ParsingError
std::shared_ptr<Expression> rhs = parseRelational();
if (t == TokenType::equal) return makeBiExp<std:: equal_to<>, int64_t>(e, rhs); //throw ParsingError
if (t == TokenType::notEqual) return makeBiExp<std::not_equal_to<>, int64_t>(e, rhs); //
}
return e;
}
std::shared_ptr<Expression> parseRelational()
{
std::shared_ptr<Expression> e = parseMultiplicative();
TokenType t = token().type;
if (t == TokenType::less || //associativity: n/a
t == TokenType::lessEqual ||
t == TokenType::greater ||
t == TokenType::greaterEqual)
{
nextToken(); //throw ParsingError
std::shared_ptr<Expression> rhs = parseMultiplicative();
if (t == TokenType::less) return makeBiExp<std::less <>, int64_t>(e, rhs); //
if (t == TokenType::lessEqual) return makeBiExp<std::less_equal <>, int64_t>(e, rhs); //throw ParsingError
if (t == TokenType::greater) return makeBiExp<std::greater <>, int64_t>(e, rhs); //
if (t == TokenType::greaterEqual) return makeBiExp<std::greater_equal<>, int64_t>(e, rhs); //
}
return e;
}
std::shared_ptr<Expression> parseMultiplicative()
{
std::shared_ptr<Expression> e = parsePrimary();
while (token().type == TokenType::modulus) //associativity: ->
{
nextToken(); //throw ParsingError
std::shared_ptr<Expression> rhs = parsePrimary();
//"compile-time" check: n % 0
if (auto literal = std::dynamic_pointer_cast<ConstNumberExp>(rhs))
if (literal->eval() == 0)
throw ParsingError();
e = makeBiExp<std::modulus<>, int64_t>(e, rhs); //throw ParsingError
}
return e;
}
std::shared_ptr<Expression> parsePrimary()
{
if (token().type == TokenType::variableN)
{
nextToken(); //throw ParsingError
return std::make_shared<VariableNumberNExp>(n_);
}
else if (token().type == TokenType::constNumber)
{
const int64_t number = token().number;
nextToken(); //throw ParsingError
return std::make_shared<ConstNumberExp>(number);
}
else if (token().type == TokenType::bracketLeft)
{
nextToken(); //throw ParsingError
std::shared_ptr<Expression> e = parseExpression();
expectToken(TokenType::bracketRight); //throw ParsingError
nextToken(); //
return e;
}
else
throw ParsingError();
}
const Token& token() const { return tk_; }
void nextToken() { tk_ = scn_.getNextToken(); } //throw ParsingError
void expectToken(TokenType t) //throw ParsingError
{
if (token().type != t)
throw ParsingError();
}
void consumeToken(TokenType t) //throw ParsingError
{
expectToken(t); //throw ParsingError
nextToken();
}
Scanner scn_;
Token tk_;
int64_t& n_;
};
}
inline
PluralFormInfo::PluralFormInfo(const std::string& definition, int pluralCount) //throw InvalidPluralForm
{
if (pluralCount < 1)
throw InvalidPluralForm();
forms_.resize(pluralCount);
try
{
PluralForm pf(definition); //throw ParsingError
//PERF_START
//perf: 80ns per iteration max (for arabic)
//=> 1000 iterations should be fast enough and still detect all "single number forms"
for (int j = 0; j < 1000; ++j)
if (const size_t formNo = pf.getForm(j);
formNo < forms_.size())
{
if (forms_[formNo].count == 0)
forms_[formNo].firstNumber = j;
++forms_[formNo].count;
}
else
throw InvalidPluralForm();
}
catch (const plural::ParsingError&)
{
throw InvalidPluralForm();
}
//ensure each form is used at least once:
if (!std::all_of(forms_.begin(), forms_.end(), [](const FormInfo& fi) { return fi.count >= 1; }))
throw InvalidPluralForm();
}
inline
PluralForm::PluralForm(const std::string& stream) : expr_(impl::Parser(stream, n_).parse()) {} //throw ParsingError
}
#endif //PARSE_PLURAL_H_180465845670839576
|