added multi-char operators and separators
This commit is contained in:
parent
b4886eff87
commit
27000668c2
1 changed files with 14 additions and 0 deletions
14
src/lexer.rs
14
src/lexer.rs
|
@ -31,10 +31,20 @@ fn is_operator(char: &str) -> bool {
|
|||
let chars = vec!["+","-","*","/","^","%","|","&","!"];
|
||||
return chars.contains(&char);
|
||||
}
|
||||
fn is_mul_operator(char: &str, next_char: &str) -> bool {
|
||||
let chars = vec!["==","!=","<=",">="];
|
||||
let check = String::from(char) + next_char;
|
||||
return chars.contains(&check.as_str());
|
||||
}
|
||||
fn is_sep(char: &str) -> bool {
|
||||
let chars = vec!["(",")","[","]","{","}",",",".","="];
|
||||
return chars.contains(&char);
|
||||
}
|
||||
fn is_mul_sep(char: &str, next_char: &str) -> bool {
|
||||
let chars = vec!["=>"];
|
||||
let check = String::from(char) + next_char;
|
||||
return chars.contains(&check.as_str());
|
||||
}
|
||||
|
||||
fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
|
||||
let mut str = String::from("");
|
||||
|
@ -227,8 +237,12 @@ pub fn lex(input: String) -> Vec<Token> {
|
|||
read_comment(&splitted, &mut pos, false);
|
||||
} else if splitted.len() >= pos+1 && splitted[(pos-1)..(pos+1)].join("") == "/*" {
|
||||
read_comment(&splitted, &mut pos, true);
|
||||
} else if is_mul_operator(char, splitted[pos]) {
|
||||
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
|
||||
} else if is_operator(char) {
|
||||
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char), pos: pos-1 });
|
||||
} else if is_mul_sep(char, splitted[pos]) {
|
||||
out.push(Token { typ: TokenType::SEPARATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
|
||||
} else if is_sep(char) {
|
||||
out.push(Token { typ: TokenType::SEPARATOR, value: String::from(char), pos: pos-1 });
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue