27 #include <QMessageLogger>
28 #include <QStringList>
56 :m_pParser( a_Reader.m_pParser ), m_strFormula( a_Reader.m_strFormula ), m_iPos( a_Reader.m_iPos ),
57 m_iSynFlags( a_Reader.m_iSynFlags ), m_bIgnoreUndefVar( a_Reader.m_bIgnoreUndefVar ),
58 m_pFunDef( a_Reader.m_pFunDef ), m_pPostOprtDef( a_Reader.m_pPostOprtDef ),
59 m_pInfixOprtDef( a_Reader.m_pInfixOprtDef ), m_pOprtDef( a_Reader.m_pOprtDef),
60 m_pConstDef( a_Reader.m_pConstDef ), m_pStrVarDef( a_Reader.m_pStrVarDef ), m_pVarDef( a_Reader.m_pVarDef ),
61 m_pFactory( a_Reader.m_pFactory ), m_pFactoryData( a_Reader.m_pFactoryData ), m_vIdentFun( a_Reader.m_vIdentFun ),
62 m_UsedVar( a_Reader.m_UsedVar ), m_fZero(0), m_iBrackets( a_Reader.m_iBrackets ), m_lastTok(),
63 m_cArgSep( a_Reader.m_cArgSep )
77 if ( &a_Reader !=
this )
129 : m_pParser ( a_pParent ), m_strFormula(), m_iPos ( 0 ), m_iSynFlags ( 0 ), m_bIgnoreUndefVar ( false ),
130 m_pFunDef ( nullptr ), m_pPostOprtDef ( nullptr ), m_pInfixOprtDef ( nullptr ), m_pOprtDef ( nullptr ),
131 m_pConstDef ( nullptr ), m_pStrVarDef ( nullptr ), m_pVarDef ( nullptr ), m_pFactory ( nullptr ),
132 m_pFactoryData ( nullptr ), m_vIdentFun(), m_UsedVar(), m_fZero ( 0 ), m_iBrackets ( 0 ), m_lastTok(),
151 ptr->SetParent ( a_pParent );
152 return ptr.release();
217 const QChar &thousand)
249 if (
IsValTok ( tok, locale, decimal, thousand ) )
325 QT_WARNING_DISABLE_MSVC(4309)
328 const std::wstring m_strFormulaStd = m_strFormula.toStdWString();
329 const std::wstring a_szCharSetStd = a_szCharSet.toStdWString();
331 int iEnd =
static_cast<int>(m_strFormulaStd.find_first_not_of ( a_szCharSetStd,
static_cast<std::size_t
>(a_iPos) ));
333 if ( iEnd ==
static_cast<int>(string_type::npos) )
335 iEnd =
static_cast<int>(m_strFormulaStd.length());
339 if ( a_iPos != iEnd )
341 a_sTok = QString().fromStdWString ( std::wstring ( m_strFormulaStd.begin() + a_iPos,
342 m_strFormulaStd.begin() + iEnd ) );
358 const std::wstring m_strFormulaStd =
m_strFormula.toStdWString();
362 int iEnd =
static_cast<int>( m_strFormulaStd.find_first_not_of ( oprtCharsStd,
static_cast<std::size_t
>(a_iPos) ) );
363 if ( iEnd ==
static_cast<int>( string_type::npos ) )
365 iEnd =
static_cast<int>( m_strFormulaStd.length() );
369 if ( a_iPos != iEnd )
371 a_sTok = QString().fromStdWString (
string_type ( m_strFormulaStd.begin() + a_iPos,
372 m_strFormulaStd.begin() + iEnd ) );
396 for (
int i = 0; i < pOprtDef.size(); ++i )
398 int len = pOprtDef.at ( i ).length();
488 a_Tok.
Set (
static_cast<ECmdCode>(i), pOprtDef.at ( i ) );
541 qDebug() <<
" Code:" << e.
GetCode() <<
"(" << e.
GetMsg() <<
")";
577 if ( sTok.indexOf ( it->first ) == 0 )
579 a_Tok.
Set ( it->second, it->first );
580 m_iPos +=
static_cast<int>(it->first.length());
612 funmap_type::const_iterator item =
m_pFunDef->find ( strTok );
624 a_Tok.
Set ( item->second, strTok );
654 QStringList::const_iterator constIterator;
655 for ( constIterator = pOprtDef.constBegin();
m_pParser->
HasBuiltInOprt() && constIterator != pOprtDef.constEnd();
658 if ( ( *constIterator ) == strTok )
673 const QString &sID = it->first;
676 a_Tok.
Set ( it->second, strTok );
746 if ( sTok.indexOf ( it->first ) == 0 )
748 a_Tok.
Set ( it->second, sTok );
749 m_iPos += it->first.length();
769 const QChar &thousand )
783 valmap_type::const_iterator item =
m_pConstDef->find ( strTok );
787 a_Tok.
SetVal ( item->second, strTok );
801 std::list<identfun_type>::const_iterator item =
m_vIdentFun.begin();
814 a_Tok.
SetVal ( fVal, strTok );
843 varmap_type::const_iterator item =
m_pVarDef->find ( strTok );
857 a_Tok.
SetVar ( item->second, strTok );
882 strmap_type::const_iterator item =
m_pStrVarDef->find ( strTok );
936 a_Tok.
SetVar ( fVar, strTok );
944 ( *m_pVarDef ) [strTok] = fVar;
976 int iEnd ( 0 ), iSkip ( 0 );
979 for ( iEnd = strBuf.indexOf (
"\"" ); iEnd != 0 && iEnd != -1; iEnd = strBuf.indexOf (
"\"", iEnd ) )
981 if ( strBuf[iEnd - 1] !=
'\\' )
985 strBuf.replace ( iEnd - 1, 2,
"\"" );
994 QString strTok = strBuf.mid ( 0, iEnd );
1004 m_iPos += strTok.length() + 2 + iSkip;
Mathematical expressions parser (base parser engine).
funmap_type m_OprtDef
Binary operator callbacks.
stringbuf_type m_vStringVarBuf
valmap_type m_ConstDef
user constants.
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.
const QString & ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
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.
stringbuf_type m_vStringBuf
String buffer, used for storing string function arguments.
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.
const QString & ValidOprtChars() const
Virtual function that defines the characters allowed in operator definitions.
varmap_type m_VarDef
user defind variables.
Error class of the parser.
EErrorCodes GetCode() const
Return the error code.
const QString & GetMsg() const
Returns the message string for this error.
Token reader for the ParserBase class.
std::list< identfun_type > m_vIdentFun
Value token identification function.
int ExtractOperatorToken(QString &a_sTok, int a_iPos) const
Check Expression for the presence of a binary operator token.
QChar m_cArgSep
The character used for separating function arguments.
varmap_type * m_pVarDef
The only non const pointer to parser internals.
const funmap_type * m_pPostOprtDef
bool IsBuiltIn(token_type &a_Tok)
Check if a built in operator or other token can be found.
const funmap_type * m_pFunDef
qreal m_fZero
Dummy value of zero, referenced by undefined variables.
int ExtractToken(const QString &a_szCharSet, QString &a_strTok, int a_iPos) const
Extract all characters that belong to a certain charset.
bool IsArgSep(token_type &a_Tok)
bool IsEOF(token_type &a_Tok)
Check for End of Formula.
void ReInit()
Reset the token reader to the start of the formula.
void AddValIdent(identfun_type a_pCallback)
void SetParent(QmuParserBase *a_pParent)
QmuParserTokenReader(QmuParserBase *a_pParent)
Constructor.
const funmap_type * m_pInfixOprtDef
bool IsValTok(token_type &a_Tok, const QLocale &locale, const QChar &decimal, const QChar &thousand)
Check whether the token at a given position is a value token.
bool IsUndefVarTok(token_type &a_Tok)
Check wheter a token at a given position is an undefined variable.
bool IsStrVarTok(token_type &a_Tok)
QmuParserToken< qreal, QString > token_type
bool IsFunTok(token_type &a_Tok)
Check whether the token at a given position is a function token.
int m_iBrackets
Keep count open brackets.
bool IsInfixOpTok(token_type &a_Tok)
Check if a string position contains a unary infix operator.
const valmap_type * m_pConstDef
bool IsString(token_type &a_Tok)
Check wheter a token at a given position is a string.
void SetFormula(const QString &a_strFormula)
Initialize the token Reader.
bool IsOprt(token_type &a_Tok)
Check if a string position contains a binary operator.
QmuParserBase * m_pParser
token_type ReadNextToken(const QLocale &locale, const QChar &decimal, const QChar &thousand)
Read the next token from the string.
QmuParserTokenReader * Clone(QmuParserBase *a_pParent) const
Create instance of a QParserTokenReader identical with this and return its pointer.
bool IsVarTok(token_type &a_Tok)
Check wheter a token at a given position is a variable token.
const strmap_type * m_pStrVarDef
void Assign(const QmuParserTokenReader &a_Reader)
Assign state of a token reader to this token reader.
const funmap_type * m_pOprtDef
QmuParserTokenReader & operator=(const QmuParserTokenReader &a_Reader)
Assignement operator.
bool IsPostOpTok(token_type &a_Tok)
Check if a string position contains a unary post value operator.
void SetVarCreator(facfun_type a_pFactory, void *pUserData)
@ noOPT
to avoid i.e. "(+)"
@ noBC
to avoid i.e. "sin)" or "()"
@ noANY
All of he above flags set.
@ noSTR
to block numeric arguments on string functions
@ noBO
to avoid i.e. "cos(7)("
@ noINFIXOP
to avoid i.e. "++4" "!!4"
@ noEND
to avoid unexpected end of formula
@ noASSIGN
to block assignement to constant i.e. "4=7"
@ noARG_SEP
to avoid i.e. ",," or "+," ...
@ noVAL
to avoid i.e. "tan 2" or "sin(8)3.14"
@ noPOSTOP
to avoid i.e. "(5!!)" "sin!"
@ noFUN
to avoid i.e. "sqrt cos" or "(1)sin"
@ noVAR
to avoid i.e. "sin a" or "sin(8)a"
token_type & SaveBeforeReturn(const token_type &tok)
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.
ECmdCode GetCode() const
Return the token type.
QmuParserToken & SetVal(TBase a_fVal, const TString &a_strTok=TString())
Make this token a value token.
QmuParserToken & SetVar(TBase *a_pVar, const TString &a_strTok)
make this token a variable token.
QmuParserToken & Set(ECmdCode a_iType, const TString &a_strTok=TString())
Assign a token type.
QmuParserToken & SetString(const TString &a_strTok, int a_iSize)
Make this token a variable token.
const TString & GetAsString() const
Return the token identifier.
Namespace for mathematical applications.
@ ecMISSING_PARENS
Missing parens. (Example: "3*sin(3")
@ ecUNEXPECTED_OPERATOR
Unexpected binary operator found.
@ ecUNEXPECTED_STR
A string has been found at an inapropriate position.
@ ecUNEXPECTED_EOF
Unexpected end of formula. (Example: "2+sin(")
@ ecUNEXPECTED_FUN
Unexpected function found. (Example: "sin(8)cos(9)")
@ ecUNASSIGNABLE_TOKEN
Token cant be identified.
@ ecUNEXPECTED_ARG_SEP
An unexpected semicolon has been found. (Example: "1;23")
@ ecUNEXPECTED_PARENS
Unexpected Parenthesis, opening or closing.
@ ecINTERNAL_ERROR
Internal error of any kind.
@ ecUNEXPECTED_VAR
An unexpected variable token has been found.
@ ecUNEXPECTED_CONDITIONAL
@ ecUNEXPECTED_VAL
An unexpected value token has been found.
@ ecUNTERMINATED_STRING
unterminated string constant. (Example: "3*valueof("hello)")
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.
@ cmBC
Operator item: closing bracket.
@ cmLE
Operator item: less or equal.
@ cmFUNC
Code for a generic function item.
@ cmASSIGN
Operator item: Assignment operator.
@ cmELSE
For use in the ternary if-then-else operator.
@ cmARG_SEP
function argument separator
@ cmIF
For use in the ternary if-then-else operator.
@ cmBO
Operator item: opening bracket.
std::wstring string_type
The stringtype used by the parser.
This file contains the class definition of the qmuparser engine.
This file contains the parser token reader definition.