Version: 9.12.0
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

Expression parser.

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

◆ ParamMap

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

parameter-to-value map

◆ Postfix

postfix representation

◆ SetList

list of operations

Member Enumeration Documentation

◆ PostfixItemType

Types of postfix representation elements.

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::QtxEvalParser ( )

Constructor.

References QtxEvalExpr::OK, and setError().

◆ ~QtxEvalParser()

QtxEvalParser::~QtxEvalParser ( )
virtual

Destructor.

References autoDeleteOperationSets(), and mySets.

Member Function Documentation

◆ autoDeleteOperationSets()

bool QtxEvalParser::autoDeleteOperationSets ( ) const

Get the 'auto-delete operations' flag value.

Returns
true if all operations shoud be automatically deleted when the parser is destroyed
See also
setAutoDeleteOperationSets()

References myAutoDel.

◆ bracketsList()

void QtxEvalParser::bracketsList ( QStringList &  list,
bool  open 
) const
private

Get list of brackets.

Parameters
listreturning list of brackets
openif true, collect opening brackets, or closing brackets otherwise

References QtxEvalSet::bracketsList(), and mySets.

◆ calculate() [1/3]

QVariant QtxEvalParser::calculate ( )

Calculate the expression without postfix rebuilding.

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.

◆ calculate() [2/3]

QVariant QtxEvalParser::calculate ( const QString &  expr)

Change the expression, rebuild the postfix and calculate it.

Parameters
exprnew expression
Returns
QVariant as result (it is invalid if there were errors during calculation)

References calculate(), and setExpression().

◆ calculate() [3/3]

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

Calculate the operation.

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().

◆ calculation()

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

Perform calculation.

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.

◆ checkOperations()

bool QtxEvalParser::checkOperations ( ) const
private

Check current operations set.

Returns
false if current set of operations is empty

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

◆ clearParameters()

void QtxEvalParser::clearParameters ( )
virtual

Remove all parameters.

References myParams.

◆ createValue()

bool QtxEvalParser::createValue ( const QString &  str,
QVariant &  val 
) const
private

Create value.

Parameters
strparsed string
valreturning value
Returns
true on success

References mySets.

◆ dump() [1/2]

QString QtxEvalParser::dump ( ) const

Dump the current postfix contents to the string.

Returns
string representation of the internal parser postfix

References myPostfix.

◆ dump() [2/2]

QString QtxEvalParser::dump ( const Postfix post) const
protected

Dump the postfix contents to the string.

Parameters
postpostfix to be dumped
Returns
string representation of the postfix

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

◆ error()

QtxEvalExpr::Error QtxEvalParser::error ( ) const

Get the code of the latest parsing error.

Returns
last error code

References myError.

◆ firstInvalid()

bool QtxEvalParser::firstInvalid ( QString &  name) const

Search first parameter with assigned invalid value.

Parameters
nameused to retrieve the name of the parameter if it is found
Returns
true if parameter is found

References myParams.

◆ globalBrackets()

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

Get the number of the globar brackets pairs.

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.

◆ hasParameter()

bool QtxEvalParser::hasParameter ( const QString &  name) const
virtual

Check if the parser contains specified parameter.

Parameters
nameparameter name
Returns
true, if the parser contains parameter

References myParams.

◆ insertOperationSet()

void QtxEvalParser::insertOperationSet ( QtxEvalSet set,
const int  idx = -1 
)

Install the operations set.

Parameters
operationsset to be added
idxindex in the list at which the operations set shoud be inserted
See also
operationSets(), removeOperationSet(), operationSet()

References mySets.

◆ isValid()

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

Check operation validity.

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.

◆ note()

QString QtxEvalParser::note ( const QString &  str,
int  pos,
int  len 
)
staticprotected

Get the substring field from the string str.

Parameters
strsource string
posstart position of the substring
lenlength of the substring
Returns
substring (leading and trailing spaces are truncated)

◆ operationList()

void QtxEvalParser::operationList ( QStringList &  list) const
private

Get names of all operations used in the expression.

Parameters
listreturning list of the operations names

References mySets, and QtxEvalSet::operationList().

◆ operationSet()

QtxEvalSet * QtxEvalParser::operationSet ( const QString &  name) const

Get the operations set by name.

Parameters
namethe name of the operation set
Returns
operation set or 0 if not found

References mySets.

◆ operationSets()

QList< QtxEvalSet * > QtxEvalParser::operationSets ( ) const

Get current operations set.

Returns
current operations set

References mySets.

◆ parameter()

QVariant QtxEvalParser::parameter ( const QString &  name) const
virtual

Get the parameter value.

Parameters
nameparameter name
Returns
parameter value or invalud QVariant if there is no such parameter

References myParams.

◆ parameters()

QStringList QtxEvalParser::parameters ( ) const

Get the list of the parameters names.

Returns
parameters names

References myPostfix, and Param.

◆ parse()

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

Parse the expression and build the posfix.

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().

◆ prepare()

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

Prepare to the parsing.

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.

◆ priority()

int QtxEvalParser::priority ( const QString &  op,
bool  isBin 
) const
private

Get the operation priority level.

Parameters
opoperation
isBintrue if the operation is binary and false if it is unary
Returns
operation priority

References mySets.

◆ removeInvalids()

void QtxEvalParser::removeInvalids ( )

Remove all parameters with assigned invalid values.

References myParams.

◆ removeOperationSet()

void QtxEvalParser::removeOperationSet ( QtxEvalSet set)

Remove the operations set.

Parameters
operationsset to be removed
See also
operationSets(), insertOperationSet(), operationSet()

References mySets.

◆ removeParameter()

bool QtxEvalParser::removeParameter ( const QString &  name)
virtual

Remove parameter.

Parameters
nameparameter name
Returns
true on success

References myParams.

◆ search()

int QtxEvalParser::search ( const QStringList &  list,
const QString &  str,
int  offset,
int &  matchLen,
int &  listind 
)
staticprotected

Search elements of the list as substrings starting from offset.

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

◆ setAutoDeleteOperationSets()

void QtxEvalParser::setAutoDeleteOperationSets ( const bool  on)

Set the 'auto-delete operations' flag value.

Parameters
onif true, all operations shoud be automatically deleted when the parser is destroyed
See also
autoDeleteOperationSets()

References myAutoDel.

◆ setError()

void QtxEvalParser::setError ( const QtxEvalExpr::Error  err)
protectedvirtual

Set the error vode.

Parameters
errerror code

References myError.

◆ setExpression()

bool QtxEvalParser::setExpression ( const QString &  expr)

Change the expression and rebuild the postfix.

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

References parse().

◆ setOperationTypes()

bool QtxEvalParser::setOperationTypes ( Postfix post)
protectedvirtual

Analyze the operations used.

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.

◆ setParameter()

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

Set parameters value.

Parameters
nameparameter name
valueparameter value

References myParams.

◆ sort()

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

Sort the operations in the postfix.

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.

◆ toString()

QString QtxEvalParser::toString ( const QList< QVariant > &  list)
static

Get the string representation for the list of QVariant values.

Parameters
listlist to be converted
Returns
string representation for the list

Member Data Documentation

◆ myAutoDel

bool QtxEvalParser::myAutoDel
private

◆ myError

QtxEvalExpr::Error QtxEvalParser::myError
private

◆ myParams

ParamMap QtxEvalParser::myParams
private

◆ myPostfix

Postfix QtxEvalParser::myPostfix
private

◆ mySets

SetList QtxEvalParser::mySets
private

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