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
|
/***************************************************************************
* Copyright (C) 2001 by Sandy Meier *
* smeier@kdevelop.org *
* *
* 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 "phpparser.h"
#include <kdevcore.h>
#include <codemodel.h>
#include <qregexp.h>
#include <kdebug.h>
#include <qfileinfo.h>
#include <qtextstream.h>
#include <iostream>
using namespace std;
PHPParser::PHPParser(KDevCore* core,CodeModel* model){
m_core = core;
m_model = model;
}
PHPParser::~PHPParser(){
}
void PHPParser::parseLines(QStringList* lines,const QString& fileName){
kdDebug(9018) << "enter parsedLines" << endl;
QRegExp classre("^[ \t]*class[ \t]+([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[ \t]*(extends[ \t]*([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))?.*$");
QRegExp methodre("^[ \t]*function[ \t&]*([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[ \t]*\\(([a-zA-Z_\x7f-\xff]*[0-9A-Za-z_\x7f-\xff\\$\\, \t=&\\'\\\"]*)\\).*$");
QRegExp varre("^[ \t]*var[ \t]*\\$([a-zA-Z_\x7f-\xff][0-9A-Za-z_\x7f-\xff]*)[ \t;=].*$");
QRegExp createMemberRe("\\$this->([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[ \t]*=[ \t&]*new[ \t]+([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)");
ClassDom lastClass = 0;
QString rawline;
QCString line;
int lineNo = 0;
int bracketOpen = 0;
int bracketClose = 0;
bool inClass = false;
for ( QStringList::Iterator it = lines->begin(); it != lines->end(); ++it ) {
line = (*it).local8Bit();
if(!line.isNull()){
// cerr << "LINE" << line << endl;
bracketOpen += line.contains("{");
bracketClose += line.contains("}");
if(bracketOpen == bracketClose && bracketOpen !=0 && bracketClose !=0){
inClass = false; // ok we are out ouf class
}
//cerr << "kdevelop (phpsupport): try match line: " << line << endl;
if (classre.search(line) != -1 ) {
// cerr << "kdevelop (phpsupport): regex match line: " << line << endl;
inClass= true;
bracketOpen = line.contains("{");
bracketClose = line.contains("}");
lastClass = m_model->create<ClassModel>();
lastClass->setName(classre.cap(1));
lastClass->setFileName(fileName);
lastClass->setStartPosition(lineNo, 0);
QString parentStr = classre.cap(3);
if(!parentStr.isEmpty()){
lastClass->addBaseClass(parentStr);
}
m_file->addClass(lastClass);
} else if (createMemberRe.search(line) != -1 ) {
if (lastClass && inClass) {
if( lastClass->hasVariable(QString("$") + createMemberRe.cap(1)) )
lastClass->variableByName(QString("$") + createMemberRe.cap(1))->setType(createMemberRe.cap(2));
}
} else if (methodre.search(line) != -1) {
// cerr << "kdevelop (phpsupport): regex match line ( method ): " << line << endl;
FunctionDom method = m_model->create<FunctionModel>();
method->setName(methodre.cap(1));
ArgumentDom anArg = m_model->create<ArgumentModel>();
QString arguments = methodre.cap(2);
anArg->setType(arguments.stripWhiteSpace().local8Bit());
method->addArgument( anArg );
method->setFileName( fileName );
method->setStartPosition( lineNo, 0 );
if (lastClass && inClass) {
// kdDebug(9018) << "in Class: " << line << endl;
if (!lastClass->hasFunction(method->name()))
lastClass->addFunction(method);
} else {
if (!m_file->hasFunction(method->name()) )
m_file->addFunction(method);
}
}
else if (varre.search(line) != -1) {
// kdDebug(9018) << "###########regex match line ( var ): " << varre.cap(1) << endl;
if (lastClass && inClass) {
VariableDom anAttr = m_model->create<VariableModel>();
anAttr->setName(varre.cap(1));
anAttr->setFileName(fileName);
anAttr->setStartPosition( lineNo, 0 );
lastClass->addVariable( anAttr );
}
}
++lineNo;
} // end for
} // end if
}
void PHPParser::parseFile(const QString& fileName){
kdDebug(9018) << "enter parsedFile" << endl;
kdDebug(9018) << "FileName:" << fileName.latin1() << endl;
QFile f(QFile::encodeName(fileName));
if (!f.open(IO_ReadOnly))
return;
QTextStream stream(&f);
QStringList list;
QString rawline;
while (!stream.eof()) {
rawline = stream.readLine();
list.append(rawline.stripWhiteSpace().local8Bit());
}
f.close();
m_file = m_model->create<FileModel>();
m_file->setName( fileName );
this->parseLines(&list,fileName);
m_model->addFile( m_file );
}
|