Seamly2D
Code documentation
qmu::QmuParserBase Class Referenceabstract

Mathematical expressions parser (base parser engine). More...

#include <qmuparserbase.h>

Inheritance diagram for qmu::QmuParserBase:
Collaboration diagram for qmu::QmuParserBase:

Classes

class  change_dec_sep
 A facet class used to change decimal and thousands separator. More...
 

Public Member Functions

 QmuParserBase ()
 Constructor. More...
 
 QmuParserBase (const QmuParserBase &a_Parser)
 Copy constructor. More...
 
QmuParserBaseoperator= (const QmuParserBase &a_Parser)
 Assignement operator. More...
 
virtual ~QmuParserBase ()
 
qreal Eval () const
 Calculate the result. More...
 
qreal * Eval (int &nStackSize) const
 Evaluate an expression containing comma seperated subexpressions. More...
 
void Eval (qreal *results, int nBulkSize) const
 
int GetNumResults () const
 Return the number of results on the calculation stack. More...
 
void SetExpr (const QString &a_sExpr)
 Set the formula. More...
 
void SetVarFactory (facfun_type a_pFactory, void *pUserData=nullptr)
 Set a function that can create variable pointer for unknown expression variables. More...
 
void ResetLocale ()
 Resets the locale. More...
 
void EnableOptimizer (bool a_bIsOn=true)
 Enable or disable the formula optimization feature. More...
 
void EnableBuiltInOprt (bool a_bIsOn=true)
 Enable or disable the built in binary operators. More...
 
bool HasBuiltInOprt () const
 Query status of built in variables. More...
 
void AddValIdent (identfun_type a_pCallback)
 Add a value parsing function. More...
 
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. More...
 
void DefineConst (const QString &a_sName, qreal a_fVal)
 Add a user defined constant. More...
 
void DefineStrConst (const QString &a_strName, const QString &a_strVal)
 Define a new string constant. More...
 
void DefineVar (const QString &a_sName, qreal *a_pVar)
 Add a user defined variable. More...
 
void DefinePostfixOprt (const QString &a_sFun, fun_type1 a_pFun, bool a_bAllowOpt=true)
 Add a user defined operator. More...
 
void DefineInfixOprt (const QString &a_sName, fun_type1 a_pFun, int a_iPrec=prINFIX, bool a_bAllowOpt=true)
 Add a user defined operator. More...
 
void ClearVar ()
 Clear all user defined variables. More...
 
void ClearFun ()
 Clear all functions. More...
 
void ClearConst ()
 Clear all user defined constants. More...
 
void ClearInfixOprt ()
 Clear the user defined Prefix operators. More...
 
void ClearPostfixOprt ()
 Clear all user defined postfix operators. More...
 
void ClearOprt ()
 Clear all user defined binary operators. More...
 
void RemoveVar (const QString &a_strVarName)
 Remove a variable from internal storage. More...
 
const varmap_typeGetUsedVar () const
 Return a map containing the used variables only. More...
 
const varmap_typeGetVar () const
 Return a map containing the used variables only. More...
 
const valmap_typeGetConst () const
 Return a map containing all parser constants. More...
 
const QString & GetExpr () const
 Retrieve the formula. More...
 
const funmap_typeGetFunDef () const
 Return prototypes of all parser functions. More...
 
QMap< int, QString > GetTokens () const
 
QMap< int, QString > GetNumbers () const
 
void DefineNameChars (const QString &a_szCharset)
 Define the set of valid characters to be used in names of functions, variables, constants. More...
 
void DefineOprtChars (const QString &a_szCharset)
 Define the set of valid characters to be used in names of binary operators and postfix operators. More...
 
void DefineInfixOprtChars (const QString &a_szCharset)
 Define the set of valid characters to be used in names of infix operators. More...
 
const QString & ValidNameChars () const
 Virtual function that defines the characters allowed in name identifiers. More...
 
const QString & ValidOprtChars () const
 Virtual function that defines the characters allowed in operator definitions. More...
 
const QString & ValidInfixOprtChars () const
 Virtual function that defines the characters allowed in infix operator definitions. More...
 
void SetArgSep (char_type cArgSep)
 Set argument separator. More...
 
QChar GetArgSep () const
 Get the argument separator character. More...
 
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. More...
 
template<typename T >
void DefineFun (const QString &a_strName, T a_pFun, bool a_bAllowOpt=true)
 Define a parser function without arguments. More...
 
void setAllowSubexpressions (bool value)
 
QLocale getLocale () const
 
void setLocale (const QLocale &value)
 
QChar getDecimalPoint () const
 
void setDecimalPoint (const QChar &c)
 
QChar getThousandsSeparator () const
 
void setThousandsSeparator (const QChar &c)
 

Static Public Member Functions

static void EnableDebugDump (bool bDumpCmd, bool bDumpStack)
 Enable the dumping of bytecode amd stack content on the console. More...
 
static QString GetVersion (EParserVersionInfo eInfo=pviFULL)
 Returns the version of muparser. More...
 
static const QStringList & GetOprtDef ()
 Get the default symbols used for the built in operators. More...
 

Protected Member Functions

void Init ()
 Initialize user defined functions. More...
 
virtual void InitCharSets ()=0
 
virtual void InitFun ()=0
 
virtual void InitConst ()=0
 
virtual void InitOprt ()=0
 
virtual void OnDetectVar (const QString &pExpr, int &nStart, int &nEnd)
 

Protected Attributes

QLocale m_locale
 The locale used by the parser. More...
 
QChar m_decimalPoint
 
QChar m_thousandsSeparator
 

Static Protected Attributes

static const QStringList c_DefaultOprt
 Identifiers for built in binary operators. More...
 
static bool g_DbgDumpCmdCode = false
 
static bool g_DbgDumpStack = false
 

Private Types

typedef qreal(QmuParserBase::* ParseFunction) () const
 Typedef for the parse functions. More...
 
typedef QVector< qreal > valbuf_type
 Type used for storing an array of values. More...
 
typedef QVector< QString > stringbuf_type
 Type for a vector of strings. More...
 
