|
1 | 1 | /****************************************************************************************************** |
2 | 2 | Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator) |
3 | | - Version : 1.3.2.2 |
| 3 | + Version : 1.3.2.3 |
4 | 4 | (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable) |
5 | 5 |
|
6 | 6 | Author : Coding Seb |
7 | 7 | Licence : MIT (https://github.com/codingseb/ExpressionEvaluator/blob/master/LICENSE.md) |
8 | 8 | *******************************************************************************************************/ |
9 | 9 |
|
10 | 10 | using System; |
| 11 | +using System.Collections; |
11 | 12 | using System.Collections.Generic; |
12 | 13 | using System.ComponentModel; |
13 | 14 | using System.Dynamic; |
@@ -51,7 +52,7 @@ public class ExpressionEvaluator |
51 | 52 |
|
52 | 53 |
|
53 | 54 | // Depending on OptionInlineNamespacesEvaluationActive. Initialized in constructor |
54 | | - private string InstanceCreationWithNewKeywordRegexPattern { get { return $@"^new\s+(?<name>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern}0-9{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?\s*((?<isfunction>[(])|(?<isArray>\[))?"; } } |
| 55 | + private string InstanceCreationWithNewKeywordRegexPattern { get { return $@"^new\s+(?<name>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern}0-9{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?\s*((?<isfunction>[(])|(?<isArray>\[)|(?<isInit>[{{]))?"; } } |
55 | 56 | private Regex instanceCreationWithNewKeywordRegex = null; |
56 | 57 | private string CastRegexPattern { get { return $@"^\(\s*(?<typeName>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern }0-9\{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }[\]<>]*[?]?)\s*\)"; } } |
57 | 58 | private Regex castRegex = null; |
@@ -1423,15 +1424,52 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx |
1423 | 1424 |
|
1424 | 1425 | i += instanceCreationMatch.Length; |
1425 | 1426 |
|
| 1427 | + if (type == null) |
| 1428 | + throw new ExpressionEvaluatorSyntaxErrorException($"Type or class {completeName}{genericTypes} is unknown"); |
| 1429 | + |
1426 | 1430 | if (instanceCreationMatch.Groups["isfunction"].Success) |
1427 | 1431 | { |
1428 | 1432 | List<string> constructorArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, true); |
1429 | | - |
1430 | | - if (type == null) |
1431 | | - throw new ExpressionEvaluatorSyntaxErrorException($"Type or class {completeName}{genericTypes} is unknown"); |
| 1433 | + i++; |
1432 | 1434 |
|
1433 | 1435 | List<object> cArgs = constructorArgs.ConvertAll(arg => Evaluate(arg)); |
1434 | | - stack.Push(Activator.CreateInstance(type, cArgs.ToArray())); |
| 1436 | + |
| 1437 | + object element = Activator.CreateInstance(type, cArgs.ToArray()); |
| 1438 | + |
| 1439 | + Match blockBeginningMatch = blockBeginningRegex.Match(expr.Substring(i)); |
| 1440 | + |
| 1441 | + if (blockBeginningMatch.Success) |
| 1442 | + { |
| 1443 | + i += blockBeginningMatch.Length; |
| 1444 | + |
| 1445 | + List<string> initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, true, ",", "{", "}"); |
| 1446 | + |
| 1447 | + if (typeof(IEnumerable).IsAssignableFrom(type)) |
| 1448 | + { |
| 1449 | + MethodInfo methodInfo = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance); |
| 1450 | + |
| 1451 | + initArgs.ForEach(subExpr => methodInfo.Invoke(element, new object[] { Evaluate(subExpr) })); |
| 1452 | + } |
| 1453 | + } |
| 1454 | + else |
| 1455 | + i--; |
| 1456 | + |
| 1457 | + stack.Push(element); |
| 1458 | + } |
| 1459 | + else if(instanceCreationMatch.Groups["isInit"].Success) |
| 1460 | + { |
| 1461 | + List<string> initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, true, ",", "{", "}"); |
| 1462 | + |
| 1463 | + object element = Activator.CreateInstance(type, new object[0]); |
| 1464 | + |
| 1465 | + if (typeof(IEnumerable).IsAssignableFrom(type)) |
| 1466 | + { |
| 1467 | + MethodInfo methodInfo = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance); |
| 1468 | + |
| 1469 | + initArgs.ForEach(subExpr => methodInfo.Invoke(element, new object[] { Evaluate(subExpr) })); |
| 1470 | + } |
| 1471 | + |
| 1472 | + stack.Push(element); |
1435 | 1473 | } |
1436 | 1474 | else if(instanceCreationMatch.Groups["isArray"].Success) |
1437 | 1475 | { |
@@ -1462,7 +1500,7 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx |
1462 | 1500 | stack.Push(array); |
1463 | 1501 | } |
1464 | 1502 | else |
1465 | | - throw new ExpressionEvaluatorSyntaxErrorException($"A new expression requires that type be followed by () or [](Check : {instanceCreationMatch.Value})"); |
| 1503 | + throw new ExpressionEvaluatorSyntaxErrorException($"A new expression requires that type be followed by (), [] or {{}}(Check : {instanceCreationMatch.Value})"); |
1466 | 1504 |
|
1467 | 1505 | return true; |
1468 | 1506 | } |
|
0 commit comments