Version: 8.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
QtxEvalParser Class Reference

Expression parser. More...

#include <QtxEvalExpr.h>

Classes

struct  PostfixItem
 Postfix representation element. More...
 

Public Member Functions

 QtxEvalParser ()
 Constructor. More...
 
virtual ~QtxEvalParser ()
 Destructor. More...
 
QVariant calculate ()
 Calculate the expression without postfix rebuilding. More...
 
QVariant calculate (const QString &)
 Change the expression, rebuild the postfix and calculate it. More...
 
bool setExpression (const QString &)
 Change the expression and rebuild the postfix. More...
 
QList< QtxEvalSet * > operationSets () const
 Get current operations set. More...
 
QtxEvalSetoperationSet (const QString &) const
 Get the operations set by name. More...
 
void removeOperationSet (QtxEvalSet *)
 Remove the operations set. More...
 
void insertOperationSet (QtxEvalSet *, const int=-1)
 Install the operations set. More...
 
bool autoDeleteOperationSets () const
 Get the 'auto-delete operations' flag value. More...
 
void setAutoDeleteOperationSets (const bool)
 Set the 'auto-delete operations' flag value. More...
 
virtual void clearParameters ()
 Remove all parameters. More...
 
virtual bool removeParameter (const QString &name)
 Remove parameter. More...
 
virtual QVariant parameter (const QString &name) const
 Get the parameter value. More...
 
virtual bool hasParameter (const QString &name) const
 Check if the parser contains specified parameter. More...
 
virtual void setParameter (const QString &name, const QVariant &value)
 Set parameters value. More...
 
QStringList parameters () const
 Get the list of the parameters names. More...
 
QtxEvalExpr::Error error () const
 Get the code of the latest parsing error. More...
 
bool firstInvalid (QString &) const
 Search first parameter with assigned invalid value. More...
 
void removeInvalids ()
 Remove all parameters with assigned invalid values. More...
 
QString dump () const
 Dump the current postfix contents to the string. More...
 

Static Public Member Functions

static QString toString (const QList< QVariant > &)
 Get the string representation for the list of QVariant values. More...
 

Protected Types

enum  PostfixItemType {
  Value, Param, Open, Close,
  Pre, Post, Binary
}
 Types of postfix representation elements. More...
 
typedef QList< PostfixItemPostfix
 postfix representation More...
 
typedef QList< QtxEvalSet * > SetList
 list of operations More...
 
typedef QMap< QString, QVariant > ParamMap
 parameter-to-value map More...
 

Protected Member Functions

QString dump (const Postfix &) const
 Dump the postfix contents to the string. More...
 
virtual bool prepare (const QString &, Postfix &)
 Prepare to the parsing. More...
 
virtual bool setOperationTypes (Postfix &)
 Analyze the operations used. More...
 
virtual bool sort (const Postfix &, Postfix &, const QStringList &, const QStringList &, int f=-1, int l=-1)
 Sort the operations in the postfix. More...
 
virtual bool parse (const QString &)
 Parse the expression and build the posfix. More...
 
virtual void setError (const QtxEvalExpr::Error)
 Set the error vode. More...
 
bool calculate (const QString &, QVariant &, QVariant &)
 Calculate the operation. More...
 

Static Protected Member Functions

static int search (const QStringList &, const QString &, int offset, int &matchLen, int &listind)
 Search elements of the list as substrings starting from offset. More...
 
static QString note (const QString &str, int pos, int len)
 Get the substring field from the string str. More...
 
static int globalBrackets (const Postfix &, int, int)
 Get the number of the globar brackets pairs. More...
 

Private Member Functions

void operationList (QStringList &) const
 Get names of all operations used in the expression. More...
 
void bracketsList (QStringList &, bool) const
 Get list of brackets. More...
 
bool createValue (const QString &, QVariant &) const
 Create value. More...
 
int priority (const QString &, bool isBin) const
 Get the operation priority level. More...
 
QtxEvalExpr::Error isValid (const QString &, const QVariant::Type, const QVariant::Type) const
 Check operation validity. More...
 
QtxEvalExpr::Error calculation (const QString &, QVariant &, QVariant &) const
 Perform calculation. More...
 
bool checkOperations () const
 Check current operations set. More...
 

Private Attributes

SetList mySets
 
QtxEvalExpr::Error myError
 
ParamMap myParams
 
Postfix myPostfix
 
bool myAutoDel
 

Detailed Description

This class provides the functionality to calculate value of the expression using defined set of operations. Standard operations (arithmetics, logic, strings, etc) are implemented in the corresponding successors of the QtxEvalSet class: QtxEvalSetArithmetic, QtxEvalSetLogic, QtxEvalSetMath, QtxEvalSetString, ...

The parser allows using parameters with help of methods has(), set(), remove(), value(). It uses postfix representation of expressions and uses class QtxEvalSet in order to make certain operation.

Every instance of parser contains only one postfix - thus, if the expression is changed, the postfix must be rebuilt. In order to increase performance of frequent calculations for many of expressions it is recommended to use different instances of the parser for each expression.

