From ad0618e8d1f81d98cd6228ab365186dad560d3c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 7 Mar 2021 23:16:26 +0100 Subject: allow trailing commas in tuples --- src/compiler.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/compiler.rs b/src/compiler.rs index dc3bd96..17c6330 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -683,10 +683,10 @@ impl Compiler { expect!(self, Token::LeftParen, "Expected '(' at start of tuple"); let mut num_args = 0; - loop { + let trailing_comma = loop { match self.peek() { Token::RightParen | Token::EOF => { - break; + break false; } Token::Newline => { self.eat(); @@ -695,7 +695,12 @@ impl Compiler { self.expression(block); num_args += 1; match self.peek() { - Token::Comma => { self.eat(); }, + Token::Comma => { + self.eat(); + if matches!(self.peek(), Token::RightParen) { + break true; + } + }, Token::RightParen => {}, _ => { error!(self, "Expected ',' or ')' in tuple"); @@ -704,9 +709,9 @@ impl Compiler { } } } - } - if num_args == 1 { - error!(self, "A tuple must contain more than 1 element."); + }; + if num_args == 1 && !trailing_comma { + error!(self, "A tuple must contain more than 1 element or end with a trailing comma."); return; } -- cgit v1.2.1