]> SALOME platform Git repositories - modules/kernel.git/blob - src/Notebook/SALOME_EvalParser.hxx
Salome HOME
Implementation of expression evaluator
[modules/kernel.git] / src / Notebook / SALOME_EvalParser.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOME_EvalParser.hxx
23 //  Author : Peter KURNEV
24 //  Module : SALOME
25
26 #ifndef SALOME_EvalParser_Header_File
27 #define SALOME_EvalParser_Header_File
28 //
29 #ifdef WNT
30 #pragma warning(disable : 4786)
31 #endif
32 //
33 #include <list>
34 #include <map>
35 //
36 #include <SALOME_EvalVariant.hxx>
37 #include <SALOME_EvalSet.hxx>
38 #include <SALOME_Eval.hxx>
39
40 class SALOME_EvalParser
41 {
42 public:
43   SALOME_EvalParser();
44   virtual ~SALOME_EvalParser();
45
46   SALOME_EvalVariant           calculate();
47   SALOME_EvalVariant           calculate( const RString& );
48   bool               setExpression( const RString& );
49
50   SALOME_ListOfPEvalSet  operationSets() const;
51   SALOME_PEvalSet        operationSet( const RString& ) const;
52   void               removeOperationSet(SALOME_PEvalSet );
53   void               insertOperationSet(SALOME_PEvalSet, 
54                                         const int = -1 );
55
56   bool               autoDeleteOperationSets() const;
57   void               setAutoDeleteOperationSets( const bool );
58
59   virtual void       clearParameters();
60   virtual bool       removeParameter( const RString& name );
61   virtual SALOME_EvalVariant   parameter( const RString& name ) const;
62   virtual bool       hasParameter( const RString& name ) const;
63   virtual void       setParameter( const RString& name, 
64                                    const SALOME_EvalVariant& value );
65   RStringList        parameters() const;
66
67   SALOME_EvalExprError error() const;
68
69   bool               firstInvalid( RString& ) const;
70   void               removeInvalids();
71   RString            dump() const;
72
73   static RString     toString( const SALOME_ListOfEvalVariant& );
74
75 protected:
76   //! Types of postfix representation elements
77   typedef enum  {
78     Value, //!< Value (number, string, etc.)
79     Param, //!< Parameter
80     Open,  //!< Open bracket
81     Close, //!< Close bracket
82     Pre,   //!< Unary prefix operation
83     Post,  //!< Unary postfix operation
84     Binary //!< Binary operation
85   } PostfixItemType;
86
87   //! Postfix representation element
88   typedef struct  {
89     SALOME_EvalVariant          myValue;
90     PostfixItemType   myType;
91   } PostfixItem;
92
93   typedef list<PostfixItem>       Postfix;   //!< postfix representation
94   typedef SALOME_ListOfPEvalSet       SetList;   //!< list of operations
95   typedef map <RString, SALOME_EvalVariant> ParamMap;  //!< parameter-to-value map
96   typedef map <RString, SALOME_EvalVariant>::value_type PairParamMap;
97
98 protected:
99   RString            dump( const Postfix& ) const;
100
101   virtual bool       prepare( const RString&, Postfix& );
102
103   virtual bool       setOperationTypes( Postfix& );
104
105   virtual bool       sort(const Postfix&, 
106                           Postfix&, 
107                           const RStringList&,
108                           const RStringList&, 
109                           int f = -1, 
110                           int l = -1 );
111
112   virtual bool       parse( const RString& );
113   virtual void       setError( const SALOME_EvalExprError );
114
115   bool               calculate(const RString&, 
116                                SALOME_EvalVariant&, 
117                                SALOME_EvalVariant& );
118
119   static int         search(const RStringList&, 
120                             const RString&,
121                             int offset, 
122                             int& matchLen, 
123                             int& listind );
124
125   static RString     note(const RString& str, 
126                           int pos, 
127                           int len );
128
129   static int         globalBrackets(const Postfix&, 
130                                     int, 
131                                     int );
132
133 private:
134   void               operationList(RStringList& ) const;
135
136   void               bracketsList(RStringList&, 
137                                   bool ) const;
138
139   bool               createValue(const RString&, 
140                                  SALOME_EvalVariant& ) const;
141
142   int                priority(const RString&, 
143                               bool isBin ) const;
144
145   SALOME_EvalExprError isValid(const RString&,
146                             const SALOME_EvalVariantType, 
147                             const SALOME_EvalVariantType ) const;
148
149   SALOME_EvalExprError calculation(const RString&, 
150                                  SALOME_EvalVariant&, 
151                                  SALOME_EvalVariant& ) const;
152
153   bool               checkOperations() const;
154   ////////////////////////////////////
155   void insert(Postfix& aL, 
156             const int aIndex,
157             PostfixItem& aItem);
158
159   const PostfixItem& at(const Postfix& aL, 
160                         const int aIndex);
161
162   void SALOME_EvalParser::append(Postfix& aL,
163                            const Postfix& aL1);
164 private:
165   SALOME_ListOfPEvalSet mySets;
166   SALOME_EvalExprError myError;
167   ParamMap          myParams;
168   Postfix           myPostfix;
169   bool              myAutoDel;
170 };
171
172 #endif