Skip to content

Commit

Permalink
Extract all the matrix commands into the command registry (ForNeVeR#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 18, 2019
1 parent 105801e commit 4635bcb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/WpfMath/Atoms/MatrixAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class MatrixAtom : Atom
public MatrixAtom(
SourceSpan source,
List<List<Atom>> cells,
MatrixCellAlignment matrixCellAlignment = MatrixCellAlignment.Center,
MatrixCellAlignment matrixCellAlignment,
double verticalPadding = 0.35,
double horizontalPadding = 0.35) : base(source)
{
Expand Down
38 changes: 32 additions & 6 deletions src/WpfMath/Parsers/MatrixCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ namespace WpfMath.Parsers
/// <summary>A parser for matrix-like constructs.</summary>
internal class MatrixCommandParser : ICommandParser
{
private readonly string _leftDelimiterSymbolName;
private readonly string _rightDelimiterSymbolName;
private readonly MatrixCellAlignment _cellAlignment;

public MatrixCommandParser(
string leftDelimiterSymbolName,
string rightDelimiterSymbolName,
MatrixCellAlignment cellAlignment)
{
_leftDelimiterSymbolName = leftDelimiterSymbolName;
_rightDelimiterSymbolName = rightDelimiterSymbolName;
_cellAlignment = cellAlignment;
}

public CommandProcessingResult ProcessCommand(CommandContext context)
{
var position = context.ArgumentsStartPosition;
Expand All @@ -17,12 +31,24 @@ public CommandProcessingResult ProcessCommand(CommandContext context)
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);
var matrix = new MatrixAtom(matrixSource, cells, _cellAlignment);

SymbolAtom GetDelimiter(string name) =>
name == null
? null
: TexFormulaParser.GetDelimiterSymbol(name, null) ??
throw new TexParseException($"The delimiter {name} could not be found");

var leftDelimiter = GetDelimiter(_leftDelimiterSymbolName);
var rightDelimiter = GetDelimiter(_rightDelimiterSymbolName);

var atom = leftDelimiter == null && rightDelimiter == null
? (Atom) matrix
: new FencedAtom(
matrixSource,
matrix,
leftDelimiter,
rightDelimiter);
return new CommandProcessingResult(atom, position);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/WpfMath/Parsers/StandardCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public CommandProcessingResult ProcessCommand(CommandContext context)

public static IReadOnlyDictionary<string, ICommandParser> Dictionary = new Dictionary<string, ICommandParser>
{
["cases"] = new MatrixCommandParser(),
["cases"] = new MatrixCommandParser("lbrace", null, MatrixCellAlignment.Left),
["matrix"] = new MatrixCommandParser(null, null, MatrixCellAlignment.Center),
["pmatrix"] = new MatrixCommandParser("lbrack", "rbrack", MatrixCellAlignment.Center),
["underline"] = new UnderlineCommand()
};
}
Expand Down
33 changes: 0 additions & 33 deletions src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public class TexFormulaParser
"colorbox",
"frac",
"left",
"matrix",
"overline",
"pmatrix",
"right",
"sqrt"
};
Expand Down Expand Up @@ -371,43 +369,12 @@ private Atom ProcessCommand(
source = value.Segment(start, position - start);
return new FencedAtom(source, internals.Body, opening, closing);
}

case "matrix":
{
if (position == value.Length)
throw new TexParseException("illegal end!");
SkipWhiteSpace(value, ref position);

var matrixsource = ReadElement(value, ref position);

var cells = GetMatrixData(formula, matrixsource);
return new MatrixAtom(matrixsource, cells);
}

case "overline":
{
var overlineFormula = this.Parse(ReadElement(value, ref position), formula.TextStyle);
source = value.Segment(start, position - start);
return new OverlinedAtom(source, overlineFormula.RootAtom);
}

case "pmatrix":
{
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);
return new FencedAtom(
matrixsource,
matrix,
GetDelimiterSymbol("lbrack", null),
GetDelimiterSymbol("rbrack", null));
}

case "right":
{
if (!allowClosingDelimiter)
Expand Down

0 comments on commit 4635bcb

Please sign in to comment.