summaryrefslogtreecommitdiffstats
path: root/languages/fortran/fixedformparser.cpp
blob: 8f07ad4ad7da28fccc1dc1b611013bfa001aa904 (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
/***************************************************************************
 *   Copyright (C) 2001 by Bernd Gehrmann                                  *
 *   bernd@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 "fixedformparser.h"

#include <tqfile.h>
#include <tqtextstream.h>
#include <kdebug.h>
#include <codemodel.h>


FixedFormParser::FixedFormParser(CodeModel* model)
{
    m_model = model;

    functionre.setPattern("(integer|real|logical|complex|character|"
                       "double(precision)?)function([^(]+).*");
    subroutinere.setPattern("subroutine([^(]+).*");

    functionre.setCaseSensitive( false );
    subroutinere.setCaseSensitive( false );
}


void FixedFormParser::process(const TQCString &line, const TQString &fileName, int lineNum)
{
    TQCString simplified;
    int l = line.length();
    for (int i=0; i < l; ++i)
        if (line[i] != ' ')
            simplified += line[i];

    if ( simplified.isEmpty() ) return;

    TQString name;
    if (functionre.search(simplified) != -1)
        name = functionre.cap(3);
    else if (subroutinere.search(simplified) != -1) 
        name = subroutinere.cap(1);
    else
        return;

    FunctionDom method = m_model->create<FunctionModel>();
    method->setName(name);
    method->setFileName(fileName);
    method->setStartPosition(lineNum, 0);

    if( !m_file->hasFunction(method->name()) )
        m_file->addFunction(method);
}


void FixedFormParser::parse(const TQString &fileName)
{
    TQFile f(TQFile::encodeName(fileName));
    if (!f.open(IO_ReadOnly))
        return;
    TQTextStream stream(&f);

    m_file = m_model->create<FileModel>();
    m_file->setName( fileName );

    TQCString line;
    int lineNum=0, startLineNum=0;
    while (!stream.atEnd()) {
        ++lineNum;
        TQCString str = stream.readLine().local8Bit();
        if (!str.isEmpty() && TQCString("*Cc#!").tqfind(str[0]) != -1)
            continue;
        // Continuation line
        if (str.length() > 6 && str.left(5) == "     " && str[5] != ' ') {
            line += str.right(str.length()-6);
            continue;
        }
        // An initial or invalid line. We don't care
        // about validity
        process(line, fileName, startLineNum);
        line = str.right(str.length()-6);
        startLineNum = lineNum-1;
    }
    process(line, fileName, startLineNum);

    f.close();

    m_model->addFile( m_file );
}