Skip to content

Commit 1fc06f2

Browse files
author
Sébastien Geiser
committed
More virtual and partial for extensibility
+ Cleaning and Refactoring + a bug correction
1 parent f27cd11 commit 1fc06f2

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public partial class ExpressionEvaluator
3636
protected static readonly Regex varOrFunctionRegEx = new Regex($@"^((?<sign>[+-])|(?<prefixOperator>[+][+]|--)|(?<varKeyword>var)\s+|(?<dynamicKeyword>dynamic)\s+|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*))(?>\s*)((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![{ diactiticsKeywordsRegexPattern}0-9]))|((?<isgeneric>[<](?>([{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*)|(?>\s+)|[,\.])+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
3737

3838
protected const string numberRegexOrigPattern = @"^(?<sign>[+-])?([0-9][0-9_{1}]*[0-9]|\d)(?<hasdecimal>{0}?([0-9][0-9_]*[0-9]|\d)(e[+-]?([0-9][0-9_]*[0-9]|\d))?)?(?<type>ul|[fdulm])?";
39-
protected string numberRegexPattern = null;
39+
protected string numberRegexPattern;
4040

4141
protected static readonly Regex otherBasesNumberRegex = new Regex("^(?<sign>[+-])?(?<value>0(?<type>x)([0-9a-f][0-9a-f_]*[0-9a-f]|[0-9a-f])|0(?<type>b)([01][01_]*[01]|[01]))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
4242
protected static readonly Regex stringBeginningRegex = new Regex("^(?<interpolated>[$])?(?<escaped>[@])?[\"]", RegexOptions.Compiled);
@@ -773,7 +773,7 @@ public bool OptionNewFunctionEvaluationActive
773773
/// If <c>ThrowSyntaxException</c> a exception is throw if no return keyword is met.
774774
/// By default : ReturnAutomaticallyLastEvaluatedExpression;
775775
/// </summary>
776-
public OptionOnNoReturnKeywordFoundInScriptAction OptionOnNoReturnKeywordFoundInScriptAction { get; set; } = OptionOnNoReturnKeywordFoundInScriptAction.ReturnAutomaticallyLastEvaluatedExpression;
776+
public OptionOnNoReturnKeywordFoundInScriptAction OptionOnNoReturnKeywordFoundInScriptAction { get; set; }
777777

778778
/// <summary>
779779
/// If <c>true</c> ScriptEvaluate need to have a semicolon [;] after each expression.
@@ -939,7 +939,7 @@ protected virtual void Init()
939939
/// <typeparam name="T">The type in which to cast the result of the expression</typeparam>
940940
/// <param name="script">the script to evaluate</param>
941941
/// <returns>The result of the last evaluated expression</returns>
942-
public T ScriptEvaluate<T>(string script)
942+
public virtual T ScriptEvaluate<T>(string script)
943943
{
944944
return (T)ScriptEvaluate(script);
945945
}
@@ -1546,8 +1546,8 @@ protected virtual bool EvaluateNumber(string expression, Stack<object> stack, re
15461546
}
15471547
else if (numberMatch.Success
15481548
&& (!numberMatch.Groups["sign"].Success
1549-
|| stack.Count == 0
1550-
|| stack.Peek() is ExpressionOperator))
1549+
|| stack.Count == 0
1550+
|| stack.Peek() is ExpressionOperator))
15511551
{
15521552
i += numberMatch.Length;
15531553
i--;
@@ -1562,16 +1562,13 @@ protected virtual bool EvaluateNumber(string expression, Stack<object> stack, re
15621562
stack.Push(parseFunc(numberNoType, CultureInfoForNumberParsing));
15631563
}
15641564
}
1565+
else if (OptionForceIntegerNumbersEvaluationsAsDoubleByDefault || numberMatch.Groups["hasdecimal"].Success)
1566+
{
1567+
stack.Push(double.Parse(numberMatch.Value.Replace("_", ""), NumberStyles.Any, CultureInfoForNumberParsing));
1568+
}
15651569
else
15661570
{
1567-
if (OptionForceIntegerNumbersEvaluationsAsDoubleByDefault || numberMatch.Groups["hasdecimal"].Success)
1568-
{
1569-
stack.Push(double.Parse(numberMatch.Value.Replace("_", ""), NumberStyles.Any, CultureInfoForNumberParsing));
1570-
}
1571-
else
1572-
{
1573-
stack.Push(int.Parse(numberMatch.Value.Replace("_", ""), NumberStyles.Any, CultureInfoForNumberParsing));
1574-
}
1571+
stack.Push(int.Parse(numberMatch.Value.Replace("_", ""), NumberStyles.Any, CultureInfoForNumberParsing));
15751572
}
15761573

