remove unwraps from escape parsing

these could not cause panics, but it is nice to reduce the number of
unwraps either way
This commit is contained in:
Connor Skees 2020-08-19 14:26:07 -04:00
parent 6849cd578f
commit f2d1a82f34
2 changed files with 10 additions and 7 deletions

View File

@ -95,16 +95,18 @@ impl<'a> Parser<'a> {
break;
}
value *= 16;
span = span.merge(next.pos());
value += as_hex(self.toks.next().unwrap().kind)
span = span.merge(next.pos);
value += as_hex(next.kind);
self.toks.next();
}
if self.toks.peek().is_some() && self.toks.peek().unwrap().kind.is_whitespace() {
if matches!(self.toks.peek(), Some(Token { kind: ' ', .. }) | Some(Token { kind: '\n', .. }) | Some(Token { kind: '\t', .. }))
{
self.toks.next();
}
} else {
let next = self.toks.next().unwrap();
span = span.merge(next.pos());
value = next.kind as u32;
span = span.merge(first.pos);
value = first.kind as u32;
self.toks.next();
}
let c = std::char::from_u32(value).ok_or(("Invalid escape sequence.", span))?;

View File

@ -372,7 +372,8 @@ impl<'a> Parser<'a> {
span = span.merge(next.pos);
self.toks.peek_forward(1);
}
if self.toks.peek().is_some() && self.toks.peek().unwrap().kind.is_whitespace() {
if matches!(self.toks.peek(), Some(Token { kind: ' ', .. }) | Some(Token { kind: '\n', .. }) | Some(Token { kind: '\t', .. }))
{
self.toks.peek_forward(1);
}
} else {