~ubuntu-branches/ubuntu/karmic/kst/karmic

« back to all changes in this revision

Viewing changes to kst/plugins/fits_nonlinear/general_levenberg_marquardt/muParserError.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-06-30 19:11:30 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060630191130-acumuar75bz4puty
Tags: 1.2.1-1ubuntu1
Merge from debian unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2004, 2005 Ingo Berg
 
3
 
 
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:
 
9
 
 
10
  The above copyright notice and this permission notice shall be included in all copies or 
 
11
  substantial portions of the Software.
 
12
 
 
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. 
 
18
*/
 
19
#include "muParserError.h"
 
20
 
 
21
 
 
22
namespace mu
 
23
{
 
24
  const ParserErrorMsg ParserErrorMsg::m_Instance;
 
25
 
 
26
  //------------------------------------------------------------------------------
 
27
  const ParserErrorMsg& ParserErrorMsg::Instance()
 
28
  {
 
29
    return m_Instance;
 
30
  }
 
31
 
 
32
  //------------------------------------------------------------------------------
 
33
  string_type ParserErrorMsg::operator[](unsigned a_iIdx) const
 
34
  {
 
35
    return (a_iIdx<m_vErrMsg.size()) ? m_vErrMsg[a_iIdx] : string_type();
 
36
  }
 
37
 
 
38
 
 
39
  //---------------------------------------------------------------------------
 
40
  ParserErrorMsg::~ParserErrorMsg()
 
41
  {}
 
42
 
 
43
  //---------------------------------------------------------------------------
 
44
  /** \brief Assignement operator is deactivated.
 
45
  */
 
46
  ParserErrorMsg& ParserErrorMsg::operator=(const ParserErrorMsg& )
 
47
  {
 
48
    assert(false);
 
49
    return *this;
 
50
  }
 
51
 
 
52
  //---------------------------------------------------------------------------
 
53
  ParserErrorMsg::ParserErrorMsg(const ParserErrorMsg&)
 
54
  {}
 
55
 
 
56
  //---------------------------------------------------------------------------
 
57
  ParserErrorMsg::ParserErrorMsg()
 
58
    :m_vErrMsg(0)
 
59
  {
 
60
    m_vErrMsg.resize(ecCOUNT);
 
61
 
 
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.";
 
90
 
 
91
    #if defined(_DEBUG)
 
92
      for (int i=0; i<ecCOUNT; ++i)
 
93
        if (!m_vErrMsg[i].length())
 
94
          assert(false);
 
95
    #endif
 
96
  }
 
97
 
 
98
  //---------------------------------------------------------------------------
 
99
  //
 
100
  //
 
101
  //
 
102
  //  ParserError class
 
103
  //
 
104
  //
 
105
  //
 
106
  //---------------------------------------------------------------------------
 
107
 
 
108
  //------------------------------------------------------------------------------
 
109
  ParserError::ParserError()
 
110
    :m_strMsg()
 
111
    ,m_strFormula()
 
112
    ,m_strTok()
 
113
    ,m_iPos(-1)
 
114
    ,m_iErrc(ecUNDEFINED)
 
115
    ,m_ErrMsg(ParserErrorMsg::Instance())
 
116
  {
 
117
  }
 
118
 
 
119
  //------------------------------------------------------------------------------
 
120
  /** \brief This Constructor is used for internal exceptions only. 
 
121
      
 
122
    It does not contain any information but the error code.
 
123
  */
 
124
  ParserError::ParserError(EErrorCodes a_iErrc) 
 
125
    :m_ErrMsg(ParserErrorMsg::Instance())
 
126
  {
 
127
    assert(a_iErrc==ecINTERNAL_ERROR);
 
128
    Reset();
 
129
    m_strMsg = "internal error";
 
130
  }
 
131
 
 
132
  //------------------------------------------------------------------------------
 
133
  ParserError::ParserError(const string_type &sMsg) 
 
134
    :m_ErrMsg(ParserErrorMsg::Instance())
 
135
  {
 
136
    Reset();
 
137
    m_strMsg = sMsg;
 
138
  }
 
139
 
 
140
  //------------------------------------------------------------------------------
 
141
  ParserError::ParserError( EErrorCodes a_iErrc,
 
142
                            const string_type &sTok,
 
143
                            const string_type &sFormula,
 
144
                            int a_iPos )
 
145
    :m_strMsg()
 
146
    ,m_strFormula(sFormula)
 
147
    ,m_strTok(sTok)
 
148
    ,m_iPos(a_iPos)
 
149
    ,m_iErrc(a_iErrc)
 
150
    ,m_ErrMsg(ParserErrorMsg::Instance())
 
151
  {
 
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);
 
157
  }
 
158
 
 
159
  //------------------------------------------------------------------------------
 
160
  ParserError::ParserError( EErrorCodes a_iErrc, int a_iPos, const string_type &sTok) 
 
161
    :m_strMsg()
 
162
    ,m_strFormula()
 
163
    ,m_strTok(sTok)
 
164
    ,m_iPos(a_iPos)
 
165
    ,m_iErrc(a_iErrc)
 
166
    ,m_ErrMsg(ParserErrorMsg::Instance())
 
167
  {
 
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);
 
173
  }
 
174
 
 
175
  //------------------------------------------------------------------------------
 
176
  ParserError::ParserError( const char_type *a_szMsg, int a_iPos, const string_type &sTok) 
 
177
    :m_strMsg(a_szMsg)
 
178
    ,m_strFormula()
 
179
    ,m_strTok(sTok)
 
180
    ,m_iPos(a_iPos)
 
181
    ,m_iErrc(ecGENERIC)
 
182
    ,m_ErrMsg(ParserErrorMsg::Instance())
 
183
  {
 
184
    stringstream_type stream;
 
185
    stream << (int)m_iPos;
 
186
    ReplaceSubString(m_strMsg, "$POS$", stream.str());
 
187
    ReplaceSubString(m_strMsg, "$TOK$", m_strTok);
 
188
  }
 
189
 
 
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())
 
198
  {
 
199
  }
 
200
 
 
201
  //------------------------------------------------------------------------------
 
202
  ParserError& ParserError::operator=(const ParserError &a_Obj)
 
203
  {
 
204
    if (this==&a_Obj)
 
205
      return *this;
 
206
 
 
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;
 
212
    return *this;
 
213
  }
 
214
 
 
215
  //------------------------------------------------------------------------------
 
216
  ParserError::~ParserError()
 
217
  {
 
218
  }
 
219
 
 
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)
 
224
  {
 
225
    string_type strResult;
 
226
    string_type::size_type iPos(0), iNext(0);
 
227
 
 
228
    for(;;)
 
229
    {
 
230
      iNext = strSource.find(strFind, iPos);
 
231
      strResult.append(strSource, iPos, iNext-iPos);
 
232
 
 
233
      if( iNext==string_type::npos )
 
234
        break;
 
235
 
 
236
      strResult.append(strReplaceWith);
 
237
      iPos = iNext + strFind.length();
 
238
    } 
 
239
 
 
240
    strSource.swap(strResult);
 
241
  }
 
242
 
 
243
  //------------------------------------------------------------------------------
 
244
  void ParserError::Reset()
 
245
  {
 
246
    m_strMsg = "";
 
247
    m_strFormula = "";
 
248
    m_strTok = "";
 
249
    m_iPos = -1;
 
250
    m_iErrc = ecUNDEFINED;
 
251
  }
 
252
      
 
253
  //------------------------------------------------------------------------------
 
254
  void ParserError::SetFormula(const string_type &a_strFormula)
 
255
  {
 
256
    m_strFormula = a_strFormula;
 
257
  }
 
258
 
 
259
  //------------------------------------------------------------------------------
 
260
  const string_type& ParserError::GetExpr() const 
 
261
  {
 
262
    return m_strFormula;
 
263
  }
 
264
 
 
265
  //------------------------------------------------------------------------------
 
266
  const string_type& ParserError::GetMsg() const
 
267
  {
 
268
    return m_strMsg;
 
269
  }
 
270
 
 
271
  //------------------------------------------------------------------------------
 
272
  /** \brief Return the formula position related to the error. 
 
273
 
 
274
    If the error is not related to a distinct position this will return -1
 
275
  */
 
276
  std::size_t ParserError::GetPos() const
 
277
  {
 
278
    return m_iPos;
 
279
  }
 
280
 
 
281
  //------------------------------------------------------------------------------
 
282
  /** \brief Return string related with this token (if available). */
 
283
  const string_type& ParserError::GetToken() const
 
284
  {
 
285
    return m_strTok;
 
286
  }
 
287
 
 
288
  //------------------------------------------------------------------------------
 
289
  /** \brief Return the error code. */
 
290
  EErrorCodes ParserError::GetCode() const
 
291
  {
 
292
    return m_iErrc;
 
293
  }
 
294
} // namespace mu