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
|
|
|
|
};
|
|
|
|
|
2021-02-18 00:20:14 +00:00
|
|
|
class ParseUtil {
|
2018-09-25 01:04:42 +01:00
|
|
|
public:
|
2021-02-18 00:20:14 +00:00
|
|
|
ParseUtil(){};
|
|
|
|
void set_root(const QString& dir);
|
|
|
|
static QString readTextFile(const QString& path);
|
|
|
|
static int textFileLineCount(const QString& path);
|
|
|
|
QList<QStringList> parseAsm(const QString& filename);
|
2021-02-16 12:15:47 +00:00
|
|
|
int evaluateDefine(const QString&, const QMap<QString, int>&);
|
2021-02-18 00:20:14 +00:00
|
|
|
QStringList readCArray(const QString& text, const QString& label);
|
|
|
|
QMap<QString, QString> readNamedIndexCArray(const QString& text, const QString& label);
|
|
|
|
QString readCIncbin(const QString& text, const QString& label);
|
|
|
|
QMap<QString, int> readCDefines(const QString& filename, const QStringList& prefixes, QMap<QString, int> = {});
|
|
|
|
QStringList readCDefinesSorted(const QString&, const QStringList&, const QMap<QString, int>& = {});
|
2021-02-16 12:15:47 +00:00
|
|
|
QList<QStringList> getLabelMacros(const QList<QStringList>&, const QString&);
|
|
|
|
QStringList getLabelValues(const QList<QStringList>&, const QString&);
|
2021-02-18 00:20:14 +00:00
|
|
|
bool tryParseJsonFile(QJsonDocument* out, const QString& filepath);
|
|
|
|
bool ensureFieldsExist(const QJsonObject& obj, const 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.
|
2021-02-18 00:20:14 +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);
|
2021-02-16 12:15:47 +00:00
|
|
|
static QString removeStringLiterals(QString text);
|
2021-02-18 00:20:14 +00:00
|
|
|
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;
|
2021-02-18 00:20:14 +00:00
|
|
|
QList<Token> tokenizeExpression(QString expression, const QMap<QString, int>& knownIdentifiers);
|
|
|
|
QList<Token> generatePostfix(const QList<Token>& tokens);
|
|
|
|
int evaluatePostfix(const QList<Token>& postfix);
|
|
|
|
void error(const QString& message, const QString& expression);
|
2018-09-25 01:04:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PARSEUTIL_H
|