diff --git a/include/minja/minja.hpp b/include/minja/minja.hpp index f0e80fd..153fdd9 100644 --- a/include/minja/minja.hpp +++ b/include/minja/minja.hpp @@ -2160,14 +2160,13 @@ class Parser { static std::regex expr_open_regex(R"(\{\{([-~])?)"); static std::regex block_open_regex(R"(^\{%([-~])?[\s\n\r]*)"); static std::regex block_keyword_tok(R"((if|else|elif|endif|for|endfor|generation|endgeneration|set|endset|block|endblock|macro|endmacro|filter|endfilter)\b)"); - static std::regex non_text_open_regex(R"(\{\{|\{%|\{#)"); + static std::regex text_regex(R"([\s\S\r\n]*?(?=\{[{%#]|$))"); static std::regex expr_close_regex(R"([\s\n\r]*([-~])?\}\})"); static std::regex block_close_regex(R"([\s\n\r]*([-~])?%\})"); TemplateTokenVector tokens; std::vector group; std::string text; - std::smatch match; try { while (it != end) { @@ -2294,15 +2293,10 @@ class Parser { } else { throw std::runtime_error("Unexpected block: " + keyword); } - } else if (std::regex_search(it, end, match, non_text_open_regex)) { - auto text_end = it + match.position(); - text = std::string(it, text_end); - it = text_end; + } else if (!(text = consumeToken(text_regex, SpaceHandling::Keep)).empty()) { tokens.push_back(std::make_unique(location, SpaceHandling::Keep, SpaceHandling::Keep, text)); } else { - text = std::string(it, end); - it = end; - tokens.push_back(std::make_unique(location, SpaceHandling::Keep, SpaceHandling::Keep, text)); + if (it != end) throw std::runtime_error("Unexpected character"); } } return tokens;