Refactored parts of the parser
Done to make testing internal parts of the Parser easier as they can now be called via the .lib
This commit is contained in:
65
parser/inc/ParserContext.h
Normal file
65
parser/inc/ParserContext.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user