-
Notifications
You must be signed in to change notification settings - Fork 324
/
Engine.cs
555 lines (470 loc) · 24.4 KB
/
Engine.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.PowerFx.Core;
using Microsoft.PowerFx.Core.Annotations;
using Microsoft.PowerFx.Core.App.Controls;
using Microsoft.PowerFx.Core.Binding;
using Microsoft.PowerFx.Core.Entities.QueryOptions;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.Glue;
using Microsoft.PowerFx.Core.Texl;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Intellisense;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
namespace Microsoft.PowerFx
{
/// <summary>
/// Expose binding logic for Power Fx.
/// Derive from this to provide evaluation abilities.
/// </summary>
public class Engine
{
/// <summary>
/// Configuration symbols for this Power Fx engine.
/// </summary>
public PowerFxConfig Config { get; }
// Volatile means it can be set by multiple threads.
// But all threads will produce the same value, so it's ok.
private static volatile string _assemblyVersion;
/// <summary>
/// For diagnostics, get the assembly version of Power Fx Engine.
/// </summary>
public static string AssemblyVersion
{
get
{
if (_assemblyVersion == null)
{
var fxAttr = typeof(Engine).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
var fullVerStr = fxAttr.InformationalVersion;
var parts = fullVerStr.Split('+');
_assemblyVersion = parts[0];
}
return _assemblyVersion;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Engine"/> class.
/// </summary>
public Engine()
: this(new PowerFxConfig())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Engine"/> class.
/// </summary>
/// <param name="powerFxConfig"></param>
public Engine(PowerFxConfig powerFxConfig)
{
Config = powerFxConfig ?? throw new ArgumentNullException(nameof(powerFxConfig));
}
// All functions that powerfx core knows about.
// Derived engines may only support a subset of these builtins,
// and they may add their own custom ones.
private static readonly ReadOnlySymbolTable _allBuiltinCoreFunctions = ReadOnlySymbolTable.NewDefault(BuiltinFunctionsCore._library);
/// <summary>
/// Builtin functions supported by this engine.
/// </summary>
public ReadOnlySymbolTable SupportedFunctions { get; protected internal set; } = _allBuiltinCoreFunctions;
/// <summary>
/// Builtin Types supported by this engine.
/// </summary>
public ReadOnlySymbolTable PrimitiveTypes { get; protected internal set; } = ReadOnlySymbolTable.PrimitiveTypesTableInstance;
// By default, we pull the core functions.
// These can be overridden.
internal TexlFunctionSet Functions => CreateResolverInternal().Functions;
/// <summary>
/// List of transforms to apply to an IR.
/// </summary>
internal readonly List<Core.IR.IRTransform> IRTransformList = new List<Core.IR.IRTransform>();
/// <summary>
/// List of error processor that will add host specific custom errors at the end of the check.
/// </summary>
private readonly IList<IPostCheckErrorHandler> _postCheckErrorHandlers = new List<IPostCheckErrorHandler>();
public IList<IPostCheckErrorHandler> PostCheckErrorHandlers => _postCheckErrorHandlers;
/// <summary>
/// Get all functions from the config and symbol tables.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public IEnumerable<FunctionInfo> FunctionInfos => Functions.Functions.Select(f => new FunctionInfo(f));
#pragma warning restore CS0618 // Type or member is obsolete
/// <summary>
/// List all functions (both builtin and custom) registered with this evaluator.
/// </summary>
#pragma warning disable CA1024 // Use properties where appropriate
public IEnumerable<string> GetAllFunctionNames()
#pragma warning restore CA1024 // Use properties where appropriate
{
return Functions.FunctionNames;
}
internal IEnumerable<TexlFunction> GetFunctionsByName(string name) => Functions.WithName(name);
internal int FunctionCount => Functions.Count();
// Additional symbols for the engine.
// A derived engine can replace this completely to inject engine-specific virtuals.
// These symbols then feed into the resolver
protected ReadOnlySymbolTable EngineSymbols { get; set; }
/// <summary>
/// Create a resolver for use in binding. This is called from <see cref="Check(string, RecordType, ParserOptions)"/>.
/// Base classes can override this is there are additional symbols not in the config.
/// </summary>
[Obsolete("Use EngineSymbols instead.")]
private protected virtual INameResolver CreateResolver()
{
return null;
}
// Returns the INameResolver and the corresponding Symbol table.
private protected INameResolver CreateResolverInternal(ReadOnlySymbolTable localSymbols = null)
{
return CreateResolverInternal(out _, localSymbols);
}
private protected INameResolver CreateResolverInternal(out ReadOnlySymbolTable symbols, ReadOnlySymbolTable localSymbols = null)
{
// For backwards compat with Prose.
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
var existing = CreateResolver();
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0612 // Type or member is obsolete
if (existing != null)
{
symbols = null;
return existing;
}
symbols = ReadOnlySymbolTable.Compose(localSymbols, GetCombinedEngineSymbols());
return symbols;
}
private readonly ComposedSymbolTableCache _functionListCache = new ComposedSymbolTableCache();
/// <summary>
/// Get a combined engine symbol table, including builtins and config.
/// </summary>
/// <returns></returns>
public ReadOnlySymbolTable GetCombinedEngineSymbols()
{
// Compose all the symbol tables most likely to have functions into a single
// symbol table and then cache that.
// That will cache unifying into a single TexlFunctionSet - which is the most expensive part.
var functionList = _functionListCache.GetComposedCached(SupportedFunctions, Config.SymbolTable);
var symbols = ReadOnlySymbolTable.Compose(EngineSymbols, functionList, PrimitiveTypes);
return symbols;
}
/// <summary>
/// Overriable method to be able to supply custom implementation of IExternalRuleScopeResolver.
/// Defaults to Null.
/// </summary>
/// <returns>Implementation of IExternalRuleScopeResolver.</returns>
// <para>IExternalRuleScopeResolver is used in Canvas App backend when calling TexlBinding.Run().
// There was no option to supply a custom implementation of this in Engine,
// so following the existing pattern in Engine,
// added this virtual function for the derived classes of Engine to override and supply custom implementation.</para>
private protected virtual IExternalRuleScopeResolver CreateExternalRuleScopeResolver()
{
return null;
}
private protected virtual IBinderGlue CreateBinderGlue()
{
return new Glue2DocumentBinderGlue();
}
public virtual ParserOptions GetDefaultParserOptionsCopy()
{
return new ParserOptions
{
Culture = null,
AllowsSideEffects = false,
MaxExpressionLength = Config.MaximumExpressionLength,
};
}
/// <summary>
/// Tokenize an expression to a sequence of <see cref="Token" />s.
/// </summary>
/// <param name="expressionText"></param>
/// <param name="culture"></param>
/// <returns></returns>
public IReadOnlyList<Token> Tokenize(string expressionText, CultureInfo culture = null)
=> TexlLexer.GetLocalizedInstance(culture).GetTokens(expressionText);
/// <summary>
/// Parse the expression without doing any binding.
/// </summary>
/// <param name="expressionText"></param>
/// <param name="options"></param>
/// <returns></returns>
public ParseResult Parse(string expressionText, ParserOptions options = null)
{
return Parse(expressionText, Config.Features, options ?? this.GetDefaultParserOptionsCopy());
}
/// <summary>
/// Parse the expression without doing any binding.
/// </summary>
public static ParseResult Parse(string expressionText, Features features = null, ParserOptions options = null)
{
if (expressionText == null)
{
throw new ArgumentNullException(nameof(expressionText));
}
options ??= new ParserOptions();
var result = options.Parse(expressionText, features ?? Features.None);
return result;
}
/// <summary>
/// Parse and Bind an expression.
/// </summary>
/// <param name="expressionText">the expression in plain text. </param>
/// <param name="parameterType">types of additional args to pass.</param>
/// <param name="options">parser options to use.</param>
/// <returns></returns>
public CheckResult Check(string expressionText, RecordType parameterType, ParserOptions options = null)
{
var check = new CheckResult(this)
.SetText(expressionText, options)
.SetBindingInfo(parameterType);
CheckWorker(check);
return check;
}
public CheckResult Check(ParseResult parse, RecordType parameterType = null)
{
var check = new CheckResult(this)
.SetText(parse)
.SetBindingInfo(parameterType);
CheckWorker(check);
return check;
}
public CheckResult Check(
string expressionText,
ParserOptions options = null,
ReadOnlySymbolTable symbolTable = null)
{
var check = new CheckResult(this)
.SetText(expressionText, options)
.SetBindingInfo(symbolTable);
CheckWorker(check);
return check;
}
// Apply a standard set of operations on the CheckResult.
// If callers want more granularity, they can create the CheckResult themselves.
private void CheckWorker(CheckResult check)
{
check.ApplyBindingInternal();
check.ApplyErrors();
check.ApplyDependencyAnalysis();
}
// Called after check result, can inject additional errors or constraints.
protected virtual IEnumerable<ExpressionError> PostCheck(CheckResult check)
{
var hostErrors = new List<ExpressionError>();
foreach (var postCheckErrorHandler in _postCheckErrorHandlers)
{
hostErrors.AddRange(postCheckErrorHandler.Process(check));
}
return hostErrors;
}
internal IEnumerable<ExpressionError> InvokePostCheck(CheckResult check)
{
return this.PostCheck(check);
}
// Setting rule sope which will get passed into Binder.
// Prefer to avoid this hook and use SymbolTables instead.
private protected virtual RecordType GetRuleScope()
{
return null;
}
internal bool TryGetRuleScope(out RecordType record)
{
record = this.GetRuleScope();
return record != null;
}
private BindingConfig GetDefaultBindingConfig()
{
var ruleScope = this.GetRuleScope();
bool useThisRecordForRuleScope = ruleScope != null;
var bindingConfig = BindingConfig.Default;
if (useThisRecordForRuleScope)
{
bindingConfig = new BindingConfig(bindingConfig.AllowsSideEffects, true);
}
return bindingConfig;
}
/// <summary>
/// Creates and returns binding config from the given parser options.
/// </summary>
/// <param name="options">Parser Options.</param>
/// <param name="ruleScope">Optional: Rule Scope. If not supplied, then rule scope from <see cref="GetRuleScope"/> would be used.</param>
/// <returns>Binding Config.</returns>
// Power Apps never set BindingConfig.UseThisRecordForRuleScope to true
// This virtual overload of GetDefaultBindingConfig allows us to supply custom binding config from PowerApps
// Default implementation is similar to how binding config is created in ComputeBinding
// Optional ruleScope is passed from ComputeBinding() so we don't have to call GetRuleScope twice
private protected virtual BindingConfig GetDefaultBindingConfig(ParserOptions options, RecordType ruleScope = null)
{
ruleScope ??= this.GetRuleScope();
// Canvas apps uses rule scope for lots of cases.
// But in general, we should only use rule scope for 'ThisRecord' binding.
// Anything else should be accomplished with SymbolTables.
bool useThisRecordForRuleScope = ruleScope != null;
return new BindingConfig(options.AllowsSideEffects, useThisRecordForRuleScope, options.NumberIsFloat);
}
// Called by CheckResult.ApplyBinding to compute the binding.
internal (TexlBinding, ReadOnlySymbolTable) ComputeBinding(CheckResult result)
{
var parse = result.ApplyParse();
ReadOnlySymbolTable symbolTable = result.Parameters;
// Ok to continue with binding even if there are parse errors.
// We can still use that for intellisense.
var resolver = CreateResolverInternal(out var combinedSymbols, symbolTable);
var externalRuleScopeResolver = CreateExternalRuleScopeResolver();
var glue = CreateBinderGlue();
var ruleScope = this.GetRuleScope();
var bindingConfig = GetDefaultBindingConfig(result.Parse.Options, ruleScope);
var binding = TexlBinding.Run(
glue,
externalRuleScopeResolver,
new DataSourceToQueryOptionsMap(),
parse.Root,
resolver,
bindingConfig,
false,
ruleScope?._type,
false,
null,
Config.Features);
return (binding, combinedSymbols);
}
/// <summary>
/// Optional hook to customize intellisense.
/// </summary>
/// <returns></returns>
private protected virtual IIntellisense CreateIntellisense()
{
return IntellisenseProvider.GetIntellisense(Config);
}
public IIntellisenseResult Suggest(string expression, RecordType parameterType, int cursorPosition)
{
var checkResult = Check(expression, parameterType);
return Suggest(checkResult, cursorPosition);
}
/// <summary>
/// Get intellisense from the formula, with parser options.
/// </summary>
public IIntellisenseResult Suggest(CheckResult checkResult, int cursorPosition)
{
return this.Suggest(checkResult, cursorPosition, null);
}
public IIntellisenseResult Suggest(CheckResult checkResult, int cursorPosition, IServiceProvider services)
{
// Note that for completions, we just need binding,
// but we don't need errors or dependency info.
var binding = checkResult.ApplyBindingInternal();
var formula = checkResult.GetParseFormula();
var expression = formula.Script;
// CheckResult has the binding, which has already captured both the INameResolver and any row scope parameters.
// So these both become available to intellisense.
var context = new IntellisenseContext(expression, cursorPosition, checkResult.ExpectedReturnType)
{
Services = services
};
var intellisense = this.CreateIntellisense();
var suggestions = intellisense.Suggest(context, binding, formula);
return suggestions;
}
/// <summary>
/// Creates a renamer instance for updating a field reference from <paramref name="parameters"/> in expressions.
/// </summary>
/// <param name="parameters">Type of parameters for formula. The fields in the parameter record can
/// be acecssed as top-level identifiers in the formula. Must be the names from before any rename operation is applied.</param>
/// <param name="pathToRename">Path to the field to rename.</param>
/// <param name="updatedName">New name. Replaces the last segment of <paramref name="pathToRename"/>.</param>
/// <param name="culture">Culture.</param>
/// <returns></returns>
public RenameDriver CreateFieldRenamer(RecordType parameters, DPath pathToRename, DName updatedName, CultureInfo culture)
{
return CreateFieldRenamer(parameters, pathToRename, updatedName, new ParserOptions() { Culture = culture });
}
/// <summary>
/// Creates a renamer instance for updating a field reference from <paramref name="parameters"/> in expressions.
/// </summary>
/// <param name="parameters">Type of parameters for formula. The fields in the parameter record can
/// be acecssed as top-level identifiers in the formula. Must be the names from before any rename operation is applied.</param>
/// <param name="pathToRename">Path to the field to rename.</param>
/// <param name="updatedName">New name. Replaces the last segment of <paramref name="pathToRename"/>.</param>
/// <param name="options">Parser option to support TextFirst (if necessary) and culture.</param>
public RenameDriver CreateFieldRenamer(RecordType parameters, DPath pathToRename, DName updatedName, ParserOptions options)
{
Contracts.CheckValue(parameters, nameof(parameters));
Contracts.CheckValid(pathToRename, nameof(pathToRename));
Contracts.CheckValid(updatedName, nameof(updatedName));
/*
** PowerFxConfig handles symbol lookup in TryGetSymbol. As part of that, if that global entity
** has a display name and we're in the process of converting an expression from invariant -> display,
** we also return that entities display name so it gets updated.
** For Rename, we're reusing that invariant->display support, but only doing it for a single name,
** specified by `pathToRename`. So, we need to make sure that names in PowerFxConfig still bind,
** but that we don't return any display names for them. Thus, we clone a PowerFxConfig but without
** display name support and construct a resolver from that instead, which we use for the rewrite binding.
*/
return new RenameDriver(parameters, pathToRename, updatedName, this, CreateResolverInternal() as ReadOnlySymbolTable, CreateBinderGlue(), options, false);
}
public RenameDriver CreateOptionSetRenamer(RecordType parameters, DPath pathToRename, DName updatedName, CultureInfo culture)
{
return new RenameDriver(parameters, pathToRename, updatedName, this, CreateResolverInternal() as ReadOnlySymbolTable, CreateBinderGlue(), culture, true);
}
/// <summary>
/// Convert references in an expression to the invariant form.
/// </summary>
/// <param name="expressionText">textual representation of the formula.</param>
/// <param name="parameters">Type of parameters for formula. The fields in the parameter record can
/// be acecssed as top-level identifiers in the formula. If DisplayNames are used, make sure to have that mapping
/// as part of the RecordType.</param>
/// <param name="parseCulture">Culture.</param>
/// <returns>The formula, with all identifiers converted to invariant form.</returns>
public string GetInvariantExpression(string expressionText, RecordType parameters, CultureInfo parseCulture = null)
{
var ruleScope = this.GetRuleScope();
var symbolTable = (parameters == null) ? null : SymbolTable.NewFromRecord(parameters);
return GetInvariantExpressionWorker(expressionText, symbolTable, parseCulture);
}
public string GetInvariantExpressionParserOption(string expressionText, RecordType parameters, ParserOptions options)
{
var ruleScope = this.GetRuleScope();
var symbolTable = (parameters == null) ? null : SymbolTable.NewFromRecord(parameters);
return GetInvariantExpressionWorker(expressionText, symbolTable, options);
}
internal string GetInvariantExpressionWorker(string expressionText, ReadOnlySymbolTable symbolTable, CultureInfo parseCulture)
{
var ruleScope = this.GetRuleScope();
return ExpressionLocalizationHelper.ConvertExpression(expressionText, ruleScope, GetDefaultBindingConfig(), CreateResolverInternal(symbolTable), CreateBinderGlue(), parseCulture, Config.Features, toDisplay: false);
}
internal string GetInvariantExpressionWorker(string expressionText, ReadOnlySymbolTable symbolTable, ParserOptions options)
{
var ruleScope = this.GetRuleScope();
return ExpressionLocalizationHelper.ConvertExpression(expressionText, ruleScope, GetDefaultBindingConfig(), CreateResolverInternal(symbolTable), CreateBinderGlue(), options, Config.Features, toDisplay: false);
}
/// <summary>
/// Convert references in an expression to the display form.
/// </summary>
/// <param name="expressionText">textual representation of the formula.</param>
/// <param name="parameters">Type of parameters for formula. The fields in the parameter record can
/// be acecssed as top-level identifiers in the formula. If DisplayNames are used, make sure to have that mapping
/// as part of the RecordType.</param>
/// <param name="culture">Culture.</param>
/// <returns>The formula, with all identifiers converted to display form.</returns>
public string GetDisplayExpression(string expressionText, RecordType parameters, CultureInfo culture = null)
{
var symbols = SymbolTable.NewFromRecord(parameters);
return GetDisplayExpression(expressionText, symbols, culture);
}
public string GetDisplayExpression(string expressionText, ReadOnlySymbolTable symbolTable, CultureInfo culture = null)
{
var ruleScope = this.GetRuleScope();
return ExpressionLocalizationHelper.ConvertExpression(expressionText, ruleScope, GetDefaultBindingConfig(), CreateResolverInternal(symbolTable), CreateBinderGlue(), culture, Config.Features, toDisplay: true);
}
public DefinitionsCheckResult AddUserDefinedFunction(string script, CultureInfo parseCulture = null, ReadOnlySymbolTable symbolTable = null, bool allowSideEffects = false)
{
var engineTypesAndFunctions = ReadOnlySymbolTable.Compose(PrimitiveTypes, SupportedFunctions);
return Config.SymbolTable.AddUserDefinedFunction(script, parseCulture, engineTypesAndFunctions, symbolTable, allowSideEffects);
}
}
}