Skip to main content

pddl_core/parsers/terms/
function_term.rs

1//! Provides parsers for function terms (`<function-term>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{parens, parse_function_symbol, parse_term};
6use crate::types::FunctionTerm;
7
8/// Parses a function term (`<function-term>`), i.e. `(<function-symbol> <term>*)`.
9pub(crate) fn parse_function_term(node: &Node) -> R<FunctionTerm> {
10    parens(node, |args| {
11        let symbol = parse_function_symbol(args.next()?)?;
12        let terms = args.list0(parse_term)?;
13        Ok(FunctionTerm::new(symbol, terms))
14    })
15}
16
17crate::parsers::impl_parser!(FunctionTerm => parse_function_term);
18
19#[cfg(test)]
20mod tests {
21    use crate::parsers::UnwrapValue;
22    use assert2::check;
23
24    use crate::*;
25
26    #[test]
27    fn test_parse() {
28        check!(FunctionTerm::from_str("(fun-sym)")
29            .is_value(FunctionTerm::new("fun-sym".into(), vec![])));
30
31        let x = Term::Name("x".into());
32        check!(FunctionTerm::from_str("(fun-sym x)")
33            .is_value(FunctionTerm::new("fun-sym".into(), vec![x])));
34
35        let x = Term::Name("x".into());
36        let y = Term::Variable("y".into());
37        check!(FunctionTerm::from_str("(fun-sym ?y x)")
38            .is_value(FunctionTerm::new("fun-sym".into(), vec![y, x])));
39
40        let x = Term::Name("x".into());
41        let y = Term::Variable("y".into());
42        let a = Term::Name("a".into());
43        let ft = Term::Function(FunctionTerm::new(FunctionSymbol::from("fn"), vec![a]));
44        check!(FunctionTerm::from_str("(fun-sym ?y x (fn a))")
45            .is_value(FunctionTerm::new("fun-sym".into(), vec![y, x, ft])));
46    }
47
48    #[test]
49    fn whitespace_before_closing_paren() {
50        for input in ["(fun-sym x )", "(fun-sym x\n  )", "(fun-sym x ; c\n)"] {
51            let x = Term::Name("x".into());
52            check!(FunctionTerm::from_str(input)
53                .is_value(FunctionTerm::new("fun-sym".into(), vec![x])));
54        }
55    }
56
57    #[test]
58    fn doc_example_0() {
59        check!(FunctionTerm::from_str("(fun-sym)")
60            .is_value(FunctionTerm::new("fun-sym".into(), vec![])));
61
62        let x = Term::Name("x".into());
63        check!(FunctionTerm::from_str("(fun-sym x)")
64            .is_value(FunctionTerm::new("fun-sym".into(), vec![x])));
65
66        let x = Term::Name("x".into());
67        let y = Term::Variable("y".into());
68        check!(FunctionTerm::from_str("(fun-sym ?y x)")
69            .is_value(FunctionTerm::new("fun-sym".into(), vec![y, x])));
70
71        let x = Term::Name("x".into());
72        let y = Term::Variable("y".into());
73        let a = Term::Name("a".into());
74        let ft = Term::Function(FunctionTerm::new(FunctionSymbol::from("fn"), vec![a]));
75        check!(FunctionTerm::from_str("(fun-sym ?y x (fn a))")
76            .is_value(FunctionTerm::new("fun-sym".into(), vec![y, x, ft])));
77    }
78}