Remove unwrap() in favor of ok_or()

This commit is contained in:
2025-12-01 15:06:53 -07:00
parent 8ea274f3bf
commit 0977d3d0d5
2 changed files with 1577 additions and 21 deletions

View File

@@ -563,7 +563,10 @@ impl<'a> Parser<'a> {
}),
})
}
TokenType::Symbol(Symbol::LParen) => Ok(*self.spanned(|p| p.priority())?.node.unwrap()),
TokenType::Symbol(Symbol::LParen) => Ok(*self
.spanned(|p| p.priority())?
.node
.ok_or(Error::UnexpectedEOF)?),
TokenType::Identifier(_)
if self_matches_peek!(self, TokenType::Symbol(Symbol::LParen)) =>
{
@@ -663,14 +666,14 @@ impl<'a> Parser<'a> {
}
fn assignment(&mut self) -> Result<AssignmentExpression, Error> {
let identifier_token = self.current_token.as_ref().unwrap();
let identifier_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
let identifier_span = Self::token_to_span(identifier_token);
let identifier = match identifier_token.token_type {
TokenType::Identifier(ref id) => id.clone(),
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
));
}
};
@@ -966,7 +969,7 @@ impl<'a> Parser<'a> {
self.tokenizer.seek(SeekFrom::Current(-1))?;
}
Ok(expressions.pop().unwrap())
expressions.pop().ok_or(Error::UnexpectedEOF)
}
fn priority(&mut self) -> Result<Option<Box<Spanned<Expression>>>, Error> {
@@ -993,14 +996,14 @@ impl<'a> Parser<'a> {
}
fn invocation(&mut self) -> Result<InvocationExpression, Error> {
let identifier_token = self.current_token.as_ref().unwrap();
let identifier_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
let identifier_span = Self::token_to_span(identifier_token);
let identifier = match identifier_token.token_type {
TokenType::Identifier(ref id) => id.clone(),
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
));
}
};
@@ -1033,7 +1036,7 @@ impl<'a> Parser<'a> {
if !self_matches_peek!(self, TokenType::Symbol(Symbol::Comma))
&& !self_matches_peek!(self, TokenType::Symbol(Symbol::RParen))
{
let next_token = self.get_next()?.unwrap();
let next_token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
return Err(Error::UnexpectedToken(
Self::token_to_span(next_token),
next_token.clone(),
@@ -1230,7 +1233,7 @@ impl<'a> Parser<'a> {
node: Expression::Block(block),
}))
} else {
let next = self.get_next()?.unwrap();
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
return Err(Error::UnexpectedToken(
Self::token_to_span(next),
next.clone(),
@@ -1325,7 +1328,7 @@ impl<'a> Parser<'a> {
self.get_next()?.ok_or(Error::UnexpectedEOF)?,
TokenType::Symbol(Symbol::RParen)
) {
let current_token = self.current_token.as_ref().unwrap();
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
let arg_span = Self::token_to_span(current_token);
let argument = match current_token.token_type {
TokenType::Identifier(ref id) => id.clone(),
@@ -1354,7 +1357,7 @@ impl<'a> Parser<'a> {
if !self_matches_peek!(self, TokenType::Symbol(Symbol::Comma))
&& !self_matches_peek!(self, TokenType::Symbol(Symbol::RParen))
{
let next = self.get_next()?.unwrap();
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
return Err(Error::UnexpectedToken(
Self::token_to_span(next),
next.clone(),
@@ -1410,14 +1413,14 @@ impl<'a> Parser<'a> {
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
))
}
},
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
))
}
}
@@ -1448,7 +1451,7 @@ impl<'a> Parser<'a> {
"sleep" => {
check_length(self, &invocation.arguments, 1)?;
let mut arg = invocation.arguments.into_iter();
let expr = arg.next().unwrap();
let expr = arg.next().ok_or(Error::UnexpectedEOF)?;
Ok(SysCall::System(System::Sleep(boxed!(expr))))
}
"hash" => {
@@ -1459,7 +1462,7 @@ impl<'a> Parser<'a> {
let LiteralOrVariable::Literal(lit_str) = lit_str else {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
));
};
@@ -1479,21 +1482,21 @@ impl<'a> Parser<'a> {
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
));
}
},
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
));
}
},
_ => {
return Err(Error::UnexpectedToken(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
));
}
};
@@ -1509,7 +1512,7 @@ impl<'a> Parser<'a> {
let mut args = invocation.arguments.into_iter();
let device = literal_or_variable!(args.next());
let logic_type = get_arg!(Literal, literal_or_variable!(args.next()));
let variable = args.next().unwrap();
let variable = args.next().ok_or(Error::UnexpectedEOF)?;
Ok(SysCall::System(sys_call::System::SetOnDevice(
device,
Literal::String(logic_type.to_string().replace("\"", "")),
@@ -1521,7 +1524,7 @@ impl<'a> Parser<'a> {
let mut args = invocation.arguments.into_iter();
let device_hash = literal_or_variable!(args.next());
let logic_type = get_arg!(Literal, literal_or_variable!(args.next()));
let variable = args.next().unwrap();
let variable = args.next().ok_or(Error::UnexpectedEOF)?;
Ok(SysCall::System(sys_call::System::SetOnDeviceBatched(
device_hash,
Literal::String(logic_type.to_string().replace("\"", "")),
@@ -1536,12 +1539,12 @@ impl<'a> Parser<'a> {
// Since Math isn't fully expanded in this snippet, we return Unsupported.
Err(Error::UnsupportedKeyword(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
))
} else {
Err(Error::UnsupportedKeyword(
self.current_span(),
self.current_token.clone().unwrap(),
self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
))
}
}