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 Dec 3, 2019
1 parent ca598ac commit b65a080
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 @@ -49,7 +49,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 @@ -383,7 +383,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 @@ -408,7 +408,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 @@ -430,7 +430,7 @@ private Atom ProcessCommand(

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 @@ -439,7 +439,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 @@ -460,7 +460,7 @@ private Atom ProcessCommand(
throw new TexParseException($"Cannot find delimiter named {delimiter}");

closedDelimiter = true;
return closing;
return (AtomAppendMode.Add, closing);
}
case "sqrt":
{
Expand All @@ -483,7 +483,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 @@ -493,7 +493,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 @@ -503,7 +503,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 @@ -517,7 +517,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 @@ -636,7 +636,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 @@ -658,7 +658,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 b65a080

Please sign in to comment.