~ubuntu-branches/ubuntu/vivid/kate/vivid-proposed

« back to all changes in this revision

Viewing changes to addons/kate/symbolviewer/cpp_parser.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                          cpp_parser.cpp  -  description
3
 
                             -------------------
4
 
    begin                : Apr 2 2003
5
 
    author               : 2003 Massimo Callegari
6
 
    email                : massimocallegari@yahoo.it
7
 
 ***************************************************************************/
8
 
 /***************************************************************************
9
 
 *                                                                         *
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.                                   *
14
 
 *                                                                         *
15
 
 ***************************************************************************/
16
 
#include "plugin_katesymbolviewer.h"
17
 
 
18
 
void KatePluginSymbolViewerView::parseCppSymbols(void)
19
 
{
20
 
  if (!mainWindow()->activeView())
21
 
   return;
22
 
 
23
 
 QString cl; // Current Line
24
 
 QString stripped;
25
 
 int i, j, tmpPos = 0;
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 );
34
 
 
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"));
39
 
 
40
 
 QTreeWidgetItem *node = NULL;
41
 
 QTreeWidgetItem *mcrNode = NULL, *sctNode = NULL, *clsNode = NULL, *mtdNode = NULL;
42
 
 QTreeWidgetItem *lastMcrNode = NULL, *lastSctNode = NULL, *lastClsNode = NULL, *lastMtdNode = NULL;
43
 
 
44
 
 KTextEditor::Document *kv = mainWindow()->activeView()->document();
45
 
 
46
 
 //kDebug(13000)<<"Lines counted :"<<kv->lines();
47
 
 if(m_plugin->treeOn)
48
 
   {
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)
56
 
      {
57
 
       m_symbols->expandItem(mcrNode);
58
 
       m_symbols->expandItem(sctNode);
59
 
       m_symbols->expandItem(clsNode);
60
 
      }
61
 
    lastMcrNode = mcrNode;
62
 
    lastSctNode = sctNode;
63
 
    lastClsNode = clsNode;
64
 
    mtdNode = clsNode;
65
 
    lastMtdNode = clsNode;
66
 
    m_symbols->setRootIsDecorated(1);
67
 
   }
68
 
 else m_symbols->setRootIsDecorated(0);
69
 
 
70
 
 for (i=0; i<kv->lines(); i++)
71
 
   {
72
 
    //kDebug(13000)<<"Current line :"<<i;
73
 
    cl = kv->line(i);
74
 
    cl = cl.trimmed();
75
 
    func_close = 0;
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;
81
 
    if (comment != 1)
82
 
      {
83
 
       /* *********************** MACRO PARSING *****************************/
84
 
       if(macro == 1)
85
 
         {
86
 
          //macro_pos = cl.indexOf('#');
87
 
          for (j = 0; j < cl.length(); j++)
88
 
             {
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))
92
 
                    {
93
 
                     macro = 2;
94
 
                     j += 6; // skip the word "define"
95
 
                    }
96
 
              if(macro == 2 && j<cl.length() &&cl.at(j) != ' ') macro = 3;
97
 
              if(macro == 3)
98
 
                {
99
 
                 if (cl.at(j) >= 0x20) stripped += cl.at(j);
100
 
                 if (cl.at(j) == ' ' || j == cl.length() - 1)
101
 
                         macro = 4;
102
 
                }
103
 
              //kDebug(13000)<<"Macro -- Stripped : "<<stripped<<" macro = "<<macro;
104
 
             }
105
 
           // I didn't find a valid macro e.g. include
106
 
           if(j == cl.length() && macro == 1) macro = 0;
107
 
           if(macro == 4)
108
 
             {
109
 
              //stripped.replace(0x9, " ");
110
 
              stripped = stripped.trimmed();
111
 
              if (macro_on == true)
112
 
                 {
113
 
                  if (m_plugin->treeOn)
114
 
                    {
115
 
                     node = new QTreeWidgetItem(mcrNode, lastMcrNode);
116
 
                     lastMcrNode = node;
117
 
                    }
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));
122
 
                 }
123
 
              macro = 0;
124
 
              //macro_pos = 0;
125
 
              stripped = "";
126
 
              //kDebug(13000)<<"Macro -- Inserted : "<<stripped<<" at row : "<<i;
127
 
              if (cl.at(cl.length() - 1) == '\\') macro = 5; // continue in rows below
128
 
              continue;
129
 
             }
130
 
          }
131
 
       if (macro == 5)
132
 
          {
133
 
           if (cl.length() == 0 || cl.at(cl.length() - 1) != '\\')
134
 
               macro = 0;
135
 
           continue;
136
 
          }
137
 
 
138
 
       /* ******************************************************************** */
139
 
 
140
 
       if ((cl.indexOf("class") >= 0 && graph == 0 && block == 0))
141
 
         {
142
 
          mclass = 1;
143
 
          for (j = 0; j < cl.length(); j++)
144
 
             {
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);
148
 
             }
149
 
          if(func_on == true)
150
 
            {
151
 
             if (m_plugin->treeOn)
152
 
               {
153
 
                node = new QTreeWidgetItem(clsNode, lastClsNode);
154
 
                if (m_plugin->expandedOn) m_symbols->expandItem(node);
155
 
                lastClsNode = node;
156
 
                mtdNode = lastClsNode;
157
 
                lastMtdNode = lastClsNode;
158
 
               }
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));
163
 
             stripped = "";
164
 
             if (mclass == 1) mclass = 3;
165
 
            }
166
 
          continue;
167
 
         }
168
 
       if (mclass == 3)
