aboutsummaryrefslogtreecommitdiff
path: root/lib/mstch/src/visitor
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mstch/src/visitor')
-rw-r--r--lib/mstch/src/visitor/get_token.hpp35
-rw-r--r--lib/mstch/src/visitor/has_token.hpp31
-rw-r--r--lib/mstch/src/visitor/is_node_empty.hpp41
-rw-r--r--lib/mstch/src/visitor/render_node.hpp56
-rw-r--r--lib/mstch/src/visitor/render_section.hpp57
5 files changed, 220 insertions, 0 deletions
diff --git a/lib/mstch/src/visitor/get_token.hpp b/lib/mstch/src/visitor/get_token.hpp
new file mode 100644
index 0000000..d41ab6e
--- /dev/null
+++ b/lib/mstch/src/visitor/get_token.hpp
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <boost/variant/static_visitor.hpp>
+
+#include "mstch/mstch.hpp"
+#include "has_token.hpp"
+
+namespace mstch {
+
+class get_token: public boost::static_visitor<const mstch::node&> {
+ public:
+ get_token(const std::string& token, const mstch::node& node):
+ m_token(token), m_node(node)
+ {
+ }
+
+ template<class T>
+ const mstch::node& operator()(const T&) const {
+ return m_node;
+ }
+
+ const mstch::node& operator()(const map& map) const {
+ return map.at(m_token);
+ }
+
+ const mstch::node& operator()(const std::shared_ptr<object>& object) const {
+ return object->at(m_token);
+ }
+
+ private:
+ const std::string& m_token;
+ const mstch::node& m_node;
+};
+
+}
diff --git a/lib/mstch/src/visitor/has_token.hpp b/lib/mstch/src/visitor/has_token.hpp
new file mode 100644
index 0000000..5ab30d4
--- /dev/null
+++ b/lib/mstch/src/visitor/has_token.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include <boost/variant/static_visitor.hpp>
+
+#include "mstch/mstch.hpp"
+
+namespace mstch {
+
+class has_token: public boost::static_visitor<bool> {
+ public:
+ has_token(const std::string& token): m_token(token) {
+ }
+
+ template<class T>
+ bool operator()(const T&) const {
+ return m_token == ".";
+ }
+
+ bool operator()(const map& map) const {
+ return map.count(m_token) == 1;
+ }
+
+ bool operator()(const std::shared_ptr<object>& object) const {
+ return object->has(m_token);
+ }
+
+ private:
+ const std::string& m_token;
+};
+
+}
diff --git a/lib/mstch/src/visitor/is_node_empty.hpp b/lib/mstch/src/visitor/is_node_empty.hpp
new file mode 100644
index 0000000..a0ae432
--- /dev/null
+++ b/lib/mstch/src/visitor/is_node_empty.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <boost/variant/static_visitor.hpp>
+
+#include "mstch/mstch.hpp"
+
+namespace mstch {
+
+class is_node_empty: public boost::static_visitor<bool> {
+ public:
+ template<class T>
+ bool operator()(const T&) const {
+ return false;
+ }
+
+ bool operator()(const std::nullptr_t&) const {
+ return true;
+ }
+
+ bool operator()(const int& value) const {
+ return value == 0;
+ }
+
+ bool operator()(const double& value) const {
+ return value == 0;
+ }
+
+ bool operator()(const bool& value) const {
+ return !value;
+ }
+
+ bool operator()(const std::string& value) const {
+ return value == "";
+ }
+
+ bool operator()(const array& array) const {
+ return array.size() == 0;
+ }
+};
+
+}
diff --git a/lib/mstch/src/visitor/render_node.hpp b/lib/mstch/src/visitor/render_node.hpp
new file mode 100644
index 0000000..633dd4d
--- /dev/null
+++ b/lib/mstch/src/visitor/render_node.hpp
@@ -0,0 +1,56 @@
+#pragma once
+
+#include <sstream>
+#include <boost/variant/static_visitor.hpp>
+
+#include "render_context.hpp"
+#include "mstch/mstch.hpp"
+#include "utils.hpp"
+
+namespace mstch {
+
+class render_node: public boost::static_visitor<std::string> {
+ public:
+ enum class flag { none, escape_html };
+ render_node(render_context& ctx, flag p_flag = flag::none):
+ m_ctx(ctx), m_flag(p_flag)
+ {
+ }
+
+ template<class T>
+ std::string operator()(const T&) const {
+ return "";
+ }
+
+ std::string operator()(const int& value) const {
+ return std::to_string(value);
+ }
+
+ std::string operator()(const double& value) const {
+ std::stringstream ss;
+ ss << value;
+ return ss.str();
+ }
+
+ std::string operator()(const bool& value) const {
+ return value ? "true" : "false";
+ }
+
+ std::string operator()(const lambda& value) const {
+ template_type interpreted{value([this](const mstch::node& n) {
+ return visit(render_node(m_ctx), n);
+ })};
+ auto rendered = render_context::push(m_ctx).render(interpreted);
+ return (m_flag == flag::escape_html) ? html_escape(rendered) : rendered;
+ }
+
+ std::string operator()(const std::string& value) const {
+ return (m_flag == flag::escape_html) ? html_escape(value) : value;
+ }
+
+ private:
+ render_context& m_ctx;
+ flag m_flag;
+};
+
+}
diff --git a/lib/mstch/src/visitor/render_section.hpp b/lib/mstch/src/visitor/render_section.hpp
new file mode 100644
index 0000000..f2d5259
--- /dev/null
+++ b/lib/mstch/src/visitor/render_section.hpp
@@ -0,0 +1,57 @@
+#pragma once
+
+#include <boost/variant/static_visitor.hpp>
+
+#include "render_context.hpp"
+#include "mstch/mstch.hpp"
+#include "utils.hpp"
+#include "render_node.hpp"
+
+namespace mstch {
+
+class render_section: public boost::static_visitor<std::string> {
+ public:
+ enum class flag { none, keep_array };
+ render_section(
+ render_context& ctx,
+ const template_type& section,
+ const delim_type& delims,
+ flag p_flag = flag::none):
+ m_ctx(ctx), m_section(section), m_delims(delims), m_flag(p_flag)
+ {
+ }
+
+ template<class T>
+ std::string operator()(const T& t) const {
+ return render_context::push(m_ctx, t).render(m_section);
+ }
+
+ std::string operator()(const lambda& fun) const {
+ std::string section_str;
+ for (auto& token: m_section)
+ section_str += token.raw();
+ template_type interpreted{fun([this](const mstch::node& n) {
+ return visit(render_node(m_ctx), n);
+ }, section_str), m_delims};
+ return render_context::push(m_ctx).render(interpreted);
+ }
+
+ std::string operator()(const array& array) const {
+ std::string out;
+ if (m_flag == flag::keep_array)
+ return render_context::push(m_ctx, array).render(m_section);
+ else
+ for (auto& item: array)
+ out += visit(render_section(
+ m_ctx, m_section, m_delims, flag::keep_array), item);
+ return out;
+ }
+
+ private:
+ render_context& m_ctx;
+ const template_type& m_section;
+ const delim_type& m_delims;
+ flag m_flag;
+};
+
+}