Seamly2D
Code documentation
qmuparsererror.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  **
3  ** Copyright (C) 2013 Ingo Berg
4  **
5  ** Permission is hereby granted, free of charge, to any person obtaining a copy of this
6  ** software and associated documentation files (the "Software"), to deal in the Software
7  ** without restriction, including without limitation the rights to use, copy, modify,
8  ** merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9  ** permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  **
11  ** The above copyright notice and this permission notice shall be included in all copies or
12  ** substantial portions of the Software.
13  **
14  ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15  ** NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17  ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  **
20  ******************************************************************************************************/
21 
22 #ifndef QMUPARSERERROR_H
23 #define QMUPARSERERROR_H
24 
25 #include <qcompilerdetection.h>
26 #include <QCoreApplication>
27 #include <QException>
28 #include <QMap>
29 #include <QString>
30 #include <QVector>
31 #include <QtGlobal>
32 #include <sstream>
33 
34 #include "qmuparserdef.h"
35 #include "qmutranslation.h"
36 
37 /** @file
38  @brief This file defines the error class used by the parser.
39 */
40 
41 namespace qmu
42 {
43 
44 /** @brief Error codes. */
46 {
47  // Formula syntax errors
48  ecUNEXPECTED_OPERATOR = 0, ///< Unexpected binary operator found
49  ecUNASSIGNABLE_TOKEN = 1, ///< Token cant be identified.
50  ecUNEXPECTED_EOF = 2, ///< Unexpected end of formula. (Example: "2+sin(")
51  ecUNEXPECTED_ARG_SEP = 3, ///< An unexpected semicolon has been found. (Example: "1;23")
52  ecUNEXPECTED_ARG = 4, ///< An unexpected argument has been found
53  ecUNEXPECTED_VAL = 5, ///< An unexpected value token has been found
54  ecUNEXPECTED_VAR = 6, ///< An unexpected variable token has been found
55  ecUNEXPECTED_PARENS = 7, ///< Unexpected Parenthesis, opening or closing
56  ecUNEXPECTED_STR = 8, ///< A string has been found at an inapropriate position
57  ecSTRING_EXPECTED = 9, ///< A string function has been called with a different type of argument
58  ecVAL_EXPECTED = 10, ///< A numerical function has been called with a non value type of argument
59  ecMISSING_PARENS = 11, ///< Missing parens. (Example: "3*sin(3")
60  ecUNEXPECTED_FUN = 12, ///< Unexpected function found. (Example: "sin(8)cos(9)")
61  ecUNTERMINATED_STRING = 13, ///< unterminated string constant. (Example: "3*valueof("hello)")
62  ecTOO_MANY_PARAMS = 14, ///< Too many function parameters
63  ecTOO_FEW_PARAMS = 15, ///< Too few function parameters. (Example: "ite(1<2;2)")
64  ecOPRT_TYPE_CONFLICT = 16, ///< binary operators may only be applied to value items of the same type
65  ecSTR_RESULT = 17, ///< result is a string
66 
67  // Invalid Parser input Parameters
68  ecINVALID_NAME = 18, ///< Invalid function, variable or constant name.
69  ecINVALID_BINOP_IDENT = 19, ///< Invalid binary operator identifier
70  ecINVALID_INFIX_IDENT = 20, ///< Invalid function, variable or constant name.
71  ecINVALID_POSTFIX_IDENT = 21, ///< Invalid function, variable or constant name.
72 
73  ecBUILTIN_OVERLOAD = 22, ///< Trying to overload builtin operator
74  ecINVALID_FUN_PTR = 23, ///< Invalid callback function pointer
75  ecINVALID_VAR_PTR = 24, ///< Invalid variable pointer
76  ecEMPTY_EXPRESSION = 25, ///< The Expression is empty
77  ecNAME_CONFLICT = 26, ///< Name conflict
78  ecOPT_PRI = 27, ///< Invalid operator priority
79  //
80  ecDOMAIN_ERROR = 28, ///< catch division by zero, sqrt(-1), log(0) (currently unused)
81  ecDIV_BY_ZERO = 29, ///< Division by zero (currently unused)
82  ecGENERIC = 30, ///< Generic error
83  ecLOCALE = 31, ///< Conflict with current locale
84 
88 
89  // internal errors
90  ecINTERNAL_ERROR = 35, ///< Internal error of any kind.
91 
92  // The last two are special entries
93  ecCOUNT, ///< This is no error code, It just stores just the total number of error codes
94  ecUNDEFINED = -1 ///< Undefined message, placeholder to detect unassigned error messages
95 };
96 
97 //---------------------------------------------------------------------------
98 /** @brief A class that handles the error messages.
99 */
101 {
102 public:
104 
107 
108  static const QmuParserErrorMsg& Instance();
109  QString operator[] ( int a_iIdx ) const;
110 
111 private:
112  Q_DISABLE_COPY(QmuParserErrorMsg)
113  QMap<int, QmuTranslation> m_vErrMsg; ///< A map with the predefined error messages
114  static const self_type m_Instance; ///< The instance pointer
115 };
116 
117 //---------------------------------------------------------------------------------------------------------------------
118 // cppcheck-suppress unusedFunction
120 {
121  return m_Instance;
122 }
123 
124 //---------------------------------------------------------------------------------------------------------------------
125 inline QString QmuParserErrorMsg::operator[] ( int a_iIdx ) const
126 {
127  return m_vErrMsg.value(a_iIdx).translate();
128 }
129 
130 //---------------------------------------------------------------------------
131 /** @brief Error class of the parser.
132  @author Ingo Berg
133 
134  Part of the math parser package.
135 */
136 class QmuParserError : public QException
137 {
138 public:
139  QmuParserError();
140  explicit QmuParserError ( EErrorCodes a_iErrc );
141  explicit QmuParserError ( const QString &sMsg );
142  QmuParserError ( EErrorCodes a_iErrc, const QString &sTok, const QString &sFormula = QString(), int a_iPos = -1 );
143  QmuParserError ( EErrorCodes a_iErrc, int a_iPos, const QString &sTok );
144  QmuParserError ( const QString &szMsg, int iPos, const QString &sTok = QString() );
145  QmuParserError ( const QmuParserError &a_Obj );
146  QmuParserError& operator= ( const QmuParserError &a_Obj );
147  virtual ~QmuParserError() QMUP_NOEXCEPT_EXPR (true) Q_DECL_OVERRIDE {}
148 
149  void SetFormula ( const QString &a_strFormula );
150  const QString& GetExpr() const;
151  const QString& GetMsg() const;
152  int GetPos() const;
153  const QString& GetToken() const;
154  EErrorCodes GetCode() const;
155  Q_NORETURN virtual void raise() const Q_DECL_OVERRIDE;
156  Q_REQUIRED_RESULT virtual QmuParserError *clone() const Q_DECL_OVERRIDE;
157 private:
158  QString m_sMsg; ///< The message string
159  QString m_sExpr; ///< Formula string
160  QString m_sTok; ///< Token related with the error
161  int m_iPos; ///< Formula position related to the error
162  EErrorCodes m_iErrc; ///< Error code
163  const QmuParserErrorMsg &m_ErrMsg;
164  void Reset();
165 };
166 
167 //---------------------------------------------------------------------------------------------------------------------
168 /**
169  * @brief Set the expression related to this error.
170  */
171 inline void QmuParserError::SetFormula ( const QString &a_strFormula )
172 {
173  m_sExpr = a_strFormula;
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 /**
178  * @brief gets the expression related tp this error.
179  */
180 inline const QString& QmuParserError::GetExpr() const
181 {
182  return m_sExpr;
183 }
184 
185 //---------------------------------------------------------------------------------------------------------------------
186 /**
187  * @brief Returns the message string for this error.
188  */
189 inline const QString& QmuParserError::GetMsg() const
190 {
191  return m_sMsg;
192 }
193 
194 //---------------------------------------------------------------------------------------------------------------------
195 /**
196  * @brief Return the formula position related to the error.
197  *
198  * If the error is not related to a distinct position this will return -1
199  */
200 inline int QmuParserError::GetPos() const
201 {
202  return m_iPos;
203 }
204 
205 //---------------------------------------------------------------------------------------------------------------------
206 /**
207  * @brief Return string related with this token (if available).
208  */
209 inline const QString& QmuParserError::GetToken() const
210 {
211  return m_sTok;
212 }
213 
214 //---------------------------------------------------------------------------------------------------------------------
215 /**
216  * @brief Return the error code.
217  */
219 {
220  return m_iErrc;
221 }
222 
223 } // namespace qmu
224 
225 #endif
A class that handles the error messages.
QmuParserErrorMsg self_type
static const QmuParserErrorMsg & Instance()
QString operator[](int a_iIdx) const
QMap< int, QmuTranslation > m_vErrMsg
A map with the predefined error messages.
static const self_type m_Instance
The instance pointer.
Error class of the parser.
EErrorCodes GetCode() const
Return the error code.
virtual ~QmuParserError() Q_DECL_OVERRIDE
const QString & GetMsg() const
Returns the message string for this error.
int GetPos() const
Return the formula position related to the error.
const QString & GetToken() const
Return string related with this token (if available).
const QString & GetExpr() const
gets the expression related tp this error.
Namespace for mathematical applications.
EErrorCodes
Error codes.
@ ecDOMAIN_ERROR
catch division by zero, sqrt(-1), log(0) (currently unused)
@ ecUNDEFINED
Undefined message, placeholder to detect unassigned error messages.
@ ecTOO_FEW_PARAMS
Too few function parameters. (Example: "ite(1<2;2)")
@ ecMISSING_PARENS
Missing parens. (Example: "3*sin(3")
@ ecUNEXPECTED_OPERATOR
Unexpected binary operator found.
@ ecOPRT_TYPE_CONFLICT
binary operators may only be applied to value items of the same type
@ ecINVALID_NAME
Invalid function, variable or constant name.
@ ecLOCALE
Conflict with current locale.
@ ecGENERIC
Generic error.
@ ecUNEXPECTED_STR
A string has been found at an inapropriate position.
@ ecINVALID_INFIX_IDENT
Invalid function, variable or constant name.
@ ecBUILTIN_OVERLOAD
Trying to overload builtin operator.
@ ecUNEXPECTED_EOF
Unexpected end of formula. (Example: "2+sin(")
@ ecINVALID_FUN_PTR
Invalid callback function pointer.
@ ecSTRING_EXPECTED
A string function has been called with a different type of argument.
@ ecNAME_CONFLICT
Name conflict.
@ ecVAL_EXPECTED
A numerical function has been called with a non value type of argument.
@ ecUNEXPECTED_FUN
Unexpected function found. (Example: "sin(8)cos(9)")
@ ecMISSING_ELSE_CLAUSE
@ ecUNASSIGNABLE_TOKEN
Token cant be identified.
@ ecUNEXPECTED_ARG_SEP
An unexpected semicolon has been found. (Example: "1;23")
@ ecUNEXPECTED_ARG
An unexpected argument has been found.
@ ecSTR_RESULT
result is a string
@ ecEMPTY_EXPRESSION
The Expression is empty.
@ ecINVALID_POSTFIX_IDENT
Invalid function, variable or constant name.
@ ecTOO_MANY_PARAMS
Too many function parameters.
@ ecOPT_PRI
Invalid operator priority.
@ ecUNEXPECTED_PARENS
Unexpected Parenthesis, opening or closing.
@ ecINVALID_BINOP_IDENT
Invalid binary operator identifier.
@ ecINTERNAL_ERROR
Internal error of any kind.
@ ecUNEXPECTED_VAR
An unexpected variable token has been found.
@ ecUNEXPECTED_CONDITIONAL
@ ecUNEXPECTED_VAL
An unexpected value token has been found.
@ ecINVALID_VAR_PTR
Invalid variable pointer.
@ ecUNTERMINATED_STRING
unterminated string constant. (Example: "3*valueof("hello)")
@ ecDIV_BY_ZERO
Division by zero (currently unused)
@ ecMISPLACED_COLON
@ ecCOUNT
This is no error code, It just stores just the total number of error codes.
This file contains standard definitions used by the parser.
#define QMUP_NOEXCEPT_EXPR(x)
Definition: qmuparserdef.h:52
The VTranslation class help store string for translation.