Skip to content

Commit

Permalink
#94 simplified collection syntax part looks OK
Browse files Browse the repository at this point in the history
  • Loading branch information
codingseb committed May 25, 2021
1 parent b494a15 commit 83b8f54
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
14 changes: 14 additions & 0 deletions CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,20 @@ ExpressionEvaluator evaluatorForMethodArgs()
.SetCategory("Anonymous")
.SetCategory("InitializerAllowStringForProperties");

yield return new TestCaseData(new ExpressionEvaluator()
{
OptionsSyntaxRules = new ExpressionEvaluator.SyntaxRules()
{
AllowSimplifiedCollectionSyntax = true
}
}
, "[3,2,6,10].Length"
, null)
.Returns(4)
.SetCategory("ExpandoObject")
.SetCategory("Anonymous")
.SetCategory("InitializerAllowStringForProperties");

#endregion

#region Issues/Bug resolution
Expand Down
58 changes: 57 additions & 1 deletion CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,32 @@ public string[] StatementTerminalPunctuators
/// </summary>
public bool InitializerAllowStringForProperties { get; set; }

/// <summary>
/// If <c>true</c> allow a simplified syntax to create a collection<para/>
/// Example : <c>var collection = [1, 2, 3]; // collection[1] -> 2</c><para/>
/// The type of the collection that is created with this syntax is defined with the linked option
/// If <c>false</c> simplified syntax for coolection is unactive
/// <para>Default value : <c>false</c></para>
/// </summary>
public bool AllowSimplifiedCollectionSyntax { get; set; }

/// <summary>
/// Specify Which type of collection is create when symplified collection syntax is used.<para/>
/// <list type="table">
/// <item>
/// <term>Array</term>
/// <description><c>object[]</c></description>
/// </item>
/// <item>
/// <term>List</term>
/// <description><c>List&lt;object&gt;</c></description>
/// </item>
///</list>
/// Only effective if AllowSimplifiedCollectionSyntax is <c>true</c>.<para />
/// <para>Default value : <c>Array</c></para>
/// </summary>
public SimplifiedCollectionMode SimplifiedCollectionMode { get; set; }

/// <summary>
/// The separator that separate each expression in block keyword's head
/// <para>Default value : <c>";"</c></para>
Expand Down Expand Up @@ -1800,6 +1826,7 @@ public T Evaluate<T>(string expression)
EvaluateOperators,
EvaluateChar,
EvaluateParenthis,
EvaluateSimplifiedCollection,
EvaluateIndexing,
EvaluateString,
EvaluateTernaryConditionalOperator,
Expand Down Expand Up @@ -2182,7 +2209,7 @@ void InitSimpleObject(object element, List<string> initArgs)
}
trimmedSubExpr = trimmedSubExpr.Substring(pos).TrimStart();
string separator = OptionsSyntaxRules.InitializerPropertyValueSeparators.FirstOrDefault(sep => trimmedSubExpr.StartsWith(sep, StringComparisonForCasing));
string separator = Array.Find(OptionsSyntaxRules.InitializerPropertyValueSeparators, sep => trimmedSubExpr.StartsWith(sep, StringComparisonForCasing));
if(separator != null)
{
Expand Down Expand Up @@ -3213,6 +3240,29 @@ protected virtual void CorrectStackWithUnaryPlusOrMinusBeforeParenthisIfNecessar
}
}

protected virtual bool EvaluateSimplifiedCollection(string expression, Stack<object> stack, ref int i)
{
if(OptionsSyntaxRules.AllowSimplifiedCollectionSyntax
&& stack.Count == 0
&& expression.Substring(i).StartsWith("["))
{
i++;
IEnumerable<object> valueArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expression, ref i, true, startToken: "[", endToken: "]")
.Select(subExp => Evaluate(subExp));

if (OptionsSyntaxRules.SimplifiedCollectionMode == SimplifiedCollectionMode.List)
stack.Push(valueArgs.ToList());
else
stack.Push(valueArgs.ToArray());

return true;
}
else
{
return false;
}
}

protected virtual bool EvaluateIndexing(string expression, Stack<object> stack, ref int i)
{
if (!OptionsFunctionalities.IndexingActive)
Expand Down Expand Up @@ -4860,6 +4910,12 @@ public enum BlockIndentation
Spaces
}

public enum SimplifiedCollectionMode
{
Array,
List
}

#endregion

#region ExpressionEvaluator linked public classes (specific Exceptions, EventArgs and Operators)
Expand Down
2 changes: 1 addition & 1 deletion TryWindow/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:ae="http://icsharpcode.net/sharpdevelop/avalonedit"
Closing="Window_Closing"
mc:Ignorable="d"
Title="Expression Evaluate" Width="400" Height="320">
Title="Expression Evaluate" Width="476" Height="320">
<DockPanel>

<StackPanel DockPanel.Dock="Bottom">
Expand Down

0 comments on commit 83b8f54

Please sign in to comment.