From 6c0c6997d12299dc3a177d68d656f6ebfdb32f72 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 13 Jun 2023 19:12:01 +0200 Subject: [PATCH] Fix parsing comma separated lists List-like AST structures now require a comma (as intended) between values, except if there's only one value. We also still allow trailing commas. This ensures that code such as this now results in a parser error: A { @a = 10 @b = 20 } Instead, this should be written like so: A { @a = 10, @b = 20 } This fixes https://github.com/inko-lang/inko/issues/562. Changelog: fixed --- ast/src/parser.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ast/src/parser.rs b/ast/src/parser.rs index 4d202c70..2fa87daa 100644 --- a/ast/src/parser.rs +++ b/ast/src/parser.rs @@ -2865,7 +2865,9 @@ impl Parser { values.push(func(self, token)?); - if self.peek().kind == TokenKind::Comma { + if values.len() >= 1 && self.peek().kind != close { + self.expect(TokenKind::Comma)?; + } else if self.peek().kind == TokenKind::Comma { self.next(); } } @@ -4189,6 +4191,17 @@ mod tests { location: cols(1, 1) })) ); + + assert_eq!( + expr("A\n{ @a = 10, }"), + Expression::Constant(Box::new(Constant { + source: None, + name: "A".to_string(), + location: cols(1, 1) + })) + ); + + assert_error_expr!("A { @a = 10 @b = 20 }", cols(13, 14)); } #[test]