Skip to content

Commit

Permalink
#14: ignore empty delimiters instead of defining them as null
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Feb 26, 2017
1 parent c2ef630 commit 9dd8ce4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 32 deletions.
9 changes: 5 additions & 4 deletions src/WpfMath.Tests/ParserTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type ParserTests() =
[<Theory>]
[<InlineData(".", ")", true, false)>]
[<InlineData("(", ".", false, true)>]
let ``Empty delimiters should work`` (left : string, right : string, isLeftNull : bool, isRightNull : bool) =
let leftBrace = if isLeftNull then null else (openBrace "lbrack")
let rightBrace = if isRightNull then null else (closeBrace "rbrack")
let ``Empty delimiters should work`` (left : string, right : string, isLeftEmpty : bool, isRightEmpty : bool) =
let empty = brace SymbolAtom.EmptyDelimiterName TexAtomType.Ordinary
let leftBrace = if isLeftEmpty then empty else (openBrace "lbrack")
let rightBrace = if isRightEmpty then empty else (closeBrace "rbrack")

assertParseResult
<| sprintf @"\left%sa\right%s" left right
Expand All @@ -55,7 +56,7 @@ type ParserTests() =
let ``Unmatched delimiters should work`` () =
assertParseResult
<| @"\left)a\right|"
<| (formula <| fenced (closeBrace "rbrack") (char 'a') (SymbolAtom("vert", TexAtomType.Ordinary, true)))
<| (formula <| fenced (closeBrace "rbrack") (char 'a') (brace "vert" TexAtomType.Ordinary))

[<Fact>]
let ``Expression in braces should be parsed`` () =
Expand Down
7 changes: 4 additions & 3 deletions src/WpfMath.Tests/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let formula (root : Atom) : TexFormula =
let char (c : char) : CharAtom = CharAtom(c)
let styledChar (c : char, style:string) : CharAtom = CharAtom(c, style)
let op (baseAtom : Atom) (useVertScripts : System.Nullable<bool>) : BigOperatorAtom = BigOperatorAtom(baseAtom, null, null, useVertScripts)
let opWithScripts (baseAtom : Atom) (subscript : Atom) (superscript : Atom) (useVertScripts : System.Nullable<bool>)
let opWithScripts (baseAtom : Atom) (subscript : Atom) (superscript : Atom) (useVertScripts : System.Nullable<bool>)
: BigOperatorAtom = BigOperatorAtom(baseAtom, subscript, superscript, useVertScripts)
let group (groupedAtom: Atom) : TypedAtom = TypedAtom(groupedAtom, TexAtomType.Ordinary, TexAtomType.Ordinary)
let symbol (name : string) : SymbolAtom = SymbolAtom(name, TexAtomType.BinaryOperator, false)
Expand All @@ -19,5 +19,6 @@ let row (children : Atom seq) : RowAtom =
result
let fenced left body right : FencedAtom = FencedAtom(body, left, right)

let openBrace (name : string) : SymbolAtom = SymbolAtom(name, TexAtomType.Opening, true)
let closeBrace (name : string) : SymbolAtom = SymbolAtom(name, TexAtomType.Closing, true)
let brace (name : string) (braceType : TexAtomType) : SymbolAtom = SymbolAtom(name, braceType, true)
let openBrace (name : string) : SymbolAtom = brace name TexAtomType.Opening
let closeBrace (name : string) : SymbolAtom = brace name TexAtomType.Closing
7 changes: 2 additions & 5 deletions src/WpfMath/FencedAtom.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfMath
{
Expand Down Expand Up @@ -57,7 +54,7 @@ public override Box CreateBox(TexEnvironment environment)
var minHeight = Math.Max((delta / 500) * delimeterFactor, 2 * delta - delimeterShortfall);

// Create and add box for left delimeter.
if (LeftDelimeter != null)
if (LeftDelimeter != null && LeftDelimeter.Name != SymbolAtom.EmptyDelimiterName)
{
var leftDelimeterBox = DelimiterFactory.CreateBox(this.LeftDelimeter.Name, minHeight, environment);
CentreBox(leftDelimeterBox, axis);
Expand All @@ -76,7 +73,7 @@ public override Box CreateBox(TexEnvironment environment)
resultBox.Add(Glue.CreateBox(this.BaseAtom.GetRightType(), TexAtomType.Closing, environment));

// Create and add box for right delimeter.
if (this.RightDelimeter != null)
if (RightDelimeter != null && RightDelimeter.Name != SymbolAtom.EmptyDelimiterName)
{
var rightDelimeterBox = DelimiterFactory.CreateBox(this.RightDelimeter.Name, minHeight, environment);
CentreBox(rightDelimeterBox, axis);
Expand Down
21 changes: 2 additions & 19 deletions src/WpfMath/TexFormulaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ public void AddEmbraced(string formula, string leftSymbol, string rightSymbol)

public void AddEmbraced(TexFormula formula, string leftSymbol, string rightSymbol)
{
Add(
PrepareFencedAtom(
formula?.RootAtom,
TexFormulaParser.GetDelimiterSymbol(leftSymbol),
TexFormulaParser.GetDelimiterSymbol(rightSymbol)));
Add(new FencedAtom(formula == null ? null : formula.RootAtom, TexFormulaParser.GetDelimiterSymbol(leftSymbol),
TexFormulaParser.GetDelimiterSymbol(rightSymbol)));
}

public void AddFraction(string numerator, string denominator, bool drawLine)
Expand Down Expand Up @@ -307,19 +304,5 @@ public void PutUnderAndOver(TexFormula underFormula, TexUnit underUnit, double u
null : underFormula.RootAtom, underUnit, underSpace, underScriptSize, over == null ? null : over.RootAtom,
overUnit, overSpace, overScriptSize);
}

internal static FencedAtom PrepareFencedAtom(
Atom baseAtom,
SymbolAtom leftDelimiter,
SymbolAtom rightDelimiter)
{
if (leftDelimiter.Name == SymbolAtom.EmptyDelimiterName)
leftDelimiter = null;

if (rightDelimiter.Name == SymbolAtom.EmptyDelimiterName)
rightDelimiter = null;

return new FencedAtom(baseAtom, leftDelimiter, rightDelimiter);
}
}
}
2 changes: 1 addition & 1 deletion src/WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private Atom ProcessCommand(
throw new TexParseException($"Cannot find delimiter named {delimiter}");

var closing = internals.ClosingDelimiter;
return TexFormulaHelper.PrepareFencedAtom(internals.Body, opening, closing);
return new FencedAtom(internals.Body, opening, closing);
}

case "right":
Expand Down

0 comments on commit 9dd8ce4

Please sign in to comment.