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