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 "qmutokenparser.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QMap>
|
|
|
|
|
|
|
|
#include "qmuparsererror.h"
|
|
|
|
|
2015-08-09 19:46:21 +02:00
|
|
|
namespace qmu
|
|
|
|
{
|
|
|
|
|
2015-10-15 12:41:42 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QmuTokenParser::QmuTokenParser()
|
|
|
|
{
|
2017-01-03 10:14:32 +01:00
|
|
|
InitCharSets();
|
2015-10-15 12:41:42 +02:00
|
|
|
}
|
|
|
|
|
2015-08-09 19:46:21 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief QmuTokenParser class constructor. Make easy initialization math parser.
|
|
|
|
*
|
|
|
|
* This constructor hide initialization variables, operators, character sets.
|
|
|
|
* Use this constuctor to get tokens from formula. All formulas must be converted to external look.
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* QmuTokenParser *cal = new QmuTokenParser(formula, osSeparator, false);
|
|
|
|
* tokens = cal->GetTokens();
|
|
|
|
* numbers = cal->GetNumbers();
|
|
|
|
* delete cal;
|
|
|
|
*
|
|
|
|
* @param formula string with formula.
|
|
|
|
* @param fromUser true if we parse formula from user
|
2017-12-04 16:12:51 +01:00
|
|
|
* @param translatedFunctions relations between translated function names and original. Contains only translated unique
|
|
|
|
* names.
|
2015-08-09 19:46:21 +02:00
|
|
|
*/
|
2017-12-04 10:44:29 +01:00
|
|
|
QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator,
|
|
|
|
bool fromUser, const QMap<QString, QString> &translatedFunctions)
|
2015-08-09 19:46:21 +02:00
|
|
|
{
|
2017-01-03 10:14:32 +01:00
|
|
|
InitCharSets();
|
2015-08-09 19:46:21 +02:00
|
|
|
SetVarFactory(AddVariable, this);
|
|
|
|
SetSepForTr(osSeparator, fromUser);
|
|
|
|
|
2020-11-20 12:18:38 +01:00
|
|
|
DefineFun(QStringLiteral("warning"), Warning);
|
|
|
|
|
2017-12-04 10:44:29 +01:00
|
|
|
// Fix for issue #776. Valentina cannot recognize translated functions.
|
|
|
|
QMap<QString, QString>::const_iterator i = translatedFunctions.constBegin();
|
|
|
|
while (i != translatedFunctions.constEnd())
|
|
|
|
{
|
|
|
|
if (i.key() != i.value())
|
|
|
|
{
|
|
|
|
auto search = m_FunDef.find(i.value());
|
|
|
|
if(search != m_FunDef.end())
|
|
|
|
{
|
|
|
|
AddCallback(i.key(), search->second, m_FunDef, ValidNameChars());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2015-08-09 19:46:21 +02:00
|
|
|
SetExpr(formula);
|
|
|
|
//Need run for making tokens. Don't catch exception here, because we want know if formula has error.
|
2018-01-24 21:08:53 +01:00
|
|
|
m_pTokenReader->IgnoreUndefVar(true);
|
2015-08-09 19:46:21 +02:00
|
|
|
Eval();
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:41:42 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief IsSingle test formula and return true if it contain only one number.
|
|
|
|
*
|
|
|
|
* Work only with expressions in internal (C) locale.
|
|
|
|
* @param formula expression for test
|
|
|
|
* @return true if fomula has single number
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
auto QmuTokenParser::IsSingle(const QString &formula) -> bool
|
2015-10-15 12:41:42 +02:00
|
|
|
{
|
2018-01-20 14:36:50 +01:00
|
|
|
QLocale c(QLocale::C);
|
|
|
|
bool ok = false;
|
|
|
|
c.toDouble(formula, &ok);
|
|
|
|
return ok;
|
2015-10-15 12:41:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 12:18:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto QmuTokenParser::Warning(const QString &warningMsg, qreal value) -> qreal
|
2020-11-20 12:18:38 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(warningMsg);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-08-09 19:46:21 +02:00
|
|
|
}// namespace qmu
|