phpparser.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "phpparser.h"
00013
00014 #include <kdevcore.h>
00015 #include <codemodel.h>
00016
00017 #include <qregexp.h>
00018 #include <kdebug.h>
00019
00020 #include <qfileinfo.h>
00021 #include <qtextstream.h>
00022
00023 #include <iostream>
00024
00025 using namespace std;
00026
00027 PHPParser::PHPParser(KDevCore* core,CodeModel* model){
00028 m_core = core;
00029 m_model = model;
00030 }
00031 PHPParser::~PHPParser(){
00032 }
00033 void PHPParser::parseLines(QStringList* lines,const QString& fileName){
00034 kdDebug(9018) << "enter parsedLines" << endl;
00035 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]*))?.*$");
00036 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=&\\'\\\"]*)\\).*$");
00037 QRegExp varre("^[ \t]*var[ \t]*\\$([a-zA-Z_\x7f-\xff][0-9A-Za-z_\x7f-\xff]*)[ \t;=].*$");
00038 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]*)");
00039
00040 ClassDom lastClass = 0;
00041 QString rawline;
00042 QCString line;
00043 int lineNo = 0;
00044 int bracketOpen = 0;
00045 int bracketClose = 0;
00046 bool inClass = false;
00047
00048 for ( QStringList::Iterator it = lines->begin(); it != lines->end(); ++it ) {
00049 line = (*it).local8Bit();
00050 if(!line.isNull()){
00051
00052 bracketOpen += line.contains("{");
00053 bracketClose += line.contains("}");
00054 if(bracketOpen == bracketClose && bracketOpen !=0 && bracketClose !=0){
00055 inClass = false;
00056 }
00057
00058 if (classre.search(line) != -1 ) {
00059
00060
00061 inClass= true;
00062 bracketOpen = line.contains("{");
00063 bracketClose = line.contains("}");
00064 lastClass = m_model->create<ClassModel>();
00065 lastClass->setName(classre.cap(1));
00066
00067 lastClass->setFileName(fileName);
00068 lastClass->setStartPosition(lineNo, 0);
00069
00070 QString parentStr = classre.cap(3);
00071 if(!parentStr.isEmpty()){
00072 lastClass->addBaseClass(parentStr);
00073 }
00074
00075 m_file->addClass(lastClass);
00076
00077 } else if (createMemberRe.search(line) != -1 ) {
00078 if (lastClass && inClass) {
00079 if( lastClass->hasVariable(QString("$") + createMemberRe.cap(1)) )
00080 lastClass->variableByName(QString("$") + createMemberRe.cap(1))->setType(createMemberRe.cap(2));
00081 }
00082 } else if (methodre.search(line) != -1) {
00083
00084 FunctionDom method = m_model->create<FunctionModel>();
00085 method->setName(methodre.cap(1));
00086
00087 ArgumentDom anArg = m_model->create<ArgumentModel>();
00088 QString arguments = methodre.cap(2);
00089 anArg->setType(arguments.stripWhiteSpace().local8Bit());
00090 method->addArgument( anArg );
00091
00092 method->setFileName( fileName );
00093 method->setStartPosition( lineNo, 0 );
00094
00095 if (lastClass && inClass) {
00096
00097 if (!lastClass->hasFunction(method->name()))
00098 lastClass->addFunction(method);
00099 } else {
00100 if (!m_file->hasFunction(method->name()) )
00101 m_file->addFunction(method);
00102 }
00103 }
00104 else if (varre.search(line) != -1) {
00105
00106 if (lastClass && inClass) {
00107 VariableDom anAttr = m_model->create<VariableModel>();
00108 anAttr->setName(varre.cap(1));
00109 anAttr->setFileName(fileName);
00110 anAttr->setStartPosition( lineNo, 0 );
00111 lastClass->addVariable( anAttr );
00112 }
00113 }
00114
00115 ++lineNo;
00116 }
00117 }
00118 }
00119 void PHPParser::parseFile(const QString& fileName){
00120 kdDebug(9018) << "enter parsedFile" << endl;
00121 kdDebug(9018) << "FileName:" << fileName.latin1() << endl;
00122 QFile f(QFile::encodeName(fileName));
00123 if (!f.open(IO_ReadOnly))
00124 return;
00125 QTextStream stream(&f);
00126 QStringList list;
00127 QString rawline;
00128 while (!stream.eof()) {
00129 rawline = stream.readLine();
00130 list.append(rawline.stripWhiteSpace().local8Bit());
00131 }
00132 f.close();
00133
00134 m_file = m_model->create<FileModel>();
00135 m_file->setName( fileName );
00136
00137 this->parseLines(&list,fileName);
00138
00139 m_model->addFile( m_file );
00140 }
This file is part of the documentation for KDevelop Version 3.1.2.