Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix input strings from memoization not passing to filters #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading