1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
20 // Author: Alexander SOLOVYOV
22 #ifndef __QTX_PARSER_HEADER__
23 #define __QTX_PARSER_HEADER__
26 #include <qvaluestack.h>
30 #pragma warning( disable:4251 )
37 \brief Alias for QVariant
39 typedef QVariant QtxValue;
45 This class allows to calculate values of expressions using different set of operations.
46 It is provided some of standard set of operations (arithmetics, logic, strings, etc - in QtxStdOperations.h).
47 This parser allows to use parameters with help of methods has(), set(), remove(), value(). It uses
48 postfix representation of expressions and uses class QtxOperations in order to make certain operation
49 Every instance of parser contains only one postfix, so that if expression has been changed, then postfix
50 must be rebuilt. In order to increase performance of frequent calculation for many of expressions it is
51 recommended to use different instances of parser for expressions
54 class QTX_EXPORT QtxParser
59 \brief Errors during parsing
63 OK, /*! \var All right */
64 OperandsNotMatch, /*! \var Types of arguments are invalid for this operation */
65 InvalidResult, /*! \var Operation cannot find result (for example, division by zero) */
66 InvalidOperation, /*! \var Name of operation is unknown */
67 OperationsNull, /*! \var Internal operations pointer of parser is null */
68 InvalidToken, /*! \var It isn't operation, parameter of value */
69 CloseExpected, /*! \var Close bracket is expected */
70 ExcessClose, /*! \var The one of close bracket is excess */
71 BracketsNotMatch, /*! \var Last open and this close bracket are different, for example [) */
72 StackUnderflow, /*! \var There is no arguments in stack for operation */
73 ExcessData /*! \var The parsing is finished, but there is more then one value in stack */
78 QtxParser( QtxOperations*, const QString& = QString::null );
82 QtxValue calculate( const QString& );
83 bool setExpr( const QString& );
86 virtual bool has ( const QString& name ) const;
87 virtual void set ( const QString& name, const QtxValue& value );
88 virtual bool remove( const QString& name );
89 virtual QtxValue value ( const QString& name ) const;
91 bool firstInvalid( QString& ) const;
92 void removeInvalids();
94 Error lastError() const;
95 void paramsList( QStringList& );
97 static QString toString( const QValueList< QtxValue >& );
101 \enum PostfixItemType
102 \brief Types of postfix representation elements
106 Value, /*! \var Value (number, string, etc.)*/
107 Param, /*! \var Parameter */
108 Open, /*! \var Open bracket */
109 Close, /*! \var Close bracket */
110 Pre, /*! \var Unary prefix operation */
111 Post, /*! \var Unary postfix operation */
112 Binary /*! \var Binary operation */
116 /*! \var postfix representation element */
120 PostfixItemType myType;
124 /*! \var postfix representation */
125 typedef QValueList< PostfixItem > Postfix;
127 /*! \var postfix representation iterator */
128 typedef Postfix::const_iterator PostfixIterator;
131 QString dump( const Postfix& ) const;
132 virtual bool prepare( const QString&, Postfix& );
133 virtual bool setOperationTypes( Postfix& );
134 virtual bool sort( const Postfix&, Postfix&,
135 const QStringList&, const QStringList&,
136 int f=-1, int l=-1 );
138 virtual bool parse( const QString& );
139 virtual void setLastError( const Error );
141 bool calculate( const QString&, QtxValue&, QtxValue& );
143 static int search ( const QStringList&, const QString&, int offset,
144 int& matchLen, int& listind );
145 static QString note ( const QString& str, int pos, int len );
146 static int globalBrackets( const Postfix&, int, int );
149 /*! \var stack of QtxValues */
150 typedef QValueStack < QtxValue > QtxValueStack;
153 QtxOperations* myOperations;
154 QMap< QString, QtxValue > myParameters;