00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "lib/query.hpp"
00025 #include "lib/util.hpp"
00026 #include "include/nobugcfg.h"
00027
00028 #include <boost/algorithm/string.hpp>
00029 #include <boost/regex.hpp>
00030 #include <map>
00031
00032 using std::map;
00033 using boost::regex;
00034 using boost::smatch;
00035 using boost::regex_search;
00036 using boost::sregex_iterator;
00037
00038 using util::contains;
00039 using util::isnil;
00040
00041
00042 namespace lumiera
00043 {
00044
00045 namespace query
00046 {
00047
00048 namespace
00049 {
00050 typedef boost::function<bool(string::value_type)> ChPredicate;
00051
00052 ChPredicate is_alpha = boost::algorithm::is_alpha();
00053 ChPredicate is_upper = boost::algorithm::is_upper();
00054 }
00055
00056
00057 void
00058 normalizeID (string& id)
00059 {
00060 id = util::sanitize(id);
00061 if (isnil(id) || !is_alpha (id[0]))
00062 id.insert(0, "o");
00063
00064
00065 REQUIRE (!isnil(id));
00066 REQUIRE (is_alpha (id[0]));
00067
00068 char first = id[0];
00069 if (is_upper (first))
00070 id[0] = std::tolower (first);
00071 }
00072
00073
00074
00075
00076 namespace
00077 {
00078 map<Symbol, regex> regexTable;
00079
00080 Symbol matchArgument = "\\(\\s*([\\w_\\.\\-]+)\\s*\\),?\\s*";
00081 regex findPredicate (string("(\\w+)")+matchArgument);
00082
00083 inline regex&
00084 getTermRegex (Symbol sym)
00085 {
00086 if (!contains (regexTable, sym))
00087 regexTable[sym] = regex (string(sym)+=matchArgument);
00088 return regexTable[sym];
00089 }
00090 }
00091
00098 const string
00099 extractID (Symbol sym, const string& termString)
00100 {
00101 smatch match;
00102 if (regex_search (termString, match, getTermRegex (sym)))
00103 return (match[1]);
00104 else
00105 return "";
00106 }
00107
00108
00113 const string
00114 removeTerm (Symbol sym, string& termString)
00115 {
00116 smatch match;
00117 if (regex_search (termString, match, getTermRegex (sym)))
00118 {
00119 string res (sym); res += "("+match[1]+")";
00120 termString.erase (match.position(), match[0].length());
00121 return res;
00122 }
00123 else
00124 return "";
00125 }
00126
00127
00133 uint
00134 countPraed (const string& q)
00135 {
00136 uint cnt (0);
00137 sregex_iterator end;
00138 for (sregex_iterator i (q.begin(),q.end(), findPredicate);
00139 i != end; ++i)
00140 ++cnt;
00141 return cnt;
00142 }
00143
00144 }
00145
00146
00150 }