Refactor parsing formulas

This commit is contained in:
Patrick Lühne 2020-05-12 05:20:48 +02:00
parent 2f48e51244
commit 32b18e2b63
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
3 changed files with 27 additions and 9 deletions

View File

@ -7,6 +7,7 @@ edition = "2018"
[dependencies] [dependencies]
atty = "0.2" atty = "0.2"
foliage = {git = "https://github.com/pluehne/foliage", branch = "parser-new"} foliage = {git = "https://github.com/pluehne/foliage", branch = "parser-new"}
itertools = "0.9"
log = "0.4" log = "0.4"
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
regex = "1" regex = "1"

View File

@ -20,6 +20,7 @@ pub enum Kind
UnknownProofDirection(String), UnknownProofDirection(String),
UnknownDomainIdentifier(String), UnknownDomainIdentifier(String),
VariableNameNotAllowed(String), VariableNameNotAllowed(String),
FormulaNotClosed(std::rc::Rc<foliage::VariableDeclarations>),
WriteTPTPProgram, WriteTPTPProgram,
RunVampire, RunVampire,
// TODO: rename to something Vampire-specific // TODO: rename to something Vampire-specific
@ -135,6 +136,12 @@ impl Error
Self::new(Kind::VariableNameNotAllowed(variable_name)) Self::new(Kind::VariableNameNotAllowed(variable_name))
} }
pub(crate) fn new_formula_not_closed(free_variables: std::rc::Rc<foliage::VariableDeclarations>)
-> Self
{
Self::new(Kind::FormulaNotClosed(free_variables))
}
pub(crate) fn new_write_tptp_program<S: Into<Source>>(source: S) -> Self pub(crate) fn new_write_tptp_program<S: Into<Source>>(source: S) -> Self
{ {
Self::new(Kind::WriteTPTPProgram).with(source) Self::new(Kind::WriteTPTPProgram).with(source)
@ -192,6 +199,17 @@ impl std::fmt::Debug for Error
"variable name “{}” not allowed (program variables must start with X, Y, or Z and integer variables with I, J, K, L, M, or N)", "variable name “{}” not allowed (program variables must start with X, Y, or Z and integer variables with I, J, K, L, M, or N)",
variable_name), variable_name),
Kind::WriteTPTPProgram => write!(formatter, "error writing TPTP program"), Kind::WriteTPTPProgram => write!(formatter, "error writing TPTP program"),
Kind::FormulaNotClosed(free_variable_declarations) =>
{
let free_variable_names = free_variable_declarations
.iter()
.map(|variable_declaration| &variable_declaration.name);
let free_variable_names_output = itertools::join(free_variable_names, ", ");
write!(formatter, "formula may not contain free variables (free variables in this formula: {})",
free_variable_names_output)
},
Kind::RunVampire => write!(formatter, "could not run Vampire"), Kind::RunVampire => write!(formatter, "could not run Vampire"),
Kind::ProveProgram(exit_code, ref stdout, ref stderr) => Kind::ProveProgram(exit_code, ref stdout, ref stderr) =>
{ {

View File

@ -117,39 +117,38 @@ where
Ok((open_formula, remaining_input)) Ok((open_formula, remaining_input))
} }
fn formula<'i, D>(input: &'i str, declarations: &D) fn formula<'i, D>(mut input: &'i str, declarations: &D)
-> Result<(foliage::Formula, &'i str), crate::Error> -> Result<(foliage::Formula, &'i str), crate::Error>
where where
D: foliage::FindOrCreateFunctionDeclaration D: foliage::FindOrCreateFunctionDeclaration
+ foliage::FindOrCreatePredicateDeclaration + foliage::FindOrCreatePredicateDeclaration
+ crate::traits::AssignVariableDeclarationDomain, + crate::traits::AssignVariableDeclarationDomain,
{ {
let (open_formula, input) = open_formula(input, declarations)?; let (open_formula, remaining_input) = open_formula(input, declarations)?;
input = remaining_input;
if !open_formula.free_variable_declarations.is_empty() if !open_formula.free_variable_declarations.is_empty()
{ {
// TODO: improve return Err(crate::Error::new_formula_not_closed(open_formula.free_variable_declarations));
panic!("formula may not contain free variables");
} }
Ok((open_formula.formula, input)) Ok((open_formula.formula, input))
} }
fn formula_statement_body<'i>(input: &'i str, problem: &crate::Problem) fn formula_statement_body<'i>(mut input: &'i str, problem: &crate::Problem)
-> Result<(foliage::Formula, &'i str), crate::Error> -> Result<(foliage::Formula, &'i str), crate::Error>
{ {
let input = input.trim_start(); input = input.trim_start();
let mut input_characters = input.chars(); let mut input_characters = input.chars();
let remaining_input = match input_characters.next() input = match input_characters.next()
{ {
Some(':') => input_characters.as_str(), Some(':') => input_characters.as_str(),
_ => return Err(crate::Error::new_expected_colon()), _ => return Err(crate::Error::new_expected_colon()),
}; };
let input = remaining_input;
formula(input, problem) formula(input, problem)
} }