Made Lexer errors fully hidden in global scope

Also improved general ease of use with debugging use __debugbreak when an error is thrown.

NOTE: Parser errors currently crash the program
This commit is contained in:
Pasha Bibko
2025-05-07 16:31:44 +01:00
parent 0c34e7174e
commit 4e78a9f6ae
11 changed files with 161 additions and 103 deletions

View File

@@ -159,6 +159,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Console.cpp" />
<ClCompile Include="src\Error.cpp" />
<ClCompile Include="src\Logger.cpp" />
<ClCompile Include="src\pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

View File

@@ -37,5 +37,8 @@
<ClCompile Include="src\Console.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Error.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -2,14 +2,41 @@ namespace LX
{
// Base error class for all LX thrown errors //
// Holds nothing apart from the v-table //
struct RuntimeError
struct COMMON_API RuntimeError
{
// Default constructor which throws a breakpoint on being created //
RuntimeError();
// Prints the error to the console //
// Include Common/Console.h for printing util functions //
virtual void PrintToConsole() const = 0;
// Returns a C-String of the type that was thrown //
virtual const char* ErrorType() const = 0;
// Virtual destructor because of polymorphism //
virtual ~RuntimeError() = default;
};
// --- Common errors that can be thrown --- //
// Error thrown when there is an invalid file path //
struct COMMON_API InvalidFilePath : public RuntimeError
{
// Constructor to turn the C++ types to C to expose them in DLL //
InvalidFilePath(const std::string& _name, const std::filesystem::path& path);
// Prints the error to the console //
void PrintToConsole() const;
// Returns the error as c-string //
const char* ErrorType() const;
// Name of the file that is invalid (used for console output) //
const char* name;
// The location of the file (used for console output) //
const char* fileLocation;
};
}

47
Common/src/Error.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include <LX-Common.h>
namespace LX
{
RuntimeError::RuntimeError()
{
// Debuggers can only be attached in Debug configuration so this code is useless in Release/Distribution builds //
#ifdef _DEBUG
// Checks a debugger is present before throwing a breakpoint //
if (IsDebuggerPresent()) { __debugbreak(); }
#endif
}
InvalidFilePath::InvalidFilePath(const std::string& _name, const std::filesystem::path& path)
: name(nullptr), fileLocation(nullptr)
{
// Stores the actual strings in static memory //
static std::string s_ErrorPath;
static std::string s_ErrorName;
s_ErrorPath = path.string();
s_ErrorName = _name;
// Assigns a pointer to the c-string of the strings //
// Done like this because of how DLLs work //
fileLocation = s_ErrorPath.c_str();
name = s_ErrorName.c_str();
}
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(fileLocation, 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";
}
}