Add font formatting parsing

pull/1/head
trivernis 4 years ago
parent d01700dfb6
commit ce19e13e45

@ -19,6 +19,7 @@ pub enum Literal {
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct PlainText {
pub(crate) text: String,
pub(crate) formatting: Option<FontCommand>,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]

@ -147,7 +147,7 @@ mod tests {
#[test]
fn it_parses_into_a_tree2() {
let str_expression = "sum_2^3 + abs(4^4) - 1^(2*2)_2";
let str_expression = "bb\"test\" bb";
let mut tokenizer = Tokenizer::new(str_expression.to_string());
let tokens = tokenizer.parse();
let mut tree_parser = TreeParser::new(tokens.clone());

@ -6,7 +6,7 @@ use crate::elements::special::{
Expression, Frac, Integral, OIntegral, Pow, Prod, Root, Special, Sqrt, Sub, Sum,
};
use crate::elements::Element;
use crate::tokens::{Grouping, Misc, Operation, Text, Token};
use crate::tokens::{FontCommand, Grouping, Misc, Operation, Text, Token};
use crate::utils::Boxed;
pub struct TreeParser {
@ -107,15 +107,44 @@ impl TreeParser {
None
}
}
Token::Font(f) => {
if let Some(literal) = self.parse_formatted_text(f) {
Some(Element::Literal(literal))
} else {
None
}
}
_ => None,
}
}
fn parse_formatted_text(&mut self, token: FontCommand) -> Option<Literal> {
let next_token = if let Some(token) = self.peek() {
Some(token.clone())
} else {
None
};
if let Some(Token::Text(Text::Plain(p))) = next_token {
self.step();
Some(Literal::Text(PlainText {
text: p,
formatting: Some(token),
}))
} else {
Some(Literal::Symbol(Symbol {
symbol: token.to_string(),
}))
}
}
fn parse_text(&self, token: Text) -> Option<Literal> {
match token {
Text::Symbol(s) => Some(Literal::Symbol(Symbol { symbol: s })),
Text::Number(n) => Some(Literal::Number(Number { number: n })),
Text::Plain(p) => Some(Literal::Text(PlainText { text: p })),
Text::Plain(p) => Some(Literal::Text(PlainText {
text: p,
formatting: None,
})),
_ => None,
}
}

@ -227,16 +227,6 @@ pub enum Greek {
BigOmega,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum FontCommand {
Big,
BigOutline,
Cursive,
TText,
Fr,
SansSerif,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum Function {
Sin,
@ -269,3 +259,26 @@ pub enum Function {
F,
G,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum FontCommand {
Big,
BigOutline,
Cursive,
TText,
Fr,
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(),
}
}
}

Loading…
Cancel
Save