From d4e35a85f9c0ac1aeeee7025bd93fc549aa4c074 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Thu, 3 Oct 2024 00:18:39 +0000 Subject: [PATCH] add .deep_clone() --- parser/src/constraint.rs | 6 ++++++ parser/src/earley/parser.rs | 7 +++++++ parser/src/tokenparser.rs | 7 +++++++ 3 files changed, 20 insertions(+) diff --git a/parser/src/constraint.rs b/parser/src/constraint.rs index c9729320..bdbd6b2c 100644 --- a/parser/src/constraint.rs +++ b/parser/src/constraint.rs @@ -65,6 +65,12 @@ impl Constraint { } } + pub fn deep_clone(&self) -> Self { + let mut copy = self.clone(); + copy.parser = self.parser.deep_clone(); + copy + } + fn save_progress_and_result(&mut self, res: StepResult) { self.last_res = res; if self.log_json_progress { diff --git a/parser/src/earley/parser.rs b/parser/src/earley/parser.rs index 9214360d..514167b1 100644 --- a/parser/src/earley/parser.rs +++ b/parser/src/earley/parser.rs @@ -1855,4 +1855,11 @@ impl Parser { let mut shared = self.shared.lock().unwrap(); self.state.model_variables(&mut shared) } + + pub fn deep_clone(&self) -> Self { + let mut copy = self.clone(); + let shared = self.shared.lock().unwrap(); + copy.shared = Arc::new(Mutex::new(shared.clone())); + copy + } } diff --git a/parser/src/tokenparser.rs b/parser/src/tokenparser.rs index 4fe3552f..b37237bd 100644 --- a/parser/src/tokenparser.rs +++ b/parser/src/tokenparser.rs @@ -109,6 +109,13 @@ impl TokenParser { }) } + // regular .clone() uses a shared lexer state + pub fn deep_clone(&self) -> Self { + let mut copy = self.clone(); + copy.parser = self.parser.deep_clone(); + copy + } + pub fn stop_reason(&self) -> StopReason { self.stop_reason }