diff --git a/src/elements/group.rs b/src/elements/group.rs index 758ebcd..df57a89 100644 --- a/src/elements/group.rs +++ b/src/elements/group.rs @@ -85,10 +85,6 @@ impl Group { Group::Brackets(b) => Some(b.inner.clone()), _ => None, }; - if let Some(inner) = inner { - Some(Group::NonEnclosed(NonEnclosed { inner })) - } else { - None - } + inner.map(|inner| Group::NonEnclosed(NonEnclosed { inner })) } } diff --git a/src/elements/special.rs b/src/elements/special.rs index 8352fb1..93b0280 100644 --- a/src/elements/special.rs +++ b/src/elements/special.rs @@ -1,7 +1,7 @@ use crate::elements::Element; use crate::utils::Boxed; -#[derive(Debug, Clone, PartialOrd, PartialEq)] +#[derive(Debug, Clone, PartialOrd, PartialEq, Default)] pub struct Expression { pub children: Vec, } @@ -19,13 +19,13 @@ pub enum Special { OIntegral(OIntegral), } -#[derive(Debug, Clone, PartialOrd, PartialEq)] +#[derive(Debug, Clone, PartialOrd, PartialEq, Default)] pub struct Sum { pub top: Option>, pub bottom: Option>, } -#[derive(Debug, Clone, PartialOrd, PartialEq)] +#[derive(Debug, Clone, PartialOrd, PartialEq, Default)] pub struct Prod { pub top: Option>, pub bottom: Option>, @@ -73,33 +73,9 @@ pub struct OIntegral { } impl Expression { - pub fn new() -> Self { - Self { - children: Vec::new(), - } - } - pub fn add_child(&mut self, child: Element) { self.children.push(child) } } impl Boxed for Expression {} - -impl Sum { - pub fn new() -> Self { - Self { - bottom: None, - top: None, - } - } -} - -impl Prod { - pub fn new() -> Self { - Self { - bottom: None, - top: None, - } - } -} diff --git a/src/format/mathml.rs b/src/format/mathml.rs index 74b4228..ae898d2 100644 --- a/src/format/mathml.rs +++ b/src/format/mathml.rs @@ -541,7 +541,7 @@ impl ToMathML for Sum { } else if let Some(top) = &self.top { format!("{}", top.to_mathml()) } else { - format!("") + "".to_string() } } } @@ -561,7 +561,7 @@ impl ToMathML for Prod { } else if let Some(top) = &self.top { format!("{}", top.to_mathml()) } else { - format!("") + "".to_string() } } } @@ -627,7 +627,7 @@ impl ToMathML for Integral { } else if let Some(top) = &self.top { format!("{}", top.to_mathml()) } else { - format!("") + "".to_string() } } } @@ -647,7 +647,7 @@ impl ToMathML for OIntegral { } else if let Some(top) = &self.top { format!("{}", top.to_mathml()) } else { - format!("") + "".to_string() } } } @@ -674,10 +674,8 @@ impl ToMathML for Expression { ///``` /// use asciimath_rs::format::mathml::ToMathML; /// - /// fn main() { - /// let expression = asciimath_rs::parse("sin(2x - 1) + 2".to_string()); - /// println!("{}", expression.to_mathml()); - /// } + /// let expression = asciimath_rs::parse("sin(2x - 1) + 2".to_string()); + /// println!("{}", expression.to_mathml()); /// ``` fn to_mathml(&self) -> String { format!( diff --git a/src/lib.rs b/src/lib.rs index 5777e57..a93f644 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,9 +23,7 @@ pub(crate) mod utils; /// Example: /// /// ```rust -/// fn main() { -/// let expression = asciimath_rs::parse("sin(2x) + 3".to_string()); -/// } +/// let expression = asciimath_rs::parse("sin(2x) + 3".to_string()); /// ``` pub fn parse(content: String) -> Expression { let mut tokenizer = Tokenizer::new(content); diff --git a/src/parsing/tree_parser.rs b/src/parsing/tree_parser.rs index a2d18b5..5058ac2 100644 --- a/src/parsing/tree_parser.rs +++ b/src/parsing/tree_parser.rs @@ -69,7 +69,7 @@ impl TreeParser { } fn parse_expression(&mut self) -> Expression { - let mut expression = Expression::new(); + let mut expression = Expression::default(); while !self.end_reached() { if let Some(element) = self.parse_element() { @@ -97,18 +97,12 @@ impl TreeParser { fn parse_element(&mut self) -> Option { let token = self.current_token().clone(); match token { - Token::Arrow(a) => Some(Element::Literal(Literal::Arrow(a.clone()))), - Token::Logical(l) => Some(Element::Literal(Literal::Logical(l.clone()))), - Token::Relation(r) => Some(Element::Literal(Literal::Relation(r.clone()))), - Token::Greek(g) => Some(Element::Literal(Literal::Greek(g.clone()))), - Token::Function(f) => Some(Element::Literal(Literal::Function(f.clone()))), - Token::Text(t) => { - if let Some(literal) = self.parse_text(t) { - Some(Element::Literal(literal)) - } else { - None - } - } + Token::Arrow(a) => Some(Element::Literal(Literal::Arrow(a))), + Token::Logical(l) => Some(Element::Literal(Literal::Logical(l))), + Token::Relation(r) => Some(Element::Literal(Literal::Relation(r))), + Token::Greek(g) => Some(Element::Literal(Literal::Greek(g))), + Token::Function(f) => Some(Element::Literal(Literal::Function(f))), + Token::Text(t) => self.parse_text(t).map(Element::Literal), Token::Operation(op) => Some(self.parse_operation(op)), Token::Misc(m) => Some(self.parse_misc(m)), Token::Grouping(g) => { @@ -116,26 +110,12 @@ impl TreeParser { Some(Element::Group(group)) } else if let Some(group) = self.parse_vector() { Some(Element::Group(group)) - } else if let Some(group) = self.parse_group(g) { - Some(Element::Group(group)) } else { - None - } - } - Token::Font(f) => { - if let Some(literal) = self.parse_formatted_text(f) { - Some(Element::Literal(literal)) - } else { - None - } - } - Token::Accent(a) => { - if let Some(accent) = self.parse_accent(a) { - Some(Element::Accent(accent)) - } else { - None + self.parse_group(g).map(Element::Group) } } + Token::Font(f) => self.parse_formatted_text(f).map(Element::Literal), + Token::Accent(a) => self.parse_accent(a).map(Element::Accent), _ => None, } } @@ -172,11 +152,7 @@ impl TreeParser { } fn parse_formatted_text(&mut self, token: FontCommand) -> Option { - let next_token = if let Some(token) = self.peek() { - Some(token.clone()) - } else { - None - }; + let next_token = self.peek().cloned(); if let Some(Token::Text(Text::Plain(p))) = next_token { self.step(); Some(Literal::Text(PlainText { @@ -205,20 +181,14 @@ impl TreeParser { fn parse_operation(&mut self, token: Operation) -> Element { match token { - Operation::Sum => { - let mut sum = Sum::new(); - sum.bottom = self.parse_sub(); - sum.top = self.parse_pow(); - - Element::Special(Special::Sum(sum)) - } - Operation::Prod => { - let mut prod = Prod::new(); - prod.bottom = self.parse_sub(); - prod.top = self.parse_pow(); - - Element::Special(Special::Prod(prod)) - } + Operation::Sum => Element::Special(Special::Sum(Sum { + bottom: self.parse_sub(), + top: self.parse_pow(), + })), + Operation::Prod => Element::Special(Special::Prod(Prod { + bottom: self.parse_sub(), + top: self.parse_pow(), + })), _ => Element::Literal(Literal::Operation(token)), } } @@ -243,7 +213,7 @@ impl TreeParser { let base = self.parse_element().unwrap_or(Element::Null).boxed(); self.step(); let inner = self.parse_element().unwrap_or(Element::Null).boxed(); - Element::Special(Special::Root(Root { inner, base })) + Element::Special(Special::Root(Root { base, inner })) } Misc::Int => Element::Special(Special::Integral(Integral { bottom: self.parse_sub(), @@ -401,11 +371,8 @@ impl TreeParser { if let Some(Token::Misc(Misc::Sub)) = self.peek() { self.step(); self.step(); - if let Some(element) = self.parse_element() { - Some(element.to_non_enclosed().boxed()) - } else { - None - } + self.parse_element() + .map(|element| element.to_non_enclosed().boxed()) } else { None } @@ -415,11 +382,8 @@ impl TreeParser { if let Some(Token::Misc(Misc::Pow)) = self.peek() { self.step(); self.step(); - if let Some(element) = self.parse_element() { - Some(element.to_non_enclosed().boxed()) - } else { - None - } + self.parse_element() + .map(|element| element.to_non_enclosed().boxed()) } else { None } @@ -494,7 +458,7 @@ impl TreeParser { } /// Validates a matrix of expressions if every row has the same length - fn validate_matrix(&self, matrix: &Vec>) -> bool { + fn validate_matrix(&self, matrix: &[Vec]) -> bool { if matrix.is_empty() { false } else { diff --git a/src/tests/parsing.rs b/src/tests/parsing.rs index ee8e892..7619b63 100644 --- a/src/tests/parsing.rs +++ b/src/tests/parsing.rs @@ -15,7 +15,7 @@ fn it_parses_into_a_tree1() { let tokens = tokenizer.parse(); let mut tree_parser = TreeParser::new(tokens.clone()); let expression = tree_parser.parse(); - let mut test_expression = Expression::new(); + let mut test_expression = Expression::default(); test_expression.add_child(Element::Special(Special::Sum(Sum { bottom: Some( Element::Literal(Literal::Number(Number { diff --git a/src/tokens/constants/accents.rs b/src/tokens/constants/accents.rs index cbd0bdc..7172793 100644 --- a/src/tokens/constants/accents.rs +++ b/src/tokens/constants/accents.rs @@ -1,12 +1,12 @@ -pub const G_HAT: &'static [&str] = &["hat"]; -pub const G_OVERLINE: &'static [&str] = &["bar", "overline"]; -pub const G_UNDERLINE: &'static [&str] = &["ul", "underline"]; -pub const G_VEC: &'static [&str] = &["vec"]; -pub const G_DOT: &'static [&str] = &["dot"]; -pub const G_DDOT: &'static [&str] = &["ddot"]; -pub const G_OVERSET: &'static [&str] = &["overset"]; -pub const G_UNDERSET: &'static [&str] = &["underset"]; -pub const G_UNDERBRACE: &'static [&str] = &["ubrace", "underbrace"]; -pub const G_OVERBRACE: &'static [&str] = &["obrace", "overbrace"]; -pub const G_COLOR: &'static [&str] = &["color("]; -pub const G_CANCEL: &'static [&str] = &["cancel"]; +pub const G_HAT: &[&str] = &["hat"]; +pub const G_OVERLINE: &[&str] = &["bar", "overline"]; +pub const G_UNDERLINE: &[&str] = &["ul", "underline"]; +pub const G_VEC: &[&str] = &["vec"]; +pub const G_DOT: &[&str] = &["dot"]; +pub const G_DDOT: &[&str] = &["ddot"]; +pub const G_OVERSET: &[&str] = &["overset"]; +pub const G_UNDERSET: &[&str] = &["underset"]; +pub const G_UNDERBRACE: &[&str] = &["ubrace", "underbrace"]; +pub const G_OVERBRACE: &[&str] = &["obrace", "overbrace"]; +pub const G_COLOR: &[&str] = &["color("]; +pub const G_CANCEL: &[&str] = &["cancel"]; diff --git a/src/tokens/constants/arrows.rs b/src/tokens/constants/arrows.rs index d305230..d88af89 100644 --- a/src/tokens/constants/arrows.rs +++ b/src/tokens/constants/arrows.rs @@ -1,13 +1,13 @@ -pub const G_UPARROW: &'static[&str] = &["uarr", "uparrow"]; -pub const G_DOWNARROW: &'static[&str] = &["darr", "downarrow"]; -pub const G_RIGHTARROW: &'static[&str] = &["rarr", "rightarrow"]; -pub const G_TO: &'static[&str] = &["->", "to"]; -pub const G_RIGHTARROWTAIL: &'static[&str] = &[">->", "rightarrowtail"]; -pub const G_TWOHEADRIGHTARROW: &'static[&str] = &["->>", "twoheadrightarrow"]; -pub const G_TWOHEADRIGHTARROWTAIL: &'static[&str]= &[">->>", "twoheadrightarrowtail"]; -pub const G_MAPSTO: &'static[&str] = &["|->", "mapsto"]; -pub const G_LEFTARROW: &'static[&str] = &["larr", "leftarrow"]; -pub const G_LEFTRIGHTARROW: &'static[&str] = &["harr", "leftrightarrow"]; -pub const G_BIGRIGHTARROW: &'static[&str] = &["rArr", "Rightarrow"]; -pub const G_BIGLEFTARROW: &'static[&str]= &["lArr", "Leftarrow"]; -pub const G_BIGLEFTRIGHTARROW: &'static[&str] = &["hArr", "Leftrightarrow"]; \ No newline at end of file +pub const G_UPARROW: &[&str] = &["uarr", "uparrow"]; +pub const G_DOWNARROW: &[&str] = &["darr", "downarrow"]; +pub const G_RIGHTARROW: &[&str] = &["rarr", "rightarrow"]; +pub const G_TO: &[&str] = &["->", "to"]; +pub const G_RIGHTARROWTAIL: &[&str] = &[">->", "rightarrowtail"]; +pub const G_TWOHEADRIGHTARROW: &[&str] = &["->>", "twoheadrightarrow"]; +pub const G_TWOHEADRIGHTARROWTAIL: &[&str] = &[">->>", "twoheadrightarrowtail"]; +pub const G_MAPSTO: &[&str] = &["|->", "mapsto"]; +pub const G_LEFTARROW: &[&str] = &["larr", "leftarrow"]; +pub const G_LEFTRIGHTARROW: &[&str] = &["harr", "leftrightarrow"]; +pub const G_BIGRIGHTARROW: &[&str] = &["rArr", "Rightarrow"]; +pub const G_BIGLEFTARROW: &[&str] = &["lArr", "Leftarrow"]; +pub const G_BIGLEFTRIGHTARROW: &[&str] = &["hArr", "Leftrightarrow"]; diff --git a/src/tokens/constants/greek.rs b/src/tokens/constants/greek.rs index adaf354..bb66440 100644 --- a/src/tokens/constants/greek.rs +++ b/src/tokens/constants/greek.rs @@ -1,36 +1,36 @@ -pub const G_ALPHA: &'static[&str] = &["alpha"]; -pub const G_BETA: &'static[&str] = &["beta"]; -pub const G_GAMMA: &'static[&str] = &["gamma"]; -pub const G_BIGGAMMA: &'static[&str] = &["Gamma"]; -pub const G_DELTA: &'static[&str] = &["delta"]; -pub const G_BIGDELTA: &'static[&str] = &["Delta"]; -pub const G_EPSILON: &'static[&str] = &["epsilon"]; -pub const G_VAREPSILON: &'static[&str] = &["varepsilon"]; -pub const G_ZETA: &'static[&str] = &["zeta"]; -pub const G_ETA: &'static[&str] = &["eta"]; -pub const G_THETA: &'static[&str] = &["theta"]; -pub const G_BIGTHETA: &'static[&str] = &["Theta"]; -pub const G_VARTHETA: &'static[&str] = &["vartheta"]; -pub const G_IOTA: &'static[&str] = &["iota"]; -pub const G_KAPPA: &'static[&str] = &["kappa"]; -pub const G_LAMBDA: &'static[&str] = &["lambda"]; -pub const G_BIGLAMBDA: &'static[&str] = &["Lambda"]; -pub const G_MU: &'static[&str] = &["mu"]; -pub const G_NU: &'static[&str] = &["nu"]; -pub const G_XI: &'static[&str] = &["xi"]; -pub const G_BIGXI: &'static[&str] = &["Xi"]; -pub const G_PI: &'static[&str] = &["pi"]; -pub const G_BIGPI: &'static[&str] = &["Pi"]; -pub const G_RHO: &'static[&str] = &["rho"]; -pub const G_SIGMA: &'static[&str] = &["sigma"]; -pub const G_BIGSIGMA: &'static[&str] = &["Sigma"]; -pub const G_TAU: &'static[&str] = &["tau"]; -pub const G_UPSILON: &'static[&str] = &["upsilon"]; -pub const G_PHI: &'static[&str] = &["phi"]; -pub const G_BIGPHI: &'static[&str] = &["Phi"]; -pub const G_VARPHI: &'static[&str] = &["varphi"]; -pub const G_CHI: &'static[&str] = &["chi"]; -pub const G_PSI: &'static[&str] = &["psi"]; -pub const G_BIGPSI: &'static[&str] = &["Psi"]; -pub const G_OMEGA: &'static[&str] = &["omega"]; -pub const G_BIGOMEGA: &'static[&str] = &["Omega"]; \ No newline at end of file +pub const G_ALPHA: &[&str] = &["alpha"]; +pub const G_BETA: &[&str] = &["beta"]; +pub const G_GAMMA: &[&str] = &["gamma"]; +pub const G_BIGGAMMA: &[&str] = &["Gamma"]; +pub const G_DELTA: &[&str] = &["delta"]; +pub const G_BIGDELTA: &[&str] = &["Delta"]; +pub const G_EPSILON: &[&str] = &["epsilon"]; +pub const G_VAREPSILON: &[&str] = &["varepsilon"]; +pub const G_ZETA: &[&str] = &["zeta"]; +pub const G_ETA: &[&str] = &["eta"]; +pub const G_THETA: &[&str] = &["theta"]; +pub const G_BIGTHETA: &[&str] = &["Theta"]; +pub const G_VARTHETA: &[&str] = &["vartheta"]; +pub const G_IOTA: &[&str] = &["iota"]; +pub const G_KAPPA: &[&str] = &["kappa"]; +pub const G_LAMBDA: &[&str] = &["lambda"]; +pub const G_BIGLAMBDA: &[&str] = &["Lambda"]; +pub const G_MU: &[&str] = &["mu"]; +pub const G_NU: &[&str] = &["nu"]; +pub const G_XI: &[&str] = &["xi"]; +pub const G_BIGXI: &[&str] = &["Xi"]; +pub const G_PI: &[&str] = &["pi"]; +pub const G_BIGPI: &[&str] = &["Pi"]; +pub const G_RHO: &[&str] = &["rho"]; +pub const G_SIGMA: &[&str] = &["sigma"]; +pub const G_BIGSIGMA: &[&str] = &["Sigma"]; +pub const G_TAU: &[&str] = &["tau"]; +pub const G_UPSILON: &[&str] = &["upsilon"]; +pub const G_PHI: &[&str] = &["phi"]; +pub const G_BIGPHI: &[&str] = &["Phi"]; +pub const G_VARPHI: &[&str] = &["varphi"]; +pub const G_CHI: &[&str] = &["chi"]; +pub const G_PSI: &[&str] = &["psi"]; +pub const G_BIGPSI: &[&str] = &["Psi"]; +pub const G_OMEGA: &[&str] = &["omega"]; +pub const G_BIGOMEGA: &[&str] = &["Omega"]; diff --git a/src/tokens/constants/grouping.rs b/src/tokens/constants/grouping.rs index a77371e..b1b20f3 100644 --- a/src/tokens/constants/grouping.rs +++ b/src/tokens/constants/grouping.rs @@ -1,24 +1,24 @@ -pub const G_RPAREN: &'static [&str] = &["("]; -pub const G_LPAREN: &'static [&str] = &[")"]; +pub const G_RPAREN: &[&str] = &["("]; +pub const G_LPAREN: &[&str] = &[")"]; -pub const G_RBRACKET: &'static [&str] = &["["]; -pub const G_LBRACKET: &'static [&str] = &["]"]; +pub const G_RBRACKET: &[&str] = &["["]; +pub const G_LBRACKET: &[&str] = &["]"]; -pub const G_RBRACE: &'static [&str] = &["{"]; -pub const G_LBRACE: &'static [&str] = &["}"]; +pub const G_RBRACE: &[&str] = &["{"]; +pub const G_LBRACE: &[&str] = &["}"]; -pub const G_RBRACE_HIDDEN: &'static [&str] = &["{:"]; -pub const G_LBRACE_HIDDEN: &'static [&str] = &[":}"]; +pub const G_RBRACE_HIDDEN: &[&str] = &["{:"]; +pub const G_LBRACE_HIDDEN: &[&str] = &[":}"]; -pub const G_LANGLE: &'static [&str] = &["(:", "<<", "langle"]; -pub const G_RANGLE: &'static [&str] = &[":)", ">>", "rangle"]; -pub const G_LXPAR: &'static [&str] = &["{: x )"]; -pub const G_RXPAR: &'static [&str] = &["( x :}"]; -pub const G_ABS: &'static [&str] = &["abs"]; -pub const G_FLOOR: &'static [&str] = &["floor"]; -pub const G_CEIL: &'static [&str] = &["ceil"]; -pub const G_NORM: &'static [&str] = &["norm"]; +pub const G_LANGLE: &[&str] = &["(:", "<<", "langle"]; +pub const G_RANGLE: &[&str] = &[":)", ">>", "rangle"]; +pub const G_LXPAR: &[&str] = &["{: x )"]; +pub const G_RXPAR: &[&str] = &["( x :}"]; +pub const G_ABS: &[&str] = &["abs"]; +pub const G_FLOOR: &[&str] = &["floor"]; +pub const G_CEIL: &[&str] = &["ceil"]; +pub const G_NORM: &[&str] = &["norm"]; -pub const G_MATRIX_SEP: &'static [&str] = &[","]; +pub const G_MATRIX_SEP: &[&str] = &[","]; pub const T_LPAREN: char = ')'; diff --git a/src/tokens/constants/logical.rs b/src/tokens/constants/logical.rs index 1417823..e2341a7 100644 --- a/src/tokens/constants/logical.rs +++ b/src/tokens/constants/logical.rs @@ -1,12 +1,12 @@ -pub const G_AND: &'static[&str] = &["and"]; -pub const G_OR: &'static[&str] = &["or"]; -pub const G_NOT: &'static[&str] = &["not", "neg"]; -pub const G_IMPLIES: &'static[&str] = &["=>", "implies"]; -pub const G_IF: &'static[&str] = &["if"]; -pub const G_IFF: &'static[&str] = &["<=>", "iff"]; -pub const G_FORALL: &'static[&str] = &["AA", "forall"]; -pub const G_EXISTS: &'static[&str] = &["EE", "exists"]; -pub const G_BOT: &'static[&str] = &["_|_", "bot"]; -pub const G_TOP: &'static[&str] = &["TT", "top"]; -pub const G_VDASH: &'static[&str] = &["|--", "vdash"]; -pub const G_MODELS: &'static[&str] = &["|==", "models"]; \ No newline at end of file +pub const G_AND: &[&str] = &["and"]; +pub const G_OR: &[&str] = &["or"]; +pub const G_NOT: &[&str] = &["not", "neg"]; +pub const G_IMPLIES: &[&str] = &["=>", "implies"]; +pub const G_IF: &[&str] = &["if"]; +pub const G_IFF: &[&str] = &["<=>", "iff"]; +pub const G_FORALL: &[&str] = &["AA", "forall"]; +pub const G_EXISTS: &[&str] = &["EE", "exists"]; +pub const G_BOT: &[&str] = &["_|_", "bot"]; +pub const G_TOP: &[&str] = &["TT", "top"]; +pub const G_VDASH: &[&str] = &["|--", "vdash"]; +pub const G_MODELS: &[&str] = &["|==", "models"]; diff --git a/src/tokens/constants/misc.rs b/src/tokens/constants/misc.rs index 2505cde..3d1240d 100644 --- a/src/tokens/constants/misc.rs +++ b/src/tokens/constants/misc.rs @@ -1,46 +1,46 @@ -pub const G_A_FRAC: &'static [&str] = &["/"]; -pub const G_T_FRAC: &'static [&str] = &["frac"]; +pub const G_A_FRAC: &[&str] = &["/"]; +pub const G_T_FRAC: &[&str] = &["frac"]; -pub const G_SUB: &'static [&str] = &["_"]; -pub const G_POW: &'static [&str] = &["^"]; -pub const G_SQRT: &'static [&str] = &["sqrt"]; -pub const G_ROOT: &'static [&str] = &["root"]; -pub const G_INT: &'static [&str] = &["int"]; -pub const G_OINT: &'static [&str] = &["oint"]; -pub const G_DEL: &'static [&str] = &["del", "partial"]; -pub const G_GRAD: &'static [&str] = &["grad", "nbla"]; -pub const G_PM: &'static [&str] = &["+-", "pm"]; -pub const G_EMPTYSET: &'static [&str] = &["O/", "emptyset"]; -pub const G_INFTY: &'static [&str] = &["oo", "infty"]; -pub const G_ALEPH: &'static [&str] = &["aleph"]; -pub const G_THEREFORE: &'static [&str] = &[":.", "therefore"]; -pub const G_BECAUSE: &'static [&str] = &[":'", "because"]; -pub const G_ELDOTS: &'static [&str] = &["|...|", "|ldots|"]; -pub const G_ECDOTS: &'static [&str] = &["|cdots|"]; -pub const G_VDOTS: &'static [&str] = &["vdots"]; -pub const G_DDOTS: &'static [&str] = &["ddots"]; -pub const G_EPIPES: &'static [&str] = &["|\\ |"]; -pub const G_QUAD: &'static [&str] = &["|quad|"]; -pub const G_ANGLE: &'static [&str] = &["/_", "angle"]; -pub const G_FROWN: &'static [&str] = &["frown"]; -pub const G_TRIANGLE: &'static [&str] = &["/_\\", "triangle"]; -pub const G_DIAMOND: &'static [&str] = &["diamond"]; -pub const G_SQUARE: &'static [&str] = &["square"]; -pub const G_LFLOOR: &'static [&str] = &["|__", "lfloor"]; -pub const G_RFLOOR: &'static [&str] = &["__|", "rfloor"]; -pub const G_LCEILING: &'static [&str] = &["|~", "lceiling"]; -pub const G_RCEILING: &'static [&str] = &["~|", "rceiling"]; +pub const G_SUB: &[&str] = &["_"]; +pub const G_POW: &[&str] = &["^"]; +pub const G_SQRT: &[&str] = &["sqrt"]; +pub const G_ROOT: &[&str] = &["root"]; +pub const G_INT: &[&str] = &["int"]; +pub const G_OINT: &[&str] = &["oint"]; +pub const G_DEL: &[&str] = &["del", "partial"]; +pub const G_GRAD: &[&str] = &["grad", "nbla"]; +pub const G_PM: &[&str] = &["+-", "pm"]; +pub const G_EMPTYSET: &[&str] = &["O/", "emptyset"]; +pub const G_INFTY: &[&str] = &["oo", "infty"]; +pub const G_ALEPH: &[&str] = &["aleph"]; +pub const G_THEREFORE: &[&str] = &[":.", "therefore"]; +pub const G_BECAUSE: &[&str] = &[":'", "because"]; +pub const G_ELDOTS: &[&str] = &["|...|", "|ldots|"]; +pub const G_ECDOTS: &[&str] = &["|cdots|"]; +pub const G_VDOTS: &[&str] = &["vdots"]; +pub const G_DDOTS: &[&str] = &["ddots"]; +pub const G_EPIPES: &[&str] = &["|\\ |"]; +pub const G_QUAD: &[&str] = &["|quad|"]; +pub const G_ANGLE: &[&str] = &["/_", "angle"]; +pub const G_FROWN: &[&str] = &["frown"]; +pub const G_TRIANGLE: &[&str] = &["/_\\", "triangle"]; +pub const G_DIAMOND: &[&str] = &["diamond"]; +pub const G_SQUARE: &[&str] = &["square"]; +pub const G_LFLOOR: &[&str] = &["|__", "lfloor"]; +pub const G_RFLOOR: &[&str] = &["__|", "rfloor"]; +pub const G_LCEILING: &[&str] = &["|~", "lceiling"]; +pub const G_RCEILING: &[&str] = &["~|", "rceiling"]; -pub const G_COMPLEX: &'static [&str] = &["CC"]; -pub const G_NATURAL: &'static [&str] = &["NN"]; -pub const G_RATIONAL: &'static [&str] = &["QQ"]; -pub const G_REAL: &'static [&str] = &["RR"]; -pub const G_INTEGER: &'static [&str] = &["ZZ"]; +pub const G_COMPLEX: &[&str] = &["CC"]; +pub const G_NATURAL: &[&str] = &["NN"]; +pub const G_RATIONAL: &[&str] = &["QQ"]; +pub const G_REAL: &[&str] = &["RR"]; +pub const G_INTEGER: &[&str] = &["ZZ"]; pub const A_TEXT: char = '"'; -pub const G_T_TEX: &'static [&str] = &["text"]; +pub const G_T_TEX: &[&str] = &["text"]; pub const A_NUMCOMMA: char = '.'; pub const A_SCIEXP: char = 'e'; -pub const G_NEWLINE: &'static [&str] = &["\\\n"]; +pub const G_NEWLINE: &[&str] = &["\\\n"]; -pub const G_NUMALLOWED: &'static [char] = &[A_NUMCOMMA, A_SCIEXP]; +pub const G_NUMALLOWED: &[char] = &[A_NUMCOMMA, A_SCIEXP]; diff --git a/src/tokens/constants/operations.rs b/src/tokens/constants/operations.rs index ee63dac..b3b64a5 100644 --- a/src/tokens/constants/operations.rs +++ b/src/tokens/constants/operations.rs @@ -1,26 +1,26 @@ -pub const G_PLUS: &'static [&str] = &["+"]; -pub const G_MINUS: &'static [&str] = &["-"]; -pub const G_CDOT: &'static [&str] = &["*", "cdot"]; -pub const G_AST: &'static [&str] = &["**", "ast"]; -pub const G_STAR: &'static [&str] = &["***", "star"]; -pub const G_SLASH: &'static [&str] = &["//"]; -pub const G_BACKSLASH: &'static [&str] = &["\\\\", "backslash", "setminus"]; -pub const G_TIMES: &'static [&str] = &["xx", "times"]; -pub const G_DIV: &'static [&str] = &["-:", "div"]; -pub const G_LTIMES: &'static [&str] = &["|><", "ltimes"]; -pub const G_RTIMES: &'static [&str] = &["><|", "rtimes"]; -pub const G_BOWTIE: &'static [&str] = &["|><|", "bowtie"]; -pub const G_CIRC: &'static [&str] = &["@", "circ"]; -pub const G_OPLUS: &'static [&str] = &["o+", "oplus"]; -pub const G_OTIMES: &'static [&str] = &["ox", "otimes"]; -pub const G_ODOT: &'static [&str] = &["o.", "odot"]; -pub const G_SUM: &'static [&str] = &["sum"]; -pub const G_PROD: &'static [&str] = &["prod"]; -pub const G_WEDGE: &'static [&str] = &["^^", "wedge"]; -pub const G_BIDWEDGE: &'static [&str] = &["^^^", "bidwedge"]; -pub const G_VEE: &'static [&str] = &["vv", "vee"]; -pub const G_BIGVEE: &'static [&str] = &["vvv", "bigvee"]; -pub const G_CAP: &'static [&str] = &["nn", "cap"]; -pub const G_BIGCAP: &'static [&str] = &["nnn", "bigcap"]; -pub const G_CUP: &'static [&str] = &["uu", "cup"]; -pub const G_BIGCUP: &'static [&str] = &["uuu", "bigcup"]; +pub const G_PLUS: &[&str] = &["+"]; +pub const G_MINUS: &[&str] = &["-"]; +pub const G_CDOT: &[&str] = &["*", "cdot"]; +pub const G_AST: &[&str] = &["**", "ast"]; +pub const G_STAR: &[&str] = &["***", "star"]; +pub const G_SLASH: &[&str] = &["//"]; +pub const G_BACKSLASH: &[&str] = &["\\\\", "backslash", "setminus"]; +pub const G_TIMES: &[&str] = &["xx", "times"]; +pub const G_DIV: &[&str] = &["-:", "div"]; +pub const G_LTIMES: &[&str] = &["|><", "ltimes"]; +pub const G_RTIMES: &[&str] = &["><|", "rtimes"]; +pub const G_BOWTIE: &[&str] = &["|><|", "bowtie"]; +pub const G_CIRC: &[&str] = &["@", "circ"]; +pub const G_OPLUS: &[&str] = &["o+", "oplus"]; +pub const G_OTIMES: &[&str] = &["ox", "otimes"]; +pub const G_ODOT: &[&str] = &["o.", "odot"]; +pub const G_SUM: &[&str] = &["sum"]; +pub const G_PROD: &[&str] = &["prod"]; +pub const G_WEDGE: &[&str] = &["^^", "wedge"]; +pub const G_BIDWEDGE: &[&str] = &["^^^", "bidwedge"]; +pub const G_VEE: &[&str] = &["vv", "vee"]; +pub const G_BIGVEE: &[&str] = &["vvv", "bigvee"]; +pub const G_CAP: &[&str] = &["nn", "cap"]; +pub const G_BIGCAP: &[&str] = &["nnn", "bigcap"]; +pub const G_CUP: &[&str] = &["uu", "cup"]; +pub const G_BIGCUP: &[&str] = &["uuu", "bigcup"]; diff --git a/src/tokens/constants/relations.rs b/src/tokens/constants/relations.rs index a566f48..f3aaabc 100644 --- a/src/tokens/constants/relations.rs +++ b/src/tokens/constants/relations.rs @@ -1,20 +1,20 @@ -pub const G_EQ: &'static[&str] = &["="]; -pub const G_NE: &'static[&str] = &["!=", "ne"]; -pub const G_LT: &'static[&str] = &["<", "lt"]; -pub const G_GT: &'static[&str] = &[">", "gt"]; -pub const G_LE: &'static[&str] = &["<=", "le"]; -pub const G_GE: &'static[&str] = &[">=", "ge"]; -pub const G_PREC: &'static[&str] = &["-<", "prec"]; -pub const G_PRECEQ: &'static[&str] = &["-<=", "preceq"]; -pub const G_SUCC: &'static[&str] = &[">-", "succ"]; -pub const G_SUCCEQ: &'static[&str] = &[">-=", "succeq"]; -pub const G_IN: &'static[&str] = &["in"]; -pub const G_NOTIN: &'static[&str] = &["!in", "notin"]; -pub const G_SUBSET: &'static[&str] = &["sub", "subset"]; -pub const G_SUPSET: &'static[&str] = &["sup", "supset"]; -pub const G_SUBSETEQ: &'static[&str] = &["sube", "subseteq"]; -pub const G_SUPSETEQ: &'static[&str] = &["supe", "supseteq"]; -pub const G_EQUIV: &'static[&str] = &["-=", "equiv"]; -pub const G_CONG: &'static[&str] = &["~=", "cong"]; -pub const G_APPROX: &'static[&str] = &["~~", "approx"]; -pub const G_PROP: &'static[&str] = &["prop", "propto"]; \ No newline at end of file +pub const G_EQ: &[&str] = &["="]; +pub const G_NE: &[&str] = &["!=", "ne"]; +pub const G_LT: &[&str] = &["<", "lt"]; +pub const G_GT: &[&str] = &[">", "gt"]; +pub const G_LE: &[&str] = &["<=", "le"]; +pub const G_GE: &[&str] = &[">=", "ge"]; +pub const G_PREC: &[&str] = &["-<", "prec"]; +pub const G_PRECEQ: &[&str] = &["-<=", "preceq"]; +pub const G_SUCC: &[&str] = &[">-", "succ"]; +pub const G_SUCCEQ: &[&str] = &[">-=", "succeq"]; +pub const G_IN: &[&str] = &["in"]; +pub const G_NOTIN: &[&str] = &["!in", "notin"]; +pub const G_SUBSET: &[&str] = &["sub", "subset"]; +pub const G_SUPSET: &[&str] = &["sup", "supset"]; +pub const G_SUBSETEQ: &[&str] = &["sube", "subseteq"]; +pub const G_SUPSETEQ: &[&str] = &["supe", "supseteq"]; +pub const G_EQUIV: &[&str] = &["-=", "equiv"]; +pub const G_CONG: &[&str] = &["~=", "cong"]; +pub const G_APPROX: &[&str] = &["~~", "approx"]; +pub const G_PROP: &[&str] = &["prop", "propto"]; diff --git a/src/tokens/mod.rs b/src/tokens/mod.rs index 89ca4eb..bbffea8 100644 --- a/src/tokens/mod.rs +++ b/src/tokens/mod.rs @@ -1,3 +1,7 @@ +use std::fmt; +use std::fmt::Display; +use std::fmt::Formatter; + pub mod constants; pub mod mappings; @@ -272,15 +276,19 @@ pub enum FontCommand { SansSerif, } -impl FontCommand { - pub fn to_string(&self) -> String { - match self { - FontCommand::BigOutline => "bbb".to_string(), - FontCommand::Big => "bb".to_string(), - FontCommand::SansSerif => "sf".to_string(), - FontCommand::Fr => "fr".to_string(), - FontCommand::TText => "tt".to_string(), - FontCommand::Cursive => "cc".to_string(), - } +impl Display for FontCommand { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match self { + FontCommand::BigOutline => "bbb", + FontCommand::Big => "bb", + FontCommand::SansSerif => "sf", + FontCommand::Fr => "fr", + FontCommand::TText => "tt", + FontCommand::Cursive => "cc", + } + ) } }