OpcUaCanOpen
CANopen OPC-UA server
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 
28 namespace mu
29 {
30  //------------------------------------------------------------------------------
32  {
33  static const ParserErrorMsg instance;
34  return instance;
35  }
36 
37  //------------------------------------------------------------------------------
38  string_type ParserErrorMsg::operator[](unsigned a_iIdx) const
39  {
40  return (a_iIdx<m_vErrMsg.size()) ? m_vErrMsg[a_iIdx] : string_type();
41  }
42 
43  //---------------------------------------------------------------------------
45  :m_vErrMsg(0)
46  {
47  m_vErrMsg.resize(ecCOUNT);
48 
49  m_vErrMsg[ecUNASSIGNABLE_TOKEN] = _T("Unexpected token \"$TOK$\" found at position $POS$.");
50  m_vErrMsg[ecINTERNAL_ERROR] = _T("Internal error");
51  m_vErrMsg[ecINVALID_NAME] = _T("Invalid function-, variable- or constant name: \"$TOK$\".");
52  m_vErrMsg[ecINVALID_BINOP_IDENT] = _T("Invalid binary operator identifier: \"$TOK$\".");
53  m_vErrMsg[ecINVALID_INFIX_IDENT] = _T("Invalid infix operator identifier: \"$TOK$\".");
54  m_vErrMsg[ecINVALID_POSTFIX_IDENT] = _T("Invalid postfix operator identifier: \"$TOK$\".");
55  m_vErrMsg[ecINVALID_FUN_PTR] = _T("Invalid pointer to callback function.");
56  m_vErrMsg[ecEMPTY_EXPRESSION] = _T("Expression is empty.");
57  m_vErrMsg[ecINVALID_VAR_PTR] = _T("Invalid pointer to variable.");
58  m_vErrMsg[ecUNEXPECTED_OPERATOR] = _T("Unexpected operator \"$TOK$\" found at position $POS$");
59  m_vErrMsg[ecUNEXPECTED_EOF] = _T("Unexpected end of expression at position $POS$");
60  m_vErrMsg[ecUNEXPECTED_ARG_SEP] = _T("Unexpected argument separator at position $POS$");
61  m_vErrMsg[ecUNEXPECTED_PARENS] = _T("Unexpected parenthesis \"$TOK$\" at position $POS$");
62  m_vErrMsg[ecUNEXPECTED_FUN] = _T("Unexpected function \"$TOK$\" at position $POS$");
63  m_vErrMsg[ecUNEXPECTED_VAL] = _T("Unexpected value \"$TOK$\" found at position $POS$");
64  m_vErrMsg[ecUNEXPECTED_VAR] = _T("Unexpected variable \"$TOK$\" found at position $POS$");
65  m_vErrMsg[ecUNEXPECTED_ARG] = _T("Function arguments used without a function (position: $POS$)");
66  m_vErrMsg[ecMISSING_PARENS] = _T("Missing parenthesis");
67  m_vErrMsg[ecTOO_MANY_PARAMS] = _T("Too many parameters for function \"$TOK$\" at expression position $POS$");
68  m_vErrMsg[ecTOO_FEW_PARAMS] = _T("Too few parameters for function \"$TOK$\" at expression position $POS$");
69  m_vErrMsg[ecDIV_BY_ZERO] = _T("Divide by zero");
70  m_vErrMsg[ecDOMAIN_ERROR] = _T("Domain error");
71  m_vErrMsg[ecNAME_CONFLICT] = _T("Name conflict");
72  m_vErrMsg[ecOPT_PRI] = _T("Invalid value for operator priority (must be greater or equal to zero).");
73  m_vErrMsg[ecBUILTIN_OVERLOAD] = _T("user defined binary operator \"$TOK$\" conflicts with a built in operator.");
74  m_vErrMsg[ecUNEXPECTED_STR] = _T("Unexpected string token found at position $POS$.");
75  m_vErrMsg[ecUNTERMINATED_STRING] = _T("Unterminated string starting at position $POS$.");
76  m_vErrMsg[ecSTRING_EXPECTED] = _T("String function called with a non string type of argument.");
77  m_vErrMsg[ecVAL_EXPECTED] = _T("String value used where a numerical argument is expected.");
78  m_vErrMsg[ecOPRT_TYPE_CONFLICT] = _T("No suitable overload for operator \"$TOK$\" at position $POS$.");
79  m_vErrMsg[ecSTR_RESULT] = _T("Function result is a string.");
80  m_vErrMsg[ecGENERIC] = _T("Parser error.");
81  m_vErrMsg[ecLOCALE] = _T("Decimal separator is identic to function argument separator.");
82  m_vErrMsg[ecUNEXPECTED_CONDITIONAL] = _T("The \"$TOK$\" operator must be preceded by a closing bracket.");
83  m_vErrMsg[ecMISSING_ELSE_CLAUSE] = _T("If-then-else operator is missing an else clause");
84  m_vErrMsg[ecMISPLACED_COLON] = _T("Misplaced colon at position $POS$");
85  m_vErrMsg[ecUNREASONABLE_NUMBER_OF_COMPUTATIONS] = _T("Number of computations to small for bulk mode. (Vectorisation overhead too costly)");
86 
87  #if defined(_DEBUG)
88  for (int i=0; i<ecCOUNT; ++i)
89  if (!m_vErrMsg[i].length())
90  assert(false);
91  #endif
92  }
93 
94  //---------------------------------------------------------------------------
95  //
96  // ParserError class
97  //
98  //---------------------------------------------------------------------------
99 
102  :m_strMsg()
103  ,m_strFormula()
104  ,m_strTok()
105  ,m_iPos(-1)
106  ,m_iErrc(ecUNDEFINED)
107  ,m_ErrMsg(ParserErrorMsg::Instance())
108  {
109  }
110 
111  //------------------------------------------------------------------------------
117  :m_strMsg()
118  ,m_strFormula()
119  ,m_strTok()
120  ,m_iPos(-1)
121  ,m_iErrc(a_iErrc)
122  ,m_ErrMsg(ParserErrorMsg::Instance())
123  {
125  stringstream_type stream;
126  stream << (int)m_iPos;
127  ReplaceSubString(m_strMsg, _T("$POS$"), stream.str());
128  ReplaceSubString(m_strMsg, _T("$TOK$"), m_strTok);
129  }
130 
131  //------------------------------------------------------------------------------
134  :m_ErrMsg(ParserErrorMsg::Instance())
135  {
136  Reset();
137  m_strMsg = sMsg;
138  }
139 
140  //------------------------------------------------------------------------------
148  const string_type &sTok,
149  const string_type &sExpr,
150  int iPos )
151  :m_strMsg()
152  ,m_strFormula(sExpr)
153  ,m_strTok(sTok)
154  ,m_iPos(iPos)
155  ,m_iErrc(iErrc)
156  ,m_ErrMsg(ParserErrorMsg::Instance())
157  {
159  stringstream_type stream;
160  stream << (int)m_iPos;
161  ReplaceSubString(m_strMsg, _T("$POS$"), stream.str());
162  ReplaceSubString(m_strMsg, _T("$TOK$"), m_strTok);
163  }
164 
165  //------------------------------------------------------------------------------
171  ParserError::ParserError(EErrorCodes iErrc, int iPos, const string_type &sTok)
172  :m_strMsg()
173  ,m_strFormula()
174  ,m_strTok(sTok)
175  ,m_iPos(iPos)
176  ,m_iErrc(iErrc)
177  ,m_ErrMsg(ParserErrorMsg::Instance())
178  {
180  stringstream_type stream;
181  stream << (int)m_iPos;
182  ReplaceSubString(m_strMsg, _T("$POS$"), stream.str());
183  ReplaceSubString(m_strMsg, _T("$TOK$"), m_strTok);
184  }
185 
186  //------------------------------------------------------------------------------
192  ParserError::ParserError(const char_type *szMsg, int iPos, const string_type &sTok)
193  :m_strMsg(szMsg)
194  ,m_strFormula()
195  ,m_strTok(sTok)
196  ,m_iPos(iPos)
197  ,m_iErrc(ecGENERIC)
198  ,m_ErrMsg(ParserErrorMsg::Instance())
199  {
200  stringstream_type stream;
201  stream << (int)m_iPos;
202  ReplaceSubString(m_strMsg, _T("$POS$"), stream.str());
203  ReplaceSubString(m_strMsg, _T("$TOK$"), m_strTok);
204  }
205 
206  //------------------------------------------------------------------------------
209  :m_strMsg(a_Obj.m_strMsg)
210  ,m_strFormula(a_Obj.m_strFormula)
211  ,m_strTok(a_Obj.m_strTok)
212  ,m_iPos(a_Obj.m_iPos)
213  ,m_iErrc(a_Obj.m_iErrc)
214  ,m_ErrMsg(ParserErrorMsg::Instance())
215  {
216  }
217 
218  //------------------------------------------------------------------------------
221  {
222  if (this==&a_Obj)
223  return *this;
224 
225  m_strMsg = a_Obj.m_strMsg;
226  m_strFormula = a_Obj.m_strFormula;
227  m_strTok = a_Obj.m_strTok;
228  m_iPos = a_Obj.m_iPos;
229  m_iErrc = a_Obj.m_iErrc;
230  return *this;
231  }
232 
233  //------------------------------------------------------------------------------
235  {}
236 
237  //------------------------------------------------------------------------------
243  const string_type &strFind,
244  const string_type &strReplaceWith)
245  {
246  string_type strResult;
247  string_type::size_type iPos(0), iNext(0);
248 
249  for(;;)
250  {
251  iNext = strSource.find(strFind, iPos);
252  strResult.append(strSource, iPos, iNext-iPos);
253 
254  if( iNext==string_type::npos )
255  break;
256 
257  strResult.append(strReplaceWith);
258  iPos = iNext + strFind.length();
259  }
260 
261  strSource.swap(strResult);
262  }
263 
264  //------------------------------------------------------------------------------
267  {
268  m_strMsg = _T("");
269  m_strFormula = _T("");
270  m_strTok = _T("");
271  m_iPos = -1;
273  }
274 
275  //------------------------------------------------------------------------------
277  void ParserError::SetFormula(const string_type &a_strFormula)
278  {
279  m_strFormula = a_strFormula;
280  }
281 
282  //------------------------------------------------------------------------------
285  {
286  return m_strFormula;
287  }
288 
289  //------------------------------------------------------------------------------
292  {
293  return m_strMsg;
294  }
295 
296  //------------------------------------------------------------------------------
302  {
303  return m_iPos;
304  }
305 
306  //------------------------------------------------------------------------------
309  {
310  return m_strTok;
311  }
312 
313  //------------------------------------------------------------------------------
316  {
317  return m_iErrc;
318  }
319 } // namespace mu