Repository navigation
Expand file tree
/
Copy pathExpressionParser.Parser.cs
More file actions
316 lines (266 loc) · 11.1 KB
/
Copy pathExpressionParser.Parser.cs
File metadata and controls
316 lines (266 loc) · 11.1 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
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
using Ramstack.Parsing.Parsers;
using static Ramstack.Parsing.Parser;
namespace Ramstack.Parsing;
partial class ExpressionParser
{
/// <summary>
/// The maximum depth of an expression tree produced by the parser.
/// </summary>
/// <remarks>
/// The expression builder, the <c>ToString</c> methods of the expression nodes, and other visitors
/// process the tree recursively.
/// On a 1 MB stack, the expression builder runs out of stack at a depth of about 1,000 in a Debug build,
/// so the limit leaves room for the caller's own stack frames.
/// </remarks>
private const int MaxDepth = 256;
/// <summary>
/// Gets the sub-expression parser, which serves as the base parser for expressions.
/// </summary>
/// <remarks>
/// Parsing stops with a fatal error if the expression tree is deeper than 256 levels.
/// Each operator, member access, call, indexer, and pair of parentheses adds a level,
/// so a chain such as <c>a + b + c</c> counts as deeply as the nested expression <c>((a + b) + c)</c>.
/// Parsing also stops with a fatal error if the thread's stack runs short. On a small stack, this can happen
/// before the limit is reached for deeply nested parentheses, arguments, indexers, and conditional expressions.
/// </remarks>
public static Parser<Expr> Subparser { get; } = CreateParser();
/// <summary>
/// Gets the main expression parser, which ensures that the entire input is consumed.
/// </summary>
public static Parser<Expr> Parser { get; } = Subparser.ThenIgnore(Eof);
[SuppressMessage("ReSharper", "InconsistentNaming")]
private static Parser<Expr> CreateParser()
{
var identifier_start =
Choice(
Character.Letter,
L('_')).Void();
var identifier_body =
Choice(
Character.LetterOrDigit,
L('_')).Void();
var identifier =
Seq(
identifier_start,
identifier_body.Many()
).Map(m => new Identifier(m.ToString())).As("identifier");
var keyword =
Seq(
OneOf("true", "false", "null"),
Not(identifier_body)
).Void();
var number_literal =
ConstantNumberParser
.NumericLiteral
.Do(Expr (v) => new Expr.Literal(v));
var string_literal =
Literal
.DoubleQuotedString
.Do(Expr (v) => new Expr.Literal(v));
var char_literal =
Literal
.QuotedCharacter
.Do(Expr (v) => new Expr.Literal(v));
var boolean_literal =
OneOf("true", "false")
.ThenIgnore(Not(identifier_body))
.Do(Expr (v) => new Expr.Literal(v == "true"));
var null_literal =
L("null")
.ThenIgnore(Not(identifier_body))
.Do(Expr (_) => new Expr.Literal(null));
var prefix_operator =
OneOf("+-!~")
.ThenIgnore(S)
.Do(v => new Identifier(v.ToString()));
var expression =
Deferred<Expr>();
var literal_expression =
Choice(
number_literal,
string_literal,
char_literal,
boolean_literal,
null_literal
).ThenIgnore(S);
var identifier_expression =
identifier
.Between(Not(keyword), S)
.Do(Expr (v) => new Expr.Reference(v));
var parenthesis_expression =
expression
.Between(
Seq(L('('), S),
Seq(L(')'), S))
.Do(Expr (e) => new Expr.Parenthesized(e));
var primary_expression =
Choice(
literal_expression,
identifier_expression,
parenthesis_expression);
var expression_list =
expression
.Separated(
Seq(L(','), S)).As("expression_list");
var call_expression =
expression_list
.Between(
Seq(L('('), S),
Seq(L(')'), S)).Do(list => (list, indexer: false));
var indexer_expression =
expression_list
.Between(
Seq(L('['), S),
Seq(L(']'), S)).Do(list => (list, indexer: true));
var call_or_indexer_expression =
Seq(
primary_expression,
Choice(
call_expression,
indexer_expression
).Many()
).Do((expr, list) =>
{
foreach (var (parameters, indexer) in list)
expr = indexer
? new Expr.Indexer(expr, parameters)
: new Expr.Call(expr, parameters);
//
// HijackMemberExpression processes this chain recursively during parsing,
// so check the depth before the chain reaches it.
//
return EnsureDepth(expr);
});
var member_expression =
call_or_indexer_expression.Fold(Seq(L('.'), S, Not(keyword)), (lhs, rhs, _) =>
{
return HijackMemberExpression(lhs, rhs);
static Expr HijackMemberExpression(Expr result, Expr value)
{
return value switch
{
Expr.Call call => new Expr.Call(HijackMemberExpression(result, call.Expression), call.Parameters),
Expr.Indexer indexer => new Expr.Indexer(HijackMemberExpression(result, indexer.Expression), indexer.Parameters),
_ => new Expr.MemberAccess(result, value)
};
}
});
//
// The conversion operator is ':>' rather than ':', which would conflict with the conditional
// operator: in 'flag ? 0 : count', ': count' would be taken for a conversion to the type 'count'.
//
var cast_expression =
Seq(
member_expression,
identifier.Between(
Seq(L(":>"), S),
S).Many()
).Do((expr, ids) =>
{
foreach (var id in ids)
expr = new Expr.Unary(id, UnaryType.Convert, expr);
return expr;
});
var prefix_expression =
Seq(
prefix_operator.Many(),
cast_expression
).Do((operators, expr) =>
{
for (var i = operators.Count - 1; i >= 0; i--)
{
var type = operators[i].Name[0] switch
{
'+' => UnaryType.UnaryPlus,
'!' => UnaryType.Not,
'~' => UnaryType.OnesComplement,
_ => UnaryType.Negate
};
expr = new Expr.Unary(operators[i], type, expr);
}
return expr;
});
var mul_expression =
prefix_expression.Fold(
OneOf("*/%").ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var add_expression =
mul_expression.Fold(
OneOf("+-").ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var shift_expression =
add_expression.Fold(
OneOf(["<<", ">>", ">>>"]).ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var relational_expression =
shift_expression.Fold(
OneOf(["<", ">", "<=", ">="]).ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var equality_expression =
relational_expression.Fold(
OneOf("==", "!=").ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var bitwise_and_expression =
equality_expression.Fold(
L('&').ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var bitwise_xor_expression =
bitwise_and_expression.Fold(
L('^').ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var bitwise_or_expression =
bitwise_xor_expression.Fold(
L('|').ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var logical_and_expression =
bitwise_or_expression.Fold(
L("&&").ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var logical_or_expression =
logical_and_expression.Fold(
L("||").ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var null_coalesce_expression =
logical_or_expression.FoldR(
L("??").ThenIgnore(S).Do(CreateIdentifier),
(lhs, rhs, op) => new Expr.Binary(op, lhs, rhs));
var conditional_expression =
Seq(
null_coalesce_expression,
Seq(
L('?'), S,
expression,
L(':').Or(Fail<char>("Expected ':'")), S,
expression.Or(Fail<Expr>("Expected expression"))
).Optional()
).Do(Expr (Expr condition, OptionalValue<(char, Unit, Expr @true, char, Unit, Expr @false)> optional) =>
optional.HasValue
? new Expr.Conditional(condition, optional.Value.@true, optional.Value.@false)
: condition);
//
// Every recursion of the grammar (parentheses, arguments, indexers, and both branches
// of a conditional expression) re-enters it through this parser, so a single check
// protects the parser from a stack overflow on deeply nested input.
//
expression.Parser =
new StackGuardParser<Expr>(conditional_expression);
return S.Then(expression).Do(EnsureDepth);
}
/// <summary>
/// Stops parsing with a fatal error if the specified expression tree is deeper than <see cref="MaxDepth"/>.
/// </summary>
/// <param name="expr">The expression tree to check.</param>
/// <returns>
/// The specified expression tree.
/// </returns>
private static Expr EnsureDepth(Expr expr)
{
if (expr.Depth > MaxDepth)
FatalError($"The expression exceeds the maximum depth of {MaxDepth}.");
return expr;
}
private static Identifier CreateIdentifier(string v) =>
new Identifier(v);
private static Identifier CreateIdentifier(char v) =>
new Identifier(v.ToString());
}