Use QFile/QString for C parser files and paths

This commit is contained in:
GriffinR 2024-12-11 09:00:51 -05:00
parent 11dd7306d3
commit 6b70abaaf0
5 changed files with 14 additions and 36 deletions

View file

@ -4,6 +4,7 @@
#include <cstdint>
#include <string>
#include <vector>
#include <QString>
namespace fex
{
@ -89,9 +90,7 @@ namespace fex
Lexer() = default;
~Lexer() = default;
std::vector<Token> LexFile(const std::string &path);
std::vector<Token> LexString(const std::string &data);
void LexFileDumpTokens(const std::string &path, const std::string &out);
std::vector<Token> LexFile(const QString &path);
private:
std::vector<Token> Lex();

View file

@ -21,7 +21,7 @@ namespace fex
std::vector<Array> ParseTopLevelArrays(std::vector<Token> tokens);
std::map<std::string, ArrayValue> ParseTopLevelObjects(std::vector<Token> tokens);
std::map<std::string, int> ReadDefines(const std::string &filename, std::vector<std::string> matching);
std::map<std::string, int> ReadDefines(const QString &filename, std::vector<std::string> matching);
private:
int EvaluateExpression(std::vector<Token> tokens);

View file

@ -596,7 +596,7 @@ bool ParseUtil::gameStringToBool(QString gameString, bool * ok) {
QMap<QString, QHash<QString, QString>> ParseUtil::readCStructs(const QString &filename, const QString &label, const QHash<int, QString> memberMap) {
QString filePath = this->root + "/" + filename;
auto cParser = fex::Parser();
auto tokens = fex::Lexer().LexFile(filePath.toStdString());
auto tokens = fex::Lexer().LexFile(filePath);
auto structs = cParser.ParseTopLevelObjects(tokens);
QMap<QString, QHash<QString, QString>> structMaps;
for (auto it = structs.begin(); it != structs.end(); it++) {

View file

@ -3,6 +3,7 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <QFile>
namespace fex
{
@ -155,48 +156,26 @@ namespace fex
return Token(Token::Type::kDefine, filename_, line_number_);
}
std::vector<Token> Lexer::LexString(const std::string &data)
std::vector<Token> Lexer::LexFile(const QString &path)
{
filename_ = "string literal";
line_number_ = 1;
index_ = 0;
data_ = data;
return Lex();
}
std::vector<Token> Lexer::LexFile(const std::string &path)
{
filename_ = path;
filename_ = path.toStdString();
line_number_ = 1;
std::ifstream file;
file.open(path);
// Note: Using QFile instead of ifstream to handle encoding differences between platforms
// (specifically to handle accented characters on Windows)
QFile file(path);
file.open(QIODevice::ReadOnly);
std::stringstream stream;
stream << file.rdbuf();
const QByteArray data = file.readAll();
index_ = 0;
data_ = stream.str();
data_ = data.toStdString();
file.close();
return Lex();
}
void Lexer::LexFileDumpTokens(const std::string &path, const std::string &out)
{
std::ofstream file;
file.open(out);
for (Token token : LexFile(path))
{
file << token.ToString() << std::endl;
}
file.close();
}
std::vector<Token> Lexer::Lex()
{
std::vector<Token> tokens;

View file

@ -337,7 +337,7 @@ namespace fex
return DefineStatement(identifer, value);
}
std::map<std::string, int> Parser::ReadDefines(const std::string &filename, std::vector<std::string> matching)
std::map<std::string, int> Parser::ReadDefines(const QString &filename, std::vector<std::string> matching)
{
std::map<std::string, int> out;