Skip to content

Commit

Permalink
Add AtomAppendMode (ForNeVeR#149)
Browse files Browse the repository at this point in the history
It will allow the command to completely replace the previously created
formula.
  • Loading branch information
ForNeVeR committed Jan 6, 2020
1 parent 0e08fe6 commit 9ba0fd6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/WpfMath/Parsers/AtomAppendMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace WpfMath
{
internal enum AtomAppendMode
{
Add,
Replace
}
}
5 changes: 4 additions & 1 deletion src/WpfMath/Parsers/CommandParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ internal class CommandProcessingResult
/// </summary>
public int NextPosition { get; }

public CommandProcessingResult(Atom atom, int nextPosition)
public AtomAppendMode AppendMode { get; }

public CommandProcessingResult(Atom atom, int nextPosition, AtomAppendMode appendMode = AtomAppendMode.Add)
{
Atom = atom;
NextPosition = nextPosition;
AppendMode = appendMode;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/WpfMath/Parsers/StandardCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public CommandProcessingResult ProcessCommand(CommandContext context)
var start = context.CommandNameStartPosition;
var atomSource = source.Segment(start, position - start);
var atom = new MatrixAtom(atomSource, rows, MatrixCellAlignment.Center);
return new CommandProcessingResult(atom, position);
return new CommandProcessingResult(atom, position, AtomAppendMode.Replace);
}

}
Expand Down
30 changes: 19 additions & 11 deletions src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private TexFormula ReadScript(
Parse(ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment());

/// <remarks>May return <c>null</c> for commands that produce no atoms.</remarks>
private Atom ProcessCommand(
private (AtomAppendMode, Atom) ProcessCommand(
TexFormula formula,
SourceSpan value,
ref int position,
Expand All @@ -471,7 +471,7 @@ private Atom ProcessCommand(
formula.TextStyle,
environment.CreateChildEnvironment());
source = value.Segment(start, position - start);
return new FractionAtom(source, numeratorFormula.RootAtom, denominatorFormula.RootAtom, true);
return (AtomAppendMode.Add, new FractionAtom(source, numeratorFormula.RootAtom, denominatorFormula.RootAtom, true));
}
case "left":
{
Expand All @@ -483,7 +483,7 @@ private Atom ProcessCommand(
var internals = ParseUntilDelimiter(value, ref position, formula.TextStyle, environment);
var closing = internals.ClosingDelimiter;
source = value.Segment(start, position - start);
return new FencedAtom(source, internals.Body, opening, closing);
return (AtomAppendMode.Add, new FencedAtom(source, internals.Body, opening, closing));
}
case "overline":
{
Expand All @@ -492,7 +492,7 @@ private Atom ProcessCommand(
formula.TextStyle,
environment.CreateChildEnvironment());
source = value.Segment(start, position - start);
return new OverlinedAtom(source, overlineFormula.RootAtom);
return (AtomAppendMode.Add, new OverlinedAtom(source, overlineFormula.RootAtom));
}
case "right":
{
Expand All @@ -506,7 +506,7 @@ private Atom ProcessCommand(
var closing = ParseDelimiter(value, start, ref position);

closedDelimiter = true;
return closing;
return (AtomAppendMode.Add, closing);
}
case "sqrt":
{
Expand All @@ -529,7 +529,7 @@ private Atom ProcessCommand(
environment.CreateChildEnvironment());

source = value.Segment(start, position - start);
return new Radical(source, sqrtFormula.RootAtom, degreeFormula?.RootAtom);
return (AtomAppendMode.Add, new Radical(source, sqrtFormula.RootAtom, degreeFormula?.RootAtom));
}
case "color":
{
Expand All @@ -539,7 +539,7 @@ private Atom ProcessCommand(
var bodyFormula = Parse(bodyValue, formula.TextStyle, environment.CreateChildEnvironment());
source = value.Segment(start, position - start);

return new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color));
return (AtomAppendMode.Add, new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color)));
}
case "colorbox":
{
Expand All @@ -549,7 +549,7 @@ private Atom ProcessCommand(
var bodyFormula = Parse(bodyValue, formula.TextStyle, environment.CreateChildEnvironment());
source = value.Segment(start, position - start);

return new StyledAtom(source, bodyFormula.RootAtom, new SolidColorBrush(color), null);
return (AtomAppendMode.Add, new StyledAtom(source, bodyFormula.RootAtom, new SolidColorBrush(color), null));
}
}

Expand All @@ -563,7 +563,7 @@ private Atom ProcessCommand(
$"Incorrect parser behavior for command {command}: NextPosition = {parseResult.NextPosition}, position = {position}. Parser did not made any progress.");

position = parseResult.NextPosition;
return parseResult.Atom;
return (parseResult.AppendMode, parseResult.Atom);
}

throw new TexParseException("Invalid command.");
Expand Down Expand Up @@ -662,7 +662,7 @@ private void ProcessEscapeSequence(TexFormula formula,
|| _commandRegistry.ContainsKey(command))
{
// Command was found.
var commandAtom = ProcessCommand(
var (appendMode, commandAtom) = ProcessCommand(
formula,
value,
ref position,
Expand All @@ -684,7 +684,15 @@ private void ProcessEscapeSequence(TexFormula formula,
environment);

var source = new SourceSpan(formulaSource.Source, formulaSource.Start, commandAtom.Source.End);
formula.Add(commandAtom, source);
switch (appendMode)
{
case AtomAppendMode.Add:
formula.Add(commandAtom, source);
break;
case AtomAppendMode.Replace:
formula.RootAtom = commandAtom;
break;
}
}
}
else
Expand Down

0 comments on commit 9ba0fd6

Please sign in to comment.