MADNESS  version 0.9
muParserBase.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 #ifndef MU_PARSER_BASE_H
26 #define MU_PARSER_BASE_H
27 
28 //--- Standard includes ------------------------------------------------------------------------
29 #include <cmath>
30 #include <string>
31 #include <iostream>
32 #include <map>
33 #include <memory>
34 
35 //--- Parser includes --------------------------------------------------------------------------
36 #include "muParserDef.h"
37 #include "muParserStack.h"
38 #include "muParserTokenReader.h"
39 #include "muParserBytecode.h"
40 #include "muParserError.h"
41 
42 
43 namespace mu
44 {
49 //--------------------------------------------------------------------------------------------------
63 class ParserBase
64 {
65 friend class ParserTokenReader;
66 
67 private:
68 
75  typedef value_type (ParserBase::*ParseFunction)() const;
76 
78  typedef std::vector<string_type> stringbuf_type;
79 
82 
85 
86  public:
87 
93 
94  ParserBase();
95  ParserBase(const ParserBase &a_Parser);
96  ParserBase& operator=(const ParserBase &a_Parser);
97 
98  virtual ~ParserBase();
99 
100  //---------------------------------------------------------------------------
116  inline value_type Eval() const
117  {
118  return (this->*m_pParseFormula)();
119  }
120 
121  void SetExpr(const string_type &a_sExpr);
122  void SetVarFactory(facfun_type a_pFactory, void *pUserData = NULL);
123 
124  void EnableOptimizer(bool a_bIsOn=true);
125  void EnableByteCode(bool a_bIsOn=true);
126  void EnableBuiltInOprt(bool a_bIsOn=true);
127 
128  bool HasBuiltInOprt() const;
129  void AddValIdent(identfun_type a_pCallback);
130 
131 #define MUP_DEFINE_FUNC(TYPE) \
132  inline void DefineFun(const string_type &a_strName, TYPE a_pFun, bool a_bAllowOpt = true) \
133  { \
134  AddCallback( a_strName, ParserCallback(a_pFun, a_bAllowOpt), \
135  m_FunDef, ValidNameChars() ); \
136  }
137 
148 #undef MUP_DEFINE_FUNC
149 
150  void DefineOprt(const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, bool a_bAllowOpt = false);
151  void DefineConst(const string_type &a_sName, value_type a_fVal);
152  void DefineStrConst(const string_type &a_sName, const string_type &a_strVal);
153  void DefineVar(const string_type &a_sName, value_type *a_fVar);
154  void DefinePostfixOprt(const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true);
155  void DefineInfixOprt(const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true);
156 
157  // Clear user defined variables, constants or functions
158  void ClearVar();
159  void ClearFun();
160  void ClearConst();
161  void ClearInfixOprt();
162  void ClearPostfixOprt();
163  void ClearOprt();
164 
165  void RemoveVar(const string_type &a_strVarName);
166  const varmap_type& GetUsedVar() const;
167  const varmap_type& GetVar() const;
168  const valmap_type& GetConst() const;
169  const string_type& GetExpr() const;
170  const funmap_type& GetFunDef() const;
171 
172  const char_type ** GetOprtDef() const;
176 
177  const char_type* ValidNameChars() const;
178  const char_type* ValidOprtChars() const;
179  const char_type* ValidInfixOprtChars() const;
180 
182  char_type GetArgSep() const;
183 
184  void Error(EErrorCodes a_iErrc,
185  int a_iPos = (int)mu::string_type::npos,
186  const string_type &a_strTok = string_type() ) const;
187 
188  protected:
189 
190  void Init();
191 
192  virtual void InitCharSets() = 0;
193  virtual void InitFun() = 0;
194  virtual void InitConst() = 0;
195  virtual void InitOprt() = 0;
196 
197  static const char_type *c_DefaultOprt[];
198 
199  private:
200 
201  void Assign(const ParserBase &a_Parser);
202  void InitTokenReader();
203  void ReInit() const;
204 
205  void AddCallback( const string_type &a_strName,
206  const ParserCallback &a_Callback,
207  funmap_type &a_Storage,
208  const char_type *a_szCharSet );
209 
210  void ApplyBinOprt(ParserStack<token_type> &a_stOpt,
211  ParserStack<token_type> &a_stVal) const;
212 
213  void ApplyFunc(ParserStack<token_type> &a_stOpt,
214  ParserStack<token_type> &a_stVal,
215  int iArgCount) const;
216 
217  token_type ApplyNumFunc(const token_type &a_FunTok,
218  const std::vector<token_type> &a_vArg) const;
219 
220  token_type ApplyStrFunc(const token_type &a_FunTok,
221  const std::vector<token_type> &a_vArg) const;
222 
223  int GetOprtPri(const token_type &a_Tok) const;
224 
225  value_type ParseString() const;
226  value_type ParseCmdCode() const;
227  value_type ParseValue() const;
228 
229  void ClearFormula();
230  void CheckName(const string_type &a_strName, const string_type &a_CharSet) const;
231 
232 #if defined(MUP_DUMP_STACK) | defined(MUP_DUMP_CMDCODE)
233  void StackDump(const ParserStack<token_type > &a_stVal,
234  const ParserStack<token_type > &a_stOprt) const;
235 #endif
236 
241  mutable ParseFunction m_pParseFormula;
242  mutable const ParserByteCode::map_type *m_pCmdCode;
243  mutable ParserByteCode m_vByteCode;
244  mutable stringbuf_type m_vStringBuf;
245  stringbuf_type m_vStringVarBuf;
246 
247  std::auto_ptr<token_reader_type> m_pTokenReader;
248 
249  funmap_type m_FunDef;
250  funmap_type m_PostOprtDef;
251  funmap_type m_InfixOprtDef;
252  funmap_type m_OprtDef;
253  valmap_type m_ConstDef;
254  strmap_type m_StrVarDef;
255  varmap_type m_VarDef;
256 
257  bool m_bOptimize;
258  bool m_bUseByteCode;
259  bool m_bBuiltInOp;
260 
261  string_type m_sNameChars;
262  string_type m_sOprtChars;
263  string_type m_sInfixOprtChars;
264 };
265 
266 } // namespace mu
267 
268 #endif
269 
void DefinePostfixOprt(const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true)
Add a user defined operator.
Definition: muParserBase.cpp:348
void DefineInfixOprtChars(const char_type *a_szCharset)
Define the set of valid characters to be used in names of infix operators.
Definition: muParserBase.cpp:309
const muChar_t cArgSep
Definition: muParserDLL.h:148
Definition of the parser bytecode class.
void DefineNameChars(const char_type *a_szCharset)
Define the set of valid characters to be used in names of functions, variables, constants.
Definition: muParserBase.cpp:291
void DefineVar(const string_type &a_sName, value_type *a_fVar)
Add a user defined variable.
Definition: muParserBase.cpp:441
virtual void InitCharSets()=0
const funmap_type & GetFunDef() const
Return prototypes of all parser functions.
Definition: muParserBase.cpp:559
std::map< string_type, std::size_t > strmap_type
Type for assigning a string name to an index in the internal string table.
Definition: muParserDef.h:256
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:250
value_type(* strfun_type3)(const char_type *, value_type, value_type)
Callback type used for functions taking a string and two values as arguments.
Definition: muParserDef.h:288
value_type(* fun_type3)(value_type, value_type, value_type)
Callback type used for functions with three arguments.
Definition: muParserDef.h:270
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:197
void ClearConst()
Clear all user defined constants.
Definition: muParserBase.cpp:1317
const muChar_t muFloat_t a_fVal
Definition: muParserDLL.h:107
const char_type * ValidInfixOprtChars() const
Virtual function that defines the characters allowed in infix operator definitions.
Definition: muParserBase.cpp:338
void ClearOprt()
Clear all user defined binary operators.
Definition: muParserBase.cpp:1340
void SetArgSep(char_type cArgSep)
Set argument separator.
Definition: muParserBase.cpp:1419
const char_type * ValidOprtChars() const
Virtual function that defines the characters allowed in operator definitions.
Definition: muParserBase.cpp:328
void AddValIdent(identfun_type a_pCallback)
Definition: muParserBase.cpp:196
Signs have a higher priority than ADD_SUB, but lower than power operator.
Definition: muParserDef.h:211
muFacFun_t a_pFactory
Definition: muParserDLL.h:79
value_type Eval() const
Calculate the result.
Definition: muParserBase.h:116
Parser stack implementation.
Definition: muParserStack.h:53
bool HasBuiltInOprt() const
Query status of built in variables.
Definition: muParserBase.cpp:1402
std::map< string_type, ParserCallback > funmap_type
Container for Callback objects.
Definition: muParserCallback.h:93
const muChar_t muFun0_t a_pFun
Definition: muParserDLL.h:84
bytecode_type map_type
Underlying type of the container.
Definition: muParserBytecode.h:65
void ClearVar()
Clear all user defined variables.
Definition: muParserBase.cpp:1262
void EnableOptimizer(bool a_bIsOn=true)
Enable or disable the formula optimization feature.
Definition: muParserBase.cpp:1362
void DefineInfixOprt(const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true)
Add a user defined operator.
Definition: muParserBase.cpp:380
void Error(EErrorCodes a_iErrc, int a_iPos=(int) mu::string_type::npos, const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
Definition: muParserBase.cpp:1251
void SetVarFactory(facfun_type a_pFactory, void *pUserData=NULL)
Definition: muParserBase.cpp:202
const string_type & GetExpr() const
Retrieve the formula.
Definition: muParserBase.cpp:566
void DefineOprtChars(const char_type *a_szCharset)
Define the set of valid characters to be used in names of binary operators and postfix operators...
Definition: muParserBase.cpp:300
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
Definition: muParserBase.cpp:318
void EnableByteCode(bool a_bIsOn=true)
Enable or disable parsing from Bytecode.
Definition: muParserBase.cpp:1374
virtual void InitFun()=0
This file contains the parser token reader definition.
value_type(* strfun_type1)(const char_type *)
Callback type used for functions taking a string as an argument.
Definition: muParserDef.h:282
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:253
void ClearPostfixOprt()
Clear all user defined postfix operators.
Definition: muParserBase.cpp:1329
value_type(* strfun_type2)(const char_type *, value_type)
Callback type used for functions taking a string and a value as arguments.
Definition: muParserDef.h:285
const valmap_type & GetConst() const
Return a map containing all parser constants.
Definition: muParserBase.cpp:543
virtual void InitOprt()=0
const muChar_t muFun1_t a_pOprt
Definition: muParserDLL.h:119
Error class of the parser.
Definition: muParserError.h:119
const varmap_type & GetVar() const
Return a map containing the used variables only.
Definition: muParserBase.cpp:536
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:222
void SetExpr(const string_type &a_sExpr)
Set the formula.
Definition: muParserBase.cpp:261
Namespace for mathematical applications.
Definition: muParser.cpp:47
void RemoveVar(const string_type &a_strVarName)
Remove a variable from internal storage.
Definition: muParserBase.cpp:1274
const char_type ** GetOprtDef() const
Get the default symbols used for the built in operators.
Definition: muParserBase.cpp:282
This file defines the stack used by muparser.
virtual ~ParserBase()
Definition: muParserBase.cpp:112
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:240
value_type(* fun_type0)()
Callback type used for functions without arguments.
Definition: muParserDef.h:261
Token reader for the ParserBase class.
Definition: muParserTokenReader.h:53
ParserBase & operator=(const ParserBase &a_Parser)
Assignement operator.
Definition: muParserBase.cpp:123
void DefineConst(const string_type &a_sName, value_type a_fVal)
Add a user defined constant.
Definition: muParserBase.cpp:465
void EnableBuiltInOprt(bool a_bIsOn=true)
Enable or disable the built in binary operators.
Definition: muParserBase.cpp:1391
value_type(* fun_type4)(value_type, value_type, value_type, value_type)
Callback type used for functions with four arguments.
Definition: muParserDef.h:273
const muChar_t muFloat_t * a_fVar
Definition: muParserDLL.h:115
void DefineOprt(const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, bool a_bAllowOpt=false)
Define a binary operator.
Definition: muParserBase.cpp:398
const muChar_t * a_szCharset
Definition: muParserDLL.h:130
Bytecode implementation of the Math Parser.
Definition: muParserBytecode.h:56
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:228
ParserBase()
Constructor.
Definition: muParserBase.cpp:66
value_type(* fun_type1)(value_type)
Callback type used for functions with a single arguments.
Definition: muParserDef.h:264
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:92
int(* identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal)
Callback used for functions that identify values in a string.
Definition: muParserDef.h:291
void ClearFun()
Clear all functions.
Definition: muParserBase.cpp:1304
void DefineStrConst(const string_type &a_sName, const string_type &a_strVal)
Define a new string constant.
Definition: muParserBase.cpp:419
char_type GetArgSep() const
Get the argument separator character.
Definition: muParserBase.cpp:1410
value_type *(* facfun_type)(const char_type *, void *)
Callback used for variable creation factory functions.
Definition: muParserDef.h:294
value_type(* fun_type5)(value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:276
muFacFun_t void * pUserData
Definition: muParserDLL.h:79
value_type(* fun_type2)(value_type, value_type)
Callback type used for functions with two arguments.
Definition: muParserDef.h:267
EErrorCodes
Error codes.
Definition: muParserError.h:46
void Init()
Initialize user defined functions.
Definition: muParserBase.cpp:363
Encapsulation of prototypes for a numerical parser function.
Definition: muParserCallback.h:51
virtual void InitConst()=0
#define MUP_DEFINE_FUNC(TYPE)
Definition: muParserBase.h:131
void ClearInfixOprt()
Clear the user defined Prefix operators.
Definition: muParserBase.cpp:1351
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:63
value_type(* multfun_type)(const value_type *, int)
Callback type used for functions with a variable argument list.
Definition: muParserDef.h:279
const varmap_type & GetUsedVar() const
Return a map containing the used variables only.
Definition: muParserBase.cpp:512
This file defines the error class used by the parser.
This file contains standard definitions used by the parser.