diff --git a/dust-lang/src/parser/mod.rs b/dust-lang/src/parser/mod.rs index 445dbc3..cf874dc 100644 --- a/dust-lang/src/parser/mod.rs +++ b/dust-lang/src/parser/mod.rs @@ -227,16 +227,7 @@ impl<'src> Parser<'src> { fn parse_grouped(&mut self, _allow_assignment: bool) -> Result<(), ParseError> { self.parse_expression()?; - - if self.previous_token == Token::RightParenthesis { - Ok(()) - } else { - Err(ParseError::ExpectedToken { - expected: TokenKind::RightParenthesis, - found: self.current_token.to_owned(), - position: self.current_position, - }) - } + self.expect(TokenKind::RightParenthesis) } fn parse_unary(&mut self, _allow_assignment: bool) -> Result<(), ParseError> { diff --git a/dust-lang/src/parser/tests.rs b/dust-lang/src/parser/tests.rs index 4a1ce40..157fea6 100644 --- a/dust-lang/src/parser/tests.rs +++ b/dust-lang/src/parser/tests.rs @@ -2,6 +2,30 @@ use crate::Local; use super::*; +#[test] +fn parentheses_precedence() { + assert_eq!( + parse("(1 + 2) * 3"), + Ok(Chunk::with_data( + vec![ + ( + *Instruction::add(0, 0, 1) + .set_first_argument_to_constant() + .set_second_argument_to_constant(), + Span(3, 4) + ), + ( + *Instruction::multiply(1, 0, 2).set_second_argument_to_constant(), + Span(8, 9) + ), + (Instruction::r#return(), Span(0, 11)), + ], + vec![Value::integer(1), Value::integer(2), Value::integer(3)], + vec![] + )) + ); +} + #[test] fn add_multiply_precedence() { assert_eq!( @@ -42,7 +66,7 @@ fn let_statement() { } #[test] -fn integer() { +fn constant() { assert_eq!( parse("42"), Ok(Chunk::with_data(