Parent: [533068] (diff)

Download this file

wasaparseaux.cpp    271 lines (240 with data), 8.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Copyright (C) 2006 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "autoconfig.h"
#include <iostream>
#include "wasatorcl.h"
#include "wasaparserdriver.h"
#include "searchdata.h"
#include "debuglog.h"
#define YYDEBUG 1
// bison-generated file
#include "wasaparse.hpp"
using namespace std;
using namespace Rcl;
void
yy::parser::error (const location_type& l, const std::string& m)
{
d->setreason(m);
}
SearchData *wasaStringToRcl(const RclConfig *config,
const std::string& stemlang,
const std::string& query, string &reason,
const std::string& autosuffs)
{
WasaParserDriver d(config, stemlang, autosuffs);
SearchData *sd = d.parse(query);
if (!sd)
reason = d.getreason();
return sd;
}
WasaParserDriver::WasaParserDriver(const RclConfig *c, const std::string sl,
const std::string& as)
: m_stemlang(sl), m_autosuffs(as), m_config(c),
m_index(0), m_result(0), m_haveDates(false),
m_maxSize((size_t)-1), m_minSize((size_t)-1)
{
}
WasaParserDriver::~WasaParserDriver()
{
}
SearchData *WasaParserDriver::parse(const std::string& in)
{
m_input = in;
m_index = 0;
delete m_result;
m_result = 0;
m_returns = stack<int>();
yy::parser parser(this);
parser.set_debug_level(0);
if (parser.parse() != 0) {
delete m_result;
m_result = 0;
}
if (m_result == 0)
return m_result;
// Set the top level filters (types, dates, size)
for (vector<string>::const_iterator it = m_filetypes.begin();
it != m_filetypes.end(); it++) {
m_result->addFiletype(*it);
}
for (vector<string>::const_iterator it = m_nfiletypes.begin();
it != m_nfiletypes.end(); it++) {
m_result->remFiletype(*it);
}
if (m_haveDates) {
m_result->setDateSpan(&m_dates);
}
if (m_minSize != (size_t)-1) {
m_result->setMinSize(m_minSize);
}
if (m_maxSize != (size_t)-1) {
m_result->setMaxSize(m_maxSize);
}
//if (m_result) m_result->dump(cout);
return m_result;
}
int WasaParserDriver::GETCHAR()
{
if (!m_returns.empty()) {
int c = m_returns.top();
m_returns.pop();
return c;
}
if (m_index < m_input.size())
return m_input[m_index++];
return 0;
}
void WasaParserDriver::UNGETCHAR(int c)
{
m_returns.push(c);
}
// Add clause to query, handling special pseudo-clauses for size/date
// etc. (mostly determined on field name).
bool WasaParserDriver::addClause(SearchData *sd,
SearchDataClauseSimple* cl)
{
if (cl->getfield().empty()) {
// Simple clause with empty field spec.
// Possibly change terms found in the "autosuffs" list into "ext"
// field queries
if (!m_autosuffs.empty()) {
vector<string> asfv;
if (stringToStrings(m_autosuffs, asfv)) {
if (find_if(asfv.begin(), asfv.end(),
StringIcmpPred(cl->gettext())) != asfv.end()) {
cl->setfield("ext");
cl->addModifier(SearchDataClause::SDCM_NOSTEMMING);
}
}
}
return sd->addClause(cl);
}
const string& ofld = cl->getfield();
string fld = stringtolower(ofld);
// MIME types and categories
if (!fld.compare("mime") || !fld.compare("format")) {
if (cl->getexclude()) {
m_nfiletypes.push_back(cl->gettext());
} else {
m_filetypes.push_back(cl->gettext());
}
delete cl;
return false;
}
if (!fld.compare("rclcat") || !fld.compare("type")) {
vector<string> mtypes;
if (m_config && m_config->getMimeCatTypes(cl->gettext(), mtypes)) {
for (vector<string>::iterator mit = mtypes.begin();
mit != mtypes.end(); mit++) {
if (cl->getexclude()) {
m_nfiletypes.push_back(*mit);
} else {
m_filetypes.push_back(*mit);
}
}
}
delete cl;
return false;
}
// Handle "date" spec
if (!fld.compare("date")) {
DateInterval di;
if (!parsedateinterval(cl->gettext(), &di)) {
LOGERR(("Bad date interval format: %s\n",
cl->gettext().c_str()));
m_reason = "Bad date interval format";
delete cl;
return false;
}
LOGDEB(("addClause:: date span: %d-%d-%d/%d-%d-%d\n",
di.y1,di.m1,di.d1, di.y2,di.m2,di.d2));
m_haveDates = true;
m_dates = di;
delete cl;
return false;
}
// Handle "size" spec
if (!fld.compare("size")) {
char *cp;
size_t size = strtoll(cl->gettext().c_str(), &cp, 10);
if (*cp != 0) {
switch (*cp) {
case 'k': case 'K': size *= 1000;break;
case 'm': case 'M': size *= 1000*1000;break;
case 'g': case 'G': size *= 1000*1000*1000;break;
case 't': case 'T': size *= size_t(1000)*1000*1000*1000;break;
default:
m_reason = string("Bad multiplier suffix: ") + *cp;
delete cl;
return false;
}
}
SearchDataClause::Relation rel = cl->getrel();
delete cl;
switch (rel) {
case SearchDataClause::REL_EQUALS:
m_maxSize = m_minSize = size;
break;
case SearchDataClause::REL_LT:
case SearchDataClause::REL_LTE:
m_maxSize = size;
break;
case SearchDataClause::REL_GT:
case SearchDataClause::REL_GTE:
m_minSize = size;
break;
default:
m_reason = "Bad relation operator with size query. Use > < or =";
return false;
}
return false;
}
if (!fld.compare("dir")) {
// dir filtering special case
SearchDataClausePath *nclause =
new SearchDataClausePath(cl->gettext(), cl->getexclude());
delete cl;
return sd->addClause(nclause);
}
if (cl->getTp() == SCLT_OR || cl->getTp() == SCLT_AND) {
// If this is a normal clause and the term has commas or
// slashes inside, take it as a list, turn the slashes/commas
// to spaces, leave unquoted. Otherwise, this would end up as
// a phrase query. This is a handy way to enter multiple terms
// to be searched inside a field. We interpret ',' as AND, and
// '/' as OR. No mixes allowed and ',' wins.
SClType tp = SCLT_FILENAME;// impossible value
string ns = neutchars(cl->gettext(), ",");
if (ns.compare(cl->gettext())) {
// had ','
tp = SCLT_AND;
} else {
ns = neutchars(cl->gettext(), "/");
if (ns.compare(cl->gettext())) {
// had not ',' but has '/'
tp = SCLT_OR;
}
}
if (tp != SCLT_FILENAME) {
SearchDataClauseSimple *ncl =
new SearchDataClauseSimple(tp, ns, ofld);
delete cl;
return sd->addClause(ncl);
}
}
return sd->addClause(cl);
}