File: abstractlexer.cpp

package info (click to toggle)
analitza 4:17.08.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,824 kB
  • sloc: cpp: 26,899; perl: 63; sh: 16; makefile: 9
file content (120 lines) | stat: -rw-r--r-- 4,714 bytes parent folder | download | duplicates (3)
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
/*************************************************************************************
 *  Copyright (C) 2008 by Aleix Pol <aleixpol@kde.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.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "abstractlexer.h"
#include "expressionparser.h"
#include <QDebug>
#include <QStringList>

QHash<QChar, int> initializeOperators()
{
    QHash<QChar, int> operators;
    operators['+']=ExpressionTable::tAdd;
    operators['-']=ExpressionTable::tSub;
    operators['*']=ExpressionTable::tMul;
    operators['/']=ExpressionTable::tDiv;
    operators['^']=ExpressionTable::tPow;
    operators['(']=ExpressionTable::tLpr;
    operators[')']=ExpressionTable::tRpr;
    operators[',']=ExpressionTable::tComa;
    operators['{']=ExpressionTable::tLcb;
    operators['}']=ExpressionTable::tRcb;
    operators['[']=ExpressionTable::tLsp;
    operators[']']=ExpressionTable::tRsp;
    operators['?']=ExpressionTable::tQm;
    operators[':']=ExpressionTable::tColon;
    operators['=']=ExpressionTable::tEq;
    operators['<']=ExpressionTable::tLt;
    operators['>']=ExpressionTable::tGt;
    operators['@']=ExpressionTable::tAt;
    operators['|']=ExpressionTable::tPipe;
    return operators;
}

QHash<QString, int> initializeLongOperators()
{
    QHash<QString, int> longOperators;
    longOperators[QStringLiteral("->")]=ExpressionTable::tLambda;
    longOperators[QStringLiteral(":=")]=ExpressionTable::tAssig;
    longOperators[QStringLiteral("..")]=ExpressionTable::tLimits;
    longOperators[QStringLiteral("**")]=ExpressionTable::tPow;
    longOperators[QStringLiteral("<=")]=ExpressionTable::tLeq;
    longOperators[QStringLiteral(">=")]=ExpressionTable::tGeq;
    longOperators[QStringLiteral("!=")]=ExpressionTable::tNeq;
    return longOperators;
}

QHash<QChar, int> AbstractLexer::m_operators=initializeOperators();
QHash<QString, int> AbstractLexer::m_longOperators=initializeLongOperators();

AbstractLexer::AbstractLexer(const QString &source)
    : current(-1, 0), m_source(source), m_lines(0), m_openPr(0), m_openCb(0)
{}

AbstractLexer::~AbstractLexer() {}

void AbstractLexer::printQueue(const QQueue<TOKEN>& q) const
{
    QStringList res;
    foreach(const TOKEN& t, q)
    {
        if(m_longOperators.values().contains(t.type))  res += m_longOperators.key(t.type);
        else if(m_operators.values().contains(t.type)) res += m_operators.key(t.type);
        else res+= (t.val + ';' + QString::number(t.type) + error());
    }
    qDebug() << q.count() << ":::" << "(" << res.join(QStringLiteral("|")) << ")";
}

int AbstractLexer::lex()
{
    if(m_tokens.isEmpty())
        getToken();
    
//     printQueue(m_tokens);
    
    Q_ASSERT(!m_tokens.isEmpty());
    current=m_tokens.takeFirst();
    
    switch(current.type) {
        case ExpressionTable::tLpr:
            m_openPr++;
            break;
        case ExpressionTable::tRpr:
            m_openPr--;
            break;
        case ExpressionTable::tLcb:
            m_openCb++;
            break;
        case ExpressionTable::tRcb:
            m_openCb--;
            break;
        default:
            break;
    }
    
    return current.type;
}

bool AbstractLexer::isCompleteExpression(bool justempty)
{
    bool anycodetoken=false;
    for(int current=lex(); current>0 && !(justempty && anycodetoken); current=lex()) {
        anycodetoken |= current!=ExpressionTable::tComment;
    }
    return anycodetoken && isCompletelyRead();
}