diff --git a/CMakeLists.txt b/CMakeLists.txt index 064d76b..3ce073e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ set(INJA_VERSION ${PROJECT_VERSION}) ## ## CONFIGURATION ## +# set(CMAKE_CXX_COMPILER g++-7) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(INJA_SOURCE_DIR src) @@ -118,4 +119,4 @@ install( EXPORT "${TARGETS_EXPORT_NAME}" NAMESPACE "${namespace}" DESTINATION "${config_install_dir}" -) \ No newline at end of file +) diff --git a/src/inja.hpp b/src/inja.hpp index d98526e..7e049ca 100644 --- a/src/inja.hpp +++ b/src/inja.hpp @@ -52,7 +52,7 @@ public: class Match: public std::smatch { size_t offset_ = 0; - int group_offset_ = 0; + unsigned int group_offset_ = 0; Regex regex_; public: @@ -60,7 +60,7 @@ public: explicit Match(size_t offset): std::smatch(), offset_(offset) { } explicit Match(size_t offset, const Regex& regex): std::smatch(), offset_(offset), regex_(regex) { } - void setGroupOffset(int group_offset) { group_offset_ = group_offset; } + void setGroupOffset(unsigned int group_offset) { group_offset_ = group_offset; } void setRegex(Regex regex) { regex_ = regex; } size_t position() const { return offset_ + std::smatch::position(); } @@ -118,7 +118,7 @@ inline Match search(const std::string& input, Regex regex, size_t position) { template -inline MatchType search(const std::string& input, std::map regexes, size_t position) { +inline MatchType search(const std::string& input, std::map& regexes, size_t position) { // Map to vectors std::vector class_vector; std::vector regexes_vector; @@ -140,7 +140,7 @@ inline MatchType search(const std::string& input, std::map regexes, if (not search_match.found()) { return MatchType(); } // Vector of id vs groups - std::vector regex_mark_counts; + std::vector regex_mark_counts = {}; for (unsigned int i = 0; i < regexes_vector.size(); i++) { for (unsigned int j = 0; j < regexes_vector[i].mark_count() + 1; j++) { regex_mark_counts.push_back(i); @@ -186,7 +186,7 @@ inline MatchClosed search_closed(const std::string& input, const Regex& regex_st template inline MatchType match(const std::string& input, std::map regexes) { MatchType match; - for (auto const& e : regexes) { + for (const auto e : regexes) { if (std::regex_match(input, match, e.second)) { match.setType(e.first); match.setRegex(e.second); @@ -426,6 +426,9 @@ public: return result; } } + + throw std::runtime_error("Unknown function in renderer."); + return json(); } std::string render(json data) { @@ -586,9 +589,11 @@ public: current_position = loop_match.end_position(); - MatchType match_command = match(match_statement.str(0), regex_map_loop); + const std::string loop_inner = match_statement.str(0); + MatchType match_command = match(loop_inner, regex_map_loop); const std::string item_name = match_command.str(1); const std::string list_name = match_command.str(2); + result.emplace_back( std::make_shared(item_name, parse_expression(list_name), loop_match.inner())); break; } @@ -600,7 +605,8 @@ public: while (else_if_match.found()) { condition_match = else_if_match.close_match; - MatchType match_command = match(else_if_match.open_match.str(1), regex_map_condition); + const std::string else_if_match_inner = else_if_match.open_match.str(1); + MatchType match_command = match(else_if_match_inner, regex_map_condition); condition_container->children.push_back( std::make_shared(else_if_match.inner(), match_command.type(), parse_expression(match_command.str(1))) ); else_if_match = search_closed_on_level(input, match_delimiter.regex(), regex_map_statement_openers.at(Parsed::Statement::Condition), regex_map_statement_closers.at(Parsed::Statement::Condition), regex_map_condition.at(Parsed::Condition::ElseIf), condition_match); @@ -610,13 +616,15 @@ public: if (else_match.found()) { condition_match = else_match.close_match; - MatchType match_command = match(else_match.open_match.str(1), regex_map_condition); + const std::string else_match_inner = else_match.open_match.str(1); + MatchType match_command = match(else_match_inner, regex_map_condition); condition_container->children.push_back( std::make_shared(else_match.inner(), match_command.type(), parse_expression(match_command.str(1))) ); } MatchClosed last_if_match = search_closed(input, match_delimiter.regex(), regex_map_statement_openers.at(Parsed::Statement::Condition), regex_map_statement_closers.at(Parsed::Statement::Condition), condition_match); - MatchType match_command = match(last_if_match.open_match.str(1), regex_map_condition); + const std::string last_if_match_inner = last_if_match.open_match.str(1); + MatchType match_command = match(last_if_match_inner, regex_map_condition); if (match_command.type() == Parsed::Condition::Else) { condition_container->children.push_back( std::make_shared(last_if_match.inner(), match_command.type()) ); } else { diff --git a/test/src/unit-files.cpp b/test/src/unit-files.cpp index 9bd4e3b..c1450a1 100644 --- a/test/src/unit-files.cpp +++ b/test/src/unit-files.cpp @@ -6,7 +6,7 @@ using json = nlohmann::json; -/* TEST_CASE("loading") { +TEST_CASE("loading") { inja::Environment env = inja::Environment(); json data; data["name"] = "Jeff"; @@ -32,4 +32,4 @@ TEST_CASE("complete-files") { CHECK( env.render_template_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_global_file(test_name + "/result.txt") ); } } -} */ +} diff --git a/test/src/unit-string-helper.cpp b/test/src/unit-string-helper.cpp index 1a4f3e8..0838645 100644 --- a/test/src/unit-string-helper.cpp +++ b/test/src/unit-string-helper.cpp @@ -60,14 +60,22 @@ TEST_CASE("search-multiple-regexes") { } SECTION("basic 3") { + auto map_functions = inja::Parser().regex_map_functions; std::map regex_patterns = { - {0, inja::Regex("upper\\((.*)\\)")}, - {1, inja::Regex("lower\\((.*)\\)")}, - {2, inja::Regex("[^()]*?")} + {0, map_functions.at(inja::Parsed::Function::Upper)}, + {1, map_functions.at(inja::Parsed::Function::Lower)}, + {2, map_functions.at(inja::Parsed::Function::ReadJson)} }; - inja::MatchType match = inja::search("upper(lower(name))", regex_patterns, 0); + + inja::MatchType match = inja::search("upper(name)", regex_patterns, 0); CHECK( match.type() == 0 ); - CHECK( match.str(1) == "lower(name)" ); + CHECK( match.str(0) == "upper(name)" ); + CHECK( match.str(1) == "name" ); + + inja::MatchType match2 = inja::search("upper(lower(name))", regex_patterns, 0); + CHECK( match2.type() == 0 ); + CHECK( match2.str(0) == "upper(lower(name))" ); + CHECK( match2.str(1) == "lower(name)" ); } }