15771574
return true;
@@ -2984,7 +2981,7 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
29842981
&& modifiedArgs[a] is InternalDelegate)
29852982
{
29862983
InternalDelegate led = modifiedArgs[a] as InternalDelegate;
2987-
modifiedArgs[a] = new Converter<object, object>(o => (led(new object[] { o })));
2984+
modifiedArgs[a] = new Converter<object, object>(o => led(new object[] { o }));
29882985
}
29892986
else
29902987
{
@@ -3118,7 +3115,7 @@ protected virtual string GetScriptBetweenCurlyBrackets(string parentScript, ref
31183115
return currentScript;
31193116
}
31203117

3121-
protected List<string> GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(string expression, ref int i, bool checkSeparator, string separator = ",", string startChar = "(", string endChar = ")")
3118+
protected virtual List<string> GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(string expression, ref int i, bool checkSeparator, string separator = ",", string startChar = "(", string endChar = ")")
31223119
{
31233120
List<string> expressionsList = new List<string>();
31243121

@@ -3223,7 +3220,9 @@ protected virtual bool DefaultFunctions(string name, List<string> args, out obje
32233220
}
32243221
else if (OptionScriptEvaluateFunctionActive && name.Equals("ScriptEvaluate", StringComparisonForCasing))
32253222
{
3223+
bool oldInScript = inScript;
32263224
result = ScriptEvaluate((string)Evaluate(args[0]));
3225+
inScript = oldInScript;
32273226
}
32283227
else
32293228
{
@@ -3700,7 +3699,7 @@ public static partial class OperatorsEvaluationsExtensions
37003699
{
37013700
public static IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> Copy(this IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>> operatorsEvaluations)
37023701
{
3703-
return (IList<IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>>)operatorsEvaluations
3702+
return operatorsEvaluations
37043703
.Select(dic => (IDictionary<ExpressionOperator, Func<dynamic, dynamic, object>>)new Dictionary<ExpressionOperator, Func<dynamic, dynamic, object>>(dic))
37053704
.ToList();
37063705
}
@@ -3790,7 +3789,7 @@ public partial class StronglyTypedVariable
37903789

37913790
public partial class ExpressionEvaluatorSyntaxErrorException : Exception
37923791
{
3793-
public ExpressionEvaluatorSyntaxErrorException() : base()
3792+
public ExpressionEvaluatorSyntaxErrorException()
37943793
{ }
37953794

37963795
public ExpressionEvaluatorSyntaxErrorException(string message) : base(message)
@@ -3802,7 +3801,7 @@ public ExpressionEvaluatorSyntaxErrorException(string message, Exception innerEx
38023801

38033802
public partial class ExpressionEvaluatorSecurityException : Exception
38043803
{
3805-
public ExpressionEvaluatorSecurityException() : base()
3804+
public ExpressionEvaluatorSecurityException()
38063805
{ }
38073806

38083807
public ExpressionEvaluatorSecurityException(string message) : base(message)
@@ -3814,8 +3813,8 @@ public ExpressionEvaluatorSecurityException(string message, Exception innerExcep
38143813

38153814
public partial class VariableEvaluationEventArg : EventArgs
38163815
{
3817-
private readonly Func<string, Type[]> evaluateGenericTypes = null;
3818-
private readonly string genericTypes = null;
3816+
private readonly Func<string, Type[]> evaluateGenericTypes;
3817+
private readonly string genericTypes;
38193818

38203819
/// <summary>
38213820
/// Constructor of the VariableEvaluationEventArg
@@ -3853,13 +3852,13 @@ public object Value
38533852
/// <summary>
38543853
/// if <c>true</c> the variable is affected, if <c>false</c> it means that the variable does not exist.
38553854
/// </summary>
3856-
public bool HasValue { get; set; } = false;
3855+
public bool HasValue { get; set; }
38573856

38583857
/// <summary>
38593858
/// In the case of on the fly instance property definition the instance of the object on which this Property is called.
38603859
/// Otherwise is set to null.
38613860
/// </summary>
3862-
public object This { get; } = null;
3861+
public object This { get; }
38633862

38643863
/// <summary>
38653864
/// A reference on the current expression evaluator.
@@ -3888,7 +3887,7 @@ public Type[] EvaluateGenericTypes()
38883887
}
38893888
}
38903889

3891-
public class VariablePreEvaluationEventArg : VariableEvaluationEventArg
3890+
public partial class VariablePreEvaluationEventArg : VariableEvaluationEventArg
38923891
{
38933892
public VariablePreEvaluationEventArg(string name, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
38943893
: base(name, evaluator, onInstance, genericTypes, evaluateGenericTypes)
@@ -3897,14 +3896,14 @@ public VariablePreEvaluationEventArg(string name, ExpressionEvaluator evaluator
38973896
/// <summary>
38983897
/// If set to true cancel the evaluation of the current variable, field or property and throw an exception it does not exists
38993898
/// </summary>
3900-
public bool CancelEvaluation { get; set; } = false;
3899+
public bool CancelEvaluation { get; set; }
39013900
}
39023901

39033902
public partial class FunctionEvaluationEventArg : EventArgs
39043903
{
3905-
private readonly Func<string, object> evaluateFunc = null;
3906-
private readonly Func<string, Type[]> evaluateGenericTypes = null;
3907-
private readonly string genericTypes = null;
3904+
private readonly Func<string, object> evaluateFunc;
3905+
private readonly Func<string, Type[]> evaluateGenericTypes;
3906+
private readonly string genericTypes;
39083907

39093908
public FunctionEvaluationEventArg(string name, Func<string, object> evaluateFunc, List<string> args = null, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
39103909
{
@@ -3957,7 +3956,7 @@ public T EvaluateArg<T>(int index)
39573956
/// </summary>
39583957
public string Name { get; }
39593958

3960-
private object returnValue = null;
3959+
private object returnValue;
39613960

39623961
/// <summary>
39633962
/// To set the return value of the function
@@ -3975,13 +3974,13 @@ public object Value
39753974
/// <summary>
39763975
/// if <c>true</c> the function evaluation has been done, if <c>false</c> it means that the function does not exist.
39773976
/// </summary>
3978-
public bool FunctionReturnedValue { get; set; } = false;
3977+
public bool FunctionReturnedValue { get; set; }
39793978

39803979
/// <summary>
39813980
/// In the case of on the fly instance method definition the instance of the object on which this method (function) is called.
39823981
/// Otherwise is set to null.
39833982
/// </summary>
3984-
public object This { get; } = null;
3983+
public object This { get; }
39853984

39863985
/// <summary>
39873986
/// A reference on the current expression evaluator.
@@ -4010,7 +4009,7 @@ public Type[] EvaluateGenericTypes()
40104009
}
40114010
}
40124011

4013-
public class FunctionPreEvaluationEventArg : FunctionEvaluationEventArg
4012+
public partial class FunctionPreEvaluationEventArg : FunctionEvaluationEventArg
40144013
{
40154014
public FunctionPreEvaluationEventArg(string name, Func<string, object> evaluateFunc, List<string> args = null, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
40164015
: base(name, evaluateFunc, args, evaluator, onInstance, genericTypes, evaluateGenericTypes)
@@ -4019,7 +4018,7 @@ public FunctionPreEvaluationEventArg(string name, Func<string, object> evaluateF
40194018
/// <summary>
40204019
/// If set to true cancel the evaluation of the current function or method and throw an exception that the function does not exists
40214020
/// </summary>
4022-
public bool CancelEvaluation { get; set; } = false;
4021+
public bool CancelEvaluation { get; set; }
40234022
}
40244023

40254024
#endregion

0 commit comments

Comments
 (0)