Parser - Removed handling of 'return' special cases

* This has been implemented in the resolver so it's pretty pointless
    to do it twice
This commit is contained in:
Emmanuel BENOîT 2023-01-11 15:33:41 +01:00
parent ee53c2fba4
commit 8b19f546c8

View file

@ -492,24 +492,16 @@ impl Parser {
/// return_statement := "return" expression? ";" /// return_statement := "return" expression? ";"
/// ``` /// ```
fn parse_return_statement(&mut self, ret_token: &Token) -> SloxResult<StmtNode> { fn parse_return_statement(&mut self, ret_token: &Token) -> SloxResult<StmtNode> {
if self.can_use_return() { let value = if self.check(&TokenType::Semicolon) {
let value = if self.check(&TokenType::Semicolon) { None
None
} else {
Some(self.parse_expression()?)
};
self.consume(&TokenType::Semicolon, "';' expected after return statement")?;
Ok(StmtNode::Return {
token: ret_token.clone(),
value,
})
} else { } else {
Err(SloxError::with_token( Some(self.parse_expression()?)
ErrorKind::Parse, };
ret_token, self.consume(&TokenType::Semicolon, "';' expected after return statement")?;
"'return' found outside of function".to_owned(), Ok(StmtNode::Return {
)) token: ret_token.clone(),
} value,
})
} }
/// Parse the following rule: /// Parse the following rule:
@ -861,19 +853,6 @@ impl Parser {
false 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. /// Generate an error at the current token.
fn error<O>(&self, message: &str) -> SloxResult<O> { fn error<O>(&self, message: &str) -> SloxResult<O> {
self.error_mv(message.to_owned()) self.error_mv(message.to_owned())