Skip to content

Commit

Permalink
Fix input string not being passed to filters from memoized
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
TheDeathlyCow committed Nov 23, 2023
1 parent d001f45 commit 04cc42f
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.Calyx/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.Calyx/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.Calyx/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.Calyx/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/.idea.Calyx/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
6 changes: 6 additions & 0 deletions .idea/.idea.Calyx/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Calyx.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=086c1704_002D764f_002D456b_002D8cda_002Dae5177d8d556/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="FiltersApplyToMemoizedRules" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;NUnit3x::7A4CD3DB-F115-4731-B257-EFBC32D8D3C3::net6.0::Calyx.Test.FilterTest.FiltersApplyToMemoizedRules&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
18 changes: 14 additions & 4 deletions Calyx/Syntax/ExpressionChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ namespace Calyx.Syntax
{
public class ExpressionChain : IProduction
{
private string ruleName;
private string[] components;
private Registry registry;

public ExpressionChain(string[] components, Registry registry)
public ExpressionChain(string ruleName, string[] components, Registry registry)
{
RemoveSigil(ref ruleName);
this.ruleName = ruleName;
this.registry = registry;
this.components = components;
this.components = components.Skip(1).ToArray();
}

public Expansion Evaluate(Options options)
{
Expansion eval = registry.Expand(components[0]).Evaluate(options);
Expansion eval = registry.Expand(ruleName).Evaluate(options);
string initial = new Expansion(Exp.Expression, eval.Tail).Flatten().ToString();

// Dynamic dispatch to string modifiers one after another
string modified = components
.Skip(1)
.Aggregate(initial, (accumulator, filterName) => {
try {
return registry.GetFilterComponent(filterName).Invoke(accumulator);
Expand All @@ -40,5 +42,13 @@ public Expansion Evaluate(Options options)

return new Expansion(Exp.Expression, new Expansion(Exp.Atom, modified));
}

private static void RemoveSigil(ref string ruleName)
{
if (ExpressionNode.IsSigil(ruleName[0]))
{
ruleName = ruleName.Substring(1);
}
}
}
}
5 changes: 5 additions & 0 deletions Calyx/Syntax/ExpressionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public Expansion Evaluate(Options options)
Expansion eval = registry.Expand(reference).Evaluate(options);
return new Expansion(Exp.Expression, eval.Tail);
}

public static bool IsSigil(char character)
{
return character == MEMO_SIGIL || character == UNIQUE_SIGIL;
}
}
}
2 changes: 1 addition & 1 deletion Calyx/Syntax/TemplateNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static TemplateNode Parse(string raw, Registry registry)
// Check if we have a post-processing chain
if (components.Length > 1) {
// Generate a chained expression headed by a non-terminal
concatNodes.Add(new ExpressionChain(components, registry));
concatNodes.Add(new ExpressionChain(components[0], components, registry));
} else {
// Generate a standalone non-terminal expression
concatNodes.Add(ExpressionNode.Parse(components[0], registry));
Expand Down
10 changes: 10 additions & 0 deletions Tests/FilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public void IncorrectFilterParameterCountThrowsException() {
Assert.Throws<IncorrectFilterSignature>(() => registry.Evaluate("start"));
}

[Test]
public void FiltersApplyToMemoizedRules() {
Registry registry = new Registry(new Options(seed: 1234));

registry.DefineRule("start", new [] { "{$names.uppercase}" });
registry.DefineRule("names", new [] { "Jewels" } );

Assert.That(registry.Evaluate("start").Flatten().ToString(), Is.EqualTo("JEWELS"));
}

internal static class TestFilter {
[FilterName("backwards")]
public static string Backwards(string input, Options options) {
Expand Down

0 comments on commit 04cc42f

Please sign in to comment.