From 2da715a12a5b04cfa7e779b11d3113c717e3b5c2 Mon Sep 17 00:00:00 2001 From: pantor Date: Fri, 12 Nov 2021 08:06:56 +0100 Subject: [PATCH] parse_filename without json --- include/inja/parser.hpp | 27 ++++++++++++++++----------- single_include/inja/inja.hpp | 27 ++++++++++++++++----------- test/test-renderer.cpp | 3 +++ 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index dbf8846..e3c26e7 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -48,7 +48,7 @@ class Parser { std::stack for_statement_stack; std::stack block_statement_stack; - inline void throw_parser_error(const std::string &message) { + inline void throw_parser_error(const std::string &message) const { INJA_THROW(ParserError(message, lexer.current_position())); } @@ -124,6 +124,19 @@ class Parser { } } + std::string parse_filename(const Token& tok) const { + if (tok.kind != Token::Kind::String) { + throw_parser_error("expected string, got '" + tok.describe() + "'"); + } + + if (tok.text.length() < 2) { + throw_parser_error("expected filename, got '" + static_cast(tok.text) + "'"); + } + + // Remove first and last character "" + return std::string {tok.text.substr(1, tok.text.length() - 2)}; + } + bool parse_expression(Template &tmpl, Token::Kind closing) { while (tok.kind != closing && tok.kind != Token::Kind::Eof) { // Literals @@ -513,11 +526,7 @@ class Parser { } else if (tok.text == static_cast("include")) { get_next_token(); - if (tok.kind != Token::Kind::String) { - throw_parser_error("expected string, got '" + tok.describe() + "'"); - } - - std::string template_name = json::parse(tok.text).get_ref(); + std::string template_name = parse_filename(tok); add_to_template_storage(path, template_name); current_block->nodes.emplace_back(std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); @@ -527,11 +536,7 @@ class Parser { } else if (tok.text == static_cast("extends")) { get_next_token(); - if (tok.kind != Token::Kind::String) { - throw_parser_error("expected string, got '" + tok.describe() + "'"); - } - - std::string template_name = json::parse(tok.text).get_ref(); + std::string template_name = parse_filename(tok); add_to_template_storage(path, template_name); current_block->nodes.emplace_back(std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index d8fb413..715a218 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -1466,7 +1466,7 @@ class Parser { std::stack for_statement_stack; std::stack block_statement_stack; - inline void throw_parser_error(const std::string &message) { + inline void throw_parser_error(const std::string &message) const { INJA_THROW(ParserError(message, lexer.current_position())); } @@ -1542,6 +1542,19 @@ class Parser { } } + std::string parse_filename(const Token& tok) const { + if (tok.kind != Token::Kind::String) { + throw_parser_error("expected string, got '" + tok.describe() + "'"); + } + + if (tok.text.length() < 2) { + throw_parser_error("expected filename, got '" + static_cast(tok.text) + "'"); + } + + // Remove first and last character "" + return std::string {tok.text.substr(1, tok.text.length() - 2)}; + } + bool parse_expression(Template &tmpl, Token::Kind closing) { while (tok.kind != closing && tok.kind != Token::Kind::Eof) { // Literals @@ -1931,11 +1944,7 @@ class Parser { } else if (tok.text == static_cast("include")) { get_next_token(); - if (tok.kind != Token::Kind::String) { - throw_parser_error("expected string, got '" + tok.describe() + "'"); - } - - std::string template_name = json::parse(tok.text).get_ref(); + std::string template_name = parse_filename(tok); add_to_template_storage(path, template_name); current_block->nodes.emplace_back(std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); @@ -1945,11 +1954,7 @@ class Parser { } else if (tok.text == static_cast("extends")) { get_next_token(); - if (tok.kind != Token::Kind::String) { - throw_parser_error("expected string, got '" + tok.describe() + "'"); - } - - std::string template_name = json::parse(tok.text).get_ref(); + std::string template_name = parse_filename(tok); add_to_template_storage(path, template_name); current_block->nodes.emplace_back(std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); diff --git a/test/test-renderer.cpp b/test/test-renderer.cpp index 61e328d..2801ed0 100644 --- a/test/test-renderer.cpp +++ b/test/test-renderer.cpp @@ -183,6 +183,9 @@ TEST_CASE("templates") { CHECK(env.render(t2, data) == "Hello Peter!"); CHECK_THROWS_WITH(env.parse("{% include \"does-not-exist\" %}!"), "[inja.exception.file_error] failed accessing file at 'does-not-exist'"); + + CHECK_THROWS_WITH(env.parse("{% include does-not-exist %}!"), + "[inja.exception.parser_error] (at 1:12) expected string, got 'does-not-exist'"); } SUBCASE("include-callback") {