From 7b66c35e16d57d5b231bc99dfbf30a8eec5f2dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 31 Dec 2022 12:27:51 +0100 Subject: [PATCH] Parser - Synchronize method --- src/parser.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index 5df20d1..5fdb0a6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -29,6 +29,27 @@ impl Parser { } } + /// Synchronize the parser after an error. + fn _synchronize(&mut self) { + self.advance(); + while !self.is_at_end() { + if self.previous().token_type == TokenType::Semicolon + || matches!( + self.peek().token_type, + TokenType::Class + | TokenType::Fun + | TokenType::If + | TokenType::Print + | TokenType::Return + | TokenType::Var + | TokenType::While + ) + { + return; + } + } + } + /* ------------------------ * * RECURSIVE DESCENT PARSER * * ------------------------ */