typedef QmuParserTokenReader token_reader_type
 Typedef for the token reader. More...
 
typedef QmuParserToken< qreal, QString > token_type
 Type used for parser tokens. More...
 

Private Member Functions

void Assign (const QmuParserBase &a_Parser)
 Copy state of a parser object to this. More...
 
void InitTokenReader ()
 Initialize the token reader. More...
 
void ReInit () const
 Reset parser to string parsing mode and clear internal buffers. More...
 
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. More...
 
void ApplyRemainingOprt (QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal) const
 Apply a binary operator. More...
 
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. More...
 
void ApplyIfElse (QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal) const
 
void ApplyFunc (QStack< token_type > &a_stOpt, QStack< token_type > &a_stVal, int iArgCount) const
 Apply a function token. More...
 
token_type ApplyStrFunc (const token_type &a_FunTok, const QVector< token_type > &a_vArg) const
 Execute a function that takes a single string argument. More...
 
int GetOprtPrecedence (const token_type &a_Tok) const
 Get operator priority. More...
 
EOprtAssociativity GetOprtAssociativity (const token_type &a_Tok) const
 Get operator priority. More...
 
void CreateRPN () const
 
qreal ParseString () const
 One of the two main parse functions. More...
 
qreal ParseCmdCode () const
 Parse the command code. More...
 
qreal ParseCmdCodeBulk (int nOffset, int nThreadID) const
 Evaluate the RPN. More...
 
void CheckName (const QString &a_sName, const QString &a_szCharSet) const
 Check if a name contains invalid characters. More...
 
void CheckOprt (const QString &a_sName, const QmuParserCallback &a_Callback, const QString &a_szCharSet) const
 Check if a name contains invalid characters. More...
 
void StackDump (const QStack< token_type > &a_stVal, const QStack< token_type > &a_stOprt) const
 Dump stack content. More...
 

Private Attributes

ParseFunction m_pParseFormula
 Pointer to the parser function. More...
 
QmuParserByteCode m_vRPN
 The Bytecode class. More...
 
stringbuf_type m_vStringBuf
 String buffer, used for storing string function arguments. More...
 
stringbuf_type m_vStringVarBuf
 
std::unique_ptr< token_reader_typem_pTokenReader
 Managed pointer to the token reader object. More...
 
funmap_type m_FunDef
 Map of function names and pointers. More...
 
funmap_type m_PostOprtDef
 Postfix operator callbacks. More...
 
funmap_type m_InfixOprtDef
 unary infix operator. More...
 
funmap_type m_OprtDef
 Binary operator callbacks. More...
 
valmap_type m_ConstDef
 user constants. More...
 
strmap_type m_StrVarDef
 user defined string constants More...
 
varmap_type m_VarDef
 user defind variables. More...
 
bool m_bBuiltInOp
 Flag that can be used for switching built in operators on and off. More...
 
QString m_sNameChars
 Charset for names. More...
 
QString m_sOprtChars
 Charset for postfix/ binary operator tokens. More...
 
QString m_sInfixOprtChars
 Charset for infix operator tokens. More...
 
int m_nIfElseCounter
 Internal counter for keeping track of nested if-then-else clauses. More...
 
valbuf_type m_vStackBuffer
 This is merely a buffer used for the stack in the cmd parsing routine. More...
 
int m_nFinalResultIdx
 
QMap< int, QString > m_Tokens
 Keep all tokens that we can translate. More...
 
QMap< int, QString > m_Numbers
 Keep all numbers what exist in formula. More...
 
bool allowSubexpressions
 

Static Private Attributes

static const int s_MaxNumOpenMPThreads = 4
 Maximum number of threads spawned by OpenMP when using the bulk mode. More...
 

Friends

class QmuParserTokenReader
 

Detailed Description

Mathematical expressions parser (base parser engine).

Author
(C) 2013 Ingo Berg

This is the implementation of a bytecode based mathematical expressions parser. The formula will be parsed from string and converted into a bytecode. Future calculations will be done with the bytecode instead the formula string 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.

Definition at line 65 of file qmuparserbase.h.

Member Typedef Documentation

◆ ParseFunction

typedef qreal(QmuParserBase::* qmu::QmuParserBase::ParseFunction) () const
private

Typedef for the parse functions.

The parse function do the actual work. The parser exchanges the function pointer to the parser function depending on which state it is in. (i.e. bytecode parser vs. string parser)

Definition at line 191 of file qmuparserbase.h.

◆ stringbuf_type

typedef QVector<QString> qmu::QmuParserBase::stringbuf_type
private

Type for a vector of strings.

Definition at line 201 of file qmuparserbase.h.

◆ token_reader_type

Typedef for the token reader.

Definition at line 206 of file qmuparserbase.h.

◆ token_type

typedef QmuParserToken<qreal, QString> qmu::QmuParserBase::token_type
private

Type used for parser tokens.

Definition at line 211 of file qmuparserbase.h.

◆ valbuf_type

typedef QVector<qreal> qmu::QmuParserBase::valbuf_type
private

Type used for storing an array of values.

Definition at line 196 of file qmuparserbase.h.

Constructor & Destructor Documentation

◆ QmuParserBase() [1/2]

qmu::QmuParserBase::QmuParserBase ( )

Constructor.

Definition at line 64 of file qmuparserbase.cpp.

References InitTokenReader().

◆ QmuParserBase() [2/2]

qmu::QmuParserBase::QmuParserBase ( const QmuParserBase a_Parser)
explicit

Copy constructor.

Tha parser can be safely copy constructed but the bytecode is reset during copy construction.

Definition at line 100 of file qmuparserbase.cpp.

References Assign(), and m_pTokenReader.

◆ ~QmuParserBase()

qmu::QmuParserBase::~QmuParserBase ( )
virtual

Definition at line 132 of file qmuparserbase.cpp.

Member Function Documentation

◆ AddCallback()

void qmu::QmuParserBase::AddCallback ( const QString &  a_strName,
const QmuParserCallback a_Callback,
funmap_type a_Storage,
const QString &  a_szCharSet 
)
private