169
 
         {
170
 
          if (cl.indexOf('{') >= 0)
171
 
            {
172
 
             cl = cl.mid(cl.indexOf('{'));
173
 
             mclass = 4;
174
 
            }
175
 
         }
176
 
 
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)
183
 
       //    block = 0;
184
 
       if(block > 0 && mclass != 1 )
185
 
         {
186
 
          for (j = 0; j < cl.length(); j++)
187
 
            {
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)
197
 
               {
198
 
                if (block == 1 && graph == 0 )
199
 
                  {
200
 
                   if(cl.at(j) >= 0x20) stripped += cl.at(j);
201
 
                   if(cl.at(j) == '(') par++;
202
 
                   if(cl.at(j) == ')')
203
 
                     {
204
 
                      par--;
205
 
                      if(par == 0)
206
 
                        {
207
 
                         stripped = stripped.trimmed();
208
 
                         stripped.remove("static ");
209
 
                         //kDebug(13000)<<"Function -- Inserted : "<<stripped<<" at row : "<<i;
210
 
                         block = 2;
211
 
                         tmpPos = i;
212
 
                        }
213
 
                     }
214
 
                  } // BLOCK 1
215
 
                if(block == 2 && graph == 0)
216
 
                  {
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; }
220
 
                   if(cl.at(j)==';')
221
 
                     {
222
 
                      stripped = "";
223
 
                      block = 0;
224
 
                      structure = false;
225
 
                      break;
226
 
                     }
227
 
 
228
 
                   if((cl.at(j)=='{' && structure == false && cl.indexOf(';') < 0) ||
229
 
                      (cl.at(j)=='{' && structure == false && cl.indexOf('}') > (int)j))
230
 
                     {
231
 
                      stripped.replace(0x9, " ");
232
 
                      if(func_on == true)
233
 
                        {
234
 
                         if (m_plugin->typesOn == false)
235
 
                           {
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);
243
 
                            while ( 
244
 
                                (stripped.length()>0) &&
245
 
                                  ( 
246
 
                                    (stripped.at(0)=='*') ||
247
 
                                    (stripped.at(0)=='&')
248
 
                                  )
249
 
                              ) stripped=stripped.right(stripped.length()-1);
250
 
                           }
251
 
                         if (m_plugin->treeOn)
252
 
                           {
253
 
                            if (mclass == 4)
254
 
                              {
255
 
                               node = new QTreeWidgetItem(mtdNode, lastMtdNode);
256
 
                               lastMtdNode = node;
257
 
                              }
258
 
                            else
259
 
                              {
260
 
                               node = new QTreeWidgetItem(clsNode, lastClsNode);
261
 
                               lastClsNode = node;
262
 
                              }
263
 
                           }
264
 
                         else
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));
270
 
                        }
271
 
                      stripped = "";
272
 
                      //retry = 0;
273
 
                      block = 3;
274
 
                     }
275
 
                   if(cl.at(j)=='{' && structure == true)
276
 
                     {
277
 
                      block = 3;
278
 
                      tmpPos = i;
279
 
                     }
280
 
                   if(cl.at(j)=='(' && structure == true)
281
 
                     {
282
 
                      //retry = 1;
283
 
                      block = 0;
284
 
                      j = 0;
285
 
                      //kDebug(13000)<<"Restart from the beginning of line...";
286
 
                      stripped = "";
287
 
                      break; // Avoid an infinite loop :(
288
 
                     }
289
 
                   if(structure == true && cl.at(j) >= 0x20) stripped += cl.at(j);
290
 
                  } // BLOCK 2
291
 
 
292
 
                if (block == 3)
293
 
                  {
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++;
297
 
                   if(cl.at(j)=='}')
298
 
                     {
299
 
                      graph--;
300
 
                      if (graph == 0 && structure == false)  { block = 0; func_close = 1; }
301
 
                      if (graph == 0 && structure == true) block = 4;
302
 
                     }
303
 
                  } // BLOCK 3
304
 
 
305
 
                if (block == 4)
306
 
                  {
307
 
                   if(cl.at(j) == ';')
308
 
                     {
309
 
                      //stripped.replace(0x9, " ");
310
 
                      stripped.remove('{');
311
 
                      stripped.replace('}', " ");
312
 
                      if(struct_on == true)
313
 
                        {
314
 
                         if (m_plugin->treeOn)
315
 
                           {
316
 
                            node = new QTreeWidgetItem(sctNode, lastSctNode);
317
 
                            lastSctNode = node;
318
 
                           }
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));
323
 
                        }
324
 
                      //kDebug(13000)<<"Structure -- Inserted : "<<stripped<<" at row : "<<i;
325
 
                      stripped = "";
326
 
                      block = 0;
327
 
                      structure = false;
328
 
                      //break;
329
 
                      continue;
330
 
                     }
331
 
                   if (cl.at(j) >= 0x20) stripped += cl.at(j);
332
 
                  } // BLOCK 4
333
 
               } // comment != 2
334
 
             //kDebug(13000)<<"Stripped : "<<stripped<<" at row : "<<i;
335
 
            } // End of For cycle
336
 
         } // BLOCK > 0
337
 
       if (mclass == 4 && block == 0 && func_close == 0)
338
 
         {
339
 
          if (cl.indexOf('}') >= 0)
340
 
            {
341
 
             cl = cl.mid(cl.indexOf('}'));
342
 
             mclass = 0;
343
 
            }
344
 
         }
345
 
      } // Comment != 1
346
 
   } // for kv->numlines
347
 
 
348
 
 //for (i= 0; i < (m_symbols->itemIndex(node) + 1); i++)
349
 
 //    kDebug(13000)<<"Symbol row :"<<positions.at(i);
350
 
}
351
 
 
352