Back to index

salome-med  6.5.0
SPythonParser.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #ifndef __SPYTHONPARSER_HXX__
00021 #define __SPYTHONPARSER_HXX__
00022 
00023 // rnv: avoid compilation warning on Linux : "_POSIX_C_SOURCE" and "_XOPEN_SOURCE" are redefined
00024 #ifdef _POSIX_C_SOURCE
00025 #undef _POSIX_C_SOURCE
00026 #endif 
00027 
00028 #ifdef _XOPEN_SOURCE
00029 #undef _XOPEN_SOURCE
00030 #endif 
00031 
00032 #include "Python.h"
00033 
00034 #include "MedCalculatorSPythonDefines.hxx"
00035 
00036 #include <string>
00037 #include <vector>
00038 
00039 namespace ParaMEDMEM
00040 {
00041   typedef enum
00042     {
00043       EMPTY_TYPE=0,
00044       FLOAT_TYPE=2,
00045       INT_TYPE=3,
00046       FIELDDB_TYPE=4,
00047       FUNC_TYPE=5,
00048       UNKNOWN_TYPE=6,
00049       IDENTITY_TYPE=7
00050     } TypeOfEntity;
00051 
00052   class SPythonParser;
00053 
00054   class MEDCALCULATORSPYTHON_EXPORT SPythonPredParser
00055   {
00056   public:
00057     SPythonPredParser();
00058     TypeOfEntity getType() const { return _type; }
00059     void assign(const std::string& s, PyObject *glob, PyObject *loc);
00060     std::string getRepr() const;
00061     bool empty() const;
00062     static TypeOfEntity getTypeOfVar(const std::string& var, PyObject *glob, PyObject *loc);
00063   public:
00064     static const char FIELD_TYPE_STR[];
00065   private:
00066     std::string _var;
00067     std::string _method;
00068     TypeOfEntity _type;
00069   };
00070 
00074   class MEDCALCULATORSPYTHON_EXPORT SPythonParser
00075   {
00076   public:
00077     SPythonParser();
00078     TypeOfEntity getType() const { return _type; }
00079     bool parseWithoutEqual(const std::string& s, int parLev, PyObject *glob, PyObject *loc, std::vector<SPythonParser>& alreadyParsed);
00080     TypeOfEntity getTypeOfElt(const std::string& elt, PyObject *glob, PyObject *loc, const std::vector<SPythonParser>& alreadyParsed) const;
00081     TypeOfEntity combineType(TypeOfEntity t1, TypeOfEntity t2) const;
00082     std::string getRepr(const std::vector<SPythonParser>& v) const;
00083     std::string replaceByCompacted(const std::string& s, int parLev, int id) const;
00084     std::string substitute(const std::vector<SPythonParser>& v) const;
00085     TypeOfEntity analyzeType(PyObject *glob, PyObject *loc, const std::vector<SPythonParser>& alreadyParsed);
00086     void keepSelectedLevOfPar(const std::string& s, int parLev, PyObject *glob, PyObject *loc);
00087     static std::vector<int> levOfParenthesis(const std::string& s);
00088     //
00089     static void replaceFromCompacted(std::string& ret,const std::vector<SPythonParser>& v);
00090     static bool isAlreadyControledParenthesis(const std::string& s);
00091     static bool isParenthesisMatching(const std::string& s, std::string& res);
00092     static bool isElementInParenthesisMatching(const std::string& s, std::string& result, bool& isNumber);
00093   public:
00094     static const char NUMBERS[];
00095   private:
00096     TypeOfEntity _type;
00097     SPythonPredParser _pred;
00098     std::string _content;
00099     std::string _content_py;
00100   };
00101 
00106   class MEDCALCULATORSPYTHON_EXPORT SPythonParserHL
00107   {
00108   public:
00109     SPythonParserHL(PyObject *glob, PyObject *loc);
00110     bool parse(const std::string& s, std::string& result);
00111     static std::vector<std::string> splitBetweenEqualChar(const std::string &s);
00112   private:
00113     bool parseWithoutEqual(const std::string& s, TypeOfEntity& type, std::string& result);
00114   private:
00115     PyObject *_glob;
00116     PyObject *_loc;
00117   };
00118 }
00119 
00120 #endif