forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptLanguage.Code.cs
More file actions
575 lines (471 loc) · 23 KB
/
ScriptLanguage.Code.cs
File metadata and controls
575 lines (471 loc) · 23 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Extensions;
using ServiceStack.Text;
namespace ServiceStack.Script
{
/// <summary>
/// Inverse of the #Script Language Template Syntax where each line is a statement
/// i.e. in contrast to #Script's default where text contains embedded template expressions {{ ... }}
/// </summary>
public sealed class ScriptCode : ScriptLanguage
{
private ScriptCode() {} // force usage of singleton
public static readonly ScriptLanguage Language = new ScriptCode();
public override string Name => "code";
public override List<PageFragment> Parse(ScriptContext context, ReadOnlyMemory<char> body, ReadOnlyMemory<char> modifiers)
{
var quiet = false;
if (!modifiers.IsEmpty)
{
quiet = modifiers.EqualsOrdinal("q") || modifiers.EqualsOrdinal("quiet") || modifiers.EqualsOrdinal("mute");
if (!quiet)
throw new NotSupportedException($"Unknown modifier '{modifiers.ToString()}', expected 'code|q', 'code|quiet' or 'code|mute'");
}
var statements = context.ParseCodeStatements(body);
return new List<PageFragment> {
new PageJsBlockStatementFragment(new JsBlockStatement(statements)) {
Quiet = quiet,
},
};
}
public override async Task<bool> WritePageFragmentAsync(ScriptScopeContext scope, PageFragment fragment, CancellationToken token)
{
var page = scope.PageResult;
if (fragment is PageJsBlockStatementFragment blockFragment)
{
var blockStatements = blockFragment.Block.Statements;
if (blockFragment.Quiet && scope.OutputStream != Stream.Null)
scope = scope.ScopeWithStream(Stream.Null);
await page.WriteStatementsAsync(scope, blockStatements, token).ConfigAwait();
return true;
}
return false;
}
public override async Task<bool> WriteStatementAsync(ScriptScopeContext scope, JsStatement statement, CancellationToken token)
{
var page = scope.PageResult;
if (statement is JsExpressionStatement exprStatement)
{
var value = exprStatement.Expression.Evaluate(scope);
if (value != null && !ReferenceEquals(value, JsNull.Value) && value != StopExecution.Value && value != IgnoreResult.Value)
{
var strValue = page.Format.EncodeValue(value);
if (!string.IsNullOrEmpty(strValue))
{
var bytes = strValue.ToUtf8Bytes();
await scope.OutputStream.WriteAsync(bytes, token).ConfigAwait();
}
await scope.OutputStream.WriteAsync(JsTokenUtils.NewLineUtf8, token).ConfigAwait();
}
}
else if (statement is JsFilterExpressionStatement filterStatement)
{
await page.WritePageFragmentAsync(scope, filterStatement.FilterExpression, token).ConfigAwait();
if (!page.Context.RemoveNewLineAfterFiltersNamed.Contains(filterStatement.FilterExpression.LastFilterName))
{
await scope.OutputStream.WriteAsync(JsTokenUtils.NewLineUtf8, token).ConfigAwait();
}
}
else if (statement is JsBlockStatement blockStatement)
{
await page.WriteStatementsAsync(scope, blockStatement.Statements, token).ConfigAwait();
}
else if (statement is JsPageBlockFragmentStatement pageFragmentStatement)
{
await page.WritePageFragmentAsync(scope, pageFragmentStatement.Block, token).ConfigAwait();
}
else return false;
return true;
}
}
public static class ScriptCodeUtils
{
[Obsolete("Use CodeSharpPage")]
public static SharpPage CodeBlock(this ScriptContext context, string code) => context.CodeSharpPage(code);
public static SharpPage CodeSharpPage(this ScriptContext context, string code)
=> context.Pages.OneTimePage(code, context.PageFormats[0].Extension,p => p.ScriptLanguage = ScriptCode.Language);
private static void AssertCode(this ScriptContext context)
{
if (!context.ScriptLanguages.Contains(ScriptCode.Language))
throw new NotSupportedException($"ScriptCode.Language is not registered in {context.GetType().Name}.{nameof(context.ScriptLanguages)}");
}
private static PageResult GetCodePageResult(ScriptContext context, string code, Dictionary<string, object> args)
{
context.AssertCode();
PageResult pageResult = null;
try
{
var page = context.CodeSharpPage(code);
pageResult = new PageResult(page);
args.Each((x, y) => pageResult.Args[x] = y);
return pageResult;
}
catch (Exception e)
{
if (ScriptContextUtils.ShouldRethrow(e))
throw;
throw ScriptContextUtils.HandleException(e, pageResult ?? new PageResult(context.EmptyPage));
}
}
public static string RenderCode(this ScriptContext context, string code, Dictionary<string, object> args=null)
{
var pageResult = GetCodePageResult(context, code, args);
return pageResult.RenderScript();
}
public static async Task<string> RenderCodeAsync(this ScriptContext context, string code, Dictionary<string, object> args=null)
{
var pageResult = GetCodePageResult(context, code, args);
return await pageResult.RenderScriptAsync().ConfigAwait();
}
public static JsBlockStatement ParseCode(this ScriptContext context, string code) =>
context.ParseCode(code.AsMemory());
public static JsBlockStatement ParseCode(this ScriptContext context, ReadOnlyMemory<char> code)
{
var statements = context.ParseCodeStatements(code);
return new JsBlockStatement(statements);
}
public static string EnsureReturn(string code)
{
if (code == null)
throw new ArgumentNullException(nameof(code));
// if code doesn't contain a return, wrap and return the expression
if (code.IndexOf(ScriptConstants.Return,StringComparison.Ordinal) == -1)
code = ScriptConstants.Return + "(" + code + ")";
return code;
}
public static T EvaluateCode<T>(this ScriptContext context, string code, Dictionary<string, object> args = null) =>
context.EvaluateCode(code, args).ConvertTo<T>();
public static object EvaluateCode(this ScriptContext context, string code, Dictionary<string, object> args=null)
{
var pageResult = GetCodePageResult(context, code, args);
if (!pageResult.EvaluateResult(out var returnValue))
throw new NotSupportedException(ScriptContextUtils.ErrorNoReturn);
return ScriptLanguage.UnwrapValue(returnValue);
}
public static async Task<T> EvaluateCodeAsync<T>(this ScriptContext context, string code, Dictionary<string, object> args = null) =>
(await context.EvaluateCodeAsync(code, args).ConfigAwait()).ConvertTo<T>();
public static async Task<object> EvaluateCodeAsync(this ScriptContext context, string code, Dictionary<string, object> args=null)
{
var pageResult = GetCodePageResult(context, code, args);
var ret = await pageResult.EvaluateResultAsync().ConfigAwait();
if (!ret.Item1)
throw new NotSupportedException(ScriptContextUtils.ErrorNoReturn);
return ScriptLanguage.UnwrapValue(ret.Item2);
}
internal static JsStatement[] ParseCodeStatements(this ScriptContext context, ReadOnlyMemory<char> code)
{
var to = new List<JsStatement>();
int startExpressionPos = -1;
var cursorPos = 0;
while (code.TryReadLine(out var line, ref cursorPos))
{
var lineLength = line.Length;
line = line.TrimStart();
var leftIndent = lineLength - line.Length;
line = line.TrimEnd();
var rightIndent = lineLength - leftIndent - line.Length;
if (line.IsEmpty)
continue;
var firstChar = line.Span[0];
// single-line comment
if (firstChar == '*')
{
if (line.EndsWith("*"))
continue;
}
// multi-line comment
if (line.StartsWith("{{*"))
{
var endPos = code.IndexOf("*}}", cursorPos - lineLength);
if (endPos == -1)
throw new SyntaxErrorException($"Unterminated multi-line comment, near {line.DebugLiteral()}");
cursorPos = endPos + 3; // "*}}".Length
continue;
}
// template block statement
if (firstChar == '{' && line.Span.SafeCharEquals(1, '{') && line.Span.SafeCharEquals(2, '#'))
{
var fromLineStart = code.ToLineStart(cursorPos, lineLength).AdvancePastWhitespace();
var literal = fromLineStart.Slice(3);
literal = literal.ParseTemplateScriptBlock(context, out var blockFragment);
blockFragment.OriginalText = fromLineStart.Slice(0, fromLineStart.Length - literal.Length);
to.Add(new JsPageBlockFragmentStatement(blockFragment));
cursorPos = code.Length - literal.Length;
continue;
}
// code block statement
if (firstChar == '#')
{
var fromLineStart = code.ToLineStart(cursorPos, lineLength).AdvancePastWhitespace();
var literal = fromLineStart.Slice(1);
literal = literal.ParseCodeScriptBlock(context, out var blockFragment);
to.Add(new JsPageBlockFragmentStatement(blockFragment));
blockFragment.OriginalText = fromLineStart.Slice(0, fromLineStart.Length - literal.Length);
cursorPos = code.Length - literal.Length;
continue;
}
const int delim = 2; // '}}'.length
// multi-line expression
if (startExpressionPos >= 0)
{
// multi-line end
if (line.EndsWith("}}"))
{
if (code.Span.SafeCharEquals(startExpressionPos, '*'))
{
if (!line.EndsWith("*}}")) // not a closing block comment, continue
continue;
// ignore multi-line comment
}
else
{
var CRLF = code.Span.SafeCharEquals(cursorPos - 2, '\r') ? 2 : 1;
var exprStr = code.Slice(startExpressionPos, cursorPos - startExpressionPos - rightIndent - delim - CRLF).Trim();
var afterExpr = exprStr.Span.ParseExpression(out var expr, out var filters);
to.AddExpression(exprStr, expr, filters);
}
startExpressionPos = -1;
}
continue;
}
if (firstChar == '{' && line.Span.SafeCharEquals(1, '{'))
{
// single-line {{ expr }}
if (line.EndsWith("}}"))
{
var exprStr = code.Slice(cursorPos - lineLength + leftIndent + delim);
exprStr = exprStr.Slice(0, exprStr.IndexOf("}}")).Trim();
var afterExpr = exprStr.Span.ParseExpression(out var expr, out var filters);
to.AddExpression(exprStr, expr, filters);
continue;
}
// multi-line start
var CRLF = code.Span.SafeCharEquals(cursorPos - 2, '\r') ? 2 : 1;
startExpressionPos = cursorPos - lineLength - CRLF + leftIndent + delim;
continue;
}
else
{
// treat line as an expression statement
var afterExpr = line.Span.ParseExpression(out var expr, out var filters);
afterExpr = afterExpr.AdvancePastWhitespace();
if (!afterExpr.IsEmpty)
throw new SyntaxErrorException($"Unexpected syntax after expression: {afterExpr.ToString()}, near {line.DebugLiteral()}");
to.AddExpression(line, expr, filters);
}
}
return to.ToArray();
}
// #if ...
// ^
public static ReadOnlyMemory<char> ParseCodeScriptBlock(this ReadOnlyMemory<char> literal, ScriptContext context,
out PageBlockFragment blockFragment)
{
literal = literal.ParseVarName(out var blockNameSpan);
var endArgumentPos = literal.IndexOf('\n');
var argument = literal.Slice(0, endArgumentPos).Trim();
literal = literal.Slice(endArgumentPos + 1);
var blockName = blockNameSpan.ToString();
var language = context.ParseAsLanguage.TryGetValue(blockName, out var lang)
? lang
: ScriptCode.Language;
if (language.Name == ScriptVerbatim.Language.Name)
{
literal = literal.ParseCodeBody(blockNameSpan, out var body);
body = body.ChopNewLine();
blockFragment = language.ParseVerbatimBlock(blockName, argument, body);
return literal;
}
literal = literal.ParseCodeBody(blockNameSpan, out var bodyText);
var bodyFragments = language.Parse(context, bodyText);
var elseBlocks = new List<PageElseBlock>();
literal = literal.AdvancePastWhitespace();
while (literal.StartsWith("else"))
{
literal = literal.ParseCodeElseBlock(blockNameSpan, out var elseArgument, out var elseBody);
var elseBlock = new PageElseBlock(elseArgument, language.Parse(context, elseBody));
elseBlocks.Add(elseBlock);
literal = literal.AdvancePastWhitespace();
}
blockFragment = new PageBlockFragment(blockName, argument, bodyFragments, elseBlocks);
return literal;
}
// cursorPos is after CRLF except at end where its at last char
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ReadOnlyMemory<char> FromStartToPreviousLine(this ReadOnlyMemory<char> literal, int cursorPos, int lineLength)
{
var ret = literal.Slice(0, cursorPos - lineLength);
while (!ret.Span.SafeCharEquals(ret.Length - 1, '\n'))
{
ret = ret.Slice(0, ret.Length - 1);
if (ret.Length == 0) // no previous line, so return empty string
return default;
}
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ReadOnlyMemory<char> ToLineStart(this ReadOnlyMemory<char> literal, int cursorPos, int lineLength)
{
var CLRF = literal.Span.SafeCharEquals(cursorPos - 2, '\r');
var ret = literal.Slice(cursorPos - lineLength -
(cursorPos == literal.Length ? 0 : 1) -
(CLRF ? 1 : 0));
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ReadOnlyMemory<char> ToLineStart(this ReadOnlyMemory<char> literal, int cursorPos, int lineLength, int statementPos)
{
var ret = literal.Slice(statementPos, cursorPos - statementPos - lineLength);
while (!ret.Span.SafeCharEquals(ret.Length - 1, '\n'))
{
ret = ret.Slice(0, ret.Length - 1);
if (ret.Length == 0) // no previous line, so return empty string
return default;
}
return ret;
}
// #block arg\n
// ^
// else
// /block
internal static ReadOnlyMemory<char> ParseCodeBody(this ReadOnlyMemory<char> literal, ReadOnlyMemory<char> blockName, out ReadOnlyMemory<char> body)
{
var inStatements = 0;
var cursorPos = 0;
while (literal.TryReadLine(out var line, ref cursorPos))
{
var lineLength = line.Length;
line = line.Trim();
if (line.IsEmpty)
continue;
var c = line.Span[0];
if (c == '#')
{
inStatements++;
continue;
}
if (c == '/')
{
if (inStatements == 0)
{
line.Slice(1).ParseVarName(out var name);
if (name.EqualsOrdinal(blockName))
{
body = literal.FromStartToPreviousLine(cursorPos, lineLength);
var ret = literal.Slice(cursorPos);
return ret;
}
}
inStatements--;
}
else if (line.StartsWith("else"))
{
if (inStatements == 0)
{
body = literal.FromStartToPreviousLine(cursorPos, lineLength);
var ret = literal.ToLineStart(cursorPos, lineLength);
return ret;
}
}
}
throw new SyntaxErrorException($"End block for '{blockName.ToString()}' not found.");
}
// else if a=b
// ^
// else
// ^
// /block
internal static ReadOnlyMemory<char> ParseCodeElseBlock(this ReadOnlyMemory<char> literal, ReadOnlyMemory<char> blockName,
out ReadOnlyMemory<char> elseArgument, out ReadOnlyMemory<char> elseBody)
{
var inStatements = 0;
var statementPos = -1;
elseBody = default;
elseArgument = default;
var cursorPos = 0;
while (literal.TryReadLine(out var line, ref cursorPos))
{
var lineLength = line.Length;
line = line.Trim();
if (line.IsEmpty)
continue;
var c = line.Span[0];
if (c == '#')
{
inStatements++;
}
else if (c == '/')
{
if (inStatements == 0)
{
line.Slice(1).ParseVarName(out var name);
if (name.EqualsOrdinal(blockName))
{
elseBody = literal.ToLineStart(cursorPos, lineLength, statementPos);
elseBody = elseBody.Trim();
var ret = literal.Slice(cursorPos);
return ret;
}
}
inStatements--;
}
else if (line.StartsWith("else"))
{
if (inStatements == 0)
{
if (statementPos >= 0)
{
elseBody = literal.Slice(statementPos, (cursorPos - lineLength) - statementPos).Trim();
var ret = literal.Slice(cursorPos - lineLength);
return ret;
}
elseArgument = line.Slice(4).Trim();
statementPos = cursorPos;
}
}
}
throw new SyntaxErrorException($"End 'else' statement not found.");
}
internal static ReadOnlySpan<char> ParseExpression(this ReadOnlySpan<char> literal, out JsToken expr, out List<JsCallExpression> filters)
{
literal = literal.ParseJsExpression(out expr, filterExpression: true);
filters = null;
literal = literal.AdvancePastWhitespace();
if (literal.FirstCharEquals(ScriptTemplateUtils.FilterSep))
{
filters = new List<JsCallExpression>();
literal = literal.AdvancePastPipeOperator();
while (true)
{
literal = literal.ParseJsCallExpression(out var filter, filterExpression: true);
filters.Add(filter);
literal = literal.AdvancePastWhitespace();
if (literal.IsNullOrEmpty())
return literal;
if (!literal.FirstCharEquals(ScriptTemplateUtils.FilterSep))
throw new SyntaxErrorException($"Expected filter separator '|' but was {literal.DebugFirstChar()}");
literal = literal.AdvancePastPipeOperator();
}
}
else if (!literal.AdvancePastWhitespace().IsNullOrEmpty())
{
throw new SyntaxErrorException($"Unexpected syntax '{literal.ToString()}', Expected pipeline operator '|>'");
}
return literal;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddExpression(this List<JsStatement> ret, ReadOnlyMemory<char> originalText,
JsToken expr, List<JsCallExpression> filters)
{
if (filters == null)
ret.Add(new JsExpressionStatement(expr));
else
ret.Add(new JsFilterExpressionStatement(originalText, expr, filters));
}
}
}