2014-04-30 07:32:29 +02:00
|
|
|
/***************************************************************************************************
|
|
|
|
**
|
2015-02-27 11:21:09 +01:00
|
|
|
** Copyright (C) 2013 Ingo Berg
|
2014-04-30 07:32:29 +02:00
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
******************************************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QMUPARSERERROR_H
|
|
|
|
#define QMUPARSERERROR_H
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QCoreApplication>
|
2014-05-06 11:45:21 +02:00
|
|
|
#include <QException>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QMap>
|
|
|
|
#include <QString>
|
2014-06-03 14:15:17 +02:00
|
|
|
#include <QVector>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <sstream>
|
2014-04-30 07:32:29 +02:00
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "qmuparser_global.h"
|
2014-04-30 07:32:29 +02:00
|
|
|
#include "qmuparserdef.h"
|
2014-12-04 18:28:47 +01:00
|
|
|
#include "qmutranslation.h"
|
2014-04-30 07:32:29 +02:00
|
|
|
|
2023-07-15 12:47:14 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
|
2022-08-12 17:50:13 +02:00
|
|
|
#include "../vmisc/defglobal.h"
|
2023-07-15 12:47:14 +02:00
|
|
|
#endif
|
2022-08-12 17:50:13 +02:00
|
|
|
|
2014-04-30 07:32:29 +02:00
|
|
|
/** @file
|
|
|
|
@brief This file defines the error class used by the parser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace qmu
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @brief Error codes. */
|
|
|
|
enum EErrorCodes
|
|
|
|
{
|
|
|
|
// Formula syntax errors
|
2023-07-13 16:49:20 +02:00
|
|
|
ecUNEXPECTED_OPERATOR = 0, ///< Unexpected binary operator found
|
|
|
|
ecUNASSIGNABLE_TOKEN = 1, ///< Token cant be identified.
|
|
|
|
ecUNEXPECTED_EOF = 2, ///< Unexpected end of formula. (Example: "2+sin(")
|
|
|
|
ecUNEXPECTED_ARG_SEP = 3, ///< An unexpected semicolon has been found. (Example: "1;23")
|
|
|
|
ecUNEXPECTED_ARG = 4, ///< An unexpected argument has been found
|
|
|
|
ecUNEXPECTED_VAL = 5, ///< An unexpected value token has been found
|
|
|
|
ecUNEXPECTED_VAR = 6, ///< An unexpected variable token has been found
|
|
|
|
ecUNEXPECTED_PARENS = 7, ///< Unexpected Parenthesis, opening or closing
|
|
|
|
ecUNEXPECTED_STR = 8, ///< A string has been found at an inapropriate position
|
|
|
|
ecSTRING_EXPECTED = 9, ///< A string function has been called with a different type of argument
|
|
|
|
ecVAL_EXPECTED = 10, ///< A numerical function has been called with a non value type of argument
|
|
|
|
ecMISSING_PARENS = 11, ///< Missing parens. (Example: "3*sin(3")
|
|
|
|
ecUNEXPECTED_FUN = 12, ///< Unexpected function found. (Example: "sin(8)cos(9)")
|
|
|
|
ecUNTERMINATED_STRING = 13, ///< unterminated string constant. (Example: "3*valueof("hello)")
|
|
|
|
ecTOO_MANY_PARAMS = 14, ///< Too many function parameters
|
|
|
|
ecTOO_FEW_PARAMS = 15, ///< Too few function parameters. (Example: "ite(1<2;2)")
|
|
|
|
ecOPRT_TYPE_CONFLICT = 16, ///< binary operators may only be applied to value items of the same type
|
|
|
|
ecSTR_RESULT = 17, ///< result is a string
|
2014-04-30 07:32:29 +02:00
|
|
|
|
|
|
|
// Invalid Parser input Parameters
|
2023-07-13 16:49:20 +02:00
|
|
|
ecINVALID_NAME = 18, ///< Invalid function, variable or constant name.
|
|
|
|
ecINVALID_BINOP_IDENT = 19, ///< Invalid binary operator identifier
|
|
|
|
ecINVALID_INFIX_IDENT = 20, ///< Invalid function, variable or constant name.
|
|
|
|
ecINVALID_POSTFIX_IDENT = 21, ///< Invalid function, variable or constant name.
|
|
|
|
|
|
|
|
ecBUILTIN_OVERLOAD = 22, ///< Trying to overload builtin operator
|
|
|
|
ecINVALID_FUN_PTR = 23, ///< Invalid callback function pointer
|
|
|
|
ecINVALID_VAR_PTR = 24, ///< Invalid variable pointer
|
|
|
|
ecEMPTY_EXPRESSION = 25, ///< The Expression is empty
|
|
|
|
ecNAME_CONFLICT = 26, ///< Name conflict
|
|
|
|
ecOPT_PRI = 27, ///< Invalid operator priority
|
2023-07-15 12:47:14 +02:00
|
|
|
//
|
|
|
|
ecDOMAIN_ERROR = 28, ///< catch division by zero, sqrt(-1), log(0) (currently unused)
|
|
|
|
ecDIV_BY_ZERO = 29, ///< Division by zero (currently unused)
|
|
|
|
ecGENERIC = 30, ///< Generic error
|
|
|
|
ecLOCALE = 31, ///< Conflict with current locale
|
2014-04-30 07:32:29 +02:00
|
|
|
|
|
|
|
ecUNEXPECTED_CONDITIONAL = 32,
|
2023-07-13 16:49:20 +02:00
|
|
|
ecMISSING_ELSE_CLAUSE = 33,
|
|
|
|
ecMISPLACED_COLON = 34,
|
2014-04-30 07:32:29 +02:00
|
|
|
|
|
|
|
// internal errors
|
2023-07-13 16:49:20 +02:00
|
|
|
ecINTERNAL_ERROR = 35, ///< Internal error of any kind.
|
2014-04-30 07:32:29 +02:00
|
|
|
|
|
|
|
// The last two are special entries
|
2023-07-13 16:49:20 +02:00
|
|
|
ecCOUNT, ///< This is no error code, It just stores just the total number of error codes
|
|
|
|
ecUNDEFINED = -1 ///< Undefined message, placeholder to detect unassigned error messages
|
2014-04-30 07:32:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
/** @brief A class that handles the error messages.
|
2023-07-13 16:49:20 +02:00
|
|
|
*/
|
2015-10-18 09:50:33 +02:00
|
|
|
class QMUPARSERSHARED_EXPORT QmuParserErrorMsg
|
2014-04-30 07:32:29 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-05-01 13:33:40 +02:00
|
|
|
typedef QmuParserErrorMsg self_type;
|
2014-04-30 07:32:29 +02:00
|
|
|
|
2014-05-01 13:33:40 +02:00
|
|
|
QmuParserErrorMsg();
|
|
|
|
~QmuParserErrorMsg();
|
2014-04-30 07:32:29 +02:00
|
|
|
|
2023-05-03 13:07:02 +02:00
|
|
|
static auto Instance() -> const QmuParserErrorMsg &;
|
|
|
|
auto operator[](int a_iIdx) const -> QString;
|
2014-04-30 07:32:29 +02:00
|
|
|
|
|
|
|
private:
|
2022-08-12 17:50:13 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
|
|
|
Q_DISABLE_COPY_MOVE(QmuParserErrorMsg) // NOLINT
|
2023-07-13 16:49:20 +02:00
|
|
|
QMap<int, QmuTranslation> m_vErrMsg; ///< A map with the predefined error messages
|
2014-04-30 07:32:29 +02:00
|
|
|
};
|
|
|
|
|
2014-05-06 16:36:42 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
// cppcheck-suppress unusedFunction
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserErrorMsg::Instance() -> const QmuParserErrorMsg &
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
2023-07-25 13:01:41 +02:00
|
|
|
const static QmuParserErrorMsg instance;
|
|
|
|
return instance;
|
2014-05-06 16:36:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserErrorMsg::operator[](int a_iIdx) const -> QString
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
2018-01-21 15:00:53 +01:00
|
|
|
return m_vErrMsg.value(a_iIdx).translate(QString());
|
2014-05-06 16:36:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 12:18:38 +01:00
|
|
|
class QMUPARSERSHARED_EXPORT QmuParserWarning : public QException
|
|
|
|
{
|
|
|
|
public:
|
2023-07-13 16:49:20 +02:00
|
|
|
explicit QmuParserWarning(const QString &sMsg);
|
|
|
|
QmuParserWarning(const QmuParserWarning &a_Obj);
|
2023-05-03 13:07:02 +02:00
|
|
|
auto operator=(const QmuParserWarning &a_Obj) -> QmuParserWarning &;
|
2022-08-08 14:25:14 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
2023-07-13 16:49:20 +02:00
|
|
|
virtual ~QmuParserWarning() QMUP_NOEXCEPT_EXPR(true) override {}
|
2020-11-20 12:18:38 +01:00
|
|
|
|
2023-05-03 13:07:02 +02:00
|
|
|
auto GetMsg() const -> const QString &;
|
2020-11-20 12:18:38 +01:00
|
|
|
|
2023-07-13 16:49:20 +02:00
|
|
|
Q_NORETURN virtual void raise() const override;
|
2023-05-03 13:07:02 +02:00
|
|
|
Q_REQUIRED_RESULT virtual auto clone() const -> QmuParserWarning * override;
|
|
|
|
|
2020-11-20 12:18:38 +01:00
|
|
|
private:
|
2023-07-13 16:49:20 +02:00
|
|
|
QString m_sMsg; ///< The message string
|
2020-11-20 12:18:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief Returns the message string for this error.
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserWarning::GetMsg() const -> const QString &
|
2020-11-20 12:18:38 +01:00
|
|
|
{
|
|
|
|
return m_sMsg;
|
|
|
|
}
|
|
|
|
|
2014-04-30 07:32:29 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
/** @brief Error class of the parser.
|
|
|
|
@author Ingo Berg
|
|
|
|
|
|
|
|
Part of the math parser package.
|
|
|
|
*/
|
2014-05-06 11:45:21 +02:00
|
|
|
class QMUPARSERSHARED_EXPORT QmuParserError : public QException
|
2014-04-30 07:32:29 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-05-01 13:33:40 +02:00
|
|
|
QmuParserError();
|
2023-07-13 16:49:20 +02:00
|
|
|
explicit QmuParserError(EErrorCodes a_iErrc);
|
|
|
|
explicit QmuParserError(const QString &sMsg);
|
|
|
|
QmuParserError(EErrorCodes a_iErrc, const QString &sTok, const QString &sFormula = QString(),
|
|
|
|
qmusizetype a_iPos = -1);
|
|
|
|
QmuParserError(EErrorCodes a_iErrc, int a_iPos, const QString &sTok);
|
|
|
|
QmuParserError(const QString &szMsg, int iPos, const QString &sTok = QString());
|
|
|
|
QmuParserError(const QmuParserError &a_Obj);
|
2023-05-03 13:07:02 +02:00
|
|
|
auto operator=(const QmuParserError &a_Obj) -> QmuParserError &;
|
2023-07-13 16:49:20 +02:00
|
|
|
virtual ~QmuParserError() QMUP_NOEXCEPT_EXPR(true) override {}
|
2014-05-01 13:33:40 +02:00
|
|
|
|
2023-07-13 16:49:20 +02:00
|
|
|
void SetFormula(const QString &a_strFormula);
|
2023-05-03 13:07:02 +02:00
|
|
|
auto GetExpr() const -> const QString &;
|
|
|
|
auto GetMsg() const -> const QString &;
|
|
|
|
auto GetPos() const -> qmusizetype;
|
|
|
|
auto GetToken() const -> const QString &;
|
|
|
|
auto GetCode() const -> EErrorCodes;
|
2023-07-13 16:49:20 +02:00
|
|
|
Q_NORETURN virtual void raise() const override;
|
2023-05-03 13:07:02 +02:00
|
|
|
Q_REQUIRED_RESULT virtual auto clone() const -> QmuParserError * override;
|
|
|
|
|
2014-04-30 07:32:29 +02:00
|
|
|
private:
|
2014-05-05 09:29:14 +02:00
|
|
|
QString m_sMsg; ///< The message string
|
|
|
|
QString m_sExpr; ///< Formula string
|
|
|
|
QString m_sTok; ///< Token related with the error
|
2023-07-13 16:49:20 +02:00
|
|
|
qmusizetype m_iPos; ///< Formula position related to the error
|
2014-05-05 09:29:14 +02:00
|
|
|
EErrorCodes m_iErrc; ///< Error code
|
2014-05-01 13:33:40 +02:00
|
|
|
const QmuParserErrorMsg &m_ErrMsg;
|
|
|
|
void Reset();
|
2014-04-30 07:32:29 +02:00
|
|
|
};
|
|
|
|
|
2014-05-06 16:36:42 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief Set the expression related to this error.
|
|
|
|
*/
|
2023-07-13 16:49:20 +02:00
|
|
|
inline void QmuParserError::SetFormula(const QString &a_strFormula)
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
|
|
|
m_sExpr = a_strFormula;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief gets the expression related tp this error.
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserError::GetExpr() const -> const QString &
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
|
|
|
return m_sExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief Returns the message string for this error.
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserError::GetMsg() const -> const QString &
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
|
|
|
return m_sMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief Return the formula position related to the error.
|
|
|
|
*
|
|
|
|
* If the error is not related to a distinct position this will return -1
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserError::GetPos() const -> qmusizetype
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
|
|
|
return m_iPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief Return string related with this token (if available).
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserError::GetToken() const -> const QString &
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
|
|
|
return m_sTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief Return the error code.
|
|
|
|
*/
|
2023-05-03 13:07:02 +02:00
|
|
|
inline auto QmuParserError::GetCode() const -> EErrorCodes
|
2014-05-06 16:36:42 +02:00
|
|
|
{
|
|
|
|
return m_iErrc;
|
|
|
|
}
|
|
|
|
|
2014-04-30 07:32:29 +02:00
|
|
|
} // namespace qmu
|
|
|
|
|
|
|
|
#endif
|