Template enginge - start parsing nodes.

This commit is contained in:
jmsgrogan 2022-10-13 08:57:26 +01:00
parent cdd0cc6b78
commit 98842b24f2
3 changed files with 166 additions and 18 deletions

View file

@ -8,9 +8,67 @@
#include <string>
#include <iostream>
class TemplateBlock
class TemplateNode
{
public:
TemplateNode()
{
}
virtual void addChild(std::unique_ptr<TemplateNode> child)
{
mChildren.push_back(std::move(child));
}
protected:
std::vector<std::unique_ptr<TemplateNode> > mChildren;
};
using TemplateNodePtr = std::unique_ptr<TemplateNode>;
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<std::string> 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 == '{')
{
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<std::string> args)
{
}
void onFoundEndBlock(const std::vector<std::string> args)
{
}
void onFoundExtends(const std::vector<std::string> args)
{
}
void onFoundExpression(const std::string& expression_string)
{
std::cout << "Got Expression: " << expression_string << std::endl;
}
private:

View file

@ -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<std::string> StringUtils::split(const std::string& input)
{
std::vector<std::string> substrings;

View file

@ -22,4 +22,5 @@ public:
static std::string convert(const std::wstring& input);
static std::string ToPaddedString(unsigned numBytes, unsigned entry);
static std::vector<std::string> split(const std::string& input);
static std::string strip(const std::string& input);
};