@@ -22,7 +22,7 @@ public class ExpressionEvaluator
22
22
private static readonly Regex internalCharRegex = new Regex ( @"^['](\\[']|[^'])*[']" ) ;
23
23
private static readonly Regex castRegex = new Regex ( @"^\(\s*(?<typeName>[a-zA-Z_][a-zA-Z0-9_\.\[\]<>]*[?]?)\s*\)" ) ;
24
24
private static readonly Regex indexingBeginningRegex = new Regex ( @"^[?]?\[" ) ;
25
- private static readonly Regex assignationOperatorRegex = new Regex ( @"^\s*(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>])" ) ;
25
+ private static readonly Regex assignationOrPostFixOperatorRegex = new Regex ( @"^\s*(( ?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>])|(?<postfixOperator>([+][+]|--)(?![a-zA-Z0-9_])) )" ) ;
26
26
private static readonly Regex endOfStringWithDollar = new Regex ( "^[^\" {]*[\" {]" ) ;
27
27
private static readonly Regex endOfStringWithoutDollar = new Regex ( "^[^\" ]*[\" ]" ) ;
28
28
private static readonly Regex endOfStringInterpolationRegex = new Regex ( "^[^}\" ]*[}\" ]" ) ;
@@ -1642,42 +1642,50 @@ private bool EvaluateIndexing(string expr, string s, Stack<object> stack, ref in
1642
1642
ExpressionOperator op = indexingBeginningMatch . Length == 2 ? ExpressionOperator . IndexingWithNullConditional : ExpressionOperator . Indexing ;
1643
1643
dynamic left = stack . Pop ( ) ;
1644
1644
1645
- Match assignationOperatorMatch ;
1645
+ Match assignationOrPostFixOperatorMatch = assignationOrPostFixOperatorRegex . Match ( expr . Substring ( i + 1 ) ) ;
1646
1646
1647
1647
object valueToPush = null ;
1648
1648
1649
- if ( ( assignationOperatorMatch = assignationOperatorRegex . Match ( expr . Substring ( i + 1 ) ) ) . Success )
1649
+ if ( assignationOrPostFixOperatorMatch . Success )
1650
1650
{
1651
- i += assignationOperatorMatch . Length + 1 ;
1651
+ i += assignationOrPostFixOperatorMatch . Length + 1 ;
1652
1652
1653
- if ( stack . Count > 1 )
1654
- throw new ExpressionEvaluatorSyntaxErrorException ( "The left part of an assignation must be a variable, a property or an indexer." ) ;
1653
+ bool postFixoperator = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Success ;
1654
+ string exceptionContext = postFixoperator ? "++ or -- operator" : " an assignation" ;
1655
1655
1656
- string rightExpression = expr . Substring ( i ) ;
1657
- i = expr . Length ;
1656
+ if ( stack . Count > 1 )
1657
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "The left part of { exceptionContext } must be a variable, a property or an indexer." ) ;
1658
1658
1659
1659
if ( op == ExpressionOperator . IndexingWithNullConditional )
1660
- throw new ExpressionEvaluatorSyntaxErrorException ( "Null coalescing is not usable left to an assignation" ) ;
1661
-
1662
- if ( rightExpression . Trim ( ) . Equals ( string . Empty ) )
1663
- throw new ExpressionEvaluatorSyntaxErrorException ( "Right part is missing in assignation" ) ;
1660
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "Null coalescing is not usable left to { exceptionContext } ") ;
1664
1661
1665
- if ( assignationOperatorMatch . Groups [ "assignmentPrefix" ] . Success )
1662
+ if ( postFixoperator )
1663
+ valueToPush = assignationOrPostFixOperatorMatch . Groups [ "postfixOperator" ] . Value . Equals ( "++" ) ? left [ right ] ++ : left [ right ] -- ;
1664
+ else
1666
1665
{
1667
- ExpressionOperator prefixOp = operatorsDictionary [ assignationOperatorMatch . Groups [ "assignmentPrefix" ] . Value ] ;
1666
+ string rightExpression = expr . Substring ( i ) ;
1667
+ i = expr . Length ;
1668
1668
1669
- valueToPush = operatorsEvaluations [ 0 ] [ op ] ( left , right ) ;
1669
+ if ( rightExpression . Trim ( ) . Equals ( string . Empty ) )
1670
+ throw new ExpressionEvaluatorSyntaxErrorException ( "Right part is missing in assignation" ) ;
1670
1671
1671
- valueToPush = operatorsEvaluations . Find ( dict => dict . ContainsKey ( prefixOp ) ) [ prefixOp ] ( valueToPush , Evaluate ( rightExpression ) ) ;
1672
- }
1673
- else
1674
- {
1675
- valueToPush = Evaluate ( rightExpression ) ;
1676
- }
1672
+ if ( assignationOrPostFixOperatorMatch . Groups [ "assignmentPrefix" ] . Success )
1673
+ {
1674
+ ExpressionOperator prefixOp = operatorsDictionary [ assignationOrPostFixOperatorMatch . Groups [ "assignmentPrefix" ] . Value ] ;
1677
1675
1678
- left [ right ] = valueToPush ;
1676
+ valueToPush = operatorsEvaluations [ 0 ] [ op ] ( left , right ) ;
1679
1677
1680
- stack . Clear ( ) ;
1678
+ valueToPush = operatorsEvaluations . Find ( dict => dict . ContainsKey ( prefixOp ) ) [ prefixOp ] ( valueToPush , Evaluate ( rightExpression ) ) ;
1679
+ }
1680
+ else
1681
+ {
1682
+ valueToPush = Evaluate ( rightExpression ) ;
1683
+ }
1684
+
1685
+ left [ right ] = valueToPush ;
1686
+
1687
+ stack . Clear ( ) ;
1688
+ }
1681
1689
}
1682
1690
else
1683
1691
{
@@ -2324,36 +2332,6 @@ private void GetCodeUntilEndOfString(string subExpr, Match stringBeginningMatch,
2324
2332
}
2325
2333
}
2326
2334
2327
- //private string GetCodeUntilEndOfStringInterpolation(string subExpr)
2328
- //{
2329
- // Match endOfStringInterpolationMatch = endOfStringInterpolationRegex.Match(subExpr);
2330
- // StringBuilder result = new StringBuilder();
2331
-
2332
- // if (endOfStringInterpolationMatch.Success)
2333
- // {
2334
- // if (endOfStringInterpolationMatch.Value.EndsWith("}"))
2335
- // {
2336
- // result.Append(endOfStringInterpolationMatch.Value);
2337
- // }
2338
- // else
2339
- // {
2340
- // Match stringBeginningForEndBlockMatch = stringBeginningForEndBlockRegex.Match(endOfStringInterpolationMatch.Value);
2341
-
2342
- // string subString = GetCodeUntilEndOfString(subExpr.Substring(endOfStringInterpolationMatch.Length), stringBeginningForEndBlockMatch);
2343
-
2344
- // result.Append(endOfStringInterpolationMatch.Value);
2345
- // result.Append(subString);
2346
- // result.Append(GetCodeUntilEndOfStringInterpolation(subExpr.Substring(endOfStringInterpolationMatch.Length + subString.Length)));
2347
- // }
2348
- // }
2349
- // else
2350
- // {
2351
- // result.Append(subExpr);
2352
- // }
2353
-
2354
- // return result.ToString();
2355
- //}
2356
-
2357
2335
private string GetCodeUntilEndOfStringInterpolation ( string subExpr )
2358
2336
{
2359
2337
Match endOfStringInterpolationMatch = endOfStringInterpolationRegex . Match ( subExpr ) ;
0 commit comments