Skip to content

Commit

Permalink
Migrate the parser for cases to a separate class (ForNeVeR#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 18, 2019
1 parent bf06105 commit 105801e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
29 changes: 29 additions & 0 deletions src/WpfMath/Parsers/MatrixCommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using WpfMath.Atoms;
using WpfMath.Exceptions;

namespace WpfMath.Parsers
{
/// <summary>A parser for matrix-like constructs.</summary>
internal class MatrixCommandParser : ICommandParser
{
public CommandProcessingResult ProcessCommand(CommandContext context)
{
var position = context.ArgumentsStartPosition;
var source = context.CommandSource;

if (position == source.Length)
throw new TexParseException("illegal end!");

var matrixSource = TexFormulaParser.ReadElement(source, ref position);

var cells = context.Parser.GetMatrixData(context.Formula, matrixSource);
var matrix = new MatrixAtom(matrixSource, cells, MatrixCellAlignment.Left);
var atom = new FencedAtom(
matrixSource,
matrix,
TexFormulaParser.GetDelimiterSymbol("lbrace", null),
null);
return new CommandProcessingResult(atom, position);
}
}
}
1 change: 1 addition & 0 deletions src/WpfMath/Parsers/StandardCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CommandProcessingResult ProcessCommand(CommandContext context)

public static IReadOnlyDictionary<string, ICommandParser> Dictionary = new Dictionary<string, ICommandParser>
{
["cases"] = new MatrixCommandParser(),
["underline"] = new UnderlineCommand()
};
}
Expand Down
21 changes: 2 additions & 19 deletions src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class TexFormulaParser
/// </summary>
private static readonly HashSet<string> embeddedCommands = new HashSet<string>
{
"cases",
"color",
"colorbox",
"frac",
Expand Down Expand Up @@ -343,23 +342,6 @@ private Atom ProcessCommand(
SourceSpan source;
switch (command)
{
case "cases":
{
if (position == value.Length)
throw new TexParseException("illegal end!");
SkipWhiteSpace(value, ref position);

var matrixsource = ReadElement(value, ref position);

var cells = GetMatrixData(formula, matrixsource);
var matrix = new MatrixAtom(matrixsource, cells, MatrixCellAlignment.Left);
return new FencedAtom(
matrixsource,
matrix,
GetDelimiterSymbol("lbrace", null),
null);
}

case "frac":
{
var numeratorFormula = this.Parse(ReadElement(value, ref position), formula.TextStyle);
Expand Down Expand Up @@ -511,7 +493,8 @@ private Atom ProcessCommand(
/// Retrives the cells of a matrix from a given source.
/// </summary>
/// <param name="matrixsource">The source to retrieve the 2D-Array of atoms from.</param>
private List<List<Atom>> GetMatrixData(TexFormula formula, SourceSpan matrixsource)
// TODO[F]: Remove this method completely, shouldn't be internal
internal List<List<Atom>> GetMatrixData(TexFormula formula, SourceSpan matrixsource)
{
// rowindent: how many characters the next row should skip for its sourcespan to start
var rowdata = new List<List<(StringBuilder builder, int rowindent)>>
Expand Down

0 comments on commit 105801e

Please sign in to comment.