Fix list result parsing

This commit is contained in:
Nicolas Luck
2023-08-22 22:46:00 +02:00
parent ce1c8aac4c
commit 7e97f16f41
2 changed files with 21 additions and 1 deletions

View File

@@ -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] #[tokio::test]
async fn consult() { async fn consult() {

View File

@@ -120,7 +120,7 @@ impl From<Vec<QueryResolutionLine>> for QueryResolution {
fn parse_prolog_response(input: &str) -> HashMap<String, String> { fn parse_prolog_response(input: &str) -> HashMap<String, String> {
let mut map: HashMap<String, String> = HashMap::new(); let mut map: HashMap<String, String> = HashMap::new();
// Use regex to match strings including commas inside them // 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) { for cap in re.captures_iter(input) {
let key = cap[1].to_string(); let key = cap[1].to_string();