◆ AddValIdent()

void qmu::QmuParserBase::AddValIdent ( identfun_type  a_pCallback)
inline

Add a value parsing function.

When parsing an expression muParser tries to detect values in the expression string using different valident callbacks. Thuis it's possible to parse for hex values, binary values and floating point values.

Definition at line 313 of file qmuparserbase.h.

References m_pTokenReader.

Referenced by qmu::QmuParser::QmuParser().

◆ ApplyBinOprt()

void qmu::QmuParserBase::ApplyBinOprt ( QStack< token_type > &  a_stOpt,
QStack< token_type > &  a_stVal 
) const
private

◆ ApplyFunc()

void qmu::QmuParserBase::ApplyFunc ( QStack< token_type > &  a_stOpt,
QStack< token_type > &  a_stVal,
int  a_iArgCount 
) const
private

◆ ApplyIfElse()

void qmu::QmuParserBase::ApplyIfElse ( QStack< token_type > &  a_stOpt,
QStack< token_type > &  a_stVal 
) const
private

◆ ApplyRemainingOprt()

void qmu::QmuParserBase::ApplyRemainingOprt ( QStack< token_type > &  stOpt,
QStack< token_type > &  stVal 
) const
private

Apply a binary operator.

Parameters
stOptThe operator stack
stValThe value stack

Definition at line 962 of file qmuparserbase.cpp.

References ApplyBinOprt(), ApplyFunc(), ApplyIfElse(), qmu::cmASSIGN, qmu::cmBO, qmu::cmELSE, qmu::cmIF, qmu::cmLE, qmu::cmOPRT_BIN, qmu::cmOPRT_INFIX, qmu::ecINTERNAL_ERROR, and Error().

◆ ApplyStrFunc()

QmuParserBase::token_type qmu::QmuParserBase::ApplyStrFunc ( const token_type a_FunTok,
const QVector< token_type > &  a_vArg 
) const
private

◆ Assign()

void qmu::QmuParserBase::Assign ( const QmuParserBase a_Parser)
private

Copy state of a parser object to this.

Clears Variables and Functions of this parser. Copies the states of all internal variables. Resets parse function to string parse mode.

Parameters
a_Parserthe source object.

Definition at line 163 of file qmuparserbase.cpp.

References m_bBuiltInOp, m_ConstDef, m_FunDef, m_InfixOprtDef, m_nFinalResultIdx, m_nIfElseCounter, m_OprtDef, m_PostOprtDef, m_pTokenReader, m_sInfixOprtChars, m_sNameChars, m_sOprtChars, m_StrVarDef, m_VarDef, m_vStackBuffer, m_vStringBuf, m_vStringVarBuf, and ReInit().

Referenced by operator=(), and QmuParserBase().

◆ CheckName()

void qmu::QmuParserBase::CheckName ( const QString &  a_sName,
const QString &  a_szCharSet 
) const
private

Check if a name contains invalid characters.

Exceptions
ParserExceptionif the name contains invalid characters.

Definition at line 411 of file qmuparserbase.cpp.

References qmu::ecINVALID_NAME, and Error().

Referenced by DefineConst(), DefineStrConst(), and DefineVar().

◆ CheckOprt()

void qmu::QmuParserBase::CheckOprt ( const QString &  a_sName,
const QmuParserCallback a_Callback,
const QString &  a_szCharSet 
) const
private

Check if a name contains invalid characters.

Exceptions
ParserExceptionif the name contains invalid charakters.

Definition at line 381 of file qmuparserbase.cpp.

References qmu::cmOPRT_INFIX, qmu::cmOPRT_POSTFIX, qmu::ecINVALID_INFIX_IDENT, qmu::ecINVALID_NAME, qmu::ecINVALID_POSTFIX_IDENT, Error(), and qmu::QmuParserCallback::GetCode().

Referenced by AddCallback().

◆ ClearConst()

void qmu::QmuParserBase::ClearConst ( )

Clear all user defined constants.

Both numeric and string constants will be removed from the internal storage.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1702 of file qmuparserbase.cpp.

Referenced by qmu::Test::QmuParserTester::TestNames().

◆ ClearFun()

void qmu::QmuParserBase::ClearFun ( )

Clear all functions.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1688 of file qmuparserbase.cpp.

◆ ClearInfixOprt()

void qmu::QmuParserBase::ClearInfixOprt ( )

Clear the user defined Prefix operators.

Postcondition
Resets the parser to string parser mode.
Exceptions
nothrow

Definition at line 1741 of file qmuparserbase.cpp.

◆ ClearOprt()

void qmu::QmuParserBase::ClearOprt ( )

Clear all user defined binary operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1728 of file qmuparserbase.cpp.

◆ ClearPostfixOprt()

void qmu::QmuParserBase::ClearPostfixOprt ( )

Clear all user defined postfix operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1715 of file qmuparserbase.cpp.

Referenced by qmu::Test::QmuParserTester::TestNames().

◆ ClearVar()

void qmu::QmuParserBase::ClearVar ( )

Clear all user defined variables.

Exceptions
nothrowResets the parser to string parsing mode by calling ReInit.

Definition at line 1658 of file qmuparserbase.cpp.

◆ CreateRPN()

◆ DefineConst()

void qmu::QmuParserBase::DefineConst ( const QString &  a_sName,
qreal  a_fVal 
)

Add a user defined constant.

Parameters
[in]a_sNameThe name of the constant.
[in]a_fValthe value of the constant.
Postcondition
Will reset the Parser to string parsing mode.
Exceptions
ParserExceptionin case the name contains invalid signs.

Definition at line 609 of file qmuparserbase.cpp.

References CheckName(), m_ConstDef, ReInit(), and ValidNameChars().

Referenced by qmu::Test::QmuParserTester::EqnTest(), qmu::Test::QmuParserTester::EqnTestBulk(), and qmu::QmuParser::InitConst().

◆ DefineFun()

template<typename T >
void qmu::QmuParserBase::DefineFun ( const QString &  a_strName,
a_pFun,
bool  a_bAllowOpt = true 
)
inline

