Tided up Parser project

This commit is contained in:
Pasha Bibko
2025-05-07 20:33:39 +01:00
parent c472cb5fc5
commit 4509250c4e
12 changed files with 142 additions and 95 deletions

43
Parser/inc/ParserErrors.h Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include <LX-Common.h>
#include <ParserInfo.h>
#include <Lexer.h>
namespace LX
{
// Thrown if there was an error during IR Generation //
CREATE_EMPTY_LX_ERROR_TYPE(IRGenerationError);
// Error thrown if the user tried to create a variable that already existed //
CREATE_EMPTY_LX_ERROR_TYPE(VariableAlreadyExists);
// Error thrown if user tries to access variable that does not exist //
CREATE_EMPTY_LX_ERROR_TYPE(VariableDoesntExist);
// Thrown if there was an unexpected (incorrect) token //
struct UnexpectedToken : public RuntimeError
{
GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
// Constructor to set the members of the error //
UnexpectedToken(Token::TokenType _expected, const ParserInfo& p);
// Constructor for custom messages in the cmd //
UnexpectedToken(Token::TokenType _expected, Token _got, const std::string& message, const ParserInfo& p);
// The file that the tokens come from //
const std::filesystem::path file;
// The token type that should be there //
const Token::TokenType expected;
// If there are multiple expected types there is an option for a custom message //
const std::string custom;
// What token was actually at that position //
// Stored as Token not TokenType to store the location of it within the source //
const Token got;
};
}

33
Parser/inc/ParserInfo.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <LX-Common.h>
// Includes Lexer so it can use LX::Token //
#include <Lexer.h>
namespace LX
{
// Local struct so everything can be public //
struct ParserInfo
{
// Passes constructor args to members //
ParserInfo(const std::vector<Token>& _tokens, const std::filesystem::path& path)
: tokens(_tokens), index(0), len(_tokens.size()), scopeDepth(0), file(path)
{}
// The file that the tokens were generated from //
const std::filesystem::path file;
// Tokens created by the lexer //
const std::vector<Token>& tokens;
// Length of the the token vector //
const size_t len;
// Current index within the token vector //
size_t index;
// Current scope depth //
size_t scopeDepth;
};
}