Skip to main content

pddl_core/parsers/terms/
basic_function_term.rs

1//! Provides parsers for basic function terms (`<basic-function-term>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{parens, parse_function_symbol, parse_name};
6use crate::types::BasicFunctionTerm;
7
8/// Parses a basic function term (`<basic-function-term>`), i.e. `<function-symbol> | (<function-symbol> <name>*)`.
9pub(crate) fn parse_basic_function_term(node: &Node) -> R<BasicFunctionTerm> {
10    if node.atom().is_some() {
11        return parse_function_symbol(node).map(|symbol| BasicFunctionTerm::new(symbol, []));
12    }
13    parens(node, |args| {
14        let symbol = parse_function_symbol(args.next()?)?;
15        let names = args.list0(parse_name)?;
16        Ok(BasicFunctionTerm::new(symbol, names))
17    })
18}
19
20crate::parsers::impl_parser!(BasicFunctionTerm => parse_basic_function_term);
21
22#[cfg(test)]
23mod tests {
24    use crate::parsers::UnwrapValue;
25    use assert2::check;
26
27    use crate::*;
28
29    #[test]
30    fn test_parse() {
31        let value = BasicFunctionTerm::from_str("abcde").unwrap();
32        check!(value == BasicFunctionTerm::new("abcde".into(), []));
33
34        let value = BasicFunctionTerm::from_str("(abcde)").unwrap();
35        check!(value == BasicFunctionTerm::new("abcde".into(), []));
36
37        let value = BasicFunctionTerm::from_str("(abcde x y z)").unwrap();
38        check!(
39            value == BasicFunctionTerm::new("abcde".into(), ["x".into(), "y".into(), "z".into()])
40        );
41    }
42
43    #[test]
44    fn doc_example_0() {
45        check!(BasicFunctionTerm::from_str("abcde")
46            .is_value(BasicFunctionTerm::new("abcde".into(), [])));
47
48        check!(BasicFunctionTerm::from_str("(abcde)")
49            .is_value(BasicFunctionTerm::new("abcde".into(), [])));
50
51        check!(
52            BasicFunctionTerm::from_str("(abcde x y z)").is_value(BasicFunctionTerm::new(
53                "abcde".into(),
54                ["x".into(), "y".into(), "z".into()]
55            ))
56        );
57    }
58
59    #[test]
60    fn doc_example_1() {
61        let value = BasicFunctionTerm::from_str("abcde").unwrap();
62        check!(value == BasicFunctionTerm::new("abcde".into(), []));
63
64        let value = BasicFunctionTerm::from_str("(abcde)").unwrap();
65        check!(value == BasicFunctionTerm::new("abcde".into(), []));
66
67        let value = BasicFunctionTerm::from_str("(abcde x y z)").unwrap();
68        check!(
69            value == BasicFunctionTerm::new("abcde".into(), ["x".into(), "y".into(), "z".into()])
70        );
71    }
72}