Define a parser function without arguments.

Parameters
a_strNameName of the function
a_pFunPointer to the callback function
a_bAllowOptA flag indicating this function may be optimized

Definition at line 288 of file qmuparserbase.h.

References AddCallback(), m_FunDef, and ValidNameChars().

Referenced by qmu::QmuParser::InitFun(), and qmu::Test::QmuParserTester::ThrowTest().

◆ DefineInfixOprt()

void qmu::QmuParserBase::DefineInfixOprt ( const QString &  a_sName,
fun_type1  a_pFun,
int  a_iPrec = prINFIX,
bool  a_bAllowOpt = true 
)

Add a user defined operator.

Postcondition
Will reset the Parser to string parsing mode.
Parameters
[in]a_sNameoperator Identifier
[in]a_pFunOperator callback function
[in]a_iPrecOperator Precedence (default=prSIGN)
[in]a_bAllowOptTrue if operator is volatile (default=false)
See also
EPrec

Definition at line 519 of file qmuparserbase.cpp.

References AddCallback(), qmu::cmOPRT_INFIX, m_InfixOprtDef, and ValidInfixOprtChars().

Referenced by qmu::QmuParser::InitOprt().

◆ DefineInfixOprtChars()

void qmu::QmuParserBase::DefineInfixOprtChars ( const QString &  a_szCharset)
inline

Define the set of valid characters to be used in names of infix operators.

Definition at line 362 of file qmuparserbase.h.

References m_sInfixOprtChars.

Referenced by qmu::QmuFormulaBase::InitCharSets(), and qmu::QmuParser::InitCharSets().

◆ DefineNameChars()

void qmu::QmuParserBase::DefineNameChars ( const QString &  a_szCharset)
inline

Define the set of valid characters to be used in names of functions, variables, constants.

Definition at line 344 of file qmuparserbase.h.

References m_sNameChars.

Referenced by qmu::QmuFormulaBase::InitCharSets(), and qmu::QmuParser::InitCharSets().

◆ DefineOprt()

void qmu::QmuParserBase::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.

Parameters
[in]a_sNameThe identifier of the operator.
[in]a_pFunPointer to the callback function.
[in]a_iPrecPrecedence of the operator.
[in]a_eAssociativityThe associativity of the operator.
[in]a_bAllowOptIf this is true the operator may be optimized away.

Adds a new Binary operator the the parser instance.

Definition at line 536 of file qmuparserbase.cpp.

References AddCallback(), c_DefaultOprt, qmu::cmENDIF, qmu::ecBUILTIN_OVERLOAD, Error(), m_bBuiltInOp, m_OprtDef, and ValidOprtChars().

◆ DefineOprtChars()

void qmu::QmuParserBase::DefineOprtChars ( const QString &  a_szCharset)
inline

Define the set of valid characters to be used in names of binary operators and postfix operators.

Definition at line 353 of file qmuparserbase.h.

References m_sOprtChars.

Referenced by qmu::QmuFormulaBase::InitCharSets(), and qmu::QmuParser::InitCharSets().

◆ DefinePostfixOprt()

void qmu::QmuParserBase::DefinePostfixOprt ( const QString &  a_sFun,
fun_type1  a_pFun,
bool  a_bAllowOpt = true 
)

Add a user defined operator.

Postcondition
Will reset the Parser to string parsing mode.

Definition at line 488 of file qmuparserbase.cpp.

References AddCallback(), qmu::cmOPRT_POSTFIX, m_PostOprtDef, qmu::prPOSTFIX, and ValidOprtChars().

Referenced by qmu::Test::QmuParserTester::ThrowTest().

◆ DefineStrConst()

void qmu::QmuParserBase::DefineStrConst ( const QString &  a_strName,
const QString &  a_strVal 
)

Define a new string constant.

Parameters
[in]a_strNameThe name of the constant.
[in]a_strValthe value of the constant.

Definition at line 559 of file qmuparserbase.cpp.

References CheckName(), qmu::ecNAME_CONFLICT, Error(), m_StrVarDef, m_vStringBuf, m_vStringVarBuf, ReInit(), and ValidNameChars().

◆ DefineVar()

void qmu::QmuParserBase::DefineVar ( const QString &  a_sName,
qreal *  a_pVar 
)

Add a user defined variable.

Parameters
[in]a_sNamethe variable name
[in]a_pVarA pointer to the variable vaule.
Postcondition
Will reset the Parser to string parsing mode.
Exceptions
ParserExceptionin case the name contains invalid signs or a_pVar is NULL.

Definition at line 583 of file qmuparserbase.cpp.

References CheckName(), qmu::ecINVALID_VAR_PTR, qmu::ecNAME_CONFLICT, Error(), m_ConstDef, m_VarDef, ReInit(), and ValidNameChars().

Referenced by qmu::Test::QmuParserTester::EqnTestBulk(), qmu::Test::QmuParserTester::EqnTestWithVarChange(), Calculator::InitVariables(), qmu::Test::QmuParserTester::TestInterface(), qmu::Test::QmuParserTester::TestVarConst(), and qmu::Test::QmuParserTester::ThrowTest().

◆ EnableBuiltInOprt()

void qmu::QmuParserBase::EnableBuiltInOprt ( bool  a_bIsOn = true)

Enable or disable the built in binary operators.

Exceptions
nothrow
See also
m_bBuiltInOp, ReInit()

If you disable the built in binary operators there will be no binary operators defined. Thus you must add them manually one by one. It is not possible to disable built in operators selectively. This function will Reinitialize the parser by calling ReInit().

Definition at line 1784 of file qmuparserbase.cpp.

Referenced by qmu::Test::QmuParserTester::TestNames().

◆ EnableDebugDump()

void qmu::QmuParserBase::EnableDebugDump ( bool  bDumpCmd,
bool  bDumpStack 
)
static

Enable the dumping of bytecode amd stack content on the console.

Parameters
bDumpCmdFlag to enable dumping of the current bytecode to the console.
bDumpStackFlag to enable dumping of the stack content is written to the console.

