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:
Pasha Bibko
2025-05-05 15:44:59 +01:00
parent a16ce34c8b
commit 616ed1ca21
26 changed files with 340 additions and 243 deletions

View File

@@ -1,27 +0,0 @@
#pragma once
#include <string>
namespace LX
{
enum class Color
{
BLACK = 0,
BLUE = 1,
GREEN = 2,
AQUA = 3,
RED = 4,
PURPLE = 5,
YELLOW = 6,
LIGHT_GRAY = 7,
LIGHT_BLUE = 9,
LIGHT_GREEN = 10,
LIGHT_AQUA = 11,
LIGHT_RED = 12,
LIGHT_PURPLE = 13,
LIGHT_YELLOW = 14,
WHITE = 15
};
void PrintStringAsColor(const std::string& str, Color c);
}

View File

@@ -1,5 +1,8 @@
#pragma once
#include <Error.h>
#include <filesystem>
#include <vector>
#include <string>
@@ -21,8 +24,15 @@ namespace std
namespace LX
{
// Error type with index and character to alert the user that LX does not understand that symbol //
struct InvalidCharInSource
struct InvalidCharInSource : public RuntimeError
{
GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
InvalidCharInSource(std::streamsize _col, std::streamsize _line, std::streamsize _index, char _invalid);
static std::string* s_Source;
static std::filesystem::path* s_SourceFile;
std::streamsize col;
std::streamsize line;
std::streamsize index;

View File

@@ -3,6 +3,8 @@
// Lexer foward declares fstream components so we can use them here //
#include <Lexer.h>
#include <Error.h>
#include <unordered_map>
#include <memory>
@@ -76,11 +78,19 @@ namespace LX::AST
namespace LX
{
// Thrown if there was an error during IR Generation //
struct IRGenerationError {};
CREATE_EMPTY_LX_ERROR_TYPE(IRGenerationError);
// Thrown if there was an unexpected (incorrect) token //
struct UnexpectedToken
struct UnexpectedToken : public RuntimeError
{
GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
UnexpectedToken(Token::TokenType _expected, std::string _override, Token _got);
//
static std::string* s_Source;
static std::filesystem::path* s_SourceFile;
// The token type that should be there //
Token::TokenType expected;
@@ -96,10 +106,10 @@ namespace LX
{
public:
// Error thrown if the user tried to create a variable that already existed //
struct __declspec(novtable) VariableAlreadyExists final {};
CREATE_EMPTY_LX_ERROR_TYPE(VariableAlreadyExists);
// Error thrown if user tries to access variable that does not exist //
struct __declspec(novtable) VariableDoesntExist final {};
CREATE_EMPTY_LX_ERROR_TYPE(VariableDoesntExist);
// Default constructor //
Scope()

View File

@@ -7,11 +7,6 @@
namespace LX
{
template<typename T, typename... Args>
// Helper function to throw given error if condition is true //
// Also micro-optimises to predict there is no errors thrown //
inline void ThrowIf(const bool condition, Args... args)
{ if (condition) [[unlikely]] { throw T(args...); }}
template<typename... Args>
// Helper function for logging //