refactor number parsing to reduce nesting

This commit is contained in:
ConnorSkees 2020-05-23 01:49:21 -04:00
parent cc15d46f9b
commit bc12c0b4e7

View File

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