This function is for debug purposes only!

Definition at line 1768 of file qmuparserbase.cpp.

◆ EnableOptimizer()

void qmu::QmuParserBase::EnableOptimizer ( bool  a_bIsOn = true)

Enable or disable the formula optimization feature.

Postcondition
Resets the parser to string parser mode.
Exceptions
nothrow

Definition at line 1753 of file qmuparserbase.cpp.

Referenced by qmu::Test::QmuParserTester::EqnTest().

◆ Error()

void Q_NORETURN qmu::QmuParserBase::Error ( EErrorCodes  a_iErrc,
int  a_iPos = -1,
const QString &  a_sTok = QString() 
) const

Create an error containing the parse error position.

This function will create an Parser Exception object containing the error text and its position.

Parameters
a_iErrc[in] The error code of type EErrorCodes.
a_iPos[in] The position where the error was detected.
a_sTok[in] The token string representation associated with the error.
Exceptions
ParserExceptionalways throws thats the only purpose of this function.

Definition at line 1645 of file qmuparserbase.cpp.

Referenced by AddCallback(), ApplyBinOprt(), ApplyFunc(), ApplyRemainingOprt(), ApplyStrFunc(), CheckName(), CheckOprt(), DefineOprt(), DefineStrConst(), DefineVar(), qmu::QmuParserTokenReader::Error(), GetOprtPrecedence(), ParseCmdCodeBulk(), and SetExpr().

◆ Eval() [1/3]

qreal qmu::QmuParserBase::Eval ( ) const
inline

Calculate the result.

A note on const correctness: I consider it important that Calc is a const function. Due to caching operations Calc changes only the state of internal variables with one exception m_UsedVar this is reset during string parsing and accessible from the outside. Instead of making Calc non const GetUsedVar is non const because it explicitely calls Eval() forcing this update.

Precondition
A formula must be set.
Variables must have been set (if needed)
See also
m_pParseFormula
Returns
The evaluation result
Exceptions
ParseExceptionif no Formula is set or in case of any other error related to the formula.

Definition at line 451 of file qmuparserbase.h.

References m_pParseFormula.

Referenced by qmu::QmuParser::Diff(), qmu::Test::QmuParserTester::EqnTest(), qmu::Test::QmuParserTester::EqnTestBulk(), qmu::Test::QmuParserTester::EqnTestWithVarChange(), Calculator::EvalFormula(), qmu::QmuTokenParser::QmuTokenParser(), qmu::Test::QmuParserTester::TestInterface(), and qmu::Test::QmuParserTester::ThrowTest().

◆ Eval() [2/3]

qreal * qmu::QmuParserBase::Eval ( int &  nStackSize) const

Evaluate an expression containing comma seperated subexpressions.

Parameters
[out]nStackSizeThe total number of results available
Returns
Pointer to the array containing all expression results

This member function can be used to retriev all results of an expression made up of multiple comma seperated subexpressions (i.e. "x+y,sin(x),cos(y)")

Definition at line 1906 of file qmuparserbase.cpp.

◆ Eval() [3/3]

void qmu::QmuParserBase::Eval ( qreal *  results,
int  nBulkSize 
) const

Definition at line 1916 of file qmuparserbase.cpp.

◆ GetArgSep()

QChar qmu::QmuParserBase::GetArgSep ( ) const

Get the argument separator character.

Definition at line 1794 of file qmuparserbase.cpp.

◆ GetConst()

const valmap_type & qmu::QmuParserBase::GetConst ( ) const
inline

Return a map containing all parser constants.

Definition at line 380 of file qmuparserbase.h.

References m_ConstDef.

◆ getDecimalPoint()

QChar qmu::QmuParserBase::getDecimalPoint ( ) const

Definition at line 258 of file qmuparserbase.cpp.

References m_decimalPoint.

◆ GetExpr()

const QString & qmu::QmuParserBase::GetExpr ( ) const
inline

Retrieve the formula.

Definition at line 405 of file qmuparserbase.h.

References m_pTokenReader.

◆ GetFunDef()

const funmap_type & qmu::QmuParserBase::GetFunDef ( ) const
inline

Return prototypes of all parser functions.

Returns
m_FunDef
See also
FunProt
Exceptions
nothrowThe return type is a map of the public type funmap_type containing the prototype definitions for all numerical parser functions. String functions are not part of this map. The Prototype definition is encapsulated in objects of the class FunProt one per parser function each associated with function names via a map construct.

Definition at line 396 of file qmuparserbase.h.

References m_FunDef.

◆ getLocale()

QLocale qmu::QmuParserBase::getLocale ( ) const

Definition at line 244 of file qmuparserbase.cpp.

References m_locale.

◆ GetNumbers()

QMap< int, QString > qmu::QmuParserBase::GetNumbers ( ) const
inline

Definition at line 335 of file qmuparserbase.h.

References m_Numbers.

◆ GetNumResults()

int qmu::QmuParserBase::GetNumResults ( ) const
inline

Return the number of results on the calculation stack.

If the expression contains comma seperated subexpressions (i.e. "sin(y), x+y"). There mey be more than one return value. This function returns the number of available results.

Definition at line 429 of file qmuparserbase.h.

References m_nFinalResultIdx.

◆ GetOprtAssociativity()

EOprtAssociativity qmu::QmuParserBase::GetOprtAssociativity ( const token_type a_Tok) const
private

◆ GetOprtDef()

const QStringList & qmu::QmuParserBase::GetOprtDef ( )
inlinestatic

Get the default symbols used for the built in operators.

See also
c_DefaultOprt

Definition at line 323 of file qmuparserbase.h.

References c_DefaultOprt.

Referenced by qmu::QmuParserTokenReader::IsBuiltIn(), and qmu::QmuParserTokenReader::IsOprt().

◆ GetOprtPrecedence()

◆ getThousandsSeparator()

QChar qmu::QmuParserBase::getThousandsSeparator ( ) const

Definition at line 270 of file qmuparserbase.cpp.

References m_thousandsSeparator.

◆ GetTokens()

QMap< int, QString > qmu::QmuParserBase::GetTokens ( ) const
inline

