From a18e6529156e181111bcfc1420bb6329d31661dc Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Wed, 20 Aug 2025 21:56:33 +0100 Subject: [PATCH] Refactored parts of the parser Done to make testing internal parts of the Parser easier as they can now be called via the .lib --- LXC/CMakeLists.txt | 5 +-- ast/CMakeLists.txt | 3 +- lexer/CMakeLists.txt | 2 +- parser/CMakeLists.txt | 4 +-- parser/inc/ParserContext.h | 65 ++++++++++++++++++++++++++++++++++++ parser/src/Parser.cpp | 67 +++----------------------------------- tests/src/ParserTests.cpp | 20 +++++++++++- 7 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 parser/inc/ParserContext.h diff --git a/LXC/CMakeLists.txt b/LXC/CMakeLists.txt index 1a1bc19..9ea2ade 100644 --- a/LXC/CMakeLists.txt +++ b/LXC/CMakeLists.txt @@ -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) diff --git a/ast/CMakeLists.txt b/ast/CMakeLists.txt index 3df4eb0..0f4e904 100644 --- a/ast/CMakeLists.txt +++ b/ast/CMakeLists.txt @@ -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) diff --git a/lexer/CMakeLists.txt b/lexer/CMakeLists.txt index 0c7e6ff..4ae6135 100644 --- a/lexer/CMakeLists.txt +++ b/lexer/CMakeLists.txt @@ -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) diff --git a/parser/CMakeLists.txt b/parser/CMakeLists.txt index 1538240..e6cd64f 100644 --- a/parser/CMakeLists.txt +++ b/parser/CMakeLists.txt @@ -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) diff --git a/parser/inc/ParserContext.h b/parser/inc/ParserContext.h new file mode 100644 index 0000000..5c38d2c --- /dev/null +++ b/parser/inc/ParserContext.h @@ -0,0 +1,65 @@ +#pragma once + +#include + +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& 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 output; + + private: + size_t index; + + const Lexer::LexerOutput& input; + const size_t length; + }; +} diff --git a/parser/src/Parser.cpp b/parser/src/Parser.cpp index 406579b..8edc2d3 100644 --- a/parser/src/Parser.cpp +++ b/parser/src/Parser.cpp @@ -2,6 +2,8 @@ #include +#include + namespace PashaBibko::LXC::Internal { template @@ -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& 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 output; - - private: - size_t index; - - const Lexer::LexerOutput& input; - const size_t length; - }; - static Util::ReturnVal 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 Parse(ParserContext& ctx) + Util::ReturnVal ParseFunctionTokens(ParserContext& ctx) { // Alias for the top of the call stack // return ParseVarDeclaration(ctx); } - static Util::ReturnVal ParseFunction(ParserContext& ctx) + Util::ReturnVal ParseFunction(ParserContext& ctx) { // Checks for the sequence of: func 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(node.Error()); // Fowards the error diff --git a/tests/src/ParserTests.cpp b/tests/src/ParserTests.cpp index 93d0a7e..d4fc2e5 100644 --- a/tests/src/ParserTests.cpp +++ b/tests/src/ParserTests.cpp @@ -1,9 +1,27 @@ #include +#include +#include + // 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); }