Version: 6.3.1
Data Structures | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Private Member Functions | Private Attributes

QtxEvalParser Class Reference

Expression parser. More...

#include <QtxEvalExpr.h>

Data Structures

struct  PostfixItem
 Postfix representation element. More...

Public Member Functions

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

Static Public Member Functions

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

Protected Types

enum  PostfixItemType {
  Value, Param, Open, Close,
  Pre, Post, Binary
}
 

Types of postfix representation elements.

More...
typedef QList< PostfixItemPostfix
 postfix representation
typedef QList< QtxEvalSet * > SetList
 list of operations
typedef QMap< QString, QVariant > ParamMap
 parameter-to-value map

Protected Member Functions

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

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.
static QString note (const QString &str, int pos, int len)
 Get the substring field from the string str.
static int globalBrackets (const Postfix &, int, int)
 Get the number of the globar brackets pairs.

Private Member Functions

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

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 QList<PostfixItem> QtxEvalParser.Postfix [protected]
typedef QList<QtxEvalSet*> QtxEvalParser.SetList [protected]
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

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::setExpression ( const QString &  expr)
Parameters:
exprnew expression
Returns:
true on success and false if error is found

References parse().

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

References mySets.

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

References mySets.

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

References mySets.

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.

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::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::clearParameters ( ) [virtual]

References myParams.

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

References myParams.

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.

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

References myParams.

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

References myParams.

QStringList QtxEvalParser::parameters ( ) const
Returns:
parameters names

References myPostfix, and Param.

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.

void QtxEvalParser::removeInvalids ( )

References myParams.

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

References myPostfix.

QString QtxEvalParser::toString ( const QList< QVariant > &  list) [static]
Parameters:
listlist to be converted
Returns:
string representation for the list
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.

bool QtxEvalParser::prepare ( const QString &  expr,
Postfix post 
) [protected, virtual]

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.

bool QtxEvalParser::setOperationTypes ( Postfix post) [protected, virtual]

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.

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

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, test_big_table.k, test_table.l, QtxEvalParser.PostfixItem.myType, QtxEvalParser.PostfixItem.myValue, QtxEvalExpr.OK, Open, Param, Post, Pre, priority(), setError(), and Value.

bool QtxEvalParser::parse ( const QString &  expr) [protected, virtual]

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

void QtxEvalParser::setError ( const QtxEvalExpr::Error  err) [protected, virtual]
Parameters:
errerror code

References myError.

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

int QtxEvalParser::search ( const QStringList &  list,
const QString &  str,
int  offset,
int &  matchLen,
int &  listind 
) [static, protected]
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
QString QtxEvalParser::note ( const QString &  str,
int  pos,
int  len 
) [static, protected]
Parameters:
strsource string
posstart position of the substring
lenlength of the substring
Returns:
substring (leading and trailing spaces are truncated)
int QtxEvalParser::globalBrackets ( const Postfix post,
int  f,
int  l 
) [static, protected]

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, test_table.l, and Open.

void QtxEvalParser::operationList ( QStringList &  list) const [private]
Parameters:
listreturning list of the operations names

References mySets, and QtxEvalSet.operationList().

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.

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

References mySets.

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.

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.

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


Field Documentation

bool QtxEvalParser.myAutoDel [private]
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS