| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Microsoft.Xrm.Sdk; |
| | | 7 | | |
| | | 8 | | namespace Xrm.Oss.XTL.Interpreter |
| | | 9 | | { |
| | | 10 | | public class XTLInterpreter |
| | | 11 | | { |
| | 162 | 12 | | private StringReader _reader = null; |
| | | 13 | | private int _position; |
| | | 14 | | private string _input; |
| | | 15 | | private char _previous; |
| | | 16 | | private char _current; |
| | | 17 | | |
| | | 18 | | private Entity _primary; |
| | | 19 | | private IOrganizationService _service; |
| | | 20 | | private ITracingService _tracing; |
| | | 21 | | private OrganizationConfig _organizationConfig; |
| | | 22 | | |
| | | 23 | | public delegate ValueExpression FunctionHandler(Entity primary, IOrganizationService service, ITracingService tr |
| | | 24 | | |
| | 162 | 25 | | private Dictionary<string, FunctionHandler> _handlers = new Dictionary<string, FunctionHandler> |
| | 162 | 26 | | { |
| | 162 | 27 | | { "And", FunctionHandlers.And }, |
| | 162 | 28 | | { "Array", FunctionHandlers.Array }, |
| | 162 | 29 | | { "Concat", FunctionHandlers.Concat }, |
| | 162 | 30 | | { "ConvertDateTime", FunctionHandlers.ConvertDateTime }, |
| | 162 | 31 | | { "DateTimeNow", FunctionHandlers.DateTimeNow }, |
| | 162 | 32 | | { "DateTimeUtcNow", FunctionHandlers.DateTimeUtcNow }, |
| | 162 | 33 | | { "DateToString", FunctionHandlers.DateToString }, |
| | 162 | 34 | | { "Fetch", FunctionHandlers.Fetch }, |
| | 162 | 35 | | { "First", FunctionHandlers.First }, |
| | 162 | 36 | | { "Format", FunctionHandlers.Format }, |
| | 162 | 37 | | { "If", FunctionHandlers.If }, |
| | 162 | 38 | | { "IndexOf", FunctionHandlers.IndexOf }, |
| | 162 | 39 | | { "IsEqual", FunctionHandlers.IsEqual }, |
| | 162 | 40 | | { "IsGreater", FunctionHandlers.IsGreater }, |
| | 162 | 41 | | { "IsGreaterEqual", FunctionHandlers.IsGreaterEqual }, |
| | 162 | 42 | | { "IsLess", FunctionHandlers.IsLess }, |
| | 162 | 43 | | { "IsLessEqual", FunctionHandlers.IsLessEqual }, |
| | 162 | 44 | | { "IsNull", FunctionHandlers.IsNull }, |
| | 162 | 45 | | { "Join", FunctionHandlers.Join }, |
| | 162 | 46 | | { "Last", FunctionHandlers.Last}, |
| | 162 | 47 | | { "Map", FunctionHandlers.Map }, |
| | 162 | 48 | | { "NewLine", FunctionHandlers.NewLine }, |
| | 162 | 49 | | { "Not", FunctionHandlers.Not }, |
| | 162 | 50 | | { "Or", FunctionHandlers.Or }, |
| | 162 | 51 | | { "OrganizationUrl", FunctionHandlers.GetOrganizationUrl }, |
| | 162 | 52 | | { "PrimaryRecord", FunctionHandlers.GetPrimaryRecord }, |
| | 162 | 53 | | { "RecordId", FunctionHandlers.GetRecordId }, |
| | 162 | 54 | | { "RecordLogicalName", FunctionHandlers.GetRecordLogicalName }, |
| | 162 | 55 | | { "RecordTable", FunctionHandlers.RenderRecordTable }, |
| | 162 | 56 | | { "RecordUrl", FunctionHandlers.GetRecordUrl }, |
| | 162 | 57 | | { "Replace", FunctionHandlers.Replace }, |
| | 162 | 58 | | { "RetrieveAudit", FunctionHandlers.RetrieveAudit }, |
| | 162 | 59 | | { "Snippet", FunctionHandlers.Snippet }, |
| | 162 | 60 | | { "Sort", FunctionHandlers.Sort }, |
| | 162 | 61 | | { "Static", FunctionHandlers.Static }, |
| | 162 | 62 | | { "Substring", FunctionHandlers.Substring }, |
| | 162 | 63 | | { "Union", FunctionHandlers.Union }, |
| | 162 | 64 | | { "Value", FunctionHandlers.GetValue } |
| | 162 | 65 | | }; |
| | | 66 | | |
| | 162 | 67 | | public XTLInterpreter(string input, Entity primary, OrganizationConfig organizationConfig, IOrganizationService |
| | 162 | 68 | | { |
| | 162 | 69 | | _primary = primary; |
| | 162 | 70 | | _service = service; |
| | 162 | 71 | | _tracing = tracing; |
| | 162 | 72 | | _organizationConfig = organizationConfig; |
| | 162 | 73 | | _input = input; |
| | 162 | 74 | | _position = 0; |
| | | 75 | | |
| | 162 | 76 | | _reader = new StringReader(input ?? string.Empty); |
| | 162 | 77 | | GetChar(); |
| | 162 | 78 | | SkipWhiteSpace(); |
| | 162 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Reads the next character and sets it as current. Old current char becomes previous. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns>True if read succeeded, false if end of input</returns> |
| | | 85 | | private void GetChar(int? index = null) |
| | 13369 | 86 | | { |
| | 13369 | 87 | | if (index != null) |
| | 14 | 88 | | { |
| | | 89 | | // Initialize a new reader to move back to the beginning |
| | 14 | 90 | | _reader = new StringReader(_input); |
| | 14 | 91 | | _position = index.Value; |
| | | 92 | | |
| | | 93 | | // Skip to searched index |
| | 4306 | 94 | | for (var i = 0; i < index; i++) |
| | 2139 | 95 | | { |
| | 2139 | 96 | | _reader.Read(); |
| | 2139 | 97 | | } |
| | 14 | 98 | | } |
| | | 99 | | |
| | 13369 | 100 | | _previous = _current; |
| | 13369 | 101 | | var character = _reader.Read(); |
| | 13369 | 102 | | _current = (char)character; |
| | | 103 | | |
| | 13369 | 104 | | if (character != -1) |
| | 13209 | 105 | | { |
| | 13209 | 106 | | _position++; |
| | 13209 | 107 | | } |
| | 13369 | 108 | | } |
| | | 109 | | |
| | | 110 | | private void Expected(string expected) |
| | 3 | 111 | | { |
| | 3 | 112 | | throw new InvalidPluginExecutionException($"{expected} expected after '{_previous}' at position {_position}, |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | private bool IsEof() |
| | 11701 | 116 | | { |
| | 11701 | 117 | | return _current == '\uffff'; |
| | 11701 | 118 | | } |
| | | 119 | | |
| | | 120 | | private void SkipWhiteSpace() |
| | 4168 | 121 | | { |
| | 6020 | 122 | | while(char.IsWhiteSpace(_current) && !IsEof()) { |
| | 926 | 123 | | GetChar(); |
| | 926 | 124 | | } |
| | 4168 | 125 | | } |
| | | 126 | | |
| | | 127 | | private void Match (char c) |
| | 1029 | 128 | | { |
| | 1029 | 129 | | if (_current != c) |
| | 2 | 130 | | { |
| | 2 | 131 | | Expected(c.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 132 | | } |
| | | 133 | | |
| | 1027 | 134 | | GetChar(); |
| | 1027 | 135 | | SkipWhiteSpace(); |
| | 1027 | 136 | | } |
| | | 137 | | |
| | | 138 | | private string GetName() |
| | 523 | 139 | | { |
| | 523 | 140 | | SkipWhiteSpace(); |
| | | 141 | | |
| | 524 | 142 | | if (!char.IsLetter(_current)) { |
| | 1 | 143 | | Expected($"Identifier"); |
| | 0 | 144 | | } |
| | | 145 | | |
| | 522 | 146 | | var name = string.Empty; |
| | | 147 | | |
| | 7386 | 148 | | while (char.IsLetterOrDigit(_current) && !IsEof()) { |
| | 3432 | 149 | | name += _current; |
| | 3432 | 150 | | GetChar(); |
| | 3432 | 151 | | } |
| | | 152 | | |
| | 522 | 153 | | SkipWhiteSpace(); |
| | 522 | 154 | | return name; |
| | 522 | 155 | | } |
| | | 156 | | |
| | | 157 | | private List<ValueExpression> Expression(char[] terminators, Dictionary<string, ValueExpression> formulaArgs) |
| | 443 | 158 | | { |
| | 443 | 159 | | var returnValue = new List<ValueExpression>(); |
| | | 160 | | |
| | | 161 | | do |
| | 963 | 162 | | { |
| | 1223 | 163 | | if (_current == ',') { |
| | 260 | 164 | | GetChar(); |
| | 260 | 165 | | } |
| | 703 | 166 | | else if (_current == '"' || _current == '\'') |
| | 313 | 167 | | { |
| | 313 | 168 | | var delimiter = _current; |
| | 313 | 169 | | var stringConstant = string.Empty; |
| | | 170 | | |
| | | 171 | | // Skip opening quote |
| | 313 | 172 | | GetChar(); |
| | | 173 | | |
| | | 174 | | // Allow to escape quotes by backslashes |
| | 7049 | 175 | | while ((_current != delimiter || _previous == '\\') && !IsEof()) |
| | 6736 | 176 | | { |
| | 6736 | 177 | | stringConstant += _current; |
| | 6736 | 178 | | GetChar(); |
| | 6736 | 179 | | } |
| | | 180 | | |
| | | 181 | | // Skip closing quote |
| | 313 | 182 | | GetChar(); |
| | 313 | 183 | | returnValue.Add(new ValueExpression(stringConstant, stringConstant)); |
| | 313 | 184 | | } |
| | 390 | 185 | | else if (char.IsDigit(_current) || _current == '-') |
| | 68 | 186 | | { |
| | 68 | 187 | | var digit = 0; |
| | 68 | 188 | | var fractionalPart = 0; |
| | 68 | 189 | | var processingFractionalPart = false; |
| | | 190 | | |
| | | 191 | | // Multiply by -1 for negative numbers |
| | 68 | 192 | | var multiplicator = 1; |
| | | 193 | | |
| | | 194 | | do |
| | 153 | 195 | | { |
| | 153 | 196 | | if (_current == '-') |
| | 3 | 197 | | { |
| | 3 | 198 | | multiplicator = -1; |
| | 3 | 199 | | } |
| | 150 | 200 | | else if (_current != '.') |
| | 121 | 201 | | { |
| | 121 | 202 | | if (processingFractionalPart) |
| | 34 | 203 | | { |
| | 34 | 204 | | fractionalPart = fractionalPart * 10 + int.Parse(_current.ToString(CultureInfo.Invariant |
| | 34 | 205 | | } |
| | | 206 | | else |
| | 87 | 207 | | { |
| | 87 | 208 | | digit = digit * 10 + int.Parse(_current.ToString(CultureInfo.InvariantCulture)); |
| | 87 | 209 | | } |
| | 121 | 210 | | } |
| | | 211 | | else |
| | 29 | 212 | | { |
| | 29 | 213 | | processingFractionalPart = true; |
| | 29 | 214 | | } |
| | | 215 | | |
| | 153 | 216 | | GetChar(); |
| | 306 | 217 | | } while ((char.IsDigit(_current) || _current == '.') && !IsEof()); |
| | | 218 | | |
| | 68 | 219 | | switch(_current) |
| | | 220 | | { |
| | | 221 | | case 'd': |
| | 15 | 222 | | double doubleValue = multiplicator * (digit + fractionalPart / Math.Pow(10, (fractionalPart. |
| | 15 | 223 | | returnValue.Add(new ValueExpression(doubleValue.ToString(CultureInfo.InvariantCulture), doub |
| | 15 | 224 | | GetChar(); |
| | 15 | 225 | | break; |
| | | 226 | | case 'm': |
| | 15 | 227 | | decimal decimalValue = multiplicator * (digit + fractionalPart / (decimal) Math.Pow(10, (fra |
| | 15 | 228 | | returnValue.Add(new ValueExpression(decimalValue.ToString(CultureInfo.InvariantCulture), dec |
| | 15 | 229 | | GetChar(); |
| | 15 | 230 | | break; |
| | | 231 | | default: |
| | 38 | 232 | | if (processingFractionalPart) |
| | 1 | 233 | | { |
| | 1 | 234 | | throw new InvalidDataException("For defining numbers with fractional parts, please appen |
| | | 235 | | } |
| | 37 | 236 | | var value = digit * multiplicator; |
| | | 237 | | |
| | 37 | 238 | | returnValue.Add(new ValueExpression(value.ToString(CultureInfo.InvariantCulture), value)); |
| | 37 | 239 | | break; |
| | | 240 | | } |
| | | 241 | | |
| | 67 | 242 | | } |
| | 322 | 243 | | else if (terminators.Contains(_current)) |
| | 19 | 244 | | { |
| | | 245 | | // Parameterless function or empty array encountered |
| | 19 | 246 | | } |
| | | 247 | | // The first char of a function must not be a digit |
| | | 248 | | else |
| | 303 | 249 | | { |
| | 303 | 250 | | var value = Formula(formulaArgs); |
| | | 251 | | |
| | 302 | 252 | | if (value != null) |
| | 298 | 253 | | { |
| | 298 | 254 | | returnValue.Add(value); |
| | 298 | 255 | | } |
| | 302 | 256 | | } |
| | | 257 | | |
| | 961 | 258 | | SkipWhiteSpace(); |
| | 1922 | 259 | | } while (!terminators.Contains(_current) && !IsEof()); |
| | | 260 | | |
| | 441 | 261 | | return returnValue; |
| | 441 | 262 | | } |
| | | 263 | | |
| | | 264 | | private ValueExpression ApplyExpression (string name, List<ValueExpression> parameters, Dictionary<string, Value |
| | 385 | 265 | | { |
| | 386 | 266 | | if (!_handlers.ContainsKey(name)) { |
| | 1 | 267 | | throw new InvalidPluginExecutionException($"Function {name} is not known!"); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | // In this case we're only stepping through in the initial interpreting of the lambda |
| | 419 | 271 | | if (formulaArgs != null && formulaArgs.Any(a => a.Value == null)) |
| | 11 | 272 | | { |
| | 11 | 273 | | return new ValueExpression(null); |
| | | 274 | | } |
| | | 275 | | |
| | 373 | 276 | | var lazyExecution = new Lazy<ValueExpression>(() => |
| | 737 | 277 | | { |
| | 737 | 278 | | _tracing.Trace($"Processing handler {name}"); |
| | 737 | 279 | | var result = _handlers[name](_primary, _service, _tracing, _organizationConfig, parameters); |
| | 731 | 280 | | _tracing.Trace($"Successfully processed handler {name}"); |
| | 373 | 281 | | |
| | 731 | 282 | | return result; |
| | 731 | 283 | | }); |
| | | 284 | | |
| | 373 | 285 | | return new ValueExpression(lazyExecution); |
| | 384 | 286 | | } |
| | | 287 | | |
| | | 288 | | private ValueExpression Formula(Dictionary<string, ValueExpression> args) |
| | 564 | 289 | | { |
| | 564 | 290 | | SkipWhiteSpace(); |
| | | 291 | | |
| | 583 | 292 | | if (_current == '[') { |
| | 19 | 293 | | Match('['); |
| | 19 | 294 | | var arrayParameters = Expression(new[] { ']' }, args); |
| | 19 | 295 | | Match(']'); |
| | | 296 | | |
| | 19 | 297 | | return ApplyExpression("Array", arrayParameters); |
| | | 298 | | } |
| | 545 | 299 | | else if(_current == '(') |
| | 4 | 300 | | { |
| | | 301 | | // Match arrow functions in style of (param) => Convert(param) |
| | 4 | 302 | | Match('('); |
| | | 303 | | |
| | 4 | 304 | | var variableNames = new List<string>(); |
| | | 305 | | |
| | | 306 | | do |
| | 7 | 307 | | { |
| | 7 | 308 | | SkipWhiteSpace(); |
| | 7 | 309 | | variableNames.Add(GetName()); |
| | 7 | 310 | | SkipWhiteSpace(); |
| | | 311 | | |
| | 7 | 312 | | if (_current == ',') |
| | 3 | 313 | | { |
| | 3 | 314 | | GetChar(); |
| | 3 | 315 | | } |
| | 14 | 316 | | } while (_current != ')'); |
| | | 317 | | |
| | | 318 | | // Initialize variables as null |
| | 18 | 319 | | var formulaArgs = variableNames.ToDictionary(n => n, v => (ValueExpression) null); |
| | 4 | 320 | | Match(')'); |
| | | 321 | | |
| | 4 | 322 | | var usedReservedWords = variableNames |
| | 11 | 323 | | .Where(n => new List<string> { "true", "false", "null" }.Concat(_handlers.Keys).Contains(n)) |
| | 4 | 324 | | .ToList(); |
| | | 325 | | |
| | 4 | 326 | | if (usedReservedWords.Count > 0) |
| | 0 | 327 | | { |
| | 0 | 328 | | throw new InvalidPluginExecutionException($"Your variable names {string.Join(", ", usedReservedWords |
| | | 329 | | } |
| | | 330 | | |
| | 4 | 331 | | SkipWhiteSpace(); |
| | 4 | 332 | | Match('='); |
| | 4 | 333 | | Match('>'); |
| | 4 | 334 | | SkipWhiteSpace(); |
| | | 335 | | |
| | 4 | 336 | | var lambdaPosition = this._position - 1; |
| | | 337 | | |
| | 4 | 338 | | var lazyExecution = new Func<List<ValueExpression>, ValueExpression>((lambdaArgs) => |
| | 11 | 339 | | { |
| | 11 | 340 | | var currentIndex = this._position; |
| | 11 | 341 | | GetChar(lambdaPosition); |
| | 4 | 342 | | |
| | 11 | 343 | | var arguments = formulaArgs.ToList(); |
| | 48 | 344 | | for (var i = 0; i < lambdaArgs.Count; i++) { |
| | 14 | 345 | | if (i < formulaArgs.Count) |
| | 14 | 346 | | { |
| | 14 | 347 | | var parameterName = arguments[i].Key; |
| | 14 | 348 | | formulaArgs[parameterName] = lambdaArgs[i]; |
| | 14 | 349 | | } |
| | 14 | 350 | | } |
| | 4 | 351 | | |
| | 11 | 352 | | var result = Formula(formulaArgs); |
| | 11 | 353 | | GetChar(currentIndex - 1); |
| | 4 | 354 | | |
| | 11 | 355 | | return result; |
| | 11 | 356 | | }); |
| | | 357 | | |
| | | 358 | | // Run only for skipping the formula part |
| | 4 | 359 | | Formula(formulaArgs); |
| | | 360 | | |
| | 4 | 361 | | return new ValueExpression(lazyExecution, formulaArgs); |
| | | 362 | | } |
| | 541 | 363 | | else if (_current == '{') |
| | 60 | 364 | | { |
| | 60 | 365 | | Match('{'); |
| | 60 | 366 | | var dictionary = new Dictionary<string, object>(); |
| | 60 | 367 | | var firstRunPassed = false; |
| | | 368 | | |
| | | 369 | | do |
| | 90 | 370 | | { |
| | 90 | 371 | | SkipWhiteSpace(); |
| | | 372 | | |
| | 90 | 373 | | if (firstRunPassed) |
| | 30 | 374 | | { |
| | 30 | 375 | | Match(','); |
| | 30 | 376 | | SkipWhiteSpace(); |
| | 30 | 377 | | } |
| | | 378 | | else |
| | 60 | 379 | | { |
| | 60 | 380 | | firstRunPassed = true; |
| | 60 | 381 | | } |
| | | 382 | | |
| | 90 | 383 | | var name = GetName(); |
| | | 384 | | |
| | 89 | 385 | | SkipWhiteSpace(); |
| | 89 | 386 | | Match(':'); |
| | 89 | 387 | | SkipWhiteSpace(); |
| | | 388 | | |
| | 89 | 389 | | dictionary[name] = Formula(args)?.Value; |
| | | 390 | | |
| | 89 | 391 | | SkipWhiteSpace(); |
| | 178 | 392 | | } while (_current != '}'); |
| | | 393 | | |
| | 59 | 394 | | Match('}'); |
| | | 395 | | |
| | 148 | 396 | | return new ValueExpression(string.Join(", ", dictionary.Select(p => $"{p.Key}: {p.Value}")), dictionary) |
| | | 397 | | } |
| | 481 | 398 | | else if (char.IsDigit(_current) || _current == '"' || _current == '\'' || _current == '-') |
| | 55 | 399 | | { |
| | | 400 | | // This is only called in object initializers / dictionaries. Only one value should be entered here |
| | 55 | 401 | | return Expression(new[] { '}', ',' }, args).First(); |
| | | 402 | | } |
| | 426 | 403 | | else { |
| | 426 | 404 | | var name = GetName(); |
| | | 405 | | |
| | 426 | 406 | | if (args != null && args.ContainsKey(name)) |
| | 19 | 407 | | { |
| | 19 | 408 | | return args[name]; |
| | | 409 | | } |
| | | 410 | | |
| | 407 | 411 | | switch(name) |
| | | 412 | | { |
| | | 413 | | case "true": |
| | 26 | 414 | | return new ValueExpression(bool.TrueString, true); |
| | | 415 | | case "false": |
| | 10 | 416 | | return new ValueExpression(bool.FalseString, false); |
| | | 417 | | case "null": |
| | 1 | 418 | | return new ValueExpression( null ); |
| | | 419 | | default: |
| | 370 | 420 | | Match('('); |
| | 369 | 421 | | var parameters = Expression(new[] { ')' }, args); |
| | 367 | 422 | | Match(')'); |
| | | 423 | | |
| | 366 | 424 | | return ApplyExpression(name, parameters, args); |
| | | 425 | | } |
| | | 426 | | } |
| | 558 | 427 | | } |
| | | 428 | | |
| | | 429 | | public string Produce() |
| | 162 | 430 | | { |
| | 162 | 431 | | _tracing.Trace($"Initiating interpreter"); |
| | | 432 | | |
| | 163 | 433 | | if (string.IsNullOrWhiteSpace(_input)) { |
| | 1 | 434 | | _tracing.Trace("No formula passed, exiting"); |
| | 1 | 435 | | return string.Empty; |
| | | 436 | | } |
| | | 437 | | |
| | 161 | 438 | | var output = Formula(new Dictionary<string, ValueExpression> { }); |
| | | 439 | | |
| | 156 | 440 | | return output?.Text; |
| | 151 | 441 | | } |
| | | 442 | | } |
| | | 443 | | } |