-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathInterpreterParser.cs
208 lines (191 loc) · 6.28 KB
/
InterpreterParser.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
using System.Collections.Generic;
namespace Analogy
{
public interface IElement
{
string Value { get; }
}
public class SearchExpression : IElement
{
public SearchExpression(string value)
{
Value = value;
}
public string Value { get; }
}
public class SearchOperation : IElement
{
private static string EscapeLikeValue(string value)
{
StringBuilder sb = new StringBuilder(value.Length);
foreach (var c in value)
{
switch (c)
{
case ']':
case '[':
case '%':
case '*':
sb.Append("[").Append(c).Append("]");
break;
case '\'':
sb.Append("''");
break;
case '@':
break;
default:
sb.Append(c);
break;
}
}
return sb.ToString();
}
public enum SearchType
{
And, Or,
}
public SearchType Type;
public IElement Left, Right;
public string Value
{
get
{
if (Left == null)
{
return Right.Value;
}
if (Right == null)
{
return Left.Value;
}
switch (Type)
{
case SearchType.And:
return $"Text like '%{EscapeLikeValue(Left.Value)}%' and Text like '%{EscapeLikeValue(Right.Value)}%'";
case SearchType.Or:
return $"Text like '%{EscapeLikeValue(Left.Value)}%' Or Text like '%{EscapeLikeValue(Right.Value)}%'";
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
public enum SearchType
{
Expression, And, Or, Lparentheses, Rparentheses,
}
public class Token
{
public string Text { get; set; }
public SearchType Type { get; set; }
public Token(string text, SearchType type)
{
Text = text;
Type = type;
}
public override string ToString()
{
return $"{nameof(Text)}: {Text}";
}
}
public class InterpreterParser
{
public static List<Token> Lex(string text)
{
var result = new List<Token>();
for (var i = 0; i < text.Length; i++)
{
char c = text[i];
switch (c)
{
case '+':
result.Add(new Token("+", SearchType.And));
break;
case '&':
result.Add(new Token("&", SearchType.And));
break;
case '|':
result.Add(new Token("|", SearchType.Or));
break;
case '(':
result.Add(new Token("(", SearchType.Lparentheses));
break;
case ')':
result.Add(new Token(")", SearchType.Rparentheses));
break;
default:
var sb = new StringBuilder(c.ToString());
for (int j = i + 1; j < text.Length; j++)
{
if (text[j] == '+' || text[j] == '&' || text[j] == '(' || text[j] == ')' || text[j] == '|')
{
result.Add(new Token(sb.ToString(), SearchType.Expression));
break;
}
else
{
sb.Append(text[j]);
++i;
}
}
break;
}
}
return result;
}
public static IElement Parse(IReadOnlyList<Token> tokens)
{
var result = new SearchOperation();
bool haveLHS = false;
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
switch (token.Type)
{
case SearchType.Expression:
if (!haveLHS)
{
result.Left = new SearchExpression(token.Text);
haveLHS = true;
}
else
{
result.Right = new SearchExpression(token.Text);
}
break;
case SearchType.And:
result.Type = SearchOperation.SearchType.And;
break;
case SearchType.Or:
result.Type = SearchOperation.SearchType.Or;
break;
case SearchType.Lparentheses:
int j = i;
for (; j < tokens.Count; ++j)
{
if (tokens[j].Type == SearchType.Rparentheses)
{
break;
}
}
var subexpression = tokens.Skip(i + 1).Take(j - i - 1).ToList();
var element = Parse(subexpression);
if (!haveLHS)
{
result.Left = element;
haveLHS = true;
}
else
{
result.Right = element;
}
i = j;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return result;
}
}
}