Files
ImHex-Patterns/tests/includes/source/main.cpp

74 lines
2.6 KiB
C++

#include <pl.hpp>
#include <pl/helpers/file.hpp>
#include <pl/core/errors/preprocessor_errors.hpp>
#include <pl/core/errors/evaluator_errors.hpp>
#include <fmt/format.h>
#include <cstdlib>
#define EXIT_SKIP 77
int main(int argc, char **argv) {
// Any number of arguments other than 4 are invalid
if (argc != 4)
return EXIT_FAILURE;
// Extract command line arguments
const std::string includeName = argv[1];
const std::fs::path includeFilePath = argv[2];
const std::fs::path includePath = argv[3];
fmt::print("Running test {} on test file {}\n", includeName, includeFilePath.filename().string());
// Open pattern file
pl::hlp::fs::File patternFile(includeFilePath, pl::hlp::fs::File::Mode::Read);
if (!patternFile.isValid())
return EXIT_FAILURE;
// Setup Pattern Language Runtime
pl::PatternLanguage runtime;
{
constexpr auto DummyPragmaHandler = [](const auto&, const auto&){
pl::core::err::M0006.throwError("Include files should never use this pragma!");
return false;
};
runtime.setDataSource([&](pl::u64 address, pl::u8 *data, size_t size) {
pl::core::err::E0011.throwError("Include files should never read from memory directly!");
}, 0x00, 0x100000);
runtime.setDangerousFunctionCallHandler([]{ return true; });
runtime.setIncludePaths({ includePath });
runtime.addPragma("endian", DummyPragmaHandler);
runtime.addPragma("MIME", DummyPragmaHandler);
runtime.addPragma("base_address", DummyPragmaHandler);
runtime.addPragma("eval_depth", DummyPragmaHandler);
runtime.addPragma("array_limit", DummyPragmaHandler);
runtime.addPragma("pattern_limit", DummyPragmaHandler);
}
// Execute pattern
if (!runtime.executeString(patternFile.readString())) {
fmt::print("Error during execution!\n");
if (const auto &hardError = runtime.getError(); hardError.has_value())
fmt::print("Hard error: {}:{} - {}\n\n", hardError->line, hardError->column, hardError->message);
for (const auto &[level, message] : runtime.getConsoleLog()) {
switch (level) {
using enum pl::core::LogConsole::Level;
case Debug: fmt::print(" [DEBUG] "); break;
case Info: fmt::print(" [INFO] "); break;
case Warning: fmt::print(" [WARN] "); break;
case Error: fmt::print(" [ERROR] "); break;
}
fmt::print("{}\n", message);
}
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}