Backtick operators supported in tokenizing

This commit is contained in:
greg 2018-04-25 03:01:41 -07:00
parent a1016293ac
commit b936132ca6

View File

@ -245,14 +245,31 @@ fn handle_operator<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut Ch
}; };
let mut buf = String::new(); let mut buf = String::new();
buf.push(c);
loop { if c == '`' {
match input.peek().map(|&(_, _, c)| { c }) { loop {
Some(c) if is_operator(&c) => { match input.peek().map(|&(_, _, c)| { c }) {
input.next(); Some(c) if c.is_alphabetic() || c == '_' => {
buf.push(c); input.next();
}, buf.push(c);
_ => break },
Some('`') => {
input.next();
break;
},
_ => break
}
}
} else {
buf.push(c);
loop {
match input.peek().map(|&(_, _, c)| { c }) {
Some(c) if is_operator(&c) => {
input.next();
buf.push(c);
},
_ => break
}
} }
} }
TokenType::Operator(Rc::new(buf)) TokenType::Operator(Rc::new(buf))
@ -286,4 +303,10 @@ mod schala_tokenizer_tests {
let token_types: Vec<TokenType> = tokenize("1 + /* hella /* bro */ */ 2").into_iter().map(move |t| t.token_type).collect(); let token_types: Vec<TokenType> = tokenize("1 + /* hella /* bro */ */ 2").into_iter().map(move |t| t.token_type).collect();
assert_eq!(token_types, vec![digit!("1"), op!("+"), digit!("2")]); assert_eq!(token_types, vec![digit!("1"), op!("+"), digit!("2")]);
} }
#[test]
fn backtick_operators() {
let token_types: Vec<TokenType> = tokenize("1 `plus` 2").into_iter().map(move |t| t.token_type).collect();
assert_eq!(token_types, vec![digit!("1"), op!("plus"), digit!("2")]);
}
} }