add utility to create regex of functions

This commit is contained in:
pantor
2018-02-17 11:59:59 +01:00
parent 570512ac95
commit 6153720155
2 changed files with 27 additions and 8 deletions
+21 -8
View File
@@ -492,6 +492,19 @@ public:
class Parser {
public:
static Regex function_regex(std::string name, int number_arguments) {
std::string pattern = name;
if (number_arguments > 0) {
pattern += "\\(";
for (int i = 0; i < number_arguments; i++) {
if (i != 0) pattern += ",";
pattern += "(.*)";
}
pattern += "\\)";
}
return Regex{"\\s*" + pattern + "\\s*"};
}
std::map<Parsed::Delimiter, Regex> regex_map_delimiters = {
{Parsed::Delimiter::Statement, Regex{"\\{\\%\\s*(.+?)\\s*\\%\\}"}},
{Parsed::Delimiter::LineStatement, Regex{"(?:^|\\n)##\\s*(.+)\\s*"}},
@@ -531,14 +544,14 @@ public:
{Parsed::Function::GreaterEqual, Regex{"(.+) >= (.+)"}},
{Parsed::Function::LessEqual, Regex{"(.+) <= (.+)"}},
{Parsed::Function::Different, Regex{"(.+) != (.+)"}},
{Parsed::Function::Upper, Regex{"\\s*upper\\((.*)\\)\\s*"}},
{Parsed::Function::Lower, Regex{"\\s*lower\\((.*)\\)\\s*"}},
{Parsed::Function::Range, Regex{"\\s*range\\((.*)\\)\\s*"}},
{Parsed::Function::Length, Regex{"\\s*length\\((.*)\\)\\s*"}},
{Parsed::Function::Round, Regex{"\\s*round\\((.*),(.*)\\)\\s*"}},
{Parsed::Function::DivisibleBy, Regex{"\\s*divisibleBy\\((.*),(.*)\\)\\s*"}},
{Parsed::Function::Odd, Regex{"\\s*odd\\((.*)\\)\\s*"}},
{Parsed::Function::Even, Regex{"\\s*even\\((.*)\\)\\s*"}},
{Parsed::Function::Upper, function_regex("upper", 1)},
{Parsed::Function::Lower, function_regex("lower", 1)},
{Parsed::Function::Range, function_regex("range", 1)},
{Parsed::Function::Length, function_regex("length", 1)},
{Parsed::Function::Round, function_regex("round", 2)},
{Parsed::Function::DivisibleBy, function_regex("divisibleBy", 2)},
{Parsed::Function::Odd, function_regex("odd", 1)},
{Parsed::Function::Even, function_regex("even", 1)},
{Parsed::Function::ReadJson, Regex{"\\s*([^\\(\\)]*\\S)\\s*"}}
};
+6
View File
@@ -159,3 +159,9 @@ TEST_CASE("match-functions") {
// CHECK_THROWS_WITH( inja::match("test(var)", map_regex), "Could not match input: test(var)" );
// CHECK_THROWS_WITH( inja::match("round(var)", map_regex), "Could not match input: round(var)" );
}
TEST_CASE("create-regex-functions") {
CHECK( inja::Parser::function_regex("upper", 1).pattern() == "\\s*upper\\((.*)\\)\\s*" );
CHECK( inja::Parser::function_regex("upper", 0).pattern() == "\\s*upper\\s*" );
CHECK( inja::Parser::function_regex("lower", 2).pattern() == "\\s*lower\\((.*),(.*)\\)\\s*" );
}