From a6afeff49313b35a40e86b8a5fb410d397e894ab Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sun, 20 Jul 2025 22:34:11 +0100 Subject: [PATCH] Implemented all Lexer errors --- Common/{FileRead.h => File.h} | 35 +++++++++++++++++++++++++++++++++++ Common/LXC.h | 2 +- LXC/LXC.cpp | 16 +++++++++++++--- Lexer/inc/Lexer.h | 4 ++-- Lexer/src/Lexer.cpp | 10 ++++++---- example/example.lx | 2 +- 6 files changed, 58 insertions(+), 11 deletions(-) rename Common/{FileRead.h => File.h} (72%) diff --git a/Common/FileRead.h b/Common/File.h similarity index 72% rename from Common/FileRead.h rename to Common/File.h index 7b10b24..3cba78a 100644 --- a/Common/FileRead.h +++ b/Common/File.h @@ -64,4 +64,39 @@ namespace LXC::Util 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, __int32 index) + { + // Returns false if outside the bounds // + if (index < 0 || index > file.length()) + return false; + + // Resets location // + location.line = 1; + location.col = 1; + + // Finds the location // + __int32 localIndex = 0; + while (localIndex != index) + { + if (file[localIndex] == '\n') + { + location.line += 1; + location.col = 0; + } + + location.col++; + localIndex++; + } + + return true; + } } diff --git a/Common/LXC.h b/Common/LXC.h index 019a0fe..2a4cd8a 100644 --- a/Common/LXC.h +++ b/Common/LXC.h @@ -7,5 +7,5 @@ // LXC util files // #include -#include +#include #include diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index 20d431b..92a6c35 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -27,8 +27,6 @@ int main(int argc, char** argv) Util::Stop(); } - Util::Log("Succesfully opened source file."); - // Turns the file contents into a vector of tokens // Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents); if (tokens.Failed()) _UNLIKELY @@ -36,11 +34,23 @@ int main(int argc, char** argv) // Stores the error for easier access // Lexer::LexerError& err = tokens.Error(); + // Finds the file location of the error // + Util::FileLocation location; + Util::GetFileLocationAtIndex(location, fileContents, err.index); + // Prints the error to the console // Util::PrintAs("[LXC] "); - Util::Print(src.filename().string()); + Util::Print(src.filename().string(), '(', location.line, ',', location.col, ')'); Util::PrintAs(" Error: "); + Util::Print(Lexer::LexerError::ReasonStr(err.reason)); + if (err.reason == Lexer::LexerError::InvalidCharacter) + Util::PrintLn(": {", fileContents.Result()[err.index], '}'); + + else + Util::PrintLn(); + + Util::Log("Error occured in Lexer: ", Lexer::LexerError::ReasonStr(err.reason)); Util::Stop(); } diff --git a/Lexer/inc/Lexer.h b/Lexer/inc/Lexer.h index cc1a7fe..d98b523 100644 --- a/Lexer/inc/Lexer.h +++ b/Lexer/inc/Lexer.h @@ -40,8 +40,8 @@ namespace LXC::Lexer { static const char* reasons[] = { - "Invalid character found in source: ", - "Unterminated string literal in source starting at: " + "Invalid character found in source", + "Unterminated string literal in source" }; return reasons[reason]; diff --git a/Lexer/src/Lexer.cpp b/Lexer/src/Lexer.cpp index 93e2a07..3f6a293 100644 --- a/Lexer/src/Lexer.cpp +++ b/Lexer/src/Lexer.cpp @@ -104,14 +104,16 @@ namespace LXC::Lexer // If an if-statement has not been triggered the character must be invalid // else - { - return Util::FunctionFail(LexerError::InvalidCharacter, -1); - } + return Util::FunctionFail(LexerError::InvalidCharacter, ctx.index); // Iterates to the next index // ctx.column++; ctx.index++; - } + } + + // Checks for an unterminated string literal // + if (trackers.inStrLiteral) + return Util::FunctionFail(LexerError::UnterminatedStringLiteral, trackers.sectionStart); return ctx.out; } diff --git a/example/example.lx b/example/example.lx index d6227fa..72dea62 100644 --- a/example/example.lx +++ b/example/example.lx @@ -1 +1 @@ -FILE 4 CONTENTS "A" GO B HERE 34 * 5 "ELLO THER" +FILE 4 CONTENTS "A" GO B HERE 34 5 "ELLO THER"