Add function constants, mapping and parsing

pull/1/head
trivernis 4 years ago
parent 01c9203bac
commit 7c345c83ec

@ -0,0 +1,119 @@
use crate::tokens::{FontCommand, Greek};
#[derive(Clone, Debug)]
pub enum Literal {
Plus,
Minus,
CDot,
Ast,
Star,
Slash,
Backslash,
Times,
Div,
LTimes,
RTimes,
Bowtie,
Circ,
OPlus,
OTimes,
ODot,
Wedge,
BidWedge,
Vee,
Cap,
BigCap,
Cup,
BigCup,
Del,
Grad,
PlusMinus,
EmptySet,
Infty,
Aleph,
Therefore,
Because,
LDots,
CDots,
VDots,
DDots,
EPipes,
Quad,
Angle,
Frown,
Triangle,
Diamond,
Square,
LFloor,
RFloor,
LCeiling,
RCeiling,
Complex,
Natural,
Rational,
Real,
Integer,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
Prec,
PrecEq,
Succ,
SuccEq,
In,
NotIn,
Subset,
SupSet,
SubSetEq,
SupSetEq,
Equiv,
Cong,
Approx,
PropTo,
And,
Or,
Not,
Implies,
If,
ForAll,
Exists,
Bot,
Top,
VDash,
Models,
UpArrow,
DownArrow,
RightArrow,
To,
RightArrowTail,
TwoHeadRightArrrow,
MapsTo,
LeftArrow,
LeftRightArrow,
BigRightArrow,
BigLeftArrow,
BigLeftRightArrow,
Text(TextNode),
Symbol(SymbolNode),
Number(NumberNode),
Greek(Greek),
FontCommand(FontCommand),
}
#[derive(Clone, Debug)]
pub struct TextNode {
text: String,
}
#[derive(Clone, Debug)]
pub struct SymbolNode {
symbol: String,
}
#[derive(Clone, Debug)]
pub struct NumberNode {
number: String,
}

@ -1,2 +1,5 @@
pub mod literal;
pub mod enclosing;
pub mod special;
#[derive(Debug, Clone)]
pub enum Element {}

@ -0,0 +1,6 @@
use crate::elements::Element;
#[derive(Clone, Debug)]
pub struct Root {
children: Vec<Element>,
}

