This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
FilterExpressionTokenizer.cs
205 lines (187 loc) · 8.82 KB
/
FilterExpressionTokenizer.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
using Superpower;
using Superpower.Model;
using System.Collections.Generic;
namespace Serilog.Filters.Expressions.Parsing
{
class FilterExpressionTokenizer : Tokenizer<FilterExpressionToken>
{
static readonly FilterExpressionToken[] SimpleOps = new FilterExpressionToken[128];
static readonly HashSet<FilterExpressionToken> PreRegexTokens = new HashSet<FilterExpressionToken>
{
FilterExpressionToken.And,
FilterExpressionToken.Or,
FilterExpressionToken.Not,
FilterExpressionToken.LParen,
FilterExpressionToken.LBracket,
FilterExpressionToken.Comma,
FilterExpressionToken.Equal,
FilterExpressionToken.NotEqual,
FilterExpressionToken.Like,
FilterExpressionToken.GreaterThan,
FilterExpressionToken.GreaterThanOrEqual,
FilterExpressionToken.LessThan,
FilterExpressionToken.LessThanOrEqual,
FilterExpressionToken.In,
FilterExpressionToken.Is
};
static readonly FilterExpressionKeyword[] Keywords =
{
new FilterExpressionKeyword("and", FilterExpressionToken.And),
new FilterExpressionKeyword("in", FilterExpressionToken.In),
new FilterExpressionKeyword("is", FilterExpressionToken.Is),
new FilterExpressionKeyword("like", FilterExpressionToken.Like),
new FilterExpressionKeyword("not", FilterExpressionToken.Not),
new FilterExpressionKeyword("or", FilterExpressionToken.Or),
new FilterExpressionKeyword("true", FilterExpressionToken.True),
new FilterExpressionKeyword("false", FilterExpressionToken.False),
new FilterExpressionKeyword("null", FilterExpressionToken.Null)
};
static FilterExpressionTokenizer()
{
SimpleOps['+'] = FilterExpressionToken.Plus;
SimpleOps['-'] = FilterExpressionToken.Minus;
SimpleOps['*'] = FilterExpressionToken.Asterisk;
SimpleOps['/'] = FilterExpressionToken.ForwardSlash;
SimpleOps['%'] = FilterExpressionToken.Percent;
SimpleOps['^'] = FilterExpressionToken.Caret;
SimpleOps['<'] = FilterExpressionToken.LessThan;
SimpleOps['>'] = FilterExpressionToken.GreaterThan;
SimpleOps['='] = FilterExpressionToken.Equal;
SimpleOps[','] = FilterExpressionToken.Comma;
SimpleOps['.'] = FilterExpressionToken.Period;
SimpleOps['('] = FilterExpressionToken.LParen;
SimpleOps[')'] = FilterExpressionToken.RParen;
SimpleOps['['] = FilterExpressionToken.LBracket;
SimpleOps[']'] = FilterExpressionToken.RBracket;
SimpleOps['*'] = FilterExpressionToken.Asterisk;
SimpleOps['?'] = FilterExpressionToken.QuestionMark;
}
protected override IEnumerable<Result<FilterExpressionToken>> Tokenize(
TextSpan stringSpan,
TokenizationState<FilterExpressionToken> tokenizationState)
{
var next = SkipWhiteSpace(stringSpan);
if (!next.HasValue)
yield break;
do
{
if (char.IsDigit(next.Value))
{
var hex = FilterExpressionTextParsers.HexInteger(next.Location);
if (hex.HasValue)
{
next = hex.Remainder.ConsumeChar();
yield return Result.Value(FilterExpressionToken.HexNumber, hex.Location, hex.Remainder);
}
else
{
var real = FilterExpressionTextParsers.Real(next.Location);
if (!real.HasValue)
yield return Result.CastEmpty<TextSpan, FilterExpressionToken>(real);
else
yield return Result.Value(FilterExpressionToken.Number, real.Location, real.Remainder);
next = real.Remainder.ConsumeChar();
}
if (!IsDelimiter(next))
{
yield return Result.Empty<FilterExpressionToken>(next.Location, new[] { "digit" });
}
}
else if (next.Value == '\'')
{
var str = FilterExpressionTextParsers.SqlString(next.Location);
if (!str.HasValue)
yield return Result.CastEmpty<string, FilterExpressionToken>(str);
next = str.Remainder.ConsumeChar();
yield return Result.Value(FilterExpressionToken.String, str.Location, str.Remainder);
}
else if (next.Value == '@')
{
var beginIdentifier = next.Location;
var startOfName = next.Remainder;
do
{
next = next.Remainder.ConsumeChar();
}
while (next.HasValue && char.IsLetterOrDigit(next.Value));
if (next.Remainder == startOfName)
{
yield return Result.Empty<FilterExpressionToken>(startOfName, new[] { "built-in identifier name" });
}
else
{
yield return Result.Value(FilterExpressionToken.BuiltInIdentifier, beginIdentifier, next.Location);
}
}
else if (char.IsLetter(next.Value) || next.Value == '_')
{
var beginIdentifier = next.Location;
do
{
next = next.Remainder.ConsumeChar();
}
while (next.HasValue && (char.IsLetterOrDigit(next.Value) || next.Value == '_'));
FilterExpressionToken keyword;
if (TryGetKeyword(beginIdentifier.Until(next.Location), out keyword))
{
yield return Result.Value(keyword, beginIdentifier, next.Location);
}
else
{
yield return Result.Value(FilterExpressionToken.Identifier, beginIdentifier, next.Location);
}
}
else if (next.Value == '/' &&
(!tokenizationState.Previous.HasValue ||
PreRegexTokens.Contains(tokenizationState.Previous.Value.Kind)))
{
var regex = FilterExpressionTextParsers.RegularExpression(next.Location);
if (!regex.HasValue)
yield return Result.CastEmpty<Unit, FilterExpressionToken>(regex);
yield return Result.Value(FilterExpressionToken.RegularExpression, next.Location, regex.Remainder);
next = regex.Remainder.ConsumeChar();
}
else
{
var compoundOp = FilterExpressionTextParsers.CompoundOperator(next.Location);
if (compoundOp.HasValue)
{
yield return Result.Value(compoundOp.Value, compoundOp.Location, compoundOp.Remainder);
next = compoundOp.Remainder.ConsumeChar();
}
else if (next.Value < SimpleOps.Length && SimpleOps[next.Value] != FilterExpressionToken.None)
{
yield return Result.Value(SimpleOps[next.Value], next.Location, next.Remainder);
next = next.Remainder.ConsumeChar();
}
else
{
yield return Result.Empty<FilterExpressionToken>(next.Location);
next = next.Remainder.ConsumeChar();
}
}
next = SkipWhiteSpace(next.Location);
} while (next.HasValue);
}
static bool IsDelimiter(Result<char> next)
{
return !next.HasValue ||
char.IsWhiteSpace(next.Value) ||
next.Value < SimpleOps.Length && SimpleOps[next.Value] != FilterExpressionToken.None;
}
static bool TryGetKeyword(TextSpan span, out FilterExpressionToken keyword)
{
foreach (var kw in Keywords)
{
if (span.EqualsValueIgnoreCase(kw.Text))
{
keyword = kw.Token;
return true;
}
}
keyword = FilterExpressionToken.None;
return false;
}
public static FilterExpressionTokenizer Instance { get; } = new FilterExpressionTokenizer();
}
}