Mathematical expressions parser (base parser engine). More...
#include <qmuparserbase.h>
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... | |
QmuParserBase & | operator= (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_type & | GetUsedVar () const |
Return a map containing the used variables only. More... | |
const varmap_type & | GetVar () const |
Return a map containing the used variables only. More... | |
const valmap_type & | GetConst () const |
Return a map containing all parser constants. More... | |
const QString & | GetExpr () const |
Retrieve the formula. More... | |
const funmap_type & | GetFunDef () 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_type > | m_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 |
Mathematical expressions parser (base parser engine).
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.
|
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.
|
private |
Type for a vector of strings.
Definition at line 201 of file qmuparserbase.h.
|
private |
Typedef for the token reader.
Definition at line 206 of file qmuparserbase.h.
|
private |
Type used for parser tokens.
Definition at line 211 of file qmuparserbase.h.
|
private |
Type used for storing an array of values.
Definition at line 196 of file qmuparserbase.h.
qmu::QmuParserBase::QmuParserBase | ( | ) |
|
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.
|
virtual |
Definition at line 132 of file qmuparserbase.cpp.
|
private |
Add a function or operator callback to the parser.
Definition at line 338 of file qmuparserbase.cpp.
References CheckOprt(), qmu::ecINVALID_FUN_PTR, qmu::ecNAME_CONFLICT, Error(), qmu::QmuParserCallback::GetAddr(), m_FunDef, m_InfixOprtDef, m_OprtDef, m_PostOprtDef, and ReInit().
Referenced by DefineFun(), DefineInfixOprt(), DefineOprt(), and DefinePostfixOprt().
|
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().
|
private |
Performs the necessary steps to write code for the execution of binary operators into the bytecode.
Definition at line 915 of file qmuparserbase.cpp.
References qmu::QmuParserByteCode::AddAssignOp(), qmu::QmuParserByteCode::AddOp(), ApplyFunc(), qmu::cmASSIGN, qmu::cmOPRT_BIN, qmu::cmVAR, qmu::ecOPRT_TYPE_CONFLICT, qmu::ecUNEXPECTED_OPERATOR, Error(), qmu::QmuParserToken< TBase, TString >::GetType(), m_pTokenReader, m_vRPN, and qmu::tpSTR.
Referenced by ApplyRemainingOprt().
|
private |
Apply a function token.
a_iArgCount | Number of Arguments actually gathered used only for multiarg functions. |
QmuParserError | if Argument count does not mach function requirements. |
Definition at line 785 of file qmuparserbase.cpp.
References qmu::QmuParserByteCode::AddBulkFun(), qmu::QmuParserByteCode::AddFun(), ApplyStrFunc(), qmu::cmFUNC, qmu::cmFUNC_BULK, qmu::cmFUNC_STR, qmu::cmOPRT_BIN, qmu::cmOPRT_INFIX, qmu::cmOPRT_POSTFIX, qmu::ecINTERNAL_ERROR, qmu::ecTOO_FEW_PARAMS, qmu::ecTOO_MANY_PARAMS, qmu::ecUNASSIGNABLE_TOKEN, qmu::ecVAL_EXPECTED, Error(), qmu::QmuParserToken< TBase, TString >::GetArgCount(), qmu::QmuParserToken< TBase, TString >::GetAsString(), qmu::QmuParserToken< TBase, TString >::GetCode(), qmu::QmuParserToken< TBase, TString >::GetFuncAddr(), qmu::QmuParserToken< TBase, TString >::GetType(), m_pTokenReader, m_vRPN, qmu::QmuParserToken< TBase, TString >::SetVal(), and qmu::tpSTR.
Referenced by ApplyBinOprt(), and ApplyRemainingOprt().
|
private |
Definition at line 882 of file qmuparserbase.cpp.
References qmu::QmuParserByteCode::AddIfElse(), qmu::cmELSE, qmu::cmENDIF, qmu::cmIF, qmu::QmuParserToken< TBase, TString >::GetCode(), qmu::QmuParserToken< TBase, TString >::GetVal(), and m_vRPN.
Referenced by ApplyRemainingOprt().
|
private |
Apply a binary operator.
stOpt | The operator stack |
stVal | The 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().
|
private |
Execute a function that takes a single string argument.
a_FunTok | Function token. |
QmuParserError | If the function token is not a string function |
Definition at line 728 of file qmuparserbase.cpp.
References qmu::QmuParserByteCode::AddStrFun(), qmu::cmSTRING, qmu::ecINTERNAL_ERROR, qmu::ecSTRING_EXPECTED, qmu::ecVAL_EXPECTED, Error(), qmu::QmuParserToken< TBase, TString >::GetArgCount(), qmu::QmuParserToken< TBase, TString >::GetAsString(), qmu::QmuParserToken< TBase, TString >::GetFuncAddr(), m_pTokenReader, m_vRPN, and qmu::QmuParserToken< TBase, TString >::SetVal().
Referenced by ApplyFunc().
|
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.
a_Parser | the 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().
|
private |
Check if a name contains invalid characters.
ParserException | if 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().
|
private |
Check if a name contains invalid characters.
ParserException | if 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().
void qmu::QmuParserBase::ClearConst | ( | ) |
Clear all user defined constants.
Both numeric and string constants will be removed from the internal storage.
nothrow |
Definition at line 1702 of file qmuparserbase.cpp.
Referenced by qmu::Test::QmuParserTester::TestNames().
void qmu::QmuParserBase::ClearFun | ( | ) |
Clear all functions.
nothrow |
Definition at line 1688 of file qmuparserbase.cpp.
void qmu::QmuParserBase::ClearInfixOprt | ( | ) |
Clear the user defined Prefix operators.
nothrow |
Definition at line 1741 of file qmuparserbase.cpp.
void qmu::QmuParserBase::ClearOprt | ( | ) |
Clear all user defined binary operators.
nothrow |
Definition at line 1728 of file qmuparserbase.cpp.
void qmu::QmuParserBase::ClearPostfixOprt | ( | ) |
Clear all user defined postfix operators.
nothrow |
Definition at line 1715 of file qmuparserbase.cpp.
Referenced by qmu::Test::QmuParserTester::TestNames().
void qmu::QmuParserBase::ClearVar | ( | ) |
Clear all user defined variables.
nothrow | Resets the parser to string parsing mode by calling ReInit. |
Definition at line 1658 of file qmuparserbase.cpp.
|
private |
Definition at line 1345 of file qmuparserbase.cpp.
References qmu::cmADD, qmu::cmARG_SEP, qmu::cmASSIGN, qmu::cmBC, qmu::cmBO, qmu::cmDIV, qmu::cmELSE, qmu::cmEND, qmu::cmENDIF, qmu::cmEQ, qmu::cmFUNC, qmu::cmFUNC_BULK, qmu::cmFUNC_STR, qmu::cmGE, qmu::cmGT, qmu::cmIF, qmu::cmLAND, qmu::cmLE, qmu::cmLOR, qmu::cmLT, qmu::cmMUL, qmu::cmNEQ, qmu::cmOPRT_BIN, qmu::cmOPRT_INFIX, qmu::cmOPRT_POSTFIX, qmu::cmPOW, qmu::cmPOW2, qmu::cmSTRING, qmu::cmSUB, qmu::cmUNKNOWN, qmu::cmVAL, qmu::cmVAR, qmu::cmVARMUL, qmu::cmVARPOW2, qmu::cmVARPOW3, qmu::cmVARPOW4, qmu::ecEMPTY_EXPRESSION, qmu::ecINTERNAL_ERROR, qmu::ecMISPLACED_COLON, qmu::ecMISSING_ELSE_CLAUSE, qmu::ecSTR_RESULT, qmu::ecUNEXPECTED_ARG, qmu::ecUNEXPECTED_ARG_SEP, qmu::ecUNEXPECTED_EOF, qmu::QmuParserToken< TBase, TString >::GetAsString(), qmu::QmuParserToken< TBase, TString >::GetCode(), qmu::QmuParserToken< TBase, TString >::GetVal(), qmu::QmuParserToken< TBase, TString >::GetVar(), qmu::oaLEFT, qmu::oaRIGHT, QMUP_FALLTHROUGH, qmu::QmuParserToken< TBase, TString >::SetIdx(), and qmu::tpDBL.
Referenced by GetUsedVar().
void qmu::QmuParserBase::DefineConst | ( | const QString & | a_sName, |
qreal | a_fVal | ||
) |
Add a user defined constant.
[in] | a_sName | The name of the constant. |
[in] | a_fVal | the value of the constant. |
ParserException | in 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().
|
inline |
Define a parser function without arguments.
a_strName | Name of the function |
a_pFun | Pointer to the callback function |
a_bAllowOpt | A 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().
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.
[in] | a_sName | operator Identifier |
[in] | a_pFun | Operator callback function |
[in] | a_iPrec | Operator Precedence (default=prSIGN) |
[in] | a_bAllowOpt | True if operator is volatile (default=false) |
Definition at line 519 of file qmuparserbase.cpp.
References AddCallback(), qmu::cmOPRT_INFIX, m_InfixOprtDef, and ValidInfixOprtChars().
Referenced by qmu::QmuParser::InitOprt().
|
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().
|
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().
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.
[in] | a_sName | The identifier of the operator. |
[in] | a_pFun | Pointer to the callback function. |
[in] | a_iPrec | Precedence of the operator. |
[in] | a_eAssociativity | The associativity of the operator. |
[in] | a_bAllowOpt | If 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().
|
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().
void qmu::QmuParserBase::DefinePostfixOprt | ( | const QString & | a_sFun, |
fun_type1 | a_pFun, | ||
bool | a_bAllowOpt = true |
||
) |
Add a user defined operator.
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().
void qmu::QmuParserBase::DefineStrConst | ( | const QString & | a_strName, |
const QString & | a_strVal | ||
) |
Define a new string constant.
[in] | a_strName | The name of the constant. |
[in] | a_strVal | the 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().
void qmu::QmuParserBase::DefineVar | ( | const QString & | a_sName, |
qreal * | a_pVar | ||
) |
Add a user defined variable.
[in] | a_sName | the variable name |
[in] | a_pVar | A pointer to the variable vaule. |
ParserException | in 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().
void qmu::QmuParserBase::EnableBuiltInOprt | ( | bool | a_bIsOn = true | ) |
Enable or disable the built in binary operators.
nothrow |
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().
|
static |
Enable the dumping of bytecode amd stack content on the console.
bDumpCmd | Flag to enable dumping of the current bytecode to the console. |
bDumpStack | Flag 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.
void qmu::QmuParserBase::EnableOptimizer | ( | bool | a_bIsOn = true | ) |
Enable or disable the formula optimization feature.
nothrow |
Definition at line 1753 of file qmuparserbase.cpp.
Referenced by qmu::Test::QmuParserTester::EqnTest().
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.
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. |
ParserException | always 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().
|
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.
ParseException | if 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().
qreal * qmu::QmuParserBase::Eval | ( | int & | nStackSize | ) | const |
Evaluate an expression containing comma seperated subexpressions.
[out] | nStackSize | The total number of results available |
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.
void qmu::QmuParserBase::Eval | ( | qreal * | results, |
int | nBulkSize | ||
) | const |
Definition at line 1916 of file qmuparserbase.cpp.
QChar qmu::QmuParserBase::GetArgSep | ( | ) | const |
Get the argument separator character.
Definition at line 1794 of file qmuparserbase.cpp.
|
inline |
Return a map containing all parser constants.
Definition at line 380 of file qmuparserbase.h.
References m_ConstDef.
QChar qmu::QmuParserBase::getDecimalPoint | ( | ) | const |
Definition at line 258 of file qmuparserbase.cpp.
References m_decimalPoint.
|
inline |
|
inline |
Return prototypes of all parser functions.
nothrow | The 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.
QLocale qmu::QmuParserBase::getLocale | ( | ) | const |
Definition at line 244 of file qmuparserbase.cpp.
References m_locale.
|
inline |
Definition at line 335 of file qmuparserbase.h.
References m_Numbers.
|
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.
|
private |
Get operator priority.
ParserException | if a_Oprt is no operator code |
Definition at line 669 of file qmuparserbase.cpp.
References qmu::cmADD, qmu::cmASSIGN, qmu::cmDIV, qmu::cmEQ, qmu::cmGE, qmu::cmGT, qmu::cmLAND, qmu::cmLE, qmu::cmLOR, qmu::cmLT, qmu::cmMUL, qmu::cmNEQ, qmu::cmOPRT_BIN, qmu::cmPOW, qmu::cmSUB, qmu::QmuParserToken< TBase, TString >::GetAssociativity(), qmu::QmuParserToken< TBase, TString >::GetCode(), qmu::oaLEFT, qmu::oaNONE, and qmu::oaRIGHT.
|
inlinestatic |
Get the default symbols used for the built in operators.
Definition at line 323 of file qmuparserbase.h.
References c_DefaultOprt.
Referenced by qmu::QmuParserTokenReader::IsBuiltIn(), and qmu::QmuParserTokenReader::IsOprt().
|
private |
Get operator priority.
ParserException | if a_Oprt is no operator code |
Definition at line 621 of file qmuparserbase.cpp.
References qmu::cmADD, qmu::cmARG_SEP, qmu::cmASSIGN, qmu::cmDIV, qmu::cmELSE, qmu::cmEND, qmu::cmEQ, qmu::cmGE, qmu::cmGT, qmu::cmIF, qmu::cmLAND, qmu::cmLE, qmu::cmLOR, qmu::cmLT, qmu::cmMUL, qmu::cmNEQ, qmu::cmOPRT_BIN, qmu::cmOPRT_INFIX, qmu::cmPOW, qmu::cmSUB, qmu::ecINTERNAL_ERROR, Error(), qmu::QmuParserToken< TBase, TString >::GetCode(), qmu::QmuParserToken< TBase, TString >::GetPri(), qmu::prADD_SUB, qmu::prCMP, qmu::prLAND, qmu::prLOR, qmu::prMUL_DIV, and qmu::prPOW.
QChar qmu::QmuParserBase::getThousandsSeparator | ( | ) | const |
Definition at line 270 of file qmuparserbase.cpp.
References m_thousandsSeparator.
|
inline |
Definition at line 329 of file qmuparserbase.h.
References m_Tokens.
Referenced by Calculator::EvalFormula(), and VPatternConverter::FixIncrementInFormulaToV0_2_0().
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().
|
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().
|
static |
Returns the version of muparser.
eInfo | A 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.
|
inline |
Query status of built in variables.
nothrow |
Definition at line 416 of file qmuparserbase.h.
References m_bBuiltInOp.
Referenced by qmu::QmuParserTokenReader::IsBuiltIn(), and qmu::QmuParserTokenReader::IsOprt().
|
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().
|
protectedpure virtual |
Implemented in qmu::QmuParser, and qmu::QmuFormulaBase.
Referenced by Init(), and setLocale().
|
protectedpure virtual |
Implemented in qmu::QmuParser.
Referenced by Init().
|
protectedpure virtual |
Implemented in qmu::QmuParser.
Referenced by Init().
|
protectedpure virtual |
Implemented in qmu::QmuParser.
Referenced by Init(), and setLocale().
|
inlineprivate |
Initialize the token reader.
Create new token reader object and submit pointers to function, operator, constant and variable definitions.
Definition at line 301 of file qmuparserbase.h.
References m_pTokenReader.
Referenced by QmuParserBase().
|
protectedvirtual |
Reimplemented in qmu::QmuParser.
Definition at line 230 of file qmuparserbase.cpp.
Referenced by qmu::QmuParserTokenReader::IsVarTok().
QmuParserBase & qmu::QmuParserBase::operator= | ( | const QmuParserBase & | a_Parser | ) |
Assignement operator.
Implemented by calling Assign(a_Parser). Self assignement is suppressed.
a_Parser | Object to copy to this. |
nothrow |
Definition at line 144 of file qmuparserbase.cpp.
References Assign().
|
private |
Parse the command code.
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().
|
private |
Evaluate the RPN.
nOffset | The offset added to variable addresses (for bulk mode) |
nThreadID | OpenMP Thread id of the calling thread |
Definition at line 1009 of file qmuparserbase.cpp.
References qmu::cmADD, qmu::cmARG_SEP, qmu::cmASSIGN, qmu::cmBC, qmu::cmBO, qmu::SToken::Cmd, qmu::cmDIV, qmu::cmELSE, qmu::cmEND, qmu::cmENDIF, qmu::cmEQ, qmu::cmFUNC, qmu::cmFUNC_BULK, qmu::cmFUNC_STR, qmu::cmGE, qmu::cmGT, qmu::cmIF, qmu::cmLAND, qmu::cmLE, qmu::cmLOR, qmu::cmLT, qmu::cmMUL, qmu::cmNEQ, qmu::cmOPRT_BIN, qmu::cmOPRT_INFIX, qmu::cmOPRT_POSTFIX, qmu::cmPOW, qmu::cmPOW2, qmu::cmSTRING, qmu::cmSUB, qmu::cmUNKNOWN, qmu::cmVAL, qmu::cmVAR, qmu::cmVARMUL, qmu::cmVARPOW2, qmu::cmVARPOW3, qmu::cmVARPOW4, qmu::ecDIV_BY_ZERO, qmu::ecINTERNAL_ERROR, Error(), qmu::QmuParserByteCode::GetBase(), m_vRPN, m_vStackBuffer, m_vStringBuf, QmuFuzzyComparePossibleNulls(), and s_MaxNumOpenMPThreads.
Referenced by ParseCmdCode().
|
private |
One of the two main parse functions.
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().
|
private |
Reset parser to string parsing mode and clear internal buffers.
Clear bytecode, reset the token reader.
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().
void qmu::QmuParserBase::RemoveVar | ( | const QString & | a_strVarName | ) |
Remove a variable from internal storage.
nothrow | Removes 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().
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().
void qmu::QmuParserBase::setAllowSubexpressions | ( | bool | value | ) |
Definition at line 238 of file qmuparserbase.cpp.
References allowSubexpressions.
Referenced by Calculator::Calculator(), and qmu::QmuTokenParser::QmuTokenParser().
void qmu::QmuParserBase::SetArgSep | ( | char_type | cArgSep | ) |
Set argument separator.
cArgSep | the argument separator character. |
Definition at line 1804 of file qmuparserbase.cpp.
Referenced by ResetLocale(), qmu::QmuFormulaBase::SetSepForEval(), and qmu::QmuFormulaBase::SetSepForTr().
void qmu::QmuParserBase::setDecimalPoint | ( | const QChar & | c | ) |
Definition at line 264 of file qmuparserbase.cpp.
References m_decimalPoint.
Referenced by qmu::QmuFormulaBase::SetSepForEval(), and qmu::QmuFormulaBase::SetSepForTr().
void qmu::QmuParserBase::SetExpr | ( | const QString & | a_sExpr | ) |
Set the formula.
a_sExpr | Formula as string_type |
ParserException | in 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().
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().
void qmu::QmuParserBase::setThousandsSeparator | ( | const QChar & | c | ) |
Definition at line 276 of file qmuparserbase.cpp.
References m_thousandsSeparator.
Referenced by qmu::QmuFormulaBase::SetSepForEval(), and qmu::QmuFormulaBase::SetSepForTr().
void qmu::QmuParserBase::SetVarFactory | ( | facfun_type | a_pFactory, |
void * | pUserData = nullptr |
||
) |
Set a function that can create variable pointer for unknown expression variables.
a_pFactory | A pointer to the variable factory. |
pUserData | A user defined context pointer. |
Definition at line 1978 of file qmuparserbase.cpp.
References m_pTokenReader.
Referenced by Calculator::EvalFormula(), and qmu::QmuTokenParser::QmuTokenParser().
|
private |
Dump stack content.
This function is used for debugging only.
Definition at line 1815 of file qmuparserbase.cpp.
References qmu::cmASSIGN, qmu::cmBC, qmu::cmBO, qmu::cmELSE, qmu::cmEND, qmu::cmENDIF, qmu::cmFUNC, qmu::cmFUNC_BULK, qmu::cmFUNC_STR, qmu::cmIF, qmu::cmOPRT_BIN, qmu::cmOPRT_INFIX, qmu::cmUNKNOWN, qmu::cmVAL, qmu::cmVAR, qmu::QmuParserToken< TBase, TString >::GetAsString(), qmu::QmuParserToken< TBase, TString >::GetCode(), qmu::QmuParserToken< TBase, TString >::GetType(), qmu::QmuParserToken< TBase, TString >::GetVal(), and qmu::tpSTR.
const QString & qmu::QmuParserBase::ValidInfixOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in infix operator definitions.
Definition at line 477 of file qmuparserbase.cpp.
References m_sInfixOprtChars.
Referenced by DefineInfixOprt(), and qmu::QmuParserTokenReader::IsInfixOpTok().
const QString & qmu::QmuParserBase::ValidNameChars | ( | ) | const |
Virtual function that defines the characters allowed in name identifiers.
Definition at line 455 of file qmuparserbase.cpp.
References m_sNameChars.
Referenced by DefineConst(), DefineFun(), DefineStrConst(), DefineVar(), qmu::QmuParserTokenReader::IsFunTok(), qmu::QmuParserTokenReader::IsStrVarTok(), qmu::QmuParserTokenReader::IsUndefVarTok(), qmu::QmuParserTokenReader::IsValTok(), qmu::QmuParserTokenReader::IsVarTok(), and qmu::QmuParserTokenReader::ReadNextToken().
const QString & qmu::QmuParserBase::ValidOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in operator definitions.
Definition at line 466 of file qmuparserbase.cpp.
References m_sOprtChars.
Referenced by DefineOprt(), DefinePostfixOprt(), qmu::QmuParserTokenReader::ExtractOperatorToken(), and qmu::QmuParserTokenReader::IsPostOpTok().
|
friend |
Definition at line 67 of file qmuparserbase.h.
|
private |
Definition at line 252 of file qmuparserbase.h.
Referenced by setAllowSubexpressions().
|
staticprotected |
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().
|
staticprotected |
Definition at line 140 of file qmuparserbase.h.
|
staticprotected |
Definition at line 141 of file qmuparserbase.h.
|
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().
|
private |
user constants.
Definition at line 234 of file qmuparserbase.h.
Referenced by Assign(), DefineConst(), DefineVar(), GetConst(), and qmu::QmuParserTokenReader::SetParent().
|
protected |
Definition at line 138 of file qmuparserbase.h.
Referenced by getDecimalPoint(), ResetLocale(), and setDecimalPoint().
|
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().
|
private |
unary infix operator.
Definition at line 232 of file qmuparserbase.h.
Referenced by AddCallback(), Assign(), DefineInfixOprt(), and qmu::QmuParserTokenReader::SetParent().
|
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().
|
mutableprivate |
Definition at line 248 of file qmuparserbase.h.
Referenced by Assign(), and GetNumResults().
|
mutableprivate |
Internal counter for keeping track of nested if-then-else clauses.
Definition at line 244 of file qmuparserbase.h.
|
mutableprivate |
Keep all numbers what exist in formula.
Definition at line 250 of file qmuparserbase.h.
Referenced by GetNumbers(), and ReInit().
|
private |
Binary operator callbacks.
Definition at line 233 of file qmuparserbase.h.
Referenced by AddCallback(), Assign(), DefineOprt(), and qmu::QmuParserTokenReader::SetParent().
|
private |
Postfix operator callbacks.
Definition at line 231 of file qmuparserbase.h.
Referenced by AddCallback(), Assign(), DefinePostfixOprt(), and qmu::QmuParserTokenReader::SetParent().
|
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().
|
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().
|
private |
Charset for infix operator tokens.
Definition at line 242 of file qmuparserbase.h.
Referenced by Assign(), DefineInfixOprtChars(), and ValidInfixOprtChars().
|
private |
Charset for names.
Definition at line 240 of file qmuparserbase.h.
Referenced by Assign(), DefineNameChars(), and ValidNameChars().
|
private |
Charset for postfix/ binary operator tokens.
Definition at line 241 of file qmuparserbase.h.
Referenced by Assign(), DefineOprtChars(), and ValidOprtChars().
|
private |
user defined string constants
Definition at line 235 of file qmuparserbase.h.
Referenced by Assign(), DefineStrConst(), and qmu::QmuParserTokenReader::SetParent().
|
protected |
Definition at line 139 of file qmuparserbase.h.
Referenced by getThousandsSeparator(), ResetLocale(), and setThousandsSeparator().
|
mutableprivate |
Keep all tokens that we can translate.
Definition at line 249 of file qmuparserbase.h.
Referenced by GetTokens(), and ReInit().
|
private |
user defind variables.
Definition at line 236 of file qmuparserbase.h.
Referenced by Assign(), DefineVar(), GetVar(), and qmu::QmuParserTokenReader::SetParent().
|
mutableprivate |
The Bytecode class.
Definition at line 224 of file qmuparserbase.h.
Referenced by ApplyBinOprt(), ApplyFunc(), ApplyIfElse(), ApplyStrFunc(), ParseCmdCodeBulk(), and ReInit().
|
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().
|
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().
|
private |
Definition at line 226 of file qmuparserbase.h.
Referenced by Assign(), DefineStrConst(), and qmu::QmuParserTokenReader::IsStrVarTok().
|
staticprivate |
Maximum number of threads spawned by OpenMP when using the bulk mode.
Definition at line 216 of file qmuparserbase.h.
Referenced by ParseCmdCodeBulk().