mod actions;
mod durative;
mod expressions;
mod goals;
mod structure;
mod terms;
pub(crate) use structure::atomic_formula_skeleton::parse_atomic_formula_skeleton;
pub(crate) use structure::atomic_function_skeleton::parse_atomic_function_skeleton;
pub(crate) use structure::constants_def::parse_constants_def;
pub(crate) use structure::domain::parse_domain;
pub(crate) use structure::domain_constraints_def::parse_domain_constraints_def;
pub(crate) use structure::functions_def::parse_functions_def;
pub(crate) use structure::goal_def::parse_problem_goal_def;
pub(crate) use structure::init_def::parse_problem_init_def;
pub(crate) use structure::init_el::parse_init_el;
pub(crate) use structure::length_spec::parse_problem_length_spec;
pub(crate) use structure::metric_spec::parse_problem_metric_spec;
pub(crate) use structure::objects_def::parse_problem_objects_declaration;
pub(crate) use structure::predicates_def::parse_predicates_def;
pub(crate) use structure::problem::parse_problem;
pub(crate) use structure::problem_constraints_def::parse_problem_constraints_def;
pub(crate) use structure::requirements::parse_require_def;
pub(crate) use structure::structure_def::parse_structure_def;
pub(crate) use structure::timeless_def::parse_timeless_def;
pub(crate) use structure::types_def::parse_types_def;
pub(crate) use actions::action_def::{parse_action_def, parse_event_def, parse_process_def};
pub(crate) use actions::action_symbol::parse_action_symbol;
pub(crate) use actions::c_effect::parse_c_effect;
pub(crate) use actions::cond_effect::parse_effect_condition;
pub(crate) use actions::derived_predicate::parse_derived_predicate;
pub(crate) use actions::effects::parse_effect;
pub(crate) use actions::p_effect::parse_p_effect;
pub(crate) use goals::atomic_formula::atomic_formula;
pub(crate) use goals::con_gd::parse_con_gd;
pub(crate) use goals::gd::parse_gd;
pub(crate) use goals::literal::literal;
pub(crate) use goals::pre_gd::parse_pre_gd;
pub(crate) use goals::pref_con_gd::parse_pref_con_gd;
pub(crate) use goals::pref_gd::parse_pref_gd;
pub(crate) use goals::pref_name::parse_pref_name;
pub(crate) use durative::assign_op_t::parse_assign_op_t;
pub(crate) use durative::d_op::parse_d_op;
pub(crate) use durative::d_value::parse_d_value;
pub(crate) use durative::da_def::parse_da_def;
pub(crate) use durative::da_effect::parse_da_effect;
pub(crate) use durative::da_gd::parse_da_gd;
pub(crate) use durative::da_symbol::parse_da_symbol;
pub(crate) use durative::duration_constraint::parse_duration_constraint;
pub(crate) use durative::f_assign_da::parse_f_assign_da;
pub(crate) use durative::f_exp_da::parse_f_exp_da;
pub(crate) use durative::f_exp_t::parse_f_exp_t;
pub(crate) use durative::interval::parse_interval;
pub(crate) use durative::pref_timed_gd::parse_pref_timed_gd;
pub(crate) use durative::simple_duration_constraint::parse_simple_duration_constraint;
pub(crate) use durative::time_specifier::parse_time_specifier;
pub(crate) use durative::timed_effect::parse_timed_effect;
pub(crate) use durative::timed_gd::parse_timed_gd;
pub(crate) use expressions::assign_op::parse_assign_op;
pub(crate) use expressions::binary_comp::parse_binary_comp;
pub(crate) use expressions::binary_op::parse_binary_op;
pub(crate) use expressions::f_comp::parse_f_comp;
pub(crate) use expressions::f_exp::{op_expr, parse_f_exp};
pub(crate) use expressions::f_head::parse_f_head;
pub(crate) use expressions::metric_f_exp::parse_metric_f_exp;
pub(crate) use expressions::optimization::parse_optimization;
pub(crate) use terms::basic_function_term::parse_basic_function_term;
pub(crate) use terms::function_symbol::parse_function_symbol;
pub(crate) use terms::function_term::parse_function_term;
pub(crate) use terms::function_typed_list::function_typed_list;
pub(crate) use terms::name::{is_name, parse_name};
pub(crate) use terms::number::parse_number;
pub(crate) use terms::predicate::parse_predicate;
pub(crate) use terms::primitive_type::parse_primitive_type;
pub(crate) use terms::r#type::parse_type;
pub(crate) use terms::term::parse_term;
pub(crate) use terms::typed_list::{typed_list, typed_variables};
pub(crate) use terms::variable::parse_variable;
mod error;
pub(crate) mod sexpr;
mod suggest;
#[cfg(test)]
mod test_helpers;
mod utilities;
pub use error::ParseError;
pub use structure::pddl_file::check;
use error::{Error, R};
use sexpr::Node;
#[cfg(test)]
pub(crate) use test_helpers::UnwrapValue;
pub(crate) use utilities::{
alt, empty_or, keyword_arg, opt_keyword_arg, parens, prefix, set_once, word, Args,
};
pub trait Parser {
type Item;
fn from_str(input: &str) -> Result<Self::Item, ParseError>;
}
pub(crate) fn parse_nodes<T>(
input: &str,
rule: impl for<'n, 'a> FnOnce(&'n [Node<'a>], usize) -> R<(T, usize)>,
) -> Result<T, ParseError> {
let run = || {
let mut forest = sexpr::read(input)?;
if forest.nodes.is_empty() {
if let Some(error) = forest.unbalanced.take() {
return Err(error);
}
}
let (value, read) = rule(&forest.nodes, input.len())?;
if let Some(extra) = forest.nodes.get(read) {
return Err(extra.error());
}
match forest.unbalanced {
Some(error) => Err(error),
None => Ok(value),
}
};
run().map_err(|e| ParseError::new(input, &e))
}
pub(crate) fn parse_one<T>(
input: &str,
rule: impl for<'n, 'a> FnOnce(&'n Node<'a>) -> R<T>,
) -> Result<T, ParseError> {
parse_nodes(input, |nodes, len| match nodes.first() {
Some(node) => rule(node).map(|value| (value, 1)),
None => Err(Error::at(len, 0)),
})
}
macro_rules! impl_parser {
($($ty:ty => $rule:path),+ $(,)?) => {$(
impl $crate::parsers::Parser for $ty {
type Item = $ty;
fn from_str(input: &str) -> Result<Self::Item, $crate::parsers::ParseError> {
$crate::parsers::parse_one(input, $rule)
}
}
)+};
}
pub(crate) use impl_parser;
#[cfg(test)]
mod tests {
use crate::{BasicFunctionTerm, Domain, Parser};
use assert2::check;
const DOMAIN: &str = "(define (domain d) (:requirements :strips) (:predicates (p)))";
#[test]
fn keyword_parsers_agree_with_try_from() {
macro_rules! check_keywords {
($ty:ty, $names:path: $($name:ident),+ $(,)?) => {{
use $names as names;
$(
let expected = <$ty>::try_from(names::$name).unwrap();
for input in [names::$name.to_string(), names::$name.to_ascii_uppercase()] {
let parsed = <$ty as Parser>::from_str(&input);
check!(parsed == Ok(expected.clone()), "{input}");
let converted = <$ty>::try_from(input.as_str());
check!(converted == Ok(expected.clone()), "{input}");
}
)+
}};
}
use crate::types::*;
check_keywords!(AssignOp, crate::types::expressions::assign_op::names:
CHANGE, ASSIGN, SCALE_UP, SCALE_DOWN, INCREASE, DECREASE);
check_keywords!(TimedAssignOperator, crate::types::durative::assign_op_t::names:
INCREASE, DECREASE);
check_keywords!(BinaryOp, crate::types::expressions::binary_op::names:
MULTIPLICATION, ADDITION, SUBTRACTION, DIVISION);
check_keywords!(BinaryComparison, crate::types::expressions::binary_comp::names:
GREATER_THAN, LESS_THAN, EQUAL, GREATER_THAN_OR_EQUAL, LESS_THAN_OR_EQUAL);
check_keywords!(DurationOperator, crate::types::durative::d_op::names:
EQUAL, GREATER_OR_EQUAL, LESS_THAN_OR_EQUAL);
check_keywords!(Interval, crate::types::durative::interval::names: ALL);
check_keywords!(MultiOp, crate::types::expressions::multi_op::names:
MULTIPLICATION, ADDITION);
check_keywords!(Optimization, crate::types::expressions::optimization::names:
MINIMIZE, MAXIMIZE);
check_keywords!(TimeSpecifier, crate::types::durative::time_specifier::names:
START, END);
check_keywords!(Requirement, crate::types::structure::requirement::names:
STRIPS, TYPING, NEGATIVE_PRECONDITIONS, DISJUNCTIVE_PRECONDITIONS, EQUALITY,
EXISTENTIAL_PRECONDITIONS, UNIVERSAL_PRECONDITIONS, QUANTIFIED_PRECONDITIONS,
CONDITIONAL_EFFECTS, FLUENTS, NUMERIC_FLUENTS, OBJECT_FLUENTS, ADL,
DURATIVE_ACTIONS, DURATION_INEQUALITIES, CONTINUOUS_EFFECTS, DERIVED_PREDICATES,
TIMED_INITIAL_LITERALS, PREFERENCES, CONSTRAINTS, ACTION_COSTS);
}
#[test]
fn test_from_str() {
let value = BasicFunctionTerm::from_str("abcde").unwrap();
check!(value == BasicFunctionTerm::new("abcde".into(), []));
}
#[test]
fn from_str_allows_trailing_whitespace_and_comments() {
let input = format!("{DOMAIN}\n ; trailing comment\n; another\n\n");
check!(Domain::from_str(&input).is_ok());
}
#[test]
fn from_str_rejects_trailing_input() {
let input = format!("{DOMAIN}\n)))garbage");
let error = Domain::from_str(&input).unwrap_err();
check!((error.line(), error.column()) == (2, 1));
check!(error.found() == Some(")))garbage"));
}
#[test]
fn from_str_rejects_empty_input() {
let error = Domain::from_str(" ; nothing\n").unwrap_err();
check!(error.offset() == 12);
check!(error.found() == None);
}
#[test]
fn from_str_rejects_a_stray_closing_parenthesis() {
let error = BasicFunctionTerm::from_str("abc)").unwrap_err();
check!(error.span() == (3..4));
}
#[test]
fn a_leading_stray_closing_parenthesis_is_marked() {
for input in [")", " ) (define (domain d))"] {
let error = Domain::from_str(input).unwrap_err();
check!(
error.found().is_some_and(|found| found.starts_with(')')),
"{input:?}"
);
check!(error.span() == (input.find(')').unwrap()..input.find(')').unwrap() + 1));
}
}
#[test]
fn from_str_rejects_a_second_define_block() {
let input = format!("{DOMAIN}\n(define (problem q) (:domain d))");
let error = Domain::from_str(&input).unwrap_err();
check!(Some(error.offset()) == input.find("(define (problem"));
check!(error.found().unwrap().starts_with("(define (problem q)"));
}
}