Skip to content

Commit

Permalink
(#63) TexPredefinedFormulaParser: restore the ability to parse recurs…
Browse files Browse the repository at this point in the history
…ive formulae
  • Loading branch information
ForNeVeR committed Jan 11, 2023
1 parent a39e7d5 commit 6763e03
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/WpfMath.Shared/TexPredefinedFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public TexPredefinedFormulaParser(IBrushFactory brushFactory)
}

// TODO: Review this API
public void Parse(IDictionary<string, Func<SourceSpan, TexFormula?>> predefinedTeXFormulas)
public void Parse(Dictionary<string, Func<SourceSpan, TexFormula?>> predefinedTeXFormulas)
{
var rootEnabled = rootElement.AttributeBooleanValue("enabled", true);
if (rootEnabled)
Expand All @@ -95,13 +95,13 @@ public void Parse(IDictionary<string, Func<SourceSpan, TexFormula?>> predefinedT
if (enabled)
{
var formulaName = formulaElement.AttributeValue("name");
predefinedTeXFormulas.Add(formulaName, source => this.ParseFormula(source, formulaElement));
predefinedTeXFormulas.Add(formulaName, source => this.ParseFormula(source, formulaElement, predefinedTeXFormulas));
}
}
}
}

private TexFormula? ParseFormula(SourceSpan source, XElement formulaElement)
private TexFormula? ParseFormula(SourceSpan source, XElement formulaElement, Dictionary<string, Func<SourceSpan, TexFormula?>> allFormulas)
{
var context = new PredefinedFormulaContext();
foreach (var element in formulaElement.Elements())
Expand All @@ -110,7 +110,7 @@ public void Parse(IDictionary<string, Func<SourceSpan, TexFormula?>> predefinedT
if (parser == null)
continue;

parser.Parse(source, element, context);
parser.Parse(source, element, context, allFormulas);
if (parser is ReturnParser)
return ((ReturnParser)parser).Result;
}
Expand All @@ -119,7 +119,11 @@ public void Parse(IDictionary<string, Func<SourceSpan, TexFormula?>> predefinedT

private record MethodInvocationParser(TexPredefinedFormulaParser Parent, IBrushFactory BrushFactory) : IActionParser
{
public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
public void Parse(
SourceSpan source,
XElement element,
PredefinedFormulaContext context,
Dictionary<string, Func<SourceSpan, TexFormula?>> allFormulas)
{
var methodName = element.AttributeValue("name");
var objectName = element.AttributeValue("formula");
Expand All @@ -135,7 +139,7 @@ public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext
formula,
source,
BrushFactory,
new Dictionary<string, Func<SourceSpan, TexFormula?>>());
allFormulas);
var methodInvocation = typeof(TexFormulaHelper).GetMethod(methodName, argTypes)!;

methodInvocation.Invoke(helper, argValues);
Expand All @@ -144,7 +148,11 @@ public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext

private record CreateTeXFormulaParser(TexPredefinedFormulaParser Parent, IBrushFactory BrushFactory) : IActionParser
{
public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
public void Parse(
SourceSpan source,
XElement element,
PredefinedFormulaContext context,
Dictionary<string, Func<SourceSpan, TexFormula?>> allFormulas)
{
var name = element.AttributeValue("name");
var args = element.Elements("Argument");
Expand All @@ -155,7 +163,7 @@ public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext
TexFormula formula;
if (argValues.Length == 1)
{
var parser = new TexFormulaParser(BrushFactory, new Dictionary<string, Func<SourceSpan, TexFormula?>>());
var parser = new TexFormulaParser(BrushFactory, allFormulas);
formula = parser.Parse((string)argValues[0]!); // Nullable TODO: This might need null checking
}
else
Expand All @@ -175,7 +183,11 @@ public TexFormula? Result
private set;
}

public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
public void Parse(
SourceSpan source,
XElement element,
PredefinedFormulaContext context,
Dictionary<string, Func<SourceSpan, TexFormula?>> allFormulas)
{
var name = element.AttributeValue("name");
var result = context[name];
Expand Down Expand Up @@ -255,7 +267,11 @@ public object Parse(string value, PredefinedFormulaContext context)

private interface IActionParser
{
void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context);
void Parse(
SourceSpan source,
XElement element,
PredefinedFormulaContext context,
Dictionary<string, Func<SourceSpan, TexFormula?>> allFormulas);
}

private interface IArgumentValueParser
Expand Down

0 comments on commit 6763e03

Please sign in to comment.