30 using Arguments = std::vector<std::shared_ptr<ExpressionNode>>;
31 using OperatorStack = std::stack<std::shared_ptr<FunctionNode>>;
36 TemplateStorage& template_storage;
40 bool have_peek_tok {
false};
42 std::string_view literal_start;
47 std::stack<IfStatementNode*> if_statement_stack;
48 std::stack<ForStatementNode*> for_statement_stack;
49 std::stack<BlockStatementNode*> block_statement_stack;
51 void throw_parser_error(
const std::string& message)
const {
52 INJA_THROW(
ParserError(message, lexer.current_position()));
55 void get_next_token() {
58 have_peek_tok =
false;
64 void get_peek_token() {
66 peek_tok = lexer.scan();
71 void add_literal(Arguments &arguments,
const char* content_ptr) {
72 const std::string_view data_text(literal_start.data(), tok.text.data() - literal_start.data() + tok.text.size());
73 arguments.emplace_back(std::make_shared<LiteralNode>(data_text, data_text.data() - content_ptr));
76 void add_operator(Arguments &arguments, OperatorStack &operator_stack) {
77 auto function = operator_stack.top();
80 if (
static_cast<int>(arguments.size()) < function->number_args) {
81 throw_parser_error(
"too few arguments");
84 for (
int i = 0; i < function->number_args; ++i) {
85 function->arguments.insert(function->arguments.begin(), arguments.back());
88 arguments.emplace_back(function);
91 void add_to_template_storage(
const std::filesystem::path& path, std::string& template_name) {
92 if (template_storage.find(template_name) != template_storage.end()) {
96 const std::string original_name = template_name;
98 if (config.search_included_templates_in_files) {
100 template_name = (path / original_name).
string();
101 if (template_name.compare(0, 2,
"./") == 0) {
102 template_name.erase(0, 2);
105 if (template_storage.find(template_name) == template_storage.end()) {
108 file.open(template_name);
110 const std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
112 auto include_template =
Template(text);
113 template_storage.emplace(template_name, include_template);
114 parse_into_template(template_storage[template_name], template_name);
116 }
else if (!config.include_callback) {
117 INJA_THROW(
FileError(
"failed accessing file at '" + template_name +
"'"));
123 if (config.include_callback) {
124 auto include_template = config.include_callback(path, original_name);
125 template_storage.emplace(template_name, include_template);
129 std::string parse_filename()
const {
130 if (tok.kind != Token::Kind::String) {
131 throw_parser_error(
"expected string, got '" + tok.describe() +
"'");
134 if (tok.text.length() < 2) {
135 throw_parser_error(
"expected filename, got '" +
static_cast<std::string
>(tok.text) +
"'");
139 return std::string {tok.text.substr(1, tok.text.length() - 2)};
142 bool parse_expression(
Template& tmpl, Token::Kind closing) {
143 current_expression_list->root = parse_expression(tmpl);
144 return tok.kind == closing;
147 std::shared_ptr<ExpressionNode> parse_expression(
Template& tmpl) {
148 size_t current_bracket_level {0};
149 size_t current_brace_level {0};
151 OperatorStack operator_stack;
153 while (tok.kind != Token::Kind::Eof) {
156 case Token::Kind::String: {
157 if (current_brace_level == 0 && current_bracket_level == 0) {
158 literal_start = tok.text;
159 add_literal(arguments, tmpl.content.c_str());
162 case Token::Kind::Number: {
163 if (current_brace_level == 0 && current_bracket_level == 0) {
164 literal_start = tok.text;
165 add_literal(arguments, tmpl.content.c_str());
168 case Token::Kind::LeftBracket: {
169 if (current_brace_level == 0 && current_bracket_level == 0) {
170 literal_start = tok.text;
172 current_bracket_level += 1;
174 case Token::Kind::LeftBrace: {
175 if (current_brace_level == 0 && current_bracket_level == 0) {
176 literal_start = tok.text;
178 current_brace_level += 1;
180 case Token::Kind::RightBracket: {
181 if (current_bracket_level == 0) {
182 throw_parser_error(
"unexpected ']'");
185 current_bracket_level -= 1;
186 if (current_brace_level == 0 && current_bracket_level == 0) {
187 add_literal(arguments, tmpl.content.c_str());
190 case Token::Kind::RightBrace: {
191 if (current_brace_level == 0) {
192 throw_parser_error(
"unexpected '}'");
195 current_brace_level -= 1;
196 if (current_brace_level == 0 && current_bracket_level == 0) {
197 add_literal(arguments, tmpl.content.c_str());
200 case Token::Kind::Id: {
204 if (tok.text ==
static_cast<decltype(tok.text)
>(
"true") || tok.text ==
static_cast<decltype(tok.text)
>(
"false") ||
205 tok.text ==
static_cast<decltype(tok.text)
>(
"null")) {
206 if (current_brace_level == 0 && current_bracket_level == 0) {
207 literal_start = tok.text;
208 add_literal(arguments, tmpl.content.c_str());
212 }
else if (tok.text ==
"and" || tok.text ==
"or" || tok.text ==
"in" || tok.text ==
"not") {
216 }
else if (peek_tok.kind == Token::Kind::LeftParen) {
217 auto func = std::make_shared<FunctionNode>(tok.text, tok.text.data() - tmpl.content.c_str());
221 auto expr = parse_expression(tmpl);
225 func->number_args += 1;
226 func->arguments.emplace_back(expr);
227 }
while (tok.kind == Token::Kind::Comma);
228 if (tok.kind != Token::Kind::RightParen) {
229 throw_parser_error(
"expected right parenthesis, got '" + tok.describe() +
"'");
232 auto function_data = function_storage.find_function(func->name, func->number_args);
233 if (function_data.operation == FunctionStorage::Operation::None) {
234 throw_parser_error(
"unknown function " + func->name);
236 func->operation = function_data.operation;
237 if (function_data.operation == FunctionStorage::Operation::Callback) {
238 func->callback = function_data.callback;
240 arguments.emplace_back(func);
244 arguments.emplace_back(std::make_shared<DataNode>(
static_cast<std::string
>(tok.text), tok.text.data() - tmpl.content.c_str()));
249 case Token::Kind::Equal:
250 case Token::Kind::NotEqual:
251 case Token::Kind::GreaterThan:
252 case Token::Kind::GreaterEqual:
253 case Token::Kind::LessThan:
254 case Token::Kind::LessEqual:
255 case Token::Kind::Plus:
256 case Token::Kind::Minus:
257 case Token::Kind::Times:
258 case Token::Kind::Slash:
259 case Token::Kind::Power:
260 case Token::Kind::Percent:
261 case Token::Kind::Dot: {
264 FunctionStorage::Operation operation;
266 case Token::Kind::Id: {
267 if (tok.text ==
"and") {
268 operation = FunctionStorage::Operation::And;
269 }
else if (tok.text ==
"or") {
270 operation = FunctionStorage::Operation::Or;
271 }
else if (tok.text ==
"in") {
272 operation = FunctionStorage::Operation::In;
273 }
else if (tok.text ==
"not") {
274 operation = FunctionStorage::Operation::Not;
276 throw_parser_error(
"unknown operator in parser.");
279 case Token::Kind::Equal: {
280 operation = FunctionStorage::Operation::Equal;
282 case Token::Kind::NotEqual: {
283 operation = FunctionStorage::Operation::NotEqual;
285 case Token::Kind::GreaterThan: {
286 operation = FunctionStorage::Operation::Greater;
288 case Token::Kind::GreaterEqual: {
289 operation = FunctionStorage::Operation::GreaterEqual;
291 case Token::Kind::LessThan: {
292 operation = FunctionStorage::Operation::Less;
294 case Token::Kind::LessEqual: {
295 operation = FunctionStorage::Operation::LessEqual;
297 case Token::Kind::Plus: {
298 operation = FunctionStorage::Operation::Add;
300 case Token::Kind::Minus: {
301 operation = FunctionStorage::Operation::Subtract;
303 case Token::Kind::Times: {
304 operation = FunctionStorage::Operation::Multiplication;
306 case Token::Kind::Slash: {
307 operation = FunctionStorage::Operation::Division;
309 case Token::Kind::Power: {
310 operation = FunctionStorage::Operation::Power;
312 case Token::Kind::Percent: {
313 operation = FunctionStorage::Operation::Modulo;
315 case Token::Kind::Dot: {
316 operation = FunctionStorage::Operation::AtId;
319 throw_parser_error(
"unknown operator in parser.");
322 auto function_node = std::make_shared<FunctionNode>(operation, tok.text.data() - tmpl.content.c_str());
324 while (!operator_stack.empty() &&
325 ((operator_stack.top()->precedence > function_node->precedence) ||
326 (operator_stack.top()->precedence == function_node->precedence && function_node->associativity == FunctionNode::Associativity::Left))) {
327 add_operator(arguments, operator_stack);
330 operator_stack.emplace(function_node);
332 case Token::Kind::Comma: {
333 if (current_brace_level == 0 && current_bracket_level == 0) {
337 case Token::Kind::Colon: {
338 if (current_brace_level == 0 && current_bracket_level == 0) {
339 throw_parser_error(
"unexpected ':'");
342 case Token::Kind::LeftParen: {
344 auto expr = parse_expression(tmpl);
345 if (tok.kind != Token::Kind::RightParen) {
346 throw_parser_error(
"expected right parenthesis, got '" + tok.describe() +
"'");
349 throw_parser_error(
"empty expression in parentheses");
351 arguments.emplace_back(expr);
355 case Token::Kind::Pipe: {
358 if (tok.kind != Token::Kind::Id) {
359 throw_parser_error(
"expected function name, got '" + tok.describe() +
"'");
361 auto func = std::make_shared<FunctionNode>(tok.text, tok.text.data() - tmpl.content.c_str());
363 func->number_args += 1;
364 func->arguments.emplace_back(arguments.back());
365 arguments.pop_back();
367 if (peek_tok.kind == Token::Kind::LeftParen) {
372 auto expr = parse_expression(tmpl);
376 func->number_args += 1;
377 func->arguments.emplace_back(expr);
378 }
while (tok.kind == Token::Kind::Comma);
379 if (tok.kind != Token::Kind::RightParen) {
380 throw_parser_error(
"expected right parenthesis, got '" + tok.describe() +
"'");
384 auto function_data = function_storage.find_function(func->name, func->number_args);
385 if (function_data.operation == FunctionStorage::Operation::None) {
386 throw_parser_error(
"unknown function " + func->name);
388 func->operation = function_data.operation;
389 if (function_data.operation == FunctionStorage::Operation::Callback) {
390 func->callback = function_data.callback;
392 arguments.emplace_back(func);
402 while (!operator_stack.empty()) {
403 add_operator(arguments, operator_stack);
406 std::shared_ptr<ExpressionNode> expr;
407 if (arguments.size() == 1) {
410 }
else if (arguments.size() > 1) {
411 throw_parser_error(
"malformed expression");
416 bool parse_statement(
Template& tmpl, Token::Kind closing,
const std::filesystem::path& path) {
417 if (tok.kind != Token::Kind::Id) {
421 if (tok.text ==
static_cast<decltype(tok.text)
>(
"if")) {
424 auto if_statement_node = std::make_shared<IfStatementNode>(current_block, tok.text.data() - tmpl.content.c_str());
425 current_block->nodes.emplace_back(if_statement_node);
426 if_statement_stack.emplace(if_statement_node.get());
427 current_block = &if_statement_node->true_statement;
428 current_expression_list = &if_statement_node->condition;
430 if (!parse_expression(tmpl, closing)) {
433 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"else")) {
434 if (if_statement_stack.empty()) {
435 throw_parser_error(
"else without matching if");
437 auto& if_statement_data = if_statement_stack.top();
440 if_statement_data->has_false_statement =
true;
441 current_block = &if_statement_data->false_statement;
444 if (tok.kind == Token::Kind::Id && tok.text ==
static_cast<decltype(tok.text)
>(
"if")) {
447 auto if_statement_node = std::make_shared<IfStatementNode>(
true, current_block, tok.text.data() - tmpl.content.c_str());
448 current_block->nodes.emplace_back(if_statement_node);
449 if_statement_stack.emplace(if_statement_node.get());
450 current_block = &if_statement_node->true_statement;
451 current_expression_list = &if_statement_node->condition;
453 if (!parse_expression(tmpl, closing)) {
457 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"endif")) {
458 if (if_statement_stack.empty()) {
459 throw_parser_error(
"endif without matching if");
463 while (if_statement_stack.top()->is_nested) {
464 if_statement_stack.pop();
467 auto& if_statement_data = if_statement_stack.top();
470 current_block = if_statement_data->parent;
471 if_statement_stack.pop();
472 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"block")) {
475 if (tok.kind != Token::Kind::Id) {
476 throw_parser_error(
"expected block name, got '" + tok.describe() +
"'");
479 const std::string block_name =
static_cast<std::string
>(tok.text);
481 auto block_statement_node = std::make_shared<BlockStatementNode>(current_block, block_name, tok.text.data() - tmpl.content.c_str());
482 current_block->nodes.emplace_back(block_statement_node);
483 block_statement_stack.emplace(block_statement_node.get());
484 current_block = &block_statement_node->block;
485 auto success = tmpl.block_storage.emplace(block_name, block_statement_node);
486 if (!success.second) {
487 throw_parser_error(
"block with the name '" + block_name +
"' does already exist");
491 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"endblock")) {
492 if (block_statement_stack.empty()) {
493 throw_parser_error(
"endblock without matching block");
496 auto& block_statement_data = block_statement_stack.top();
499 current_block = block_statement_data->parent;
500 block_statement_stack.pop();
501 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"for")) {
505 if (tok.kind != Token::Kind::Id) {
506 throw_parser_error(
"expected id, got '" + tok.describe() +
"'");
509 Token value_token = tok;
513 std::shared_ptr<ForStatementNode> for_statement_node;
514 if (tok.kind == Token::Kind::Comma) {
516 if (tok.kind != Token::Kind::Id) {
517 throw_parser_error(
"expected id, got '" + tok.describe() +
"'");
520 const Token key_token = value_token;
524 for_statement_node = std::make_shared<ForObjectStatementNode>(
static_cast<std::string
>(key_token.text),
static_cast<std::string
>(value_token.text),
525 current_block, tok.text.data() - tmpl.content.c_str());
530 std::make_shared<ForArrayStatementNode>(
static_cast<std::string
>(value_token.text), current_block, tok.text.data() - tmpl.content.c_str());
533 current_block->nodes.emplace_back(for_statement_node);
534 for_statement_stack.emplace(for_statement_node.get());
535 current_block = &for_statement_node->body;
536 current_expression_list = &for_statement_node->condition;
538 if (tok.kind != Token::Kind::Id || tok.text !=
static_cast<decltype(tok.text)
>(
"in")) {
539 throw_parser_error(
"expected 'in', got '" + tok.describe() +
"'");
543 if (!parse_expression(tmpl, closing)) {
546 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"endfor")) {
547 if (for_statement_stack.empty()) {
548 throw_parser_error(
"endfor without matching for");
551 auto& for_statement_data = for_statement_stack.top();
554 current_block = for_statement_data->parent;
555 for_statement_stack.pop();
556 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"include")) {
559 std::string template_name = parse_filename();
560 add_to_template_storage(path, template_name);
562 current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
565 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"extends")) {
568 std::string template_name = parse_filename();
569 add_to_template_storage(path, template_name);
571 current_block->nodes.emplace_back(std::make_shared<ExtendsStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
574 }
else if (tok.text ==
static_cast<decltype(tok.text)
>(
"set")) {
577 if (tok.kind != Token::Kind::Id) {
578 throw_parser_error(
"expected variable name, got '" + tok.describe() +
"'");
581 const std::string key =
static_cast<std::string
>(tok.text);
584 auto set_statement_node = std::make_shared<SetStatementNode>(key, tok.text.data() - tmpl.content.c_str());
585 current_block->nodes.emplace_back(set_statement_node);
586 current_expression_list = &set_statement_node->expression;
588 if (tok.text !=
static_cast<decltype(tok.text)
>(
"=")) {
589 throw_parser_error(
"expected '=', got '" + tok.describe() +
"'");
593 if (!parse_expression(tmpl, closing)) {
602 void parse_into(
Template& tmpl,
const std::filesystem::path& path) {
603 lexer.start(tmpl.content);
604 current_block = &tmpl.root;
609 case Token::Kind::Eof: {
610 if (!if_statement_stack.empty()) {
611 throw_parser_error(
"unmatched if");
613 if (!for_statement_stack.empty()) {
614 throw_parser_error(
"unmatched for");
617 current_block =
nullptr;
619 case Token::Kind::Text: {
620 current_block->nodes.emplace_back(std::make_shared<TextNode>(tok.text.data() - tmpl.content.c_str(), tok.text.size()));
622 case Token::Kind::StatementOpen: {
624 if (!parse_statement(tmpl, Token::Kind::StatementClose, path)) {
625 throw_parser_error(
"expected statement, got '" + tok.describe() +
"'");
627 if (tok.kind != Token::Kind::StatementClose) {
628 throw_parser_error(
"expected statement close, got '" + tok.describe() +
"'");
631 case Token::Kind::LineStatementOpen: {
633 if (!parse_statement(tmpl, Token::Kind::LineStatementClose, path)) {
634 throw_parser_error(
"expected statement, got '" + tok.describe() +
"'");
636 if (tok.kind != Token::Kind::LineStatementClose && tok.kind != Token::Kind::Eof) {
637 throw_parser_error(
"expected line statement close, got '" + tok.describe() +
"'");
640 case Token::Kind::ExpressionOpen: {
643 auto expression_list_node = std::make_shared<ExpressionListNode>(tok.text.data() - tmpl.content.c_str());
644 current_block->nodes.emplace_back(expression_list_node);
645 current_expression_list = expression_list_node.get();
647 if (!parse_expression(tmpl, Token::Kind::ExpressionClose)) {
648 throw_parser_error(
"expected expression close, got '" + tok.describe() +
"'");
651 case Token::Kind::CommentOpen: {
653 if (tok.kind != Token::Kind::CommentClose) {
654 throw_parser_error(
"expected comment close, got '" + tok.describe() +
"'");
658 throw_parser_error(
"unexpected token '" + tok.describe() +
"'");
662 current_block =
nullptr;
668 : config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) {}
670 Template parse(std::string_view input,
const std::filesystem::path& path) {
671 auto result =
Template(std::string(input));
672 parse_into(result, path);
676 void parse_into_template(
Template& tmpl,
const std::filesystem::path& filename) {
677 auto sub_parser =
Parser(config, lexer.get_config(), template_storage, function_storage);
678 sub_parser.parse_into(tmpl, filename.parent_path());
681 static std::string load_file(
const std::filesystem::path& filename) {
685 INJA_THROW(
FileError(
"failed accessing file at '" + filename.string() +
"'"));
687 std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());