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? ";"
/// ```
fn parse_return_statement(&mut self, ret_token: &Token) -> SloxResult<StmtNode> {
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<O>(&self, message: &str) -> SloxResult<O> {
self.error_mv(message.to_owned())