22 #ifndef QMUQPARSERBASE_H
23 #define QMUQPARSERBASE_H
26 #include <qcompilerdetection.h>
31 #include <QStringList>
76 qreal*
Eval(
int &nStackSize)
const;
77 void Eval(qreal *results,
int nBulkSize)
const;
79 void SetExpr(
const QString &a_sExpr);
88 void DefineConst(
const QString &a_sName, qreal a_fVal);
89 void DefineStrConst(
const QString &a_strName,
const QString &a_strVal);
90 void DefineVar(
const QString &a_sName, qreal *a_pVar);
93 bool a_bAllowOpt=
true);
101 void RemoveVar(
const QString &a_strVarName);
105 const QString&
GetExpr()
const;
119 void Q_NORETURN
Error(
EErrorCodes a_iErrc,
int a_iPos = -1,
const QString &a_sTok = QString() )
const;
122 void DefineFun(
const QString &a_strName, T a_pFun,
bool a_bAllowOpt =
true);
147 virtual void OnDetectVar(
const QString &pExpr,
int &nStart,
int &nEnd);
151 template<
class TChar>
258 funmap_type &a_Storage,
const QString &a_szCharSet );
271 void CheckName(
const QString &a_sName,
const QString &a_szCharSet)
const;
274 const QString &a_szCharSet)
const;
A facet class used to change decimal and thousands separator.
virtual std::string do_grouping() const Q_DECL_OVERRIDE
virtual char_type do_thousands_sep() const Q_DECL_OVERRIDE
virtual char_type do_decimal_point() const Q_DECL_OVERRIDE
change_dec_sep(char_type cDecSep, char_type cThousandsSep=0, int nGroup=3)
char_type m_cThousandsSep
Mathematical expressions parser (base parser engine).
void setDecimalPoint(const QChar &c)
void CheckOprt(const QString &a_sName, const QmuParserCallback &a_Callback, const QString &a_szCharSet) const
Check if a name contains invalid characters.
void Assign(const QmuParserBase &a_Parser)
Copy state of a parser object to this.
void AddCallback(const QString &a_strName, const QmuParserCallback &a_Callback, funmap_type &a_Storage, const QString &a_szCharSet)
Add a function or operator callback to the parser.
void CheckName(const QString &a_sName, const QString &a_szCharSet) const
Check if a name contains invalid characters.
QLocale getLocale() const
void InitTokenReader()
Initialize the token reader.
funmap_type m_OprtDef
Binary operator callbacks.
void SetArgSep(char_type cArgSep)
Set argument separator.
valbuf_type m_vStackBuffer
This is merely a buffer used for the stack in the cmd parsing routine.
void RemoveVar(const QString &a_strVarName)
Remove a variable from internal storage.
QmuParserByteCode m_vRPN
The Bytecode class.
static QString GetVersion(EParserVersionInfo eInfo=pviFULL)
Returns the version of muparser.
stringbuf_type m_vStringVarBuf
void DefineStrConst(const QString &a_strName, const QString &a_strVal)
Define a new string constant.
void DefineOprt(const QString &a_sName, fun_type2 a_pFun, unsigned a_iPrec=0, EOprtAssociativity a_eAssociativity=oaLEFT, bool a_bAllowOpt=false)
Define a binary operator.
void EnableOptimizer(bool a_bIsOn=true)
Enable or disable the formula optimization feature.
void setAllowSubexpressions(bool value)
EOprtAssociativity GetOprtAssociativity(const token_type &a_Tok) const
Get operator priority.
const valmap_type & GetConst() const
Return a map containing all parser constants.
void DefineOprtChars(const QString &a_szCharset)
Define the set of valid characters to be used in names of binary operators and postfix operators.
static const QStringList c_DefaultOprt
Identifiers for built in binary operators.
valmap_type m_ConstDef
user constants.
const QString & GetExpr() const
Retrieve the formula.
const funmap_type & GetFunDef() const
Return prototypes of all parser functions.
int GetOprtPrecedence(const token_type &a_Tok) const
Get operator priority.
QmuParserTokenReader token_reader_type
Typedef for the token reader.
QMap< int, QString > GetTokens() const
strmap_type m_StrVarDef
user defined string constants
funmap_type m_FunDef
Map of function names and pointers.
void Q_NORETURN Error(EErrorCodes a_iErrc, int a_iPos=-1, const QString &a_sTok=QString()) const
Create an error containing the parse error position.
funmap_type m_PostOprtDef
Postfix operator callbacks.
QVector< QString > stringbuf_type
Type for a vector of strings.
void ReInit() const
Reset parser to string parsing mode and clear internal buffers.
void DefineFun(const QString &a_strName, T a_pFun, bool a_bAllowOpt=true)
Define a parser function without arguments.
void DefineVar(const QString &a_sName, qreal *a_pVar)
Add a user defined variable.
void ApplyRemainingOprt(QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal) const
Apply a binary operator.
virtual void InitConst()=0
const QString & ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
QString m_sInfixOprtChars
Charset for infix operator tokens.
void StackDump(const QStack< token_type > &a_stVal, const QStack< token_type > &a_stOprt) const
Dump stack content.
static void EnableDebugDump(bool bDumpCmd, bool bDumpStack)
Enable the dumping of bytecode amd stack content on the console.
QChar GetArgSep() const
Get the argument separator character.
static const int s_MaxNumOpenMPThreads
Maximum number of threads spawned by OpenMP when using the bulk mode.
QChar getDecimalPoint() const
void EnableBuiltInOprt(bool a_bIsOn=true)
Enable or disable the built in binary operators.
void ClearFun()
Clear all functions.
QmuParserToken< qreal, QString > token_type
Type used for parser tokens.
int m_nIfElseCounter
Internal counter for keeping track of nested if-then-else clauses.
void DefineInfixOprtChars(const QString &a_szCharset)
Define the set of valid characters to be used in names of infix operators.
qreal ParseCmdCodeBulk(int nOffset, int nThreadID) const
Evaluate the RPN.
void ClearVar()
Clear all user defined variables.
QLocale m_locale
The locale used by the parser.
QMap< int, QString > m_Numbers
Keep all numbers what exist in formula.
void Init()
Initialize user defined functions.
QChar getThousandsSeparator() const
void ApplyFunc(QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal, int iArgCount) const
Apply a function token.
void ClearConst()
Clear all user defined constants.
bool HasBuiltInOprt() const
Query status of built in variables.
funmap_type m_InfixOprtDef
unary infix operator.
static const QStringList & GetOprtDef()
Get the default symbols used for the built in operators.
bool m_bBuiltInOp
Flag that can be used for switching built in operators on and off.
static bool g_DbgDumpStack
const varmap_type & GetVar() const
Return a map containing the used variables only.
void ApplyBinOprt(QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal) const
Performs the necessary steps to write code for the execution of binary operators into the bytecode.
void setThousandsSeparator(const QChar &c)
void DefinePostfixOprt(const QString &a_sFun, fun_type1 a_pFun, bool a_bAllowOpt=true)
Add a user defined operator.
const varmap_type & GetUsedVar() const
Return a map containing the used variables only.
stringbuf_type m_vStringBuf
String buffer, used for storing string function arguments.
token_type ApplyStrFunc(const token_type &a_FunTok, const QVector< token_type > &a_vArg) const
Execute a function that takes a single string argument.
void SetExpr(const QString &a_sExpr)
Set the formula.
QMap< int, QString > m_Tokens
Keep all tokens that we can translate.
void setLocale(const QLocale &value)
void SetVarFactory(facfun_type a_pFactory, void *pUserData=nullptr)
Set a function that can create variable pointer for unknown expression variables.
void ClearPostfixOprt()
Clear all user defined postfix operators.
QChar m_thousandsSeparator
qreal ParseCmdCode() const
Parse the command code.
void ResetLocale()
Resets the locale.
virtual void OnDetectVar(const QString &pExpr, int &nStart, int &nEnd)
const QString & ValidInfixOprtChars() const
Virtual function that defines the characters allowed in infix operator definitions.
virtual void InitCharSets()=0
const QString & ValidOprtChars() const
Virtual function that defines the characters allowed in operator definitions.
QString m_sOprtChars
Charset for postfix/ binary operator tokens.
QmuParserBase()
Constructor.
qreal Eval() const
Calculate the result.
std::unique_ptr< token_reader_type > m_pTokenReader
Managed pointer to the token reader object.
void AddValIdent(identfun_type a_pCallback)
Add a value parsing function.
ParseFunction m_pParseFormula
Pointer to the parser function.
QMap< int, QString > GetNumbers() const
varmap_type m_VarDef
user defind variables.
qreal ParseString() const
One of the two main parse functions.
QVector< qreal > valbuf_type
Type used for storing an array of values.
void ClearOprt()
Clear all user defined binary operators.
static bool g_DbgDumpCmdCode
void ApplyIfElse(QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal) const
void ClearInfixOprt()
Clear the user defined Prefix operators.
void DefineConst(const QString &a_sName, qreal a_fVal)
Add a user defined constant.
QmuParserBase & operator=(const QmuParserBase &a_Parser)
Assignement operator.
virtual void InitOprt()=0
void DefineInfixOprt(const QString &a_sName, fun_type1 a_pFun, int a_iPrec=prINFIX, bool a_bAllowOpt=true)
Add a user defined operator.
int GetNumResults() const
Return the number of results on the calculation stack.
QString m_sNameChars
Charset for names.
qreal(QmuParserBase::* ParseFunction)() const
Typedef for the parse functions.
void DefineNameChars(const QString &a_szCharset)
Define the set of valid characters to be used in names of functions, variables, constants.
Bytecode implementation of the Math Parser.
Encapsulation of prototypes for a numerical parser function.
Token reader for the ParserBase class.
Namespace for mathematical applications.
std::map< QString, QmuParserCallback > funmap_type
Container for Callback objects.
std::map< QString, int > strmap_type
Type for assigning a string name to an index in the internal string table.
string_type::value_type char_type
The character type used by the parser.
qreal *(* facfun_type)(const QString &, void *)
Callback used for variable creation factory functions.
int(* identfun_type)(const QString &sExpr, int *nPos, qreal *fVal, const QLocale &locale, const QChar &decimal, const QChar &thousand)
Callback used for functions that identify values in a string.
std::map< QString, qreal > valmap_type
Type used for storing constants.
qreal(* fun_type2)(qreal, qreal)
Callback type used for functions with two arguments.
EOprtAssociativity
Parser operator precedence values.
qreal(* fun_type1)(qreal)
Callback type used for functions with a single arguments.
@ prINFIX
Signs have a higher priority than ADD_SUB, but lower than power operator.
std::map< QString, qreal * > varmap_type
Type used for storing variables.
Definition of the parser bytecode class.
Definition of the parser callback class.
This file contains standard definitions used by the parser.
This file defines the error class used by the parser.
This file contains the parser token definition.
This file contains the parser token reader definition.