Add grouping mappings and parsing

pull/1/head
trivernis 4 years ago
parent 3daf942a3c
commit 46c2e99099

@ -1,7 +1,9 @@
use crate::tokens::constants::TokenPattern;
use crate::tokens::mappings::{
get_logical_mappings, get_misc_mappings, get_operation_mappings, get_relation_mapping,
get_grouping_mappings, get_logical_mappings, get_misc_mappings, get_operation_mappings,
get_relation_mapping,
};
use crate::tokens::{Logical, Misc, Operation, Relation, Token};
use crate::tokens::{Grouping, Logical, Misc, Operation, Relation, Token};
use charred::tapemachine::CharTapeMachine;
use std::collections::HashMap;
@ -20,8 +22,7 @@ impl Tokenizer {
fn parse_misc(&mut self) -> Option<Misc> {
lazy_static! {
static ref MISC_MAPPINGS: Vec<HashMap<&'static [&'static str], Misc>> =
get_misc_mappings();
static ref MISC_MAPPINGS: Vec<HashMap<TokenPattern, Misc>> = get_misc_mappings();
}
for mapping in MISC_MAPPINGS.iter() {
for key in mapping.keys() {
@ -35,7 +36,7 @@ impl Tokenizer {
fn parse_operation(&mut self) -> Option<Operation> {
lazy_static! {
static ref OPERATION_MAPPINGS: Vec<HashMap<&'static [&'static str], Operation>> =
static ref OPERATION_MAPPINGS: Vec<HashMap<TokenPattern, Operation>> =
get_operation_mappings();
}
for mapping in OPERATION_MAPPINGS.iter() {
@ -50,7 +51,7 @@ impl Tokenizer {
fn parse_relation(&mut self) -> Option<Relation> {
lazy_static! {
static ref RELATION_MAPPINGS: Vec<HashMap<&'static [&'static str], Relation>> =
static ref RELATION_MAPPINGS: Vec<HashMap<TokenPattern, Relation>> =
get_relation_mapping();
}
for mapping in RELATION_MAPPINGS.iter() {
@ -65,7 +66,7 @@ impl Tokenizer {
fn parse_logical(&mut self) -> Option<Logical> {
lazy_static! {
static ref LOGICAL_MAPPINGS: Vec<HashMap<&'static [&'static str], Logical>> =
static ref LOGICAL_MAPPINGS: Vec<HashMap<TokenPattern, Logical>> =
get_logical_mappings();
}
for mapping in LOGICAL_MAPPINGS.iter() {
@ -77,4 +78,19 @@ impl Tokenizer {
}
None
}
fn parse_grouping(&mut self) -> Option<Grouping> {
lazy_static! {
static ref GROUPING_MAPPINGS: Vec<HashMap<TokenPattern, Grouping>> =
get_grouping_mappings();
}
for mapping in GROUPING_MAPPINGS.iter() {
for key in mapping.keys() {
if self.ctm.check_any_str_sequence(*key) {
return Some(mapping[key].clone());
}
}
}
None
}
}

@ -1,17 +1,17 @@
pub const A_RPAREN: char = '(';
pub const A_LPAREN: char = ')';
pub const G_RPAREN: &'static [&str] = &["("];
pub const G_LPAREN: &'static [&str] = &[")"];
pub const A_RBRAC: char = '[';
pub const A_LBRAC: char = ']';
pub const G_RBRAC: &'static [&str] = &["["];
pub const G_LBRAC: &'static [&str] = &["]"];
pub const A_RCURL: char = '{';
pub const A_LCURL: char = '}';
pub const G_RCURL: &'static [&str] = &["{"];
pub const G_LCURL: &'static [&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: &'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"];

@ -1,9 +1,11 @@
pub mod operations;
pub mod misc;
pub mod relations;
pub mod logical;
pub mod grouping;
pub mod arrows;
pub mod accents;
pub mod arrows;
pub mod font_commands;
pub mod greek;
pub mod font_commands;
pub mod grouping;
pub mod logical;
pub mod misc;
pub mod operations;
pub mod relations;
pub type TokenPattern = &'static [&'static str];

@ -1,12 +1,14 @@
use crate::tokens::constants::grouping::*;
use crate::tokens::constants::logical::*;
use crate::tokens::constants::misc::*;
use crate::tokens::constants::operations::*;
use crate::tokens::constants::relations::*;
use crate::tokens::{Logical, Misc, Operation, Relation};
use crate::tokens::constants::TokenPattern;
use crate::tokens::{Grouping, Logical, Misc, Operation, Relation};
use std::cell::RefCell;
use std::collections::HashMap;
pub fn get_operation_mappings() -> Vec<HashMap<&'static [&'static str], Operation>> {
pub fn get_operation_mappings() -> Vec<HashMap<TokenPattern, Operation>> {
vec![
hashmap! {
G_STAR => Operation::Star,
@ -43,7 +45,7 @@ pub fn get_operation_mappings() -> Vec<HashMap<&'static [&'static str], Operatio
]
}
pub fn get_misc_mappings() -> Vec<HashMap<&'static [&'static str], Misc>> {
pub fn get_misc_mappings() -> Vec<HashMap<TokenPattern, Misc>> {
vec![
hashmap! {
G_TRIANGLE => Misc::Triangle,
@ -91,7 +93,7 @@ pub fn get_misc_mappings() -> Vec<HashMap<&'static [&'static str], Misc>> {
]
}
pub fn get_relation_mapping() -> Vec<HashMap<&'static [&'static str], Relation>> {
pub fn get_relation_mapping() -> Vec<HashMap<TokenPattern, Relation>> {
vec![
hashmap! {
G_SUBSETEQ => Relation::SubSetEq,
@ -122,7 +124,7 @@ pub fn get_relation_mapping() -> Vec<HashMap<&'static [&'static str], Relation>>
]
}
pub fn get_logical_mappings() -> Vec<HashMap<&'static [&'static str], Logical>> {
pub fn get_logical_mappings() -> Vec<HashMap<TokenPattern, Logical>> {
vec![
hashmap! {
G_IFF => Logical::Iff,
@ -142,3 +144,26 @@ pub fn get_logical_mappings() -> Vec<HashMap<&'static [&'static str], Logical>>
},
]
}
pub fn get_grouping_mappings() -> Vec<HashMap<TokenPattern, Grouping>> {
vec![
hashmap! {
G_LANGLE => Grouping::LAngle,
G_RANGLE => Grouping::RAngle,
G_RXPAR => Grouping::RXPar,
G_LXPAR => Grouping::LXPar,
},
hashmap! {
G_RPAREN => Grouping::RParen,
G_LPAREN => Grouping::LParen,
G_RBRAC => Grouping::RBrace,
G_LBRAC => Grouping::LBrace,
G_RCURL => Grouping::RCurl,
G_LCURL => Grouping::LCurl,
G_ABS => Grouping::Abs,
G_FLOOR => Grouping::Floor,
G_CEIL => Grouping::Ceil,
G_NORM => Grouping::Norm,
},
]
}

Loading…
Cancel
Save