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... | |
QtxEvalSet * | operationSet (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< PostfixItem > | Postfix |
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 |
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.
|
protected |
|
protected |
|
protected |
|
protected |
QtxEvalParser::QtxEvalParser | ( | ) |
References QtxEvalExpr::OK, and setError().
|
virtual |
References autoDeleteOperationSets(), and mySets.
bool QtxEvalParser::autoDeleteOperationSets | ( | ) | const |
true
if all operations shoud be automatically deleted when the parser is destroyed References myAutoDel.
|
private |
list | returning list of brackets |
open | if true , collect opening brackets, or closing brackets otherwise |
References QtxEvalSet::bracketsList(), and mySets.
QVariant QtxEvalParser::calculate | ( | ) |
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 | ) |
expr | new expression |
References calculate(), and setExpression().
|
protected |
The result of the operation is returned in the parameter v1.
op | operation name |
v1 | first argument (not valid for unary prefix operations) |
v2 | second argument (not valid for unary postfix operations) |
true
on success and false
if error is found References calculation(), error(), isValid(), QtxEvalExpr::OK, and setError().
|
private |
The result of the operation is returned in the parameter v1. If the operation is calculated correctly, the function returns QtxEvalExpr::OK.
op | operation name |
v1 | first argument (not valid for unary prefix operations) |
v2 | second argument (not valid for unary postfix operations) |
References QtxEvalExpr::InvalidOperation, QtxEvalExpr::InvalidResult, mySets, and QtxEvalExpr::OK.
|
private |
false
if current set of operations is empty References mySets, QtxEvalExpr::OperationsNull, and setError().
|
virtual |
References myParams.
|
private |
QString QtxEvalParser::dump | ( | ) | const |
References myPostfix.
post | postfix to be dumped |
References Binary, checkOperations(), Post, Pre, and Value.
QtxEvalExpr::Error QtxEvalParser::error | ( | ) | const |
References myError.
bool QtxEvalParser::firstInvalid | ( | QString & | name | ) | const |
name | used to retrieve the name of the parameter if it is found |
true
if parameter is found References myParams.
|
virtual |
void QtxEvalParser::insertOperationSet | ( | QtxEvalSet * | set, |
const int | idx = -1 |
||
) |
operations | set to be added |
idx | index in the list at which the operations set shoud be inserted |
References mySets.
|
private |
If the operation is valid, QtxEvalExpr::OK is returned.
op | operation |
t1 | first operand type |
t2 | second operand type |
References mySets, and QtxEvalExpr::OK.
|
staticprotected |
str | source string |
pos | start position of the substring |
len | length of the substring |
|
private |
list | returning list of the operations names |
References mySets, and QtxEvalSet::operationList().
QtxEvalSet * QtxEvalParser::operationSet | ( | const QString & | name | ) | const |
name | the name of the operation set |
References mySets.
QList< QtxEvalSet * > QtxEvalParser::operationSets | ( | ) | const |
References mySets.
|
virtual |
name | parameter name |
References myParams.
QStringList QtxEvalParser::parameters | ( | ) | const |
|
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.
expr | string expression |
true
on success and false
if error is found References bracketsList(), checkOperations(), myPostfix, QtxEvalExpr::OK, prepare(), setError(), setOperationTypes(), and sort().
Performs the first step of the parsing:
expr | string expression |
post | postfix to be created |
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.
|
private |
op | operation |
isBin | true if the operation is binary and false if it is unary |
References mySets.
void QtxEvalParser::removeInvalids | ( | ) |
References myParams.
void QtxEvalParser::removeOperationSet | ( | QtxEvalSet * | set | ) |
operations | set to be removed |
References mySets.
|
virtual |
|
staticprotected |
list | list of substrings |
str | string in which the searching is performed |
offset | starting index for search |
matchLen | returning matching length of any substring |
listind | returning index of the found substring in the list |
void QtxEvalParser::setAutoDeleteOperationSets | ( | const bool | on | ) |
on | if true , all operations shoud be automatically deleted when the parser is destroyed |
References myAutoDel.
|
protectedvirtual |
err | error code |
References myError.
bool QtxEvalParser::setExpression | ( | const QString & | expr | ) |
expr | new expression |
true
on success and false
if error is found References parse().
|
protectedvirtual |
Second step of the parsing: analyze the types of the operations used in the expression.
post | unsorted postfix |
true
on success and false
if error is found References Binary, bracketsList(), checkOperations(), Close, error(), QtxEvalExpr::OK, Open, Param, Post, Pre, and Value.
name | parameter name |
value | parameter value |
References myParams.
|
protectedvirtual |
Third step of parsing: sort the postfix operations in order to convert it to real postfix.
post | source postfix |
res | returning resulting postfix |
anOpen | list of open brackets |
aClose | list of close brackets |
f | start index of postfix for sorting |
l | last index of postfix for sorting |
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.
list | list to be converted |
|
private |
|
private |
|
private |
|
private |
|
private |