MADNESS  version 0.9
muParserToken.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2008 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 
26 #ifndef MU_PARSER_TOKEN_H
27 #define MU_PARSER_TOKEN_H
28 
29 #include <cassert>
30 #include <string>
31 #include <stack>
32 #include <vector>
33 #include <memory>
34 
35 #include "muParserError.h"
36 #include "muParserCallback.h"
37 
42 namespace mu
43 {
60  template<typename TBase, typename TString>
62  {
63  public:
64 
66  enum ETokFlags
67  {
69  };
70 
71  private:
72 
73  ECmdCode m_iCode;
74  ETypeCode m_iType;
75  void *m_pTok;
76  int m_iFlags;
77  int m_iIdx;
78  TString m_strTok;
79  TString m_strVal;
80  value_type m_fVal;
81  std::auto_ptr<ParserCallback> m_pCallback;
82 
83  public:
84 
85  //---------------------------------------------------------------------------
93  :m_iCode(cmUNKNOWN)
94  ,m_iType(tpVOID)
95  ,m_pTok(0)
96  ,m_iFlags(0)
97  ,m_iIdx(-1)
98  ,m_strTok()
99  ,m_pCallback()
100  {}
101 
102  //------------------------------------------------------------------------------
110  ParserToken(const ParserToken &a_Tok)
111  {
112  Assign(a_Tok);
113  }
114 
115  //------------------------------------------------------------------------------
123  {
124  Assign(a_Tok);
125  return *this;
126  }
127 
128  //------------------------------------------------------------------------------
133  void Assign(const ParserToken &a_Tok)
134  {
135  m_iCode = a_Tok.m_iCode;
136  m_pTok = a_Tok.m_pTok;
137  m_iFlags = a_Tok.m_iFlags;
138  m_strTok = a_Tok.m_strTok;
139  m_iIdx = a_Tok.m_iIdx;
140  m_strVal = a_Tok.m_strVal;
141  m_iType = a_Tok.m_iType;
142  m_fVal = a_Tok.m_fVal;
143  // create new callback object if a_Tok has one
144  m_pCallback.reset(a_Tok.m_pCallback.get() ? a_Tok.m_pCallback->Clone() : 0);
145  }
146 
147  //------------------------------------------------------------------------------
153  void AddFlags(int a_iFlags)
154  {
155  m_iFlags |= a_iFlags;
156  }
157 
158  //------------------------------------------------------------------------------
163  bool IsFlagSet(int a_iFlags) const
164  {
165  #if defined(_MSC_VER)
166  #pragma warning( disable : 4800 )
167  #endif
168 
169  return (bool)(m_iFlags & a_iFlags);
170 
171  #if defined(_MSC_VER)
172  #pragma warning( default : 4800 ) // int: Variable set to boolean value (may degrade performance)
173  #endif
174  }
175 
176  //------------------------------------------------------------------------------
187  ParserToken& Set(ECmdCode a_iType, const TString &a_strTok=TString())
188  {
189  // The following types cant be set this way, they have special Set functions
190  assert(a_iType!=cmVAR);
191  assert(a_iType!=cmVAL);
192  assert(a_iType!=cmFUNC);
193 
194  m_iCode = a_iType;
195  m_iType = tpVOID;
196  m_pTok = 0;
197  m_iFlags = 0;
198  m_strTok = a_strTok;
199  m_iIdx = -1;
200 
201  return *this;
202  }
203 
204  //------------------------------------------------------------------------------
206  ParserToken& Set(const ParserCallback &a_pCallback, const TString &a_sTok)
207  {
208  assert(a_pCallback.GetAddr());
209 
210  m_iCode = a_pCallback.GetCode();
211  m_iType = tpVOID;
212  m_strTok = a_sTok;
213  m_pCallback.reset(new ParserCallback(a_pCallback));
214 
215  m_pTok = 0;
216  m_iFlags = 0;
217  m_iIdx = -1;
218 
219  if (!m_pCallback->IsOptimizable())
221 
222  return *this;
223  }
224 
225  //------------------------------------------------------------------------------
231  ParserToken& SetVal(TBase a_fVal, const TString &a_strTok=TString())
232  {
233  m_iCode = cmVAL;
234  m_iType = tpDBL;
235  m_fVal = a_fVal;
236  m_iFlags = 0;
237  m_strTok = a_strTok;
238  m_iIdx = -1;
239 
240  m_pTok = 0;
241  m_pCallback.reset(0);
242 
243  return *this;
244  }
245 
246  //------------------------------------------------------------------------------
252  ParserToken& SetVar(TBase *a_pVar, const TString &a_strTok)
253  {
254  m_iCode = cmVAR;
255  m_iType = tpDBL;
256  m_iFlags = 0;
257  m_strTok = a_strTok;
258  m_iIdx = -1;
259  m_pTok = (void*)a_pVar;
260  m_pCallback.reset(0);
261 
263  return *this;
264  }
265 
266  //------------------------------------------------------------------------------
272  ParserToken& SetString(const TString &a_strTok, std::size_t a_iSize)
273  {
274  m_iCode = cmSTRING;
275  m_iType = tpSTR;
276  m_iFlags = 0;
277  m_strTok = a_strTok;
278  m_iIdx = static_cast<int>(a_iSize);
279 
280  m_pTok = 0;
281  m_pCallback.reset(0);
282 
284  return *this;
285  }
286 
287  //------------------------------------------------------------------------------
294  void SetIdx(int a_iIdx)
295  {
296  if (m_iCode!=cmSTRING || a_iIdx<0)
298 
299  m_iIdx = a_iIdx;
300  }
301 
302  //------------------------------------------------------------------------------
310  int GetIdx() const
311  {
312  if (m_iIdx<0 || m_iCode!=cmSTRING )
314 
315  return m_iIdx;
316  }
317 
318  //------------------------------------------------------------------------------
325  {
326  if (m_pCallback.get())
327  {
328  return m_pCallback->GetCode();
329  }
330  else
331  {
332  return m_iCode;
333  }
334  }
335 
336  //------------------------------------------------------------------------------
338  {
339  if (m_pCallback.get())
340  {
341  return m_pCallback->GetType();
342  }
343  else
344  {
345  return m_iType;
346  }
347  }
348 
349  //------------------------------------------------------------------------------
350  int GetPri() const
351  {
352  if ( !m_pCallback.get())
354 
355  if ( m_pCallback->GetCode()!=cmOPRT_BIN && m_pCallback->GetCode()!=cmOPRT_INFIX)
357 
358  return m_pCallback->GetPri();
359  }
360 
361  //------------------------------------------------------------------------------
376  void* GetFuncAddr() const
377  {
378  return (m_pCallback.get()) ? m_pCallback->GetAddr() : 0;
379  }
380 
381  //------------------------------------------------------------------------------
387  TBase GetVal() const
388  {
389  switch (m_iCode)
390  {
391  case cmVAL: return m_fVal;
392  case cmVAR: return *((TBase*)m_pTok);
393  default: throw ParserError(ecVAL_EXPECTED);
394  }
395  }
396 
397  //------------------------------------------------------------------------------
403  TBase* GetVar() const
404  {
405  if (m_iCode!=cmVAR)
407 
408  return (TBase*)m_pTok;
409  }
410 
411  //------------------------------------------------------------------------------
416  int GetArgCount() const
417  {
418  assert(m_pCallback.get());
419 
420  if (!m_pCallback->GetAddr())
422 
423  return m_pCallback->GetArgc();
424  }
425 
426  //------------------------------------------------------------------------------
435  const TString& GetAsString() const
436  {
437  return m_strTok;
438  }
439  };
440 } // namespace mu
441 
442 #endif
code for infix operators
Definition: muParserDef.h:184
user defined binary operator
Definition: muParserDef.h:182
ParserToken & SetVar(TBase *a_pVar, const TString &a_strTok)
make this token a variable token.
Definition: muParserToken.h:252
int GetArgCount() const
Return the number of function arguments.
Definition: muParserToken.h:416
TBase * GetVar() const
Get address of a variable token.
Definition: muParserToken.h:403
bool IsFlagSet(int a_iFlags) const
Check if a certain flag ist set.
Definition: muParserToken.h:163
Internal error of any kind.
Definition: muParserError.h:83
const muChar_t muFloat_t a_fVal
Definition: muParserDLL.h:107
ParserToken & SetVal(TBase a_fVal, const TString &a_strTok=TString())
Make this token a value token.
Definition: muParserToken.h:231
void Assign(const ParserToken &a_Tok)
Copy token information from argument.
Definition: muParserToken.h:133
Mark a token that depends on a variable or a function that is not conservative.
Definition: muParserToken.h:68
ParserToken & operator=(const ParserToken &a_Tok)
Assignement operator.
Definition: muParserToken.h:122
ECmdCode GetCode() const
Return the callback code.
Definition: muParserCallback.cpp:211
ParserToken & SetString(const TString &a_strTok, std::size_t a_iSize)
Make this token a variable token.
Definition: muParserToken.h:272
Code for a function item.
Definition: muParserDef.h:179
ETokFlags
Additional token flags.
Definition: muParserToken.h:66
unsigned const muChar_t muFloat_t ** a_pVar
Definition: muParserDLL.h:145
int GetIdx() const
Return Index associated with the token related data.
Definition: muParserToken.h:310
void AddFlags(int a_iFlags)
Add additional flags to the token.
Definition: muParserToken.h:153
ETypeCode
Types internally used by the parser.
Definition: muParserDef.h:192
ParserToken()
Constructor (default).
Definition: muParserToken.h:92
Error class of the parser.
Definition: muParserError.h:119
ECmdCode
Bytecode values.
Definition: muParserDef.h:154
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:222
String type (Function arguments and constants only, no string variables)
Definition: muParserDef.h:194
A numerical function has been called with a non value type of argument.
Definition: muParserError.h:59
ECmdCode GetCode() const
Return the token type.
Definition: muParserToken.h:324
Namespace for mathematical applications.
Definition: muParser.cpp:47
TBase GetVal() const
Get value of the token.
Definition: muParserToken.h:387
ParserToken & Set(ECmdCode a_iType, const TString &a_strTok=TString())
Assign a token type.
Definition: muParserToken.h:187
ETypeCode GetType() const
Definition: muParserToken.h:337
int GetPri() const
Definition: muParserToken.h:350
void * GetFuncAddr() const
Return the address of the callback function assoziated with function and operator tokens...
Definition: muParserToken.h:376
value item
Definition: muParserDef.h:178
Undefined type.
Definition: muParserDef.h:196
Definition of the parser callback class.
void * GetAddr() const
Get the callback address for the parser function.
Definition: muParserCallback.cpp:203
void SetIdx(int a_iIdx)
Set an index associated with the token related data.
Definition: muParserToken.h:294
uninitialized item
Definition: muParserDef.h:186
Code for a string token.
Definition: muParserDef.h:181
variable item
Definition: muParserDef.h:177
Encapsulation of the data for a single formula token.
Definition: muParserToken.h:61
const TString & GetAsString() const
Return the token identifier.
Definition: muParserToken.h:435
Floating point variables.
Definition: muParserDef.h:195
ParserToken(const ParserToken &a_Tok)
Create token from another one.
Definition: muParserToken.h:110
Encapsulation of prototypes for a numerical parser function.
Definition: muParserCallback.h:51
ParserToken & Set(const ParserCallback &a_pCallback, const TString &a_sTok)
Set Callback type.
Definition: muParserToken.h:206
This file defines the error class used by the parser.