/
QueryParser.cpp
338 lines (287 loc) · 9.11 KB
/
QueryParser.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// The MIT License (MIT)
// Copyright (c) 2016, Microsoft
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <iostream> // TODO: remove.
#include <cctype>
#include <istream>
#include <sstream>
#include "BitFunnel/Allocators/IAllocator.h"
#include "BitFunnel/Configuration/IStreamConfiguration.h"
#include "BitFunnel/Plan/TermMatchNode.h"
#include "BitFunnel/Utilities/StringBuilder.h"
#include "QueryParser.h"
#include "StringVector.h"
namespace BitFunnel
{
QueryParser::QueryParser(std::istream& input,
IStreamConfiguration const & streamConfiguration,
IAllocator& allocator)
: m_input(input),
m_streamConfiguration(streamConfiguration),
m_allocator(allocator),
m_currentPosition(0),
m_haveChar(false)
{
}
TermMatchNode const * QueryParser::Parse()
{
return ParseOr();
}
TermMatchNode const * QueryParser::ParseOr()
{
TermMatchNode::Builder builder(TermMatchNode::OrMatch, m_allocator);
auto left = ParseAnd();
builder.AddChild(left);
for (;;)
{
SkipWhite();
if (PeekChar() != '|')
{
break;
}
GetChar();
auto child = ParseAnd();
builder.AddChild(child);
}
return builder.Complete();
}
TermMatchNode const * QueryParser::ParseAnd()
{
TermMatchNode::Builder builder(TermMatchNode::AndMatch, m_allocator);
char const * c_endOfAndProduction = ")|";
auto leftSimple = ParseSimple();
builder.AddChild(leftSimple);
for (;;)
{
SkipWhite();
char c = PeekChar();
if (c == '&')
{
// The '&' operator indicates there must be another Simple operand.
GetChar();
auto child = ParseSimple();
builder.AddChild(child);
}
else if (strchr(c_endOfAndProduction, c) == nullptr)
{
// The absense of a ')' or '|' indicates an implicit '&' operator
// which forces us to stay in the And-production and process another
// Simple operand.
auto child = ParseSimple();
builder.AddChild(child);
}
else
{
// Otherwise, we are done with the And-production.
break;
}
}
return builder.Complete();
}
TermMatchNode const * QueryParser::ParseSimple()
{
SkipWhite();
if (PeekChar() == '-')
{
GetChar();
SkipWhite();
auto simpleNode = ParseSimple();
TermMatchNode::Builder builder(TermMatchNode::NotMatch, m_allocator);
builder.AddChild(simpleNode);
return builder.Complete();
}
else if (PeekChar() == '(')
{
GetChar();
auto orNode = ParseOr();
SkipWhite();
ExpectDelimeter(')');
return orNode;
}
else
{
return ParseTerm();
}
}
TermMatchNode const * QueryParser::ParseTerm()
{
// Default streamId is always 0.
Term::StreamId streamId = 0;
SkipWhite();
if (PeekChar() == '"')
{
return ParsePhrase(streamId);
}
else
{
char const * left = ParseToken();
if (PeekChar() == ':')
{
// 'left' turns out to be a StreamId.
GetChar();
streamId = StreamIdFromText(left);
// Look for a phrase or term following the streamId.
if (PeekChar() == '"')
{
return ParsePhrase(streamId);
}
else
{
char const * right = ParseToken();
return TermMatchNode::Builder::CreateUnigramNode(right, streamId, m_allocator);
}
}
else
{
return TermMatchNode::Builder::CreateUnigramNode(left, streamId, m_allocator);
}
}
}
TermMatchNode const * QueryParser::ParsePhrase(Term::StreamId streamId)
{
ExpectDelimeter('"');
const unsigned arbitraryInitialCapacity = 6;
StringVector& grams =
*new(m_allocator.Allocate(sizeof(StringVector)))
StringVector(m_allocator, arbitraryInitialCapacity);
for (;;)
{
SkipWhite();
if (PeekChar() == '"')
{
ExpectDelimeter('"');
break;
}
// TODO: consider how we handle escapes.
char const * token = ParseToken();
grams.AddString(token);
}
return TermMatchNode::Builder::CreatePhraseNode(grams, streamId, m_allocator);
}
char const * QueryParser::ParseToken()
{
char const * c_endOfToken = "&|():-\"";
StringBuilder builder(m_allocator);
while (!isspace(PeekChar()) && strchr(c_endOfToken, PeekChar()) == nullptr)
{
char temp = GetWithEscape();
builder.push_back(temp);
};
char const* token = static_cast<char*>(builder);
if (*token == '\0')
{
throw ParseError("Expected token.", m_currentPosition);
}
return token;
}
void QueryParser::ExpectDelimeter(char c)
{
if (PeekChar() != c)
{
// TODO: REVIEW: Check lifetime of c_str() passed to exception constructor.
std::stringstream message;
message << "Expected '" << c << "' Got '" << PeekChar() << "'";
throw ParseError(message.str().c_str(), m_currentPosition);
}
else
{
GetChar();
}
}
void QueryParser::SkipWhite()
{
while (isspace(PeekChar()))
{
GetChar();
}
}
char QueryParser::GetWithEscape()
{
char c = PeekChar();
char const * legalEscapes = " \t\f\v&|\\()\":-";
if (c == '\\')
{
GetChar();
c = PeekChar();
if (strchr(legalEscapes, c) != nullptr)
{
return GetChar();
}
else
{
throw ParseError("Bad escape char",
c);
}
}
else
{
return GetChar();
}
}
char QueryParser::GetChar()
{
char result = PeekChar();
if (result == '\0')
{
throw ParseError("Attempting to read past NULL byte",
m_currentPosition);
}
++m_currentPosition;
m_haveChar = false;
return result;
}
char QueryParser::PeekChar()
{
if (!m_haveChar)
{
int temp = m_input.get();
// See https://github.com/BitFunnel/BitFunnel/issues/189.
if (temp != -1)
{
m_nextChar = static_cast<char>(temp);
}
else
{
m_nextChar = '\0';
}
m_haveChar = true;
}
return m_nextChar;
}
Term::StreamId QueryParser::StreamIdFromText(char const * streamName) const
{
return m_streamConfiguration.GetStreamId(streamName);
}
//*************************************************************************
//
// QueryParser::ParseError
//
//*************************************************************************
QueryParser::ParseError::ParseError(char const * message, size_t position)
: RecoverableError(message),
m_position(position)
{
}
std::ostream& operator<< (std::ostream &out, const QueryParser::ParseError &e)
{
out << std::string(e.m_position, ' ') << '^' << std::endl;
out << "Parser error (position = " << e.m_position << "): ";
out << e.what();
out << std::endl;
return out;
}
}