File indexing completed on 2024-04-28 03:40:38

0001 /*************************************************************************************
0002  *  Copyright (C) 2008 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "abstractlexer.h"
0020 #include "expressionparser.h"
0021 #include <QDebug>
0022 
0023 QHash<QChar, int> initializeOperators()
0024 {
0025     QHash<QChar, int> operators;
0026     operators['+']=ExpressionTable::tAdd;
0027     operators['-']=ExpressionTable::tSub;
0028     operators['*']=ExpressionTable::tMul;
0029     operators['/']=ExpressionTable::tDiv;
0030     operators['^']=ExpressionTable::tPow;
0031     operators['(']=ExpressionTable::tLpr;
0032     operators[')']=ExpressionTable::tRpr;
0033     operators[',']=ExpressionTable::tComa;
0034     operators['{']=ExpressionTable::tLcb;
0035     operators['}']=ExpressionTable::tRcb;
0036     operators['[']=ExpressionTable::tLsp;
0037     operators[']']=ExpressionTable::tRsp;
0038     operators['?']=ExpressionTable::tQm;
0039     operators[':']=ExpressionTable::tColon;
0040     operators['=']=ExpressionTable::tEq;
0041     operators['<']=ExpressionTable::tLt;
0042     operators['>']=ExpressionTable::tGt;
0043     operators['@']=ExpressionTable::tAt;
0044     operators['|']=ExpressionTable::tPipe;
0045     return operators;
0046 }
0047 
0048 QHash<QString, int> initializeLongOperators()
0049 {
0050     QHash<QString, int> longOperators;
0051     longOperators[QStringLiteral("->")]=ExpressionTable::tLambda;
0052     longOperators[QStringLiteral(":=")]=ExpressionTable::tAssig;
0053     longOperators[QStringLiteral("..")]=ExpressionTable::tLimits;
0054     longOperators[QStringLiteral("**")]=ExpressionTable::tPow;
0055     longOperators[QStringLiteral("<=")]=ExpressionTable::tLeq;
0056     longOperators[QStringLiteral(">=")]=ExpressionTable::tGeq;
0057     longOperators[QStringLiteral("!=")]=ExpressionTable::tNeq;
0058     return longOperators;
0059 }
0060 
0061 QHash<QChar, int> AbstractLexer::m_operators=initializeOperators();
0062 QHash<QString, int> AbstractLexer::m_longOperators=initializeLongOperators();
0063 
0064 AbstractLexer::AbstractLexer(const QString &source)
0065     : current(-1, 0), m_source(source), m_lines(0), m_openPr(0), m_openCb(0)
0066 {}
0067 
0068 AbstractLexer::~AbstractLexer() {}
0069 
0070 void AbstractLexer::printQueue(const QQueue<TOKEN>& q) const
0071 {
0072     QStringList res;
0073     foreach(const TOKEN& t, q)
0074     {
0075         if(m_longOperators.values().contains(t.type))  res += m_longOperators.key(t.type);
0076         else if(m_operators.values().contains(t.type)) res += m_operators.key(t.type);
0077         else res+= (t.val + ';' + QString::number(t.type) + error());
0078     }
0079     qDebug() << q.count() << ":::" << "(" << res.join(QStringLiteral("|")) << ")";
0080 }
0081 
0082 int AbstractLexer::lex()
0083 {
0084     if(m_tokens.isEmpty())
0085         getToken();
0086     
0087 //     printQueue(m_tokens);
0088     
0089     Q_ASSERT(!m_tokens.isEmpty());
0090     current=m_tokens.takeFirst();
0091     
0092     switch(current.type) {
0093         case ExpressionTable::tLpr:
0094             m_openPr++;
0095             break;
0096         case ExpressionTable::tRpr:
0097             m_openPr--;
0098             break;
0099         case ExpressionTable::tLcb:
0100             m_openCb++;
0101             break;
0102         case ExpressionTable::tRcb:
0103             m_openCb--;
0104             break;
0105         default:
0106             break;
0107     }
0108     
0109     return current.type;
0110 }
0111 
0112 bool AbstractLexer::isCompleteExpression(bool justempty)
0113 {
0114     bool anycodetoken=false;
0115     for(int current=lex(); current>0 && !(justempty && anycodetoken); current=lex()) {
0116         anycodetoken |= current!=ExpressionTable::tComment;
0117     }
0118     return anycodetoken && isCompletelyRead();
0119 }