2 ////////////////////////////////////////////////////////////////////////////////////////
4 // This file is part of Boost Statechart Viewer.
6 // Boost Statechart Viewer is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // Boost Statechart Viewer is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Boost Statechart Viewer. If not, see <http://www.gnu.org/licenses/>.
19 ////////////////////////////////////////////////////////////////////////////////////////
24 using namespace clang;
26 string clean_spaces(const string line) /** Remove gaps from string.*/
30 for(i = 0;i<line.length();i++)
32 if(!isspace(line[i])) new_line+=line[i];
38 string cut_commentary(const string line) /** This function cuts commentaries at the end of line. */
41 for(i = 0;i<line.length()-1;i++)
43 if(line[i]=='/' && line[i+1]=='/') return line.substr(0,i);
44 if(line[i]=='/' && line[i+1]=='*') return line.substr(0,i);
49 string get_line_of_code(const string code) /** Thie function return the line of code that belongs to a declaration. The output string line doesn't have gaps and commentaries.*/
53 for(i = 0;i<code.length();i++)
55 if(code[i]=='\n'||code[i]=='{') break;
57 ret = code.substr(0,i);
58 ret = clean_spaces(ret);
59 return cut_commentary(ret);
62 string get_return(const string code) /** Return return statement. */
66 for(i = 0;i<code.length();i++)
68 if(code[i]==' ') break;
70 return code.substr(0,i);
73 string cut_namespaces(string line) /** Cut namespaces from the declarations. */
77 for(i = line.length()-1;i>0;i--)
79 if(line[i]=='<') brackets+=1;
80 if(line[i]=='>') brackets-=1;
81 if(line[i]==':' && line[i-1]==':')
83 if(brackets == 0) break;
88 return line.substr(i+1);
91 string cut_namespaces(string line, int ortho) /** Cut namespaces ORTHOGONAL STATES. */
95 for(i = line.length()-1;i>0;i--)
97 if(line[i]=='<') brackets+=1;
98 if(line[i]=='>') brackets-=1;
99 if(line[i]==':' && line[i-1]==':')
101 if(brackets == 0) break;
105 if(i==0) return line;
106 return line.substr(0,i-1);
109 bool is_derived(const string line) /** Test whether this struct or class is derived */
112 for(i = 0;i<line.length()-2;i++)
116 if(line[i+1]!=':') return true;
123 string get_super_class(const string line) /** Get the super classes of this declarations. */
126 for(i = 0;i<line.length()-1;i++)
130 if(line[i+1]!=':') break;
134 return line.substr(i+1);
137 string get_next_base(const string line) /** Get next super classes of this declarations Do the first super class is ommitted. */
141 for(i = 0;i<line.length()-1;i++)
143 if(line[i]=='<') brackets+=1;
144 if(line[i]=='>') brackets-=1;
145 if(line[i]==',' && brackets == 0) break;
147 return line.substr(i+1);
150 string get_first_base(const string line) /** Get the first super class of this declarations. */
154 for(i = 0;i<line.length()-1;i++)
156 if(line[i]=='<') brackets+=1;
157 if(line[i]=='>') brackets-=1;
158 if(line[i]==',' && brackets == 0) break;
160 return line.substr(0,i);
163 bool is_state(const string line) /** Test if this declaration is a state. It is used to test the base classes. */
165 cout<<cut_namespaces(line)<<endl;
166 if(cut_namespaces(line).compare(0,12,"simple_state")==0)
172 if(cut_namespaces(line).compare(0,5,"state")==0)
180 string cut_type(string line) /** This function cuts type of the declaration from the string. */
183 for(i = 0;i<line.length();i++)
185 if(line[i]==' ') break;
187 return line.substr(i);
190 int count(const string line, const char c) /** Count all specified char in string. So it returns the number of specified characters. */
193 for(unsigned i = 0;i<line.length();i++)
195 if(line[i]==c) number+=1;
200 bool is_list(const string line) /** Test if this decl is mpl::list. */
204 for(unsigned i = 0; i<line.length();i++)
206 if(line[i]=='<') break;
207 if(line[i]==':' && line[i+1]==':') pos = i+2;
209 if(line.substr(pos).compare(0,4,"list")==0)
219 string get_inner_part(const string line) /** Get inner part of the list. */
223 for(i = 0;i<line.length();i++)
225 if(line[i]=='<') break;
227 str = line.substr(i+1);
228 for(i = 0;i<str.length();i++)
230 if(str[i]=='<') pos+=1;
237 return str.substr(0,i);
240 bool test_model(const string line, const string model) /** Test the string to has a specified model. */
242 if(cut_namespaces(line).compare(0,model.length(),model)==0)
252 string get_params(string line) /** Return parameters of the specified transition */
254 int pos_front = line.find("<")+1;
255 int pos_end = line.rfind(">");
256 return line.substr(pos_front,pos_end-pos_front);
259 string find_states(const CXXRecordDecl *cRecDecl, string line) /** test if the struct/class is he state (must be derived from simple_state or state). */
261 string super_class = get_super_class(line), base, params;
262 if(cRecDecl->getNumBases()>1)
265 for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
267 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
268 else base = super_class;
269 if(is_state(super_class))
271 params = get_params(super_class);
275 super_class = get_next_base(super_class);
281 if(is_state(super_class))
283 params = get_params(super_class);
290 string find_name_of_machine(const CXXRecordDecl *cRecDecl, string line) /** Find name of the state machine and the start state. */
292 string super_class = get_super_class(line), base, params;
293 if(cRecDecl->getNumBases()>1)
295 for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
297 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
298 else base = super_class;
299 if(test_model(base,"state_machine"))
301 params = get_params(base);
305 super_class = get_next_base(super_class);
311 if(test_model(super_class,"state_machine"))
313 params = get_params(super_class);
319 string find_transitions (const string name_of_state, string line) /** Traverse all methods for finding declarations of transitions. */
321 string dest, params, base, trans;
322 int num = count(line,'<');
328 line = get_inner_part(line);
331 for(int j = 0;j<num;j++)
333 if(j!=num-1) base = get_first_base(line);
335 if(test_model(base,"transition"))
337 dest = name_of_state;
338 params = get_params(base);
339 //cout<<params<<"\n";
342 line = get_next_base(line);
344 if(j+1!=num) trans.append(";");
346 else if(test_model(base,"custom_reaction"))
349 dest.append(name_of_state);
350 params = get_params(base);
351 //cout<<params<<"\n";
354 line = get_next_base(line);
356 if(j+1!=num) trans.append(";");
360 line = get_next_base(line);
363 if(trans[trans.length()-1]==';') return trans.substr(0,trans.length()-1);
367 bool find_events(const CXXRecordDecl *cRecDecl, string line) /** This function provides testing if the decl is decl of event*/
369 string super_class = get_super_class(line), base, params;
370 if(cRecDecl->getNumBases()>1)
372 for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
374 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
375 else base = super_class;
376 if(test_model(base,"event")) return true;
379 super_class = get_next_base(super_class);
385 if(test_model(super_class,"event"))return true;