From 980cb61503b113e3b2add6773ee900d781672cb1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 6 Aug 2020 11:26:12 +0200 Subject: [PATCH] Fix elements being enclosed when not needed --- Cargo.toml | 2 +- src/elements/group.rs | 22 ++++++++++++++++++++++ src/format/mathml.rs | 10 +++++++++- src/parsing/tree_parser.rs | 10 ++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 61036e9..1251002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "asciimath-rs" description = "AsciiMath parser" repository = "https://github.com/trivernis/asciimath-rs" -version = "0.4.5" +version = "0.4.6" authors = ["trivernis "] edition = "2018" readme = "README.md" diff --git a/src/elements/group.rs b/src/elements/group.rs index 8667ece..758ebcd 100644 --- a/src/elements/group.rs +++ b/src/elements/group.rs @@ -14,6 +14,7 @@ pub enum Group { Norm(Norm), Matrix(Matrix), Vector(Vector), + NonEnclosed(NonEnclosed), } #[derive(Debug, Clone, PartialOrd, PartialEq)] @@ -70,3 +71,24 @@ pub struct Matrix { pub struct Vector { pub inner: Vec>, } + +#[derive(Debug, Clone, PartialOrd, PartialEq)] +pub struct NonEnclosed { + pub inner: Box, +} + +impl Group { + pub(crate) fn to_non_enclosed(&self) -> Option { + let inner = match self { + Group::Parentheses(p) => Some(p.inner.clone()), + Group::Braces(b) => Some(b.inner.clone()), + Group::Brackets(b) => Some(b.inner.clone()), + _ => None, + }; + if let Some(inner) = inner { + Some(Group::NonEnclosed(NonEnclosed { inner })) + } else { + None + } + } +} diff --git a/src/format/mathml.rs b/src/format/mathml.rs index 1a5bfb7..c6a0d8c 100644 --- a/src/format/mathml.rs +++ b/src/format/mathml.rs @@ -1,6 +1,7 @@ use crate::elements::accent::{Color, ExpressionAccent, GenericAccent, OverSet, UnderSet}; use crate::elements::group::{ - Abs, Angles, Braces, Brackets, Ceil, Floor, Group, Matrix, Norm, Parentheses, Vector, XGroup, + Abs, Angles, Braces, Brackets, Ceil, Floor, Group, Matrix, NonEnclosed, Norm, Parentheses, + Vector, XGroup, }; use crate::elements::literal::{Literal, Number, PlainText, Symbol}; use crate::elements::special::{ @@ -382,6 +383,7 @@ impl ToMathML for Group { Group::Ceil(c) => c.to_mathml(), Group::Norm(n) => n.to_mathml(), Group::Matrix(m) => m.to_mathml(), + Group::NonEnclosed(ne) => ne.to_mathml(), } } } @@ -501,6 +503,12 @@ impl ToMathML for Vector { } } +impl ToMathML for NonEnclosed { + fn to_mathml(&self) -> String { + format!("{}", self.inner.to_mathml()) + } +} + impl ToMathML for Special { fn to_mathml(&self) -> String { match self { diff --git a/src/parsing/tree_parser.rs b/src/parsing/tree_parser.rs index 4470660..a965ec5 100644 --- a/src/parsing/tree_parser.rs +++ b/src/parsing/tree_parser.rs @@ -400,6 +400,11 @@ impl TreeParser { self.step(); self.step(); if let Some(element) = self.parse_element() { + if let Element::Group(g) = &element { + if let Some(ne) = g.to_non_enclosed() { + return Some(Element::Group(ne).boxed()); + } + } Some(element.boxed()) } else { None @@ -414,6 +419,11 @@ impl TreeParser { self.step(); self.step(); if let Some(element) = self.parse_element() { + if let Element::Group(g) = &element { + if let Some(ne) = g.to_non_enclosed() { + return Some(Element::Group(ne).boxed()); + } + } Some(element.boxed()) } else { None