Skip to content

Commit c3ab0f5

Browse files
committed
in keyword ok for copy paste with real C#
1 parent e061531 commit c3ab0f5

File tree

3 files changed

+113
-9
lines changed

3 files changed

+113
-9
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

+90
Original file line numberDiff line numberDiff line change
@@ -2371,5 +2371,95 @@ public void MethodWithRefParameterInlineVarDeclarationAndInit()
23712371
}
23722372

23732373
#endregion
2374+
2375+
#region Method with in parameter
2376+
2377+
[Test]
2378+
[Category("InKeywordMethod")]
2379+
public void MethodWithInParameter()
2380+
{
2381+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2382+
2383+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2384+
2385+
evaluator.Variables["x"] = 3;
2386+
2387+
evaluator.Evaluate("refObj.AddOneOnIn(in x)")
2388+
.ShouldBe(4);
2389+
2390+
evaluator.Variables.ShouldContainKey("x");
2391+
evaluator.Variables["x"]
2392+
.ShouldBeOfType<int>()
2393+
.ShouldBe(3);
2394+
}
2395+
2396+
[Test]
2397+
[Category("InKeywordMethod")]
2398+
public void MethodWithInParameterWithoutExistingVariable()
2399+
{
2400+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2401+
2402+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2403+
2404+
evaluator.Evaluate("refObj.AddOneOnIn(in x)")
2405+
.ShouldBe(1);
2406+
2407+
evaluator.Variables.ShouldContainKey("x");
2408+
evaluator.Variables["x"]
2409+
.ShouldBeNull();
2410+
}
2411+
2412+
[Test]
2413+
[Category("InKeywordMethod")]
2414+
public void MethodWithInParameterInlineVarDeclaration()
2415+
{
2416+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2417+
2418+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2419+
2420+
evaluator.Evaluate("refObj.AddOneOnIn(in int x)")
2421+
.ShouldBe(1);
2422+
2423+
evaluator.Variables.ShouldContainKey("x");
2424+
evaluator.Variables["x"]
2425+
.ShouldBeOfType<StronglyTypedVariable>()
2426+
.Value.ShouldBe(0);
2427+
}
2428+
2429+
[Test]
2430+
[Category("InKeywordMethod")]
2431+
public void MethodWithInParameterWithoutExistingVariableAndInit()
2432+
{
2433+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2434+
2435+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2436+
2437+
evaluator.Evaluate("refObj.AddOneOnIn(in x = 4)")
2438+
.ShouldBe(5);
2439+
2440+
evaluator.Variables.ShouldContainKey("x");
2441+
evaluator.Variables["x"]
2442+
.ShouldBeOfType<int>()
2443+
.ShouldBe(4);
2444+
}
2445+
2446+
[Test]
2447+
[Category("InKeywordMethod")]
2448+
public void MethodWithInParameterInlineVarDeclarationAndInit()
2449+
{
2450+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2451+
2452+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2453+
2454+
evaluator.Evaluate("refObj.AddOneOnIn(in int x = 6)")
2455+
.ShouldBe(7);
2456+
2457+
evaluator.Variables.ShouldContainKey("x");
2458+
evaluator.Variables["x"]
2459+
.ShouldBeOfType<StronglyTypedVariable>()
2460+
.Value.ShouldBe(6);
2461+
}
2462+
2463+
#endregion
23742464
}
23752465
}

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/MethodArgKeywordClass.cs

