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

1553
2 Normal file

File diff suppressed because it is too large Load Diff

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(_) TokenType::Identifier(_)
if self_matches_peek!(self, TokenType::Symbol(Symbol::LParen)) => if self_matches_peek!(self, TokenType::Symbol(Symbol::LParen)) =>
{ {
@@ -663,14 +666,14 @@ impl<'a> Parser<'a> {
} }
fn assignment(&mut self) -> Result<AssignmentExpression, Error> { 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_span = Self::token_to_span(identifier_token);
let identifier = match identifier_token.token_type { let identifier = match identifier_token.token_type {
TokenType::Identifier(ref id) => id.clone(), TokenType::Identifier(ref id) => id.clone(),
_ => { _ => {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), 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))?; 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> { 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> { 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_span = Self::token_to_span(identifier_token);
let identifier = match identifier_token.token_type { let identifier = match identifier_token.token_type {
TokenType::Identifier(ref id) => id.clone(), TokenType::Identifier(ref id) => id.clone(),
_ => { _ => {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), 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)) if !self_matches_peek!(self, TokenType::Symbol(Symbol::Comma))
&& !self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) && !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( return Err(Error::UnexpectedToken(
Self::token_to_span(next_token), Self::token_to_span(next_token),
next_token.clone(), next_token.clone(),
@@ -1230,7 +1233,7 @@ impl<'a> Parser<'a> {
node: Expression::Block(block), node: Expression::Block(block),
})) }))
} else { } else {
let next = self.get_next()?.unwrap(); let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
Self::token_to_span(next), Self::token_to_span(next),
next.clone(), next.clone(),
@@ -1325,7 +1328,7 @@ impl<'a> Parser<'a> {
self.get_next()?.ok_or(Error::UnexpectedEOF)?, self.get_next()?.ok_or(Error::UnexpectedEOF)?,
TokenType::Symbol(Symbol::RParen) 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 arg_span = Self::token_to_span(current_token);
let argument = match current_token.token_type { let argument = match current_token.token_type {
TokenType::Identifier(ref id) => id.clone(), TokenType::Identifier(ref id) => id.clone(),
@@ -1354,7 +1357,7 @@ impl<'a> Parser<'a> {
if !self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) if !self_matches_peek!(self, TokenType::Symbol(Symbol::Comma))
&& !self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) && !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( return Err(Error::UnexpectedToken(
Self::token_to_span(next), Self::token_to_span(next),
next.clone(), next.clone(),
@@ -1410,14 +1413,14 @@ impl<'a> Parser<'a> {
_ => { _ => {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), self.current_span(),
self.current_token.clone().unwrap(), self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
)) ))
} }
}, },
_ => { _ => {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), 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" => { "sleep" => {
check_length(self, &invocation.arguments, 1)?; check_length(self, &invocation.arguments, 1)?;
let mut arg = invocation.arguments.into_iter(); 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)))) Ok(SysCall::System(System::Sleep(boxed!(expr))))
} }
"hash" => { "hash" => {
@@ -1459,7 +1462,7 @@ impl<'a> Parser<'a> {
let LiteralOrVariable::Literal(lit_str) = lit_str else { let LiteralOrVariable::Literal(lit_str) = lit_str else {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), 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( return Err(Error::UnexpectedToken(
self.current_span(), self.current_span(),
self.current_token.clone().unwrap(), self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
)); ));
} }
}, },
_ => { _ => {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), self.current_span(),
self.current_token.clone().unwrap(), self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
)); ));
} }
}, },
_ => { _ => {
return Err(Error::UnexpectedToken( return Err(Error::UnexpectedToken(
self.current_span(), 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 mut args = invocation.arguments.into_iter();
let device = literal_or_variable!(args.next()); let device = literal_or_variable!(args.next());
let logic_type = get_arg!(Literal, 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( Ok(SysCall::System(sys_call::System::SetOnDevice(
device, device,
Literal::String(logic_type.to_string().replace("\"", "")), Literal::String(logic_type.to_string().replace("\"", "")),
@@ -1521,7 +1524,7 @@ impl<'a> Parser<'a> {
let mut args = invocation.arguments.into_iter(); let mut args = invocation.arguments.into_iter();
let device_hash = literal_or_variable!(args.next()); let device_hash = literal_or_variable!(args.next());
let logic_type = get_arg!(Literal, 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( Ok(SysCall::System(sys_call::System::SetOnDeviceBatched(
device_hash, device_hash,
Literal::String(logic_type.to_string().replace("\"", "")), 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. // Since Math isn't fully expanded in this snippet, we return Unsupported.
Err(Error::UnsupportedKeyword( Err(Error::UnsupportedKeyword(
self.current_span(), self.current_span(),
self.current_token.clone().unwrap(), self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
)) ))
} else { } else {
Err(Error::UnsupportedKeyword( Err(Error::UnsupportedKeyword(
self.current_span(), self.current_span(),
self.current_token.clone().unwrap(), self.current_token.clone().ok_or(Error::UnexpectedEOF)?,
)) ))
} }
} }