refactor number parsing to reduce nesting
@@ -84,14 +84,15 @@ pub(crate) fn eat_number<I: Iterator<Item = Token>>(
let mut times_ten = String::new();
let mut times_ten_is_postive = true;
loop {
- match toks.peek() {
// TODO: https://github.com/rust-lang/rust/issues/54883
- Some(Token { kind: 'e', .. }) | Some(Token { kind: 'E', .. }) => {
- if toks.peek_forward(1).is_none() {
- break;
+ if let Some(Token { kind: 'e', .. }) | Some(Token { kind: 'E', .. }) = toks.peek() {
+ let t = if let Some(tok) = toks.peek_forward(1) {
+ *tok
} else {
- let Token { kind, pos } = *toks.peek().unwrap();
- match kind {
+ break;
+ };
+
+ match t.kind {
'-' => {
toks.next();
times_ten_is_postive = false;
@@ -105,14 +106,8 @@ pub(crate) fn eat_number<I: Iterator<Item = Token>>(
eat_whole_number(toks, &mut times_ten);
if times_ten.is_empty() && !times_ten_is_postive {
- if let Some(t) = toks.peek() {
- return Err(("Expected digit.", t.pos()).into());
- }
- return Err(("Expected digit.", pos).into());
- }
- }
+ return Err(("Expected digit.", toks.peek().unwrap_or(&t).pos).into());
}
- Some(..) | None => break,
}
break;
}
