parse_filename without json

This commit is contained in:
pantor
2021-11-12 08:06:56 +01:00
parent 623c267383
commit 2da715a12a
3 changed files with 35 additions and 22 deletions

View File

@@ -48,7 +48,7 @@ class Parser {
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 @@ 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<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 @@ class Parser {
} 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 @@ class Parser {
} 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()));

View File

@@ -1466,7 +1466,7 @@ class Parser {
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 @@ 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<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 @@ class Parser {
} 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 @@ class Parser {
} 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()));

View File

@@ -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") {