2015-08-09 19:46:21 +02:00
|
|
|
|
/***************************************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2015 Roman Telezhynskyi
|
|
|
|
|
**
|
|
|
|
|
** Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
|
|
|
** software and associated documentation files (the "Software"), to deal in the Software
|
|
|
|
|
** without restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
|
** merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
** permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
**
|
|
|
|
|
** The above copyright notice and this permission notice shall be included in all copies or
|
|
|
|
|
** substantial portions of the Software.
|
|
|
|
|
**
|
|
|
|
|
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
|
|
|
** NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
|
** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
**
|
|
|
|
|
******************************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "qmuformulabase.h"
|
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
|
#include <QChar>
|
|
|
|
|
#include <QList>
|
2015-10-18 21:30:51 +02:00
|
|
|
|
#include <QLocale>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QStringList>
|
2015-10-18 21:30:51 +02:00
|
|
|
|
|
2022-08-17 09:01:51 +02:00
|
|
|
|
#include "qmudef.h"
|
|
|
|
|
|
2015-08-09 19:46:21 +02:00
|
|
|
|
namespace qmu
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
QmuFormulaBase::QmuFormulaBase()
|
2023-08-12 09:31:10 +02:00
|
|
|
|
: QmuParser()
|
2015-08-09 19:46:21 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
QmuFormulaBase::~QmuFormulaBase()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
2017-01-03 10:14:32 +01:00
|
|
|
|
* @brief InitCharSets init character set for parser.
|
2015-08-09 19:46:21 +02:00
|
|
|
|
*
|
|
|
|
|
* QMuParser require setting character set for legal characters. Because we try make our expresion language independent
|
|
|
|
|
* we set all posible unique characters from all alphabets.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-01-03 10:14:32 +01:00
|
|
|
|
void QmuFormulaBase::InitCharSets()
|
2015-08-09 19:46:21 +02:00
|
|
|
|
{
|
2018-04-22 18:06:34 +02:00
|
|
|
|
QString symbolsStr;
|
|
|
|
|
{
|
2023-08-12 09:31:10 +02:00
|
|
|
|
// String with all unique symbols for supported alphabets.
|
|
|
|
|
// See script alphabets.py for generation and more information.
|
|
|
|
|
// Note. MSVC doesn't support normal string concatenation for long string. That's why we use QStringList in this
|
|
|
|
|
// case.
|
|
|
|
|
const QStringList symbols{
|
|
|
|
|
QStringLiteral(
|
|
|
|
|
"ցЀĆЈVӧĎАғΕĖӅИқΝĞơРңњΥĦШҫ̆έجگĮаҳѕεشԶиһνԾрÃυلՆӝшËύՎїPÓՖXӛӟŞãզhëҔծpóӞնxßվāŁЃֆĉЋΊCŬđҐГΒęҘЛΚŘġҠУGا"),
|
|
|
|
|
QStringLiteral("հЫΪŪدԱҰгβطԹõлκKՁÀуςهՉÈыvیՑÐSOřӘћաőcӐթèkàѓżűðsķչøӥӔĀփӣІĈΏЎґĐΗЖҙĘȚΟОҡĠآΧЦتίЮұİزηжԸغẽοоÁՀقχц"),
|
|
|
|
|
QStringLiteral(
|
|
|
|
|
"ÉՈيюÑՐђӋіәťӆўáŠĺѐfөըnñŰӤӨӹոľЁրăЉŭċΌБӸēłΔҖЙŤěΜӜDСձģΤӰЩīņحάҮбưԳصδHйԻŇμӴсՃمτƠщՋόєLQŹՓŕÖYśÞaգĽ"),
|
|
|
|
|
QStringLiteral(
|
|
|
|
|
"æiŽիӓîqճöyջþĂօЄӦΉĊЌΑĒДҗјΙȘĚМΡéĵĢФūӚΩبĪЬүќαذԲдҷιظԺмρՂфÇωوՊьÏՒTŚĻJբdçժlïӪղtպӫAւąЇΆčŃЏΎĕӯЗΖEțŮ"),
|
|
|
|
|
QStringLiteral(
|
|
|
|
|
"ĝПΞأĥĹЧΦثÆӳЯήIسŲԵзζԽпξكՅÄчφNMՍӌяώӢӲՕÔWÎŝÜџёźեägխoӒյôwĶBžսüЂĄև̈ЊČƏљΓВҕĔӮΛКĜΣТҥĤکΫЪƯخγвŅԴϊضλкԼ"),
|
|
|
|
|
QStringLiteral(
|
|
|
|
|
"ĴσтÅՄنϋъÍՌRӕՔZÝŜbåդﻩjíլļrӵմӱzýռپêЅքćچΈЍďΐҒЕůėژșΘØҚНğńءΠFҢХħΨҪЭųįҶرΰҲеԷňعθҺнԿفπÂхՇψÊэšՏÒUəÚѝ"),
|
|
|
|
|
QStringLiteral("ŻşҤӑâeէŐımկòuշÕúտŔ")};
|
|
|
|
|
|
|
|
|
|
symbolsStr = symbols.join(QString());
|
2018-04-22 18:06:34 +02:00
|
|
|
|
}
|
2015-08-09 19:46:21 +02:00
|
|
|
|
|
2017-01-03 10:14:32 +01:00
|
|
|
|
INIT_LOCALE_VARIABLES(m_locale);
|
2017-01-03 10:40:40 +01:00
|
|
|
|
Q_UNUSED(expUpper)
|
|
|
|
|
Q_UNUSED(expLower)
|
2017-07-26 18:28:26 +02:00
|
|
|
|
Q_UNUSED(decimalPoint)
|
|
|
|
|
Q_UNUSED(groupSeparator)
|
2017-01-03 10:14:32 +01:00
|
|
|
|
|
2021-06-11 14:30:06 +02:00
|
|
|
|
// Together with localized symbols, we always must include canonical symbols
|
|
|
|
|
auto AddNotCanonical = [](QString &chars, const QChar &c, const QChar &canonical)
|
|
|
|
|
{
|
|
|
|
|
if (c != canonical)
|
|
|
|
|
{
|
|
|
|
|
chars += c;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 19:46:21 +02:00
|
|
|
|
// Defining identifier character sets
|
2021-06-11 14:30:06 +02:00
|
|
|
|
QString nameChars = QStringLiteral("0123456789\\_@#'") + symbolsStr;
|
|
|
|
|
|
|
|
|
|
AddNotCanonical(nameChars, sign0, '0');
|
|
|
|
|
AddNotCanonical(nameChars, sign1, '1');
|
|
|
|
|
AddNotCanonical(nameChars, sign2, '2');
|
|
|
|
|
AddNotCanonical(nameChars, sign3, '3');
|
|
|
|
|
AddNotCanonical(nameChars, sign4, '4');
|
|
|
|
|
AddNotCanonical(nameChars, sign5, '5');
|
|
|
|
|
AddNotCanonical(nameChars, sign6, '6');
|
|
|
|
|
AddNotCanonical(nameChars, sign7, '7');
|
|
|
|
|
AddNotCanonical(nameChars, sign8, '8');
|
|
|
|
|
AddNotCanonical(nameChars, sign9, '9');
|
|
|
|
|
|
2017-01-03 10:14:32 +01:00
|
|
|
|
DefineNameChars(nameChars);
|
|
|
|
|
|
2021-06-11 14:30:06 +02:00
|
|
|
|
const QString opChars = QStringLiteral("+-*^/?<>=!$%&|~'_");
|
|
|
|
|
|
|
|
|
|
QString oprtChars = symbolsStr + opChars;
|
|
|
|
|
AddNotCanonical(oprtChars, positiveSign, '+');
|
|
|
|
|
AddNotCanonical(oprtChars, negativeSign, '-');
|
2018-04-22 18:06:34 +02:00
|
|
|
|
|
2017-01-03 10:14:32 +01:00
|
|
|
|
DefineOprtChars(oprtChars);
|
|
|
|
|
|
2021-06-11 14:30:06 +02:00
|
|
|
|
QString infixOprtChars = opChars;
|
|
|
|
|
|
|
|
|
|
AddNotCanonical(infixOprtChars, positiveSign, '+');
|
|
|
|
|
AddNotCanonical(infixOprtChars, negativeSign, '-');
|
|
|
|
|
|
2017-01-03 10:14:32 +01:00
|
|
|
|
DefineInfixOprtChars(infixOprtChars);
|
2015-08-09 19:46:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// Factory function for creating new parser variables
|
|
|
|
|
// This could as well be a function performing database queries.
|
2023-05-03 13:07:02 +02:00
|
|
|
|
auto QmuFormulaBase::AddVariable(const QString &a_szName, void *a_pUserData) -> qreal *
|
2015-08-09 19:46:21 +02:00
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(a_szName)
|
|
|
|
|
Q_UNUSED(a_pUserData)
|
|
|
|
|
|
|
|
|
|
static qreal value = 0;
|
|
|
|
|
return &value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief SetSepForTr set separators for translation expression.
|
|
|
|
|
* @param fromUser true if expression come from user (from dialog).
|
|
|
|
|
*/
|
|
|
|
|
void QmuFormulaBase::SetSepForTr(bool osSeparator, bool fromUser)
|
|
|
|
|
{
|
|
|
|
|
if (fromUser)
|
|
|
|
|
{
|
2017-01-03 10:14:32 +01:00
|
|
|
|
const QLocale loc = QLocale();
|
|
|
|
|
setLocale(loc);
|
|
|
|
|
SetArgSep(';');
|
2019-10-30 10:34:01 +01:00
|
|
|
|
setCNumbers(not osSeparator);
|
2015-08-09 19:46:21 +02:00
|
|
|
|
if (osSeparator)
|
|
|
|
|
{
|
2023-02-09 14:42:34 +01:00
|
|
|
|
setDecimalPoint(LocaleDecimalPoint(loc));
|
|
|
|
|
setThousandsSeparator(LocaleGroupSeparator(loc));
|
2015-08-09 19:46:21 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 09:31:10 +02:00
|
|
|
|
SetSepForEval(); // Same separators (internal) as for eval.
|
2015-08-09 19:46:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief SetSepForEval set separators for eval. Each expression eval in internal (C) locale.
|
|
|
|
|
*/
|
|
|
|
|
void QmuFormulaBase::SetSepForEval()
|
|
|
|
|
{
|
|
|
|
|
SetArgSep(';');
|
2017-01-03 10:14:32 +01:00
|
|
|
|
setThousandsSeparator(',');
|
|
|
|
|
setDecimalPoint('.');
|
2015-08-09 19:46:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 12:41:42 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief RemoveAll remove token from token list.
|
|
|
|
|
*
|
|
|
|
|
* Standard Qt class QMap doesn't have method RemoveAll.
|
|
|
|
|
* Example: remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
|
|
|
|
|
*
|
|
|
|
|
* @param map map with tokens
|
|
|
|
|
* @param val token that need delete
|
|
|
|
|
*/
|
2023-08-12 09:31:10 +02:00
|
|
|
|
void QmuFormulaBase::RemoveAll(QMap<qmusizetype, QString> &map, const QString &val)
|
2015-10-15 12:41:42 +02:00
|
|
|
|
{
|
2023-08-12 09:31:10 +02:00
|
|
|
|
const QList<qmusizetype> listKeys = map.keys(val); // Take all keys that contain token.
|
2018-04-03 13:36:38 +02:00
|
|
|
|
for (auto key : listKeys)
|
2015-10-15 12:41:42 +02:00
|
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
|
map.remove(key);
|
2015-10-15 12:41:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 09:31:10 +02:00
|
|
|
|
} // namespace qmu
|