summaryrefslogtreecommitdiffstats
path: root/languages/perl/perlparser.cpp
blob: 4ab48333f1950d08af30f32c0e2a78594ea9448a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/***************************************************************************
                          perlparser.cpp  -  description
                             -------------------
    begin                : Sun Nov 2 2003
    copyright            : (C) 2003 by luc
    email                : willems.luc(at)pandora.be
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "perlparser.h"

#include <kdebug.h>
#include <tqfile.h>
#include <tqregexp.h>
#include <tqfileinfo.h>

perlparser::perlparser(KDevCore* core,CodeModel* model, TQString interpreter) {
  m_core = core;
  m_model = model;
  m_interpreter=interpreter;
  //get INC paths for current installed perl
  getPerlINC();

}

perlparser::~perlparser(){
}

const TQStringList perlparser::UseFiles()
{
  return m_usefiles;
}

void perlparser::initialParse() {
  m_usefiles.clear();
}

void perlparser::parse(const TQString &fileName){
  TQFile f(fileName);
  if (!f.open(IO_ReadOnly))
      return;
  TQTextStream stream(&f);
  TQStringList list;
  TQString rawline;
  while (!stream.eof()) {
   rawline = stream.readLine();
   list.append(rawline.stripWhiteSpace().local8Bit());
 }
 f.close();
 kdDebug(9016) << "parsing " << fileName << endl;

 m_file = m_model->create<FileModel>();
 m_file->setName(fileName );
 this->parseLines(&list,fileName);
 m_model->addFile( m_file );
}

void perlparser::parseLines(TQStringList* lines,const TQString &fileName)
{
  TQRegExp  packagere("^[ \t]*package[ \t]+([+A-Za-z0-9_:]*).*\\;");
  TQRegExp     basere("^[ \t]*use[ \t]+base[ \t]*\\(\'*\"*([A-Za-z0-9_:]*)");
  TQRegExp      libre("^[ \t]*use[ \t]+lib[ \t]*\\(\'*\"*([A-Za-z0-9_:]*)");
  TQRegExp      usere("^[ \t]*use[ \t]+([+A-Za-z0-9_:]*).*\\;");
  TQRegExp      isare("^[ \t]*@ISA[ \t=qw\\(\'\"]*([A-Za-z0-9_: ]*)");
  TQRegExp   globalre("^[ \t]*our[ \t]+\\(*([ \t,$%@*+A-Za-z0-9_]*)\\)*.*");
  TQRegExp   myre("^[ \t]*my[ \t]+\\(*([ \t,$%@*+A-Za-z0-9_]*)\\)*.*");
  TQRegExp      subre("^[ \t]*sub[ \t]+([A-Za-z0-9_]+)([A-Za-z0-9_]|([ \t]*[{])?)$");
  TQRegExp    blessre("bless[ \t]*[\\( ]*([,$%@*+A-Za-z0-9_]*).*;");
  TQRegExp     namere("^[ \t]*([$%@*])([A-Za-z0-9_]*).*$");
  TQRegExp  privatere("^_([A-Za-z0-9_]*)");
  TQRegExp  startpod("^=[a-z0-9]+ [a-z0-9]*");
  TQRegExp  cutpod("^=cut");

  TQString line;

  //clear all "last" know things
  m_lastsub="";
  m_lastattr="";
  m_inpackage = false;
  m_inscript = false;
  m_inclass=false;

  m_lastscript=0;
  m_lastpackage=0;
  m_lastclass=0;

  int lineNo = -1;
  
  bool inpod = false;
  bool endpod = false;

  //check if we are parsing a script or module
  TQFileInfo fi(fileName);
  bool inscript =(fi.extension() == "pl");
  kdDebug(9016) << "inscript : " << inscript << "," << fi.extension() << endl;

  if (inscript) {
    addScript(fileName,lineNo,fi.fileName());
  }

  for ( TQStringList::Iterator it = lines->begin(); it != lines->end(); ++it ) {
    ++lineNo;
    line = (*it).local8Bit();
    //empty line ?
    if (line.isEmpty()) { continue;}
    //some POD checking , quick and dirty but it seams to work
    if(inpod && endpod) { inpod=false; endpod=false;}
    //are we in pod documentation ?
    if (startpod.search(line)>=0) {inpod=true; continue;}
    //are we in pod documentation ?
    if (inpod) { endpod=( cutpod.search(line)>=0 ); continue; }


    //sub matching
    if (subre.search(line)>=0){
          TQString subname=subre.cap(1);
          kdDebug(9016) << "subre match [" << subname << "]" << endl;
          bool prive = privatere.search(subname) >= 0;
          kdDebug(9016) << "prive match [" << prive << "]" << endl;
          if (m_inscript)      { addScriptSub(fileName,lineNo,subname,prive);}
          else {
             if (m_inclass)    { addClassMethod(fileName,lineNo,subname,prive);}
             else              { addPackageSub(fileName,lineNo,subname,prive);}
          }
          continue;
    } //sub

    //our matching
    if (globalre.search(line)>=0) {
        //splitup multible ours
        TQString varlist=globalre.cap(1);
        kdDebug(9016) << "globalre match [" << varlist <<"]" << endl;
        TQStringList vars=TQStringList::split(",",varlist);
        for ( TQStringList::Iterator it = vars.begin(); it != vars.end(); ++it ) {
            if (namere.search(*it)>=0) {
              TQString var = namere.cap(2);
              kdDebug(9016) << "namere match [" << var << "]" << endl;
              if (m_lastpackage)  { addAttributetoPackage(fileName,lineNo,var); }
              else                { addAttributetoScript(fileName,lineNo,var); }
            }
        }
        continue;
    } //globalre

        
    //bless matching
    if ((blessre.search(line)>=0) && (!m_inscript)) {
         kdDebug(9016) << "blessre match []" << endl;
         addClass(fileName,lineNo);
         addConstructor(fileName,lineNo,m_lastsub);
         continue;
    } //bless

    //base matching
    if ((basere.search(line)>=0) && (!m_inscript)) {
         TQString tqparent = basere.cap(1);
         //create child & tqparent classes
         kdDebug(9016) << "basere match [" << tqparent << "]" << endl;
         addClass(fileName,lineNo);
         addParentClass(tqparent);
         continue;
    } else {
      if (libre.search(line)>=0) {
         TQString path = libre.cap(1);
         //add lib to INC path list
         kdDebug(9016) << "libre match [" << path << "]" << endl;
         m_INClist.append(path);
         continue;
      } else {
         if (usere.search(line)>=0) {
           //add lib to use list for later parsing
           TQString lib = usere.cap(1);
           kdDebug(9016) << "usere match [" << lib << "]" << endl;
           addUseLib(lib);
           continue;
         }                              \
     }
    } //base

    if ((isare.search(line)>=0) && (!m_inscript)) {
         TQString tqparent = isare.cap(1);
         //create child & tqparent classes
         kdDebug(9016) << "isare match [" << tqparent << "]" << endl;
         addClass(fileName,lineNo);
         addParentClass(tqparent);
         continue;
    } //isa

    if ((packagere.search(line)>=0) && (!m_inscript)) {
         TQString package=packagere.cap(1);
         kdDebug(9016) << "packagere match [" << package << "]" << endl;
         addPackage(fileName,lineNo,package);
         continue;
    }//package

  } // for lines loop
}

void perlparser::addPackage(const TQString& fileName ,int lineNr , const TQString& name)
{
 kdDebug(9016) << "AddPackage [" << name << "]" << endl;
 NamespaceDom package = m_model->create<NamespaceModel>();

 package->setName(name);
 package->setFileName(fileName );
 package->setStartPosition(lineNr, 0 );
 package->setScope(name);

 if (!m_file->hasNamespace(name)) {
     m_file->addNamespace(package);
     m_lastpackage=package;
 } else {
   kdDebug(9016) << "addPackage [" << name << " exist]" << endl;
 }

 //clear all "last" know things
 m_lastpackagename=name;
 m_lastsub="";
 m_lastattr="";
 m_inpackage=true;
 m_inscript = false;
 m_inclass=false;

 m_lastclass=0;
 m_lastscript=0;

}

void perlparser::addScript(const TQString& fileName ,int lineNr ,const TQString& name)
{
 kdDebug(9016) << "addScript [" << name << "]" << endl;

 //map name of script under /scripts
 //m_file->setName("/Scripts/"+name);

 kdDebug(9016) << "addScript [" << name << "]" << endl;
 NamespaceDom script = m_model->create<NamespaceModel>();

 script->setName(name);
 script->setFileName(fileName );
 script->setStartPosition(lineNr, 0 );
 script->setScope(name);

 if (!m_file->hasNamespace(name)) {
      m_file->addNamespace(script);
      m_lastscript=script;
 } else {
   kdDebug(9016) << "addScript [" << name << " exist]" << endl;
 }
 
 //clear all "last" know things
 m_lastsub="";
 m_lastattr="";
 m_inpackage = false;
 m_inscript = true;
 m_inclass=false;

 m_lastscriptname=name;
 m_lastpackage=0;
 m_lastclass=0;

}

void perlparser::addAttributetoPackage(const TQString& fileName ,int lineNr ,const TQString& name)
{
 kdDebug(9016) << "addAttributetoPackage [" << name << "]" << endl;
 VariableDom var = m_model->create<VariableModel>();
 var->setName(name);
 var->setFileName( fileName );
 var->setStartPosition( lineNr, 0 );
 if (m_lastpackage) {
    if (!m_lastpackage->hasVariable(var->name()))
         m_lastpackage->addVariable(var);
 } else {
    kdDebug(9016) << "addAttributetoPackge[ no m_file]" << endl;
 }

 m_lastattr=name;
}

void perlparser::addAttributetoScript(const TQString& fileName ,int lineNr ,const TQString& name)
{
  kdDebug(9016) << "addAttributetoScript [" << name << "]" << endl;
  VariableDom var = m_model->create<VariableModel>();
  var->setName(name);
  var->setFileName( fileName );
  var->setStartPosition( lineNr, 0 );
  if (m_lastscript) {
    if (!m_lastscript->hasVariable(var->name()))
       m_lastscript->addVariable(var);
  } else {
    kdDebug(9016) << "addAttributeScript[ no m_file]" << endl;
  }
}

void perlparser::addClass(const TQString& fileName ,int lineNr)
{
  kdDebug(9016) << "addClass [ " << m_lastpackagename << " ]" << endl;
  if (m_lastpackage->hasClass(m_lastpackagename)) {
    kdDebug(9016) << "Class already defined" << endl;
  } else {
    kdDebug(9016) << "new Class" << endl;
    ClassDom lastClass = m_model->create<ClassModel>();
    lastClass->setName(m_lastpackagename);
   	lastClass->setFileName(fileName);
	  lastClass->setStartPosition(lineNr, 0);
    m_lastpackage->addClass(lastClass);
    m_lastclass=lastClass;
    m_inclass=true;
  }
}

void perlparser::addConstructor(const TQString& fileName ,int lineNr ,const TQString& name)
{
  kdDebug(9016) << "set Constructor["<< name << "]" << endl;

  FunctionDom method;

  if (m_lastpackage->hasFunction(name)) {
      //remove last sub frompackage scope
      method = m_lastpackage->functionByName(name)[0];
      method->getStartPosition(&lineNr,0);
      m_lastpackage->removeFunction(method);
  }
  method = m_lastclass->functionByName(name)[0];
  if (!method) { 
     kdDebug(9016) << "add new Constructor["<< name << ", " << lineNr << "]" << endl;
     method = m_model->create<FunctionModel>();
     method->setName(name);
     method->setFileName( fileName );
     method->setStartPosition( lineNr, 0 );
     m_lastclass->addFunction(method);
  }
  method->setStatic(true);
  //update class position
  m_lastclass->setStartPosition(lineNr,0);
}

void perlparser::addGlobalSub(const TQString& fileName ,int lineNr ,const TQString& name ,bool privatesub)
{
  kdDebug(9016) << "addGlobalSub[ " << name << "]" << endl;

  FunctionDom method = m_model->create<FunctionModel>();
  method->setName(name);
  method->setFileName( fileName );
  method->setStartPosition( lineNr, 0 );
  method->setStatic(true);
  if (privatesub)
     method->setAccess(CodeModelItem::Private);
  if (m_lastpackage) {
    if (!m_lastpackage->hasFunction(method->name()))
         m_lastpackage->addFunction(method);
  } else {
    kdDebug(9016) << "addGlobalsub[ no m_lastpackage]" << endl;
  }

 //also add seperate to namespace
 addPackageSub(fileName,lineNr,name,privatesub);
 m_lastsub=name;
}

void perlparser::addScriptSub(const TQString& fileName ,int lineNr ,const TQString& name ,bool privatesub)

{
  kdDebug(9016) << "addScriptSub[ " << name << "]" << endl;
  FunctionDom method = m_model->create<FunctionModel>();
  method->setName(name);
  method->setFileName( fileName );
  method->setStartPosition( lineNr, 0 );
  if (privatesub)
     method->setAccess(CodeModelItem::Private);
  if(m_lastscript) {
    m_lastscript->addFunction(method);
  } else {
  }

  m_lastsub=name;
}

void perlparser::addClassMethod(const TQString& fileName ,int lineNr ,const TQString& name ,bool privatesub)
{
  kdDebug(9016) << "addClassMethod[ " << name << "]" << endl;
  FunctionDom method = m_model->create<FunctionModel>();
  method->setName(name);
  method->setFileName( fileName );
  method->setStartPosition( lineNr, 0 );
  method->setVirtual(true);
  if (privatesub)
     method->setAccess(CodeModelItem::Private);
  if (m_lastclass) {
    if (!m_lastclass->hasFunction(method->name()))
       m_lastclass->addFunction(method);
  } else {
    kdDebug(9016) << "addClassmethod[ no m_lastclass]" << endl;
  }

// addPackageSub(fileName,lineNr,name,privatesub);
 m_lastsub=name;
}

void perlparser::addPackageSub(const TQString& fileName ,int lineNr ,const TQString& name ,bool privatesub)
{
  kdDebug(9016) << "addPackageSub[ " << name << "]" << endl;
  FunctionDom method = m_model->create<FunctionModel>();
  method->setName(name);
  method->setFileName( fileName );
  method->setStartPosition( lineNr, 0 );
  if (privatesub)
     method->setAccess(CodeModelItem::Private);
  if (m_lastpackage) {
    if (!m_lastpackage->hasFunction(method->name()))
       m_lastpackage->addFunction(method);
 } else {
   kdDebug(9016) << "addPackageSub[ no m_file]" << endl;
 }
 m_lastsub=name;
}

void perlparser::addParentClass(const TQString& tqparent)
{
 kdDebug(9016) << "addParentClass[ " << tqparent << "]" << endl;
 if (m_lastclass) {
        m_lastclass->addBaseClass(tqparent);
 } else {
    kdDebug(9016) << "addParentClass[ no m_lastclass]" << endl;
 }
}

void perlparser::addUseLib(const TQString& lib)
{
 if (!m_model->hasFile(lib)) {
   if (m_usefiles.tqfindIndex(lib) == -1) {
      //only add if not already parsed or in the list
      kdDebug(9016) << "add lib for later parsing [" << lib << "]" << endl;
      m_usefiles.append(lib);
   }
 }
}

void perlparser::getPerlINC() {


 m_INClist.clear();

 TQString cmd = "/usr/bin/perl -e\" print join('|',@INC);\"";
 TQString result;

 FILE *fd = popen(cmd.local8Bit().data(), "r");
 char buffer[4090];
 TQByteArray array;

 while (!feof(fd)) {
        int n = fread(buffer, 1, 2048, fd);
        if (n == -1) {
            pclose(fd);
            return;
        }
        array.setRawData(buffer, n);
        result=TQString(array);
        array.resetRawData(buffer, n);
 }
 pclose(fd);
 //get INC list so we can use it to parse "use" modules
 m_INClist = TQStringList::split(TQString("|"),result);
 kdDebug(9016) << "INC " << m_INClist.size() << " "<< result << endl;
}

TQString perlparser::findLib( const TQString& lib)
{
  TQString result;

  TQString file=lib;
  file.tqreplace( TQRegExp("::"), TQString("/"));

  //find the correct path by using the INC list
  TQStringList::Iterator inc = m_INClist.begin();
  while((inc != m_INClist.end()) && (result.isEmpty()) ) {
     TQFileInfo fi((*inc) + "/" + file + ".pm");
     if ( fi.exists() ) {
        result = (*inc) + "/" + file + ".pm";
     }
     ++inc;
  }
  return result;
}