Refactor parsing input statements

This commit is contained in:
Patrick Lühne 2020-05-12 05:05:49 +02:00
parent e5d8a8a96b
commit 0d63a721c7
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -153,11 +153,11 @@ fn formula_statement_body<'i>(input: &'i str, problem: &crate::Problem)
formula(input, problem) formula(input, problem)
} }
fn domain_specifier<'i>(input: &'i str) fn domain_specifier<'i>(mut input: &'i str)
-> Result<(Option<crate::Domain>, &'i str), crate::Error> -> Result<(Option<crate::Domain>, &'i str), crate::Error>
{ {
let original_input = input; let original_input = input;
let input = input.trim_start(); input = input.trim_start();
if input.starts_with("->") if input.starts_with("->")
{ {
@ -165,13 +165,13 @@ fn domain_specifier<'i>(input: &'i str)
input_characters.next(); input_characters.next();
input_characters.next(); input_characters.next();
let input = input_characters.as_str().trim_start(); input = input_characters.as_str().trim_start();
let (identifier, remaining_input) = let (identifier, remaining_input) =
foliage::parse::tokens::identifier(input) foliage::parse::tokens::identifier(input)
.ok_or_else(|| crate::Error::new_expected_identifier())?; .ok_or_else(|| crate::Error::new_expected_identifier())?;
let input = remaining_input; input = remaining_input;
match identifier match identifier
{ {
@ -186,6 +186,32 @@ fn domain_specifier<'i>(input: &'i str)
} }
} }
fn predicate_arity_specifier<'i>(mut input: &'i str)
-> Result<(Option<usize>, &'i str), crate::Error>
{
let original_input = input;
input = input.trim_start();
let mut input_characters = input.chars();
if input_characters.next() == Some('/')
{
input = input_characters.as_str().trim_start();
let (arity, remaining_input) = foliage::parse::tokens::number(input)
.map_err(|error| crate::Error::new_parse_predicate_declaration().with(error))?
.ok_or_else(|| crate::Error::new_parse_predicate_declaration())?;
input = remaining_input;
Ok((Some(arity), input))
}
else
{
Ok((None, original_input))
}
}
fn input_statement_body<'i>(mut input: &'i str, problem: &crate::Problem) fn input_statement_body<'i>(mut input: &'i str, problem: &crate::Problem)
-> Result<&'i str, crate::Error> -> Result<&'i str, crate::Error>
{ {
@ -211,42 +237,23 @@ fn input_statement_body<'i>(mut input: &'i str, problem: &crate::Problem)
input = remaining_input.trim_start(); input = remaining_input.trim_start();
let mut input_characters = input.chars();
match input_characters.next()
{
// Parse input predicate specifiers // Parse input predicate specifiers
Some('/') => if let (Some(arity), remaining_input) = predicate_arity_specifier(input)?
{ {
input = input_characters.as_str().trim_start(); input = remaining_input;
let (arity, remaining_input) = foliage::parse::tokens::number(input)
.map_err(|error| crate::Error::new_parse_predicate_declaration().with(error))?
.ok_or_else(|| crate::Error::new_parse_predicate_declaration())?;
input = remaining_input.trim_start();
let mut input_predicate_declarations = let mut input_predicate_declarations =
problem.input_predicate_declarations.borrow_mut(); problem.input_predicate_declarations.borrow_mut();
use foliage::FindOrCreatePredicateDeclaration; use foliage::FindOrCreatePredicateDeclaration as _;
let predicate_declaration = let predicate_declaration =
problem.find_or_create_predicate_declaration(constant_or_predicate_name, arity); problem.find_or_create_predicate_declaration(constant_or_predicate_name, arity);
input_predicate_declarations.insert(predicate_declaration); input_predicate_declarations.insert(predicate_declaration);
let mut input_characters = input.chars();
match input_characters.next()
{
Some(',') => input = input_characters.as_str(),
_ => break,
} }
},
// Parse input constant specifiers // Parse input constant specifiers
Some(_) else
| None =>
{ {
let (domain, remaining_input) = match domain_specifier(input)? let (domain, remaining_input) = match domain_specifier(input)?
{ {
@ -259,7 +266,7 @@ fn input_statement_body<'i>(mut input: &'i str, problem: &crate::Problem)
let mut input_constant_declarations = let mut input_constant_declarations =
problem.input_constant_declarations.borrow_mut(); problem.input_constant_declarations.borrow_mut();
use foliage::FindOrCreateFunctionDeclaration; use foliage::FindOrCreateFunctionDeclaration as _;
let constant_declaration = let constant_declaration =
problem.find_or_create_function_declaration(constant_or_predicate_name, 0); problem.find_or_create_function_declaration(constant_or_predicate_name, 0);
@ -270,6 +277,7 @@ fn input_statement_body<'i>(mut input: &'i str, problem: &crate::Problem)
problem.input_constant_declaration_domains.borrow_mut(); problem.input_constant_declaration_domains.borrow_mut();
input_constant_declaration_domains.insert(constant_declaration, domain); input_constant_declaration_domains.insert(constant_declaration, domain);
}
let mut input_characters = input.chars(); let mut input_characters = input.chars();
@ -279,8 +287,6 @@ fn input_statement_body<'i>(mut input: &'i str, problem: &crate::Problem)
_ => break, _ => break,
} }
} }
}
}
input = input.trim_start(); input = input.trim_start();