fix(parser::tokenizer): fix useless pop bug

This commit is contained in:
gbrochar 2023-08-03 15:24:45 +02:00
parent 22d9225235
commit 7a94fe932f
1 changed files with 7 additions and 14 deletions

View File

@ -36,14 +36,7 @@ pub fn tokenize(query: &str) -> Result<Vec<Token>, String> {
')' => tokens.push(Token::CloseParenthesis()), ')' => tokens.push(Token::CloseParenthesis()),
'a'..='z' | 'A'..='Z' => { 'a'..='z' | 'A'..='Z' => {
if is_last_number == true { if is_last_number == true {
let mut tmp_token = None;
if is_pop_needed {
tmp_token = tokens.pop();
}
tokens.push(check_number(my_string, i)?); tokens.push(check_number(my_string, i)?);
if let Some(tok) = tmp_token {
tokens.push(tok);
}
is_last_number = false; is_last_number = false;
my_string = String::new(); my_string = String::new();
} }
@ -55,17 +48,10 @@ pub fn tokenize(query: &str) -> Result<Vec<Token>, String> {
}, },
'0'..='9' | '.' => { '0'..='9' | '.' => {
if is_last_variable == true { if is_last_variable == true {
let mut tmp_token = None;
if is_pop_needed {
tmp_token = tokens.pop();
}
match my_string.as_str() { match my_string.as_str() {
"i" => tokens.push(Token::ImaginaryUnit()), "i" => tokens.push(Token::ImaginaryUnit()),
_ => tokens.push(Token::Variable(my_string)), _ => tokens.push(Token::Variable(my_string)),
} }
if let Some(tok) = tmp_token {
tokens.push(tok);
}
is_last_variable = false; is_last_variable = false;
my_string = String::new(); my_string = String::new();
} }
@ -297,4 +283,11 @@ mod tests {
let query = "324*43224+243_+234=234"; let query = "324*43224+243_+234=234";
tokenize(query).unwrap(); tokenize(query).unwrap();
} }
#[test]
fn bug_pop_not_needed_between_variables_and_numbers() {
let query = "5(7i)";
let result: Vec<Token> = vec![Token::Number(String::from("5")), Token::OpenParenthesis(), Token::Number(String::from("7")), Token::ImaginaryUnit(), Token::CloseParenthesis()];
assert_eq!(tokenize(query).unwrap(), result);
}
} }