partial lambda support
This commit is contained in:
parent
ba8a8bee91
commit
61a700479d
@ -4,12 +4,26 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mstch {
|
||||
// booleans must be wrapped, because std::function is implicitly convertible
|
||||
// to a bool.
|
||||
class boolean {
|
||||
private:
|
||||
bool state;
|
||||
public:
|
||||
boolean(bool b): state(b) {}
|
||||
operator bool() const { return state; }
|
||||
};
|
||||
|
||||
using renderer = std::function<std::string(const std::string&)>;
|
||||
using string_lambda = std::function<std::string()>;
|
||||
using renderer_lambda = std::function<std::function<std::string(const std::string&,renderer)>()>;
|
||||
using node = boost::make_recursive_variant<
|
||||
boost::blank, std::string, int, bool,
|
||||
boost::blank, std::string, int, boolean, string_lambda, renderer_lambda,
|
||||
std::map<const std::string,boost::recursive_variant_>,
|
||||
std::vector<boost::recursive_variant_>>::type;
|
||||
using object = std::map<const std::string,node>;
|
||||
|
@ -25,3 +25,11 @@ bool visitor::is_node_empty::operator()(const array& arr) const {
|
||||
bool visitor::is_node_empty::operator()(const object& obj) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool visitor::is_node_empty::operator()(const string_lambda& lambda) const {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
bool visitor::is_node_empty::operator()(const renderer_lambda& lambda) const {
|
||||
return false; // TODO
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ namespace mstch {
|
||||
bool operator()(const std::string& str) const;
|
||||
bool operator()(const array& arr) const;
|
||||
bool operator()(const object& obj) const;
|
||||
bool operator()(const string_lambda& lambda) const;
|
||||
bool operator()(const renderer_lambda& lambda) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -31,3 +31,17 @@ std::string visitor::render_node::operator()(const array& arr) const {
|
||||
std::string visitor::render_node::operator()(const object& obj) const {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string visitor::render_node::operator()(
|
||||
const string_lambda& lambda) const
|
||||
{
|
||||
return this->operator()(lambda());
|
||||
}
|
||||
|
||||
std::string visitor::render_node::operator()(
|
||||
const renderer_lambda& lambda) const
|
||||
{
|
||||
return this->operator()((lambda())("", [](const std::string& text) {
|
||||
return std::string{""};
|
||||
}));
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ namespace mstch {
|
||||
std::string operator()(const std::string& str) const;
|
||||
std::string operator()(const array& arr) const;
|
||||
std::string operator()(const object& obj) const;
|
||||
std::string operator()(const string_lambda& lambda) const;
|
||||
std::string operator()(const renderer_lambda& lambda) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -46,3 +46,17 @@ std::string visitor::render_section::operator()(const array& a) const {
|
||||
render_section(context, section, {flag::keep_array}), item);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string visitor::render_section::operator()(
|
||||
const string_lambda& lambda) const
|
||||
{
|
||||
return lambda();
|
||||
}
|
||||
|
||||
std::string visitor::render_section::operator()(
|
||||
const renderer_lambda& lambda) const
|
||||
{
|
||||
return (lambda())(section, [&](const std::string& text) {
|
||||
return render_context(mstch::object{}, context).render(text);
|
||||
});
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ namespace mstch {
|
||||
std::string operator()(const std::string& str) const;
|
||||
std::string operator()(const array& arr) const;
|
||||
std::string operator()(const object& obj) const;
|
||||
std::string operator()(const string_lambda& lambda) const;
|
||||
std::string operator()(const renderer_lambda& lambda) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -41,3 +41,11 @@ std::string visitor::to_json::operator()(const object& obj) const {
|
||||
out << "}";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string visitor::to_json::operator()(const string_lambda& lambda) const {
|
||||
return ""; // TODO
|
||||
}
|
||||
|
||||
std::string visitor::to_json::operator()(const renderer_lambda& lambda) const {
|
||||
return ""; // TODO
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ namespace mstch {
|
||||
std::string operator()(const std::string& str) const;
|
||||
std::string operator()(const array& arr) const;
|
||||
std::string operator()(const object& obj) const;
|
||||
std::string operator()(const string_lambda& lambda) const;
|
||||
std::string operator()(const renderer_lambda& lambda) const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
5
test/data/comments.data
Normal file
5
test/data/comments.data
Normal file
@ -0,0 +1,5 @@
|
||||
mstch::object{
|
||||
{"title", []() {
|
||||
return std::string{"A Comedy of Errors"};
|
||||
}}
|
||||
};
|
6
test/data/escaped.data
Normal file
6
test/data/escaped.data
Normal file
@ -0,0 +1,6 @@
|
||||
mstch::object{
|
||||
{"title", []() {
|
||||
return "Bear > Shark";
|
||||
}},
|
||||
{"entities", std::string{"" \"'<>/"}}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
({
|
||||
title: function () {
|
||||
return "A Comedy of Errors";
|
||||
}
|
||||
})
|
@ -1,6 +0,0 @@
|
||||
({
|
||||
title: function () {
|
||||
return "Bear > Shark";
|
||||
},
|
||||
entities: "" \"'<>/"
|
||||
})
|
@ -1,8 +0,0 @@
|
||||
({
|
||||
bold: function () {
|
||||
return function (text, render) {
|
||||
return '<b>' + render(text) + '</b>';
|
||||
};
|
||||
},
|
||||
person: { name: 'Jonas' }
|
||||
});
|
@ -1,6 +0,0 @@
|
||||
({
|
||||
title: function () {
|
||||
return "Welcome";
|
||||
},
|
||||
again: "Goodbye"
|
||||
})
|
@ -1,7 +0,0 @@
|
||||
({
|
||||
bold: function(){
|
||||
return function(text, render) {
|
||||
return "<b>" + render(text) + "</b>";
|
||||
}
|
||||
}
|
||||
})
|
@ -1,5 +0,0 @@
|
||||
({
|
||||
title: function () {
|
||||
return "Bear > Shark";
|
||||
}
|
||||
})
|
8
test/data/nested_higher_order_sections.data
Normal file
8
test/data/nested_higher_order_sections.data
Normal file
@ -0,0 +1,8 @@
|
||||
mstch::object{
|
||||
{"bold", []() {
|
||||
return [](const std::string& text, mstch::renderer render) {
|
||||
return std::string{"<b>"} + render(text) + std::string{"</b>"};
|
||||
};
|
||||
}},
|
||||
{"person", mstch::object{{"name", std::string{"Jonas"}}}}
|
||||
}
|
6
test/data/partial_template.data
Normal file
6
test/data/partial_template.data
Normal file
@ -0,0 +1,6 @@
|
||||
mstch::object{
|
||||
{"title", []() {
|
||||
return std::string{"Welcome"};
|
||||
}},
|
||||
{"again", std::string{"Goodbye"}}
|
||||
}
|
7
test/data/section_functions_in_partials.data
Normal file
7
test/data/section_functions_in_partials.data
Normal file
@ -0,0 +1,7 @@
|
||||
mstch::object{
|
||||
{"bold", []() {
|
||||
return [](const std::string& text, mstch::renderer render) {
|
||||
return std::string{"<b>"} + render(text) + std::string{"</b>"};
|
||||
};
|
||||
}}
|
||||
}
|
5
test/data/unescaped.data
Normal file
5
test/data/unescaped.data
Normal file
@ -0,0 +1,5 @@
|
||||
mstch::object{
|
||||
{"title", [](){
|
||||
return std::string{"Bear > Shark"};
|
||||
}}
|
||||
}
|
@ -20,6 +20,7 @@ MSTCH_TEST(array_of_strings)
|
||||
MSTCH_TEST(backslashes)
|
||||
MSTCH_TEST(bug_11_eating_whitespace)
|
||||
MSTCH_TEST(bug_length_property)
|
||||
MSTCH_TEST(comments)
|
||||
MSTCH_TEST(context_lookup)
|
||||
MSTCH_TEST(disappearing_whitespace)
|
||||
MSTCH_TEST(double_render)
|
||||
@ -28,6 +29,7 @@ MSTCH_TEST(empty_sections)
|
||||
MSTCH_TEST(empty_string)
|
||||
MSTCH_TEST(empty_template)
|
||||
MSTCH_TEST(error_not_found)
|
||||
MSTCH_TEST(escaped)
|
||||
MSTCH_TEST(falsy)
|
||||
MSTCH_TEST(falsy_array)
|
||||
MSTCH_TEST(grandparent_context)
|
||||
@ -37,6 +39,7 @@ MSTCH_TEST(inverted_section)
|
||||
MSTCH_TEST(keys_with_questionmarks)
|
||||
MSTCH_TEST(multiline_comment)
|
||||
MSTCH_TEST(nested_dot)
|
||||
MSTCH_TEST(nested_higher_order_sections)
|
||||
MSTCH_TEST(nested_iterating)
|
||||
MSTCH_TEST(nesting)
|
||||
MSTCH_TEST(nesting_same_name)
|
||||
@ -47,11 +50,14 @@ MSTCH_PARTIAL_TEST(partial_array)
|
||||
MSTCH_PARTIAL_TEST(partial_array_of_partials)
|
||||
MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit)
|
||||
MSTCH_PARTIAL_TEST(partial_empty)
|
||||
MSTCH_PARTIAL_TEST(partial_template)
|
||||
MSTCH_TEST(recursion_with_same_names)
|
||||
MSTCH_TEST(reuse_of_enumerables)
|
||||
MSTCH_TEST(section_as_context)
|
||||
MSTCH_PARTIAL_TEST(section_functions_in_partials)
|
||||
MSTCH_TEST(string_as_context)
|
||||
MSTCH_TEST(two_in_a_row)
|
||||
MSTCH_TEST(two_sections)
|
||||
MSTCH_TEST(unescaped)
|
||||
MSTCH_TEST(whitespace)
|
||||
MSTCH_TEST(zero_view)
|
||||
|
Loading…
x
Reference in New Issue
Block a user