1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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
20 #include "SPythonInterpreter.hxx"
21 #include "SPythonParser.hxx"
27 using namespace ParaMEDMEM;
29 const char *SPythonInterpreter::INDENT_TOKEN[]={"def","class","for","if","while","try","except"};
31 const char SPythonInterpreter::NUMBERS[]="0123456789";
33 SPythonInterpreter::SPythonInterpreter(PyObject *globals, PyObject *locals):_indent_must_change(false),_glob(globals),_loc(locals)
35 _indent_pos.insert(0);
38 void SPythonInterpreter::initialize()
41 _indent_pos.insert(0);
42 _indent_must_change=false;
46 bool SPythonInterpreter::run(const char *str, bool& isSPython)
53 _indent_must_change=false;
56 std::size_t pos=s.find_first_not_of(' ');
57 if(pos==std::string::npos)
61 if(!checkIndentCoherency(s,pos))
63 if(!isIndenter(s,pos))
65 _indent_must_change=false;
68 if(isSPythonExpression(s))
77 return finishSession();
86 _indent_must_change=true;
93 bool SPythonInterpreter::finishSession()
98 res=PyRun_String(_cmd.c_str(),Py_file_input,_glob,_loc);
100 checkPythonInterp(res);
101 //_indent_pos.clear();
102 //_indent_pos.insert(0);
106 void SPythonInterpreter::checkPythonInterp(PyObject *r)
112 bool SPythonInterpreter::checkIndentCoherency(const std::string& s, std::size_t p)
114 if(!_indent_must_change)
116 if(_indent_pos.find(p)!=_indent_pos.end())
118 std::set<int>::iterator it=_indent_pos.begin();
120 for(;it!=_indent_pos.end() && !found;it++)
124 _indent_pos.erase(it,_indent_pos.end());
128 {//let python doing the job of error msg !
137 if((int)p>*_indent_pos.rbegin())
139 _indent_pos.insert(p);
143 {//let python doing the job of error msg !
153 * looks that s contains at the begin of non empty char a python keyword that forces indentation of next line.
155 bool SPythonInterpreter::isIndenter(const std::string& s, std::size_t p)
157 std::string s1=s.substr(p);
158 for(int i=0;i<NB_OF_INDENT;i++)
160 std::string elt(INDENT_TOKEN[i]);
161 std::size_t sz=elt.length();
163 if(s1.substr(0,sz)==elt)
169 std::string SPythonInterpreter::strip(const std::string& s)
171 std::size_t sz=s.length();
172 std::size_t n1=std::count(s.c_str(),s.c_str()+sz,' ');
173 std::size_t n2=std::count(s.c_str(),s.c_str()+sz,'\n');
174 std::size_t n3=std::count(s.c_str(),s.c_str()+sz,'\t');
175 std::string ret(sz-n1-n2-n3,'$');
177 for(;i!=std::string::npos;)
179 i=s.find_first_not_of(" \n\t",i);
180 if(i!=std::string::npos)
186 bool SPythonInterpreter::isSPythonExpression(const std::string& s)
189 if(w.find("import ")!=std::string::npos)
191 if(w.find_first_of('@')!=std::string::npos)
193 if(w.find("del ")!=std::string::npos)
195 const char PRINT[]="print ";
196 bool isPrint=w.find(PRINT)!=std::string::npos;
199 std::size_t p=w.find(PRINT);
200 w=w.substr(p+sizeof(PRINT)-1);
203 if(!isSPythonExpressionLev1(w,result))
206 result=std::string(PRINT)+result;
212 bool SPythonInterpreter::isSPythonExpressionLev1(const std::string& s, std::string& result)
214 std::string sst=strip(s);
215 SPythonParserHL p(_glob,_loc);
216 if(!p.parse(sst,result))
221 bool SPythonInterpreter::isCandidateParenthesis(const std::string& s, std::size_t p1, std::size_t& n)
223 std::size_t p2=s.find_first_of(')',p1);
224 std::size_t p3=s.find_first_of('(',p1+1);
225 if(p2!=std::string::npos && (p3==std::string::npos || (p3!=std::string::npos && p2<p3) ))