From 263915832993dd12beee10e204f9ebcc6c786ed2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 30 Dec 2023 23:39:37 +0100 Subject: Meta: initial commit of WestCo optimized toolchain. Signed-off-by: Amlal El Mahrouss --- C++Kit/ParserKit.hpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 C++Kit/ParserKit.hpp (limited to 'C++Kit/ParserKit.hpp') diff --git a/C++Kit/ParserKit.hpp b/C++Kit/ParserKit.hpp new file mode 100644 index 0000000..62eff00 --- /dev/null +++ b/C++Kit/ParserKit.hpp @@ -0,0 +1,87 @@ +/* + * ======================================================== + * + * C++Kit + * Copyright WestCo, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include + +namespace ParserKit +{ + using namespace CxxKit; + + class CompilerBackend + { + public: + explicit CompilerBackend() = default; + virtual ~CompilerBackend() = default; + + CXXKIT_COPY_DEFAULT(CompilerBackend); + + // NOTE: cast this to your user defined ast. + typedef void* AstType; + + //! @brief Compile a syntax tree ouf of the text. + //! Also takes the source file name for metadata. + + virtual void Compile(const char* text, const char* file) = 0; + + //! @brief What language are we dealing with? + virtual const char* Language() { return "Generic Language"; } + + }; + + struct SyntaxLeafList; + struct SyntaxLeafList; + + struct SyntaxLeafList final + { + struct SyntaxLeaf final + { + Int32 fUserType; + std::string fUserData; + std::string fUserValue; + struct SyntaxLeaf* fNext; + }; + + std::vector fLeafList; + SizeType fNumLeafs; + + size_t SizeOf() { return fNumLeafs; } + std::vector& Get() { return fLeafList; } + SyntaxLeaf& At(size_t index) { return fLeafList[index]; } + + }; + + /// find the perfect matching word in a haystack. + /// \param haystack base string + /// \param needle the string we search for. + /// \return if we found it or not. + inline bool find_word(const std::string& haystack,const std::string& needle) noexcept + { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) + return false; + + // declare lambda + auto not_part_of_word = [&](int index){ + if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) + return true; + + if (index < 0 || index >= haystack.size()) + return true; + + return false; + }; + + return not_part_of_word(index - 1) && + not_part_of_word(index + needle.size()); + } +} \ No newline at end of file -- cgit v1.2.3