@ -8,18 +8,18 @@ extern crate maplit;
extern crate lazy_static;
pub mod elements;
pub mod tokenizer;
pub mod parsing;
mod tokens;
#[cfg(test)]
mod tests {
use crate::tokenizer::Tokenizer;
use crate::tokens::{Grouping, Misc, Operation, Relation, Text, Token};
use crate::parsing::tokenizer::Tokenizer;
use crate::tokens::{Function, Grouping, Misc, Operation, Relation, Text, Token};
use test::Bencher;
#[test]
fn it_tokenizes_expressions1() {
let expression = "sum_(i=1)^n";
let expression = "sum_(i=1)^n*sin(x)";
let mut tokenizer = Tokenizer::new(expression.to_string());
let tokens = tokenizer.parse();
assert_eq!(
@ -33,7 +33,12 @@ mod tests {
Token::Text(Text::Number("1".to_string())),
Token::Grouping(Grouping::LParen),
Token::Misc(Misc::Pow),
Token::Text(Text::Symbol("n".to_string()))
Token::Text(Text::Symbol("n".to_string())),
Token::Operation(Operation::CDot),
Token::Function(Function::Sin),
Token::Grouping(Grouping::RParen),
Token::Text(Text::Symbol("x".to_string())),
Token::Grouping(Grouping::LParen),
]
);
}

@ -0,0 +1,2 @@
pub mod tokenizer;
pub mod tree_parser;

@ -1,12 +1,13 @@
use crate::tokens::constants::misc::{A_TEXT, G_NUMALLOWED};
use crate::tokens::constants::TokenPattern;
use crate::tokens::mappings::{
get_accent_mappings, get_arrow_mapping, get_font_mappings, get_greek_mappings,
get_grouping_mappings, get_logical_mappings, get_misc_mappings, get_operation_mappings,
get_relation_mapping,
get_accent_mappings, get_arrow_mapping, get_font_mappings, get_function_mappings,
get_greek_mappings, get_grouping_mappings, get_logical_mappings, get_misc_mappings,
get_operation_mappings, get_relation_mapping,
};
use crate::tokens::{
Accent, Arrow, FontCommand, Greek, Grouping, Logical, Misc, Operation, Relation, Text, Token,
Accent, Arrow, FontCommand, Function, Greek, Grouping, Logical, Misc, Operation, Relation,
Text, Token,
};
use charred::tapemachine::CharTapeMachine;
use std::collections::HashMap;
@ -47,6 +48,8 @@ impl Tokenizer {
tokens.push(Token::Greek(greek))
} else if let Some(font) = self.parse_font_command() {
tokens.push(Token::Font(font))
} else if let Some(function) = self.parse_function() {
tokens.push(Token::Function(function))
} else if let Some(whitespace) = self.parse_whitespace() {
tokens.push(Token::Text(whitespace))
} else if let Some(text) = self.parse_text() {
@ -198,6 +201,21 @@ impl Tokenizer {
None
}
fn parse_function(&mut self) -> Option<Function> {
lazy_static! {
static ref FUNCTION_MAPPING: Vec<HashMap<&'static str, Function>> =
get_function_mappings();
}
for mapping in FUNCTION_MAPPING.iter() {
for key in mapping.keys() {
if self.ctm.check_str_sequence(*key) {
return Some(mapping[key].clone());
}
}
}
None
}
fn parse_whitespace(&mut self) -> Option<Text> {
if self.ctm.get_current().is_whitespace() {
self.ctm.seek_whitespace();

@ -0,0 +1,5 @@
use crate::tokens::Token;
pub struct TreeParser {
tokens: Vec<Token>,
}

@ -0,0 +1,29 @@
pub const F_SIN: &str = "sin";
pub const F_COS: &str = "cos";
pub const F_TAN: &str = "tan";
pub const F_SEC: &str = "sec";
pub const F_CSC: &str = "csc";
pub const F_COT: &str = "cot";
pub const F_ARCSIN: &str = "arcsin";
pub const F_ARCCOS: &str = "arccos";
pub const F_ARCTAN: &str = "arctan";
pub const F_SINH: &str = "sinh";
pub const F_COSH: &str = "cosh";
pub const F_TANH: &str = "tanh";
pub const F_SECH: &str = "sech";
pub const F_CSCH: &str = "csch";
pub const F_COTH: &str = "coth";
pub const F_EXP: &str = "exp";
pub const F_LOG: &str = "log";
pub const F_LN: &str = "ln";
pub const F_DET: &str = "det";
pub const F_DIM: &str = "dim";
pub const F_MOD: &str = "mod";
pub const F_GCD: &str = "gcd";
pub const F_LCM: &str = "lcm";
pub const F_LUB: &str = "lub";
pub const F_GLB: &str = "glb";
pub const F_MIN: &str = "min";
pub const F_MAX: &str = "max";
pub const F_F: &str = "f";
pub const F_G: &str = "g";

@ -1,6 +1,7 @@
pub mod accents;
pub mod arrows;
pub mod font_commands;
pub mod functions;
pub mod greek;
pub mod grouping;
pub mod logical;

@ -1,6 +1,7 @@
use crate::tokens::constants::accents::*;
use crate::tokens::constants::arrows::*;
use crate::tokens::constants::font_commands::*;
use crate::tokens::constants::functions::*;
use crate::tokens::constants::greek::*;
use crate::tokens::constants::grouping::*;
use crate::tokens::constants::logical::*;
@ -10,7 +11,7 @@ use crate::tokens::constants::relations::*;
use crate::tokens::constants::TokenPattern;
use crate::tokens::{
Accent, Arrow, FontCommand, Greek, Grouping, Logical, Misc, Operation, Relation,
Accent, Arrow, FontCommand, Function, Greek, Grouping, Logical, Misc, Operation, Relation,
};
use std::collections::HashMap;
@ -268,3 +269,43 @@ pub fn get_font_mappings() -> Vec<HashMap<&'static str, FontCommand>> {
},
]
}
pub fn get_function_mappings() -> Vec<HashMap<&'static str, Function>> {
vec![
hashmap! {
F_SINH => Function::Sinh,
F_COSH => Function::Cosh,
F_TANH => Function::Tanh,
F_SECH => Function::Sech,
F_CSCH => Function::Csch,
F_COTH => Function::Coth,
},
hashmap! {
F_SIN => Function::Sin,
F_COS => Function::Cos,
F_TAN => Function::Tan,
F_SEC => Function::Sec,
F_CSC => Function::Csc,
F_COT => Function::Cot,
F_ARCSIN => Function::ArcSin,
F_ARCCOS => Function::ArcCos,
F_ARCTAN => Function::ArcTan,
F_EXP => Function::Exp,
F_LOG => Function::Log,
F_LN => Function::Ln,
F_DET => Function::Det,
F_DIM => Function::Dim,
F_MOD => Function::Mod,
F_GCD => Function::Gcd,
F_LCM => Function::Lcm,
F_LUB => Function::Lub,
F_GLB => Function::Glb,
F_MIN => Function::Min,
F_MAX => Function::Max,
},
hashmap! {
F_F => Function::F,
F_G => Function::G,
},
]
}

@ -12,6 +12,7 @@ pub enum Token {
Accent(Accent),
Greek(Greek),
Font(FontCommand),
Function(Function),
Text(Text),
}
@ -234,3 +235,36 @@ pub enum FontCommand {
Fr,
SansSerif,
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum Function {
Sin,
Cos,
Tan,
Sec,
Csc,
Cot,
ArcSin,
ArcCos,
ArcTan,
Sinh,
Cosh,
Tanh,
Sech,
Csch,
Coth,
Exp,
Log,
Ln,
Det,
Dim,
Mod,
Gcd,
Lcm,
Lub,
Glb,
Min,
Max,
F,
G,
}

Loading…
Cancel
Save