-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
78 lines (66 loc) · 1.6 KB
/
Copy pathEnvironment.cpp
File metadata and controls
78 lines (66 loc) · 1.6 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
*
* @file Environment.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira.
* All rights reserved.
* https://github.com/vixcpp/vix
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/template/Environment.hpp>
#include <vix/template/Compiler.hpp>
#include <vix/template/Error.hpp>
#include <vix/template/Lexer.hpp>
#include <vix/template/Parser.hpp>
#include <utility>
namespace vix::template_
{
Environment::Environment(std::shared_ptr<Loader> loader)
: loader_(std::move(loader))
{
if (!loader_)
{
throw LoaderError("environment requires a valid loader");
}
}
void Environment::set_auto_escape(bool enabled) noexcept
{
auto_escape_ = enabled;
}
bool Environment::auto_escape() const noexcept
{
return auto_escape_;
}
Template Environment::load(const std::string &name) const
{
if (!loader_)
{
throw LoaderError("no loader configured");
}
if (!loader_->exists(name))
{
throw LoaderError("template not found: " + name);
}
// 1. Load raw template source
std::string source = loader_->load(name);
// 2. Lexing
Lexer lexer(std::move(source));
auto tokens = lexer.tokenize();
// 3. Parsing
Parser parser(std::move(tokens));
RootNode root = parser.parse();
// 4. Compilation
Compiler compiler;
return compiler.compile(name, std::move(root), loader_);
}
const std::shared_ptr<Loader> &Environment::loader() const noexcept
{
return loader_;
}
} // namespace vix::template_