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