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

@@ -3,7 +3,7 @@
#include <Lexer.h>
#include <Token.h>
namespace LXC::Lexer
namespace LXC::Internal
{
static constexpr bool IsNumeric(const char c)
{
@@ -15,6 +15,14 @@ namespace LXC::Lexer
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static constexpr bool IsWhitespace(const char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
}
namespace LXC::Lexer
{
LexerContext::LexerContext(const std::string& _source) :
source(_source), index(0), out{}, len((__int32)_source.length()), column(0), line(0)
{}
@@ -57,40 +65,49 @@ namespace LXC::Lexer
// Creates the token (if at the end of the string literal) //
if (!trackers.inStrLiteral)
ctx.out.emplace_back(ctx, trackers.sectionStart + 1, (USHORT)(ctx.index - trackers.sectionStart - 1), Token::String_Literal);
ctx.out.emplace_back(ctx, trackers.sectionStart + 1, (USHORT)(ctx.index - trackers.sectionStart - 1), Token::StringLiteral);
} else if (trackers.inStrLiteral) {}
// === Numbers === //
else if (IsNumeric(current))
else if (Internal::IsNumeric(current))
{
// Updates trackers //
trackers.sectionStart = trackers.inNumLiteral ? trackers.sectionStart : ctx.index;
trackers.inNumLiteral = true;
// Checks for the end of the number literal to create the token //
if (!IsNumeric(next)) _UNLIKELY
if (!Internal::IsNumeric(next)) _UNLIKELY
{
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::Num_Literal);
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral);
trackers.inNumLiteral = false;
}
}
// === Words === //
else if (IsAlpha(current))
else if (Internal::IsAlpha(current))
{
// Updates trackers //
trackers.sectionStart = trackers.inIdentifier ? trackers.sectionStart : ctx.index;
trackers.inIdentifier = true;
// Checks for the end of the word to create the token //
if (!IsAlpha(next)) _UNLIKELY
if (!Internal::IsAlpha(next)) _UNLIKELY
{
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::Identifier);
trackers.inIdentifier = false;
}
}
// === Whitespace === //
else if (Internal::IsWhitespace(current)) {}
// If an if-statement has not been triggered the character must be invalid //
else
{
return Util::FunctionFail<LexerError>(LexerError::InvalidCharacter, -1);
}
// Iterates to the next index //
ctx.column++;
ctx.index++;

View File

@@ -48,21 +48,21 @@ namespace LXC::Lexer
TOKEN_TYPE_CASE(Token::For);
TOKEN_TYPE_CASE(Token::While);
TOKEN_TYPE_CASE(Token::If);
TOKEN_TYPE_CASE(Token::Else_If);
TOKEN_TYPE_CASE(Token::ElseIf);
TOKEN_TYPE_CASE(Token::Else);
TOKEN_TYPE_CASE(Token::Return);
TOKEN_TYPE_CASE(Token::String_Literal);
TOKEN_TYPE_CASE(Token::Num_Literal);
TOKEN_TYPE_CASE(Token::StringLiteral);
TOKEN_TYPE_CASE(Token::NumLiteral);
TOKEN_TYPE_CASE(Token::Identifier);
TOKEN_TYPE_CASE(Token::Assign);
TOKEN_TYPE_CASE(Token::Close_bracket);
TOKEN_TYPE_CASE(Token::Open_bracket);
TOKEN_TYPE_CASE(Token::Close_brace);
TOKEN_TYPE_CASE(Token::Open_brace);
TOKEN_TYPE_CASE(Token::Close_paren);
TOKEN_TYPE_CASE(Token::Open_paren);
TOKEN_TYPE_CASE(Token::CloseBracket);
TOKEN_TYPE_CASE(Token::OpenBracket);
TOKEN_TYPE_CASE(Token::CloseBrace);
TOKEN_TYPE_CASE(Token::OpenBrace);
TOKEN_TYPE_CASE(Token::CloseParen);
TOKEN_TYPE_CASE(Token::OpenParen);
TOKEN_TYPE_CASE(Token::Comma);
TOKEN_TYPE_CASE(Token::End_of_file);