forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsExpressionUtils.cs
More file actions
312 lines (249 loc) · 11.1 KB
/
JsExpressionUtils.cs
File metadata and controls
312 lines (249 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public static class JsExpressionUtils
{
public static object GetJsExpressionAndEvaluate(this ReadOnlyMemory<char> expr, ScriptScopeContext scope, Action ifNone = null)
{
if (expr.IsNullOrEmpty())
{
ifNone?.Invoke();
return null;
}
var token = expr.GetCachedJsExpression(scope);
if (token == null)
{
ifNone?.Invoke();
return null;
}
var result = token.Evaluate(scope);
return result;
}
public static async Task<object> GetJsExpressionAndEvaluateAsync(this ReadOnlyMemory<char> expr, ScriptScopeContext scope, Action ifNone = null)
{
if (expr.IsNullOrEmpty())
{
ifNone?.Invoke();
return TypeConstants.EmptyTask;
}
var token = expr.GetCachedJsExpression(scope);
if (token == null)
{
ifNone?.Invoke();
return TypeConstants.EmptyTask;
}
var ret = await token.EvaluateAsync(scope);
return ret == JsNull.Value
? null
: ret;
}
public static bool GetJsExpressionAndEvaluateToBool(this ReadOnlyMemory<char> expr, ScriptScopeContext scope, Action ifNone = null)
{
if (expr.IsNullOrEmpty())
{
ifNone?.Invoke();
return false;
}
var token = expr.GetCachedJsExpression(scope);
if (token == null)
{
ifNone?.Invoke();
return false;
}
var result = token.EvaluateToBool(scope);
return result;
}
public static Task<bool> GetJsExpressionAndEvaluateToBoolAsync(this ReadOnlyMemory<char> expr, ScriptScopeContext scope, Action ifNone = null)
{
if (expr.IsNullOrEmpty())
{
ifNone?.Invoke();
return TypeConstants.FalseTask;
}
var token = expr.GetCachedJsExpression(scope);
if (token == null)
{
ifNone?.Invoke();
return TypeConstants.FalseTask;
}
return token.EvaluateToBoolAsync(scope);
}
public static JsToken GetCachedJsExpression(this ReadOnlyMemory<char> expr, ScriptScopeContext scope)
{
if (expr.IsEmpty)
return null;
if (scope.Context.JsTokenCache.TryGetValue(expr, out var token))
return token;
expr.Span.ParseJsExpression(out token);
if (token != null)
scope.Context.JsTokenCache[expr] = token;
return token;
}
public static JsToken GetCachedJsExpression(this string expr, ScriptScopeContext scope) =>
GetCachedJsExpression(expr.AsMemory(), scope);
public static ReadOnlySpan<char> ParseJsExpression(this string literal, out JsToken token) =>
literal.AsSpan().ParseJsExpression(out token);
public static ReadOnlySpan<char> ParseJsExpression(this ReadOnlySpan<char> literal, out JsToken token) =>
literal.ParseJsExpression(out token, filterExpression:false);
public static ReadOnlySpan<char> ParseJsExpression(this ReadOnlyMemory<char> literal, out JsToken token) =>
literal.Span.ParseJsExpression(out token, filterExpression:false);
private const char ConditionalExpressionTestChar = '?';
public static ReadOnlySpan<char> ParseJsExpression(this ReadOnlySpan<char> literal, out JsToken token, bool filterExpression)
{
var peekLiteral = literal.ParseJsToken(out var node, filterExpression:filterExpression);
peekLiteral = peekLiteral.AdvancePastWhitespace();
var peekChar = peekLiteral.SafeGetChar(0);
if (literal.IsNullOrEmpty() || peekChar.IsExpressionTerminatorChar())
{
token = node;
return peekLiteral;
}
if (peekChar == ConditionalExpressionTestChar &&
peekLiteral.SafeGetChar(1) != ConditionalExpressionTestChar) // not ??
{
literal = peekLiteral.ParseJsConditionalExpression(node, out var expression);
token = expression;
return literal;
}
if (node is JsIdentifier identifier && peekLiteral.StartsWith("=>"))
{
literal = peekLiteral.ParseArrowExpressionBody(new[]{ identifier }, out var arrowExpr);
token = arrowExpr;
return literal;
}
peekLiteral = peekLiteral.AdvancePastWhitespace();
if (!peekLiteral.IsNullOrEmpty())
{
if (filterExpression && peekLiteral.Length > 2)
{
var char1 = peekLiteral[0];
var char2 = peekLiteral[1];
if ((char1 == '|' && char2 != '|') || (char1 == '}' && char2 == '}'))
{
token = node;
return peekLiteral;
}
}
}
peekLiteral = peekLiteral.ParseJsBinaryOperator(out var op);
if (op != null)
{
literal = literal.ParseBinaryExpression(out var expr, filterExpression);
token = expr;
literal = literal.AdvancePastWhitespace();
if (literal.FirstCharEquals(ConditionalExpressionTestChar))
{
literal = literal.ParseJsConditionalExpression(expr, out var conditionalExpr);
token = conditionalExpr;
return literal;
}
return literal;
}
literal = peekLiteral.ParseJsMemberExpression(ref node, filterExpression);
token = node;
return literal;
}
private static ReadOnlySpan<char> ParseJsConditionalExpression(this ReadOnlySpan<char> literal, JsToken test, out JsConditionalExpression expression)
{
literal = literal.Advance(1);
literal = literal.ParseJsExpression(out var consequent);
literal = literal.AdvancePastWhitespace();
if (!literal.FirstCharEquals(':'))
throw new SyntaxErrorException($"Expected Conditional ':' but was {literal.DebugFirstChar()}");
literal = literal.Advance(1);
literal = literal.ParseJsExpression(out var alternate);
expression = new JsConditionalExpression(test, consequent, alternate);
return literal;
}
public static ReadOnlySpan<char> ParseBinaryExpression(this ReadOnlySpan<char> literal, out JsExpression expr, bool filterExpression)
{
literal = literal.AdvancePastWhitespace();
literal = literal.ParseJsToken(out var lhs, filterExpression:filterExpression);
if (literal.IsNullOrEmpty())
{
expr = lhs is JsExpression jsExpr
? jsExpr
: throw new SyntaxErrorException($"Expected Expression but was {lhs.DebugToken()}");
}
else
{
literal = literal.ParseJsBinaryOperator(out var op);
if (op == null)
throw new SyntaxErrorException($"Expected binary operator near: {literal.DebugLiteral()}");
var prec = JsTokenUtils.GetBinaryPrecedence(op.Token);
if (prec > 0)
{
literal = literal.ParseJsToken(out JsToken rhs, filterExpression:filterExpression);
var stack = new Stack<JsToken>();
stack.Push(lhs);
stack.Push(op);
stack.Push(rhs);
var precedences = new List<int> { prec };
while (true)
{
literal = literal.AdvancePastWhitespace();
if (filterExpression && literal.Length > 2 && (literal[0] == '|' && literal[1] != '|'))
{
break;
}
prec = literal.GetNextBinaryPrecedence();
if (prec == 0)
break;
while ((stack.Count > 2) && prec <= precedences[precedences.Count - 1])
{
rhs = stack.Pop();
var operand = (JsBinaryOperator)stack.Pop();
precedences.RemoveAt(precedences.Count - 1);
lhs = stack.Pop();
stack.Push(CreateJsExpression(lhs, operand, rhs));
}
literal = literal.ParseJsBinaryOperator(out op);
if (literal.IsNullOrEmpty())
throw new SyntaxErrorException($"Expected expression near: '{literal.DebugLiteral()}'");
literal = literal.ParseJsToken(out var token, filterExpression:filterExpression);
stack.Push(op);
stack.Push(token);
precedences.Add(prec);
}
var i = stack.Count - 1;
var ret = stack.Pop();
while (stack.Count > 0)
{
op = (JsBinaryOperator) stack.Pop();
lhs = stack.Pop();
ret = CreateJsExpression(lhs, op, ret);
}
expr = (JsExpression) ret;
}
else
{
expr = lhs is JsExpression jsExpr
? jsExpr
: throw new SyntaxErrorException($"Expected Expression but was {lhs.DebugToken()}");
}
}
return literal;
}
public static JsExpression CreateJsExpression(JsToken lhs, JsBinaryOperator op, JsToken rhs)
{
if (op is JsAnd opAnd)
return new JsLogicalExpression(lhs, opAnd, rhs);
if (op is JsOr opOr)
return new JsLogicalExpression(lhs, opOr, rhs);
return new JsBinaryExpression(lhs, op, rhs);
}
static int GetNextBinaryPrecedence(this ReadOnlySpan<char> literal)
{
if (!literal.IsNullOrEmpty() && !literal[0].IsExpressionTerminatorChar())
{
literal.ParseJsBinaryOperator(out var binaryOp);
if (binaryOp != null)
return JsTokenUtils.GetBinaryPrecedence(binaryOp.Token);
}
return 0;
}
}
}