readCDefines() - don't crash on invalid expressions, add better debugging info

This commit is contained in:
garak
2019-05-05 16:11:00 -04:00
committed by huderlem
parent 54e431617f
commit 675a064df6
5 changed files with 206 additions and 141 deletions

View File

@@ -2,6 +2,7 @@
#define PARSEUTIL_H
#include "heallocation.h"
#include "log.h"
#include <QString>
#include <QList>
@@ -10,6 +11,30 @@
enum TokenType {
Number,
Operator,
Error,
};
class DebugInfo {
public:
DebugInfo(QString file, QStringList lines) {
this->file = file;
this->lines = lines;
};
QString file;
int line;
bool err;
QStringList lines;
void error(QString expression, QString token) {
int lineNo = 0;
for (QString line_ : lines) {
lineNo++;
if (line_.contains(expression)) {
this->line = lineNo;
break;
}
}
logError(QString("%1:%2: unknown identifier found in expression: '%3'.").arg(file).arg(line).arg(token));
}
};
class Token {
@@ -22,6 +47,8 @@ public:
this->operatorPrecedence = -1;
} else if (type == "operator") {
this->operatorPrecedence = precedenceMap[value];
} else if (type == "error") {
this->type = TokenType::Error;
}
}
static QMap<QString, int> precedenceMap;
@@ -33,10 +60,11 @@ public:
class ParseUtil
{
public:
ParseUtil();
ParseUtil(QString, QString);
void strip_comment(QString*);
QList<QStringList>* parseAsm(QString);
int evaluateDefine(QString, QMap<QString, int>*);
DebugInfo *debug;
private:
QList<Token> tokenizeExpression(QString expression, QMap<QString, int>* knownIdentifiers);
QList<Token> generatePostfix(QList<Token> tokens);