parse_filename without json
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 @@
std::stack<ForStatementNode*> for_statement_stack;
std::stack<BlockStatementNode*> 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 @@
}
}
+ 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<std::string>(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 @@
} else if (tok.text == static_cast<decltype(tok.text)>("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<const std::string &>();
+ std::string template_name = parse_filename(tok);
add_to_template_storage(path, template_name);
current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
@@ -527,11 +536,7 @@
} else if (tok.text == static_cast<decltype(tok.text)>("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<const std::string &>();
+ std::string template_name = parse_filename(tok);
add_to_template_storage(path, template_name);
current_block->nodes.emplace_back(std::make_shared<ExtendsStatementNode>(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 @@
std::stack<ForStatementNode*> for_statement_stack;
std::stack<BlockStatementNode*> 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 @@
}
}
+ 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<std::string>(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 @@
} else if (tok.text == static_cast<decltype(tok.text)>("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<const std::string &>();
+ std::string template_name = parse_filename(tok);
add_to_template_storage(path, template_name);
current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
@@ -1945,11 +1954,7 @@
} else if (tok.text == static_cast<decltype(tok.text)>("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<const std::string &>();
+ std::string template_name = parse_filename(tok);
add_to_template_storage(path, template_name);
current_block->nodes.emplace_back(std::make_shared<ExtendsStatementNode>(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 @@
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") {