+5
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ public void AddOneOnRef(ref int i)
66
{
77
i++;
88
}
9+
10+
public int AddOneOnIn(in int i)
11+
{
12+
return i + 1;
13+
}
914
}
1015
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+18-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public partial class ExpressionEvaluator
5353
protected static readonly Regex lambdaExpressionRegex = new Regex(@"^(?>\s*)(?<args>((?>\s*)[(](?>\s*)([\p{L}_](?>[\p{L}_0-9]*)(?>\s*)([,](?>\s*)[\p{L}_][\p{L}_0-9]*(?>\s*))*)?[)])|[\p{L}_](?>[\p{L}_0-9]*))(?>\s*)=>(?<expression>.*)$", RegexOptions.Singleline | RegexOptions.Compiled);
5454
protected static readonly Regex lambdaArgRegex = new Regex(@"[\p{L}_](?>[\p{L}_0-9]*)", RegexOptions.Compiled);
5555
protected static readonly Regex initInNewBeginningRegex = new Regex(@"^(?>\s*){", RegexOptions.Compiled);
56-
protected static readonly Regex functionArgKeywordsRegex = new Regex(@"^\s*(?<keyword>out|ref)\s+((?<typeName>[\p{L}_][\p{L}_0-9\.\[\]<>]*[?]?)\s+(?=[\p{L}_]))?(?<toEval>(?<varName>[\p{L}_](?>[\p{L}_0-9]*))\s*(=.*)?)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
56+
protected static readonly Regex functionArgKeywordsRegex = new Regex(@"^\s*(?<keyword>out|ref|in)\s+((?<typeName>[\p{L}_][\p{L}_0-9\.\[\]<>]*[?]?)\s+(?=[\p{L}_]))?(?<toEval>(?<varName>[\p{L}_](?>[\p{L}_0-9]*))\s*(=.*)?)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
5757

5858
// Depending on OptionInlineNamespacesEvaluationActive. Initialized in constructor
5959
protected string InstanceCreationWithNewKeywordRegexPattern { get { return @"^new(?>\s*)((?<isAnonymous>[{{])|((?<name>[\p{L}_][\p{L}_0-9"+ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) + @"]*)(?>\s*)(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?>\s*)((?<isfunction>[(])|(?<isArray>\[)|(?<isInit>[{{]))?))"; } }
@@ -1896,7 +1896,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
18961896
else
18971897
{
18981898
int argIndex = 0;
1899-
List<OutOrRefArg> outOrRefArgs = new List<OutOrRefArg>();
1899+
List<ArgKeywordsEncaps> argsWithKeywords = new List<ArgKeywordsEncaps>();
19001900

19011901
List<object> oArgs = funcArgs.ConvertAll(arg =>
19021902
{
@@ -1905,18 +1905,24 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
19051905

19061906
if (functionArgKeywordsMatch.Success)
19071907
{
1908-
OutOrRefArg outOrRefArg = new OutOrRefArg() { Index = argIndex, VariableName = functionArgKeywordsMatch.Groups["varName"].Value };
1909-
outOrRefArgs.Add(outOrRefArg);
1908+
ArgKeywordsEncaps argKeywordEncaps = new ArgKeywordsEncaps()
1909+
{
1910+
Index = argIndex,
1911+
Keyword = functionArgKeywordsMatch.Groups["keyword"].Value,
1912+
VariableName = functionArgKeywordsMatch.Groups["varName"].Value
1913+
};
1914+
1915+
argsWithKeywords.Add(argKeywordEncaps);
19101916

19111917
if (functionArgKeywordsMatch.Groups["typeName"].Success)
19121918
{
19131919
Type fixedType = ((ClassOrEnumType)Evaluate(functionArgKeywordsMatch.Groups["typeName"].Value)).Type;
19141920

1915-
variables[outOrRefArg.VariableName] = new StronglyTypedVariable() { Type = fixedType, Value = GetDefaultValueOfType(fixedType) };
1921+
variables[argKeywordEncaps.VariableName] = new StronglyTypedVariable() { Type = fixedType, Value = GetDefaultValueOfType(fixedType) };
19161922
}
1917-
else if (!variables.ContainsKey(outOrRefArg.VariableName))
1923+
else if (!variables.ContainsKey(argKeywordEncaps.VariableName))
19181924
{
1919-
variables[outOrRefArg.VariableName] = null;
1925+
variables[argKeywordEncaps.VariableName] = null;
19201926
}
19211927

19221928
argValue = Evaluate(functionArgKeywordsMatch.Groups["toEval"].Value);
@@ -1979,7 +1985,9 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
19791985
{
19801986
object[] argsArray = oArgs.ToArray();
19811987
stack.Push(methodInfo.Invoke(isExtention ? null : obj, argsArray));
1982-
outOrRefArgs.ForEach(outOrRefArg => AssignVariable(outOrRefArg.VariableName, argsArray[outOrRefArg.Index]));
1988+
argsWithKeywords
1989+
.FindAll(argWithKeyword => argWithKeyword.Keyword.Equals("out", StringComparisonForCasing) || argWithKeyword.Keyword.Equals("ref", StringComparisonForCasing))
1990+
.ForEach(outOrRefArg => AssignVariable(outOrRefArg.VariableName, argsArray[outOrRefArg.Index]));
19831991
}
19841992
else if (objType.GetProperty(varFuncName, StaticBindingFlag) is PropertyInfo staticPropertyInfo
19851993
&& (staticPropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || staticPropertyInfo.PropertyType == typeof(Delegate))
@@ -3695,9 +3703,10 @@ public void AssignValue()
36953703
protected class NullConditionalNullValue
36963704
{ }
36973705

3698-
protected class OutOrRefArg
3706+
protected class ArgKeywordsEncaps
36993707
{
37003708
public int Index { get; set; }
3709+
public string Keyword { get; set; }
37013710
public string VariableName { get; set; }
37023711
}
37033712

0 commit comments

Comments
 (0)