Skip to content

Commit a57f48a

Browse files
committed
OptionPropertyOrFieldSetActive
1 parent d472545 commit a57f48a

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@ public bool OptionNewFunctionEvaluationActive
538538
/// </summary>
539539
public bool OptionEvaluateFunctionActive { get; set; } = false;
540540

541+
/// <summary>
542+
/// If <c>true</c> allow to set/modify a property or a field value with (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, ++ or --)
543+
/// If <c>false</c> unactive this functionality
544+
/// By default : true
545+
/// </summary>
546+
public bool OptionPropertyOrFieldSetActive { get; set; } = true;
547+
541548
/// <summary>
542549
/// If <c>true</c> ScriptEvaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
543550
/// By default : false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list)
@@ -1179,11 +1186,6 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
11791186

11801187
if (varFuncMatch.Groups["isfunction"].Success)
11811188
{
1182-
//if (varFuncMatch.Groups["assignationOperator"].Success)
1183-
//{
1184-
// throw new ExpressionEvaluatorSyntaxErrorException("The left part of an assignation must be a variable, a property or an indexer");
1185-
//}
1186-
11871189
List<string> funcArgs = GetExpressionsBetweenParenthis(expr, ref i, true);
11881190
if (varFuncMatch.Groups["inObject"].Success)
11891191
{
@@ -1358,38 +1360,45 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
13581360

13591361
stack.Push(varValue);
13601362

1361-
if (inScript && varFuncMatch.Groups["assignationOperator"].Success)
1363+
if (OptionPropertyOrFieldSetActive)
13621364
{
1363-
if (stack.Count > 1)
1364-
throw new ExpressionEvaluatorSyntaxErrorException();
1365+
if (varFuncMatch.Groups["assignationOperator"].Success)
1366+
{
1367+
if (stack.Count > 1)
1368+
throw new ExpressionEvaluatorSyntaxErrorException();
13651369

1366-
string rightExpression = expr.Substring(i);
1367-
i = expr.Length;
1370+
string rightExpression = expr.Substring(i);
1371+
i = expr.Length;
13681372

1369-
if (rightExpression.Trim().Equals(string.Empty))
1370-
throw new ExpressionEvaluatorSyntaxErrorException("Right part is missing in assignation");
1373+
if (rightExpression.Trim().Equals(string.Empty))
1374+
throw new ExpressionEvaluatorSyntaxErrorException("Right part is missing in assignation");
13711375

1372-
if (varFuncMatch.Groups["assignmentPrefix"].Success)
1373-
{
1374-
ExpressionOperator op = operatorsDictionary[varFuncMatch.Groups["assignmentPrefix"].Value];
1376+
if (varFuncMatch.Groups["assignmentPrefix"].Success)
1377+
{
1378+
ExpressionOperator op = operatorsDictionary[varFuncMatch.Groups["assignmentPrefix"].Value];
1379+
1380+
varValue = operatorsEvaluations.Find(dict => dict.ContainsKey(op))[op](varValue, Evaluate(rightExpression));
1381+
}
1382+
else
1383+
{
1384+
varValue = Evaluate(rightExpression);
1385+
}
13751386

1376-
varValue = operatorsEvaluations.Find(dict => dict.ContainsKey(op))[op](varValue, Evaluate(rightExpression));
1387+
stack.Clear();
1388+
stack.Push(varValue);
13771389
}
1390+
else if (varFuncMatch.Groups["postfixOperator"].Success)
1391+
varValue = varFuncMatch.Groups["postfixOperator"].Value.Equals("++") ? varValue + 1 : varValue - 1;
13781392
else
1379-
{
1380-
varValue = Evaluate(rightExpression);
1381-
}
1393+
assign = false;
13821394

1383-
stack.Clear();
1384-
stack.Push(varValue);
1395+
if (assign)
1396+
member.SetValue(obj, varValue);
13851397
}
1386-
else if (varFuncMatch.Groups["postfixOperator"].Success)
1387-
varValue = varFuncMatch.Groups["postfixOperator"].Value.Equals("++") ? varValue + 1 : varValue - 1;
1388-
else
1389-
assign = false;
1390-
1391-
if(assign)
1392-
member.SetValue(obj, varValue);
1398+
else if(varFuncMatch.Groups["assignationOperator"].Success)
1399+
i -= varFuncMatch.Groups["assignationOperator"].Length;
1400+
else if(varFuncMatch.Groups["postfixOperator"].Success)
1401+
i -= varFuncMatch.Groups["postfixOperator"].Length;
13931402
}
13941403
}
13951404
}

0 commit comments

Comments
 (0)