-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathSymbolTable.cs
More file actions
498 lines (411 loc) · 18.4 KB
/
Copy pathSymbolTable.cs
File metadata and controls
498 lines (411 loc) · 18.4 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core;
using Microsoft.PowerFx.Core.Annotations;
using Microsoft.PowerFx.Core.Binding;
using Microsoft.PowerFx.Core.Binding.BindInfo;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Errors;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.Glue;
using Microsoft.PowerFx.Core.Types.Enums;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
namespace Microsoft.PowerFx
{
/// <summary>
/// Provides symbols to the engine. This includes variables (locals, globals), enums, options sets, and functions.
/// SymbolTables are mutable to support sessionful scenarios and can be chained together.
/// This is a publicly facing class around a <see cref="INameResolver"/>.
/// </summary>
[DebuggerDisplay("{DebugName}")]
[NotThreadSafe]
public class SymbolTable : ReadOnlySymbolTable, IGlobalSymbolNameResolver
{
private readonly GuardSingleThreaded _guard = new GuardSingleThreaded();
private readonly TexlFunctionSet _functions = new TexlFunctionSet();
private readonly SlotMap<NameLookupInfo?> _slots = new SlotMap<NameLookupInfo?>();
private readonly IDictionary<DName, FormulaType> _namedTypes = new Dictionary<DName, FormulaType>();
private DisplayNameProvider _environmentSymbolDisplayNameProvider = new SingleSourceDisplayNameProvider();
IEnumerable<KeyValuePair<string, NameLookupInfo>> IGlobalSymbolNameResolver.GlobalSymbols => _variables;
IEnumerable<KeyValuePair<DName, FormulaType>> INameResolver.NamedTypes => _namedTypes;
internal IReadOnlyDictionary<DName, FormulaType> NamedTypes => _namedTypes.ToDictionary(v => v.Key, v => v.Value);
internal const string UserInfoSymbolName = "User";
/// <summary>
/// Does this SymbolTable require a corresponding SymbolValue?
/// True if we have AddVariables, but not needed if we just have constants or functions.
/// </summary>
public bool NeedsValues => !_slots.IsEmpty;
private DName ValidateName(string name)
{
if (!DName.IsValidDName(name))
{
throw new ArgumentException("Invalid name: ${name}");
}
return new DName(name);
}
public SymbolTable()
{
EnumStoreBuilder = new EnumStoreBuilder();
}
TexlFunctionSet INameResolver.Functions => _guard.VerifyNoWriters(_functions);
public override FormulaType GetTypeFromSlot(ISymbolSlot slot)
{
if (_slots.TryGet(slot.SlotIndex, out var nameInfo))
{
return FormulaType.Build(nameInfo.Value.Type);
}
throw NewBadSlotException(slot);
}
internal override bool TryGetVariable(DName name, out NameLookupInfo symbol, out DName displayName)
{
var lookupName = name;
if (_environmentSymbolDisplayNameProvider.TryGetDisplayName(name, out displayName))
{
// do nothing as provided name can be used for lookup with logical name
}
else if (_environmentSymbolDisplayNameProvider.TryGetLogicalName(name, out var logicalName))
{
lookupName = logicalName;
displayName = name;
}
return _variables.TryGetValue(lookupName, out symbol);
}
// Exists for binary backcompat.
public ISymbolSlot AddVariable(string name, FormulaType type, bool mutable = false, string displayName = null)
{
var props = new SymbolProperties
{
CanSet = mutable,
CanMutate = mutable,
CanSetMutate = false
};
return AddVariable(name, type, props, displayName);
}
/// <summary>
/// Provide variable for binding only.
/// Value must be provided at runtime.
/// This can throw an exception in case of there is a conflict in name with existing names.
/// </summary>
/// <param name="name"></param>
/// <param name="type"></param>
/// <param name="props"></param>
/// <param name="displayName"></param>
public ISymbolSlot AddVariable(string name, FormulaType type, SymbolProperties props, string displayName = null)
{
if (props == null)
{
// Default.
props = new SymbolProperties();
}
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
DName displayDName = default;
DName varDName = ValidateName(name);
if (type is Types.Void)
{
throw new InvalidOperationException($"Can't set {name} to a Void value.");
}
if (displayName != null)
{
displayDName = ValidateName(displayName);
}
if (_variables.ContainsKey(name))
{
throw new InvalidOperationException($"{name} is already defined");
}
var slotIndex = _slots.Alloc();
var data = new NameSymbol(name, props)
{
Owner = this,
SlotIndex = slotIndex
};
var info = new NameLookupInfo(
BindKind.PowerFxResolvedObject,
type._type,
DPath.Root,
0,
data: data,
displayName: displayDName);
_slots.SetInitial(slotIndex, info);
// Attempt to update display name provider before symbol table,
// since it can throw on collision and we want to leave the config in a good state.
if (_environmentSymbolDisplayNameProvider is SingleSourceDisplayNameProvider ssDnp)
{
_environmentSymbolDisplayNameProvider = ssDnp.AddField(varDName, displayDName != default ? displayDName : varDName);
}
_variables.Add(name, info); // can't exist
return data;
}
/// <summary>
/// Add a constant. This is like a variable, but the value is known at bind time.
/// </summary>
/// <param name="name"></param>
/// <param name="data"></param>
public void AddConstant(string name, FormulaValue data)
{
using var guard = _guard.Enter(); // Region is single threaded.
var type = data.Type;
Inc();
ValidateName(name);
var info = new NameLookupInfo(
BindKind.PowerFxResolvedObject,
type._type,
DPath.Root,
0,
data);
// Attempt to update display name provider before symbol table,
// since it can throw on collision and we want to leave the config in a good state.
// add (logical, logical) pair to display name provider so it still can be included in collision checks.
if (_environmentSymbolDisplayNameProvider is SingleSourceDisplayNameProvider ssDnp)
{
var dName = new DName(name);
_environmentSymbolDisplayNameProvider = ssDnp.AddField(dName, dName);
}
_variables.Add(name, info); // can't exist
}
internal static ParserOptions GetUDFParserOptions(CultureInfo culture, bool allowSideEffects)
{
return new ParserOptions()
{
AllowsSideEffects = allowSideEffects,
Culture = culture ?? CultureInfo.InvariantCulture,
};
}
/// <summary>
/// Adds user defined functions in the script.
/// </summary>
/// <param name="script">String representation of the user defined function.</param>
/// <param name="parseCulture">CultureInfo to parse the script againts. Default is invariant.</param>
/// <param name="symbolTable">Extra symbols to bind UDF. Commonly coming from Engine.</param>
/// <param name="extraSymbolTable">Additional symbols to bind UDF.</param>
/// <param name="allowSideEffects">Allow for curly brace parsing.</param>
/// <param name="features">Features in effect for processing the body of the UDF.</param>
internal DefinitionsCheckResult AddUserDefinedFunction(string script, CultureInfo parseCulture = null, ReadOnlySymbolTable symbolTable = null, ReadOnlySymbolTable extraSymbolTable = null, bool allowSideEffects = false, Features features = null)
{
var composedSymbolTable = ReadOnlySymbolTable.Compose(this, symbolTable, extraSymbolTable);
var checkResult = GetDefinitionsCheckResult(script, parseCulture, composedSymbolTable, allowSideEffects, features);
var udfs = checkResult.ApplyCreateUserDefinedFunctions();
Contracts.AssertValue(udfs);
if (checkResult.IsSuccess)
{
AddFunctions(udfs);
}
return checkResult;
}
internal static DefinitionsCheckResult GetDefinitionsCheckResult(string script, CultureInfo parseCulture = null, ReadOnlySymbolTable symbolTable = null, bool allowSideEffects = false, Features features = null)
{
var options = GetUDFParserOptions(parseCulture, allowSideEffects);
var checkResult = new DefinitionsCheckResult(features);
return checkResult.SetText(script, options)
.SetBindingInfo(symbolTable);
}
/// <summary>
/// Remove variable, entity or constant of a given name.
/// </summary>
/// <param name="name">display or logical name for the variable or entity to be removed. Logical name of constant to be removed.</param>
public void RemoveVariable(string name)
{
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
// Also remove from display name provider
if (_environmentSymbolDisplayNameProvider is SingleSourceDisplayNameProvider ssDP)
{
var lookupName = new DName(name);
if (_environmentSymbolDisplayNameProvider.TryGetDisplayName(lookupName, out var displayName))
{
// Do nothing as supplied name was logical name.
}
else if (_environmentSymbolDisplayNameProvider.TryGetLogicalName(lookupName, out var logicalName))
{
name = logicalName.Value;
lookupName = logicalName;
}
_environmentSymbolDisplayNameProvider = ssDP.RemoveField(lookupName);
}
if (_variables.TryGetValue(name, out var info))
{
if (info.Data is NameSymbol info2)
{
_slots.Remove(info2.SlotIndex);
info2.DisposeSlot();
}
}
// Ok to remove if missing.
_variables.Remove(name);
}
/// <summary>
/// Remove function of given name.
/// </summary>
/// <param name="name"></param>
public void RemoveFunction(string name)
{
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
_functions.RemoveAll(name);
}
internal void RemoveFunction(TexlFunction function)
{
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
_functions.RemoveAll(function);
}
internal void AddFunctions(TexlFunctionSet functions)
{
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
if (functions._count == 0)
{
return;
}
_functions.Add(functions);
// Add any associated enums
EnumStoreBuilder?.WithRequiredEnums(functions);
}
internal void AddFunction(TexlFunction function)
{
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
_functions.Add(function);
if (EnumStoreBuilder == null)
{
_enumStoreBuilder = new EnumStoreBuilder();
}
// Add any associated enums
EnumStoreBuilder.WithRequiredEnums(new TexlFunctionSet(function));
}
internal EnumStoreBuilder EnumStoreBuilder
{
get => _enumStoreBuilder;
set
{
Inc();
_enumStoreBuilder = value;
}
}
public void AddOptionSet(OptionSet optionSet, DName displayName = default)
{
AddEntity(optionSet, displayName);
}
internal void AddEntity(IExternalEntity entity, DName displayName = default)
{
Inc();
NameLookupInfo nameInfo;
if (entity is IExternalOptionSet optionSet)
{
nameInfo = new NameLookupInfo(BindKind.OptionSet, optionSet.Type, DPath.Root, 0, optionSet, displayName);
}
else if (entity is IExternalDataSource)
{
nameInfo = new NameLookupInfo(BindKind.Data, entity.Type, DPath.Root, 0, entity, displayName);
}
else
{
throw new NotImplementedException($"{entity.GetType().Name} not supported.");
}
// Attempt to update display name provider before symbol table,
// since it can throw on collision and we want to leave the config in a good state.
// For entities without a display name, add (logical, logical) pair to still be included in collision checks.
if (_environmentSymbolDisplayNameProvider is SingleSourceDisplayNameProvider ssDnp)
{
displayName = displayName != default ? displayName : entity.EntityName;
_environmentSymbolDisplayNameProvider = ssDnp.AddField(entity.EntityName, displayName);
}
_variables.Add(entity.EntityName, nameInfo);
}
// Sync version for convenience.
public void AddHostObject(string name, FormulaType type, Func<IServiceProvider, FormulaValue> getValue)
{
this.AddHostObject(name, type, (sp) => Task.FromResult(getValue(sp)));
}
/// <summary>
/// Adds a host object schema, that can be referenced in the formula.
/// Actual object is added in Runtime config service provider.
/// </summary>
/// <param name="name">Name of the object.</param>
/// <param name="type">Type of the object.</param>
/// <param name="getValue">Call back that will retrieve object from the service provider.
/// It can throw CustomFunctionErrorException, that fx will convert to an error.</param>
public void AddHostObject(string name, FormulaType type, Func<IServiceProvider, Task<FormulaValue>> getValue)
{
using var guard = _guard.Enter(); // Region is single threaded.
var hostDName = ValidateName(name);
// Attempt to update display name provider before symbol table,
// since it can throw on collision and we want to leave the config in a good state.
if (_environmentSymbolDisplayNameProvider is SingleSourceDisplayNameProvider ssDnp)
{
_environmentSymbolDisplayNameProvider = ssDnp.AddField(hostDName, default);
}
var info = new NameLookupInfo(
BindKind.PowerFxResolvedObject,
type._type,
DPath.Root,
0,
data: getValue,
displayName: default);
_variables.Add(hostDName, info);
}
/// <summary>
/// Adds a named type that can be referenced in expression.
/// </summary>
/// <param name="typeName">Name of the type to be added into Symbol table.</param>
/// <param name="type">Type associated with the name.</param>
public void AddType(DName typeName, FormulaType type)
{
Contracts.AssertValue(typeName.Value);
Contracts.AssertValue(type);
Contracts.Assert(typeName.Value.Length > 0);
Contracts.AssertValid(typeName);
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
if (_namedTypes.ContainsKey(typeName))
{
throw new InvalidOperationException($"{typeName} is already defined.");
}
_namedTypes.Add(typeName, type);
}
internal void AddTypes(IEnumerable<KeyValuePair<DName, FormulaType>> types)
{
Contracts.AssertValue(types);
using var guard = _guard.Enter(); // Region is single threaded.
Inc();
foreach (var type in types)
{
if (_namedTypes.ContainsKey(type.Key))
{
throw new InvalidOperationException($"{type.Key} is already defined.");
}
_namedTypes.Add(type.Key, type.Value);
}
}
/// <summary>
/// Helper to create a symbol table with primitive types.
/// </summary>
/// <returns>SymbolTable with primitive types.</returns>
public static SymbolTable WithPrimitiveTypes()
{
var s = new SymbolTable
{
DebugName = $"SymbolTable with PrimitiveTypes"
};
s.AddTypes(FormulaType.PrimitiveTypes);
return s;
}
bool INameResolver.LookupType(DName name, out FormulaType fType)
{
if (_namedTypes.TryGetValue(name, out fType))
{
return true;
}
return false;
}
}
}