fix(tazjin/rlox): Resynchronise after panicking

Change-Id: I60939f7a2c523b6ca1e9782e58c97959da38cfff
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2591
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-03-03 13:08:40 +02:00 committed by tazjin
parent b810c46a45
commit 822e5ae57f

View file

@ -174,7 +174,13 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
}
fn declaration(&mut self) -> LoxResult<()> {
self.statement()
self.statement()?;
if self.panic {
self.synchronise();
}
Ok(())
}
fn statement(&mut self) -> LoxResult<()> {
@ -396,6 +402,31 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
fn check(&self, token: &TokenKind) -> bool {
return self.current().kind == *token;
}
fn synchronise(&mut self) {
self.panic = false;
while self.current().kind != TokenKind::Eof {
if self.previous().kind == TokenKind::Semicolon {
return;
}
match self.current().kind {
TokenKind::Class
| TokenKind::Fun
| TokenKind::Var
| TokenKind::For
| TokenKind::If
| TokenKind::While
| TokenKind::Print
| TokenKind::Return => return,
_ => {
self.advance();
}
}
}
}
}
pub fn compile(code: &str) -> Result<(Interner, Chunk), Vec<Error>> {