NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
muParserError.cpp
Go to the documentation of this file.
1/*
2 __________
3 _____ __ __\______ \_____ _______ ______ ____ _______
4 / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5 | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6 |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7 \/ \/ \/ \/
8 Copyright (C) 2011 Ingo Berg
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy of this
11 software and associated documentation files (the "Software"), to deal in the Software
12 without restriction, including without limitation the rights to use, copy, modify,
13 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all copies or
17 substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24*/
25#include "muParserError.h"
26
27
28namespace mu
29{
30 const ParserErrorMsg ParserErrorMsg::m_Instance;
31
32 //------------------------------------------------------------------------------
34 {
35 return m_Instance;
36 }
37
38 //------------------------------------------------------------------------------
40 {
41 return (a_iIdx<m_vErrMsg.size()) ? m_vErrMsg[a_iIdx] : string_type();
42 }
43
44 //---------------------------------------------------------------------------
46 {}
47
48 //---------------------------------------------------------------------------
52 {
53 assert(false);
54 return *this;
55 }
56
57 //---------------------------------------------------------------------------
59 {}
60
61 //---------------------------------------------------------------------------
63 :m_vErrMsg(0)
64 {
65 m_vErrMsg.resize(ecCOUNT);
66
67 m_vErrMsg[ecUNASSIGNABLE_TOKEN] = "ERR_MUP_UNASSIGNABLE_TOKEN";
68 m_vErrMsg[ecINTERNAL_ERROR] = "ERR_MUP_INTERNAL_ERROR";
69 m_vErrMsg[ecINVALID_NAME] = "ERR_MUP_INVALID_NAME";
70 m_vErrMsg[ecINVALID_BINOP_IDENT] = "ERR_MUP_INVALID_BINOP_IDENT";
71 m_vErrMsg[ecINVALID_INFIX_IDENT] = "ERR_MUP_INVALID_INFIX_IDENT";
72 m_vErrMsg[ecINVALID_POSTFIX_IDENT] = "ERR_MUP_INVALID_POSTFIX_IDENT";
73 m_vErrMsg[ecINVALID_FUN_PTR] = "ERR_MUP_INVALID_FUN_PTR";
74 m_vErrMsg[ecEMPTY_EXPRESSION] = "ERR_MUP_EMPTY_EXPRESSION";
75 m_vErrMsg[ecINVALID_VAR_PTR] = "ERR_MUP_INVALID_VAR_PTR";
76 m_vErrMsg[ecUNEXPECTED_OPERATOR] = "ERR_MUP_UNEXPECTED_OPERATOR";
77 m_vErrMsg[ecUNEXPECTED_EOF] = "ERR_MUP_UNEXPECTED_EOF";
78 m_vErrMsg[ecUNEXPECTED_ARG_SEP] = "ERR_MUP_UNEXPECTED_ARG_SEP";
79 m_vErrMsg[ecUNEXPECTED_PARENS] = "ERR_MUP_UNEXPECTED_PARENS";
80 m_vErrMsg[ecUNEXPECTED_FUN] = "ERR_MUP_UNEXPECTED_FUN";
81 m_vErrMsg[ecUNEXPECTED_VAL] = "ERR_MUP_UNEXPECTED_VAL";
82 m_vErrMsg[ecUNEXPECTED_VAR] = "ERR_MUP_UNEXPECTED_VAR";
83 m_vErrMsg[ecUNEXPECTED_ARG] = "ERR_MUP_UNEXPECTED_ARG";
84 m_vErrMsg[ecMISSING_PARENS] = "ERR_MUP_MISSING_PARENS";
85 m_vErrMsg[ecTOO_MANY_PARAMS] = "ERR_MUP_TOO_MANY_PARAMS";
86 m_vErrMsg[ecTOO_FEW_PARAMS] = "ERR_MUP_TOO_FEW_PARAMS";
87 m_vErrMsg[ecDIV_BY_ZERO] = "ERR_MUP_DIV_BY_ZERO";
88 m_vErrMsg[ecDOMAIN_ERROR] = "ERR_MUP_DOMAIN_ERROR";
89 m_vErrMsg[ecNAME_CONFLICT] = "ERR_MUP_NAME_CONFLICT";
90 m_vErrMsg[ecOPT_PRI] = "ERR_MUP_OPT_PRI";
91 m_vErrMsg[ecBUILTIN_OVERLOAD] = "ERR_MUP_BUILTIN_OVERLOAD";
92 m_vErrMsg[ecUNEXPECTED_STR] = "ERR_MUP_UNEXPECTED_STR";
93 m_vErrMsg[ecUNTERMINATED_STRING] = "ERR_MUP_UNTERMINATED_STRING";
94 m_vErrMsg[ecSTRING_EXPECTED] = "ERR_MUP_STRING_EXPECTED";
95 m_vErrMsg[ecVAL_EXPECTED] = "ERR_MUP_VAL_EXPECTED";
96 m_vErrMsg[ecOPRT_TYPE_CONFLICT] = "ERR_MUP_OPRT_TYPE_CONFLICT";
97 m_vErrMsg[ecSTR_RESULT] = "ERR_MUP_STR_RESULT";
98 m_vErrMsg[ecGENERIC] = "ERR_MUP_GENERIC";
99 m_vErrMsg[ecLOCALE] = "ERR_MUP_LOCALE";
100 m_vErrMsg[ecUNEXPECTED_CONDITIONAL] = "ERR_MUP_UNEXPECTED_CONDITIONAL";
101 m_vErrMsg[ecMISSING_ELSE_CLAUSE] = "ERR_MUP_MISSING_ELSE_CLAUSE";
102 m_vErrMsg[ecMISPLACED_COLON] = "ERR_MUP_MISPLACED_COLON";
103
104 #if defined(_DEBUG)
105 for (int i=0; i<ecCOUNT; ++i)
106 if (!m_vErrMsg[i].length())
107 assert(false);
108 #endif
109 }
110
111 //---------------------------------------------------------------------------
112 //
113 // ParserError class
114 //
115 //---------------------------------------------------------------------------
116
119 :m_strMsg()
120 ,m_strFormula()
121 ,m_strTok()
122 ,m_iPos(-1)
123 ,m_iErrc(ecUNDEFINED)
124 ,m_ErrMsg(ParserErrorMsg::Instance())
125 {
126 }
127
128 //------------------------------------------------------------------------------
134 :m_ErrMsg(ParserErrorMsg::Instance())
135 {
136 Reset();
137 m_strMsg = _nrT("parser error");
138 }
139
140 //------------------------------------------------------------------------------
143 :m_ErrMsg(ParserErrorMsg::Instance())
144 {
145 Reset();
146 m_strMsg = sMsg;
147 }
148
149 //------------------------------------------------------------------------------
157 const string_type &sTok,
158 const string_type &sExpr,
159 int iPos )
160 :m_strMsg()
161 ,m_strFormula(sExpr)
162 ,m_strTok(sTok)
163 ,m_iPos(iPos)
164 ,m_iErrc(iErrc)
165 ,m_ErrMsg(ParserErrorMsg::Instance())
166 {
168 stringstream_type stream;
169 stream << (int)m_iPos;
170 ReplaceSubString(m_strMsg, _nrT("$POS$"), stream.str());
172 }
173
174 //------------------------------------------------------------------------------
181 :m_strMsg()
182 ,m_strFormula()
183 ,m_strTok(sTok)
184 ,m_iPos(iPos)
185 ,m_iErrc(iErrc)
186 ,m_ErrMsg(ParserErrorMsg::Instance())
187 {
189 stringstream_type stream;
190 stream << (int)m_iPos;
191 ReplaceSubString(m_strMsg, _nrT("$POS$"), stream.str());
193 }
194
195 //------------------------------------------------------------------------------
201 ParserError::ParserError(const char_type *szMsg, int iPos, const string_type &sTok)
202 :m_strMsg(szMsg)
203 ,m_strFormula()
204 ,m_strTok(sTok)
205 ,m_iPos(iPos)
206 ,m_iErrc(ecGENERIC)
207 ,m_ErrMsg(ParserErrorMsg::Instance())
208 {
209 stringstream_type stream;
210 stream << (int)m_iPos;
211 ReplaceSubString(m_strMsg, _nrT("$POS$"), stream.str());
213 }
214
215 //------------------------------------------------------------------------------
218 :m_strMsg(a_Obj.m_strMsg)
219 ,m_strFormula(a_Obj.m_strFormula)
220 ,m_strTok(a_Obj.m_strTok)
221 ,m_iPos(a_Obj.m_iPos)
222 ,m_iErrc(a_Obj.m_iErrc)
223 ,m_ErrMsg(ParserErrorMsg::Instance())
224 {
225 }
226
227 //------------------------------------------------------------------------------
230 {
231 if (this==&a_Obj)
232 return *this;
233
234 m_strMsg = a_Obj.m_strMsg;
236 m_strTok = a_Obj.m_strTok;
237 m_iPos = a_Obj.m_iPos;
238 m_iErrc = a_Obj.m_iErrc;
239 return *this;
240 }
241
242 //------------------------------------------------------------------------------
244 {}
245
246 //------------------------------------------------------------------------------
252 const string_type &strFind,
253 const string_type &strReplaceWith)
254 {
255 string_type strResult;
256 string_type::size_type iPos(0), iNext(0);
257
258 for(;;)
259 {
260 iNext = strSource.find(strFind, iPos);
261 strResult.append(strSource, iPos, iNext-iPos);
262
263 if( iNext==string_type::npos )
264 break;
265
266 strResult.append(strReplaceWith);
267 iPos = iNext + strFind.length();
268 }
269
270 strSource.swap(strResult);
271 }
272
273 //------------------------------------------------------------------------------
276 {
277 m_strMsg = _nrT("");
278 m_strFormula = _nrT("");
279 m_strTok = _nrT("");
280 m_iPos = -1;
282 }
283
284 //------------------------------------------------------------------------------
286 void ParserError::SetFormula(const string_type &a_strFormula)
287 {
288 m_strFormula = a_strFormula;
289 }
290
291 //------------------------------------------------------------------------------
294 {
295 return m_strFormula;
296 }
297
298 //------------------------------------------------------------------------------
301 {
302 return m_strMsg;
303 }
304
305 //------------------------------------------------------------------------------
310 std::size_t ParserError::GetPos() const
311 {
312 return m_iPos;
313 }
314
315 //------------------------------------------------------------------------------
318 {
319 return m_strTok;
320 }
321
322 //------------------------------------------------------------------------------
325 {
326 return m_iErrc;
327 }
328} // namespace mu
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
Definition: language.cpp:292
Error class of the parser.
EErrorCodes GetCode() const
Return the error code.
void SetFormula(const string_type &a_strFormula)
Set the expression related to this error.
std::size_t GetPos() const
Return the formula position related to the error.
string_type m_strMsg
The message string.
void ReplaceSubString(string_type &strSource, const string_type &strFind, const string_type &strReplaceWith)
Replace all ocuurences of a substring with another string.
const ParserErrorMsg & m_ErrMsg
string_type m_strTok
Token related with the error.
int m_iPos
Formula position related to the error.
string_type m_strFormula
Formula string.
EErrorCodes m_iErrc
Error code.
const string_type & GetMsg() const
Returns the message string for this error.
const string_type & GetExpr() const
gets the expression related tp this error.
const string_type & GetToken() const
Return string related with this token (if available).
ParserError()
Default constructor.
void Reset()
Reset the erro object.
ParserError & operator=(const ParserError &a_Obj)
Assignment operator.
A class that handles the error messages.
ParserErrorMsg & operator=(const ParserErrorMsg &)
Assignement operator is deactivated.
static const self_type m_Instance
The instance pointer.
string_type operator[](unsigned a_iIdx) const
static const ParserErrorMsg & Instance()
std::vector< string_type > m_vErrMsg
A vector with the predefined error messages.
Language _lang
Definition: kernel.cpp:39
#define _nrT(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:62
This file defines the error class used by the parser.
Namespace for mathematical applications.
Definition: muParser.cpp:53
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:263
std::basic_stringstream< char_type, std::char_traits< char_type >, std::allocator< char_type > > stringstream_type
Typedef for easily using stringstream that respect the parser stringtype.
Definition: muParserDef.h:268
EErrorCodes
Error codes.
Definition: muParserError.h:50
@ ecINVALID_NAME
Invalid function, variable or constant name.
Definition: muParserError.h:72
@ ecINVALID_BINOP_IDENT
Invalid binary operator identifier.
Definition: muParserError.h:73
@ ecMISPLACED_COLON
Definition: muParserError.h:91
@ ecUNASSIGNABLE_TOKEN
Token cant be identified.
Definition: muParserError.h:53
@ ecMISSING_PARENS
Missing parens. (Example: "3*sin(3")
Definition: muParserError.h:63
@ ecBUILTIN_OVERLOAD
Trying to overload builtin operator.
Definition: muParserError.h:77
@ ecUNEXPECTED_EOF
Unexpected end of formula. (Example: "2+sin(")
Definition: muParserError.h:54
@ ecUNEXPECTED_VAR
An unexpected variable token has been found.
Definition: muParserError.h:58
@ ecNAME_CONFLICT
Name conflict.
Definition: muParserError.h:81
@ ecSTRING_EXPECTED
A string function has been called with a different type of argument.
Definition: muParserError.h:61
@ ecTOO_MANY_PARAMS
Too many function parameters.
Definition: muParserError.h:66
@ ecUNDEFINED
Undefined message, placeholder to detect unassigned error messages.
Definition: muParserError.h:98
@ ecVAL_EXPECTED
A numerical function has been called with a non value type of argument.
Definition: muParserError.h:62
@ ecUNEXPECTED_FUN
Unexpected function found. (Example: "sin(8)cos(9)")
Definition: muParserError.h:64
@ ecSTR_RESULT
result is a string
Definition: muParserError.h:69
@ ecUNEXPECTED_ARG
An unexpected argument has been found.
Definition: muParserError.h:56
@ ecUNEXPECTED_OPERATOR
Unexpected binary operator found.
Definition: muParserError.h:52
@ ecINVALID_INFIX_IDENT
Invalid function, variable or constant name.
Definition: muParserError.h:74
@ ecOPRT_TYPE_CONFLICT
binary operators may only be applied to value items of the same type
Definition: muParserError.h:68
@ ecLOCALE
Conflict with current locale.
Definition: muParserError.h:87
@ ecUNEXPECTED_STR
A string has been found at an inapropriate position.
Definition: muParserError.h:60
@ ecUNEXPECTED_PARENS
Unexpected Parenthesis, opening or closing.
Definition: muParserError.h:59
@ ecINTERNAL_ERROR
Internal error of any kind.
Definition: muParserError.h:94
@ ecDOMAIN_ERROR
catch division by zero, sqrt(-1), log(0) (currently unused)
Definition: muParserError.h:84
@ ecINVALID_VAR_PTR
Invalid variable pointer.
Definition: muParserError.h:79
@ ecUNEXPECTED_ARG_SEP
An unexpected comma has been found. (Example: "1,23")
Definition: muParserError.h:55
@ ecMISSING_ELSE_CLAUSE
Definition: muParserError.h:90
@ ecOPT_PRI
Invalid operator priority.
Definition: muParserError.h:82
@ ecUNTERMINATED_STRING
unterminated string constant. (Example: "3*valueof("hello)")
Definition: muParserError.h:65
@ ecUNEXPECTED_CONDITIONAL
Definition: muParserError.h:89
@ ecINVALID_FUN_PTR
Invalid callback function pointer.
Definition: muParserError.h:78
@ ecTOO_FEW_PARAMS
Too few function parameters. (Example: "ite(1<2,2)")
Definition: muParserError.h:67
@ ecCOUNT
This is no error code, It just stores just the total number of error codes.
Definition: muParserError.h:97
@ ecINVALID_POSTFIX_IDENT
Invalid function, variable or constant name.
Definition: muParserError.h:75
@ ecEMPTY_EXPRESSION
The Expression is empty.
Definition: muParserError.h:80
@ ecGENERIC
Generic error.
Definition: muParserError.h:86
@ ecUNEXPECTED_VAL
An unexpected value token has been found.
Definition: muParserError.h:57
@ ecDIV_BY_ZERO
Division by zero (currently unused)
Definition: muParserError.h:85
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:257