Salome HOME
Base implementation of Notebook
[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 SALOME_String& );
48   bool                       setExpression( const SALOME_String& );
49
50   SALOME_ListOfEvalSet       operationSets() const;
51   SALOME_EvalSet*            operationSet( const SALOME_String& ) const;
52   void                       removeOperationSet( SALOME_EvalSet* );
53   void                       insertOperationSet( SALOME_EvalSet*, const int = -1 );
54
55   bool                       autoDeleteOperationSets() const;
56   void                       setAutoDeleteOperationSets( const bool );
57
58   virtual void               clearParameters();
59   virtual bool               removeParameter( const SALOME_String& name );
60   virtual SALOME_EvalVariant parameter( const SALOME_String& name ) const;
61   virtual bool               hasParameter( const SALOME_String& name ) const;
62   virtual void               setParameter( const SALOME_String& name, const SALOME_EvalVariant& value );
63   SALOME_StringList          parameters() const;
64
65   SALOME_EvalExprError       error() const;
66
67   bool                       firstInvalid( SALOME_String& ) const;
68   void                       removeInvalids();
69   SALOME_String              dump() const;
70
71   static SALOME_String       toString( const SALOME_ListOfEvalVariant& );
72
73 protected:
74   //! Types of postfix representation elements
75   typedef enum 
76   {
77     Value, //!< Value (number, string, etc.)
78     Param, //!< Parameter
79     Open,  //!< Open bracket
80     Close, //!< Close bracket
81     Pre,   //!< Unary prefix operation
82     Post,  //!< Unary postfix operation
83     Binary //!< Binary operation
84   } PostfixItemType;
85
86   //! Postfix representation element
87   typedef struct 
88   {
89     SALOME_EvalVariant myValue;
90     PostfixItemType    myType;
91   } PostfixItem;
92
93   typedef list<PostfixItem>                 Postfix;   //!< postfix representation
94   typedef SALOME_ListOfEvalSet             SetList;   //!< list of operations
95   typedef map <SALOME_String, SALOME_EvalVariant> ParamMap;  //!< parameter-to-value map
96   typedef map <SALOME_String, SALOME_EvalVariant>::value_type PairParamMap;
97
98 protected:
99   SALOME_String dump( const Postfix& ) const;
100
101   virtual bool   prepare( const SALOME_String&, Postfix& );
102   virtual bool   setOperationTypes( Postfix& );
103   virtual bool   sort( const Postfix&, Postfix&, const SALOME_StringList&, const SALOME_StringList&, int f = -1, int l = -1 );
104   virtual bool   parse( const SALOME_String& );
105   virtual void   setError( const SALOME_EvalExprError );
106
107   bool           calculate( const SALOME_String&, SALOME_EvalVariant&, SALOME_EvalVariant& );
108
109   static int           search( const SALOME_StringList&, const SALOME_String&, int offset, int& matchLen, int& listind );
110   static SALOME_String note( const SALOME_String& str, int pos, int len );
111   static int           globalBrackets( const Postfix&, int, int );
112
113 private:
114   void operationList( SALOME_StringList& ) const;
115   void bracketsList( SALOME_StringList&, bool ) const;
116   bool createValue( const SALOME_String&, SALOME_EvalVariant& ) const;
117   int  priority( const SALOME_String&, bool isBin ) const;
118
119   SALOME_EvalExprError isValid( const SALOME_String&, const SALOME_EvalVariantType, const SALOME_EvalVariantType ) const;
120   SALOME_EvalExprError calculation( const SALOME_String&, SALOME_EvalVariant&, SALOME_EvalVariant& ) const;
121
122   bool checkOperations() const;
123   void insert( Postfix& aL, const int aIndex, PostfixItem& aItem );
124   const PostfixItem& at( const Postfix& aL, const int aIndex );
125   void append( Postfix& aL, const Postfix& aL1 );
126
127 private:
128   SALOME_ListOfEvalSet  mySets;
129   SALOME_EvalExprError  myError;
130   ParamMap              myParams;
131   Postfix               myPostfix;
132   bool                  myAutoDel;
133 };
134
135 #endif