◆ GetUsedVar()

const varmap_type & qmu::QmuParserBase::GetUsedVar ( ) const

Return a map containing the used variables only.

Definition at line 700 of file qmuparserbase.cpp.

References CreateRPN(), m_pParseFormula, m_pTokenReader, and ParseString().

Referenced by qmu::Test::QmuParserTester::TestVarConst().

◆ GetVar()

const varmap_type & qmu::QmuParserBase::GetVar ( ) const
inline

Return a map containing the used variables only.

Definition at line 371 of file qmuparserbase.h.

References m_VarDef.

Referenced by qmu::Test::QmuParserTester::TestVarConst().

◆ GetVersion()

QString qmu::QmuParserBase::GetVersion ( EParserVersionInfo  eInfo = pviFULL)
static

Returns the version of muparser.

Parameters
eInfoA flag indicating whether the full version info should be returned or not.

Format is as follows: "MAJOR.MINOR (COMPILER_FLAGS)" The COMPILER_FLAGS are returned only if eInfo==pviFULL.

Definition at line 289 of file qmuparserbase.cpp.

References qmu::pviFULL, QMUP_VERSION, and QMUP_VERSION_DATE.

◆ HasBuiltInOprt()

bool qmu::QmuParserBase::HasBuiltInOprt ( ) const
inline

Query status of built in variables.

Returns
m_bBuiltInOp; true if built in operators are enabled.
Exceptions
nothrow

Definition at line 416 of file qmuparserbase.h.

References m_bBuiltInOp.

Referenced by qmu::QmuParserTokenReader::IsBuiltIn(), and qmu::QmuParserTokenReader::IsOprt().

◆ Init()

void qmu::QmuParserBase::Init ( )
protected

Initialize user defined functions.

Calls the virtual functions InitFun(), InitConst() and InitOprt().

Definition at line 501 of file qmuparserbase.cpp.

References InitCharSets(), InitConst(), InitFun(), and InitOprt().

◆ InitCharSets()

virtual void qmu::QmuParserBase::InitCharSets ( )
protectedpure virtual

Implemented in qmu::QmuParser, and qmu::QmuFormulaBase.

Referenced by Init(), and setLocale().

◆ InitConst()

virtual void qmu::QmuParserBase::InitConst ( )
protectedpure virtual

Implemented in qmu::QmuParser.

Referenced by Init().

◆ InitFun()

virtual void qmu::QmuParserBase::InitFun ( )
protectedpure virtual

Implemented in qmu::QmuParser.

Referenced by Init().

◆ InitOprt()

virtual void qmu::QmuParserBase::InitOprt ( )
protectedpure virtual

Implemented in qmu::QmuParser.

Referenced by Init(), and setLocale().

◆ InitTokenReader()

void qmu::QmuParserBase::InitTokenReader ( )
inlineprivate

Initialize the token reader.

Create new token reader object and submit pointers to function, operator, constant and variable definitions.

Postcondition
m_pTokenReader.get()!=0

Definition at line 301 of file qmuparserbase.h.

References m_pTokenReader.

Referenced by QmuParserBase().

◆ OnDetectVar()

void qmu::QmuParserBase::OnDetectVar ( const QString &  pExpr,
int &  nStart,
int &  nEnd 
)
protectedvirtual

Reimplemented in qmu::QmuParser.

Definition at line 230 of file qmuparserbase.cpp.

Referenced by qmu::QmuParserTokenReader::IsVarTok().

◆ operator=()

QmuParserBase & qmu::QmuParserBase::operator= ( const QmuParserBase a_Parser)

Assignement operator.

Implemented by calling Assign(a_Parser). Self assignement is suppressed.

Parameters
a_ParserObject to copy to this.
Returns
*this
Exceptions
nothrow

Definition at line 144 of file qmuparserbase.cpp.

References Assign().

◆ ParseCmdCode()

qreal qmu::QmuParserBase::ParseCmdCode ( ) const
private

Parse the command code.

See also
ParseString(...)

Command code contains precalculated stack positions of the values and the associated operators. The Stack is filled beginning from index one the value at index zero is not used at all.

Definition at line 998 of file qmuparserbase.cpp.

References ParseCmdCodeBulk().

◆ ParseCmdCodeBulk()

◆ ParseString()

qreal qmu::QmuParserBase::ParseString ( ) const
private

One of the two main parse functions.

See also
ParseCmdCode(...)

Parse expression from input string. Perform syntax checking and create bytecode. After parsing the string and creating the bytecode the function pointer m_pParseFormula will be changed to the second parse routine the uses bytecode instead of string parsing.

Definition at line 1619 of file qmuparserbase.cpp.

References qmu::QmuParserError::SetFormula().

Referenced by GetUsedVar(), and ReInit().

◆ ReInit()

void qmu::QmuParserBase::ReInit ( ) const
private

Reset parser to string parsing mode and clear internal buffers.

Clear bytecode, reset the token reader.

Exceptions
nothrow

Definition at line 218 of file qmuparserbase.cpp.

References qmu::QmuParserByteCode::clear(), m_nIfElseCounter, m_Numbers, m_pParseFormula, m_pTokenReader, m_Tokens, m_vRPN, m_vStringBuf, and ParseString().

Referenced by AddCallback(), Assign(), DefineConst(), DefineStrConst(), DefineVar(), and SetExpr().

◆ RemoveVar()

void qmu::QmuParserBase::RemoveVar ( const QString &  a_strVarName)

Remove a variable from internal storage.

Exceptions
nothrowRemoves a variable if it exists. If the Variable does not exist nothing will be done.

Definition at line 1671 of file qmuparserbase.cpp.

Referenced by qmu::Test::QmuParserTester::TestInterface().

◆ ResetLocale()

void qmu::QmuParserBase::ResetLocale ( )

Resets the locale.

The default locale used "." as decimal separator, no thousands separator and "," as function argument separator.

Definition at line 203 of file qmuparserbase.cpp.

References m_decimalPoint, m_locale, m_thousandsSeparator, SetArgSep(), and setLocale().

