Files
LXC/parser/inc/ParserContext.h
Pasha Bibko a18e652915 Refactored parts of the parser
Done to make testing internal parts of the Parser easier as they can now be called via the .lib
2025-08-20 21:56:33 +01:00

66 lines
2.0 KiB
C++

#pragma once
#include <LX_Parser.h>
namespace PashaBibko::LXC::Parser
{
class ParserContext final
{
public:
// Sets the information of the context //
ParserContext(const Lexer::LexerOutput& inputTokens) :
input(inputTokens), length(inputTokens.size()), index(0)
{}
// Returns a pointer to the token x-distance away (default 1) //
inline const Lexer::Token* Peek(size_t peekDistance = 1) const
{
// Checks if at the end of the tokens before fetching to stop errors //
size_t peekIndex = index + peekDistance;
if (peekIndex >= length)
return nullptr;
return &input[peekIndex];
}
// Returns a pointer to the current token //
inline const Lexer::Token* At() const { return Peek(0); }
// Advances x-distance in the tokens (default 1) //
inline const Lexer::Token* Advance(size_t distance = 1)
{
// Adds the distance and checks if in bounds to stop read errors //
index += distance;
if (index >= length)
return nullptr;
return &input[index];
}
// Checks if the tokens are the correct types //
inline bool Expect(const std::span<const Lexer::Token::TokenType>& tokens) const
{
for (int i = 0; i < tokens.size(); i++)
{
const Lexer::Token* current = Peek(i);
if (!current || current->type != tokens[i])
return false;
}
return true;
}
// Is the current index in bounds //
inline bool InBounds() const { return index < length; }
// Output //
std::vector<FunctionAST> output;
private:
size_t index;
const Lexer::LexerOutput& input;
const size_t length;
};
}