Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDCalculator / Swig / SPythonParser.hxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF 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/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __SPYTHONPARSER_HXX__
21 #define __SPYTHONPARSER_HXX__
22
23 // rnv: avoid compilation warning on Linux : "_POSIX_C_SOURCE" and "_XOPEN_SOURCE" are redefined
24 #ifdef _POSIX_C_SOURCE
25 #undef _POSIX_C_SOURCE
26 #endif 
27
28 #ifdef _XOPEN_SOURCE
29 #undef _XOPEN_SOURCE
30 #endif 
31
32 #include "Python.h"
33
34 #include "MedCalculatorSPythonDefines.hxx"
35
36 #include <string>
37 #include <vector>
38
39 namespace ParaMEDMEM
40 {
41   typedef enum
42     {
43       EMPTY_TYPE=0,
44       FLOAT_TYPE=2,
45       INT_TYPE=3,
46       FIELDDB_TYPE=4,
47       FUNC_TYPE=5,
48       UNKNOWN_TYPE=6,
49       IDENTITY_TYPE=7
50     } TypeOfEntity;
51
52   class SPythonParser;
53
54   class MEDCALCULATORSPYTHON_EXPORT SPythonPredParser
55   {
56   public:
57     SPythonPredParser();
58     TypeOfEntity getType() const { return _type; }
59     void assign(const std::string& s, PyObject *glob, PyObject *loc);
60     std::string getRepr() const;
61     bool empty() const;
62     static TypeOfEntity getTypeOfVar(const std::string& var, PyObject *glob, PyObject *loc);
63   public:
64     static const char FIELD_TYPE_STR[];
65   private:
66     std::string _var;
67     std::string _method;
68     TypeOfEntity _type;
69   };
70
71   /*!
72    * This class works on strings previously stripped.
73    */
74   class MEDCALCULATORSPYTHON_EXPORT SPythonParser
75   {
76   public:
77     SPythonParser();
78     TypeOfEntity getType() const { return _type; }
79     bool parseWithoutEqual(const std::string& s, int parLev, PyObject *glob, PyObject *loc, std::vector<SPythonParser>& alreadyParsed);
80     TypeOfEntity getTypeOfElt(const std::string& elt, PyObject *glob, PyObject *loc, const std::vector<SPythonParser>& alreadyParsed) const;
81     TypeOfEntity combineType(TypeOfEntity t1, TypeOfEntity t2) const;
82     std::string getRepr(const std::vector<SPythonParser>& v) const;
83     std::string replaceByCompacted(const std::string& s, int parLev, int id) const;
84     std::string substitute(const std::vector<SPythonParser>& v) const;
85     TypeOfEntity analyzeType(PyObject *glob, PyObject *loc, const std::vector<SPythonParser>& alreadyParsed);
86     void keepSelectedLevOfPar(const std::string& s, int parLev, PyObject *glob, PyObject *loc);
87     static std::vector<int> levOfParenthesis(const std::string& s);
88     //
89     static void replaceFromCompacted(std::string& ret,const std::vector<SPythonParser>& v);
90     static bool isAlreadyControledParenthesis(const std::string& s);
91     static bool isParenthesisMatching(const std::string& s, std::string& res);
92     static bool isElementInParenthesisMatching(const std::string& s, std::string& result, bool& isNumber);
93   public:
94     static const char NUMBERS[];
95   private:
96     TypeOfEntity _type;
97     SPythonPredParser _pred;
98     std::string _content;
99     std::string _content_py;
100   };
101
102   /*!
103    * The main aim of this class is to deal with operator = between spython parts and aggregate it.
104    * HL means HL because parse method dispatch the core operation on SPythonParse class.
105    */
106   class MEDCALCULATORSPYTHON_EXPORT SPythonParserHL
107   {
108   public:
109     SPythonParserHL(PyObject *glob, PyObject *loc);
110     bool parse(const std::string& s, std::string& result);
111     static std::vector<std::string> splitBetweenEqualChar(const std::string &s);
112   private:
113     bool parseWithoutEqual(const std::string& s, TypeOfEntity& type, std::string& result);
114   private:
115     PyObject *_glob;
116     PyObject *_loc;
117   };
118 }
119
120 #endif