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:
Pasha Bibko
2025-08-20 21:56:33 +01:00
parent 466d861e0b
commit a18e652915
7 changed files with 93 additions and 73 deletions

View File

@@ -2,10 +2,7 @@
add_executable(lxc LXC.cpp)
# Links to the all needed internal libraries #
target_link_libraries(lxc PRIVATE PashaBibko-UTIL)
target_link_libraries(lxc PRIVATE Lexer)
target_link_libraries(lxc PRIVATE AST)
target_link_libraries(lxc PRIVATE Parser)
target_link_libraries(lxc PUBLIC Parser)
# Creates the precompiled header for the binary #
target_include_directories(lxc PRIVATE ${CMAKE_SOURCE_DIR}/external/util)

View File

@@ -11,8 +11,7 @@ target_include_directories (
)
# Links to the all needed internal libraries #
target_link_libraries(AST PRIVATE PashaBibko-UTIL)
target_link_libraries(AST PRIVATE Lexer)
target_link_libraries(AST PUBLIC Lexer)
# Creates the precompiled header of the binary #
target_include_directories(AST PRIVATE ${CMAKE_SOURCE_DIR}/external/util)

View File

@@ -11,7 +11,7 @@ target_include_directories (
)
# Links to the all needed internal libraries #
target_link_libraries(Lexer PRIVATE PashaBibko-UTIL)
target_link_libraries(Lexer PUBLIC PashaBibko-UTIL)
# Creates the precompiled header for the binary #
target_include_directories(Lexer PRIVATE ${CMAKE_SOURCE_DIR}/external/util)

View File

@@ -10,9 +10,7 @@ target_include_directories (
)
# Links to the all needed internal libraries #
target_link_libraries(Parser PRIVATE PashaBibko-UTIL)
target_link_libraries(Parser PRIVATE Lexer)
target_link_libraries(Parser PRIVATE AST)
target_link_libraries(Parser PUBLIC AST)
# Creates the precompiled header of the binary #
target_include_directories(Parser PRIVATE ${CMAKE_SOURCE_DIR}/external/util)

View 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;
};
}

View File

@@ -2,6 +2,8 @@
#include <LX_Parser.h>
#include <ParserContext.h>
namespace PashaBibko::LXC::Internal
{
template<typename NodeType, typename... Args>
@@ -21,65 +23,6 @@ namespace PashaBibko::LXC::Internal
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;
};
static Util::ReturnVal<AST::NodeValuePtr, ParserError> ParsePrimary(ParserContext& ctx)
{
// Gets the current token and returns if out of bounds //
@@ -230,13 +173,13 @@ namespace PashaBibko::LXC::Parser
return ParseReturn(ctx);
}
static inline Util::ReturnVal<AST::NodePtr, ParserError> Parse(ParserContext& ctx)
Util::ReturnVal<AST::NodePtr, ParserError> ParseFunctionTokens(ParserContext& ctx)
{
// Alias for the top of the call stack //
return ParseVarDeclaration(ctx);
}
static Util::ReturnVal<FunctionAST, ParserError> ParseFunction(ParserContext& ctx)
Util::ReturnVal<FunctionAST, ParserError> ParseFunction(ParserContext& ctx)
{
// Checks for the sequence of: func<T> funcName( //
if (!ctx.Expect(std::array{ Lexer::Token::FunctionDef, Lexer::Token::OpenCrocodile, Lexer::Token::Identifier, Lexer::Token::CloseCrocodile, Lexer::Token::Identifier, Lexer::Token::OpenParen }))
@@ -291,7 +234,7 @@ namespace PashaBibko::LXC::Parser
}
// Parses the current token //
Util::ReturnVal node = Parse(ctx);
Util::ReturnVal node = ParseFunctionTokens(ctx);
if (node.Failed()) _UNLIKELY
return Util::FunctionFail<ParserError>(node.Error()); // Fowards the error

View File

@@ -1,9 +1,27 @@
#include <gtest/gtest.h>
#include <ParserContext.h>
#include <LX_Parser.h>
// The tests for the parser //
namespace PashaBibko::LXC::Parser
{
TEST(ParserTests, ARE_PARSER_TESTS_COMPILING)
TEST(ParserTests, ParserContextAtAndInBounds)
{
EXPECT_TRUE(true);
}
TEST(ParserTests, ParserContextPeek)
{
EXPECT_TRUE(true);
}
TEST(ParserTests, ParserContextAdvance)
{
EXPECT_TRUE(true);
}
TEST(ParserTests, ParserContextExpect)
{
EXPECT_TRUE(true);
}