Skip to content

Commit

Permalink
refactor(parser): made intermediate_token use consts instead of runti…
Browse files Browse the repository at this point in the history
…me iters
  • Loading branch information
AurumTheEnd authored and daemontus committed May 20, 2024
1 parent aa8b4b5 commit ae85619
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
51 changes: 33 additions & 18 deletions src/parser/structs/intermediate_token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use itertools::Itertools;
use regex::RegexSet;

use crate::parser::utils::LITERAL_IDENTIFIER;
Expand Down Expand Up @@ -103,25 +102,41 @@ impl<'a> IntermediateToken<'a> {
.concat()
}

pub fn longest_token_len() -> usize {
Self::all_token_patterns()
.iter()
.max_by(|a, b| a.chars().count().cmp(&b.chars().count()))
.expect("No patterns defined in the library")
.chars()
.count()
}
pub const ALL_TOKEN_PATTERNS_FROM_LONGEST: [&'static str; 26] = [
Self::FALSE_PATTERN_WORD,
Self::TRUE_PATTERN_WORD,
Self::AND_PATTERN_WORD,
Self::NOT_PATTERN_WORD,
Self::AND_PATTERN_LOGIC,
Self::OR_PATTERN_LOGIC,
Self::OR_PATTERN_WORD,
Self::AND_PATTERN_BIT,
Self::AND_PATTERN_MATH,
Self::AND_PATTERN_MATH_2,
Self::AND_PATTERN_BOOL,
Self::OR_PATTERN_BIT,
Self::OR_PATTERN_MATH,
Self::OR_PATTERN_MATH_2,
Self::OR_PATTERN_BOOL,
Self::NOT_PATTERN_TILDE,
Self::NOT_PATTERN_MARK,
Self::NOT_PATTERN_MATH,
Self::FALSE_PATTERN_CHAR,
Self::FALSE_PATTERN_NUM,
Self::TRUE_PATTERN_CHAR,
Self::TRUE_PATTERN_NUM,
Self::LITERAL_START_PATTERN,
Self::LITERAL_END_PATTERN,
Self::PARENTHESIS_START_PATTERN,
Self::PARENTHESIS_END_PATTERN,
];

fn all_token_patterns_ordered_from_longest() -> Vec<&'a str> {
Self::all_token_patterns()
.into_iter()
.sorted_by(|a, b| b.chars().count().cmp(&a.chars().count()))
.collect()
}
// FALSE_PATTERN_WORD == "false"
pub const LONGEST_TOKEN_LEN: usize = 5;

// TODO make a trait method
pub fn try_from(value: &'a str) -> Option<IntermediateToken> {
let input = Self::all_token_patterns_ordered_from_longest();
let input = Self::ALL_TOKEN_PATTERNS_FROM_LONGEST;

// escape the pattern so that e.g. "^" is not treated as regex, but as a literal character for the And operation
let set = RegexSet::new(input.iter().map(|pattern| {
Expand Down Expand Up @@ -196,15 +211,15 @@ mod tests {

#[test]
fn test_longest() {
let actual = IntermediateToken::longest_token_len();
let actual = IntermediateToken::LONGEST_TOKEN_LEN;
let expected = IntermediateToken::FALSE_PATTERN_WORD.len();

assert_eq!(actual, expected);
}

#[test]
fn test_ordered_patterns() {
let tokens = IntermediateToken::all_token_patterns_ordered_from_longest();
let tokens = IntermediateToken::ALL_TOKEN_PATTERNS_FROM_LONGEST;

assert!(tokens
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn tokenize_level(
) -> Result<Vec<FinalToken>, TokenizeError> {
let mut result = vec![];
let mut buffer = String::new();
let take_size = IntermediateToken::longest_token_len() + 1;
let take_size = IntermediateToken::LONGEST_TOKEN_LEN + 1;

// trim whitespace in case of whitespace after opening parenthesis
trim_whitespace_left(input);
Expand Down

0 comments on commit ae85619

Please sign in to comment.