11/******************************************************************************************************
22 Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
33 Version : 1.3.0.1
4- (if last digit not zero version is an intermediate version and can be unstable)
4+ (if last digit is not a zero, the version is an intermediate version and can be unstable)
55
66 Author : Coding Seb
77 Licence : MIT (https://github.com/codingseb/ExpressionEvaluator/blob/master/LICENSE.md)
@@ -43,7 +43,7 @@ public class ExpressionEvaluator
4343 private static readonly Regex lambdaExpressionRegex = new Regex ( @"^\s*(?<args>(\s*[(]\s*([" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*\s*([,]\s*[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*\s*)*)?[)])|[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*)\s*=>(?<expression>.*)$" , RegexOptions . Singleline ) ;
4444 private static readonly Regex lambdaArgRegex = new Regex ( @"[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*" ) ;
4545
46- private static readonly string instanceCreationWithNewKeywordRegexPattern = @"^new\s+(?<name>[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9.]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?( ?<isfunction>[(])?" ;
46+ private static readonly string instanceCreationWithNewKeywordRegexPattern = @"^new\s+(?<name>[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9.]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?\s*(( ?<isfunction>[(])|(?<isArray>\[) )?" ;
4747 private Regex instanceCreationWithNewKeywordRegex = new Regex ( instanceCreationWithNewKeywordRegexPattern ) ;
4848 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_]|$)" ;
4949 private Regex primaryTypesRegex = new Regex ( primaryTypesRegexPattern ) ;
@@ -803,7 +803,7 @@ bool TryParseStringAndParenthisAndCurlyBrackets(ref int index)
803803 else if ( script [ index ] == '(' )
804804 {
805805 index ++ ;
806- GetExpressionsBetweenParenthis ( script , ref index , false ) ;
806+ GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( script , ref index , false ) ;
807807 }
808808 else if ( script [ index ] == '{' )
809809 {
@@ -906,7 +906,7 @@ void ExecuteBlocksStacks()
906906 {
907907 i += blockKeywordsBeginingMatch . Success ? blockKeywordsBeginingMatch . Length : blockKeywordsWithoutParenthesesBeginningMatch . Length ;
908908 string keyword = blockKeywordsBeginingMatch . Success ? blockKeywordsBeginingMatch . Groups [ "keyword" ] . Value . Replace ( " " , "" ) . Replace ( "\t " , "" ) : ( blockKeywordsWithoutParenthesesBeginningMatch ? . Groups [ "keyword" ] . Value ?? string . Empty ) ;
909- List < string > keywordAttributes = blockKeywordsBeginingMatch . Success ? GetExpressionsBetweenParenthis ( script , ref i , true , ";" ) : null ;
909+ List < string > keywordAttributes = blockKeywordsBeginingMatch . Success ? GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( script , ref i , true , ";" ) : null ;
910910
911911 if ( blockKeywordsBeginingMatch . Success )
912912 i ++ ;
@@ -1021,7 +1021,7 @@ void ExecuteBlocksStacks()
10211021 && blockKeywordsBeginingMatch . Groups [ "keyword" ] . Value . ManageCasing ( OptionCaseSensitiveEvaluationActive ) . Equals ( "while" ) )
10221022 {
10231023 i += blockKeywordsBeginingMatch . Length ;
1024- keywordAttributes = GetExpressionsBetweenParenthis ( script , ref i , true , ";" ) ;
1024+ keywordAttributes = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( script , ref i , true , ";" ) ;
10251025
10261026 i ++ ;
10271027
@@ -1245,7 +1245,7 @@ public object Evaluate(string expression)
12451245 else if ( s2 . Equals ( "(" ) )
12461246 {
12471247 j ++ ;
1248- GetExpressionsBetweenParenthis ( restOfExpression , ref j , false ) ;
1248+ GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( restOfExpression , ref j , false ) ;
12491249 }
12501250 else if ( s2 . Equals ( ":" ) )
12511251 {
@@ -1348,21 +1348,39 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
13481348 || stack . Peek ( ) is ExpressionOperator ) )
13491349 {
13501350 string completeName = instanceCreationMatch . Groups [ "name" ] . Value ;
1351+ Type type = GetTypeByFriendlyName ( completeName , true ) ;
13511352
13521353 i += instanceCreationMatch . Length ;
13531354
1354- if ( ! instanceCreationMatch . Groups [ "isfunction" ] . Success )
1355- throw new ExpressionEvaluatorSyntaxErrorException ( $ "No '(' found after { instanceCreationMatch . Value } ") ;
1355+ if ( instanceCreationMatch . Groups [ "isfunction" ] . Success )
1356+ {
1357+ List < string > constructorArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true ) ;
13561358
1357- List < string > constructorArgs = GetExpressionsBetweenParenthis ( expr , ref i , true ) ;
1359+ if ( type == null )
1360+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "type or class { completeName } is unknown") ;
13581361
1359- Type type = GetTypeByFriendlyName ( completeName , true ) ;
1362+ List < object > cArgs = constructorArgs . ConvertAll ( arg => Evaluate ( arg ) ) ;
1363+ stack . Push ( Activator . CreateInstance ( type , cArgs . ToArray ( ) ) ) ;
1364+ }
1365+ else if ( instanceCreationMatch . Groups [ "isArray" ] . Success )
1366+ {
1367+ List < string > arrayArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true , "," , "[" , "]" ) ;
1368+
1369+ if ( arrayArgs . Count > 0 )
1370+ {
1371+ stack . Push ( Array . CreateInstance ( type , arrayArgs . ConvertAll ( subExpression => ( int ) Evaluate ( subExpression ) ) . ToArray ( ) ) ) ;
1372+ }
1373+ else
1374+ {
1375+ //int[,] test = new int[4, 2];
13601376
1361- if ( type == null )
1362- throw new ExpressionEvaluatorSyntaxErrorException ( $ "type or class { completeName } is unknown") ;
1377+ //Array.CreateInstance()
13631378
1364- List < object > cArgs = constructorArgs . ConvertAll ( arg => Evaluate ( arg ) ) ;
1365- stack . Push ( Activator . CreateInstance ( type , cArgs . ToArray ( ) ) ) ;
1379+ //test[0, 1] = 3;
1380+ }
1381+ }
1382+ else
1383+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "A new expression requires that type be followed by (), [], or { "{}" } (Check : { instanceCreationMatch . Value } )") ;
13661384
13671385 return true ;
13681386 }
@@ -1388,7 +1406,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
13881406
13891407 if ( varFuncMatch . Groups [ "isfunction" ] . Success )
13901408 {
1391- List < string > funcArgs = GetExpressionsBetweenParenthis ( expr , ref i , true ) ;
1409+ List < string > funcArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true ) ;
13921410 if ( varFuncMatch . Groups [ "inObject" ] . Success )
13931411 {
13941412 if ( stack . Count == 0 || stack . Peek ( ) is ExpressionOperator )
@@ -1833,7 +1851,7 @@ private bool EvaluateParenthis(string expr, string s, Stack<object> stack, ref i
18331851
18341852 if ( stack . Count > 0 && stack . Peek ( ) is InternalDelegate )
18351853 {
1836- List < string > expressionsInParenthis = GetExpressionsBetweenParenthis ( expr , ref i , true ) ;
1854+ List < string > expressionsInParenthis = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true ) ;
18371855
18381856 InternalDelegate lambdaDelegate = stack . Pop ( ) as InternalDelegate ;
18391857
@@ -1843,7 +1861,7 @@ private bool EvaluateParenthis(string expr, string s, Stack<object> stack, ref i
18431861 {
18441862 CorrectStackWithUnaryPlusOrMinusBeforeParenthisIfNecessary ( stack ) ;
18451863
1846- List < string > expressionsInParenthis = GetExpressionsBetweenParenthis ( expr , ref i , false ) ;
1864+ List < string > expressionsInParenthis = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , false ) ;
18471865
18481866 stack . Push ( Evaluate ( expressionsInParenthis [ 0 ] ) ) ;
18491867 }
@@ -2418,7 +2436,7 @@ string GetScriptBetweenCurlyBrackets(string parentScript, ref int index)
24182436 return currentScript ;
24192437 }
24202438
2421- private List < string > GetExpressionsBetweenParenthis ( string expr , ref int i , bool checkSeparator , string separator = "," )
2439+ private List < string > GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( string expr , ref int i , bool checkSeparator , string separator = "," , string startChar = "(" , string endChar = ") ")
24222440 {
24232441 List < string > expressionsList = new List < string > ( ) ;
24242442
@@ -2446,9 +2464,9 @@ private List<string> GetExpressionsBetweenParenthis(string expr, ref int i, bool
24462464 {
24472465 s = expr . Substring ( i , 1 ) ;
24482466
2449- if ( s . Equals ( "(" ) ) bracketCount ++ ;
2467+ if ( s . Equals ( startChar ) ) bracketCount ++ ;
24502468
2451- if ( s . Equals ( ")" ) )
2469+ if ( s . Equals ( endChar ) )
24522470 {
24532471 bracketCount -- ;
24542472 if ( bracketCount == 0 )
@@ -2472,7 +2490,7 @@ private List<string> GetExpressionsBetweenParenthis(string expr, ref int i, bool
24722490 if ( bracketCount > 0 )
24732491 {
24742492 string beVerb = bracketCount == 1 ? "is" : "are" ;
2475- throw new Exception ( $ "{ bracketCount } ') ' character { beVerb } missing in expression : [{ expr } ]") ;
2493+ throw new Exception ( $ "{ bracketCount } '{ endChar } ' character { beVerb } missing in expression : [{ expr } ]") ;
24762494 }
24772495
24782496 return expressionsList ;
0 commit comments