Added lexer tests
This commit is contained in:
102
Common/modules/File.h
Normal file
102
Common/modules/File.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
// Error returned when Util::ReadFile runs into errors //
|
||||
struct FileReadError final
|
||||
{
|
||||
// Different reasons why the error can occur //
|
||||
enum Reason
|
||||
{
|
||||
FileNotFound,
|
||||
PermissionDenied,
|
||||
NotAFile
|
||||
};
|
||||
|
||||
// Constructor to pass arguments to the struct //
|
||||
FileReadError(const std::filesystem::path& _path, Reason _reason)
|
||||
: path(_path), reason(_reason)
|
||||
{}
|
||||
|
||||
// Error information //
|
||||
const std::filesystem::path path;
|
||||
const Reason reason;
|
||||
|
||||
// Turns the error into a c-string //
|
||||
inline static const char* const ReasonStr(Reason reason)
|
||||
{
|
||||
static const char* reasons[] =
|
||||
{
|
||||
"File cannot be found",
|
||||
"File reading permissions are denied",
|
||||
"Not a file"
|
||||
};
|
||||
|
||||
return reasons[reason];
|
||||
}
|
||||
};
|
||||
|
||||
// Util function to read a file as quick as possible with error handling //
|
||||
inline ReturnVal<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)
|
||||
{
|
||||
// Checks the file exists //
|
||||
if (!std::filesystem::exists(filepath))
|
||||
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::FileNotFound);
|
||||
|
||||
// Checks it is a regular file //
|
||||
if (!std::filesystem::is_regular_file(filepath))
|
||||
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::NotAFile);
|
||||
|
||||
// Checks it can open the file //
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file)
|
||||
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::PermissionDenied);
|
||||
|
||||
// Copies the file to the output string //
|
||||
const std::streamsize len = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::string contents(len, '\0');
|
||||
file.read(&contents[0], len);
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
// Struct to hold a position within a file //
|
||||
struct FileLocation
|
||||
{
|
||||
unsigned short col;
|
||||
unsigned short line;
|
||||
};
|
||||
|
||||
// Finds the location of a given index within a file //
|
||||
inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, uint32_t index)
|
||||
{
|
||||
// Resets location //
|
||||
location.line = 1;
|
||||
location.col = 1;
|
||||
|
||||
// Returns false if outside the bounds //
|
||||
if (index < 0 || index > file.length())
|
||||
return false;
|
||||
|
||||
// Finds the location //
|
||||
uint32_t localIndex = 0;
|
||||
while (localIndex != index)
|
||||
{
|
||||
if (file[localIndex] == '\n')
|
||||
{
|
||||
location.line += 1;
|
||||
location.col = 0;
|
||||
}
|
||||
|
||||
location.col++;
|
||||
localIndex++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
188
Common/modules/IO.h
Normal file
188
Common/modules/IO.h
Normal file
@@ -0,0 +1,188 @@
|
||||
#pragma once
|
||||
|
||||
#include <modules/OS.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
// Enum to translate to the Win32 code for the colors //
|
||||
enum Color
|
||||
{
|
||||
DEFAULT = 0x07,
|
||||
|
||||
BLACK = 0x00,
|
||||
BLUE = 0x01,
|
||||
GREEN = 0x02,
|
||||
AQUA = 0x03,
|
||||
RED = 0x04,
|
||||
PURPLE = 0x05,
|
||||
YELLOW = 0x06,
|
||||
LIGHT_GRAY = 0x07,
|
||||
LIGHT_BLUE = 0x09,
|
||||
LIGHT_GREEN = 0x0a,
|
||||
LIGHT_AQUA = 0x0b,
|
||||
LIGHT_RED = 0x0c,
|
||||
LIGHT_PURPLE = 0x0d,
|
||||
LIGHT_YELLOW = 0x0e,
|
||||
WHITE = 0x0f
|
||||
};
|
||||
}
|
||||
|
||||
namespace LXC::Internal
|
||||
{
|
||||
// Checks if a type can be outputted to std::ostream //
|
||||
template<typename T> concept Logable = requires(std::ostream & os, T t)
|
||||
{
|
||||
// I have no idea what this part does at all //
|
||||
{ os << t } -> std::same_as<std::ostream&>;
|
||||
};
|
||||
|
||||
// Checks if a list of types can be outputted to std::ostream //
|
||||
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
||||
|
||||
// Checks if the type has a custom log method //
|
||||
template <typename T> concept HasLogStrFunc = requires(T obj)
|
||||
{
|
||||
// Checks the type has a function LogStr that returns a string //
|
||||
{ obj.LogStr() } -> std::same_as<std::string>;
|
||||
};
|
||||
|
||||
// Returns a reference to the log file //
|
||||
inline std::ofstream& Log()
|
||||
{
|
||||
static std::ofstream sLog;
|
||||
return sLog;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
// Base function for all derived Print() functions //
|
||||
template<Util::Color col, bool newLine, typename... Args>
|
||||
inline void WriteImpl(std::ostream& os, Args&&... args)
|
||||
{
|
||||
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if constexpr (col != Util::Color::DEFAULT)
|
||||
SetConsoleTextAttribute(hConsole, static_cast<WORD>(col));
|
||||
|
||||
(os << ... << std::forward<Args>(args));
|
||||
|
||||
if constexpr (newLine)
|
||||
os << std::endl;
|
||||
|
||||
if constexpr (col != Util::Color::DEFAULT)
|
||||
SetConsoleTextAttribute(hConsole, static_cast<WORD>(Util::Color::DEFAULT));
|
||||
}
|
||||
|
||||
#elif defined(__unix__)
|
||||
|
||||
static const char* GetAnsiCode(Util::Color color)
|
||||
{
|
||||
switch (color)
|
||||
{
|
||||
case Util::Color::BLACK: return "\x1b[30m";
|
||||
case Util::Color::BLUE: return "\x1b[34m";
|
||||
case Util::Color::GREEN: return "\x1b[32m";
|
||||
case Util::Color::AQUA: return "\x1b[36m";
|
||||
case Util::Color::RED: return "\x1b[31m";
|
||||
case Util::Color::PURPLE: return "\x1b[35m";
|
||||
case Util::Color::YELLOW: return "\x1b[33m";
|
||||
case Util::Color::LIGHT_GRAY: return "\x1b[0m";
|
||||
case Util::Color::LIGHT_BLUE: return "\x1b[94m";
|
||||
case Util::Color::LIGHT_GREEN: return "\x1b[92m";
|
||||
case Util::Color::LIGHT_AQUA: return "\x1b[96m";
|
||||
case Util::Color::LIGHT_RED: return "\x1b[91m";
|
||||
case Util::Color::LIGHT_PURPLE: return "\x1b[95m";
|
||||
case Util::Color::LIGHT_YELLOW: return "\x1b[93m";
|
||||
case Util::Color::WHITE: return "\x1b[97m";
|
||||
default: return "\x1b[0m";
|
||||
}
|
||||
}
|
||||
|
||||
template<Util::Color col, bool newLine, typename... Args>
|
||||
inline void WriteImpl(std::ostream& os, Args&&... args)
|
||||
{
|
||||
if constexpr (col != Util::Color::DEFAULT)
|
||||
os << GetAnsiCode(col);
|
||||
|
||||
(os << ... << std::forward<Args>(args));
|
||||
|
||||
if constexpr (newLine)
|
||||
os << std::endl;
|
||||
|
||||
if constexpr (col != Util::Color::DEFAULT)
|
||||
os << GetAnsiCode(Util::Color::DEFAULT);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
// Prints arguments to the console with the given color //
|
||||
template<Color col, typename... Args>
|
||||
requires Internal::AllLogable<Args...>
|
||||
inline void PrintAs(Args&&... args)
|
||||
{
|
||||
Internal::WriteImpl<col, false>(std::cout, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Prints arguments to the console //
|
||||
template<typename... Args>
|
||||
requires Internal::AllLogable<Args...>
|
||||
inline void Print(Args&&... args)
|
||||
{
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, false>(std::cout, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Prints arguments to the console with a new-line character at the end //
|
||||
template<typename... Args>
|
||||
requires Internal::AllLogable<Args...>
|
||||
inline void PrintLn(Args&&... args)
|
||||
{
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Logs all the arguments to the file (automatically flushes) //
|
||||
template<typename... Args>
|
||||
requires Internal::AllLogable<Args...> && (sizeof...(Args) > 1)
|
||||
inline void Log(Args&&... args)
|
||||
{
|
||||
std::ofstream& log = Internal::Log();
|
||||
if (log.is_open()) _UNLIKELY
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, R"([LXC] ")", std::forward<Args>(args)..., '"');
|
||||
}
|
||||
|
||||
// Logs a singular argument to the log, calls Log() if it can on the object //
|
||||
template<typename T>
|
||||
requires Internal::HasLogStrFunc<T> || Internal::Logable<T>
|
||||
inline void Log(T arg)
|
||||
{
|
||||
std::ofstream& log = Internal::Log();
|
||||
if (log.is_open()) _UNLIKELY
|
||||
{
|
||||
if constexpr (Internal::HasLogStrFunc<T>)
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '{', arg.LogStr(), '}');
|
||||
|
||||
else
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '"', arg, '"');
|
||||
}
|
||||
}
|
||||
|
||||
// Intitalises the log with the given file name //
|
||||
inline void CreateLog(const std::filesystem::path& path)
|
||||
{
|
||||
// Opens the log file with the given path //
|
||||
std::ofstream& log = Internal::Log();
|
||||
log.open(path);
|
||||
|
||||
// Assigns a function to close the log file on program exit //
|
||||
std::atexit([]()
|
||||
{
|
||||
std::ofstream& log = Internal::Log();
|
||||
log.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
12
Common/modules/OS.h
Normal file
12
Common/modules/OS.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
// Platform specific includes //
|
||||
#if defined(_WIN32)
|
||||
#define NOMINMAX
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#elif defined(__unix__)
|
||||
// No platform specific includes are needed for UNIX based systems //
|
||||
#else
|
||||
#error "OS is not supported"
|
||||
#endif
|
||||
116
Common/modules/Result.h
Normal file
116
Common/modules/Result.h
Normal file
@@ -0,0 +1,116 @@
|
||||
#pragma once
|
||||
|
||||
#include <modules/OS.h>
|
||||
|
||||
#include <type_traits>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
// Util function to stop/ the program //
|
||||
inline void Stop()
|
||||
{
|
||||
// Only checks for a debugger when compiled in Debug mode //
|
||||
#ifdef _DEBUG
|
||||
|
||||
// Triggers a breakpoint so the debugger can work out where the program exits //
|
||||
if (IsDebuggerPresent())
|
||||
DebugBreak();
|
||||
|
||||
#endif // _DEBUG
|
||||
|
||||
// Force exits the program //
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Custom version of std::unexpected //
|
||||
template<typename ErrorType> struct FunctionFail final
|
||||
{
|
||||
// Basic constructor to copy the error across //
|
||||
explicit FunctionFail(ErrorType _err) : error(_err)
|
||||
{
|
||||
// Only checks for a debugger when compiled in Debug mode //
|
||||
#ifdef _DEBUG
|
||||
|
||||
// Triggers a breakpoint when a debugger is attached as a function has failed //
|
||||
if (IsDebuggerPresent())
|
||||
DebugBreak();
|
||||
|
||||
#endif // _DEBUG
|
||||
}
|
||||
|
||||
// Constructs the FunctionFail with the error itself //
|
||||
template<typename... Args> requires std::constructible_from<ErrorType, Args...>
|
||||
explicit FunctionFail(Args&&... args)
|
||||
: error(std::forward<Args>(args)...)
|
||||
{
|
||||
// Only checks for a debugger when compiled in Debug mode //
|
||||
#ifdef _DEBUG
|
||||
|
||||
// Triggers a breakpoint when a debugger is attached as a function has failed //
|
||||
if (IsDebuggerPresent())
|
||||
DebugBreak();
|
||||
|
||||
#endif // _DEBUG
|
||||
}
|
||||
|
||||
const ErrorType error;
|
||||
};
|
||||
|
||||
// Custom version of std::expected //
|
||||
template<typename ResultType, typename ErrorType>
|
||||
requires (!std::same_as<ResultType, bool>) // ResultType being bool causes issues with operator overloads
|
||||
class ReturnVal final
|
||||
{
|
||||
public:
|
||||
// Constructor for function sucess //
|
||||
ReturnVal(ResultType result)
|
||||
: m_Result(result), m_FunctionFailed(false)
|
||||
{}
|
||||
|
||||
// Constructor for function fail //
|
||||
ReturnVal(FunctionFail<ErrorType> error)
|
||||
: m_Error(error.error), m_FunctionFailed(true)
|
||||
{}
|
||||
|
||||
// Destructor //
|
||||
~ReturnVal() {}
|
||||
|
||||
// Different getters of the class //
|
||||
|
||||
inline bool Failed() const { return m_FunctionFailed; }
|
||||
inline bool Suceeded() const { return !m_FunctionFailed; }
|
||||
|
||||
inline ResultType& Result()
|
||||
{
|
||||
if (Suceeded()) _LIKELY
|
||||
return m_Result;
|
||||
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
inline ErrorType& Error()
|
||||
{
|
||||
if (Failed()) _LIKELY
|
||||
return m_Error;
|
||||
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Operator overloads //
|
||||
|
||||
operator bool() const { return !m_FunctionFailed; }
|
||||
operator ResultType() { return Result(); }
|
||||
|
||||
private:
|
||||
// Union to hold either the result or the error //
|
||||
union
|
||||
{
|
||||
ResultType m_Result;
|
||||
ErrorType m_Error;
|
||||
};
|
||||
|
||||
// Tracks what item is currently in the union //
|
||||
bool m_FunctionFailed;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user