mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Improved logging
Small errors with logging parser unexpected token errors. Tokens need to be re-written with how they store memory as it can be halved.
This commit is contained in:
@@ -17,6 +17,22 @@ namespace LX
|
||||
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)
|
||||
{
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
||||
// Returns the string between start and end //
|
||||
return src.substr(start, end - start);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
@@ -26,6 +42,9 @@ int main(int argc, char** argv)
|
||||
std::filesystem::path outPath;
|
||||
std::filesystem::path logPath;
|
||||
|
||||
// Creates the contents string outside of the try-catch so they can be used in errors //
|
||||
std::string contents;
|
||||
|
||||
// Creates the log-file out of the try-catch so it can be closed propely if an error is thrown //
|
||||
std::unique_ptr<std::ofstream> log = nullptr;
|
||||
|
||||
@@ -43,6 +62,12 @@ int main(int argc, char** argv)
|
||||
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);
|
||||
|
||||
// Copies the file into the string //
|
||||
const std::streamsize len = inpFile.tellg(); // Gets length of file because it was opened at the end
|
||||
inpFile.seekg(0, std::ios::beg); // Goes back to the beginning
|
||||
contents = std::string(len, '\0'); // Allocates all the space for the string
|
||||
inpFile.read(&contents[0], len); // Transfers file contents to string
|
||||
|
||||
// Opens / Creates the output file //
|
||||
std::ofstream outFile(outPath);
|
||||
LX::ThrowIf<LX::InvalidOutputFilePath>(outFile.is_open() == false);
|
||||
@@ -60,7 +85,7 @@ int main(int argc, char** argv)
|
||||
std::cout << std::filesystem::absolute(inpPath) << " -> " << std::filesystem::absolute(outPath) << std::endl;
|
||||
|
||||
// Create tokens out of the input file //
|
||||
std::vector<LX::Token>tokens = LX::LexicalAnalyze(inpFile, log.get());
|
||||
std::vector<LX::Token>tokens = LX::LexicalAnalyze(contents, len, log.get());
|
||||
LX::SafeFlush(log.get());
|
||||
|
||||
// Turns the tokens into an AST //
|
||||
@@ -144,14 +169,17 @@ int main(int argc, char** argv)
|
||||
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 << ":\n";
|
||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.line << " | " << e.lineContents << "\n";
|
||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.index);
|
||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.line << " | " << line << "\n";
|
||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.col);
|
||||
LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
|
||||
std::cout << "\n";
|
||||
|
||||
@@ -159,6 +187,41 @@ int main(int argc, char** argv)
|
||||
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 - 1) << "";
|
||||
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;
|
||||
}
|
||||
|
||||
// Catches any std errors, there should be none //
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user