From b65a08075a4b5b5fb589989b7f4b4ce9d6a64c1f Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Tue, 3 Dec 2019 21:18:50 +0300 Subject: [PATCH] Add AtomAppendMode (#149) It will allow the command to completely replace the previously created formula. --- src/WpfMath/Parsers/AtomAppendMode.cs | 8 +++++++ src/WpfMath/Parsers/CommandParsers.cs | 5 ++++- src/WpfMath/Parsers/StandardCommands.cs | 2 +- src/WpfMath/TexFormulaParser.cs | 30 ++++++++++++++++--------- 4 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 src/WpfMath/Parsers/AtomAppendMode.cs diff --git a/src/WpfMath/Parsers/AtomAppendMode.cs b/src/WpfMath/Parsers/AtomAppendMode.cs new file mode 100644 index 00000000..e5f24966 --- /dev/null +++ b/src/WpfMath/Parsers/AtomAppendMode.cs @@ -0,0 +1,8 @@ +namespace WpfMath +{ + internal enum AtomAppendMode + { + Add, + Replace + } +} diff --git a/src/WpfMath/Parsers/CommandParsers.cs b/src/WpfMath/Parsers/CommandParsers.cs index 46672450..1aa87668 100644 --- a/src/WpfMath/Parsers/CommandParsers.cs +++ b/src/WpfMath/Parsers/CommandParsers.cs @@ -57,10 +57,13 @@ internal class CommandProcessingResult /// 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; } } diff --git a/src/WpfMath/Parsers/StandardCommands.cs b/src/WpfMath/Parsers/StandardCommands.cs index db5ff162..7ec2d7e8 100644 --- a/src/WpfMath/Parsers/StandardCommands.cs +++ b/src/WpfMath/Parsers/StandardCommands.cs @@ -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); } } diff --git a/src/WpfMath/TexFormulaParser.cs b/src/WpfMath/TexFormulaParser.cs index ab087833..e66dcd27 100644 --- a/src/WpfMath/TexFormulaParser.cs +++ b/src/WpfMath/TexFormulaParser.cs @@ -383,7 +383,7 @@ private TexFormula ReadScript( Parse(ReadElement(value, ref position), formula.TextStyle, environment.CreateChildEnvironment()); /// May return null for commands that produce no atoms. - private Atom ProcessCommand( + private (AtomAppendMode, Atom) ProcessCommand( TexFormula formula, SourceSpan value, ref int position, @@ -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": { @@ -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": { @@ -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": { @@ -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": { @@ -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": { @@ -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": { @@ -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)); } } @@ -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."); @@ -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, @@ -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