Skip to content

Commit

Permalink
#244: TexPredefinedFormulaParser cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Nov 7, 2020
1 parent 88667ff commit ff4f4b4
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions src/WpfMath/TexPredefinedFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ namespace WpfMath
// Parses definitions of predefined formulas from XML file.
internal class TexPredefinedFormulaParser
{
public static readonly string resourceName = TexUtilities.ResourcesDataDirectory + "PredefinedTexFormulas.xml";
private static readonly string resourceName = TexUtilities.ResourcesDataDirectory + "PredefinedTexFormulas.xml";

private static IDictionary<string, Type> typeMappings;
private static IDictionary<string, ArgumentValueParser> argValueParsers;
private static IDictionary<string, ActionParser> actionParsers;
private static TexFormulaParser formulaParser;
private static readonly IDictionary<string, Type> typeMappings;
private static readonly IDictionary<string, IArgumentValueParser> argValueParsers;
private static readonly IDictionary<string, IActionParser> actionParsers;

static TexPredefinedFormulaParser()
{
typeMappings = new Dictionary<string, Type>();
argValueParsers = new Dictionary<string, ArgumentValueParser>();
actionParsers = new Dictionary<string, ActionParser>();
formulaParser = new TexFormulaParser();
argValueParsers = new Dictionary<string, IArgumentValueParser>();
actionParsers = new Dictionary<string, IActionParser>();

typeMappings.Add("Formula", typeof(TexFormula));
typeMappings.Add("string", typeof(string));
Expand Down Expand Up @@ -74,8 +72,8 @@ private static Type[] GetArgumentTypes(IEnumerable<XElement> args)
var typeName = curArg.AttributeValue("type");
var value = curArg.AttributeValue("value");

var parser = ((ArgumentValueParser)argValueParsers[typeName]);
result.Add(parser.Parse(value, typeName, context));
var parser = argValueParsers[typeName];
result.Add(parser.Parse(value, context));
}

return result.ToArray();
Expand Down Expand Up @@ -106,7 +104,7 @@ public void Parse(IDictionary<string, Func<SourceSpan, TexFormula?>> predefinedT
}
}

public TexFormula? ParseFormula(SourceSpan source, XElement formulaElement)
private TexFormula? ParseFormula(SourceSpan source, XElement formulaElement)
{
var context = new PredefinedFormulaContext();
foreach (var element in formulaElement.Elements())
Expand All @@ -122,9 +120,9 @@ public void Parse(IDictionary<string, Func<SourceSpan, TexFormula?>> predefinedT
return null;
}

public class MethodInvocationParser : ActionParser
private class MethodInvocationParser : IActionParser
{
public override void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
{
var methodName = element.AttributeValue("name");
var objectName = element.AttributeValue("formula");
Expand All @@ -143,14 +141,13 @@ public override void Parse(SourceSpan source, XElement element, PredefinedFormul
}
}

public class CreateTeXFormulaParser : ActionParser
private class CreateTeXFormulaParser : IActionParser
{
public override void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
{
var name = element.AttributeValue("name");
var args = element.Elements("Argument");

var argTypes = GetArgumentTypes(args);
var argValues = GetArgumentValues(args, context);

Debug.Assert(argValues.Length == 1 || argValues.Length == 0);
Expand All @@ -169,15 +166,15 @@ public override void Parse(SourceSpan source, XElement element, PredefinedFormul
}
}

public class ReturnParser : ActionParser
private class ReturnParser : IActionParser
{
public TexFormula? Result
{
get;
private set;
}

public override void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
public void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context)
{
var name = element.AttributeValue("name");
var result = context[name];
Expand All @@ -186,50 +183,50 @@ public override void Parse(SourceSpan source, XElement element, PredefinedFormul
}
}

public class DoubleValueParser : ArgumentValueParser
private class DoubleValueParser : IArgumentValueParser
{
public override object Parse(string value, string type, PredefinedFormulaContext context)
public object Parse(string value, PredefinedFormulaContext context)
{
return double.Parse(value, CultureInfo.InvariantCulture);
}
}

public class CharValueParser : ArgumentValueParser
private class CharValueParser : IArgumentValueParser
{
public override object Parse(string value, string type, PredefinedFormulaContext context)
public object Parse(string value, PredefinedFormulaContext context)
{
Debug.Assert(value.Length == 1);
return value[0];
}
}

public class BooleanValueParser : ArgumentValueParser
private class BooleanValueParser : IArgumentValueParser
{
public override object Parse(string value, string type, PredefinedFormulaContext context)
public object Parse(string value, PredefinedFormulaContext context)
{
return bool.Parse(value);
}
}

public class IntValueParser : ArgumentValueParser
private class IntValueParser : IArgumentValueParser
{
public override object Parse(string value, string type, PredefinedFormulaContext context)
public object Parse(string value, PredefinedFormulaContext context)
{
return int.Parse(value, CultureInfo.InvariantCulture);
}
}

public class StringValueParser : ArgumentValueParser
private class StringValueParser : IArgumentValueParser
{
public override object Parse(string value, string type, PredefinedFormulaContext context)
public object Parse(string value, PredefinedFormulaContext context)
{
return value;
}
}

public class TeXFormulaValueParser : ArgumentValueParser
private class TeXFormulaValueParser : IArgumentValueParser
{
public override object? Parse(string value, string type, PredefinedFormulaContext context)
public object? Parse(string value, PredefinedFormulaContext context)
{
if (value == null)
return null;
Expand All @@ -240,15 +237,15 @@ public class TeXFormulaValueParser : ArgumentValueParser
}
}

public class ColorConstantValueParser : ArgumentValueParser
private class ColorConstantValueParser : IArgumentValueParser
{
public override object? Parse(string value, string type, PredefinedFormulaContext context)
public object? Parse(string value, PredefinedFormulaContext context)
{
return typeof(Color).GetField(value)!.GetValue(null);
}
}

public class EnumParser : ArgumentValueParser
private class EnumParser : IArgumentValueParser
{
private Type enumType;

Expand All @@ -257,20 +254,20 @@ public EnumParser(Type enumType)
this.enumType = enumType;
}

public override object Parse(string value, string type, PredefinedFormulaContext context)
public object Parse(string value, PredefinedFormulaContext context)
{
return Enum.Parse(this.enumType, value);
}
}

public abstract class ActionParser
private interface IActionParser
{
public abstract void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context);
void Parse(SourceSpan source, XElement element, PredefinedFormulaContext context);
}

public abstract class ArgumentValueParser
private interface IArgumentValueParser
{
public abstract object? Parse(string value, string type, PredefinedFormulaContext context);
object? Parse(string value, PredefinedFormulaContext context);
}
}
}

0 comments on commit ff4f4b4

Please sign in to comment.