-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFilterParser.cs
607 lines (537 loc) · 24.7 KB
/
FilterParser.cs
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
namespace Ceen.Database
{
/// <summary>
/// Helper class that can parse a limited subset of an SQL where statement
/// </summary>
public static class FilterParser
{
/// <summary>
/// Exception for invalid filter strings
/// </summary>
public class ParserException : Exception
{
/// <summary>
/// Creats a new parser exception
/// </summary>
/// <param name="message">The error message</param>
public ParserException(string message)
: base(message)
{
}
}
/// <summary>
/// The UNIX timestamp epoch value
/// </summary>
private static readonly DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0);
/// <summary>
/// The regular expression to use for parsing an orderBy string
/// </summary>
private static readonly Regex _orderByTokenizer =
new Regex(
@"\s*(?<sortorder1>\+|\-)?((?<nonquoted>\w+)|((?<quoted>""[^""]*"")))(\s+(?<sortorder2>\w+))?\s*(?<comma>,?)"
);
/// <summary>
/// A regular expression to tokenize a filter string,
/// looking for quoted and unquoted identifiers
/// and supporting a small number of arithmetic and compare operators
/// </summary>
private static readonly Regex _filterTokenizer =
new Regex(
@"(?<number>(\d+(\.\d*))|(\.\d+))|(?<nonquoted>\w+)|((?<quoted>""[^""]*""))|(?<special>\<=|\>=|==|!=|<>|\(|\)|<|=|>|\+|-|\*|/|\%|,)|(?<whitespace>\s+)"
);
/// <summary>
/// Operator preceedence table, based on:
/// https://www.sqlite.org/lang_expr.html
///
/// The operators are applied bottom-up, meaning we split on the lowest
/// priority and then recursively evaluate the parts, leaving us with
/// the correct "higher-value-first" bindings.
///
/// But we need to make sure the parenthesis is always binding hardest
/// as it changes precedence, thus it has the lowest priority value
/// </summary>
private static readonly Dictionary<string, int> _priorityTable
= new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
{ ")", 50 }, { "(", 1 }, { ",", 2 },
{ "*", 40 }, { "%", 40 }, { "/", 40 },
{ "+", 35 }, { "-", 35 }, { "not", 35 },
{ "<", 30 }, { "<=", 30 }, { ">", 30 }, { ">=", 30 },
{ "=", 25 }, { "==", 25 }, { "!=", 25 }, { "<>", 25 }, { "in", 25 }, { "like", 25 },
{ "and", 20 },
{ "or", 15 }
};
/// <summary>
/// Map of all supported arithmetic operators
/// </summary>
private static readonly HashSet<string> _arithmeticOps
= new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"*", "%", "/", "+", "-"
};
/// <summary>
/// Map of all supported compare operators
/// </summary>
private static readonly HashSet<string> _compareOps
= new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"<", ">", "<=", ">=", "=", "==",
"!=", "<>", "in", "like"
};
/// <summary>
/// Map of all supported binary operators
/// </summary>
private static readonly HashSet<string> _binOps
= new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"*", "%", "/", "+", "-", "<", ">",
"<=", ">=", "=", "==", "!=", "<>",
"in", "like", "and", "or"
};
/// <summary>
/// Map of all supported unary operators
/// </summary>
private static readonly HashSet<string> _unOps
= new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"+", "-", "not"
};
/// <summary>
/// Structure for keeping track of semi-parsed tokens
/// </summary>
private class SemiParsed
{
/// <summary>
/// The original token string
/// </summary>
public readonly string Token;
/// <summary>
/// The offset into the original string
/// </summary>
public readonly int Offset;
/// <summary>
/// The potentially parsed item
/// </summary>
public QueryElement Parsed;
/// <summary>
/// The priority of the item
/// </summary>
public readonly int Priority;
/// <summary>
/// Constructs a new semi-parsed instance
/// </summary>
/// <param name="map">The table mapping</param>
/// <param name="value">The token being parsed</param>
/// <param name="offset">The string offset</param>
public SemiParsed(TableMapping map, string value, int offset)
{
Token = value;
Offset = offset;
if (!_priorityTable.TryGetValue(value, out Priority))
{
if (Token.StartsWith("\"") && Token.EndsWith("\""))
{
Parsed = new Value(Token.Substring(1, Token.Length - 2));
}
else
{
var prop = map.AllColumns.FirstOrDefault(x => string.Equals(x.MemberName, Token, StringComparison.OrdinalIgnoreCase));
if (prop != null)
Parsed = new Property(prop.MemberName);
else
Parsed = new Value(Token);
}
}
}
}
/// <summary>
/// Parses an order string
/// </summary>
/// <param name="map">The table map to yse</param>
/// <param name="order">The order string</param>
/// <returns>The parsed order</returns>
public static QueryOrder ParseOrder(TableMapping map, string order)
{
QueryOrder res = null;
foreach(var n in ParseOrderList(map, order).Reverse())
res = new QueryOrder(n, res);
return res;
}
/// <summary>
/// Parses an order string
/// </summary>
/// <param name="map">The table map to yse</param>
/// <param name="order">The order string</param>
/// <returns>The parsed order elements</returns>
public static IEnumerable<QueryOrder> ParseOrderList(TableMapping map, string order)
{
if (string.IsNullOrWhiteSpace(order))
yield break;
// Make a case-insensitive lookup table for the column names
var propmap = map.AllColumns
.ToDictionary(
x => x.MemberName,
StringComparer.OrdinalIgnoreCase
);
var prevcomma = true;
var pos = 0;
foreach (var m in _orderByTokenizer.Matches(order).Cast<Match>())
{
if (!prevcomma)
throw new ParserException($"Missing comma before: {m.Value} at {m.Index}");
if (!m.Success)
throw new ParserException($"No match at {m.Index}");
if (pos != m.Index)
throw new ParserException($"Failed to parse {order.Substring(pos, m.Index - pos)} at offset {pos}");
pos += m.Length;
var dir = string.Empty;
if (m.Groups["sortorder1"].Success)
dir = m.Groups["sortorder1"].Value;
if (m.Groups["sortorder2"].Success)
{
if (!string.IsNullOrWhiteSpace(dir))
throw new ParserException($"Cannot use both pre- and post-fix direction specifiers: {m.Value} at offset {m.Index}");
dir = m.Groups["sortorder2"].Value;
}
if (string.IsNullOrWhiteSpace(dir) || string.Equals(dir, "ASC", StringComparison.OrdinalIgnoreCase))
dir = "+";
if (string.Equals(dir, "DESC", StringComparison.OrdinalIgnoreCase))
dir = "-";
if (dir != "-" && dir != "+")
throw new ParserException($"Unsupported direction specifier: {dir}");
var column = m.Groups["quoted"].Success
? m.Groups["quoted"].Value
: m.Groups["nonquoted"].Value;
if (!propmap.ContainsKey(column))
throw new ParserException($"The property {column} does not exist on the type");
yield return new QueryOrder(propmap[column].MemberName, dir == "-");
prevcomma = m.Groups["comma"].Length > 0;
}
if (pos != order.Length)
throw new ParserException($"Failed to parse {order.Substring(pos)} at offset {pos}");
if (prevcomma)
throw new ParserException($"Invalid trailing comma: {order}");
}
/// <summary>
/// Parsese a filter string and returns a query element for it
/// </summary>
/// <param name="map">The table map to yse</param>
/// <param name="filter">The filter to parse</param>
/// <returns>The parsed query</returns>
public static QueryElement ParseFilter(TableMapping map, string filter)
{
if (string.IsNullOrWhiteSpace(filter))
return new Empty();
var lst = Parse(map, Tokenize(map, filter).ToList());
if (lst.Count() == 0)
return new Empty();
else if (lst.Count() > 1)
throw new ParserException($"Found multiple expressions: {string.Join(", ", lst.Select(x => x.Offset))}");
return CorrectValues(map, lst.First().Parsed ?? throw new ParserException("Unexpected null value"));
}
/// <summary>
/// Visits the query elements and converts any string values to match the operands
/// </summary>
/// <param name="map">The table mapping</param>
/// <param name="top">The element to explore</param>
/// <returns>The top element</returns>
private static QueryElement CorrectValues(TableMapping map, QueryElement top)
{
// Keep a reference to the element we return
var entry = top;
// Remove any parenthesis
while (top is ParenthesisExpression pe)
top = (QueryElement)pe.Expression;
if (top is Compare cp)
CorrectValues(map, null, (QueryElement)cp.LeftHandSide, (QueryElement)cp.RightHandSide);
if (top is Arithmetic am)
CorrectValues(map, null, (QueryElement)am.LeftHandSide, (QueryElement)am.RightHandSide);
if (top is And andExp)
foreach (var e in andExp.Items)
CorrectValues(map, typeof(bool), (QueryElement)e);
if (top is Or orExp)
foreach (var e in orExp.Items)
CorrectValues(map, typeof(bool), (QueryElement)e);
if (top is UnaryOperator ue)
{
if (string.Equals(ue.Operator, "not", StringComparison.OrdinalIgnoreCase))
CorrectValues(map, typeof(bool), (QueryElement)ue.Expression);
else
CorrectValues(map, (QueryElement)ue.Expression);
}
return entry;
}
/// <summary>
/// Changes the string value of any <see name="Value" /> instances to match the property types
/// </summary>
/// <param name="map">The table mapping</param>
/// <param name="targettype">The type to change the item to</param>
/// <param name="left">The left element</param>
/// <param name="right">The right element</param>
private static void CorrectValues(TableMapping map, Type targettype, QueryElement left, QueryElement right)
{
if (left is Property lpr)
CorrectValues(map, map.AllColumnsByMemberName[lpr.PropertyName].MemberType, right);
else if (right is Property rpr)
CorrectValues(map, map.AllColumnsByMemberName[rpr.PropertyName].MemberType, left);
else if (left is Compare lcp)
CorrectValues(map, typeof(bool), right);
else if (left is Compare rcp)
CorrectValues(map, typeof(bool), left);
else
{
CorrectValues(map, targettype, left);
CorrectValues(map, targettype, right);
}
}
/// <summary>
/// Changes the string value of any <see name="Value" /> instances to match the property types
/// </summary>
/// <param name="map">The table mapping</param>
/// <param name="targettype">The type to change the item to</param>
/// <param name="el">The element to visit</param>
private static void CorrectValues(TableMapping map, Type targettype, QueryElement el)
{
while(el is ParenthesisExpression p)
el = (QueryElement)p.Expression;
if (el is Arithmetic a)
CorrectValues(map, targettype, (QueryElement)a.LeftHandSide, (QueryElement)a.RightHandSide);
if (el is Compare c)
CorrectValues(map, targettype, (QueryElement)c.LeftHandSide, (QueryElement)c.RightHandSide);
if (el is And andExp)
foreach (var n in andExp.Items)
CorrectValues(map, targettype, (QueryElement)n);
if (el is Or orExp)
foreach (var n in orExp.Items)
CorrectValues(map, targettype, (QueryElement)n);
// Change the type if we get here
if (el is Value v && targettype != null && v.Item is string vs)
{
v.Item = ConvertEl(vs, targettype);
}
else if (el is Value vx && targettype != null && vx.Item is Array en)
{
for(var i = 0; i < en.Length; i++)
if (en.GetValue(i) is string vsa)
en.SetValue(ConvertEl(vsa, targettype), i);
}
}
/// <summary>
/// Converts a string value to the target type
/// </summary>
/// <param name="vs">The input string</param>
/// <param name="targettype">The desired type</param>
/// <returns>The converted object</returns>
private static object ConvertEl(string vs, Type targettype)
{
if (targettype.IsEnum)
{
object e = null;
try { e = Enum.Parse(targettype, vs, true); }
catch { }
if (e == null)
throw new ParserException($"Cannot parse {vs} as a {targettype.Name}");
// We return as a string, because enums are stored as strings
return e.ToString();
}
else if (targettype == typeof(bool))
{
if (string.Equals("true", vs, StringComparison.OrdinalIgnoreCase))
return true;
else if (string.Equals("false", vs, StringComparison.OrdinalIgnoreCase))
return false;
else
throw new ParserException($"Cannot parse {vs} as a boolean");
}
else if (targettype.IsPrimitive)
{
try { return Convert.ChangeType(vs, targettype); }
catch
{
throw new ParserException($"Cannot parse {vs} as a {targettype.Name}");
}
}
else if (targettype == typeof(TimeSpan))
{
if (!double.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture, out var lval))
throw new ParserException($"Cannot parse {vs} as a {targettype.Name}");
return TimeSpan.FromSeconds(lval);
}
else if (targettype == typeof(DateTime))
{
if (!double.TryParse(vs, NumberStyles.Any, CultureInfo.InvariantCulture, out var lval))
throw new ParserException($"Cannot parse {vs} as a {targettype.Name}");
return EPOCH.AddSeconds(lval);
}
return vs;
}
/// <summary>
/// Parses a sequence of tokens into a list of parsed elements
/// </summary>
/// <param name="map">The table map</param>
/// <param name="semiparsed">The list of semi-parsed items</param>
/// <returns>An updated condensed list of parsed items</returns>
private static List<SemiParsed> Parse(TableMapping map, List<SemiParsed> semiparsed)
{
// Keep on parsing untill everything is parsed
while(semiparsed.Any(x => x.Parsed == null))
{
var best_index = -1;
for (int i = 0; i < semiparsed.Count; i++)
{
var sp = semiparsed[i];
if (sp.Parsed == null && (best_index < 0 || sp.Priority < semiparsed[best_index].Priority))
best_index = i;
}
if (best_index < 0)
throw new ParserException("Unable to parse filter");
var best = semiparsed[best_index];
if (best.Token == "(")
{
// Find the next matching brace
var count = 1;
var p = best_index + 1;
for (; p < semiparsed.Count; p++)
{
var t = semiparsed[p];
if (t.Parsed != null)
continue;
if (t.Token == "(")
count++;
else if (t.Token == ")")
count--;
if (count == 0)
break;
}
if (count != 0 || p >= semiparsed.Count)
throw new ParserException($"Unbalanced parenthesis starting at {best.Offset}");
var subseq = semiparsed.GetRange(best_index + 1, p - best_index - 1);
semiparsed.RemoveRange(best_index + 1, p - best_index);
var parsed = Parse(map, subseq);
if (parsed.Count != 1)
throw new ParserException($"Unable to parse sub expression starting at {best.Offset}");
best.Parsed = new ParenthesisExpression(
parsed.First().Parsed
?? throw new ParserException($"Failed to parse {parsed.First().Token} at {parsed.First().Offset}")
);
}
else if (_binOps.Contains(best.Token) || _unOps.Contains(best.Token))
{
if (best_index == semiparsed.Count - 1)
throw new ParserException($"No right-hand operand for {best.Token} at {best.Offset}");
var right = Parse(map, semiparsed.GetRange(best_index + 1, semiparsed.Count - best_index - 1));
var right_hand = right.First();
// Handle unary operators
if (best_index == 0 || !_binOps.Contains(best.Token))
{
if (_unOps.Contains(best.Token))
{
right[0] = best;
best.Parsed = new UnaryOperator(best.Token, right_hand);
semiparsed = right;
}
else
{
throw new ParserException($"No left-hand operand for {best.Token} at {best.Offset}");
}
}
else
{
var left = Parse(map, semiparsed.GetRange(0, best_index));
var left_hand = left.Last();
var t = best.Token;
if (_arithmeticOps.Contains(t))
best.Parsed = new Arithmetic(left_hand.Parsed, t, right_hand.Parsed);
else if (_compareOps.Contains(t))
{
if (t == "==")
t = "=";
else if (t == "<>")
t = "!=";
best.Parsed = new Compare(left_hand.Parsed, t, right_hand.Parsed);
}
else if (string.Equals(t, "and"))
best.Parsed = new And(left_hand.Parsed, right_hand.Parsed);
else if (string.Equals(t, "or"))
best.Parsed = new Or(left_hand.Parsed, right_hand.Parsed);
else
throw new ParserException($"Failed to classify operator {best.Token} at {best.Offset}");
// Remove the left-hand symbol
left.RemoveAt(left.Count - 1);
// Replace the right-hand symbol
right[0] = best;
// Build the new list of unifished tasks
semiparsed = left.Concat(right).ToList();
}
}
else
{
if (best.Token == ")")
throw new ParserException($"Dangling parenthesis at {best.Offset}");
else if (best.Token == ",")
{
// Most likely this is a sequence of elements
if (semiparsed.Count > 1 && (semiparsed.Count % 2) == 1)
{
var good = true;
for (int i = 0; i < semiparsed.Count; i++)
{
if (i % 2 == 0)
good &= semiparsed[i].Parsed is Value;
else
good &= semiparsed[i].Token == ",";
}
if (good)
{
semiparsed[0].Parsed = new Value(
semiparsed
.Where(x => x != null)
.Select(x => x.Parsed)
.OfType<Value>()
.Select(x => x.Item)
.ToArray()
);
semiparsed.RemoveRange(1, semiparsed.Count - 1);
continue;
}
}
throw new ParserException($"Mismatched comma at {best.Offset}");
}
else
throw new ParserException($"Unable to process token {best.Token} at {best.Offset}");
}
}
return semiparsed;
}
/// <summary>
/// Divides a filter string into tokens
/// </summary>
/// <param name="filter">The filter string</param>
/// <returns>A sequence of tokens</returns>
private static IEnumerable<SemiParsed> Tokenize(TableMapping map, string filter)
{
if (string.IsNullOrWhiteSpace(filter))
yield break;
var pos = 0;
foreach (var item in _filterTokenizer.Matches(filter).Cast<Match>())
{
if (!item.Success)
throw new ParserException($"Failed to parse {item.Value} at offset {item.Index}");
if (pos != item.Index)
throw new ParserException($"Failed to parse {filter.Substring(pos, item.Index - pos)} at offset {pos}");
// Ignore whitespace tokens
if (!string.IsNullOrWhiteSpace(item.Value))
yield return new SemiParsed(map, item.Value, item.Index);
pos += item.Length;
}
if (pos != filter.Length)
throw new ParserException($"Failed to parse {filter.Substring(pos)} at offset {pos}");
}
}
}