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/compiler.rs') 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 From 294d304768dad5fca5f9844500c7003cd9f9ed8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 7 Mar 2021 23:16:51 +0100 Subject: cleanup comment --- src/compiler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index 17c6330..46fe38c 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -51,7 +51,7 @@ macro_rules! parse_branch { let num_errors = $compiler.errors.len(); let mut stored_errors = Vec::new(); - // Closures for early return on success. + // Loop for early return on success. let success = loop { // We risk getting a lot of errors if we are in an invalid state // when we start the parse. -- cgit v1.2.1 From 8e2c0e345facee83d63991dda548b71e6a0c4cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 8 Mar 2021 00:15:46 +0100 Subject: error on '(,)'-tuples Currently creats a lot of syntax errors. See #100. --- src/compiler.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index 46fe38c..c4f687f 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -691,6 +691,14 @@ impl Compiler { Token::Newline => { self.eat(); } + Token::Comma => { + //TODO(gu): This creates a lot of syntax errors since the compiler panic is + // ignored and the statement is tried as a grouping instead, even though we + // _know_ that this can't be parsed as a grouping either. + // Tracked in #100. + error!(self, "Tuples must begin with an element or ')'."); + return; + } _ => { self.expression(block); num_args += 1; -- cgit v1.2.1 From bd2371faefb2d2cd52477dbd2628013ed8feb103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 8 Mar 2021 00:16:45 +0100 Subject: minor fix some errors --- src/compiler.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index c4f687f..b2900b6 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -680,7 +680,7 @@ impl Compiler { } fn tuple(&mut self, block: &mut Block) { - expect!(self, Token::LeftParen, "Expected '(' at start of tuple"); + expect!(self, Token::LeftParen, "Expected '(' at start of tuple."); let mut num_args = 0; let trailing_comma = loop { @@ -711,7 +711,7 @@ impl Compiler { }, Token::RightParen => {}, _ => { - error!(self, "Expected ',' or ')' in tuple"); + error!(self, "Expected ',' or ')' after tuple element."); return; }, } @@ -719,7 +719,7 @@ impl Compiler { } }; if num_args == 1 && !trailing_comma { - error!(self, "A tuple must contain more than 1 element or end with a trailing comma."); + error!(self, "A tuple with 1 element must end with a trailing comma."); return; } -- cgit v1.2.1 From 16ba42c52bbf57fa78217327bd9759fec9f09fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 8 Mar 2021 17:58:34 +0100 Subject: parse indexing precedence --- src/compiler.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index b2900b6..c04a36f 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -186,6 +186,7 @@ nextable_enum!(Prec { Comp, Term, Factor, + Index, }); #[derive(Clone, Debug)] @@ -603,6 +604,8 @@ impl Compiler { fn precedence(&self, token: Token) -> Prec { match token { + Token::LeftBracket => Prec::Index, + Token::Star | Token::Slash => Prec::Factor, Token::Minus | Token::Plus => Prec::Term, -- cgit v1.2.1