From 98842b24f26cde4e064d1457582cf36e1d6be059 Mon Sep 17 00:00:00 2001 From: jmsgrogan Date: Thu, 13 Oct 2022 08:57:26 +0100 Subject: [PATCH] Template enginge - start parsing nodes. --- src/compiler/TemplatingEngine.h | 147 ++++++++++++++++++++++++++++---- src/core/StringUtils.cpp | 36 ++++++++ src/core/StringUtils.h | 1 + 3 files changed, 166 insertions(+), 18 deletions(-) diff --git a/src/compiler/TemplatingEngine.h b/src/compiler/TemplatingEngine.h index 60bae69..381eb9e 100644 --- a/src/compiler/TemplatingEngine.h +++ b/src/compiler/TemplatingEngine.h @@ -8,9 +8,67 @@ #include #include -class TemplateBlock +class TemplateNode { +public: + TemplateNode() + { + + } + + virtual void addChild(std::unique_ptr child) + { + mChildren.push_back(std::move(child)); + } + +protected: + std::vector > mChildren; +}; + +using TemplateNodePtr = std::unique_ptr; + +class TemplateBlock : public TemplateNode +{ +public: + TemplateBlock(const std::string& name) + : mName(name) + { + + } + + void addLine(const std::string& line) + { + mBody.push_back(line); + } + +private: + std::string mName; + std::vector mBody; +}; + +class TemplateExpression : public TemplateNode +{ +public: + TemplateExpression(const std::string& content) + : mContent(content) + { + + } +private: + std::string mContent; +}; + +class TemplateTextBody : public TemplateNode +{ +public: + TemplateTextBody(const std::string& content) + : mContent(content) + { + + } +private: + std::string mContent; }; class TemplateFile @@ -47,49 +105,102 @@ public: void processLine(const std::string& line) { bool last_was_ldelimiter{ false }; - bool last_was_rdelimiter{ false }; - bool in_tag{ false }; + bool last_was_statement_rdelimiter{ false }; + bool last_was_expression_rdelimiter{ false }; + bool in_statement{ false }; + bool in_expression{ false }; std::string working_string; for (auto c : line) { if (c == '{') { - last_was_ldelimiter = true; + if (last_was_ldelimiter) + { + in_expression = true; + last_was_ldelimiter = false; + working_string = ""; + } + else + { + last_was_ldelimiter = true; + } } else if (c == '%' && last_was_ldelimiter) { last_was_ldelimiter = false; - in_tag = true; + in_statement = true; working_string = ""; } - else if (c == '%' && in_tag) + else if (c == '%' && in_statement) { - last_was_rdelimiter = true; + last_was_statement_rdelimiter = true; } - else if (c == '}' && last_was_rdelimiter) + else if (c == '}' && (last_was_statement_rdelimiter || in_expression || last_was_expression_rdelimiter)) { - break; + if (last_was_statement_rdelimiter) + { + onFoundStatement(working_string); + last_was_statement_rdelimiter = false; + working_string = ""; + } + else if (in_expression) + { + last_was_expression_rdelimiter = true; + in_expression = false; + } + else + { + onFoundExpression(working_string); + last_was_expression_rdelimiter = false; + working_string = ""; + } } else { working_string += c; } } - - if (in_tag) - { - onFoundTag(working_string); - } } - void onFoundTag(const std::string& tag_string) + void onFoundStatement(const std::string& statement_string) { - const auto tag_elements = StringUtils::split(tag_string); + std::cout << "Got Statement: " << statement_string << std::endl; + const auto statement_elements = StringUtils::split(statement_string); - for (const auto& element : tag_elements) + if (statement_elements.size() == 0) { - std::cout << "Got tag element: " << element << std::endl; + return; } + const auto statement_type = statement_elements[0]; + if (statement_type == "block") + { + onFoundBlock(statement_elements); + } + else if (statement_type == "endblock") + { + onFoundEndBlock(statement_elements); + } + + } + + void onFoundBlock(const std::vector args) + { + + } + + void onFoundEndBlock(const std::vector args) + { + + } + + void onFoundExtends(const std::vector args) + { + + } + + void onFoundExpression(const std::string& expression_string) + { + std::cout << "Got Expression: " << expression_string << std::endl; } private: diff --git a/src/core/StringUtils.cpp b/src/core/StringUtils.cpp index 145b0ea..e2727a1 100644 --- a/src/core/StringUtils.cpp +++ b/src/core/StringUtils.cpp @@ -20,6 +20,42 @@ bool StringUtils::IsSpace(char c) return std::isspace(c, loc); } +std::string StringUtils::strip(const std::string& input) +{ + if (input.empty()) + { + return {}; + } + + std::locale loc; + std::string working_string; + std::size_t first_nonspace = 0; + std::size_t last_nonspace = working_string.size() - 1; + for (std::size_t idx = 0; idx < working_string.size(); idx++) + { + if (!std::isspace(working_string[idx], loc)) + { + first_nonspace = idx; + break; + } + } + + if (first_nonspace == last_nonspace) + { + return {}; + } + + for (std::size_t idx = last_nonspace; idx > 0; idx--) + { + if (!std::isspace(working_string[idx], loc)) + { + last_nonspace = idx; + break; + } + } + return working_string.substr(first_nonspace, last_nonspace-first_nonspace); +} + std::vector StringUtils::split(const std::string& input) { std::vector substrings; diff --git a/src/core/StringUtils.h b/src/core/StringUtils.h index 7336b52..3b85a50 100644 --- a/src/core/StringUtils.h +++ b/src/core/StringUtils.h @@ -22,4 +22,5 @@ public: static std::string convert(const std::wstring& input); static std::string ToPaddedString(unsigned numBytes, unsigned entry); static std::vector split(const std::string& input); + static std::string strip(const std::string& input); };