Started implementing Lexer errors

This commit is contained in:
Pasha Bibko
2025-07-20 21:54:37 +01:00
parent 6a0d2fc0a2
commit bb3d8fb13e
7 changed files with 84 additions and 32 deletions

View File

@@ -4,7 +4,7 @@
namespace LXC::Lexer
{
struct LexerContext
struct LexerContext final
{
// Constructor to set the information of the context //
LexerContext(const std::string& _source);
@@ -21,8 +21,36 @@ namespace LXC::Lexer
unsigned short line;
};
struct LexerError
{};
struct LexerError final
{
// Different reasons why the Lexer can fail //
enum Reason
{
InvalidCharacter,
UnterminatedStringLiteral
};
// Constructor to pass arguments through to the struct //
LexerError(Reason _reason, __int32 errorIndex)
: reason(_reason), index(errorIndex)
{}
// Turns the error into a c-string //
inline static const char* const ReasonStr(Reason reason)
{
static const char* reasons[] =
{
"Invalid character found in source: ",
"Unterminated string literal in source starting at: "
};
return reasons[reason];
}
// Error information //
const Reason reason;
const __int32 index;
};
// Turns a file into a vector of tokens //
Util::ReturnVal<LexerOutput, LexerError> TokenizeFile(const std::string& fileContents);

View File

@@ -48,28 +48,28 @@ namespace LXC::Lexer
For = TokenClass::Keyword,
While,
If,
Else_If,
ElseIf,
Else,
Return,
// === User defined === //
String_Literal = TokenClass::UserDefined,
Num_Literal,
StringLiteral = TokenClass::UserDefined,
NumLiteral,
Identifier,
// === Symbols === //
Assign = TokenClass::Symbols,
Close_bracket,
Open_bracket,
CloseBracket,
OpenBracket,
Close_brace,
Open_brace,
CloseBrace,
OpenBrace,
Close_paren,
Open_paren,
CloseParen,
OpenParen,
Comma,