◆ setAllowSubexpressions()

void qmu::QmuParserBase::setAllowSubexpressions ( bool  value)

◆ SetArgSep()

void qmu::QmuParserBase::SetArgSep ( char_type  cArgSep)

Set argument separator.

Parameters
cArgSepthe argument separator character.

Definition at line 1804 of file qmuparserbase.cpp.

Referenced by ResetLocale(), qmu::QmuFormulaBase::SetSepForEval(), and qmu::QmuFormulaBase::SetSepForTr().

◆ setDecimalPoint()

void qmu::QmuParserBase::setDecimalPoint ( const QChar &  c)

◆ SetExpr()

void qmu::QmuParserBase::SetExpr ( const QString &  a_sExpr)

Set the formula.

Parameters
a_sExprFormula as string_type
Exceptions
ParserExceptionin case of syntax errors.

Triggers first time calculation thus the creation of the bytecode and scanning of used variables.

Definition at line 431 of file qmuparserbase.cpp.

References qmu::ecLOCALE, Error(), m_pTokenReader, and ReInit().

Referenced by qmu::Test::QmuParserTester::EqnTestBulk(), qmu::Test::QmuParserTester::EqnTestWithVarChange(), Calculator::EvalFormula(), qmu::QmuTokenParser::QmuTokenParser(), qmu::Test::QmuParserTester::TestInterface(), qmu::Test::QmuParserTester::TestVarConst(), and qmu::Test::QmuParserTester::ThrowTest().

◆ setLocale()

void qmu::QmuParserBase::setLocale ( const QLocale &  value)

Definition at line 250 of file qmuparserbase.cpp.

References InitCharSets(), InitOprt(), and m_locale.

Referenced by ResetLocale(), and qmu::QmuFormulaBase::SetSepForTr().

◆ setThousandsSeparator()

void qmu::QmuParserBase::setThousandsSeparator ( const QChar &  c)

◆ SetVarFactory()

void qmu::QmuParserBase::SetVarFactory ( facfun_type  a_pFactory,
void *  pUserData = nullptr 
)

Set a function that can create variable pointer for unknown expression variables.

Parameters
a_pFactoryA pointer to the variable factory.
pUserDataA user defined context pointer.

Definition at line 1978 of file qmuparserbase.cpp.

References m_pTokenReader.

Referenced by Calculator::EvalFormula(), and qmu::QmuTokenParser::QmuTokenParser().

◆ StackDump()

◆ ValidInfixOprtChars()

const QString & qmu::QmuParserBase::ValidInfixOprtChars ( ) const

Virtual function that defines the characters allowed in infix operator definitions.

See also
ValidNameChars, ValidOprtChars

Definition at line 477 of file qmuparserbase.cpp.

References m_sInfixOprtChars.

Referenced by DefineInfixOprt(), and qmu::QmuParserTokenReader::IsInfixOpTok().

◆ ValidNameChars()

const QString & qmu::QmuParserBase::ValidNameChars ( ) const

◆ ValidOprtChars()

const QString & qmu::QmuParserBase::ValidOprtChars ( ) const

Virtual function that defines the characters allowed in operator definitions.

See also
ValidNameChars, #ValidPrefixOprtChars

Definition at line 466 of file qmuparserbase.cpp.

References m_sOprtChars.

Referenced by DefineOprt(), DefinePostfixOprt(), qmu::QmuParserTokenReader::ExtractOperatorToken(), and qmu::QmuParserTokenReader::IsPostOpTok().

Friends And Related Function Documentation

◆ QmuParserTokenReader

friend class QmuParserTokenReader
friend

Definition at line 67 of file qmuparserbase.h.

Member Data Documentation

◆ allowSubexpressions

bool qmu::QmuParserBase::allowSubexpressions
private

Definition at line 252 of file qmuparserbase.h.

Referenced by setAllowSubexpressions().

◆ c_DefaultOprt

const QStringList qmu::QmuParserBase::c_DefaultOprt
staticprotected
Initial value:
= QStringList()<< "<=" << ">=" << "!=" << "==" << "<" << ">" << "+"
<< "-" << "*" << "/" << "^" << "&&" << "||" << "="
<< "(" << ")" << "?" << ":"

Identifiers for built in binary operators.

When defining custom binary operators with #AddOprt(...) make sure not to choose names conflicting with these definitions.

Definition at line 136 of file qmuparserbase.h.

Referenced by DefineOprt(), and GetOprtDef().

◆ g_DbgDumpCmdCode

bool qmu::QmuParserBase::g_DbgDumpCmdCode = false
staticprotected

Definition at line 140 of file qmuparserbase.h.

◆ g_DbgDumpStack

bool qmu::QmuParserBase::g_DbgDumpStack = false
staticprotected

Definition at line 141 of file qmuparserbase.h.

◆ m_bBuiltInOp

bool qmu::QmuParserBase::m_bBuiltInOp
private

Flag that can be used for switching built in operators on and off.

Definition at line 238 of file qmuparserbase.h.

Referenced by Assign(), DefineOprt(), and HasBuiltInOprt().

◆ m_ConstDef

valmap_type qmu::QmuParserBase::m_ConstDef
private

user constants.

Definition at line 234 of file qmuparserbase.h.

Referenced by Assign(), DefineConst(), DefineVar(), GetConst(), and qmu::QmuParserTokenReader::SetParent().

◆ m_decimalPoint

QChar qmu::QmuParserBase::m_decimalPoint
protected

Definition at line 138 of file qmuparserbase.h.

Referenced by getDecimalPoint(), ResetLocale(), and setDecimalPoint().

◆ m_FunDef

funmap_type qmu::QmuParserBase::m_FunDef
private

Map of function names and pointers.

Definition at line 230 of file qmuparserbase.h.

Referenced by AddCallback(), Assign(), DefineFun(), GetFunDef(), and qmu::QmuParserTokenReader::SetParent().

◆ m_InfixOprtDef

funmap_type qmu::QmuParserBase::m_InfixOprtDef
private

unary infix operator.

Definition at line 232 of file qmuparserbase.h.

