1 // Copyright (C) 2007-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "SPythonInterpreter.hxx"
22 #include "SPythonParser.hxx"
28 using namespace MEDCoupling;
30 const char *SPythonInterpreter::INDENT_TOKEN[]={"def","class","for","if","while","try","except"};
32 const char SPythonInterpreter::NUMBERS[]="0123456789";
34 SPythonInterpreter::SPythonInterpreter(PyObject *globals, PyObject *locals):_indent_must_change(false),_glob(globals),_loc(locals)
36 _indent_pos.insert(0);
39 void SPythonInterpreter::initialize()
42 _indent_pos.insert(0);
43 _indent_must_change=false;
47 bool SPythonInterpreter::run(const char *str, bool& isSPython)
54 _indent_must_change=false;
57 std::size_t pos=s.find_first_not_of(' ');
58 if(pos==std::string::npos)
62 if(!checkIndentCoherency(s,pos))
64 if(!isIndenter(s,pos))
66 _indent_must_change=false;
69 if(isSPythonExpression(s))
78 return finishSession();
87 _indent_must_change=true;
94 bool SPythonInterpreter::finishSession()
99 res=PyRun_String(_cmd.c_str(),Py_file_input,_glob,_loc);
101 checkPythonInterp(res);
102 //_indent_pos.clear();
103 //_indent_pos.insert(0);
107 void SPythonInterpreter::checkPythonInterp(PyObject *r)
113 bool SPythonInterpreter::checkIndentCoherency(const std::string& s, std::size_t p)
115 if(!_indent_must_change)
117 if(_indent_pos.find(p)!=_indent_pos.end())
119 std::set<int>::iterator it=_indent_pos.begin();
121 for(;it!=_indent_pos.end() && !found;it++)
125 _indent_pos.erase(it,_indent_pos.end());
129 {//let python doing the job of error msg !
138 if((int)p>*_indent_pos.rbegin())
140 _indent_pos.insert(p);
144 {//let python doing the job of error msg !
154 * looks that s contains at the begin of non empty char a python keyword that forces indentation of next line.
156 bool SPythonInterpreter::isIndenter(const std::string& s, std::size_t p)
158 std::string s1=s.substr(p);
159 for(int i=0;i<NB_OF_INDENT;i++)
161 std::string elt(INDENT_TOKEN[i]);
162 std::size_t sz=elt.length();
164 if(s1.substr(0,sz)==elt)
170 std::string SPythonInterpreter::strip(const std::string& s)
172 std::size_t sz=s.length();
173 std::size_t n1=std::count(s.c_str(),s.c_str()+sz,' ');
174 std::size_t n2=std::count(s.c_str(),s.c_str()+sz,'\n');
175 std::size_t n3=std::count(s.c_str(),s.c_str()+sz,'\t');
176 std::string ret(sz-n1-n2-n3,'$');
178 for(;i!=std::string::npos;)
180 i=s.find_first_not_of(" \n\t",i);
181 if(i!=std::string::npos)
187 bool SPythonInterpreter::isSPythonExpression(const std::string& s)
190 if(w.find("import ")!=std::string::npos)
192 if(w.find_first_of('@')!=std::string::npos)
194 if(w.find("del ")!=std::string::npos)
196 const char PRINT[]="print(";
197 const char ENDPRINT[]=")";
198 bool isPrint=w.find(PRINT)!=std::string::npos;
199 isPrint &= w.find(ENDPRINT)!=std::string::npos;
202 std::size_t p=w.find(PRINT);
203 w=w.substr(p+sizeof(PRINT)-sizeof(ENDPRINT)-1);
206 if(!isSPythonExpressionLev1(w,result))
209 result=std::string(PRINT)+result;
215 bool SPythonInterpreter::isSPythonExpressionLev1(const std::string& s, std::string& result)
217 std::string sst=strip(s);
218 SPythonParserHL p(_glob,_loc);
219 if(!p.parse(sst,result))
224 bool SPythonInterpreter::isCandidateParenthesis(const std::string& s, std::size_t p1, std::size_t& n)
226 std::size_t p2=s.find_first_of(')',p1);
227 std::size_t p3=s.find_first_of('(',p1+1);
228 if(p2!=std::string::npos && (p3==std::string::npos || (p3!=std::string::npos && p2<p3) ))