add line statements, new tests, c++11 in cmake
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6aff159..7df3f54 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,7 +14,7 @@
##
## CONFIGURATION
##
-set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD 11)
set(INJA_SOURCE_DIR src/)
set(INJA_HEADER_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dist)
diff --git a/src/inja.hpp b/src/inja.hpp
index 23b7a1e..9fb5aa8 100644
--- a/src/inja.hpp
+++ b/src/inja.hpp
@@ -28,6 +28,22 @@
return ss.str();
}
+
+class smatch: public std::smatch {
+ size_t offset;
+
+ smatch() {}
+ smatch(std::smatch match, size_t offset): std::smatch(match), offset(offset) { }
+
+public:
+ size_t pos() { return offset + position(); }
+ size_t end_pos() { return pos() + length(); }
+ size_t found() { return !empty(); }
+ string outer() { return str(0); }
+ string inner() { return str(1); }
+};
+
+
struct SearchMatch {
SearchMatch() { }
@@ -54,11 +70,15 @@
struct SearchMatchVector: public SearchMatch {
SearchMatchVector(): SearchMatch() { }
- SearchMatchVector(std::smatch match, size_t offset, int inner_group, int regex_number): SearchMatch(match, offset), regex_number(regex_number) {
+ SearchMatchVector(std::smatch match, size_t offset, int inner_group, int regex_number, std::vector<string> regex_patterns): SearchMatch(match, offset), regex_number(regex_number) {
inner = match.str(inner_group);
+ if (regex_number >= 0) {
+ regex_delimiter = std::regex( regex_patterns.at(regex_number) );
+ }
}
int regex_number = -1;
+ std::regex regex_delimiter;
};
struct SearchClosedMatch: public SearchMatch {
@@ -123,7 +143,7 @@
}
}
- return SearchMatchVector(match, position, number_inner, number_regex);
+ return SearchMatchVector(match, position, number_inner, number_regex, regex_patterns);
}
inline SearchClosedMatch search_closed_match_on_level(string input, std::regex regex_statement, std::regex regex_level_up, std::regex regex_level_down, std::regex regex_search, SearchMatch open_match) {
@@ -151,7 +171,16 @@
class Parser {
public:
- enum Type {
+ enum class Delimiter {
+ Statement,
+ LineStatement,
+ Expression,
+ Comment
+ };
+
+ // std::map<Delimiter, string> regex_pattern_map;
+
+ enum class Type {
String,
Loop,
Condition,
@@ -178,15 +207,18 @@
Environment(string global_path): global_path(global_path) {
const string regex_pattern_statement = "\\(\\%\\s*(.+?)\\s*\\%\\)";
- const string regex_pattern_line_statement = "^##\\s*(.+)\\s*$";
+ const string regex_pattern_line_statement = "(?:^|\\n)##\\s*(.+)\\s*";
const string regex_pattern_expression = "\\{\\{\\s*(.+?)\\s*\\}\\}";
const string regex_pattern_comment = "\\{#\\s*(.*?)\\s*#\\}";
+ // Parser parser;
+ // parser.regex_pattern_map[Parser::Delimiter::Statement] = "\\(\\%\\s*(.+?)\\s*\\%\\)";
+
regex_statement = std::regex(regex_pattern_statement);
regex_line_statement = std::regex(regex_pattern_line_statement);
regex_expression = std::regex(regex_pattern_expression);
regex_comment = std::regex(regex_pattern_comment);
- regex_pattern_delimiters = { regex_pattern_statement, regex_pattern_expression, regex_pattern_comment };
+ regex_pattern_delimiters = { regex_pattern_statement, regex_pattern_line_statement, regex_pattern_expression, regex_pattern_comment }; // Order of Parser::Delimiter enum
}
@@ -201,72 +233,80 @@
result += {{"type", Parser::Type::String}, {"text", statement_match.prefix}};
}
- // Regex matched a statement "(% ... %)"
- if (statement_match.regex_number == 0) {
- const std::regex regex_loop_open("for (.*)");
- const std::regex regex_loop_close("endfor");
+ switch ( static_cast<Parser::Delimiter>(statement_match.regex_number) ) {
+ case Parser::Delimiter::Statement:
+ case Parser::Delimiter::LineStatement: {
+ const std::regex regex_loop_open("for (.*)");
+ const std::regex regex_loop_close("endfor");
- const std::regex regex_include("include \"(.*)\"");
+ const std::regex regex_include("include \"(.*)\"");
- const std::regex regex_condition_open("if (.*)");
- const std::regex regex_condition_else_if("else if (.*)");
- const std::regex regex_condition_else("else");
- const std::regex regex_condition_close("endif");
+ const std::regex regex_condition_open("if (.*)");
+ const std::regex regex_condition_else_if("else if (.*)");
+ const std::regex regex_condition_else("else");
+ const std::regex regex_condition_close("endif");
- std::smatch inner_statement_match;
- // Loop
- if (std::regex_match(statement_match.inner, inner_statement_match, regex_loop_open)) {
- SearchClosedMatch loop_match = search_closed_match(input, regex_statement, regex_loop_open, regex_loop_close, statement_match);
+ std::smatch inner_statement_match;
+ // Loop
+ if (std::regex_match(statement_match.inner, inner_statement_match, regex_loop_open)) {
+ SearchClosedMatch loop_match = search_closed_match(input, statement_match.regex_delimiter, regex_loop_open, regex_loop_close, statement_match);
- current_position = loop_match.end_position;
- string loop_command = inner_statement_match.str(0);
- result += {{"type", Parser::Type::Loop}, {"command", loop_command}, {"inner", loop_match.inner}};
- }
- // Include
- else if (std::regex_match(statement_match.inner, inner_statement_match, regex_include)) {
- string include_command = inner_statement_match.str(0);
- string filename = inner_statement_match.str(1);
- result += {{"type", Parser::Type::Include}, {"filename", filename}};
- }
- // Condition
- else if (std::regex_match(statement_match.inner, inner_statement_match, regex_condition_open)) {
- string if_command = inner_statement_match.str(0);
- json condition_result = {{"type", Parser::Type::Condition}, {"children", json::array()}};
+ current_position = loop_match.end_position;
+ string loop_command = inner_statement_match.str(0);
+ result += {{"type", Parser::Type::Loop}, {"command", loop_command}, {"inner", loop_match.inner}};
+ }
+ // Include
+ else if (std::regex_match(statement_match.inner, inner_statement_match, regex_include)) {
+ string include_command = inner_statement_match.str(0);
+ string filename = inner_statement_match.str(1);
+ result += {{"type", Parser::Type::Include}, {"filename", filename}};
+ }
+ // Condition
+ else if (std::regex_match(statement_match.inner, inner_statement_match, regex_condition_open)) {
+ string if_command = inner_statement_match.str(0);
+ json condition_result = {{"type", Parser::Type::Condition}, {"children", json::array()}};
- SearchMatch condition_match = statement_match;
+ SearchMatch condition_match = statement_match;
- SearchClosedMatch else_if_match = search_closed_match_on_level(input, regex_statement, regex_condition_open, regex_condition_close, regex_condition_else_if, condition_match);
- while (else_if_match.found) {
- condition_match = else_if_match.close_match;
+ SearchClosedMatch else_if_match = search_closed_match_on_level(input, statement_match.regex_delimiter, regex_condition_open, regex_condition_close, regex_condition_else_if, condition_match);
+ while (else_if_match.found) {
+ condition_match = else_if_match.close_match;
- condition_result["children"] += {{"type", Parser::Type::ConditionBranch}, {"command", else_if_match.open_match.inner}, {"inner", else_if_match.inner}};
+ condition_result["children"] += {{"type", Parser::Type::ConditionBranch}, {"command", else_if_match.open_match.inner}, {"inner", else_if_match.inner}};
- else_if_match = search_closed_match_on_level(input, regex_statement, regex_condition_open, regex_condition_close, regex_condition_else_if, condition_match);
+ else_if_match = search_closed_match_on_level(input, regex_statement, regex_condition_open, regex_condition_close, regex_condition_else_if, condition_match);
+ }
+
+ SearchClosedMatch else_match = search_closed_match_on_level(input, statement_match.regex_delimiter, regex_condition_open, regex_condition_close, regex_condition_else, condition_match);
+ if (else_match.found) {
+ condition_match = else_match.close_match;
+
+ condition_result["children"] += {{"type", Parser::Type::ConditionBranch}, {"command", else_match.open_match.inner}, {"inner", else_match.inner}};
+ }
+
+ SearchClosedMatch last_if_match = search_closed_match(input, statement_match.regex_delimiter, regex_condition_open, regex_condition_close, condition_match);
+
+ condition_result["children"] += {{"type", Parser::Type::ConditionBranch}, {"command", last_if_match.open_match.inner}, {"inner", last_if_match.inner}};
+
+ current_position = last_if_match.end_position;
+ result += condition_result;
}
- SearchClosedMatch else_match = search_closed_match_on_level(input, regex_statement, regex_condition_open, regex_condition_close, regex_condition_else, condition_match);
- if (else_match.found) {
- condition_match = else_match.close_match;
-
- condition_result["children"] += {{"type", Parser::Type::ConditionBranch}, {"command", else_match.open_match.inner}, {"inner", else_match.inner}};
- }
-
- SearchClosedMatch last_if_match = search_closed_match(input, regex_statement, regex_condition_open, regex_condition_close, condition_match);
-
- condition_result["children"] += {{"type", Parser::Type::ConditionBranch}, {"command", last_if_match.open_match.inner}, {"inner", last_if_match.inner}};
-
- current_position = last_if_match.end_position;
- result += condition_result;
+ break;
}
- }
- // Regex matched an expression "{{ ... }}"
- else if (statement_match.regex_number == 1) {
- result += {{"type", Parser::Type::Variable}, {"command", statement_match.inner}};
- }
- // Regex matched an comment "{# ... #}"
- else if (statement_match.regex_number == 2) {
- result += {{"type", Parser::Type::Comment}, {"text", statement_match.inner}};
+ case Parser::Delimiter::Expression: {
+ result += {{"type", Parser::Type::Variable}, {"command", statement_match.inner}};
+ break;
+ }
+ case Parser::Delimiter::Comment: {
+ result += {{"type", Parser::Type::Comment}, {"text", statement_match.inner}};
+ break;
+ }
+ default: {
+ throw std::runtime_error("Parser error: Unknown delimiter.");
+ break;
+ }
}
statement_match = search(input, regex_pattern_delimiters, current_position);
@@ -390,11 +430,11 @@
string result = "";
for (auto element: input) {
switch ( (Parser::Type) element["type"] ) {
- case Parser::Type::String: {
+ case Parser::Type::String: {
result += element["text"];
break;
}
- case Parser::Type::Variable: {
+ case Parser::Type::Variable: {
json variable = parse_variable(element["command"], data);
result += render_json(variable);
break;
diff --git a/test/data/nested-line/data.json b/test/data/nested-line/data.json
new file mode 100644
index 0000000..f7e4949
--- /dev/null
+++ b/test/data/nested-line/data.json
@@ -0,0 +1,4 @@
+{
+ "xarray": [0, 1],
+ "yarray": [0, 1, 2]
+}
\ No newline at end of file
diff --git a/test/data/nested-line/result.txt b/test/data/nested-line/result.txt
new file mode 100644
index 0000000..109ebdb
--- /dev/null
+++ b/test/data/nested-line/result.txt
@@ -0,0 +1,6 @@
+0-0
+0-1
+0-2
+1-0
+1-1
+1-2
diff --git a/test/data/nested-line/template.txt b/test/data/nested-line/template.txt
new file mode 100644
index 0000000..fdc1a00
--- /dev/null
+++ b/test/data/nested-line/template.txt
@@ -0,0 +1,6 @@
+## for x in xarray
+## for y in yarray
+{{x}}-{{y}}
+
+## endfor
+## endfor
diff --git a/test/src/unit-files.cpp b/test/src/unit-files.cpp
index 8b2877f..62aa628 100644
--- a/test/src/unit-files.cpp
+++ b/test/src/unit-files.cpp
@@ -28,7 +28,7 @@
TEST_CASE("Complete files") {
Environment env = Environment("../test/data/");
- for (std::string test_name : {"simple-file", "nested"}) {
+ for (std::string test_name : {"simple-file", "nested", "nested-line"}) {
SECTION(test_name) {
CHECK( env.render_template_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt") );
}
diff --git a/test/src/unit-parser.cpp b/test/src/unit-parser.cpp
index 7853447..cbda515 100644
--- a/test/src/unit-parser.cpp
+++ b/test/src/unit-parser.cpp
@@ -114,6 +114,20 @@
json result = {{{"type", Type::Comment}, {"text", "lorem ipsum"}}};
CHECK( env.parse(test) == result );
}
+
+ SECTION("Line Statements") {
+ std::string test = R"(## if true
+lorem ipsum
+## endif)";
+ json result = {
+ {{"type", Type::Condition}, {"children", {
+ {{"type", Type::ConditionBranch}, {"command", "if true"}, {"children", {
+ {{"type", Type::String}, {"text", "lorem ipsum"}}
+ }}}
+ }}}
+ };
+ CHECK( env.parse(test) == result );
+ }
}
TEST_CASE("Parse json") {
diff --git a/test/src/unit-string-helper.cpp b/test/src/unit-string-helper.cpp
index 256499c..f058fb9 100644
--- a/test/src/unit-string-helper.cpp
+++ b/test/src/unit-string-helper.cpp
@@ -49,6 +49,14 @@
CHECK( match.outer == "ipsum" );
CHECK( match.inner == "s" );
}
+
+ SECTION("Basic 3") {
+ std::vector<std::string> regex_patterns = { "asdf", "qwer", "ip(\\w*)um", "ip(\\w*)um", "es(\\w*)as" };
+ inja::SearchMatchVector match = inja::search(input, regex_patterns, 0);
+ CHECK( match.regex_number == 2 );
+ CHECK( match.outer == "ipsum" );
+ CHECK( match.inner == "s" );
+ }
}
TEST_CASE("Search on level") {