Child: [c39c94] (diff)

Download this file

wasaparseaux.cpp    237 lines (208 with data), 7.0 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
/* 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.h"
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;
}
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;
}
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& fld = cl->getfield();
// MIME types and categories
if (!stringicmp("mime", fld) ||!stringicmp("format", fld)) {
if (cl->getexclude()) {
sd->remFiletype(cl->gettext());
} else {
sd->addFiletype(cl->gettext());
}
delete cl;
return true;
}
if (!stringicmp("rclcat", fld) || !stringicmp("type", fld)) {
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()) {
sd->remFiletype(*mit);
} else {
sd->addFiletype(*mit);
}
}
}
delete cl;
return true;
}
// Handle "date" spec
if (!stringicmp("date", fld)) {
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));
sd->setDateSpan(&di);
delete cl;
return true;
}
// Handle "size" spec
if (!stringicmp("size", fld)) {
char *cp;
size_t size = strtoll(cl->gettext().c_str(), &cp, 10);
if (*cp != 0) {
switch (*cp) {
case 'k': case 'K': size *= 1E3;break;
case 'm': case 'M': size *= 1E6;break;
case 'g': case 'G': size *= 1E9;break;
case 't': case 'T': size *= 1E12;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:
sd->setMaxSize(size);
sd->setMinSize(size);
break;
case SearchDataClause::REL_LT:
case SearchDataClause::REL_LTE:
sd->setMaxSize(size);
break;
case SearchDataClause::REL_GT:
case SearchDataClause::REL_GTE:
sd->setMinSize(size);
break;
default:
m_reason = "Bad relation operator with size query. Use > < or =";
return false;
}
return true;
}
if (!stringicmp("dir", fld)) {
// 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, fld);
delete cl;
return sd->addClause(ncl);
}
}
return sd->addClause(cl);
}