From 7e97f16f4126d1a30e813a110b45341d6bfa11d8 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Tue, 22 Aug 2023 22:46:00 +0200 Subject: [PATCH] Fix list result parsing --- src/machine/lib_machine.rs | 20 ++++++++++++++++++++ src/machine/parsed_results.rs | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index a90e7adc..9c524588 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -171,6 +171,26 @@ mod tests { ); } + #[tokio::test] + async fn list_results() { + let mut machine = Machine::new_lib(); + machine.load_module_string( + "facts", + r#" + list([1,2,3]). + "#.to_string()); + + let result = machine.run_query(String::from("list(X).")); + assert_eq!( + result, + Ok(QueryResolution::Matches(vec![ + QueryMatch::from(btreemap! { + "X" => Value::List(Vec::from([Value::from("1"), Value::from("2"), Value::from("3")])) + }), + ])) + ); + } + #[tokio::test] async fn consult() { diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index aaec386d..ec9b92b8 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -120,7 +120,7 @@ impl From> for QueryResolution { fn parse_prolog_response(input: &str) -> HashMap { let mut map: HashMap = HashMap::new(); // Use regex to match strings including commas inside them - let re = Regex::new(r"(\w+)\s=\s([^,]*'[^']*'[^,]*|[^,]*)").unwrap(); + let re = Regex::new(r"(\w+)\s=\s((?:[^,\[]|\[[^\]]*\])*)").unwrap(); for cap in re.captures_iter(input) { let key = cap[1].to_string(); -- 2.54.0