From 6e6d7f765b25d56c2535a903d65be80d2fe33b37 Mon Sep 17 00:00:00 2001 From: tezlm Date: Tue, 3 Oct 2023 16:41:47 -0700 Subject: [PATCH] some changes --- src/lexer.rs | 33 ++++++++++++++++++--------------- src/parser.rs | 14 ++++++++++++++ test/t7 | 3 +++ 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 test/t7 diff --git a/src/lexer.rs b/src/lexer.rs index 9949537..6d852f2 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -207,6 +207,18 @@ impl Lexer { }; } + macro_rules! next_is { + ($ch:expr) => { + match self.input.get(self.pos + 1) { + Some($ch) => { + self.pos += 1; + true + } + _ => false, + } + }; + } + let token = match ch { '(' => Some(Token::OpenParan), ')' => Some(Token::CloseParan), @@ -314,24 +326,15 @@ impl Lexer { _ => Symbol::Greater, }, '.' => match self.input.get(self.pos + 1) { - Some('.') => match self.input.get(self.pos + 1) { - Some('.') => { - self.pos += 2; - Symbol::TripleDot - } - _ => { - self.pos += 1; - Symbol::DoubleDot - } + Some('.') => match next_is!('.') { + true => Symbol::TripleDot, + false => Symbol::DoubleDot, }, _ => Symbol::Dot, }, - ':' => match self.input.get(self.pos + 1) { - Some(':') => { - self.pos += 1; - Symbol::DoubleColon - } - _ => Symbol::Colon, + ':' => match next_is!(':') { + true => Symbol::DoubleColon, + false => Symbol::Colon, }, ',' => Symbol::Comma, ';' => Symbol::Semicolon, diff --git a/src/parser.rs b/src/parser.rs index 5584607..52c3a4e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -119,6 +119,20 @@ impl Parser { "bool" => Type::Boolean, _ => return Err(Error::TypeError(format!("unknown type {ident}"))), }, + Token::OpenParan => { + let mut tys = vec![]; + while !self.peek_tok().is_some_and(|tok| tok == &Token::CloseParan) { + tys.push(self.parse_type()?); + + if self.peek_tok() == Some(&Token::Symbol(Symbol::Comma)) { + self.eat(Token::Symbol(Symbol::Comma))?; + } else { + break; + } + } + self.eat(Token::CloseParan)?; + Type::Tuple(tys) + }, _ => todo!(), }; Ok(ty) diff --git a/test/t7 b/test/t7 new file mode 100644 index 0000000..6573ded --- /dev/null +++ b/test/t7 @@ -0,0 +1,3 @@ +fn main() -> (i32, i32) { + (123, 456) +}