diff --git a/pumpkin-solver/src/bin/pumpkin-solver/flatzinc/parser.rs b/pumpkin-solver/src/bin/pumpkin-solver/flatzinc/parser.rs index 3cb413a86..517a9ba72 100644 --- a/pumpkin-solver/src/bin/pumpkin-solver/flatzinc/parser.rs +++ b/pumpkin-solver/src/bin/pumpkin-solver/flatzinc/parser.rs @@ -1,3 +1,7 @@ +//! Reads a FlatZinc file into a [`FlatZincAst`] with [`parse`]. +//! +//! The `flatzinc` crate parses each line as one statement. + use std::io::BufRead; use std::io::BufReader; use std::io::Read; @@ -19,11 +23,19 @@ pub(crate) fn parse(source: impl Read) -> Result { let mut ast_builder = FlatZincAst::builder(); for line in reader.lines() { - let line = line?; + let mut line = line?; + + // When MiniZinc compiles a FlatZinc file again, it adds a `bool2int` call around the + // Boolean variables of an `int_search`. + // The `flatzinc` crate rejects a call inside an annotation, while Pumpkin can search over + // the Boolean variables directly. + if line.trim_start().starts_with("solve") { + line = strip_bool2int(&line); + } match flatzinc::statements::Stmt::from_str(&line) { Ok(stmt) => match stmt { - // Ignore. + // Pumpkin needs neither comments nor predicate declarations. flatzinc::Stmt::Comment(_) | flatzinc::Stmt::Predicate(_) => {} flatzinc::Stmt::Parameter(decl) => ast_builder.add_parameter_decl(decl), @@ -40,6 +52,63 @@ pub(crate) fn parse(source: impl Read) -> Result { ast_builder.build() } +/// Replaces every `bool2int()` in `line` by ``, including calls nested in an +/// argument. +/// +/// A call without a closing parenthesis is left in place, for the parser to reject. +fn strip_bool2int(line: &str) -> String { + const BOOL2INT_CALL_START: &str = "bool2int("; + + // `stripped_line` holds the part of `line` that has been processed, with its calls removed. + // `remaining_line` holds the part that has not been processed yet. + let mut stripped_line = String::new(); + let mut remaining_line = line; + + // Each iteration removes the first call in `remaining_line`. + // For `x, bool2int(f(b)), y`, `before_call` is `x, `, `argument` is `f(b)` and `after_call` is + // `, y`. + while let Some((before_call, argument_and_after)) = + remaining_line.split_once(BOOL2INT_CALL_START) + { + // Without a closing parenthesis, the call stays in the text copied after the loop. + let Some(argument_length) = closing_parenthesis_index(argument_and_after) else { + break; + }; + let argument = &argument_and_after[..argument_length]; + let after_call = &argument_and_after[argument_length + 1..]; + + // Copy the text before the call and the argument, which drops `bool2int(` and its `)`. + // The argument may itself contain calls, so they are stripped as well. + stripped_line.push_str(before_call); + stripped_line.push_str(&strip_bool2int(argument)); + remaining_line = after_call; + } + + // Copy the text after the last call. + stripped_line.push_str(remaining_line); + stripped_line +} + +/// Returns the index of the `)` that closes a parenthesis opened just before `text`, if `text` +/// contains it. +/// +/// For `f(b)), y` it is the second `)`, at index 4. +fn closing_parenthesis_index(text: &str) -> Option { + // The number of parentheses opened in `text` and not yet closed. + let mut open_parentheses = 0; + + for (index, character) in text.char_indices() { + match character { + '(' => open_parentheses += 1, + ')' if open_parentheses == 0 => return Some(index), + ')' => open_parentheses -= 1, + _ => {} + } + } + + None +} + fn parse_var_decl( ast: &mut FlatZincAstBuilder, decl: flatzinc::VarDeclItem, @@ -136,7 +205,6 @@ fn parse_var_decl( } flatzinc::VarDeclItem::Int { id, expr, annos } => { - // For unbounded integers, we take the minimum and maximum possible values info!( "Unbounded integer detected in the model, defaulting to minimum value {MIN_INTEGER_VALUE} and maximum value {MAX_INTEGER_VALUE}. If this is insufficient then consider setting the bounds manually" ); @@ -167,3 +235,38 @@ fn parse_var_decl( } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn bool2int_in_search_annotation_is_stripped() { + let line = "solve :: seq_search([int_search(x,first_fail,indomain_min,complete),\ + int_search(bool2int(b),first_fail,indomain_min,complete),\ + int_search(bool2int([c,d]),input_order,indomain_max)]) minimize o;"; + + assert_eq!( + strip_bool2int(line), + "solve :: seq_search([int_search(x,first_fail,indomain_min,complete),\ + int_search(b,first_fail,indomain_min,complete),\ + int_search([c,d],input_order,indomain_max)]) minimize o;" + ); + } + + #[test] + fn nested_bool2int_is_stripped_up_to_its_closing_parenthesis() { + assert_eq!( + strip_bool2int("solve :: f(bool2int(g(bool2int(b))), bool2int(c)) satisfy;"), + "solve :: f(g(b), c) satisfy;" + ); + } + + #[test] + fn unclosed_bool2int_is_left_in_place() { + assert_eq!( + strip_bool2int("solve :: f(bool2int(b satisfy;"), + "solve :: f(bool2int(b satisfy;" + ); + } +}