1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Provides parsers for constant definitions.
use nom::combinator::map;
use crate::parsers::{function_typed_list, parse_atomic_function_skeleton};
use crate::parsers::{prefix_expr, ParseResult, Span};
use crate::types::Functions;
/// Parses constant definitions, i.e. `(:constants <typed list (name)>)`.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_functions_def, preamble::*};
/// # use pddl::{Variable, AtomicFormulaSkeleton, Predicate, PredicateDefinitions, FunctionTypedList, FunctionTyped, AtomicFunctionSkeleton, FunctionSymbol, Functions};
/// # use pddl::{Type, Typed, TypedList};
/// let input = "(:functions (battery-amount ?r - rover))";
/// assert!(parse_functions_def(input).is_value(
/// Functions::from_iter([
/// FunctionTyped::new_number(
/// AtomicFunctionSkeleton::new(
/// FunctionSymbol::from_str("battery-amount"),
/// TypedList::from_iter([
/// Typed::new(Variable::from("r"), Type::Exactly("rover".into()))
/// ])
/// )
/// )
/// ])
/// ));
/// ```
pub fn parse_functions_def<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, Functions> {
map(
prefix_expr(
":functions",
function_typed_list(parse_atomic_function_skeleton),
),
Functions::new,
)(input.into())
}
impl crate::parsers::Parser for Functions {
type Item = Functions;
/// See [`parse_functions_def`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_functions_def(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{
AtomicFunctionSkeleton, FunctionSymbol, FunctionTyped, Functions, Parser, Type, Typed,
TypedList, Variable,
};
#[test]
fn test_parse() {
let input = "(:functions (battery-amount ?r - rover))";
assert!(Functions::parse(input).is_value(Functions::from_iter([
FunctionTyped::new_number(AtomicFunctionSkeleton::new(
FunctionSymbol::from_str("battery-amount"),
TypedList::from_iter([Typed::new(
Variable::from("r"),
Type::Exactly("rover".into())
)])
))
])));
}
}