summaryrefslogtreecommitdiffstats
path: root/languages/pascal/pascalsupport_part.cpp
blob: 0d8c049bfa398015b529586a3c6f9b6f98a9c55c (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
/***************************************************************************
 *   Copyright (C) 2003 Alexander Dymo                                     *
 *   cloudtemple@mksat.net                                                 *
 *                                                                         *
 *   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 <fstream>

#include <tqdir.h>
#include <tqtimer.h>
#include <tqwhatsthis.h>

#include <kiconloader.h>
#include <klocale.h>
#include <kdevgenericfactory.h>
#include <kdebug.h>
#include <kapplication.h>
#include <kstatusbar.h>

#include <antlr/ASTFactory.hpp>

#include "catalog.h"
#include <kdevcore.h>
#include <kdevmainwindow.h>
#include <kdevpartcontroller.h>
#include <kdevproject.h>
#include <kdevplugininfo.h>

#include "pascalsupport_part.h"
#include "problemreporter.h"
#include "PascalLexer.hpp"
#include "PascalParser.hpp"
#include "PascalStoreWalker.hpp"

struct PascalSupportPartData{
    ProblemReporter* problemReporter;

    PascalSupportPartData()
        : problemReporter( 0 )
        {}
};

typedef KDevGenericFactory<PascalSupportPart> PascalSupportFactory;
static const KDevPluginInfo data("kdevpascalsupport");
K_EXPORT_COMPONENT_FACTORY( libkdevpascalsupport, PascalSupportFactory( data ) )

PascalSupportPart::PascalSupportPart(TQObject *parent, const char *name, const TQStringList &)
  : KDevLanguageSupport(&data, parent, name ? name : "KDevPascalSupport" ),
    d( new PascalSupportPartData() )
{
    setInstance(PascalSupportFactory::instance());
    setXMLFile("kdevpascalsupport.rc");

    d->problemReporter = new ProblemReporter( this );
    connect( core(), TQT_SIGNAL(configWidget(KDialogBase*)),
             d->problemReporter, TQT_SLOT(configWidget(KDialogBase*)) );

    connect( core(), TQT_SIGNAL(projectOpened()), this, TQT_SLOT(projectOpened()) );
    connect( core(), TQT_SIGNAL(projectClosed()), this, TQT_SLOT(projectClosed()) );
    connect( partController(), TQT_SIGNAL(savedFile(const KURL&)),
             this, TQT_SLOT(savedFile(const KURL&)) );
    connect( core(), TQT_SIGNAL(contextMenu(TQPopupMenu *, const Context *)),
             this, TQT_SLOT(contextMenu(TQPopupMenu *, const Context *)) );
    connect( core(), TQT_SIGNAL(configWidget(KDialogBase*)),
             this, TQT_SLOT(configWidget(KDialogBase*)) );
    connect( core( ), TQT_SIGNAL( projectConfigWidget( KDialogBase* ) ), this,
             TQT_SLOT( projectConfigWidget( KDialogBase* ) ) );

    mainWindow()->embedOutputView( d->problemReporter, i18n("Problems"), i18n("problem reporter") );
    TQWhatsThis::add(d->problemReporter, i18n("<b>Problem reporter</b><p>This window shows various \"problems\" in your project. "
        "It displays errors reported by a language parser."));
}

PascalSupportPart::~PascalSupportPart()
{
    mainWindow()->removeView( d->problemReporter );
    delete( d->problemReporter );

    delete( d );
}

PascalSupportPart::Features PascalSupportPart::features()
{
    return Features(Classes | Structs | Functions | Variables | Declarations);
}

void PascalSupportPart::projectOpened()
{
    connect(project(), TQT_SIGNAL(addedFilesToProject(const TQStringList &)),
        this, TQT_SLOT(addedFilesToProject(const TQStringList &)));
    connect(project(), TQT_SIGNAL(removedFilesFromProject(const TQStringList &)),
        this, TQT_SLOT(removedFilesFromProject(const TQStringList &)));
    connect(project(), TQT_SIGNAL(projectCompiled()),
        this, TQT_SLOT(slotProjectCompiled()) );

    m_projectFileList = project()->allFiles();
    m_projectClosed = false;

    TQTimer::singleShot(0, this, TQT_SLOT(initialParse()));
}

void PascalSupportPart::projectClosed()
{
    m_projectClosed = true;
}

void PascalSupportPart::configWidget(KDialogBase *dlg)
{
    Q_UNUSED( dlg );
    return;
}

void PascalSupportPart::projectConfigWidget(KDialogBase *dlg)
{
    Q_UNUSED( dlg );
    return;
}

void PascalSupportPart::contextMenu(TQPopupMenu *popup, const Context *context)
{
    Q_UNUSED( popup );
    Q_UNUSED( context );
    return;
}

void PascalSupportPart::savedFile(const KURL &fileName)
{
    maybeParse(fileName.path());
    emit updatedSourceInfo();
}

void PascalSupportPart::addedFilesToProject(const TQStringList &fileList)
{
    for (TQStringList::ConstIterator it = fileList.begin(); it != fileList.end() ;++it)
    {
        TQString fn = project()->projectDirectory() + "/" + *it;
        maybeParse( fn );
        kapp->processEvents( 500 );
        emit addedSourceInfo(fn);
    }
}

void PascalSupportPart::removedFilesFromProject(const TQStringList &fileList)
{
    for (TQStringList::ConstIterator it = fileList.begin(); it != fileList.end() ;++it)
    {
        TQString fn = project()->projectDirectory() + "/" + *it;

        emit aboutToRemoveSourceInfo(fn);
        codeModel()->removeFile( codeModel()->fileByName(fn) );
    }
}

void PascalSupportPart::slotProjectCompiled()
{
    return;
}

void PascalSupportPart::initialParse( )
{
    kdDebug(9013) << "------------------------------------------> initialParse()" << endl;

    if (project())
    {
        kapp->setOverrideCursor(waitCursor);

        /// @todo Progress indicator!

        TQStringList files = project()->allFiles();
        for (TQStringList::Iterator it = files.begin(); it != files.end() ;++it){
            TQString fn = project()->projectDirectory() + "/" + *it;
            maybeParse( fn );
            kapp->processEvents( 500 );
        }

        emit updatedSourceInfo();

        kapp->restoreOverrideCursor();
        mainWindow()->statusBar()->message( i18n("Found 1 problem", "Found %n problems", d->problemReporter->childCount()) );
    }
}

void PascalSupportPart::maybeParse( const TQString & fileName )
{
    kdDebug(9013) << "Maybe parse: " << fileName << endl;

    KMimeType::Ptr mime = KMimeType::findByURL( KURL( fileName ) );
    if( !mime || mime->name() != "text/x-pascal" )
	return;

    mainWindow()->statusBar()->message( i18n("Parsing file: %1").arg(fileName) );
    parse( fileName );
}

void PascalSupportPart::parse( const TQString & fileName )
{
    kdDebug(9013) << "PascalSupportPart::parse() -- " << fileName << endl;

    std::ifstream stream( TQFile::encodeName( fileName ).data() );
    TQCString _fn = fileName.utf8();
    std::string fn( _fn.data() );

    PascalLexer lexer( stream );
    lexer.setFilename( fn );
    lexer.setProblemReporter( d->problemReporter );

    PascalParser parser( lexer );
    parser.setFilename( fn );
    parser.setProblemReporter( d->problemReporter );

    try{
        antlr::ASTFactory my_factory( "PascalAST", PascalAST::factory );
        parser.initializeASTFactory(my_factory);
        parser.setASTFactory( &my_factory );

        lexer.resetErrors();
        parser.resetErrors();

        parser.compilationUnit();
        int errors = lexer.numberOfErrors() + parser.numberOfErrors();
        RefPascalAST ast = RefPascalAST( parser.getAST() );

        if( errors == 0 && ast != antlr::nullAST ){
            kdDebug(9013) << "-------------------> start StoreWalker" << endl;
/*            PascalStoreWalker walker;
            walker.setFileName( fileName );
            walker.setCodeModel( codeModel() );
            walker.compilationUnit( ast );*/
        }

    } catch( antlr::ANTLRException& ex ){
        kdDebug() << "*exception*: " << ex.toString().c_str() << endl;
        d->problemReporter->reportError( ex.getMessage().c_str(),
                                         fileName,
                                         lexer.getLine(),
                                         lexer.getColumn() );
    }
}

