mirror of
https://github.com/pantor/inja.git
synced 2026-06-12 13:59:51 +00:00
parse_filename without json
This commit is contained in:
+16
-11
@@ -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()));
|
||||
|
||||
Reference in New Issue
Block a user