2020-09-13 23:37:55 +01:00
|
|
|
#pragma once
|
2018-09-25 01:04:42 +01:00
|
|
|
#ifndef PARSEUTIL_H
|
|
|
|
#define PARSEUTIL_H
|
|
|
|
|
2018-09-25 01:12:29 +01:00
|
|
|
#include "heallocation.h"
|
2019-05-05 21:11:00 +01:00
|
|
|
#include "log.h"
|
2018-09-25 01:04:42 +01:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMap>
|
2019-05-06 17:42:09 +01:00
|
|
|
#include <QRegularExpression>
|
2018-09-25 01:04:42 +01:00
|
|
|
|
2019-09-21 22:06:04 +01:00
|
|
|
enum TokenClass {
|
2018-09-25 01:04:42 +01:00
|
|
|
Number,
|
|
|
|
Operator,
|
2019-05-05 21:11:00 +01:00
|
|
|
Error,
|
|
|
|
};
|
|
|
|
|
2018-09-25 01:04:42 +01:00
|
|
|
class Token {
|
|
|
|
public:
|
|
|
|
Token(QString value = "", QString type = "") {
|
|
|
|
this->value = value;
|
2019-09-21 22:06:04 +01:00
|
|
|
this->type = TokenClass::Operator;
|
2018-09-25 01:04:42 +01:00
|
|
|
if (type == "decimal" || type == "hex") {
|
2019-09-21 22:06:04 +01:00
|
|
|
this->type = TokenClass::Number;
|
2018-09-25 01:04:42 +01:00
|
|
|
this->operatorPrecedence = -1;
|
|
|
|
} else if (type == "operator") {
|
|
|
|
this->operatorPrecedence = precedenceMap[value];
|
2019-05-05 21:11:00 +01:00
|
|
|
} else if (type == "error") {
|
2019-09-21 22:06:04 +01:00
|
|
|
this->type = TokenClass::Error;
|
2018-09-25 01:04:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static QMap<QString, int> precedenceMap;
|
|
|
|
QString value;
|
2019-09-21 22:06:04 +01:00
|
|
|
TokenClass type;
|
2018-09-25 01:04:42 +01:00
|
|
|
int operatorPrecedence; // only relevant for operator tokens
|
|
|
|
};
|
|
|
|
|
|
|
|
class ParseUtil
|
|
|
|
{
|
|
|
|
public:
|
2019-05-06 17:42:09 +01:00
|
|
|
ParseUtil();
|
|
|
|
void set_root(QString);
|
2020-11-22 06:04:46 +00:00
|
|
|
static QString readTextFile(QString);
|
2021-02-03 15:24:59 +00:00
|
|
|
static int textFileLineCount(const QString &path);
|
2018-09-25 01:04:42 +01:00
|
|
|
void strip_comment(QString*);
|
2021-02-16 11:15:54 +00:00
|
|
|
QList<QStringList> parseAsm(const QString &filename);
|
2018-09-25 01:04:42 +01:00
|
|
|
int evaluateDefine(QString, QMap<QString, int>*);
|
2019-05-06 17:42:09 +01:00
|
|
|
QStringList readCArray(QString text, QString label);
|
|
|
|
QMap<QString, QString> readNamedIndexCArray(QString text, QString label);
|
|
|
|
QString readCIncbin(QString text, QString label);
|
2020-07-13 21:36:43 +01:00
|
|
|
QMap<QString, int> readCDefines(QString filename, QStringList prefixes, QMap<QString, int> = QMap<QString, int>());
|
|
|
|
void readCDefinesSorted(QString, QStringList, QStringList*, QMap<QString, int> = QMap<QString, int>());
|
2021-02-16 11:15:54 +00:00
|
|
|
QList<QStringList> getLabelMacros(const QList<QStringList> &, const QString &);
|
|
|
|
QStringList getLabelValues(const QList<QStringList> &, const QString &);
|
2019-05-06 17:42:09 +01:00
|
|
|
bool tryParseJsonFile(QJsonDocument *out, QString filepath);
|
2020-02-12 00:34:08 +00:00
|
|
|
bool ensureFieldsExist(QJsonObject obj, QList<QString> fields);
|
2019-05-06 17:42:09 +01:00
|
|
|
|
2020-11-22 06:04:46 +00:00
|
|
|
// Returns the 1-indexed line number for the definition of scriptLabel in the scripts file at filePath.
|
|
|
|
// Returns 0 if a definition for scriptLabel cannot be found.
|
2020-11-26 11:09:58 +00:00
|
|
|
static int getScriptLineNumber(const QString &filePath, const QString &scriptLabel);
|
|
|
|
static int getRawScriptLineNumber(QString text, const QString &scriptLabel);
|
|
|
|
static int getPoryScriptLineNumber(QString text, const QString &scriptLabel);
|
|
|
|
static QString &removeStringLiterals(QString &text);
|
|
|
|
static QString &removeLineComments(QString &text, const QString &commentSymbol);
|
|
|
|
static QString &removeLineComments(QString &text, const QStringList &commentSymbols);
|
2020-11-22 06:04:46 +00:00
|
|
|
|
2020-12-13 09:00:00 +00:00
|
|
|
static QStringList splitShellCommand(QStringView command);
|
|
|
|
|
2018-09-25 01:04:42 +01:00
|
|
|
private:
|
2019-05-06 17:42:09 +01:00
|
|
|
QString root;
|
|
|
|
QString text;
|
|
|
|
QString file;
|
2018-09-25 01:04:42 +01:00
|
|
|
QList<Token> tokenizeExpression(QString expression, QMap<QString, int>* knownIdentifiers);
|
|
|
|
QList<Token> generatePostfix(QList<Token> tokens);
|
|
|
|
int evaluatePostfix(QList<Token> postfix);
|
2019-05-06 17:42:09 +01:00
|
|
|
void error(QString message, QString expression);
|
2018-09-25 01:04:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PARSEUTIL_H
|