Salome HOME
Initial version
[modules/gui.git] / src / Qtx / QtxParser.h
1 // File:      QtxParser.h
2 // Author:    Alexander SOLOVYOV
3
4 #ifndef __QTX_PARSER_HEADER__
5 #define __QTX_PARSER_HEADER__
6
7 #include "Qtx.h"
8 #include <qvaluestack.h>
9 #include <qvariant.h>
10
11 #ifdef WIN32
12 #pragma warning( disable:4251 )
13 #endif
14
15
16 class QtxOperations;
17
18 typedef QVariant QtxValue;
19
20
21 //================================================================
22 // Class    : 
23 // Purpose  : 
24 //================================================================
25 class QTX_EXPORT QtxParser
26 {
27 public:
28     typedef enum
29     {
30         OK, OperandsNotMatch, InvalidResult, InvalidOperation,
31         OperationsNull, InvalidToken, CloseExpected, ExcessClose,
32         BracketsNotMatch, StackUnderflow, ExcessData
33
34     } Error;
35
36 public:
37     QtxParser( QtxOperations*, const QString& = QString::null );
38     virtual ~QtxParser();
39
40     QtxValue   calculate();
41     QtxValue   calculate( const QString& );
42     bool       setExpr( const QString& );
43
44     virtual void     clear();
45     virtual bool     has   ( const QString& name ) const;
46     virtual void     set   ( const QString& name, const QtxValue& value );
47     virtual bool     remove( const QString& name );
48     virtual QtxValue value ( const QString& name ) const;
49
50     bool       firstInvalid( QString& ) const;
51     void       removeInvalids();
52     QString    dump() const;
53     Error      lastError() const;
54     void       paramsList( QStringList& );
55
56     static QString toString( const QValueList< QtxValue >& );
57
58 protected:
59     typedef enum { Value, Param, Open, Close, Pre, Post, Binary } PostfixItemType;
60
61     typedef struct
62     {
63         QtxValue          myValue;
64         PostfixItemType   myType;
65
66     } PostfixItem;
67
68     typedef QValueList< PostfixItem > Postfix;
69     typedef Postfix::const_iterator PostfixIterator;
70
71 protected:
72             QString  dump( const Postfix& ) const;
73     virtual bool     prepare( const QString&, Postfix& );
74     virtual bool     setOperationTypes( Postfix& );
75     virtual bool     sort( const Postfix&, Postfix&, 
76                            const QStringList&, const QStringList&, 
77                            int f=-1, int l=-1 );
78
79     virtual bool     parse( const QString& );
80     virtual void     setLastError( const Error );
81
82             bool     calculate( const QString&, QtxValue&, QtxValue& );
83
84     static int       search        ( const QStringList&, const QString&, int offset,
85                                      int& matchLen, int& listind );
86     static QString   note          ( const QString& str, int pos, int len );
87     static int       globalBrackets( const Postfix&, int, int );
88
89 private:
90     typedef QValueStack < QtxValue >  QtxValueStack;
91
92 private:
93     QtxOperations*              myOperations;
94     QMap< QString, QtxValue >   myParameters;
95     Error                       myLastError;
96     Postfix                     myPost;
97 };
98
99 #endif