Member Typedef Documentation

typedef QMap<QString, QVariant> QtxEvalParser::ParamMap
protected

Member Enumeration Documentation

Enumerator
Value 

Value (number, string, etc.)

Param 

Parameter.

Open 

Open bracket.

Close 

Close bracket.

Pre 

Unary prefix operation.

Post 

Unary postfix operation.

Binary 

Binary operation.

Constructor & Destructor Documentation

QtxEvalParser::QtxEvalParser ( )

References QtxEvalExpr::OK, and setError().

QtxEvalParser::~QtxEvalParser ( )
virtual

Member Function Documentation

bool QtxEvalParser::autoDeleteOperationSets ( ) const
Returns
true if all operations shoud be automatically deleted when the parser is destroyed
See Also
setAutoDeleteOperationSets()

References myAutoDel.

void QtxEvalParser::bracketsList ( QStringList &  list,
bool  open 
) const
private
Parameters
listreturning list of brackets
openif true, collect opening brackets, or closing brackets otherwise

References QtxEvalSet::bracketsList(), and mySets.

QVariant QtxEvalParser::calculate ( )
Returns
QVariant as result (it is invalid if there were errors during calculation)

References Binary, bracketsList(), checkOperations(), error(), QtxEvalExpr::ExcessData, hasParameter(), QtxEvalExpr::InvalidToken, myParams, myPostfix, QtxEvalExpr::OK, Param, Post, Pre, setError(), QtxEvalExpr::StackUnderflow, and Value.

QVariant QtxEvalParser::calculate ( const QString &  expr)
Parameters
exprnew expression
Returns
QVariant as result (it is invalid if there were errors during calculation)

References calculate(), and setExpression().

bool QtxEvalParser::calculate ( const QString &  op,
QVariant &  v1,
QVariant &  v2 
)
protected

The result of the operation is returned in the parameter v1.

Parameters
opoperation name
v1first argument (not valid for unary prefix operations)
v2second argument (not valid for unary postfix operations)
Returns
true on success and false if error is found

References calculation(), error(), isValid(), QtxEvalExpr::OK, and setError().

QtxEvalExpr::Error QtxEvalParser::calculation ( const QString &  op,
QVariant &  v1,
QVariant &  v2 
) const
private

The result of the operation is returned in the parameter v1. If the operation is calculated correctly, the function returns QtxEvalExpr::OK.

Parameters
opoperation name
v1first argument (not valid for unary prefix operations)
v2second argument (not valid for unary postfix operations)
Returns
error code (QtxEvalExpr::Error)

References QtxEvalExpr::InvalidOperation, QtxEvalExpr::InvalidResult, mySets, and QtxEvalExpr::OK.

bool QtxEvalParser::checkOperations ( ) const
private
Returns
false if current set of operations is empty

References mySets, QtxEvalExpr::OperationsNull, and setError().

void QtxEvalParser::clearParameters ( )
virtual

References myParams.

bool QtxEvalParser::createValue ( const QString &  str,
QVariant &  val 
) const
private
Parameters
strparsed string
valreturning value
Returns
true on success

References mySets.

QString QtxEvalParser::dump ( ) const
Returns
string representation of the internal parser postfix

References myPostfix.

QString QtxEvalParser::dump ( const Postfix post) const
protected
Parameters
postpostfix to be dumped
Returns
string representation of the postfix

References Binary, checkOperations(), Post, Pre, and Value.

QtxEvalExpr::Error QtxEvalParser::error ( ) const
Returns
last error code

References myError.

bool QtxEvalParser::firstInvalid ( QString &  name) const
Parameters
nameused to retrieve the name of the parameter if it is found
Returns
true if parameter is found

References myParams.

int QtxEvalParser::globalBrackets ( const Postfix post,
int  f,
int  l 
)
staticprotected

For example, the expression '((2+3))' has 2 global brackets pairs.

Parameters
postpostfix to be checked
fstarting position for the search
llast position for the search
Returns
number of brackets pairs

References Close, and Open.

bool QtxEvalParser::hasParameter ( const QString &  name) const
virtual
Parameters
nameparameter name
Returns
true, if the parser contains parameter

References myParams.

void QtxEvalParser::insertOperationSet ( QtxEvalSet set,
const int  idx = -1 
)
Parameters
operationsset to be added
idxindex in the list at which the operations set shoud be inserted
See Also
operationSets(), removeOperationSet(), operationSet()

References mySets.

QtxEvalExpr::Error QtxEvalParser::isValid ( const QString &  op,
const QVariant::Type  t1,
const QVariant::Type  t2 
) const
private

If the operation is valid, QtxEvalExpr::OK is returned.

Parameters
opoperation
t1first operand type
t2second operand type
Returns
error code (QtxEvalExpr::Error)

References mySets, and QtxEvalExpr::OK.

QString QtxEvalParser::note ( const QString &  str,
int  pos,
int  len 
)
staticprotected
Parameters
strsource string
posstart position of the substring
lenlength of the substring
Returns
substring (leading and trailing spaces are truncated)
void QtxEvalParser::operationList ( QStringList &  list) const
private
Parameters
listreturning list of the operations names

