Seamly2D
Code documentation
qmuparsererror.cpp
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 #include "qmuparsererror.h"
23 
24 #ifdef Q_OS_WIN
25 #include <assert.h>
26 #endif
27 
28 namespace qmu
29 {
30 const QmuParserErrorMsg QmuParserErrorMsg::m_Instance;
31 
32 //---------------------------------------------------------------------------------------------------------------------
34 {}
35 
36 #define translate(context, source, disambiguation) QmuTranslation::translate((context), (source), (disambiguation))
37 
38 //---------------------------------------------------------------------------------------------------------------------
40  : m_vErrMsg ()
41 {
42  m_vErrMsg.clear();
43 
45  translate("QmuParserErrorMsg", "Unexpected token \"$TOK$\" found at position $POS$.",
46  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
48  translate("QmuParserErrorMsg", "Internal error",
49  "Math parser error messages."));
51  translate("QmuParserErrorMsg", "Invalid function-, variable- or constant name: \"$TOK$\".",
52  "Math parser error messages. Left untouched \"$TOK$\""));
54  translate("QmuParserErrorMsg", "Invalid binary operator identifier: \"$TOK$\".",
55  "Math parser error messages. Left untouched \"$TOK$\""));
57  translate("QmuParserErrorMsg", "Invalid infix operator identifier: \"$TOK$\".",
58  "Math parser error messages. Left untouched \"$TOK$\""));
60  translate("QmuParserErrorMsg", "Invalid postfix operator identifier: \"$TOK$\".",
61  "Math parser error messages. Left untouched \"$TOK$\""));
63  translate("QmuParserErrorMsg", "Invalid pointer to callback function.",
64  "Math parser error messages."));
66  translate("QmuParserErrorMsg", "Expression is empty.",
67  "Math parser error messages."));
69  translate("QmuParserErrorMsg", "Invalid pointer to variable.",
70  "Math parser error messages."));
72  translate("QmuParserErrorMsg", "Unexpected operator \"$TOK$\" found at position $POS$",
73  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
75  translate("QmuParserErrorMsg", "Unexpected end of expression at position $POS$",
76  "Math parser error messages. Left untouched $POS$"));
78  translate("QmuParserErrorMsg", "Unexpected argument separator at position $POS$",
79  "Math parser error messages. Left untouched $POS$"));
81  translate("QmuParserErrorMsg", "Unexpected parenthesis \"$TOK$\" at position $POS$",
82  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
84  translate("QmuParserErrorMsg", "Unexpected function \"$TOK$\" at position $POS$",
85  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
87  translate("QmuParserErrorMsg", "Unexpected value \"$TOK$\" found at position $POS$",
88  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
90  translate("QmuParserErrorMsg", "Unexpected variable \"$TOK$\" found at position $POS$",
91  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
93  translate("QmuParserErrorMsg", "Function arguments used without a function (position: $POS$)",
94  "Math parser error messages. Left untouched $POS$"));
96  translate("QmuParserErrorMsg", "Missing parenthesis",
97  "Math parser error messages."));
99  translate("QmuParserErrorMsg",
100  "Too many parameters for function \"$TOK$\" at expression position $POS$",
101  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
103  translate("QmuParserErrorMsg",
104  "Too few parameters for function \"$TOK$\" at expression position $POS$",
105  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
106  m_vErrMsg.insert(ecDIV_BY_ZERO,
107  translate("QmuParserErrorMsg", "Divide by zero",
108  "Math parser error messages."));
109  m_vErrMsg.insert(ecDOMAIN_ERROR,
110  translate("QmuParserErrorMsg", "Domain error",
111  "Math parser error messages."));
112  m_vErrMsg.insert(ecNAME_CONFLICT,
113  translate("QmuParserErrorMsg", "Name conflict",
114  "Math parser error messages."));
115  m_vErrMsg.insert(ecOPT_PRI,
116  translate("QmuParserErrorMsg",
117  "Invalid value for operator priority (must be greater or equal to zero).",
118  "Math parser error messages."));
120  translate("QmuParserErrorMsg",
121  "user defined binary operator \"$TOK$\" conflicts with a built in operator.",
122  "Math parser error messages. Left untouched \"$TOK$\""));
124  translate("QmuParserErrorMsg", "Unexpected string token found at position $POS$.",
125  "Math parser error messages. Left untouched $POS$"));
127  translate("QmuParserErrorMsg", "Unterminated string starting at position $POS$.",
128  "Math parser error messages. Left untouched $POS$"));
130  translate("QmuParserErrorMsg", "String function called with a non string type of argument.",
131  "Math parser error messages."));
132  m_vErrMsg.insert(ecVAL_EXPECTED,
133  translate("QmuParserErrorMsg", "String value used where a numerical argument is expected.",
134  "Math parser error messages."));
136  translate("QmuParserErrorMsg", "No suitable overload for operator \"$TOK$\" at position $POS$.",
137  "Math parser error messages. Left untouched \"$TOK$\" and $POS$"));
138  m_vErrMsg.insert(ecSTR_RESULT,
139  translate("QmuParserErrorMsg", "Function result is a string.",
140  "Math parser error messages."));
141  m_vErrMsg.insert(ecGENERIC,
142  translate("QmuParserErrorMsg", "Parser error.",
143  "Math parser error messages."));
144  m_vErrMsg.insert(ecLOCALE,
145  translate("QmuParserErrorMsg", "Decimal separator is identic to function argument separator.",
146  "Math parser error messages."));
148  translate("QmuParserErrorMsg", "The \"$TOK$\" operator must be preceded by a closing bracket.",
149  "Math parser error messages. Left untouched \"$TOK$\""));
151  translate("QmuParserErrorMsg", "If-then-else operator is missing an else clause",
152  "Math parser error messages. Do not translate operator name."));
154  translate("QmuParserErrorMsg", "Misplaced colon at position $POS$",
155  "Math parser error messages. Left untouched $POS$"));
156 }
157 
158 #undef translate
159 
160 //---------------------------------------------------------------------------------------------------------------------
161 //
162 // QParserError class
163 //
164 //---------------------------------------------------------------------------------------------------------------------
165 
166 /**
167  * @brief Default constructor.
168  */
170  : QException(), m_sMsg(), m_sExpr(), m_sTok(), m_iPos ( -1 ), m_iErrc ( ecUNDEFINED ),
171  m_ErrMsg ( QmuParserErrorMsg::Instance() )
172 {}
173 
174 //---------------------------------------------------------------------------------------------------------------------
175 /**
176  * @brief This Constructor is used for internal exceptions only.
177  *
178  * It does not contain any information but the error code.
179  */
181  : QException(), m_sMsg(), m_sExpr(), m_sTok(), m_iPos ( -1 ), m_iErrc ( a_iErrc ),
182  m_ErrMsg ( QmuParserErrorMsg::Instance() )
183 {
185  m_sMsg.replace("$POS$", QString().setNum ( m_iPos ));
186  m_sMsg.replace("$TOK$", m_sTok );
187 }
188 
189 //---------------------------------------------------------------------------------------------------------------------
190 /**
191  * @brief Construct an error from a message text.
192  */
193 QmuParserError::QmuParserError ( const QString &sMsg )
194  : QException(), m_sMsg(sMsg), m_sExpr(), m_sTok(), m_iPos ( -1 ), m_iErrc ( ecUNDEFINED ),
195  m_ErrMsg ( QmuParserErrorMsg::Instance() )
196 {}
197 
198 //---------------------------------------------------------------------------------------------------------------------
199 /**
200  * @brief Construct an error object.
201  * @param [in] iErrc the error code.
202  * @param [in] sTok The token string related to this error.
203  * @param [in] sExpr The expression related to the error.
204  * @param [in] iPos the position in the expression where the error occured.
205  */
206 QmuParserError::QmuParserError ( EErrorCodes iErrc, const QString &sTok, const QString &sExpr, int iPos )
207  : QException(), m_sMsg(), m_sExpr ( sExpr ), m_sTok ( sTok ), m_iPos ( iPos ), m_iErrc ( iErrc ),
208  m_ErrMsg ( QmuParserErrorMsg::Instance() )
209 {
211  m_sMsg.replace("$POS$", QString().setNum ( m_iPos ));
212  m_sMsg.replace("$TOK$", m_sTok );
213 }
214 
215 //---------------------------------------------------------------------------------------------------------------------
216 /**
217  * @brief Construct an error object.
218  * @param [in] a_iErrc the error code.
219  * @param [in] a_iPos the position in the expression where the error occured.
220  * @param [in] sTok The token string related to this error.
221  */
222 QmuParserError::QmuParserError ( EErrorCodes a_iErrc, int a_iPos, const QString &sTok )
223  : QException(), m_sMsg(), m_sExpr(), m_sTok ( sTok ), m_iPos ( a_iPos ), m_iErrc ( a_iErrc ),
224  m_ErrMsg ( QmuParserErrorMsg::Instance() )
225 {
227  m_sMsg.replace("$POS$", QString().setNum ( m_iPos ));
228  m_sMsg.replace("$TOK$", m_sTok );
229 }
230 
231 //---------------------------------------------------------------------------------------------------------------------
232 /** @brief Construct an error object.
233  * @param [in] szMsg The error message text.
234  * @param [in] iPos the position related to the error.
235  * @param [in] sTok The token string related to this error.
236  */
237 QmuParserError::QmuParserError ( const QString &szMsg, int iPos, const QString &sTok )
238  : QException(), m_sMsg ( szMsg ), m_sExpr(), m_sTok ( sTok ), m_iPos ( iPos ), m_iErrc ( ecGENERIC ),
239  m_ErrMsg ( QmuParserErrorMsg::Instance() )
240 {
241  m_sMsg.replace("$POS$", QString().setNum ( m_iPos ));
242  m_sMsg.replace("$TOK$", m_sTok );
243 }
244 
245 //---------------------------------------------------------------------------------------------------------------------
246 /**
247  * @brief Copy constructor.
248  */
250  : QException(), m_sMsg ( a_Obj.m_sMsg ), m_sExpr ( a_Obj.m_sExpr ), m_sTok ( a_Obj.m_sTok ),
251  m_iPos ( a_Obj.m_iPos ), m_iErrc ( a_Obj.m_iErrc ), m_ErrMsg ( QmuParserErrorMsg::Instance() )
252 {}
253 
254 //---------------------------------------------------------------------------------------------------------------------
255 /** @brief Assignment operator. */
257 {
258  if ( this == &a_Obj )
259  {
260  return *this;
261  }
262 
263  m_sMsg = a_Obj.m_sMsg;
264  m_sExpr = a_Obj.m_sExpr;
265  m_sTok = a_Obj.m_sTok;
266  m_iPos = a_Obj.m_iPos;
267  m_iErrc = a_Obj.m_iErrc;
268  return *this;
269 }
270 
271 //---------------------------------------------------------------------------------------------------------------------
272 /**
273  * @brief Reset the erro object.
274  */
275 // cppcheck-suppress unusedFunction
277 {
278  m_sMsg.clear();
279  m_sExpr.clear();
280  m_sTok.clear();
281  m_iPos = -1;
283 }
284 
285 //---------------------------------------------------------------------------------------------------------------------
286 /**
287  * @brief raise method raise for exception
288  */
289 Q_NORETURN void QmuParserError::raise() const
290 {
291  throw *this;
292 }
293 
294 //---------------------------------------------------------------------------------------------------------------------
295 /**
296  * @brief clone clone exception
297  * @return new exception
298  */
300 {
301  return new QmuParserError(*this);
302 }
303 
304 } // namespace qmu
A class that handles the error messages.
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.
QString m_sExpr
Formula string.
QString m_sTok
Token related with the error.
int m_iPos
Formula position related to the error.
EErrorCodes m_iErrc
Error code.
QmuParserError()
Default constructor.
virtual Q_REQUIRED_RESULT QmuParserError * clone() const Q_DECL_OVERRIDE
clone clone exception
void Reset()
Reset the erro object.
virtual Q_NORETURN void raise() const Q_DECL_OVERRIDE
raise method raise for exception
QString m_sMsg
The message string.
const QmuParserErrorMsg & m_ErrMsg
QmuParserError & operator=(const QmuParserError &a_Obj)
Assignment operator.
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
#define translate(context, source, disambiguation)
This file defines the error class used by the parser.