Continue templating engine - initial tree structure.

This commit is contained in:
jmsgrogan 2022-10-19 08:56:02 +01:00
parent 98842b24f2
commit dbd7591dc5

View file

@ -6,33 +6,81 @@
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
class TemplateNode
{
public:
TemplateNode()
TemplateNode(TemplateNode* parent)
: mParent(parent)
{
}
TemplateNode* getParent() const
{
return mParent;
}
virtual void addChild(std::unique_ptr<TemplateNode> child)
{
mChildren.push_back(std::move(child));
}
std::size_t getNumChildren() const
{
return mChildren.size();
}
TemplateNode* getChild(std::size_t index) const
{
return mChildren[index].get();
}
virtual std::string getRawContent() const
{
std::string content;
for (const auto& child : mChildren)
{
content += child->getRawContent() + "\n";
}
return content;
}
protected:
std::vector<std::unique_ptr<TemplateNode> > mChildren;
TemplateNode* mParent{ nullptr };
};
using TemplateNodePtr = std::unique_ptr<TemplateNode>;
class TemplateExtends : public TemplateNode
{
public:
TemplateExtends(TemplateNode* parent, const std::string& path)
: TemplateNode(parent),
mPath(path)
{
};
std::string getRawContent() const override
{
return "TemplateExtends: " + mPath;
}
private:
std::string mPath;
};
class TemplateBlock : public TemplateNode
{
public:
TemplateBlock(const std::string& name)
: mName(name)
TemplateBlock(TemplateNode* parent, const std::string& name)
: TemplateNode(parent),
mName(name)
{
}
@ -42,6 +90,13 @@ public:
mBody.push_back(line);
}
std::string getRawContent() const override
{
std::string content = "TemplateBlock: " + mName + "\n";
content += TemplateNode::getRawContent();
return content;
}
private:
std::string mName;
std::vector<std::string> mBody;
@ -50,11 +105,18 @@ private:
class TemplateExpression : public TemplateNode
{
public:
TemplateExpression(const std::string& content)
: mContent(content)
TemplateExpression(TemplateNode* parent, const std::string& content)
: TemplateNode(parent),
mContent(content)
{
}
std::string getRawContent() const override
{
return "TemplateExpression: " + mContent;
}
private:
std::string mContent;
};
@ -62,20 +124,42 @@ private:
class TemplateTextBody : public TemplateNode
{
public:
TemplateTextBody(const std::string& content)
: mContent(content)
TemplateTextBody(TemplateNode* parent)
: TemplateNode(parent)
{
}
void addLine(const std::string& content)
{
mContent.push_back(content);
}
bool hasContent() const
{
return !mContent.empty();
}
std::string getRawContent() const override
{
std::string content;
for (const auto& line : mContent)
{
content += "TemplateBody: " + line + "\n";
}
return content;
}
private:
std::string mContent;
std::vector<std::string> mContent;
};
class TemplateFile
{
public:
TemplateFile(const Path& path)
: mPath(path)
: mPath(path),
mRootNode(std::make_unique<TemplateNode>(nullptr))
{
}
@ -95,11 +179,23 @@ public:
std::cout << "Trying to load file at: " << mPath << std::endl;
mRawContent = File(mPath).readLines();
mWorkingLine = 0;
mWorkingNode = mRootNode.get();
mWorkingTextBody = std::make_unique<TemplateTextBody>(mWorkingNode);
for (const auto& line : mRawContent)
{
processLine(line);
mWorkingLine++;
}
onTextBodyFinished();
}
void onTextBodyFinished()
{
if (mWorkingTextBody->hasContent())
{
mWorkingNode->addChild(std::move(mWorkingTextBody));
mWorkingTextBody = std::make_unique<TemplateTextBody>(mWorkingNode);
}
}
void processLine(const std::string& line)
@ -139,6 +235,7 @@ public:
{
if (last_was_statement_rdelimiter)
{
onTextBodyFinished();
onFoundStatement(working_string);
last_was_statement_rdelimiter = false;
working_string = "";
@ -150,6 +247,7 @@ public:
}
else
{
onTextBodyFinished();
onFoundExpression(working_string);
last_was_expression_rdelimiter = false;
working_string = "";
@ -160,6 +258,11 @@ public:
working_string += c;
}
}
if (!working_string.empty())
{
mWorkingTextBody->addLine(working_string);
}
}
void onFoundStatement(const std::string& statement_string)
@ -172,7 +275,7 @@ public:
return;
}
const auto statement_type = statement_elements[0];
if (statement_type == "block")
if (statement_type == "block" && statement_elements.size() == 2)
{
onFoundBlock(statement_elements);
}
@ -185,22 +288,41 @@ public:
void onFoundBlock(const std::vector<std::string> args)
{
if (args.size() != 2)
{
return;
}
auto block = std::make_unique<TemplateBlock>(mWorkingNode, args[1]);
auto temp = block.get();
mWorkingNode->addChild(std::move(block));
mWorkingNode = temp;
}
void onFoundEndBlock(const std::vector<std::string> args)
{
mWorkingNode = mWorkingNode->getParent();
}
void onFoundExtends(const std::vector<std::string> args)
{
if (args.size() != 2)
{
return;
}
auto extends = std::make_unique<TemplateExtends>(mWorkingNode, args[1]);
mWorkingNode->addChild(std::move(extends));
}
void onFoundExpression(const std::string& expression_string)
{
std::cout << "Got Expression: " << expression_string << std::endl;
auto expression = std::make_unique<TemplateExpression>(mWorkingNode, expression_string);
mWorkingNode->addChild(std::move(expression));
}
void dumpContent()
{
auto content = mRootNode->getRawContent();
std::cout << content << std::endl;
}
private:
@ -209,7 +331,9 @@ private:
std::string mParentName;
std::vector<std::string> mRawContent;
std::vector<std::string> mProcessedContent;
std::vector<TemplateBlock> mBlocks;
std::unique_ptr<TemplateNode> mRootNode;
TemplateNode* mWorkingNode{ nullptr };
std::unique_ptr<TemplateTextBody> mWorkingTextBody;
};
class TemplatingEngine
@ -246,6 +370,7 @@ public:
void processTemplate(TemplateFile& file)
{
file.loadContent();
file.dumpContent();
}