Added symbols/operators to lexer

This commit is contained in:
Pasha Bibko
2025-07-21 17:34:47 +01:00
parent a6afeff493
commit 24fde1b770
6 changed files with 158 additions and 96 deletions

View File

@@ -75,14 +75,14 @@ namespace LXC::Util
// Finds the location of a given index within a file // // Finds the location of a given index within a file //
inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, __int32 index) 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 // // Resets location //
location.line = 1; location.line = 1;
location.col = 1; location.col = 1;
// Returns false if outside the bounds //
if (index < 0 || index > file.length())
return false;
// Finds the location // // Finds the location //
__int32 localIndex = 0; __int32 localIndex = 0;
while (localIndex != index) while (localIndex != index)

View File

@@ -2,6 +2,7 @@
// Standard libraries // // Standard libraries //
#include <unordered_map>
#include <vector> #include <vector>
// LXC util files // // LXC util files //

View File

@@ -47,6 +47,9 @@ int main(int argc, char** argv)
if (err.reason == Lexer::LexerError::InvalidCharacter) if (err.reason == Lexer::LexerError::InvalidCharacter)
Util::PrintLn(": {", fileContents.Result()[err.index], '}'); Util::PrintLn(": {", fileContents.Result()[err.index], '}');
if (err.reason == Lexer::LexerError::UnknownSymbolOrOperand)
Util::PrintLn(": {", err.info, '}');
else else
Util::PrintLn(); Util::PrintLn();

View File

@@ -27,12 +27,13 @@ namespace LXC::Lexer
enum Reason enum Reason
{ {
InvalidCharacter, InvalidCharacter,
UnterminatedStringLiteral UnterminatedStringLiteral,
UnknownSymbolOrOperand
}; };
// Constructor to pass arguments through to the struct // // Constructor to pass arguments through to the struct //
LexerError(Reason _reason, __int32 errorIndex) LexerError(Reason _reason, __int32 errorIndex, std::string _info = "")
: reason(_reason), index(errorIndex) : reason(_reason), index(errorIndex), info(_info)
{} {}
// Turns the error into a c-string // // Turns the error into a c-string //
@@ -41,7 +42,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" "Unterminated string literal in source",
"Unknown symbol or operand in source"
}; };
return reasons[reason]; return reasons[reason];
@@ -50,6 +52,7 @@ namespace LXC::Lexer
// Error information // // Error information //
const Reason reason; const Reason reason;
const __int32 index; const __int32 index;
const std::string info;
}; };
// Turns a file into a vector of tokens // // Turns a file into a vector of tokens //

View File

@@ -19,6 +19,39 @@ namespace LXC::Internal
{ {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'; return c == ' ' || c == '\t' || c == '\n' || c == '\r';
} }
static constexpr bool IsSymbolOrOperator(const char c)
{
return
c == '+' || c == '-' ||
c == '*' || c == '/' ||
c == '%' || c == '=' ||
c == ',' || c == '[' ||
c == ']' || c == '{' ||
c == '}' || c == '(' ||
c == ')';
}
static const std::unordered_map<std::string_view, Lexer::Token::TokenType> symbolAndOpMap =
{
{ "+", Lexer::Token::Add },
{ "-", Lexer::Token::Sub },
{ "*", Lexer::Token::Mul },
{ "/", Lexer::Token::Div },
{ "%", Lexer::Token::Mod },
{ "=", Lexer::Token::Assign },
{ ",", Lexer::Token::Comma },
{ "[", Lexer::Token::CloseBracket },
{ "]", Lexer::Token::OpenBracket },
{ "{", Lexer::Token::CloseBrace },
{ "}", Lexer::Token::OpenBrace },
{ ")", Lexer::Token::CloseParen },
{ "(", Lexer::Token::OpenParen }
};
} }
namespace LXC::Lexer namespace LXC::Lexer
@@ -37,6 +70,7 @@ namespace LXC::Lexer
bool inStrLiteral = false; bool inStrLiteral = false;
bool inIdentifier = false; bool inIdentifier = false;
bool inNumLiteral = false; bool inNumLiteral = false;
bool inSymbolOrOp = false;
bool inComment = false; bool inComment = false;
@@ -99,6 +133,27 @@ namespace LXC::Lexer
} }
} }
// === Symbols/Operators === //
else if (Internal::IsSymbolOrOperator(current))
{
// Updates trackers //
trackers.sectionStart = trackers.inSymbolOrOp ? trackers.sectionStart : ctx.index;
trackers.inSymbolOrOp = true;
// Checks for the end of the symbol or operator //
if (!Internal::IsSymbolOrOperator(next))
{
// Finds the operator/symbol if it can //
std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1);
auto it = Internal::symbolAndOpMap.find(fullSymbol);
if (it != Internal::symbolAndOpMap.end())
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), it->second);
else
return Util::FunctionFail<LexerError>(LexerError::UnknownSymbolOrOperand, trackers.sectionStart, std::string(fullSymbol));
}
}
// === Whitespace === // // === Whitespace === //
else if (Internal::IsWhitespace(current)) {} else if (Internal::IsWhitespace(current)) {}

View File

@@ -1 +1 @@
FILE 4 CONTENTS "A" GO B HERE 34 5 "ELLO THER" FILE 4 CONTENTS "A" GO B HERE 34 += 5 "ELLO THER"