Add accent parsing

pull/1/head
trivernis 4 years ago
parent 74caac521f
commit 6f1d112729

@ -2,7 +2,7 @@
name = "asciimath-rs"
description = "AsciiMath parser"
repository = "https://github.com/trivernis/asciimath-rs"
version = "0.1.0"
version = "0.2.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
readme = "README.md"

@ -0,0 +1,34 @@
use crate::elements::Element;
use crate::tokens::Accent;
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum ExpressionAccent {
Generic(GenericAccent),
OverSet(OverSet),
UnderSet(UnderSet),
Color(Color),
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct GenericAccent {
pub inner: Box<Element>,
pub accent: Accent,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct OverSet {
pub top: Box<Element>,
pub bottom: Box<Element>,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct UnderSet {
pub top: Box<Element>,
pub bottom: Box<Element>,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct Color {
pub color: String,
pub inner: Box<Element>,
}

@ -1,8 +1,10 @@
use crate::elements::accent::ExpressionAccent;
use crate::elements::group::Group;
use crate::elements::literal::Literal;
use crate::elements::special::Special;
use crate::utils::Boxed;
pub mod accent;
pub mod group;
pub mod literal;
pub mod special;
@ -12,6 +14,7 @@ pub enum Element {
Literal(Literal),
Special(Special),
Group(Group),
Accent(ExpressionAccent),
}
impl Boxed for Element {}

@ -33,7 +33,6 @@ mod tests {
use crate::parse;
use crate::parsing::tokenizer::Tokenizer;
use crate::parsing::tree_parser::TreeParser;
use crate::tokens::Function::Exp;
use crate::tokens::{Function, Grouping, Misc, Operation, Relation, Text, Token};
use crate::utils::Boxed;
use std::fs;
@ -163,7 +162,7 @@ mod tests {
fn it_parses_into_a_tree2() {
fs::write(
"test-files/test.txt",
format!("{:#?}", parse("a * b^4 - c(c-2)".to_string())),
format!("{:#?}", parse("color(red)(a) * b^4 - c(c-2)".to_string())),
);
}

@ -1,3 +1,5 @@
use crate::tokens::constants::accents::G_COLOR;
use crate::tokens::constants::grouping::T_LPAREN;
use crate::tokens::constants::misc::{A_TEXT, G_NUMALLOWED};
use crate::tokens::constants::TokenPattern;
use crate::tokens::mappings::{
@ -160,17 +162,25 @@ impl Tokenizer {
}
fn parse_accent(&mut self) -> Option<Accent> {
lazy_static! {
static ref ACCENT_MAPPINGS: Vec<HashMap<TokenPattern, Accent>> = get_accent_mappings();
}
for mapping in ACCENT_MAPPINGS.iter() {
for key in mapping.keys() {
if self.ctm.check_any_str_sequence(*key) {
return Some(mapping[key].clone());
if self.ctm.check_any_str_sequence(G_COLOR) {
self.ctm.seek_one().unwrap();
Some(Accent::Color(
self.ctm.get_string_until_any(&[T_LPAREN], &[]).unwrap(),
))
} else {
lazy_static! {
static ref ACCENT_MAPPINGS: Vec<HashMap<TokenPattern, Accent>> =
get_accent_mappings();
}
for mapping in ACCENT_MAPPINGS.iter() {
for key in mapping.keys() {
if self.ctm.check_any_str_sequence(*key) {
return Some(mapping[key].clone());
}
}
}
None
}
None
}
fn parse_greek(&mut self) -> Option<Greek> {

@ -1,3 +1,4 @@
use crate::elements::accent::{Color, ExpressionAccent, GenericAccent, OverSet, UnderSet};
use crate::elements::group::{
Abs, Angles, Braces, Brackets, Ceil, Floor, Group, Norm, Parentheses, XGroup,
};
@ -6,7 +7,7 @@ use crate::elements::special::{
Expression, Frac, Integral, OIntegral, Pow, Prod, Root, Special, Sqrt, Sub, Sum,
};
use crate::elements::Element;
use crate::tokens::{FontCommand, Grouping, Misc, Operation, Text, Token};
use crate::tokens::{Accent, FontCommand, Grouping, Misc, Operation, Text, Token};
use crate::utils::Boxed;
pub struct TreeParser {
@ -114,10 +115,48 @@ impl TreeParser {
None
}
}
Token::Accent(a) => {
if let Some(accent) = self.parse_accent(a) {
Some(Element::Accent(accent))
} else {
None
}
}
_ => None,
}
}
fn parse_accent(&mut self, token: Accent) -> Option<ExpressionAccent> {
match token {
Accent::OverSet => {
self.step();
let top = self.parse_element()?.boxed();
self.step();
let bottom = self.parse_element()?.boxed();
Some(ExpressionAccent::OverSet(OverSet { top, bottom }))
}
Accent::UnderSet => {
self.step();
let bottom = self.parse_element()?.boxed();
self.step();
let top = self.parse_element()?.boxed();
Some(ExpressionAccent::UnderSet(UnderSet { top, bottom }))
}
Accent::Color(color) => {
self.step();
let inner = self.parse_element()?.boxed();
Some(ExpressionAccent::Color(Color { color, inner }))
}
_ => {
self.step();
Some(ExpressionAccent::Generic(GenericAccent {
inner: self.parse_element()?.boxed(),
accent: token,
}))
}
}
}
fn parse_formatted_text(&mut self, token: FontCommand) -> Option<Literal> {
let next_token = if let Some(token) = self.peek() {
Some(token.clone())

@ -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: &'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"];

@ -15,3 +15,5 @@ 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 T_LPAREN: char = ')';

@ -209,7 +209,6 @@ pub fn get_accent_mappings() -> Vec<HashMap<TokenPattern, Accent>> {
G_UNDERSET => Accent::UnderSet,
G_UNDERBRACE => Accent::UnderBrace,
G_OVERBRACE => Accent::OverBrace,
G_COLOR => Accent::Color,
G_CANCEL => Accent::Cancel,
}]
}

@ -183,7 +183,7 @@ pub enum Accent {
UnderSet,
UnderBrace,
OverBrace,
Color,
Color(String),
Cancel,
}

Loading…
Cancel
Save