From 954727535514e9198a6bd52d328596a02fa1ea69 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 25 Apr 2018 03:01:41 -0700 Subject: [PATCH] Backtick operators supported in tokenizing --- schala-lang/src/tokenizing.rs | 39 ++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/schala-lang/src/tokenizing.rs b/schala-lang/src/tokenizing.rs index 57a8659..14e4b3c 100644 --- a/schala-lang/src/tokenizing.rs +++ b/schala-lang/src/tokenizing.rs @@ -245,14 +245,31 @@ fn handle_operator>(c: char, input: &mut Ch }; let mut buf = String::new(); - buf.push(c); - loop { - match input.peek().map(|&(_, _, c)| { c }) { - Some(c) if is_operator(&c) => { - input.next(); - buf.push(c); - }, - _ => break + + if c == '`' { + loop { + match input.peek().map(|&(_, _, c)| { c }) { + Some(c) if c.is_alphabetic() || c == '_' => { + input.next(); + buf.push(c); + }, + 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)) @@ -286,4 +303,10 @@ mod schala_tokenizer_tests { let token_types: Vec = tokenize("1 + /* hella /* bro */ */ 2").into_iter().map(move |t| t.token_type).collect(); assert_eq!(token_types, vec![digit!("1"), op!("+"), digit!("2")]); } + + #[test] + fn backtick_operators() { + let token_types: Vec = tokenize("1 `plus` 2").into_iter().map(move |t| t.token_type).collect(); + assert_eq!(token_types, vec![digit!("1"), op!("plus"), digit!("2")]); + } }