2017-08-22 22:34:41 +00:00
|
|
|
/*
|
2017-11-18 21:49:52 +00:00
|
|
|
__________
|
|
|
|
_____ __ __\______ \_____ _______ ______ ____ _______
|
2017-08-22 22:34:41 +00:00
|
|
|
/ \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
|
|
|
|
| Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
|
2017-11-18 21:49:52 +00:00
|
|
|
|__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
|
|
|
|
\/ \/ \/ \/
|
2017-08-22 22:34:41 +00:00
|
|
|
Copyright (C) 2013 Ingo Berg
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
2017-08-22 22:34:41 +00:00
|
|
|
software and associated documentation files (the "Software"), to deal in the Software
|
2017-11-18 21:49:52 +00:00
|
|
|
without restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
2017-08-22 22:34:41 +00:00
|
|
|
permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
The above copyright notice and this permission notice shall be included in all copies or
|
2017-08-22 22:34:41 +00:00
|
|
|
substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
2017-11-18 21:49:52 +00:00
|
|
|
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2017-08-22 22:34:41 +00:00
|
|
|
*/
|
|
|
|
#ifndef MU_PARSER_BASE_H
|
|
|
|
#define MU_PARSER_BASE_H
|
|
|
|
|
|
|
|
//--- Standard includes ------------------------------------------------------------------------
|
2017-11-18 21:49:52 +00:00
|
|
|
#include <limits.h>
|
2017-08-22 22:34:41 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2017-11-18 21:49:52 +00:00
|
|
|
#include <locale>
|
2017-08-22 22:34:41 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2017-11-18 21:49:52 +00:00
|
|
|
#include <string>
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
//--- Parser includes --------------------------------------------------------------------------
|
2017-11-18 21:49:52 +00:00
|
|
|
#include "muParserBytecode.h"
|
2017-08-22 22:34:41 +00:00
|
|
|
#include "muParserDef.h"
|
|
|
|
#include "muParserTokenReader.h"
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
namespace mu {
|
2017-08-22 22:34:41 +00:00
|
|
|
/** \file
|
|
|
|
\brief This file contains the class definition of the muparser engine.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
/** \brief Mathematical expressions parser (base parser engine).
|
|
|
|
\author (C) 2013 Ingo Berg
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
This is the implementation of a bytecode based mathematical expressions parser.
|
|
|
|
The formula will be parsed from string and converted into a bytecode.
|
2017-08-22 22:34:41 +00:00
|
|
|
Future calculations will be done with the bytecode instead the formula string
|
2017-11-18 21:49:52 +00:00
|
|
|
resulting in a significant performance increase.
|
|
|
|
Complementary to a set of internally implemented functions the parser is able to handle
|
|
|
|
user defined functions and variables.
|
2017-08-22 22:34:41 +00:00
|
|
|
*/
|
2017-11-18 21:49:52 +00:00
|
|
|
class ParserBase {
|
|
|
|
friend class ParserTokenReader;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
private:
|
|
|
|
/** \brief Typedef for the parse functions.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
The parse function do the actual work. The parser exchanges
|
2017-11-18 21:49:52 +00:00
|
|
|
the function pointer to the parser function depending on
|
2017-08-22 22:34:41 +00:00
|
|
|
which state it is in. (i.e. bytecode parser vs. string parser)
|
|
|
|
*/
|
2017-11-18 21:49:52 +00:00
|
|
|
typedef value_type (ParserBase::*ParseFunction)() const;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
/** \brief Type used for storing an array of values. */
|
|
|
|
typedef std::vector<value_type> valbuf_type;
|
|
|
|
|
|
|
|
/** \brief Type for a vector of strings. */
|
|
|
|
typedef std::vector<string_type> stringbuf_type;
|
|
|
|
|
|
|
|
/** \brief Typedef for the token reader. */
|
|
|
|
typedef ParserTokenReader token_reader_type;
|
2017-11-18 21:49:52 +00:00
|
|
|
|
2017-08-22 22:34:41 +00:00
|
|
|
/** \brief Type used for parser tokens. */
|
|
|
|
typedef ParserToken<value_type, string_type> token_type;
|
|
|
|
|
|
|
|
/** \brief Maximum number of threads spawned by OpenMP when using the bulk mode. */
|
|
|
|
static const int s_MaxNumOpenMPThreads = 16;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
public:
|
|
|
|
/** \brief Type of the error class.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
Included for backwards compatibility.
|
|
|
|
*/
|
|
|
|
typedef ParserError exception_type;
|
|
|
|
|
|
|
|
static void EnableDebugDump(bool bDumpCmd, bool bDumpStack);
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
ParserBase();
|
2017-08-22 22:34:41 +00:00
|
|
|
ParserBase(const ParserBase &a_Parser);
|
2017-11-18 21:49:52 +00:00
|
|
|
ParserBase &operator=(const ParserBase &a_Parser);
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
virtual ~ParserBase();
|
2017-11-18 21:49:52 +00:00
|
|
|
|
|
|
|
value_type Eval() const;
|
|
|
|
value_type *Eval(int &nStackSize) const;
|
2017-08-22 22:34:41 +00:00
|
|
|
void Eval(value_type *results, int nBulkSize);
|
|
|
|
|
|
|
|
int GetNumResults() const;
|
|
|
|
|
|
|
|
void SetExpr(const string_type &a_sExpr);
|
|
|
|
void SetVarFactory(facfun_type a_pFactory, void *pUserData = NULL);
|
|
|
|
|
|
|
|
void SetDecSep(char_type cDecSep);
|
|
|
|
void SetThousandsSep(char_type cThousandsSep = 0);
|
|
|
|
void ResetLocale();
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void EnableOptimizer(bool a_bIsOn = true);
|
|
|
|
void EnableBuiltInOprt(bool a_bIsOn = true);
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
bool HasBuiltInOprt() const;
|
|
|
|
void AddValIdent(identfun_type a_pCallback);
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
/** \fn void mu::ParserBase::DefineFun(const string_type &a_strName, fun_type0 a_pFun, bool
|
|
|
|
a_bAllowOpt = true)
|
2017-08-22 22:34:41 +00:00
|
|
|
\brief Define a parser function without arguments.
|
|
|
|
\param a_strName Name of the function
|
|
|
|
\param a_pFun Pointer to the callback function
|
|
|
|
\param a_bAllowOpt A flag indicating this function may be optimized
|
|
|
|
*/
|
2017-11-18 21:49:52 +00:00
|
|
|
template <typename T>
|
|
|
|
void DefineFun(const string_type &a_strName, T a_pFun, bool a_bAllowOpt = true) {
|
|
|
|
AddCallback(a_strName, ParserCallback(a_pFun, a_bAllowOpt), m_FunDef, ValidNameChars());
|
2017-08-22 22:34:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void DefineOprt(const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri = 0,
|
|
|
|
EOprtAssociativity a_eAssociativity = oaLEFT, bool a_bAllowOpt = false);
|
2017-08-22 22:34:41 +00:00
|
|
|
void DefineConst(const string_type &a_sName, value_type a_fVal);
|
|
|
|
void DefineStrConst(const string_type &a_sName, const string_type &a_strVal);
|
|
|
|
void DefineVar(const string_type &a_sName, value_type *a_fVar);
|
2017-11-18 21:49:52 +00:00
|
|
|
void DefinePostfixOprt(const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt = true);
|
|
|
|
void DefineInfixOprt(const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec = prINFIX,
|
|
|
|
bool a_bAllowOpt = true);
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
// Clear user defined variables, constants or functions
|
|
|
|
void ClearVar();
|
|
|
|
void ClearFun();
|
|
|
|
void ClearConst();
|
|
|
|
void ClearInfixOprt();
|
|
|
|
void ClearPostfixOprt();
|
|
|
|
void ClearOprt();
|
2017-11-18 21:49:52 +00:00
|
|
|
|
2017-08-22 22:34:41 +00:00
|
|
|
void RemoveVar(const string_type &a_strVarName);
|
2017-11-18 21:49:52 +00:00
|
|
|
const varmap_type &GetUsedVar() const;
|
|
|
|
const varmap_type &GetVar() const;
|
|
|
|
const valmap_type &GetConst() const;
|
|
|
|
const string_type &GetExpr() const;
|
|
|
|
const funmap_type &GetFunDef() const;
|
2017-08-22 22:34:41 +00:00
|
|
|
string_type GetVersion(EParserVersionInfo eInfo = pviFULL) const;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
const char_type **GetOprtDef() const;
|
2017-08-22 22:34:41 +00:00
|
|
|
void DefineNameChars(const char_type *a_szCharset);
|
|
|
|
void DefineOprtChars(const char_type *a_szCharset);
|
|
|
|
void DefineInfixOprtChars(const char_type *a_szCharset);
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
const char_type *ValidNameChars() const;
|
|
|
|
const char_type *ValidOprtChars() const;
|
|
|
|
const char_type *ValidInfixOprtChars() const;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
void SetArgSep(char_type cArgSep);
|
|
|
|
char_type GetArgSep() const;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void Error(EErrorCodes a_iErrc, int a_iPos = (int)mu::string_type::npos,
|
|
|
|
const string_type &a_strTok = string_type()) const;
|
|
|
|
|
|
|
|
protected:
|
2017-08-22 22:34:41 +00:00
|
|
|
void Init();
|
|
|
|
|
|
|
|
virtual void InitCharSets() = 0;
|
|
|
|
virtual void InitFun() = 0;
|
|
|
|
virtual void InitConst() = 0;
|
2017-11-18 21:49:52 +00:00
|
|
|
virtual void InitOprt() = 0;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
virtual void OnDetectVar(string_type *pExpr, int &nStart, int &nEnd);
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
static const char_type *c_DefaultOprt[];
|
2017-08-22 22:34:41 +00:00
|
|
|
static std::locale s_locale; ///< The locale used by the parser
|
|
|
|
static bool g_DbgDumpCmdCode;
|
|
|
|
static bool g_DbgDumpStack;
|
|
|
|
|
|
|
|
/** \brief A facet class used to change decimal and thousands separator. */
|
2017-11-18 21:49:52 +00:00
|
|
|
template <class TChar>
|
|
|
|
class change_dec_sep : public std::numpunct<TChar> {
|
|
|
|
public:
|
|
|
|
explicit change_dec_sep(char_type cDecSep, char_type cThousandsSep = 0, int nGroup = 3)
|
|
|
|
: std::numpunct<TChar>(),
|
|
|
|
m_nGroup(nGroup),
|
|
|
|
m_cDecPoint(cDecSep),
|
|
|
|
m_cThousandsSep(cThousandsSep) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual char_type do_decimal_point() const { return m_cDecPoint; }
|
|
|
|
|
|
|
|
virtual char_type do_thousands_sep() const { return m_cThousandsSep; }
|
|
|
|
|
|
|
|
virtual std::string do_grouping() const {
|
|
|
|
// fix for issue 4: https://code.google.com/p/muparser/issues/detail?id=4
|
|
|
|
// courtesy of Jens Bartsch
|
|
|
|
// original code:
|
|
|
|
// return std::string(1, (char)m_nGroup);
|
|
|
|
// new code:
|
|
|
|
return std::string(1, (char)(m_cThousandsSep > 0 ? m_nGroup : CHAR_MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_nGroup;
|
|
|
|
char_type m_cDecPoint;
|
|
|
|
char_type m_cThousandsSep;
|
2017-08-22 22:34:41 +00:00
|
|
|
};
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
private:
|
2017-08-22 22:34:41 +00:00
|
|
|
void Assign(const ParserBase &a_Parser);
|
|
|
|
void InitTokenReader();
|
|
|
|
void ReInit() const;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void AddCallback(const string_type &a_strName, const ParserCallback &a_Callback,
|
|
|
|
funmap_type &a_Storage, const char_type *a_szCharSet);
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
void ApplyRemainingOprt(ParserStack<token_type> &a_stOpt,
|
2017-11-18 21:49:52 +00:00
|
|
|
ParserStack<token_type> &a_stVal) const;
|
|
|
|
void ApplyBinOprt(ParserStack<token_type> &a_stOpt, ParserStack<token_type> &a_stVal) const;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void ApplyIfElse(ParserStack<token_type> &a_stOpt, ParserStack<token_type> &a_stVal) const;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void ApplyFunc(ParserStack<token_type> &a_stOpt, ParserStack<token_type> &a_stVal,
|
|
|
|
int iArgCount) const;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
token_type ApplyStrFunc(const token_type &a_FunTok,
|
|
|
|
const std::vector<token_type> &a_vArg) const;
|
|
|
|
|
|
|
|
int GetOprtPrecedence(const token_type &a_Tok) const;
|
|
|
|
EOprtAssociativity GetOprtAssociativity(const token_type &a_Tok) const;
|
|
|
|
|
|
|
|
void CreateRPN() const;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
value_type ParseString() const;
|
2017-08-22 22:34:41 +00:00
|
|
|
value_type ParseCmdCode() const;
|
|
|
|
value_type ParseCmdCodeBulk(int nOffset, int nThreadID) const;
|
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
void CheckName(const string_type &a_strName, const string_type &a_CharSet) const;
|
|
|
|
void CheckOprt(const string_type &a_sName, const ParserCallback &a_Callback,
|
|
|
|
const string_type &a_szCharSet) const;
|
|
|
|
|
|
|
|
void StackDump(const ParserStack<token_type> &a_stVal,
|
|
|
|
const ParserStack<token_type> &a_stOprt) const;
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
/** \brief Pointer to the parser function.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
Eval() calls the function whose address is stored there.
|
|
|
|
*/
|
2017-11-18 21:49:52 +00:00
|
|
|
mutable ParseFunction m_pParseFormula;
|
|
|
|
mutable ParserByteCode m_vRPN; ///< The Bytecode class.
|
|
|
|
mutable stringbuf_type
|
|
|
|
m_vStringBuf; ///< String buffer, used for storing string function arguments
|
|
|
|
stringbuf_type m_vStringVarBuf;
|
|
|
|
|
|
|
|
std::auto_ptr<token_reader_type>
|
|
|
|
m_pTokenReader; ///< Managed pointer to the token reader object.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
funmap_type m_FunDef; ///< Map of function names and pointers.
|
|
|
|
funmap_type m_PostOprtDef; ///< Postfix operator callbacks
|
|
|
|
funmap_type m_InfixOprtDef; ///< unary infix operator.
|
|
|
|
funmap_type m_OprtDef; ///< Binary operator callbacks
|
|
|
|
valmap_type m_ConstDef; ///< user constants.
|
|
|
|
strmap_type m_StrVarDef; ///< user defined string constants
|
|
|
|
varmap_type m_VarDef; ///< user defind variables.
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
bool m_bBuiltInOp; ///< Flag that can be used for switching built in operators on and off
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
string_type m_sNameChars; ///< Charset for names
|
|
|
|
string_type m_sOprtChars; ///< Charset for postfix/ binary operator tokens
|
|
|
|
string_type m_sInfixOprtChars; ///< Charset for infix operator tokens
|
2017-08-22 22:34:41 +00:00
|
|
|
|
2017-11-18 21:49:52 +00:00
|
|
|
mutable int
|
|
|
|
m_nIfElseCounter; ///< Internal counter for keeping track of nested if-then-else clauses
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
// items merely used for caching state information
|
2017-11-18 21:49:52 +00:00
|
|
|
mutable valbuf_type
|
|
|
|
m_vStackBuffer; ///< This is merely a buffer used for the stack in the cmd parsing routine
|
2017-08-22 22:34:41 +00:00
|
|
|
mutable int m_nFinalResultIdx;
|
|
|
|
};
|
2017-11-18 22:16:14 +00:00
|
|
|
} // namespace mu
|
2017-08-22 22:34:41 +00:00
|
|
|
|
|
|
|
#endif
|