From c0210d7c3fdec462a53cb367388741aaaedba6ef Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Sun, 3 Jan 2021 00:07:30 +0100 Subject: [PATCH] Removed regex dependency from kalk crate and made 'test-case' a dev-dependency --- kalk/Cargo.toml | 4 ++-- kalk/src/lexer.rs | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/kalk/Cargo.toml b/kalk/Cargo.toml index 103d970..0381599 100644 --- a/kalk/Cargo.toml +++ b/kalk/Cargo.toml @@ -15,13 +15,13 @@ crate-type = ["cdylib", "rlib"] [dependencies] rug = { version = "1.11.0", features = ["float"], optional = true } -test-case = "1.0.0" -regex = "1" lazy_static = "1.4.0" wasm-bindgen = "0.2.69" [dev-dependencies] wasm-bindgen-test = "0.3.19" +test-case = "1.0.0" +regex = "1" [features] default = ["rug"] diff --git a/kalk/src/lexer.rs b/kalk/src/lexer.rs index 2e6c6ce..e346e71 100644 --- a/kalk/src/lexer.rs +++ b/kalk/src/lexer.rs @@ -149,7 +149,6 @@ impl<'a> Lexer<'a> { fn next_identifier(&mut self) -> Token { let start = self.index; let mut end = start; - let letter_reg = regex::Regex::new(r"[A-z'_]").unwrap(); let mut value = String::new(); while is_valid_identifier(self.peek()) { @@ -167,7 +166,7 @@ impl<'a> Lexer<'a> { // Only allow identifiers with a special character to have *one* character. No more. // Break the loop if it isn't the first run and the current character is a special character. - if end - start > 0 && !letter_reg.is_match(&c.to_string()) { + if end - start > 0 && !(c.is_ascii_alphabetic() || c == '\'' || c == '_') { break; } @@ -209,9 +208,11 @@ fn build(kind: TokenKind, value: &str, span: (usize, usize)) -> Token { fn is_valid_identifier(c: Option<&char>) -> bool { if let Some(c) = c { - regex::Regex::new(r"[^\s\n\r0-9\+-/%\*\^!\(\)=\.,;|⌊⌋⌈⌉]") - .unwrap() - .is_match(&c.to_string()) + match c { + '+' | '-' | '/' | '*' | '%' | '^' | '!' | '(' | ')' | '=' | '.' | ',' | ';' | '|' + | '⌊' | '⌋' | '⌈' | '⌉' | ']' => false, + _ => !c.is_digit(10), + } } else { false }