-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.cpp
65 lines (51 loc) · 1.77 KB
/
template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "template.h"
#include "actions/actions.h"
#include "actions/context.h"
#include "actions/types.h"
#include "grammar/grammar.h"
#include <map>
#include <string>
#include "finally.h"
namespace qrqma {
struct Template::Pimpl {
actions::Context symbol_context;
actions::Context render_context;
Pimpl(symbol::SymbolTable symbols, TemplateLoader loader, symbol::BlockTable blocks)
: symbol_context{{}, std::move(blocks)}
, render_context{&symbol_context}
{
for (auto& [k, v] : symbols) {
symbol_context.setSymbol(k, actions::types::ConstantExpression{[v=std::move(v)] { return v; }});
}
symbol_context.setTemplateLoader(std::move(loader));
}
};
namespace pegtl = tao::pegtl;
Template::Template(std::string_view input, symbol::SymbolTable symbols, TemplateLoader loader, symbol::BlockTable blocks)
: pimpl{std::make_unique<Pimpl>(std::move(symbols), std::move(loader), std::move(blocks))}
{
pegtl::parse<pegtl::if_must<grammar::grammar, pegtl::eof>, actions::action>(
pegtl::memory_input{input, ""},
pimpl->render_context
);
}
Template::Template(Template&& rhs) {
std::swap(pimpl, rhs.pimpl);
}
Template& Template::operator=(Template&& rhs) {
std::swap(pimpl, rhs.pimpl);
return *this;
}
Template::~Template() {}
std::string Template::operator()(symbol::SymbolTable symbols) const {
actions::Context rootC{};
for (auto& [k, v] : symbols) {
rootC.setSymbol(k, actions::types::ConstantExpression{ [v=std::move(v)] { return v; } });
}
pimpl->symbol_context.setParentContext(&rootC);
internal::Finally reset {[this]{
pimpl->symbol_context.setParentContext(nullptr);
}};
return std::move(pimpl->render_context(false).rendered);
}
} // namespace qrqma