References mySets, and QtxEvalSet::operationList().

QtxEvalSet * QtxEvalParser::operationSet ( const QString &  name) const
Parameters
namethe name of the operation set
Returns
operation set or 0 if not found

References mySets.

QList< QtxEvalSet * > QtxEvalParser::operationSets ( ) const
Returns
current operations set

References mySets.

QVariant QtxEvalParser::parameter ( const QString &  name) const
virtual
Parameters
nameparameter name
Returns
parameter value or invalud QVariant if there is no such parameter

References myParams.

QStringList QtxEvalParser::parameters ( ) const
Returns
parameters names

References myPostfix, and Param.

bool QtxEvalParser::parse ( const QString &  expr)
protectedvirtual

If by parsing error is found, the function returns false. In this case the code of the error can be retrieved with error() method.

Parameters
exprstring expression
Returns
true on success and false if error is found

References bracketsList(), checkOperations(), myPostfix, QtxEvalExpr::OK, prepare(), setError(), setOperationTypes(), and sort().

bool QtxEvalParser::prepare ( const QString &  expr,
Postfix post 
)
protectedvirtual

Performs the first step of the parsing:

  • find tokens
  • determine tokens types
  • create unsorted pseudo-postfix (with brackets)
Parameters
exprstring expression
postpostfix to be created
Returns
true on success and false if error is found

References Binary, bracketsList(), QtxEvalExpr::BracketsNotMatch, checkOperations(), Close, QtxEvalExpr::CloseExpected, createValue(), error(), QtxEvalExpr::ExcessClose, QtxEvalParser::PostfixItem::myType, QtxEvalParser::PostfixItem::myValue, note(), QtxEvalExpr::OK, Open, operationList(), Param, search(), setError(), and Value.

int QtxEvalParser::priority ( const QString &  op,
bool  isBin 
) const
private
Parameters
opoperation
isBintrue if the operation is binary and false if it is unary
Returns
operation priority

References mySets.

void QtxEvalParser::removeInvalids ( )

References myParams.

void QtxEvalParser::removeOperationSet ( QtxEvalSet set)
Parameters
operationsset to be removed
See Also
operationSets(), insertOperationSet(), operationSet()

References mySets.

bool QtxEvalParser::removeParameter ( const QString &  name)
virtual
Parameters
nameparameter name
Returns
true on success

References myParams.

int QtxEvalParser::search ( const QStringList &  list,
const QString &  str,
int  offset,
int &  matchLen,
int &  listind 
)
staticprotected
Parameters
listlist of substrings
strstring in which the searching is performed
offsetstarting index for search
matchLenreturning matching length of any substring
listindreturning index of the found substring in the list
Returns
position of first found substring inside the str or -1 if no matches is found
void QtxEvalParser::setAutoDeleteOperationSets ( const bool  on)
Parameters
onif true, all operations shoud be automatically deleted when the parser is destroyed
See Also
autoDeleteOperationSets()

References myAutoDel.

void QtxEvalParser::setError ( const QtxEvalExpr::Error  err)
protectedvirtual
Parameters
errerror code

References myError.

bool QtxEvalParser::setExpression ( const QString &  expr)
Parameters
exprnew expression
Returns
true on success and false if error is found

References parse().

bool QtxEvalParser::setOperationTypes ( Postfix post)
protectedvirtual

Second step of the parsing: analyze the types of the operations used in the expression.

Parameters
postunsorted postfix
Returns
true on success and false if error is found

References Binary, bracketsList(), checkOperations(), Close, error(), QtxEvalExpr::OK, Open, Param, Post, Pre, and Value.

void QtxEvalParser::setParameter ( const QString &  name,
const QVariant &  value 
)
virtual
Parameters
nameparameter name
valueparameter value

References myParams.

bool QtxEvalParser::sort ( const Postfix post,
Postfix res,
const QStringList &  anOpen,
const QStringList &  aClose,
int  f = -1,
int  l = -1 
)
protectedvirtual

Third step of parsing: sort the postfix operations in order to convert it to real postfix.

Parameters
postsource postfix
resreturning resulting postfix
anOpenlist of open brackets
aCloselist of close brackets
fstart index of postfix for sorting
llast index of postfix for sorting
Returns
true on success and false if error is found

References Binary, checkOperations(), Close, QtxEvalExpr::CloseExpected, error(), globalBrackets(), QtxEvalExpr::InvalidOperation, QtxEvalParser::PostfixItem::myType, QtxEvalParser::PostfixItem::myValue, QtxEvalExpr::OK, Open, Param, Post, Pre, priority(), setError(), and Value.

QString QtxEvalParser::toString ( const QList< QVariant > &  list)
static
Parameters
listlist to be converted
Returns
string representation for the list

Member Data Documentation

bool QtxEvalParser::myAutoDel
private
QtxEvalExpr::Error QtxEvalParser::myError
private
ParamMap QtxEvalParser::myParams
private
Postfix QtxEvalParser::myPostfix
private
SetList QtxEvalParser::mySets
private

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