1
/***************************************************************************
2
cpp_parser.cpp - description
5
author : 2003 Massimo Callegari
6
email : massimocallegari@yahoo.it
7
***************************************************************************/
8
/***************************************************************************
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
***************************************************************************/
16
#include "plugin_katesymbolviewer.h"
18
void KatePluginSymbolViewerView::parseCppSymbols(void)
20
if (!mainWindow()->activeView())
23
QString cl; // Current Line
26
int par = 0, graph = 0/*, retry = 0*/;
27
char mclass = 0, block = 0, comment = 0; // comment: 0-no comment 1-inline comment 2-multiline comment 3-string
28
char macro = 0/*, macro_pos = 0*/, func_close = 0;
29
bool structure = false;
30
QPixmap cls( ( const char** ) class_xpm );
31
QPixmap sct( ( const char** ) struct_xpm );
32
QPixmap mcr( ( const char** ) macro_xpm );
33
QPixmap mtd( ( const char** ) method_xpm );
35
//It is necessary to change names to defaults
36
m_macro->setText(i18n("Show Macros"));
37
m_struct->setText(i18n("Show Structures"));
38
m_func->setText(i18n("Show Functions"));
40
QTreeWidgetItem *node = NULL;
41
QTreeWidgetItem *mcrNode = NULL, *sctNode = NULL, *clsNode = NULL, *mtdNode = NULL;
42
QTreeWidgetItem *lastMcrNode = NULL, *lastSctNode = NULL, *lastClsNode = NULL, *lastMtdNode = NULL;
44
KTextEditor::Document *kv = mainWindow()->activeView()->document();
46
//kDebug(13000)<<"Lines counted :"<<kv->lines();
49
mcrNode = new QTreeWidgetItem(m_symbols, QStringList( i18n("Macros") ) );
50
sctNode = new QTreeWidgetItem(m_symbols, QStringList( i18n("Structures") ) );
51
clsNode = new QTreeWidgetItem(m_symbols, QStringList( i18n("Functions") ) );
52
mcrNode->setIcon(0, QIcon(mcr));
53
sctNode->setIcon(0, QIcon(sct));
54
clsNode->setIcon(0, QIcon(cls));
55
if (m_plugin->expandedOn)
57
m_symbols->expandItem(mcrNode);
58
m_symbols->expandItem(sctNode);
59
m_symbols->expandItem(clsNode);
61
lastMcrNode = mcrNode;
62
lastSctNode = sctNode;
63
lastClsNode = clsNode;
65
lastMtdNode = clsNode;
66
m_symbols->setRootIsDecorated(1);
68
else m_symbols->setRootIsDecorated(0);
70
for (i=0; i<kv->lines(); i++)
72
//kDebug(13000)<<"Current line :"<<i;
76
if ( (cl.length()>=2) && (cl.at(0) == '/' && cl.at(1) == '/')) continue;
77
if(cl.indexOf("/*") == 0 && (cl.indexOf("*/") == ((signed)cl.length() - 2)) && graph == 0) continue; // workaround :(
78
if(cl.indexOf("/*") >= 0 && graph == 0) comment = 1;
79
if(cl.indexOf("*/") >= 0 && graph == 0) comment = 0;
80
if(cl.indexOf('#') >= 0 && graph == 0 ) macro = 1;
83
/* *********************** MACRO PARSING *****************************/
86
//macro_pos = cl.indexOf('#');
87
for (j = 0; j < cl.length(); j++)
89
if ( ((j+1) <cl.length()) && (cl.at(j)=='/' && cl.at(j+1)=='/')) { macro = 4; break; }
90
if( cl.indexOf("define") == j &&
91
!(cl.indexOf("defined") == j))
94
j += 6; // skip the word "define"
96
if(macro == 2 && j<cl.length() &&cl.at(j) != ' ') macro = 3;
99
if (cl.at(j) >= 0x20) stripped += cl.at(j);
100
if (cl.at(j) == ' ' || j == cl.length() - 1)
103
//kDebug(13000)<<"Macro -- Stripped : "<<stripped<<" macro = "<<macro;
105
// I didn't find a valid macro e.g. include
106
if(j == cl.length() && macro == 1) macro = 0;
109
//stripped.replace(0x9, " ");
110
stripped = stripped.trimmed();
111
if (macro_on == true)
113
if (m_plugin->treeOn)
115
node = new QTreeWidgetItem(mcrNode, lastMcrNode);
118
else node = new QTreeWidgetItem(m_symbols);
119
node->setText(0, stripped);
120
node->setIcon(0, QIcon(mcr));
121
node->setText(1, QString::number( i, 10));
126
//kDebug(13000)<<"Macro -- Inserted : "<<stripped<<" at row : "<<i;
127
if (cl.at(cl.length() - 1) == '\\') macro = 5; // continue in rows below
133
if (cl.length() == 0 || cl.at(cl.length() - 1) != '\\')
138
/* ******************************************************************** */
140
if ((cl.indexOf("class") >= 0 && graph == 0 && block == 0))
143
for (j = 0; j < cl.length(); j++)
145
if(((j+1) < cl.length()) && (cl.at(j)=='/' && cl.at(j+1)=='/')) { mclass = 2; break; }
146
if(cl.at(j)=='{') { mclass = 4; break;}
147
stripped += cl.at(j);
151
if (m_plugin->treeOn)
153
node = new QTreeWidgetItem(clsNode, lastClsNode);
154
if (m_plugin->expandedOn) m_symbols->expandItem(node);
156
mtdNode = lastClsNode;
157
lastMtdNode = lastClsNode;
159
else node = new QTreeWidgetItem(m_symbols);
160
node->setText(0, stripped);
161
node->setIcon(0, QIcon(cls));
162
node->setText(1, QString::number( i, 10));
164
if (mclass == 1) mclass = 3;
170
if (cl.indexOf('{') >= 0)
172
cl = cl.mid(cl.indexOf('{'));
177
if(cl.indexOf('(') >= 0 && cl.at(0) != '#' && block == 0 && comment != 2)
178
{ structure = false; block = 1; }
179
if((cl.indexOf("typedef") >= 0 || cl.indexOf("struct") >= 0) &&
180
graph == 0 && block == 0)
181
{ structure = true; block = 2; stripped = ""; }
182
//if(cl.indexOf(';') >= 0 && graph == 0)
184
if(block > 0 && mclass != 1 )
186
for (j = 0; j < cl.length(); j++)
188
if ( ((j+1) < cl.length()) && (cl.at(j) == '/' && (cl.at(j + 1) == '*') && comment != 3)) comment = 2;
189
if ( ((j+1) < cl.length()) && (cl.at(j) == '*' && (cl.at(j + 1) == '/') && comment != 3) )
190
{ comment = 0; j+=2; if (j>=cl.length()) break;}
191
// Handles a string. Those are freaking evilish !
192
if (cl.at(j) == '"' && comment == 3) { comment = 0; j++; if (j>=cl.length()) break;}
193
else if (cl.at(j) == '"' && comment == 0) comment = 3;
194
if ( ((j+1) <cl.length()) &&(cl.at(j)=='/' && cl.at(j+1)=='/') && comment == 0 )
195
{ if(block == 1 && stripped.isEmpty()) block = 0; break; }
196
if (comment != 2 && comment != 3)
198
if (block == 1 && graph == 0 )
200
if(cl.at(j) >= 0x20) stripped += cl.at(j);
201
if(cl.at(j) == '(') par++;
207
stripped = stripped.trimmed();
208
stripped.remove("static ");
209
//kDebug(13000)<<"Function -- Inserted : "<<stripped<<" at row : "<<i;
215
if(block == 2 && graph == 0)
217
if ( ((j+1)<cl.length()) && (cl.at(j)=='/' && cl.at(j+1)=='/') && comment == 0) break;
218
//if(cl.at(j)==':' || cl.at(j)==',') { block = 1; continue; }
219
if(cl.at(j)==':') { block = 1; continue; }
228
if((cl.at(j)=='{' && structure == false && cl.indexOf(';') < 0) ||
229
(cl.at(j)=='{' && structure == false && cl.indexOf('}') > (int)j))
231
stripped.replace(0x9, " ");
234
if (m_plugin->typesOn == false)
236
while (stripped.indexOf('(') >= 0)
237
stripped = stripped.left(stripped.indexOf('('));
238
while (stripped.indexOf("::") >= 0)
239
stripped = stripped.mid(stripped.indexOf("::") + 2);
240
stripped = stripped.trimmed();
241
while (stripped.indexOf(0x20) >= 0)
242
stripped = stripped.mid(stripped.indexOf(0x20, 0) + 1);
244
(stripped.length()>0) &&
246
(stripped.at(0)=='*') ||
247
(stripped.at(0)=='&')
249
) stripped=stripped.right(stripped.length()-1);
251
if (m_plugin->treeOn)
255
node = new QTreeWidgetItem(mtdNode, lastMtdNode);
260
node = new QTreeWidgetItem(clsNode, lastClsNode);
265
node = new QTreeWidgetItem(m_symbols);
266
node->setText(0, stripped);
267
if (mclass == 4) node->setIcon(0, QIcon(mtd));
268
else node->setIcon(0, QIcon(cls));
269
node->setText(1, QString::number( tmpPos, 10));
275
if(cl.at(j)=='{' && structure == true)
280
if(cl.at(j)=='(' && structure == true)
285
//kDebug(13000)<<"Restart from the beginning of line...";
287
break; // Avoid an infinite loop :(
289
if(structure == true && cl.at(j) >= 0x20) stripped += cl.at(j);
294
// A comment...there can be anything
295
if( ((j+1)<cl.length()) && (cl.at(j)=='/' && cl.at(j+1)=='/') && comment == 0 ) break;
296
if(cl.at(j)=='{') graph++;
300
if (graph == 0 && structure == false) { block = 0; func_close = 1; }
301
if (graph == 0 && structure == true) block = 4;
309
//stripped.replace(0x9, " ");
310
stripped.remove('{');
311
stripped.replace('}', " ");
312
if(struct_on == true)
314
if (m_plugin->treeOn)
316
node = new QTreeWidgetItem(sctNode, lastSctNode);
319
else node = new QTreeWidgetItem(m_symbols);
320
node->setText(0, stripped);
321
node->setIcon(0, QIcon(sct));
322
node->setText(1, QString::number( tmpPos, 10));
324
//kDebug(13000)<<"Structure -- Inserted : "<<stripped<<" at row : "<<i;
331
if (cl.at(j) >= 0x20) stripped += cl.at(j);
334
//kDebug(13000)<<"Stripped : "<<stripped<<" at row : "<<i;
335
} // End of For cycle
337
if (mclass == 4 && block == 0 && func_close == 0)
339
if (cl.indexOf('}') >= 0)
341
cl = cl.mid(cl.indexOf('}'));
346
} // for kv->numlines
348
//for (i= 0; i < (m_symbols->itemIndex(node) + 1); i++)
349
// kDebug(13000)<<"Symbol row :"<<positions.at(i);