mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Refactored error handling
Now uses base error class which has an abstract function for overiding how it is displayed to the console.
This commit is contained in:
@@ -4,36 +4,41 @@
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <llvm/Support/Error.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
#include <Console.h>
|
||||
#include <Parser.h>
|
||||
#include <Lexer.h>
|
||||
#include <Util.h>
|
||||
|
||||
#include <Error.h>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Different errors thrown by main //
|
||||
|
||||
struct IncorrectCommandLineArgs {};
|
||||
struct InvalidInputFilePath {};
|
||||
struct InvalidOutputFilePath {};
|
||||
struct InvalidLogFilePath {};
|
||||
|
||||
// Util function for getting a line of the source at a given index (used for errors) //
|
||||
static std::string GetLineAtIndexOf(const std::string src, const std::streamsize index)
|
||||
struct InvalidFilePath : public RuntimeError
|
||||
{
|
||||
// Finds the start of the line //
|
||||
size_t start = src.rfind('\n', index);
|
||||
if (start == std::string::npos) { start = 0; } // None means first line
|
||||
else { start = start + 1; } // Skips new line char
|
||||
GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
|
||||
|
||||
// Finds the end of the line //
|
||||
size_t end = src.find('\n', index);
|
||||
if (end == std::string::npos) { end = src.size(); } // None means last line
|
||||
InvalidFilePath(const std::string& _name, const std::filesystem::path& _path)
|
||||
: name(_name), path(_path)
|
||||
{}
|
||||
|
||||
// Returns the string between start and end //
|
||||
return src.substr(start, end - start);
|
||||
std::string name;
|
||||
std::filesystem::path path;
|
||||
};
|
||||
|
||||
void InvalidFilePath::PrintToConsole() const
|
||||
{
|
||||
// Tells the user the input file could not be found and how to fix the issue //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid " << name << ": ";
|
||||
LX::PrintStringAsColor(path.string().c_str(), LX::Color::WHITE);
|
||||
std::cout << "\n\nMake sure the file exists and the process has the correct path to the file\n";
|
||||
}
|
||||
|
||||
const char* InvalidFilePath::ErrorType() const
|
||||
{
|
||||
return "Invalid File Path";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,9 +63,9 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
outPath = a_outPath;
|
||||
|
||||
// Checks the input file exists and opens it //
|
||||
LX::ThrowIf<LX::InvalidInputFilePath>(std::filesystem::exists(inpPath) == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(std::filesystem::exists(inpPath) == false, "input file path", inpPath);
|
||||
std::ifstream inpFile(inpPath, std::ios::binary | std::ios::ate); // Opens in binary at the end for microptimisation //
|
||||
LX::ThrowIf<LX::InvalidInputFilePath>(inpFile.is_open() == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(inpFile.is_open() == false, "input file path", inpPath);
|
||||
|
||||
// Copies the file into the string //
|
||||
const std::streamsize len = inpFile.tellg(); // Gets length of file because it was opened at the end
|
||||
@@ -70,7 +75,7 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
|
||||
// Opens / Creates the output file //
|
||||
std::ofstream outFile(outPath);
|
||||
LX::ThrowIf<LX::InvalidOutputFilePath>(outFile.is_open() == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(outFile.is_open() == false, "output file path", outPath);
|
||||
outFile.close(); // Opened just to check we can
|
||||
|
||||
// Opens the log file (if there is one specified //
|
||||
@@ -78,147 +83,49 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
{
|
||||
logPath = a_logPath;
|
||||
log = std::make_unique<std::ofstream>(logPath);
|
||||
LX::ThrowIf<LX::InvalidLogFilePath>(log->is_open() == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(log->is_open() == false, "log file path", logPath);
|
||||
}
|
||||
|
||||
// Prints the full paths to the console to let the user know compiling is being done //
|
||||
std::cout << std::filesystem::absolute(inpPath) << " -> " << std::filesystem::absolute(outPath) << std::endl;
|
||||
|
||||
// Create tokens out of the input file //
|
||||
LX::InvalidCharInSource::s_Source = &contents;
|
||||
LX::InvalidCharInSource::s_SourceFile = &inpPath;
|
||||
std::vector<LX::Token>tokens = LX::LexicalAnalyze(contents, len, log.get());
|
||||
LX::SafeFlush(log.get());
|
||||
std::cout << "\t|- Created tokens" << std::endl;
|
||||
|
||||
// Turns the tokens into an AST //
|
||||
LX::UnexpectedToken::s_Source = &contents;
|
||||
LX::UnexpectedToken::s_SourceFile = &inpPath;
|
||||
|
||||
LX::FileAST AST = LX::TurnTokensIntoAbstractSyntaxTree(tokens, log.get());
|
||||
LX::SafeFlush(log.get());
|
||||
std::cout << "\t|- Created AST" << std::endl;
|
||||
|
||||
// Turns the AST into LLVM IR //
|
||||
LX::GenerateIR(AST, inpPath.filename().string(), outPath);
|
||||
LX::SafeFlush(log.get());
|
||||
std::cout << "\t|- Generated LLVM IR" << std::endl;
|
||||
|
||||
// Returns success
|
||||
return 0;
|
||||
}
|
||||
|
||||
catch (LX::InvalidInputFilePath)
|
||||
catch(LX::RuntimeError& e)
|
||||
{
|
||||
// Tells the user the input file could not be found and how to fix the issue //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid file path: ";
|
||||
LX::PrintStringAsColor(inpPath.string(), LX::Color::WHITE);
|
||||
std::cout << "\n\nMake sure the file exists and the process has the correct path to the file\n";
|
||||
|
||||
// Returns Exit id of 2 so other process can be alerted of the error //
|
||||
return 2;
|
||||
}
|
||||
|
||||
catch (LX::InvalidOutputFilePath)
|
||||
{
|
||||
// Tells the user that the output file could not be found/created //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid file path: ";
|
||||
LX::PrintStringAsColor(outPath.string(), LX::Color::WHITE);
|
||||
std::cout << "\n\nThe file could not be created or written to.\n";
|
||||
std::cout << "Check it is a valid file path and it has the permissions to modify the file\n";
|
||||
|
||||
// Returns Exit id of 3 so other process can be alerted of the error //
|
||||
return 3;
|
||||
}
|
||||
|
||||
catch (LX::InvalidLogFilePath)
|
||||
{
|
||||
// Tells the user that the log file cound not be found/created //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid file path: ";
|
||||
LX::PrintStringAsColor(logPath.string(), LX::Color::WHITE);
|
||||
std::cout << "\n\nThe file could not be created or written to.\n";
|
||||
std::cout << "Check it is a valid file path and it has the permissions to modify the file\n";
|
||||
|
||||
// Returns Exit id of 4 so other process can be alerted of the error //
|
||||
return 4;
|
||||
}
|
||||
|
||||
catch (LX::InvalidCharInSource& e)
|
||||
{
|
||||
// Adds the error to the log and closes it to save all the output //
|
||||
LX::SafeLog(log.get(), LX::LOG_BREAK, "Error thrown from Lexer:\n\tInvalid character: ", e.invalid, " on line: ", e.line, LX::LOG_BREAK);
|
||||
// Closes the log to save everything outputted to it after logging the error //
|
||||
LX::SafeLog(log.get(), LX::LOG_BREAK, "Error thrown of type: ", e.ErrorType(), LX::LOG_BREAK);
|
||||
if (log != nullptr) { log->close(); }
|
||||
|
||||
// Calculates the length of the line number in the console so it is formatted correctly //
|
||||
std::ostringstream oss;
|
||||
oss << std::setw(3) << e.line;
|
||||
size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
|
||||
|
||||
// Gets the line of the error //
|
||||
std::string line = LX::GetLineAtIndexOf(contents, e.index);
|
||||
|
||||
// Prints the error with the relevant information to the console //
|
||||
std::cout << "\n";
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid character found in ";
|
||||
LX::PrintStringAsColor(inpPath.filename().string(), LX::Color::WHITE);
|
||||
std::cout << " {";
|
||||
LX::PrintStringAsColor(std::string(1, e.invalid), LX::Color::LIGHT_RED);
|
||||
std::cout << "}:\n";
|
||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.line << " | " << line << "\n";
|
||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.col - 1) << "";
|
||||
LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
|
||||
// Logs the errors type to the console if built as Debug //
|
||||
#ifdef _DEBUG
|
||||
std::cout << "LX::RuntimeError thrown of type: ";
|
||||
LX::PrintStringAsColor(e.ErrorType(), LX::Color::WHITE);
|
||||
std::cout << "\n";
|
||||
#endif // _DEBUG
|
||||
|
||||
// Returns Exit id of 5 so other process can be alerted of the error //
|
||||
return 5;
|
||||
}
|
||||
|
||||
catch (LX::UnexpectedToken& e)
|
||||
{
|
||||
// Calculates the length of the line number in the console so it is formatted correctly //
|
||||
std::ostringstream oss;
|
||||
oss << std::setw(3) << e.got.line;
|
||||
size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
|
||||
|
||||
// Gets the line of the error //
|
||||
std::string line = LX::GetLineAtIndexOf(contents, e.got.index);
|
||||
|
||||
// Prints the error to the console with the relevant info //
|
||||
std::cout << "\n";
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Incorrect syntax in ";
|
||||
LX::PrintStringAsColor(inpPath.filename().string(), LX::Color::WHITE);
|
||||
std::cout << ", found ";
|
||||
LX::PrintStringAsColor(LX::ToString(e.got.type), LX::Color::WHITE);
|
||||
std::cout << " expected: ";
|
||||
|
||||
// Allows the error to have a custom type that is printed to the console //
|
||||
if (e.expected == LX::Token::UNDEFINED) { LX::PrintStringAsColor(e.override, LX::Color::WHITE); }
|
||||
else { LX::PrintStringAsColor(LX::ToString(e.expected), LX::Color::WHITE); }
|
||||
std::cout << "\n";
|
||||
|
||||
// Prints the code with the error to the console //
|
||||
std::string errorSquiggle(e.got.length, '~');
|
||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.got.line << " | " << line << "\n";
|
||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.got.column) << "";
|
||||
LX::PrintStringAsColor(errorSquiggle, LX::Color::LIGHT_RED);
|
||||
std::cout << "\n";
|
||||
|
||||
// Returns Exit id of 6 so other process can be alerted of the error //
|
||||
return 6;
|
||||
}
|
||||
|
||||
catch (LX::Scope::VariableAlreadyExists)
|
||||
{
|
||||
std::cout << "Tried to create a variable that already exists\n";
|
||||
|
||||
return 7;
|
||||
}
|
||||
|
||||
catch (LX::Scope::VariableDoesntExist)
|
||||
{
|
||||
std::cout << "Tried to access a variable that doesn't exist\n";
|
||||
|
||||
return 8;
|
||||
// Prints the error to the console and returns //
|
||||
e.PrintToConsole();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Catches any std errors, there should be none //
|
||||
@@ -236,19 +143,6 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Catches any LLVM errors, there should be none //
|
||||
catch (llvm::Error& e)
|
||||
{
|
||||
// Closes the log if it is open to get as much info as possible //
|
||||
if (log != nullptr) { log->close(); }
|
||||
|
||||
// Prints the LLVM error to the console //
|
||||
std::cout << "A LLVM error occured. Please report this on the github page.\n" << std::endl;
|
||||
|
||||
// Exit code -1 means an undefined error //
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Catches errors that i was too lazy to code //
|
||||
catch (int)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user