Lumiera  0.pre.03
»edit your freedom«
query-util.cpp
Go to the documentation of this file.
1 /*
2  QueryUtil - support for working with terms and queries
3 
4  Copyright (C)
5  2008, 2012 Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
20 #include "lib/error.hpp"
21 #include "lib/query-util.hpp"
22 #include "lib/util.hpp"
23 
24 #include <boost/algorithm/string.hpp>
25 #include <functional>
26 #include <regex>
27 #include <map>
28 
29 using std::map;
30 using std::regex;
31 using std::smatch;
32 using std::regex_search;
33 using std::sregex_iterator;
34 
35 using util::contains;
36 using util::isnil;
37 
38 
39 namespace lib {
40  namespace query {
41 
42  namespace { // local definitions
43 
44  using ChPredicate = std::function<bool(string::value_type)> ;
45 
46  ChPredicate is_alpha = boost::algorithm::is_alpha();
47  ChPredicate is_upper = boost::algorithm::is_upper();
48  } // local defs
49 
50 
51  void
52  normaliseID (string& id)
53  {
54  id = util::sanitise(id);
55  if (isnil(id) || !is_alpha (id[0]))
56  id.insert(0, "o");
57 
58 
59  REQUIRE (!isnil(id));
60  REQUIRE (is_alpha (id[0]));
61 
62  char first = id[0];
63  if (is_upper (first))
64  id[0] = std::tolower (first);
65  }
66 
67 
68 
70  namespace{ // Implementation details
71 
72  map<Symbol, regex> regexTable;
73 
74  Literal MATCH_ARGUMENT = R"~(\(\s*([\w_\.\-]+)\s*\),?\s*)~";
75  const regex FIND_PREDICATE{string{"(\\w+)"} + MATCH_ARGUMENT};
76 
77  inline regex&
78  getTermRegex (Symbol sym)
79  {
80  if (!contains (regexTable, sym))
81  regexTable[sym] = regex (string(sym)+MATCH_ARGUMENT);
82  return regexTable[sym];
83  }
84  }
85 
91  string
92  extractID (Symbol sym, const string& termString)
93  {
94  smatch match;
95  if (regex_search (termString, match, getTermRegex (sym)))
96  return (match[1]);
97  else
98  return "";
99  }
100 
101 
108  string
109  removeTerm (Symbol sym, string& queryString)
110  {
111  smatch match;
112  if (regex_search (queryString, match, getTermRegex (sym)))
113  {
114  string res (sym); res += "("+match[1]+")";
115  queryString.erase (match.position(), match[0].length());
116  return res;
117  }
118  else
119  return "";
120  }
121 
122 
123  bool
124  hasTerm (Symbol sym, string const& queryString)
125  {
126  smatch match;
127  return regex_search (queryString, match, getTermRegex (sym));
128  }
129 
130 
136  uint
137  countPred (const string& q)
138  {
139  uint cnt (0);
140  sregex_iterator end;
141  for (sregex_iterator i (q.begin(),q.end(), FIND_PREDICATE);
142  i != end; ++i)
143  ++cnt;
144  return cnt;
145  }
146 
147 
151  string
152  appendTerms (string const& pred1, string const& pred2)
153  {
154  return isnil(pred1)? pred2
155  : isnil(pred2)? pred1
156  : pred1 + ", " + pred2;
157  }
158 
159 
160  } // namespace query
161 
162 } // namespace lib
Utilities to support working with predicate queries.
uint countPred(const string &q)
count the top-level predicates in the query string.
Definition: query-util.cpp:137
void normaliseID(string &id)
ensure standard format for a given id string.
Definition: query-util.cpp:52
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
Implementation namespace for support and library code.
string appendTerms(string const &pred1, string const &pred2)
Definition: query-util.cpp:152
string extractID(Symbol sym, const string &termString)
(preliminary) helper: instead of really parsing and evaluating the terms, just do a regular expressio...
Definition: query-util.cpp:92
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
string removeTerm(Symbol sym, string &queryString)
(preliminary) helper: cut a term with the given symbol.
Definition: query-util.cpp:109
Lumiera error handling (C++ interface).
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255