Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / Qtx / QtxParser.h
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 // File:      QtxParser.h
20 // Author:    Alexander SOLOVYOV
21
22 #ifndef __QTX_PARSER_HEADER__
23 #define __QTX_PARSER_HEADER__
24
25 #include "Qtx.h"
26 #include <qvaluestack.h>
27 #include <qvariant.h>
28
29 #ifdef WIN32
30 #pragma warning( disable:4251 )
31 #endif
32
33
34 class QtxOperations;
35
36 /*! \var QtxValue
37     \brief Alias for QVariant
38 */
39 typedef QVariant QtxValue;
40
41
42 /*!
43   \class QtxParser
44
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
52
53 */
54 class QTX_EXPORT QtxParser
55 {
56 public:
57     /*!
58       \enum Error
59       \brief Errors during parsing
60     */
61     typedef enum
62     {
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 */
74
75     } Error;
76
77 public:
78     QtxParser( QtxOperations*, const QString& = QString::null );
79     virtual ~QtxParser();
80
81     QtxValue   calculate();
82     QtxValue   calculate( const QString& );
83     bool       setExpr( const QString& );
84
85     virtual void     clear();
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;
90
91     bool       firstInvalid( QString& ) const;
92     void       removeInvalids();
93     QString    dump() const;
94     Error      lastError() const;
95     void       paramsList( QStringList& );
96
97     static QString toString( const QValueList< QtxValue >& );
98
99 protected:
100     /*!
101       \enum PostfixItemType
102       \brief Types of postfix representation elements
103     */  
104     typedef enum
105     {
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 */
113
114     } PostfixItemType;
115
116     /*! \var postfix representation element */
117     typedef struct
118     {
119         QtxValue          myValue;
120         PostfixItemType   myType;
121
122     } PostfixItem;
123
124     /*! \var postfix representation */
125     typedef QValueList< PostfixItem > Postfix;
126
127     /*! \var postfix representation iterator */
128     typedef Postfix::const_iterator PostfixIterator;
129
130 protected:
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 );
137
138     virtual bool     parse( const QString& );
139     virtual void     setLastError( const Error );
140
141             bool     calculate( const QString&, QtxValue&, QtxValue& );
142
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 );
147
148 private:
149     /*! \var stack of QtxValues */
150     typedef QValueStack < QtxValue >  QtxValueStack;
151
152 private:
153     QtxOperations*              myOperations;
154     QMap< QString, QtxValue >   myParameters;
155     Error                       myLastError;
156     Postfix                     myPost;
157 };
158
159 #endif