-
Notifications
You must be signed in to change notification settings - Fork 1
/
atomic_formula_skeleton.rs
83 lines (76 loc) · 2.69 KB
/
atomic_formula_skeleton.rs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Provides parsers for atomic formula skeletons.
use crate::parsers::{parens, typed_list, ws, ParseResult, Span};
use crate::parsers::{parse_predicate, parse_variable};
use crate::types::AtomicFormulaSkeleton;
use nom::combinator::map;
use nom::sequence::tuple;
/// Parses an atomic formula skeleton, i.e. `(<predicate> <typed list (variable)>)`.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_atomic_formula_skeleton, Span, UnwrapValue};
/// # use pddl::{Variable, AtomicFormulaSkeleton, Predicate};
/// # use pddl::{ToTyped, TypedList};
/// assert!(parse_atomic_formula_skeleton(Span::new("(at ?x - physob ?y - location)")).is_value(
/// AtomicFormulaSkeleton::new(
/// Predicate::from("at"),
/// TypedList::from_iter([
/// Variable::from("x").to_typed("physob"),
/// Variable::from("y").to_typed("location")
/// ]))
/// ));
/// ```
pub fn parse_atomic_formula_skeleton<'a, T: Into<Span<'a>>>(
input: T,
) -> ParseResult<'a, AtomicFormulaSkeleton> {
map(
parens(tuple((parse_predicate, ws(typed_list(parse_variable))))),
AtomicFormulaSkeleton::from,
)(input.into())
}
impl crate::parsers::Parser for AtomicFormulaSkeleton {
type Item = AtomicFormulaSkeleton;
/// Parses an atomic formula skeleton.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_atomic_formula_skeleton, Span, UnwrapValue};
/// # use pddl::{Variable, AtomicFormulaSkeleton, Predicate, Parser};
/// # use pddl::{ToTyped, TypedList};
/// let (_, value) = AtomicFormulaSkeleton::parse("(at ?x - physob ?y - location)").unwrap();
///
/// assert_eq!(value,
/// AtomicFormulaSkeleton::new(
/// Predicate::from("at"),
/// TypedList::from_iter([
/// Variable::from("x").to_typed("physob"),
/// Variable::from("y").to_typed("location")
/// ]))
/// );
/// ```
///
/// ## See also
/// See [`parse_atomic_formula_skeleton`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_atomic_formula_skeleton(input)
}
}
#[cfg(test)]
mod tests {
use crate::{AtomicFormulaSkeleton, Parser, Predicate, Variable};
use crate::{ToTyped, TypedList};
#[test]
fn test_parse() {
let (_, value) = AtomicFormulaSkeleton::parse("(at ?x - physob ?y - location)").unwrap();
assert_eq!(
value,
AtomicFormulaSkeleton::new(
Predicate::from("at"),
TypedList::from_iter([
Variable::from("x").to_typed("physob"),
Variable::from("y").to_typed("location")
])
)
);
}
}