@@ -35,7 +35,8 @@ public class ExpressionEvaluator
35
35
private static readonly Regex internalCharRegex = new Regex ( @"^['](\\[']|[^'])*[']" ) ;
36
36
private static readonly Regex indexingBeginningRegex = new Regex ( @"^[?]?\[" ) ;
37
37
private static readonly Regex assignationOrPostFixOperatorRegex = new Regex ( @"^\s*((?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>])|(?<postfixOperator>([+][+]|--)(?![" + diactiticsKeywordsRegexPattern + @"0-9])))" ) ;
38
- private static readonly Regex genericsDecodeRegex = new Regex ( "[^,<>]+(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?" , RegexOptions . Compiled ) ;
38
+ private static readonly Regex genericsDecodeRegex = new Regex ( "(?<name>[^,<>]+)(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?" , RegexOptions . Compiled ) ;
39
+ private static readonly Regex genericsEndOnlyOneTrim = new Regex ( @"\s+[>]\s+$" ) ;
39
40
40
41
private static readonly Regex endOfStringWithDollar = new Regex ( "^([^\" {\\ \\ ]|\\ \\ [\\ \\ \" 0abfnrtv])*[\" {]" ) ;
41
42
private static readonly Regex endOfStringWithoutDollar = new Regex ( "^([^\" \\ \\ ]|\\ \\ [\\ \\ \" 0abfnrtv])*[\" ]" ) ;
@@ -50,9 +51,9 @@ public class ExpressionEvaluator
50
51
51
52
52
53
// Depending on OptionInlineNamespacesEvaluationActive. Initialized in constructor
53
- private string CastRegexPattern { get { return $@ "^\(\s*(?<typeName>[{ diactiticsKeywordsRegexPattern } ][{ diactiticsKeywordsRegexPattern } 0-9\{ ( OptionInlineNamespacesEvaluationActive ? @"\." : string . Empty ) } [\]<>]*[?]?)\s*\)"; } }
54
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
55
private Regex instanceCreationWithNewKeywordRegex = null ;
56
+ private string CastRegexPattern { get { return $@ "^\(\s*(?<typeName>[{ diactiticsKeywordsRegexPattern } ][{ diactiticsKeywordsRegexPattern } 0-9\{ ( OptionInlineNamespacesEvaluationActive ? @"\." : string . Empty ) } [\]<>]*[?]?)\s*\)"; } }
56
57
private Regex castRegex = null ;
57
58
58
59
private static readonly string primaryTypesRegexPattern = @"(?<=^|[^" + diactiticsKeywordsRegexPattern + @"])(?<primaryType>object|string|bool[?]?|byte[?]?|char[?]?|decimal[?]?|double[?]?|short[?]?|int[?]?|long[?]?|sbyte[?]?|float[?]?|ushort[?]?|uint[?]?|void)(?=[^a-zA-Z_]|$)" ;
@@ -1348,6 +1349,7 @@ private bool EvaluateCast(string restOfExpression, Stack<object> stack, ref int
1348
1349
if ( castMatch . Success )
1349
1350
{
1350
1351
string typeName = castMatch . Groups [ "typeName" ] . Value ;
1352
+
1351
1353
Type type = GetTypeByFriendlyName ( typeName ) ;
1352
1354
1353
1355
if ( type != null )
@@ -1426,7 +1428,7 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
1426
1428
List < string > constructorArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true ) ;
1427
1429
1428
1430
if ( type == null )
1429
- throw new ExpressionEvaluatorSyntaxErrorException ( $ "type or class { completeName } is unknown") ;
1431
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "Type or class { completeName } { genericTypes } is unknown") ;
1430
1432
1431
1433
List < object > cArgs = constructorArgs . ConvertAll ( arg => Evaluate ( arg ) ) ;
1432
1434
stack . Push ( Activator . CreateInstance ( type , cArgs . ToArray ( ) ) ) ;
@@ -2499,9 +2501,9 @@ private MethodInfo MakeConcreteMethodIfGeneric(MethodInfo methodInfo, string gen
2499
2501
private Type [ ] GetConcreteTypes ( string genericsTypes )
2500
2502
{
2501
2503
return genericsDecodeRegex
2502
- . Matches ( genericsTypes . TrimStart ( ' ' , '<' ) . TrimEnd ( ' ' , '>' ) )
2504
+ . Matches ( genericsEndOnlyOneTrim . Replace ( genericsTypes . TrimStart ( ' ' , '<' ) , "" ) )
2503
2505
. Cast < Match > ( )
2504
- . Select ( match => GetTypeByFriendlyName ( match . Value ) )
2506
+ . Select ( match => GetTypeByFriendlyName ( match . Groups [ "name" ] . Value , match . Groups [ "isgeneric" ] . Value , true ) )
2505
2507
. ToArray ( ) ;
2506
2508
}
2507
2509
@@ -2660,7 +2662,7 @@ private bool DefaultFunctions(string name, List<string> args, out object result)
2660
2662
return functionExists ;
2661
2663
}
2662
2664
2663
- private Type GetTypeByFriendlyName ( string typeName , string genericTypes = "" )
2665
+ private Type GetTypeByFriendlyName ( string typeName , string genericTypes = "" , bool throwExceptionIfNotFound = false )
2664
2666
{
2665
2667
Type result = null ;
2666
2668
try
@@ -2703,11 +2705,18 @@ private Type GetTypeByFriendlyName(string typeName, string genericTypes = "")
2703
2705
}
2704
2706
}
2705
2707
}
2708
+ catch ( ExpressionEvaluatorSyntaxErrorException )
2709
+ {
2710
+ throw ;
2711
+ }
2706
2712
catch { }
2707
2713
2708
2714
if ( result != null && TypesToBlock . Contains ( result ) )
2709
2715
result = null ;
2710
2716
2717
+ if ( result == null && throwExceptionIfNotFound )
2718
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "Type or class { typeName } { genericTypes } is unknown") ;
2719
+
2711
2720
return result ;
2712
2721
}
2713
2722
0 commit comments