KDevelop API Documentation

languages/java/driver.cpp

Go to the documentation of this file.
00001 /* This file is part of KDevelop 00002 Copyright (C) 2002,2003 Roberto Raggi <roberto@kdevelop.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "JavaAST.hpp" 00021 #include "JavaLexer.hpp" 00022 #include "JavaRecognizer.hpp" 00023 00024 #include <kdebug.h> 00025 #include <stdlib.h> 00026 #include <qfile.h> 00027 #include <qfileinfo.h> 00028 #include <qdir.h> 00029 00030 #include <string> 00031 #include <strstream> 00032 00033 class DefaultSourceProvider: public SourceProvider 00034 { 00035 public: 00036 DefaultSourceProvider() {} 00037 00038 virtual QString contents( const QString& fileName ) 00039 { 00040 QString source; 00041 00042 QFile f( fileName ); 00043 if( f.open(IO_ReadOnly) ){ 00044 QTextStream s( &f ); 00045 source = s.read(); 00046 f.close(); 00047 } 00048 return source; 00049 } 00050 00051 virtual bool isModified( const QString& fileName ) 00052 { 00053 Q_UNUSED( fileName ); 00054 return true; 00055 } 00056 00057 private: 00058 DefaultSourceProvider( const DefaultSourceProvider& source ); 00059 void operator = ( const DefaultSourceProvider& source ); 00060 }; 00061 00062 00063 Driver::Driver() 00064 : lexer( 0 ) 00065 { 00066 m_sourceProvider = new DefaultSourceProvider(); 00067 } 00068 00069 Driver::~Driver() 00070 { 00071 reset(); 00072 delete( m_sourceProvider ); 00073 } 00074 00075 SourceProvider* Driver::sourceProvider() 00076 { 00077 return m_sourceProvider; 00078 } 00079 00080 void Driver::setSourceProvider( SourceProvider* sourceProvider ) 00081 { 00082 if( m_sourceProvider ) 00083 delete( m_sourceProvider ); 00084 m_sourceProvider = sourceProvider; 00085 } 00086 00087 void Driver::reset( ) 00088 { 00089 m_problems.clear(); 00090 m_includePaths.clear(); 00091 00092 while( m_parsedUnits.size() ){ 00093 RefJavaAST unit = *m_parsedUnits.begin(); 00094 m_parsedUnits.remove( m_parsedUnits.begin() ); 00095 delete( unit ); 00096 } 00097 } 00098 00099 void Driver::remove( const QString & fileName ) 00100 { 00101 m_problems.remove( fileName ); 00102 00103 QMap<QString, RefJavaAST>::Iterator it = m_parsedUnits.find( fileName ); 00104 if( it != m_parsedUnits.end() ){ 00105 RefJavaAST unit = *it; 00106 m_parsedUnits.remove( it ); 00107 delete( unit ); 00108 } 00109 } 00110 00111 RefJavaAST Driver::takeTranslationUnit( const QString& fileName ) 00112 { 00113 QMap<QString, RefJavaAST>::Iterator it = m_parsedUnits.find( fileName ); 00114 RefJavaAST unit( *it ); 00115 //m_parsedUnits.remove( it ); 00116 m_parsedUnits[ fileName] = 0; 00117 return unit; 00118 } 00119 00120 RefJavaAST Driver::translationUnit( const QString& fileName ) const 00121 { 00122 QMap<QString, RefJavaAST>::ConstIterator it = m_parsedUnits.find( fileName ); 00123 return it != m_parsedUnits.end() ? *it : RefJavaAST(); 00124 } 00125 00126 void Driver::addProblem( const QString & fileName, const Problem & problem ) 00127 { 00128 findOrInsertProblemList( fileName ).append( problem ); 00129 } 00130 00131 QValueList < Problem >& Driver::findOrInsertProblemList( const QString & fileName ) 00132 { 00133 QMap<QString, QValueList<Problem> >::Iterator it = m_problems.find( fileName ); 00134 if( it != m_problems.end() ) 00135 return it.data(); 00136 00137 QValueList<Problem> l; 00138 m_problems.insert( fileName, l ); 00139 return m_problems[ fileName ]; 00140 } 00141 00142 QValueList < Problem > Driver::problems( const QString & fileName ) const 00143 { 00144 QMap<QString, QValueList<Problem> >::ConstIterator it = m_problems.find( fileName ); 00145 if( it != m_problems.end() ) 00146 return it.data(); 00147 return QValueList<Problem>(); 00148 } 00149 00150 void Driver::parseFile( const QString& fileName, bool onlyPreProcess, bool force ) 00151 { 00152 QFileInfo fileInfo( fileName ); 00153 QString absFilePath = fileInfo.absFilePath(); 00154 00155 QMap<QString, RefJavaAST>::Iterator it = m_parsedUnits.find( absFilePath ); 00156 00157 if( force && it != m_parsedUnits.end() ){ 00158 takeTranslationUnit( absFilePath ); 00159 } else if( it != m_parsedUnits.end() && *it != 0 ){ 00160 // file already processed 00161 return; 00162 } 00163 00164 m_problems.remove( fileName ); 00165 00166 m_currentFileName = fileName; 00167 00168 std::string source( sourceProvider()->contents(fileName).utf8() ); 00169 std::istrstream in( source.c_str() ); 00170 00171 JavaLexer lex( in ); 00172 lex.setDriver( this ); 00173 lexer = &lex; 00174 setupLexer( &lex ); 00175 00176 00178 00179 RefJavaAST translationUnit; 00180 if( !onlyPreProcess ){ 00181 JavaRecognizer parser( lex ); 00182 parser.setDriver( this ); 00183 setupParser( &parser ); 00184 00185 00186 try{ 00187 // make an ast factory 00188 ANTLR_USE_NAMESPACE(antlr)JavaASTFactory ast_factory; 00189 // initialize and put it in the parser... 00190 parser.initializeASTFactory (ast_factory); 00191 parser.setASTFactory (&ast_factory); 00192 00193 parser.compilationUnit(); 00194 00195 RefJavaAST translationUnit = parser.getAST(); 00196 m_parsedUnits.insert( fileName, translationUnit ); 00197 00198 } catch( ANTLR_USE_NAMESPACE(antlr)ANTLRException& ex ){} 00199 00200 } 00201 00202 m_currentFileName = QString::null; 00203 lexer = 0; 00204 00205 fileParsed( fileName ); 00206 } 00207 00208 void Driver::setupLexer( JavaLexer * lexer ) 00209 { 00210 } 00211 00212 void Driver::setupParser( JavaRecognizer * parser ) 00213 { 00214 Q_UNUSED( parser ); 00215 } 00216 00217 void Driver::addIncludePath( const QString &path ) 00218 { 00219 if( !path.stripWhiteSpace().isEmpty() ) 00220 m_includePaths << path; 00221 } 00222 00223 void Driver::fileParsed( const QString & fileName ) 00224 { 00225 Q_UNUSED( fileName ); 00226 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:07 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003