Implemented all Lexer errors
This commit is contained in:
@@ -64,4 +64,39 @@ namespace LXC::Util
|
|||||||
|
|
||||||
return contents;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,5 +7,5 @@
|
|||||||
// LXC util files //
|
// LXC util files //
|
||||||
|
|
||||||
#include <Result.h>
|
#include <Result.h>
|
||||||
#include <FileRead.h>
|
#include <File.h>
|
||||||
#include <IO.h>
|
#include <IO.h>
|
||||||
|
|||||||
16
LXC/LXC.cpp
16
LXC/LXC.cpp
@@ -27,8 +27,6 @@ int main(int argc, char** argv)
|
|||||||
Util::Stop();
|
Util::Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
Util::Log("Succesfully opened source file.");
|
|
||||||
|
|
||||||
// Turns the file contents into a vector of tokens //
|
// Turns the file contents into a vector of tokens //
|
||||||
Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents);
|
Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents);
|
||||||
if (tokens.Failed()) _UNLIKELY
|
if (tokens.Failed()) _UNLIKELY
|
||||||
@@ -36,11 +34,23 @@ int main(int argc, char** argv)
|
|||||||
// Stores the error for easier access //
|
// Stores the error for easier access //
|
||||||
Lexer::LexerError& err = tokens.Error();
|
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 //
|
// Prints the error to the console //
|
||||||
Util::PrintAs<Util::WHITE>("[LXC] ");
|
Util::PrintAs<Util::WHITE>("[LXC] ");
|
||||||
Util::Print(src.filename().string());
|
Util::Print(src.filename().string(), '(', location.line, ',', location.col, ')');
|
||||||
Util::PrintAs<Util::LIGHT_RED>(" Error: ");
|
Util::PrintAs<Util::LIGHT_RED>(" 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();
|
Util::Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,8 @@ namespace LXC::Lexer
|
|||||||
{
|
{
|
||||||
static const char* reasons[] =
|
static const char* reasons[] =
|
||||||
{
|
{
|
||||||
"Invalid character found in source: ",
|
"Invalid character found in source",
|
||||||
"Unterminated string literal in source starting at: "
|
"Unterminated string literal in source"
|
||||||
};
|
};
|
||||||
|
|
||||||
return reasons[reason];
|
return reasons[reason];
|
||||||
|
|||||||
@@ -104,14 +104,16 @@ namespace LXC::Lexer
|
|||||||
|
|
||||||
// If an if-statement has not been triggered the character must be invalid //
|
// If an if-statement has not been triggered the character must be invalid //
|
||||||
else
|
else
|
||||||
{
|
return Util::FunctionFail<LexerError>(LexerError::InvalidCharacter, ctx.index);
|
||||||
return Util::FunctionFail<LexerError>(LexerError::InvalidCharacter, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterates to the next index //
|
// Iterates to the next index //
|
||||||
ctx.column++;
|
ctx.column++;
|
||||||
ctx.index++;
|
ctx.index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks for an unterminated string literal //
|
||||||
|
if (trackers.inStrLiteral)
|
||||||
|
return Util::FunctionFail<LexerError>(LexerError::UnterminatedStringLiteral, trackers.sectionStart);
|
||||||
|
|
||||||
return ctx.out;
|
return ctx.out;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
FILE 4 CONTENTS "A" GO B HERE 34 * 5 "ELLO THER"
|
FILE 4 CONTENTS "A" GO B HERE 34 5 "ELLO THER"
|
||||||
|
|||||||
Reference in New Issue
Block a user