Pass declarations to term parser
This commit is contained in:
parent
555f983285
commit
f8918628fa
@ -332,8 +332,12 @@ where
|
|||||||
let input_right = comparison_operator_split.next().unwrap()?;
|
let input_right = comparison_operator_split.next().unwrap()?;
|
||||||
assert!(comparison_operator_split.next().is_none());
|
assert!(comparison_operator_split.next().is_none());
|
||||||
|
|
||||||
let argument_left = TermStr::new(input_left).parse(level + 1)?;
|
let argument_left =
|
||||||
let argument_right = TermStr::new(input_right).parse(level + 1)?;
|
TermStr::new(input_left, self.declarations, self.variable_declaration_stack)
|
||||||
|
.parse(level + 1)?;
|
||||||
|
let argument_right =
|
||||||
|
TermStr::new(input_right, self.declarations, self.variable_declaration_stack)
|
||||||
|
.parse(level + 1)?;
|
||||||
|
|
||||||
return Ok(crate::Formula::compare(comparison_operator, Box::new(argument_left),
|
return Ok(crate::Formula::compare(comparison_operator, Box::new(argument_left),
|
||||||
Box::new(argument_right)));
|
Box::new(argument_right)));
|
||||||
@ -353,7 +357,9 @@ where
|
|||||||
{
|
{
|
||||||
let functor = |token: &_| *token == Token::Symbol(Symbol::Comma);
|
let functor = |token: &_| *token == Token::Symbol(Symbol::Comma);
|
||||||
let arguments = Tokens::new_filter(parenthesized_expression, functor).split()
|
let arguments = Tokens::new_filter(parenthesized_expression, functor).split()
|
||||||
.map(|argument| TermStr::new(argument?).parse(level + 1))
|
.map(|argument| TermStr::new(argument?, self.declarations,
|
||||||
|
self.variable_declaration_stack)
|
||||||
|
.parse(level + 1))
|
||||||
.collect::<Result<_, _>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
|
|
||||||
(arguments, input)
|
(arguments, input)
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
use super::tokens::*;
|
use super::tokens::*;
|
||||||
|
|
||||||
pub fn parse_term(input: &str) -> Result<crate::Term, crate::parse::Error>
|
|
||||||
{
|
|
||||||
let term_str = TermStr::new(input);
|
|
||||||
term_str.parse(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn function_name(input: &str) -> Option<(&str, &str)>
|
pub(crate) fn function_name(input: &str) -> Option<(&str, &str)>
|
||||||
{
|
{
|
||||||
let (identifier, remaining_input) = identifier(input)?;
|
let (identifier, remaining_input) = identifier(input)?;
|
||||||
@ -168,18 +162,26 @@ impl std::fmt::Debug for ArithmeticOperatorClass
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct TermStr<'i>
|
pub(crate) struct TermStr<'i, 'd, 'v, 'p, D>
|
||||||
{
|
{
|
||||||
input: &'i str,
|
input: &'i str,
|
||||||
|
declarations: &'d D,
|
||||||
|
variable_declaration_stack: &'v crate::VariableDeclarationStackLayer<'p>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'i> TermStr<'i>
|
impl<'i, 'd, 'v, 'p, D> TermStr<'i, 'd, 'v, 'p, D>
|
||||||
|
where
|
||||||
|
D: crate::FindOrCreateFunctionDeclaration + crate::FindOrCreatePredicateDeclaration,
|
||||||
{
|
{
|
||||||
pub fn new(input: &'i str) -> Self
|
pub fn new(input: &'i str, declarations: &'d D,
|
||||||
|
variable_declaration_stack: &'v crate::VariableDeclarationStackLayer<'p>)
|
||||||
|
-> Self
|
||||||
{
|
{
|
||||||
Self
|
Self
|
||||||
{
|
{
|
||||||
input,
|
input,
|
||||||
|
declarations,
|
||||||
|
variable_declaration_stack,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,7 +326,7 @@ impl<'i> TermStr<'i>
|
|||||||
|
|
||||||
if top_level_arithmetic_operator_class == ArithmeticOperatorClass::Exponential
|
if top_level_arithmetic_operator_class == ArithmeticOperatorClass::Exponential
|
||||||
{
|
{
|
||||||
return exponentiate(
|
return self.exponentiate(
|
||||||
self.filter_by_arithmetic_operator_class(top_level_arithmetic_operator_class)
|
self.filter_by_arithmetic_operator_class(top_level_arithmetic_operator_class)
|
||||||
.split(), level + 1);
|
.split(), level + 1);
|
||||||
}
|
}
|
||||||
@ -336,7 +338,9 @@ impl<'i> TermStr<'i>
|
|||||||
let (first_argument, first_binary_operator) = argument_iterator.next().ok_or_else(||
|
let (first_argument, first_binary_operator) = argument_iterator.next().ok_or_else(||
|
||||||
crate::parse::Error::new_expected_term(
|
crate::parse::Error::new_expected_term(
|
||||||
crate::parse::error::Location::new(0, Some(0))))??;
|
crate::parse::error::Location::new(0, Some(0))))??;
|
||||||
let first_argument = TermStr::new(first_argument).parse(level + 1)?;
|
let first_argument =
|
||||||
|
TermStr::new(first_argument, self.declarations, self.variable_declaration_stack)
|
||||||
|
.parse(level + 1)?;
|
||||||
// TODO: improve error handling if the terms between the operators are invalid
|
// TODO: improve error handling if the terms between the operators are invalid
|
||||||
|
|
||||||
let (accumulator, last_binary_operator) =
|
let (accumulator, last_binary_operator) =
|
||||||
@ -344,7 +348,9 @@ impl<'i> TermStr<'i>
|
|||||||
|(accumulator, binary_operator), argument|
|
|(accumulator, binary_operator), argument|
|
||||||
{
|
{
|
||||||
let (argument, next_binary_operator) = argument?;
|
let (argument, next_binary_operator) = argument?;
|
||||||
let argument = TermStr::new(argument).parse(level + 1)?;
|
let argument = TermStr::new(argument, self.declarations,
|
||||||
|
self.variable_declaration_stack)
|
||||||
|
.parse(level + 1)?;
|
||||||
let binary_operation =
|
let binary_operation =
|
||||||
crate::BinaryOperation::new(binary_operator, Box::new(accumulator),
|
crate::BinaryOperation::new(binary_operator, Box::new(accumulator),
|
||||||
Box::new(argument));
|
Box::new(argument));
|
||||||
@ -355,7 +361,9 @@ impl<'i> TermStr<'i>
|
|||||||
|
|
||||||
// The last item hasn’t been consumed yet, so it’s safe to unwrap it
|
// The last item hasn’t been consumed yet, so it’s safe to unwrap it
|
||||||
let last_argument = argument_iterator.remaining_input().unwrap();
|
let last_argument = argument_iterator.remaining_input().unwrap();
|
||||||
let last_argument = TermStr::new(last_argument).parse(level + 1)?;
|
let last_argument =
|
||||||
|
TermStr::new(last_argument, self.declarations, self.variable_declaration_stack)
|
||||||
|
.parse(level + 1)?;
|
||||||
let last_binary_operation =
|
let last_binary_operation =
|
||||||
crate::BinaryOperation::new(last_binary_operator, Box::new(accumulator),
|
crate::BinaryOperation::new(last_binary_operator, Box::new(accumulator),
|
||||||
Box::new(last_argument));
|
Box::new(last_argument));
|
||||||
@ -445,7 +453,9 @@ impl<'i> TermStr<'i>
|
|||||||
{
|
{
|
||||||
let functor = |token: &_| *token == Token::Symbol(Symbol::Comma);
|
let functor = |token: &_| *token == Token::Symbol(Symbol::Comma);
|
||||||
let arguments = Tokens::new_filter(parenthesized_expression, functor).split()
|
let arguments = Tokens::new_filter(parenthesized_expression, functor).split()
|
||||||
.map(|argument| TermStr::new(argument?).parse(level + 1))
|
.map(|argument| TermStr::new(argument?, self.declarations,
|
||||||
|
self.variable_declaration_stack)
|
||||||
|
.parse(level + 1))
|
||||||
.collect::<Result<_, _>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
|
|
||||||
(arguments, input)
|
(arguments, input)
|
||||||
@ -481,16 +491,17 @@ impl<'i> TermStr<'i>
|
|||||||
crate::parse::error::Location::new(0, Some(0))));
|
crate::parse::error::Location::new(0, Some(0))));
|
||||||
}
|
}
|
||||||
|
|
||||||
return TermStr::new(parenthesized_expression).parse(level + 1);
|
return TermStr::new(parenthesized_expression, self.declarations,
|
||||||
|
self.variable_declaration_stack)
|
||||||
|
.parse(level + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(crate::parse::Error::new_unexpected_token(
|
Err(crate::parse::Error::new_unexpected_token(
|
||||||
crate::parse::error::Location::new(0, Some(0))))
|
crate::parse::error::Location::new(0, Some(0))))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: refactor
|
// TODO: refactor
|
||||||
fn exponentiate_inner<'i, T>(mut argument_iterator: T, level: usize)
|
fn exponentiate_inner<T>(&self, mut argument_iterator: T, level: usize)
|
||||||
-> Result<Option<crate::Term>, crate::parse::Error>
|
-> Result<Option<crate::Term>, crate::parse::Error>
|
||||||
where
|
where
|
||||||
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
||||||
@ -500,8 +511,10 @@ where
|
|||||||
Some(argument) =>
|
Some(argument) =>
|
||||||
{
|
{
|
||||||
// TODO: improve error handling if antecedent cannot be parsed
|
// TODO: improve error handling if antecedent cannot be parsed
|
||||||
let argument = TermStr::new(argument?).parse(level)?;
|
let argument =
|
||||||
match exponentiate_inner(argument_iterator, level)?
|
TermStr::new(argument?, self.declarations, self.variable_declaration_stack)
|
||||||
|
.parse(level)?;
|
||||||
|
match self.exponentiate_inner(argument_iterator, level)?
|
||||||
{
|
{
|
||||||
Some(next_argument) => Ok(Some(crate::Term::exponentiate(Box::new(argument),
|
Some(next_argument) => Ok(Some(crate::Term::exponentiate(Box::new(argument),
|
||||||
Box::new(next_argument)))),
|
Box::new(next_argument)))),
|
||||||
@ -512,7 +525,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exponentiate<'i, T>(mut argument_iterator: T, level: usize)
|
fn exponentiate<T>(&self, mut argument_iterator: T, level: usize)
|
||||||
-> Result<crate::Term, crate::parse::Error>
|
-> Result<crate::Term, crate::parse::Error>
|
||||||
where
|
where
|
||||||
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
||||||
@ -522,8 +535,10 @@ where
|
|||||||
Some(argument) =>
|
Some(argument) =>
|
||||||
{
|
{
|
||||||
// TODO: improve error handling if antecedent cannot be parsed
|
// TODO: improve error handling if antecedent cannot be parsed
|
||||||
let argument = TermStr::new(argument?).parse(level)?;
|
let argument =
|
||||||
match exponentiate_inner(argument_iterator, level)?
|
TermStr::new(argument?, self.declarations, self.variable_declaration_stack)
|
||||||
|
.parse(level)?;
|
||||||
|
match self.exponentiate_inner(argument_iterator, level)?
|
||||||
{
|
{
|
||||||
Some(next_argument) =>
|
Some(next_argument) =>
|
||||||
Ok(crate::Term::exponentiate(Box::new(argument), Box::new(next_argument))),
|
Ok(crate::Term::exponentiate(Box::new(argument), Box::new(next_argument))),
|
||||||
@ -535,6 +550,7 @@ where
|
|||||||
crate::parse::error::Location::new(0, Some(0)))),
|
crate::parse::error::Location::new(0, Some(0)))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests
|
mod tests
|
||||||
|
Loading…
Reference in New Issue
Block a user