From 8b19f546c8cf8226ac0536ed0df585d8c672d7f8 Mon Sep 17 00:00:00 2001 From: Emmanuel Benoit Date: Wed, 11 Jan 2023 15:33:41 +0100 Subject: [PATCH] Parser - Removed handling of 'return' special cases * This has been implemented in the resolver so it's pretty pointless to do it twice --- src/parser.rs | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 923bb78..378656f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -492,24 +492,16 @@ impl Parser { /// return_statement := "return" expression? ";" /// ``` fn parse_return_statement(&mut self, ret_token: &Token) -> SloxResult { - if self.can_use_return() { - let value = if self.check(&TokenType::Semicolon) { - None - } else { - Some(self.parse_expression()?) - }; - self.consume(&TokenType::Semicolon, "';' expected after return statement")?; - Ok(StmtNode::Return { - token: ret_token.clone(), - value, - }) + let value = if self.check(&TokenType::Semicolon) { + None } else { - Err(SloxError::with_token( - ErrorKind::Parse, - ret_token, - "'return' found outside of function".to_owned(), - )) - } + Some(self.parse_expression()?) + }; + self.consume(&TokenType::Semicolon, "';' expected after return statement")?; + Ok(StmtNode::Return { + token: ret_token.clone(), + value, + }) } /// Parse the following rule: @@ -861,19 +853,6 @@ impl Parser { false } - /// Check whether the `return` keyword can be used. This is true whenever - /// the first `LoopParsingState::None` found in the loop parsing state is - /// not the one at position 0. - fn can_use_return(&self) -> bool { - let mut pos = self.loop_state.len() - 1; - loop { - if self.loop_state[pos] == LoopParsingState::None { - return pos != 0; - } - pos -= 1; - } - } - /// Generate an error at the current token. fn error(&self, message: &str) -> SloxResult { self.error_mv(message.to_owned())