Referenced by AddCallback(), Assign(), DefineInfixOprt(), and qmu::QmuParserTokenReader::SetParent().

◆ m_locale

QLocale qmu::QmuParserBase::m_locale
protected

The locale used by the parser.

Definition at line 137 of file qmuparserbase.h.

Referenced by getLocale(), qmu::QmuFormulaBase::InitCharSets(), qmu::QmuParser::InitOprt(), ResetLocale(), and setLocale().

◆ m_nFinalResultIdx

int qmu::QmuParserBase::m_nFinalResultIdx
mutableprivate

Definition at line 248 of file qmuparserbase.h.

Referenced by Assign(), and GetNumResults().

◆ m_nIfElseCounter

int qmu::QmuParserBase::m_nIfElseCounter
mutableprivate

Internal counter for keeping track of nested if-then-else clauses.

Definition at line 244 of file qmuparserbase.h.

Referenced by Assign(), and ReInit().

◆ m_Numbers

QMap<int, QString> qmu::QmuParserBase::m_Numbers
mutableprivate

Keep all numbers what exist in formula.

Definition at line 250 of file qmuparserbase.h.

Referenced by GetNumbers(), and ReInit().

◆ m_OprtDef

funmap_type qmu::QmuParserBase::m_OprtDef
private

Binary operator callbacks.

Definition at line 233 of file qmuparserbase.h.

Referenced by AddCallback(), Assign(), DefineOprt(), and qmu::QmuParserTokenReader::SetParent().

◆ m_PostOprtDef

funmap_type qmu::QmuParserBase::m_PostOprtDef
private

Postfix operator callbacks.

Definition at line 231 of file qmuparserbase.h.

Referenced by AddCallback(), Assign(), DefinePostfixOprt(), and qmu::QmuParserTokenReader::SetParent().

◆ m_pParseFormula

ParseFunction qmu::QmuParserBase::m_pParseFormula
mutableprivate

Pointer to the parser function.

Eval() calls the function whose address is stored there.

Definition at line 223 of file qmuparserbase.h.

Referenced by Eval(), GetUsedVar(), and ReInit().

◆ m_pTokenReader

std::unique_ptr<token_reader_type> qmu::QmuParserBase::m_pTokenReader
private

Managed pointer to the token reader object.

Definition at line 228 of file qmuparserbase.h.

Referenced by AddValIdent(), ApplyBinOprt(), ApplyFunc(), ApplyStrFunc(), Assign(), GetExpr(), GetUsedVar(), InitTokenReader(), QmuParserBase(), ReInit(), SetExpr(), and SetVarFactory().

◆ m_sInfixOprtChars

QString qmu::QmuParserBase::m_sInfixOprtChars
private

Charset for infix operator tokens.

Definition at line 242 of file qmuparserbase.h.

Referenced by Assign(), DefineInfixOprtChars(), and ValidInfixOprtChars().

◆ m_sNameChars

QString qmu::QmuParserBase::m_sNameChars
private

Charset for names.

Definition at line 240 of file qmuparserbase.h.

Referenced by Assign(), DefineNameChars(), and ValidNameChars().

◆ m_sOprtChars

QString qmu::QmuParserBase::m_sOprtChars
private

Charset for postfix/ binary operator tokens.

Definition at line 241 of file qmuparserbase.h.

Referenced by Assign(), DefineOprtChars(), and ValidOprtChars().

◆ m_StrVarDef

strmap_type qmu::QmuParserBase::m_StrVarDef
private

user defined string constants

Definition at line 235 of file qmuparserbase.h.

Referenced by Assign(), DefineStrConst(), and qmu::QmuParserTokenReader::SetParent().

◆ m_thousandsSeparator

QChar qmu::QmuParserBase::m_thousandsSeparator
protected

Definition at line 139 of file qmuparserbase.h.

Referenced by getThousandsSeparator(), ResetLocale(), and setThousandsSeparator().

◆ m_Tokens

QMap<int, QString> qmu::QmuParserBase::m_Tokens
mutableprivate

Keep all tokens that we can translate.

Definition at line 249 of file qmuparserbase.h.

Referenced by GetTokens(), and ReInit().

◆ m_VarDef

varmap_type qmu::QmuParserBase::m_VarDef
private

user defind variables.

Definition at line 236 of file qmuparserbase.h.

Referenced by Assign(), DefineVar(), GetVar(), and qmu::QmuParserTokenReader::SetParent().

◆ m_vRPN

QmuParserByteCode qmu::QmuParserBase::m_vRPN
mutableprivate

The Bytecode class.

Definition at line 224 of file qmuparserbase.h.

Referenced by ApplyBinOprt(), ApplyFunc(), ApplyIfElse(), ApplyStrFunc(), ParseCmdCodeBulk(), and ReInit().

◆ m_vStackBuffer

valbuf_type qmu::QmuParserBase::m_vStackBuffer
mutableprivate

This is merely a buffer used for the stack in the cmd parsing routine.

Definition at line 247 of file qmuparserbase.h.

Referenced by Assign(), and ParseCmdCodeBulk().

◆ m_vStringBuf

stringbuf_type qmu::QmuParserBase::m_vStringBuf
mutableprivate

String buffer, used for storing string function arguments.

Definition at line 225 of file qmuparserbase.h.

Referenced by Assign(), DefineStrConst(), qmu::QmuParserTokenReader::IsString(), ParseCmdCodeBulk(), and ReInit().

◆ m_vStringVarBuf

stringbuf_type qmu::QmuParserBase::m_vStringVarBuf
private

Definition at line 226 of file qmuparserbase.h.

Referenced by Assign(), DefineStrConst(), and qmu::QmuParserTokenReader::IsStrVarTok().

◆ s_MaxNumOpenMPThreads

const int qmu::QmuParserBase::s_MaxNumOpenMPThreads = 4
staticprivate

Maximum number of threads spawned by OpenMP when using the bulk mode.

Definition at line 216 of file qmuparserbase.h.

Referenced by ParseCmdCodeBulk().


The documentation for this class was generated from the following files: