00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef MU_PARSER_BASE_H
00026 #define MU_PARSER_BASE_H
00027
00028
00029 #include <cmath>
00030 #include <string>
00031 #include <iostream>
00032 #include <map>
00033 #include <memory>
00034
00035
00036 #include "muParserDef.h"
00037 #include "muParserStack.h"
00038 #include "muParserTokenReader.h"
00039 #include "muParserBytecode.h"
00040 #include "muParserError.h"
00041
00042
00043 namespace mu
00044 {
00049
00063 class ParserBase
00064 {
00065 friend class ParserTokenReader;
00066
00067 private:
00068
00075 typedef value_type (ParserBase::*ParseFunction)() const;
00076
00078 typedef std::vector<string_type> stringbuf_type;
00079
00081 typedef ParserTokenReader token_reader_type;
00082
00084 typedef ParserToken<value_type, string_type> token_type;
00085
00086 public:
00087
00092 typedef ParserError exception_type;
00093
00094 ParserBase();
00095 ParserBase(const ParserBase &a_Parser);
00096 ParserBase& operator=(const ParserBase &a_Parser);
00097
00098 virtual ~ParserBase();
00099
00100
00116 inline value_type Eval() const
00117 {
00118 return (this->*m_pParseFormula)();
00119 }
00120
00121 void SetExpr(const string_type &a_sExpr);
00122 void SetVarFactory(facfun_type a_pFactory, void *pUserData = NULL);
00123
00124 void EnableOptimizer(bool a_bIsOn=true);
00125 void EnableByteCode(bool a_bIsOn=true);
00126 void EnableBuiltInOprt(bool a_bIsOn=true);
00127
00128 bool HasBuiltInOprt() const;
00129 void AddValIdent(identfun_type a_pCallback);
00130
00131 #define MUP_DEFINE_FUNC(TYPE) \
00132 inline void DefineFun(const string_type &a_strName, TYPE a_pFun, bool a_bAllowOpt = true) \
00133 { \
00134 AddCallback( a_strName, ParserCallback(a_pFun, a_bAllowOpt), \
00135 m_FunDef, ValidNameChars() ); \
00136 }
00137
00138 MUP_DEFINE_FUNC(fun_type0)
00139 MUP_DEFINE_FUNC(fun_type1)
00140 MUP_DEFINE_FUNC(fun_type2)
00141 MUP_DEFINE_FUNC(fun_type3)
00142 MUP_DEFINE_FUNC(fun_type4)
00143 MUP_DEFINE_FUNC(fun_type5)
00144 MUP_DEFINE_FUNC(multfun_type)
00145 MUP_DEFINE_FUNC(strfun_type1)
00146 MUP_DEFINE_FUNC(strfun_type2)
00147 MUP_DEFINE_FUNC(strfun_type3)
00148 #undef MUP_DEFINE_FUNC
00149
00150 void DefineOprt(const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, bool a_bAllowOpt = false);
00151 void DefineConst(const string_type &a_sName, value_type a_fVal);
00152 void DefineStrConst(const string_type &a_sName, const string_type &a_strVal);
00153 void DefineVar(const string_type &a_sName, value_type *a_fVar);
00154 void DefinePostfixOprt(const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true);
00155 void DefineInfixOprt(const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true);
00156
00157
00158 void ClearVar();
00159 void ClearFun();
00160 void ClearConst();
00161 void ClearInfixOprt();
00162 void ClearPostfixOprt();
00163 void ClearOprt();
00164
00165 void RemoveVar(const string_type &a_strVarName);
00166 const varmap_type& GetUsedVar() const;
00167 const varmap_type& GetVar() const;
00168 const valmap_type& GetConst() const;
00169 const string_type& GetExpr() const;
00170 const funmap_type& GetFunDef() const;
00171
00172 const char_type ** GetOprtDef() const;
00173 void DefineNameChars(const char_type *a_szCharset);
00174 void DefineOprtChars(const char_type *a_szCharset);
00175 void DefineInfixOprtChars(const char_type *a_szCharset);
00176
00177 const char_type* ValidNameChars() const;
00178 const char_type* ValidOprtChars() const;
00179 const char_type* ValidInfixOprtChars() const;
00180
00181 void SetArgSep(char_type cArgSep);
00182 char_type GetArgSep() const;
00183
00184 void Error(EErrorCodes a_iErrc,
00185 int a_iPos = (int)mu::string_type::npos,
00186 const string_type &a_strTok = string_type() ) const;
00187
00188 protected:
00189
00190 void Init();
00191
00192 virtual void InitCharSets() = 0;
00193 virtual void InitFun() = 0;
00194 virtual void InitConst() = 0;
00195 virtual void InitOprt() = 0;
00196
00197 static char_type *c_DefaultOprt[];
00198
00199 private:
00200
00201 void Assign(const ParserBase &a_Parser);
00202 void InitTokenReader();
00203 void ReInit() const;
00204
00205 void AddCallback( const string_type &a_strName,
00206 const ParserCallback &a_Callback,
00207 funmap_type &a_Storage,
00208 const char_type *a_szCharSet );
00209
00210 void ApplyBinOprt(ParserStack<token_type> &a_stOpt,
00211 ParserStack<token_type> &a_stVal) const;
00212
00213 void ApplyFunc(ParserStack<token_type> &a_stOpt,
00214 ParserStack<token_type> &a_stVal,
00215 int iArgCount) const;
00216
00217 token_type ApplyNumFunc(const token_type &a_FunTok,
00218 const std::vector<token_type> &a_vArg) const;
00219
00220 token_type ApplyStrFunc(const token_type &a_FunTok,
00221 const std::vector<token_type> &a_vArg) const;
00222
00223 int GetOprtPri(const token_type &a_Tok) const;
00224
00225 value_type ParseString() const;
00226 value_type ParseCmdCode() const;
00227 value_type ParseValue() const;
00228
00229 void ClearFormula();
00230 void CheckName(const string_type &a_strName, const string_type &a_CharSet) const;
00231
00232 #if defined(MUP_DUMP_STACK) | defined(MUP_DUMP_CMDCODE)
00233 void StackDump(const ParserStack<token_type > &a_stVal,
00234 const ParserStack<token_type > &a_stOprt) const;
00235 #endif
00236
00241 mutable ParseFunction m_pParseFormula;
00242 mutable const ParserByteCode::map_type *m_pCmdCode;
00243 mutable ParserByteCode m_vByteCode;
00244 mutable stringbuf_type m_vStringBuf;
00245 stringbuf_type m_vStringVarBuf;
00246
00247 std::auto_ptr<token_reader_type> m_pTokenReader;
00248
00249 funmap_type m_FunDef;
00250 funmap_type m_PostOprtDef;
00251 funmap_type m_InfixOprtDef;
00252 funmap_type m_OprtDef;
00253 valmap_type m_ConstDef;
00254 strmap_type m_StrVarDef;
00255 varmap_type m_VarDef;
00256
00257 bool m_bOptimize;
00258 bool m_bUseByteCode;
00259 bool m_bBuiltInOp;
00260
00261 string_type m_sNameChars;
00262 string_type m_sOprtChars;
00263 string_type m_sInfixOprtChars;
00264 };
00265
00266 }
00267
00268 #endif
00269