diff --git a/src/common/query.cpp b/src/common/query.cpp index d7fb51257..67873a5e8 100644 --- a/src/common/query.cpp +++ b/src/common/query.cpp @@ -77,8 +77,16 @@ namespace lumiera { map regexTable; - Symbol matchArgument = "\\(\\s*([\\w_\\.\\-]+)\\s*\\)"; + Symbol matchArgument = "\\(\\s*([\\w_\\.\\-]+)\\s*\\),?\\s*"; regex findPredicate (string("(\\w+)")+matchArgument); + + inline regex& + getTermRegex (Symbol sym) + { + if (!contains (regexTable, sym)) + regexTable[sym] = regex (string(sym)+=matchArgument); + return regexTable[sym]; + } } /** (preliminary) helper: instead of really parsing and evaluating the terms, @@ -90,17 +98,31 @@ namespace lumiera const string extractID (Symbol sym, const string& termString) { - - if (!contains (regexTable, sym)) - regexTable[sym] = regex (string(sym)+=matchArgument); - smatch match; - if (regex_search (termString, match, regexTable[sym])) - return string (match[1]); + if (regex_search (termString, match, getTermRegex (sym))) + return (match[1]); else return ""; + } - } + + /** (preliminary) helper: cut a term with the given symbol. + * The term is matched, removed from the original string and returned + * @note parameter termString will be modified! + */ + const string + removeTerm (Symbol sym, string& termString) + { + smatch match; + if (regex_search (termString, match, getTermRegex (sym))) + { + string res (sym); res += "("+match[1]+")"; + termString.erase (match.position(), match[0].length()); + return res; + } + else + return ""; + } /** @note this is a very hackish preliminary implementation. diff --git a/src/common/query.hpp b/src/common/query.hpp index b7d22498b..14d23982b 100644 --- a/src/common/query.hpp +++ b/src/common/query.hpp @@ -69,7 +69,6 @@ namespace lumiera */ void normalizeID (string& id); - const string extractID (Symbol, const string& termString); /** count the top-level predicates in the query string. * usable for ordering queries, as more predicates usually @@ -77,7 +76,12 @@ namespace lumiera */ uint countPraed (const string&); - + + const string extractID (Symbol, const string& termString); + + const string removeTerm (Symbol, string& termString); + + } // namespace query } // namespace lumiera diff --git a/tests/52query.tests b/tests/52query.tests index ce5f4f31e..2c598b3b0 100644 --- a/tests/52query.tests +++ b/tests/52query.tests @@ -18,6 +18,11 @@ return: 0 END +TEST "QueryUtils_test" QueryUtils_test removeTerm < @@ -25,20 +25,11 @@ #define LUMIERA_TEST_QUERY_QUERYDIAGNOSTICS_H -//#include "common/factory.hpp" -//#include "common/util.hpp" - -//#include -//#include #include -//#include #include -//using boost::algorithm::join; -//using boost::lexical_cast; using boost::format; using std::string; -//using std::cout; using std::rand; diff --git a/tests/components/common/query/queryutilstest.cpp b/tests/components/common/query/queryutilstest.cpp index 6a5b7f375..05cfab7ef 100644 --- a/tests/components/common/query/queryutilstest.cpp +++ b/tests/components/common/query/queryutilstest.cpp @@ -68,11 +68,12 @@ namespace lumiera virtual void run (Arg arg) { - if (isnil(arg)) arg = Cmdline ("Query normalizeID extractID countPraed"); + if (isnil(arg)) arg = Cmdline ("Query normalizeID extractID removeTerm countPraed"); if (contains (arg, "Query" )) check_Query (); if (contains (arg, "normalizeID")) check_normalizeID (); if (contains (arg, "extractID" )) check_extractID (); + if (contains (arg, "removeTerm" )) check_removeTerm (); if (contains (arg, "countPraed" )) check_countPraed (); } @@ -122,6 +123,32 @@ namespace lumiera + /** @test the regexp based cutting of a term with given symbol */ + void + check_removeTerm () + { + // successfull-----Symbol---input-string----------------------extracted------remaining------------- + ASSERT_removeTerm ("pred", "pred(tok).", "pred(tok)", "." ); + ASSERT_removeTerm ("pred", " pred( tok )", "pred(tok)", " " ); + ASSERT_removeTerm ("pred", "pred(tok), pred(tux).", "pred(tok)", "pred(tux)." ); + ASSERT_removeTerm ("pred", "other(xyz) pred(tok) pred(tux)", "pred(tok)", "other(xyz) pred(tux)" ); + ASSERT_removeTerm ("pred", "some( pred(tok)", "pred(tok)", "some( " ); + + // not successfull + ASSERT_removeTerm ("pred", "pred (tok", "", "pred (tok" ); + ASSERT_removeTerm ("pred", "pred tok)", "", "pred tok)" ); + ASSERT_removeTerm ("pred", "pred(tok", "", "pred(tok" ); + } + + void + ASSERT_removeTerm (Symbol sym, string input, Symbol extracted, Symbol modified) + { + ASSERT (extracted == removeTerm (sym, input)); + ASSERT (modified == input); + } + + + /** @test counting of predicates in a quiery * (currently 4/08 regexp based...) */