2
Copyright (C) 2004, 2005 Ingo Berg
4
Permission is hereby granted, free of charge, to any person obtaining a copy of this
5
software and associated documentation files (the "Software"), to deal in the Software
6
without restriction, including without limitation the rights to use, copy, modify,
7
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8
permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
The above copyright notice and this permission notice shall be included in all copies or
11
substantial portions of the Software.
13
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
#include "muParserError.h"
24
const ParserErrorMsg ParserErrorMsg::m_Instance;
26
//------------------------------------------------------------------------------
27
const ParserErrorMsg& ParserErrorMsg::Instance()
32
//------------------------------------------------------------------------------
33
string_type ParserErrorMsg::operator[](unsigned a_iIdx) const
35
return (a_iIdx<m_vErrMsg.size()) ? m_vErrMsg[a_iIdx] : string_type();
39
//---------------------------------------------------------------------------
40
ParserErrorMsg::~ParserErrorMsg()
43
//---------------------------------------------------------------------------
44
/** \brief Assignement operator is deactivated.
46
ParserErrorMsg& ParserErrorMsg::operator=(const ParserErrorMsg& )
52
//---------------------------------------------------------------------------
53
ParserErrorMsg::ParserErrorMsg(const ParserErrorMsg&)
56
//---------------------------------------------------------------------------
57
ParserErrorMsg::ParserErrorMsg()
60
m_vErrMsg.resize(ecCOUNT);
62
m_vErrMsg[ecUNASSIGNABLE_TOKEN] = "Undefined token \"$TOK$\" found at position $POS$.";
63
m_vErrMsg[ecINTERNAL_ERROR] = "Internal error";
64
m_vErrMsg[ecINVALID_NAME] = "Invalid function-, variable- or constant name.";
65
m_vErrMsg[ecINVALID_FUN_PTR] = "Invalid pointer to callback function.";
66
m_vErrMsg[ecINVALID_VAR_PTR] = "Invalid pointer to variable.";
67
m_vErrMsg[ecUNEXPECTED_OPERATOR] = "Unexpected operator \"$TOK$\" found at position $POS$";
68
m_vErrMsg[ecUNEXPECTED_EOF] = "Unexpected end of formula at position $POS$";
69
m_vErrMsg[ecUNEXPECTED_COMMA] = "Unexpected comma at position $POS$";
70
m_vErrMsg[ecUNEXPECTED_PARENS] = "Unexpected parenthesis \"$TOK$\" at position $POS$";
71
m_vErrMsg[ecUNEXPECTED_FUN] = "Unexpected function \"$TOK$\" at position $POS$";
72
m_vErrMsg[ecUNEXPECTED_VAL] = "Unexpected value \"$TOK$\" found at position $POS$";
73
m_vErrMsg[ecUNEXPECTED_VAR] = "Unexpected variable \"$TOK$\" found at position $POS$";
74
m_vErrMsg[ecUNEXPECTED_ARG] = "Function arguments used without a function (position: $POS$)";
75
m_vErrMsg[ecMISSING_PARENS] = "Missing parenthesis";
76
m_vErrMsg[ecTOO_MANY_PARAMS] = "Too many parameters for function \"$TOK$\" at formula position $POS$";
77
m_vErrMsg[ecTOO_FEW_PARAMS] = "Too few parameters for function \"$TOK$\" at formula position $POS$";
78
m_vErrMsg[ecDIV_BY_ZERO] = "Divide by zero";
79
m_vErrMsg[ecDOMAIN_ERROR] = "Domain error";
80
m_vErrMsg[ecNAME_CONFLICT] = "Name conflict";
81
m_vErrMsg[ecOPT_PRI] = "Invalid value for operator priority (must be greater or equal to zero).";
82
m_vErrMsg[ecBUILTIN_OVERLOAD] = "Binary operator identifier conflicts with a built in operator.";
83
m_vErrMsg[ecUNEXPECTED_STR] = "Unexpected string token found at position $POS$.";
84
m_vErrMsg[ecUNTERMINATED_STRING] = "Unterminated string starting at position $POS$.";
85
m_vErrMsg[ecSTRING_EXPECTED] = "String function called with a non string type of argument.";
86
m_vErrMsg[ecVAL_EXPECTED] = "Numerical function called with a non value type of argument.";
87
m_vErrMsg[ecOPRT_TYPE_CONFLICT] = "No suitable overload for operator \"$TOK$\" at position $POS$.";
88
m_vErrMsg[ecGENERIC] = "Parser error.";
89
m_vErrMsg[ecSTR_RESULT] = "Function result is a string.";
92
for (int i=0; i<ecCOUNT; ++i)
93
if (!m_vErrMsg[i].length())
98
//---------------------------------------------------------------------------
106
//---------------------------------------------------------------------------
108
//------------------------------------------------------------------------------
109
ParserError::ParserError()
114
,m_iErrc(ecUNDEFINED)
115
,m_ErrMsg(ParserErrorMsg::Instance())
119
//------------------------------------------------------------------------------
120
/** \brief This Constructor is used for internal exceptions only.
122
It does not contain any information but the error code.
124
ParserError::ParserError(EErrorCodes a_iErrc)
125
:m_ErrMsg(ParserErrorMsg::Instance())
127
assert(a_iErrc==ecINTERNAL_ERROR);
129
m_strMsg = "internal error";
132
//------------------------------------------------------------------------------
133
ParserError::ParserError(const string_type &sMsg)
134
:m_ErrMsg(ParserErrorMsg::Instance())
140
//------------------------------------------------------------------------------
141
ParserError::ParserError( EErrorCodes a_iErrc,
142
const string_type &sTok,
143
const string_type &sFormula,
146
,m_strFormula(sFormula)
150
,m_ErrMsg(ParserErrorMsg::Instance())
152
m_strMsg = m_ErrMsg[m_iErrc];
153
stringstream_type stream;
154
stream << (int)m_iPos;
155
ReplaceSubString(m_strMsg, "$POS$", stream.str());
156
ReplaceSubString(m_strMsg, "$TOK$", m_strTok);
159
//------------------------------------------------------------------------------
160
ParserError::ParserError( EErrorCodes a_iErrc, int a_iPos, const string_type &sTok)
166
,m_ErrMsg(ParserErrorMsg::Instance())
168
m_strMsg = m_ErrMsg[m_iErrc];
169
stringstream_type stream;
170
stream << (int)m_iPos;
171
ReplaceSubString(m_strMsg, "$POS$", stream.str());
172
ReplaceSubString(m_strMsg, "$TOK$", m_strTok);
175
//------------------------------------------------------------------------------
176
ParserError::ParserError( const char_type *a_szMsg, int a_iPos, const string_type &sTok)
182
,m_ErrMsg(ParserErrorMsg::Instance())
184
stringstream_type stream;
185
stream << (int)m_iPos;
186
ReplaceSubString(m_strMsg, "$POS$", stream.str());
187
ReplaceSubString(m_strMsg, "$TOK$", m_strTok);
190
//------------------------------------------------------------------------------
191
ParserError::ParserError(const ParserError &a_Obj)
192
:m_strMsg(a_Obj.m_strMsg)
193
,m_strFormula(a_Obj.m_strFormula)
194
,m_strTok(a_Obj.m_strTok)
195
,m_iPos(a_Obj.m_iPos)
196
,m_iErrc(a_Obj.m_iErrc)
197
,m_ErrMsg(ParserErrorMsg::Instance())
201
//------------------------------------------------------------------------------
202
ParserError& ParserError::operator=(const ParserError &a_Obj)
207
m_strMsg = a_Obj.m_strMsg;
208
m_strFormula = a_Obj.m_strFormula;
209
m_strTok = a_Obj.m_strTok;
210
m_iPos = a_Obj.m_iPos;
211
m_iErrc = a_Obj.m_iErrc;
215
//------------------------------------------------------------------------------
216
ParserError::~ParserError()
220
/** \brief Replace all ocuurences of a substring with another string. */
221
void ParserError::ReplaceSubString( string_type &strSource,
222
const string_type &strFind,
223
const string_type &strReplaceWith)
225
string_type strResult;
226
string_type::size_type iPos(0), iNext(0);
230
iNext = strSource.find(strFind, iPos);
231
strResult.append(strSource, iPos, iNext-iPos);
233
if( iNext==string_type::npos )
236
strResult.append(strReplaceWith);
237
iPos = iNext + strFind.length();
240
strSource.swap(strResult);
243
//------------------------------------------------------------------------------
244
void ParserError::Reset()
250
m_iErrc = ecUNDEFINED;
253
//------------------------------------------------------------------------------
254
void ParserError::SetFormula(const string_type &a_strFormula)
256
m_strFormula = a_strFormula;
259
//------------------------------------------------------------------------------
260
const string_type& ParserError::GetExpr() const
265
//------------------------------------------------------------------------------
266
const string_type& ParserError::GetMsg() const
271
//------------------------------------------------------------------------------
272
/** \brief Return the formula position related to the error.
274
If the error is not related to a distinct position this will return -1
276
std::size_t ParserError::GetPos() const
281
//------------------------------------------------------------------------------
282
/** \brief Return string related with this token (if available). */
283
const string_type& ParserError::GetToken() const
288
//------------------------------------------------------------------------------
289
/** \brief Return the error code. */
290
EErrorCodes ParserError::GetCode() const