KMimeType::List PascalSupportPart::mimeTypes( )
{
    KMimeType::List list;
    KMimeType::Ptr mime = KMimeType::mimeType( "text/x-pascal" );
    if( mime )
	list << mime;
    return list;
}

TQString PascalSupportPart::formatTag( const Tag & inputTag )
{
    Tag tag = inputTag;

    switch( tag.kind() )
    {
        case Tag::Kind_Namespace:
            return TQString::fromLatin1("unit ") + tag.name();

        case Tag::Kind_Class:
            return TQString::fromLatin1("class ") + tag.name();

        case Tag::Kind_Function:
        case Tag::Kind_FunctionDeclaration:
        {
            return tag.name() + "()";
        }
        break;

        case Tag::Kind_Variable:
        case Tag::Kind_VariableDeclaration:
        {
            return TQString::fromLatin1("var ") + tag.name();
        }
        break;
    }
    return tag.name();
}

TQString PascalSupportPart::formatModelItem( const CodeModelItem * item, bool shortDescription )
{
    if (item->isFunction() || item->isFunctionDefinition() )
    {
        const FunctionModel *model = static_cast<const FunctionModel*>(item);
        TQString function;
        TQString args;
        ArgumentList argumentList = model->argumentList();
        for (ArgumentList::const_iterator it = argumentList.begin(); it != argumentList.end(); ++it)
        {
            args.isEmpty() ? args += "" : args += ", " ;
            args += formatModelItem((*it).data());
        }

        function += model->name() + "(" + args + ")";

        if( !shortDescription )
            function += (model->isVirtual() ? TQString("virtual; ") : TQString("") ) + model->resultType() + " ";

        return function;
    }
    else if (item->isVariable())
    {
        const VariableModel *model = static_cast<const VariableModel*>(item);
        if( shortDescription )
            return model->name();
        return model->name() + ": " + model->type();
    }
    else if (item->isArgument())
    {
        const ArgumentModel *model = static_cast<const ArgumentModel*>(item);
        TQString arg;
        arg += model->name();
        arg += ": " + model->type();
        if( !shortDescription )
            arg += model->defaultValue().isEmpty() ? TQString("") : TQString(" = ") + model->defaultValue();
        return arg.stripWhiteSpace();
    }
    else
        return KDevLanguageSupport::formatModelItem( item, shortDescription );
}

#include "pascalsupport_part.moc"