1#ifndef INCLUDE_INJA_PARSER_HPP_
2#define INCLUDE_INJA_PARSER_HPP_
12#include "exceptions.hpp"
13#include "function_storage.hpp"
16#include "template.hpp"
20#include <nlohmann/json.hpp>
31 TemplateStorage &template_storage;
35 bool have_peek_tok {
false};
37 size_t current_paren_level {0};
38 size_t current_bracket_level {0};
39 size_t current_brace_level {0};
41 nonstd::string_view json_literal_start;
45 std::stack<std::pair<FunctionNode*, size_t>> function_stack;
46 std::vector<std::shared_ptr<ExpressionNode>> arguments;
48 std::stack<std::shared_ptr<FunctionNode>> operator_stack;
49 std::stack<IfStatementNode*> if_statement_stack;
50 std::stack<ForStatementNode*> for_statement_stack;
51 std::stack<BlockStatementNode*> block_statement_stack;
53 inline void throw_parser_error(
const std::string &message) {
54 INJA_THROW(
ParserError(message, lexer.current_position()));
57 inline void get_next_token() {
60 have_peek_tok =
false;
66 inline void get_peek_token() {
68 peek_tok = lexer.scan();
73 inline void add_json_literal(
const char* content_ptr) {
74 nonstd::string_view json_text(json_literal_start.data(), tok.text.data() - json_literal_start.data() + tok.text.size());
75 arguments.emplace_back(std::make_shared<LiteralNode>(json::parse(json_text), json_text.data() - content_ptr));
78 inline void add_operator() {
79 auto function = operator_stack.top();
82 for (
int i = 0; i < function->number_args; ++i) {
83 function->arguments.insert(function->arguments.begin(), arguments.back());
86 arguments.emplace_back(function);
89 void add_to_template_storage(nonstd::string_view path, std::string& template_name) {
90 if (config.search_included_templates_in_files && template_storage.find(template_name) == template_storage.end()) {
92 template_name =
static_cast<std::string
>(path) + template_name;
93 if (template_name.compare(0, 2,
"./") == 0) {
94 template_name.erase(0, 2);
97 if (template_storage.find(template_name) == template_storage.end()) {
98 auto include_template =
Template(load_file(template_name));
99 template_storage.emplace(template_name, include_template);
100 parse_into_template(template_storage[template_name], template_name);
105 bool parse_expression(
Template &tmpl, Token::Kind closing) {
106 while (tok.kind != closing && tok.kind != Token::Kind::Eof) {
109 case Token::Kind::String: {
110 if (current_brace_level == 0 && current_bracket_level == 0) {
111 json_literal_start = tok.text;
112 add_json_literal(tmpl.content.c_str());
116 case Token::Kind::Number: {
117 if (current_brace_level == 0 && current_bracket_level == 0) {
118 json_literal_start = tok.text;
119 add_json_literal(tmpl.content.c_str());
123 case Token::Kind::LeftBracket: {
124 if (current_brace_level == 0 && current_bracket_level == 0) {
125 json_literal_start = tok.text;
127 current_bracket_level += 1;
130 case Token::Kind::LeftBrace: {
131 if (current_brace_level == 0 && current_bracket_level == 0) {
132 json_literal_start = tok.text;
134 current_brace_level += 1;
137 case Token::Kind::RightBracket: {
138 if (current_bracket_level == 0) {
139 throw_parser_error(
"unexpected ']'");
142 current_bracket_level -= 1;
143 if (current_brace_level == 0 && current_bracket_level == 0) {
144 add_json_literal(tmpl.content.c_str());
148 case Token::Kind::RightBrace: {
149 if (current_brace_level == 0) {
150 throw_parser_error(
"unexpected '}'");
153 current_brace_level -= 1;
154 if (current_brace_level == 0 && current_bracket_level == 0) {
155 add_json_literal(tmpl.content.c_str());
159 case Token::Kind::Id: {
163 if (tok.text ==
static_cast<decltype(tok.text)
>(
"true") || tok.text ==
static_cast<decltype(tok.text)
>(
"false") || tok.text ==
static_cast<decltype(tok.text)
>(
"null")) {
164 if (current_brace_level == 0 && current_bracket_level == 0) {
165 json_literal_start = tok.text;
166 add_json_literal(tmpl.content.c_str());
170 }
else if (tok.text ==
"and" || tok.text ==
"or" || tok.text ==
"in" || tok.text ==
"not") {
174 }
else if (peek_tok.kind == Token::Kind::LeftParen) {
175 operator_stack.emplace(std::make_shared<FunctionNode>(
static_cast<std::string
>(tok.text), tok.text.data() - tmpl.content.c_str()));
176 function_stack.emplace(operator_stack.top().get(), current_paren_level);
180 arguments.emplace_back(std::make_shared<JsonNode>(
static_cast<std::string
>(tok.text), tok.text.data() - tmpl.content.c_str()));
185 case Token::Kind::Equal:
186 case Token::Kind::NotEqual:
187 case Token::Kind::GreaterThan:
188 case Token::Kind::GreaterEqual:
189 case Token::Kind::LessThan:
190 case Token::Kind::LessEqual:
191 case Token::Kind::Plus:
192 case Token::Kind::Minus:
193 case Token::Kind::Times:
194 case Token::Kind::Slash:
195 case Token::Kind::Power:
196 case Token::Kind::Percent:
197 case Token::Kind::Dot: {
200 FunctionStorage::Operation operation;
202 case Token::Kind::Id: {
203 if (tok.text ==
"and") {
204 operation = FunctionStorage::Operation::And;
205 }
else if (tok.text ==
"or") {
206 operation = FunctionStorage::Operation::Or;
207 }
else if (tok.text ==
"in") {
208 operation = FunctionStorage::Operation::In;
209 }
else if (tok.text ==
"not") {
210 operation = FunctionStorage::Operation::Not;
212 throw_parser_error(
"unknown operator in parser.");
215 case Token::Kind::Equal: {
216 operation = FunctionStorage::Operation::Equal;
218 case Token::Kind::NotEqual: {
219 operation = FunctionStorage::Operation::NotEqual;
221 case Token::Kind::GreaterThan: {
222 operation = FunctionStorage::Operation::Greater;
224 case Token::Kind::GreaterEqual: {
225 operation = FunctionStorage::Operation::GreaterEqual;
227 case Token::Kind::LessThan: {
228 operation = FunctionStorage::Operation::Less;
230 case Token::Kind::LessEqual: {
231 operation = FunctionStorage::Operation::LessEqual;
233 case Token::Kind::Plus: {
234 operation = FunctionStorage::Operation::Add;
236 case Token::Kind::Minus: {
237 operation = FunctionStorage::Operation::Subtract;
239 case Token::Kind::Times: {
240 operation = FunctionStorage::Operation::Multiplication;
242 case Token::Kind::Slash: {
243 operation = FunctionStorage::Operation::Division;
245 case Token::Kind::Power: {
246 operation = FunctionStorage::Operation::Power;
248 case Token::Kind::Percent: {
249 operation = FunctionStorage::Operation::Modulo;
251 case Token::Kind::Dot: {
252 operation = FunctionStorage::Operation::AtId;
255 throw_parser_error(
"unknown operator in parser.");
258 auto function_node = std::make_shared<FunctionNode>(operation, tok.text.data() - tmpl.content.c_str());
260 while (!operator_stack.empty() && ((operator_stack.top()->precedence > function_node->precedence) || (operator_stack.top()->precedence == function_node->precedence && function_node->associativity == FunctionNode::Associativity::Left)) && (operator_stack.top()->operation != FunctionStorage::Operation::ParenLeft)) {
264 operator_stack.emplace(function_node);
267 case Token::Kind::Comma: {
268 if (current_brace_level == 0 && current_bracket_level == 0) {
269 if (function_stack.empty()) {
270 throw_parser_error(
"unexpected ','");
273 function_stack.top().first->number_args += 1;
277 case Token::Kind::Colon: {
278 if (current_brace_level == 0 && current_bracket_level == 0) {
279 throw_parser_error(
"unexpected ':'");
283 case Token::Kind::LeftParen: {
284 current_paren_level += 1;
285 operator_stack.emplace(std::make_shared<FunctionNode>(FunctionStorage::Operation::ParenLeft, tok.text.data() - tmpl.content.c_str()));
288 if (peek_tok.kind == Token::Kind::RightParen) {
289 if (!function_stack.empty() && function_stack.top().second == current_paren_level - 1) {
290 function_stack.top().first->number_args = 0;
295 case Token::Kind::RightParen: {
296 current_paren_level -= 1;
297 while (!operator_stack.empty() && operator_stack.top()->operation != FunctionStorage::Operation::ParenLeft) {
301 if (!operator_stack.empty() && operator_stack.top()->operation == FunctionStorage::Operation::ParenLeft) {
302 operator_stack.pop();
305 if (!function_stack.empty() && function_stack.top().second == current_paren_level) {
306 auto func = function_stack.top().first;
307 auto function_data = function_storage.find_function(func->name, func->number_args);
308 if (function_data.operation == FunctionStorage::Operation::None) {
309 throw_parser_error(
"unknown function " + func->name);
311 func->operation = function_data.operation;
312 if (function_data.operation == FunctionStorage::Operation::Callback) {
313 func->callback = function_data.callback;
316 if (operator_stack.empty()) {
317 throw_parser_error(
"internal error at function " + func->name);
321 function_stack.pop();
331 while (!operator_stack.empty()) {
335 if (arguments.size() == 1) {
336 current_expression_list->root = arguments[0];
339 }
else if (arguments.size() > 1) {
340 throw_parser_error(
"malformed expression");
346 bool parse_statement(
Template &tmpl, Token::Kind closing, nonstd::string_view path) {
347 if (tok.kind != Token::Kind::Id) {
351 if (tok.text ==
static_cast<decltype(tok.text)
>(
"if")) {
354 auto if_statement_node = std::make_shared<IfStatementNode>(current_block, tok.text.data() - tmpl.content.c_str());
355 current_block->nodes.emplace_back(if_statement_node);
356 if_statement_stack.emplace(if_statement_node.get());
357 current_block = &if_statement_node->true_statement;
358 current_expression_list = &if_statement_node->condition;
360 if (!parse_expression(tmpl, closing)) {
364 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"else")) {
365 if (if_statement_stack.empty()) {
366 throw_parser_error(
"else without matching if");
368 auto &if_statement_data = if_statement_stack.top();
371 if_statement_data->has_false_statement =
true;
372 current_block = &if_statement_data->false_statement;
375 if (tok.kind == Token::Kind::Id && tok.text ==
static_cast<decltype(tok.text)
>(
"if")) {
378 auto if_statement_node = std::make_shared<IfStatementNode>(
true, current_block, tok.text.data() - tmpl.content.c_str());
379 current_block->nodes.emplace_back(if_statement_node);
380 if_statement_stack.emplace(if_statement_node.get());
381 current_block = &if_statement_node->true_statement;
382 current_expression_list = &if_statement_node->condition;
384 if (!parse_expression(tmpl, closing)) {
389 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"endif")) {
390 if (if_statement_stack.empty()) {
391 throw_parser_error(
"endif without matching if");
395 while (if_statement_stack.top()->is_nested) {
396 if_statement_stack.pop();
399 auto &if_statement_data = if_statement_stack.top();
402 current_block = if_statement_data->parent;
403 if_statement_stack.pop();
405 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"block")) {
408 if (tok.kind != Token::Kind::Id) {
409 throw_parser_error(
"expected block name, got '" + tok.describe() +
"'");
412 const std::string block_name =
static_cast<std::string
>(tok.text);
414 auto block_statement_node = std::make_shared<BlockStatementNode>(current_block, block_name, tok.text.data() - tmpl.content.c_str());
415 current_block->nodes.emplace_back(block_statement_node);
416 block_statement_stack.emplace(block_statement_node.get());
417 current_block = &block_statement_node->block;
418 auto success = tmpl.block_storage.emplace(block_name, block_statement_node);
419 if (!success.second) {
420 throw_parser_error(
"block with the name '" + block_name +
"' does already exist");
425 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"endblock")) {
426 if (block_statement_stack.empty()) {
427 throw_parser_error(
"endblock without matching block");
430 auto &block_statement_data = block_statement_stack.top();
433 current_block = block_statement_data->parent;
434 block_statement_stack.pop();
436 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"for")) {
440 if (tok.kind != Token::Kind::Id) {
441 throw_parser_error(
"expected id, got '" + tok.describe() +
"'");
444 Token value_token = tok;
448 std::shared_ptr<ForStatementNode> for_statement_node;
449 if (tok.kind == Token::Kind::Comma) {
451 if (tok.kind != Token::Kind::Id) {
452 throw_parser_error(
"expected id, got '" + tok.describe() +
"'");
455 Token key_token = std::move(value_token);
459 for_statement_node = std::make_shared<ForObjectStatementNode>(
static_cast<std::string
>(key_token.text),
static_cast<std::string
>(value_token.text), current_block, tok.text.data() - tmpl.content.c_str());
463 for_statement_node = std::make_shared<ForArrayStatementNode>(
static_cast<std::string
>(value_token.text), current_block, tok.text.data() - tmpl.content.c_str());
466 current_block->nodes.emplace_back(for_statement_node);
467 for_statement_stack.emplace(for_statement_node.get());
468 current_block = &for_statement_node->body;
469 current_expression_list = &for_statement_node->condition;
471 if (tok.kind != Token::Kind::Id || tok.text !=
static_cast<decltype(tok.text)
>(
"in")) {
472 throw_parser_error(
"expected 'in', got '" + tok.describe() +
"'");
476 if (!parse_expression(tmpl, closing)) {
480 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"endfor")) {
481 if (for_statement_stack.empty()) {
482 throw_parser_error(
"endfor without matching for");
485 auto &for_statement_data = for_statement_stack.top();
488 current_block = for_statement_data->parent;
489 for_statement_stack.pop();
491 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"include")) {
494 if (tok.kind != Token::Kind::String) {
495 throw_parser_error(
"expected string, got '" + tok.describe() +
"'");
498 std::string template_name = json::parse(tok.text).get_ref<
const std::string &>();
499 add_to_template_storage(path, template_name);
501 current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
505 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"extends")) {
508 if (tok.kind != Token::Kind::String) {
509 throw_parser_error(
"expected string, got '" + tok.describe() +
"'");
512 std::string template_name = json::parse(tok.text).get_ref<
const std::string &>();
513 add_to_template_storage(path, template_name);
515 current_block->nodes.emplace_back(std::make_shared<ExtendsStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
519 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"set")) {
522 if (tok.kind != Token::Kind::Id) {
523 throw_parser_error(
"expected variable name, got '" + tok.describe() +
"'");
526 std::string key =
static_cast<std::string
>(tok.text);
529 auto set_statement_node = std::make_shared<SetStatementNode>(key, tok.text.data() - tmpl.content.c_str());
530 current_block->nodes.emplace_back(set_statement_node);
531 current_expression_list = &set_statement_node->expression;
533 if (tok.text !=
static_cast<decltype(tok.text)
>(
"=")) {
534 throw_parser_error(
"expected '=', got '" + tok.describe() +
"'");
538 if (!parse_expression(tmpl, closing)) {
548 void parse_into(
Template &tmpl, nonstd::string_view path) {
549 lexer.start(tmpl.content);
550 current_block = &tmpl.root;
555 case Token::Kind::Eof: {
556 if (!if_statement_stack.empty()) {
557 throw_parser_error(
"unmatched if");
559 if (!for_statement_stack.empty()) {
560 throw_parser_error(
"unmatched for");
563 case Token::Kind::Text: {
564 current_block->nodes.emplace_back(std::make_shared<TextNode>(tok.text.data() - tmpl.content.c_str(), tok.text.size()));
566 case Token::Kind::StatementOpen: {
568 if (!parse_statement(tmpl, Token::Kind::StatementClose, path)) {
569 throw_parser_error(
"expected statement, got '" + tok.describe() +
"'");
571 if (tok.kind != Token::Kind::StatementClose) {
572 throw_parser_error(
"expected statement close, got '" + tok.describe() +
"'");
575 case Token::Kind::LineStatementOpen: {
577 if (!parse_statement(tmpl, Token::Kind::LineStatementClose, path)) {
578 throw_parser_error(
"expected statement, got '" + tok.describe() +
"'");
580 if (tok.kind != Token::Kind::LineStatementClose && tok.kind != Token::Kind::Eof) {
581 throw_parser_error(
"expected line statement close, got '" + tok.describe() +
"'");
584 case Token::Kind::ExpressionOpen: {
587 auto expression_list_node = std::make_shared<ExpressionListNode>(tok.text.data() - tmpl.content.c_str());
588 current_block->nodes.emplace_back(expression_list_node);
589 current_expression_list = expression_list_node.get();
591 if (!parse_expression(tmpl, Token::Kind::ExpressionClose)) {
592 throw_parser_error(
"expected expression, got '" + tok.describe() +
"'");
595 if (tok.kind != Token::Kind::ExpressionClose) {
596 throw_parser_error(
"expected expression close, got '" + tok.describe() +
"'");
599 case Token::Kind::CommentOpen: {
601 if (tok.kind != Token::Kind::CommentClose) {
602 throw_parser_error(
"expected comment close, got '" + tok.describe() +
"'");
606 throw_parser_error(
"unexpected token '" + tok.describe() +
"'");
615 TemplateStorage &template_storage,
const FunctionStorage &function_storage)
616 : config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) { }
618 Template parse(nonstd::string_view input, nonstd::string_view path) {
619 auto result =
Template(
static_cast<std::string
>(input));
620 parse_into(result, path);
624 Template parse(nonstd::string_view input) {
625 return parse(input,
"./");
628 void parse_into_template(
Template& tmpl, nonstd::string_view filename) {
629 nonstd::string_view path = filename.substr(0, filename.find_last_of(
"/\\") + 1);
632 auto sub_parser =
Parser(config, lexer.get_config(), template_storage, function_storage);
633 sub_parser.parse_into(tmpl, path);
636 std::string load_file(nonstd::string_view filename) {
638 open_file_or_throw(
static_cast<std::string
>(filename), file);
639 std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
Class for builtin functions and user-defined callbacks.
Definition: function_storage.hpp:19
Class for lexing an inja Template.
Definition: lexer.hpp:16
Class for parsing an inja Template.
Definition: parser.hpp:27
Class for lexer configuration.
Definition: config.hpp:14
Class for parser configuration.
Definition: config.hpp:66
Definition: exceptions.hpp:29
The main inja Template.
Definition: template.hpp:18
Helper-class for the inja Lexer.
Definition: token.hpp:13